Skip to contentSkip to conversation

scsi: ufs: add UFS Host Performance Booster(HPB) support

RFC PATCH In review [email protected] 6 participants 22 messages May 4, 2020 – May 8, 2020 SCSI

lore
Share & tools
lore
#0 lore raw

v3 cover scsi: ufs: add UFS Host Performance Booster(HPB) support

From: Bean Huo <[email protected]>

Re-sent this patchset since the reference link missed in the cover letter.

This patchset is an RFC to add UFS HPB support in the upstream Linux. Before this series, I have submitted two versions [2] and [4] of UFS HPB driver in the community. The first two series HPB driver patchsets follow the Samsung HPB driver[1] approach, in which the HPB driver submits its HPB READ BUFFER and HPB WRITE BUFFER requests to the SCSI device->request_queueu to execute. Since this fly submission is unacceptable, in this RFC version, I changed it, and now the HPB requests will be directly submitted to UFS device, don't go backwards to SCSI mid-layer. In this approach, the L2P mapping entries loading path will be shorter, and the latency is smaller.

Regarding the UFS HPB feature, which is defined in Jedec Standard Universal Flash (UFS) Host Performance Booster (HPB) Extension Version 1.0, its purpose is to improve read performance by utilizing the host side memory. For the more detail information about HPB, you can refer to the HPB Spec. If the approach in this RFC patchset is accepted and feasible, it is necessary to add a document to detail HPB feature and its implementation later.

Based on the Avri's comments in the second patchset, to be reviewable patches, I shortened HPB driver, deleted HPB host control mode. Also, for change tracing, this patch is RFC v3, including the first two patchsets changes.

This series patch is still based on the [3] branch 5.8/scsi-queue

v2--v3
1. delete GET_BYTE_* and SHIFT_BYTE_* macro definition , using get_unaligned_*()/put_unaligned_*() instead. (Bart Van Assche)
2. change Kconfig help message (Randy Dunlap)
3. delete HPB host control mode code
4. change the way of submission of HPB request, and let the HPB driver directly submit HPB request to UFS.

v1--v2:
1. Rebased the patch on the [3] branch 5.8/scsi-queue
2. Optimized and simplified several functions
3. Add parameter read_threshold in HPB sysfs, by which the user can change read_threshold for the HPB host control mode
4. Add HPB memory limitation, let the user adjust its size according to the system memory capacity

[1] https://github.com/OpenMPDK/HPBDriver
[2] https://www.spinics.net/lists/kernel/msg3449471.html
[3] https://git.kernel.org/pub/scm/linux/kernel/git/mkp/scsi.git
[4] https://marc.info/?l=linux-scsi&m=158706915110258&w=4

Bean Huo (5):
scsi; ufs: add device descriptor for Host Performance Booster
scsi: ufs: make ufshcd_read_unit_desc_param() non-static func
scsi: ufs: add ufs_features parameter in structure ufs_dev_info
scsi: ufs: add unit and geometry parameters for HPB
scsi: ufs: UFS Host Performance Booster(HPB) driver

 drivers/scsi/ufs/Kconfig  |   62 +
 drivers/scsi/ufs/Makefile |    1 +
 drivers/scsi/ufs/ufs.h    |   15 +
 drivers/scsi/ufs/ufshcd.c |  245 +++-
 drivers/scsi/ufs/ufshcd.h |   28 +-
 drivers/scsi/ufs/ufshpb.c | 2767 +++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshpb.h |  423 ++++++
 7 files changed, 3534 insertions(+), 7 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshpb.c
 create mode 100644 drivers/scsi/ufs/ufshpb.h
Signature · 1 line
2.17.1
#1 lore raw

RFC PATCH v3 1/5 scsi; ufs: add device descriptor for Host Performance Booster

From: Bean Huo <[email protected]>

Add HPB related attributes in devivce descriptor. HPB support is specified by Bit7 of bUFSFeatureSupport, HPB version is indicated by wHPBVersion and the HPB Control Mode is specified by bHPBControl in UFS device descriptor.

---
 drivers/scsi/ufs/ufs.h    |  6 ++++++
 drivers/scsi/ufs/ufshcd.c | 11 +++++++++++
 2 files changed, 17 insertions(+)
drivers/scsi/ufs/ufs.h +6 -0
index b7fec5c73688..53a5e263f7c8 100644
@@ -258,6 +258,8 @@ enum device_desc_param {
 	DEVICE_DESC_PARAM_PSA_MAX_DATA		= 0x25,
 	DEVICE_DESC_PARAM_PSA_TMT		= 0x29,
 	DEVICE_DESC_PARAM_PRDCT_REV		= 0x2A,
+	DEVICE_DESC_PARAM_HPB_VER		= 0x40,
+	DEVICE_DESC_PARAM_HPB_CTRL_MODE		= 0x42,
 };
 
 /* Interconnect descriptor parameters offsets in bytes*/
@@ -537,6 +539,10 @@ struct ufs_dev_info {
 	u8 *model;
 	u16 wspecversion;
 	u32 clk_gating_wait_us;
+	/* HPB Version */
+	u16 hpb_ver;
+	/* bHPBControl */
+	u8 hpb_control_mode;
 };
 
 /**
drivers/scsi/ufs/ufshcd.c +11 -0
index 698e8d20b4ba..de13d2333f1f 100644
@@ -6627,6 +6627,17 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
 		goto out;
 	}
 
+	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
+		hba->dev_info.hpb_control_mode =
+			desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];
+		hba->dev_info.hpb_ver =
+			(u16) (desc_buf[DEVICE_DESC_PARAM_HPB_VER] << 8) |
+			desc_buf[DEVICE_DESC_PARAM_HPB_VER + 1];
+		dev_info(hba->dev, "HPB Version: 0x%2x\n",
+			 hba->dev_info.hpb_ver);
+		dev_info(hba->dev, "HPB control mode: %d\n",
+			 hba->dev_info.hpb_control_mode);
+	}
 	/*
 	 * getting vendor (manufacturerID) and Bank Index in big endian
 	 * format
Signature · 1 line
2.17.1
#2 lore raw

Re: [RESENT PATCH RFC v3 1/5] scsi; ufs: add device descriptor for Host Performance Booster

Quoted 12 lines · On 2020-05-04 07:20, [email protected] wrote:
drivers/scsi/ufs/ufshcd.c +4 -0
index 698e8d20b4ba..de13d2333f1f 100644
@@ -6627,6 +6627,17 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
 		goto out;
 	}
 
+	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
+		hba->dev_info.hpb_control_mode =
+			desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];
+		hba->dev_info.hpb_ver =

drivers/scsi/ufs/ufshcd.c static int ufs_get_device_desc(struct ufs_hba *hba) · line 6643

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +7 -0
+			(u16) (desc_buf[DEVICE_DESC_PARAM_HPB_VER] << 8) |
+			desc_buf[DEVICE_DESC_PARAM_HPB_VER + 1];
+		dev_info(hba->dev, "HPB Version: 0x%2x\n",
+			 hba->dev_info.hpb_ver);
+		dev_info(hba->dev, "HPB control mode: %d\n",
+			 hba->dev_info.hpb_control_mode);
+	}
 	/*
 	 * getting vendor (manufacturerID) and Bank Index in big endian
 	 * format

Please introduce a symbolic name for the constant 0x80, e.g. UFS_FEATURE_HPB.

Please use get_unaligned_be16() instead of open-coding it.

Thanks,

Bart.

author
#3 lore raw

Re: [RESENT PATCH RFC v3 1/5] scsi; ufs: add device descriptor for Host Performance Booster

Quoted 29 lines · Bart Van Assche
Quoted 15 lines · On 2020-05-04 07:20, [email protected] wrote:
drivers/scsi/ufs/ufshcd.c +5 -0
index 698e8d20b4ba..de13d2333f1f 100644
@@ -6627,6 +6627,17 @@ static int ufs_get_device_desc(struct
 ufs_hba *hba)
 		goto out;
 	}
 
+	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
+		hba->dev_info.hpb_control_mode =
+			desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];
+		hba->dev_info.hpb_ver =
+			(u16) (desc_buf[DEVICE_DESC_PARAM_HPB_VER] <<

8) |

drivers/scsi/ufs/ufshcd.c static int ufs_get_device_desc(struct ufs_hba *hba) · line 6643

Quoted 9 lines
drivers/scsi/ufs/ufshcd.c +6 -0
+			desc_buf[DEVICE_DESC_PARAM_HPB_VER + 1];
+		dev_info(hba->dev, "HPB Version: 0x%2x\n",
+			 hba->dev_info.hpb_ver);
+		dev_info(hba->dev, "HPB control mode: %d\n",
+			 hba->dev_info.hpb_control_mode);
+	}
 	/*
 	 * getting vendor (manufacturerID) and Bank Index in big endian
 	 * format

Please introduce a symbolic name for the constant 0x80, e.g. UFS_FEATURE_HPB.

Quoted 7 lines

Please use get_unaligned_be16() instead of open-coding it.

Thanks,

Bart.

Bart
thanks, I will change them in the next version.

thanks.

Bean

#4 lore raw

RFC PATCH v3 2/5 scsi: ufs: make ufshcd_read_unit_desc_param() non-static func

In reply to [email protected]

From: Bean Huo <[email protected]>

HPB driver needs to read unit descriptors through ufshcd_read_unit_desc_param(), make it an extern Function.

---
 drivers/scsi/ufs/ufshcd.c | 8 +++-----
 drivers/scsi/ufs/ufshcd.h | 5 ++++-
 2 files changed, 7 insertions(+), 6 deletions(-)
drivers/scsi/ufs/ufshcd.c +3 -5
index de13d2333f1f..83ed2879d930 100644
@@ -3326,11 +3326,9 @@ int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
  *
  * Return 0 in case of success, non-zero otherwise
  */
