public inbox for linux-scsi@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v4 00/10] Simplify the UFS driver initialization code
@ 2024-09-05 22:01 Bart Van Assche
  2024-09-05 22:01 ` [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host() Bart Van Assche
                   ` (9 more replies)
  0 siblings, 10 replies; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen; +Cc: linux-scsi, Bart Van Assche

Hi Martin,

This patch series addresses the following issues in the UFS driver
initialization code:
* The legacy and MCQ scsi_add_host() calls occur in different functions. This
  patch series reduces the number of scsi_add_host() calls from two to one
  and hence makes the UFS driver easier to maintain.
* Two functions have a boolean 'init_dev_params' argument. This patch series
  removes that argument from both functions by splitting functions and by
  pushing some function calls from caller into callee.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v3:
 - Split patch "Move the MCQ scsi_add_host() call" into two patches to make
   it easier for reviewers.

Changes compared to v2:
 - Improved several patch descriptions.
 - Moved one source code comment.

Changes compared to v1:
 - Fixed a compiler warning reported by the kernel build robot.
 - Improved patch descriptions.

Bart Van Assche (10):
  scsi: ufs: core: Introduce ufshcd_add_scsi_host()
  scsi: ufs: core: Introduce ufshcd_activate_link()
  scsi: ufs: core: Introduce ufshcd_post_device_init()
  scsi: ufs: core: Call ufshcd_add_scsi_host() later
  scsi: ufs: core: Move the ufshcd_device_init() call
  scsi: ufs: core: Move the ufshcd_device_init(hba, true) call
  scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call
  scsi: ufs: core: Move the MCQ scsi_add_host() call
  scsi: ufs: core: Move code out of an if-statement
  scsi: ufs: core: Remove the second argument of ufshcd_device_init()

 drivers/ufs/core/ufshcd.c | 268 ++++++++++++++++++++++----------------
 1 file changed, 153 insertions(+), 115 deletions(-)


^ permalink raw reply	[flat|nested] 21+ messages in thread

* [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host()
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-06 23:51   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link() Bart Van Assche
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Avri Altman, Bean Huo,
	James E.J. Bottomley, Manivannan Sadhasivam, Peter Wang,
	Andrew Halaney

Move the code for adding a SCSI host and also the code for managing
TMF tags from ufshcd_init() into a new function called
ufshcd_add_scsi_host(). This patch prepares for combining the two
scsi_add_host() calls into a single call. No functionality has been
changed.

Reviewed-by: Avri Altman <avri.altman@wdc.com>
Reviewed-by: Bean Huo <beanhuo@micron.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 84 ++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 31 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 8ea5a82503a9..ecf6da2efed1 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10357,6 +10357,56 @@ static const struct blk_mq_ops ufshcd_tmf_ops = {
 	.queue_rq = ufshcd_queue_tmf,
 };
 
+static int ufshcd_add_scsi_host(struct ufs_hba *hba)
+{
+	int err;
+
+	if (!is_mcq_supported(hba)) {
+		err = scsi_add_host(hba->host, hba->dev);
+		if (err) {
+			dev_err(hba->dev, "scsi_add_host failed\n");
+			return err;
+		}
+		hba->scsi_host_added = true;
+	}
+
+	hba->tmf_tag_set = (struct blk_mq_tag_set) {
+		.nr_hw_queues	= 1,
+		.queue_depth	= hba->nutmrs,
+		.ops		= &ufshcd_tmf_ops,
+		.flags		= BLK_MQ_F_NO_SCHED,
+	};
+	err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
+	if (err < 0)
+		goto remove_scsi_host;
+	hba->tmf_queue = blk_mq_alloc_queue(&hba->tmf_tag_set, NULL, NULL);
+	if (IS_ERR(hba->tmf_queue)) {
+		err = PTR_ERR(hba->tmf_queue);
+		goto free_tmf_tag_set;
+	}
+	hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs,
+				    sizeof(*hba->tmf_rqs), GFP_KERNEL);
+	if (!hba->tmf_rqs) {
+		err = -ENOMEM;
+		goto free_tmf_queue;
+	}
+
+	return 0;
+
+free_tmf_queue:
+	blk_mq_destroy_queue(hba->tmf_queue);
+	blk_put_queue(hba->tmf_queue);
+
+free_tmf_tag_set:
+	blk_mq_free_tag_set(&hba->tmf_tag_set);
+
+remove_scsi_host:
+	if (hba->scsi_host_added)
+		scsi_remove_host(hba->host);
+
+	return err;
+}
+
 /**
  * ufshcd_init - Driver initialization routine
  * @hba: per-adapter instance
@@ -10488,35 +10538,9 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 		hba->is_irq_enabled = true;
 	}
 
-	if (!is_mcq_supported(hba)) {
-		err = scsi_add_host(host, hba->dev);
-		if (err) {
-			dev_err(hba->dev, "scsi_add_host failed\n");
-			goto out_disable;
-		}
-		hba->scsi_host_added = true;
-	}
-
-	hba->tmf_tag_set = (struct blk_mq_tag_set) {
-		.nr_hw_queues	= 1,
-		.queue_depth	= hba->nutmrs,
-		.ops		= &ufshcd_tmf_ops,
-		.flags		= BLK_MQ_F_NO_SCHED,
-	};
-	err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
-	if (err < 0)
-		goto out_remove_scsi_host;
-	hba->tmf_queue = blk_mq_alloc_queue(&hba->tmf_tag_set, NULL, NULL);
-	if (IS_ERR(hba->tmf_queue)) {
-		err = PTR_ERR(hba->tmf_queue);
-		goto free_tmf_tag_set;
-	}
-	hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs,
-				    sizeof(*hba->tmf_rqs), GFP_KERNEL);
-	if (!hba->tmf_rqs) {
-		err = -ENOMEM;
-		goto free_tmf_queue;
-	}
+	err = ufshcd_add_scsi_host(hba);
+	if (err)
+		goto out_disable;
 
 	/* Reset the attached device */
 	ufshcd_device_reset(hba);
