From 2b411f1257895f5d05ad2e221c856893af010012 Mon Sep 17 00:00:00 2001 From: Jaegeuk Kim Date: Thu, 30 Sep 2021 11:12:50 -0700 Subject: [PATCH] BACKPORT: FROMGIT: scsi: ufs: core: Retry START_STOP on UNIT_ATTENTION Commit 57d104c153d3 ("ufs: add UFS power management support") made the UFS driver submit a REQUEST SENSE command before submitting a power management command to a WLUN to clear the POWER ON unit attention. Instead of submitting a REQUEST SENSE command before submitting a power management command, retry the power management command until it succeeds. This is the preparation to get rid of all UNIT ATTENTION code which should be handled by users. Bug: 194712579 Link: https://lore.kernel.org/r/20211001182015.1347587-2-jaegeuk@kernel.org (cherry picked from commit af21c3fd5b3ec540a97b367a70b26616ff7e0c55 git://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git for-next) Cc: Adrian Hunter Reviewed-by: Bart Van Assche Signed-off-by: Jaegeuk Kim Signed-off-by: Martin K. Petersen Change-Id: I7e639d89ae9fbd5ff0f1b3a6e5cbe77682ebefc0 --- drivers/scsi/ufs/ufshcd.c | 12 +++++++++--- include/scsi/scsi.h | 15 +++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/drivers/scsi/ufs/ufshcd.c b/drivers/scsi/ufs/ufshcd.c index f160d0fcdc59..d47c32510bd3 100644 --- a/drivers/scsi/ufs/ufshcd.c +++ b/drivers/scsi/ufs/ufshcd.c @@ -8573,7 +8573,7 @@ static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, struct scsi_sense_hdr sshdr; struct scsi_device *sdp; unsigned long flags; - int ret; + int ret, retries; spin_lock_irqsave(hba->host->host_lock, flags); sdp = hba->sdev_ufs_device; @@ -8607,8 +8607,14 @@ static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, * callbacks hence set the RQF_PM flag so that it doesn't resume the * already suspended childs. */ - ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, - START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); + for (retries = 3; retries > 0; --retries) { + ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr, + START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL); + if (!scsi_status_is_check_condition(ret) || + !scsi_sense_valid(&sshdr) || + sshdr.sense_key != UNIT_ATTENTION) + break; + } if (ret) { sdev_printk(KERN_WARNING, sdp, "START_STOP failed for power mode: %d, result %x\n", diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h index 5339baadc082..ef3345119b29 100644 --- a/include/scsi/scsi.h +++ b/include/scsi/scsi.h @@ -88,6 +88,21 @@ static inline int scsi_is_wlun(u64 lun) return (lun & 0xff00) == SCSI_W_LUN_BASE; } +/** + * scsi_status_is_check_condition - check the status return. + * + * @status: the status passed up from the driver (including host and + * driver components) + * + * This returns true if the status code is SAM_STAT_CHECK_CONDITION. + */ +static inline int scsi_status_is_check_condition(int status) +{ + if (status < 0) + return false; + status &= 0xfe; + return status == SAM_STAT_CHECK_CONDITION; +} /* * MESSAGE CODES