All of lore.kernel.org
 help / color / mirror / Atom feed
* [U-Boot] [RFC PATCH] dm: ensure device names are unique
@ 2016-04-26 21:30 Stephen Warren
  2016-04-26 21:36 ` Stephen Warren
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Stephen Warren @ 2016-04-26 21:30 UTC (permalink / raw)
  To: u-boot

From: Stephen Warren <swarren@nvidia.com>

It is possible for HW to contain multiple instances of the same device. In
this case, the name passed to device_bind() may not be unique across all
devices within its uclass. One example is a system with multiple identical
PCI Ethernet devices. Another might be a system with multiple identical
I2C GPIO expanders, each connected to a separate I2C bus, yet using the
same I2C address on that bus and hence having the same DT node name.

Enhance the code to detect this situation, and append a sequence number so
the device name to ensure uniqueness.

Signed-off-by: Stephen Warren <swarren@nvidia.com>
---
Some possible issues with this patch:

1) Doing this in bind() rather than probe() means dev->seq isn't
available, so can't be used to generate the unique name. This process
should be done during bind() rather than probe() though, since devices can
be seen (e.g. by running "dm tree") before they're probed. Perhaps the
uclass_resolve_seq() should be called by bind() not probe().

2) uclass_find_device_by_name() needs to look up the uclass pointer again
even though device_bind() already knows it.

3) Iterating over the list to find the count of devices in the uclass is a
bit annoying. Should the uclass maintain this count so it doesn't need to
be re-calculated each time?
---
 drivers/core/device-remove.c |  4 ++++
 drivers/core/device.c        | 23 ++++++++++++++++++++++-
 include/dm/device.h          |  3 +++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
index e1714b2202b6..a2400989024a 100644
--- a/drivers/core/device-remove.c
+++ b/drivers/core/device-remove.c
@@ -112,6 +112,10 @@ int device_unbind(struct udevice *dev)
 
 	devres_release_all(dev);
 
+	if (dev->flags & DM_FLAG_ALLOC_NAME) {
+		free((char *)dev->name);
+		dev->name = NULL;
+	}
 	free(dev);
 
 	return 0;
diff --git a/drivers/core/device.c b/drivers/core/device.c
index 269087a084cf..fcac0073c642 100644
--- a/drivers/core/device.c
+++ b/drivers/core/device.c
@@ -30,7 +30,7 @@ int device_bind(struct udevice *parent, const struct driver *drv,
 		const char *name, void *platdata, int of_offset,
 		struct udevice **devp)
 {
-	struct udevice *dev;
+	struct udevice *dev, *dev2;
 	struct uclass *uc;
 	int size, ret = 0;
 
@@ -62,6 +62,23 @@ int device_bind(struct udevice *parent, const struct driver *drv,
 	dev->driver = drv;
 	dev->uclass = uc;
 
+	if (!uclass_find_device_by_name(uc->uc_drv->id, dev->name, &dev2)) {
+		char *unique_name;
+		int count;
+		size = strlen(name) + 4 + 1;
+		unique_name = malloc(size);
+		if (!unique_name) {
+			ret = -ENOMEM;
+			goto fail_alloc1;
+		}
+		dev->flags |= DM_FLAG_ALLOC_NAME;
+		dev->name = unique_name;
+		count = 0;
+		list_for_each_entry(dev2, &uc->dev_head, uclass_node)
+			count++;
+		snprintf(unique_name, size, "%s.%d", name, count);
+	}
+
 	dev->seq = -1;
 	dev->req_seq = -1;
 	if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
@@ -186,6 +203,10 @@ fail_alloc2:
 		dev->platdata = NULL;
 	}
 fail_alloc1:
+	if (dev->flags & DM_FLAG_ALLOC_NAME) {
+		free((char *)dev->name);
+		dev->name = NULL;
+	}
 	devres_release_all(dev);
 
 	free(dev);
diff --git a/include/dm/device.h b/include/dm/device.h
index dad7591dfacb..976ba72a550c 100644
--- a/include/dm/device.h
+++ b/include/dm/device.h
@@ -41,6 +41,9 @@ struct driver_info;
 /* Device is bound */
 #define DM_FLAG_BOUND			(1 << 6)
 
+/* DM is responsible for allocating and freeing device name */
+#define DM_FLAG_ALLOC_NAME		(1 << 7)
+
 /**
  * struct udevice - An instance of a driver
  *
-- 
2.8.1

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-26 21:30 [U-Boot] [RFC PATCH] dm: ensure device names are unique Stephen Warren
@ 2016-04-26 21:36 ` Stephen Warren
  2016-04-27 18:40   ` Stephen Warren
  2016-04-28  4:42 ` Joe Hershberger
  2016-04-28  4:50 ` Simon Glass
  2 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-26 21:36 UTC (permalink / raw)
  To: u-boot

On 04/26/2016 03:30 PM, Stephen Warren wrote:
> From: Stephen Warren <swarren@nvidia.com>
>
> It is possible for HW to contain multiple instances of the same device. In
> this case, the name passed to device_bind() may not be unique across all
> devices within its uclass. One example is a system with multiple identical
> PCI Ethernet devices. Another might be a system with multiple identical
> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
> same I2C address on that bus and hence having the same DT node name.
>
> Enhance the code to detect this situation, and append a sequence number so
> the device name to ensure uniqueness.
>
> Signed-off-by: Stephen Warren <swarren@nvidia.com>
> ---
> Some possible issues with this patch:
>
> 1) Doing this in bind() rather than probe() means dev->seq isn't
> available, so can't be used to generate the unique name. This process
> should be done during bind() rather than probe() though, since devices can
> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
> uclass_resolve_seq() should be called by bind() not probe().
>
> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
> even though device_bind() already knows it.
>
> 3) Iterating over the list to find the count of devices in the uclass is a
> bit annoying. Should the uclass maintain this count so it doesn't need to
> be re-calculated each time?

4) This causes "ut dm autobind" to fail. I'll investigate that locally, 
but won't bother reposting until the questions above are considered.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-26 21:36 ` Stephen Warren
@ 2016-04-27 18:40   ` Stephen Warren
  2016-04-28  4:44     ` Joe Hershberger
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-27 18:40 UTC (permalink / raw)
  To: u-boot

