Skip to contentSkip to conversation

i3c: add i3cdev module to expose i3c dev in /dev

PATCH In review Sam Agazaryan 7 participants 33 messages Sep 21 – Nov 12

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

v5 cover i3c: add i3cdev module to expose i3c dev in /dev

This patch series introduces the i3cdev module, exposing unbound I3C target devices to userspace via character device nodes (/dev/bus/i3c/*), along with the i3ctransfer userspace utility in tools/i3c/.

Userspace access to I3C targets is needed for devices that do not have a kernel driver bound to them, such as targets in ROM/bootloader recovery mode (e.g., OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows), as well as hardware bring-up and diagnostics.

When a kernel driver later binds to an I3C device (for example via dynamic module loading), i3cdev automatically detaches via the BUS_NOTIFY_BIND_DRIVER notifier so kernel drivers always take precedence.

Testing status:
This v5 series has been compile-tested across all modified I3C controller drivers with W=1. Posting v5 now so collaborators and controller owners can test the unified UAPI and actual_len updates on their respective hardware and provide Tested-by tags while we complete final hardware verification of the v5 updates on our platform.

Changes in v5:
- Updated Patch 2/5 ("i3c: master: add i3c_for_each_dev helper"):
- Dropped mutex_lock(&i3c_core_lock) around bus_for_each_dev() to avoid lock inversion and deadlock with external callbacks (sashiko-bot).
- Added Patch 3/5 ("i3c: use actual_len for read transfers"):
- Clarified @actual_len kerneldoc in <linux/i3c/device.h> (Adrian Hunter).
- Incorporated Meagan Lloyd's controller updates (adi, dw, cdns, mipi-i3c-hci, renesas) so all I3C controller drivers populate xfer->actual_len on reads without mutating xfer->len (Adrian Hunter, Meagan Lloyd).
- Updated mctp-i3c to read xfer.actual_len instead of xfer.len (Adrian Hunter).
- Updated Patch 4/5 ("i3c: add i3cdev module to expose i3c dev in /dev"):
- Unified the UAPI around struct i3c_ioc_xfer and I3C_IOC_XFER(N) in <uapi/linux/i3c/i3cdev.h>, supporting both SDR and HDR modes via a mode field and union { __u8 rnw; __u8 cmd; } (Frank Li).
- Added __u16 actual_len to struct i3c_ioc_xfer and updated i3cdev_do_xfer() to copy actual_len bytes to userspace and write actual_len back to userspace via put_user() on read transfers (Adrian Hunter, Frank Li).
- Tracked active i3cdev instances in a private list (i3cdev_list) instead of using i3cdev_set_drvdata()/i3cdev_get_drvdata() so i3cdev never clobbers client drivers' dev->driver_data (Meagan Lloyd).
- Validated direction field and verified that reserved padding pad[2] is zeroed (Greg KH).
- Clamped read()/write() byte count to type_max(xfers.len) (u16) to prevent silent integer truncation and unbounded kernel allocations (sashiko-bot).
- Used ida_alloc_max() capped to MINORMASK to prevent minor number overflow (sashiko-bot).
- Added i3cdev_attach_lock mutex and idempotency check in i3cdev_attach()/i3cdev_detach() to serialize concurrent attach/detach calls (sashiko-bot).
- Updated bus notifier to handle BUS_NOTIFY_DRIVER_NOT_BOUND and return NOTIFY_OK instead of raw errno values (sashiko-bot).
- Added Patch 5/5 ("tools: i3c: add i3ctransfer utility"):
- Added tools/i3c/i3ctransfer.c (adapted from Vitor Soares's i3c-tools) updated for struct i3c_ioc_xfer, supporting SDR and HDR (-m, -c) modes and reporting actual_len bytes on reads (Wolfram Sang).
- Note to Vitor Soares: Since your original i3ctransfer commit in i3c-tools did not include a Signed-off-by tag, could you please reply with your Signed-off-by / Acked-by for Patch 5/5?

Changes in v4:
- Dropped dev_open count tracking and used cdev_device_add/del with mutex locking to prevent use-after-free on detach (Greg KH, Wolfram Sang).
- Fixed 32/64-bit UAPI alignment and added compat_ptr_ioctl.
- Switched to dynamic minor allocation via IDA.

Sam Agazaryan (2):
i3c: use actual_len for read transfers
tools: i3c: add i3ctransfer utility

Vitor Soares (3):
i3c: master: export i3c_masterdev_type
i3c: master: add i3c_for_each_dev helper
i3c: add i3cdev module to expose i3c dev in /dev

 MAINTAINERS                            |   2 +
 drivers/i3c/Kconfig                    |  11 +
 drivers/i3c/Makefile                   |   1 +
 drivers/i3c/i3cdev.c                   | 491 +++++++++++++++++++++++++
 drivers/i3c/internals.h                |   4 +
 drivers/i3c/master.c                   |   9 +-
 drivers/i3c/master/adi-i3c-master.c    |   5 +-
 drivers/i3c/master/dw-i3c-master.c     |   2 +-
 drivers/i3c/master/i3c-master-cdns.c   |   5 +-
 drivers/i3c/master/mipi-i3c-hci/core.c |   2 +-
 drivers/i3c/master/renesas-i3c.c       |   3 +
 drivers/net/mctp/mctp-i3c.c            |  10 +-
 include/linux/i3c/device.h             |   2 +-
 include/uapi/linux/i3c/i3cdev.h        |  57 +++
 tools/Makefile                         |  13 +-
 tools/i3c/Build                        |   1 +
 tools/i3c/Makefile                     |  58 +++
 tools/i3c/i3ctransfer.c                | 307 ++++++++++++++++
 18 files changed, 966 insertions(+), 17 deletions(-)
 create mode 100644 drivers/i3c/i3cdev.c
 create mode 100644 include/uapi/linux/i3c/i3cdev.h
 create mode 100644 tools/i3c/Build
 create mode 100644 tools/i3c/Makefile
 create mode 100644 tools/i3c/i3ctransfer.c
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#1 lore raw

PATCH v5 1/5 i3c: master: export i3c_masterdev_type

From: Vitor Soares <[email protected]>

Export i3c_masterdev_type so i3cdev module can verify if an i3c device is a master.

---
 drivers/i3c/internals.h | 2 ++
 drivers/i3c/master.c    | 3 ++-
 2 files changed, 4 insertions(+), 1 deletion(-)
drivers/i3c/internals.h +2 -0
index 86a36b951e0d..cbc00c862104 100644
@@ -11,6 +11,8 @@
 #include <linux/i3c/master.h>
 #include <linux/io.h>
 
+extern const struct device_type i3c_masterdev_type;
+
 int __must_check i3c_bus_rpm_get(struct i3c_bus *bus);
 void i3c_bus_rpm_put(struct i3c_bus *bus);
 bool i3c_bus_rpm_ibi_allowed(struct i3c_bus *bus);
drivers/i3c/master.c +2 -1
index afcd7a21a3e6..e8a9f8c3041e 100644
@@ -864,9 +864,10 @@ static void i3c_masterdev_release(struct device *dev)
 	i3c_master_free_i3c_dev(master->this);
 }
 
-static const struct device_type i3c_masterdev_type = {
+const struct device_type i3c_masterdev_type = {
 	.groups	= i3c_masterdev_groups,
 };
+EXPORT_SYMBOL_GPL(i3c_masterdev_type);
 
 static void i3c_master_shutdown(struct i3c_master_controller *master)
 {
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#2 lore raw

PATCH v5 2/5 i3c: master: add i3c_for_each_dev helper

In reply to Sam Agazaryan

From: Vitor Soares <[email protected]>

Introduce i3c_for_each_dev(), an i3c device iterator for use by i3cdev.

---
 drivers/i3c/internals.h | 2 ++
 drivers/i3c/master.c    | 6 ++++++
 2 files changed, 8 insertions(+)
drivers/i3c/internals.h +2 -0
index cbc00c862104..3c53a5108b81 100644
@@ -79,4 +79,6 @@ static inline struct i3c_master_controller *i3c_bus_to_i3c_master(struct i3c_bus
 	return container_of(i3cbus, struct i3c_master_controller, bus);
 }
 
+int i3c_for_each_dev(void *data, int (*fn)(struct device *, void *));
+
 #endif /* I3C_INTERNAL_H */
drivers/i3c/master.c +6 -0
index e8a9f8c3041e..07540ea73aec 100644
@@ -4011,6 +4011,12 @@ void i3c_dev_free_ibi_locked(struct i3c_dev_desc *dev)
 }
 EXPORT_SYMBOL_GPL(i3c_dev_free_ibi_locked);
 
+int i3c_for_each_dev(void *data, int (*fn)(struct device *, void *))
+{
+	return bus_for_each_dev(&i3c_bus_type, NULL, data, fn);
+}
+EXPORT_SYMBOL_GPL(i3c_for_each_dev);
+
 static int __init i3c_init(void)
 {
 	int res;
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#3 lore raw

PATCH v5 3/5 i3c: use actual_len for read transfers

In reply to Sam Agazaryan

Currently only the amd and svc I3C controller drivers populate i3c_xfer.actual_len on reads, while dw-i3c-master and mipi-i3c-hci overwrite i3c_xfer.len with the received byte count and leave actual_len as 0. Because of this inconsistency, mctp-i3c reads xfer.len instead of xfer.actual_len, making it fail on controllers that do not mutate len.

Unify read length reporting across the subsystem:
- Clarify in <linux/i3c/device.h> that actual_len reflects the actual number of bytes transferred on reads.
- Update adi, cdns, dw, mipi-i3c-hci, and renesas controller drivers to populate actual_len on read transfers without mutating len.
- Update mctp-i3c to read xfer.actual_len instead of xfer.len.

---
 drivers/i3c/master/adi-i3c-master.c    |  5 ++++-
 drivers/i3c/master/dw-i3c-master.c     |  2 +-
 drivers/i3c/master/i3c-master-cdns.c   |  5 ++++-
 drivers/i3c/master/mipi-i3c-hci/core.c |  2 +-
 drivers/i3c/master/renesas-i3c.c       |  3 +++
 drivers/net/mctp/mctp-i3c.c            | 10 +++++-----
 include/linux/i3c/device.h             |  2 +-
 7 files changed, 19 insertions(+), 10 deletions(-)
drivers/i3c/master/adi-i3c-master.c +4 -1
index b35386260350..d033bfd4ecc2 100644
@@ -412,8 +412,11 @@ static int adi_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = adi_i3c_cmd_get_err(&xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = xfer->cmds[i].rx_len;
+	}
 
 	return ret;
 }
drivers/i3c/master/dw-i3c-master.c +1 -1
index 4563d8761ba0..28e8eb293747 100644
@@ -1029,7 +1029,7 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
 
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = cmd->rx_len;
+			i3c_xfers[i].actual_len = cmd->rx_len;
 	}
 
 	ret = xfer->ret;
drivers/i3c/master/i3c-master-cdns.c +4 -1
index 2d98c1ce9b12..331062c10b31 100644
@@ -795,8 +795,11 @@ static int cdns_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = cdns_xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = cdns_i3c_cmd_get_err(&cdns_xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = cdns_xfer->cmds[i].rx_len;
+	}
 
 	cdns_i3c_master_free_xfer(cdns_xfer);
 
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index dadf049bd4b5..f74c02fab9e3 100644
@@ -509,7 +509,7 @@ static int i3c_hci_i3c_xfers(struct i3c_dev_desc *dev,
 		goto out;
 	for (i = 0; i < nxfers; i++) {
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = RESP_DATA_LENGTH(xfer[i].response);
+			i3c_xfers[i].actual_len = RESP_DATA_LENGTH(xfer[i].response);
 		if (RESP_STATUS(xfer[i].response) != RESP_SUCCESS) {
 			ret = -EIO;
 			goto out;
drivers/i3c/master/renesas-i3c.c +3 -0
index 28c0927a0179..a3dcb4cd9b0e 100644
@@ -918,6 +918,9 @@ static int renesas_i3c_i3c_xfers(struct i3c_dev_desc *dev, struct i3c_xfer *i3c_
 		time_left = renesas_i3c_wait_xfer(i3c, xfer);
 		if (!time_left)
 			xfer_failed = true;
+
+		if (i3c_xfers[i].rnw)
+			i3c_xfers[i].actual_len = cmd->rx_count;
 	}
 
 	if (xfer_failed)
drivers/net/mctp/mctp-i3c.c +5 -5
index 6d2bbae7477b..5f94c11a1712 100644
@@ -131,12 +131,12 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	if (rc < 0)
 		goto err;
 
-	if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
+	if (WARN_ON_ONCE(xfer.actual_len > mi->mrl)) {
 		/* Bad i3c bus driver */
 		rc = -EIO;
 		goto err;
 	}
-	if (xfer.len < MCTP_I3C_MINLEN) {
+	if (xfer.actual_len < MCTP_I3C_MINLEN) {
 		stats->rx_length_errors++;
 		rc = -EIO;
 		goto err;
@@ -145,15 +145,15 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	/* check PEC, including address byte */
 	addr = mi->addr << 1 | 1;
 	pec = i2c_smbus_pec(0, &addr, 1);
-	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
-	if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
+	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.actual_len - 1);
+	if (pec != ((u8 *)xfer.data.in)[xfer.actual_len - 1]) {
 		stats->rx_crc_errors++;
 		rc = -EINVAL;
 		goto err;
 	}
 
 	/* Remove PEC */
-	skb_trim(skb, xfer.len - 1);
+	skb_trim(skb, xfer.actual_len - 1);
 
 	cb = __mctp_cb(skb);
 	cb->halen = PID_SIZE;
include/linux/i3c/device.h +1 -1
index 971d53349b6f..f868a7bf2bc9 100644
@@ -59,7 +59,7 @@ enum i3c_xfer_mode {
  * @rnw: encodes the transfer direction. true for a read, false for a write
  * @cmd: Read/Write command in HDR mode, read: 0x80 - 0xff, write: 0x00 - 0x7f
  * @len: transfer length in bytes of the transfer
- * @actual_len: actual length in bytes are transferred by the controller
+ * @actual_len: actual length in bytes transferred by the controller on read
  * @data: input/output buffer
  * @data.in: input buffer. Must point to a DMA-able buffer
  * @data.out: output buffer. Must point to a DMA-able buffer
Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#4 lore raw

Re: [PATCH v5 3/5] i3c: use actual_len for read transfers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] net/mctp: mctp_i3c_read() uses xfer.len instead of xfer.actual_len for stats --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] net/mctp: mctp_i3c_read() uses xfer.len instead of xfer.actual_len for stats --

commit ff8e6e542d5759da3d1770a07040b513a924c331
Author: Sam Agazaryan <[email protected]>
i3c: use actual_len for read transfers

This commit unifies read length reporting across the I3C subsystem by using actual_len for reads. It updates several controller drivers and the mctp-i3c driver to use actual_len without mutating len.

Quoted 4 lines
drivers/net/mctp/mctp-i3c.c +0 -0
index 4e857dd5df642..5324385be8c01 100644

[ ... ]

Quoted 9 lines
drivers/net/mctp/mctp-i3c.c +2 -2
@@ -145,15 +145,15 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	/* check PEC, including address byte */
 	addr = mi->addr << 1 | 1;
 	pec = i2c_smbus_pec(0, &addr, 1);
-	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
-	if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
+	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.actual_len - 1);
+	if (pec != ((u8 *)xfer.data.in)[xfer.actual_len - 1]) {
 		stats->rx_crc_errors++;

drivers/net/mctp/mctp-i3c.c static int mctp_i3c_read(struct mctp_i3c_device *mi) · line 159

Quoted 10 lines
drivers/net/mctp/mctp-i3c.c +1 -1
 		rc = -EINVAL;
 		goto err;
 	}
 
 	/* Remove PEC */
-	skb_trim(skb, xfer.len - 1);
+	skb_trim(skb, xfer.actual_len - 1);
 
 	cb = __mctp_cb(skb);
 	cb->halen = PID_SIZE;

[Severity: Medium]
Does mctp_i3c_read() also need to use xfer.actual_len when updating the network statistics?

A few lines below this hunk, the code still updates the rx_bytes counter using the unmutated xfer.len:

	if (net_status == NET_RX_SUCCESS) {
		stats->rx_packets++;
		stats->rx_bytes += xfer.len - 1;
	} else {
		stats->rx_dropped++;
	}

Will this inflate the rx_bytes counter by the maximum requested read length (mi->mrl) rather than the actual received bytes?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3

-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#5 lore raw

Re: [PATCH v5 3/5] i3c: use actual_len for read transfers

In reply to Sam Agazaryan
Quoted 13 lines · Sam Agazaryan

Currently only the amd and svc I3C controller drivers populate i3c_xfer.actual_len on reads, while dw-i3c-master and mipi-i3c-hci overwrite i3c_xfer.len with the received byte count and leave actual_len as 0. Because of this inconsistency, mctp-i3c reads xfer.len instead of xfer.actual_len, making it fail on controllers that do not mutate len.

Unify read length reporting across the subsystem:
- Clarify in <linux/i3c/device.h> that actual_len reflects the actual number of bytes transferred on reads.
- Update adi, cdns, dw, mipi-i3c-hci, and renesas controller drivers to populate actual_len on read transfers without mutating len.
- Update mctp-i3c to read xfer.actual_len instead of xfer.len.

Quoted 3 lines

Looks good.

1 issue from Sashiko:

	https://sashiko.dev/#/patchset/20260921230603.2518652-1-samagazaryan%40google.com?part=3

Also i3c_dev_do_xfers_locked() should probably clamp actual_len to len.

Quoted 135 lines
---
 drivers/i3c/master/adi-i3c-master.c    |  5 ++++-
 drivers/i3c/master/dw-i3c-master.c     |  2 +-
 drivers/i3c/master/i3c-master-cdns.c   |  5 ++++-
 drivers/i3c/master/mipi-i3c-hci/core.c |  2 +-
 drivers/i3c/master/renesas-i3c.c       |  3 +++
 drivers/net/mctp/mctp-i3c.c            | 10 +++++-----
 include/linux/i3c/device.h             |  2 +-
 7 files changed, 19 insertions(+), 10 deletions(-)
drivers/i3c/master/adi-i3c-master.c +4 -1
index b35386260350..d033bfd4ecc2 100644
@@ -412,8 +412,11 @@ static int adi_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = adi_i3c_cmd_get_err(&xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = xfer->cmds[i].rx_len;
+	}
 
 	return ret;
 }
drivers/i3c/master/dw-i3c-master.c +1 -1
index 4563d8761ba0..28e8eb293747 100644
@@ -1029,7 +1029,7 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
 
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = cmd->rx_len;
+			i3c_xfers[i].actual_len = cmd->rx_len;
 	}
 
 	ret = xfer->ret;
