Skip to contentSkip to conversation

i2c: atr: allow usage of nested ATRs

PATCH Superseded Cosmin Tanislav 3 participants 27 messages Apr 28, 2025 – May 5, 2025

Subsystems (7)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v4 cover i2c: atr: allow usage of nested ATRs

For upcoming GMSL drivers, we need to be able to use nested ATRs. The deserializer changes the address of the serializers, and can only do that for the serializers, while the serializers have proper address translation hardware, and can translate the address of its children.

To achieve this, add a static flag and a passthrough flag. The static flag prevents usage of dynamic remapping by disallowing creation of new mappings outside of the attach_addr() function. The passthrough flag prevents messages coming from non-direct children (which don't have a local mapping) to be treated as erroneous.

This series also contains various fixes to the logic observed during development.

This series depends on:
https://lore.kernel.org/lkml/[email protected]

The previous version is at:
https://lore.kernel.org/lkml/[email protected]

V4:
* rebase on latest merged changes, and latest submitted fixes

V3:
* remove i2c_atr_new_flags(), add flags parameter to i2c_atr_new() in a new patch
* remove "i2c: atr: unlock mutex after c2a access" patch as it will be moved into the base series
* remove alias_pairs variable used only once
* remove err_del_c2a label used only once
* add lockdep_assert_held to i2c_atr_create_mapping_by_addr()
* I2C_ATR_STATIC -> I2C_ATR_F_STATIC
* I2C_ATR_PASSTHROUGH -> I2C_ATR_F_PASSTHROUGH
* add passthrough check to i2c_atr_smbus_xfer()

V2:
* rename and split up i2c_atr_find_mapping_by_addr() to allow for usage of parts of its logic where applicable

Cosmin Tanislav (8):
i2c: atr: find_mapping() -> get_mapping()
i2c: atr: split up i2c_atr_get_mapping_by_addr()
i2c: atr: do not create mapping in detach_addr()
i2c: atr: deduplicate logic in attach_addr()
i2c: atr: allow replacing mappings in attach_addr()
i2c: atr: add flags parameter to i2c_atr_new()
i2c: atr: add static flag
i2c: atr: add passthrough flag

Cosmin Tanislav (8):
i2c: atr: find_mapping() -> get_mapping()
i2c: atr: split up i2c_atr_get_mapping_by_addr()
i2c: atr: do not create mapping in detach_addr()
i2c: atr: deduplicate logic in attach_addr()
i2c: atr: allow replacing mappings in attach_addr()
i2c: atr: add flags parameter to i2c_atr_new()
i2c: atr: add static flag
i2c: atr: add passthrough flag

Tomi Valkeinen (1):
i2c: atr: Fix lockdep for nested ATRs

 drivers/i2c/i2c-atr.c         | 185 ++++++++++++++++++++++------------
 drivers/media/i2c/ds90ub960.c |   2 +-
 drivers/misc/ti_fpc202.c      |   2 +-
 include/linux/i2c-atr.h       |  15 ++-
 4 files changed, 136 insertions(+), 68 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH v4 1/9 i2c: atr: Fix lockdep for nested ATRs

From: Tomi Valkeinen <[email protected]>

When we have an ATR, and another ATR as a subdevice of the first ATR, we get lockdep warnings for the i2c_atr.lock and i2c_atr_chan.orig_addrs_lock. This is because lockdep uses a static key for the locks, and doesn't see the locks of the separate ATR instances as separate.

Fix this by generating a dynamic lock key per lock instance.

---
 drivers/i2c/i2c-atr.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
drivers/i2c/i2c-atr.c +18 -3
index 1aeaecacc26c..a79ca87e8bbd 100644
@@ -68,11 +68,13 @@ struct i2c_atr_alias_pool {
  * @atr:             The parent I2C ATR
  * @chan_id:         The ID of this channel
  * @alias_pairs_lock: Mutex protecting @alias_pairs
+ * @alias_pairs_lock_key: Lock key for @alias_pairs_lock
  * @alias_pairs:     List of @struct i2c_atr_alias_pair containing the
  *                   assigned aliases
  * @alias_pool:      Pool of available client aliases
  *
  * @orig_addrs_lock: Mutex protecting @orig_addrs
+ * @orig_addrs_lock_key: Lock key for @orig_addrs_lock
  * @orig_addrs:      Buffer used to store the original addresses during transmit
  * @orig_addrs_size: Size of @orig_addrs
  */
@@ -83,11 +85,13 @@ struct i2c_atr_chan {
 
 	/* Lock alias_pairs during attach/detach */
 	struct mutex alias_pairs_lock;
+	struct lock_class_key alias_pairs_lock_key;
 	struct list_head alias_pairs;
 	struct i2c_atr_alias_pool *alias_pool;
 
 	/* Lock orig_addrs during xfer */
 	struct mutex orig_addrs_lock;
+	struct lock_class_key orig_addrs_lock_key;
 	u16 *orig_addrs;
 	unsigned int orig_addrs_size;
 };
@@ -100,6 +104,7 @@ struct i2c_atr_chan {
  * @priv:      Private driver data, set with i2c_atr_set_driver_data()
  * @algo:      The &struct i2c_algorithm for adapters
  * @lock:      Lock for the I2C bus segment (see &struct i2c_lock_operations)
+ * @lock_key:  Lock key for @lock
  * @max_adapters: Maximum number of adapters this I2C ATR can have
  * @alias_pool: Optional common pool of available client aliases
  * @i2c_nb:    Notifier for remote client add & del events
@@ -115,6 +120,7 @@ struct i2c_atr {
 	struct i2c_algorithm algo;
 	/* lock for the I2C bus segment (see struct i2c_lock_operations) */
 	struct mutex lock;
+	struct lock_class_key lock_key;
 	int max_adapters;
 
 	struct i2c_atr_alias_pool *alias_pool;
@@ -683,7 +689,8 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
 	if (!atr)
 		return ERR_PTR(-ENOMEM);
 
-	mutex_init(&atr->lock);
+	lockdep_register_key(&atr->lock_key);
+	mutex_init_with_key(&atr->lock, &atr->lock_key);
 
 	atr->parent = parent;
 	atr->dev = dev;
@@ -711,6 +718,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
 	i2c_atr_free_alias_pool(atr->alias_pool);
 err_destroy_mutex:
 	mutex_destroy(&atr->lock);
+	lockdep_unregister_key(&atr->lock_key);
 	kfree(atr);
 
 	return ERR_PTR(ret);
@@ -727,6 +735,7 @@ void i2c_atr_delete(struct i2c_atr *atr)
 	bus_unregister_notifier(&i2c_bus_type, &atr->i2c_nb);
 	i2c_atr_free_alias_pool(atr->alias_pool);
 	mutex_destroy(&atr->lock);
+	lockdep_unregister_key(&atr->lock_key);
 	kfree(atr);
 }
 EXPORT_SYMBOL_NS_GPL(i2c_atr_delete, "I2C_ATR");
@@ -761,8 +770,10 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *desc)
 	chan->atr = atr;
 	chan->chan_id = chan_id;
 	INIT_LIST_HEAD(&chan->alias_pairs);
-	mutex_init(&chan->alias_pairs_lock);
-	mutex_init(&chan->orig_addrs_lock);
+	lockdep_register_key(&chan->alias_pairs_lock_key);
+	lockdep_register_key(&chan->orig_addrs_lock_key);
+	mutex_init_with_key(&chan->alias_pairs_lock, &chan->alias_pairs_lock_key);
+	mutex_init_with_key(&chan->orig_addrs_lock, &chan->orig_addrs_lock_key);
 
 	snprintf(chan->adap.name, sizeof(chan->adap.name), "i2c-%d-atr-%d",
 		 i2c_adapter_id(parent), chan_id);
@@ -839,6 +850,8 @@ int i2c_atr_add_adapter(struct i2c_atr *atr, struct i2c_atr_adap_desc *desc)
 	fwnode_handle_put(dev_fwnode(&chan->adap.dev));
 	mutex_destroy(&chan->orig_addrs_lock);
 	mutex_destroy(&chan->alias_pairs_lock);
+	lockdep_unregister_key(&chan->orig_addrs_lock_key);
+	lockdep_unregister_key(&chan->alias_pairs_lock_key);
 	kfree(chan);
 	return ret;
 }
@@ -876,6 +889,8 @@ void i2c_atr_del_adapter(struct i2c_atr *atr, u32 chan_id)
 	fwnode_handle_put(fwnode);
 	mutex_destroy(&chan->orig_addrs_lock);
 	mutex_destroy(&chan->alias_pairs_lock);
+	lockdep_unregister_key(&chan->orig_addrs_lock_key);
+	lockdep_unregister_key(&chan->alias_pairs_lock_key);
 	kfree(chan->orig_addrs);
 	kfree(chan);
 }
