Skip to contentSkip to conversation

mmc: core: allow detection of locked cards

PATCH In review [email protected] 4 participants 16 messages Jun 6, 2024 – Jul 14, 2024 MMC, SD and SDIO

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 mmc: core: allow detection of locked cards

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

---
 drivers/mmc/core/sd.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/mmc/core/sd.c +12 -1
index 1c8148cdd..ae821df7d 100644
@@ -928,8 +928,19 @@ int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
 	bool reinit)
 {
 	int err;
+	u32 card_status;
 
-	if (!reinit) {
+	err = mmc_send_status(card, &card_status);
+	if (err){
+		pr_err("%s: unable to get card status\n", mmc_hostname(host));
+		return err;
+	}
+
+	if (card_status & R1_CARD_IS_LOCKED){
+		pr_warn("%s: card is locked\n", mmc_hostname(host));
+	}
+
+	if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
 		/*
 		 * Fetch SCR from card.
 		 */
Signature · 1 line
2.34.1
#1 lore raw
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Skipping the commands above, only means the card gets partially initialized. Leaving a card in that state seems fragile. What will happen to upper block layers and filesystems when trying to access it?

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make sense to me. It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to manage. Perhaps you can have a closer look at that series and see if it's possible to update it?

Kind regards
Uffe

[1]
https://lore.kernel.org/linux-mmc/[email protected]/

Quoted 34 lines
---
 drivers/mmc/core/sd.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/mmc/core/sd.c +12 -2
index 1c8148cdd..ae821df7d 100644
@@ -928,8 +928,19 @@ int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
        bool reinit)
 {
        int err;
+       u32 card_status;
 
-       if (!reinit) {
+       err = mmc_send_status(card, &card_status);
+       if (err){
+               pr_err("%s: unable to get card status\n", mmc_hostname(host));
+               return err;
+       }
+
+       if (card_status & R1_CARD_IS_LOCKED){
+               pr_warn("%s: card is locked\n", mmc_hostname(host));
+       }
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*
                 * Fetch SCR from card.
                 */
--

2.34.1

#2 lore raw
Quoted 12 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Quoted 2 lines

Skipping the commands above, only means the card gets partially initialized.

Correct, but it's an improvement in comparison to current state.

Quoted 1 line

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

Quoted 2 lines

What will
happen to upper block layers and filesystems when trying to access it?

Everything will simply time-out.

Quoted 7 lines

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make sense to me. It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to manage.

Yes, he did a great work but obviously no-one got too excited to review/merge his work. His solution was complex.

Mine targets the smallest change possible to make it work at least. I wanted to avoid a solution that would be hard to test, review and maintain. Especially when there is only a small interest in the feature.

Quoted 2 lines

Perhaps you can have a closer look at that series and see if it's possible to update it?

I don't think I have the skills to revive his work.

Quoted 3 lines

Kind regards
Uffe

BR
Daniel

Quoted 40 lines

[1]
https://lore.kernel.org/linux-mmc/[email protected]/

Quoted 36 lines
---
 drivers/mmc/core/sd.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/mmc/core/sd.c +12 -1
index 1c8148cdd..ae821df7d 100644
@@ -928,8 +928,19 @@ int mmc_sd_setup_card(struct mmc_host *host, 
 struct mmc_card *card,
        bool reinit)
 {
        int err;
+       u32 card_status;
 
-       if (!reinit) {
+       err = mmc_send_status(card, &card_status);
+       if (err){
+               pr_err("%s: unable to get card status\n", 
 mmc_hostname(host));
+               return err;
+       }
+
+       if (card_status & R1_CARD_IS_LOCKED){
+               pr_warn("%s: card is locked\n", mmc_hostname(host));
+       }
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*

* Fetch SCR from card.
*/
--
2.34.1

#3 lore raw
Quoted 14 lines · Daniel Kucera
Quoted 12 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Quoted 4 lines
Quoted 2 lines

Skipping the commands above, only means the card gets partially initialized.

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Quoted 4 lines
Quoted 2 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 5 lines
Quoted 3 lines

What will
happen to upper block layers and filesystems when trying to access it?

Everything will simply time-out.

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management support, for example.

Quoted 9 lines
Quoted 8 lines

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make sense to me. It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to manage.

Quoted 3 lines

Yes, he did a great work but obviously no-one got too excited to review/merge
his work. His solution was complex.

It's quite some amount of code. On the other hand, it seemed quite straight forward, not that complex to me.

Quoted 10 lines

Mine targets the smallest change possible to make it work at least. I wanted to avoid a solution that would be hard to test, review and maintain.
Especially when there is only a small interest in the feature.

Quoted 2 lines

Perhaps you can have a closer look at that series and see if it's possible to update it?

I don't think I have the skills to revive his work.

I see, maybe we should ping Al and other interested folkz to see if there still is some interest to move this forward?

[...]

Kind regards
Uffe

#4 lore raw
Quoted 38 lines · Ulf Hansson
Quoted 14 lines · Daniel Kucera
Quoted 12 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Quoted 4 lines
Quoted 2 lines

Skipping the commands above, only means the card gets partially initialized.

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Quoted 4 lines
Quoted 2 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 4 lines
Quoted 3 lines

What will
happen to upper block layers and filesystems when trying to access it?

Quoted 6 lines
Quoted 1 line

Everything will simply time-out.

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management support, for example.

Definitely an aspect that needs to be considered, probably even just powering it off after 10ish seconds would be better, then you still get the chance of unlocking it without having a locked card unknowingly consuming power.
Having a saved key and sending that to any card being plugged in seems wrong if you consider security, then again if you consider security you should probably somewhere else than the SD/MMC LOCK/UNLOCK ;)

Kind Regards,
Christian

#5 lore raw
In reply to Ulf Hansson
Quoted 15 lines · Ulf Hansson
Quoted 14 lines · Daniel Kucera
Quoted 12 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Skipping the commands above, only means the card gets partially initialized.

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Are you saying that that being able to work with locked card is not an improvement in comparison to not being able to? Or did I misunderstand that?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

It's not half-way initialized, it's initialized correctly, it's just not using the full potential of the card (higher speeds, etc.).

The situation would be the same as it is currently with emmc. Locked emmc gets initialized correctly but reads/writes time-out. What is wrong in having the same result for both sd and emmc?

Quoted 8 lines

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 4 lines
Quoted 3 lines

What will
happen to upper block layers and filesystems when trying to access it?

Quoted 6 lines
Quoted 1 line

Everything will simply time-out.

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management support, for example.

Okay, this is a valid concern. Would it be acceptable if we just enabled this "feature" with a module parameter, something like "sd_initialize_locked=1" with default 0?

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make sense to me. It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to manage.

Quoted 6 lines
Quoted 3 lines

Yes, he did a great work but obviously no-one got too excited to review/merge
his work. His solution was complex.

It's quite some amount of code. On the other hand, it seemed quite straight forward, not that complex to me.

It doesn't solve the situation when you don't know the password and you just want to erase the card and continue using in. The (un)locking doesn't belong to kernel IMO, if it can be implemented in user-space, it should and it is more flexible that way.

Quoted 7 lines
Quoted 7 lines

Mine targets the smallest change possible to make it work at least. I wanted to avoid a solution that would be hard to test, review and maintain.
Especially when there is only a small interest in the feature.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Perhaps you can have a closer look at that series and see if it's possible to update it?

I don't think I have the skills to revive his work.

I see, maybe we should ping Al and other interested folkz to see if there still is some interest to move this forward?

Okay, Al, are you interested?

Quoted 5 lines

[...]

Kind regards
Uffe

BR
Daniel

#6 lore raw
Quoted 20 lines · Ulf Hansson
Quoted 19 lines
Quoted 17 lines · Daniel Kucera
Quoted 14 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection.
For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Skipping the commands above, only means the card gets partially initialized.

Quoted 17 lines
Quoted 3 lines
Quoted 1 line

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Are you saying that that being able to work with locked card is not an improvement in comparison to not being able to? Or did I misunderstand that?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

Actually, it's what the spec expects - the CARD_IS_LOCKED is carried in CMD7 response. Then the card responds to class 0, class 7, and ACMD41.

Quoted 43 lines

It's not half-way initialized, it's initialized correctly, it's just not using the full potential of the card (higher speeds, etc.).

The situation would be the same as it is currently with emmc. Locked emmc gets initialized correctly but reads/writes time-out. What is wrong in having the same result for both sd and emmc?

Quoted 5 lines

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

What will
happen to upper block layers and filesystems when trying to access it?

Everything will simply time-out.

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management

support,

Quoted 1 line

for example.

Okay, this is a valid concern. Would it be acceptable if we just enabled this "feature" with a module parameter, something like "sd_initialize_locked=1" with default 0?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make

sense to me.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to

manage.

Quoted 4 lines
Quoted 3 lines

Yes, he did a great work but obviously no-one got too excited to review/merge his work. His solution was complex.

Quoted 7 lines
Quoted 2 lines

It's quite some amount of code. On the other hand, it seemed quite straight forward, not that complex to me.

It doesn't solve the situation when you don't know the password and you just want to erase the card and continue using in. The (un)locking doesn't belong to kernel IMO, if it can be implemented in user-space, it should and it is more flexible that way.

I also see little value in handling the unlocking process in the kernel.
I find the proposed approach, e.g. co-exists with its sibling mmc-utils patch, straight forward and simpler.

Thanks,
Avri

Quoted 27 lines
Quoted 8 lines
Quoted 8 lines

Mine targets the smallest change possible to make it work at least. I wanted to avoid a solution that would be hard to test, review and maintain.
Especially when there is only a small interest in the feature.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Perhaps you can have a closer look at that series and see if it's possible to update it?

I don't think I have the skills to revive his work.

I see, maybe we should ping Al and other interested folkz to see if there still is some interest to move this forward?

Okay, Al, are you interested?

Quoted 5 lines

[...]

Kind regards
Uffe

BR
Daniel

#7 lore raw

Hi Ulf,

could you please reconsider this?
Or give some tips how to make this acceptable for merging?

Thank you,
Daniel.

Quoted 124 lines · Avri Altman
Quoted 20 lines · Ulf Hansson
Quoted 19 lines
Quoted 17 lines · Daniel Kucera
Quoted 14 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection.
For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Skipping the commands above, only means the card gets partially initialized.

Quoted 17 lines
Quoted 3 lines
Quoted 1 line

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Are you saying that that being able to work with locked card is not an improvement in comparison to not being able to? Or did I misunderstand that?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

Actually, it's what the spec expects - the CARD_IS_LOCKED is carried in CMD7 response.
Then the card responds to class 0, class 7, and ACMD41.

Quoted 43 lines

It's not half-way initialized, it's initialized correctly, it's just not using the full potential of the card (higher speeds, etc.).

The situation would be the same as it is currently with emmc. Locked emmc gets initialized correctly but reads/writes time-out. What is wrong in having the same result for both sd and emmc?

Quoted 5 lines

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 1 line
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

What will
happen to upper block layers and filesystems when trying to access it?

Everything will simply time-out.

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management

support,

Quoted 1 line

for example.

Okay, this is a valid concern. Would it be acceptable if we just enabled this "feature" with a module parameter, something like "sd_initialize_locked=1" with default 0?

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

Several years ago Al Cooper made an attempt [1] to manage the unlocking of the card during initialization, by finding the password through the "keys" subsystem. The series didn't really make it to the upstream kernel, but overall the approach seemed to make

sense to me.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

It should allow us to complete the initialization of the card inside the kernel and prevent unnecessary complexity for userspace to

manage.

Quoted 4 lines
Quoted 3 lines

Yes, he did a great work but obviously no-one got too excited to review/merge his work. His solution was complex.

Quoted 7 lines
Quoted 2 lines

It's quite some amount of code. On the other hand, it seemed quite straight forward, not that complex to me.

It doesn't solve the situation when you don't know the password and you just want to erase the card and continue using in. The (un)locking doesn't belong to kernel IMO, if it can be implemented in user-space, it should and it is more flexible that way.

I also see little value in handling the unlocking process in the kernel. I find the proposed approach, e.g. co-exists with its sibling mmc-utils patch, straight forward and simpler.

Thanks,
Avri

Quoted 27 lines
Quoted 8 lines
Quoted 8 lines

Mine targets the smallest change possible to make it work at least. I wanted to avoid a solution that would be hard to test, review and maintain.
Especially when there is only a small interest in the feature.

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

Perhaps you can have a closer look at that series and see if it's possible to update it?

I don't think I have the skills to revive his work.

I see, maybe we should ping Al and other interested folkz to see if there still is some interest to move this forward?

Okay, Al, are you interested?

Quoted 5 lines

[...]

Kind regards
Uffe

BR
Daniel

#8 lore raw
In reply to Daniel Kucera
Quoted 39 lines · Daniel Kucera
Quoted 16 lines · Ulf Hansson
Quoted 14 lines · Daniel Kucera
Quoted 12 lines · Ulf Hansson
Quoted 10 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

Skipping the commands above, only means the card gets partially initialized.

Correct, but it's an improvement in comparison to current state.

Not sure I agree with that, sorry.

Are you saying that that being able to work with locked card is not an improvement in comparison to not being able to? Or did I misunderstand that?

Quoted 9 lines
Quoted 5 lines
Quoted 3 lines

Leaving a card in that state seems fragile.

Fragile in what sense? Nothing can happen to the card as it is locked.

We may end up having a card half-way initialized that we can't really communicate with in a stable manner. From a system point of view, I would be worried.

Quoted 6 lines

It's not half-way initialized, it's initialized correctly, it's just not using the full potential of the card (higher speeds, etc.).

The situation would be the same as it is currently with emmc. Locked emmc gets initialized correctly but reads/writes time-out. What is wrong in having the same result for both sd and emmc?

Very good point!

Quoted 11 lines
Quoted 11 lines

I would rather just power off the card if initialization fails and remove its corresponding device from the system.

Quoted 5 lines
Quoted 3 lines

What will
happen to upper block layers and filesystems when trying to access it?

Everything will simply time-out.

Quoted 8 lines
Quoted 4 lines

Yes, but it's uncertain what that could lead to?

What will happen with power consumption and power management support, for example.

Okay, this is a valid concern. Would it be acceptable if we just enabled this "feature" with a module parameter, something like "sd_initialize_locked=1" with default 0?

That would be an option, but I don't quite like that either. I guess using locked cards isn't that terribly common. So, perhaps we should simply leave this to be managed entirely by userspace, for now. As pointed out by Avri too.

We should still be able to power on/off the card during suspend/resume, which I think is the most important part here.

[...]

Let me get back to reviewing the actual code for $subject patch, to see how we can move this forward then.

Kind regards
Uffe

#9 lore raw
In reply to [email protected]
Quoted 26 lines · On Thu, 6 Jun 2024 at 15:12, <[email protected]> wrote:

From: Daniel Kucera <[email protected]>

Locked SD card will not reply to SEND_SCR or SD_STATUS commands so it was failing to initialize previously. When skipped, the card will get initialized and CMD42 can be sent using ioctl to unlock the card or remove password protection. For eMMC, this is not necessary because all initialization commands are allowed in locked state.
Until unlocked, all read/write calls will timeout.

---
 drivers/mmc/core/sd.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
drivers/mmc/core/sd.c +1 -0
index 1c8148cdd..ae821df7d 100644
@@ -928,8 +928,19 @@ int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
        bool reinit)
 {
        int err;
+       u32 card_status;
 

drivers/mmc/core/sd.c int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card, · line 941

Quoted 10 lines
drivers/mmc/core/sd.c +9 -1
-       if (!reinit) {
+       err = mmc_send_status(card, &card_status);
+       if (err){
+               pr_err("%s: unable to get card status\n", mmc_hostname(host));
+               return err;
+       }
+
+       if (card_status & R1_CARD_IS_LOCKED){
+               pr_warn("%s: card is locked\n", mmc_hostname(host));
+       }

If I understand correctly, there is no point in sending the CMD13 above, unless this is the first attempt to initialize the card. Therefore, it's better to move the whole part above, inside the below if-clause too, otherwise we would end up sending a CMD13 in cases when it's not needed.

Quoted 8 lines
drivers/mmc/core/sd.c +2 -1
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*
                 * Fetch SCR from card.
                 */
--

2.34.1

Kind regards
Uffe

#10 lore raw
Quoted 5 lines

If I understand correctly, there is no point in sending the CMD13 above, unless this is the first attempt to initialize the card.
Therefore, it's better to move the whole part above, inside the below if-clause too, otherwise we would end up sending a CMD13 in cases when it's not needed.

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well, So theoretically you want to skip mmc_sd_setup_card altogether. ACMD6, CMD6, and CMD19 should wait for CMD42 to unlock the card.

Thanks,
Avri

Quoted 12 lines
Quoted 9 lines
drivers/mmc/core/sd.c +2 -1
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*
                 * Fetch SCR from card.
                 */
--

2.34.1

Kind regards
Uffe

#11 lore raw
Quoted 7 lines · Avri Altman
Quoted 5 lines

If I understand correctly, there is no point in sending the CMD13 above, unless this is the first attempt to initialize the card.
Therefore, it's better to move the whole part above, inside the below if-clause too, otherwise we would end up sending a CMD13 in cases when it's not needed.

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well, So theoretically you want to skip mmc_sd_setup_card altogether.

Do you mean to modify:
mmc_select_card(struct mmc_card *card)
to somehow return or save the R1 response to card struct?

Because currently, it is not available.

Thank you,
Daniel.

Quoted 17 lines

ACMD6, CMD6, and CMD19 should wait for CMD42 to unlock the card.

Thanks,
Avri

Quoted 12 lines
Quoted 9 lines
drivers/mmc/core/sd.c +2 -1
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*
                 * Fetch SCR from card.
                 */
--

2.34.1

Kind regards
Uffe

#12 lore raw
Quoted 6 lines · Avri Altman
Quoted 5 lines
Quoted 5 lines

If I understand correctly, there is no point in sending the CMD13 above, unless this is the first attempt to initialize the card. Therefore, it's better to move the whole part above, inside the below if-clause too, otherwise we would end up sending a CMD13 in cases when it's not needed.

Quoted 8 lines
Quoted 2 lines

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well, So theoretically you want to skip mmc_sd_setup_card altogether.

Do you mean to modify:
mmc_select_card(struct mmc_card *card)
to somehow return or save the R1 response to card struct?

Because currently, it is not available.

Its just a suggestion - CMD13 does carry this bit as well.
Either way, the card will not respond to ACMD6, CMD6, and CMD19 while waiting for CMD42 to unlock the card, Hence failing those should not eventually cause mmc_remove_card().

Thanks,
Avri

Quoted 21 lines

Thank you,
Daniel.

Quoted 17 lines

ACMD6, CMD6, and CMD19 should wait for CMD42 to unlock the card.

Thanks,
Avri

Quoted 12 lines
Quoted 9 lines
drivers/mmc/core/sd.c +2 -1
+
+       if (!reinit && !(card_status & R1_CARD_IS_LOCKED)) {
                /*
                 * Fetch SCR from card.
                 */
--

2.34.1

Kind regards
Uffe

#13 lore raw
In reply to Daniel Kucera
Quoted 9 lines · Daniel Kucera
Quoted 7 lines · Avri Altman
Quoted 7 lines

If I understand correctly, there is no point in sending the CMD13 above, unless
this is the first attempt to initialize the card. Therefore, it's better to move the whole part above, inside the below if-clause
too, otherwise we would end up sending a CMD13 in cases when it's not needed.

Quoted 7 lines
Quoted 3 lines

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well,
So theoretically you want to skip mmc_sd_setup_card altogether.

Do you mean to modify:
mmc_select_card(struct mmc_card *card)
to somehow return or save the R1 response to card struct?

I quite like this, as it avoids us from sending an unnecessary command during initialization.

A suggestion is to let _mmc_select_card() take an additional out-parameter to provide the card's status. Then we can let mmc_select_card() parse the status - and if it finds that the card is locked, it can set a new state in card->state (similar to how we use MMC_STATE_BLOCKADDR, for example).

In another future step, we may also want to keep track of whether a locked card becomes unlocked. Using the card->state should work fine for that too, I think.

[...]

Kind regards
Uffe

#14 lore raw
Quoted 14 lines · Ulf Hansson
Quoted 13 lines · Daniel Kucera
Quoted 10 lines · Avri Altman
Quoted 7 lines

If I understand correctly, there is no point in sending the CMD13 above, unless
this is the first attempt to initialize the card. Therefore, it's better to move the whole part above, inside the below if-clause
too, otherwise we would end up sending a CMD13 in cases when it's not needed.

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well,
So theoretically you want to skip mmc_sd_setup_card altogether.

Quoted 6 lines
Quoted 3 lines

Do you mean to modify:
mmc_select_card(struct mmc_card *card)
to somehow return or save the R1 response to card struct?

I quite like this, as it avoids us from sending an unnecessary command during initialization.

Okay, but what if the host is SPI?

        if (!mmc_host_is_spi(host)) {
                err = mmc_select_card(card);
                if (err)
                        goto free_card;
        }

        err = mmc_sd_setup_card(host, card, oldcard != NULL);
Quoted 15 lines

A suggestion is to let _mmc_select_card() take an additional out-parameter to provide the card's status. Then we can let mmc_select_card() parse the status - and if it finds that the card is locked, it can set a new state in card->state (similar to how we use MMC_STATE_BLOCKADDR, for example).

In another future step, we may also want to keep track of whether a locked card becomes unlocked. Using the card->state should work fine for that too, I think.

[...]

Kind regards
Uffe

#15 lore raw
Quoted 15 lines · Ulf Hansson
Quoted 13 lines
Quoted 11 lines · Daniel Kucera
Quoted 8 lines · Avri Altman
Quoted 5 lines

If I understand correctly, there is no point in sending the CMD13 above, unless this is the first attempt to initialize the card. Therefore, it's better to move the whole part above, inside the below if-clause too, otherwise we would end up sending a CMD13 in cases when it's not needed.

R1_CARD_IS_LOCKED is CMD13 response, but already in CMD7 response as well, So theoretically you want to skip mmc_sd_setup_card altogether.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

Do you mean to modify:
mmc_select_card(struct mmc_card *card) to somehow return or save the R1 response to card struct?

I quite like this, as it avoids us from sending an unnecessary command during initialization.

Okay, but what if the host is SPI?

Looking in the spec, In SPI mode, the card moves to transfer state post ACMD41+CMD58. On the other hand, it does support lock/unlock functionality - identical to SD mode, And the R1_CARD_IS_LOCKED should be obtained from the results of CMD13 - Just like you proposed.

So, looks like your original proposal is more inclusive, but let me ask around and get back to you.

Thanks,
Avri

Quoted 24 lines
         if (!mmc_host_is_spi(host)) {
                 err = mmc_select_card(card);
                 if (err)
                         goto free_card;
         }

         err = mmc_sd_setup_card(host, card, oldcard != NULL);
Quoted 15 lines

A suggestion is to let _mmc_select_card() take an additional out-parameter to provide the card's status. Then we can let mmc_select_card() parse the status - and if it finds that the card is locked, it can set a new state in card->state (similar to how we use MMC_STATE_BLOCKADDR, for example).

In another future step, we may also want to keep track of whether a locked card becomes unlocked. Using the card->state should work fine for that too, I think.

[...]

Kind regards
Uffe