-static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
-					      int lun,
-					      enum unit_desc_param param_offset,
-					      u8 *param_read_buf,
-					      u32 param_size)
+int ufshcd_read_unit_desc_param(struct ufs_hba *hba, int lun,
+				enum unit_desc_param param_offset,
+				u8 *param_read_buf, u32 param_size)
 {
 	/*
 	 * Unit descriptors are only available for general purpose LUs (LUN id
drivers/scsi/ufs/ufshcd.h +4 -1
index 6ffc08ad85f6..7ce9cc2f10fe 100644
@@ -856,7 +856,10 @@ extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
 extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
 			       u32 *mib_val, u8 peer);
 extern int ufshcd_config_pwr_mode(struct ufs_hba *hba,
-			struct ufs_pa_layer_attr *desired_pwr_mode);
+				  struct ufs_pa_layer_attr *desired_pwr_mode);
+extern int ufshcd_read_unit_desc_param(struct ufs_hba *hba, int lun,
+				       enum unit_desc_param param_offset,
+				       u8 *param_read_buf, u32 param_size);
 
 /* UIC command interfaces for DME primitives */
 #define DME_LOCAL	0
Signature · 1 line
2.17.1
#5 lore raw

RFC PATCH v3 3/5 scsi: ufs: add ufs_features parameter in structure ufs_dev_info

In reply to [email protected]

From: Bean Huo <[email protected]>

Make a copy of bUFSFeaturesSupport, name it ufs_features, add it to structure ufs_dev_info.

---
 drivers/scsi/ufs/ufs.h    | 2 ++
 drivers/scsi/ufs/ufshcd.c | 2 ++
 2 files changed, 4 insertions(+)
drivers/scsi/ufs/ufs.h +2 -0
index 53a5e263f7c8..1f2d4b4950b8 100644
@@ -543,6 +543,8 @@ struct ufs_dev_info {
 	u16 hpb_ver;
 	/* bHPBControl */
 	u8 hpb_control_mode;
+	/* bUFSFeaturesSupport */
+	u8 ufs_features;
 };
 
 /**
drivers/scsi/ufs/ufshcd.c +2 -0
index 83ed2879d930..1fe7ffc1a75a 100644
@@ -6625,6 +6625,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
 		goto out;
 	}
 
+	dev_info->ufs_features = desc_buf[DEVICE_DESC_PARAM_UFS_FEAT];
+
 	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
 		hba->dev_info.hpb_control_mode =
 			desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];
Signature · 1 line
2.17.1
#6 lore raw

Re: [RESENT PATCH RFC v3 3/5] scsi: ufs: add ufs_features parameter in structure ufs_dev_info

Quoted 29 lines · On 2020-05-04 07:20, [email protected] wrote:

From: Bean Huo <[email protected]>

Make a copy of bUFSFeaturesSupport, name it ufs_features, add it to structure ufs_dev_info.

---
 drivers/scsi/ufs/ufs.h    | 2 ++
 drivers/scsi/ufs/ufshcd.c | 2 ++
 2 files changed, 4 insertions(+)
drivers/scsi/ufs/ufs.h +2 -0
index 53a5e263f7c8..1f2d4b4950b8 100644
@@ -543,6 +543,8 @@ struct ufs_dev_info {
 	u16 hpb_ver;
 	/* bHPBControl */
 	u8 hpb_control_mode;
+	/* bUFSFeaturesSupport */
+	u8 ufs_features;
 };
 
 /**
drivers/scsi/ufs/ufshcd.c +0 -0
index 83ed2879d930..1fe7ffc1a75a 100644

drivers/scsi/ufs/ufshcd.c static int ufs_get_device_desc(struct ufs_hba *hba) · line 6632

Quoted 8 lines
drivers/scsi/ufs/ufshcd.c +2 -0
@@ -6625,6 +6625,8 @@ static int ufs_get_device_desc(struct ufs_hba *hba)
 		goto out;
 	}
 
+	dev_info->ufs_features = desc_buf[DEVICE_DESC_PARAM_UFS_FEAT];
+
 	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
 		hba->dev_info.hpb_control_mode =
 			desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];

Since this patch touches the same code as patch 1/5, please merge patches 1/5 and 3/5 into a single patch.

Thanks,

Bart.

author
#7 lore raw

Re: [RESENT PATCH RFC v3 3/5] scsi: ufs: add ufs_features parameter in structure ufs_dev_info

Quoted 43 lines · Bart Van Assche
Quoted 37 lines · On 2020-05-04 07:20, [email protected] wrote:

From: Bean Huo <[email protected]>

Make a copy of bUFSFeaturesSupport, name it ufs_features, add it to structure ufs_dev_info.

---
 drivers/scsi/ufs/ufs.h    | 2 ++
 drivers/scsi/ufs/ufshcd.c | 2 ++
 2 files changed, 4 insertions(+)
drivers/scsi/ufs/ufs.h +2 -0
index 53a5e263f7c8..1f2d4b4950b8 100644
@@ -543,6 +543,8 @@ struct ufs_dev_info {
 	u16 hpb_ver;
 	/* bHPBControl */
 	u8 hpb_control_mode;
+	/* bUFSFeaturesSupport */
+	u8 ufs_features;
 };
 
 /**
drivers/scsi/ufs/ufshcd.c +2 -0
index 83ed2879d930..1fe7ffc1a75a 100644
@@ -6625,6 +6625,8 @@ static int ufs_get_device_desc(struct ufs_hba
 *hba)
 		goto out;
 	}
 
+	dev_info->ufs_features = desc_buf[DEVICE_DESC_PARAM_UFS_FEAT];
+
 	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
 		hba->dev_info.hpb_control_mode =
Quoted 1 line

desc_buf[DEVICE_DESC_PARAM_HPB_CTRL_MODE];

Since this patch touches the same code as patch 1/5, please merge patches 1/5 and 3/5 into a single patch.

Quoted 3 lines

Thanks,

Bart.

Bart
you have addressed this in the V2, sorry for this fault. I will merge them in the next version.

thanks,

Bean

Quoted 2 lines
#8 lore raw

RFC PATCH v3 4/5 scsi: ufs: add unit and geometry parameters for HPB

In reply to [email protected]

From: Bean Huo <[email protected]>

Add HPB related parameters introduced in Unit Descriptor and Geometry Descriptor.

---
 drivers/scsi/ufs/ufs.h | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/scsi/ufs/ufs.h +7 -0
index 1f2d4b4950b8..6210e489d2ce 100644
@@ -219,6 +219,9 @@ enum unit_desc_param {
 	UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT	= 0x18,
 	UNIT_DESC_PARAM_CTX_CAPABILITIES	= 0x20,
 	UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1	= 0x22,
+	UNIT_DESC_PARAM_HPB_MAX_ACTIVE_REGIONS	= 0x23,
+	UNIT_DESC_PARAM_HPB_PIN_REGION_START_OFFSET	= 0x25,
+	UNIT_DESC_PARAM_HPB_NUM_PIN_REGIONS             = 0x27,
 };
 
 /* Device descriptor parameters offsets in bytes*/
@@ -304,6 +307,10 @@ enum geometry_desc_param {
 	GEOMETRY_DESC_PARAM_ENM4_MAX_NUM_UNITS	= 0x3E,
 	GEOMETRY_DESC_PARAM_ENM4_CAP_ADJ_FCTR	= 0x42,
 	GEOMETRY_DESC_PARAM_OPT_LOG_BLK_SIZE	= 0x44,
+	GEOMETRY_DESC_PARAM_HPB_REGION_SIZE	= 0x48,
+	GEOMETRY_DESC_PARAM_HPB_NUMBER_LU	= 0x49,
+	GEOMETRY_DESC_PARAM_HPB_SUBREGION_SIZE  = 0x4A,
+	GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGIONS = 0x4B,
 };
 
 /* Health descriptor parameters offsets in bytes*/
Signature · 1 line
2.17.1
#9 lore raw

Re: [RESENT PATCH RFC v3 4/5] scsi: ufs: add unit and geometry parameters for HPB

Quoted 25 lines · On 2020-05-04 07:20, [email protected] wrote:

From: Bean Huo <[email protected]>

Add HPB related parameters introduced in Unit Descriptor and Geometry Descriptor.

---
 drivers/scsi/ufs/ufs.h | 7 +++++++
 1 file changed, 7 insertions(+)
drivers/scsi/ufs/ufs.h +3 -0
index 1f2d4b4950b8..6210e489d2ce 100644
@@ -219,6 +219,9 @@ enum unit_desc_param {
 	UNIT_DESC_PARAM_PHY_MEM_RSRC_CNT	= 0x18,
 	UNIT_DESC_PARAM_CTX_CAPABILITIES	= 0x20,
 	UNIT_DESC_PARAM_LARGE_UNIT_SIZE_M1	= 0x22,
+	UNIT_DESC_PARAM_HPB_MAX_ACTIVE_REGIONS	= 0x23,
+	UNIT_DESC_PARAM_HPB_PIN_REGION_START_OFFSET	= 0x25,
+	UNIT_DESC_PARAM_HPB_NUM_PIN_REGIONS             = 0x27,
 };
 
 /* Device descriptor parameters offsets in bytes*/

drivers/scsi/ufs/ufs.h enum geometry_desc_param { · line 314

Quoted 8 lines
drivers/scsi/ufs/ufs.h +4 -0
@@ -304,6 +307,10 @@ enum geometry_desc_param {
 	GEOMETRY_DESC_PARAM_ENM4_MAX_NUM_UNITS	= 0x3E,
 	GEOMETRY_DESC_PARAM_ENM4_CAP_ADJ_FCTR	= 0x42,
 	GEOMETRY_DESC_PARAM_OPT_LOG_BLK_SIZE	= 0x44,
+	GEOMETRY_DESC_PARAM_HPB_REGION_SIZE	= 0x48,
+	GEOMETRY_DESC_PARAM_HPB_NUMBER_LU	= 0x49,
+	GEOMETRY_DESC_PARAM_HPB_SUBREGION_SIZE  = 0x4A,
+	GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGIONS = 0x4B,
 };

How about adding the names from the spec as a comment above the new constants, e.g. as follows?

	/* wHPBPinnedRegionStartIdx */
	UNIT_DESC_PARAM_HPB_PIN_REGION_START_OFFSET = 0x25,

Thanks,

Bart.

author
#10 lore raw

Re: [RESENT PATCH RFC v3 4/5] scsi: ufs: add unit and geometry parameters for HPB

Quoted 9 lines · Bart Van Assche

drivers/scsi/ufs/ufs.h enum geometry_desc_param { · line 314

Quoted 5 lines
drivers/scsi/ufs/ufs.h +4 -0
+     GEOMETRY_DESC_PARAM_HPB_REGION_SIZE     = 0x48,
+     GEOMETRY_DESC_PARAM_HPB_NUMBER_LU       = 0x49,
+     GEOMETRY_DESC_PARAM_HPB_SUBREGION_SIZE  = 0x4A,
+     GEOMETRY_DESC_PARAM_HPB_MAX_ACTIVE_REGIONS = 0x4B,
  };

How about adding the names from the spec as a comment above the new constants, e.g. as follows?

Quoted 6 lines
        /* wHPBPinnedRegionStartIdx */
        UNIT_DESC_PARAM_HPB_PIN_REGION_START_OFFSET = 0x25,

Thanks,

Bart.

Bart
I will add in the next version.

thanks,

Bean

#11 lore raw

RFC PATCH v3 5/5 scsi: ufs: UFS Host Performance Booster(HPB) driver

In reply to [email protected]

From: Bean Huo <[email protected]>

This patch is to add support for the UFS Host Performance Booster (HPB v1.0), which is used to improve UFS read performance, especially for the random read.

NAND flash-based storage devices, including UFS, have mechanisms to translate logical addresses of requests to the corresponding physical addresses of the flash storage. Traditionally this L2P mapping data is loaded to the internal SRAM in the storage controller. When the capacity of storage is larger, a larger size of SRAM for the L2P map data is required. Since increased SRAM size affects the manufacturing cost significantly, it is not cost-effective to allocate all the amount of SRAM needed to keep all the Logical-address to Physical-address (L2P) map data. Therefore, L2P map data, which is required to identify the physical address for the requested IOs, can only be partially stored in SRAM from NAND flash. Due to this partial loading, accessing the flash address area where the L2P information for that address is not loaded in the SRAM can result in serious performance degradation.

The HPB is a software solution for the above problem, which uses the host’s system memory as a cache for the FTL L2P mapping table. It does not need additional hardware support from the host side. By using HPB, the L2P mapping table can be read from host memory and stored in host-side memory. while reading the operation, the corresponding L2P information will be sent to the UFS device along with the reading request. Since the L2P entry is provided in the read request, UFS device does not have to load L2P entry from flash memory. This will significantly improve random read performance.

---
 drivers/scsi/ufs/Kconfig  |   62 +
 drivers/scsi/ufs/Makefile |    1 +
 drivers/scsi/ufs/ufshcd.c |  234 +++-
 drivers/scsi/ufs/ufshcd.h |   23 +
 drivers/scsi/ufs/ufshpb.c | 2767 +++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshpb.h |  423 ++++++
 6 files changed, 3504 insertions(+), 6 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshpb.c
 create mode 100644 drivers/scsi/ufs/ufshpb.h
drivers/scsi/ufs/Kconfig +62 -0
index e2005aeddc2d..0224f224a641 100644
@@ -160,3 +160,65 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHPB
+	bool "UFS Host Performance Booster (EXPERIMENTAL)"
+	depends on SCSI_UFSHCD
+	help
+	  NAND flash-based storage devices, including UFS, have mechanisms to
+	  translate logical addresses of the IO requests to the corresponding
+	  physical addresses of the flash storage. Traditionally, this L2P
+	  mapping data is loaded to the internal SRAM in the storage controller.
+	  When the capacity of storage is larger, a larger size of SRAM for the
+	  L2P map data is required. Since increased SRAM size affects the
+	  manufacturing cost significantly, it is not cost-effective to allocate
+	  all the amount of SRAM needed to keep all the Logical-address to
+	  Physical-address (L2P) map data. Therefore, L2P map data, which is
+	  required to identify the physical address for the requested IOs, can
+	  only be partially stored in SRAM from NAND flash. Due to this partial
+	  loading, accessing the flash address area where the L2P information
+	  for that address is not loaded in the SRAM can result in serious
+	  performance degradation.
+
+	  UFS Host Performance Booster (HPB) is a software solution for the
+	  above problem, which uses the host side system memory as a cache for
+	  the FTL L2P mapping table. It does not need additional hardware
+	  support from the host side. By using HPB, the L2P mapping table can be
+	  read from host memory and stored in host-side memory. when performing
+	  the read operation, the corresponding L2P information will be sent to
+	  the UFS device along with the reading request. Since the L2P entry is
+	  provided in the read request, UFS device does not have to load L2P
+	  entry from flash memory to UFS internal SRAM. This will significantly
+	  improve the read performance.
+
+	  When selected, this feature will be built in the UFS driver.
+
+	  If in doubt, say N.
+
+config UFSHPB_MAX_MEM_SIZE
+	int "UFS HPB maximum memory size per controller (in MiB)"
+	depends on SCSI_UFSHPB
+	default 128
+	range 0 65536
+	help
+	  This parameter defines the maximum UFS HPB memory/cache size in the
+	  host system. The recommended HPB cache size by the UFS device can be
+	  calculated from bHPBRegionSize and wDeviceMaxActiveHPBRegions. The
+	  reference formula can be
+
+		(bHPBRegionSize(in KB) / 4KB) * 8 * wDeviceMaxActiveHPBRegions.
+
+	  The HPB cache in the host system is used to contain L2P mapping
+	  entries. If the allocated HPB cache size is lower than what calculated
+	  by the above formula, the use of HPB feature may provide lower
+	  performance advantage. But the system memory resource has the
+	  limitation, we can not let HPB driver allocate its cache at will
+	  according to the UFS device recommendation, so an appropriate size of
+	  the cache for HPB should be specified before you choose to use HPB,
+	  then please enter a non-zero positive integer value.
+
+	  Nevertheless, if you want to leave this to the HPB driver, and let the
+	  HPB driver allocate the HPB cache based on the recommendation of the
+	  UFS device. Just give 0 value to this parameter.
+
+	  Leave the default value if unsure.
drivers/scsi/ufs/Makefile +1 -0
index 94c6c5d7334b..24d9b69eab0c 100644
@@ -7,6 +7,7 @@ obj-$(CONFIG_SCSI_UFS_QCOM) += ufs-qcom.o
 obj-$(CONFIG_SCSI_UFSHCD) += ufshcd-core.o
 ufshcd-core-y				+= ufshcd.o ufs-sysfs.o
 ufshcd-core-$(CONFIG_SCSI_UFS_BSG)	+= ufs_bsg.o
+ufshcd-core-$(CONFIG_SCSI_UFSHPB) += ufshpb.o
 obj-$(CONFIG_SCSI_UFSHCD_PCI) += ufshcd-pci.o
 obj-$(CONFIG_SCSI_UFSHCD_PLATFORM) += ufshcd-pltfrm.o
 obj-$(CONFIG_SCSI_UFS_HISI) += ufs-hisi.o
drivers/scsi/ufs/ufshcd.c +228 -6

317 lines not on this page · raw message

drivers/scsi/ufs/ufshcd.h +23 -0
index 7ce9cc2f10fe..03890309a963 100644
@@ -70,6 +70,7 @@
 
 #include "ufs.h"
 #include "ufshci.h"
+#include "ufshpb.h"
 
 #define UFSHCD "ufshcd"
 #define UFSHCD_DRIVER_VERSION "0.2"
@@ -162,6 +163,17 @@ struct ufs_pm_lvl_states {
 	enum uic_link_state link_state;
 };
 
+struct ufs_req {
+	struct request *req;
+	u8 cmd[16];
+	u8 cmd_len;
+	u8 *sense_buffer;
+	u8 lun;
+	unsigned int payload_len;
+	struct sg_table sgt;
+	enum dma_data_direction cmd_dir;
+	struct completion *complete;
+};
 /**
  * struct ufshcd_lrb - local reference block
  * @utr_descriptor_ptr: UTRD address of the command
@@ -206,6 +218,7 @@ struct ufshcd_lrb {
 	bool intr_cmd;
 	ktime_t issue_time_stamp;
 	ktime_t compl_time_stamp;
+	struct ufs_req *ureq;
 
 	bool req_abort_skip;
 };
@@ -727,6 +740,15 @@ struct ufs_hba {
 
 	struct device		bsg_dev;
 	struct request_queue	*bsg_queue;
+
+#if defined(CONFIG_SCSI_UFSHPB)
+	enum UFSHPB_STATE ufshpb_state;
+	struct ufshpb_geo hpb_geo;
+	struct ufshpb_lu *ufshpb_lup[32];
+	struct delayed_work ufshpb_init_work;
+	struct work_struct ufshpb_eh_work;
+	struct scsi_device *sdev_ufs_lu[32];
+#endif
 };
 
 /* Returns true if clocks can be gated. Otherwise false */
@@ -962,6 +984,7 @@ int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
 			     u8 *desc_buff, int *buff_len,
 			     enum query_opcode desc_op);
 
