[PATCH v23 1/5] i3c: hub: Fix downstream address reservations on parent bus

Lakshay Piplani lakshay.piplani at nxp.com
Sun Nov 29 01:05:02 PST 2026


Downstream I3C devices share the physical parent bus address space but are
managed by virtual hub controllers. Marking their DT-assigned addresses as
I3C_ADDR_SLOT_I3C_DEV causes the parent controller to incorrectly probe and
register them during address reconciliation.

Mark DT-assigned addresses as desired to prevent their allocation during
the first pass of the parent address allocator.

Track parent bus address ownership per downstream device. Reserve the
address during attach before SETDASA or SETAASA, update the reservation
during reattach when the dynamic address changes, and release only the
device-owned address during detach.

Use I3C_ADDR_SLOT_RSVD for occupied downstream addresses to prevent their
allocation in both allocator passes without triggering parent address
reconciliation. Preserve the I3C_ADDR_SLOT_EXT_DESIRED marking when
claiming and releasing addresses.

Keep ENTDAA discovery and boardinfo matching within the parent controller
to simplify address management across the physical and virtual buses.
For ENTDAA-only devices, the desired address marking prevents allocation
during the first pass of the parent allocator.

Fixes: b0cfe49a2e3b ("i3c: hub: Add support for the I3C interface in the I3C hub")
Signed-off-by: Lakshay Piplani <lakshay.piplani at nxp.com>

---
Changes in v23:
 - Track parent bus address ownership per downstream I3C device.
 - Reserve addresses during attach before SETDASA or SETAASA.
 - Update parent address reservations during reattach.
 - Use I3C_ADDR_SLOT_RSVD to prevent occupied downstream addresses
   from being allocated or treated as orphaned parent-bus devices.
 - Release only device-owned parent address reservations during detach.
 - Document parent-owned ENTDAA discovery and address assignment.
 - Add the Fixes tag.
 - Rebase onto v7.4-rc4.

Changes in v22:
 - Add a generic I3C core fix for downstream DT-assigned address handling
   on the parent bus.
 - Introduce i3c_bus_set_addr_slot_desired() to set
   I3C_ADDR_SLOT_EXT_DESIRED without changing slot occupancy.
 - Avoid treating downstream addresses as orphaned parent-bus devices
   during DAA reconciliation.
 - Preserve downstream address preferences across parent DAA.
---
---
 drivers/i3c/hub.c          | 185 ++++++++++++++++++++++++++++++++++---
 drivers/i3c/master.c       |  20 ++++
 include/linux/i3c/master.h |   2 +
 3 files changed, 196 insertions(+), 11 deletions(-)

diff --git a/drivers/i3c/hub.c b/drivers/i3c/hub.c
index 539028768084..c05881895be0 100644
--- a/drivers/i3c/hub.c
+++ b/drivers/i3c/hub.c
@@ -14,6 +14,7 @@
  * @parent_desc: Permanent parent-facing descriptor whose master points at the
  *		 physical parent controller, used to forward controller-specific
  *		 operations there.
+ * @parent_addr: Parent bus address reserved for this device, or 0 if none.
  *
  * The logical descriptor on the virtual hub bus keeps its master aimed at the
  * virtual hub controller and is never modified. This separate descriptor lets
@@ -22,8 +23,64 @@
  */
 struct i3c_hub_dev_data {
 	struct i3c_dev_desc parent_desc;
+	u8 parent_addr;
 };
 
