Skip to contentSkip to conversation

Combine the UFS driver scsi_add_host() calls

PATCH Superseded Bart Van Assche 3 participants 20 messages Sep 10, 2024 – Oct 9, 2024

Subsystems (2)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

v5 cover Combine the UFS driver scsi_add_host() calls

Hi Martin,

In the UFS driver 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.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v4:
- Left out the changes that remove the 'init_dev_params' argument.
- Added a few new patches.

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_post_device_init()
scsi: ufs: core: Call ufshcd_add_scsi_host() later
scsi: ufs: core: Introduce ufshcd_process_device_init_result()
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: Remove code that is no longer needed
scsi: ufs: core: Move the MCQ scsi_add_host() call
scsi: ufs: core: Move code out of an if-statement

 drivers/ufs/core/ufshcd.c | 288 ++++++++++++++++++++++++--------------
 include/ufs/ufshcd.h      |   3 +
 2 files changed, 183 insertions(+), 108 deletions(-)
author reviewer
#1 lore raw

PATCH v5 1/10 scsi: ufs: core: Introduce ufshcd_add_scsi_host()

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.

---
 drivers/ufs/core/ufshcd.c | 84 ++++++++++++++++++++++++---------------
 1 file changed, 53 insertions(+), 31 deletions(-)
drivers/ufs/core/ufshcd.c +53 -31
index 8ea5a82503a9..ecf6da2efed1 100644
@@ -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:
author reviewer
#2 lore raw

PATCH v5 2/10 scsi: ufs: core: Introduce ufshcd_post_device_init()

In reply to Bart Van Assche

Prepare for inlining one ufshcd_device_init() call by introducing the new function ufshcd_post_device_init(). No functionality has been changed.

---
 drivers/ufs/core/ufshcd.c | 62 ++++++++++++++++++++++-----------------
 1 file changed, 35 insertions(+), 27 deletions(-)
drivers/ufs/core/ufshcd.c +35 -27
index ecf6da2efed1..0ca20f069cbd 100644
@@ -8709,6 +8709,40 @@ static void ufshcd_config_mcq(struct ufs_hba *hba)
 		 hba->nutrs);
 }
 
+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)
 {
 	int ret;
@@ -8778,33 +8812,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);
 }
 
 /**
author reviewer
#3 lore raw

PATCH v5 3/10 scsi: ufs: core: Call ufshcd_add_scsi_host() later

In reply to Bart Van Assche

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

---
 drivers/ufs/core/ufshcd.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/ufs/core/ufshcd.c +5 -11
index 0ca20f069cbd..6ebafd08ad6e 100644
@@ -10546,10 +10546,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);
 
@@ -10561,7 +10557,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;
 	}
 
 	/*
@@ -10596,6 +10592,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);
 
@@ -10603,12 +10603,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);
author reviewer
#4 lore raw

PATCH v5 4/10 scsi: ufs: core: Introduce ufshcd_process_device_init_result()

In reply to Bart Van Assche

Prepare for moving a ufshcd_device_init() call from inside ufshcd_probe_hba() into the ufshcd_probe_hba() callers by introducing the function ufshcd_process_device_init_result(). No functionality has been changed.

---
 drivers/ufs/core/ufshcd.c | 35 ++++++++++++++++++++++++-----------
 1 file changed, 24 insertions(+), 11 deletions(-)
drivers/ufs/core/ufshcd.c +24 -11
index 6ebafd08ad6e..1094c1ba2212 100644
@@ -7664,6 +7664,29 @@ static int ufshcd_abort(struct scsi_cmnd *cmd)
 	return err;
 }
 
+/**
+ * ufshcd_process_device_init_result - Process the ufshcd_device_init() result.
+ * @hba: UFS host controller instance.
+ * @start: time when the ufshcd_device_init() call started.
+ * @ret: ufshcd_device_init() return value.
+ */
+static void ufshcd_process_device_init_result(struct ufs_hba *hba,
+					ktime_t device_init_start, int ret)
+{
+	unsigned long flags;
+
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	if (ret)
+		hba->ufshcd_state = UFSHCD_STATE_ERROR;
+	else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
+		hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+
+	trace_ufshcd_init(dev_name(hba->dev), ret,
+			ktime_to_us(ktime_sub(ktime_get(), device_init_start)),
+			hba->curr_dev_pwr_mode, hba->uic_link_state);
+}
+
 /**
  * ufshcd_host_reset_and_restore - reset and restore host controller
  * @hba: per-adapter instance
@@ -8827,7 +8850,6 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 {
 	ktime_t start = ktime_get();
-	unsigned long flags;
 	int ret;
 
 	ret = ufshcd_device_init(hba, init_dev_params);
@@ -8873,16 +8895,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 	ufshcd_configure_auto_hibern8(hba);
 
 out:
-	spin_lock_irqsave(hba->host->host_lock, flags);
-	if (ret)
-		hba->ufshcd_state = UFSHCD_STATE_ERROR;
-	else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
-		hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
-	spin_unlock_irqrestore(hba->host->host_lock, flags);
-
-	trace_ufshcd_init(dev_name(hba->dev), ret,
-		ktime_to_us(ktime_sub(ktime_get(), start)),
-		hba->curr_dev_pwr_mode, hba->uic_link_state);
+	ufshcd_process_device_init_result(hba, start, ret);
 	return ret;
 }
 
#5 lore raw

Re: [PATCH v5 04/10] scsi: ufs: core: Introduce ufshcd_process_device_init_result()

Quoted 4 lines · Bart Van Assche

+/**
+ * ufshcd_process_device_init_result - Process the ufshcd_device_init() result.
+ * @hba: UFS host controller instance.
+ * @start: time when the ufshcd_device_init() call started.

Replace @start with @device_init_start?

Quoted 5 lines

+ * @ret: ufshcd_device_init() return value.
+ */

