Skip to contentSkip to conversation

SD & SDHCI fixes for-11.1

PATCH In review Bernhard Beschow 2 participants 5 messages Jul 20 – Jul 21

lore
Share & tools
lore
#3 lore raw

PATCH v2 2/2 hw/sd/sdhci: Extract uSDHC-specific quirk

In reply to Bernhard Beschow

In Linux, the ESDHC_MIX_CTRL quirk is guarded by esdhc_is_usdhc() while the eSDHC code path uses the standard SDHC interface. Extract the quirk into a new usdhc_write() function.

Fixes file system corruption on emulated i.MX53 where Linux' esdhc_is_usdhc() returns false. The same likely happens on e500 and imx25-pdk machines.

Fixes 75e98bc4f859 ("hw/sd/sdhci: Add TYPE_FSL_ESDHC_BE")
cc qemu-stable
---
 hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 44 insertions(+), 29 deletions(-)
hw/sd/sdhci.c +44 -29
index c86dfa281f..e58a610397 100644
@@ -1795,34 +1795,6 @@ esdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
         sdhci_write(opaque, offset, value, size);
         break;
 
-    case ESDHC_MIX_CTRL:
-        /*
-         * So, when SD/MMC stack in Linux tries to write to "Transfer
-         * Mode Register", ESDHC i.MX quirk code will translate it
-         * into a write to ESDHC_MIX_CTRL, so we do the opposite in
-         * order to get where we started
-         *
-         * Note that Auto CMD23 Enable bit is located in a wrong place
-         * on i.MX, but since it is not used by QEMU we do not care.
-         *
-         * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
-         * here because it will result in a call to
-         * sdhci_send_command(s) which we don't want.
-         *
-         */
-        s->trnmod = value & UINT16_MAX;
-        break;
-    case SDHC_TRNMOD:
-        /*
-         * Similar to above, but this time a write to "Command
-         * Register" will be translated into a 4-byte write to
-         * "Transfer Mode register" where lower 16-bit of value would
-         * be set to zero. So what we do is fill those bits with
-         * cached value from s->trnmod and let the SDHCI
-         * infrastructure handle the rest
-         */
-        sdhci_write(opaque, offset, val | s->trnmod, size);
-        break;
     case SDHC_BLKSIZE:
         /*
          * ESDHCI does not implement "Host SDMA Buffer Boundary", and
@@ -1891,9 +1863,52 @@ static void fsl_esdhc_le_init(Object *obj)
     qdev_prop_set_uint8(dev, "sd-spec-version", 2);
 }
 
+static void
+usdhc_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
+{
+    SDHCIState *s = SYSBUS_SDHCI(opaque);
+    uint32_t value = (uint32_t)val;
+
+    switch (offset) {
+    case ESDHC_MIX_CTRL:
+        /*
+         * So, when SD/MMC stack in Linux tries to write to "Transfer
+         * Mode Register", uSDHC i.MX quirk code will translate it
+         * into a write to ESDHC_MIX_CTRL, so we do the opposite in
+         * order to get where we started.
+         *
+         * Note that Auto CMD23 Enable bit is located in a wrong place
+         * on i.MX, but since it is not used by QEMU we do not care.
+         *
+         * We don't want to call sdhci_write(.., SDHC_TRNMOD, ...)
+         * here because it will result in a call to
+         * sdhci_send_command(s) which we don't want.
+         *
+         */
+        s->trnmod = value & UINT16_MAX;
+        break;
+
+    case SDHC_TRNMOD:
+        /*
+         * Similar to above, but this time a write to "Command
+         * Register" will be translated into a 4-byte write to
+         * "Transfer Mode register" where lower 16-bit of value would
+         * be set to zero. So what we do is fill those bits with
+         * cached value from s->trnmod and let the SDHCI
+         * infrastructure handle the rest
+         */
+        sdhci_write(opaque, offset, val | s->trnmod, size);
+        break;
+
+    default:
+        esdhc_write(opaque, offset, val, size);
+        break;
+    }
+}
+
 static const MemoryRegionOps usdhc_mmio_ops = {
     .read = esdhc_read,
-    .write = esdhc_write,
+    .write = usdhc_write,
     .valid = {
         .min_access_size = 1,
         .max_access_size = 4,
Signature · 1 line
2.55.0
#1 lore raw

PATCH v2 1/2 hw/sd/sd: Fix error case for CMD18

In reply to Bernhard Beschow

In commit 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len"), sd_read_byte() changed its contract to return the read size rather than the read value (and was renamed to sd_read_data() accordingly). In an error case, however, sd_read_data() returns 0 by means of dummy_byte which is the code for the old contract. Moreover, sdbus_read_data() asserts the virtual method read_data() (and thus sd_read_data()) to return a non-zero size, i.e. to make progress and not loop forever. Fix the code to behave like the "DAT read illegal for command" case.

Fixes 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len")

---
cc: Christian Speich <[email protected]>

---
 hw/sd/sd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/sd.c +2 -1
index 3360757004..a30c541df0 100644
@@ -2860,7 +2860,8 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)
         if (sd->data_offset == 0) {
             if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
                                   sd->data_start, io_len)) {
-                return dummy_byte;
+                *value = dummy_byte;
+                return length;
             }
             partition_access = sd->ext_csd[EXT_CSD_PART_CONFIG]
                     & EXT_CSD_PART_CONFIG_ACC_MASK;
