Skip to contentSkip to conversation

i2c: atr: allow usage of nested ATRs

PATCH Applied Cosmin Tanislav 3 participants 22 messages May 7, 2025 – May 22, 2025

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

v5 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.

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

V5:
* pick up Reviewed-by tags
* expand the I2C_ATR_F_STATIC description
* place i2c_atr_create_mapping_by_addr() before
i2c_atr_replace_mapping_by_addr()

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

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

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

PATCH v5 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
#2 lore raw

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

Quoted 8 lines · Cosmin Tanislav

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.

Quoted 4 lines

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

Your SoB is missing. I will add it for you if you confirm here.

#3 lore raw

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

Quoted 9 lines · Wolfram Sang
Quoted 8 lines · Cosmin Tanislav

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.

Quoted 7 lines
Quoted 4 lines

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

Your SoB is missing. I will add it for you if you confirm here.

My bad.

reviewer
#4 lore raw

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

In reply to Wolfram Sang
Quoted 9 lines · On Thu, 22 May 2025 11:06:06 +0200 Wolfram Sang…
Quoted 8 lines · Cosmin Tanislav

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.

Quoted 6 lines
Quoted 4 lines

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

Your SoB is missing. I will add it for you if you confirm here.

Mine? I didn't think it would be needed based on:

Quoted 3 lines

The Signed-off-by: tag indicates that the signer was involved in the development of the patch, or that he/she was in the patch’s delivery path.

I'm not involved in the development, and "being in the delivery path" is probably not clear enought to me.

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

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

Hi,

bleurgh. Sent by mistake before I finished writing. :-/

Quoted 19 lines · On Thu, 22 May 2025 12:07:13 +0200 Luca Ceresoli <[email protected]>…
Quoted 9 lines · On Thu, 22 May 2025 11:06:06 +0200 Wolfram Sang…
Quoted 8 lines · Cosmin Tanislav

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.

Quoted 6 lines
Quoted 4 lines

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

Your SoB is missing. I will add it for you if you confirm here.

Quoted 7 lines

Mine? I didn't think it would be needed based on:

Quoted 3 lines

The Signed-off-by: tag indicates that the signer was involved in the development of the patch, or that he/she was in the patch’s delivery path.

I'm not involved in the development, and "being in the delivery path" is probably not clear enought to me.

However, if it is needed, I confirm you can add my:

Luca

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

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

In reply to Luca Ceresoli
Quoted 12 lines · Luca Ceresoli
Quoted 9 lines · On Thu, 22 May 2025 11:06:06 +0200 Wolfram Sang…
Quoted 8 lines · Cosmin Tanislav

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.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

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

Your SoB is missing. I will add it for you if you confirm here.

Mine? I didn't think it would be needed based on:

Ah, I was talking to Cosmin. Sorry if that wasn't clear.

#7 lore raw

PATCH v5 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
#8 lore raw

PATCH v5 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 | 122 +++++++++++++++++++++++++++++-------------
 1 file changed, 84 insertions(+), 38 deletions(-)
drivers/i2c/i2c-atr.c +84 -38
index 939fb95fe781..215b6773fe06 100644
@@ -239,56 +239,40 @@ 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_create_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 {
 	struct i2c_atr *atr = chan->atr;
 	struct i2c_atr_alias_pair *c2a;
-	struct list_head *alias_pairs;
-	bool found = false;
 	u16 alias;
 	int ret;
 
 	lockdep_assert_held(&chan->alias_pairs_lock);
 
-	alias_pairs = &chan->alias_pairs;
-
-	list_for_each_entry(c2a, alias_pairs, node) {
-		if (c2a->addr == addr)
-			return c2a;
-	}
-
 	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 +290,68 @@ i2c_atr_get_mapping_by_addr(struct i2c_atr_chan *chan, u16 addr)
 	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;
+	struct list_head *alias_pairs;
+	bool found = false;
+	u16 alias;
+	int ret;
+
+	lockdep_assert_held(&chan->alias_pairs_lock);
+
+	alias_pairs = &chan->alias_pairs;
+
+	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_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 Reviewed-by on v5 3/9
#9 lore raw

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

Quoted 9 lines · On Wed, 7 May 2025 15:19:09 +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.

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

PATCH v5 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 215b6773fe06..178b203c8777 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
#11 lore raw

PATCH v5 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 178b203c8777..ae5c2ee629f0 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
#12 lore raw

PATCH v5 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 ae5c2ee629f0..91aabfb4379b 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 Reviewed-by on v5 6/9
#13 lore raw

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

Quoted 8 lines · On Wed, 7 May 2025 15:19:12 +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.

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

PATCH v5 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 91aabfb4379b..121cabbdb85d 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
#15 lore raw

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

Quoted 6 lines · 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.

How did you guys get it to build?

CC drivers/i2c/i2c-atr.o
In file included from drivers/i2c/i2c-atr.c:12:
./include/linux/i2c-atr.h:25:1: error: empty enum is invalid

   25 | };
      | ^