+int ufshcd_exec_internal_req(struct ufs_hba *hba, struct ufs_req *ureq);
 /* Wrapper functions for safely calling variant operations */
 static inline const char *ufshcd_get_var_name(struct ufs_hba *hba)
 {
drivers/scsi/ufs/ufshpb.c new file +2767 -0

2,558 lines not on this page · raw message

drivers/scsi/ufs/ufshpb.h new file +423 -0

423 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 1 line
2.17.1

Part of the diff is not shown here. View the raw message.

#12 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

[if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please…

Hi,

[FYI, it's a private test report for your RFC patch.]
[auto build test ERROR on v5.7-rc4]
[cannot apply to mkp-scsi/for-next scsi/for-next next-20200505] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system. BTW, we also suggest to use '--base' option to specify the base tree in git format-patch, please see https://stackoverflow.com/a/37406982]

url:    https://github.com/0day-ci/linux/commits/huobean-gmail-com/scsi-ufs-add-UFS-Host-Performance-Booster-HPB-support/20200505-073112
base:    0e698dfa282211e414076f9dc7e83c1c288314fd
config: m68k-allyesconfig (attached as .config)
compiler: m68k-linux-gcc (GCC) 9.3.0

reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
COMPILER_INSTALL_PATH=$HOME/0day GCC_VERSION=9.3.0 make.cross ARCH=m68k

If you fix the issue, kindly add following tag as appropriate

All errors (new ones prefixed by >>):

m68k-linux-ld: drivers/scsi/ufs/ufshpb.o: in function `ufshpb_init_workq_fn':

>> ufshpb.c:(.text+0x22c6): undefined reference to `__udivdi3'
>> m68k-linux-ld: ufshpb.c:(.text+0x23ec): undefined reference to `__udivdi3'
>> m68k-linux-ld: ufshpb.c:(.text+0x2524): undefined reference to `__umoddi3'

m68k-linux-ld: ufshpb.c:(.text+0x254c): undefined reference to `__umoddi3'
m68k-linux-ld: ufshpb.c:(.text+0x2598): undefined reference to `__udivdi3'
m68k-linux-ld: ufshpb.c:(.text+0x261e): undefined reference to `__udivdi3'
m68k-linux-ld: ufshpb.c:(.text+0x265a): undefined reference to `__udivdi3'

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all(a)lists.01.org

#13 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

In reply to [email protected]

Hi,

Quoted 23 lines · On 5/4/20 7:20 AM, [email protected] wrote:
drivers/scsi/ufs/Kconfig +15 -0
index e2005aeddc2d..0224f224a641 100644
@@ -160,3 +160,65 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS controller.
 	  If unsure, say N.
+
+config SCSI_UFSHPB
+	bool "UFS Host Performance Booster (EXPERIMENTAL)"
+	depends on SCSI_UFSHCD
+	help
+	  NAND flash-based storage devices, including UFS, have mechanisms to
+	  translate logical addresses of the IO requests to the corresponding
+	  physical addresses of the flash storage. Traditionally, this L2P
+	  mapping data is loaded to the internal SRAM in the storage controller.
+	  When the capacity of storage is larger, a larger size of SRAM for the
+	  L2P map data is required. Since increased SRAM size affects the
+	  manufacturing cost significantly, it is not cost-effective to allocate
+	  all the amount of SRAM needed to keep all the Logical-address to
+	  Physical-address (L2P) map data. Therefore, L2P map data, which is
+	  required to identify the physical address for the requested IOs, can

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 187

Quoted 10 lines
drivers/scsi/ufs/Kconfig +10 -0
+	  only be partially stored in SRAM from NAND flash. Due to this partial
+	  loading, accessing the flash address area where the L2P information
+	  for that address is not loaded in the SRAM can result in serious
+	  performance degradation.
+
+	  UFS Host Performance Booster (HPB) is a software solution for the
+	  above problem, which uses the host side system memory as a cache for
+	  the FTL L2P mapping table. It does not need additional hardware
+	  support from the host side. By using HPB, the L2P mapping table can be
+	  read from host memory and stored in host-side memory. when performing

Should that be: from device memory and stored in host-side memory. ?

Also, s/when/When/

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 189

Quoted 2 lines
drivers/scsi/ufs/Kconfig +2 -0
+	  the read operation, the corresponding L2P information will be sent to
+	  the UFS device along with the reading request. Since the L2P entry is

s/reading/read/

Quoted 8 lines
drivers/scsi/ufs/Kconfig +8 -0
+	  provided in the read request, UFS device does not have to load L2P
+	  entry from flash memory to UFS internal SRAM. This will significantly
+	  improve the read performance.
+
+	  When selected, this feature will be built in the UFS driver.
+
+	  If in doubt, say N.
+

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 207

Quoted 10 lines
drivers/scsi/ufs/Kconfig +10 -0
+config UFSHPB_MAX_MEM_SIZE
+	int "UFS HPB maximum memory size per controller (in MiB)"
+	depends on SCSI_UFSHPB
+	default 128
+	range 0 65536
+	help
+	  This parameter defines the maximum UFS HPB memory/cache size in the
+	  host system. The recommended HPB cache size by the UFS device can be
+	  calculated from bHPBRegionSize and wDeviceMaxActiveHPBRegions. The
+	  reference formula can be

s/can be/is/

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 212

Quoted 5 lines
drivers/scsi/ufs/Kconfig +5 -0
+
+		(bHPBRegionSize(in KB) / 4KB) * 8 * wDeviceMaxActiveHPBRegions.
+
+	  The HPB cache in the host system is used to contain L2P mapping
+	  entries. If the allocated HPB cache size is lower than what calculated

than that

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 224

Quoted 12 lines
drivers/scsi/ufs/Kconfig +12 -0
+	  by the above formula, the use of HPB feature may provide lower
+	  performance advantage. But the system memory resource has the
+	  limitation, we can not let HPB driver allocate its cache at will
+	  according to the UFS device recommendation, so an appropriate size of
+	  the cache for HPB should be specified before you choose to use HPB,
+	  then please enter a non-zero positive integer value.
+
+	  Nevertheless, if you want to leave this to the HPB driver, and let the
+	  HPB driver allocate the HPB cache based on the recommendation of the
+	  UFS device. Just give 0 value to this parameter.
+
+	  Leave the default value if unsure.

thanks.

Signature · 1 line
~Randy
author
#14 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Quoted 118 lines · Randy Dunlap

Hi,

Quoted 51 lines · On 5/4/20 7:20 AM, [email protected] wrote:
drivers/scsi/ufs/Kconfig +0 -0
index e2005aeddc2d..0224f224a641 100644
@@ -160,3 +160,65 @@ config SCSI_UFS_BSG
 
 	  Select this if you need a bsg device node for your UFS

controller.

drivers/scsi/ufs/Kconfig +6 -0
 	  If unsure, say N.
+
+config SCSI_UFSHPB
+	bool "UFS Host Performance Booster (EXPERIMENTAL)"
+	depends on SCSI_UFSHCD
+	help
+	  NAND flash-based storage devices, including UFS, have

mechanisms to

drivers/scsi/ufs/Kconfig +1 -0
+	  translate logical addresses of the IO requests to the

corresponding

drivers/scsi/ufs/Kconfig +1 -0
+	  physical addresses of the flash storage. Traditionally, this

L2P

drivers/scsi/ufs/Kconfig +1 -0
+	  mapping data is loaded to the internal SRAM in the storage

controller.

drivers/scsi/ufs/Kconfig +1 -0
+	  When the capacity of storage is larger, a larger size of SRAM

for the

drivers/scsi/ufs/Kconfig +1 -0
+	  L2P map data is required. Since increased SRAM size affects

the

drivers/scsi/ufs/Kconfig +1 -0
+	  manufacturing cost significantly, it is not cost-effective to

allocate

drivers/scsi/ufs/Kconfig +1 -0
+	  all the amount of SRAM needed to keep all the Logical-address 

to

drivers/scsi/ufs/Kconfig +1 -0
+	  Physical-address (L2P) map data. Therefore, L2P map data,

which is

drivers/scsi/ufs/Kconfig +1 -0
+	  required to identify the physical address for the requested

IOs, can

drivers/scsi/ufs/Kconfig +1 -0
+	  only be partially stored in SRAM from NAND flash. Due to this

partial

drivers/scsi/ufs/Kconfig +1 -0
+	  loading, accessing the flash address area where the L2P

information

drivers/scsi/ufs/Kconfig +1 -0
+	  for that address is not loaded in the SRAM can result in

serious

drivers/scsi/ufs/Kconfig +3 -0
+	  performance degradation.
+
+	  UFS Host Performance Booster (HPB) is a software solution for

the

drivers/scsi/ufs/Kconfig +1 -0
+	  above problem, which uses the host side system memory as a

cache for

drivers/scsi/ufs/Kconfig +1 -0
+	  the FTL L2P mapping table. It does not need additional

hardware

drivers/scsi/ufs/Kconfig +1 -0
+	  support from the host side. By using HPB, the L2P mapping

table can be

drivers/scsi/ufs/Kconfig +1 -0
+	  read from host memory and stored in host-side memory. when
Quoted 1 line

performing

Should that be: from device memory and stored in host-side memory. ?

Also, s/when/When/

Quoted 4 lines
drivers/scsi/ufs/Kconfig +1 -0
+	  the read operation, the corresponding L2P information will be

sent to

drivers/scsi/ufs/Kconfig +1 -0
+	  the UFS device along with the reading request. Since the L2P

entry is

s/reading/read/

Quoted 22 lines
drivers/scsi/ufs/Kconfig +1 -0
+	  provided in the read request, UFS device does not have to

load L2P

drivers/scsi/ufs/Kconfig +1 -0
+	  entry from flash memory to UFS internal SRAM. This will

significantly

drivers/scsi/ufs/Kconfig +13 -0
+	  improve the read performance.
+
+	  When selected, this feature will be built in the UFS driver.
+
+	  If in doubt, say N.
+
+config UFSHPB_MAX_MEM_SIZE
+	int "UFS HPB maximum memory size per controller (in MiB)"
+	depends on SCSI_UFSHPB
+	default 128
+	range 0 65536
+	help
+	  This parameter defines the maximum UFS HPB memory/cache size

in the

drivers/scsi/ufs/Kconfig +1 -0
+	  host system. The recommended HPB cache size by the UFS device

can be

drivers/scsi/ufs/Kconfig +1 -0
+	  calculated from bHPBRegionSize and

wDeviceMaxActiveHPBRegions. The

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 207

Quoted 1 line
drivers/scsi/ufs/Kconfig +1 -0
+	  reference formula can be

s/can be/is/

Quoted 8 lines
drivers/scsi/ufs/Kconfig +2 -0
+
+		(bHPBRegionSize(in KB) / 4KB) * 8 *

wDeviceMaxActiveHPBRegions.

drivers/scsi/ufs/Kconfig +2 -0
+
+	  The HPB cache in the host system is used to contain L2P

mapping

drivers/scsi/ufs/Kconfig +1 -0
+	  entries. If the allocated HPB cache size is lower than what

calculated

than that

Quoted 12 lines
drivers/scsi/ufs/Kconfig +1 -0
+	  by the above formula, the use of HPB feature may provide

lower

drivers/scsi/ufs/Kconfig +2 -0
+	  performance advantage. But the system memory resource has the
+	  limitation, we can not let HPB driver allocate its cache at

will

drivers/scsi/ufs/Kconfig +1 -0
+	  according to the UFS device recommendation, so an appropriate

size of

drivers/scsi/ufs/Kconfig +1 -0
+	  the cache for HPB should be specified before you choose to

use HPB,

drivers/scsi/ufs/Kconfig +3 -0
+	  then please enter a non-zero positive integer value.
+
+	  Nevertheless, if you want to leave this to the HPB driver,
Quoted 8 lines

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG · line 224

Quoted 6 lines

and let the

drivers/scsi/ufs/Kconfig +1 -0
+	  HPB driver allocate the HPB cache based on the recommendation

of the

drivers/scsi/ufs/Kconfig +3 -0
+	  UFS device. Just give 0 value to this parameter.
+
+	  Leave the default value if unsure.

thanks.

Hi Randy
Thanks for your review. I will change them based on your comments.

thanks,

Bean

#15 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

In reply to [email protected]

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG

Quoted 11 lines · On 2020-05-04 07:20, [email protected] wrote:
drivers/scsi/ufs/Kconfig +11 -0
+config SCSI_UFSHPB
+	bool "UFS Host Performance Booster (EXPERIMENTAL)"
+	depends on SCSI_UFSHCD
+	[ ... ]
+
+config UFSHPB_MAX_MEM_SIZE
+	int "UFS HPB maximum memory size per controller (in MiB)"
+	depends on SCSI_UFSHPB
+	default 128
+	range 0 65536
+	[ ... ]

Kernel module parameters are an inflexible mechanism because the value of these parameters cannot be changed without rebuilding the kernel followed by a reboot. Has it been considered to make these two parameters configurable through sysfs? Has it been considered to make these parameters UFS device parameters instead of global parameters that apply to all UFS devices?

Quoted 1 line

+static int ufshcd_clear_cmd(struct ufs_hba *hba, int tag);

Is it possible to reorder the function definitions such that this function declaration can be left out?

Quoted 6 lines

static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag) {

drivers/scsi/ufs/ufshcd.c +0 -0
 	return tag >= 0 && tag < hba->nutrs;
@@ -2391,6 +2392,12 @@ static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
 		ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
 						lrbp->cmd->sc_data_direction);

drivers/scsi/ufs/ufshcd.c static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) · line 2403

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +6 -0
 		ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
+
+#if defined(CONFIG_SCSI_UFSHPB)
+		/* If HPB works, prepare for the HPB READ */
+		if (hba->ufshpb_state == HPB_PRESENT)
+			ufshpb_prep_fn(hba, lrbp);
+#endif
 	} else {
 		ret = -EINVAL;
 	}