+/**
+ * i3c_hub_claim_parent_addr() - Reserve a parent bus address
+ * @parent: Physical parent controller whose address space the hub shares.
+ * @addr: Dynamic address a downstream device uses on the parent bus.
+ *
+ * Mark @addr as I3C_ADDR_SLOT_RSVD so that the parent does not assign it to
+ * another device. A reserved slot is skipped by both passes of the parent
+ * address allocator and ignored by address reconciliation, which would treat
+ * an I3C_ADDR_SLOT_I3C_DEV slot with no parent descriptor as an orphaned
+ * device and register it on the parent bus. Any I3C_ADDR_SLOT_EXT_DESIRED
+ * marking is preserved.
+ *
+ * Context: The caller must hold the parent bus maintenance lock.
+ *
+ * Return: 0 on success, -EINVAL if @addr is not a valid dynamic address, or
+ *	   -EBUSY if it is already in use.
+ */
+static int i3c_hub_claim_parent_addr(struct i3c_master_controller *parent,
+				     u8 addr)
+{
+	lockdep_assert_held_write(&parent->bus.lock);
+
+	if (!addr || addr > I3C_MAX_ADDR)
+		return -EINVAL;
+
+	if (i3c_bus_get_addr_slot_status(&parent->bus, addr) !=
+	    I3C_ADDR_SLOT_FREE)
+		return -EBUSY;
+
+	i3c_bus_set_addr_slot_status(&parent->bus, addr, I3C_ADDR_SLOT_RSVD);
+
+	return 0;
+}
+
+/**
+ * i3c_hub_release_parent_addr() - Release a reserved parent bus address
+ * @parent: Physical parent controller whose address space the hub shares.
+ * @addr: Address reserved by i3c_hub_claim_parent_addr(), or 0 for none.
+ *
+ * Return the slot to I3C_ADDR_SLOT_FREE so that the parent can assign @addr
+ * again. Any I3C_ADDR_SLOT_EXT_DESIRED marking is preserved.
+ *
+ * Context: The caller must hold the parent bus maintenance lock.
+ */
+static void i3c_hub_release_parent_addr(struct i3c_master_controller *parent,
+					u8 addr)
+{
+	lockdep_assert_held_write(&parent->bus.lock);
+
+	if (!addr || addr > I3C_MAX_ADDR)
+		return;
+
+	i3c_bus_set_addr_slot_status(&parent->bus, addr, I3C_ADDR_SLOT_FREE);
+}
+
 /*
  * All i3c_bus rw_semaphores are initialized from a single call site in the
  * I3C core, so lockdep assigns them one shared class. When a hub forwards an
@@ -132,6 +189,18 @@ static void i3c_hub_master_bus_cleanup(struct i3c_master_controller *controller)
 	controller->this = NULL;
 }
 
+/**
+ * i3c_hub_attach_i3c_dev() - Attach a downstream device to the parent
+ * @dev: Downstream device descriptor on a virtual hub bus.
+ *
+ * Allocate the parent-facing state for @dev, reserve the address @dev uses on
+ * the parent bus and forward the attach to the parent controller. The address
+ * is reserved before the core assigns it with SETDASA or SETAASA, so that a
+ * conflict is reported while it is still unused. A device without an address
+ * reserves one later in i3c_hub_reattach_i3c_dev().
+ *
+ * Return: 0 on success, or a negative error code.
+ */
 static int i3c_hub_attach_i3c_dev(struct i3c_dev_desc *dev)
 {
 	struct i3c_master_controller *controller = i3c_dev_get_master(dev);
@@ -139,6 +208,7 @@ static int i3c_hub_attach_i3c_dev(struct i3c_dev_desc *dev)
 	struct i3c_hub_dev_data *data;
 	struct i3c_master_controller *parent;
 	struct i3c_hub *hub;
+	u8 addr;
 	int ret;
 
 	hub_controller = dev_get_drvdata(&controller->dev);
@@ -164,26 +234,70 @@ static int i3c_hub_attach_i3c_dev(struct i3c_dev_desc *dev)
 	data->parent_desc.common.master = parent;
 	data->parent_desc.info = dev->info;
 
+	/*
+	 * An existing dynamic address stays in use; otherwise the core is about
+	 * to apply init_dyn_addr, which for SETAASA is the static address.
+	 */
+	addr = dev->info.dyn_addr;
+	if (!addr && dev->boardinfo)
+		addr = dev->boardinfo->init_dyn_addr;
+
 	i3c_bus_maintenance_lock(&parent->bus);
+	if (addr) {
+		ret = i3c_hub_claim_parent_addr(parent, addr);
+		if (ret) {
+			i3c_bus_maintenance_unlock(&parent->bus);
+			dev_err(&controller->dev,
+				"address 0x%02x is already in use on the parent bus\n",
+				addr);
+			goto err_free_data;
+		}
+	}
+
 	ret = i3c_master_attach_i3c_dev_controller_locked(&data->parent_desc);
-	i3c_bus_maintenance_unlock(&parent->bus);
 	if (ret) {
-		mutex_destroy(&data->parent_desc.ibi_lock);
-		kfree(data);
-		return ret;
+		/* Nothing was sent yet, so the address is still unused. */
+		if (addr)
+			i3c_hub_release_parent_addr(parent, addr);
+		i3c_bus_maintenance_unlock(&parent->bus);
+		goto err_free_data;
 	}
 
+	data->parent_addr = addr;
+	i3c_bus_maintenance_unlock(&parent->bus);
+
 	/* Link the hub-private data (see struct i3c_hub_dev_data). */
 	i3c_dev_set_master_data(dev, data);
 
 	return 0;
+
+err_free_data:
+	mutex_destroy(&data->parent_desc.ibi_lock);
+	kfree(data);
+
+	return ret;
 }
 
