Skip to contentSkip to conversation

Simplify ufshcd_execute_start_stop()

PATCH Superseded Bart Van Assche 2 participants 9 messages Feb 9, 2023 – Feb 10, 2023

Subsystems (2)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

v2 cover Simplify ufshcd_execute_start_stop()

Hi Martin,

This patch series simplifies ufshcd_execute_start_stop() by using the new scsi_execute_cmd() function instead of open-coding it. Please consider this patch for the next merge window.

Thanks,

Bart.

Changes compared to v1:
- Addressed John's feedback that RQF_PM should always be set if BLK_MQ_REQ_PM is set.
- Added a third patch that makes RQF_PM to be set implicitly.

Bart Van Assche (3):
scsi: core: Extend struct scsi_exec_args
scsi: ufs: Rely on the block layer for setting RQF_PM
scsi: ufs: Simplify ufshcd_execute_start_stop()

 drivers/scsi/scsi_lib.c    |  1 +
 drivers/ufs/core/ufshcd.c  | 35 ++++++++---------------------------
 include/scsi/scsi_device.h |  1 +
 3 files changed, 10 insertions(+), 27 deletions(-)
author reviewer
#1 lore raw

PATCH v2 1/3 scsi: core: Extend struct scsi_exec_args

Allow SCSI LLDs to specify SCMD_* flags.

Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/scsi/scsi_lib.c    | 1 +
 include/scsi/scsi_device.h | 1 +
 2 files changed, 2 insertions(+)
drivers/scsi/scsi_lib.c +1 -0
index abe93ec8b7d0..b7c569a42aa4 100644
@@ -229,6 +229,7 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
 	scmd->cmd_len = COMMAND_SIZE(cmd[0]);
 	memcpy(scmd->cmnd, cmd, scmd->cmd_len);
 	scmd->allowed = retries;
+	scmd->flags |= args->scmd_flags;
 	req->timeout = timeout;
 	req->rq_flags |= RQF_QUIET;
 
include/scsi/scsi_device.h +1 -0
index 7e95ec45138f..c7bfb1f5a8e7 100644
@@ -462,6 +462,7 @@ struct scsi_exec_args {
 	unsigned int sense_len;		/* sense buffer len */
 	struct scsi_sense_hdr *sshdr;	/* decoded sense header */
 	blk_mq_req_flags_t req_flags;	/* BLK_MQ_REQ flags */
+	unsigned int scmd_flags;	/* SCMD flags */
 	int *resid;			/* residual length */
 };
 
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] scsi: core: Extend struct scsi_exec_args

Quoted 5 lines · Bart Van Assche

Allow SCSI LLDs to specify SCMD_* flags.

Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>

Just a couple of nits, below:

Quoted 10 lines
---
  drivers/scsi/scsi_lib.c    | 1 +
  include/scsi/scsi_device.h | 1 +
  2 files changed, 2 insertions(+)
drivers/scsi/scsi_lib.c +0 -0
index abe93ec8b7d0..b7c569a42aa4 100644

drivers/scsi/scsi_lib.c int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd, · line 232

Quoted 4 lines
drivers/scsi/scsi_lib.c +1 -0
@@ -229,6 +229,7 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
  	scmd->cmd_len = COMMAND_SIZE(cmd[0]);
  	memcpy(scmd->cmnd, cmd, scmd->cmd_len);
  	scmd->allowed = retries;
+	scmd->flags |= args->scmd_flags;

nit: we zero the scsi_cmnd payload in scsi_alloc_request() -> scsi_initialize_rq(), so don't really need the bitwise OR. However it may be better to keep for change of scsi_initialize_rq() changing in terms of init.

