Skip to contentSkip to conversation

qtest/npcm7xx_sdhci: Use card-provided address (RCA)

PATCH In review Philippe Mathieu-Daudé 2 participants 9 messages Jul 2, 2024 – Jul 5, 2024

lore
Share & tools
lore
#0 lore raw

cover qtest/npcm7xx_sdhci: Use card-provided address (RCA)

Avoid to use the SDCard hardcoded 0x4567 RCA in npcm7xx SDHCI qtest, so we can generate one in the card model.

Philippe Mathieu-Daudé (4):
hw/sd/npcm7xx_sdhci: Use TYPE_SYSBUS_SDHCI definition hw/sd/sdhci: Log non-sequencial access as GUEST_ERROR tests/qtest/npcm7xx_sdhci: Access the card using its published address hw/sd/sdcard: Generate random RCA value

 tests/qtest/libqos/sdhci-cmd.h   |  2 ++
 hw/sd/npcm7xx_sdhci.c            |  3 ++-
 hw/sd/sd.c                       | 11 ++++++++---
 hw/sd/sdhci.c                    |  5 +++--
 tests/qtest/npcm7xx_sdhci-test.c |  8 ++++++--
 hw/sd/trace-events               |  1 +
 6 files changed, 22 insertions(+), 8 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/4 hw/sd/npcm7xx_sdhci: Use TYPE_SYSBUS_SDHCI definition

Use the macro instead of two explicit string literals.

---
 hw/sd/npcm7xx_sdhci.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/npcm7xx_sdhci.c +2 -1
index e93dab8dbd..fb51821e11 100644
@@ -16,6 +16,7 @@
 
 #include "qemu/osdep.h"
 
+#include "hw/sd/sdhci.h"
 #include "hw/sd/npcm7xx_sdhci.h"
 #include "migration/vmstate.h"
 #include "sdhci-internal.h"
