hw/sd: sd: Erase operation and other fixes
v2 cover hw/sd: sd: Erase operation and other fixes
From: Bin Meng <[email protected]>
This includes several fixes related to erase operation of a SD card.
Currently sd_erase() does not perform the actual erase operation to reset the requested block contents to 0xFFs. When trying to add such functionality, one issue was spotted that the write protection is only supported by SDSC cards. This seems to be missed when adding high capability card support to the SD model.
The write groups check is now bypassed in the erase and block write handling codes for high capacity cards.
The last patch was previously sent as a standalone patch [1], and is now included in this v2 series. It was about SD card in SPI mode that CMD13 argument should be ignored.
[1] http://patchwork.ozlabs.org/project/qemu-devel/patch/[email protected]/
Based-on:
http://patchwork.ozlabs.org/project/qemu-devel/list/?series=226785
Changes in v2:
- new patch: sd: Only SDSC cards support CMD28/29/30
- new patch: sd: Fix CMD30 response type
- new patch: sd: Skip write protect groups check in sd_erase() for high capacity card
- honor the write protection bits for SDSC cards
- new patch: sd: Skip write protect groups check in CMD24/25 for high capacity cards
- update commit message to include the reference in the spec
Bin Meng (8):
hw/sd: sd: Fix address check in sd_erase()
hw/sd: sd: Only SDSC cards support CMD28/29/30
hw/sd: sd: Fix CMD30 response type
hw/sd: sd: Move the sd_block_{read,write} and macros ahead hw/sd: sd: Skip write protect groups check in sd_erase() for high
capacity cards
hw/sd: sd: Actually perform the erase operation
hw/sd: sd: Skip write protect groups check in CMD24/25 for high
capacity cards
hw/sd: sd: Bypass the RCA check for CMD13 in SPI mode
hw/sd/sd.c | 99 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 35 deletions(-)
Signature · 1 line
2.25.1
PATCH v2 1/8 hw/sd: sd: Fix address check in sd_erase()
From: Bin Meng <[email protected]>
For high capacity memory cards, the erase start address and end address are multiplied by 512, but the address check is still based on the original block number in sd->erase_{start, end}.
---
(no changes since v1)
hw/sd/sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sd.c +1 -1
@@ -760,7 +760,7 @@ static void sd_erase(SDState *sd) erase_end *=512 ; } -if (sd->erase_start > sd->size || sd->erase_end > sd->size) { +if (erase_start > sd->size || erase_end > sd->size) { sd->card_status |=OUT_OF_RANGE ; sd->erase_start =INVALID_ADDRESS ; sd->erase_end =INVALID_ADDRESS ;
Signature · 1 line
2.25.1
PATCH v2 2/8 hw/sd: sd: Only SDSC cards support CMD28/29/30
In reply to Bin MengFrom: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00", table 4-26 (SD mode) and table 7-3 (SPI mode) command descriptions, the following commands:
- CMD28 (SET_WRITE_PROT)
- CMD29 (CLR_WRITE_PROT)
- CMD30 (SEND_WRITE_PROT)
are only supported by SDSC cards.
---
Changes in v2:
- new patch: sd: Only SDSC cards support CMD28/29/30
hw/sd/sd.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
hw/sd/sd.c +12 -0
@@ -1284,6 +1284,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)/* Write protection (Class 6) */ case 28 :/* CMD28: SET_WRITE_PROT */ +if (sd->size >SDSC_MAX_CAPACITY ) { +return sd_illegal; + } +switch (sd->state) {case sd_transfer_state:if (addr >= sd->size) {
@@ -1303,6 +1307,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)break ;case 29 :/* CMD29: CLR_WRITE_PROT */ +if (sd->size >SDSC_MAX_CAPACITY ) { +return sd_illegal; + } +switch (sd->state) {case sd_transfer_state:if (addr >= sd->size) {
@@ -1322,6 +1330,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)break ;case 30 :/* CMD30: SEND_WRITE_PROT */ +if (sd->size >SDSC_MAX_CAPACITY ) { +return sd_illegal; + } +switch (sd->state) {case sd_transfer_state: sd->state = sd_sendingdata_state;
Signature · 1 line
2.25.1
Re: [PATCH v2 2/8] hw/sd: sd: Only SDSC cards support CMD28/29/30
Quoted 14 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00", table 4-26 (SD mode) and table 7-3 (SPI mode) command descriptions, the following commands:
- CMD28 (
SET_WRITE_PROT)
- CMD29 (CLR_WRITE_PROT)
- CMD30 (SEND_WRITE_PROT)are only supported by SDSC cards.
Signed-off-by Bin Meng <[email protected]>
Quoted 7 lines
---
Changes in v2:
- new patch: sd: Only SDSC cards support CMD28/29/30hw/sd/sd.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
PATCH v2 3/8 hw/sd: sd: Fix CMD30 response type
In reply to Bin MengFrom: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00", table 4-26 (SD mode) and table 7-3 (SPI mode) command descriptions, CMD30 response type is R1, not R1b.
---
Changes in v2:
- new patch: sd: Fix CMD30 response type
hw/sd/sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/sd/sd.c +1 -1
@@ -1340,7 +1340,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) *(uint32_t *) sd->data =sd_wpbits (sd, req.arg); sd->data_start = addr; sd->data_offset =0 ; -return sd_r1b; +return sd_r1;default :break ;
Signature · 1 line
2.25.1
Re: [PATCH v2 3/8] hw/sd: sd: Fix CMD30 response type
Quoted 9 lines · Bin Meng
From: Bin Meng <[email protected]>
Per the "Physical Layer Specification Version 8.00", table 4-26 (SD mode) and table 7-3 (SPI mode) command descriptions, CMD30 response type is R1, not R1b.
Fixes a1bb27b1e98a ("SD card emulation initial implementation")Signed-off-by Bin Meng <[email protected]>
Quoted 7 lines
---
Changes in v2:
- new patch: sd: Fix CMD30 response typehw/sd/sd.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
PATCH v2 4/8 hw/sd: sd: Move the sd_block_{read, write} and macros ahead
In reply to Bin MengFrom: Bin Meng <[email protected]>
These APIs and macros may be referenced by functions that are currently before them. Move them ahead a little bit.
---
(no changes since v1)
hw/sd/sd.c | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-)
hw/sd/sd.c +21 -21
@@ -739,6 +739,27 @@ void sd_set_cb(SDState *sd, qemu_irq readonly, qemu_irq insert)qemu_set_irq (insert, sd->blk ?blk_is_inserted (sd->blk) :0 ); } +static void sd_blk_read (SDState *sd,uint64_t addr,uint32_t len) +{ +trace_sdcard_read_block (addr, len); +if (!sd->blk ||blk_pread (sd->blk, addr, sd->data, len) <0 ) { +fprintf (stderr,"sd_blk_read: read error on host side\n" ); + } +} + +static void sd_blk_write (SDState *sd,uint64_t addr,uint32_t len) +{ +trace_sdcard_write_block (addr, len); +if (!sd->blk ||blk_pwrite (sd->blk, addr, sd->data, len,0 ) <0 ) { +fprintf (stderr,"sd_blk_write: write error on host side\n" ); + } +} + +#define BLK_READ_BLOCK (a, len) sd_blk_read(sd, a, len) +#define BLK_WRITE_BLOCK (a, len) sd_blk_write(sd, a, len) +#define APP_READ_BLOCK (a, len) memset(sd->data, 0xec, len) +#define APP_WRITE_BLOCK (a, len) +static void sd_erase (SDState *sd) {int i;
@@ -1754,27 +1775,6 @@ send_response:return rsplen; } -static void sd_blk_read (SDState *sd,uint64_t addr,uint32_t len) -{ -trace_sdcard_read_block (addr, len); -if (!sd->blk ||blk_pread (sd->blk, addr, sd->data, len) <0 ) { -fprintf (stderr,"sd_blk_read: read error on host side\n" ); - } -} - -static void sd_blk_write (SDState *sd,uint64_t addr,uint32_t len) -{ -trace_sdcard_write_block (addr, len); -if (!sd->blk ||blk_pwrite (sd->blk, addr, sd->data, len,0 ) <0 ) { -fprintf (stderr,"sd_blk_write: write error on host side\n" ); - } -} - -#define BLK_READ_BLOCK (a, len) sd_blk_read(sd, a, len) -#define BLK_WRITE_BLOCK (a, len) sd_blk_write(sd, a, len) -#define APP_READ_BLOCK (a, len) memset(sd->data, 0xec, len) -#define APP_WRITE_BLOCK (a, len) -void sd_write_byte (SDState *sd,uint8_t value) {int i;
Signature · 1 line
2.25.1
PATCH v2 5/8 hw/sd: sd: Skip write protect groups check in sd_erase() for high capacity cards
In reply to Bin MengFrom: Bin Meng <[email protected]>
High capacity cards don't support write protection hence we should not preform the write protect groups check in sd_erase() for them.
---
Changes in v2:
- new patch: sd: Skip write protect groups check in sd_erase() for high capacity card
hw/sd/sd.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
hw/sd/sd.c +12 -6
@@ -765,6 +765,7 @@ static void sd_erase(SDState *sd)int i;uint64_t erase_start = sd->erase_start;uint64_t erase_end = sd->erase_end; +bool sdsc = true;trace_sdcard_erase (sd->erase_start, sd->erase_end); if (sd->erase_start == INVALID_ADDRESS
@@ -779,6 +780,7 @@ static void sd_erase(SDState *sd)/* High capacity memory card: erase units are 512 byte blocks */ erase_start *=512 ; erase_end *=512 ; + sdsc = false; }if (erase_start > sd->size || erase_end > sd->size) {
@@ -788,16 +790,20 @@ static void sd_erase(SDState *sd)return ; } - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); sd->erase_start =INVALID_ADDRESS ; sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -for (i = erase_start; i <= erase_end; i++) { -assert (i < sd->wpgrps_size); -if (test_bit (i, sd->wp_groups)) { - sd->card_status |=WP_ERASE_SKIP ; +/* Only SDSC cards support write protect groups */ +if (sdsc) { + erase_start =sd_addr_to_wpnum (erase_start); + erase_end =sd_addr_to_wpnum (erase_end); + +for (i = erase_start; i <= erase_end; i++) { +assert (i < sd->wpgrps_size); +if (test_bit (i, sd->wp_groups)) { + sd->card_status |=WP_ERASE_SKIP ; + } } } }
Signature · 1 line
2.25.1
Re: [PATCH v2 5/8] hw/sd: sd: Skip write protect groups check in sd_erase() for high capacity cards
Quoted 7 lines · Bin Meng
From: Bin Meng <[email protected]>
High capacity cards don't support write protection hence we should not preform the write protect groups check in
sd_erase()for them.Signed-off-by Bin Meng <[email protected]>
Quoted 7 lines
---
Changes in v2:
- new patch: sd: Skip write protect groups check insd_erase()for high capacity cardhw/sd/sd.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
PATCH v2 6/8 hw/sd: sd: Actually perform the erase operation
In reply to Bin MengFrom: Bin Meng <[email protected]>
At present the sd_erase() does not erase the requested range of card data to 0xFFs. Let's make the erase operation actually happen.
---
Changes in v2:
- honor the write protection bits for SDSC cards
hw/sd/sd.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
hw/sd/sd.c +14 -8
@@ -766,6 +766,9 @@ static void sd_erase(SDState *sd)uint64_t erase_start = sd->erase_start;uint64_t erase_end = sd->erase_end;bool sdsc = true; +uint64_t wpnum; +uint64_t erase_addr; +int erase_len =1 <<HWBLOCK_SHIFT ;trace_sdcard_erase (sd->erase_start, sd->erase_end); if (sd->erase_start == INVALID_ADDRESS
@@ -794,17 +797,20 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -/* Only SDSC cards support write protect groups */ -if (sdsc) { - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); - -for (i = erase_start; i <= erase_end; i++) { -assert (i < sd->wpgrps_size); -if (test_bit (i, sd->wp_groups)) { +memset (sd->data,0xff , erase_len); + erase_addr = erase_start; +for (i =0 ; i <= (erase_end - erase_start) / erase_len; i++) { +if (sdsc) { +/* Only SDSC cards support write protect groups */ + wpnum =sd_addr_to_wpnum (erase_addr); +assert (wpnum < sd->wpgrps_size); +if (test_bit (wpnum, sd->wp_groups)) { sd->card_status |=WP_ERASE_SKIP ; +continue ; } } +BLK_WRITE_BLOCK (erase_addr, erase_len); + erase_addr += erase_len; } }
Signature · 1 line
2.25.1
Re: [PATCH v2 6/8] hw/sd: sd: Actually perform the erase operation
Quoted 41 lines · Bin Meng
From: Bin Meng <[email protected]>
At present the
sd_erase()does not erase the requested range of card data to 0xFFs. Let's make the erase operation actually happen.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- honor the write protection bits for SDSC cardshw/sd/sd.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)hw/sd/sd.c +3
-8@@ -766,6 +766,9 @@ static void sd_erase(SDState *sd)uint64_t erase_start = sd->erase_start;uint64_t erase_end = sd->erase_end;bool sdsc = true; +uint64_t wpnum; +uint64_t erase_addr; +int erase_len =1 <<HWBLOCK_SHIFT ;trace_sdcard_erase (sd->erase_start, sd->erase_end); if (sd->erase_start == INVALID_ADDRESS@@ -794,17 +797,20 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -/* Only SDSC cards support write protect groups */ -if (sdsc) { - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); - -for (i = erase_start; i <= erase_end; i++) { -assert (i < sd->wpgrps_size); -if (test_bit (i, sd->wp_groups)) {
hw/sd/sd.c static void sd_erase(SDState *sd) · line 809
Quoted 10 lines
hw/sd/sd.c +9
-0+memset (sd->data,0xff , erase_len); + erase_addr = erase_start; +for (i =0 ; i <= (erase_end - erase_start) / erase_len; i++) { +if (sdsc) { +/* Only SDSC cards support write protect groups */ + wpnum =sd_addr_to_wpnum (erase_addr); +assert (wpnum < sd->wpgrps_size); +if (test_bit (wpnum, sd->wp_groups)) { sd->card_status |=WP_ERASE_SKIP ; +continue ;
So if a group is protected, you skip it but don't increase erase_addr. If G#4 is protected and G#5 isn't, when you check G#5 you end erasing G#4.
Quoted 8 lines
hw/sd/sd.c +2
-0} } +BLK_WRITE_BLOCK (erase_addr, erase_len); + erase_addr += erase_len; } }
Re: [PATCH v2 6/8] hw/sd: sd: Actually perform the erase operation
Quoted 49 lines · Philippe Mathieu-Daudé
Quoted 47 lines · Bin Meng
From: Bin Meng <[email protected]>
At present the
sd_erase()does not erase the requested range of card data to 0xFFs. Let's make the erase operation actually happen.Signed-off-by Bin Meng <[email protected]>---
Changes in v2:
- honor the write protection bits for SDSC cardshw/sd/sd.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)hw/sd/sd.c +9
-8@@ -766,6 +766,9 @@ static void sd_erase(SDState *sd)uint64_t erase_start = sd->erase_start;uint64_t erase_end = sd->erase_end;bool sdsc = true; +uint64_t wpnum; +uint64_t erase_addr; +int erase_len =1 <<HWBLOCK_SHIFT ;trace_sdcard_erase (sd->erase_start, sd->erase_end); if (sd->erase_start == INVALID_ADDRESS@@ -794,17 +797,20 @@ static void sd_erase(SDState *sd) sd->erase_end =INVALID_ADDRESS ; sd->csd[14 ] |=0x40 ; -/* Only SDSC cards support write protect groups */ -if (sdsc) { - erase_start =sd_addr_to_wpnum (erase_start); - erase_end =sd_addr_to_wpnum (erase_end); - -for (i = erase_start; i <= erase_end; i++) { -assert (i < sd->wpgrps_size); -if (test_bit (i, sd->wp_groups)) { +memset (sd->data,0xff , erase_len); + erase_addr = erase_start; +for (i =0 ; i <= (erase_end - erase_start) / erase_len; i++) { +if (sdsc) { +/* Only SDSC cards support write protect groups */ + wpnum =sd_addr_to_wpnum (erase_addr);
Quoted 9 lines
hw/sd/sd.c static void sd_erase(SDState *sd) · line 809
Quoted 4 lines
hw/sd/sd.c +3
-0+assert (wpnum < sd->wpgrps_size); +if (test_bit (wpnum, sd->wp_groups)) { sd->card_status |=WP_ERASE_SKIP ; +continue ;So if a group is protected, you skip it but don't increase
erase_addr. If G#4 is protected and G#5 isn't, when you check G#5 you end erasing G#4.
Oops, good catch!
I will send v2.
Quoted 6 lines
hw/sd/sd.c static void sd_erase(SDState *sd) · line 815
Quoted 6 lines
hw/sd/sd.c +2
-0} } +BLK_WRITE_BLOCK (erase_addr, erase_len); + erase_addr += erase_len; } }
Regards,
Bin
PATCH v2 7/8 hw/sd: sd: Skip write protect groups check in CMD24/25 for high capacity cards
In reply to Bin MengFrom: Bin Meng <[email protected]>
High capacity cards don't support write protection hence we should not preform the write protect groups check in CMD24/25 for them.
---
Changes in v2:
- new patch: sd: Skip write protect groups check in CMD24/25 for high capacity cards
hw/sd/sd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
hw/sd/sd.c +9 -5
@@ -1274,8 +1274,10 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req) sd->data_offset =0 ; sd->blk_written =0 ; -if (sd_wp_addr (sd, sd->data_start)) { - sd->card_status |=WP_VIOLATION ; +if (sd->size <=SDSC_MAX_CAPACITY ) { +if (sd_wp_addr (sd, sd->data_start)) { + sd->card_status |=WP_VIOLATION ; + } }if (sd->csd[14 ] &0x30 ) { sd->card_status |=WP_VIOLATION ;
@@ -1827,9 +1829,11 @@ void sd_write_byte(SDState *sd, uint8_t value) sd->card_status |=ADDRESS_ERROR ;break ; } -if (sd_wp_addr (sd, sd->data_start)) { - sd->card_status |=WP_VIOLATION ; -break ; +if (sd->size <=SDSC_MAX_CAPACITY ) { +if (sd_wp_addr (sd, sd->data_start)) { + sd->card_status |=WP_VIOLATION ; +break ; + } } } sd->data[sd->data_offset++] = value;
Signature · 1 line
2.25.1
Re: [PATCH v2 7/8] hw/sd: sd: Skip write protect groups check in CMD24/25 for high capacity cards
Quoted 7 lines · Bin Meng
From: Bin Meng <[email protected]>
High capacity cards don't support write protection hence we should not preform the write protect groups check in CMD24/25 for them.
Signed-off-by Bin Meng <[email protected]>
Quoted 7 lines
---
Changes in v2:
- new patch: sd: Skip write protect groups check in CMD24/25 for high capacity cardshw/sd/sd.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
PATCH v2 8/8 hw/sd: sd: Bypass the RCA check for CMD13 in SPI mode
In reply to Bin MengFrom: Bin Meng <[email protected]>
Unlike SD mode, when SD card is working in SPI mode, the argument of CMD13 is stuff bits. Hence we should bypass the RCA check.
See "Physical Layer Specification Version 8.00", chapter 7.3.1.3 Detailed Command Description (SPI mode):
"The card shall ignore stuff bits and reserved bits in an argument"
and Table 7-3 Commands and Arguments (SPI mode):
"CMD13 Argument [31:0] stuff bits"
---
Changes in v2:
- update commit message to include the reference in the spec
hw/sd/sd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/sd/sd.c +2 -1
@@ -1169,8 +1169,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)case 13 :/* CMD13: SEND_STATUS */ switch (sd->mode) {case sd_data_transfer_mode: -if (sd->rca != rca) +if (!sd->spi && sd->rca != rca) {return sd_r0; + }return sd_r1;
Signature · 1 line
2.25.1
Re: [PATCH v2 8/8] hw/sd: sd: Bypass the RCA check for CMD13 in SPI mode
Quoted 16 lines · Bin Meng
From: Bin Meng <[email protected]>
Unlike SD mode, when SD card is working in SPI mode, the argument of CMD13 is stuff bits. Hence we should bypass the RCA check.
See "Physical Layer Specification Version 8.00", chapter 7.3.1.3 Detailed Command Description (SPI mode):
"The card shall ignore stuff bits and reserved bits in an argument"
and Table 7-3 Commands and Arguments (SPI mode):
"CMD13 Argument [31:0] stuff bits"
Signed-off-by Bin Meng <[email protected]>
Quoted 7 lines
---
Changes in v2:
- update commit message to include the reference in the spechw/sd/sd.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines · Bin Meng
From: Bin Meng <[email protected]>
This includes several fixes related to erase operation of a SD card.
Bin Meng (8):
hw/sd: sd: Fix address check insd_erase()
hw/sd: sd: Only SDSC cards support CMD28/29/30
hw/sd: sd: Fix CMD30 response type
hw/sd: sd: Move the sd_block_{read,write} and macros ahead hw/sd: sd: Skip write protect groups check insd_erase()for high
capacity cards
hw/sd: sd: Actually perform the erase operation
hw/sd: sd: Skip write protect groups check in CMD24/25 for high
capacity cards
hw/sd: sd: Bypass the RCA check for CMD13 in SPI mode
Quoted 2 lines
hw/sd/sd.c | 99 +++++++++++++++++++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 35 deletions(-)
Thanks, patches 1-5 and 7-8 applied to sdmmc-next tree.