@@ -10574,9 +10598,7 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 free_tmf_queue:
 	blk_mq_destroy_queue(hba->tmf_queue);
 	blk_put_queue(hba->tmf_queue);
-free_tmf_tag_set:
 	blk_mq_free_tag_set(&hba->tmf_tag_set);
-out_remove_scsi_host:
 	if (hba->scsi_host_added)
 		scsi_remove_host(hba->host);
 out_disable:

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link()
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
  2024-09-05 22:01 ` [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host() Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-06 23:59   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init() Bart Van Assche
                   ` (7 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Avri Altman, Manivannan Sadhasivam,
	Bean Huo, James E.J. Bottomley, Peter Wang, Andrew Halaney

Prepare for introducing a second caller by moving the code for
activating the link between UFS controller and device into a new
function. No functionality has been changed.

Reviewed-by: Avri Altman <avri.altman@wdc.com>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Reviewed-by: Bean Huo <beanhuo@micron.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index ecf6da2efed1..4cfa8dd5500a 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -8709,10 +8709,9 @@ static void ufshcd_config_mcq(struct ufs_hba *hba)
 		 hba->nutrs);
 }
 
-static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
+static int ufshcd_activate_link(struct ufs_hba *hba)
 {
 	int ret;
-	struct Scsi_Host *host = hba->host;
 
 	hba->ufshcd_state = UFSHCD_STATE_RESET;
 
@@ -8729,6 +8728,18 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 	/* UniPro link is active now */
 	ufshcd_set_link_active(hba);
 
+	return 0;
+}
+
+static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
+{
+	struct Scsi_Host *host = hba->host;
+	int ret;
+
+	ret = ufshcd_activate_link(hba);
+	if (ret)
+		return ret;
+
 	/* Reconfigure MCQ upon reset */
 	if (hba->mcq_enabled && !init_dev_params) {
 		ufshcd_config_mcq(hba);

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init()
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
  2024-09-05 22:01 ` [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host() Bart Van Assche
  2024-09-05 22:01 ` [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link() Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07  0:24   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later Bart Van Assche
                   ` (6 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Avri Altman, Manivannan Sadhasivam,
	James E.J. Bottomley, Peter Wang, Andrew Halaney, Bean Huo

Prepare for introducing a second caller by moving more code from
ufshcd_device_init() into a new function.

Reviewed-by: Avri Altman <avri.altman@wdc.com>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 63 ++++++++++++++++++++++-----------------
 1 file changed, 36 insertions(+), 27 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 4cfa8dd5500a..916da4c054be 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -8731,6 +8731,41 @@ static int ufshcd_activate_link(struct ufs_hba *hba)
 	return 0;
 }
 
+static int ufshcd_post_device_init(struct ufs_hba *hba)
+{
+	int ret;
+
+	ufshcd_tune_unipro_params(hba);
+
+	/* UFS device is also active now */
+	ufshcd_set_ufs_dev_active(hba);
+	ufshcd_force_reset_auto_bkops(hba);
+
+	ufshcd_set_timestamp_attr(hba);
+	schedule_delayed_work(&hba->ufs_rtc_update_work,
+			      msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
+
+	if (!hba->max_pwr_info.is_valid)
+		return 0;
+
+	/*
+	 * Set the right value to bRefClkFreq before attempting to
+	 * switch to HS gears.
+	 */
+	if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
+		ufshcd_set_dev_ref_clk(hba);
+	/* Gear up to HS gear. */
+	ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
+	if (ret) {
+		dev_err(hba->dev,
+			"%s: Failed setting power mode, err = %d\n",
+			__func__, ret);
+		return ret;
+	}
+
+	return 0;
+}
+
 static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 {
 	struct Scsi_Host *host = hba->host;
@@ -8789,33 +8824,7 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 		}
 	}
 
-	ufshcd_tune_unipro_params(hba);
-
-	/* UFS device is also active now */
-	ufshcd_set_ufs_dev_active(hba);
-	ufshcd_force_reset_auto_bkops(hba);
-
-	ufshcd_set_timestamp_attr(hba);
-	schedule_delayed_work(&hba->ufs_rtc_update_work,
-			      msecs_to_jiffies(UFS_RTC_UPDATE_INTERVAL_MS));
-
-	/* Gear up to HS gear if supported */
-	if (hba->max_pwr_info.is_valid) {
-		/*
-		 * Set the right value to bRefClkFreq before attempting to
-		 * switch to HS gears.
-		 */
-		if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
-			ufshcd_set_dev_ref_clk(hba);
-		ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
-		if (ret) {
-			dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
-					__func__, ret);
-			return ret;
-		}
-	}
-
-	return 0;
+	return ufshcd_post_device_init(hba);
 }
 
 /**

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (2 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init() Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 10:52   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call Bart Van Assche
                   ` (5 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, Manivannan Sadhasivam, Bean Huo,
	James E.J. Bottomley, Peter Wang, Avri Altman, Andrew Halaney

Call ufshcd_add_scsi_host() after host controller initialization has
completed. This is possible because no code between the old and new
ufshcd_add_scsi_host() call site depends on the scsi_add_host() call.

Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Reviewed-by: Bean Huo <beanhuo@micron.com>
Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 916da4c054be..e2137bcf3de7 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10558,10 +10558,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 		hba->is_irq_enabled = true;
 	}
 
-	err = ufshcd_add_scsi_host(hba);
-	if (err)
-		goto out_disable;
-
 	/* Reset the attached device */
 	ufshcd_device_reset(hba);
 
@@ -10573,7 +10569,7 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 		dev_err(hba->dev, "Host controller enable failed\n");
 		ufshcd_print_evt_hist(hba);
 		ufshcd_print_host_state(hba);
-		goto free_tmf_queue;
+		goto out_disable;
 	}
 
 	/*
@@ -10608,6 +10604,10 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	 */
 	ufshcd_set_ufs_dev_active(hba);
 
+	err = ufshcd_add_scsi_host(hba);
+	if (err)
+		goto out_disable;
+
 	async_schedule(ufshcd_async_scan, hba);
 	ufs_sysfs_add_nodes(hba->dev);
 
@@ -10615,12 +10615,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	ufshcd_pm_qos_init(hba);
 	return 0;
 
-free_tmf_queue:
-	blk_mq_destroy_queue(hba->tmf_queue);
-	blk_put_queue(hba->tmf_queue);
-	blk_mq_free_tag_set(&hba->tmf_tag_set);
-	if (hba->scsi_host_added)
-		scsi_remove_host(hba->host);
 out_disable:
 	hba->is_irq_enabled = false;
 	ufshcd_hba_exit(hba);

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (3 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 11:12   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call Bart Van Assche
                   ` (4 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

Move the ufshcd_device_init() call to the ufshcd_probe_hba() callers and
remove the 'init_dev_params' argument of ufshcd_probe_hba(). This change
refactors the code without modifying the behavior of the UFSHCI driver.
This change prepares for moving one ufshcd_device_init() call into
ufshcd_init().

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index e2137bcf3de7..6e3cffcdf9a6 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -298,7 +298,8 @@ static int ufshcd_reset_and_restore(struct ufs_hba *hba);
 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
 static void ufshcd_hba_exit(struct ufs_hba *hba);
-static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params);
+static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params);
+static int ufshcd_probe_hba(struct ufs_hba *hba);
 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
@@ -7693,8 +7694,11 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
 	err = ufshcd_hba_enable(hba);
 
 	/* Establish the link again and restore the device */
-	if (!err)
-		err = ufshcd_probe_hba(hba, false);
+	if (!err) {
+		err = ufshcd_device_init(hba, /*init_dev_params=*/false);
+		if (!err)
+			err = ufshcd_probe_hba(hba);
+	}
 
 	if (err)
 		dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
@@ -8830,21 +8834,16 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 /**
  * ufshcd_probe_hba - probe hba to detect device and initialize it
  * @hba: per-adapter instance
- * @init_dev_params: whether or not to call ufshcd_device_params_init().
  *
  * Execute link-startup and verify device initialization
  *
  * Return: 0 upon success; < 0 upon failure.
  */
-static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
+static int ufshcd_probe_hba(struct ufs_hba *hba)
 {
 	ktime_t start = ktime_get();
 	unsigned long flags;
-	int ret;
-
-	ret = ufshcd_device_init(hba, init_dev_params);
-	if (ret)
-		goto out;
+	int ret = 0;
 
 	if (!hba->pm_op_in_progress &&
 	    (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) {
@@ -8862,7 +8861,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 		}
 
 		/* Reinit the device */
-		ret = ufshcd_device_init(hba, init_dev_params);
+		ret = ufshcd_device_init(hba, /*init_dev_params=*/false);
 		if (ret)
 			goto out;
 	}
@@ -8910,7 +8909,9 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 
 	down(&hba->host_sem);
 	/* Initialize hba, detect and initialize UFS device */
-	ret = ufshcd_probe_hba(hba, true);
+	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
+	if (ret == 0)
+		ret = ufshcd_probe_hba(hba);
 	up(&hba->host_sem);
 	if (ret)
 		goto out;

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (4 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 19:38   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 07/10] scsi: ufs: core: Expand " Bart Van Assche
                   ` (3 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

Move the ufshcd_device_init(hba, true) call from ufshcd_async_scan()
into ufshcd_init(). This patch prepares for moving both scsi_add_host()
calls into ufshcd_add_scsi_host(). Calling ufshcd_device_init() from
ufshcd_init() without holding hba->host_sem is safe. This is safe because
hba->host_sem serializes core code and sysfs callbacks. The
ufshcd_device_init() call is moved before the scsi_add_host() call and
hence happens before any SCSI sysfs attributes are created.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 6e3cffcdf9a6..843566720afa 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -8908,10 +8908,7 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 	int ret;
 
 	down(&hba->host_sem);
-	/* Initialize hba, detect and initialize UFS device */
-	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
-	if (ret == 0)
-		ret = ufshcd_probe_hba(hba);
+	ret = ufshcd_probe_hba(hba);
 	up(&hba->host_sem);
 	if (ret)
 		goto out;
@@ -10605,6 +10602,10 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	 */
 	ufshcd_set_ufs_dev_active(hba);
 
+	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
+	if (err)
+		goto out_disable;
+
 	err = ufshcd_add_scsi_host(hba);
 	if (err)
 		goto out_disable;

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 07/10] scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (5 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 20:15   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call Bart Van Assche
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

Expand the ufshcd_device_init(hba, true) call and remove all code that
depends on init_dev_params == false. This change prepares for combining
the two scsi_add_host() calls.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 43 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 42 insertions(+), 1 deletion(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 843566720afa..014bc74390af 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10602,7 +10602,48 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	 */
 	ufshcd_set_ufs_dev_active(hba);
 
-	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
+	err = ufshcd_activate_link(hba);
+	if (err)
+		goto out_disable;
+
+	/* Verify device initialization by sending NOP OUT UPIU. */
+	err = ufshcd_verify_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	/* Initiate UFS initialization and waiting for completion. */
+	err = ufshcd_complete_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	/*
+	 * Initialize UFS device parameters used by driver, these
+	 * parameters are associated with UFS descriptors.
+	 */
+	err = ufshcd_device_params_init(hba);
+	if (err)
+		goto out_disable;
+	if (is_mcq_supported(hba)) {
+		ufshcd_mcq_enable(hba);
+		err = ufshcd_alloc_mcq(hba);
+		if (!err) {
+			ufshcd_config_mcq(hba);
+		} else {
+			/* Continue with SDB mode */
+			ufshcd_mcq_disable(hba);
+			use_mcq_mode = false;
+			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
+				err);
+		}
+		err = scsi_add_host(host, hba->dev);
+		if (err) {
+			dev_err(hba->dev, "scsi_add_host failed\n");
+			goto out_disable;
+		}
+		hba->scsi_host_added = true;
+	}
+
+	err = ufshcd_post_device_init(hba);
 	if (err)
 		goto out_disable;
 

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (6 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 07/10] scsi: ufs: core: Expand " Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 20:27   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement Bart Van Assche
  2024-09-05 22:01 ` [PATCH v4 10/10] scsi: ufs: core: Remove the second argument of ufshcd_device_init() Bart Van Assche
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

Whether or not MCQ is used, call scsi_add_host from ufshcd_add_scsi_host().
For MCQ this patch swaps the order of the scsi_add_host() and
ufshcd_post_device_init() calls. This patch also prepares for moving
both scsi_add_host() calls into ufshcd_add_scsi_host().

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 39 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 20 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 014bc74390af..b46e9b526839 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10379,7 +10379,25 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
 {
 	int err;
 
-	if (!is_mcq_supported(hba)) {
+	if (is_mcq_supported(hba)) {
+		ufshcd_mcq_enable(hba);
+		err = ufshcd_alloc_mcq(hba);
+		if (!err) {
+			ufshcd_config_mcq(hba);
+		} else {
+			/* Continue with SDB mode */
+			ufshcd_mcq_disable(hba);
+			use_mcq_mode = false;
+			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
+				err);
+		}
+		err = scsi_add_host(hba->host, hba->dev);
+		if (err) {
+			dev_err(hba->dev, "scsi_add_host failed\n");
+			return err;
+		}
+		hba->scsi_host_added = true;
+	} else {
 		err = scsi_add_host(hba->host, hba->dev);
 		if (err) {
 			dev_err(hba->dev, "scsi_add_host failed\n");
@@ -10623,25 +10641,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	err = ufshcd_device_params_init(hba);
 	if (err)
 		goto out_disable;
-	if (is_mcq_supported(hba)) {
-		ufshcd_mcq_enable(hba);
-		err = ufshcd_alloc_mcq(hba);
-		if (!err) {
-			ufshcd_config_mcq(hba);
-		} else {
-			/* Continue with SDB mode */
-			ufshcd_mcq_disable(hba);
-			use_mcq_mode = false;
-			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
-				err);
-		}
-		err = scsi_add_host(host, hba->dev);
-		if (err) {
-			dev_err(hba->dev, "scsi_add_host failed\n");
-			goto out_disable;
-		}
-		hba->scsi_host_added = true;
-	}
 
 	err = ufshcd_post_device_init(hba);
 	if (err)

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (7 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  2024-09-07 20:36   ` Bao D. Nguyen
  2024-09-05 22:01 ` [PATCH v4 10/10] scsi: ufs: core: Remove the second argument of ufshcd_device_init() Bart Van Assche
  9 siblings, 1 reply; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

The previous patch in this series introduced identical code in both
branches of an if-statement. Move that code outside the if-statement.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index b46e9b526839..17615b56e83e 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -10391,21 +10391,15 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
 			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
 				err);
 		}
-		err = scsi_add_host(hba->host, hba->dev);
-		if (err) {
-			dev_err(hba->dev, "scsi_add_host failed\n");
-			return err;
-		}
-		hba->scsi_host_added = true;
-	} else {
-		err = scsi_add_host(hba->host, hba->dev);
-		if (err) {
-			dev_err(hba->dev, "scsi_add_host failed\n");
-			return err;
-		}
-		hba->scsi_host_added = true;
 	}
 
+	err = scsi_add_host(hba->host, hba->dev);
+	if (err) {
+		dev_err(hba->dev, "scsi_add_host failed\n");
+		return err;
+	}
+	hba->scsi_host_added = true;
+
 	hba->tmf_tag_set = (struct blk_mq_tag_set) {
 		.nr_hw_queues	= 1,
 		.queue_depth	= hba->nutmrs,

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* [PATCH v4 10/10] scsi: ufs: core: Remove the second argument of ufshcd_device_init()
  2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
                   ` (8 preceding siblings ...)
  2024-09-05 22:01 ` [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement Bart Van Assche
@ 2024-09-05 22:01 ` Bart Van Assche
  9 siblings, 0 replies; 21+ messages in thread
From: Bart Van Assche @ 2024-09-05 22:01 UTC (permalink / raw)
  To: Martin K . Petersen
  Cc: linux-scsi, Bart Van Assche, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Avri Altman, Andrew Halaney,
	Bean Huo

Both ufshcd_device_init() callers pass 'false' as second argument. Hence,
remove that second argument.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
---
 drivers/ufs/core/ufshcd.c | 44 +++++----------------------------------
 1 file changed, 5 insertions(+), 39 deletions(-)

diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
index 17615b56e83e..779a0c7559b2 100644
--- a/drivers/ufs/core/ufshcd.c
+++ b/drivers/ufs/core/ufshcd.c
@@ -298,7 +298,7 @@ static int ufshcd_reset_and_restore(struct ufs_hba *hba);
 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
 static void ufshcd_hba_exit(struct ufs_hba *hba);
-static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params);
+static int ufshcd_device_init(struct ufs_hba *hba);
 static int ufshcd_probe_hba(struct ufs_hba *hba);
 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
@@ -7695,7 +7695,7 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
 
 	/* Establish the link again and restore the device */
 	if (!err) {
-		err = ufshcd_device_init(hba, /*init_dev_params=*/false);
+		err = ufshcd_device_init(hba);
 		if (!err)
 			err = ufshcd_probe_hba(hba);
 	}
@@ -8770,9 +8770,8 @@ static int ufshcd_post_device_init(struct ufs_hba *hba)
 	return 0;
 }
 
-static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
+static int ufshcd_device_init(struct ufs_hba *hba)
 {
-	struct Scsi_Host *host = hba->host;
 	int ret;
 
 	ret = ufshcd_activate_link(hba);
@@ -8780,7 +8779,7 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 		return ret;
 
 	/* Reconfigure MCQ upon reset */
-	if (hba->mcq_enabled && !init_dev_params) {
+	if (hba->mcq_enabled) {
 		ufshcd_config_mcq(hba);
 		ufshcd_mcq_enable(hba);
 	}
@@ -8795,39 +8794,6 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 	if (ret)
 		return ret;
 
-	/*
-	 * Initialize UFS device parameters used by driver, these
-	 * parameters are associated with UFS descriptors.
-	 */
-	if (init_dev_params) {
-		ret = ufshcd_device_params_init(hba);
-		if (ret)
-			return ret;
-		if (is_mcq_supported(hba) && !hba->scsi_host_added) {
-			ufshcd_mcq_enable(hba);
-			ret = ufshcd_alloc_mcq(hba);
-			if (!ret) {
-				ufshcd_config_mcq(hba);
-			} else {
-				/* Continue with SDB mode */
-				ufshcd_mcq_disable(hba);
-				use_mcq_mode = false;
-				dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
-					 ret);
-			}
-			ret = scsi_add_host(host, hba->dev);
-			if (ret) {
-				dev_err(hba->dev, "scsi_add_host failed\n");
-				return ret;
-			}
-			hba->scsi_host_added = true;
-		} else if (is_mcq_supported(hba)) {
-			/* UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is set */
-			ufshcd_config_mcq(hba);
-			ufshcd_mcq_enable(hba);
-		}
-	}
-
 	return ufshcd_post_device_init(hba);
 }
 
@@ -8861,7 +8827,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba)
 		}
 
 		/* Reinit the device */