@@ -162,7 +163,7 @@ static void npcm7xx_sdhci_instance_init(Object *obj)
 {
     NPCM7xxSDHCIState *s = NPCM7XX_SDHCI(obj);
 
-    object_initialize_child(OBJECT(s), "generic-sdhci", &s->sdhci,
+    object_initialize_child(OBJECT(s), TYPE_SYSBUS_SDHCI, &s->sdhci,
                             TYPE_SYSBUS_SDHCI);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] hw/sd/npcm7xx_sdhci: Use TYPE_SYSBUS_SDHCI definition

Quoted 3 lines · Philippe Mathieu-Daudé

Use the macro instead of two explicit string literals.

Thanks,

C.

Quoted 25 lines
---
  hw/sd/npcm7xx_sdhci.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/npcm7xx_sdhci.c +2 -1
index e93dab8dbd..fb51821e11 100644
@@ -16,6 +16,7 @@
 
  #include "qemu/osdep.h"
 
+#include "hw/sd/sdhci.h"
  #include "hw/sd/npcm7xx_sdhci.h"
  #include "migration/vmstate.h"
  #include "sdhci-internal.h"
@@ -162,7 +163,7 @@ static void npcm7xx_sdhci_instance_init(Object *obj)
  {
      NPCM7xxSDHCIState *s = NPCM7XX_SDHCI(obj);
 
-    object_initialize_child(OBJECT(s), "generic-sdhci", &s->sdhci,
+    object_initialize_child(OBJECT(s), TYPE_SYSBUS_SDHCI, &s->sdhci,
                              TYPE_SYSBUS_SDHCI);
  }
 
#3 lore raw

PATCH 2/4 hw/sd/sdhci: Log non-sequencial access as GUEST_ERROR

In reply to Philippe Mathieu-Daudé
---
 hw/sd/sdhci.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
hw/sd/sdhci.c +3 -2
index 27673e1c70..d02c3e3963 100644
@@ -983,8 +983,9 @@ static inline bool
 sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
 {
     if ((s->data_count & 0x3) != byte_num) {
-        trace_sdhci_error("Non-sequential access to Buffer Data Port register"
-                          "is prohibited\n");
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "SDHCI: Non-sequential access to Buffer Data Port"
+                      " register is prohibited\n");
         return false;
     }
     return true;
Signature · 1 line
2.41.0
Reviewed-by on v1 2/4
#4 lore raw

Re: [PATCH 2/4] hw/sd/sdhci: Log non-sequencial access as GUEST_ERROR

Quoted 1 line · Philippe Mathieu-Daudé

Thanks,

C.

Quoted 20 lines
---
  hw/sd/sdhci.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/sd/sdhci.c +3 -2
index 27673e1c70..d02c3e3963 100644
@@ -983,8 +983,9 @@ static inline bool
  sdhci_buff_access_is_sequential(SDHCIState *s, unsigned byte_num)
  {
      if ((s->data_count & 0x3) != byte_num) {
-        trace_sdhci_error("Non-sequential access to Buffer Data Port register"
-                          "is prohibited\n");
+        qemu_log_mask(LOG_GUEST_ERROR,
+                      "SDHCI: Non-sequential access to Buffer Data Port"
+                      " register is prohibited\n");
          return false;
      }
      return true;
#5 lore raw

PATCH 3/4 tests/qtest/npcm7xx_sdhci: Access the card using its published address

In reply to Philippe Mathieu-Daudé

Currently setup_sd_card() asks the card its address, but discard the response and use hardcoded 0x4567.

Set the SDHC_CMD_RESPONSE bit to have the controller record the bus response, and read the response from the RSPREG0 register. Then we can select the card with its real address.

---
Cc: Hao Wu <[email protected]>
Cc: Chris Rauer <[email protected]>
Cc: Shengtan Mao <[email protected]>
Cc: Patrick Venture <[email protected]>
Cc: Tyrone Ting <[email protected]>

---
 tests/qtest/libqos/sdhci-cmd.h   | 2 ++
 tests/qtest/npcm7xx_sdhci-test.c | 8 ++++++--
 2 files changed, 8 insertions(+), 2 deletions(-)
tests/qtest/libqos/sdhci-cmd.h +2 -0
index 9e61dd4944..90efa028ef 100644
@@ -22,6 +22,7 @@
 #define SDHC_ARGUMENT 0x08
 #define SDHC_TRNMOD 0x0C
 #define SDHC_CMDREG 0x0E
+#define SDHC_RSPREG0 0x10
 #define SDHC_BDATA 0x20
 #define SDHC_PRNSTS 0x24
 #define SDHC_BLKGAP 0x2A
@@ -38,6 +39,7 @@
 #define SDHC_TRNS_MULTI 0x0020
 
 /* CMD Reg */
+#define SDHC_CMD_RESPONSE (3 << 0)
 #define SDHC_CMD_DATA_PRESENT (1 << 5)
 #define SDHC_ALL_SEND_CID (2 << 8)
 #define SDHC_SEND_RELATIVE_ADDR (3 << 8)
tests/qtest/npcm7xx_sdhci-test.c +6 -2
index 5d68540e52..01f237a816 100644
@@ -30,6 +30,8 @@ char *sd_path;
 
 static QTestState *setup_sd_card(void)
 {
+    uint16_t rca;
+
     QTestState *qts = qtest_initf(
         "-machine kudo-bmc "
         "-device sd-card,drive=drive0 "
@@ -43,8 +45,10 @@ static QTestState *setup_sd_card(void)
     sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_APP_CMD);
     sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x41200000, 0, (41 << 8));
     sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_ALL_SEND_CID);
-    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_SEND_RELATIVE_ADDR);
-    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x45670000, 0,
+    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_SEND_RELATIVE_ADDR
+                                                    | SDHC_CMD_RESPONSE);
+    rca = qtest_readl(qts, NPCM7XX_MMC_BA + SDHC_RSPREG0) >> 16;
+    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, rca << 16, 0,
                    SDHC_SELECT_DESELECT_CARD);
 
     return qts;
Signature · 1 line
2.41.0
Tested-by Reviewed-by on v1 3/4
#6 lore raw

Re: [PATCH 3/4] tests/qtest/npcm7xx_sdhci: Access the card using its published address

Quoted 15 lines · Philippe Mathieu-Daudé

Currently setup_sd_card() asks the card its address, but discard the response and use hardcoded 0x4567.

Set the SDHC_CMD_RESPONSE bit to have the controller record the bus response, and read the response from the RSPREG0 register. Then we can select the card with its real address.

---
Cc: Hao Wu <[email protected]>
Cc: Chris Rauer <[email protected]>
Cc: Shengtan Mao <[email protected]>
Cc: Patrick Venture <[email protected]>
Cc: Tyrone Ting <[email protected]>

Quoted 4 lines
---
  tests/qtest/libqos/sdhci-cmd.h   | 2 ++
  tests/qtest/npcm7xx_sdhci-test.c | 8 ++++++--
  2 files changed, 8 insertions(+), 2 deletions(-)

Thanks,

C.

Quoted 47 lines
tests/qtest/libqos/sdhci-cmd.h +2 -0
index 9e61dd4944..90efa028ef 100644
@@ -22,6 +22,7 @@
  #define SDHC_ARGUMENT 0x08
  #define SDHC_TRNMOD 0x0C
  #define SDHC_CMDREG 0x0E
+#define SDHC_RSPREG0 0x10
  #define SDHC_BDATA 0x20
  #define SDHC_PRNSTS 0x24
  #define SDHC_BLKGAP 0x2A
@@ -38,6 +39,7 @@
  #define SDHC_TRNS_MULTI 0x0020
 
  /* CMD Reg */
+#define SDHC_CMD_RESPONSE (3 << 0)
  #define SDHC_CMD_DATA_PRESENT (1 << 5)
  #define SDHC_ALL_SEND_CID (2 << 8)
  #define SDHC_SEND_RELATIVE_ADDR (3 << 8)
tests/qtest/npcm7xx_sdhci-test.c +6 -2
index 5d68540e52..01f237a816 100644
@@ -30,6 +30,8 @@ char *sd_path;
 
  static QTestState *setup_sd_card(void)
  {
+    uint16_t rca;
+
      QTestState *qts = qtest_initf(
          "-machine kudo-bmc "
          "-device sd-card,drive=drive0 "
@@ -43,8 +45,10 @@ static QTestState *setup_sd_card(void)
      sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_APP_CMD);
      sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x41200000, 0, (41 << 8));
      sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_ALL_SEND_CID);
-    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_SEND_RELATIVE_ADDR);
-    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0x45670000, 0,
+    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, 0, 0, SDHC_SEND_RELATIVE_ADDR
+                                                    | SDHC_CMD_RESPONSE);
+    rca = qtest_readl(qts, NPCM7XX_MMC_BA + SDHC_RSPREG0) >> 16;
+    sdhci_cmd_regs(qts, NPCM7XX_MMC_BA, 0, 0, rca << 16, 0,
                     SDHC_SELECT_DESELECT_CARD);
 
      return qts;
#7 lore raw

PATCH 4/4 hw/sd/sdcard: Generate random RCA value

In reply to Philippe Mathieu-Daudé

Rather than using the obscure 0x4567 magic value, use a real random one.

---
 hw/sd/sd.c         | 11 ++++++++---
 hw/sd/trace-events |  1 +
 2 files changed, 9 insertions(+), 3 deletions(-)
hw/sd/sd.c +8 -3
index a48010cfc1..9443a9439d 100644
@@ -46,6 +46,7 @@
 #include "qemu/error-report.h"
 #include "qemu/timer.h"
 #include "qemu/log.h"
+#include "qemu/guest-random.h"
 #include "qemu/module.h"
 #include "sdmmc-internal.h"
 #include "trace.h"
@@ -490,9 +491,10 @@ static void sd_set_csd(SDState *sd, uint64_t size)
 
 /* Relative Card Address register */
 
-static void sd_set_rca(SDState *sd)
+static void sd_set_rca(SDState *sd, uint16_t value)
 {
-    sd->rca += 0x4567;
+    trace_sdcard_set_rca(value);
+    sd->rca = value;
 }
 
 static uint16_t sd_req_get_rca(SDState *s, SDRequest req)
@@ -1103,11 +1105,14 @@ static sd_rsp_type_t sd_cmd_ALL_SEND_CID(SDState *sd, SDRequest req)
 /* CMD3 */
 static sd_rsp_type_t sd_cmd_SEND_RELATIVE_ADDR(SDState *sd, SDRequest req)
 {
+    uint16_t random_rca;
+
     switch (sd->state) {
     case sd_identification_state:
     case sd_standby_state:
         sd->state = sd_standby_state;
-        sd_set_rca(sd);
+        qemu_guest_getrandom_nofail(&random_rca, sizeof(random_rca));
+        sd_set_rca(sd, random_rca);
         return sd_r6;
 
     default:
hw/sd/trace-events +1 -0
index 724365efc3..f1281aecd3 100644
@@ -43,6 +43,7 @@ sdcard_response(const char *rspdesc, int rsplen) "%s (sz:%d)"
 sdcard_powerup(void) ""
 sdcard_inquiry_cmd41(void) ""
 sdcard_reset(void) ""
+sdcard_set_rca(uint16_t value) "new RCA: 0x%04x"
 sdcard_set_blocklen(uint16_t length) "block len 0x%03x"
 sdcard_set_block_count(uint32_t cnt) "block cnt 0x%"PRIx32
 sdcard_inserted(bool readonly) "read_only: %u"
Signature · 1 line
2.41.0
#8 lore raw
In reply to Philippe Mathieu-Daudé
Quoted 5 lines · Philippe Mathieu-Daudé

Philippe Mathieu-Daudé (4):
hw/sd/npcm7xx_sdhci: Use TYPE_SYSBUS_SDHCI definition hw/sd/sdhci: Log non-sequencial access as GUEST_ERROR tests/qtest/npcm7xx_sdhci: Access the card using its published address hw/sd/sdcard: Generate random RCA value

Series queued, thanks.