Skip to contentSkip to conversation

Enable DMA clustering in the UFS driver

PATCH Applied Bart Van Assche 3 participants 8 messages Jan 12, 2023 – Jan 24, 2023

Subsystems (4)
lore
Share & tools
lore

Bart Van Assche

author reviewer
#0 lore raw

v2 cover Enable DMA clustering in the UFS driver

Hi Martin,

The third patch in this series enables DMA clustering in the UFS driver since UFS host controllers support DMA clustering. The first two patches fix bugs in the Exynos host controller driver.

Please consider this patch series for the next merge window.

Thanks,

Bart.

Changes compared to v1:
- Expanded the description of patch 3/3.
- Reworked patch 2/3 such that setting host->max_segment_size no longer races with LUN scanning.

Bart Van Assche (3):
scsi: ufs: Exynos: Fix DMA alignment for PAGE_SIZE != 4096
scsi: ufs: Exynos: Fix the maximum segment size
scsi: ufs: Enable DMA clustering

 drivers/ufs/core/ufshcd.c     |  5 ++---
 drivers/ufs/host/ufs-exynos.c | 10 +++++++++-
 include/ufs/ufshcd.h          |  4 ++--
 3 files changed, 13 insertions(+), 6 deletions(-)
author reviewer
#1 lore raw

PATCH v2 1/3 scsi: ufs: Exynos: Fix DMA alignment for PAGE_SIZE != 4096

The Exynos UFS controller only supports scatter/gather list elements that are aligned on a 4 KiB boundary. Fix DMA alignment in case PAGE_SIZE != 4096. Rename UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE into UFSHCD_QUIRK_4KB_DMA_ALIGNMENT.

Cc Kiwoong Kim <[email protected]>
Fixes 2b2bfc8aa519 ("scsi: ufs: Introduce a quirk to allow only page-aligned sg entries")
---
 drivers/ufs/core/ufshcd.c     | 4 ++--
 drivers/ufs/host/ufs-exynos.c | 2 +-
 include/ufs/ufshcd.h          | 4 ++--
 3 files changed, 5 insertions(+), 5 deletions(-)
drivers/ufs/core/ufshcd.c +2 -2
index 0514669e03be..5fdbc983ce2e 100644
@@ -5029,8 +5029,8 @@ static int ufshcd_slave_configure(struct scsi_device *sdev)
 	ufshcd_hpb_configure(hba, sdev);
 
 	blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
-	if (hba->quirks & UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE)
-		blk_queue_update_dma_alignment(q, PAGE_SIZE - 1);
+	if (hba->quirks & UFSHCD_QUIRK_4KB_DMA_ALIGNMENT)
+		blk_queue_update_dma_alignment(q, 4096 - 1);
 	/*
 	 * Block runtime-pm until all consumers are added.
 	 * Refer ufshcd_setup_links().
drivers/ufs/host/ufs-exynos.c +1 -1
index c3628a8645a5..3cdac89a28b8 100644
@@ -1673,7 +1673,7 @@ static const struct exynos_ufs_drv_data exynos_ufs_drvs = {
 				  UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR |
 				  UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL |
 				  UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING |
-				  UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE,
+				  UFSHCD_QUIRK_4KB_DMA_ALIGNMENT,
 	.opts			= EXYNOS_UFS_OPT_HAS_APB_CLK_CTRL |
 				  EXYNOS_UFS_OPT_BROKEN_AUTO_CLK_CTRL |
 				  EXYNOS_UFS_OPT_BROKEN_RX_SEL_IDX |
include/ufs/ufshcd.h +2 -2
index fc7373a1a15e..ca32df0ce6ba 100644
@@ -570,9 +570,9 @@ enum ufshcd_quirks {
 	UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING = 1 << 13,
 
 	/*
-	 * This quirk allows only sg entries aligned with page size.
+	 * Align DMA SG entries on a 4 KiB boundary.
 	 */
-	UFSHCD_QUIRK_ALIGN_SG_WITH_PAGE_SIZE		= 1 << 14,
+	UFSHCD_QUIRK_4KB_DMA_ALIGNMENT			= 1 << 14,
 
 	/*
 	 * This quirk needs to be enabled if the host controller does not
author reviewer
#2 lore raw

PATCH v2 2/3 scsi: ufs: Exynos: Fix the maximum segment size

In reply to Bart Van Assche

Prepare for enabling DMA clustering and also for supporting PAGE_SIZE != 4096 by declaring explicitly that the maximum segment size is 4096 bytes for Exynos UFS host controllers. Add this code in exynos_ufs_hce_enable_notify() such that it happens after scsi_host_alloc() and before __scsi_init_queue() is called by the LUN scanning code.

Cc Alim Akhtar <[email protected]>
Cc Kiwoong Kim <[email protected]>
---
 drivers/ufs/host/ufs-exynos.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/ufs/host/ufs-exynos.c +8 -0
index 3cdac89a28b8..7c985fc38db1 100644
@@ -1300,6 +1300,14 @@ static int exynos_ufs_hce_enable_notify(struct ufs_hba *hba,
 
 	switch (status) {
 	case PRE_CHANGE:
+		/*
+		 * The maximum segment size must be set after scsi_host_alloc()
+		 * has been called and before LUN scanning starts
+		 * (ufshcd_async_scan()). Note: this callback may also be called
+		 * from other functions than ufshcd_init().
+		 */
+		hba->host->max_segment_size = 4096;
+
 		if (ufs->drv_data->pre_hce_enable) {
 			ret = ufs->drv_data->pre_hce_enable(ufs);
 			if (ret)
#3 lore raw

Re: [PATCH v2 2/3] scsi: ufs: Exynos: Fix the maximum segment size

Alim,

Quoted 6 lines

Prepare for enabling DMA clustering and also for supporting PAGE_SIZE != 4096 by declaring explicitly that the maximum segment size is 4096 bytes for Exynos UFS host controllers. Add this code in exynos_ufs_hce_enable_notify() such that it happens after scsi_host_alloc() and before __scsi_init_queue() is called by the LUN scanning code.

Now that you're Exynos maintainer it falls upon you to review Bart's patch.

Thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering
maintainer Reviewed-by Tested-by on v2 2/3
#4 lore raw

RE: [PATCH v2 2/3] scsi: ufs: Exynos: Fix the maximum segment size

In reply to Bart Van Assche
Quoted 15 lines

-----Original Message-----
From: Bart Van Assche [mailto:[email protected]]
Sent: Friday, January 13, 2023 5:12 AM
To: Martin K . Petersen <[email protected]>
Cc: Jaegeuk Kim <[email protected]>; [email protected]; Adrian Hunter <[email protected]>; Alim Akhtar <[email protected]>; Bart Van Assche <[email protected]>; Kiwoong Kim <[email protected]>; James E.J. Bottomley <[email protected]>; Krzysztof Kozlowski <[email protected]>; Chanho Park <[email protected]>; Bean Huo <[email protected]>
Subject: [PATCH v2 2/3] scsi: ufs: Exynos: Fix the maximum segment size

Quoted 2 lines

Prepare for enabling DMA clustering and also for supporting PAGE_SIZE != 4096 by declaring explicitly that the maximum segment size is 4096 bytes

for

Quoted 3 lines

Exynos UFS host controllers. Add this code in exynos_ufs_hce_enable_notify() such that it happens after scsi_host_alloc() and before __scsi_init_queue() is called by the LUN

scanning

Quoted 6 lines

code.

Cc Alim Akhtar <[email protected]>
Cc Kiwoong Kim <[email protected]>

---

Tested basic read/write on platform containing Exynos UFS HCI, so

Quoted 25 lines

drivers/ufs/host/ufs-exynos.c | 8 ++++++++

1 file changed, 8 insertions(+)
drivers/ufs/host/ufs-exynos.c +4 -0
index 3cdac89a28b8..7c985fc38db1 100644
@@ -1300,6 +1300,14 @@ static int exynos_ufs_hce_enable_notify(struct
 ufs_hba *hba,
 
 	switch (status) {
 	case PRE_CHANGE:
+		/*
+		 * The maximum segment size must be set after
 scsi_host_alloc()
+		 * has been called and before LUN scanning starts
+		 * (ufshcd_async_scan()). Note: this callback may also be

called

drivers/ufs/host/ufs-exynos.c +4 -0
+		 * from other functions than ufshcd_init().
+		 */
+		hba->host->max_segment_size = 4096;
+
		if (ufs->drv_data->pre_hce_enable) {
			ret = ufs->drv_data->pre_hce_enable(ufs);
			if (ret)
author reviewer
#5 lore raw

PATCH v2 3/3 scsi: ufs: Enable DMA clustering

In reply to Bart Van Assche

All UFS host controllers support DMA clustering. Hence enable DMA clustering.

Notes:
- The max_segment_size parameter implements the 256 KiB limit for the PRDT. The dma_boundary parameter represents a boundary that must not be crossed by DMA scatter/gather lists. I'm not aware of any restrictions on DMA scatter/gather lists in the UFSHCI specification other than the 256 KiB limit for the PRDT and the 32-bit address restriction for controllers that only support 32-bits DMA. The latter restriction is already handled by ufshcd_set_dma_mask().
- Without patch "Exynos: Fix the maximum segment size", this patch breaks support for the Exynos controller.

The history of the dma_boundary parameter in the UFS driver is as follows:
* The initial UFS driver did not set the dma_boundary parameter.
* Commit 4dd4130a722f ("scsi: make sure all drivers set the use_clustering flag") set the .use_clustering flag.
* Commit 4af14d113bcf ("scsi: remove the use_clustering flag") removed the use_clustering flag and set the dma_boundary parameter instead.

---
 drivers/ufs/core/ufshcd.c | 1 -
 1 file changed, 1 deletion(-)
drivers/ufs/core/ufshcd.c +0 -1
index 5fdbc983ce2e..d28b44a1ffcf 100644
@@ -8460,7 +8460,6 @@ static struct scsi_host_template ufshcd_driver_template = {
 	.max_host_blocked	= 1,
 	.track_queue_depth	= 1,
 	.sdev_groups		= ufshcd_driver_groups,
-	.dma_boundary		= PAGE_SIZE - 1,
 	.rpm_autosuspend_delay	= RPM_AUTOSUSPEND_DELAY_MS,
 };
 
maintainer Reviewed-by on v2 3/3
#6 lore raw

RE: [PATCH v2 3/3] scsi: ufs: Enable DMA clustering

Quoted 37 lines

-----Original Message-----
From: Bart Van Assche [mailto:[email protected]]
Sent: Friday, January 13, 2023 5:12 AM
To: Martin K . Petersen <[email protected]>
Cc: Jaegeuk Kim <[email protected]>; [email protected]; Adrian Hunter <[email protected]>; Alim Akhtar <[email protected]>; Bart Van Assche <[email protected]>; Avri Altman <[email protected]>; Kiwoong Kim <[email protected]>; James E.J. Bottomley <[email protected]>; Bean Huo <[email protected]>; Stanley Chu <[email protected]>; Jinyoung Choi <[email protected]>
Subject: [PATCH v2 3/3] scsi: ufs: Enable DMA clustering

All UFS host controllers support DMA clustering. Hence enable DMA clustering.

Notes:
- The max_segment_size parameter implements the 256 KiB limit for the PRDT. The dma_boundary parameter represents a boundary that must not be crossed by DMA scatter/gather lists. I'm not aware of any restrictions on DMA scatter/gather lists in the UFSHCI specification other than the 256 KiB limit for the PRDT and the 32-bit address restriction for controllers that only support 32-bits DMA. The latter restriction is already handled by ufshcd_set_dma_mask().
- Without patch "Exynos: Fix the maximum segment size", this patch breaks support for the Exynos controller.

The history of the dma_boundary parameter in the UFS driver is as follows:
* The initial UFS driver did not set the dma_boundary parameter.
* Commit 4dd4130a722f ("scsi: make sure all drivers set the use_clustering flag") set the .use_clustering flag.
* Commit 4af14d113bcf ("scsi: remove the use_clustering flag") removed the use_clustering flag and set the dma_boundary parameter instead.

Quoted 6 lines

---

Quoted 16 lines

drivers/ufs/core/ufshcd.c | 1 -

1 file changed, 1 deletion(-)
drivers/ufs/core/ufshcd.c index renamed from drivers/ufs/core/ufshcd.c +0 -0

5fdbc983ce2e..d28b44a1ffcf 100644

drivers/ufs/core/ufshcd.c +0 -1
@@ -8460,7 +8460,6 @@ static struct scsi_host_template
 ufshcd_driver_template = {
 	.max_host_blocked	= 1,
 	.track_queue_depth	= 1,
 	.sdev_groups		= ufshcd_driver_groups,
-	.dma_boundary		= PAGE_SIZE - 1,
 	.rpm_autosuspend_delay	= RPM_AUTOSUSPEND_DELAY_MS,
 };
#7 lore raw
In reply to Bart Van Assche

Bart,

Quoted 3 lines

The third patch in this series enables DMA clustering in the UFS driver since UFS host controllers support DMA clustering. The first two patches fix bugs in the Exynos host controller driver.

Applied to 6.3/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen	Oracle Linux Engineering