What does the name "HPB_PRESENT" mean? If it means that HPB is enabled, wouldn't HPB_ENABLED be a better name for this constant? If so, is the comment above the if-statement useful?

Quoted 19 lines
drivers/scsi/ufs/ufshcd.c +15 -0
@@ -2430,6 +2437,181 @@ static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i)
 	lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset;
 }
 
+#if defined(CONFIG_SCSI_UFSHPB)
+static void ufshcd_prepare_ureq_upiu(struct ufshcd_lrb *lrbp, struct ufs_req
+				     *ureq, u32 *upiu_flags)
+{
+	struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
+	unsigned short cdb_len;
+
+	/* command descriptor fields */
+	ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
+					UPIU_TRANSACTION_COMMAND, *upiu_flags,
+					lrbp->lun, lrbp->task_tag);
+	ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
+					UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
+
+	/* Total EHS length and Data segment length will be zero */

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2464

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+	ucd_req_ptr->header.dword_2 = 0;
+
+	ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(ureq->payload_len);
+
+	cdb_len = min_t(unsigned short, ureq->cmd_len, UFS_CDB_SIZE);
+	memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
+	memcpy(ucd_req_ptr->sc.cdb, ureq->cmd, cdb_len);
+
+	memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
+}

Please add a comment above this function that explains what "ureq_upiu" means and that also explains the meaning of the function arguments. Please also keep "struct ufs_req *ureq" on a single line.

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2473

Quoted 8 lines
drivers/scsi/ufs/ufshcd.c +8 -0
+static void ufshcd_comp_ureq_upiu(struct ufshcd_lrb *lrbp, struct ufs_req *ureq)
+{
+	u32 upiu_flags;
+
+	lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
+	ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, ureq->cmd_dir);
+	ufshcd_prepare_ureq_upiu(lrbp, ureq, &upiu_flags);
+}

To me the abbreviation "comp" suggests completion. Since this function prepares a command, how about changing _comp_ into _prep_?

Quoted 21 lines
drivers/scsi/ufs/ufshcd.c +21 -0
+static int ufshcd_ureq_dma_map(struct ufs_hba *hba, struct ufs_req *ureq,
+			       struct ufshcd_lrb *lrbp)
+{
+	int nseg = 0;
+	struct ufshcd_sg_entry *prdt;
+	struct scatterlist *sg;
+	int i;
+	struct device *dev = hba->host->dma_dev;
+
+	nseg = dma_map_sg(dev, ureq->sgt.sgl, ureq->sgt.nents, ureq->cmd_dir);
+	if (nseg < 0)
+		return nseg;
+
+	if (nseg == 0) {
+		lrbp->utr_descriptor_ptr->prd_table_length = 0;
+		return 0;
+	}
+
+	lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16((u16)nseg);
+	prdt = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
+	for_each_sg(ureq->sgt.sgl, sg, nseg, i) {

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2505

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+		prdt[i].size  = cpu_to_le32(((u32)sg_dma_len(sg)) - 1);
+		prdt[i].base_addr =
+			cpu_to_le32(lower_32_bits(sg_dma_address(sg)));
+		prdt[i].upper_addr =
+			cpu_to_le32(upper_32_bits(sg_dma_address(sg)));
+		prdt[i].reserved = 0;
+	}
+	wmb();
+	return 0;
+}

Is the if (nseg == 0) ... statement necessary?

Is the (u16) cast in front of nseg necessary?

Is the (u32) cast in front of sg_dma_len() necessary? Is it useful?

Can the definition of ufshcd_sg_entry be changed such that the base_addr and upper_addr members are combined into a single __le64 member variable? Would that allow to write the DMA address using put_unaligned_le64()?

Please move the wmb() call to the caller of this function and add a comment about why it is necessary. wmb() calls should not occur at the end of a function.

Quoted 25 lines
drivers/scsi/ufs/ufshcd.c +25 -0
+static int ufshcd_wait_for_ureq(struct ufs_hba *hba, struct ufshcd_lrb *lrbp,
+				struct ufs_req *ureq, int max_timeout)
+{
+	int err = 0;
+	unsigned long time_left;
+	unsigned long flags;
+
+	time_left = wait_for_completion_timeout(ureq->complete,
+			msecs_to_jiffies(max_timeout));
+
+	/* Make sure descriptors are ready before ringing the doorbell */
+	wmb();
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	ureq->complete = NULL;
+	if (likely(time_left))
+		err = ufshcd_get_tr_ocs(lrbp);
+	spin_unlock_irqrestore(hba->host->host_lock, flags);
+
+	if (!time_left) {
+		err = -ETIMEDOUT;
+		dev_warn(hba->dev, "%s: ureq timedout, tag %d\n", __func__,
+			 lrbp->task_tag);
+		if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
+			/* successfully cleared the command, retry if needed */
+			err = -EAGAIN;

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2541

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+		/*
+		 * in case of an error, after clearing the doorbell,
+		 * we also need to clear the outstanding_request
+		 * field in hba
+		 */
+		ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
+	}
+
+	return err;
+}

The wmb() call above seems superfluous. Calling wmb() after wait_for_completion() is almost always wrong because wait_for_completion() already includes a barrier. See also Documentation/memory-barriers.txt.

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2544

Quoted 2 lines
drivers/scsi/ufs/ufshcd.c +2 -0
+int ufshcd_exec_internal_req(struct ufs_hba *hba, struct ufs_req *ureq)
+{

Please document what this function does and also the meaning of the arguments of this function.

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2590

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+	lrbp = &hba->lrb[tag];
+	WARN_ON(lrbp->cmd);
+	lrbp->cmd = NULL;
+	lrbp->ureq = ureq;
+	lrbp->sense_bufflen = UFS_SENSE_SIZE;
+	lrbp->sense_buffer = ureq->sense_buffer;
+	lrbp->task_tag = tag;
+	lrbp->lun = ufshcd_scsi_to_upiu_lun(ureq->lun);
+	lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
+	lrbp->req_abort_skip = false;

The "? true : false" part seems superfluous to me.

Quoted 12 lines
drivers/scsi/ufs/ufshcd.c +12 -0
+	ufshcd_comp_ureq_upiu(lrbp, ureq);
+
+	err = ufshcd_ureq_dma_map(hba, ureq, lrbp);
+	if (err) {
+		dev_err(hba->dev,
+			"%s: ufshcd_ureq_dma_map err %d\n", __func__, err);
+		goto out;
+	}
+	ureq->complete = &wait;
+	spin_lock_irqsave(hba->host->host_lock, flags);
+	ufshcd_send_command(hba, tag);
+	spin_unlock_irqrestore(hba->host->host_lock, flags);

drivers/scsi/ufs/ufshcd.c static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) · line 2613

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+
+	err = ufshcd_wait_for_ureq(hba, lrbp, ureq, 20000);
+
+out:
+	ufshcd_release(hba);
+unlock:
+	up_read(&hba->clk_scaling_lock);
+	return err;
+}
+#endif

Please make command submission asynchronous such that this command can be sumitted from a context where it is not allowed to sleep.

Quoted 19 lines
drivers/scsi/ufs/ufshcd.c +4 -1
 /**
  * ufshcd_queuecommand - main entry point for SCSI requests
  * @host: SCSI host pointer
@@ -4622,7 +4804,10 @@ static int ufshcd_slave_configure(struct scsi_device *sdev)
 
 	if (ufshcd_is_rpm_autosuspend_allowed(hba))
 		sdev->rpm_autosuspend = 1;
-
+#if defined(CONFIG_SCSI_UFSHPB)
+	if (sdev->lun < hba->dev_info.max_lu_supported)
+		hba->sdev_ufs_lu[sdev->lun] = sdev;
+#endif
 	return 0;
 }
 
@@ -4738,6 +4923,16 @@ ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
 				 */
 				pm_runtime_get_noresume(hba->dev);
 			}

drivers/scsi/ufs/ufshcd.c ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) · line 4935

Quoted 10 lines
drivers/scsi/ufs/ufshcd.c +10 -0
+
+#if defined(CONFIG_SCSI_UFSHPB)
+			/*
+			 * HPB recommendations are provided in RESPONSE UPIU
+			 * packets of successfully completed commands, which
+			 * are commands terminated with GOOD status.
+			 */
+			if (scsi_status == SAM_STAT_GOOD)
+				ufshpb_rsp_handler(hba, lrbp);
+#endif

How about renaming ufshpb_rsp_handle() into ufshpb_handle_rsp() or ufshpb_handle_response()?

drivers/scsi/ufs/ufshcd.c static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) · line 6300

Quoted 12 lines
drivers/scsi/ufs/ufshcd.c +5 -0
@@ -6088,6 +6290,11 @@ static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
 	hba->req_abort_count = 0;
 	ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err);
 	if (!err) {
+#if defined(CONFIG_SCSI_UFSHPB)
+		if (hba->ufshpb_state == HPB_NEED_RESET)
+			schedule_delayed_work(&hba->ufshpb_init_work,
+					      msecs_to_jiffies(10));
+#endif
 		err = SUCCESS;
 	} else {
 		dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);

So UFS HPB reset work is scheduled instead of being performed synchronously? I don't like this.

drivers/scsi/ufs/ufshcd.c static int ufs_get_device_desc(struct ufs_hba *hba) · line 6838