On 04/26/2016 03:36 PM, Stephen Warren wrote:
> On 04/26/2016 03:30 PM, Stephen Warren wrote:
>> From: Stephen Warren <swarren@nvidia.com>
>>
>> It is possible for HW to contain multiple instances of the same
>> device. In
>> this case, the name passed to device_bind() may not be unique across all
>> devices within its uclass. One example is a system with multiple
>> identical
>> PCI Ethernet devices. Another might be a system with multiple identical
>> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>> same I2C address on that bus and hence having the same DT node name.
>>
>> Enhance the code to detect this situation, and append a sequence
>> number so
>> the device name to ensure uniqueness.
>>
>> Signed-off-by: Stephen Warren <swarren@nvidia.com>
>> ---
>> Some possible issues with this patch:
>>
>> 1) Doing this in bind() rather than probe() means dev->seq isn't
>> available, so can't be used to generate the unique name. This process
>> should be done during bind() rather than probe() though, since devices
>> can
>> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
>> uclass_resolve_seq() should be called by bind() not probe().
>>
>> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
>> even though device_bind() already knows it.
>>
>> 3) Iterating over the list to find the count of devices in the uclass
>> is a
>> bit annoying. Should the uclass maintain this count so it doesn't need to
>> be re-calculated each time?
>
> 4) This causes "ut dm autobind" to fail. I'll investigate that locally,
> but won't bother reposting until the questions above are considered.

It looks like that's because I need to add "gd->dm_root && " to the 
start of the following condition:

> @@ -62,6 +62,23 @@ int device_bind(struct udevice *parent, const struct driver *drv,
>  	dev->driver = drv;
>  	dev->uclass = uc;
>
> +	if (!uclass_find_device_by_name(uc->uc_drv->id, dev->name, &dev2)) {

... otherwise, the uc_class_get/find() nested inside that end up 
creating a second root uclass.

I can fold that into v2, pending any other comments.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-26 21:30 [U-Boot] [RFC PATCH] dm: ensure device names are unique Stephen Warren
  2016-04-26 21:36 ` Stephen Warren
@ 2016-04-28  4:42 ` Joe Hershberger
  2016-04-28 16:00   ` Stephen Warren
  2016-04-28  4:50 ` Simon Glass
  2 siblings, 1 reply; 14+ messages in thread
From: Joe Hershberger @ 2016-04-28  4:42 UTC (permalink / raw)
  To: u-boot

On Tue, Apr 26, 2016 at 4:30 PM, Stephen Warren <swarren@wwwdotorg.org> wrote:
> From: Stephen Warren <swarren@nvidia.com>
>
> It is possible for HW to contain multiple instances of the same device. In
> this case, the name passed to device_bind() may not be unique across all
> devices within its uclass. One example is a system with multiple identical
> PCI Ethernet devices. Another might be a system with multiple identical
> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
> same I2C address on that bus and hence having the same DT node name.
>
> Enhance the code to detect this situation, and append a sequence number so
> the device name to ensure uniqueness.
>
> Signed-off-by: Stephen Warren <swarren@nvidia.com>
> ---
> Some possible issues with this patch:
>
> 1) Doing this in bind() rather than probe() means dev->seq isn't
> available, so can't be used to generate the unique name. This process
> should be done during bind() rather than probe() though, since devices can
> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
> uclass_resolve_seq() should be called by bind() not probe().

We (Simon and I) had discussion about this when I first added support
for eth devices. He convinced me the correct time for seq to be
evaluated is at probe time. I can dig through the mail history for
reasons if you're interested.

> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
> even though device_bind() already knows it.

Maybe we could add a parameter to provide the pointer and if it's
NULL, then it gets looked up. Might be a bit noisy change, though. Is
that optimization very valuable?

> 3) Iterating over the list to find the count of devices in the uclass is a
> bit annoying. Should the uclass maintain this count so it doesn't need to
> be re-calculated each time?

These lists aren't long, right? It seems like the optimization to
store the value is only helpful if the lists could be expected to be
long, or the size is looked up many times.

