Skip to contentSkip to conversation

Subject: [PATCH 2/2] scsi: sd: Treat locked encrypted drives as "no media"

PATCH Kamil Kaminski 1 participant 1 message Feb 25 SCSI

lore
Share & tools
lore
#0 lore raw

From 4cf844d0840836e406e6eb1b65bd512b89fcdc20 Mon Sep 17 00:00:00 2001
From: Kamil Kaminski <[email protected]>
Date: Wed, 25 Feb 2026 13:43:29 -0600
Subject: [PATCH 2/2] scsi: sd: Treat locked encrypted drives as "no media"

SanDisk Extreme Portable SSD and similar hardware-encrypted drives return "Logical unit access not authorized" (ASC 0x74, ASCQ 0x71) when password-locked. Currently, the driver attempts to read capacity and partition tables, causing excessive I/O errors that crash USB controllers.

Detect the locked condition early in sd_revalidate_disk() using scsi_test_unit_ready(). If locked, mark the drive as having no media and set capacity to 0. This prevents partition scanning and allows graceful handling (similar to an empty optical drive).

The virtual CD-ROM interface remains available for vendor unlock software.

---
 drivers/scsi/sd.c | 34 ++++++++++++++++++++++++++++++++++
 1 file changed, 34 insertions(+)
drivers/scsi/sd.c +33 -0
index 628a1d0a74ba..c83004100bc4 100644
@@ -3721,6 +3721,8 @@ static void sd_revalidate_disk(struct gendisk *disk)
 	struct scsi_device *sdp = sdkp->device;
 	sector_t old_capacity = sdkp->capacity;
 	struct queue_limits *lim = NULL;
+	struct scsi_sense_hdr sshdr;
+	int retval;
 	unsigned char *buffer = NULL;
 	unsigned int dev_max;
 	int err;
@@ -3743,6 +3744,37 @@ static void sd_revalidate_disk(struct gendisk *disk)
 	if (!buffer)
 		goto out;
 
+	/*
+	 * Check for hardware-encrypted locked devices early.
+	 * SanDisk Extreme and similar drives return ASC 0x74/ASCQ 0x71
+	 * "Logical unit access not authorized" when password locked.
+	 * Detect this before attempting capacity reads to prevent
+	 * excessive I/O errors during partition scanning.
+	 */
+	memset(&sshdr, 0, sizeof(sshdr));
+	retval = scsi_test_unit_ready(sdp, SD_TIMEOUT, sdkp->max_retries, &sshdr);
+	if (retval && sshdr.sense_key == DATA_PROTECT &&
+	    sshdr.asc == 0x74 && sshdr.ascq == 0x71) {
+		sd_printk(KERN_WARNING, sdkp,
+			"Device is locked (hardware encryption) - treating as no media to prevent I/O errors\n");
+		set_media_not_present(sdkp);
+		sdkp->capacity = 0;
+		/* Set minimal valid block sizes */
+		sdkp->physical_block_size = 512;
+		sdp->sector_size = 512;
+
+		*lim = queue_limits_start_update(sdkp->disk->queue);
+		lim->logical_block_size = 512;
+		lim->physical_block_size = 512;
+		err = queue_limits_commit_update_frozen(sdkp->disk->queue, lim);
+		if (err)
+			goto out;
+
+		set_capacity_and_notify(disk, 0);
+		sdkp->first_scan = 0;
+		goto out;
+	}
+
 	sd_spinup_disk(sdkp);
 
 	*lim = queue_limits_start_update(sdkp->disk->queue);
Signature · 1 line
2.53.0