Simplify and optimize the UFS driver
v2 cover Simplify and optimize the UFS driver
Hi Avri,
As promised, this is v2 of the patch series that simplifies and optimizes the UFS driver. These patches are entirely untested. Any feedback is welcome.
Thanks,
Bart.
Bart Van Assche (5):
Allow SCSI LLDs to reserve block layer tags
ufs: Use reserved tags for TMFs
ufs: Avoid busy-waiting by eliminating tag conflicts
ufs: Use blk_{get,put}_request() to allocate and free TMFs
ufs: Simplify the clock scaling mechanism implementation
drivers/scsi/scsi_lib.c | 1 + drivers/scsi/ufs/ufshcd.c | 349 +++++++++++++++----------------------- drivers/scsi/ufs/ufshcd.h | 21 +-- include/scsi/scsi_host.h | 2 + 4 files changed, 142 insertions(+), 231 deletions(-)
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
RFC PATCH v2 1/5 Allow SCSI LLDs to reserve block layer tags
blk_get_request() allocates a reserved tag if BLK_MQ_REQ_RESERVED has been set in its third argument and a regular tag if that flag has not been set. Make it possible for SCSI LLDs to mark a subset of the blk-mq tags as 'reserved'.
--- drivers/scsi/scsi_lib.c | 1 + include/scsi/scsi_host.h | 2 ++ 2 files changed, 3 insertions(+)
drivers/scsi/scsi_lib.c +1 -0
@@ -1894,6 +1894,7 @@ int scsi_mq_setup_tags(struct Scsi_Host *shost) shost->tag_set.ops = &scsi_mq_ops_no_commit; shost->tag_set.nr_hw_queues = shost->nr_hw_queues ? :1 ; shost->tag_set.queue_depth = shost->can_queue; + shost->tag_set.reserved_tags = shost->reserved_tags; shost->tag_set.cmd_size = cmd_size; shost->tag_set.numa_node =NUMA_NO_NODE ; shost->tag_set.flags =BLK_MQ_F_SHOULD_MERGE ;
include/scsi/scsi_host.h +2 -0
@@ -612,6 +612,8 @@ struct Scsi_Host {* is nr_hw_queues * can_queue. */ unsigned nr_hw_queues; +/* Number of tags that blk_mq_init_queue() should consider reserved. */ +unsigned reserved_tags; unsigned active_mode:2; unsigned unchecked_isa_dma:1;
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
RFC PATCH v2 2/5 ufs: Use reserved tags for TMFs
In reply to Bart Van AsscheReserved tags are numerically lower than non-reserved tags. Compensate the change caused by reserving tags by subtracting the number of reserved tags from the tag number assigned by the block layer.
--- drivers/scsi/ufs/ufshcd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/scsi/ufs/ufshcd.c +6 -4
@@ -2402,7 +2402,7 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs;if (!ufshcd_valid_tag (hba, tag)) { dev_err(hba->dev, "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
@@ -5965,7 +5965,8 @@ static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) host = cmd->device->host; hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag)); lrbp = &hba->lrb[tag]; err =ufshcd_issue_tm_cmd (hba, lrbp->lun,0 ,UFS_LOGICAL_RESET , &resp);
@@ -6036,7 +6037,7 @@ static int ufshcd_abort(struct scsi_cmnd *cmd) host = cmd->device->host; hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; lrbp = &hba->lrb[tag];if (!ufshcd_valid_tag (hba, tag)) { dev_err(hba->dev,
@@ -8320,7 +8321,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)/* Configure LRB */ ufshcd_host_memory_configure (hba); - host->can_queue = hba->nutrs; + host->can_queue = hba->nutrs + hba->nutmrs; + host->reserved_tags = hba->nutmrs; host->cmd_per_lun = hba->nutrs; host->max_id =UFSHCD_MAX_ID ; host->max_lun =UFS_MAX_LUNS ;
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
Re: [PATCH RFC v2 2/5] ufs: Use reserved tags for TMFs
Quoted 3 lines · Bart Van Assche
Reserved tags are numerically lower than non-reserved tags. Compensate the change caused by reserving tags by subtracting the number of reserved tags from the tag number assigned by the block layer.
Why would you do that? Do we really care about the exact tag number? If so would it make sense to reverse in the block layer how we allocate private vs normal tags?
Also this change should probably merged into the patch that actually starts using the private tags by actually allocating requests using them.
Re: [PATCH RFC v2 2/5] ufs: Use reserved tags for TMFs
Quoted 5 lines · Christoph Hellwig
Quoted 3 lines · Bart Van Assche
Reserved tags are numerically lower than non-reserved tags. Compensate the change caused by reserving tags by subtracting the number of reserved tags from the tag number assigned by the block layer.
Quoted 7 lines
Why would you do that? Do we really care about the exact tag number? If so would it make sense to reverse in the block layer how we allocate private vs normal tags?
Also this change should probably merged into the patch that actually starts using the private tags by actually allocating requests using them.
Hi Christoph,
The UFS driver writes the actual tags into doorbell registers. There are two such doorbell registers: one for regular commands and one for task management functions. Both doorbell registers are bitmasks that start from bit zero. So I don't see how to avoid this kind of tag conversions? From the UFS driver, for regular commands:
ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
And for TMFs:
ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Thanks,
Bart.
RE: [EXT] [PATCH RFC v2 2/5] ufs: Use reserved tags for TMFs
In reply to Bart Van AsscheHi, Bart
Quoted 32 lines
Reserved tags are numerically lower than non-reserved tags. Compensate the change caused by reserving tags by subtracting the number of reserved tags from the tag number assigned by the block layer.
Cc Yaniv Gardi <[email protected]>Cc Subhash Jadavani <[email protected]>Cc Stanley Chu <[email protected]>Cc Avri Altman <[email protected]>Cc Tomas Winkler <[email protected]>Signed-off-by Bart Van Assche <[email protected]>--- drivers/scsi/ufs/ufshcd.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)drivers/scsi/ufs/ufshcd.c index renamed from drivers/scsi/ufs/ufshcd.c +0
-0
9fc05a535624..3e3c6257a343100644drivers/scsi/ufs/ufshcd.c +1
-1@@ -2402,7 +2402,7 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs;if (!ufshcd_valid_tag (hba, tag)) { dev_err(hba->dev,"%s: invalid command tag %d: cmd=0x%p, cmd-
Quoted 1 line
request=0x%p", @@ -5965,7 +5965,8 @@ static int
ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
drivers/scsi/ufs/ufshcd.c static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) · line 5969
Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +2
-1host = cmd->device->host; hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag));
Changing request tag number here is not proper way, we have trace tool using this tag to track request from block, SCSI to UFS layer. If tags being changed in UFS driver, there will be a confusion.
Quoted 13 lines
drivers/scsi/ufs/ufshcd.c +0
-0lrbp = &hba->lrb[tag]; err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET,&resp); @@ -6036,7 +6037,7 @@ static int
ufshcd_abort(struct scsi_cmnd *cmd)drivers/scsi/ufs/ufshcd.c +1
-1host = cmd->device->host; hba =shost_priv (host); - tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; lrbp = &hba->lrb[tag];if (!ufshcd_valid_tag (hba, tag)) { dev_err(hba->dev,
drivers/scsi/ufs/ufshcd.c int ufshcd_init(struct ufs_hba *hba, void __iomem · line 8326
Quoted 7 lines
drivers/scsi/ufs/ufshcd.c +2
-1@@ -8320,7 +8321,8 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)/* Configure LRB */ ufshcd_host_memory_configure (hba ); - host->can_queue = hba->nutrs; + host->can_queue = hba->nutrs + hba->nutmrs; + host->reserved_tags = hba->nutmrs;
hba->nutmrs is for task management, relevant door-bell register is bit0-bit7 validate.
If my understanding is correct, UFS task management requests normally issued by UFS layer, rather than upper layers. Before issuing task management request, UFS driver layer will apply a free slot, see ufshcd_get_tm_free_slot(). I don't think it is necessary to change host->can_queue size by adding task management depth hba->nutmrs.
Quoted 5 lines
drivers/scsi/ufs/ufshcd.c +0
-0host->cmd_per_lun = hba->nutrs; host->max_id =UFSHCD_MAX_ID ;
host->max_lun=UFS_MAX_LUNS;
--
2.24.0.rc1.363.gb1bccd3e3d-goog
//Bean
Re: [EXT] [PATCH RFC v2 2/5] ufs: Use reserved tags for TMFs
Quoted 8 lines · Bean Huo (beanhuo)
drivers/scsi/ufs/ufshcd.c static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) · line 5969
Quoted 5 lines
host = cmd->device->host; hba = shost_priv(host);drivers/scsi/ufs/ufshcd.c +2
-1- tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag));Changing request tag number here is not proper way, we have trace tool using this tag to track request from block, SCSI to UFS layer. If tags being changed in UFS driver, there will be a confusion.
Hi Bean,
Thanks for having taken a look. Which information is used by the tracing tool? cmd->request->tag or the variable called 'tag' above? The latter should not be modified by this patch. cmd->request->tag is modified however for every command that is not a TMF. Preserving the block layer tag value is possible but would require to introduce a new tag set for TMFs.
Thanks,
Bart.
RE: [EXT] [PATCH RFC v2 2/5] ufs: Use reserved tags for TMFs
Quoted 20 lines · Bean Huo (beanhuo)
Quoted 8 lines
drivers/scsi/ufs/ufshcd.c static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) · line 5969
Quoted 5 lines
host = cmd->device->host; hba = shost_priv(host);drivers/scsi/ufs/ufshcd.c +2
-1- tag = cmd->request->tag; + tag = cmd->request->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag));Changing request tag number here is not proper way, we have trace tool using this tag to track request from block, SCSI to UFS layer. If tags being
changed in UFS driver, there will be a confusion.
Hi Bean,
Thanks for having taken a look. Which information is used by the tracing tool?
cmd->request->tagor the variable called 'tag' above? The latter should not be modified by this patch.cmd->request->tagis modified however for every command that is not a TMF. Preserving the block layer tag value is possible but would require to introduce a new tag set for TMFs.
Quoted 3 lines
Thanks,
Bart.
Hi, Bart
We are using latter variable 'tag'.
What I concern is that cmd->request->tag and variable tag(UFS driver uses this variable tag) are consistent without this patch, and now we should change our tag tracking handling mechanism. But it is not big deal, we can plus hba->nutmrs subtracted before in order to in line with block/scsi tag.
Thanks,
//Bean
RFC PATCH v2 3/5 ufs: Avoid busy-waiting by eliminating tag conflicts
In reply to Bart Van AsscheInstead of tracking which tags are in use in the ufs_hba.lrb_in_use bitmask, rely on the block layer tag allocation mechanism. This patch removes the following busy-waiting loop if ufshcd_issue_devman_upiu_cmd() and the block layer accidentally allocate the same tag for a SCSI request:
* ufshcd_queuecommand() returns SCSI_MLQUEUE_HOST_BUSY.
* The SCSI core requeues the SCSI command.
--- drivers/scsi/ufs/ufshcd.c | 124 ++++++++++++++++---------------------- drivers/scsi/ufs/ufshcd.h | 9 +-- 2 files changed, 57 insertions(+), 76 deletions(-)
drivers/scsi/ufs/ufshcd.c +52 -72
@@ -497,8 +497,8 @@ static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)static void ufshcd_print_host_state (struct ufs_hba *hba) {dev_err (hba->dev,"UFS Host state=%d\n" , hba->ufshcd_state); -dev_err (hba->dev,"lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n" , - hba->lrb_in_use, hba->outstanding_reqs, hba->outstanding_tasks); +dev_err (hba->dev,"outstanding reqs=0x%lx tasks=0x%lx\n" , + hba->outstanding_reqs, hba->outstanding_tasks);dev_err (hba->dev,"saved_err=0x%x, saved_uic_err=0x%x\n" , hba->saved_err, hba->saved_uic_err); dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
@@ -1596,6 +1596,29 @@ int ufshcd_hold(struct ufs_hba *hba, bool async) }EXPORT_SYMBOL_GPL (ufshcd_hold); +static bool ufshcd_is_busy (struct request *req,void *priv,bool reserved) +{ +int *busy = priv; + +if (reserved) +return true; + (*busy)++; +return false; +} + +/* +* Whether or not any non-reserved tag is in use by a request that is in +* progress. +*/ +static bool ufshcd_any_tag_in_use (struct ufs_hba *hba) +{ +struct request_queue *q = hba->tag_alloc_queue; +int busy =0 ; + +blk_mq_tagset_busy_iter (q->tag_set, ufshcd_is_busy, &busy); +return busy; +} +static void ufshcd_gate_work (struct work_struct *work) { struct ufs_hba *hba = container_of(work, struct ufs_hba,
@@ -1619,7 +1642,7 @@ static void ufshcd_gate_work(struct work_struct *work)if (hba->clk_gating.active_reqs || hba->ufshcd_state !=UFSHCD_STATE_OPERATIONAL - || hba->lrb_in_use || hba->outstanding_tasks + ||ufshcd_any_tag_in_use (hba) || hba->outstanding_tasks || hba->active_uic_cmd || hba->uic_async_done) goto rel_lock;
@@ -1673,7 +1696,7 @@ static void __ufshcd_release(struct ufs_hba *hba)if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended || hba->ufshcd_state !=UFSHCD_STATE_OPERATIONAL - || hba->lrb_in_use || hba->outstanding_tasks + ||ufshcd_any_tag_in_use (hba) || hba->outstanding_tasks || hba->active_uic_cmd || hba->uic_async_done ||ufshcd_eh_in_progress (hba))return ;
@@ -2443,22 +2466,9 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) hba->req_abort_count =0 ; -/* acquire the tag to make sure device cmds don't use it */ -if (test_and_set_bit_lock (tag, &hba->lrb_in_use)) { -/* -* Dev manage command in progress, requeue the command. -* Requeuing the command helps in cases where the request *may* -* find different tag instead of waiting for dev manage command -* completion. -*/ - err =SCSI_MLQUEUE_HOST_BUSY ; - goto out; - } - err =ufshcd_hold (hba, true);if (err) { err =SCSI_MLQUEUE_HOST_BUSY ; -clear_bit_unlock (tag, &hba->lrb_in_use); goto out; }WARN_ON (hba->clk_gating.state !=CLKS_ON );
@@ -2479,7 +2489,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) err =ufshcd_map_sg (hba, lrbp);if (err) { lrbp->cmd =NULL ; -clear_bit_unlock (tag, &hba->lrb_in_use); goto out; }/* Make sure descriptors are ready before ringing the doorbell */
@@ -2625,44 +2634,6 @@ static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,return err; } -/** -* ufshcd_get_dev_cmd_tag - Get device management command tag -* @hba: per-adapter instance -* @tag_out: pointer to variable with available slot value -* -* Get a free slot and lock it until device management command -* completes. -* -* Returns false if free slot is unavailable for locking, else -* return true with tag value in @tag. -*/ -static bool ufshcd_get_dev_cmd_tag (struct ufs_hba *hba,int *tag_out) -{ -int tag; -bool ret = false; -unsigned long tmp; - -if (!tag_out) - goto out; - -do { - tmp = ~hba->lrb_in_use; - tag =find_last_bit (&tmp, hba->nutrs); -if (tag >= hba->nutrs) - goto out; - }while (test_and_set_bit_lock (tag, &hba->lrb_in_use)); - - *tag_out = tag; - ret = true; -out: -return ret; -} - -static inline void ufshcd_put_dev_cmd_tag (struct ufs_hba *hba,int tag) -{ -clear_bit_unlock (tag, &hba->lrb_in_use); -} -/** * ufshcd_exec_dev_cmd - API for sending device management requests * @hba: UFS hba
@@ -2675,6 +2646,8 @@ static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)static int ufshcd_exec_dev_cmd (struct ufs_hba *hba,enum dev_cmd_type cmd_type,int timeout) { +struct request_queue *q = hba->tag_alloc_queue; +struct request *req;struct ufshcd_lrb *lrbp;int err;int tag;
@@ -2688,7 +2661,11 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,* Even though we use wait_event() which sleeps indefinitely, * the maximum wait time is bounded by SCSI request timeout. */ -wait_event (hba->dev_cmd.tag_wq,ufshcd_get_dev_cmd_tag (hba, &tag)); + req =blk_get_request (q,REQ_OP_DRV_OUT ,0 ); +if (IS_ERR (req)) +return PTR_ERR (req); + tag = req->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag));init_completion (&wait); lrbp = &hba->lrb[tag];
@@ -2712,8 +2689,7 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, err ? "query_complete_err" : "query_complete"); out_put_tag: -ufshcd_put_dev_cmd_tag (hba, tag); -wake_up (&hba->dev_cmd.tag_wq); +blk_put_request (req);up_read (&hba->clk_scaling_lock);return err; }
@@ -4832,7 +4808,6 @@ static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, cmd->result = result;/* Mark completed command as NULL in LRB */ lrbp->cmd =NULL ; -clear_bit_unlock (index, &hba->lrb_in_use);/* Do not touch lrbp after scsi done */ cmd->scsi_done (cmd);__ufshcd_release (hba);
@@ -4854,9 +4829,6 @@ static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, hba->outstanding_reqs ^= completed_reqs;ufshcd_clk_scaling_update_busy (hba); - -/* we might have free'd some tags above */ -wake_up (&hba->dev_cmd.tag_wq); }/**
@@ -5785,6 +5757,8 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,enum dev_cmd_type cmd_type, enum query_opcode desc_op) { + struct request_queue *q = hba->tag_alloc_queue; +struct request *req;struct ufshcd_lrb *lrbp;int err =0 ;int tag;
@@ -5794,7 +5768,11 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,down_read (&hba->clk_scaling_lock); -wait_event (hba->dev_cmd.tag_wq,ufshcd_get_dev_cmd_tag (hba, &tag)); + req =blk_get_request (q,REQ_OP_DRV_OUT ,0 ); +if (IS_ERR (req)) +return PTR_ERR (req); + tag = req->tag - hba->nutmrs; +WARN_ON_ONCE (!ufshcd_valid_tag (hba, tag));init_completion (&wait); lrbp = &hba->lrb[tag];
@@ -5868,8 +5846,7 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, } } -ufshcd_put_dev_cmd_tag (hba, tag); -wake_up (&hba->dev_cmd.tag_wq); +blk_put_request (req);up_read (&hba->clk_scaling_lock);return err; }
@@ -6165,9 +6142,6 @@ static int ufshcd_abort(struct scsi_cmnd *cmd) hba->lrb[tag].cmd =NULL ;spin_unlock_irqrestore (host->host_lock, flags); -clear_bit_unlock (tag, &hba->lrb_in_use); -wake_up (&hba->dev_cmd.tag_wq); - out:if (!err) { err =SUCCESS ;
@@ -6874,6 +6848,11 @@ static int ufshcd_probe_hba(struct ufs_hba *hba)int ret;ktime_t start =ktime_get (); + ret = -ENOMEM ; + hba->tag_alloc_queue =blk_mq_init_queue (&hba->host->tag_set); +if (!hba->tag_alloc_queue) + goto out; + ret =ufshcd_link_startup (hba);if (ret) goto out;
@@ -7506,6 +7485,10 @@ static void ufshcd_hba_exit(struct ufs_hba *hba)ufshcd_setup_hba_vreg (hba, false); hba->is_powered = false; } + +if (hba->tag_alloc_queue) +blk_cleanup_queue (hba->tag_alloc_queue); + hba->tag_alloc_queue =NULL ; } static int
@@ -8348,9 +8331,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)init_rwsem (&hba->clk_scaling_lock); -/* Initialize device management tag acquire wait queue */ -init_waitqueue_head (&hba->dev_cmd.tag_wq); -ufshcd_init_clk_gating (hba);ufshcd_init_clk_scaling (hba);
drivers/scsi/ufs/ufshcd.h +5 -4
@@ -212,13 +212,11 @@ struct ufs_query {* @type: device management command type - Query, NOP OUT * @lock: lock to allow one command at a time * @complete: internal commands completion -* @tag_wq: wait queue until free command slot is available */ struct ufs_dev_cmd {enum dev_cmd_type type;struct mutex lock;struct completion *complete; -wait_queue_head_t tag_wq;struct ufs_query query; };
@@ -480,7 +478,10 @@ struct ufs_stats {* @host: Scsi_Host instance of the driver * @dev: device handle * @lrb: local reference block -* @lrb_in_use: lrb in use +* @tag_alloc_queue: None of the exported block layer functions allows to +* allocate a tag directly from a tag set. The purpose of this request queue +* is to support allocating tags from hba->host->tag_set before any LUNs have +* been associated with this HBA. * @outstanding_tasks: Bits representing outstanding task requests * @outstanding_reqs: Bits representing outstanding transfer requests * @capabilities: UFS Controller Capabilities
@@ -538,6 +539,7 @@ struct ufs_hba {struct Scsi_Host *host;struct device *dev; +struct request_queue *tag_alloc_queue;/* * This field is to keep a reference to "scsi_device" corresponding to * "UFS device" W-LU.
@@ -558,7 +560,6 @@ struct ufs_hba {u32 ahit;struct ufshcd_lrb *lrb; -unsigned long lrb_in_use;unsigned long outstanding_tasks;unsigned long outstanding_reqs;
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
RFC PATCH v2 4/5 ufs: Use blk_{get,put}_request() to allocate and free TMFs
In reply to Bart Van AsscheManage TMF tags with blk_{get,put}_request() instead of ufshcd_get_tm_free_slot() / ufshcd_put_tm_slot(). Store a per-request completion pointer in request.end_io_data instead of using a waitqueue to report TMF completion.
--- drivers/scsi/ufs/ufshcd.c | 84 +++++++++++++++++---------------------- drivers/scsi/ufs/ufshcd.h | 9 ----- 2 files changed, 36 insertions(+), 57 deletions(-)
drivers/scsi/ufs/ufshcd.c +36 -48
@@ -645,40 +645,6 @@ static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)return le32_to_cpu (lrbp->utr_descriptor_ptr->header.dword_2) &MASK_OCS ; } -/** -* ufshcd_get_tm_free_slot - get a free slot for task management request -* @hba: per adapter instance -* @free_slot: pointer to variable with available slot value -* -* Get a free tag and lock it until ufshcd_put_tm_slot() is called. -* Returns 0 if free slot is not available, else return 1 with tag value -* in @free_slot. -*/ -static bool ufshcd_get_tm_free_slot (struct ufs_hba *hba,int *free_slot) -{ -int tag; -bool ret = false; - -if (!free_slot) - goto out; - -do { - tag =find_first_zero_bit (&hba->tm_slots_in_use, hba->nutmrs); -if (tag >= hba->nutmrs) - goto out; - }while (test_and_set_bit_lock (tag, &hba->tm_slots_in_use)); - - *free_slot = tag; - ret = true; -out: -return ret; -} - -static inline void ufshcd_put_tm_slot (struct ufs_hba *hba,int slot) -{ -clear_bit_unlock (slot, &hba->tm_slots_in_use); -} -/** * ufshcd_utrl_clear - Clear a bit in UTRLCLR register * @hba: per adapter instance
@@ -5519,17 +5485,35 @@ static void ufshcd_check_errors(struct ufs_hba *hba)*/ } +struct ctm_info { +struct ufs_hba *hba; +unsigned long pending; +}; + +static bool ufshcd_compl_tm (struct request *req,void *priv,bool reserved) +{ +const struct ctm_info *const ci = priv; +struct completion *c; + +if (!reserved ||test_bit (req->tag, &ci->pending)) +return true; + c = req->end_io_data; +if (c) +complete (c); +return true; +} +/** * ufshcd_tmc_handler - handle task management function completion * @hba: per adapter instance */ static void ufshcd_tmc_handler (struct ufs_hba *hba) { -u32 tm_doorbell; +struct request_queue *q = hba->tag_alloc_queue; +struct ctm_info ci = { .hba = hba }; - tm_doorbell =ufshcd_readl (hba,REG_UTP_TASK_REQ_DOOR_BELL ); - hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; -wake_up (&hba->tm_wq); + ci.pending =ufshcd_readl (hba,REG_UTP_TASK_REQ_DOOR_BELL ); +blk_mq_tagset_busy_iter (q->tag_set, ufshcd_compl_tm, &ci); }/**
@@ -5622,7 +5606,10 @@ static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)static int __ufshcd_issue_tm_cmd (struct ufs_hba *hba,struct utp_task_req_desc *treq,u8 tm_function) { +struct request_queue *q = hba->tag_alloc_queue;struct Scsi_Host *host = hba->host; +DECLARE_COMPLETION_ONSTACK (wait); +struct request *req;unsigned long flags;int free_slot, task_tag, err;
@@ -5631,7 +5618,10 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,* Even though we use wait_event() which sleeps indefinitely, * the maximum wait time is bounded by %TM_CMD_TIMEOUT. */ -wait_event (hba->tm_tag_wq,ufshcd_get_tm_free_slot (hba, &free_slot)); + req =blk_get_request (q,REQ_OP_DRV_OUT ,BLK_MQ_REQ_RESERVED ); + req->end_io_data = &wait; + free_slot = req->tag; +WARN_ON_ONCE (free_slot <0 || free_slot >= hba->nutmrs);ufshcd_hold (hba, false);spin_lock_irqsave (host->host_lock, flags);
@@ -5657,10 +5647,14 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,ufshcd_add_tm_upiu_trace (hba, task_tag,"tm_send" );/* wait until the task management command is completed */ - err =wait_event_timeout (hba->tm_wq, -test_bit (free_slot, &hba->tm_condition), + err =wait_for_completion_io_timeout (&wait,msecs_to_jiffies (TM_CMD_TIMEOUT ));if (!err) { +/* +* Make sure that ufshcd_compl_tm() does not trigger a +* use-after-free. +*/ + req->end_io_data =NULL ;ufshcd_add_tm_upiu_trace (hba, task_tag,"tm_complete_err" );dev_err (hba->dev,"%s: task management cmd 0x%.2x timed-out\n" , __func__, tm_function);
@@ -5679,9 +5673,7 @@ static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,__clear_bit (free_slot, &hba->outstanding_tasks);spin_unlock_irqrestore (hba->host->host_lock, flags); -clear_bit (free_slot, &hba->tm_condition); -ufshcd_put_tm_slot (hba, free_slot); -wake_up (&hba->tm_tag_wq); +blk_put_request (req);ufshcd_release (hba);return err;
@@ -8315,10 +8307,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) hba->max_pwr_info.is_valid = false; -/* Initailize wait queue for task management */ -init_waitqueue_head (&hba->tm_wq); -init_waitqueue_head (&hba->tm_tag_wq); -/* Initialize work queues */ INIT_WORK (&hba->eh_work, ufshcd_err_handler);INIT_WORK (&hba->eeh_work, ufshcd_exception_event_handler);
drivers/scsi/ufs/ufshcd.h +0 -9
@@ -493,11 +493,7 @@ struct ufs_stats {* @irq: Irq number of the controller * @active_uic_cmd: handle of active UIC command * @uic_cmd_mutex: mutex for uic command -* @tm_wq: wait queue for task management -* @tm_tag_wq: wait queue for free task management slots -* @tm_slots_in_use: bit map of task management request slots in use * @pwr_done: completion for power mode change -* @tm_condition: condition variable for task management * @ufshcd_state: UFSHCD states * @eh_flags: Error handling flags * @intr_mask: Interrupt Mask Bits
@@ -641,11 +637,6 @@ struct ufs_hba {/* Device deviations from standard UFS device spec. */ unsigned int dev_quirks; -wait_queue_head_t tm_wq; -wait_queue_head_t tm_tag_wq; -unsigned long tm_condition; -unsigned long tm_slots_in_use; -struct uic_command *active_uic_cmd;struct mutex uic_cmd_mutex;struct completion *uic_async_done;
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog
RE: [EXT] [PATCH RFC v2 4/5] ufs: Use blk_{get,put}_request() to allocate and free TMFs
Hi, Bart
drivers/scsi/ufs/ufshcd.c static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, · line 5625
Quoted 8 lines
(quoted diff) +0
-1- wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba,
&free_slot));drivers/scsi/ufs/ufshcd.c +4
-0+ req =blk_get_request (q,REQ_OP_DRV_OUT ,BLK_MQ_REQ_RESERVED ); + req->end_io_data = &wait; + free_slot = req->tag; +WARN_ON_ONCE (free_slot <0 || free_slot >= hba->nutmrs);ufshcd_hold (hba, false);
Understand now , you delete ufshcd_get_tm_free_slot(). Run a big circle to get a free_slot from reserved tags by calling blk_get_request(). But UFS data transfer queue depth is 32, not 32 + hba->nutmrs. How to make sure we see the tag is consistent across block/scsi/ufs? Thanks,
//Bean
Re: [EXT] [PATCH RFC v2 4/5] ufs: Use blk_{get,put}_request() to allocate and free TMFs
Quoted 10 lines · Bean Huo (beanhuo)
drivers/scsi/ufs/ufshcd.c static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, · line 5625
Quoted 8 lines
(quoted diff) +0
-1- wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba,
&free_slot));drivers/scsi/ufs/ufshcd.c +4
-0+ req =blk_get_request (q,REQ_OP_DRV_OUT ,BLK_MQ_REQ_RESERVED ); + req->end_io_data = &wait; + free_slot = req->tag; +WARN_ON_ONCE (free_slot <0 || free_slot >= hba->nutmrs);ufshcd_hold (hba, false);Understand now , you delete
ufshcd_get_tm_free_slot(). Run a big circle to get afree_slotfrom reserved tags by callingblk_get_request(). But UFS data transfer queue depth is 32, not 32 +hba->nutmrs. How to make sure we see the tag is consistent acrossblock/scsi/ufs?
Hi Bean,
Please have a look at the blk_mq_get_tag() function in the block layer. The implementation of that function makes it clear that the tags with numbers [0 .. nr_reserved) are considered reserved tags and also that the tags with numbers [nr_reserved .. queue_depth) are considered regular tags. In other words, adding hba->nutmrs to can_queue does not increase the queue depth because the same number of tags are considered reserved tags.
Bart.
RE: [EXT] [PATCH RFC v2 4/5] ufs: Use blk_{get,put}_request() to allocate and free TMFs
Quoted 17 lines · Bean Huo (beanhuo)
Quoted 9 lines
drivers/scsi/ufs/ufshcd.c static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, · line 5625
Quoted 8 lines
(quoted diff) +0
-1- wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba,
&free_slot));drivers/scsi/ufs/ufshcd.c +4
-0+ req =blk_get_request (q,REQ_OP_DRV_OUT ,BLK_MQ_REQ_RESERVED ); + req->end_io_data = &wait; + free_slot = req->tag; +WARN_ON_ONCE (free_slot <0 || free_slot >= hba->nutmrs);ufshcd_hold (hba, false);Understand now , you delete
ufshcd_get_tm_free_slot(). Run a big circle toget a
free_slotfrom reserved tags by callingblk_get_request().Quoted 1 line
But UFS data transfer queue depth is 32, not 32 +
hba->nutmrs. How to makesure we see the tag is consistent across
block/scsi/ufs?Hi Bean,
Quoted 8 lines
Please have a look at the
blk_mq_get_tag()function in the block layer. The implementation of that function makes it clear that the tags with numbers [0 ..nr_reserved) are considered reserved tags and also that the tags with numbers [nr_reserved..queue_depth) are considered regular tags. In other words, addinghba->nutmrstocan_queuedoes not increase the queue depth because the same number of tags are considered reserved tags.Bart.
Hi, Bart
Yes, I saw that. And actually, task management requests and regular data transfer Requests will be stored to different queues, and use different door-bell registers. So as you said, to introduce a new tag set for TMFs is better.
Thanks,
//Bean
RFC PATCH v2 5/5 ufs: Simplify the clock scaling mechanism implementation
In reply to Bart Van AsscheScaling the clock is only safe while no commands are in progress. Use blk_mq_{un,}freeze_queue() to block submission of new commands and to wait for ongoing commands to complete.
--- drivers/scsi/ufs/ufshcd.c | 131 ++++++++++++-------------------------- drivers/scsi/ufs/ufshcd.h | 3 - 2 files changed, 40 insertions(+), 94 deletions(-)
drivers/scsi/ufs/ufshcd.c +40 -91
@@ -292,14 +292,46 @@ static inline void ufshcd_disable_irq(struct ufs_hba *hba)static void ufshcd_scsi_unblock_requests (struct ufs_hba *hba) { -if (atomic_dec_and_test (&hba->scsi_block_reqs_cnt)) -scsi_unblock_requests (hba->host); +struct scsi_device *sdev; + +blk_mq_unfreeze_queue (hba->tag_alloc_queue); +shost_for_each_device (sdev, hba->host) +blk_mq_unfreeze_queue (sdev->request_queue); } -static void ufshcd_scsi_block_requests (struct ufs_hba *hba) +static int ufshcd_scsi_block_requests (struct ufs_hba *hba, +unsigned long timeout) { -if (atomic_inc_return (&hba->scsi_block_reqs_cnt) ==1 ) -scsi_block_requests (hba->host); +struct scsi_device *sdev; +unsigned long deadline = jiffies + timeout; +bool success = true; + +if (timeout ==ULONG_MAX ) { +shost_for_each_device (sdev, hba->host) +blk_mq_freeze_queue (sdev->request_queue); +blk_mq_freeze_queue (hba->tag_alloc_queue); +return 0 ; + } + +shost_for_each_device (sdev, hba->host) +blk_freeze_queue_start (sdev->request_queue); +blk_freeze_queue_start (hba->tag_alloc_queue); +if (blk_mq_freeze_queue_wait_timeout (hba->tag_alloc_queue, +max_t (long,0 , deadline - jiffies)) <=0 ) + goto err; +shost_for_each_device (sdev, hba->host) { +if (blk_mq_freeze_queue_wait_timeout (sdev->request_queue, +max_t (long,0 , deadline - jiffies)) <=0 ) { + success = false; +break ; + } + } +if (!success) { +err: +ufshcd_scsi_unblock_requests (hba); +return -ETIMEDOUT ; + } +return 0 ; } static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
@@ -971,65 +1003,6 @@ static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,return false; } -static int ufshcd_wait_for_doorbell_clr (struct ufs_hba *hba, -u64 wait_timeout_us) -{ -unsigned long flags; -int ret =0 ; -u32 tm_doorbell; -u32 tr_doorbell; -bool timeout = false, do_last_check = false; -ktime_t start; - -ufshcd_hold (hba, false); -spin_lock_irqsave (hba->host->host_lock, flags); -/* -* Wait for all the outstanding tasks/transfer requests. -* Verify by checking the doorbell registers are clear. -*/ - start =ktime_get (); -do { -if (hba->ufshcd_state !=UFSHCD_STATE_OPERATIONAL ) { - ret = -EBUSY ; - goto out; - } - - tm_doorbell =ufshcd_readl (hba,REG_UTP_TASK_REQ_DOOR_BELL ); - tr_doorbell =ufshcd_readl (hba,REG_UTP_TRANSFER_REQ_DOOR_BELL ); -if (!tm_doorbell && !tr_doorbell) { - timeout = false; -break ; - }else if (do_last_check) { -break ; - } - -spin_unlock_irqrestore (hba->host->host_lock, flags); -schedule (); -if (ktime_to_us (ktime_sub (ktime_get (), start)) > - wait_timeout_us) { - timeout = true; -/* -* We might have scheduled out for long time so make -* sure to check if doorbells are cleared by this time -* or not. -*/ - do_last_check = true; - } -spin_lock_irqsave (hba->host->host_lock, flags); - }while (tm_doorbell || tr_doorbell); - -if (timeout) { -dev_err (hba->dev, -"%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n" , - __func__, tm_doorbell, tr_doorbell); - ret = -EBUSY ; - } -out: -spin_unlock_irqrestore (hba->host->host_lock, flags); -ufshcd_release (hba); -return ret; -} -/** * ufshcd_scale_gear - scale up/down UFS gear * @hba: per adapter instance
@@ -1079,26 +1052,15 @@ static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)static int ufshcd_clock_scaling_prepare (struct ufs_hba *hba) { -#define DOORBELL_CLR_TOUT_US (1000 * 1000)/* 1 sec */ -int ret =0 ;/* * make sure that there are no outstanding requests when * clock scaling is in progress */ -ufshcd_scsi_block_requests (hba); -down_write (&hba->clk_scaling_lock); -if (ufshcd_wait_for_doorbell_clr (hba,DOORBELL_CLR_TOUT_US )) { - ret = -EBUSY ; -up_write (&hba->clk_scaling_lock); -ufshcd_scsi_unblock_requests (hba); - } - -return ret; +return ufshcd_scsi_block_requests (hba,HZ ); }static void ufshcd_clock_scaling_unprepare (struct ufs_hba *hba) { -up_write (&hba->clk_scaling_lock);ufshcd_scsi_unblock_requests (hba); }
@@ -1528,7 +1490,7 @@ int ufshcd_hold(struct ufs_hba *hba, bool async)*/ /* fallthrough */ case CLKS_OFF : -ufshcd_scsi_block_requests (hba); +ufshcd_scsi_block_requests (hba,ULONG_MAX ); hba->clk_gating.state =REQ_CLKS_ON ;trace_ufshcd_clk_gating (dev_name (hba->dev), hba->clk_gating.state);
@@ -2399,9 +2361,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)BUG (); } -if (!down_read_trylock (&hba->clk_scaling_lock)) -return SCSI_MLQUEUE_HOST_BUSY ; -spin_lock_irqsave (hba->host->host_lock, flags);switch (hba->ufshcd_state) {case UFSHCD_STATE_OPERATIONAL :
@@ -2466,7 +2425,6 @@ static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) out_unlock:spin_unlock_irqrestore (hba->host->host_lock, flags); out: -up_read (&hba->clk_scaling_lock);return err; }
@@ -2620,8 +2578,6 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,struct completion wait;unsigned long flags; -down_read (&hba->clk_scaling_lock); -/* * Get free slot, sleep if slots are unavailable. * Even though we use wait_event() which sleeps indefinitely,
@@ -2656,7 +2612,6 @@ static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, out_put_tag:blk_put_request (req); -up_read (&hba->clk_scaling_lock);return err; }
@@ -5455,7 +5410,7 @@ static void ufshcd_check_errors(struct ufs_hba *hba)/* handle fatal errors only when link is functional */ if (hba->ufshcd_state ==UFSHCD_STATE_OPERATIONAL ) {/* block commands from scsi mid-layer */ -ufshcd_scsi_block_requests (hba); +ufshcd_scsi_block_requests (hba,ULONG_MAX ); hba->ufshcd_state =UFSHCD_STATE_EH_SCHEDULED ;
@@ -5758,8 +5713,6 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,unsigned long flags;u32 upiu_flags; -down_read (&hba->clk_scaling_lock); - req =blk_get_request (q,REQ_OP_DRV_OUT ,0 );if (IS_ERR (req))return PTR_ERR (req);
@@ -5839,7 +5792,6 @@ static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, }blk_put_request (req); -up_read (&hba->clk_scaling_lock);return err; }
@@ -8317,8 +8269,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)/* Initialize mutex for device management commands */ mutex_init (&hba->dev_cmd.lock); -init_rwsem (&hba->clk_scaling_lock); -ufshcd_init_clk_gating (hba);ufshcd_init_clk_scaling (hba);
@@ -8384,7 +8334,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)/* Hold auto suspend until async scan completes */ pm_runtime_get_sync (dev); -atomic_set (&hba->scsi_block_reqs_cnt,0 );/* * We are assuming that device wasn't put in sleep/power-down * state exclusively during the boot stage before kernel.
drivers/scsi/ufs/ufshcd.h +0 -3
@@ -518,7 +518,6 @@ struct ufs_stats {* @urgent_bkops_lvl: keeps track of urgent bkops level for device * @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for * device is known or not. -* @scsi_block_reqs_cnt: reference counting for scsi block requests */ struct ufs_hba {void __iomem *mmio_base;
@@ -719,9 +718,7 @@ struct ufs_hba {enum bkops_status urgent_bkops_lvl;bool is_urgent_bkops_lvl_checked; -struct rw_semaphore clk_scaling_lock;struct ufs_desc_size desc_size; -atomic_t scsi_block_reqs_cnt;struct device bsg_dev;struct request_queue *bsg_queue;
Signature · 1 line
2.24.0.rc1.363.gb1bccd3e3d-goog