Skip to contentSkip to conversation

scsi: sd: infer lbpme from VPD B2 when READ CAPACITY 16 lacks LBPME

PATCH In review Hao Yao 2 participants 2 messages Apr 2 – Apr 14 SCSI

lore
Share & tools
lore

Hao Yao

author
#0 lore raw

PATCH 1/1 scsi: sd: infer lbpme from VPD B2 when READ CAPACITY 16 lacks LBPME

Some USB-NVMe bridge devices (e.g. Realtek RTL9210) correctly report UNMAP support via VPD page B2 (LBPU=1) and block limits via VPD page B0, but fail to set the LBPME (Logical Block Provisioning Management Enabled) flag in the READ CAPACITY 16 response. This prevents the SCSI disk driver from enabling discard/TRIM support.

Fix this by removing the early return in sd_read_block_provisioning() when lbpme is not set, allowing the function to read VPD B2 regardless. If VPD B2 indicates LBPU (UNMAP) support, set lbpme so that sd_read_block_limits() and sd_discard_mode() can properly configure discard.

---
 drivers/scsi/sd.c | 17 ++++++++++++++---
 1 file changed, 14 insertions(+), 3 deletions(-)
drivers/scsi/sd.c +14 -3
index 628a1d0a74ba..ba653988dd13 100644
@@ -3466,9 +3466,6 @@ static void sd_read_block_provisioning(struct scsi_disk *sdkp)
 {
 	struct scsi_vpd *vpd;
 
-	if (sdkp->lbpme == 0)
-		return;
-
 	rcu_read_lock();
 	vpd = rcu_dereference(sdkp->device->vpd_pgb2);
 
@@ -3481,6 +3478,20 @@ static void sd_read_block_provisioning(struct scsi_disk *sdkp)
 	sdkp->lbpu	= (vpd->data[5] >> 7) & 1; /* UNMAP */
 	sdkp->lbpws	= (vpd->data[5] >> 6) & 1; /* WRITE SAME(16) w/ UNMAP */
 	sdkp->lbpws10	= (vpd->data[5] >> 5) & 1; /* WRITE SAME(10) w/ UNMAP */
+
+	/*
+	 * Some USB-NVMe bridge devices (e.g. Realtek RTL9210) report UNMAP
+	 * support via VPD B2 (LBPU=1) but fail to set the LBPME flag in the
+	 * READ CAPACITY 16 response. If VPD B2 indicates UNMAP support,
+	 * enable lbpme so discard can be configured properly.
+	 */
+	if (!sdkp->lbpme && sdkp->lbpu) {
+		if (sdkp->first_scan)
+			sd_printk(KERN_NOTICE, sdkp,
+				  "LBPME not set in READ CAPACITY 16 but LBPU set in VPD B2; enabling discard\n");
+		sdkp->lbpme = 1;
+	}
+
 	rcu_read_unlock();
 }
 
Signature · 1 line
2.43.0
#1 lore raw

Hi Hao!

Quoted 3 lines

If VPD B2 indicates LBPU (UNMAP) support, set lbpme so that sd_read_block_limits() and sd_discard_mode() can properly configure discard.

If a storage device vendor can not be trusted to fill out the most basic protocol fields correctly, how can they be trusted to safeguard your data?

You can create a udev rule to override the provisioning_mode in sysfs.

Signature · 1 line
Martin K. Petersen