drivers/i3c/master/i3c-master-cdns.c +4 -1
index 2d98c1ce9b12..331062c10b31 100644
@@ -795,8 +795,11 @@ static int cdns_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = cdns_xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = cdns_i3c_cmd_get_err(&cdns_xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = cdns_xfer->cmds[i].rx_len;
+	}
 
 	cdns_i3c_master_free_xfer(cdns_xfer);
 
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index dadf049bd4b5..f74c02fab9e3 100644
@@ -509,7 +509,7 @@ static int i3c_hci_i3c_xfers(struct i3c_dev_desc *dev,
 		goto out;
 	for (i = 0; i < nxfers; i++) {
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = RESP_DATA_LENGTH(xfer[i].response);
+			i3c_xfers[i].actual_len = RESP_DATA_LENGTH(xfer[i].response);
 		if (RESP_STATUS(xfer[i].response) != RESP_SUCCESS) {
 			ret = -EIO;
 			goto out;
drivers/i3c/master/renesas-i3c.c +3 -0
index 28c0927a0179..a3dcb4cd9b0e 100644
@@ -918,6 +918,9 @@ static int renesas_i3c_i3c_xfers(struct i3c_dev_desc *dev, struct i3c_xfer *i3c_
 		time_left = renesas_i3c_wait_xfer(i3c, xfer);
 		if (!time_left)
 			xfer_failed = true;
+
+		if (i3c_xfers[i].rnw)
+			i3c_xfers[i].actual_len = cmd->rx_count;
 	}
 
 	if (xfer_failed)
drivers/net/mctp/mctp-i3c.c +5 -5
index 6d2bbae7477b..5f94c11a1712 100644
@@ -131,12 +131,12 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	if (rc < 0)
 		goto err;
 
-	if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
+	if (WARN_ON_ONCE(xfer.actual_len > mi->mrl)) {
 		/* Bad i3c bus driver */
 		rc = -EIO;
 		goto err;
 	}
-	if (xfer.len < MCTP_I3C_MINLEN) {
+	if (xfer.actual_len < MCTP_I3C_MINLEN) {
 		stats->rx_length_errors++;
 		rc = -EIO;
 		goto err;
@@ -145,15 +145,15 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	/* check PEC, including address byte */
 	addr = mi->addr << 1 | 1;
 	pec = i2c_smbus_pec(0, &addr, 1);
-	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
-	if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
+	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.actual_len - 1);
+	if (pec != ((u8 *)xfer.data.in)[xfer.actual_len - 1]) {
 		stats->rx_crc_errors++;
 		rc = -EINVAL;
 		goto err;
 	}
 
 	/* Remove PEC */
-	skb_trim(skb, xfer.len - 1);
+	skb_trim(skb, xfer.actual_len - 1);
 
 	cb = __mctp_cb(skb);
 	cb->halen = PID_SIZE;
include/linux/i3c/device.h +1 -1
index 971d53349b6f..f868a7bf2bc9 100644
@@ -59,7 +59,7 @@ enum i3c_xfer_mode {
  * @rnw: encodes the transfer direction. true for a read, false for a write
  * @cmd: Read/Write command in HDR mode, read: 0x80 - 0xff, write: 0x00 - 0x7f
  * @len: transfer length in bytes of the transfer
- * @actual_len: actual length in bytes are transferred by the controller
+ * @actual_len: actual length in bytes transferred by the controller on read
  * @data: input/output buffer
  * @data.in: input buffer. Must point to a DMA-able buffer
  * @data.out: output buffer. Must point to a DMA-able buffer
#6 lore raw

Re: [PATCH v5 3/5] i3c: use actual_len for read transfers

In reply to Sam Agazaryan
Quoted 16 lines · Sam Agazaryan

Currently only the amd and svc I3C controller drivers populate i3c_xfer.actual_len on reads, while dw-i3c-master and mipi-i3c-hci overwrite i3c_xfer.len with the received byte count and leave actual_len as 0. Because of this inconsistency, mctp-i3c reads xfer.len instead of xfer.actual_len, making it fail on controllers that do not mutate len.

Unify read length reporting across the subsystem:
- Clarify in <linux/i3c/device.h> that actual_len reflects the actual number of bytes transferred on reads.
- Update adi, cdns, dw, mipi-i3c-hci, and renesas controller drivers to populate actual_len on read transfers without mutating len.
- Update mctp-i3c to read xfer.actual_len instead of xfer.len.

Quoted 7 lines
---
 drivers/i3c/master/adi-i3c-master.c    |  5 ++++-
 drivers/i3c/master/dw-i3c-master.c     |  2 +-
 drivers/i3c/master/i3c-master-cdns.c   |  5 ++++-
 drivers/i3c/master/mipi-i3c-hci/core.c |  2 +-
 drivers/i3c/master/renesas-i3c.c       |  3 +++
 drivers/net/mctp/mctp-i3c.c            | 10 +++++-----

There are not dependence, suppose mctp-i3c.c change go through other tree It'd better use sperate patch to fix it.

Need fix tag.

Frank

Quoted 131 lines
 include/linux/i3c/device.h             |  2 +-
 7 files changed, 19 insertions(+), 10 deletions(-)
drivers/i3c/master/adi-i3c-master.c +4 -1
index b35386260350..d033bfd4ecc2 100644
@@ -412,8 +412,11 @@ static int adi_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = adi_i3c_cmd_get_err(&xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = xfer->cmds[i].rx_len;
+	}
 
 	return ret;
 }
drivers/i3c/master/dw-i3c-master.c +1 -1
index 4563d8761ba0..28e8eb293747 100644
@@ -1029,7 +1029,7 @@ static int dw_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 		struct dw_i3c_cmd *cmd = &xfer->cmds[i];
 
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = cmd->rx_len;
+			i3c_xfers[i].actual_len = cmd->rx_len;
 	}
 
 	ret = xfer->ret;
drivers/i3c/master/i3c-master-cdns.c +4 -1
index 2d98c1ce9b12..331062c10b31 100644
@@ -795,8 +795,11 @@ static int cdns_i3c_master_i3c_xfers(struct i3c_dev_desc *dev,
 
 	ret = cdns_xfer->ret;
 
-	for (i = 0; i < nxfers; i++)
+	for (i = 0; i < nxfers; i++) {
 		xfers[i].err = cdns_i3c_cmd_get_err(&cdns_xfer->cmds[i]);
+		if (xfers[i].rnw)
+			xfers[i].actual_len = cdns_xfer->cmds[i].rx_len;
+	}
 
 	cdns_i3c_master_free_xfer(cdns_xfer);
 
drivers/i3c/master/mipi-i3c-hci/core.c +1 -1
index dadf049bd4b5..f74c02fab9e3 100644
@@ -509,7 +509,7 @@ static int i3c_hci_i3c_xfers(struct i3c_dev_desc *dev,
 		goto out;
 	for (i = 0; i < nxfers; i++) {
 		if (i3c_xfers[i].rnw)
-			i3c_xfers[i].len = RESP_DATA_LENGTH(xfer[i].response);
+			i3c_xfers[i].actual_len = RESP_DATA_LENGTH(xfer[i].response);
 		if (RESP_STATUS(xfer[i].response) != RESP_SUCCESS) {
 			ret = -EIO;
 			goto out;
drivers/i3c/master/renesas-i3c.c +3 -0
index 28c0927a0179..a3dcb4cd9b0e 100644
@@ -918,6 +918,9 @@ static int renesas_i3c_i3c_xfers(struct i3c_dev_desc *dev, struct i3c_xfer *i3c_
 		time_left = renesas_i3c_wait_xfer(i3c, xfer);
 		if (!time_left)
 			xfer_failed = true;
+
+		if (i3c_xfers[i].rnw)
+			i3c_xfers[i].actual_len = cmd->rx_count;
 	}
 
 	if (xfer_failed)
drivers/net/mctp/mctp-i3c.c +5 -5
index 6d2bbae7477b..5f94c11a1712 100644
@@ -131,12 +131,12 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	if (rc < 0)
 		goto err;
 
-	if (WARN_ON_ONCE(xfer.len > mi->mrl)) {
+	if (WARN_ON_ONCE(xfer.actual_len > mi->mrl)) {
 		/* Bad i3c bus driver */
 		rc = -EIO;
 		goto err;
 	}
-	if (xfer.len < MCTP_I3C_MINLEN) {
+	if (xfer.actual_len < MCTP_I3C_MINLEN) {
 		stats->rx_length_errors++;
 		rc = -EIO;
 		goto err;
@@ -145,15 +145,15 @@ static int mctp_i3c_read(struct mctp_i3c_device *mi)
 	/* check PEC, including address byte */
 	addr = mi->addr << 1 | 1;
 	pec = i2c_smbus_pec(0, &addr, 1);
-	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.len - 1);
-	if (pec != ((u8 *)xfer.data.in)[xfer.len - 1]) {
+	pec = i2c_smbus_pec(pec, xfer.data.in, xfer.actual_len - 1);
+	if (pec != ((u8 *)xfer.data.in)[xfer.actual_len - 1]) {
 		stats->rx_crc_errors++;
 		rc = -EINVAL;
 		goto err;
 	}
 
 	/* Remove PEC */
-	skb_trim(skb, xfer.len - 1);
+	skb_trim(skb, xfer.actual_len - 1);
 
 	cb = __mctp_cb(skb);
 	cb->halen = PID_SIZE;
include/linux/i3c/device.h +1 -2
index 971d53349b6f..f868a7bf2bc9 100644
@@ -59,7 +59,7 @@ enum i3c_xfer_mode {
  * @rnw: encodes the transfer direction. true for a read, false for a write
  * @cmd: Read/Write command in HDR mode, read: 0x80 - 0xff, write: 0x00 - 0x7f
  * @len: transfer length in bytes of the transfer
- * @actual_len: actual length in bytes are transferred by the controller
+ * @actual_len: actual length in bytes transferred by the controller on read
  * @data: input/output buffer
  * @data.in: input buffer. Must point to a DMA-able buffer
  * @data.out: output buffer. Must point to a DMA-able buffer
--

2.55.0.1082.g2b9226bbc0-goog

#7 lore raw

Re: [PATCH v5 3/5] i3c: use actual_len for read transfers

Quoted 25 lines · Frank Li
Quoted 16 lines · Sam Agazaryan

Currently only the amd and svc I3C controller drivers populate i3c_xfer.actual_len on reads, while dw-i3c-master and mipi-i3c-hci overwrite i3c_xfer.len with the received byte count and leave actual_len as 0. Because of this inconsistency, mctp-i3c reads xfer.len instead of xfer.actual_len, making it fail on controllers that do not mutate len.

Unify read length reporting across the subsystem:
- Clarify in <linux/i3c/device.h> that actual_len reflects the actual number of bytes transferred on reads.
- Update adi, cdns, dw, mipi-i3c-hci, and renesas controller drivers to populate actual_len on read transfers without mutating len.
- Update mctp-i3c to read xfer.actual_len instead of xfer.len.

Quoted 7 lines
---
 drivers/i3c/master/adi-i3c-master.c    |  5 ++++-
 drivers/i3c/master/dw-i3c-master.c     |  2 +-
 drivers/i3c/master/i3c-master-cdns.c   |  5 ++++-
 drivers/i3c/master/mipi-i3c-hci/core.c |  2 +-
 drivers/i3c/master/renesas-i3c.c       |  3 +++
 drivers/net/mctp/mctp-i3c.c            | 10 +++++-----
Quoted 2 lines

There are not dependence, suppose mctp-i3c.c change go through other tree It'd better use sperate patch to fix it.

Isn't mctp-i3c.c dependent on the len -> actual_len change in any of the master drivers?

#8 lore raw

Re: [PATCH v5 3/5] i3c: use actual_len for read transfers

Quoted 1 line

Also i3c_dev_do_xfers_locked() should probably clamp actual_len to len.

Done - fixed the missing rx_bytes line and clamped actual_len to len in i3c_dev_do_xfers_locked() in v6.

Quoted 7 lines
Quoted 4 lines

There are not dependence, suppose mctp-i3c.c change go through other tree It'd better use sperate patch to fix it.

Need fix tag.

Isn't mctp-i3c.c dependent on the len -> actual_len change in any of the master drivers?

Yeah that was my reasoning for adding it here since we're converting all drivers to stop overwriting xfer.len.

Also, will add a Fixes tag for v6.

Thanks,
Sam

#9 lore raw

PATCH v5 4/5 i3c: add i3cdev module to expose i3c dev in /dev

In reply to Sam Agazaryan

From: Vitor Soares <[email protected]>

Add userspace character device support for I3C transfers via /dev.

The module allows userspace programs to interact directly with I3C targets that do not have a kernel driver bound to them, such as devices in ROM/bootloader recovery mode (e.g. OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows).

Features:
- Dynamically exposes /dev/bus/i3c/<device> character devices for I3C devices when unbound from kernel drivers.
- Dynamically allocates character device minor numbers using the IDA allocator.
- Implements SDR and HDR transfers via I3C_IOC_XFER ioctl with 64-bit aligned UAPI data structures and actual_len read reporting.
- Supports compat_ptr_ioctl for 32-bit userspace on 64-bit kernels.
- Uses cdev_device_add/cdev_device_del with device refcounting to ensure safe lifecycle management and prevent use-after-free on driver detach.

---
 MAINTAINERS                     |   1 +
 drivers/i3c/Kconfig             |  11 +
 drivers/i3c/Makefile            |   1 +
 drivers/i3c/i3cdev.c            | 491 ++++++++++++++++++++++++++++++++
 include/uapi/linux/i3c/i3cdev.h |  57 ++++
 5 files changed, 561 insertions(+)
 create mode 100644 drivers/i3c/i3cdev.c
 create mode 100644 include/uapi/linux/i3c/i3cdev.h
MAINTAINERS +1 -0
index 81a9a02c919d..30a5cb12c4f0 100644
@@ -12364,6 +12364,7 @@ F:	Documentation/driver-api/i3c
 F:	drivers/i3c/
 F:	include/dt-bindings/i3c/
 F:	include/linux/i3c/
+F:	include/uapi/linux/i3c/
 
 IBM Operation Panel Input Driver
 M:	Eddie James <[email protected]>
drivers/i3c/Kconfig +11 -0
index 626c54b386d5..166875837ec6 100644
@@ -20,6 +20,17 @@ menuconfig I3C
 	  will be called i3c.
 
 if I3C
+
+config I3CDEV
+	tristate "I3C device interface"
+	help
+	  Say Y here to use i3c-* device files, usually found in the /dev
+	  directory on your system.  They make it possible to have user-space
+	  programs use the I3C devices.
+
+	  This support is also available as a module.  If so, the module
+	  will be called i3cdev.
+
 source "drivers/i3c/master/Kconfig"
 endif # I3C
 
drivers/i3c/Makefile +1 -0
index 11982efbc6d9..606d422841b2 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 i3c-y				:= device.o master.o
 obj-$(CONFIG_I3C)		+= i3c.o
+obj-$(CONFIG_I3CDEV)		+= i3cdev.o
 obj-$(CONFIG_I3C)		+= master/
drivers/i3c/i3cdev.c new file +491 -0

491 lines not on this page · raw message

include/uapi/linux/i3c/i3cdev.h new file +57 -0

57 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#10 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] i3c: out-of-bounds shift due to unvalidated transfer mode - [High] i3c: global DoS via userfaultfd…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] i3c: out-of-bounds shift due to unvalidated transfer mode
- [High] i3c: global DoS via userfaultfd stalling per-device xfer_lock
- [Medium] i3c: out-of-bounds read due to unverified actual_len in copy_to_user
- [Low] cleanup: mixing goto-based error handling with scope-based cleanup --

commit b5d90eba8091c7ed2bb03166ab238062306fafa4
Author: Vitor Soares <[email protected]>

i3c: add i3cdev module to expose i3c dev in /dev

Add userspace character device support for I3C transfers via /dev. This allows userspace programs to interact directly with I3C targets that do not have a kernel driver bound to them.

Quoted 6 lines
drivers/i3c/i3cdev.c new file +0 -0
new file mode 100644
index 0000000000000..309ce81812096

drivers/i3c/i3cdev.c line 119

Quoted 10 lines
drivers/i3c/i3cdev.c new file +8 -0
@@ -0,0 +1,491 @@
[ ... ]
+static ssize_t
+i3cdev_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
+{
[ ... ]
+	ret = i3c_device_do_xfers(i3c, &xfers, 1, I3C_SDR);
+	if (ret)
+		return ret;
+
+	if (copy_to_user(buf, tmp, xfers.actual_len))

[Severity: Medium]
Could this result in an out-of-bounds read to user space? If a buggy hardware driver returns an actual_len that is greater than the requested transfer len (or the allocated count), actual_len is used directly in copy_to_user() without being clamped to the dynamically allocated buffer size, which might leak kernel memory.

drivers/i3c/i3cdev.c line 164

Quoted 10 lines
drivers/i3c/i3cdev.c new file +9 -0
+		return -EFAULT;
+
+	return xfers.actual_len;
+}
[ ... ]
+static int
+i3cdev_do_xfer(struct i3c_device *dev, struct i3c_ioc_xfer *xfers,
+	       struct i3c_ioc_xfer __user *u_xfers, unsigned int nxfers)
+{
+	struct i3c_xfer *k_xfers __free(kfree) = NULL;

[Severity: Low]
Is it appropriate to mix scope-based and goto-based cleanup here? The subsystem guidelines indicate that using goto-based error handling alongside __free() in the same function creates confusing ownership semantics. In this function, we have __free(kfree) here and goto err_free_mem below.

drivers/i3c/i3cdev.c line 165

Quoted 1 line
drivers/i3c/i3cdev.c new file +1 -0
+	enum i3c_xfer_mode mode = xfers[0].mode;

[Severity: High]
Can this user-provided mode cause an out-of-bounds shift?

If a mode value greater than or equal to 64 is passed via the ioctl, it is extracted without validation and passed down:

i3cdev_do_xfer()
i3c_device_do_xfers(dev, k_xfers, nxfers, mode)
i3c_dev_do_xfers_locked()
BIT(mode)

Since BIT(mode) does a bitwise shift, this would trigger undefined behavior. Should this value be validated against valid I3C modes before proceeding?

Quoted 5 lines
drivers/i3c/i3cdev.c new file +4 -0
+	u8 **data_ptrs;
+	int i, j, nalloc, ret = 0;
[ ... ]
+	ret = i3c_device_do_xfers(dev, k_xfers, nxfers, mode);
+	if (ret)

drivers/i3c/i3cdev.c line 238

Quoted 10 lines
drivers/i3c/i3cdev.c new file +10 -0
+		goto err_free_mem;
+
+	for (i = 0; i < nxfers; i++) {
+		bool is_read = (mode == I3C_SDR) ?
+			       (xfers[i].rnw == I3C_DEV_DIR_READ) :
+			       (xfers[i].cmd & 0x80);
+
+		if (is_read) {
+			if (copy_to_user(u64_to_user_ptr(xfers[i].data),
+					 data_ptrs[i], k_xfers[i].actual_len) ||

[Severity: Medium]
Similar to i3cdev_read(), is actual_len verified here? An inflated actual_len from the hardware driver could result in an out-of-bounds read to user space without bounds clamping.

Quoted 15 lines
drivers/i3c/i3cdev.c new file +14 -0
+			    put_user(k_xfers[i].actual_len,
+				     &u_xfers[i].actual_len)) {
+				ret = -EFAULT;
+				break;
+			}
+		}
+	}
+
+err_free_mem:
+	for (j = 0; j < nalloc; j++)
+		kfree(data_ptrs[j]);
+	return ret;
+}
[ ... ]
+static long

drivers/i3c/i3cdev.c line 293

Quoted 10 lines
drivers/i3c/i3cdev.c new file +10 -0
+i3cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct i3cdev_data *i3cdev = file->private_data;
+	struct i3c_device *i3c;
+
+	if (_IOC_TYPE(cmd) != I3C_DEV_IOC_MAGIC)
+		return -ENOTTY;
+
+	/* Use the xfer_lock to prevent device detach during ioctl call */
+	guard(mutex)(&i3cdev->xfer_lock);

[Severity: High]
Could holding this per-device xfer_lock throughout the ioctl create a global DoS vulnerability?

The lock is held while calling sleepable user-memory access functions like memdup_user() and copy_to_user() in i3cdev_do_xfer(). A userfaultfd stall could freeze the thread here indefinitely.

Because i3cdev_detach() uses scoped_guard(mutex, &i3cdev->xfer_lock) while also being called under the global i3cdev_attach_lock, any detach event during this stall will block on xfer_lock, deadlocking the entire I3C subsystem's hotplug and notification framework globally.

Quoted 6 lines
drivers/i3c/i3cdev.c new file +5 -0
+	i3c = i3cdev->i3c;
+	if (!i3c || i3c->dev.driver)
+		return -ENODEV;
[ ... ]
+static int i3cdev_attach(struct device *dev, void *dummy)
+{

drivers/i3c/i3cdev.c line 353

Quoted 10 lines
drivers/i3c/i3cdev.c new file +10 -0
+	struct i3cdev_data *i3cdev;
+	struct i3c_device *i3c;
+	int res;
+
+	if (dev->type == &i3c_masterdev_type)
+		return 0;
+
+	i3c = dev_to_i3cdev(dev);
+
+	guard(mutex)(&i3cdev_attach_lock);

[Severity: Low]
Does this mix scope-based and goto-based cleanup? Like in i3cdev_do_xfer(), this function uses guard() here but later uses goto error_put_dev for error handling.

Quoted 3 lines
drivers/i3c/i3cdev.c new file +2 -0
+	if (dev->driver || i3cdev_get_by_i3c(i3c))
+		return 0;
[ ... ]
Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4

-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#11 lore raw
In reply to Sam Agazaryan
Quoted 177 lines · Sam Agazaryan

From: Vitor Soares <[email protected]>

Add userspace character device support for I3C transfers via /dev.

The module allows userspace programs to interact directly with I3C targets that do not have a kernel driver bound to them, such as devices in ROM/bootloader recovery mode (e.g. OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows).

Features:
- Dynamically exposes /dev/bus/i3c/<device> character devices for I3C devices when unbound from kernel drivers.
- Dynamically allocates character device minor numbers using the IDA allocator.
- Implements SDR and HDR transfers via I3C_IOC_XFER ioctl with 64-bit aligned UAPI data structures and actual_len read reporting.
- Supports compat_ptr_ioctl for 32-bit userspace on 64-bit kernels.
- Uses cdev_device_add/cdev_device_del with device refcounting to ensure safe lifecycle management and prevent use-after-free on driver detach.

---
 MAINTAINERS                     |   1 +
 drivers/i3c/Kconfig             |  11 +
 drivers/i3c/Makefile            |   1 +
 drivers/i3c/i3cdev.c            | 491 ++++++++++++++++++++++++++++++++
 include/uapi/linux/i3c/i3cdev.h |  57 ++++
 5 files changed, 561 insertions(+)
 create mode 100644 drivers/i3c/i3cdev.c
 create mode 100644 include/uapi/linux/i3c/i3cdev.h
MAINTAINERS +1 -0
index 81a9a02c919d..30a5cb12c4f0 100644
@@ -12364,6 +12364,7 @@ F:	Documentation/driver-api/i3c
 F:	drivers/i3c/
 F:	include/dt-bindings/i3c/
 F:	include/linux/i3c/
+F:	include/uapi/linux/i3c/
 
 IBM Operation Panel Input Driver
 M:	Eddie James <[email protected]>
drivers/i3c/Kconfig +11 -0
index 626c54b386d5..166875837ec6 100644
@@ -20,6 +20,17 @@ menuconfig I3C
 	  will be called i3c.
 
 if I3C
+
+config I3CDEV
+	tristate "I3C device interface"
+	help
+	  Say Y here to use i3c-* device files, usually found in the /dev
+	  directory on your system.  They make it possible to have user-space
+	  programs use the I3C devices.
+
+	  This support is also available as a module.  If so, the module
+	  will be called i3cdev.
+
 source "drivers/i3c/master/Kconfig"
 endif # I3C
 
drivers/i3c/Makefile +1 -0
index 11982efbc6d9..606d422841b2 100644
@@ -1,4 +1,5 @@
 # SPDX-License-Identifier: GPL-2.0
 i3c-y				:= device.o master.o
 obj-$(CONFIG_I3C)		+= i3c.o
+obj-$(CONFIG_I3CDEV)		+= i3cdev.o
 obj-$(CONFIG_I3C)		+= master/
drivers/i3c/i3cdev.c new file +92 -0
new file mode 100644
index 000000000000..309ce8181209
@@ -0,0 +1,491 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2020 Synopsys, Inc. and/or its affiliates.
+ * Copyright (c) 2026 Google LLC
+ *
+ * Author: Vitor Soares <[email protected]>
+ * Author: Sam Agazaryan <[email protected]>
+ */
+
+#include <linux/cdev.h>
+#include <linux/cleanup.h>
+#include <linux/compat.h>
+#include <linux/device.h>
+#include <linux/fs.h>
+#include <linux/init.h>
+#include <linux/jiffies.h>
+#include <linux/kernel.h>
+#include <linux/list.h>
+#include <linux/module.h>
+#include <linux/notifier.h>
+#include <linux/overflow.h>
+#include <linux/slab.h>
+#include <linux/uaccess.h>
+
+#include <linux/i3c/i3cdev.h>
+
+#include "internals.h"
+
+struct i3cdev_data {
+	struct list_head list;
+	struct i3c_device *i3c;
+	struct device dev;
+	struct mutex xfer_lock; /* prevent detach while transferring */
+	struct cdev cdev;
+	int id;
+};
+
+static DEFINE_IDA(i3cdev_ida);
+static LIST_HEAD(i3cdev_list);
+static DEFINE_MUTEX(i3cdev_attach_lock);
+static dev_t i3cdev_number;
+#define I3C_MINORS (MINORMASK + 1)
+
+static void i3cdev_dev_release(struct device *dev)
+{
+	struct i3cdev_data *i3cdev = container_of(dev, struct i3cdev_data, dev);
+
+	ida_free(&i3cdev_ida, i3cdev->id);
+	kfree(i3cdev);
+}
+
+static struct i3cdev_data *i3cdev_get_by_i3c(struct i3c_device *i3c)
+{
+	struct i3cdev_data *i3cdev;
+
+	list_for_each_entry(i3cdev, &i3cdev_list, list) {
+		if (i3cdev->i3c == i3c)
+			return i3cdev;
+	}
+
+	return NULL;
+}
+
+static struct i3cdev_data *get_free_i3cdev(struct i3c_device *i3c)
+{
+	struct i3cdev_data *i3cdev;
+	int id;
+
+	id = ida_alloc_max(&i3cdev_ida, MINORMASK, GFP_KERNEL);
+	if (id < 0) {
+		pr_err("i3cdev: no minor number available!\n");
+		return ERR_PTR(id);
+	}
+
+	i3cdev = kzalloc_obj(*i3cdev, GFP_KERNEL);
+	if (!i3cdev) {
+		ida_free(&i3cdev_ida, id);
+		return ERR_PTR(-ENOMEM);
+	}
+
+	i3cdev->i3c = i3c;
+	i3cdev->id = id;
+	list_add_tail(&i3cdev->list, &i3cdev_list);
+
+	return i3cdev;
+}
+
+static ssize_t
+i3cdev_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
+{
+	char *tmp __free(kfree) = NULL;
+	struct i3cdev_data *i3cdev = file->private_data;

drivers/i3c/i3cdev.c line 102

Quoted 10 lines
drivers/i3c/i3cdev.c new file +10 -0
+	struct i3c_xfer xfers = {
+		.rnw = true,
+	};
+	struct i3c_device *i3c;
+	int ret;
+
+	count = min_t(size_t, count, type_max(xfers.len));
+	xfers.len = count;
+
+	tmp = kzalloc(count, GFP_KERNEL);

cleanup.h prefer declear tmp here

	char *tmp __free(kfree) = kzalloc(count, GFP_KERNEL);

check others

Quoted 431 lines
drivers/i3c/i3cdev.c new file +389 -0
+	if (!tmp)
+		return -ENOMEM;
+
+	guard(mutex)(&i3cdev->xfer_lock);
+	i3c = i3cdev->i3c;
+	if (!i3c || i3c->dev.driver)
+		return -ENODEV;
+
+	xfers.data.in = tmp;
+
+	dev_dbg(&i3c->dev, "Reading %zu bytes.\n", count);
+
+	ret = i3c_device_do_xfers(i3c, &xfers, 1, I3C_SDR);
+	if (ret)
+		return ret;
+
+	if (copy_to_user(buf, tmp, xfers.actual_len))
+		return -EFAULT;
+
+	return xfers.actual_len;
+}
+
+static ssize_t
+i3cdev_write(struct file *file, const char __user *buf, size_t count,
+	     loff_t *f_pos)
+{
+	void *tmp __free(kfree) = NULL;
+	struct i3cdev_data *i3cdev = file->private_data;
+	struct i3c_xfer xfers = {
+		.rnw = false,
+	};
+	struct i3c_device *i3c;
+	int ret;
+
+	count = min_t(size_t, count, type_max(xfers.len));
+	xfers.len = count;
+
+	tmp = memdup_user(buf, count);
+	if (IS_ERR(tmp))
+		return PTR_ERR(tmp);
+
+	guard(mutex)(&i3cdev->xfer_lock);
+	i3c = i3cdev->i3c;
+	if (!i3c || i3c->dev.driver)
+		return -ENODEV;
+
+	xfers.data.out = tmp;
+
+	dev_dbg(&i3c->dev, "Writing %zu bytes.\n", count);
+
+	ret = i3c_device_do_xfers(i3c, &xfers, 1, I3C_SDR);
+	if (ret)
+		return ret;
+
+	return count;
+}
+
+static int
+i3cdev_do_xfer(struct i3c_device *dev, struct i3c_ioc_xfer *xfers,
+	       struct i3c_ioc_xfer __user *u_xfers, unsigned int nxfers)
+{
+	struct i3c_xfer *k_xfers __free(kfree) = NULL;
+	enum i3c_xfer_mode mode = xfers[0].mode;
+	u8 **data_ptrs;
+	int i, j, nalloc, ret = 0;
+
+	/* Since we have nxfers we may allocate k_xfer + *data_ptrs together */
+	k_xfers = kcalloc(nxfers, sizeof(*k_xfers) + sizeof(*data_ptrs),
+			  GFP_KERNEL);
+	if (!k_xfers)
+		return -ENOMEM;
+
+	/* set data_ptrs to be after nxfers * i3c_xfer */
+	data_ptrs = (void *)k_xfers + (nxfers * sizeof(*k_xfers));
+
+	for (i = 0; i < nxfers; i++) {
+		bool is_read;
+
+		if (xfers[i].mode != mode) {
+			ret = -EINVAL;
+			break;
+		}
+
+		if (memchr_inv(xfers[i].pad, 0, sizeof(xfers[i].pad))) {
+			ret = -EINVAL;
+			break;
+		}
+
+		if (mode == I3C_SDR) {
+			if (xfers[i].rnw != I3C_DEV_DIR_WRITE &&
+			    xfers[i].rnw != I3C_DEV_DIR_READ) {
+				ret = -EINVAL;
+				break;
+			}
+			is_read = xfers[i].rnw == I3C_DEV_DIR_READ;
+		} else {
+			is_read = xfers[i].cmd & 0x80;
+		}
+
+		if (is_read) {
+			data_ptrs[i] = kzalloc(xfers[i].len, GFP_KERNEL);
+			if (!data_ptrs[i]) {
+				ret = -ENOMEM;
+				break;
+			}
+			k_xfers[i].data.in = data_ptrs[i];
+		} else {
+			data_ptrs[i] = memdup_user(u64_to_user_ptr(xfers[i].data),
+						   xfers[i].len);
+			if (IS_ERR(data_ptrs[i])) {
+				ret = PTR_ERR(data_ptrs[i]);
+				break;
+			}
+			k_xfers[i].data.out = data_ptrs[i];
+		}
+
+		k_xfers[i].cmd = xfers[i].cmd;
+		k_xfers[i].len = xfers[i].len;
+	}
+	nalloc = i;
+
+	if (ret < 0)
+		goto err_free_mem;
+
+	ret = i3c_device_do_xfers(dev, k_xfers, nxfers, mode);
+	if (ret)
+		goto err_free_mem;
+
+	for (i = 0; i < nxfers; i++) {
+		bool is_read = (mode == I3C_SDR) ?
+			       (xfers[i].rnw == I3C_DEV_DIR_READ) :
+			       (xfers[i].cmd & 0x80);
+
+		if (is_read) {
+			if (copy_to_user(u64_to_user_ptr(xfers[i].data),
+					 data_ptrs[i], k_xfers[i].actual_len) ||
+			    put_user(k_xfers[i].actual_len,
+				     &u_xfers[i].actual_len)) {
+				ret = -EFAULT;
+				break;
+			}
+		}
+	}
+
+err_free_mem:
+	for (j = 0; j < nalloc; j++)
+		kfree(data_ptrs[j]);
+	return ret;
+}
+
+static struct i3c_ioc_xfer *
+i3cdev_get_ioc_xfer(unsigned int cmd, struct i3c_ioc_xfer __user *u_xfers,
+		    unsigned int *nxfers)
+{
+	u32 tmp = _IOC_SIZE(cmd);
+
+	if ((tmp % sizeof(struct i3c_ioc_xfer)) != 0)
+		return ERR_PTR(-EINVAL);
+
+	*nxfers = tmp / sizeof(struct i3c_ioc_xfer);
+	if (*nxfers == 0)
+		return ERR_PTR(-EINVAL);
+
+	return memdup_user(u_xfers, tmp);
+}
+
+static int
+i3cdev_ioc_xfer(struct i3c_device *i3c, unsigned int cmd,
+		struct i3c_ioc_xfer __user *u_xfers)
+{
+	struct i3c_ioc_xfer *k_xfers __free(kfree) = NULL;
+	unsigned int nxfers;
+
+	k_xfers = i3cdev_get_ioc_xfer(cmd, u_xfers, &nxfers);
+	if (IS_ERR(k_xfers))
+		return PTR_ERR(k_xfers);
+
+	return i3cdev_do_xfer(i3c, k_xfers, u_xfers, nxfers);
+}
+
+static long
+i3cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+	struct i3cdev_data *i3cdev = file->private_data;
+	struct i3c_device *i3c;
+
+	if (_IOC_TYPE(cmd) != I3C_DEV_IOC_MAGIC)
+		return -ENOTTY;
+
+	/* Use the xfer_lock to prevent device detach during ioctl call */
+	guard(mutex)(&i3cdev->xfer_lock);
+	i3c = i3cdev->i3c;
+	if (!i3c || i3c->dev.driver)
+		return -ENODEV;
+
+	dev_dbg(&i3c->dev, "ioctl, cmd=0x%02x, arg=0x%02lx\n", cmd, arg);
+
+	/* Check command number and direction */
+	if (_IOC_NR(cmd) == _IOC_NR(I3C_IOC_XFER(0)) &&
+	    _IOC_DIR(cmd) == (_IOC_READ | _IOC_WRITE))
+		return i3cdev_ioc_xfer(i3c, cmd,
+				       (struct i3c_ioc_xfer __user *)arg);
+
+	return -ENOTTY;
+}
+
+static int i3cdev_open(struct inode *inode, struct file *file)
+{
+	struct i3cdev_data *i3cdev = container_of(inode->i_cdev,
+						  struct i3cdev_data,
+						  cdev);
+	file->private_data = i3cdev;
+
+	return 0;
+}
+
+static int i3cdev_release(struct inode *inode, struct file *file)
+{
+	file->private_data = NULL;
+
+	return 0;
+}
+
+static const struct file_operations i3cdev_fops = {
+	.owner		= THIS_MODULE,
+	.read		= i3cdev_read,
+	.write		= i3cdev_write,
+	.unlocked_ioctl	= i3cdev_ioctl,
+	.compat_ioctl	= compat_ptr_ioctl,
+	.open		= i3cdev_open,
+	.release	= i3cdev_release,
+};
+
+/* ------------------------------------------------------------------------- */
+
+static const struct class i3cdev_class = {
+	.name = "i3cdev",
+};
+
+static int i3cdev_attach(struct device *dev, void *dummy)
+{
+	struct i3cdev_data *i3cdev;
+	struct i3c_device *i3c;
+	int res;
+
+	if (dev->type == &i3c_masterdev_type)
+		return 0;
+
+	i3c = dev_to_i3cdev(dev);
+
+	guard(mutex)(&i3cdev_attach_lock);
+	if (dev->driver || i3cdev_get_by_i3c(i3c))
+		return 0;
+
+	/* Get a device */
+	i3cdev = get_free_i3cdev(i3c);
+	if (IS_ERR(i3cdev))
+		return PTR_ERR(i3cdev);
+
+	mutex_init(&i3cdev->xfer_lock);
+	cdev_init(&i3cdev->cdev, &i3cdev_fops);
+	i3cdev->cdev.owner = THIS_MODULE;
+
+	device_initialize(&i3cdev->dev);
+	i3cdev->dev.devt = MKDEV(MAJOR(i3cdev_number), i3cdev->id);
+	i3cdev->dev.class = &i3cdev_class;
+	i3cdev->dev.parent = &i3c->dev;
+	i3cdev->dev.release = i3cdev_dev_release;
+
+	res = dev_set_name(&i3cdev->dev, "bus!i3c!%s", dev_name(&i3c->dev));
+	if (res)
+		goto error_put_dev;
+
+	res = cdev_device_add(&i3cdev->cdev, &i3cdev->dev);
+	if (res)
+		goto error_put_dev;
+
+	pr_debug("i3cdev: I3C device [%s] registered as minor %d\n",
+		 dev_name(&i3c->dev), i3cdev->id);
+	return 0;
+
+error_put_dev:
+	list_del(&i3cdev->list);
+	put_device(&i3cdev->dev);
+	return res;
+}
+
+static int i3cdev_detach(struct device *dev, void *dummy)
+{
+	struct i3cdev_data *i3cdev;
+	struct i3c_device *i3c;
+
+	if (dev->type == &i3c_masterdev_type)
+		return 0;
+
+	i3c = dev_to_i3cdev(dev);
+
+	guard(mutex)(&i3cdev_attach_lock);
+	i3cdev = i3cdev_get_by_i3c(i3c);
+	if (!i3cdev)
+		return 0;
+
+	list_del(&i3cdev->list);
+
+	/* Prevent transfers while cdev removal */
+	scoped_guard(mutex, &i3cdev->xfer_lock)
+		i3cdev->i3c = NULL;
+
+	cdev_device_del(&i3cdev->cdev, &i3cdev->dev);
+	put_device(&i3cdev->dev);
+
+	pr_debug("i3cdev: device [%s] unregistered\n", dev_name(&i3c->dev));
+
+	return 0;
+}
+
+static int i3cdev_notifier_call(struct notifier_block *nb,
+				unsigned long action,
+				void *data)
+{
+	struct device *dev = data;
+
+	switch (action) {
+	case BUS_NOTIFY_ADD_DEVICE:
+	case BUS_NOTIFY_UNBOUND_DRIVER:
+	case BUS_NOTIFY_DRIVER_NOT_BOUND:
+		i3cdev_attach(dev, NULL);
+		break;
+	case BUS_NOTIFY_DEL_DEVICE:
+	case BUS_NOTIFY_REMOVED_DEVICE:
+	case BUS_NOTIFY_BIND_DRIVER:
+		i3cdev_detach(dev, NULL);
+		break;
+	}
+
+	return NOTIFY_OK;
+}
+
+static struct notifier_block i3cdev_notifier = {
+	.notifier_call = i3cdev_notifier_call,
+};
+
+static int __init i3cdev_init(void)
+{
+	int res;
+
+	/* Dynamically request unused major number */
+	res = alloc_chrdev_region(&i3cdev_number, 0, I3C_MINORS, "i3c");
+	if (res)
+		goto out;
+
+	/* Register device class to populate sysfs entries */
+	res = class_register(&i3cdev_class);
+	if (res)
+		goto out_unreg_chrdev;
+
+	/* Keep track of busses which have devices to add or remove later */
+	res = bus_register_notifier(&i3c_bus_type, &i3cdev_notifier);
+	if (res)
+		goto out_unreg_class;
+
+	/* Bind to already existing device without driver right away */
+	i3c_for_each_dev(NULL, i3cdev_attach);
+
+	return 0;
+
+out_unreg_class:
+	class_unregister(&i3cdev_class);
+out_unreg_chrdev:
+	unregister_chrdev_region(i3cdev_number, I3C_MINORS);
+out:
+	pr_err("%s: Driver Initialisation failed\n", __FILE__);
+	return res;
+}
+
+static void __exit i3cdev_exit(void)
+{
+	bus_unregister_notifier(&i3c_bus_type, &i3cdev_notifier);
+	i3c_for_each_dev(NULL, i3cdev_detach);
+	class_unregister(&i3cdev_class);
+	unregister_chrdev_region(i3cdev_number, I3C_MINORS);
+}
+
+MODULE_AUTHOR("Vitor Soares <[email protected]>");
+MODULE_DESCRIPTION("I3C /dev entries driver");
+MODULE_LICENSE("GPL");
+
+module_init(i3cdev_init);
+module_exit(i3cdev_exit);
include/uapi/linux/i3c/i3cdev.h new file +36 -0
new file mode 100644
index 000000000000..69e901017378
@@ -0,0 +1,57 @@
+/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
+/*
+ * Copyright (c) 2020 Synopsys, Inc. and/or its affiliates.
+ * Copyright (c) 2026 Google LLC
+ *
+ * Author: Vitor Soares <[email protected]>
+ */
+
+#ifndef _UAPI_I3C_DEV_H_
+#define _UAPI_I3C_DEV_H_
+
+#include <linux/types.h>
+#include <linux/ioctl.h>
+
+/* IOCTL commands */
+#define I3C_DEV_IOC_MAGIC	0x07
+
+#define I3C_DEV_DIR_WRITE	0
+#define I3C_DEV_DIR_READ	1
+
+#define I3C_XFER_MODE_HDR_DDR	0
+#define I3C_XFER_MODE_HDR_TSP	1
+#define I3C_XFER_MODE_HDR_TSL	2
+#define I3C_XFER_MODE_SDR	31
+
+/**
+ * struct i3c_ioc_xfer - I3C ioctl transfer
+ * @data: Holds pointer to userspace buffer with transmit/receive data.
+ * @len: Length of data buffer, in bytes.
+ * @actual_len: Actual length of data transferred on read, in bytes (output).
+ * @rnw: Transfer direction for SDR mode (I3C_DEV_DIR_WRITE or I3C_DEV_DIR_READ).
+ * @cmd: Command byte for HDR mode (0x00-0x7f write, 0x80-0xff read).
+ * @mode: Transfer mode (I3C_XFER_MODE_SDR, I3C_XFER_MODE_HDR_DDR, etc.).
+ * @pad: Reserved for future extensions; must be zeroed.
+ */
+struct i3c_ioc_xfer {

include/uapi/linux/i3c/i3cdev.h line 46

Quoted 10 lines
include/uapi/linux/i3c/i3cdev.h new file +10 -0
+	__u64 data;
+	__u16 len;
+	__u16 actual_len;
+	union {
+		__u8 rnw;
+		__u8 cmd;
+	};
+	__u8 mode;
+	__u8 pad[2];
+};

Can we share the above information include MODE* with i3c system to avoid duplicate it?

Frank

Quoted 14 lines
include/uapi/linux/i3c/i3cdev.h new file +11 -1
+
+#define __I3C_XFER_SIZE(type)	\
+	((((sizeof(struct i3c_ioc_xfer)) * (type)) < (1 << _IOC_SIZEBITS)) \
+	? ((sizeof(struct i3c_ioc_xfer)) * (type)) : 0)
+
+#define I3C_XFER_SIZE(N)	__I3C_XFER_SIZE(N)
+
+#define I3C_IOC_XFER(N)	\
+	_IOC(_IOC_READ | _IOC_WRITE, I3C_DEV_IOC_MAGIC, 30, I3C_XFER_SIZE(N))
+
+#endif
--

2.55.0.1082.g2b9226bbc0-goog

#12 lore raw
Quoted 7 lines

cleanup.h prefer declear tmp here

        char *tmp __free(kfree) = kzalloc(count, GFP_KERNEL);

check others

Declared all __free(kfree) vars inline when they are allocated (i3cdev_read(), i3cdev_write(), i3cdev_do_xfer(), i3cdev_ioc_xfer())

Quoted 3 lines

Can we share the above information include MODE* with i3c system to avoid duplicate it?

For v6 the i3c_xfer_mode enum will live in include/uapi/linux/i3c/i3cdev.h and be included by include/linux/i3c/device.h.

Thanks,
Sam

#13 lore raw

PATCH v5 5/5 tools: i3c: add i3ctransfer utility

In reply to Sam Agazaryan

Add the i3ctransfer userspace utility to tools/i3c/ for testing and interacting with I3C target devices via /dev/bus/i3c/* character devices.

Based on the i3ctransfer tool from i3c-tools by Vitor Soares, updated for the unified I3C_IOC_XFER UAPI (struct i3c_ioc_xfer):
- Supports SDR and HDR (HDR-DDR, HDR-TSP, HDR-TSL) transfer modes via -m/--mode and -c/--command options.
- Reports actual_len bytes received on read transfers.

---
Note to Vitor Soares:
This patch ports your GPL-2.0 i3ctransfer utility from i3c-tools into tools/i3c/ and updates it for struct i3c_ioc_xfer (SDR/HDR modes and actual_len read reporting). Since the original GitHub commit did not include a Signed-off-by tag, could you please reply with your Signed-off-by or Acked-by so we can properly record your sign-off?

 MAINTAINERS             |   1 +
 tools/Makefile          |  13 +-
 tools/i3c/Build         |   1 +
 tools/i3c/Makefile      |  58 ++++++++
 tools/i3c/i3ctransfer.c | 307 ++++++++++++++++++++++++++++++++++++++++
 5 files changed, 374 insertions(+), 6 deletions(-)
 create mode 100644 tools/i3c/Build
 create mode 100644 tools/i3c/Makefile
 create mode 100644 tools/i3c/i3ctransfer.c
MAINTAINERS +1 -0
index 30a5cb12c4f0..8a4166ebc9dc 100644
@@ -12365,6 +12365,7 @@ F:	drivers/i3c/
 F:	include/dt-bindings/i3c/
 F:	include/linux/i3c/
 F:	include/uapi/linux/i3c/
+F:	tools/i3c/
 
 IBM Operation Panel Input Driver
 M:	Eddie James <[email protected]>
tools/Makefile +7 -6
index cb40961a740f..7c17e7555fd7 100644
@@ -20,6 +20,7 @@ help:
 	@echo '  freefall               - laptop accelerometer program for disk protection'
 	@echo '  gpio                   - GPIO tools'
 	@echo '  hv                     - tools used when in Hyper-V clients'
+	@echo '  i3c                    - I3C tools'
 	@echo '  iio                    - IIO tools'
 	@echo '  intel-speed-select     - Intel Speed Select tool'
 	@echo '  kvm_stat               - top-like utility for displaying kvm statistics'
@@ -70,7 +71,7 @@ acpi: FORCE
 cpupower: FORCE
 	$(call descend,power/$@)
 
-counter dma firewire hv guest bootconfig spi usb virtio mm bpf iio gpio objtool leds wmi firmware debugging tracing: FORCE
+counter dma firewire hv guest bootconfig spi usb virtio mm bpf i3c iio gpio objtool leds wmi firmware debugging tracing: FORCE
 	$(call descend,$@)
 
 bpf/%: FORCE
@@ -126,7 +127,7 @@ ynl: FORCE
 all: acpi counter cpupower dma gpio hv firewire \
 		perf selftests bootconfig spi turbostat usb \
 		virtio mm bpf x86_energy_perf_policy \
-		tmon freefall iio objtool kvm_stat wmi \
+		tmon freefall i3c iio objtool kvm_stat wmi \
 		debugging tracing thermal thermometer thermal-engine ynl
 
 acpi_install:
@@ -135,7 +136,7 @@ acpi_install:
 cpupower_install:
 	$(call descend,power/$(@:_install=),install)
 
-counter_install dma_install firewire_install gpio_install hv_install iio_install perf_install bootconfig_install spi_install usb_install virtio_install mm_install bpf_install objtool_install wmi_install debugging_install tracing_install:
+counter_install dma_install firewire_install gpio_install hv_install i3c_install iio_install perf_install bootconfig_install spi_install usb_install virtio_install mm_install bpf_install objtool_install wmi_install debugging_install tracing_install:
 	$(call descend,$(@:_install=),install)
 
 selftests_install:
@@ -166,7 +167,7 @@ ynl_install:
 	$(call descend,net/$(@:_install=),install)
 
 install: acpi_install counter_install cpupower_install dma_install gpio_install \
-		hv_install firewire_install iio_install \
+		hv_install firewire_install i3c_install iio_install \
 		perf_install selftests_install turbostat_install usb_install \
 		virtio_install mm_install bpf_install x86_energy_perf_policy_install \
 		tmon_install freefall_install objtool_install kvm_stat_install \
@@ -179,7 +180,7 @@ acpi_clean:
 cpupower_clean:
 	$(call descend,power/cpupower,clean)
 
-counter_clean dma_clean hv_clean firewire_clean bootconfig_clean spi_clean usb_clean virtio_clean mm_clean wmi_clean bpf_clean iio_clean gpio_clean objtool_clean leds_clean firmware_clean debugging_clean tracing_clean:
+counter_clean dma_clean hv_clean firewire_clean bootconfig_clean spi_clean usb_clean virtio_clean mm_clean wmi_clean bpf_clean i3c_clean iio_clean gpio_clean objtool_clean leds_clean firmware_clean debugging_clean tracing_clean:
 	$(call descend,$(@:_clean=),clean)
 
 libapi_clean:
@@ -227,7 +228,7 @@ ynl_clean:
 
 clean: acpi_clean counter_clean cpupower_clean dma_clean hv_clean firewire_clean \
 		perf_clean selftests_clean turbostat_clean bootconfig_clean spi_clean usb_clean virtio_clean \
-		mm_clean bpf_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
+		mm_clean bpf_clean i3c_clean iio_clean x86_energy_perf_policy_clean tmon_clean \
 		freefall_clean build_clean libbpf_clean libsubcmd_clean \
 		gpio_clean objtool_clean leds_clean wmi_clean firmware_clean debugging_clean \
 		intel-speed-select_clean tracing_clean thermal_clean thermometer_clean thermal-engine_clean \
tools/i3c/Build new file +1 -0
new file mode 100644
index 000000000000..6a22328ad983
@@ -0,0 +1 @@
+i3ctransfer-y += i3ctransfer.o
tools/i3c/Makefile new file +58 -0

58 lines not on this page · raw message

tools/i3c/i3ctransfer.c new file +307 -0

307 lines not on this page · raw message

Signature · 1 line
2.55.0.1082.g2b9226bbc0-goog
#14 lore raw

Re: [PATCH v5 5/5] tools: i3c: add i3ctransfer utility

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Medium] tools/i3c: missing per-message mode parsing in getopt second pass - [Medium] tools/i3c: silent…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Medium] tools/i3c: missing per-message mode parsing in getopt second pass
- [Medium] tools/i3c: silent data truncation of write payloads exceeding 255 bytes
- [Low] tools/i3c: potential heap out-of-bounds read in print_rx_data
--

commit 2852ade0c3821ed20131d9f8a5931798c8b93c03
Author: Sam Agazaryan <[email protected]>

tools: i3c: add i3ctransfer utility

This adds the i3ctransfer utility for interacting with I3C target devices. It supports SDR and HDR transfer modes and reports the actual length of read bytes.

Quoted 5 lines
tools/i3c/i3ctransfer.c new file +0 -0
new file mode 100644
index 0000000000000..a5b071e635306

[ ... ]

Quoted 11 lines
tools/i3c/i3ctransfer.c new file +11 -0
+static int w_args_to_xfer(struct i3c_ioc_xfer *xfer, char *arg,
+			  uint8_t mode, int cmd)
+{
+	char *data_ptrs[256];
+	int len, i = 0;
+	uint8_t *tmp;
+
+	if (mode == I3C_XFER_MODE_SDR) {
+		xfer->rnw = I3C_DEV_DIR_WRITE;
+	} else {
+		if (cmd < 0 || (cmd & 0x80)) {

tools/i3c/i3ctransfer.c line 146

Quoted 10 lines
tools/i3c/i3ctransfer.c new file +10 -0
+			fprintf(stderr,
+				"Error: HDR write requires command (-c) in range 0x00..0x7f\n");
+			return -1;
+		}
+		xfer->cmd = (uint8_t)cmd;
+	}
+
+	data_ptrs[i] = strtok(arg, ",");
+	while (data_ptrs[i] && i < 255)
+		data_ptrs[++i] = strtok(NULL, ",");

[Severity: Medium]
Does this silently discard data if the user provides a comma-separated write payload longer than 255 bytes? The loop bounds token parsing to 255 elements, meaning any remaining bytes are dropped without returning an error. Should this return an error if strtok() indicates more tokens remain?

Quoted 13 lines
tools/i3c/i3ctransfer.c new file +13 -0
+
+	if (i == 0) {
+		fprintf(stderr, "Error: empty write data block\n");
+		return -1;
+	}
+
+	tmp = calloc(i, sizeof(uint8_t));
+	if (!tmp)
+		return -1;
+
+	for (len = 0; len < i; len++) {
+		char *endptr;
+		long val = strtol(data_ptrs[len], &endptr, 0);

[ ... ]

tools/i3c/i3ctransfer.c line 185

Quoted 9 lines
tools/i3c/i3ctransfer.c new file +9 -0
+static void print_rx_data(const struct i3c_ioc_xfer *xfer)
+{
+	const uint8_t *tmp = (const uint8_t *)(uintptr_t)xfer->data;
+	int i;
+
+	fprintf(stdout, "  received data (%u bytes):\n", xfer->actual_len);
+	for (i = 0; i < xfer->actual_len; i++)
+		fprintf(stdout, "    0x%02x\n", tmp[i]);
+}

[Severity: Low]
Could this lead to an out-of-bounds read if the kernel returns an actual_len that is larger than the requested xfer->len? Since tmp is allocated based on the requested length, iterating up to actual_len might read past the end of the buffer if a buggy driver or device returns an unexpected length.

[ ... ]

Quoted 2 lines
tools/i3c/i3ctransfer.c new file +2 -0
+int main(int argc, char *argv[])
+{

[ ... ]

tools/i3c/i3ctransfer.c line 261

Quoted 11 lines
tools/i3c/i3ctransfer.c new file +11 -0
+	optind = 1;
+	nxfers = 0;
+	cmd = -1;
+
+	while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != EOF) {
+		switch (opt) {
+		case 'h':
+		case 'v':
+		case 'd':
+		case 'm':
+			break;

[Severity: Medium]
Does skipping the 'm' argument in the second getopt_long pass break mixed-mode transfers? If a user interleaves multiple transfers with different -m flags, it appears the utility assigns the mode of the very last -m flag to all transfers, because the second pass doesn't update the local mode variable per message.

Quoted 3 lines
tools/i3c/i3ctransfer.c new file +3 -0
+		case 'c':
+			cmd = (int)strtol(optarg, NULL, 0);
+			break;

[ ... ]

Quoted 5 lines
tools/i3c/i3ctransfer.c new file +5 -0
+	if (ioctl(file, I3C_IOC_XFER(nxfers), xfers) < 0) {
+		fprintf(stderr, "Error: transfer failed: %s\n", strerror(errno));
+		ret = EXIT_FAILURE;
+		goto err_free;
+	}

tools/i3c/i3ctransfer.c line 296

Quoted 10 lines
tools/i3c/i3ctransfer.c new file +10 -0
+
+	for (i = 0; i < nxfers; i++) {
+		bool is_read = (mode == I3C_XFER_MODE_SDR) ?
+			       (xfers[i].rnw == I3C_DEV_DIR_READ) :
+			       (xfers[i].cmd & 0x80);
+
+		fprintf(stdout, "Success on message %d\n", i);
+		if (is_read)
+			print_rx_data(&xfers[i]);
+	}

[Severity: Medium]
Because of the issue above, the output loop here also checks the global mode variable instead of xfers[i].mode. Could this cause the tool to incorrectly evaluate the read/write status of individual messages during a mixed-mode sequence?

Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5

-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#15 lore raw
In reply to Sam Agazaryan
Quoted 14 lines · Sam Agazaryan

This patch series introduces the i3cdev module, exposing unbound I3C target devices to userspace via character device nodes (/dev/bus/i3c/*), along with the i3ctransfer userspace utility in tools/i3c/.

Userspace access to I3C targets is needed for devices that do not have a kernel driver bound to them, such as targets in ROM/bootloader recovery mode (e.g., OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows), as well as hardware bring-up and diagnostics.

When a kernel driver later binds to an I3C device (for example via dynamic module loading), i3cdev automatically detaches via the BUS_NOTIFY_BIND_DRIVER notifier so kernel drivers always take precedence.

Quoted 6 lines

Testing status:
This v5 series has been compile-tested across all modified I3C controller drivers with W=1. Posting v5 now so collaborators and controller owners can test the unified UAPI and actual_len updates on their respective hardware and provide Tested-by tags while we complete final hardware verification of the v5 updates on our platform.

I do not know how we end up here. The not-settled yet discussion is in thread with [email protected].

But I have a big concern about exposing i3c to user space in the way we have it in i2c. Taking into account that i2c is an odd bus and might lead even to HW *physical* breakage, I would thing 100 times before making the same mistake in i3c. If you ever want to do this, this must not be user visible feature (hidden under expert and debug and maybe even more guards for the starter).

Personally from my perspective this is no go, but I'm not a maintainer here.

P.S.
And you need to gather the opinion of Wolfram as well.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#16 lore raw

Hi Andy,

Quoted 1 line

And you need to gather the opinion of Wolfram as well.

Dunno if I can help here. When it comes to driver development, a dev interface is surely useful but can stay in some debug corner. However, cover letter says:

Quoted 4 lines
Quoted 4 lines

Userspace access to I3C targets is needed for devices that do not have a kernel driver bound to them, such as targets in ROM/bootloader recovery mode (e.g., OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows), as well as hardware bring-up and diagnostics.

I don't know anything about these recovery mechanisms and their needs. I might have time to look into I3C things after all the conferences in Prague, but no promises...

Happy hacking,

Wolfram

#17 lore raw

Hey Wolfram and Andy,

Andy:

Quoted 2 lines

I do not know how we end up here. The not-settled yet discussion is in thread with [email protected].

There has been some back and forth but I originally uploaded this patch set using
notifiers for binding looking for comments on the revival of a potential upstream
i3cdev driver - Meagan had been working on a similar effort with i3cdev using a different approach for binding and as of right now the bus notifier approach seems to be more favored, but not approved nor decided on yet.

Quoted 6 lines

But I have a big concern about exposing i3c to user space in the way we have it in i2c. Taking into account that i2c is an odd bus and might lead even to HW *physical* breakage, I would thing 100 times before making the same mistake in i3c. If you ever want to do this, this must not be user visible feature (hidden under expert and debug and maybe even more guards for the starter).

Just to make sure we're on the same page here about this - is your concern that we can arbitrarily address any device on the i3c bus at-will as is the case with i2c-dev?

FWIW, i3cdev as it exists in this patchset does not have as much free reign over the i3c bus as i2c-dev does over an i2c bus:

1. i3cdev attaches to discrete target devices rather than exposing the bus as a whole.
This means if I have 5 devices on my electrical bus that are up and operational and 4 of them have a proper i3c driver to bind to (E.G. mctp-i3c) then i3cdev will
not touch those devices, only the 5th device without a bound driver and that would be via the i3cdev instance created specifically for that device. A matching
kernel driver being found for a device later means i3cdev automatically detaches itself
and makes way for a compatible client driver to bind.

2. Master controllers are also not a binding option for the driver, so no free access to
send out arbitrary CCCs, perform bus scans, do DAA work, etc.

CONFIG_I3CDEV defaults to n and only creates root-owned character devices for unbound targets, though if i3c maintainers prefer adding "depends on EXPERT" or expanding the Kconfig help text, I am happy to do that in v6.

Wolfram:

Quoted 8 lines
Quoted 4 lines
Quoted 4 lines

Userspace access to I3C targets is needed for devices that do not have a kernel driver bound to them, such as targets in ROM/bootloader recovery mode (e.g., OCP Secure Firmware Recovery v1.1 and Caliptra Silicon Root of Trust recovery flows), as well as hardware bring-up and diagnostics.

I don't know anything about these recovery mechanisms and their needs. I might have time to look into I3C things after all the conferences in Prague, but no promises...

Targets such as Caliptra (Silicon Root of
Trust), SoCs, and accelerators implement the OCP Secure Firmware Recovery specification over I3C. When a target's runtime firmware is corrupted, unprovisioned, or fails secure boot, its ROM/bootloader exposes an I3C target endpoint implementing the OCP Recovery register interface (PROT_CAP, DEVICE_ID, DEVICE_STATUS, RECOVERY_CTRL, INDIRECT_FIFO_DATA) over standard I3C private transfers.

Per Section 8.4 of the OCP Secure Firmware Recovery v1.1 spec [1], because I3C does not have native SMBus-style block transfers, the recovery protocol emulates them over I3C using framed Private Write transfers and combined Private Write + Repeated START Private Read transfers (with PEC) to poll status/reason registers, pull crash logs from Component Memory Spaces (CMS), stream signed recovery images into the FIFO, and trigger activation.

The spec defines this same command set across SMBus, USB EP0, and I3C so that a userspace Recovery Agent on a BMC can drive the recovery, diagnostic log extraction, and attestation workflow across transports (using i2c-dev for SMBus, usbfs/libusb for USB EP0, and i3cdev for I3C), alongside general hardware bring-up and vendor-specific provisioning.

[1] https://www.opencompute.org/documents/ocp-recovery-document-1p1-final-pdf/

Thanks,
Sam

#18 lore raw
Quoted 17 lines · Sam Agazaryan
Quoted 2 lines

I do not know how we end up here. The not-settled yet discussion is in thread with [email protected].

There has been some back and forth but I originally uploaded this patch set using
notifiers for binding looking for comments on the revival of a potential upstream
i3cdev driver - Meagan had been working on a similar effort with i3cdev using a different approach for binding and as of right now the bus notifier approach seems to be more favored, but not approved nor decided on yet.

Quoted 5 lines

But I have a big concern about exposing i3c to user space in the way we have it in i2c. Taking into account that i2c is an odd bus and might lead even to HW *physical* breakage, I would thing 100 times before making the same mistake in i3c. If you ever want to do this, this must not be user visible feature (hidden under expert and debug and maybe even more guards for the starter).

Quoted 3 lines

Just to make sure we're on the same page here about this - is your concern that we can arbitrarily address any device on the i3c bus at-will as is the case with i2c-dev?

Yes and more as this bus (i3c) is still non-discoverable (am I mistaken?).

Quoted 14 lines

FWIW, i3cdev as it exists in this patchset does not have as much free reign over the i3c bus as i2c-dev does over an i2c bus:

1. i3cdev attaches to discrete target devices rather than exposing the bus as a whole.
This means if I have 5 devices on my electrical bus that are up and operational and 4 of them have a proper i3c driver to bind to (E.G. mctp-i3c) then i3cdev will
not touch those devices, only the 5th device without a bound driver and that would be via the i3cdev instance created specifically for that device. A matching
kernel driver being found for a device later means i3cdev automatically detaches itself
and makes way for a compatible client driver to bind.

This makes even less usefulness in my eyes. Immediate Q: if 4 already have the proper driver in the kernel, what's wrong to make it for the fifth one?

What if the device needs more? Or if this is a multi-functional device that may have driver for only one part?

What to do with the i2c devices connected to i3c bus in the compatible mode?

Quoted 8 lines

2. Master controllers are also not a binding option for the driver, so no free access to
send out arbitrary CCCs, perform bus scans, do DAA work, etc.

CONFIG_I3CDEV defaults to n and only creates root-owned character devices for unbound targets, though if i3c maintainers prefer adding "depends on EXPERT" or expanding the Kconfig help text, I am happy to do that in v6.

I really want to announce that as debug feature. Maybe I missed some use cases when it's the only way to handle the target devices, but for now I fail to see that.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#19 lore raw
Quoted 1 line

Yes and more as this bus (i3c) is still non-discoverable (am I mistaken?).

I3C is a dynamically discoverable bus. During DAA each I3C target reports a Provisioned ID (PID), Bus Characteristic Register (BCR), and Device Characteristic
Register (DCR), and is assigned a dynamic address by the controller.

Quoted 2 lines

This makes even less usefulness in my eyes. Immediate Q: if 4 already have the proper driver in the kernel, what's wrong to make it for the fifth one?

1. The original use case was hardware bring-up, silicon validation, and debug (i3ctransfer): Just like spidev, engineers need a debug interface to issue ad-hoc SDR/HDR transfers during bring-up or hardware validation before a driver exists.

2. Userspace firmware recovery / update tools (OCP Secure Firmware Recovery, Caliptra RoT):
The recovery protocol is purely a polled request/response transport (no IBIs allowed per spec)
used by userspace update tooling (fwupd / BMC recovery daemons) to parse signed firmware
manifests, stream recovery image chunks to INDIRECT_FIFO_DATA, and poll RECOVERY_STATUS—similar
to how userspace firmware updaters operate over spidev, hidraw, or USB/NVMe ioctls rather than
putting firmware image parsing and update policy inside the kernel. In my response to
Wolfram above I discussed this some more.

Quoted 2 lines

What if the device needs more? Or if this is a multi-functional device that may have driver for only one part?

If a device needs more than basic transfers (such as IBIs, hot-join handling, or integration with a subsystem like hwmon/iio/mctp), it should use a dedicated kernel driver.

For multi-function devices, if the functions are exposed as MIPI I3C Targets (e.g. a device exposing both an mctp-i3c interface and an OCP recovery interface), each virtual target is assigned its own dynamic address and DCR during ENTDAA and is enumerated as a distinct i3c_device instance on the bus. Conversely, if multiple functions share a single I3C target address and register map, i3cdev intentionally does not allow userspace to access a target while a kernel driver is bound to it (avoiding the I2C_SLAVE_FORCE problem where userspace can race with a bound driver), so that case should be handled by an in-kernel MFD driver - but that is not the use case being targeted here with OCP recovery.

Quoted 1 line

What to do with the i2c devices connected to i3c bus in the compatible mode?

I3C master controllers register an i2c adapter for i2c devices running on an i3c mixed mode bus - they are identified as i2c_bus_type, not i3c.

Thanks,
Sam

#20 lore raw
Quoted 6 lines · Sam Agazaryan
Quoted 1 line

Yes and more as this bus (i3c) is still non-discoverable (am I mistaken?).

I3C is a dynamically discoverable bus. During DAA each I3C target reports a Provisioned ID (PID), Bus Characteristic Register (BCR), and Device Characteristic Register (DCR), and is assigned a dynamic address by the controller.

Did I miss this in the initial explanation? If not, please add it next time.

Quoted 7 lines
Quoted 2 lines

This makes even less usefulness in my eyes. Immediate Q: if 4 already have the proper driver in the kernel, what's wrong to make it for the fifth one?

1. The original use case was hardware bring-up, silicon validation, and debug (i3ctransfer): Just like spidev, engineers need a debug interface to issue ad-hoc SDR/HDR transfers during bring-up or hardware validation before a driver exists.

Totally debug feature to me. Once you expose something in /dev (imply IOCTL), it will become an ABI. And I'm fully against this.

Quoted 9 lines

2. Userspace firmware recovery / update tools (OCP Secure Firmware Recovery, Caliptra RoT):
The recovery protocol is purely a polled request/response transport (no IBIs allowed per spec) used by userspace update tooling (fwupd / BMC recovery daemons) to parse signed firmware manifests, stream recovery image chunks to INDIRECT_FIFO_DATA, and poll RECOVERY_STATUS—similar to how userspace firmware updaters operate over spidev, hidraw, or USB/NVMe ioctls rather than putting firmware image parsing and update policy inside the kernel. In my response to Wolfram above I discussed this some more.

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Quoted 7 lines
Quoted 2 lines

What if the device needs more? Or if this is a multi-functional device that may have driver for only one part?

If a device needs more than basic transfers (such as IBIs, hot-join handling, or integration with a subsystem like hwmon/iio/mctp), it should use a dedicated kernel driver.

Quoted 11 lines

For multi-function devices, if the functions are exposed as MIPI I3C Targets (e.g. a device exposing both an mctp-i3c interface and an OCP recovery interface), each virtual target is assigned its own dynamic address and DCR during ENTDAA and is enumerated as a distinct i3c_device instance on the bus. Conversely, if multiple functions share a single I3C target address and register map, i3cdev intentionally does not allow userspace to access a target while a kernel driver is bound to it (avoiding the I2C_SLAVE_FORCE problem where userspace can race with a bound driver), so that case should be handled by an in-kernel MFD driver - but that is not the use case being targeted here with OCP recovery.

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Quoted 4 lines
Quoted 1 line

What to do with the i2c devices connected to i3c bus in the compatible mode?

I3C master controllers register an i2c adapter for i2c devices running on an i3c mixed mode bus - they are identified as i2c_bus_type, not i3c.

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Signature · 9 lines
With Best Regards,
Andy Shevchenko



-- 
linux-i3c mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-i3c
#21 lore raw
Quoted 54 lines · Andy Shevchenko
Quoted 6 lines · Sam Agazaryan
Quoted 1 line

Yes and more as this bus (i3c) is still non-discoverable (am I mistaken?).

I3C is a dynamically discoverable bus. During DAA each I3C target reports a Provisioned ID (PID), Bus Characteristic Register (BCR), and Device Characteristic Register (DCR), and is assigned a dynamic address by the controller.

Did I miss this in the initial explanation? If not, please add it next time.

Quoted 7 lines
Quoted 2 lines

This makes even less usefulness in my eyes. Immediate Q: if 4 already have the proper driver in the kernel, what's wrong to make it for the fifth one?

1. The original use case was hardware bring-up, silicon validation, and debug (i3ctransfer): Just like spidev, engineers need a debug interface to issue ad-hoc SDR/HDR transfers during bring-up or hardware validation before a driver exists.

Totally debug feature to me. Once you expose something in /dev (imply IOCTL), it will become an ABI. And I'm fully against this.

Quoted 9 lines

2. Userspace firmware recovery / update tools (OCP Secure Firmware Recovery, Caliptra RoT):
The recovery protocol is purely a polled request/response transport (no IBIs allowed per spec) used by userspace update tooling (fwupd / BMC recovery daemons) to parse signed firmware manifests, stream recovery image chunks to INDIRECT_FIFO_DATA, and poll RECOVERY_STATUS—similar to how userspace firmware updaters operate over spidev, hidraw, or USB/NVMe ioctls rather than putting firmware image parsing and update policy inside the kernel. In my response to Wolfram above I discussed this some more.

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Quoted 7 lines
Quoted 2 lines

What if the device needs more? Or if this is a multi-functional device that may have driver for only one part?

If a device needs more than basic transfers (such as IBIs, hot-join handling, or integration with a subsystem like hwmon/iio/mctp), it should use a dedicated kernel driver.

Quoted 11 lines

For multi-function devices, if the functions are exposed as MIPI I3C Targets (e.g. a device exposing both an mctp-i3c interface and an OCP recovery interface), each virtual target is assigned its own dynamic address and DCR during ENTDAA and is enumerated as a distinct i3c_device instance on the bus. Conversely, if multiple functions share a single I3C target address and register map, i3cdev intentionally does not allow userspace to access a target while a kernel driver is bound to it (avoiding the I2C_SLAVE_FORCE problem where userspace can race with a bound driver), so that case should be handled by an in-kernel MFD driver - but that is not the use case being targeted here with OCP recovery.

Quoted 13 lines

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Quoted 4 lines
Quoted 1 line

What to do with the i2c devices connected to i3c bus in the compatible mode?

I3C master controllers register an i2c adapter for i2c devices running on an i3c mixed mode bus - they are identified as i2c_bus_type, not i3c.

Forgot to ask, but do they interfere?

Quoted 2 lines

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#22 lore raw
Quoted 1 line

Did I miss this in the initial explanation? If not, please add it next time.

That is just how the existing I3C core enumerates targets during DAA, not something added by this series. I will add a sentence to the cover letter and Patch 4 commit message in v6 contrasting it with i2c-dev to make that context clear.

Quoted 3 lines

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Yes, this is used in real systems today:

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

- OCP Secure Firmware Recovery (DCR 0xBD) [1] and Caliptra Root-of-Trust recovery agents on OpenBMC/gBMC platforms (e.g., AST2700 / Caliptra MCU recovery tooling and BMC recovery daemons). Because Vitor's original i3cdev series was not merged in 2019, multiple silicon vendors and OpenBMC trees have been carrying out-of-tree forks of i3cdev; upstreaming consolidates those diverging implementations.

2. Why an ioctl is required over a read and write:

- In the OCP Secure Firmware Recovery v1.1 specification (Section 8.4, "Recovery I3C Interface Block Read via Private Write and Private Read"), every recovery command read is a combined transaction consisting of a Private Write with no STOP, followed by a Repeated START, followed by a Private Read, and finally a STOP.

- Because write() and read() only submit a single transfer at a time (nxfers = 1), the controller emits a STOP condition on the wire at the end of write() and drops the bus lock. Submitting multiple i3c_ioc_xfer descriptors in a single I3C_IOC_XFER(N) ioctl is required so i3c_device_do_xfers() queues them together under one bus lock acquisition and the controller emits a Repeated START between messages (as well as passing HDR mode/cmd parameters).

[1] https://www.opencompute.org/documents/ocp-recovery-document-1p1-final-pdf

Quoted 8 lines

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Note that a user does not need to recompile the kernel if a kernel driver binds to a device they wish to access via i3cdev. Because i3cdev handles BUS_NOTIFY_UNBOUND_DRIVER, unbinding the kernel driver at runtime via sysfs:

echo <dev> > /sys/bus/i3c/devices/<dev>/driver/unbind

automatically triggers i3cdev_attach() and exposes /dev/bus/i3c/<dev> without rebuilding the kernel. Also, I3C drivers match against a target's MIPI-assigned DCR or 48-bit PID (Manufacturer ID + Part ID), so a driver for a simple device will not accidentally match a different chip's PID or a dedicated DCR like 0xBD.

Quoted 2 lines

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Production BMC recovery daemons cannot rely on debugfs, which is optional and restricted or unmounted on production builds. A character device in /dev/bus/i3c/* provides the stable ioctl those tools need and integrates with udev so non-root daemons can be granted access to specific target nodes (e.g. matching on DCR 0xBD), similar to spidev and hidraw.

Quoted 1 line

Forgot to ask, but do they interfere?

Is this in reference to i2c devices on the i3c bus? No, I3C transfers and legacy
I2C transfers on a mixed-mode bus do not interfere:

1. Both i3c_device_do_xfers() and i3c_master_i2c_adapter_xfer() acquire i3c_bus_normaluse_lock(&master->bus), so I3C and I2C transactions on the same bus are serialized by the I3C core.

2. Attached I2C client addresses are marked as I3C_ADDR_SLOT_I2C_DEV in the bus address slot table, preventing DAA from assigning an I2C device's address to an I3C target.

Thanks,
Sam

#23 lore raw
Quoted 83 lines · Sam Agazaryan
Quoted 1 line

Did I miss this in the initial explanation? If not, please add it next time.

That is just how the existing I3C core enumerates targets during DAA, not something added by this series. I will add a sentence to the cover letter and Patch 4 commit message in v6 contrasting it with i2c-dev to make that context clear.

Quoted 3 lines

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Yes, this is used in real systems today:

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

- OCP Secure Firmware Recovery (DCR 0xBD) [1] and Caliptra Root-of-Trust recovery agents on OpenBMC/gBMC platforms (e.g., AST2700 / Caliptra MCU recovery tooling and BMC recovery daemons). Because Vitor's original i3cdev series was not merged in 2019, multiple silicon vendors and OpenBMC trees have been carrying out-of-tree forks of i3cdev; upstreaming consolidates those diverging implementations.

2. Why an ioctl is required over a read and write:

- In the OCP Secure Firmware Recovery v1.1 specification (Section 8.4, "Recovery I3C Interface Block Read via Private Write and Private Read"), every recovery command read is a combined transaction consisting of a Private Write with no STOP, followed by a Repeated START, followed by a Private Read, and finally a STOP.

- Because write() and read() only submit a single transfer at a time (nxfers = 1), the controller emits a STOP condition on the wire at the end of write() and drops the bus lock. Submitting multiple i3c_ioc_xfer descriptors in a single I3C_IOC_XFER(N) ioctl is required so i3c_device_do_xfers() queues them together under one bus lock acquisition and the controller emits a Repeated START between messages (as well as passing HDR mode/cmd parameters).

[1] https://www.opencompute.org/documents/ocp-recovery-document-1p1-final-pdf

Quoted 8 lines

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Note that a user does not need to recompile the kernel if a kernel driver binds to a device they wish to access via i3cdev. Because i3cdev handles BUS_NOTIFY_UNBOUND_DRIVER, unbinding the kernel driver at runtime via sysfs:

echo <dev> > /sys/bus/i3c/devices/<dev>/driver/unbind

automatically triggers i3cdev_attach() and exposes /dev/bus/i3c/<dev> without rebuilding the kernel. Also, I3C drivers match against a target's MIPI-assigned DCR or 48-bit PID (Manufacturer ID + Part ID), so a driver for a simple device will not accidentally match a different chip's PID or a dedicated DCR like 0xBD.

Quoted 2 lines

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Production BMC recovery daemons cannot rely on debugfs, which is optional and restricted or unmounted on production builds. A character device in /dev/bus/i3c/* provides the stable ioctl those tools need and integrates with udev so non-root daemons can be granted access to specific target nodes (e.g. matching on DCR 0xBD), similar to spidev and hidraw.

Quoted 1 line

Forgot to ask, but do they interfere?

Is this in reference to i2c devices on the i3c bus? No, I3C transfers and legacy
I2C transfers on a mixed-mode bus do not interfere:

Quoted 7 lines

1. Both i3c_device_do_xfers() and i3c_master_i2c_adapter_xfer() acquire i3c_bus_normaluse_lock(&master->bus), so I3C and I2C transactions on the same bus are serialized by the I3C core.

2. Attached I2C client addresses are marked as I3C_ADDR_SLOT_I2C_DEV in the bus address slot table, preventing DAA from assigning an I2C device's address to an I3C target.

On my side, I believe i3cdev can be added to the kernel and that it is way safer than i2cdev but I agree that the ABI has to be properly designed, maybe we need to version it or have a way to probe for supported features so we can update safely later on. It is failry simple right now but I feel like we are going to see people with more needs than initiating transfers.

Also, I think this should not bind to any devices by default and make binding explicit.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#24 lore raw
Quoted 92 lines · Alexandre Belloni
Quoted 83 lines · Sam Agazaryan
Quoted 1 line

Did I miss this in the initial explanation? If not, please add it next time.

That is just how the existing I3C core enumerates targets during DAA, not something added by this series. I will add a sentence to the cover letter and Patch 4 commit message in v6 contrasting it with i2c-dev to make that context clear.

Quoted 3 lines

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Yes, this is used in real systems today:

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

- OCP Secure Firmware Recovery (DCR 0xBD) [1] and Caliptra Root-of-Trust recovery agents on OpenBMC/gBMC platforms (e.g., AST2700 / Caliptra MCU recovery tooling and BMC recovery daemons). Because Vitor's original i3cdev series was not merged in 2019, multiple silicon vendors and OpenBMC trees have been carrying out-of-tree forks of i3cdev; upstreaming consolidates those diverging implementations.

2. Why an ioctl is required over a read and write:

- In the OCP Secure Firmware Recovery v1.1 specification (Section 8.4, "Recovery I3C Interface Block Read via Private Write and Private Read"), every recovery command read is a combined transaction consisting of a Private Write with no STOP, followed by a Repeated START, followed by a Private Read, and finally a STOP.

- Because write() and read() only submit a single transfer at a time (nxfers = 1), the controller emits a STOP condition on the wire at the end of write() and drops the bus lock. Submitting multiple i3c_ioc_xfer descriptors in a single I3C_IOC_XFER(N) ioctl is required so i3c_device_do_xfers() queues them together under one bus lock acquisition and the controller emits a Repeated START between messages (as well as passing HDR mode/cmd parameters).

[1] https://www.opencompute.org/documents/ocp-recovery-document-1p1-final-pdf

Quoted 8 lines

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Note that a user does not need to recompile the kernel if a kernel driver binds to a device they wish to access via i3cdev. Because i3cdev handles BUS_NOTIFY_UNBOUND_DRIVER, unbinding the kernel driver at runtime via sysfs:

echo <dev> > /sys/bus/i3c/devices/<dev>/driver/unbind

automatically triggers i3cdev_attach() and exposes /dev/bus/i3c/<dev> without rebuilding the kernel. Also, I3C drivers match against a target's MIPI-assigned DCR or 48-bit PID (Manufacturer ID + Part ID), so a driver for a simple device will not accidentally match a different chip's PID or a dedicated DCR like 0xBD.

Quoted 2 lines

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Production BMC recovery daemons cannot rely on debugfs, which is optional and restricted or unmounted on production builds. A character device in /dev/bus/i3c/* provides the stable ioctl those tools need and integrates with udev so non-root daemons can be granted access to specific target nodes (e.g. matching on DCR 0xBD), similar to spidev and hidraw.

Quoted 1 line

Forgot to ask, but do they interfere?

Is this in reference to i2c devices on the i3c bus? No, I3C transfers and legacy
I2C transfers on a mixed-mode bus do not interfere:

Quoted 7 lines

1. Both i3c_device_do_xfers() and i3c_master_i2c_adapter_xfer() acquire i3c_bus_normaluse_lock(&master->bus), so I3C and I2C transactions on the same bus are serialized by the I3C core.

2. Attached I2C client addresses are marked as I3C_ADDR_SLOT_I2C_DEV in the bus address slot table, preventing DAA from assigning an I2C device's address to an I3C target.

Quoted 6 lines

On my side, I believe i3cdev can be added to the kernel and that it is way safer than i2cdev but I agree that the ABI has to be properly designed, maybe we need to version it or have a way to probe for supported features so we can update safely later on. It is failry simple right now but I feel like we are going to see people with more needs than initiating transfers.

Yeah, I really think that i2cdev was a pure mistake (as to be an ABI). I don't want to repeat it now, when we learnt about that case a lot. If we ever going to have i3cdev (which justification I also put in doubts) it needs to be very strict of what can be done and how.

Quoted 2 lines

Also, I think this should not bind to any devices by default and make binding explicit.

Yes, this is quite dangerous "feature".

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#25 lore raw
Quoted 7 lines
Quoted 5 lines

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

So, nothing in production, meaning we can design that in kernel differently.

In hindsight I should have ordered that list differently, the second bullet right below that is the production use case: production BMC firmware update and recovery daemons running in OpenBMC server fleets (managing Caliptra Root-of-Trust and SoC recovery targets over I3C), as well as production server accelerator management daemons.

Quoted 3 lines

But why recovery mechanism can't be kernel-driven? With some sysfs (just as an example) knobs to say "I want to run now a recovery flow on this bus", et cetera.

To make sure we're talking about the same thing: "OCP Secure Firmware Recovery" is not electrical bus recovery (like recovering an i2c or i3c bus itself by bit-banging to unstick a hung bus). It is device firmware recovery and provisioning (similar in concept to USB DFU) to recover and boot a target device that sits on the bus.

This lives in userspace because it is part of the BMC's Redfish/RoT firmware management stack (handling network image streaming, signing/key management, and chassis-level recovery policies), rather than a single "recover this bus" hardware toggle. And it uses I3C_IOC_XFER because each protocol register read/write requires combined Write + Repeated START + Read transfers (or HDR transfers) to that specific target node.

Quoted 5 lines

Oh, that's quite a dangerous "feature"! Can we avoid doing that? Otherwise it means any missed guard against unbind for the system controller related chips may lead to real HW damage. I'm totally against this feature to be in any way of an ABI. If you want that, do it exclusively via debugfs with a big fat warning that this may lead to HW damages.

Agreed -- as Alexandre also suggested in his reply, it sounds like automatic binding isn't favored here due to risk of hardware damage.

Quoted 2 lines

Can i2c device require an i2c recovery flow on the bus? If so, how would it be combined together with the i3c recovery flow(s)?

If you mean electrical bus recovery, i3cdev doesn't perform bus-level electrical recovery. It only submits normal target-addressed transfers via i3c_device_do_xfers().

If you mean OCP device firmware recovery on a mixed bus:

An I2C recovery target on a mixed bus uses standard SMBus Block Read/Write transactions through the controller's i2c_adapter (not i3cdev) while an I3C recovery target uses I3C Private Write/Read transfers via i3cdev. On a mixed bus, the Linux I3C core already configures the bus mode/SCL rate according to the I2C targets' LVR spike-filter capabilities and serializes both paths via i3c_bus_normaluse_lock(&master->bus).

Thanks,
Sam

#26 lore raw
Quoted 6 lines · Sam Agazaryan
Quoted 6 lines
Quoted 5 lines

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

Quoted 7 lines
Quoted 1 line

So, nothing in production, meaning we can design that in kernel differently.

In hindsight I should have ordered that list differently, the second bullet right below that is the production use case: production BMC firmware update and recovery daemons running in OpenBMC server fleets (managing Caliptra Root-of-Trust and SoC recovery targets over I3C), as well as production server accelerator management daemons.

Okay, but they are not (obviously) use the upstream solutions for that.

Quoted 9 lines
Quoted 3 lines

But why recovery mechanism can't be kernel-driven? With some sysfs (just as an example) knobs to say "I want to run now a recovery flow on this bus", et cetera.

To make sure we're talking about the same thing: "OCP Secure Firmware Recovery" is not electrical bus recovery (like recovering an i2c or i3c bus itself by bit-banging to unstick a hung bus). It is device firmware recovery and provisioning (similar in concept to USB DFU) to recover and boot a target device that sits on the bus.

A-ha, so this is a device programming protocol on the top of the i3c bus.

Quoted 6 lines

This lives in userspace because it is part of the BMC's Redfish/RoT firmware management stack (handling network image streaming, signing/key management, and chassis-level recovery policies), rather than a single "recover this bus" hardware toggle. And it uses I3C_IOC_XFER because each protocol register read/write requires combined Write + Repeated START + Read transfers (or HDR transfers) to that specific target node.

But it's still unclear to me why this can't be moved to the kernel space? There is no user space that communicates with upstream kernel in the way you described, they should have used a custom (out-of-tree) solutions, right?

Quoted 9 lines
Quoted 5 lines

Oh, that's quite a dangerous "feature"! Can we avoid doing that? Otherwise it means any missed guard against unbind for the system controller related chips may lead to real HW damage. I'm totally against this feature to be in any way of an ABI. If you want that, do it exclusively via debugfs with a big fat warning that this may lead to HW damages.

Agreed -- as Alexandre also suggested in his reply, it sounds like automatic binding isn't favored here due to risk of hardware damage.

Quoted 6 lines
Quoted 2 lines

Can i2c device require an i2c recovery flow on the bus? If so, how would it be combined together with the i3c recovery flow(s)?

If you mean electrical bus recovery, i3cdev doesn't perform bus-level electrical recovery. It only submits normal target-addressed transfers via i3c_device_do_xfers().

But how it will do if the i2c device blocks the i3c bus in mixed mode? I mean, how will it affect the i3c transfers on the wire?

Quoted 9 lines

If you mean OCP device firmware recovery on a mixed bus:

An I2C recovery target on a mixed bus uses standard SMBus Block Read/Write transactions through the controller's i2c_adapter (not i3cdev) while an I3C recovery target uses I3C Private Write/Read transfers via i3cdev. On a mixed bus, the Linux I3C core already configures the bus mode/SCL rate according to the I2C targets' LVR spike-filter capabilities and serializes both paths via i3c_bus_normaluse_lock(&master->bus).

Good to know, thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#27 lore raw
Quoted 6 lines
Quoted 4 lines

[...] production BMC firmware update
and recovery daemons running in OpenBMC server fleets (managing Caliptra Root-of-Trust and SoC recovery targets over I3C), as well as production server accelerator management daemons.

Okay, but they are not (obviously) use the upstream solutions for that.

Right, they run on vendor kernels carrying out-of-tree i3cdev patches based on an earlier version of this series, so there is no upstream ABI to stay compatible with. This series already departs from that ABI and v6 will change it further based on the review in this thread.

Quoted 3 lines

But it's still unclear to me why this can't be moved to the kernel space? There is no user space that communicates with upstream kernel in the way you described, they should have used a custom (out-of-tree) solutions, right?

It could be, and the closest in kernel fit would be an OCP recovery i3c_driver exposing fw_upload. But pushing an image is only one part of the spec, and an in kernel driver would have to cover all of it, not just the subset one recovery agent happens to use:

- status and identity: PROT_CAP, DEVICE_ID, DEVICE_STATUS (with recovery reason codes), RECOVERY_STATUS and HW_STATUS
- reset and boot control: DEVICE_RESET with forced recovery and flashless boot, and multi-stage activation through RECOVERY_CTRL
- memory spaces: code, critical logging and vendor-defined CMS regions, through either the indirect window or the FIFO interface
- the VENDOR command and vendor-specific status, which differ per SoC vendor

Which of these an agent uses, when to trigger recovery, and which image goes with each stage all vary by platform, and the BMC's userspace already makes those decisions.

An in-kernel driver would need new sysfs ABI for this, and that ABI would have to grow as the spec evolves: v1.1 alone added multi-stage activation, flashless boot and the indirect FIFO interface. With a target-scoped transfer interface, none of that needs kernel changes. fw_upload and lib/pldmfw fit well where a kernel driver already owns the device; here the only consumer is the BMC's management stack.

The kernel already splits things this way on I3C for MCTP: mctp-i3c binds by DCR and provides the transport, while PLDM firmware update runs in userspace. i3cdev could be made to bind the same way, by the OCP recovery DCR, instead of binding to every unbound target. Outside I3C it is the same pattern: nvme-cli does NVMe firmware download/commit through the passthrough ioctls, and fwupd updates devices over hidraw, usbfs and drm_dp_aux_dev.

So the kernel side stays a small mechanism consisting of target-scoped transfers to explicitly bound devices, and the recovery policy stays in userspace, where it can follow new spec revisions without new kernel ABI.

Quoted 6 lines
Quoted 3 lines

If you mean electrical bus recovery, i3cdev doesn't perform bus-level electrical recovery. It only submits normal target-addressed transfers via i3c_device_do_xfers().

But how it will do if the i2c device blocks the i3c bus in mixed mode? I mean, how will it affect the i3c transfers on the wire?

Electrically speaking, SDA and SCL are shared, so an I2C target that holds SDA low blocks every transfer on that bus, I3C and I2C alike, regardless of which driver issued it. The controller reports the failure as a NACK, arbitration loss or timeout, depending on the controller. i3c_device_do_xfers() returns the error, and i3cdev passes it back to userspace from the ioctl.

So i3cdev behaves the same as any in kernel I3C driver on that bus since it uses the same transfer path. Getting a stuck bus released is up to the controller driver rather than i3cdev. For what it's worth, section 8.4.1 of the OCP recovery spec recommends a pure I3C bus for critical recovery paths, and for mixed buses requires validating that I2C targets have spike filters and considering isolating or disabling the ones that can't. I2C devices on a mixed bus are also not allowed to stretch the clock.

Quoted 6 lines
Quoted 4 lines

On a mixed bus, the Linux I3C core already
configures the bus mode/SCL rate according to the I2C targets' LVR spike-filter capabilities and serializes both paths via i3c_bus_normaluse_lock(&master->bus).

Good to know, thanks!

One correction to my previous statements: i3c_bus_normaluse_lock() is a shared lock that only excludes maintenance operations such as DAA. The I2C and I3C transfers themselves are serialized by the master driver's own locking, which is what keeps them from being interleaved on the wire.

Thanks,
Sam

#28 lore raw

On Sun, Nov 08, 2026 at 10:11:52PM -0800, Sam Agazaryan wrote:

[...]

Quoted 20 lines
Quoted 5 lines
Quoted 3 lines

production BMC firmware update and recovery daemons running in OpenBMC server fleets (managing Caliptra Root-of-Trust and SoC recovery targets over I3C), as well as production server accelerator management daemons.

Okay, but they are not (obviously) use the upstream solutions for that.

Right, they run on vendor kernels carrying out-of-tree i3cdev patches based on an earlier version of this series, so there is no upstream ABI to stay compatible with. This series already departs from that ABI and v6 will change it further based on the review in this thread.

Quoted 3 lines

But it's still unclear to me why this can't be moved to the kernel space? There is no user space that communicates with upstream kernel in the way you described, they should have used a custom (out-of-tree) solutions, right?

It could be, and the closest in kernel fit would be an OCP recovery i3c_driver exposing fw_upload. But pushing an image is only one part of the spec, and an in kernel driver would have to cover all of it, not just the subset one recovery agent happens to use:

Quoted 8 lines

- status and identity: PROT_CAP, DEVICE_ID, DEVICE_STATUS (with recovery reason codes), RECOVERY_STATUS and HW_STATUS
- reset and boot control: DEVICE_RESET with forced recovery and flashless boot, and multi-stage activation through RECOVERY_CTRL
- memory spaces: code, critical logging and vendor-defined CMS regions, through either the indirect window or the FIFO interface
- the VENDOR command and vendor-specific status, which differ per SoC vendor

Good. We can see that being implemented step-by-step.

Quoted 3 lines

Which of these an agent uses, when to trigger recovery, and which image goes with each stage all vary by platform, and the BMC's userspace already makes those decisions.

Based on the absence of any interface in the kernel before, there is no chance to regress anything. It might be just a good will to try hard to support existing out-of-tree solutions.

Quoted 6 lines

An in-kernel driver would need new sysfs ABI for this, and that ABI would have to grow as the spec evolves: v1.1 alone added multi-stage activation, flashless boot and the indirect FIFO interface. With a target-scoped transfer interface, none of that needs kernel changes. fw_upload and lib/pldmfw fit well where a kernel driver already owns the device; here the only consumer is the BMC's management stack.

Yes, and I see no issues with that. Opening the whole i3cdev just for the same reason sounds like an overkill. Especially if we do *not* expect any more users.

Quoted 8 lines

The kernel already splits things this way on I3C for MCTP: mctp-i3c binds by DCR and provides the transport, while PLDM firmware update runs in userspace. i3cdev could be made to bind the same way, by the OCP recovery DCR, instead of binding to every unbound target. Outside I3C it is the same pattern: nvme-cli does NVMe firmware download/commit through the passthrough ioctls, and fwupd updates devices over hidraw, usbfs and drm_dp_aux_dev.

Quoted 4 lines

So the kernel side stays a small mechanism consisting of target-scoped transfers to explicitly bound devices, and the recovery policy stays in userspace, where it can follow new spec revisions without new kernel ABI.

If there are success stories, those examples might help to justify your approach. It makes sense to mention that in the cover letter with the summary on their designs (and split).

Quoted 22 lines
Quoted 6 lines
Quoted 3 lines

If you mean electrical bus recovery, i3cdev doesn't perform bus-level electrical recovery. It only submits normal target-addressed transfers via i3c_device_do_xfers().

But how it will do if the i2c device blocks the i3c bus in mixed mode? I mean, how will it affect the i3c transfers on the wire?

Electrically speaking, SDA and SCL are shared, so an I2C target that holds SDA low blocks every transfer on that bus, I3C and I2C alike, regardless of which driver issued it. The controller reports the failure as a NACK, arbitration loss or timeout, depending on the controller. i3c_device_do_xfers() returns the error, and i3cdev passes it back to userspace from the ioctl.

So i3cdev behaves the same as any in kernel I3C driver on that bus since it uses the same transfer path. Getting a stuck bus released is up to the controller driver rather than i3cdev. For what it's worth, section 8.4.1 of the OCP recovery spec recommends a pure I3C bus for critical recovery paths, and for mixed buses requires validating that I2C targets have spike filters and considering isolating or disabling the ones that can't. I2C devices on a mixed bus are also not allowed to stretch the clock.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#29 lore raw
Quoted 9 lines
Quoted 6 lines

An in-kernel driver would need new sysfs ABI for this, and that ABI would have to grow as the spec evolves: v1.1 alone added multi-stage activation, flashless boot and the indirect FIFO interface. With a target-scoped transfer interface, none of that needs kernel changes. fw_upload and lib/pldmfw fit well where a kernel driver already owns the device; here the only consumer is the BMC's management stack.

Yes, and I see no issues with that. Opening the whole i3cdev just for the same reason sounds like an overkill. Especially if we do *not* expect any more users.

OCP recovery isn't the only user. In her thread, Meagan described a separate one [1]: accelerator cards that plug into different server SKUs, where the I3C hubs and devices on the bus can differ per SKU, so the hub setup has to be discovered and programmed from userspace at runtime instead of being fixed in devicetree. She posted her own i3cdev series and can speak to the details better than I can.

Also, with the explicit binding planned for v6, i3cdev would not open the whole bus. It would only bind device classes listed in its ID table, starting with the OCP recovery DCR, and any other class would have to be added there deliberately, with its own justification.

Quoted 3 lines

If there are success stories, those examples might help to justify your approach. It makes sense to mention that in the cover letter with the summary on their designs (and split).

Will do. I'll summarize in the v6 cover letter.

[1] https://lore.kernel.org/linux-i3c/[email protected]/

Thanks,
Sam

#30 lore raw
In reply to Alexandre Belloni
Quoted 6 lines

On my side, I believe i3cdev can be added to the kernel and that it is way safer than i2cdev but I agree that the ABI has to be properly designed, maybe we need to version it or have a way to probe for supported features so we can update safely later on. It is failry simple right now but I feel like we are going to see people with more needs than initiating transfers.

For feature probing and ABI extension, what do you think of:

1. Adding an extensible I3C_IOC_GET_CAPS ioctl:

   struct i3c_ioc_caps {
       __u32 size; /* sizeof(struct i3c_ioc_caps) */
       __u32 pad;  /* must be 0 */
       __u64 caps; /* Bitmask of supported I3C_CAP_* features */
   };

Today caps can report the transfer modes supported by the controller and target (I3C_CAP_SDR, I3C_CAP_HDR_DDR, I3C_CAP_HDR_TSP, I3C_CAP_HDR_TSL), and new bits in caps (or new fields appended to the end of the struct) can advertise future capabilities beyond basic transfers.

2. In struct i3c_ioc_xfer, we already have __u8 pad[2] at the end which is strictly checked to be 0 (-EINVAL if non-zero), reserving 16 bits for future per-transfer flags without changing the struct size.

Quoted 2 lines

Also, I think this should not bind to any devices by default and make binding explicit.

This seems to be the consensus so far. I can convert i3cdev from a bus notifier into a standard struct i3c_driver so it does not auto-attach to all unbound targets on boot or on unbind while dropping patches 1 and 2.

Since Greg KH noted earlier [1] that driver_override / manual sysfs bind is only for debugging and will taint the kernel in the near future, what do you think of:

Matching explicit opt-in entries in i3cdev's i3c_device_id table (starting with 0xBD for OCP Secure Firmware Recovery), and optionally exposing a new_id sysfs attribute on the i3cdev driver for dynamically adding target IDs at runtime? That also keeps everything self-contained in i3cdev without needing cross-subsystem changes to hwmon/iio drivers.

[1] https://lore.kernel.org/linux-i3c/2026091834-bats-palpitate-5614@gregkh/

Thanks,
Sam

#31 lore raw
Quoted 17 lines · Sam Agazaryan
Quoted 6 lines

On my side, I believe i3cdev can be added to the kernel and that it is way safer than i2cdev but I agree that the ABI has to be properly designed, maybe we need to version it or have a way to probe for supported features so we can update safely later on. It is failry simple right now but I feel like we are going to see people with more needs than initiating transfers.

For feature probing and ABI extension, what do you think of:

1. Adding an extensible I3C_IOC_GET_CAPS ioctl:

   struct i3c_ioc_caps {
       __u32 size; /* sizeof(struct i3c_ioc_caps) */
       __u32 pad;  /* must be 0 */
       __u64 caps; /* Bitmask of supported I3C_CAP_* features */
   };
Quoted 5 lines

Today caps can report the transfer modes supported by the controller and target (I3C_CAP_SDR, I3C_CAP_HDR_DDR, I3C_CAP_HDR_TSP, I3C_CAP_HDR_TSL), and new bits in caps (or new fields appended to the end of the struct) can advertise future capabilities beyond basic transfers.

This works but my point was also that we need to expose the i3cdev capabilities so userspace can discover what is available. I guess we can assume .read and .write are going to always be available and so this is fine for now.

Quoted 17 lines

2. In struct i3c_ioc_xfer, we already have __u8 pad[2] at the end which is strictly checked to be 0 (-EINVAL if non-zero), reserving 16 bits for future per-transfer flags without changing the struct size.

Quoted 2 lines

Also, I think this should not bind to any devices by default and make binding explicit.

This seems to be the consensus so far. I can convert i3cdev from a bus notifier into a standard struct i3c_driver so it does not auto-attach to all unbound targets on boot or on unbind while dropping patches 1 and 2.

Since Greg KH noted earlier [1] that driver_override / manual sysfs bind is only for debugging and will taint the kernel in the near future, what do you think of:

Quoted 6 lines

Matching explicit opt-in entries in i3cdev's i3c_device_id table (starting with 0xBD for OCP Secure Firmware Recovery), and optionally exposing a new_id sysfs attribute on the i3cdev driver for dynamically adding target IDs at runtime? That also keeps everything self-contained in i3cdev without needing cross-subsystem changes to hwmon/iio drivers.

We can add new_id later if necessary.

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#32 lore raw
In reply to Sam Agazaryan
Quoted 11 lines · Sam Agazaryan
Quoted 1 line

Did I miss this in the initial explanation? If not, please add it next time.

That is just how the existing I3C core enumerates targets during DAA, not something added by this series. I will add a sentence to the cover letter and Patch 4 commit message in v6 contrasting it with i2c-dev to make that context clear.

Quoted 3 lines

Do we have such use cases IRL? Do we have tooling support (at least pending) somewhere? Why does it require user space interaction in _this_ way (meaning IOCTL and /dev node)?

Quoted 7 lines

Yes, this is used in real systems today:

1. Tooling & production users:

- i3ctransfer (Patch 5/5 of this series, and carried out-of-tree in AspeedTech-BMC/i3c-tools and vendor OpenBMC BSPs for several years).

So, nothing in production, meaning we can design that in kernel differently.

Quoted 15 lines

- OCP Secure Firmware Recovery (DCR 0xBD) [1] and Caliptra Root-of-Trust recovery agents on OpenBMC/gBMC platforms (e.g., AST2700 / Caliptra MCU recovery tooling and BMC recovery daemons). Because Vitor's original i3cdev series was not merged in 2019, multiple silicon vendors and OpenBMC trees have been carrying out-of-tree forks of i3cdev; upstreaming consolidates those diverging implementations.

2. Why an ioctl is required over a read and write:

- In the OCP Secure Firmware Recovery v1.1 specification (Section 8.4, "Recovery I3C Interface Block Read via Private Write and Private Read"), every recovery command read is a combined transaction consisting of a Private Write with no STOP, followed by a Repeated START, followed by a Private Read, and finally a STOP.

Quoted 7 lines

- Because write() and read() only submit a single transfer at a time (nxfers = 1), the controller emits a STOP condition on the wire at the end of write() and drops the bus lock. Submitting multiple i3c_ioc_xfer descriptors in a single I3C_IOC_XFER(N) ioctl is required so i3c_device_do_xfers() queues them together under one bus lock acquisition and the controller emits a Repeated START between messages (as well as passing HDR mode/cmd parameters).

Thanks, probably I was unclear, my specific question was why this needs a user space to issue those recovery flows. And why it needs to be IOCTL-based and not some higher level one.

Quoted 16 lines

[1] https://www.opencompute.org/documents/ocp-recovery-document-1p1-final-pdf

Quoted 8 lines

So, imagine that someone submits a kernel driver for a simple device, while another person already uses the driver for multi-functional device where the former one is driving only part of it. Strictly speaking the kernel driver submission becomes a user space regression. Which effectively means that whenever you open i3cdev, no other kernel drivers may appear. Yes, I understand that a poor victim of such an unlikely course of events may recompile the kernel, disable driver, et cetera, but this still raises the same question, why do we do that to the mere users?

Note that a user does not need to recompile the kernel if a kernel driver binds to a device they wish to access via i3cdev. Because i3cdev handles BUS_NOTIFY_UNBOUND_DRIVER, unbinding the kernel driver at runtime via sysfs:

Quoted 4 lines

echo <dev> > /sys/bus/i3c/devices/<dev>/driver/unbind

automatically triggers i3cdev_attach() and exposes /dev/bus/i3c/<dev> without rebuilding the kernel.

Oh, that's quite a dangerous "feature"! Can we avoid doing that? Otherwise it means any missed guard against unbind for the system controller related chips may lead to real HW damage. I'm totally against this feature to be in any way of an ABI. If you want that, do it exclusively via debugfs with a big fat warning that this may lead to HW damages.

Quoted 4 lines

Also, I3C drivers match against a
target's MIPI-assigned DCR or 48-bit PID (Manufacturer ID + Part ID), so a driver for a simple device will not accidentally match a different chip's PID or a dedicated DCR like 0xBD.

Yes, but we are talking about opening a Pandora's box in user space, aren't we?

Quoted 9 lines
Quoted 2 lines

P.S> Still not convinced, that this has to be an ABI. For debugfs and something like this, no objections, but ABI.

Production BMC recovery daemons cannot rely on debugfs, which is optional and restricted or unmounted on production builds. A character device in /dev/bus/i3c/* provides the stable ioctl those tools need and integrates with udev so non-root daemons can be granted access to specific target nodes (e.g. matching on DCR 0xBD), similar to spidev and hidraw.

But why recovery mechanism can't be kernel-driven? With some sysfs (just as an example) knobs to say "I want to run now a recovery flow on this bus", et cetera.

Quoted 5 lines
Quoted 1 line

Forgot to ask, but do they interfere?

Is this in reference to i2c devices on the i3c bus? No, I3C transfers and legacy I2C transfers on a mixed-mode bus do not interfere:

Quoted 7 lines

1. Both i3c_device_do_xfers() and i3c_master_i2c_adapter_xfer() acquire i3c_bus_normaluse_lock(&master->bus), so I3C and I2C transactions on the same bus are serialized by the I3C core.

2. Attached I2C client addresses are marked as I3C_ADDR_SLOT_I2C_DEV in the bus address slot table, preventing DAA from assigning an I2C device's address to an I3C target.

Can i2c device require an i2c recovery flow on the bus? If so, how would it be combined together with the i3c recovery flow(s)?

Signature · 2 lines
With Best Regards,
Andy Shevchenko