diff mbox series

[1/2] dm: blk: assign media ID to block devices

Message ID 20220915200242.18358-2-heinrich.schuchardt@canonical.com
State Changes Requested, archived
Delegated to: Heinrich Schuchardt
Headers show
Series efi_loader: provide media ID | expand

Commit Message

Heinrich Schuchardt Sept. 15, 2022, 8:02 p.m. UTC
Currently block devices are only identified by uclass_id and device number.
When dealing with removable media this is not enough to uniquely identify
the medium.

E.g. after host unbind, host bind we can have the same device number but a
different backing file.

The EFI specification uses a 32bit number media ID to identify media. Add a
matching field to the block device descriptor.

Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
 drivers/block/blk-uclass.c | 16 +++++++++++++++-
 include/blk.h              | 11 +++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

Comments

Simon Glass Sept. 16, 2022, 1:30 a.m. UTC | #1
Hi Heinrich,

On Thu, 15 Sept 2022 at 14:02, Heinrich Schuchardt
<heinrich.schuchardt@canonical.com> wrote:
>
> Currently block devices are only identified by uclass_id and device number.
> When dealing with removable media this is not enough to uniquely identify
> the medium.
>
> E.g. after host unbind, host bind we can have the same device number but a
> different backing file.
>
> The EFI specification uses a 32bit number media ID to identify media. Add a
> matching field to the block device descriptor.
>
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---
>  drivers/block/blk-uclass.c | 16 +++++++++++++++-
>  include/blk.h              | 11 +++++++++++
>  2 files changed, 26 insertions(+), 1 deletion(-)

Shouldn't this be handled by connecting the  EFI data to its udevice.
I think Takahiro has been looking at this?

NAK to any EFI fields in blk_desc, in any case

Regards,
Simon
Heinrich Schuchardt Sept. 16, 2022, 6:41 a.m. UTC | #2
On 9/16/22 03:30, Simon Glass wrote:
> Hi Heinrich,
> 
> On Thu, 15 Sept 2022 at 14:02, Heinrich Schuchardt
> <heinrich.schuchardt@canonical.com> wrote:
>>
>> Currently block devices are only identified by uclass_id and device number.
>> When dealing with removable media this is not enough to uniquely identify
>> the medium.
>>
>> E.g. after host unbind, host bind we can have the same device number but a
>> different backing file.
>>
>> The EFI specification uses a 32bit number media ID to identify media. Add a
>> matching field to the block device descriptor.
>>
>> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
>> ---
>>   drivers/block/blk-uclass.c | 16 +++++++++++++++-
>>   include/blk.h              | 11 +++++++++++
>>   2 files changed, 26 insertions(+), 1 deletion(-)
> 
> Shouldn't this be handled by connecting the  EFI data to its udevice.
> I think Takahiro has been looking at this?
> 
> NAK to any EFI fields in blk_desc, in any case

The information that a medium has changed can only come from the DM 
layer. If for instance the SD-card is swapped, this is indicated by the 
card detector switch. USB also has a signal for media changes.

What are your ideas how the DM layer shall indicate media changes?

Best regards

Heinrich
Simon Glass Sept. 16, 2022, 8:29 p.m. UTC | #3
Hi Heinrich,

On Fri, 16 Sept 2022 at 00:41, Heinrich Schuchardt
<heinrich.schuchardt@canonical.com> wrote:
>
>
>
> On 9/16/22 03:30, Simon Glass wrote:
> > Hi Heinrich,
> >
> > On Thu, 15 Sept 2022 at 14:02, Heinrich Schuchardt
> > <heinrich.schuchardt@canonical.com> wrote:
> >>
> >> Currently block devices are only identified by uclass_id and device number.
> >> When dealing with removable media this is not enough to uniquely identify
> >> the medium.
> >>
> >> E.g. after host unbind, host bind we can have the same device number but a
> >> different backing file.
> >>
> >> The EFI specification uses a 32bit number media ID to identify media. Add a
> >> matching field to the block device descriptor.
> >>
> >> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> >> ---
> >>   drivers/block/blk-uclass.c | 16 +++++++++++++++-
> >>   include/blk.h              | 11 +++++++++++
> >>   2 files changed, 26 insertions(+), 1 deletion(-)
> >
> > Shouldn't this be handled by connecting the  EFI data to its udevice.
> > I think Takahiro has been looking at this?
> >
> > NAK to any EFI fields in blk_desc, in any case
>
> The information that a medium has changed can only come from the DM
> layer. If for instance the SD-card is swapped, this is indicated by the
> card detector switch. USB also has a signal for media changes.
>
> What are your ideas how the DM layer shall indicate media changes?

It should send an event, e.g. EVT_MEDIA and subscribers can then do
what is needed.

Regards,
Simon
diff mbox series

Patch

diff --git a/drivers/block/blk-uclass.c b/drivers/block/blk-uclass.c
index 21c5209bb6..c6f6be81c3 100644
--- a/drivers/block/blk-uclass.c
+++ b/drivers/block/blk-uclass.c
@@ -51,6 +51,11 @@  static enum uclass_id if_type_uclass_id[IF_TYPE_COUNT] = {
 	[IF_TYPE_PVBLOCK]	= UCLASS_PVBLOCK,
 };
 
+/*
+ * Global counter for media IDs
+ */
+static u32 media_id_counter;
+
 static enum if_type if_typename_to_iftype(const char *if_typename)
 {
 	int i;
@@ -741,11 +746,20 @@  int blk_unbind_all(int if_type)
 	return 0;
 }
 
+void blk_assign_media_id(struct blk_desc *desc)
+{
+	desc->media_id = ++media_id_counter;
+}
+
 static int blk_post_probe(struct udevice *dev)
 {
+	struct blk_desc *desc = dev_get_uclass_plat(dev);
+
+	if (!desc->media_id)
+		blk_assign_media_id(desc);
+
 	if (CONFIG_IS_ENABLED(PARTITIONS) &&
 	    IS_ENABLED(CONFIG_HAVE_BLOCK_DEVICE)) {
-		struct blk_desc *desc = dev_get_uclass_plat(dev);
 
 		part_init(desc);
 
diff --git a/include/blk.h b/include/blk.h
index 9503369db8..c176f8374e 100644
--- a/include/blk.h
+++ b/include/blk.h
@@ -77,6 +77,7 @@  struct blk_desc {
 	unsigned char	hwpart;		/* HW partition, e.g. for eMMC */
 	unsigned char	type;		/* device type */
 	unsigned char	removable;	/* removable device */
+	u32		media_id;	/* ID to identify media */
 #ifdef CONFIG_LBA48
 	/* device can use 48bit addr (ATA/ATAPI v7) */
 	unsigned char	lba48;
@@ -808,4 +809,14 @@  int blk_find_next(enum blk_flag_t flags, struct udevice **devp);
  */
 int blk_count_devices(enum blk_flag_t flag);
 
+/**
+ * blk_assign_media_id() - assign a media ID to the block device
+ *
+ * The field media_id of the block device descriptor is filled from a global
+ * counter.
+ *
+ * @desc:	block device descriptor
+ */
+void blk_assign_media_id(struct blk_desc *desc);
+
 #endif