Signature · 1 line
2.49.0
reviewer Reviewed-by on v4 1/9
#2 lore raw

Re: [PATCH v4 1/9] i2c: atr: Fix lockdep for nested ATRs

Quoted 11 lines · On Mon, 28 Apr 2025 13:25:06 +0300 Cosmin Tanislav <[email protected]>…

From: Tomi Valkeinen <[email protected]>

When we have an ATR, and another ATR as a subdevice of the first ATR, we get lockdep warnings for the i2c_atr.lock and i2c_atr_chan.orig_addrs_lock. This is because lockdep uses a static key for the locks, and doesn't see the locks of the separate ATR instances as separate.

Fix this by generating a dynamic lock key per lock instance.

I'm not very qualified about the lockdep side of the changes, but regarding the ATR side this looks OK:

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#3 lore raw

PATCH v4 2/9 i2c: atr: find_mapping() -> get_mapping()

In reply to Cosmin Tanislav

A find operation implies that a null result is not an error.

Use get naming to clarify things and to prepare for splitting up the logic inside this function.

---
 drivers/i2c/i2c-atr.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/i2c/i2c-atr.c +4 -4
index a79ca87e8bbd..939fb95fe781 100644
@@ -241,7 +241,7 @@ static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 ali
 
 /* Must be called with alias_pairs_lock held */
 static struct i2c_atr_alias_pair *
-i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
@@ -339,7 +339,7 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs,
 	for (i = 0; i < num; i++) {
 		chan->orig_addrs[i] = msgs[i].addr;
 
-		c2a = i2c_atr_find_mapping_by_addr(chan, msgs[i].addr);
+		c2a = i2c_atr_get_mapping_by_addr(chan, msgs[i].addr);
 
 		if (!c2a) {
 			dev_err(atr->dev, "client 0x%02x not mapped!\n",
@@ -432,7 +432,7 @@ static int i2c_atr_smbus_xfer(struct i2c_adapter *adap, u16 addr,
 
 	mutex_lock(&chan->alias_pairs_lock);
 
-	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
+	c2a = i2c_atr_get_mapping_by_addr(chan, addr);
 
 	if (!c2a) {
 		dev_err(atr->dev, "client 0x%02x not mapped!\n", addr);
@@ -540,7 +540,7 @@ static void i2c_atr_detach_addr(struct i2c_adapter *adapter,
 
 	mutex_lock(&chan->alias_pairs_lock);
 
-	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
+	c2a = i2c_atr_get_mapping_by_addr(chan, addr);
 	if (!c2a) {
 		 /* This should never happen */
 		dev_warn(atr->dev, "Unable to find address mapping\n");
Signature · 1 line
2.49.0
reviewer Reviewed-by on v4 2/9
#4 lore raw

Re: [PATCH v4 2/9] i2c: atr: find_mapping() -> get_mapping()

Quoted 6 lines · On Mon, 28 Apr 2025 13:25:07 +0300 Cosmin Tanislav <[email protected]>…

A find operation implies that a null result is not an error.

Use get naming to clarify things and to prepare for splitting up the logic inside this function.

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#5 lore raw

PATCH v4 3/9 i2c: atr: split up i2c_atr_get_mapping_by_addr()

In reply to Cosmin Tanislav

The i2c_atr_get_mapping_by_addr() function handles three separate
usecases: finding an existing mapping, creating a new mapping, or replacing an existing mapping if a new mapping cannot be created because there aren't enough aliases available.

Split up the function into three different functions handling its individual usecases to prepare for better usage of each one.

---
 drivers/i2c/i2c-atr.c | 110 ++++++++++++++++++++++++++++++------------
 1 file changed, 78 insertions(+), 32 deletions(-)
drivers/i2c/i2c-atr.c +78 -32
index 939fb95fe781..184c57c31e60 100644
@@ -239,9 +239,23 @@ static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 ali
 	spin_unlock(&alias_pool->lock);
 }
 
-/* Must be called with alias_pairs_lock held */
 static struct i2c_atr_alias_pair *
-i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr_alias_pair *c2a;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
+	list_for_each_entry(c2a, &chan->alias_pairs, node) {
+		if (c2a->addr == addr)
+			return c2a;
+	}
+
+	return NULL;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_replace_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
@@ -254,41 +268,57 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 
 	alias_pairs = &chan->alias_pairs;
 
-	list_for_each_entry(c2a, alias_pairs, node) {
-		if (c2a->addr == addr)
-			return c2a;
+	if (unlikely(list_empty(alias_pairs)))
+		return NULL;
+
+	list_for_each_entry_reverse(c2a, alias_pairs, node) {
+		if (!c2a->fixed) {
+			found = true;
+			break;
+		}
 	}
 
+	if (!found)
+		return NULL;
+
+	atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
+	c2a->addr = addr;
+
+	list_move(&c2a->node, alias_pairs);
+
+	alias = c2a->alias;
+
+	ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias);
+	if (ret) {
+		dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n",
+			addr, chan->chan_id, ret);
+		i2c_atr_destroy_c2a(&c2a);
+		i2c_atr_release_alias(chan->alias_pool, alias);
+		return NULL;
+	}
+
+	return c2a;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr *atr = chan->atr;
+	struct i2c_atr_alias_pair *c2a;
+	u16 alias;
+	int ret;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
 	ret = i2c_atr_reserve_alias(chan->alias_pool);
-	if (ret < 0) {
-		// If no free aliases are left, replace an existing one
-		if (unlikely(list_empty(alias_pairs)))
-			return NULL;
+	if (ret < 0)
+		return NULL;
 
-		list_for_each_entry_reverse(c2a, alias_pairs, node) {
-			if (!c2a->fixed) {
-				found = true;
-				break;
-			}
-		}
+	alias = ret;
 
-		if (!found)
-			return NULL;
-
-		atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
-		c2a->addr = addr;
-
-		// Move updated entry to beginning of list
-		list_move(&c2a->node, alias_pairs);
-
-		alias = c2a->alias;
-	} else {
-		alias = ret;
-
-		c2a = i2c_atr_create_c2a(chan, alias, addr);
-		if (!c2a)
-			goto err_release_alias;
-	}
+	c2a = i2c_atr_create_c2a(chan, alias, addr);
+	if (!c2a)
+		goto err_release_alias;
 
 	ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias);
 	if (ret) {
@@ -306,6 +336,22 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 	return NULL;
 }
 
+static struct i2c_atr_alias_pair *
+i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr_alias_pair *c2a;
+
+	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
+	if (c2a)
+		return c2a;
+
+	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
+	if (c2a)
+		return c2a;
+
+	return i2c_atr_replace_mapping_by_addr(chan, addr);
+}
+
 /*
  * Replace all message addresses with their aliases, saving the original
  * addresses.
Signature · 1 line
2.49.0
reviewer
#6 lore raw

Re: [PATCH v4 3/9] i2c: atr: split up i2c_atr_get_mapping_by_addr()

Quoted 9 lines · On Mon, 28 Apr 2025 13:25:08 +0300 Cosmin Tanislav <[email protected]>…

The i2c_atr_get_mapping_by_addr() function handles three separate
usecases: finding an existing mapping, creating a new mapping, or replacing an existing mapping if a new mapping cannot be created because there aren't enough aliases available.

Split up the function into three different functions handling its individual usecases to prepare for better usage of each one.

This function has become quite complex over time, so this looks like a good cleanup by itself even not counting the advantages coming with the following patches.

I have only one small remark, see below.

Quoted 61 lines
drivers/i2c/i2c-atr.c +38 -5
index 939fb95fe781..184c57c31e60 100644
@@ -239,9 +239,23 @@ static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 ali
 	spin_unlock(&alias_pool->lock);
 }
 
-/* Must be called with alias_pairs_lock held */
 static struct i2c_atr_alias_pair *
-i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr_alias_pair *c2a;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
+	list_for_each_entry(c2a, &chan->alias_pairs, node) {
+		if (c2a->addr == addr)
+			return c2a;
+	}
+
+	return NULL;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_replace_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
@@ -254,41 +268,57 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 
 	alias_pairs = &chan->alias_pairs;
 
-	list_for_each_entry(c2a, alias_pairs, node) {
-		if (c2a->addr == addr)
-			return c2a;
+	if (unlikely(list_empty(alias_pairs)))
+		return NULL;
+
+	list_for_each_entry_reverse(c2a, alias_pairs, node) {
+		if (!c2a->fixed) {
+			found = true;
+			break;
+		}
 	}
 
+	if (!found)
+		return NULL;
+
+	atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
+	c2a->addr = addr;
+
+	list_move(&c2a->node, alias_pairs);
+
+	alias = c2a->alias;
+
+	ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias);
+	if (ret) {
+		dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n",
+			addr, chan->chan_id, ret);

drivers/i2c/i2c-atr.c i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) · line 304

Quoted 10 lines
drivers/i2c/i2c-atr.c +10 -0
+		i2c_atr_destroy_c2a(&c2a);
+		i2c_atr_release_alias(chan->alias_pool, alias);
+		return NULL;
+	}
+
+	return c2a;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)