drivers/ufs/core/ufshcd.c +3 -0
+static void ufshcd_process_device_init_result(struct ufs_hba *hba,
+					ktime_t device_init_start, int ret)
+{
author reviewer
#6 lore raw

Re: [PATCH v5 04/10] scsi: ufs: core: Introduce ufshcd_process_device_init_result()

Quoted 9 lines · Bao D. Nguyen
Quoted 5 lines · Bart Van Assche

+/**
+ * ufshcd_process_device_init_result - Process the ufshcd_device_init() result.
+ * @hba: UFS host controller instance.
+ * @start: time when the ufshcd_device_init() call started.

Replace @start with @device_init_start?

I will fix this.

Thanks,

Bart.

#7 lore raw

Re: [PATCH v5 04/10] scsi: ufs: core: Introduce ufshcd_process_device_init_result()

In reply to Bart Van Assche
kernel test robot noticed the following build warnings:

Hi Bart,

kernel test robot noticed the following build warnings:

[auto build test WARNING on mkp-scsi/for-next]
[cannot apply to jejb-scsi/for-next linus/master v6.11-rc7 next-20240913] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Bart-Van-Assche/scsi-ufs-core-Introduce-ufshcd_add_scsi_host/20240911-055406
base:   https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git for-next
patch link:    https://lore.kernel.org/r/20240910215139.3352387-5-bvanassche%40acm.org
patch subject: [PATCH v5 04/10] scsi: ufs: core: Introduce ufshcd_process_device_init_result()
config: i386-allyesconfig (https://download.01.org/0day-ci/archive/20240914/[email protected]/config)
compiler: gcc-12 (Debian 12.2.0-14) 12.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240914/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> drivers/ufs/core/ufshcd.c:7675: warning: Function parameter or struct member 'device_init_start' not described in 'ufshcd_process_device_init_result'
>> drivers/ufs/core/ufshcd.c:7675: warning: Excess function parameter 'start' description in 'ufshcd_process_device_init_result'

vim +7675 drivers/ufs/core/ufshcd.c

7666
7667 /**
7668 * ufshcd_process_device_init_result - Process the ufshcd_device_init() result.
7669 * @hba: UFS host controller instance.
7670 * @start: time when the ufshcd_device_init() call started.
7671 * @ret: ufshcd_device_init() return value.
7672 */
7673 static void ufshcd_process_device_init_result(struct ufs_hba *hba,
7674 ktime_t device_init_start, int ret)

> 7675	{

7676 unsigned long flags;
7677
7678 spin_lock_irqsave(hba->host->host_lock, flags);
7679 if (ret)
7680 hba->ufshcd_state = UFSHCD_STATE_ERROR;
7681 else if (hba->ufshcd_state == UFSHCD_STATE_RESET)
7682 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
7683 spin_unlock_irqrestore(hba->host->host_lock, flags);
7684
7685 trace_ufshcd_init(dev_name(hba->dev), ret,
7686 ktime_to_us(ktime_sub(ktime_get(), device_init_start)),
7687 hba->curr_dev_pwr_mode, hba->uic_link_state);
7688 }
7689

Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
author reviewer
#8 lore raw

PATCH v5 5/10 scsi: ufs: core: Move the ufshcd_device_init() call

In reply to Bart Van Assche

Move the ufshcd_device_init() and ufshcd_process_device_init_result() calls to the ufshcd_probe_hba() callers. 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().

---
 drivers/ufs/core/ufshcd.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
drivers/ufs/core/ufshcd.c +18 -13
index 1094c1ba2212..f62d257a92da 100644
@@ -298,6 +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_probe_hba(struct ufs_hba *hba, bool init_dev_params);
 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
@@ -7716,8 +7717,14 @@ 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) {
+		ktime_t device_init_start = ktime_get();
+
+		err = ufshcd_device_init(hba, /*init_dev_params=*/false);
+		if (!err)
+			err = ufshcd_probe_hba(hba, false);
+		ufshcd_process_device_init_result(hba, device_init_start, err);
+	}
 
 	if (err)
 		dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
