Skip to contentSkip to conversation

mmc: rtsx: fix incorrect last byte in R2 response

PATCH In review [email protected] 3 participants 7 messages Aug 11, 2014 – Aug 14, 2014 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mmc: rtsx: fix incorrect last byte in R2 response

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

---
 drivers/mmc/host/rtsx_pci_sdmmc.c |    1 +
 drivers/mmc/host/rtsx_usb_sdmmc.c |    1 +
 2 files changed, 2 insertions(+)
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
index dfde4a2..54849d8 100644
@@ -412,6 +412,7 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
 	}
 
 	if (rsp_type == SD_RSP_TYPE_R2) {
+		ptr[16] = 1;
 		for (i = 0; i < 4; i++) {
 			cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
 			dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
drivers/mmc/host/rtsx_usb_sdmmc.c +1 -0
index 5d3766e..ca08df1 100644
@@ -435,6 +435,7 @@ static void sd_send_cmd_get_rsp(struct rtsx_usb_sdmmc *host,
 	}
 
 	if (rsp_type == SD_RSP_TYPE_R2) {
+		ptr[16] = 1;
 		for (i = 0; i < 4; i++) {
 			cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
 			dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
Signature · 1 line
1.7.10.4
#1 lore raw
Quoted 21 lines · On Mon, Aug 11, 2014 at 04:32:16PM +0800, [email protected] wrote:

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

---
 drivers/mmc/host/rtsx_pci_sdmmc.c |    1 +
 drivers/mmc/host/rtsx_usb_sdmmc.c |    1 +
 2 files changed, 2 insertions(+)
drivers/mmc/host/rtsx_pci_sdmmc.c +0 -0
index dfde4a2..54849d8 100644

drivers/mmc/host/rtsx_pci_sdmmc.c static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host, · line 415

Quoted 4 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
@@ -412,6 +412,7 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
 	}
 
 	if (rsp_type == SD_RSP_TYPE_R2) {
+		ptr[16] = 1;

Avoid magic numbers like 16 and 0x1.

This is subtle enough that it deserves a comment.

		ptr[stat_idx] = 0x1 /* 0x1 chosen randomly */

drivers/mmc/host/rtsx_pci_sdmmc.c static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host, · line 418

Quoted 3 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +0 -0
 		for (i = 0; i < 4; i++) {
 			cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
 			dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",

There are a lot of magic numbers in this function. We could get rid of this i < 4 loop but doing:

		memcpy(cmd->resp, ptr + 1, resp_len);

Currently we don't use resp_len and the resp_len = 5 assignment is off by one... It should be resp_len = 4. This function is quite ugly.

regards,
dan carpenter

author
#2 lore raw
Quoted 24 lines · Dan Carpenter
Quoted 23 lines · On Mon, Aug 11, 2014 at 04:32:16PM +0800, [email protected] wrote:

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

---
  drivers/mmc/host/rtsx_pci_sdmmc.c |    1 +
  drivers/mmc/host/rtsx_usb_sdmmc.c |    1 +
  2 files changed, 2 insertions(+)
drivers/mmc/host/rtsx_pci_sdmmc.c +0 -0
index dfde4a2..54849d8 100644
@@ -412,6 +412,7 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
  	}
 
Quoted 9 lines

drivers/mmc/host/rtsx_pci_sdmmc.c static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host, · line 415

Quoted 2 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
  	if (rsp_type == SD_RSP_TYPE_R2) {
+		ptr[16] = 1;

Avoid magic numbers like 16 and 0x1.

This is subtle enough that it deserves a comment.

		ptr[stat_idx] = 0x1 /* 0x1 chosen randomly */

The 0x1 consists of 7-bit dummy zero CRC and stop bit 1, described in SD card. Anyway, I'll give a comment to this in the next version.

Quoted 11 lines

drivers/mmc/host/rtsx_pci_sdmmc.c static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host, · line 418

Quoted 3 lines
drivers/mmc/host/rtsx_pci_sdmmc.c +0 -0
  		for (i = 0; i < 4; i++) {
  			cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
  			dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",

There are a lot of magic numbers in this function. We could get rid of this i < 4 loop but doing:

		memcpy(cmd->resp, ptr + 1, resp_len);

Currently we don't use resp_len and the resp_len = 5 assignment is off by one... It should be resp_len = 4. This function is quite ugly.

I can remove the unused rsp_len in this function. But I'm afraid the loop is still required. The destination cmd->resp is cpu-endian, but the raw response from SD card in the buffer (pointed by ptr) is big-endian.

Quoted 7 lines

regards,
dan carpenter

------Please consider the environment before printing this e-mail. .

Best regards,
Roger Tseng

#3 lore raw
Quoted 4 lines · Roger

I can remove the unused rsp_len in this function. But I'm afraid the loop is still required. The destination cmd->resp is cpu-endian, but the raw response from SD card in the buffer (pointed by ptr) is big-endian.

Oh, yes. Of course. Sorry for the noise.

regards,
dan carpenter

#4 lore raw
In reply to [email protected]
Quoted 11 lines · On 11 August 2014 10:32, <[email protected]> wrote:

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

Thanks! Queued for 3.18.

I guess this should go for stable as well?

Kind regards
Uffe

Quoted 32 lines
---
 drivers/mmc/host/rtsx_pci_sdmmc.c |    1 +
 drivers/mmc/host/rtsx_usb_sdmmc.c |    1 +
 2 files changed, 2 insertions(+)
drivers/mmc/host/rtsx_pci_sdmmc.c +1 -0
index dfde4a2..54849d8 100644
@@ -412,6 +412,7 @@ static void sd_send_cmd_get_rsp(struct realtek_pci_sdmmc *host,
        }
 
        if (rsp_type == SD_RSP_TYPE_R2) {
+               ptr[16] = 1;
                for (i = 0; i < 4; i++) {
                        cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
                        dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
drivers/mmc/host/rtsx_usb_sdmmc.c +1 -1
index 5d3766e..ca08df1 100644
@@ -435,6 +435,7 @@ static void sd_send_cmd_get_rsp(struct rtsx_usb_sdmmc *host,
        }
 
        if (rsp_type == SD_RSP_TYPE_R2) {
+               ptr[16] = 1;
                for (i = 0; i < 4; i++) {
                        cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4);
                        dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",
--

1.7.10.4

#5 lore raw
Quoted 9 lines · Ulf Hansson
Quoted 8 lines · On 11 August 2014 10:32, <[email protected]> wrote:

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Quoted 7 lines
Quoted 3 lines

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

Thanks! Queued for 3.18.

I guess this should go for stable as well?

Yes. However, since rtsx_usb* is present in 3.16 and later, this patch will not apply on 3.15.y or older. Should I separately send an adapted version to stable?

By the way, according to Dan's comment I would like to add a few word to explain the code. Would you help fix it up by following diff?

(quoted diff) +0 -0

b/drivers/mmc/host/rtsx_pci_sdmmc.c
index 54849d8..ca31279 100644

drivers/mmc/host/rtsx_pci_sdmmc.c +0 -0
@@ -412,7 +412,13 @@ static void sd_send_cmd_get_rsp(struct

realtek_pci_sdmmc *host,
}

if (rsp_type == SD_RSP_TYPE_R2) {
+ /*
+ * The controller offloads the last byte {CRC-7, stop bit 1'b1}
+ * of response type R2. Assign a dummy CRC, 0, and stop bit to
+ * the byte(ptr[16], goes into the LSB of resp[3] later).
+ */
ptr[16] = 1;
+
for (i = 0; i < 4; i++) {
cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4); dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",

(quoted diff) +0 -0

b/drivers/mmc/host/rtsx_usb_sdmmc.c
index ca08df1..727a88d 100644

drivers/mmc/host/rtsx_usb_sdmmc.c +0 -0
@@ -435,7 +435,13 @@ static void sd_send_cmd_get_rsp(struct

rtsx_usb_sdmmc *host,
}

if (rsp_type == SD_RSP_TYPE_R2) {
+ /*
+ * The controller offloads the last byte {CRC-7, stop bit 1'b1}
+ * of response type R2. Assign a dummy CRC, 0, and stop bit to
+ * the byte(ptr[16], goes into the LSB of resp[3] later).
+ */
ptr[16] = 1;
+
for (i = 0; i < 4; i++) {
cmd->resp[i] = get_unaligned_be32(ptr + 1 + i * 4); dev_dbg(sdmmc_dev(host), "cmd->resp[%d] = 0x%08x\n",

Signature · 2 lines
Best regards,
Roger Tseng
#6 lore raw
Quoted 12 lines · Roger Tseng
Quoted 11 lines · Ulf Hansson
Quoted 10 lines · On 11 August 2014 10:32, <[email protected]> wrote:

From: Roger Tseng <[email protected]>

Current code erroneously fill the last byte of R2 response with an undefined value. In addition, it is impossible to obtain the real values since the controller actually 'offloads' the last byte(CRC7, end bit) while receiving R2 response. This could cause mmc stack to obtain inconsistent CID from the same card after resume and misidentify it as a different card.

Fix by assigning a dummy value 0x01 to the last byte of R2 response.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

Thanks! Queued for 3.18.

I guess this should go for stable as well?

Yes. However, since rtsx_usb* is present in 3.16 and later, this patch will not apply on 3.15.y or older. Should I separately send an adapted version to stable?

I haven't pushed this externally yet. I will drop the patch from my 3.18 branch.

Then, let's split the patch into two, one for usb and one for pci - that should simplify patch management.

Additionally, you should include a Cc tag with proper hashmark telling which kernel each patch should be included into.

Kind regards
Uffe