include/scsi/scsi_device.h struct scsi_exec_args { · line 465

Quoted 12 lines
drivers/scsi/scsi_lib.c +0 -0
  	req->timeout = timeout;
  	req->rq_flags |= RQF_QUIET;
 
include/scsi/scsi_device.h +1 -0
index 7e95ec45138f..c7bfb1f5a8e7 100644
@@ -462,6 +462,7 @@ struct scsi_exec_args {
  	unsigned int sense_len;		/* sense buffer len */
  	struct scsi_sense_hdr *sshdr;	/* decoded sense header */
  	blk_mq_req_flags_t req_flags;	/* BLK_MQ_REQ flags */
+	unsigned int scmd_flags;	/* SCMD flags */

nit: scsi_cmnd.flags is an int, so prob should keep the same type

Quoted 3 lines
include/scsi/scsi_device.h +0 -0
  	int *resid;			/* residual length */
  };
 
author reviewer
#3 lore raw

Re: [PATCH v2 1/3] scsi: core: Extend struct scsi_exec_args

Quoted 13 lines · John Garry

drivers/scsi/scsi_lib.c int scsi_execute_cmd(struct scsi_device *sdev, · line 233

Quoted 10 lines · Bart Van Assche
drivers/scsi/scsi_lib.c +1 -0
index abe93ec8b7d0..b7c569a42aa4 100644
@@ -229,6 +229,7 @@ int scsi_execute_cmd(struct scsi_device *sdev, 
 const unsigned char *cmd,
       scmd->cmd_len = COMMAND_SIZE(cmd[0]);
       memcpy(scmd->cmnd, cmd, scmd->cmd_len);
       scmd->allowed = retries;
+    scmd->flags |= args->scmd_flags;

nit: we zero the scsi_cmnd payload in scsi_alloc_request() -> scsi_initialize_rq(), so don't really need the bitwise OR. However it may be better to keep for change of scsi_initialize_rq() changing in terms of init.

Hi John,

Thanks for the review.

If scmd->flags would be initialized to a non-zero value in the future then it would be non-trivial to remember that the above assignment would have to be changed into a logical or. So I'd like to keep the logical or.

Quoted 5 lines
Quoted 5 lines
drivers/scsi/scsi_lib.c +0 -0
       req->timeout = timeout;
       req->rq_flags |= RQF_QUIET;
include/scsi/scsi_device.h +0 -1
index 7e95ec45138f..c7bfb1f5a8e7 100644
--- a/include/scsi/scsi_device.h
Quoted 8 lines
Quoted 6 lines

+++ b/include/scsi/scsi_device.h

include/scsi/scsi_device.h +0 -0
@@ -462,6 +462,7 @@ struct scsi_exec_args {

      unsigned int sense_len;        /* sense buffer len */ struct scsi_sense_hdr *sshdr;    /* decoded sense header */ blk_mq_req_flags_t req_flags;    /* BLK_MQ_REQ flags */ +    unsigned int scmd_flags;    /* SCMD flags */

nit: scsi_cmnd.flags is an int, so prob should keep the same type

How about changing the type of scsi_cmnd.flags from 'int' into 'unsigned int'? I can't think of any reason to make a flags variable signed instead of unsigned.

Thanks,

Bart.

#4 lore raw

Re: [PATCH v2 1/3] scsi: core: Extend struct scsi_exec_args

Quoted 1 line · Bart Van Assche

If scmd->flags would be initialized to a non-zero value in the future then it would be non-trivial to remember that the above assignment would have to be changed into a logical or. So I'd like to keep the logical or.

ok

Quoted 15 lines
Quoted 14 lines
Quoted 8 lines

      req->timeout = timeout;
      req->rq_flags |= RQF_QUIET;

include/scsi/scsi_device.h +0 -0
index 7e95ec45138f..c7bfb1f5a8e7 100644
@@ -462,6 +462,7 @@ struct scsi_exec_args {
Quoted 4 lines

      unsigned int sense_len;        /* sense buffer len */ struct scsi_sense_hdr *sshdr;    /* decoded sense header */ blk_mq_req_flags_t req_flags;    /* BLK_MQ_REQ flags */ +    unsigned int scmd_flags;    /* SCMD flags */

nit: scsi_cmnd.flags is an int, so prob should keep the same type

Quoted 1 line

How about changing the type of scsi_cmnd.flags from 'int' into 'unsigned int'? I can't think of any reason to make a flags variable signed instead of unsigned.

Yeah, it is odd to have a signed flags, but it's not the only one in scsi_cmnd. You suggest a reasonable change, however scsi core structures have many odd member types already.. so I'll leave it to you to decide on the proposed change.

Thanks,
John

author reviewer
#5 lore raw

PATCH v2 2/3 scsi: ufs: Rely on the block layer for setting RQF_PM

In reply to Bart Van Assche

Do not set RQF_PM explicitly since scsi_alloc_request() sets it indirectly if BLK_MQ_REQ_PM is set. The call chain for the code that sets RQF_PM is as follows:

    scsi_alloc_request()
      blk_mq_alloc_request()
        __blk_mq_alloc_requests()
          blk_mq_rq_ctx_init()
            if (data->flags & BLK_MQ_REQ_PM)
              data->rq_flags |= RQF_PM;
Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/ufs/core/ufshcd.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +2 -1
index 0cfe112ff8c3..3512dcc152c8 100644
@@ -9141,7 +9141,8 @@ static int ufshcd_execute_start_stop(struct scsi_device *sdev,
 	scmd->allowed = 0/*retries*/;
 	scmd->flags |= SCMD_FAIL_IF_RECOVERING;
 	req->timeout = 1 * HZ;
-	req->rq_flags |= RQF_PM | RQF_QUIET;
+	req->rq_flags |= RQF_QUIET;
+	WARN_ON_ONCE(!(req->rq_flags & RQF_PM));
 
 	blk_execute_rq(req, /*at_head=*/true);
 
Reviewed-by on v2 2/3
#6 lore raw

Re: [PATCH v2 2/3] scsi: ufs: Rely on the block layer for setting RQF_PM

Quoted 11 lines · Bart Van Assche

Do not set RQF_PM explicitly since scsi_alloc_request() sets it indirectly if BLK_MQ_REQ_PM is set. The call chain for the code that sets RQF_PM is as follows:

     scsi_alloc_request()
       blk_mq_alloc_request()
         __blk_mq_alloc_requests()
           blk_mq_rq_ctx_init()
             if (data->flags & BLK_MQ_REQ_PM)
               data->rq_flags |= RQF_PM;
Quoted 3 lines
Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>

To me, this looks ok. JFYI, this is where I understood that Christoph mentioned that we are required to set both flags:

https://urldefense.com/v3/__https://lore.kernel.org/linux-scsi/[email protected]/__;!!ACWV5N9M2RV99hQ!JQJXru7NACJ7vH7xvV-26xPrgI4gDRZ8NP0Q9uYYKB_5RYq949y6HINgGq1D6prwH3V9FU21ntPhxpoi--Pe_qd2tz0$

However, to me, we currently set RQF_PM after alloc the req, so I don't see why just using BLK_MQ_REQ_PM in the scsi_alloc_request() call won't suffice (since it would lead to RQF_PM being set).

Anyway, feel free to add:

Quoted 18 lines
---
  drivers/ufs/core/ufshcd.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +2 -1
index 0cfe112ff8c3..3512dcc152c8 100644
@@ -9141,7 +9141,8 @@ static int ufshcd_execute_start_stop(struct scsi_device *sdev,
  	scmd->allowed = 0/*retries*/;
  	scmd->flags |= SCMD_FAIL_IF_RECOVERING;
  	req->timeout = 1 * HZ;
-	req->rq_flags |= RQF_PM | RQF_QUIET;
+	req->rq_flags |= RQF_QUIET;
+	WARN_ON_ONCE(!(req->rq_flags & RQF_PM));
 
  	blk_execute_rq(req, /*at_head=*/true);
 
author reviewer
#7 lore raw

PATCH v2 3/3 scsi: ufs: Simplify ufshcd_execute_start_stop()

In reply to Bart Van Assche

Use scsi_execute_cmd() instead of open-coding it.

Cc Mike Christie <[email protected]>
Cc John Garry <[email protected]>
---
 drivers/ufs/core/ufshcd.c | 36 ++++++++----------------------------
 1 file changed, 8 insertions(+), 28 deletions(-)
drivers/ufs/core/ufshcd.c +8 -28
index 3512dcc152c8..cb59f6b55cfd 100644
@@ -9125,35 +9125,15 @@ static int ufshcd_execute_start_stop(struct scsi_device *sdev,
 				     enum ufs_dev_pwr_mode pwr_mode,
 				     struct scsi_sense_hdr *sshdr)
 {
-	unsigned char cdb[6] = { START_STOP, 0, 0, 0, pwr_mode << 4, 0 };
-	struct request *req;
-	struct scsi_cmnd *scmd;
-	int ret;
-
-	req = scsi_alloc_request(sdev->request_queue, REQ_OP_DRV_IN,
-				 BLK_MQ_REQ_PM);
-	if (IS_ERR(req))
-		return PTR_ERR(req);
-
-	scmd = blk_mq_rq_to_pdu(req);
-	scmd->cmd_len = COMMAND_SIZE(cdb[0]);
-	memcpy(scmd->cmnd, cdb, scmd->cmd_len);
-	scmd->allowed = 0/*retries*/;
-	scmd->flags |= SCMD_FAIL_IF_RECOVERING;
-	req->timeout = 1 * HZ;
-	req->rq_flags |= RQF_QUIET;
-	WARN_ON_ONCE(!(req->rq_flags & RQF_PM));
-
-	blk_execute_rq(req, /*at_head=*/true);
-
-	if (sshdr)
-		scsi_normalize_sense(scmd->sense_buffer, scmd->sense_len,
-				     sshdr);
-	ret = scmd->result;
-
-	blk_mq_free_request(req);
+	const unsigned char cdb[6] = { START_STOP, 0, 0, 0, pwr_mode << 4, 0 };
+	const struct scsi_exec_args args = {
+		.sshdr = sshdr,
+		.req_flags = BLK_MQ_REQ_PM,
+		.scmd_flags = SCMD_FAIL_IF_RECOVERING,
+	};
 
-	return ret;
+	return scsi_execute_cmd(sdev, cdb, REQ_OP_DRV_IN, /*buffer=*/NULL,
+			/*bufflen=*/0, /*timeout=*/HZ, /*retries=*/0, &args);
 }
 
 /**