Quoted 4 lines
drivers/scsi/ufs/ufshcd.c +2 -2
-	if (desc_buf[DEVICE_DESC_PARAM_UFS_FEAT] & 0x80) {
-		hba->dev_info.hpb_control_mode =
+	if (dev_info->ufs_features & 0x80) {
+		dev_info->hpb_control_mode =

The change of "desc_buf[DEVICE_DESC_PARAM_UFS_FEAT]" into "dev_info->ufs_features" should be moved into a previous patch.

drivers/scsi/ufs/ufshcd.c static void ufshcd_async_scan(void *data, async_cookie_t cookie) · line 7404

Quoted 11 lines
drivers/scsi/ufs/ufshcd.c +7 -0
@@ -7188,6 +7395,14 @@ static void ufshcd_async_scan(void *data, async_cookie_t cookie)
 
 	/* Probe and add UFS logical units  */
 	ret = ufshcd_add_lus(hba);
+
+#if defined(CONFIG_SCSI_UFSHPB)
+	/* Initialize HPB  */
+	if (hba->ufshpb_state == HPB_NEED_INIT)
+		schedule_delayed_work(&hba->ufshpb_init_work,
+				      msecs_to_jiffies(10));
+#endif

Aargh. More asynchronous work. Can this HPB initialization be done synchronously?

Quoted 17 lines
drivers/scsi/ufs/ufshcd.h +2 -0
index 7ce9cc2f10fe..03890309a963 100644
@@ -70,6 +70,7 @@
 
 #include "ufs.h"
 #include "ufshci.h"
+#include "ufshpb.h"
 
 #define UFSHCD "ufshcd"
 #define UFSHCD_DRIVER_VERSION "0.2"
@@ -162,6 +163,17 @@ struct ufs_pm_lvl_states {
 	enum uic_link_state link_state;
 };
 
+struct ufs_req {

drivers/scsi/ufs/ufshcd.h struct ufs_pm_lvl_states { · line 176

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+	struct request *req;
+	u8 cmd[16];
+	u8 cmd_len;
+	u8 *sense_buffer;
+	u8 lun;
+	unsigned int payload_len;
+	struct sg_table sgt;
+	enum dma_data_direction cmd_dir;
+	struct completion *complete;
+};

Is ufs_req only used to submit HPB requests and not to submit any other type of request? If so, please make that clear in the name of this structure.

Please also add a comment above this data structure that explains its purpose.

drivers/scsi/ufs/ufshcd.h struct ufshcd_lrb { · line 224

Quoted 11 lines
drivers/scsi/ufs/ufshcd.h +1 -0
 /**
  * struct ufshcd_lrb - local reference block
  * @utr_descriptor_ptr: UTRD address of the command
@@ -206,6 +218,7 @@ struct ufshcd_lrb {
 	bool intr_cmd;
 	ktime_t issue_time_stamp;
 	ktime_t compl_time_stamp;
+	struct ufs_req *ureq;
 
 	bool req_abort_skip;
 };

Please document the meaning of the 'ureq' member.

Quoted 4 lines
drivers/scsi/ufs/ufshcd.h +0 -0
@@ -727,6 +740,15 @@ struct ufs_hba {
 
 	struct device		bsg_dev;
 	struct request_queue	*bsg_queue;

drivers/scsi/ufs/ufshcd.h struct ufs_hba { · line 752

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +9 -0
+
+#if defined(CONFIG_SCSI_UFSHPB)
+	enum UFSHPB_STATE ufshpb_state;
+	struct ufshpb_geo hpb_geo;
+	struct ufshpb_lu *ufshpb_lup[32];
+	struct delayed_work ufshpb_init_work;
+	struct work_struct ufshpb_eh_work;
+	struct scsi_device *sdev_ufs_lu[32];
+#endif
 };

If the sdev and ufshpb_lu pointers would be passed directly to ufshpb_execute_req(), would that make the two above 32-element arrays superfluous? I don't like these arrays since these duplicate SCSI core data structures.

drivers/scsi/ufs/ufshcd.h struct ufs_hba {

Quoted 7 lines
drivers/scsi/ufs/ufshcd.h +7 -0
+#define hpb_trace(hpb, args...)						\
+	do { if ((hpb)->hba->sdev_ufs_lu[(hpb)->lun] &&			\
+		(hpb)->hba->sdev_ufs_lu[(hpb)->lun]->request_queue)	\
+		blk_add_trace_msg(					\
+		(hpb)->hba->sdev_ufs_lu[(hpb)->lun]->request_queue,	\
+				##args);				\
+	} while (0)

So a new variable has been introduced at the controller level that represents the LUN for which a HPB command is being processed? Does this mean that this implementation forces serialization of all UFS commands that need HPB support? Aargh! Please don't do that.

drivers/scsi/ufs/ufshcd.h

Quoted 7 lines
drivers/scsi/ufs/ufshcd.h +7 -0
+/*
+ * define global constants
+ */
+static int sects_per_blk_shift;
+static int bits_per_dword_shift;
+static int bits_per_dword_mask;
+static int bits_per_byte_shift;

From ufshpb_init_constant():

sects_per_blk_shift = ffs(UFS_LOGICAL_BLOCK_SIZE) - ffs(SECTOR_SIZE); bits_per_dword_shift = ffs(BITS_PER_DWORD) - 1; bits_per_dword_mask = BITS_PER_DWORD - 1; bits_per_byte_shift = ffs(BITS_PER_BYTE) - 1;

If you want me to have a look at a later version of this patch series, please remove these variables and use #define or enum { ... = ... } instead. Please also use ilog2() instead of ffs(...) - 1. Please use SECTOR_SHIFT instead of ilog2(SECTOR_SIZE).

Quoted 4 lines
drivers/scsi/ufs/ufshcd.h +4 -0
+static void ufshpb_init_constant(struct ufs_hba *hba,
+				 struct ufshpb_geo_desc *geo_desc)
+{
+	struct ufshpb_geo *geo = &hba->hpb_geo;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+	u64 region_entry_sz;
+	int entries_per_region;
+
+	sects_per_blk_shift = ffs(UFS_LOGICAL_BLOCK_SIZE) - ffs(SECTOR_SIZE);
+	bits_per_dword_shift = ffs(BITS_PER_DWORD) - 1;
+	bits_per_dword_mask = BITS_PER_DWORD - 1;
+	bits_per_byte_shift = ffs(BITS_PER_BYTE) - 1;
+
+	geo->region_size_bytes = (u64)
+			SECTOR_SIZE * (0x01 << geo_desc->hpb_region_size);

Please use 1ULL << ... instead of the (u64) cast.

drivers/scsi/ufs/ufshcd.h

Quoted 2 lines
drivers/scsi/ufs/ufshcd.h +2 -0
+	geo->subregion_size_bytes = (u64)
+			SECTOR_SIZE * (0x01 << geo_desc->hpb_subregion_size);

Same comment here.

drivers/scsi/ufs/ufshcd.h

Quoted 6 lines
drivers/scsi/ufs/ufshcd.h +6 -0
+	lu_hpb->lu_region_cnt = ((unsigned long long)lu_num_blocks
+			       + (region_entry_size / HPB_ENTRY_SIZE) - 1) /
+				(region_entry_size / HPB_ENTRY_SIZE);
+	lu_hpb->lu_subregion_cnt = ((unsigned long long)lu_num_blocks
+			+ (subregion_entry_size / HPB_ENTRY_SIZE) - 1) /
+			(subregion_entry_size / HPB_ENTRY_SIZE);

Please use (u64) or (uint64_t) instead of (unsigned long long).

drivers/scsi/ufs/ufshcd.h

Quoted 11 lines
drivers/scsi/ufs/ufshcd.h +11 -0
+static inline int ufshpb_blocksize_is_supported(struct ufshpb_lu *hpb,
+						int sectors)
+{
+	int ret = true;
+
+	if (sectors > SECTORS_PER_BLOCK)
+		ret = false;
+		/* HPB 1.0 only supports 4KB TRANSFER LENGTH */
+
+	return ret;
+}

Please document the meaning of the 'sectors' argument and also its unit.

Can the body of the above function be simplified into the following?

	return sectors <= SECTORS_PER_BLOCK;
Quoted 22 lines
drivers/scsi/ufs/ufshcd.h +22 -0
+static struct
+ufshpb_region *ufshpb_victim_region_select(struct active_region_info *lru_info)
+{
+	struct ufshpb_region *cb;
+	struct ufshpb_region *victim_cb = NULL;
+	u32 hit_count = 0xffffffff;
+
+	switch (lru_info->selection_type) {
+	case LRU:
+		/*
+		 * Choose first region from the active region list
+		 */
+		victim_cb = list_first_entry(&lru_info->lru,
+					     struct ufshpb_region, list_region);
+		break;
+	case LFU:
+		/*
+		 * Choose active region with the lowest hit_count
+		 */
+		list_for_each_entry(cb, &lru_info->lru, list_region) {
+			if (hit_count > cb->hit_count) {
+				hit_count = cb->hit_count;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+				victim_cb = cb;
+			}
+		}
+		break;
+	default:
+		break;
+	}
+
+	return victim_cb;
+}

Which synchronization object protects the lru_info->lru list? How about documenting this with lockdep_assert_held()?

drivers/scsi/ufs/ufshcd.h

Quoted 5 lines
drivers/scsi/ufs/ufshcd.h +5 -0
+static inline struct
+hpb_sense_data *ufshpb_get_sense_data(struct ufshcd_lrb *lrbp)
+{
+	return (struct hpb_sense_data *)&lrbp->ucd_rsp_ptr->sr.sense_data_len;
+}

The above seems weird to me. sense_data_len has type __be16 and hence a size of two bytes. hpb_sense_data has a size of 20 bytes. How can it make sense to cast a pointer to __be16 to a pointer to strut hpb_sense_data?

Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +9 -0
+static void ufshpb_prepare_hpb_read(struct ufshpb_lu *hpb,
+				    struct ufshcd_lrb *lrbp,
+				    unsigned long long entry, int blk_cnt)
+{
+	unsigned char cmd[16] = { };
+	/*
+	 * cmd[14] = Transfer length
+	 */
+	cmd[0] = UFSHPB_READ;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+	cmd[2] = lrbp->cmd->cmnd[2];
+	cmd[3] = lrbp->cmd->cmnd[3];
+	cmd[4] = lrbp->cmd->cmnd[4];
+	cmd[5] = lrbp->cmd->cmnd[5];
+	put_unaligned_be64(entry, &cmd[6]);
+	cmd[14] = blk_cnt;
+	cmd[15] = 0x00;
+	memcpy(lrbp->cmd->cmnd, cmd, 16);
+	memcpy(lrbp->ucd_req_ptr->sc.cdb, cmd, 16);
+}

It seems weird to me that there is a copy of the CDB in struct utp_upiu_cmd. I don't know any other SCSI LLD that maintains two copies of the SCSI CDB. Can the CDB copy be removed from struct utp_upiu_cmd?

drivers/scsi/ufs/ufshcd.h

Quoted 12 lines
drivers/scsi/ufs/ufshcd.h +12 -0
+
+/*
+ * Note: hpb->hpb_lock (irq) should be held before calling this function
+ */
+static inline void ufshpb_l2p_entry_dirty_bit_set(struct ufshpb_lu *hpb,
+						  struct ufshpb_region *cb,
+						  struct ufshpb_subregion *cp,
+						  int dword, int offset,
+						  unsigned int count)
+{
+	struct ufshpb_geo *geo;
+	const unsigned long mask = ((1UL << count) - 1) & 0xffffffff;

Please change the type of mask into u32 and remove "& 0xffffffff".

Quoted 7 lines
drivers/scsi/ufs/ufshcd.h +7 -0
+static inline struct ufshpb_map_ctx *ufshpb_mctx_get(struct ufshpb_lu *hpb,
+						     int *err)
+{
+	struct ufshpb_map_ctx *mctx;
+
+	mctx = list_first_entry_or_null(&hpb->lh_map_ctx, struct ufshpb_map_ctx,
+					list_table);

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+	if (mctx) {
+		list_del_init(&mctx->list_table);
+		hpb->free_mctx_count--;
+		*err = 0;
+		return mctx;
+	}
+	*err = -ENOMEM;
+
+	return NULL;
+}

Please add a lockdep_assert_held() call that documents which synchronization object serializes accesses.

Quoted 29 lines
drivers/scsi/ufs/ufshcd.h +29 -0
+struct ufshpb_map_ctx *ufshpb_subregion_mctx_alloc(struct ufshpb_lu *hpb)
+{
+	int i, j;
+	int entries;
+	struct ufshpb_map_ctx *mctx;
+	struct ufshpb_geo *geo;
+
+	geo = &hpb->hba->hpb_geo;
+	entries = geo->entries_per_subregion;
+
+	mctx = kzalloc(sizeof(*mctx), GFP_KERNEL);
+	if (!mctx)
+		return NULL;
+
+	mctx->m_page = kcalloc(geo->mpages_per_subregion, sizeof(struct page *),
+			       GFP_KERNEL);
+	if (!mctx->m_page)
+		goto release_mem;
+
+	mctx->ppn_dirty = kvzalloc(entries >> bits_per_byte_shift, GFP_KERNEL);
+	if (!mctx->ppn_dirty)
+		goto release_mem;
+
+	for (i = 0; i < geo->mpages_per_subregion; i++) {
+		mctx->m_page[i] = alloc_page(GFP_KERNEL | __GFP_ZERO);
+		if (!mctx->m_page[i]) {
+			for (j = 0; j < i; j++)
+				kfree(mctx->m_page[j]);
+			goto release_mem;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+		}
+	}
+	return mctx;
+
+release_mem:
+	kfree(mctx->m_page);
+	kvfree(mctx->ppn_dirty);
+	kfree(mctx);
+	return NULL;
+}

Has it been considered to use vmalloc() or kvmalloc() instead of multiple alloc_page() calls?

Quoted 19 lines
drivers/scsi/ufs/ufshcd.h +19 -0
+static int ufshpb_add_bio_page(struct ufshpb_lu *hpb, struct request_queue *q,
+			       struct bio *bio, struct bio_vec *bvec,
+			       struct ufshpb_map_ctx *mctx)
+{
+	struct ufshpb_geo *geo;
+	struct page *page = NULL;
+	int i, ret = 0;
+
+	WARN_ON(!mctx);
+	WARN_ON(!bvec);
+
+	geo = &hpb->hba->hpb_geo;
+	bio_init(bio, bvec, geo->mpages_per_subregion);
+
+	for (i = 0; i < geo->mpages_per_subregion; i++) {
+		page = mctx->m_page[i];
+		if (!page)
+			return -ENOMEM;
+

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+		ret = bio_add_pc_page(q, bio, page, geo->mpage_bytes, 0);
+
+		if (ret != geo->mpage_bytes) {
+			hpb_err("bio_add_pc_page() error with ret %d\n", ret);
+			return -EINVAL;
+		}
+	}
+
+	return 0;
+}

Why does the above function use bio_add_pc_page() instead of bio_add_page()?

Quoted 82 lines
drivers/scsi/ufs/ufshcd.h +82 -0
+static int ufshpb_l2p_load(struct ufshpb_lu *hpb,
+			   struct ufshpb_map_req *map_req)
+{
+	struct ufs_req *ureq;
+	struct ufs_hba *hba = hpb->hba;
+	struct request_queue *q = hpb->hba->cmd_queue;
+	int retried = 0;
+	struct bio *bio;
+	int alloc_len;
+	int ret;
+
+	map_req->ureq = kmalloc(sizeof(struct ufs_req), GFP_KERNEL);
+	if (!map_req->ureq)
+		return -ENOMEM;
+	ureq = map_req->ureq;
+	ureq->req = NULL;
+
+	map_req->ureq->sense_buffer = kmalloc(UFS_SENSE_SIZE, GFP_KERNEL);
+	if (!map_req->ureq->sense_buffer) {
+		kfree(map_req->ureq);
+		return -ENOMEM;
+	}
+
+	bio = &map_req->bio;
+	ret = ufshpb_add_bio_page(hpb, q, bio, map_req->bvec, map_req->mctx);
+	if (ret) {
+		hpb_err("ufshpb_add_bio_page() failed with ret %d\n", ret);
+		goto free_mem;
+	}
+
+	alloc_len = hpb->hba->hpb_geo.subregion_entry_sz;
+	/*
+	 * HPB Sub-Regions are equally sized except for the last one which is
+	 * smaller if the last hpb Region is not an integer multiple of
+	 * bHPBSubRegionSize.
+	 */
+	if (map_req->region == (hpb->lu_region_cnt - 1) &&
+	    map_req->subregion == (hpb->subregions_in_last_region - 1))
+		alloc_len = hpb->last_subregion_entry_size;
+
+	ufshpb_prepare_read_buf_cmd(ureq->cmd, map_req->region,
+				    map_req->subregion, alloc_len);
+	if (!ureq->req) {
+		ureq->req = blk_get_request(q, REQ_OP_SCSI_IN, 0);
+		if (IS_ERR(ureq->req)) {
+			ret =  PTR_ERR(ureq->req);
+			goto free_mem;
+		}
+	}
+
+	blk_rq_append_bio(ureq->req, &bio);
+
+	ureq->cmd_len = 10;
+	ureq->lun = map_req->lun;
+	ureq->cmd_dir = DMA_FROM_DEVICE;
+
+	ufshpb_req_buffer_map(ureq);
+
+	map_req->req_issue_t = ktime_to_ns(ktime_get());
+	atomic64_inc(&hpb->status.map_req_cnt);
+
+	hpb_dbg(SCHEDULE_INFO, hpb, "ISSUE READ_BUFFER : (%d-%d) retry = %d\n",
+		map_req->region, map_req->subregion, map_req->retry_cnt);
+	hpb_trace(hpb, "%s: I RB %d - %d", DRIVER_NAME, map_req->region,
+		  map_req->subregion);
+	ret = ufshcd_exec_internal_req(hpb->hba, ureq);
+
+	if (hba->ufshpb_state != HPB_PRESENT)
+		goto free_mem_req;
+
+	if (ret) {
+		hpb_err("ureq Request result %d\n", ret);
+		ufshpb_l2p_load_error_handler(hpb, map_req, &retried);
+		if (retried)
+			return 0;
+		goto free_mem_req;
+	}
+	ufshpb_l2p_load_req_compl(hpb, map_req);
+
+free_mem_req:
+	blk_put_request(map_req->ureq->req);
+	map_req->ureq->req = NULL;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+free_mem:
+	kfree(ureq->sense_buffer);
+	kfree(ureq->sgt.sgl);
+	kfree(map_req->ureq);
+	INIT_LIST_HEAD(&map_req->list_map_req);
+	spin_lock(&hpb->hpb_lock);
+	list_add_tail(&map_req->list_map_req, &hpb->lh_map_req_free);
+	spin_unlock(&hpb->hpb_lock);
+	return ret;
+}

Do you agree that there is a lot of overlap between this function and scsi_execute()? How to prevent such code duplication?

Quoted 16 lines
drivers/scsi/ufs/ufshcd.h +16 -0
+static ssize_t ufshpb_sysfs_miss_show(struct ufshpb_lu *hpb, char *buf)
+{
+	long long region_miss, subregion_miss, entry_dirty_miss, rb_fail;
+	int ret = 0, count = 0;
+
+	region_miss = atomic64_read(&hpb->status.region_miss);
+	subregion_miss = atomic64_read(&hpb->status.subregion_miss);
+	entry_dirty_miss = atomic64_read(&hpb->status.entry_dirty_miss);
+	rb_fail = atomic64_read(&hpb->status.rb_fail);
+
+	ret = sprintf(buf + count, "Total entries missed: %lld\n",
+		      region_miss + subregion_miss + entry_dirty_miss);
+	count += ret;
+
+	ret = sprintf(buf + count, "Region: %lld\n", region_miss);
+	count += ret;

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+
+	ret = sprintf(buf + count, "subregion: %lld, entry dirty: %lld\n",
+		      subregion_miss, entry_dirty_miss);
+	count += ret;
+
+	ret = sprintf(buf + count, "HPB Read Buffer CMD failure: %lld\n",
+		      rb_fail);
+
+	return (count + ret);
+}

Please follow the sysfs one value per attribute rule.

Quoted 16 lines
drivers/scsi/ufs/ufshcd.h +16 -0
+static ssize_t ufshpb_sysfs_act_subregion_show(struct ufshpb_lu *hpb, char *buf)
+{
+	int ret = 0, count = 0, region, sub;
+	struct ufshpb_region *cb;
+	int state;
+
+	ret = sprintf(buf, "Clean Subregion List:\n");
+	count = ret;
+
+	for (region = 0; region < hpb->lu_region_cnt; region++) {
+		cb = hpb->region_tbl + region;
+		for (sub = 0; sub < cb->subregion_count; sub++) {
+			state = cb->subregion_tbl[sub].subregion_state;
+			if (state == SUBREGION_CLEAN) {
+				ret = sprintf(buf + count, "%d:%d ", region,
+					      sub);

drivers/scsi/ufs/ufshcd.h

Quoted 10 lines
drivers/scsi/ufs/ufshcd.h +10 -0
+				count += ret;
+			}
+		}
+	}
+
+	ret = sprintf(buf + count, "\n");
+	count += ret;
+
+	return count;
+}

Same comment here.

drivers/scsi/ufs/ufshcd.h

Quoted 9 lines
drivers/scsi/ufs/ufshcd.h +9 -0
+#define hpb_warn(fmt, ...)					\
+	pr_warn("%s:" fmt, DRIVER_NAME, ##__VA_ARGS__)
+#define hpb_notice(fmt, ...)					\
+	pr_notice("%s:" fmt, DRIVER_NAME, ##__VA_ARGS__)
+#define hpb_info(fmt, ...)					\
+	pr_info("%s:" fmt, DRIVER_NAME, ##__VA_ARGS__)
+#define hpb_err(fmt, ...)					\
+	pr_err("%s: %s:" fmt, __func__, DRIVER_NAME,		\
+					##__VA_ARGS__)

Please define pr_fmt() and use pr_warn() etc. directly instead of defining the above macros.

Thanks,

Bart.

author
#16 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Quoted 13 lines · Bart Van Assche

drivers/scsi/ufs/Kconfig config SCSI_UFS_BSG

Quoted 11 lines · On 2020-05-04 07:20, [email protected] wrote:
drivers/scsi/ufs/Kconfig +11 -0
+config SCSI_UFSHPB
+	bool "UFS Host Performance Booster (EXPERIMENTAL)"
+	depends on SCSI_UFSHCD
+	[ ... ]
+
+config UFSHPB_MAX_MEM_SIZE
+	int "UFS HPB maximum memory size per controller (in MiB)"
+	depends on SCSI_UFSHPB
+	default 128
+	range 0 65536
+	[ ... ]
Quoted 10 lines

Kernel module parameters are an inflexible mechanism because the value
of these parameters cannot be changed without rebuilding the kernel followed by a reboot. Has it been considered to make these two parameters configurable through sysfs? Has it been considered to make these parameters UFS device parameters instead of global parameters that
apply to all UFS devices?

Hi Bart
I ever tried these two approaches. let me explain why I finally choose kernel module "make menuconfig" way.

1: driver module parameter, while system booting, specify maximum HPB cache size in kernel boot Parameters:

   	static int max_hpb_mem = 128;
   	module_param(max_hpb_mem,int,0444);

but this paramter is added in the ufshcd.c since the ufshpb.c is not a independent module. looks not very natural.

2: sysfs parameters

This is a feasible option. we can enable HPB after system boot up, also, change the HPB cache maximum size.
this is the same with the current way, moreover, we don't want user to change HPB cache size while HPB is running.

If you prefer to convert current HPB driver to be a kernel module driver, I prefer first apporach.

thanks,

Bean

#17 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Quoted 11 lines · Bean Huo

1: driver module parameter, while system booting, specify maximum HPB cache size in kernel boot Parameters:

   	static int max_hpb_mem = 128;
   	module_param(max_hpb_mem,int,0444);

but this paramter is added in the ufshcd.c since the ufshpb.c is not a independent module. looks not very natural.
[ ... ]
If you prefer to convert current HPB driver to be a kernel module driver, I prefer first apporach.

Hi Bean,

A kernel module parameter sounds good to me.

Thanks,

Bart.

#18 lore raw

RE: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

In reply to [email protected]

Hi Bean,
This patch is ~3,000 lines long.
Is it possible to divide it into a series of a smaller, more reviewable patches?
E.g. it can be something like:
1) Init part I - Read HPB config
2) Init part II - prepare for L2P cache management (introduce here all the new data structures, memory allocation, etc.)
3) L2P cache management - activation / inactivation / eviction handlers
4) Add response API
5) Add prep_fn handler - the flows that contruct HPB-READ command.
Or any other division that makes sense to you.

Also, Is there a way to avoid all those #ifdefs everywhere?

Thanks,
Avri

Quoted 46 lines

From: Bean Huo <[email protected]>

This patch is to add support for the UFS Host Performance Booster (HPB v1.0),
which is used to improve UFS read performance, especially for the random read.

NAND flash-based storage devices, including UFS, have mechanisms to translate
logical addresses of requests to the corresponding physical addresses of the flash storage. Traditionally this L2P mapping data is loaded to the internal SRAM in the storage controller. When the capacity of storage is larger, a larger size of SRAM for the L2P map data is required. Since increased SRAM size affects the manufacturing cost significantly, it is not cost-effective to allocate all the amount of SRAM needed to keep all the Logical-address to Physical-address (L2P) map data. Therefore, L2P map data, which is required to identify the physical address for the requested IOs, can only be partially stored in SRAM from NAND flash. Due to this partial loading, accessing the flash address area where the L2P information for that address is not loaded in the SRAM can result in serious performance degradation.

The HPB is a software solution for the above problem, which uses the host’s system memory as a cache for the FTL L2P mapping table. It does not need additional hardware support from the host side. By using HPB, the L2P mapping
table can be read from host memory and stored in host-side memory. while reading the operation, the corresponding L2P information will be sent to the UFS device along with the reading request. Since the L2P entry is provided in the read request, UFS device does not have to load L2P entry from flash memory.
This will significantly improve random read performance.

---
 drivers/scsi/ufs/Kconfig  |   62 +
 drivers/scsi/ufs/Makefile |    1 +
 drivers/scsi/ufs/ufshcd.c |  234 +++-
 drivers/scsi/ufs/ufshcd.h |   23 +
 drivers/scsi/ufs/ufshpb.c | 2767
+++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshpb.h |  423 ++++++
 6 files changed, 3504 insertions(+), 6 deletions(-)
 create mode 100644 drivers/scsi/ufs/ufshpb.c
 create mode 100644 drivers/scsi/ufs/ufshpb.h
author
#19 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Quoted 14 lines · Avri Altman

Hi Bean,
This patch is ~3,000 lines long.
Is it possible to divide it into a series of a smaller, more reviewable patches?
E.g. it can be something like:
1) Init part I - Read HPB config
2) Init part II - prepare for L2P cache management (introduce here all the new data structures, memory allocation, etc.)
3) L2P cache management - activation / inactivation / eviction handlers
4) Add response API
5) Add prep_fn handler - the flows that contruct HPB-READ command. Or any other division that makes sense to you.

