linux-scsi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bart Van Assche <bvanassche@acm.org>
To: "Martin K . Petersen" <martin.petersen@oracle.com>
Cc: linux-scsi@vger.kernel.org, Bart Van Assche <bvanassche@acm.org>,
	Hannes Reinecke <hare@suse.de>,
	John Garry <john.g.garry@oracle.com>,
	"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>
Subject: [PATCH v6 06/28] scsi: core: Add scsi_{get,put}_internal_cmd() helpers
Date: Tue, 14 Oct 2025 13:15:48 -0700	[thread overview]
Message-ID: <20251014201707.3396650-7-bvanassche@acm.org> (raw)
In-Reply-To: <20251014201707.3396650-1-bvanassche@acm.org>

From: Hannes Reinecke <hare@suse.de>

Add helper functions to allow LLDDs to allocate and free internal commands.

Reviewed-by: John Garry <john.g.garry@oracle.com>
Signed-off-by: Hannes Reinecke <hare@suse.de>
[ bvanassche: changed the 'nowait' argument into a 'flags' argument /
  added scsi_get_internal_cmd_hctx(). See also
  https://lore.kernel.org/linux-scsi/20211125151048.103910-3-hare@suse.de/ ]
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/scsi/scsi_lib.c    | 79 ++++++++++++++++++++++++++++++++++++++
 include/scsi/scsi_device.h |  7 ++++
 2 files changed, 86 insertions(+)

diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index d4e874bbf2ea..9b1a254f92ba 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -1247,6 +1247,17 @@ struct request *scsi_alloc_request(struct request_queue *q, blk_opf_t opf,
 }
 EXPORT_SYMBOL_GPL(scsi_alloc_request);
 
+static struct request *scsi_alloc_request_hctx(struct request_queue *q,
+		blk_opf_t opf, blk_mq_req_flags_t flags, unsigned int hctx_idx)
+{
+	struct request *rq;
+
+	rq = blk_mq_alloc_request_hctx(q, opf, flags, hctx_idx);
+	if (!IS_ERR(rq))
+		scsi_initialize_rq(rq);
+	return rq;
+}
+
 /*
  * Only called when the request isn't completed by SCSI, and not freed by
  * SCSI
@@ -2134,6 +2145,74 @@ void scsi_mq_free_tags(struct kref *kref)
 	complete(&shost->tagset_freed);
 }
 
+/**
+ * scsi_get_internal_cmd() - Allocate an internal SCSI command.
+ * @sdev: SCSI device from which to allocate the command
+ * @data_direction: Data direction for the allocated command
+ * @flags: request allocation flags, e.g. BLK_MQ_REQ_RESERVED or
+ *	BLK_MQ_REQ_NOWAIT.
+ *
+ * Allocates a SCSI command for internal LLDD use.
+ */
+struct scsi_cmnd *scsi_get_internal_cmd(struct scsi_device *sdev,
+					enum dma_data_direction data_direction,
+					blk_mq_req_flags_t flags)
+{
+	enum req_op op = data_direction == DMA_TO_DEVICE ? REQ_OP_DRV_OUT :
+							   REQ_OP_DRV_IN;
+	struct scsi_cmnd *scmd;
+	struct request *rq;
+
+	rq = scsi_alloc_request(sdev->request_queue, op, flags);
+	if (IS_ERR(rq))
+		return NULL;
+	scmd = blk_mq_rq_to_pdu(rq);
+	scmd->device = sdev;
+
+	return scmd;
+}
+EXPORT_SYMBOL_GPL(scsi_get_internal_cmd);
+
+/**
+ * scsi_get_internal_cmd_hctx() - Allocate an internal SCSI command from a given
+ *	hardware queue.
+ * @sdev: SCSI device from which to allocate the command
+ * @data_direction: Data direction for the allocated command
+ * @flags: request allocation flags, e.g. BLK_MQ_REQ_RESERVED or
+ *	BLK_MQ_REQ_NOWAIT.
+ * @hctx_idx: Hardware queue index.
+ *
+ * Allocates a SCSI command for internal LLDD use.
+ */
+struct scsi_cmnd *scsi_get_internal_cmd_hctx(struct scsi_device *sdev,
+			enum dma_data_direction data_direction,
+			blk_mq_req_flags_t flags, unsigned int hctx_idx)
+{
+	enum req_op op = data_direction == DMA_TO_DEVICE ? REQ_OP_DRV_OUT :
+							   REQ_OP_DRV_IN;
+	struct scsi_cmnd *scmd;
+	struct request *rq;
+
+	rq = scsi_alloc_request_hctx(sdev->request_queue, op, flags, hctx_idx);
+	if (IS_ERR(rq))
+		return NULL;
+	scmd = blk_mq_rq_to_pdu(rq);
+	scmd->device = sdev;
+
+	return scmd;
+}
+EXPORT_SYMBOL_GPL(scsi_get_internal_cmd_hctx);
+
+/**
+ * scsi_put_internal_cmd() - Free an internal SCSI command.
+ * @scmd: SCSI command to be freed
+ */
+void scsi_put_internal_cmd(struct scsi_cmnd *scmd)
+{
+	blk_mq_free_request(blk_mq_rq_from_pdu(scmd));
+}
+EXPORT_SYMBOL_GPL(scsi_put_internal_cmd);
+
 /**
  * scsi_device_from_queue - return sdev associated with a request_queue
  * @q: The request queue to return the sdev from
diff --git a/include/scsi/scsi_device.h b/include/scsi/scsi_device.h
index 3846f5dfc51c..1d83b17d95e6 100644
--- a/include/scsi/scsi_device.h
+++ b/include/scsi/scsi_device.h
@@ -558,6 +558,13 @@ int scsi_execute_cmd(struct scsi_device *sdev, const unsigned char *cmd,
 		     const struct scsi_exec_args *args);
 void scsi_failures_reset_retries(struct scsi_failures *failures);
 
+struct scsi_cmnd *scsi_get_internal_cmd(struct scsi_device *sdev,
+					enum dma_data_direction data_direction,
+					blk_mq_req_flags_t flags);
+struct scsi_cmnd *scsi_get_internal_cmd_hctx(struct scsi_device *sdev,
+			enum dma_data_direction data_direction,
+			blk_mq_req_flags_t flags, unsigned int hctx_idx);
+void scsi_put_internal_cmd(struct scsi_cmnd *scmd);
 extern void sdev_disable_disk_events(struct scsi_device *sdev);
 extern void sdev_enable_disk_events(struct scsi_device *sdev);
 extern int scsi_vpd_lun_id(struct scsi_device *, char *, size_t);

  parent reply	other threads:[~2025-10-14 20:18 UTC|newest]

Thread overview: 37+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-14 20:15 [PATCH v6 00/28] Optimize the hot path in the UFS driver Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 01/28] scsi: core: Support allocating reserved commands Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 02/28] scsi: core: Move two statements Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 03/28] scsi: core: Make the budget map optional Bart Van Assche
2025-10-16  6:06   ` Hannes Reinecke
2025-10-22  8:29   ` John Garry
2025-10-22 17:23     ` Bart Van Assche
2025-10-23 10:02       ` John Garry
2025-10-14 20:15 ` [PATCH v6 04/28] scsi: core: Support allocating a pseudo SCSI device Bart Van Assche
2025-10-20 11:10   ` John Garry
2025-10-20 16:10     ` Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 05/28] scsi: core: Introduce .queue_reserved_command() Bart Van Assche
2025-10-14 20:15 ` Bart Van Assche [this message]
2025-10-14 20:15 ` [PATCH v6 07/28] scsi_debug: Abort SCSI commands via an internal command Bart Van Assche
2025-10-22  8:46   ` John Garry
2025-10-23 16:19     ` Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 08/28] ufs: core: Move an assignment in ufshcd_mcq_process_cqe() Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 09/28] ufs: core: Change the type of one ufshcd_add_cmd_upiu_trace() argument Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 10/28] ufs: core: Only call ufshcd_add_command_trace() for SCSI commands Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 11/28] ufs: core: Change the type of one ufshcd_add_command_trace() argument Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 12/28] ufs: core: Change the type of one ufshcd_send_command() argument Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 13/28] ufs: core: Only call ufshcd_should_inform_monitor() for SCSI commands Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 14/28] ufs: core: Change the monitor function argument types Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 15/28] ufs: core: Rework ufshcd_mcq_compl_pending_transfer() Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 16/28] ufs: core: Rework ufshcd_eh_device_reset_handler() Bart Van Assche
2025-10-14 20:15 ` [PATCH v6 17/28] ufs: core: Rework the SCSI host queue depth calculation code Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 18/28] ufs: core: Allocate the SCSI host earlier Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 19/28] ufs: core: Call ufshcd_init_lrb() later Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 20/28] ufs: core: Use hba->reserved_slot Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 21/28] ufs: core: Make the reserved slot a reserved request Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 22/28] ufs: core: Do not clear driver-private command data Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 23/28] ufs: core: Optimize the hot path Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 24/28] ufs: core: Pass a SCSI pointer instead of an LRB pointer Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 25/28] ufs: core: Remove the ufshcd_lrb task_tag member Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 26/28] ufs: core: Make blk_mq_tagset_busy_iter() skip reserved requests Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 27/28] ufs: core: Move code out of ufshcd_wait_for_dev_cmd() Bart Van Assche
2025-10-14 20:16 ` [PATCH v6 28/28] ufs: core: Switch to scsi_get_internal_cmd() Bart Van Assche

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251014201707.3396650-7-bvanassche@acm.org \
    --to=bvanassche@acm.org \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=hare@suse.de \
    --cc=john.g.garry@oracle.com \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).