I would move the _create function before the _replace one, because that's the logical order in which they are called.

As a nice side effect, this might make the diff more readable.

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#7 lore raw

Re: [PATCH v4 3/9] i2c: atr: split up i2c_atr_get_mapping_by_addr()

Quoted 85 lines · Luca Ceresoli
Quoted 9 lines · On Mon, 28 Apr 2025 13:25:08 +0300 Cosmin Tanislav <[email protected]>…

The i2c_atr_get_mapping_by_addr() function handles three separate
usecases: finding an existing mapping, creating a new mapping, or replacing an existing mapping if a new mapping cannot be created because there aren't enough aliases available.

Split up the function into three different functions handling its individual usecases to prepare for better usage of each one.

This function has become quite complex over time, so this looks like a good cleanup by itself even not counting the advantages coming with the following patches.

I have only one small remark, see below.

Quoted 66 lines
drivers/i2c/i2c-atr.c +43 -5
index 939fb95fe781..184c57c31e60 100644
@@ -239,9 +239,23 @@ static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 ali
  	spin_unlock(&alias_pool->lock);
  }
 
-/* Must be called with alias_pairs_lock held */
  static struct i2c_atr_alias_pair *
-i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr_alias_pair *c2a;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
+	list_for_each_entry(c2a, &chan->alias_pairs, node) {
+		if (c2a->addr == addr)
+			return c2a;
+	}
+
+	return NULL;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_replace_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
  {
  	struct i2c_atr *atr = chan->atr;
  	struct i2c_atr_alias_pair *c2a;
@@ -254,41 +268,57 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 
  	alias_pairs = &chan->alias_pairs;
 
-	list_for_each_entry(c2a, alias_pairs, node) {
-		if (c2a->addr == addr)
-			return c2a;
+	if (unlikely(list_empty(alias_pairs)))
+		return NULL;
+
+	list_for_each_entry_reverse(c2a, alias_pairs, node) {
+		if (!c2a->fixed) {
+			found = true;
+			break;
+		}
  	}
 
+	if (!found)
+		return NULL;
+
+	atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
+	c2a->addr = addr;
+
+	list_move(&c2a->node, alias_pairs);
+
+	alias = c2a->alias;
+
+	ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias);
+	if (ret) {
+		dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n",
+			addr, chan->chan_id, ret);
+		i2c_atr_destroy_c2a(&c2a);
+		i2c_atr_release_alias(chan->alias_pool, alias);
+		return NULL;
+	}
+
Quoted 9 lines

drivers/i2c/i2c-atr.c i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) · line 304

Quoted 5 lines
drivers/i2c/i2c-atr.c +5 -0
+	return c2a;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)

I would move the _create function before the _replace one, because that's the logical order in which they are called.

Sadly the diff actually becomes bigger by doing this.
before: 78 insertions(+), 32 deletions(-)
after: 84 insertions(+), 38 deletions(-)

If we were to put things in a logical order then should we put _find() after create(), or after replace()? There's no specific order in that case. I think we should keep things as-is as it matches the previous branches of the code, just split into separate functions.

Quoted 4 lines

As a nice side effect, this might make the diff more readable.

Luca

reviewer
#8 lore raw

Re: [PATCH v4 3/9] i2c: atr: split up i2c_atr_get_mapping_by_addr()

Quoted 96 lines · On Mon, 5 May 2025 13:26:54 +0300 Cosmin Tanislav <[email protected]> wrote:
Quoted 85 lines · Luca Ceresoli
Quoted 9 lines · On Mon, 28 Apr 2025 13:25:08 +0300 Cosmin Tanislav <[email protected]>…

The i2c_atr_get_mapping_by_addr() function handles three separate
usecases: finding an existing mapping, creating a new mapping, or replacing an existing mapping if a new mapping cannot be created because there aren't enough aliases available.

Split up the function into three different functions handling its individual usecases to prepare for better usage of each one.

This function has become quite complex over time, so this looks like a good cleanup by itself even not counting the advantages coming with the following patches.

I have only one small remark, see below.

Quoted 66 lines
drivers/i2c/i2c-atr.c +43 -5
index 939fb95fe781..184c57c31e60 100644
@@ -239,9 +239,23 @@ static void i2c_atr_release_alias(struct i2c_atr_alias_pool *alias_pool, u16 ali
  	spin_unlock(&alias_pool->lock);
  }
 
-/* Must be called with alias_pairs_lock held */
  static struct i2c_atr_alias_pair *
