Skip to contentSkip to conversation

sd: sdhci: correct transfer mode register usage

PATCH Superseded P J P 2 participants 9 messages Feb 11, 2017 – Feb 14, 2017

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

P J P

author
#0 lore raw

v3 cover sd: sdhci: correct transfer mode register usage

From: Prasad J Pandit <[email protected]>

Hello,

In SDHCI protocol, the 'Block Count Enable' bit of the Transfer Mode register is used to control 's->blkcnt' value. This bit is not relevant in single block transfers. Also, Transfer Mode register value could be set such that 's->blkcnt' would not see an update during multi block transfers. Thus leading to an infinite loop.

This patch set attempts to correct 'Block Count Enable' bit usage.

This series incorporates changes suggested in patch set v2:
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg01608.html

Thank you.
--
Prasad J Pandit (4):
sd: sdhci: check transfer mode register in multi block transfer
sd: sdhci: mask transfer mode register value
sd: sdhci: conditionally invoke multi block transfer
sd: sdhci: Remove block count enable check in single block transfers

 hw/sd/sdhci.c | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)
Signature · 1 line
2.9.3
author
#1 lore raw

PATCH v3 1/4 sd: sdhci: check transfer mode register in multi block transfer

From: Prasad J Pandit <[email protected]>

In the SDHCI protocol, the transfer mode register value is used during multi block transfer to check if block count register is enabled and should be updated. Transfer mode register could be set such that, block count register would not be updated, thus leading to an infinite loop. Add check to avoid it.

---
 hw/sd/sdhci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Update: use qemu_log_mask(LOG_UNIMP, ...)
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02354.html

hw/sd/sdhci.c +5 -5
index 5bd5ab6..a9c744b 100644
@@ -486,6 +486,11 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
     uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
 
+    if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
+        qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
+        return;
+    }
+
     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
      * possible stop at page boundary if initial address is not page aligned,
      * allow them to work properly */