Quoted 5 lines

Also, Is there a way to avoid all those #ifdefs everywhere?

Thanks,
Avri

Hi, Avri

Thanks. I will split it in the next version based on each different funcitonality.

thanks,

Bean

#20 lore raw

RE: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

In reply to Avri Altman

+ seunguk.shin (who is one of the original author of the HPB, in case he has some more improvement inputs)

Hi Bean,
I second Avri input on splitting this patch series into logical smaller patches, that will helps reviewers. Also if you can add support to build HPB as kernel module that will be useful.
I am looking into the HPB 1.0 spec, will review your patches soon.

Regards,
Alim

Quoted 75 lines

-----Original Message-----
From: Avri Altman <[email protected]>
Sent: 08 May 2020 17:09
To: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]; [email protected]; [email protected]
Subject: RE: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Hi Bean,
This patch is ~3,000 lines long.
Is it possible to divide it into a series of a smaller, more reviewable patches? E.g. it can be something like:
1) Init part I - Read HPB config
2) Init part II - prepare for L2P cache management (introduce here all the new data structures, memory allocation, etc.)
3) L2P cache management - activation / inactivation / eviction handlers
4) Add response API
5) Add prep_fn handler - the flows that contruct HPB-READ command. Or any other division that makes sense to you.

Also, Is there a way to avoid all those #ifdefs everywhere?