-i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+i2c_atr_find_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
+{
+	struct i2c_atr_alias_pair *c2a;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
+	list_for_each_entry(c2a, &chan->alias_pairs, node) {
+		if (c2a->addr == addr)
+			return c2a;
+	}
+
+	return NULL;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_replace_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
  {
  	struct i2c_atr *atr = chan->atr;
  	struct i2c_atr_alias_pair *c2a;
@@ -254,41 +268,57 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 
  	alias_pairs = &chan->alias_pairs;
 
-	list_for_each_entry(c2a, alias_pairs, node) {
-		if (c2a->addr == addr)
-			return c2a;
+	if (unlikely(list_empty(alias_pairs)))
+		return NULL;
+
+	list_for_each_entry_reverse(c2a, alias_pairs, node) {
+		if (!c2a->fixed) {
+			found = true;
+			break;
+		}
  	}
 
+	if (!found)
+		return NULL;
+
+	atr->ops->detach_addr(atr, chan->chan_id, c2a->addr);
+	c2a->addr = addr;
+
+	list_move(&c2a->node, alias_pairs);
+
+	alias = c2a->alias;
+
+	ret = atr->ops->attach_addr(atr, chan->chan_id, c2a->addr, c2a->alias);
+	if (ret) {
+		dev_err(atr->dev, "failed to attach 0x%02x on channel %d: err %d\n",
+			addr, chan->chan_id, ret);
+		i2c_atr_destroy_c2a(&c2a);
+		i2c_atr_release_alias(chan->alias_pool, alias);
+		return NULL;
+	}
+
Quoted 9 lines

drivers/i2c/i2c-atr.c i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr) · line 304

Quoted 5 lines
drivers/i2c/i2c-atr.c +5 -0
+	return c2a;
+}
+
+static struct i2c_atr_alias_pair *
+i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)  

I would move the _create function before the _replace one, because that's the logical order in which they are called.

Quoted 3 lines

Sadly the diff actually becomes bigger by doing this.
before: 78 insertions(+), 32 deletions(-)
after: 84 insertions(+), 38 deletions(-)

The diff size is not at all the primary goal. I just epected it would reduce, but OK, it does not matter.

Quoted 4 lines

If we were to put things in a logical order then should we put _find() after create(), or after replace()? There's no specific order in that case. I think we should keep things as-is as it matches the previous branches of the code, just split into separate functions.

Definitely find, create, replace. It's the order in which they are executed, as clearly visible i2c_atr_get_mapping_by_addr(). It's also the logical order in the old code, even though it is visually looking reverse:

[old] i2c_atr_find_mapping_by_addr():
- list_for_each_entry() # then new _find
- i2c_atr_reserve_alias() # this is the 1st half of the new _create
- if (success)
- i2c_atr_create_c2a() # 2nd half of the new _create
- else
- list_for_each_entry_reverse... atr->ops->detach_addr...
list_move... # the new _replace

This has of course no impact on the actual executed code, it's just a matter of code organization which I believe should be intuitive when doable with a small effort.

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#9 lore raw

PATCH v4 4/9 i2c: atr: do not create mapping in detach_addr()

In reply to Cosmin Tanislav

It is useless to create a new mapping just to detach it immediately.

Use the newly added i2c_atr_find_mapping_by_addr() function to avoid it, and exit without logging an error if not found.

---
 drivers/i2c/i2c-atr.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/i2c/i2c-atr.c +1 -3
index 184c57c31e60..42f433846f63 100644
@@ -586,10 +586,8 @@ static void i2c_atr_detach_addr(struct i2c_adapter *adapter,
 
 	mutex_lock(&chan->alias_pairs_lock);
 
-	c2a = i2c_atr_get_mapping_by_addr(chan, addr);
+	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
 	if (!c2a) {
-		 /* This should never happen */
-		dev_warn(atr->dev, "Unable to find address mapping\n");
 		mutex_unlock(&chan->alias_pairs_lock);
 		return;
 	}
Signature · 1 line
2.49.0
reviewer Reviewed-by on v4 4/9
#10 lore raw

Re: [PATCH v4 4/9] i2c: atr: do not create mapping in detach_addr()

Quoted 6 lines · On Mon, 28 Apr 2025 13:25:09 +0300 Cosmin Tanislav <[email protected]>…

It is useless to create a new mapping just to detach it immediately.

Use the newly added i2c_atr_find_mapping_by_addr() function to avoid it, and exit without logging an error if not found.

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#11 lore raw

PATCH v4 5/9 i2c: atr: deduplicate logic in attach_addr()

In reply to Cosmin Tanislav

This is the same logic as in i2c_atr_create_mapping_by_addr().

---
 drivers/i2c/i2c-atr.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)
drivers/i2c/i2c-atr.c +6 -24
index 42f433846f63..bf7b2ac5e9cf 100644
@@ -538,38 +538,20 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
 	struct i2c_atr_chan *chan = adapter->algo_data;
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
-	u16 alias;
-	int ret;
-
-	ret = i2c_atr_reserve_alias(chan->alias_pool);
-	if (ret < 0) {
-		dev_err(atr->dev, "failed to find a free alias\n");
-		return ret;
-	}
-
-	alias = ret;
+	int ret = 0;
 
 	mutex_lock(&chan->alias_pairs_lock);
 
-	c2a = i2c_atr_create_c2a(chan, alias, addr);
+	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
 	if (!c2a) {
-		ret = -ENOMEM;
-		goto err_release_alias;
+		dev_err(atr->dev, "failed to find a free alias\n");
+		ret = -EBUSY;
+		goto out_unlock;
 	}
 
-	ret = atr->ops->attach_addr(atr, chan->chan_id, addr, alias);
-	if (ret)
-		goto err_del_c2a;
-
 	dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n",
-		chan->chan_id, alias, addr);
+		chan->chan_id, c2a->alias, addr);
 
-	goto out_unlock;
-
-err_del_c2a:
-	i2c_atr_destroy_c2a(&c2a);
-err_release_alias:
-	i2c_atr_release_alias(chan->alias_pool, alias);
 out_unlock:
 	mutex_unlock(&chan->alias_pairs_lock);
 	return ret;
Signature · 1 line
2.49.0
maintainer Reviewed-by on v4 5/9
#13 lore raw

Re: [PATCH v4 5/9] i2c: atr: deduplicate logic in attach_addr()

In reply to Cosmin Tanislav
Quoted 46 lines · On Monday, 28 April 2025 12:25:10 CEST Cosmin Tanislav wrote:

This is the same logic as in i2c_atr_create_mapping_by_addr().

---
 drivers/i2c/i2c-atr.c | 30 ++++++------------------------
 1 file changed, 6 insertions(+), 24 deletions(-)
drivers/i2c/i2c-atr.c +6 -18
index 42f433846f63..bf7b2ac5e9cf 100644
@@ -538,38 +538,20 @@ static int i2c_atr_attach_addr(struct i2c_adapter
 *adapter, struct i2c_atr_chan *chan = adapter->algo_data;
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
-	u16 alias;
-	int ret;
-
-	ret = i2c_atr_reserve_alias(chan->alias_pool);
-	if (ret < 0) {
-		dev_err(atr->dev, "failed to find a free alias\n");
-		return ret;
-	}
-
-	alias = ret;
+	int ret = 0;
 
 	mutex_lock(&chan->alias_pairs_lock);
 
-	c2a = i2c_atr_create_c2a(chan, alias, addr);
+	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
 	if (!c2a) {
-		ret = -ENOMEM;
-		goto err_release_alias;
+		dev_err(atr->dev, "failed to find a free alias\n");
+		ret = -EBUSY;
+		goto out_unlock;
 	}
 
-	ret = atr->ops->attach_addr(atr, chan->chan_id, addr, alias);
-	if (ret)
-		goto err_del_c2a;
-
 	dev_dbg(atr->dev, "chan%u: using alias 0x%02x for addr 0x%02x\n",
-		chan->chan_id, alias, addr);
+		chan->chan_id, c2a->alias, addr);

drivers/i2c/i2c-atr.c static int i2c_atr_attach_addr(struct i2c_adapter · line 557

Quoted 10 lines
drivers/i2c/i2c-atr.c +0 -6
 
-	goto out_unlock;
-
-err_del_c2a:
-	i2c_atr_destroy_c2a(&c2a);
-err_release_alias:
-	i2c_atr_release_alias(chan->alias_pool, alias);
 out_unlock:
 	mutex_unlock(&chan->alias_pairs_lock);
 	return ret;
#14 lore raw

PATCH v4 6/9 i2c: atr: allow replacing mappings in attach_addr()

In reply to Cosmin Tanislav

It is possible for aliases to be exhausted while we are still attaching children.

Allow replacing mapping on attach by calling i2c_atr_replace_mapping_by_addr() if i2c_atr_create_mapping_by_addr() fails.

---
 drivers/i2c/i2c-atr.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/i2c/i2c-atr.c +3 -0
index bf7b2ac5e9cf..7214a59ddf15 100644
@@ -543,6 +543,9 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
 	mutex_lock(&chan->alias_pairs_lock);
 
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
+	if (!c2a)
+		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
+
 	if (!c2a) {
 		dev_err(atr->dev, "failed to find a free alias\n");
 		ret = -EBUSY;
Signature · 1 line
2.49.0
reviewer
#15 lore raw

Re: [PATCH v4 6/9] i2c: atr: allow replacing mappings in attach_addr()

Quoted 17 lines · On Mon, 28 Apr 2025 13:25:11 +0300 Cosmin Tanislav <[email protected]>…

It is possible for aliases to be exhausted while we are still attaching children.

Allow replacing mapping on attach by calling i2c_atr_replace_mapping_by_addr() if i2c_atr_create_mapping_by_addr() fails.

---
 drivers/i2c/i2c-atr.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/i2c/i2c-atr.c +0 -0
index bf7b2ac5e9cf..7214a59ddf15 100644

drivers/i2c/i2c-atr.c static int i2c_atr_attach_addr(struct i2c_adapter *adapter, · line 551

Quoted 9 lines
drivers/i2c/i2c-atr.c +3 -0
@@ -543,6 +543,9 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
 	mutex_lock(&chan->alias_pairs_lock);
 
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
+	if (!c2a)
+		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
+
 	if (!c2a) {
 		dev_err(atr->dev, "failed to find a free alias\n");
 		ret = -EBUSY;

Looks like this should be squashed into patch 5, no? I might be wrong, but IIUC the change in patch 5 is introducing a "bug" ("It is possible for aliases to be exhausted while we are still attaching children") and this patch fixes it.

Ah, nitpick: I wouldn't add that empty line.

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#16 lore raw

Re: [PATCH v4 6/9] i2c: atr: allow replacing mappings in attach_addr()

Quoted 22 lines · Luca Ceresoli
Quoted 19 lines · On Mon, 28 Apr 2025 13:25:11 +0300 Cosmin Tanislav <[email protected]>…

It is possible for aliases to be exhausted while we are still attaching children.

Allow replacing mapping on attach by calling i2c_atr_replace_mapping_by_addr() if i2c_atr_create_mapping_by_addr() fails.

---
  drivers/i2c/i2c-atr.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/i2c/i2c-atr.c +0 -0
index bf7b2ac5e9cf..7214a59ddf15 100644
@@ -543,6 +543,9 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
  	mutex_lock(&chan->alias_pairs_lock);
 
Quoted 13 lines

drivers/i2c/i2c-atr.c static int i2c_atr_attach_addr(struct i2c_adapter *adapter, · line 551

Quoted 7 lines
drivers/i2c/i2c-atr.c +3 -0
  	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
+	if (!c2a)
+		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
+
  	if (!c2a) {
  		dev_err(atr->dev, "failed to find a free alias\n");
  		ret = -EBUSY;

Looks like this should be squashed into patch 5, no? I might be wrong, but IIUC the change in patch 5 is introducing a "bug" ("It is possible for aliases to be exhausted while we are still attaching children") and this patch fixes it.

Patch 5 doesn't introduce a bug, this is just how things were before. If you look at the diff in patch 5, the error case of i2c_atr_reserve_alias() just returns an error.

The logic in i2c_atr_attach_addr() didn't handle the case where a mapping is not able to be created, matching the logic in i2c_atr_create_mapping_by_addr().

This patch adds handling for that case.

Quoted 2 lines

Ah, nitpick: I wouldn't add that empty line.

I can remove it I guess.

Quoted 2 lines

Luca

reviewer
#17 lore raw

Re: [PATCH v4 6/9] i2c: atr: allow replacing mappings in attach_addr()

Quoted 41 lines · On Mon, 5 May 2025 13:33:39 +0300 Cosmin Tanislav <[email protected]> wrote:
Quoted 22 lines · Luca Ceresoli
Quoted 19 lines · On Mon, 28 Apr 2025 13:25:11 +0300 Cosmin Tanislav <[email protected]>…

It is possible for aliases to be exhausted while we are still attaching children.

Allow replacing mapping on attach by calling i2c_atr_replace_mapping_by_addr() if i2c_atr_create_mapping_by_addr() fails.

---
  drivers/i2c/i2c-atr.c | 3 +++
  1 file changed, 3 insertions(+)
drivers/i2c/i2c-atr.c +0 -0
index bf7b2ac5e9cf..7214a59ddf15 100644
@@ -543,6 +543,9 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
  	mutex_lock(&chan->alias_pairs_lock);
 
Quoted 13 lines

drivers/i2c/i2c-atr.c static int i2c_atr_attach_addr(struct i2c_adapter *adapter, · line 551

Quoted 7 lines
drivers/i2c/i2c-atr.c +3 -0
  	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
+	if (!c2a)
+		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
+
  	if (!c2a) {
  		dev_err(atr->dev, "failed to find a free alias\n");
  		ret = -EBUSY;  

Looks like this should be squashed into patch 5, no? I might be wrong, but IIUC the change in patch 5 is introducing a "bug" ("It is possible for aliases to be exhausted while we are still attaching children") and this patch fixes it.

Patch 5 doesn't introduce a bug, this is just how things were before. If you look at the diff in patch 5, the error case of i2c_atr_reserve_alias() just returns an error.

Quoted 5 lines

The logic in i2c_atr_attach_addr() didn't handle the case where a mapping is not able to be created, matching the logic in i2c_atr_create_mapping_by_addr().

This patch adds handling for that case.

Ah, I see now. Makes sense. Thanks for the clarification!

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#18 lore raw

PATCH v4 7/9 i2c: atr: add flags parameter to i2c_atr_new()

In reply to Cosmin Tanislav

In preparation for adding multiple flags that change the behavior, add a flags parameter to i2c_atr_new() and an i2c_atr_flags enum.

---
 drivers/i2c/i2c-atr.c         |  6 +++++-
 drivers/media/i2c/ds90ub960.c |  2 +-
 drivers/misc/ti_fpc202.c      |  2 +-
 include/linux/i2c-atr.h       | 10 +++++++++-
 4 files changed, 16 insertions(+), 4 deletions(-)
drivers/i2c/i2c-atr.c +5 -1
index 7214a59ddf15..e2350fcf3d68 100644
@@ -106,6 +106,7 @@ struct i2c_atr_chan {
  * @lock:      Lock for the I2C bus segment (see &struct i2c_lock_operations)
  * @lock_key:  Lock key for @lock
  * @max_adapters: Maximum number of adapters this I2C ATR can have
+ * @flags:     Flags for ATR
  * @alias_pool: Optional common pool of available client aliases
  * @i2c_nb:    Notifier for remote client add & del events
  * @adapter:   Array of adapters
@@ -122,6 +123,7 @@ struct i2c_atr {
 	struct mutex lock;
 	struct lock_class_key lock_key;
 	int max_adapters;
+	u32 flags;
 
 	struct i2c_atr_alias_pool *alias_pool;
 
@@ -703,7 +705,8 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *atr)
 }
 
 struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
-			    const struct i2c_atr_ops *ops, int max_adapters)
+			    const struct i2c_atr_ops *ops, int max_adapters,
+			    u32 flags)
 {
 	struct i2c_atr *atr;
 	int ret;
@@ -725,6 +728,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
 	atr->dev = dev;
 	atr->ops = ops;
 	atr->max_adapters = max_adapters;
+	atr->flags = flags;
 
 	if (parent->algo->master_xfer)
 		atr->algo.master_xfer = i2c_atr_master_xfer;
drivers/media/i2c/ds90ub960.c +1 -1
index 869e32bd07e8..6f475bae94b3 100644
@@ -1122,7 +1122,7 @@ static int ub960_init_atr(struct ub960_data *priv)
 	struct i2c_adapter *parent_adap = priv->client->adapter;
 
 	priv->atr = i2c_atr_new(parent_adap, dev, &ub960_atr_ops,
-				priv->hw_data->num_rxports);
+				priv->hw_data->num_rxports, 0);
 	if (IS_ERR(priv->atr))
 		return PTR_ERR(priv->atr);
 
drivers/misc/ti_fpc202.c +1 -1
index b9c9ee4bfc4e..f7cde245ac95 100644
@@ -349,7 +349,7 @@ static int fpc202_probe(struct i2c_client *client)
 		goto disable_gpio;
 	}
 
-	priv->atr = i2c_atr_new(client->adapter, dev, &fpc202_atr_ops, 2);
+	priv->atr = i2c_atr_new(client->adapter, dev, &fpc202_atr_ops, 2, 0);
 	if (IS_ERR(priv->atr)) {
 		ret = PTR_ERR(priv->atr);
 		dev_err(dev, "failed to create i2c atr err %d\n", ret);
include/linux/i2c-atr.h +9 -1
index 1c3a5bcd939f..5082f4dd0e23 100644
@@ -18,6 +18,12 @@ struct device;
 struct fwnode_handle;
 struct i2c_atr;
 
+/**
+ * enum i2c_atr_flags - Flags for an I2C ATR driver
+ */
+enum i2c_atr_flags {
+};
+
 /**
  * struct i2c_atr_ops - Callbacks from ATR to the device driver.
  * @attach_addr: Notify the driver of a new device connected on a child
@@ -65,6 +71,7 @@ struct i2c_atr_adap_desc {
  * @dev:          The device acting as an ATR
  * @ops:          Driver-specific callbacks
  * @max_adapters: Maximum number of child adapters
+ * @flags:        Flags for ATR
  *
  * The new ATR helper is connected to the parent adapter but has no child
  * adapters. Call i2c_atr_add_adapter() to add some.
@@ -74,7 +81,8 @@ struct i2c_atr_adap_desc {
  * Return: pointer to the new ATR helper object, or ERR_PTR
  */
 struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
-			    const struct i2c_atr_ops *ops, int max_adapters);
+			    const struct i2c_atr_ops *ops, int max_adapters,
+			    u32 flags);
 
 /**
  * i2c_atr_delete - Delete an I2C ATR helper.
Signature · 1 line
2.49.0
reviewer Reviewed-by on v4 7/9
#19 lore raw

Re: [PATCH v4 7/9] i2c: atr: add flags parameter to i2c_atr_new()

Quoted 4 lines · On Mon, 28 Apr 2025 13:25:12 +0300 Cosmin Tanislav <[email protected]>…

In preparation for adding multiple flags that change the behavior, add a flags parameter to i2c_atr_new() and an i2c_atr_flags enum.

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer Reviewed-by on v4 7/9
#20 lore raw

Re: [PATCH v4 7/9] i2c: atr: add flags parameter to i2c_atr_new()

In reply to Cosmin Tanislav
Quoted 101 lines · On Monday, 28 April 2025 12:25:12 CEST Cosmin Tanislav wrote:

In preparation for adding multiple flags that change the behavior, add a flags parameter to i2c_atr_new() and an i2c_atr_flags enum.

---
 drivers/i2c/i2c-atr.c         |  6 +++++-
 drivers/media/i2c/ds90ub960.c |  2 +-
 drivers/misc/ti_fpc202.c      |  2 +-
 include/linux/i2c-atr.h       | 10 +++++++++-
 4 files changed, 16 insertions(+), 4 deletions(-)
drivers/i2c/i2c-atr.c +0 -0
index 7214a59ddf15..e2350fcf3d68 100644
@@ -106,6 +106,7 @@ struct i2c_atr_chan {
  * @lock:      Lock for the I2C bus segment (see &struct

i2c_lock_operations) * @lock_key: Lock key for @lock
* @max_adapters: Maximum number of adapters this I2C ATR can have
+ * @flags: Flags for ATR
* @alias_pool: Optional common pool of available client aliases
* @i2c_nb: Notifier for remote client add & del events
* @adapter: Array of adapters

drivers/i2c/i2c-atr.c +4 -1
@@ -122,6 +123,7 @@ struct i2c_atr {
 	struct mutex lock;
 	struct lock_class_key lock_key;
 	int max_adapters;
+	u32 flags;
 
 	struct i2c_atr_alias_pool *alias_pool;
 
@@ -703,7 +705,8 @@ static int i2c_atr_parse_alias_pool(struct i2c_atr *atr)
 }
 
 struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
-			    const struct i2c_atr_ops *ops, int max_adapters)
+			    const struct i2c_atr_ops *ops, int max_adapters,
+			    u32 flags)
 {
 	struct i2c_atr *atr;
 	int ret;
@@ -725,6 +728,7 @@ struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent,
 struct device *dev, atr->dev = dev;
 	atr->ops = ops;
 	atr->max_adapters = max_adapters;
+	atr->flags = flags;
 
 	if (parent->algo->master_xfer)
 		atr->algo.master_xfer = i2c_atr_master_xfer;
drivers/media/i2c/ds90ub960.c +1 -1
index 869e32bd07e8..6f475bae94b3 100644
@@ -1122,7 +1122,7 @@ static int ub960_init_atr(struct ub960_data *priv)
 	struct i2c_adapter *parent_adap = priv->client->adapter;
 
 	priv->atr = i2c_atr_new(parent_adap, dev, &ub960_atr_ops,
-				priv->hw_data->num_rxports);
+				priv->hw_data->num_rxports, 0);
 	if (IS_ERR(priv->atr))
 		return PTR_ERR(priv->atr);
 
drivers/misc/ti_fpc202.c +1 -1
index b9c9ee4bfc4e..f7cde245ac95 100644
@@ -349,7 +349,7 @@ static int fpc202_probe(struct i2c_client *client)
 		goto disable_gpio;
 	}
 
-	priv->atr = i2c_atr_new(client->adapter, dev, &fpc202_atr_ops, 2);
+	priv->atr = i2c_atr_new(client->adapter, dev, &fpc202_atr_ops, 2, 0);
 	if (IS_ERR(priv->atr)) {
 		ret = PTR_ERR(priv->atr);
 		dev_err(dev, "failed to create i2c atr err %d\n", ret);
include/linux/i2c-atr.h +7 -0
index 1c3a5bcd939f..5082f4dd0e23 100644
@@ -18,6 +18,12 @@ struct device;
 struct fwnode_handle;
 struct i2c_atr;
 
+/**
+ * enum i2c_atr_flags - Flags for an I2C ATR driver
+ */
+enum i2c_atr_flags {
+};
+
 /**
  * struct i2c_atr_ops - Callbacks from ATR to the device driver.
  * @attach_addr: Notify the driver of a new device connected on a child
@@ -65,6 +71,7 @@ struct i2c_atr_adap_desc {
  * @dev:          The device acting as an ATR
  * @ops:          Driver-specific callbacks
  * @max_adapters: Maximum number of child adapters
+ * @flags:        Flags for ATR
  *
  * The new ATR helper is connected to the parent adapter but has no child
  * adapters. Call i2c_atr_add_adapter() to add some.

include/linux/i2c-atr.h struct i2c_atr_adap_desc { · line 88

Quoted 9 lines
include/linux/i2c-atr.h +2 -1
@@ -74,7 +81,8 @@ struct i2c_atr_adap_desc {
  * Return: pointer to the new ATR helper object, or ERR_PTR
  */
 struct i2c_atr *i2c_atr_new(struct i2c_adapter *parent, struct device *dev,
-			    const struct i2c_atr_ops *ops, int max_adapters);
+			    const struct i2c_atr_ops *ops, int max_adapters,
+			    u32 flags);
 
 /**
  * i2c_atr_delete - Delete an I2C ATR helper.

Thanks!

#21 lore raw

PATCH v4 8/9 i2c: atr: add static flag

In reply to Cosmin Tanislav

Some I2C ATRs do not support dynamic remapping, only static mapping of direct children.

Add a new flag that prevents old mappings to be replaced or new mappings to be created in the alias finding code paths.

---
 drivers/i2c/i2c-atr.c   | 6 +++++-
 include/linux/i2c-atr.h | 3 +++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/i2c/i2c-atr.c +5 -1
index e2350fcf3d68..721dd680f2ac 100644
@@ -341,12 +341,16 @@ i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 static struct i2c_atr_alias_pair *
 i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
+	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
 
 	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
 	if (c2a)
 		return c2a;
 
+	if (atr->flags & I2C_ATR_F_STATIC)
+		return NULL;
+
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
 	if (c2a)
 		return c2a;
@@ -545,7 +549,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
 	mutex_lock(&chan->alias_pairs_lock);
 
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
-	if (!c2a)
+	if (!c2a && !(atr->flags & I2C_ATR_F_STATIC))
 		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
 
 	if (!c2a) {
include/linux/i2c-atr.h +3 -0
index 5082f4dd0e23..7c6a9627191d 100644
@@ -20,8 +20,11 @@ struct i2c_atr;
 
 /**
  * enum i2c_atr_flags - Flags for an I2C ATR driver
+ *
+ * @I2C_ATR_F_STATIC: ATR does not support dynamic mapping, use static mapping
  */
 enum i2c_atr_flags {
+	I2C_ATR_F_STATIC = BIT(0),
 };
 
 /**
Signature · 1 line
2.49.0
reviewer
#22 lore raw

Re: [PATCH v4 8/9] i2c: atr: add static flag

Quoted 47 lines · On Mon, 28 Apr 2025 13:25:13 +0300 Cosmin Tanislav <[email protected]>…

Some I2C ATRs do not support dynamic remapping, only static mapping of direct children.

Add a new flag that prevents old mappings to be replaced or new mappings to be created in the alias finding code paths.

---
 drivers/i2c/i2c-atr.c   | 6 +++++-
 include/linux/i2c-atr.h | 3 +++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/i2c/i2c-atr.c +5 -1
index e2350fcf3d68..721dd680f2ac 100644
@@ -341,12 +341,16 @@ i2c_atr_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 static struct i2c_atr_alias_pair *
 i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
+	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
 
 	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
 	if (c2a)
 		return c2a;
 
+	if (atr->flags & I2C_ATR_F_STATIC)
+		return NULL;
+
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
 	if (c2a)
 		return c2a;
@@ -545,7 +549,7 @@ static int i2c_atr_attach_addr(struct i2c_adapter *adapter,
 	mutex_lock(&chan->alias_pairs_lock);
 
 	c2a = i2c_atr_create_mapping_by_addr(chan, addr);
-	if (!c2a)
+	if (!c2a && !(atr->flags & I2C_ATR_F_STATIC))
 		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
 
 	if (!c2a) {
include/linux/i2c-atr.h +0 -0
index 5082f4dd0e23..7c6a9627191d 100644

include/linux/i2c-atr.h struct i2c_atr; · line 24

Quoted 5 lines
include/linux/i2c-atr.h +2 -0
@@ -20,8 +20,11 @@ struct i2c_atr;
 
 /**
  * enum i2c_atr_flags - Flags for an I2C ATR driver
+ *
+ * @I2C_ATR_F_STATIC: ATR does not support dynamic mapping, use static mapping

Maybe add something along the lines of: "mappings can only be added/removed by the higher level driver via i2c_atr_attach_addr/i2c_atr_detach_addr"

Other than that, looks good.

Luca

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer
#23 lore raw

Re: [PATCH v4 8/9] i2c: atr: add static flag

In reply to Cosmin Tanislav

Hi Cosmin,

Quoted 30 lines · On Monday, 28 April 2025 12:25:13 CEST Cosmin Tanislav wrote:

Some I2C ATRs do not support dynamic remapping, only static mapping of direct children.

Add a new flag that prevents old mappings to be replaced or new mappings to be created in the alias finding code paths.

---
 drivers/i2c/i2c-atr.c   | 6 +++++-
 include/linux/i2c-atr.h | 3 +++
 2 files changed, 8 insertions(+), 1 deletion(-)
drivers/i2c/i2c-atr.c +4 -0
index e2350fcf3d68..721dd680f2ac 100644
@@ -341,12 +341,16 @@ i2c_atr_create_mapping_by_addr(struct i2c_atr_chan
 *chan, u16 addr) static struct i2c_atr_alias_pair *
 i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
+	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
 
 	c2a = i2c_atr_find_mapping_by_addr(chan, addr);
 	if (c2a)
 		return c2a;
 
+	if (atr->flags & I2C_ATR_F_STATIC)
+		return NULL;
+

...

Quoted 13 lines
drivers/i2c/i2c-atr.c +0 -0
 		c2a = i2c_atr_replace_mapping_by_addr(chan, addr);
 
 	if (!c2a) {
include/linux/i2c-atr.h +2 -0
index 5082f4dd0e23..7c6a9627191d 100644
@@ -20,8 +20,11 @@ struct i2c_atr;
 
 /**
  * enum i2c_atr_flags - Flags for an I2C ATR driver
+ *
+ * @I2C_ATR_F_STATIC: ATR does not support dynamic mapping, use static
Quoted 1 line

mapping */

I would suggest clarifying a bit more what "dynamic mapping" means in this doc. Maybe something like "ATR does not support on-the-fly modifications of its translation table, use static mappings". IMO this will make it easier for people who don't have the full technical context of this series and want to understand what the flag is for.

Everything else looks good to me.

Thanks,

Signature · 3 lines
Romain Gantois, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#24 lore raw

PATCH v4 9/9 i2c: atr: add passthrough flag

In reply to Cosmin Tanislav

Some I2C ATRs can have other I2C ATRs as children. The I2C messages of the child ATRs need to be forwarded as-is if the parent I2C ATR can only do static mapping.

In the case of GMSL, the deserializer I2C ATR actually doesn't have I2C address remapping hardware capabilities, but it is able to select which GMSL link to talk to, allowing it to change the address of the serializer.

The child ATRs need to have their alias pools defined in such a way to prevent overlapping addresses between them, but there's no way around this without orchestration between multiple ATR instances.

To allow for this use-case, add a flag that allows unmapped addresses to be passed through, since they are already remapped by the child ATRs.

There's no case where an address that has not been remapped by the child ATR will hit the parent ATR.

---
 drivers/i2c/i2c-atr.c   | 7 +++++--
 include/linux/i2c-atr.h | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/i2c/i2c-atr.c +5 -2
index 721dd680f2ac..eccb85c34609 100644
@@ -394,6 +394,9 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan, struct i2c_msg *msgs,
 		c2a = i2c_atr_get_mapping_by_addr(chan, msgs[i].addr);
 
 		if (!c2a) {
+			if (atr->flags & I2C_ATR_F_PASSTHROUGH)
+				continue;
+
 			dev_err(atr->dev, "client 0x%02x not mapped!\n",
 				msgs[i].addr);
 
@@ -486,13 +489,13 @@ static int i2c_atr_smbus_xfer(struct i2c_adapter *adap, u16 addr,
 
 	c2a = i2c_atr_get_mapping_by_addr(chan, addr);
 
-	if (!c2a) {
+	if (!c2a && !(atr->flags & I2C_ATR_F_PASSTHROUGH)) {
 		dev_err(atr->dev, "client 0x%02x not mapped!\n", addr);
 		mutex_unlock(&chan->alias_pairs_lock);
 		return -ENXIO;
 	}
 
-	alias = c2a->alias;
+	alias = c2a ? c2a->alias : addr;
 
 	mutex_unlock(&chan->alias_pairs_lock);
 
include/linux/i2c-atr.h +2 -0
index 7c6a9627191d..f979b931ca05 100644
@@ -22,9 +22,11 @@ struct i2c_atr;
  * enum i2c_atr_flags - Flags for an I2C ATR driver
  *
  * @I2C_ATR_F_STATIC: ATR does not support dynamic mapping, use static mapping
+ * @I2C_ATR_F_PASSTHROUGH: Allow unmapped incoming addresses to pass through
  */
 enum i2c_atr_flags {
 	I2C_ATR_F_STATIC = BIT(0),
+	I2C_ATR_F_PASSTHROUGH = BIT(1),
 };
 
 /**
Signature · 1 line
2.49.0
reviewer Reviewed-by on v4 9/9
#25 lore raw

Re: [PATCH v4 9/9] i2c: atr: add passthrough flag

Quoted 13 lines · On Mon, 28 Apr 2025 13:25:14 +0300 Cosmin Tanislav <[email protected]>…

Some I2C ATRs can have other I2C ATRs as children. The I2C messages of the child ATRs need to be forwarded as-is if the parent I2C ATR can only do static mapping.

In the case of GMSL, the deserializer I2C ATR actually doesn't have I2C address remapping hardware capabilities, but it is able to select which GMSL link to talk to, allowing it to change the address of the serializer.

The child ATRs need to have their alias pools defined in such a way to prevent overlapping addresses between them, but there's no way around this without orchestration between multiple ATR instances.

Quoted 7 lines

To allow for this use-case, add a flag that allows unmapped addresses to be passed through, since they are already remapped by the child ATRs.

There's no case where an address that has not been remapped by the child ATR will hit the parent ATR.

Signature · 3 lines
Luca Ceresoli, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
maintainer Reviewed-by on v4 9/9
#26 lore raw

Re: [PATCH v4 9/9] i2c: atr: add passthrough flag

In reply to Cosmin Tanislav
Quoted 68 lines · On Monday, 28 April 2025 12:25:14 CEST Cosmin Tanislav wrote:

Some I2C ATRs can have other I2C ATRs as children. The I2C messages of the child ATRs need to be forwarded as-is if the parent I2C ATR can only do static mapping.

In the case of GMSL, the deserializer I2C ATR actually doesn't have I2C address remapping hardware capabilities, but it is able to select which GMSL link to talk to, allowing it to change the address of the serializer.

The child ATRs need to have their alias pools defined in such a way to prevent overlapping addresses between them, but there's no way around this without orchestration between multiple ATR instances.

To allow for this use-case, add a flag that allows unmapped addresses to be passed through, since they are already remapped by the child ATRs.

There's no case where an address that has not been remapped by the child ATR will hit the parent ATR.

---
 drivers/i2c/i2c-atr.c   | 7 +++++--
 include/linux/i2c-atr.h | 2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)
drivers/i2c/i2c-atr.c +5 -2
index 721dd680f2ac..eccb85c34609 100644
@@ -394,6 +394,9 @@ static int i2c_atr_map_msgs(struct i2c_atr_chan *chan,
 struct i2c_msg *msgs, c2a = i2c_atr_get_mapping_by_addr(chan,
 msgs[i].addr);
 
 		if (!c2a) {
+			if (atr->flags & I2C_ATR_F_PASSTHROUGH)
+				continue;
+
 			dev_err(atr->dev, "client 0x%02x not mapped!\n",
 				msgs[i].addr);
@@ -486,13 +489,13 @@ static int i2c_atr_smbus_xfer(struct i2c_adapter
 *adap, u16 addr,
 
 	c2a = i2c_atr_get_mapping_by_addr(chan, addr);
 
-	if (!c2a) {
+	if (!c2a && !(atr->flags & I2C_ATR_F_PASSTHROUGH)) {
 		dev_err(atr->dev, "client 0x%02x not mapped!\n", addr);
 		mutex_unlock(&chan->alias_pairs_lock);
 		return -ENXIO;
 	}
 
-	alias = c2a->alias;
+	alias = c2a ? c2a->alias : addr;
 
 	mutex_unlock(&chan->alias_pairs_lock);
include/linux/i2c-atr.h +0 -0
index 7c6a9627191d..f979b931ca05 100644
@@ -22,9 +22,11 @@ struct i2c_atr;
  * enum i2c_atr_flags - Flags for an I2C ATR driver
  *
  * @I2C_ATR_F_STATIC: ATR does not support dynamic mapping, use static

mapping + * @I2C_ATR_F_PASSTHROUGH: Allow unmapped incoming addresses to pass through */
enum i2c_atr_flags {

include/linux/i2c-atr.h struct i2c_atr; · line 32

Quoted 5 lines
include/linux/i2c-atr.h +1 -0
 	I2C_ATR_F_STATIC = BIT(0),
+	I2C_ATR_F_PASSTHROUGH = BIT(1),
 };
 
 /**