@@ -797,11 +802,6 @@ static void sdhci_data_transfer(void *opaque)
     if (s->trnmod & SDHC_TRNS_DMA) {
         switch (SDHC_DMA_TYPE(s->hostctl)) {
         case SDHC_CTRL_SDMA:
-            if ((s->trnmod & SDHC_TRNS_MULTI) &&
-                    (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || s->blkcnt == 0)) {
-                break;
-            }
-
             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
                 sdhci_sdma_transfer_single_block(s);
             } else {
Signature · 1 line
2.9.3
Reviewed-by on v3 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH v3 1/4] sd: sdhci: check transfer mode register in multi block transfer

Quoted 9 lines · P J P

From: Prasad J Pandit <[email protected]>

In the SDHCI protocol, the transfer mode register value is used during multi block transfer to check if block count register is enabled and should be updated. Transfer mode register could be set such that, block count register would not be updated, thus leading to an infinite loop. Add check to avoid it.

Quoted 3 lines

Thanks,

Alistair

Quoted 38 lines
---
 hw/sd/sdhci.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

Update: use qemu_log_mask(LOG_UNIMP, ...)
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02354.html

hw/sd/sdhci.c +5 -6
index 5bd5ab6..a9c744b 100644
@@ -486,6 +486,11 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
     uint32_t boundary_chk = 1 << (((s->blksize & 0xf000) >> 12) + 12);
     uint32_t boundary_count = boundary_chk - (s->sdmasysad % boundary_chk);
 
+    if (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || !s->blkcnt) {
+        qemu_log_mask(LOG_UNIMP, "infinite transfer is not supported\n");
+        return;
+    }
+
     /* XXX: Some sd/mmc drivers (for example, u-boot-slp) do not account for
      * possible stop at page boundary if initial address is not page aligned,
      * allow them to work properly */
@@ -797,11 +802,6 @@ static void sdhci_data_transfer(void *opaque)
     if (s->trnmod & SDHC_TRNS_DMA) {
         switch (SDHC_DMA_TYPE(s->hostctl)) {
         case SDHC_CTRL_SDMA:
-            if ((s->trnmod & SDHC_TRNS_MULTI) &&
-                    (!(s->trnmod & SDHC_TRNS_BLK_CNT_EN) || s->blkcnt == 0)) {
-                break;
-            }
-
             if ((s->blkcnt == 1) || !(s->trnmod & SDHC_TRNS_MULTI)) {
                 sdhci_sdma_transfer_single_block(s);
             } else {
--

2.9.3

author
#3 lore raw

PATCH v3 2/4 sd: sdhci: mask transfer mode register value

In reply to P J P

From: Prasad J Pandit <[email protected]>

In SDHCI protocol, the transfer mode register is defined to be of 6 bits. Mask its value with '0x0037' so that an invalid value couldn't be assigned.

---
 hw/sd/sdhci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Update: mask s->trnmod register value
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02354.html

hw/sd/sdhci.c +1 -1
index a9c744b..0307b8c 100644
@@ -1050,7 +1050,7 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
         if (!(s->capareg & SDHC_CAN_DO_DMA)) {
             value &= ~SDHC_TRNS_DMA;
         }
-        MASKED_WRITE(s->trnmod, mask, value);
+        MASKED_WRITE(s->trnmod, mask, value & 0x0037);
         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
 
         /* Writing to the upper byte of CMDREG triggers SD command generation */
Signature · 1 line
2.9.3
Reviewed-by on v3 2/4
#4 lore raw

Re: [Qemu-devel] [PATCH v3 2/4] sd: sdhci: mask transfer mode register value

Quoted 19 lines · P J P

From: Prasad J Pandit <[email protected]>

In SDHCI protocol, the transfer mode register is defined to be of 6 bits. Mask its value with '0x0037' so that an invalid value couldn't be assigned.

---
 hw/sd/sdhci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Update: mask s->trnmod register value
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02354.html

hw/sd/sdhci.c +0 -0
index a9c744b..0307b8c 100644

hw/sd/sdhci.c sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size) · line 1053

Quoted 5 lines
hw/sd/sdhci.c +1 -1
@@ -1050,7 +1050,7 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
         if (!(s->capareg & SDHC_CAN_DO_DMA)) {
             value &= ~SDHC_TRNS_DMA;
         }
-        MASKED_WRITE(s->trnmod, mask, value);
+        MASKED_WRITE(s->trnmod, mask, value & 0x0037);

This looks good.

Can you use a macro for the value so then it is explained and easier to change in the future?

Once you have done that:

Thanks,

Alistair

Quoted 6 lines
hw/sd/sdhci.c +0 -1
         MASKED_WRITE(s->cmdreg, mask >> 16, value >> 16);
 
         /* Writing to the upper byte of CMDREG triggers SD command generation */
--

2.9.3

author
#5 lore raw

Re: [Qemu-devel] [PATCH v3 2/4] sd: sdhci: mask transfer mode register value

+-- On Mon, 13 Feb 2017, Alistair Francis wrote --+
| > -        MASKED_WRITE(s->trnmod, mask, value);
| > +        MASKED_WRITE(s->trnmod, mask, value & 0x0037);
| 
| This looks good.
| 
| Can you use a macro for the value so then it is explained and easier
| to change in the future?

Done, sent patch v4.

Thank you.
--
Prasad J Pandit / Red Hat Product Security Team
47AF CE69 3A90 54AA 9045 1053 DD13 3D32 FE5B 041F

author
#6 lore raw

PATCH v3 3/4 sd: sdhci: conditionally invoke multi block transfer

In reply to P J P

From: Prasad J Pandit <[email protected]>

In sdhci_write invoke multi block transfer if it is enabled in the transfer mode register 's->trnmod'.

---
 hw/sd/sdhci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Update: test if (s->trnmod & SDHC_TRNS_MULTI) is true
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02357.html

hw/sd/sdhci.c +5 -1
index 0307b8c..99fa0c7 100644
@@ -1022,7 +1022,11 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
         /* Writing to last byte of sdmasysad might trigger transfer */
         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
                 s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) {
-            sdhci_sdma_transfer_multi_blocks(s);
+            if (s->trnmod & SDHC_TRNS_MULTI) {
+                sdhci_sdma_transfer_multi_blocks(s);
+            } else {
+                sdhci_sdma_transfer_single_block(s);
+            }
         }
         break;
     case SDHC_BLKSIZE:
Signature · 1 line
2.9.3
Reviewed-by on v3 3/4
#7 lore raw

Re: [Qemu-devel] [PATCH v3 3/4] sd: sdhci: conditionally invoke multi block transfer

Quoted 6 lines · P J P

From: Prasad J Pandit <[email protected]>

In sdhci_write invoke multi block transfer if it is enabled in the transfer mode register 's->trnmod'.

Thanks,

Alistair

Quoted 27 lines
---
 hw/sd/sdhci.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

Update: test if (s->trnmod & SDHC_TRNS_MULTI) is true
-> https://lists.gnu.org/archive/html/qemu-devel/2017-02/msg02357.html

hw/sd/sdhci.c +5 -2
index 0307b8c..99fa0c7 100644
@@ -1022,7 +1022,11 @@ sdhci_write(void *opaque, hwaddr offset, uint64_t val, unsigned size)
         /* Writing to last byte of sdmasysad might trigger transfer */
         if (!(mask & 0xFF000000) && TRANSFERRING_DATA(s->prnsts) && s->blkcnt &&
                 s->blksize && SDHC_DMA_TYPE(s->hostctl) == SDHC_CTRL_SDMA) {
-            sdhci_sdma_transfer_multi_blocks(s);
+            if (s->trnmod & SDHC_TRNS_MULTI) {
+                sdhci_sdma_transfer_multi_blocks(s);
+            } else {
+                sdhci_sdma_transfer_single_block(s);
+            }
         }
         break;
     case SDHC_BLKSIZE:
--

2.9.3

author
#8 lore raw

PATCH v3 4/4 sd: sdhci: Remove block count enable check in single block transfers

In reply to P J P

From: Prasad J Pandit <[email protected]>

In SDHCI protocol, the 'Block count enable' bit of the Transfer Mode register is relevant only in multi block transfers. We need not check it in single block transfers.

---
 hw/sd/sdhci.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
hw/sd/sdhci.c +1 -5
index 99fa0c7..d612a80 100644
@@ -569,7 +569,6 @@ static void sdhci_sdma_transfer_multi_blocks(SDHCIState *s)
 }
 
 /* single block SDMA transfer */
-
 static void sdhci_sdma_transfer_single_block(SDHCIState *s)
 {
     int n;
@@ -588,10 +587,7 @@ static void sdhci_sdma_transfer_single_block(SDHCIState *s)
             sdbus_write_data(&s->sdbus, s->fifo_buffer[n]);
         }
     }
-
-    if (s->trnmod & SDHC_TRNS_BLK_CNT_EN) {
-        s->blkcnt--;
-    }
+    s->blkcnt--;
 
     sdhci_end_transfer(s);
 }
Signature · 1 line
2.9.3