From: Ahmad Fatoum <a.fatoum@barebox.org>
To: barebox@lists.infradead.org
Cc: Ahmad Fatoum <a.fatoum@barebox.org>
Subject: [PATCH 23/26] usb-storage: preserve READ CAPACITY sector size
Date: Fri, 26 Jun 2026 10:42:34 +0200 [thread overview]
Message-ID: <20260626084608.1388806-24-a.fatoum@barebox.org> (raw)
In-Reply-To: <20260626084608.1388806-1-a.fatoum@barebox.org>
READ CAPACITY reports the logical sector size, but as the common block and
partition support used to be incompatible with non-512-byte sectors, it
just warned about non-512-byte media and forced SECTOR_SHIFT anyway.
Remove the hardcoded SECTOR_SHIFT/SECTOR_SIZE instances to gain support
for 4K-sectors and more.
Assisted-by: Codex:gpt-5.5
Signed-off-by: Ahmad Fatoum <a.fatoum@barebox.org>
---
drivers/usb/storage/usb.c | 44 +++++++++++++++++++++++----------------
1 file changed, 26 insertions(+), 18 deletions(-)
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index b3116dc6e6c2..a991a589dd48 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -11,6 +11,7 @@
#include <init.h>
#include <malloc.h>
#include <dma.h>
+#include <disks.h>
#include <errno.h>
#include <scsi.h>
#include <linux/usb/usb.h>
@@ -150,6 +151,22 @@ static int usb_stor_test_unit_ready(struct us_blk_dev *usb_blkdev, u64 timeout_n
return ret ? -ENODEV : 0;
}
+static int usb_stor_set_capacity(struct us_blk_dev *usb_blkdev, sector_t lba,
+ unsigned int sector_size)
+{
+ struct device *dev = &usb_blkdev->us->pusb_dev->dev;
+ int blockbits;
+
+ blockbits = block_size_bits(dev, sector_size);
+ if (blockbits < 0)
+ return blockbits;
+
+ usb_blkdev->blk.blockbits = blockbits;
+ usb_blkdev->blk.num_blocks = lba + 1;
+
+ return 0;
+}
+
static int read_capacity_16(struct us_blk_dev *usb_blkdev)
{
struct device *dev = &usb_blkdev->us->pusb_dev->dev;
@@ -186,10 +203,7 @@ static int read_capacity_16(struct us_blk_dev *usb_blkdev)
goto fail;
}
- usb_blkdev->blk.blockbits = SECTOR_SHIFT;
- usb_blkdev->blk.num_blocks = lba + 1;
-
- ret = sector_size;
+ ret = usb_stor_set_capacity(usb_blkdev, lba, sector_size);
fail:
dma_free(data);
return ret;
@@ -222,13 +236,7 @@ static int read_capacity_10(struct us_blk_dev *usb_blkdev)
dev_dbg(dev, "LBA (10) = 0x%llx w/ sector size = %u\n",
lba, sector_size);
- 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;
-
- ret = SECTOR_SIZE;
+ ret = usb_stor_set_capacity(usb_blkdev, lba, sector_size);
fail:
dma_free(data);
return ret;
@@ -237,6 +245,7 @@ static int read_capacity_10(struct us_blk_dev *usb_blkdev)
static int usb_stor_io_16(struct us_blk_dev *usb_blkdev, u8 opcode,
sector_t start, u8 *data, u16 blocks)
{
+ u32 bytes = (u32)blocks << usb_blkdev->blk.blockbits;
u8 cmd[16];
memset(cmd, 0, sizeof(cmd));
@@ -244,13 +253,14 @@ static int usb_stor_io_16(struct us_blk_dev *usb_blkdev, u8 opcode,
put_unaligned_be64(start, &cmd[2]);
put_unaligned_be32(blocks, &cmd[10]);
- return usb_stor_transport(usb_blkdev, cmd, sizeof(cmd), data,
- blocks * SECTOR_SIZE, 10, 0);
+ return usb_stor_transport(usb_blkdev, cmd, sizeof(cmd), data, bytes,
+ 10, 0);
}
static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 opcode,
sector_t start, u8 *data, u16 blocks)
{
+ u32 bytes = (u32)blocks << usb_blkdev->blk.blockbits;
u8 cmd[10];
memset(cmd, 0, sizeof(cmd));
@@ -258,8 +268,8 @@ static int usb_stor_io_10(struct us_blk_dev *usb_blkdev, u8 opcode,
put_unaligned_be32(start, &cmd[2]);
put_unaligned_be16(blocks, &cmd[7]);
- return usb_stor_transport(usb_blkdev, cmd, sizeof(cmd), data,
- blocks * SECTOR_SIZE, 10, 0);
+ return usb_stor_transport(usb_blkdev, cmd, sizeof(cmd), data, bytes,
+ 10, 0);
}
/***********************************************************************
@@ -315,7 +325,7 @@ static int usb_stor_blk_io(struct block_device *disk_dev,
sector_start += n;
sector_count -= n;
- buffer += n * SECTOR_SIZE;
+ buffer += (u32)n << disk_dev->blockbits;
}
return sector_count ? -EIO : 0;
@@ -420,7 +430,6 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
dev_info(dev, "registering as disk%d\n", result);
pblk_dev->blk.cdev.name = basprintf("disk%d", result);
- pblk_dev->blk.blockbits = SECTOR_SHIFT;
pblk_dev->blk.type = BLK_TYPE_USB;
pblk_dev->blk.removable = true;
pblk_dev->blk.rootwait = true;
@@ -631,4 +640,3 @@ static int __init usb_stor_init(void)
return usb_driver_register(&usb_storage_driver);
}
device_initcall(usb_stor_init);
-
--
2.47.3
next prev parent reply other threads:[~2026-06-26 8:46 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-26 8:42 [PATCH 00/26] block: add support for non-512 byte sectors Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 01/26] nvme: use barebox-appropriate 64-bit type for timeouts Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 02/26] nvme: fix buffer advancement when chunking due to max_hw_sectors Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 03/26] nvme: allow flush opcode Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 04/26] nvme: honor namespace block size for I/O Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 05/26] bootscan: fix detection of GPT Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 06/26] block: clarify that writebuffer_io_len returns sector counts Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 07/26] block: fix wrong type for discard_start/size byte ranges Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 08/26] block: fix discard zeroing too little memory Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 09/26] block: use logical block size for reparse checks Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 10/26] block: require lower bound of sector size to be 512 bytes Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 11/26] filetype: don't hardcode sector size in file_detect_partition_table Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 12/26] block: define helpers for non-512-byte sector support Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 13/26] bootscan: use block size for partition table probe Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 14/26] ramdisk: validate exported sector size Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 15/26] efi: block: fix sector size mismatch in block device registration and ops Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 16/26] efi: loader: disk: report block device size in Block I/O Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 17/26] efi: loader: file: report cdev block size in file info Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 18/26] partitions: use byte offset for first partition policy Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 19/26] partitions: dos: allocate correctly sized buffer for dos_partition_desc Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 20/26] partition: support non-512 byte sectors Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 21/26] fs: fat: fix garbage read when writing with bigger block size Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 22/26] fs: fat: support larger block device sectors Ahmad Fatoum
2026-06-26 8:42 ` Ahmad Fatoum [this message]
2026-06-26 8:42 ` [PATCH 24/26] fuzz: add 4K-sector partition ramdisk target Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 25/26] commands: parted: prepare use of non-512-byte sectors Ahmad Fatoum
2026-06-26 8:42 ` [PATCH 26/26] commands: parted: exit if block size if not 512 Ahmad Fatoum
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=20260626084608.1388806-24-a.fatoum@barebox.org \
--to=a.fatoum@barebox.org \
--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