[PATCH v2 023/113] block: record block device type

Ahmad Fatoum a.fatoum at pengutronix.de
Mon Mar 4 10:59:08 PST 2024


Software running under EFI can query the type of a block device. For
barebox to be able to report this, start assigning types to all block
devices it can create. No functional change yet.

Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
v1 -> v2:
  - fix typo breaking compilation of disk_ata_drive.c
---
 arch/sandbox/board/hostfile.c |  1 +
 common/block.c                | 24 ++++++++++++++++++++++++
 drivers/ata/ahci.c            |  1 +
 drivers/ata/disk_ata_drive.c  |  1 +
 drivers/block/efi-block-io.c  |  1 +
 drivers/block/virtio_blk.c    |  1 +
 drivers/mci/mci-core.c        |  1 +
 drivers/nvme/host/core.c      |  1 +
 drivers/usb/storage/usb.c     |  1 +
 include/ata_drive.h           |  5 +++--
 include/block.h               | 16 +++++++++++++++-
 11 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/arch/sandbox/board/hostfile.c b/arch/sandbox/board/hostfile.c
index 88d4d6605fd1..7afad95b6d8b 100644
--- a/arch/sandbox/board/hostfile.c
+++ b/arch/sandbox/board/hostfile.c
@@ -161,6 +161,7 @@ static int hf_probe(struct device *dev)
 		priv->blk.ops = &hf_blk_ops;
 		priv->blk.blockbits = SECTOR_SHIFT;
 		priv->blk.num_blocks = reg[1] / SECTOR_SIZE;
+		priv->blk.type = BLK_TYPE_VIRTUAL;
 
 		err = blockdevice_register(&priv->blk);
 		if (err)
diff --git a/common/block.c b/common/block.c
index faf0e7c0cbe5..c7ca4fb4034a 100644
--- a/common/block.c
+++ b/common/block.c
@@ -495,3 +495,27 @@ unsigned file_list_add_blockdevs(struct file_list *files)
 
 	return count;
 }
+
+const char *blk_type_str(enum blk_type type)
+{
+	switch (type) {
+	case BLK_TYPE_UNSPEC:
+		return "unspecified";
+	case BLK_TYPE_SD:
+		return "SD";
+	case BLK_TYPE_MMC:
+		return "MMC";
+	case BLK_TYPE_VIRTUAL:
+		return "virtual";
+	case BLK_TYPE_IDE:
+		return "IDE";
+	case BLK_TYPE_AHCI:
+		return "AHCI";
+	case BLK_TYPE_USB:
+		return "USB";
+	case BLK_TYPE_NVME:
+		return "NVMe";
+	default:
+		return "unknown";
+	}
+}
diff --git a/drivers/ata/ahci.c b/drivers/ata/ahci.c
index d4d1005f6165..de6748288141 100644
--- a/drivers/ata/ahci.c
+++ b/drivers/ata/ahci.c
@@ -624,6 +624,7 @@ int ahci_add_host(struct ahci_device *ahci)
 		ahci_port->ata.dev = ahci->dev;
 		ahci_port->port_mmio = ahci_port_base(ahci->mmio_base, i);
 		ahci_port->ata.ops = &ahci_ops;
+		ahci_port->ata.ahci = true;
 		ata_port_register(&ahci_port->ata);
 	}
 
diff --git a/drivers/ata/disk_ata_drive.c b/drivers/ata/disk_ata_drive.c
index 2d97710b827a..a49acc1641da 100644
--- a/drivers/ata/disk_ata_drive.c
+++ b/drivers/ata/disk_ata_drive.c
@@ -245,6 +245,7 @@ static int ata_port_init(struct ata_port *port)
 
 	port->blk.num_blocks = ata_id_n_sectors(port->id);
 	port->blk.blockbits = SECTOR_SHIFT;