Signature · 1 line
2.55.0
#0 lore raw

v2 cover SD & SDHCI fixes for-11.1

This series fixes two issues in the SD subsystem. The first one could send QEMU into an endless loop or crash it (depending on whether assert() does something). The second patch fixes possible file system corruption in e500 and imx25-pdk machines, i.e. the ones using eSDHC rather than uSDHC, though I could only verify that with imx53 emulation on my imx53 branch [1].

v2:
* Add missing R-b tag in first commit (Bin)
* Fix typo in commit message of second commit (Bin)
* Fix topic (fixes are intended for 11.1)

[1] https://github.com/shentok/qemu/tree/imx53

Supersedes: [email protected]

Bernhard Beschow (2):
hw/sd/sd: Fix error case for CMD18
hw/sd/sdhci: Extract uSDHC-specific quirk

 hw/sd/sd.c    |  3 ++-
 hw/sd/sdhci.c | 73 +++++++++++++++++++++++++++++++--------------------
 2 files changed, 46 insertions(+), 30 deletions(-)
Signature · 1 line
2.55.0
Reviewed-by on v2 1/2
#2 lore raw

Re: [PATCH v2 1/2] hw/sd/sd: Fix error case for CMD18

In reply to Bernhard Beschow
Quoted 24 lines · Bernhard Beschow

In commit 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len"), sd_read_byte() changed its contract to return the read size rather than the read value (and was renamed to sd_read_data() accordingly). In an error case, however, sd_read_data() returns 0 by means of dummy_byte which is the code for the old contract. Moreover, sdbus_read_data() asserts the virtual method read_data() (and thus sd_read_data()) to return a non-zero size, i.e. to make progress and not loop forever. Fix the code to behave like the "DAT read illegal for command" case.

Fixes 468fa450a7e0 ("hw/sd: Switch read/write primitive to buf+len")

---
cc: Christian Speich <[email protected]>

---
  hw/sd/sd.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/sd.c +0 -0
index 3360757004..a30c541df0 100644

hw/sd/sd.c static size_t sd_read_data(SDState *sd, void *buf, size_t length) · line 2864

Quoted 6 lines
hw/sd/sd.c +2 -1
@@ -2860,7 +2860,8 @@ static size_t sd_read_data(SDState *sd, void *buf, size_t length)
          if (sd->data_offset == 0) {
              if (!address_in_range(sd, "READ_MULTIPLE_BLOCK",
                                    sd->data_start, io_len)) {
-                return dummy_byte;
+                *value = dummy_byte;
+                return length;

Oops, sorry for missing that.

Quoted 1 line
hw/sd/sd.c +0 -0
              }
#4 lore raw
In reply to Bernhard Beschow
Quoted 11 lines · Bernhard Beschow

This series fixes two issues in the SD subsystem. The first one could send QEMU into an endless loop or crash it (depending on whether assert() does something). The second patch fixes possible file system corruption in e500 and imx25-pdk machines, i.e. the ones using eSDHC rather than uSDHC, though I could only verify that with imx53 emulation on my imx53 branch [1].

v2:
* Add missing R-b tag in first commit (Bin)
* Fix typo in commit message of second commit (Bin)
* Fix topic (fixes are intended for 11.1)

Quoted 6 lines

[1] https://github.com/shentok/qemu/tree/imx53

Supersedes: [email protected]

Bernhard Beschow (2):
hw/sd/sd: Fix error case for CMD18

Patch #1 queued, thanks.