diff mbox series

[10/16] i2c: mux: Create missing devlink between mux and adapter supplier

Message ID 20250407145546.270683-11-herve.codina@bootlin.com (mailing list archive)
State New
Headers show
Series lan966x pci device: Add support for SFPs | expand

Commit Message

Herve Codina April 7, 2025, 2:55 p.m. UTC
When removing an i2c controller device handling an i2c bus where an i2c
mux is connected to, the removal process hangs and is stuck in the
wait_completion() call done in i2c_del_adapter().

The i2c_del_adapter() tries to removed the i2c adapter related to the
i2c controller device and the wait_completion() is waiting for the i2c
adapter dev release. This release is performed when the device is no
more used (i.e. refcount reaches zero).

When an i2c mux is involved in an i2c path, the struct dev topology is
the following:
    +----------------+                +-------------------+
    | i2c controller |                |      i2c mux      |
    |     device     |                |      device       |
    |       ^        |                |                   |
    |       |        |                |                   |
    |  dev's parent  |                |                   |
    |       |        |                |                   |
    |   i2c adapter  |                | i2c adapter chanX |
    |     device  <---- dev's parent ------  device       |
    |   (no driver)  |                |    (no driver)    |
    +----------------+                +-------------------+

When an i2c mux device creates an i2c adapter for its downstream
channel, a reference is taken its adapter dev's parent. This parent is
the i2c mux upstream adapter device.

No relationship exists between the i2c mux device itself and the i2c
controller device in order to have the i2c mux device calling
i2c_del_adapter() to remove its downtream adapters and so, release
references taken to the upstream adapter.

This consumer/supplier relationship is typically a devlink relationship.

Also, i2c muxes can be chained and so, the upstream adapter can be
supplied by either an i2c controller device or an other i2c mux device.

In order to get the supplier of the adapter a mux is connected to,
rely on the newly introduced i2c_adapter_get_supplier() and create the
missing devlink between the i2c mux device and the supplier of the
adapter the mux is connected to.

With that done, the i2c mux device is removed before the device
handling the upstream i2c adapter (i2c controller device or i2c mux
device). All references are released and the i2c_del_adapter() call
performed by driver handling the upstream adapter device is not blocking
anymore.

Signed-off-by: Herve Codina <herve.codina@bootlin.com>
---
 drivers/i2c/i2c-mux.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
diff mbox series

Patch

diff --git a/drivers/i2c/i2c-mux.c b/drivers/i2c/i2c-mux.c
index d8bdb3b40acf..84ffd58e4d6d 100644
--- a/drivers/i2c/i2c-mux.c
+++ b/drivers/i2c/i2c-mux.c
@@ -271,7 +271,9 @@  int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
 			u32 force_nr, u32 chan_id)
 {
 	struct i2c_adapter *parent = muxc->parent;
+	struct device *parent_supplier;
 	struct i2c_mux_priv *priv;
+	struct device_link *dl;
 	char symlink_name[20];
 	int ret;
 
@@ -378,6 +380,24 @@  int i2c_mux_add_adapter(struct i2c_mux_core *muxc,
 				      ACPI_COMPANION(muxc->dev),
 				      chan_id);
 
+	/*
+	 * There is no relationship set between the mux dev and the device
+	 * handling the parent adapter. Create this missing relationship in
+	 * order to remove the i2c mux device (consumer) and so the dowstream
+	 * channel adapters before removing the device (supplier) which handles
+	 * the i2c mux upstream adapter.
+	 */
+	parent_supplier = i2c_get_adapter_supplier(parent);
+	dl = device_link_add(muxc->dev, parent_supplier, DL_FLAG_AUTOREMOVE_CONSUMER);
+	if (!dl) {
+		dev_err(muxc->dev, "failed to create device link to %s\n",
+			dev_name(parent_supplier));
+		put_device(parent_supplier);
+		ret = -EINVAL;
+		goto err_free_priv;
+	}
+	put_device(parent_supplier);
+
 	if (force_nr) {
 		priv->adap.nr = force_nr;
 		ret = i2c_add_numbered_adapter(&priv->adap);