+	port->blk.type = port->ahci ? BLK_TYPE_AHCI : BLK_TYPE_IDE;
 
 	rc = blockdevice_register(&port->blk);
 	if (rc != 0) {
diff --git a/drivers/block/efi-block-io.c b/drivers/block/efi-block-io.c
index 79a62d53d30a..810b6da32d55 100644
--- a/drivers/block/efi-block-io.c
+++ b/drivers/block/efi-block-io.c
@@ -179,6 +179,7 @@ static int efi_bio_probe(struct efi_device *efidev)
 	priv->blk.num_blocks = media->last_block + 1;
 	priv->blk.ops = &efi_bio_ops;
 	priv->blk.dev = &efidev->dev;
+	priv->blk.type = BLK_TYPE_VIRTUAL;
 
 	priv->media_id = media->media_id;
 
diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index 625daf6879c0..cbef500d59b3 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -105,6 +105,7 @@ static int virtio_blk_probe(struct virtio_device *vdev)
 	virtio_cread(vdev, struct virtio_blk_config, capacity, &cap);
 	priv->blk.num_blocks = cap;
 	priv->blk.ops = &virtio_blk_ops;
+	priv->blk.type = BLK_TYPE_VIRTUAL;
 
 	return blockdevice_register(&priv->blk);
 }
diff --git a/drivers/mci/mci-core.c b/drivers/mci/mci-core.c
index 663d366666b0..bd9083b9f77c 100644
--- a/drivers/mci/mci-core.c
+++ b/drivers/mci/mci-core.c
@@ -1901,6 +1901,7 @@ static int mci_register_partition(struct mci_part *part)
 	 */
 	part->blk.dev = &mci->dev;
 	part->blk.ops = &mci_ops;
+	part->blk.type = IS_SD(mci) ? BLK_TYPE_SD : BLK_TYPE_MMC;
 
 	rc = blockdevice_register(&part->blk);
 	if (rc != 0) {
diff --git a/drivers/nvme/host/core.c b/drivers/nvme/host/core.c
index 79a5f9325ef8..33a592caeb14 100644
--- a/drivers/nvme/host/core.c
+++ b/drivers/nvme/host/core.c
@@ -361,6 +361,7 @@ static void nvme_alloc_ns(struct nvme_ctrl *ctrl, unsigned nsid)
 
 	ns->blk.dev = ctrl->dev;
 	ns->blk.ops = &nvme_block_device_ops;
+	ns->blk.type = BLK_TYPE_NVME;
 	ns->blk.cdev.name = strdup(disk_name);
 
 	__nvme_revalidate_disk(&ns->blk, id);
diff --git a/drivers/usb/storage/usb.c b/drivers/usb/storage/usb.c
index f281e0186d68..cc241e69be1b 100644
--- a/drivers/usb/storage/usb.c
+++ b/drivers/usb/storage/usb.c
@@ -421,6 +421,7 @@ static int usb_stor_add_blkdev(struct us_data *us, unsigned char lun)
 
 	pblk_dev->blk.cdev.name = basprintf("disk%d", result);
 	pblk_dev->blk.blockbits = SECTOR_SHIFT;
+	pblk_dev->blk.type = BLK_TYPE_USB;
 
 	result = blockdevice_register(&pblk_dev->blk);
 	if (result != 0) {
diff --git a/include/ata_drive.h b/include/ata_drive.h
index 47b74826866c..e840d89f4ae8 100644
--- a/include/ata_drive.h
+++ b/include/ata_drive.h
@@ -134,8 +134,9 @@ struct ata_port {
 	void *drvdata;
 	struct block_device blk;
 	uint16_t *id;
-	int lba48;
-	int initialized;
+	bool lba48;
+	bool initialized;
+	bool ahci;
 	int probe;
 };
 
diff --git a/include/block.h b/include/block.h
index e27579cdbd49..a0f226c7649f 100644
--- a/include/block.h
+++ b/include/block.h
@@ -17,11 +17,25 @@ struct block_device_ops {
 
 struct chunk;
 
+enum blk_type {
+	BLK_TYPE_UNSPEC = 0,
+	BLK_TYPE_USB,
+	BLK_TYPE_SD,
+	BLK_TYPE_AHCI,
+	BLK_TYPE_IDE,
+	BLK_TYPE_NVME,
+	BLK_TYPE_VIRTUAL,
+	BLK_TYPE_MMC,
+};
+
+const char *blk_type_str(enum blk_type);
+
 struct block_device {
 	struct device *dev;
 	struct list_head list;
 	struct block_device_ops *ops;
-	int blockbits;
+	u8 blockbits;
+	u8 type; /* holds enum blk_type */
 	blkcnt_t num_blocks;
 	int rdbufsize;
 	int blkmask;
-- 
2.39.2




More information about the barebox mailing list