Thanks,
Avri

Quoted 7 lines

From: Bean Huo <[email protected]>

This patch is to add support for the UFS Host Performance Booster (HPB v1.0), which is used to improve UFS read performance, especially for the random read.

Quoted 13 lines

NAND flash-based storage devices, including UFS, have mechanisms to translate logical addresses of requests to the corresponding physical addresses of the flash storage. Traditionally this L2P mapping data is loaded to the internal SRAM in the storage controller. When the capacity of storage is larger, a larger size of SRAM for the L2P map data is required. Since increased SRAM size affects the manufacturing cost significantly, it is not cost-effective to allocate all the amount of SRAM needed to keep all the Logical-address to Physical-address (L2P) map data. Therefore, L2P map data, which is required to identify the physical address for the requested IOs, can only be partially stored in SRAM from NAND flash. Due to this partial loading, accessing the flash address area where the L2P information for that address is not loaded in the SRAM can result in serious performance

degradation.

Quoted 24 lines

The HPB is a software solution for the above problem, which uses the host’s system memory as a cache for the FTL L2P mapping table. It does not need additional hardware support from the host side. By using HPB, the L2P mapping table can be read from host memory and stored in host-side memory. while reading the operation, the corresponding L2P information will be sent to the UFS device along with the reading request. Since the L2P entry is provided in the read request, UFS device does not have to load L2P entry from flash memory. This will significantly improve random read performance.

---
 drivers/scsi/ufs/Kconfig  |   62 +
 drivers/scsi/ufs/Makefile |    1 +
 drivers/scsi/ufs/ufshcd.c |  234 +++-
 drivers/scsi/ufs/ufshcd.h |   23 +
 drivers/scsi/ufs/ufshpb.c | 2767
+++++++++++++++++++++++++++++++++++++
 drivers/scsi/ufs/ufshpb.h |  423 ++++++
 6 files changed, 3504 insertions(+), 6 deletions(-)  create mode

100644 drivers/scsi/ufs/ufshpb.c create mode 100644 drivers/scsi/ufs/ufshpb.h

author
#21 lore raw

Re: [RESENT PATCH RFC v3 5/5] scsi: ufs: UFS Host Performance Booster(HPB) driver

Quoted 10 lines · Alim Akhtar

+ seunguk.shin (who is one of the original author of the HPB, in case he has some more improvement inputs)

Hi Bean,
I second Avri input on splitting this patch series into logical smaller patches, that will helps reviewers.
Also if you can add support to build HPB as kernel module that will be useful.
I am looking into the HPB 1.0 spec, will review your patches soon.

Quoted 2 lines

Regards,
Alim

Hi, Alim
that is great you added him, if he can give us more inputs,

For the HPB modulization, I did, also prefer it.
but I don't know how others think about it, since HPB is not a device .also, I should export some properties from UFSHCD. let's see if there are more supporters, let me try.

thanks,

Bean