Skip to content

Commit a0d4c7e

Browse files
ChaotianJingstorulf
authored andcommitted
mmc: block: Add CMD13 polling for MMC IOCTLS with R1B response
MMC IOCTLS with R1B responses may cause the card to enter the busy state, which means it's not ready to receive a new request. To prevent new requests from being sent to the card, use a CMD13 polling loop to verify that the card returns to the transfer state, before completing the request. Signed-off-by: Chaotian Jing <chaotian.jing@mediatek.com> Reviewed-by: Avri Altman <avri.altman@wdc.com> Cc: stable@vger.kernel.org Signed-off-by: Ulf Hansson <ulf.hansson@linaro.org>
1 parent 3869468 commit a0d4c7e

1 file changed

Lines changed: 55 additions & 92 deletions

File tree

drivers/mmc/core/block.c

Lines changed: 55 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -408,38 +408,6 @@ static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
408408
return 0;
409409
}
410410

411-
static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
412-
u32 retries_max)
413-
{
414-
int err;
415-
u32 retry_count = 0;
416-
417-
if (!status || !retries_max)
418-
return -EINVAL;
419-
420-
do {
421-
err = __mmc_send_status(card, status, 5);
422-
if (err)
423-
break;
424-
425-
if (!R1_STATUS(*status) &&
426-
(R1_CURRENT_STATE(*status) != R1_STATE_PRG))
427-
break; /* RPMB programming operation complete */
428-
429-
/*
430-
* Rechedule to give the MMC device a chance to continue
431-
* processing the previous command without being polled too
432-
* frequently.
433-
*/
434-
usleep_range(1000, 5000);
435-
} while (++retry_count < retries_max);
436-
437-
if (retry_count == retries_max)
438-
err = -EPERM;
439-
440-
return err;
441-
}
442-
443411
static int ioctl_do_sanitize(struct mmc_card *card)
444412
{
445413
int err;
@@ -468,6 +436,58 @@ static int ioctl_do_sanitize(struct mmc_card *card)
468436
return err;
469437
}
470438

439+
static inline bool mmc_blk_in_tran_state(u32 status)
440+
{
441+
/*
442+
* Some cards mishandle the status bits, so make sure to check both the
443+
* busy indication and the card state.
444+
*/
445+
return status & R1_READY_FOR_DATA &&
446+
(R1_CURRENT_STATE(status) == R1_STATE_TRAN);
447+
}
448+
449+
static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
450+
u32 *resp_errs)
451+
{
452+
unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
453+
int err = 0;
454+
u32 status;
455+
456+
do {
457+
bool done = time_after(jiffies, timeout);
458+
459+
err = __mmc_send_status(card, &status, 5);
460+
if (err) {
461+
dev_err(mmc_dev(card->host),
462+
"error %d requesting status\n", err);
463+
return err;
464+
}
465+
466+
/* Accumulate any response error bits seen */
467+
if (resp_errs)
468+
*resp_errs |= status;
469+
470+
/*
471+
* Timeout if the device never becomes ready for data and never
472+
* leaves the program state.
473+
*/
474+
if (done) {
475+
dev_err(mmc_dev(card->host),
476+
"Card stuck in wrong state! %s status: %#x\n",
477+
__func__, status);
478+
return -ETIMEDOUT;
479+
}
480+
481+
/*
482+
* Some cards mishandle the status bits,
483+
* so make sure to check both the busy
484+
* indication and the card state.
485+
*/
486+
} while (!mmc_blk_in_tran_state(status));
487+
488+
return err;
489+
}
490+
471491
static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
472492
struct mmc_blk_ioc_data *idata)
473493
{
@@ -477,7 +497,6 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
477497
struct scatterlist sg;
478498
int err;
479499
unsigned int target_part;
480-
u32 status = 0;
481500

482501
if (!card || !md || !idata)
483502
return -EINVAL;
@@ -611,16 +630,12 @@ static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
611630

612631
memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
613632

614-
if (idata->rpmb) {
633+
if (idata->rpmb || (cmd.flags & MMC_RSP_R1B)) {
615634
/*
616-
* Ensure RPMB command has completed by polling CMD13
635+
* Ensure RPMB/R1B command has completed by polling CMD13
617636
* "Send Status".
618637
*/
619-
err = ioctl_rpmb_card_status_poll(card, &status, 5);
620-
if (err)
621-
dev_err(mmc_dev(card->host),
622-
"%s: Card Status=0x%08X, error %d\n",
623-
__func__, status, err);
638+
err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
624639
}
625640

626641
return err;
@@ -970,58 +985,6 @@ static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
970985
return ms;
971986
}
972987

973-
static inline bool mmc_blk_in_tran_state(u32 status)
974-
{
975-
/*
976-
* Some cards mishandle the status bits, so make sure to check both the
977-
* busy indication and the card state.
978-
*/
979-
return status & R1_READY_FOR_DATA &&
980-
(R1_CURRENT_STATE(status) == R1_STATE_TRAN);
981-
}
982-
983-
static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
984-
u32 *resp_errs)
985-
{
986-
unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
987-
int err = 0;
988-
u32 status;
989-
990-
do {
991-
bool done = time_after(jiffies, timeout);
992-
993-
err = __mmc_send_status(card, &status, 5);
994-
if (err) {
995-
dev_err(mmc_dev(card->host),
996-
"error %d requesting status\n", err);
997-
return err;
998-
}
999-
1000-
/* Accumulate any response error bits seen */
1001-
if (resp_errs)
1002-
*resp_errs |= status;
1003-
1004-
/*
1005-
* Timeout if the device never becomes ready for data and never
1006-
* leaves the program state.
1007-
*/
1008-
if (done) {
1009-
dev_err(mmc_dev(card->host),
1010-
"Card stuck in wrong state! %s status: %#x\n",
1011-
__func__, status);
1012-
return -ETIMEDOUT;
1013-
}
1014-
1015-
/*
1016-
* Some cards mishandle the status bits,
1017-
* so make sure to check both the busy
1018-
* indication and the card state.
1019-
*/
1020-
} while (!mmc_blk_in_tran_state(status));
1021-
1022-
return err;
1023-
}
1024-
1025988
static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1026989
int type)
1027990
{

0 commit comments

Comments
 (0)