+/**
+ * i3c_hub_reattach_i3c_dev() - Follow a downstream address change
+ * @dev: Downstream device descriptor on a virtual hub bus.
+ * @old_dyn_addr: Address the device used before the change, or 0.
+ *
+ * Called after @dev has been assigned a new dynamic address. Move the parent
+ * bus reservation to the new address, reserving it before the old one is
+ * released. If the new address is already in use on the parent bus, the old
+ * reservation is kept and the error is returned; it is released when @dev is
+ * detached.
+ *
+ * Return: 0 on success, or a negative error code.
+ */
 static int i3c_hub_reattach_i3c_dev(struct i3c_dev_desc *dev,
 				    u8 old_dyn_addr)
 {
 	struct i3c_hub_dev_data *data = i3c_dev_get_master_data(dev);
 	struct i3c_master_controller *parent;
+	int claim_ret = 0;
+	u8 new_addr;
 	int ret;
 
 	if (!data)
@@ -209,6 +323,32 @@ static int i3c_hub_reattach_i3c_dev(struct i3c_dev_desc *dev,
 	 */
 	i3c_bus_maintenance_lock(&parent->bus);
 	data->parent_desc.info = dev->info;
+
+	new_addr = dev->info.dyn_addr;
+	if (new_addr != data->parent_addr) {
+		if (new_addr)
+			claim_ret = i3c_hub_claim_parent_addr(parent, new_addr);
+
+		if (claim_ret) {
+			/*
+			 * The device already answers at new_addr, which is in
+			 * use on the parent bus. Keep the reservation of the
+			 * old address and report the conflict.
+			 */
+			dev_err(&parent->dev,
+				"downstream device %llx moved to in-use parent address 0x%02x (%d)\n",
+				dev->info.pid, new_addr, claim_ret);
+			i3c_bus_maintenance_unlock(&parent->bus);
+
+			return claim_ret;
+		}
+
+		/* Release the old address after reserving the new one. */
+		if (data->parent_addr)
+			i3c_hub_release_parent_addr(parent, data->parent_addr);
+		data->parent_addr = new_addr;
+	}
+
 	ret = i3c_master_reattach_i3c_dev_controller_locked(&data->parent_desc,
 							    old_dyn_addr);
 	i3c_bus_maintenance_unlock(&parent->bus);
@@ -216,6 +356,14 @@ static int i3c_hub_reattach_i3c_dev(struct i3c_dev_desc *dev,
 	return ret;
 }
 
+/**
+ * i3c_hub_detach_i3c_dev() - Detach a downstream device from the parent
+ * @dev: Downstream device descriptor on a virtual hub bus.
+ *
+ * Forward the detach to the parent controller and release the parent bus
+ * address reserved for @dev. No RSTDAA is sent, so a powered target may still
+ * answer at the released address until it is reset.
+ */
 static void i3c_hub_detach_i3c_dev(struct i3c_dev_desc *dev)
 {
 	struct i3c_hub_dev_data *data = i3c_dev_get_master_data(dev);
@@ -237,6 +385,8 @@ static void i3c_hub_detach_i3c_dev(struct i3c_dev_desc *dev)
 	if (parent) {
 		i3c_bus_maintenance_lock(&parent->bus);
 		i3c_master_detach_i3c_dev_controller_locked(&data->parent_desc);
+		i3c_hub_release_parent_addr(parent, data->parent_addr);
+		data->parent_addr = 0;
 		i3c_bus_maintenance_unlock(&parent->bus);
 	}
 
@@ -763,10 +913,17 @@ EXPORT_SYMBOL_GPL(i3c_hub_init);
  * @hubc: I3C hub controller for a target-port virtual bus.
  * @node: Target-port bus Device Tree node.
  *
- * Reserve parent bus address slots for the assigned dynamic address of each
- * downstream I3C device described in the Device Tree, so that parent DAA does
- * not hand the same address to another device. Downstream devices behind hub
- * target ports share the parent controller's dynamic address space.
+ * Mark the assigned dynamic address of each downstream I3C device described in
+ * the Device Tree as desired on the parent bus, so that parent DAA avoids
+ * handing the same address to another device while it is still free.
+ * Downstream devices behind hub target ports share the parent controller's
+ * dynamic address space.
+ *
+ * A desired address is only a preference: the slot stays free, so the parent
+ * does not report it as occupied and address reconciliation does not mistake
+ * it for an orphaned device that has to be probed. A slot that is not free is
+ * left unchanged: it is already unavailable to the allocator, and the status
+ * bits do not record who reserved it.
  *
  * The reservations describe a fixed Device Tree topology and are kept for the
  * lifetime of the parent bus; they are not released when a target port is
@@ -780,6 +937,13 @@ EXPORT_SYMBOL_GPL(i3c_hub_init);
  * would also require tracking ownership of these reservations and releasing
  * them when a hub is removed.
  *
+ * Downstream devices with static addresses are configured by their
+ * virtual controllers using the corresponding Device Tree entries.
+ * ENTDAA discovery and boardinfo matching are handled by the parent
+ * controller to simplify address management. For ENTDAA-only devices,
+ * marking an address as desired keeps it unavailable during the first
+ * pass of the parent address allocator.
+ *
  * Return: 0 on success, or a negative error code.
  */
 int i3c_hub_reserve_parent_addrslots_from_dt(struct i3c_hub_controller *hubc,
@@ -816,9 +980,8 @@ int i3c_hub_reserve_parent_addrslots_from_dt(struct i3c_hub_controller *hubc,
 		status = i3c_bus_get_addr_slot_status(&parent->bus,
 						      assigned_addr);
 		if (status == I3C_ADDR_SLOT_FREE)
-			i3c_bus_set_addr_slot_status(&parent->bus,
-						     assigned_addr,
-						     I3C_ADDR_SLOT_I3C_DEV);
+			i3c_bus_set_addr_slot_desired(&parent->bus,
+						      assigned_addr);
 		i3c_bus_maintenance_unlock(&parent->bus);
 	}
 	return 0;
diff --git a/drivers/i3c/master.c b/drivers/i3c/master.c
index 9928c5dffb5b..146edbc279a3 100644
--- a/drivers/i3c/master.c
+++ b/drivers/i3c/master.c
@@ -448,6 +448,26 @@ void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr,
 }
 EXPORT_SYMBOL_GPL(i3c_bus_set_addr_slot_status);
 
+/**
+ * i3c_bus_set_addr_slot_desired() - Mark an I3C address as desired
+ * @bus: I3C bus.
+ * @addr: I3C address preferred by a device.
+ *
+ * Record that a device would like @addr without claiming that a device
+ * already uses it. Dynamic address assignment then avoids @addr while it
+ * is still free, but address reconciliation does not treat it as an
+ * orphaned device.
+ *
+ * The caller must hold the bus maintenance lock.
+ */
+void i3c_bus_set_addr_slot_desired(struct i3c_bus *bus, u16 addr)
+{
+	/* Narrow mask: set the desired flag without touching the status. */
+	i3c_bus_set_addr_slot_status_mask(bus, addr, I3C_ADDR_SLOT_EXT_DESIRED,
+					  I3C_ADDR_SLOT_EXT_DESIRED);
+}
+EXPORT_SYMBOL_GPL(i3c_bus_set_addr_slot_desired);
+
 static bool i3c_bus_dev_addr_is_avail(struct i3c_bus *bus, u8 addr)
 {
 	enum i3c_addr_slot_status status;
diff --git a/include/linux/i3c/master.h b/include/linux/i3c/master.h
index d58070a72a49..6c90ccb41e49 100644
--- a/include/linux/i3c/master.h
+++ b/include/linux/i3c/master.h
@@ -793,6 +793,8 @@ i3c_bus_get_addr_slot_status(struct i3c_bus *bus, u16 addr);
 void i3c_bus_set_addr_slot_status(struct i3c_bus *bus, u16 addr,
 				  enum i3c_addr_slot_status status);
 
+void i3c_bus_set_addr_slot_desired(struct i3c_bus *bus, u16 addr);
+
 void i3c_bus_maintenance_lock(struct i3c_bus *bus);
 void i3c_bus_maintenance_unlock(struct i3c_bus *bus);
 #endif /* I3C_MASTER_H */
-- 
2.25.1




More information about the linux-i3c mailing list