@@ -8849,13 +8856,8 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
  */
 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 {
-	ktime_t start = ktime_get();
 	int ret;
 
-	ret = ufshcd_device_init(hba, init_dev_params);
-	if (ret)
-		goto out;
-
 	if (!hba->pm_op_in_progress &&
 	    (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) {
 		/* Reset the device and controller before doing reinit */
@@ -8868,13 +8870,13 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 			dev_err(hba->dev, "Host controller enable failed\n");
 			ufshcd_print_evt_hist(hba);
 			ufshcd_print_host_state(hba);
-			goto out;
+			return ret;
 		}
 
 		/* Reinit the device */
 		ret = ufshcd_device_init(hba, init_dev_params);
 		if (ret)
-			goto out;
+			return ret;
 	}
 
 	ufshcd_print_pwr_info(hba);
@@ -8894,9 +8896,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 		ufshcd_write_ee_control(hba);
 	ufshcd_configure_auto_hibern8(hba);
 
-out:
-	ufshcd_process_device_init_result(hba, start, ret);
-	return ret;
+	return 0;
 }
 
 /**
@@ -8907,11 +8907,16 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 {
 	struct ufs_hba *hba = (struct ufs_hba *)data;
+	ktime_t device_init_start;
 	int ret;
 
 	down(&hba->host_sem);
 	/* Initialize hba, detect and initialize UFS device */
-	ret = ufshcd_probe_hba(hba, true);
+	device_init_start = ktime_get();
+	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
+	if (ret == 0)
+		ret = ufshcd_probe_hba(hba, true);
+	ufshcd_process_device_init_result(hba, device_init_start, ret);
 	up(&hba->host_sem);
 	if (ret)
 		goto out;
#9 lore raw

Re: [PATCH v5 05/10] scsi: ufs: core: Move the ufshcd_device_init() call

Quoted 25 lines · Bart Van Assche

Move the ufshcd_device_init() and ufshcd_process_device_init_result() calls to the ufshcd_probe_hba() callers. 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().

---
  drivers/ufs/core/ufshcd.c | 31 ++++++++++++++++++-------------
  1 file changed, 18 insertions(+), 13 deletions(-)
drivers/ufs/core/ufshcd.c +1 -0
index 1094c1ba2212..f62d257a92da 100644
@@ -298,6 +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_probe_hba(struct ufs_hba *hba, bool init_dev_params);
  static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
  static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
@@ -7716,8 +7717,14 @@ static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
  	err = ufshcd_hba_enable(hba);
 

drivers/ufs/core/ufshcd.c static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) · line 7726

Quoted 10 lines
drivers/ufs/core/ufshcd.c +7 -2
  	/* Establish the link again and restore the device */
-	if (!err)
-		err = ufshcd_probe_hba(hba, false);
+	if (!err) {
+		ktime_t device_init_start = ktime_get();
+
+		err = ufshcd_device_init(hba, /*init_dev_params=*/false);
+		if (!err)
+			err = ufshcd_probe_hba(hba, false);
+		ufshcd_process_device_init_result(hba, device_init_start, err);

Hi Bart, IMO the function name ufshcd_process_device_init_result() is not correctly describing the original code. As you can see, the "ret" passed into the function here is from the result of the ufshcd_probe_hba(), not the result of the ufshcd_device_init(). Same comment for the name device_init_start. Originally, it is the time spent in the ufshcd_probe_hba().

Quoted 50 lines
drivers/ufs/core/ufshcd.c +5 -10
+	}
 
  	if (err)
  		dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
@@ -8849,13 +8856,8 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
   */
  static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  {
-	ktime_t start = ktime_get();
  	int ret;
 
-	ret = ufshcd_device_init(hba, init_dev_params);
-	if (ret)
-		goto out;
-
  	if (!hba->pm_op_in_progress &&
  	    (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) {
  		/* Reset the device and controller before doing reinit */
@@ -8868,13 +8870,13 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  			dev_err(hba->dev, "Host controller enable failed\n");
  			ufshcd_print_evt_hist(hba);
  			ufshcd_print_host_state(hba);
-			goto out;
+			return ret;
  		}
 
  		/* Reinit the device */
  		ret = ufshcd_device_init(hba, init_dev_params);
  		if (ret)
-			goto out;
+			return ret;
  	}
 
  	ufshcd_print_pwr_info(hba);
@@ -8894,9 +8896,7 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  		ufshcd_write_ee_control(hba);
  	ufshcd_configure_auto_hibern8(hba);
 
-out:
-	ufshcd_process_device_init_result(hba, start, ret);
-	return ret;
+	return 0;
  }
 
  /**
@@ -8907,11 +8907,16 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  {
  	struct ufs_hba *hba = (struct ufs_hba *)data;
+	ktime_t device_init_start;

drivers/ufs/core/ufshcd.c static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params) · line 8919

Quoted 10 lines
drivers/ufs/core/ufshcd.c +5 -1
  	int ret;
 
  	down(&hba->host_sem);
  	/* Initialize hba, detect and initialize UFS device */
-	ret = ufshcd_probe_hba(hba, true);
+	device_init_start = ktime_get();
+	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
+	if (ret == 0)
+		ret = ufshcd_probe_hba(hba, true);
+	ufshcd_process_device_init_result(hba, device_init_start, ret);

Same comment about the function name. The "ret" here is the result of ufshcd_probe_hba().

Quoted 4 lines
drivers/ufs/core/ufshcd.c +0 -0
  	up(&hba->host_sem);
  	if (ret)
  		goto out;
author reviewer
#10 lore raw

Re: [PATCH v5 05/10] scsi: ufs: core: Move the ufshcd_device_init() call

Quoted 1 line · Bao D. Nguyen

Hi Bart, IMO the function name ufshcd_process_device_init_result() is not correctly describing the original code. As you can see, the "ret" passed into the function here is from the result of the ufshcd_probe_hba(), not the result of the ufshcd_device_init().

I will rename that function into ufshcd_process_probe_result().

Thanks,

Bart.

author reviewer
#11 lore raw

PATCH v5 6/10 scsi: ufs: core: Move the ufshcd_device_init(hba, true) call

In reply to Bart Van Assche

ufshcd_async_scan() is called (asynchronously) only by ufshcd_init(). 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.

Since ufshcd_add_scsi_host() checks whether or not the MCQ mode is enabled and since ufshcd_device_init() may modify hba->mcq_enabled, only call scsi_add_host() from ufshcd_device_init() if the SCSI host has not yet been added by ufshcd_device_init().

---
 drivers/ufs/core/ufshcd.c | 16 +++++++++-------
 include/ufs/ufshcd.h      |  3 +++
 2 files changed, 12 insertions(+), 7 deletions(-)
drivers/ufs/core/ufshcd.c +9 -7
index f62d257a92da..a3c5493ccc8f 100644
@@ -8907,16 +8907,12 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
 static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 {
 	struct ufs_hba *hba = (struct ufs_hba *)data;
-	ktime_t device_init_start;
 	int ret;
 
 	down(&hba->host_sem);
 	/* Initialize hba, detect and initialize UFS device */
-	device_init_start = ktime_get();
-	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
-	if (ret == 0)
-		ret = ufshcd_probe_hba(hba, true);
-	ufshcd_process_device_init_result(hba, device_init_start, ret);
+	ret = ufshcd_probe_hba(hba, true);
+	ufshcd_process_device_init_result(hba, hba->device_init_start, ret);
 	up(&hba->host_sem);
 	if (ret)
 		goto out;
@@ -10387,7 +10383,7 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
 {
 	int err;
 
-	if (!is_mcq_supported(hba)) {
+	if (!hba->scsi_host_added) {
 		err = scsi_add_host(hba->host, hba->dev);
 		if (err) {
 			dev_err(hba->dev, "scsi_add_host failed\n");
@@ -10610,6 +10606,12 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	 */
 	ufshcd_set_ufs_dev_active(hba);
 
+	/* Initialize hba, detect and initialize UFS device */
+	hba->device_init_start = ktime_get();
+	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;
include/ufs/ufshcd.h +3 -0
index a43b14276bc3..55ec89fa16af 100644
@@ -839,6 +839,7 @@ enum ufshcd_mcq_opr {
  * @dev: device handle
  * @ufs_device_wlun: WLUN that controls the entire UFS device.
  * @hwmon_device: device instance registered with the hwmon core.
+ * @device_init_start: start time of first ufshcd_device_init() call.
  * @curr_dev_pwr_mode: active UFS device power mode.
  * @uic_link_state: active state of the link to the UFS device.
  * @rpm_lvl: desired UFS power management level during runtime PM.
@@ -972,6 +973,8 @@ struct ufs_hba {
 	struct device *hwmon_device;
 #endif
 
+	ktime_t device_init_start;
+
 	enum ufs_dev_pwr_mode curr_dev_pwr_mode;
 	enum uic_link_state uic_link_state;
 	/* Desired UFS power management level during runtime PM */
#12 lore raw

Re: [PATCH v5 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call

Quoted 30 lines · Bart Van Assche

ufshcd_async_scan() is called (asynchronously) only by ufshcd_init(). 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.

Since ufshcd_add_scsi_host() checks whether or not the MCQ mode is enabled and since ufshcd_device_init() may modify hba->mcq_enabled, only call scsi_add_host() from ufshcd_device_init() if the SCSI host has not yet been added by ufshcd_device_init().

---
  drivers/ufs/core/ufshcd.c | 16 +++++++++-------
  include/ufs/ufshcd.h      |  3 +++
  2 files changed, 12 insertions(+), 7 deletions(-)
drivers/ufs/core/ufshcd.c +0 -1
index f62d257a92da..a3c5493ccc8f 100644
@@ -8907,16 +8907,12 @@ static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params)
  static void ufshcd_async_scan(void *data, async_cookie_t cookie)
  {
  	struct ufs_hba *hba = (struct ufs_hba *)data;
-	ktime_t device_init_start;
  	int ret;

drivers/ufs/core/ufshcd.c static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params) · line 8915

Quoted 10 lines
drivers/ufs/core/ufshcd.c +2 -5
 
  	down(&hba->host_sem);
  	/* Initialize hba, detect and initialize UFS device */
-	device_init_start = ktime_get();
-	ret = ufshcd_device_init(hba, /*init_dev_params=*/true);
-	if (ret == 0)
-		ret = ufshcd_probe_hba(hba, true);
-	ufshcd_process_device_init_result(hba, device_init_start, ret);
+	ret = ufshcd_probe_hba(hba, true);
+	ufshcd_process_device_init_result(hba, hba->device_init_start, ret);

This patch probably changed the original trace print. In the original code, the trace only prints the time spent in probe_hba(). In this patch however the trace print includes the time spent in ufshcd_add_scsi_host() plus the time wait for async_schedule(ufshcd_async_scan, hba).

Quoted 38 lines
drivers/ufs/core/ufshcd.c +7 -1
  	up(&hba->host_sem);
  	if (ret)
  		goto out;
@@ -10387,7 +10383,7 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
  {
  	int err;
 
-	if (!is_mcq_supported(hba)) {
+	if (!hba->scsi_host_added) {
  		err = scsi_add_host(hba->host, hba->dev);
  		if (err) {
  			dev_err(hba->dev, "scsi_add_host failed\n");
@@ -10610,6 +10606,12 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
  	 */
  	ufshcd_set_ufs_dev_active(hba);
 
+	/* Initialize hba, detect and initialize UFS device */
+	hba->device_init_start = ktime_get();
+	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;
include/ufs/ufshcd.h +1 -0
index a43b14276bc3..55ec89fa16af 100644
@@ -839,6 +839,7 @@ enum ufshcd_mcq_opr {
   * @dev: device handle
   * @ufs_device_wlun: WLUN that controls the entire UFS device.
   * @hwmon_device: device instance registered with the hwmon core.
+ * @device_init_start: start time of first ufshcd_device_init() call.
   * @curr_dev_pwr_mode: active UFS device power mode.
   * @uic_link_state: active state of the link to the UFS device.
   * @rpm_lvl: desired UFS power management level during runtime PM.

include/ufs/ufshcd.h struct ufs_hba { · line 976

Quoted 4 lines
include/ufs/ufshcd.h +1 -0
@@ -972,6 +973,8 @@ struct ufs_hba {
  	struct device *hwmon_device;
  #endif
 
+	ktime_t device_init_start;

IMO, it's kinda overkill to add this device_init_start member for the purpose of trace print the time spent in the first probe_hba() only.
How about print the time spent in probe_hba() outside of the ufshcd_process_device_init_result() function so that you don't need to pass device_init_start into ufshcd_process_device_init_result()?

Quoted 5 lines
include/ufs/ufshcd.h +1 -0
+
  	enum ufs_dev_pwr_mode curr_dev_pwr_mode;
  	enum uic_link_state uic_link_state;
  	/* Desired UFS power management level during runtime PM */
author reviewer
#13 lore raw

Re: [PATCH v5 06/10] scsi: ufs: core: Move the ufshcd_device_init(hba, true) call

Quoted 1 line · Bao D. Nguyen

On 9/10/2024 2:50 PM, Bart Van Assche wrote:

() plus the time wait for

Quoted 1 line

async_schedule(ufshcd_async_scan, hba).

The above comment is not clear to me. I'm not aware of any code in the UFS driver that waits for the asynchronously called ufshcd_async_scan() function to finish? It's not clear to me either how the time spent in ufshcd_add_scsi_host() would be included in the trace print?

Quoted 4 lines
Quoted 1 line

+    ktime_t device_init_start;

IMO, it's kinda overkill to add this device_init_start member for the purpose of trace print the time spent in the first probe_hba() only.
How about print the time spent in probe_hba() outside of the ufshcd_process_device_init_result() function so that you don't need to pass device_init_start into ufshcd_process_device_init_result()?

I will remove the above struct ufs_hba member again.

Thanks,

Bart.

author reviewer
#14 lore raw

PATCH v5 7/10 scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call

In reply to Bart Van Assche

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.

---
 drivers/ufs/core/ufshcd.c | 55 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 54 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +54 -1
index a3c5493ccc8f..efa9c177a80f 100644
@@ -10608,7 +10608,60 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 
 	/* Initialize hba, detect and initialize UFS device */
 	hba->device_init_start = ktime_get();
-	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
+
+	hba->ufshcd_state = UFSHCD_STATE_RESET;
+
+	err = ufshcd_link_startup(hba);
+	if (err)
+		goto out_disable;
+
+	if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION)
+		goto initialized;
+
+	/* Debug counters initialization */
+	ufshcd_clear_dbg_ufs_stats(hba);
+
+	/* UniPro link is active now */
+	ufshcd_set_link_active(hba);
+
+	/* Verify device initialization by sending NOP OUT UPIU */
+	err = ufshcd_verify_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	/* Initiate UFS initialization, and waiting until completion */
+	err = ufshcd_complete_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	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);
+
+initialized:
+	ufshcd_process_device_init_result(hba, hba->device_init_start, err);
 	if (err)
 		goto out_disable;
 
#15 lore raw

Re: [PATCH v5 07/10] scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call

Quoted 62 lines · Bart Van Assche

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.

---
  drivers/ufs/core/ufshcd.c | 55 ++++++++++++++++++++++++++++++++++++++-
  1 file changed, 54 insertions(+), 1 deletion(-)
drivers/ufs/core/ufshcd.c +44 -1
index a3c5493ccc8f..efa9c177a80f 100644
@@ -10608,7 +10608,60 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 
  	/* Initialize hba, detect and initialize UFS device */
  	hba->device_init_start = ktime_get();
-	err = ufshcd_device_init(hba, /*init_dev_params=*/true);
+
+	hba->ufshcd_state = UFSHCD_STATE_RESET;
+
+	err = ufshcd_link_startup(hba);
+	if (err)
+		goto out_disable;
+
+	if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION)
+		goto initialized;
+
+	/* Debug counters initialization */
+	ufshcd_clear_dbg_ufs_stats(hba);
+
+	/* UniPro link is active now */
+	ufshcd_set_link_active(hba);
+
+	/* Verify device initialization by sending NOP OUT UPIU */
+	err = ufshcd_verify_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	/* Initiate UFS initialization, and waiting until completion */
+	err = ufshcd_complete_dev_init(hba);
+	if (err)
+		goto out_disable;
+
+	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) {

drivers/ufs/core/ufshcd.c int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) · line 10664

Quoted 10 lines
drivers/ufs/core/ufshcd.c +10 -0
+			dev_err(hba->dev, "scsi_add_host failed\n");
+			goto out_disable;
+		}
+		hba->scsi_host_added = true;
+	}
+
+	err = ufshcd_post_device_init(hba);
+
+initialized:
+	ufshcd_process_device_init_result(hba, hba->device_init_start, err);

I have similar comment as in previous patch #6. This patch probably changed the print. In the original code, it prints the time spent in probe_hba(), but here it prints some part of the time spent in ufshcd_init(). There will be some trace prints during init, but the data it prints would have inconsistent meaning.

Quoted 4 lines
drivers/ufs/core/ufshcd.c +0 -0
  	if (err)
  		goto out_disable;
 
author reviewer
#16 lore raw

Re: [PATCH v5 07/10] scsi: ufs: core: Expand the ufshcd_device_init(hba, true) call

Quoted 4 lines · Bao D. Nguyen
Quoted 1 line · Bart Van Assche

+    ufshcd_process_device_init_result(hba, hba->device_init_start, err);

I have similar comment as in previous patch #6. This patch probably changed the print. In the original code, it prints the time spent in probe_hba(), but here it prints some part of the time spent in ufshcd_init(). There will be some trace prints during init, but the data it prints would have inconsistent meaning.

I will see what I can do about this.

Thanks,

Bart.

author reviewer
#17 lore raw

PATCH v5 8/10 scsi: ufs: core: Remove code that is no longer needed

In reply to Bart Van Assche

Previous changes guarantee that hba->scsi_host_added is true before ufshcd_device_init() is called. Hence, remove the code from ufshcd_device_init() that depends on hba->scsi_host_added being false.

---
 drivers/ufs/core/ufshcd.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)
drivers/ufs/core/ufshcd.c +3 -20
index efa9c177a80f..ce4f9c8554c3 100644
@@ -8776,7 +8776,8 @@ static int ufshcd_post_device_init(struct ufs_hba *hba)
 static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params)
 {
 	int ret;
-	struct Scsi_Host *host = hba->host;
+
+	WARN_ON_ONCE(!hba->scsi_host_added);
 
 	hba->ufshcd_state = UFSHCD_STATE_RESET;
 
@@ -8817,25 +8818,7 @@ static int ufshcd_device_init(struct ufs_hba *hba, bool 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)) {
+		if (is_mcq_supported(hba)) {
 			/* UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH is set */
 			ufshcd_config_mcq(hba);
 			ufshcd_mcq_enable(hba);
author reviewer
#18 lore raw

PATCH v5 9/10 scsi: ufs: core: Move the MCQ scsi_add_host() call

In reply to Bart Van Assche

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 prepares for combining the two scsi_add_host() calls.

---
 drivers/ufs/core/ufshcd.c | 40 +++++++++++++++++++--------------------
 1 file changed, 19 insertions(+), 21 deletions(-)
drivers/ufs/core/ufshcd.c +19 -21
index ce4f9c8554c3..8e4e25da9a6f 100644
@@ -10366,7 +10366,25 @@ static int ufshcd_add_scsi_host(struct ufs_hba *hba)
 {
 	int err;
 
-	if (!hba->scsi_host_added) {
+	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");
@@ -10621,26 +10639,6 @@ int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
 	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);
 
 initialized:
author reviewer
#19 lore raw

PATCH v5 10/10 scsi: ufs: core: Move code out of an if-statement

In reply to Bart Van Assche

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

---
 drivers/ufs/core/ufshcd.c | 20 +++++++-------------
 1 file changed, 7 insertions(+), 13 deletions(-)
drivers/ufs/core/ufshcd.c +7 -13
index 8e4e25da9a6f..2ad2c3e968c4 100644
@@ -10378,21 +10378,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,