Skip to contentSkip to conversation

scsi: ufs: cleanup ufs initialization

PATCH Applied Bean Huo 3 participants 8 messages Jun 3, 2020 – Jun 16, 2020 SCSI

lore
Share & tools
lore

Bean Huo

author
#0 lore raw

v5 cover scsi: ufs: cleanup ufs initialization

From: Bean Huo <[email protected]>

Resent this patchset since [email protected] and [email protected] rejected my email

Cleanup UFS descriptor length initialization, and delete some unnecessary code.

Changelog:
v4 - v5:
1. Rebased patch
2. In the patch 3/5, change "param_size > buff_len" to "(param_offset + param_size) > buff_len"

v3 - v4:
1. add desc_id >= QUERY_DESC_IDN_MAX check in patch 4/5 (Avri Altman)
2. update buff_len to hold the true descriptor size in 4/5 (Avri Altman)
3. add new patch 3/5

v2 - v3:
1. Fix typo in the commit message (Avri Altman & Bart van Assche)
2. Delete ufshcd_init_desc_sizes() in patch 3/4 (Stanley Chu)
3. Remove max_t() and buff_len in patch 1/4 (Bart van Assche)
4. Add patch 4/4 to compatable with 3.1 UFS unit descriptor length

v1 - v2:
1. split patch
2. fix one compiling WARNING (Reported-by: kbuild test robot <[email protected]>)

Bart van Assche (1):
scsi: ufs: remove max_t in ufs_get_device_desc

Bean Huo (4):
scsi: ufs: delete ufshcd_read_desc()
scsi: ufs: fix potential access NULL pointer while memcpy
scsi: ufs: cleanup ufs initialization path
scsi: ufs: add compatibility with 3.1 UFS unit descriptor length

 drivers/scsi/ufs/ufs.h     |  11 +-
 drivers/scsi/ufs/ufs_bsg.c |   5 +-
 drivers/scsi/ufs/ufshcd.c  | 207 +++++++++----------------------------
 drivers/scsi/ufs/ufshcd.h  |  16 +--
 4 files changed, 54 insertions(+), 185 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v5 1/5 scsi: ufs: remove max_t in ufs_get_device_desc

From: Bean Huo <[email protected]>

For the UFS device, the maximum descriptor size is 255, max_t called in ufs_get_device_desc() is useless.

---
 drivers/scsi/ufs/ufshcd.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