>  drivers/core/device-remove.c |  4 ++++
>  drivers/core/device.c        | 23 ++++++++++++++++++++++-
>  include/dm/device.h          |  3 +++
>  3 files changed, 29 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/core/device-remove.c b/drivers/core/device-remove.c
> index e1714b2202b6..a2400989024a 100644
> --- a/drivers/core/device-remove.c
> +++ b/drivers/core/device-remove.c
> @@ -112,6 +112,10 @@ int device_unbind(struct udevice *dev)
>
>         devres_release_all(dev);
>
> +       if (dev->flags & DM_FLAG_ALLOC_NAME) {
> +               free((char *)dev->name);
> +               dev->name = NULL;
> +       }
>         free(dev);
>
>         return 0;
> diff --git a/drivers/core/device.c b/drivers/core/device.c
> index 269087a084cf..fcac0073c642 100644
> --- a/drivers/core/device.c
> +++ b/drivers/core/device.c
> @@ -30,7 +30,7 @@ int device_bind(struct udevice *parent, const struct driver *drv,
>                 const char *name, void *platdata, int of_offset,
>                 struct udevice **devp)
>  {
> -       struct udevice *dev;
> +       struct udevice *dev, *dev2;
>         struct uclass *uc;
>         int size, ret = 0;
>
> @@ -62,6 +62,23 @@ int device_bind(struct udevice *parent, const struct driver *drv,
>         dev->driver = drv;
>         dev->uclass = uc;
>
> +       if (!uclass_find_device_by_name(uc->uc_drv->id, dev->name, &dev2)) {
> +               char *unique_name;
> +               int count;
> +               size = strlen(name) + 4 + 1;
> +               unique_name = malloc(size);
> +               if (!unique_name) {
> +                       ret = -ENOMEM;
> +                       goto fail_alloc1;
> +               }
> +               dev->flags |= DM_FLAG_ALLOC_NAME;
> +               dev->name = unique_name;
> +               count = 0;
> +               list_for_each_entry(dev2, &uc->dev_head, uclass_node)
> +                       count++;
> +               snprintf(unique_name, size, "%s.%d", name, count);
> +       }
> +
>         dev->seq = -1;
>         dev->req_seq = -1;
>         if (CONFIG_IS_ENABLED(OF_CONTROL) && CONFIG_IS_ENABLED(DM_SEQ_ALIAS)) {
> @@ -186,6 +203,10 @@ fail_alloc2:
>                 dev->platdata = NULL;
>         }
>  fail_alloc1:
> +       if (dev->flags & DM_FLAG_ALLOC_NAME) {
> +               free((char *)dev->name);
> +               dev->name = NULL;
> +       }
>         devres_release_all(dev);
>
>         free(dev);
> diff --git a/include/dm/device.h b/include/dm/device.h
> index dad7591dfacb..976ba72a550c 100644
> --- a/include/dm/device.h
> +++ b/include/dm/device.h
> @@ -41,6 +41,9 @@ struct driver_info;
>  /* Device is bound */
>  #define DM_FLAG_BOUND                  (1 << 6)
>
> +/* DM is responsible for allocating and freeing device name */
> +#define DM_FLAG_ALLOC_NAME             (1 << 7)
> +
>  /**
>   * struct udevice - An instance of a driver
>   *
> --
> 2.8.1
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-27 18:40   ` Stephen Warren
@ 2016-04-28  4:44     ` Joe Hershberger
  0 siblings, 0 replies; 14+ messages in thread
From: Joe Hershberger @ 2016-04-28  4:44 UTC (permalink / raw)
  To: u-boot

On Wed, Apr 27, 2016 at 1:40 PM, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 04/26/2016 03:36 PM, Stephen Warren wrote:
>>
>> On 04/26/2016 03:30 PM, Stephen Warren wrote:
>>>
>>> From: Stephen Warren <swarren@nvidia.com>
>>>
>>> It is possible for HW to contain multiple instances of the same
>>> device. In
>>> this case, the name passed to device_bind() may not be unique across all
>>> devices within its uclass. One example is a system with multiple
>>> identical
>>> PCI Ethernet devices. Another might be a system with multiple identical
>>> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>>> same I2C address on that bus and hence having the same DT node name.
>>>
>>> Enhance the code to detect this situation, and append a sequence
>>> number so
>>> the device name to ensure uniqueness.
>>>
>>> Signed-off-by: Stephen Warren <swarren@nvidia.com>
>>> ---
>>> Some possible issues with this patch:
>>>
>>> 1) Doing this in bind() rather than probe() means dev->seq isn't
>>> available, so can't be used to generate the unique name. This process
>>> should be done during bind() rather than probe() though, since devices
>>> can
>>> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
>>> uclass_resolve_seq() should be called by bind() not probe().
>>>
>>> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
>>> even though device_bind() already knows it.
>>>
>>> 3) Iterating over the list to find the count of devices in the uclass
>>> is a
>>> bit annoying. Should the uclass maintain this count so it doesn't need to
>>> be re-calculated each time?
>>
>>
>> 4) This causes "ut dm autobind" to fail. I'll investigate that locally,
>> but won't bother reposting until the questions above are considered.
>
>
> It looks like that's because I need to add "gd->dm_root && " to the start of
> the following condition:
>
>> @@ -62,6 +62,23 @@ int device_bind(struct udevice *parent, const struct
>> driver *drv,
>>         dev->driver = drv;
>>         dev->uclass = uc;
>>
>> +       if (!uclass_find_device_by_name(uc->uc_drv->id, dev->name, &dev2))
>> {
>
>
> ... otherwise, the uc_class_get/find() nested inside that end up creating a
> second root uclass.

Makes sense.

> I can fold that into v2, pending any other comments.
>
> _______________________________________________
> U-Boot mailing list
> U-Boot at lists.denx.de
> http://lists.denx.de/mailman/listinfo/u-boot

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-26 21:30 [U-Boot] [RFC PATCH] dm: ensure device names are unique Stephen Warren
  2016-04-26 21:36 ` Stephen Warren
  2016-04-28  4:42 ` Joe Hershberger
@ 2016-04-28  4:50 ` Simon Glass
  2016-04-28 15:55   ` Stephen Warren
  2 siblings, 1 reply; 14+ messages in thread
From: Simon Glass @ 2016-04-28  4:50 UTC (permalink / raw)
  To: u-boot

Hi Stephen,

On 26 April 2016 at 15:30, Stephen Warren <swarren@wwwdotorg.org> wrote:
> It is possible for HW to contain multiple instances of the same device. In
> this case, the name passed to device_bind() may not be unique across all
> devices within its uclass. One example is a system with multiple identical
> PCI Ethernet devices. Another might be a system with multiple identical
> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
> same I2C address on that bus and hence having the same DT node name.
>
> Enhance the code to detect this situation, and append a sequence number so
> the device name to ensure uniqueness.
>
> Signed-off-by: Stephen Warren <swarren@nvidia.com>
> ---
> Some possible issues with this patch:
>
> 1) Doing this in bind() rather than probe() means dev->seq isn't
> available, so can't be used to generate the unique name. This process
> should be done during bind() rather than probe() though, since devices can
> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
> uclass_resolve_seq() should be called by bind() not probe().
>
> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
> even though device_bind() already knows it.
>
> 3) Iterating over the list to find the count of devices in the uclass is a
> bit annoying. Should the uclass maintain this count so it doesn't need to
> be re-calculated each time?
> ---
> drivers/core/device-remove.c | 4 ++++
> drivers/core/device.c | 23 ++++++++++++++++++++++-
> include/dm/device.h | 3 +++
> 3 files changed, 29 insertions(+), 1 deletion(-)

I would rather that the caller handles this. But failing this perhaps a new
function that does it? Is this for the Ethernet use case?

Regards,
Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-28  4:50 ` Simon Glass
@ 2016-04-28 15:55   ` Stephen Warren
  2016-04-29 13:23     ` Simon Glass
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-28 15:55 UTC (permalink / raw)
  To: u-boot

On 04/27/2016 10:50 PM, Simon Glass wrote:
> Hi Stephen,
>
> On 26 April 2016 at 15:30, Stephen Warren wrote:
>  > It is possible for HW to contain multiple instances of the same device. In
>  > this case, the name passed to device_bind() may not be unique across all
>  > devices within its uclass. One example is a system with multiple  identical
>  > PCI Ethernet devices. Another might be a system with multiple identical
>  > I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>  > same I2C address on that bus and hence having the same DT node name.
>  >
>  > Enhance the code to detect this situation, and append a sequence  number so
>  > the device name to ensure uniqueness.
>  >
>  > Signed-off-by: Stephen Warren <swarren at nvidia.com <swarren@nvidia.com>>
>
> I would rather that the caller handles this. But failing this perhaps a
> new function that does it? Is this for the Ethernet use case?

Wouldn't all callers of this function simply call the new function? I'm 
not aware of any case where the code to avoid duplicate names would not 
be desired.

I hit this for the Ethernet case, but I believe it applies to any type 
of device at all; see another possible trigger case in the commit 
description.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-28  4:42 ` Joe Hershberger
@ 2016-04-28 16:00   ` Stephen Warren
  2016-04-29 13:24     ` Simon Glass
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-28 16:00 UTC (permalink / raw)
  To: u-boot

On 04/27/2016 10:42 PM, Joe Hershberger wrote:
> On Tue, Apr 26, 2016 at 4:30 PM, Stephen Warren <swarren@wwwdotorg.org> wrote:
>> From: Stephen Warren <swarren@nvidia.com>
>>
>> It is possible for HW to contain multiple instances of the same device. In
>> this case, the name passed to device_bind() may not be unique across all
>> devices within its uclass. One example is a system with multiple identical
>> PCI Ethernet devices. Another might be a system with multiple identical
>> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>> same I2C address on that bus and hence having the same DT node name.
>>
>> Enhance the code to detect this situation, and append a sequence number so
>> the device name to ensure uniqueness.
>>
>> Signed-off-by: Stephen Warren <swarren@nvidia.com>
>> ---
>> Some possible issues with this patch:
>>
>> 1) Doing this in bind() rather than probe() means dev->seq isn't
>> available, so can't be used to generate the unique name. This process
>> should be done during bind() rather than probe() though, since devices can
>> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
>> uclass_resolve_seq() should be called by bind() not probe().
>
> We (Simon and I) had discussion about this when I first added support
> for eth devices. He convinced me the correct time for seq to be
> evaluated is at probe time. I can dig through the mail history for
> reasons if you're interested.

That might be interesting. Right now, about the only thing I see moving 
the evaluation from probe to bind would do is very marginally increase 
the amount of work done in bind, which potentially might be skipped if a 
particular Ethernet device was never probed. I would not expect this to 
make a noticeable difference, especially since IIRC when the network is 
first used, all the Ethernet devices are probed anyway, so this just 
moves work around?

>> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
>> even though device_bind() already knows it.
>
> Maybe we could add a parameter to provide the pointer and if it's
> NULL, then it gets looked up. Might be a bit noisy change, though. Is
> that optimization very valuable?

I don't imagine there are too many uclasses, so it's probably not going 
to save too much time. Still, Simon has in the past objected to code 
that parses DT scanning the DT to find parent offsets, and this feels 
like exactly the same kind of thing. I'm not personally concerned about 
it; simply pointing it out in case anyone wanted that to be addressed.

>> 3) Iterating over the list to find the count of devices in the uclass is a
>> bit annoying. Should the uclass maintain this count so it doesn't need to
>> be re-calculated each time?
>
> These lists aren't long, right? It seems like the optimization to
> store the value is only helpful if the lists could be expected to be
> long, or the size is looked up many times.

Yes, I don't imagine this would be a particular issue in practice.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-28 15:55   ` Stephen Warren
@ 2016-04-29 13:23     ` Simon Glass
  2016-04-29 16:23       ` Stephen Warren
  0 siblings, 1 reply; 14+ messages in thread
From: Simon Glass @ 2016-04-29 13:23 UTC (permalink / raw)
  To: u-boot

Hi Stephen,

On 28 April 2016 at 09:55, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 04/27/2016 10:50 PM, Simon Glass wrote:
>>
>> Hi Stephen,
>>
>> On 26 April 2016 at 15:30, Stephen Warren wrote:
>>  > It is possible for HW to contain multiple instances of the same device.
>> In
>>  > this case, the name passed to device_bind() may not be unique across
>> all
>>  > devices within its uclass. One example is a system with multiple
>> identical
>>  > PCI Ethernet devices. Another might be a system with multiple identical
>>  > I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>>  > same I2C address on that bus and hence having the same DT node name.
>>  >
>>  > Enhance the code to detect this situation, and append a sequence
>> number so
>>  > the device name to ensure uniqueness.
>>  >
>>  > Signed-off-by: Stephen Warren <swarren at nvidia.com <swarren@nvidia.com>>
>>
>> I would rather that the caller handles this. But failing this perhaps a
>> new function that does it? Is this for the Ethernet use case?
>
>
> Wouldn't all callers of this function simply call the new function? I'm not
> aware of any case where the code to avoid duplicate names would not be
> desired.
>
> I hit this for the Ethernet case, but I believe it applies to any type of
> device at all; see another possible trigger case in the commit description.

This does not happen with devices from the device tree. It only
happens with auto-probed devices. Your I2C GPIO example is odd but I'd
rather solve that by using the device tree node name.

I'd like to avoid putting things in the base code that we don't need
early on in boot. How about a helper function for those subsystems
that create devices outside the device tree system?

Regards,
Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-28 16:00   ` Stephen Warren
@ 2016-04-29 13:24     ` Simon Glass
  0 siblings, 0 replies; 14+ messages in thread
From: Simon Glass @ 2016-04-29 13:24 UTC (permalink / raw)
  To: u-boot

Hi Stephen,

On 28 April 2016 at 10:00, Stephen Warren <swarren@wwwdotorg.org> wrote:
>
> On 04/27/2016 10:42 PM, Joe Hershberger wrote:
>>
>> On Tue, Apr 26, 2016 at 4:30 PM, Stephen Warren <swarren@wwwdotorg.org> wrote:
>>>
>>> From: Stephen Warren <swarren@nvidia.com>
>>>
>>> It is possible for HW to contain multiple instances of the same device. In
>>> this case, the name passed to device_bind() may not be unique across all
>>> devices within its uclass. One example is a system with multiple identical
>>> PCI Ethernet devices. Another might be a system with multiple identical
>>> I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>>> same I2C address on that bus and hence having the same DT node name.
>>>
>>> Enhance the code to detect this situation, and append a sequence number so
>>> the device name to ensure uniqueness.
>>>
>>> Signed-off-by: Stephen Warren <swarren@nvidia.com>
>>> ---
>>> Some possible issues with this patch:
>>>
>>> 1) Doing this in bind() rather than probe() means dev->seq isn't
>>> available, so can't be used to generate the unique name. This process
>>> should be done during bind() rather than probe() though, since devices can
>>> be seen (e.g. by running "dm tree") before they're probed. Perhaps the
>>> uclass_resolve_seq() should be called by bind() not probe().
>>
>>
>> We (Simon and I) had discussion about this when I first added support
>> for eth devices. He convinced me the correct time for seq to be
>> evaluated is at probe time. I can dig through the mail history for
>> reasons if you're interested.
>
>
> That might be interesting. Right now, about the only thing I see moving the evaluation from probe to bind would do is very marginally increase the amount of work done in bind, which potentially might be skipped if a particular Ethernet device was never probed. I would not expect this to make a noticeable difference, especially since IIRC when the network is first used, all the Ethernet devices are probed anyway, so this just moves work around?

There's a somewhat deep design issue there, but so far I feel that
having the numbering resolved when a device is probed works best. It
is very seldom useful until then. Device names don't really need to
match there sequence number.

>
>>> 2) uclass_find_device_by_name() needs to look up the uclass pointer again
>>> even though device_bind() already knows it.
>>
>>
>> Maybe we could add a parameter to provide the pointer and if it's
>> NULL, then it gets looked up. Might be a bit noisy change, though. Is
>> that optimization very valuable?
>
>
> I don't imagine there are too many uclasses, so it's probably not going to save too much time. Still, Simon has in the past objected to code that parses DT scanning the DT to find parent offsets, and this feels like exactly the same kind of thing. I'm not personally concerned about it; simply pointing it out in case anyone wanted that to be addressed.
>
>>> 3) Iterating over the list to find the count of devices in the uclass is a
>>> bit annoying. Should the uclass maintain this count so it doesn't need to
>>> be re-calculated each time?
>>
>>
>> These lists aren't long, right? It seems like the optimization to
>> store the value is only helpful if the lists could be expected to be
>> long, or the size is looked up many times.

There are essentially no optimisations in driver model at present. So
far the benefit hasn't shown itself. Perhaps it won't in a boot
loader...

>
>
> Yes, I don't imagine this would be a particular issue in practice.

Regards,
Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-29 13:23     ` Simon Glass
@ 2016-04-29 16:23       ` Stephen Warren
  2016-04-29 16:28         ` Simon Glass
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-29 16:23 UTC (permalink / raw)
  To: u-boot

On 04/29/2016 07:23 AM, Simon Glass wrote:
> Hi Stephen,
>
> On 28 April 2016 at 09:55, Stephen Warren <swarren@wwwdotorg.org> wrote:
>> On 04/27/2016 10:50 PM, Simon Glass wrote:
>>>
>>> Hi Stephen,
>>>
>>> On 26 April 2016 at 15:30, Stephen Warren wrote:
>>>   > It is possible for HW to contain multiple instances of the same device.
>>> In
>>>   > this case, the name passed to device_bind() may not be unique across
>>> all
>>>   > devices within its uclass. One example is a system with multiple
>>> identical
>>>   > PCI Ethernet devices. Another might be a system with multiple identical
>>>   > I2C GPIO expanders, each connected to a separate I2C bus, yet using the
>>>   > same I2C address on that bus and hence having the same DT node name.
>>>   >
>>>   > Enhance the code to detect this situation, and append a sequence
>>> number so
>>>   > the device name to ensure uniqueness.
>>>   >
>>>   > Signed-off-by: Stephen Warren <swarren at nvidia.com <swarren@nvidia.com>>
>>>
>>> I would rather that the caller handles this. But failing this perhaps a
>>> new function that does it? Is this for the Ethernet use case?
>>
>>
>> Wouldn't all callers of this function simply call the new function? I'm not
>> aware of any case where the code to avoid duplicate names would not be
>> desired.
>>
>> I hit this for the Ethernet case, but I believe it applies to any type of
>> device at all; see another possible trigger case in the commit description.
>
> This does not happen with devices from the device tree. It only
> happens with auto-probed devices. Your I2C GPIO example is odd but I'd
> rather solve that by using the device tree node name.

DT itself imposes no such rule; node names must be unique only within 
their parent node but there's no restriction on identical node names 
appearing in different parts of the tree.

If U-Boot imposes that rule on DT, then there's no way in general that 
we can guarantee U-Boot will be able to use standard DTs (i.e. identical 
to those used by Linux or any other OS) for any platform; it'd be 
another change someone would need to make to transform a DT to be 
"U-Boot compatible", which rather reduces a potential benefit of DT for 
U-Boot; being able to just drop a DT in and have it work.

It would be possible for U-Boot to decouple its internal device name 
from the DT node name. In which case, your statement would work. 
However, I don't think that's the case at the moment, and in fact it's 
effectively what this patch is doing, although admittedly there are 
other ways of doing this.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-29 16:23       ` Stephen Warren
@ 2016-04-29 16:28         ` Simon Glass
  2016-04-29 16:30           ` Stephen Warren
  0 siblings, 1 reply; 14+ messages in thread
From: Simon Glass @ 2016-04-29 16:28 UTC (permalink / raw)
  To: u-boot

Hi Stephen,

On 29 April 2016 at 10:23, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 04/29/2016 07:23 AM, Simon Glass wrote:
>>
>> Hi Stephen,
>>
>> On 28 April 2016 at 09:55, Stephen Warren <swarren@wwwdotorg.org> wrote:
>>>
>>> On 04/27/2016 10:50 PM, Simon Glass wrote:
>>>>
>>>>
>>>> Hi Stephen,
>>>>
>>>> On 26 April 2016 at 15:30, Stephen Warren wrote:
>>>>   > It is possible for HW to contain multiple instances of the same
>>>> device.
>>>> In
>>>>   > this case, the name passed to device_bind() may not be unique across
>>>> all
>>>>   > devices within its uclass. One example is a system with multiple
>>>> identical
>>>>   > PCI Ethernet devices. Another might be a system with multiple
>>>> identical
>>>>   > I2C GPIO expanders, each connected to a separate I2C bus, yet using
>>>> the
>>>>   > same I2C address on that bus and hence having the same DT node name.
>>>>   >
>>>>   > Enhance the code to detect this situation, and append a sequence
>>>> number so
>>>>   > the device name to ensure uniqueness.
>>>>   >
>>>>   > Signed-off-by: Stephen Warren <swarren@nvidia.com
>>>> <swarren@nvidia.com>>
>>>>
>>>> I would rather that the caller handles this. But failing this perhaps a
>>>> new function that does it? Is this for the Ethernet use case?
>>>
>>>
>>>
>>> Wouldn't all callers of this function simply call the new function? I'm
>>> not
>>> aware of any case where the code to avoid duplicate names would not be
>>> desired.
>>>
>>> I hit this for the Ethernet case, but I believe it applies to any type of
>>> device at all; see another possible trigger case in the commit
>>> description.
>>
>>
>> This does not happen with devices from the device tree. It only
>> happens with auto-probed devices. Your I2C GPIO example is odd but I'd
>> rather solve that by using the device tree node name.
>
>
> DT itself imposes no such rule; node names must be unique only within their
> parent node but there's no restriction on identical node names appearing in
> different parts of the tree.
>
> If U-Boot imposes that rule on DT, then there's no way in general that we
> can guarantee U-Boot will be able to use standard DTs (i.e. identical to
> those used by Linux or any other OS) for any platform; it'd be another
> change someone would need to make to transform a DT to be "U-Boot
> compatible", which rather reduces a potential benefit of DT for U-Boot;
> being able to just drop a DT in and have it work.

U-Boot does not impose a rule. If you want duplicate device names you
can have them. I think it is bad practice though.

>
> It would be possible for U-Boot to decouple its internal device name from
> the DT node name. In which case, your statement would work. However, I don't
> think that's the case at the moment, and in fact it's effectively what this
> patch is doing, although admittedly there are other ways of doing this.

Anyway I believe my point stands. Whereas users can edit the device
tree and avoid conflicts they cannot do this with auto-probed devices.
So for that case we should have a way of allocating a name before
calling device_bind().

Regards,
Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-29 16:28         ` Simon Glass
@ 2016-04-29 16:30           ` Stephen Warren
  2016-04-29 17:22             ` Simon Glass
  0 siblings, 1 reply; 14+ messages in thread
From: Stephen Warren @ 2016-04-29 16:30 UTC (permalink / raw)
  To: u-boot

On 04/29/2016 10:28 AM, Simon Glass wrote:
> Hi Stephen,
>
> On 29 April 2016 at 10:23, Stephen Warren <swarren@wwwdotorg.org> wrote:
>> On 04/29/2016 07:23 AM, Simon Glass wrote:
>>>
>>> Hi Stephen,
>>>
>>> On 28 April 2016 at 09:55, Stephen Warren <swarren@wwwdotorg.org> wrote:
>>>>
>>>> On 04/27/2016 10:50 PM, Simon Glass wrote:
>>>>>
>>>>>
>>>>> Hi Stephen,
>>>>>
>>>>> On 26 April 2016 at 15:30, Stephen Warren wrote:
>>>>>    > It is possible for HW to contain multiple instances of the same
>>>>> device.
>>>>> In
>>>>>    > this case, the name passed to device_bind() may not be unique across
>>>>> all
>>>>>    > devices within its uclass. One example is a system with multiple
>>>>> identical
>>>>>    > PCI Ethernet devices. Another might be a system with multiple
>>>>> identical
>>>>>    > I2C GPIO expanders, each connected to a separate I2C bus, yet using
>>>>> the
>>>>>    > same I2C address on that bus and hence having the same DT node name.
>>>>>    >
>>>>>    > Enhance the code to detect this situation, and append a sequence
>>>>> number so
>>>>>    > the device name to ensure uniqueness.
>>>>>    >
>>>>>    > Signed-off-by: Stephen Warren <swarren@nvidia.com
>>>>> <swarren@nvidia.com>>
>>>>>
>>>>> I would rather that the caller handles this. But failing this perhaps a
>>>>> new function that does it? Is this for the Ethernet use case?
>>>>
>>>>
>>>>
>>>> Wouldn't all callers of this function simply call the new function? I'm
>>>> not
>>>> aware of any case where the code to avoid duplicate names would not be
>>>> desired.
>>>>
>>>> I hit this for the Ethernet case, but I believe it applies to any type of
>>>> device at all; see another possible trigger case in the commit
>>>> description.
>>>
>>>
>>> This does not happen with devices from the device tree. It only
>>> happens with auto-probed devices. Your I2C GPIO example is odd but I'd
>>> rather solve that by using the device tree node name.
>>
>>
>> DT itself imposes no such rule; node names must be unique only within their
>> parent node but there's no restriction on identical node names appearing in
>> different parts of the tree.
>>
>> If U-Boot imposes that rule on DT, then there's no way in general that we
>> can guarantee U-Boot will be able to use standard DTs (i.e. identical to
>> those used by Linux or any other OS) for any platform; it'd be another
>> change someone would need to make to transform a DT to be "U-Boot
>> compatible", which rather reduces a potential benefit of DT for U-Boot;
>> being able to just drop a DT in and have it work.
>
> U-Boot does not impose a rule. If you want duplicate device names you
> can have them. I think it is bad practice though.
>
>>
>> It would be possible for U-Boot to decouple its internal device name from
>> the DT node name. In which case, your statement would work. However, I don't
>> think that's the case at the moment, and in fact it's effectively what this
>> patch is doing, although admittedly there are other ways of doing this.
>
> Anyway I believe my point stands. Whereas users can edit the device
> tree and avoid conflicts they cannot do this with auto-probed devices.
> So for that case we should have a way of allocating a name before
> calling device_bind().

OK. I'm just going to solve this by unplugging the second Ethernet card, 
or holding this patch locally for the case when I need to test the PCIe 
port.

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [U-Boot] [RFC PATCH] dm: ensure device names are unique
  2016-04-29 16:30           ` Stephen Warren
@ 2016-04-29 17:22             ` Simon Glass
  0 siblings, 0 replies; 14+ messages in thread
From: Simon Glass @ 2016-04-29 17:22 UTC (permalink / raw)
  To: u-boot

Hi Stephen,

On 29 April 2016 at 10:30, Stephen Warren <swarren@wwwdotorg.org> wrote:
> On 04/29/2016 10:28 AM, Simon Glass wrote:
>>
>> Hi Stephen,
>>
>> On 29 April 2016 at 10:23, Stephen Warren <swarren@wwwdotorg.org> wrote:
>>>
>>> On 04/29/2016 07:23 AM, Simon Glass wrote:
>>>>
>>>>
>>>> Hi Stephen,
>>>>
>>>> On 28 April 2016 at 09:55, Stephen Warren <swarren@wwwdotorg.org> wrote:
>>>>>
>>>>>
>>>>> On 04/27/2016 10:50 PM, Simon Glass wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>> Hi Stephen,
>>>>>>
>>>>>> On 26 April 2016 at 15:30, Stephen Warren wrote:
>>>>>>    > It is possible for HW to contain multiple instances of the same
>>>>>> device.
>>>>>> In
>>>>>>    > this case, the name passed to device_bind() may not be unique
>>>>>> across
>>>>>> all
>>>>>>    > devices within its uclass. One example is a system with multiple
>>>>>> identical
>>>>>>    > PCI Ethernet devices. Another might be a system with multiple
>>>>>> identical
>>>>>>    > I2C GPIO expanders, each connected to a separate I2C bus, yet
>>>>>> using
>>>>>> the
>>>>>>    > same I2C address on that bus and hence having the same DT node
>>>>>> name.
>>>>>>    >
>>>>>>    > Enhance the code to detect this situation, and append a sequence
>>>>>> number so
>>>>>>    > the device name to ensure uniqueness.
>>>>>>    >
>>>>>>    > Signed-off-by: Stephen Warren <swarren@nvidia.com
>>>>>> <swarren@nvidia.com>>
>>>>>>
>>>>>> I would rather that the caller handles this. But failing this perhaps
>>>>>> a
>>>>>> new function that does it? Is this for the Ethernet use case?
>>>>>
>>>>>
>>>>>
>>>>>
>>>>> Wouldn't all callers of this function simply call the new function? I'm
>>>>> not
>>>>> aware of any case where the code to avoid duplicate names would not be
>>>>> desired.
>>>>>
>>>>> I hit this for the Ethernet case, but I believe it applies to any type
>>>>> of
>>>>> device at all; see another possible trigger case in the commit
>>>>> description.
>>>>
>>>>
>>>>
>>>> This does not happen with devices from the device tree. It only
>>>> happens with auto-probed devices. Your I2C GPIO example is odd but I'd
>>>> rather solve that by using the device tree node name.
>>>
>>>
>>>
>>> DT itself imposes no such rule; node names must be unique only within
>>> their
>>> parent node but there's no restriction on identical node names appearing
>>> in
>>> different parts of the tree.
>>>
>>> If U-Boot imposes that rule on DT, then there's no way in general that we
>>> can guarantee U-Boot will be able to use standard DTs (i.e. identical to
>>> those used by Linux or any other OS) for any platform; it'd be another
>>> change someone would need to make to transform a DT to be "U-Boot
>>> compatible", which rather reduces a potential benefit of DT for U-Boot;
>>> being able to just drop a DT in and have it work.
>>
>>
>> U-Boot does not impose a rule. If you want duplicate device names you
>> can have them. I think it is bad practice though.
>>
>>>
>>> It would be possible for U-Boot to decouple its internal device name from
>>> the DT node name. In which case, your statement would work. However, I
>>> don't
>>> think that's the case at the moment, and in fact it's effectively what
>>> this
>>> patch is doing, although admittedly there are other ways of doing this.
>>
>>
>> Anyway I believe my point stands. Whereas users can edit the device
>> tree and avoid conflicts they cannot do this with auto-probed devices.
>> So for that case we should have a way of allocating a name before
>> calling device_bind().
>
>
> OK. I'm just going to solve this by unplugging the second Ethernet card, or
> holding this patch locally for the case when I need to test the PCIe port.
>

Binding a device needs to be as cheap as possible. 99% of the time the
name is fine, and we know the cases where it might not be. This is
similar to the DM_UC_FLAG_SEQ_ALIAS - expensive functionality should
be separated out so we don't require everyone to use it. It just
creates bloat.

So please when contributing things to driver model (which is most
welcome and appreciated) be a little more flexible. It should not take
this much energy to explain the design principles and get a change to
a patch.

Also see u-boot-dm/blkb-working which includes the allocation of
device names. I need to resolve the comments and respin...

Regards,
Simon

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2016-04-29 17:22 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-04-26 21:30 [U-Boot] [RFC PATCH] dm: ensure device names are unique Stephen Warren
2016-04-26 21:36 ` Stephen Warren
2016-04-27 18:40   ` Stephen Warren
2016-04-28  4:44     ` Joe Hershberger
2016-04-28  4:42 ` Joe Hershberger
2016-04-28 16:00   ` Stephen Warren
2016-04-29 13:24     ` Simon Glass
2016-04-28  4:50 ` Simon Glass
2016-04-28 15:55   ` Stephen Warren
2016-04-29 13:23     ` Simon Glass
2016-04-29 16:23       ` Stephen Warren
2016-04-29 16:28         ` Simon Glass
2016-04-29 16:30           ` Stephen Warren
2016-04-29 17:22             ` Simon Glass

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.