-		ret = ufshcd_device_init(hba, /*init_dev_params=*/false);
+		ret = ufshcd_device_init(hba);
 		if (ret)
 			goto out;
 	}

^ permalink raw reply related	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host()
  2024-09-05 22:01 ` [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host() Bart Van Assche
@ 2024-09-06 23:51   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-06 23:51 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, Bean Huo, James E.J. Bottomley,
	Manivannan Sadhasivam, Peter Wang, Andrew Halaney

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Move the code for adding a SCSI host and also the code for managing
> TMF tags from ufshcd_init() into a new function called
> ufshcd_add_scsi_host(). This patch prepares for combining the two
> scsi_add_host() calls into a single call. No functionality has been
> changed.
> 

Reviewed-by: Bao D. Nguyen <quic_nguyenb@quicinc.com>

Thanks, Bao

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link()
  2024-09-05 22:01 ` [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link() Bart Van Assche
@ 2024-09-06 23:59   ` Bao D. Nguyen
  2024-09-09 17:08     ` Bart Van Assche
  0 siblings, 1 reply; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-06 23:59 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, Manivannan Sadhasivam, Bean Huo,
	James E.J. Bottomley, Peter Wang, Andrew Halaney

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Prepare for introducing a second caller by moving the code for
> activating the link between UFS controller and device into a new
> function. No functionality has been changed.
> 
> Reviewed-by: Avri Altman <avri.altman@wdc.com>
> Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> Reviewed-by: Bean Huo <beanhuo@micron.com>
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>   drivers/ufs/core/ufshcd.c | 15 +++++++++++++--
>   1 file changed, 13 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index ecf6da2efed1..4cfa8dd5500a 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -8709,10 +8709,9 @@ static void ufshcd_config_mcq(struct ufs_hba *hba)
>   		 hba->nutrs);
>   }
>   
> -static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
> +static int ufshcd_activate_link(struct ufs_hba *hba)
>   {
>   	int ret;
> -	struct Scsi_Host *host = hba->host;
>   
>   	hba->ufshcd_state = UFSHCD_STATE_RESET;
>   
> @@ -8729,6 +8728,18 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
>   	/* UniPro link is active now */
>   	ufshcd_set_link_active(hba);
>   
> +	return 0;
> +}
> +
> +static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
> +{
> +	struct Scsi_Host *host = hba->host;
> +	int ret;
> +
> +	ret = ufshcd_activate_link(hba);
> +	if (ret)
> +		return ret;

Hi Bart,
There may be a problem in this patch.
In the original code, if UFSHCD_QUIRK_SKIP_PH_CONFIGURATION is set, 
ufshcd_device_init() would exit early. However, in this patch, if 
UFSHCD_QUIRK_SKIP_PH_CONFIGURATION is set, the new function 
ufshcd_activate_link() would return 0 which would cause the 
ufshcd_device_init() to continue further down. As a result, the 
ufshcd_device_init() would fail to handle the 
UFSHCD_QUIRK_SKIP_PH_CONFIGURATION flag as its original intention, right?

Thanks, Bao

> +
>   	/* Reconfigure MCQ upon reset */
>   	if (hba->mcq_enabled && !init_dev_params) {
>   		ufshcd_config_mcq(hba);
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init()
  2024-09-05 22:01 ` [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init() Bart Van Assche
@ 2024-09-07  0:24   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07  0:24 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, Manivannan Sadhasivam,
	James E.J. Bottomley, Peter Wang, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:

> +	ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
> +	if (ret) {
> +		dev_err(hba->dev,

Nit picking. A line break here is not necessary. The original code fits 
the message fine.
> +			"%s: Failed setting power mode, err = %d\n",
> +			__func__, ret);
> +		return ret;
> +	}

Reviewed-by: Bao D. Nguyen <quic_nguyenb@quicinc.com>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later
  2024-09-05 22:01 ` [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later Bart Van Assche
@ 2024-09-07 10:52   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 10:52 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, Manivannan Sadhasivam, Bean Huo, James E.J. Bottomley,
	Peter Wang, Avri Altman, Andrew Halaney

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Call ufshcd_add_scsi_host() after host controller initialization has
> completed. This is possible because no code between the old and new
> ufshcd_add_scsi_host() call site depends on the scsi_add_host() call.
> 
> Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
> Reviewed-by: Bean Huo <beanhuo@micron.com>
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>

Reviewed-by: Bao D. Nguyen <quic_nguyenb@quicinc.com>

^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call
  2024-09-05 22:01 ` [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call Bart Van Assche
@ 2024-09-07 11:12   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 11:12 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, James E.J. Bottomley, Manivannan Sadhasivam,
	Peter Wang, Avri Altman, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Move the ufshcd_device_init() call to the ufshcd_probe_hba() callers and
> remove the 'init_dev_params' argument of ufshcd_probe_hba(). This change
> refactors the code without modifying the behavior of the UFSHCI driver.
> This change prepares for moving one ufshcd_device_init() call into
> ufshcd_init().
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>   drivers/ufs/core/ufshcd.c | 25 +++++++++++++------------
>   1 file changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index e2137bcf3de7..6e3cffcdf9a6 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -298,7 +298,8 @@ static int ufshcd_reset_and_restore(struct ufs_hba *hba);
>   static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
>   static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
>   static void ufshcd_hba_exit(struct ufs_hba *hba);
> -static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params);
> +static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params);
> +static int ufshcd_probe_hba(struct ufs_hba *hba);
>   static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
>   static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
>   static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
> @@ -7693,8 +7694,11 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
>   	err = ufshcd_hba_enable(hba);
>   
>   	/* Establish the link again and restore the device */
> -	if (!err)
> -		err = ufshcd_probe_hba(hba, false);

In the original code, if ufshcd_probe_hba()->ufshcd_device_init() fails, 
the hba->ufshcd_state is updated to UFSHCD_STATE_ERROR;

> +	if (!err) {
> +		err = ufshcd_device_init(hba, /*init_dev_params=*/false);

Calling ufshcd_device_init() here changes the behavior of the code 
slightly. If the ufshcd_device_init() fails, it exits without updating 
the hba->ufshcd_state to UFSHCD_STATE_ERROR.


> +		if (!err)
> +			err = ufshcd_probe_hba(hba);
> +	}
>   
>   	if (err)
>   		dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
> @@ -8830,21 +8834,16 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
>   /**
>    * ufshcd_probe_hba - probe hba to detect device and initialize it
>    * @hba: per-adapter instance
> - * @init_dev_params: whether or not to call ufshcd_device_params_init().
>    *
>    * Execute link-startup and verify device initialization
>    *
>    * Return: 0 upon success; < 0 upon failure.
>    */
> -static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
> +static int ufshcd_probe_hba(struct ufs_hba *hba)
>   {
>   	ktime_t start = ktime_get();
>   	unsigned long flags;
> -	int ret;
> -
> -	ret = ufshcd_device_init(hba, init_dev_params);
> -	if (ret)
> -		goto out;
> +	int ret = 0;
>   
>   	if (!hba->pm_op_in_progress &&
>   	    (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) {
> @@ -8862,7 +8861,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
>   		}
>   
>   		/* Reinit the device */
> -		ret = ufshcd_device_init(hba, init_dev_params);
> +		ret = ufshcd_device_init(hba, /*init_dev_params=*/false);

Originally, the ufshcd_async_scan()->ufshcd_probe_hba() has the 
init_dev_params = true. However the init_dev_params is updated to false 
here for the ufshcd_async_scan()->ufshcd_probe_hba() path.

>   		if (ret)
>   			goto out;
>   	}
> @@ -8910,7 +8909,9 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
>   
>   	down(&hba->host_sem);
>   	/* Initialize hba, detect and initialize UFS device */
> -	ret = ufshcd_probe_hba(hba, true);
> +	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);

Same issue as mentioned earlier. If ufshcd_device_init() fails here, the 
hba->ufshcd_state is not updated to UFSHCD_STATE_ERROR. Compared to the 
original ufshcd_probe_hba()->ufshcd_device_init() failure would update 
hba->ufshcd_state.

Thanks, Bao

> +	if (ret == 0)
> +		ret = ufshcd_probe_hba(hba);
>   	up(&hba->host_sem);
>   	if (ret)
>   		goto out;
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call
  2024-09-05 22:01 ` [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call Bart Van Assche
@ 2024-09-07 19:38   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 19:38 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, James E.J. Bottomley, Manivannan Sadhasivam,
	Peter Wang, Avri Altman, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Move the ufshcd_device_init(hba, true) call from ufshcd_async_scan()
> into ufshcd_init(). This patch prepares for moving both scsi_add_host()
> calls into ufshcd_add_scsi_host(). Calling ufshcd_device_init() from
> ufshcd_init() without holding hba->host_sem is safe. This is safe because
> hba->host_sem serializes core code and sysfs callbacks. The
> ufshcd_device_init() call is moved before the scsi_add_host() call and
> hence happens before any SCSI sysfs attributes are created.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>   drivers/ufs/core/ufshcd.c | 9 +++++----
>   1 file changed, 5 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index 6e3cffcdf9a6..843566720afa 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -8908,10 +8908,7 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
>   	int ret;
>   
>   	down(&hba->host_sem);
> -	/* Initialize hba, detect and initialize UFS device */
> -	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
> -	if (ret == 0)
> -		ret = ufshcd_probe_hba(hba);
> +	ret = ufshcd_probe_hba(hba);
>   	up(&hba->host_sem);
>   	if (ret)
>   		goto out;
> @@ -10605,6 +10602,10 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
>   	 */
>   	ufshcd_set_ufs_dev_active(hba);
>   
> +	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
> +	if (err)
> +		goto out_disable;
> +

In SDB mode, the order of execution for these two functions changed by 
this patch. In the original code, the scsi_add_host() happens first, 
then the code within ufshcd_post_device_init(). Here it is the opposite. 
However, it seems the order can be swapped without any issue.

>   	err = ufshcd_add_scsi_host(hba);
>   	if (err)
>   		goto out_disable;
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 07/10] scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call
  2024-09-05 22:01 ` [PATCH v4 07/10] scsi: ufs: core: Expand " Bart Van Assche
@ 2024-09-07 20:15   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 20:15 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, James E.J. Bottomley, Manivannan Sadhasivam,
	Peter Wang, Avri Altman, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Expand the ufshcd_device_init(hba, true) call and remove all code that
> depends on init_dev_params == false. This change prepares for combining
> the two scsi_add_host() calls.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>   drivers/ufs/core/ufshcd.c | 43 ++++++++++++++++++++++++++++++++++++++-
>   1 file changed, 42 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index 843566720afa..014bc74390af 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -10602,7 +10602,48 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
>   	 */
>   	ufshcd_set_ufs_dev_active(hba);
>   
> -	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
> +	err = ufshcd_activate_link(hba);
> +	if (err)
> +		goto out_disable;
> +
> +	/* Verify device initialization by sending NOP OUT UPIU. */
> +	err = ufshcd_verify_dev_init(hba);
> +	if (err)
> +		goto out_disable;
> +
> +	/* Initiate UFS initialization and waiting for completion. */
> +	err = ufshcd_complete_dev_init(hba);
> +	if (err)
> +		goto out_disable;
> +
> +	/*
> +	 * Initialize UFS device parameters used by driver, these
> +	 * parameters are associated with UFS descriptors.
> +	 */
> +	err = ufshcd_device_params_init(hba);
> +	if (err)
> +		goto out_disable;
> +	if (is_mcq_supported(hba)) {
> +		ufshcd_mcq_enable(hba);
> +		err = ufshcd_alloc_mcq(hba);
> +		if (!err) {
> +			ufshcd_config_mcq(hba);
> +		} else {
> +			/* Continue with SDB mode */
> +			ufshcd_mcq_disable(hba);
> +			use_mcq_mode = false;

If ufshcd_alloc_mcq() fails here, sdb mode is used. The scsi_add_host() 
is also called for sdb mode. Later on, when the ufshcd_add_scsi_host() 
function is called, we will call scsi_add_host() again for sdb mode. 
Therefore, scsi_add_host() would be called twice.

> +			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
> +				err);
> +		}
> +		err = scsi_add_host(host, hba->dev);
> +		if (err) {
> +			dev_err(hba->dev, "scsi_add_host failed\n");
> +			goto out_disable;
> +		}
> +		hba->scsi_host_added = true;
> +	}
> +
> +	err = ufshcd_post_device_init(hba);
>   	if (err)
>   		goto out_disable;
>   
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call
  2024-09-05 22:01 ` [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call Bart Van Assche
@ 2024-09-07 20:27   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 20:27 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, James E.J. Bottomley, Manivannan Sadhasivam,
	Peter Wang, Avri Altman, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> Whether or not MCQ is used, call scsi_add_host from ufshcd_add_scsi_host().
> For MCQ this patch swaps the order of the scsi_add_host() and
> ufshcd_post_device_init() calls. This patch also prepares for moving
> both scsi_add_host() calls into ufshcd_add_scsi_host().

This patch fixes the issue about scsi_add_host() being called twice 
introduced in patch #7. It also acknowledged the order swap I mentioned 
in patch #6.

> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>
> ---
>   drivers/ufs/core/ufshcd.c | 39 +++++++++++++++++++--------------------
>   1 file changed, 19 insertions(+), 20 deletions(-)
> 
> diff --git a/drivers/ufs/core/ufshcd.c b/drivers/ufs/core/ufshcd.c
> index 014bc74390af..b46e9b526839 100644
> --- a/drivers/ufs/core/ufshcd.c
> +++ b/drivers/ufs/core/ufshcd.c
> @@ -10379,7 +10379,25 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
>   {
>   	int err;
>   
> -	if (!is_mcq_supported(hba)) {
> +	if (is_mcq_supported(hba)) {
> +		ufshcd_mcq_enable(hba);
> +		err = ufshcd_alloc_mcq(hba);
> +		if (!err) {
> +			ufshcd_config_mcq(hba);
> +		} else {
> +			/* Continue with SDB mode */
> +			ufshcd_mcq_disable(hba);
> +			use_mcq_mode = false;
> +			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
> +				err);
> +		}
> +		err = scsi_add_host(hba->host, hba->dev);
> +		if (err) {
> +			dev_err(hba->dev, "scsi_add_host failed\n");
> +			return err;
> +		}
> +		hba->scsi_host_added = true;
> +	} else {
>   		err = scsi_add_host(hba->host, hba->dev);
>   		if (err) {
>   			dev_err(hba->dev, "scsi_add_host failed\n");
> @@ -10623,25 +10641,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
>   	err = ufshcd_device_params_init(hba);
>   	if (err)
>   		goto out_disable;
> -	if (is_mcq_supported(hba)) {
> -		ufshcd_mcq_enable(hba);
> -		err = ufshcd_alloc_mcq(hba);
> -		if (!err) {
> -			ufshcd_config_mcq(hba);
> -		} else {
> -			/* Continue with SDB mode */
> -			ufshcd_mcq_disable(hba);
> -			use_mcq_mode = false;
> -			dev_err(hba->dev, "MCQ mode is disabled, err=%d\n",
> -				err);
> -		}
> -		err = scsi_add_host(host, hba->dev);
> -		if (err) {
> -			dev_err(hba->dev, "scsi_add_host failed\n");
> -			goto out_disable;
> -		}
> -		hba->scsi_host_added = true;
> -	}
>   
>   	err = ufshcd_post_device_init(hba);
>   	if (err)
> 


^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement
  2024-09-05 22:01 ` [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement Bart Van Assche
@ 2024-09-07 20:36   ` Bao D. Nguyen
  0 siblings, 0 replies; 21+ messages in thread
From: Bao D. Nguyen @ 2024-09-07 20:36 UTC (permalink / raw)
  To: Bart Van Assche, Martin K . Petersen
  Cc: linux-scsi, James E.J. Bottomley, Manivannan Sadhasivam,
	Peter Wang, Avri Altman, Andrew Halaney, Bean Huo

On 9/5/2024 3:01 PM, Bart Van Assche wrote:
> The previous patch in this series introduced identical code in both
> branches of an if-statement. Move that code outside the if-statement.
> 
> Signed-off-by: Bart Van Assche <bvanassche@acm.org>

Reviewed-by: Bao D. Nguyen <quic_nguyenb@quicinc.com>



^ permalink raw reply	[flat|nested] 21+ messages in thread

* Re: [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link()
  2024-09-06 23:59   ` Bao D. Nguyen
@ 2024-09-09 17:08     ` Bart Van Assche
  0 siblings, 0 replies; 21+ messages in thread
From: Bart Van Assche @ 2024-09-09 17:08 UTC (permalink / raw)
  To: Bao D. Nguyen, Martin K . Petersen
  Cc: linux-scsi, Avri Altman, Manivannan Sadhasivam, Bean Huo,
	James E.J. Bottomley, Peter Wang, Andrew Halaney

On 9/6/24 4:59 PM, Bao D. Nguyen wrote:
> In the original code, if UFSHCD_QUIRK_SKIP_PH_CONFIGURATION is set, 
> ufshcd_device_init() would exit early. However, in this patch, if 
> UFSHCD_QUIRK_SKIP_PH_CONFIGURATION is set, the new function 
> ufshcd_activate_link() would return 0 which would cause the 
> ufshcd_device_init() to continue further down. As a result, the 
> ufshcd_device_init() would fail to handle the 
> UFSHCD_QUIRK_SKIP_PH_CONFIGURATION flag as its original intention, right?

Hi Bao,

I was assuming that UFSHCD_STATE_RESET != 0 but apparently it is zero. I
will fix this patch before I repost this patch series.

Thanks,

Bart.


^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2024-09-09 17:09 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-05 22:01 [PATCH v4 00/10] Simplify the UFS driver initialization code Bart Van Assche
2024-09-05 22:01 ` [PATCH v4 01/10] scsi: ufs: core: Introduce ufshcd_add_scsi_host() Bart Van Assche
2024-09-06 23:51   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 02/10] scsi: ufs: core: Introduce ufshcd_activate_link() Bart Van Assche
2024-09-06 23:59   ` Bao D. Nguyen
2024-09-09 17:08     ` Bart Van Assche
2024-09-05 22:01 ` [PATCH v4 03/10] scsi: ufs: core: Introduce ufshcd_post_device_init() Bart Van Assche
2024-09-07  0:24   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 04/10] scsi: ufs: core: Call ufshcd_add_scsi_host() later Bart Van Assche
2024-09-07 10:52   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 05/10] scsi: ufs: core: Move the ufshcd_device_init() call Bart Van Assche
2024-09-07 11:12   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call Bart Van Assche
2024-09-07 19:38   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 07/10] scsi: ufs: core: Expand " Bart Van Assche
2024-09-07 20:15   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 08/10] scsi: ufs: core: Move the MCQ scsi_add_host() call Bart Van Assche
2024-09-07 20:27   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 09/10] scsi: ufs: core: Move code out of an if-statement Bart Van Assche
2024-09-07 20:36   ` Bao D. Nguyen
2024-09-05 22:01 ` [PATCH v4 10/10] scsi: ufs: core: Remove the second argument of ufshcd_device_init() Bart Van Assche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox