mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH 1/2] usb: storage: refactor usb 32-bit capacity read
Date: Thu, 25 Feb 2021 11:46:17 +0100	[thread overview]
Message-ID: <20210225104618.29874-1-a.fatoum@pengutronix.de> (raw)

usb_stor_read_capacity uses SCSI Read Capacity 10, which assumes
capacity never exceeds 32-bit, which equals 2TiB max capacity at
512 byte sector size.

In preparation for porting support for SCSI Read Capacity 16 from
Linux, move over all Read Capacity 10 related code into a single
function. Some more refactoring is done to make the function look
more like the Linux implementation. This also makes it easier to spot
the differences in retry/timeout handling, which we might want to
adopt in future.

No functional change intended.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 drivers/usb/storage/usb.c | 62 +++++++++++++++++++++------------------
 1 file changed, 33 insertions(+), 29 deletions(-)

diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index c264dd4b71e2..122af659820f 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -157,31 +157,49 @@ static int usb_stor_test_unit_ready(struct us_blk_dev *usb_blkdev)
 	return 0;
 }
 
-static int usb_stor_read_capacity(struct us_blk_dev *usb_blkdev,
-				  u32 *last_lba, u32 *block_length)
+static int read_capacity_10(struct us_blk_dev *usb_blkdev)
 {
 	struct device_d *dev = &usb_blkdev->us->pusb_dev->dev;
+	unsigned char cmd[16];
 	const u32 datalen = 8;
-	u32 *data = xzalloc(datalen);
-	u8 cmd[10];
+	__be32 *data = xzalloc(datalen);
 	int ret;
+	sector_t lba;
+	unsigned sector_size;
 
 	memset(cmd, 0, sizeof(cmd));
 	cmd[0] = SCSI_RD_CAPAC;
 
 	ret = usb_stor_transport(usb_blkdev, cmd, sizeof(cmd), data, datalen,
 				 3, USB_STOR_NO_REQUEST_SENSE);
-	if (ret < 0)
-		goto exit;
 
-	dev_dbg(dev, "Read Capacity returns: 0x%x, 0x%x\n",
-		data[0], data[1]);
-	*last_lba = be32_to_cpu(data[0]);
-	*block_length = be32_to_cpu(data[1]);
+	if (ret < 0) {
+		dev_dbg(dev, "Cannot read device capacity\n");
+		return ret;
+	}
 
-exit:
-	free(data);
-	return ret;
+	sector_size = be32_to_cpu(data[1]);
+	lba = be32_to_cpu(data[0]);
+
+	dev_dbg(dev, "LBA (10) = 0x%llx w/ sector size = %u\n",
+		lba, sector_size);
+
+
+	if (lba == U32_MAX) {
+		lba = U32_MAX - 1;
+		dev_warn(dev,
+			 "Limiting device size due to 32 bit constraints\n");
+		/* To support LBA >= U32_MAX, a READ CAPACITY (16) should be issued instead */
+	}
+
+
+	if (sector_size != SECTOR_SIZE)
+		dev_warn(dev, "Support only %d bytes sectors\n", SECTOR_SIZE);
+
+	usb_blkdev->blk.num_blocks = lba + 1;
+	usb_blkdev->blk.blockbits = SECTOR_SHIFT;
+
+	return SECTOR_SIZE;
 }
 
 static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 opcode,
@@ -275,7 +293,6 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)
 {
 	struct us_data *us = pblk_dev->us;
 	struct device_d *dev = &us->pusb_dev->dev;
-	u32 last_lba = 0, block_length = 0;
 	int result;
 
 	/* get device info */
@@ -299,23 +316,10 @@ static int usb_stor_init_blkdev(struct us_blk_dev *pblk_dev)
 	/* read capacity */
 	dev_dbg(dev, "Reading capacity\n");
 
-	result = usb_stor_read_capacity(pblk_dev, &last_lba, &block_length);
-	if (result < 0) {
-		dev_dbg(dev, "Cannot read device capacity\n");
+	result = read_capacity_10(pblk_dev);
+	if (result < 0)
 		return result;
-	}
-
-	if (last_lba == U32_MAX) {
-		last_lba = U32_MAX - 1;
-		dev_warn(dev,
-			 "Limiting device size due to 32 bit constraints\n");
-		/* To support LBA >= U32_MAX, a READ CAPACITY (16) should be issued here */
-	}
 
-	pblk_dev->blk.num_blocks = last_lba + 1;
-	if (block_length != SECTOR_SIZE)
-		pr_warn("Support only %d bytes sectors\n", SECTOR_SIZE);
-	pblk_dev->blk.blockbits = SECTOR_SHIFT;
 	dev_dbg(dev, "Capacity = 0x%llx, blockshift = 0x%x\n",
 		 pblk_dev->blk.num_blocks, pblk_dev->blk.blockbits);
 
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


             reply	other threads:[~2021-02-25 10:47 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25 10:46 Ahmad Fatoum [this message]
2021-02-25 10:46 ` [PATCH 2/2] usb: storage: add support for drivers larger than 2TiB Ahmad Fatoum
2021-03-01 15:52 ` [PATCH 1/2] usb: storage: refactor usb 32-bit capacity read Sascha Hauer

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=20210225104618.29874-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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