make[4]: *** [scripts/Makefile.build:203: drivers/i2c/i2c-atr.o] Error 1

I'll squash patches 7+8 to avoid it.

reviewer
#16 lore raw

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

Quoted 9 lines · On Thu, 22 May 2025 10:50:24 +0200 Wolfram Sang…
Quoted 6 lines · 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.

How did you guys get it to build?

I haven't :-)

I just reviewed the code, and apparently gcc's parser is stricter than I am.

Luca

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

PATCH v5 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.

Mappings will only be added or removed as a result of devices being added or removed from a child bus.

The ATR pool will have to be big enough to accomodate all devices expected to be added to the child buses.

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 | 7 +++++++
 2 files changed, 12 insertions(+), 1 deletion(-)
drivers/i2c/i2c-atr.c +5 -1
index 121cabbdb85d..76d70efdf190 100644
@@ -341,12 +341,16 @@ i2c_atr_replace_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 +7 -0
index 5082f4dd0e23..5aaab1598084 100644
@@ -20,8 +20,15 @@ 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.
+ *                    Mappings will only be added or removed as a result of
+ *                    devices being added or removed from a child bus.
+ *                    The ATR pool will have to be big enough to accomodate all
+ *                    devices expected to be added to the child buses.
  */
 enum i2c_atr_flags {
+	I2C_ATR_F_STATIC = BIT(0),
 };
 
 /**
Signature · 1 line
2.49.0
reviewer Reviewed-by on v5 8/9
#18 lore raw

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

Quoted 6 lines · On Wed, 7 May 2025 15:19:14 +0300 Cosmin Tanislav <[email protected]>…

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

Mappings will only be added or removed as a result of devices being added or removed from a child bus.

Quoted 7 lines

The ATR pool will have to be big enough to accomodate all devices expected to be added to the child buses.

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

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

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

In reply to Cosmin Tanislav
Quoted 7 lines · Cosmin Tanislav

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

Mappings will only be added or removed as a result of devices being added or removed from a child bus.

The ATR pool will have to be big enough to accomodate all devices

CHECKPATCH
WARNING: 'accomodate' may be misspelled - perhaps 'accommodate'? #12:
The ATR pool will have to be big enough to accomodate all devices

                                           ^^^^^^^^^^

WARNING: 'accomodate' may be misspelled - perhaps 'accommodate'? #134: FILE: include/linux/i2c-atr.h:27:
+ * The ATR pool will have to be big enough to accomodate all

                                                                  ^^^^^^^^^^

total: 0 errors, 2 warnings, 0 checks, 105 lines checked

I fixed it for you this time.

Also, please make you sure you have a To: recipient in the mail header next time.

#20 lore raw

PATCH v5 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 76d70efdf190..be7d6d41e0b2 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 5aaab1598084..2bb54dc87c8e 100644
@@ -26,9 +26,11 @@ struct i2c_atr;
  *                    devices being added or removed from a child bus.
  *                    The ATR pool will have to be big enough to accomodate all
  *                    devices expected to be added to the child buses.
+ * @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
Applied
#21 lore raw
In reply to Cosmin Tanislav
Quoted 52 lines · Cosmin Tanislav

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.

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

V5:
* pick up Reviewed-by tags
* expand the I2C_ATR_F_STATIC description
* place i2c_atr_create_mapping_by_addr() before
i2c_atr_replace_mapping_by_addr()

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

Quoted 3 lines

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

Applied to for-next, thanks!