drivers/scsi/ufs/ufshcd.c +1 -4
index ad4fc829cbb2..2cf077ab9dfd 100644
@@ -6903,14 +6903,11 @@ static void ufs_fixup_device_setup(struct ufs_hba *hba)
 static int ufs_get_device_desc(struct ufs_hba *hba)
 {
 	int err;
-	size_t buff_len;
 	u8 model_index;
 	u8 *desc_buf;
 	struct ufs_dev_info *dev_info = &hba->dev_info;
 
-	buff_len = max_t(size_t, hba->desc_size.dev_desc,
-			 QUERY_DESC_MAX_SIZE + 1);
-	desc_buf = kmalloc(buff_len, GFP_KERNEL);
+	desc_buf = kmalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
 	if (!desc_buf) {
 		err = -ENOMEM;
 		goto out;
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v5 2/5 scsi: ufs: delete ufshcd_read_desc()

In reply to Bean Huo

From: Bean Huo <[email protected]>

Delete ufshcd_read_desc(). Instead, let caller directly call ufshcd_read_desc_param().

---
 drivers/scsi/ufs/ufshcd.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)
drivers/scsi/ufs/ufshcd.c +8 -19
index 2cf077ab9dfd..c18c2aadbe14 100644
@@ -3221,16 +3221,6 @@ int ufshcd_read_desc_param(struct ufs_hba *hba,
 	return ret;
 }
 
-static inline int ufshcd_read_desc(struct ufs_hba *hba,
-				   enum desc_idn desc_id,
-				   int desc_index,
-				   void *buf,
-				   u32 size)
-{
-	return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
-}
-
-
 /**
  * struct uc_string_id - unicode string
  *
@@ -3278,9 +3268,8 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
 	if (!uc_str)
 		return -ENOMEM;
 
-	ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING,
-			       desc_index, uc_str,
-			       QUERY_DESC_MAX_SIZE);
+	ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0,
+				     (u8 *)uc_str, QUERY_DESC_MAX_SIZE);
 	if (ret < 0) {
 		dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
 			QUERY_REQ_RETRIES, ret);
@@ -6711,8 +6700,8 @@ static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
 	if (!desc_buf)
 		return;
 
-	ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0,
-			desc_buf, buff_len);
+	ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0,
+				     desc_buf, buff_len);
 	if (ret) {
 		dev_err(hba->dev,
 			"%s: Failed reading power descriptor.len = %d ret = %d",
@@ -6913,8 +6902,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
 		goto out;
 	}
 
-	err = ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, desc_buf,
-			hba->desc_size.dev_desc);
+	err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf,
+				     hba->desc_size.dev_desc);
 	if (err) {
 		dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
 			__func__, err);
@@ -7196,8 +7185,8 @@ static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
 		goto out;
 	}
 
-	err = ufshcd_read_desc(hba, QUERY_DESC_IDN_GEOMETRY, 0,
-			desc_buf, buff_len);
+	err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0,
+				     desc_buf, buff_len);
 	if (err) {
 		dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
 				__func__, err);
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH v5 3/5 scsi: ufs: fix potential access NULL pointer while memcpy

In reply to Bean Huo

From: Bean Huo <[email protected]>

If param_offset is not 0, the memcpy length shouldn't be the true descriptor length.

---
 drivers/scsi/ufs/ufshcd.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/scsi/ufs/ufshcd.c +2 -2
index c18c2aadbe14..7163b268ed0b 100644
@@ -3210,8 +3210,8 @@ int ufshcd_read_desc_param(struct ufs_hba *hba,
 	}
 
 	/* Check wherher we will not copy more data, than available */
-	if (is_kmalloc && param_size > buff_len)
-		param_size = buff_len;
+	if (is_kmalloc && (param_offset + param_size) > buff_len)
+		param_size = buff_len - param_offset;
 
 	if (is_kmalloc)
 		memcpy(param_read_buf, &desc_buf[param_offset], param_size);
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v5 4/5 scsi: ufs: cleanup ufs initialization path

In reply to Bean Huo

From: Bean Huo <[email protected]>

At UFS initialization stage, to get the length of the descriptor, ufshcd_read_desc_length() being called 6 times. Instead, we will capture the descriptor size the first time we'll read it.

Delete unnecessary redundant code, remove ufshcd_read_desc_length(), ufshcd_init_desc_sizes(), and boost UFS initialization.

---
 drivers/scsi/ufs/ufs.h     |  10 ---
 drivers/scsi/ufs/ufs_bsg.c |   5 +-
 drivers/scsi/ufs/ufshcd.c  | 168 ++++++++-----------------------------
 drivers/scsi/ufs/ufshcd.h  |  16 +---
 4 files changed, 38 insertions(+), 161 deletions(-)
drivers/scsi/ufs/ufs.h +0 -10
index c70845d41449..8770255b5dc0 100644
@@ -200,16 +200,6 @@ enum desc_header_offset {
 	QUERY_DESC_DESC_TYPE_OFFSET	= 0x01,
 };
 
-enum ufs_desc_def_size {
-	QUERY_DESC_DEVICE_DEF_SIZE		= 0x59,
-	QUERY_DESC_CONFIGURATION_DEF_SIZE	= 0x90,
-	QUERY_DESC_UNIT_DEF_SIZE		= 0x2D,
-	QUERY_DESC_INTERCONNECT_DEF_SIZE	= 0x06,
-	QUERY_DESC_GEOMETRY_DEF_SIZE		= 0x48,
-	QUERY_DESC_POWER_DEF_SIZE		= 0x62,
-	QUERY_DESC_HEALTH_DEF_SIZE		= 0x25,
-};
-
 /* Unit descriptor parameters offsets in bytes*/
 enum unit_desc_param {
 	UNIT_DESC_PARAM_LEN			= 0x0,
drivers/scsi/ufs/ufs_bsg.c +2 -3
index 53dd87628cbe..27f54615ee84 100644
@@ -11,13 +11,12 @@ static int ufs_bsg_get_query_desc_size(struct ufs_hba *hba, int *desc_len,
 {
 	int desc_size = be16_to_cpu(qr->length);
 	int desc_id = qr->idn;
-	int ret;
 
 	if (desc_size <= 0)
 		return -EINVAL;
 
-	ret = ufshcd_map_desc_id_to_length(hba, desc_id, desc_len);
-	if (ret || !*desc_len)
+	ufshcd_map_desc_id_to_length(hba, desc_id, desc_len);
+	if (!*desc_len)
 		return -EINVAL;
 
 	*desc_len = min_t(int, *desc_len, desc_size);
drivers/scsi/ufs/ufshcd.c +33 -135

234 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.h +3 -13
index bf97d616e597..2b62869fa459 100644
@@ -236,16 +236,6 @@ struct ufs_dev_cmd {
 	struct ufs_query query;
 };
 
-struct ufs_desc_size {
-	int dev_desc;
-	int pwr_desc;
-	int geom_desc;
-	int interc_desc;
-	int unit_desc;
-	int conf_desc;
-	int hlth_desc;
-};
-
 /**
  * struct ufs_clk_info - UFS clock related info
  * @list: list headed by hba->clk_list_head
@@ -738,7 +728,7 @@ struct ufs_hba {
 	bool is_urgent_bkops_lvl_checked;
 
 	struct rw_semaphore clk_scaling_lock;
-	struct ufs_desc_size desc_size;
+	unsigned char desc_size[QUERY_DESC_IDN_MAX];
 	atomic_t scsi_block_reqs_cnt;
 
 	struct device		bsg_dev;
@@ -976,8 +966,8 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
 int ufshcd_hold(struct ufs_hba *hba, bool async);
 void ufshcd_release(struct ufs_hba *hba);
 
-int ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
-	int *desc_length);
+void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
+				  int *desc_length);
 
 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba);
 
Signature · 1 line
2.17.1
author
#5 lore raw

PATCH v5 5/5 scsi: ufs: add compatibility with 3.1 UFS unit descriptor length

In reply to Bean Huo

From: Bean Huo <[email protected]>

For UFS 3.1, the normal unit descriptor is 10 bytes larger than the RPMB unit, however, both descriptors share the same desc_idn, to cover both unit descriptors with one length, we choose the normal unit descriptor length by desc_index.

---
 drivers/scsi/ufs/ufs.h    |  1 +
 drivers/scsi/ufs/ufshcd.c | 11 ++++++++---
 2 files changed, 9 insertions(+), 3 deletions(-)
drivers/scsi/ufs/ufs.h +1 -0
index 8770255b5dc0..250e1a905a14 100644
@@ -63,6 +63,7 @@
 #define UFS_UPIU_MAX_UNIT_NUM_ID	0x7F
 #define UFS_MAX_LUNS		(SCSI_W_LUN_BASE + UFS_UPIU_MAX_UNIT_NUM_ID)
 #define UFS_UPIU_WLUN_ID	(1 << 7)
+#define UFS_RPMB_UNIT		0xC4
 
 /* WriteBooster buffer is available only for the logical unit from 0 to 7 */
 #define UFS_UPIU_MAX_WB_LUN_ID	8
drivers/scsi/ufs/ufshcd.c +8 -3
index 5ad0eebccc98..f283b9eb97ac 100644
@@ -3070,11 +3070,16 @@ void ufshcd_map_desc_id_to_length(struct ufs_hba *hba, enum desc_idn desc_id,
 EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
 
 static void ufshcd_update_desc_length(struct ufs_hba *hba,
-				      enum desc_idn desc_id,
+				      enum desc_idn desc_id, int desc_index,
 				      unsigned char desc_len)
 {
 	if (hba->desc_size[desc_id] == QUERY_DESC_MAX_SIZE &&
-	    desc_id != QUERY_DESC_IDN_STRING)
+	    desc_id != QUERY_DESC_IDN_STRING && desc_index != UFS_RPMB_UNIT)
+		/* For UFS 3.1, the normal unit descriptor is 10 bytes larger
+		 * than the RPMB unit, however, both descriptors share the same
+		 * desc_idn, to cover both unit descriptors with one length, we
+		 * choose the normal unit descriptor length by desc_index.
+		 */
 		hba->desc_size[desc_id] = desc_len;
 }
 
@@ -3143,7 +3148,7 @@ int ufshcd_read_desc_param(struct ufs_hba *hba,
 
 	/* Update descriptor length */
 	buff_len = desc_buf[QUERY_DESC_LENGTH_OFFSET];
-	ufshcd_update_desc_length(hba, desc_id, buff_len);
+	ufshcd_update_desc_length(hba, desc_id, desc_index, buff_len);
 
 	/* Check wherher we will not copy more data, than available */
 	if (is_kmalloc && (param_offset + param_size) > buff_len)
Signature · 1 line
2.17.1
#6 lore raw
In reply to Bean Huo

Looks fine to me.
Thanks,
Avri

Quoted 49 lines

From: Bean Huo <[email protected]>

Resent this patchset since [email protected] and [email protected] rejected my email

Cleanup UFS descriptor length initialization, and delete some unnecessary code.

Changelog:
v4 - v5:
1. Rebased patch
2. In the patch 3/5, change "param_size > buff_len" to "(param_offset + param_size) > buff_len"

v3 - v4:
1. add desc_id >= QUERY_DESC_IDN_MAX check in patch 4/5 (Avri Altman)
2. update buff_len to hold the true descriptor size in 4/5 (Avri Altman)
3. add new patch 3/5

v2 - v3:
1. Fix typo in the commit message (Avri Altman & Bart van Assche)
2. Delete ufshcd_init_desc_sizes() in patch 3/4 (Stanley Chu)
3. Remove max_t() and buff_len in patch 1/4 (Bart van Assche)
4. Add patch 4/4 to compatable with 3.1 UFS unit descriptor length

v1 - v2:
1. split patch
2. fix one compiling WARNING (Reported-by: kbuild test robot
<[email protected]>)

Bart van Assche (1):
scsi: ufs: remove max_t in ufs_get_device_desc

Bean Huo (4):
scsi: ufs: delete ufshcd_read_desc()
scsi: ufs: fix potential access NULL pointer while memcpy
scsi: ufs: cleanup ufs initialization path
scsi: ufs: add compatibility with 3.1 UFS unit descriptor length

 drivers/scsi/ufs/ufs.h     |  11 +-
 drivers/scsi/ufs/ufs_bsg.c |   5 +-
 drivers/scsi/ufs/ufshcd.c  | 207 +++++++++----------------------------
 drivers/scsi/ufs/ufshcd.h  |  16 +--
 4 files changed, 54 insertions(+), 185 deletions(-)

--
2.17.1

#7 lore raw
In reply to Bean Huo
Quoted 6 lines · Bean Huo

Resent this patchset since [email protected] and [email protected] rejected my email

Cleanup UFS descriptor length initialization, and delete some unnecessary code.

Quoted 7 lines

Changelog:
v4 - v5:
1. Rebased patch
2. In the patch 3/5, change "param_size > buff_len" to "(param_offset + param_size) > buff_len"

[...]

Applied to 5.9/scsi-queue, thanks!

[1/5] scsi: ufs: Remove max_t in ufs_get_device_desc

      https://git.kernel.org/mkp/scsi/c/458a45f5262b
[2/5] scsi: ufs: Delete ufshcd_read_desc()
      https://git.kernel.org/mkp/scsi/c/c4607a09450d
[3/5] scsi: ufs: Fix potential NULL pointer access during memcpy
      https://git.kernel.org/mkp/scsi/c/cbe193f6f093
[4/5] scsi: ufs: Clean up ufs initialization path
      https://git.kernel.org/mkp/scsi/c/7a0bf85b5e18
[5/5] scsi: ufs: Add compatibility with 3.1 UFS unit descriptor length
      https://git.kernel.org/mkp/scsi/c/72fb690eece1
Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering