public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kamil Kaminski <kylek389@gmail.com>
To: linux-scsi@vger.kernel.org
Cc: James.Bottomley@HansenPartnership.com,
	martin.petersen@oracle.com, linux-kernel@vger.kernel.org,
	kylek389@gmail.com
Subject: Subject: [PATCH 2/2] scsi: sd: Treat locked encrypted drives as "no media"
Date: Wed, 25 Feb 2026 17:16:12 -0600	[thread overview]
Message-ID: <293e4493-023b-415f-b2b0-0fe3cdfe7149@gmail.com> (raw)

 From 4cf844d0840836e406e6eb1b65bd512b89fcdc20 Mon Sep 17 00:00:00 2001
From: Kamil Kaminski <kylek389@gmail.com>
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.

Closes: https://bugzilla.kernel.org/show_bug.cgi?id=216696
Signed-off-by: Kamil Kaminski <kylek389@gmail.com>
---
  drivers/scsi/sd.c | 34 ++++++++++++++++++++++++++++++++++
  1 file changed, 34 insertions(+)

diff --git a/drivers/scsi/sd.c b/drivers/scsi/sd.c
index 628a1d0a74ba..c83004100bc4 100644
--- a/drivers/scsi/sd.c
+++ b/drivers/scsi/sd.c
@@ -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);
-- 
2.53.0



                 reply	other threads:[~2026-02-25 23:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=293e4493-023b-415f-b2b0-0fe3cdfe7149@gmail.com \
    --to=kylek389@gmail.com \
    --cc=James.Bottomley@HansenPartnership.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-scsi@vger.kernel.org \
    --cc=martin.petersen@oracle.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox