Skip to contentSkip to conversation

i3c: Fixes, cleanups and HDR-DDR support

PATCH Applied Adrian Hunter 4 participants 22 messages Sep 20 – Sep 21 I3C

lore
Share & tools
lore
#0 lore raw

v3 cover i3c: Fixes, cleanups and HDR-DDR support

Hi

This series brings a collection of fixes, specification-conformance cleanups and feature additions to the I3C core and MIPI I3C HCI driver.

The first three patches are independent fixes tagged for stable. The remaining patches address a mixture of correctness issues in the core and HCI driver, improve conformance with the I3C and HCI specifications, add support for configurable device NACK retries, and finally add HDR-DDR support.

The series is organised as follows:

Stable fixes (patches 1-3)
--------------------------

- Fix an out-of-bounds read in the core DMA bounce-buffer helper.

- Fix a destination buffer overrun in the HCI DMA path for reads whose length is not a multiple of four bytes. The issue is independent of IOMMU usage, although the existing workaround was only applied when an IOMMU was present.

- Close a probe-time race in the HCI PCI driver where the LTR sysfs attribute becomes visible before drvdata has been initialized.

Core fixes (patches 4-5)
------------------------

- Fix ACPI target enumeration on multi-instance controllers by extracting the bus-controller instance number and PID from the correct _ADR fields.

- Remove a stale GETSTATUS response-length check that became obsolete when actual_len-based validation was introduced.

HCI IBI and suspend fixes (patches 6-11)
----------------------------------------

- Restore controller state when enabling IBIs fails, so that a failed ENEC does not leave controller-side setup in place.

- Send DISEC before disabling IBIs in hardware so that an IBI racing with teardown is not silently dropped.

- Avoid accessing controller hardware from ->free_ibi(), where runtime PM does not guarantee the device is resumed.

- Drain all pending IBIs whenever INTR_IBI_READY is received instead of processing only the first queued entry.

- Reorganize the DMA suspend/resume callbacks in preparation for the following change.

- Rework DMA suspend handling to stop Ring Bundles according to the HCI specification, synchronize with the interrupt handler, and flush any pending IBI before disabling the rings.

HCI specification cleanups (patches 12-14)
-------------------------------------------

- Correct the Response Descriptor DATA_LENGTH field definition.

- Remove an invalid maximum-transfer-size capability derived from reserved HC_CAPABILITIES bits.

- Remove HDR-BT and Fm/Fm+ definitions that are not defined by any released version of the HCI specification.

Feature additions (patches 15-17)
---------------------------------

- Implement the .set_dev_nack_retry() master operation using DAT support provided by HCI v1 controllers.

- Restrict advertised and permitted HDR modes to the intersection of:
* controller capabilities,
* bus-configuration constraints, and
* target capabilities.

- Add HDR-DDR private-transfer support to the HCI driver, including descriptor generation and capability advertisement when supported by both the controller and the active command-descriptor format.

Changes in V3:

i3c: mipi-i3c-hci: Support configurable device NACK retries
Moved the retry count range check and the loop over allocated DAT entries from the DAT layer into i3c_hci_set_dev_nack_retry(), and removed the set_all_nack_retry() DAT operation. Moved the DAT_0_DEV_NACK_RETRY_CNT definition to dat.h so that it is visible to the caller.
Amended the commit message accordingly.

Added Frank Li's Reviewed-by tags.

Changes in V2:

i3c: mipi-i3c-hci: Bounce short reads irrespective of the IOMMU
Added the slub_debug report to the commit message.

i3c: mipi-i3c-hci: Restore controller state if i3c_hci_enable_ibi() returns an error
Reworded the subject, which was previously "i3c: mipi-i3c-hci:
Fix i3c_hci_enable_ibi() error path", to say what the patch does rather than that it fixes something.

i3c: mipi-i3c-hci: Stop rings gracefully when suspending
Added comments explaining the choice of the RING_STOP_TIMEOUT_US and RING_STOP_SLEEP_US values.

i3c: mipi-i3c-hci: Correct RESP_DATA_LENGTH to bits 15:0
Reworded the subject, which was previously "i3c: mipi-i3c-hci:
Fix Response Descriptor DATA_LENGTH mask", to name the macro and the corrected bit range.

Added Frank Li's Reviewed-by tags.

Adrian Hunter (17):
i3c: master: Fix out-of-bounds read in DMA bounce buffer setup
i3c: mipi-i3c-hci: Bounce short reads irrespective of the IOMMU
i3c: mipi-i3c-hci-pci: Set drvdata before creating LTR sysfs attribute
i3c: master: Match ACPI targets to the correct bus controller instance
i3c: master: Remove stale GETSTATUS length check
i3c: mipi-i3c-hci: Restore controller state if i3c_hci_enable_ibi() returns an error
i3c: mipi-i3c-hci: Send DISEC before disabling IBIs in hardware
i3c: mipi-i3c-hci: Fix runtime PM violation in i3c_hci_free_ibi()
i3c: mipi-i3c-hci: Process multiple IBIs per interrupt
i3c: mipi-i3c-hci: Move DMA suspend/resume callbacks
i3c: mipi-i3c-hci: Stop rings gracefully when suspending
i3c: mipi-i3c-hci: Correct RESP_DATA_LENGTH to bits 15:0
i3c: mipi-i3c-hci: Remove invalid transfer size limit
i3c: mipi-i3c-hci: Remove invalid HDR-BT and Fm/Fm+ definitions
i3c: mipi-i3c-hci: Support configurable device NACK retries
i3c: Restrict HDR modes to those supported by the bus and target
i3c: mipi-i3c-hci: Add HDR-DDR support

 drivers/i3c/device.c                               |  15 ++-
 drivers/i3c/internals.h                            |   2 +
 drivers/i3c/master.c                               |  88 +++++++++++--
 drivers/i3c/master/mipi-i3c-hci/cmd.h              |   4 +-
 drivers/i3c/master/mipi-i3c-hci/cmd_v1.c           |  28 ++++-
 drivers/i3c/master/mipi-i3c-hci/cmd_v2.c           |   4 +-
 drivers/i3c/master/mipi-i3c-hci/core.c             | 106 +++++++++++++---
 drivers/i3c/master/mipi-i3c-hci/dat.h              |   2 +
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c           |  13 +-
 drivers/i3c/master/mipi-i3c-hci/dma.c              | 136 +++++++++++++++------
 drivers/i3c/master/mipi-i3c-hci/hci.h              |   9 +-
 drivers/i3c/master/mipi-i3c-hci/mipi-i3c-hci-pci.c |   5 +-
 include/linux/i3c/device.h                         |   4 +
 include/linux/i3c/master.h                         |   3 +
 include/linux/platform_data/mipi-i3c-hci.h         |   4 +
 15 files changed, 335 insertions(+), 88 deletions(-)

Regards
Adrian

#1 lore raw

PATCH v3 1/17 i3c: master: Fix out-of-bounds read in DMA bounce buffer setup

When a bounce buffer is required for DMA_TO_DEVICE transfers, i3c_master_dma_map_single() rounds the DMA mapping length up to a cache-line boundary:

	map_len = ALIGN(len, cache_line_size());

It then allocates the bounce buffer with:

	kmemdup(buf, map_len, GFP_KERNEL);

kmemdup() copies the full allocation size, causing it to read map_len bytes from buf even though only len bytes are valid. This results in an out-of-bounds read of up to cache_line_size() - 1 bytes past the end of the caller's buffer.

Fix the issue by allocating the bounce buffer with kzalloc() and copying only len bytes from the original buffer. The remaining bytes up to map_len stay zero-filled, avoiding both the out-of-bounds read and exposure of unrelated memory contents to the DMA engine.

Fixes f8d9e56aeb87 ("i3c: master: Add helpers for DMA mapping and bounce buffer handling")

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
drivers/i3c/master.c +3 -4
index afcd7a21a3e6..f9a6c8560fab 100644
@@ -2216,12 +2216,11 @@ struct i3c_dma *i3c_master_dma_map_single(struct device *dev, void *buf,
 
 	if (force_bounce) {
 		dma_xfer->map_len = ALIGN(len, cache_line_size());
-		if (dir == DMA_FROM_DEVICE)
-			bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL);
-		else
-			bounce = kmemdup(buf, dma_xfer->map_len, GFP_KERNEL);
+		bounce = kzalloc(dma_xfer->map_len, GFP_KERNEL);
 		if (!bounce)
 			return NULL;
+		if (dir != DMA_FROM_DEVICE)
+			memcpy(bounce, buf, len);
 		dma_buf = bounce;
 	}
 
Signature · 1 line
2.53.0
#2 lore raw

PATCH v3 2/17 i3c: mipi-i3c-hci: Bounce short reads irrespective of the IOMMU

In reply to Adrian Hunter

The controller writes whole DWORDs, so a read whose length is not a multiple of 4 overwrites up to 3 bytes past the end of the destination buffer. That is a property of the controller, not of the IOMMU, but the bounce buffer that works around it was used only when the device was IOMMU mapped. Everywhere else the buffer is left unprotected.

Drop the device_iommu_mapped() condition.

The overrun is easily seen with CONFIG_SLUB_DEBUG=y and kernel command line options intel_iommu=off slub_debug=FZPU, which reports it as a kmalloc redzone overwrite, like:

[kmalloc Redzone overwritten] 0xffff8a354561570e-0xffff8a354561570f @offset=1806. First byte 0x15 instead of 0xcc
=============================================================================
BUG kmalloc-8 (Not tainted): Object corrupt
Allocated in i3c_master_retrieve_dev_info+0xc1/0x760 age=40 cpu=6 pid=1
...
Freed in i3c_master_enec_disec_locked+0xeb/0x140 age=40 cpu=6 pid=1
...

  WARNING: mm/slub.c:1233 at object_err+0x1c1/0x1cf, CPU#6: swapper/0/1
  ...
Fixes 9e23897bca62 ("i3c: mipi-i3c-hci: Use physical device pointer with DMA API")

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

Added the slub_debug report to the commit message.

 drivers/i3c/master/mipi-i3c-hci/dma.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +1 -1
index 7c2b20474130..5b195978f376 100644
@@ -428,7 +428,7 @@ static void hci_dma_unmap_xfer(struct i3c_hci *hci,
 static struct i3c_dma *hci_dma_map_xfer(struct device *dev, struct hci_xfer *xfer)
 {
 	enum dma_data_direction dir = xfer->rnw ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
-	bool need_bounce = device_iommu_mapped(dev) && xfer->rnw && (xfer->data_len & 3);
+	bool need_bounce = xfer->rnw && (xfer->data_len & 3);
 
 	return i3c_master_dma_map_single(dev, xfer->data, xfer->data_len, need_bounce, dir);
 }
Signature · 1 line
2.53.0
#3 lore raw

PATCH v3 3/17 i3c: mipi-i3c-hci-pci: Set drvdata before creating LTR sysfs attribute

In reply to Adrian Hunter

intel_i3c_init() creates the pm_qos_latency_tolerance_us sysfs attribute before probe calls pci_set_drvdata(). A userspace write to the attribute can therefore invoke intel_ltr_set(), which uses pci_get_drvdata(), before drvdata has been initialized.

Move pci_set_drvdata() before hci->info->init() so drvdata is available before the sysfs attribute becomes accessible.

Fixes 884a33131f2a ("i3c: mipi-i3c-hci-pci: Add LTR support for Intel controllers")

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/mipi-i3c-hci-pci.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/mipi-i3c-hci-pci.c +2 -2
index ab595661db1e..af9595009b03 100644
@@ -445,6 +445,8 @@ static int mipi_i3c_hci_pci_probe(struct pci_dev *pci,
 	if (ret < 0)
 		return ret;
 
+	pci_set_drvdata(pci, hci);
+
 	hci->info = (const struct mipi_i3c_hci_pci_info *)id->driver_data;
 
 	ret = hci->info->init ? hci->info->init(hci) : 0;
@@ -455,8 +457,6 @@ static int mipi_i3c_hci_pci_probe(struct pci_dev *pci,
 	if (ret)
 		goto err_exit;
 
-	pci_set_drvdata(pci, hci);
-
 	mipi_i3c_hci_pci_rpm_allow(&pci->dev);
 
 	return 0;
Signature · 1 line
2.53.0
#4 lore raw

PATCH v3 4/17 i3c: master: Match ACPI targets to the correct bus controller instance

In reply to Adrian Hunter

For ACPI-enumerated I3C targets, _ADR encodes both the zero-based bus controller instance number in bits 51:48 and the Provisioned ID (PID) in bits 47:0, as defined by ACPI 6.6 Table 6.2 and MIPI DisCo for I3C v1.1 section 5.2.

However, i3c_master_add_acpi_dev() treats the entire _ADR value as the PID. On multi-instance controllers this folds the instance number into the PID, causing i3c_master_add_i3c_boardinfo() to reject devices on instance 1 and above with -EINVAL.

The problem is exposed by the MIPI I3C HCI PCI driver, which creates one MFD child device per bus controller instance. Since the child devices do not have their own ACPI companions, they all inherit the parent's ACPI node and therefore enumerate the same set of child devices. As a result, a target belonging to any instance other than 0 prevents registration of all controller instances.

Add an instance number to struct i3c_master_controller, propagate it from the HCI PCI driver, extract the PID from bits 47:0 of _ADR, and ignore ACPI child nodes that belong to a different controller instance.

ACPI-enumerated I2C devices are unaffected. Their controller instance is encoded in the Resource Source Index of the I2cSerialBusV2 descriptor rather than in _ADR and will require separate handling.

Fixes b46a4b3c5d1e ("i3c: master: Support ACPI enumeration of child devices")

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master.c                          | 19 +++++++++++++++++--
 drivers/i3c/master/mipi-i3c-hci/core.c        |  4 +++-
 .../master/mipi-i3c-hci/mipi-i3c-hci-pci.c    |  1 +
 include/linux/i3c/master.h                    |  3 +++
 include/linux/platform_data/mipi-i3c-hci.h    |  4 ++++
 5 files changed, 28 insertions(+), 3 deletions(-)
drivers/i3c/master.c +17 -2
index f9a6c8560fab..215a22f2c7b6 100644
@@ -8,6 +8,7 @@
 #include <dt-bindings/i3c/i3c.h>
 #include <linux/acpi.h>
 #include <linux/atomic.h>
+#include <linux/bitfield.h>
 #include <linux/bitmap.h>
 #include <linux/bug.h>
 #include <linux/delay.h>
@@ -3076,6 +3077,13 @@ static int i3c_master_add_of_dev(struct i3c_master_controller *master,
 	return ret;
 }
 
+#define I3C_ACPI_ADR_PID GENMASK_U64(47, 0)
+/*
+ * Zero-based instance number of the Bus Controller to which the Target is
+ * connected.
+ */
+#define I3C_ACPI_ADR_INSTANCE GENMASK_U64(51, 48)
+
 #ifdef CONFIG_ACPI
 static int i3c_master_add_acpi_dev(struct i3c_master_controller *master,
 				   struct fwnode_handle *fwnode)
@@ -3083,6 +3091,7 @@ static int i3c_master_add_acpi_dev(struct i3c_master_controller *master,
 	struct acpi_device *adev = to_acpi_device_node(fwnode);
 	acpi_bus_address adr;
 	u32 reg[3] = { 0 };
+	u64 pid;
 	int ret;
 
 	/*
@@ -3100,9 +3109,15 @@ static int i3c_master_add_acpi_dev(struct i3c_master_controller *master,
 
 	adr = acpi_device_adr(adev);
 
+	/* Match the multi-bus instance number */
+	if (FIELD_GET(I3C_ACPI_ADR_INSTANCE, adr) != master->instance)
+		return 0;
+
 	/* For I3C devices, _ADR will have the 48 bit PID of the device  */
-	reg[1] = upper_32_bits(adr);
-	reg[2] = lower_32_bits(adr);
+	pid = FIELD_GET(I3C_ACPI_ADR_PID, adr);
+
+	reg[1] = upper_32_bits(pid);
+	reg[2] = lower_32_bits(pid);
 
 	fwnode_property_read_u32(fwnode, "mipi-i3c-static-address", &reg[0]);
 
drivers/i3c/master/mipi-i3c-hci/core.c +3 -1
index dadf049bd4b5..8b3a87d8a8bb 100644
@@ -1142,8 +1142,10 @@ static int i3c_hci_probe(struct platform_device *pdev)
 	 * necessarily in separate contiguous sub-ranges. To avoid overlapping
 	 * mappings, provide base_regs from the parent mapping.
 	 */
-	if (pdata)
+	if (pdata) {
 		hci->base_regs = pdata->base_regs;
+		hci->master.instance = pdata->instance;
+	}
 
 	if (!hci->base_regs) {
 		hci->base_regs = devm_platform_ioremap_resource(pdev, 0);
drivers/i3c/master/mipi-i3c-hci/mipi-i3c-hci-pci.c +1 -0
index af9595009b03..5b8e10d05cee 100644
@@ -390,6 +390,7 @@ static void mipi_i3c_hci_pci_setup_cell(struct mipi_i3c_hci_pci *hci, int idx,
 					struct mfd_cell *cell)
 {
 	data->pdata.base_regs = hci->base + hci->info->instance_offset[idx];
+	data->pdata.instance = idx;
 
 	data->res = DEFINE_RES_IRQ(0);
 
include/linux/i3c/master.h +3 -0
index f7ceec2b4477..72065715feee 100644
@@ -536,6 +536,8 @@ struct i3c_master_controller_ops {
  *		 Bit 0: SETDASA
  *		 Bit 1: SETAASA
  *		 All other bits are reserved.
+ * @instance: Zero-based instance number of the Bus Controller as defined by the
+ *            DisCo specification I3C Target Address (_ADR) Encoding
  * @wq: freezable workqueue which can be used by master
  *	drivers if they need to postpone operations that need to take place
  *	in a thread context. Typical examples are Hot Join processing which
@@ -573,6 +575,7 @@ struct i3c_master_controller {
 	} boardinfo;
 	struct i3c_bus bus;
 	u8 addr_method;
+	u8 instance;
 	struct workqueue_struct *wq;
 	struct work_struct hj_work;
 	struct work_struct reg_work;
include/linux/platform_data/mipi-i3c-hci.h +4 -0
index ab7395f455f9..6f61d8a02378 100644
@@ -3,13 +3,17 @@
 #define INCLUDE_PLATFORM_DATA_MIPI_I3C_HCI_H
 
 #include <linux/compiler_types.h>
+#include <linux/types.h>
 
 /**
  * struct mipi_i3c_hci_platform_data - Platform-dependent data for mipi_i3c_hci
  * @base_regs: Register set base address (to support multi-bus instances)
+ * @instance: Zero-based instance number of the Bus Controller as defined by the
+ *            DisCo specification I3C Target Address (_ADR) Encoding
  */
 struct mipi_i3c_hci_platform_data {
 	void __iomem *base_regs;
+	u8 instance;
 };
 
 #endif
Signature · 1 line
2.53.0
#5 lore raw

PATCH v3 5/17 i3c: master: Remove stale GETSTATUS length check

In reply to Adrian Hunter

i3c_master_getstatus_locked() verifies dest.payload.len against sizeof(*getstatus) after issuing a GETSTATUS CCC.

This check predates the introduction of dest.payload.actual_len. Since then, dest.payload.len has held the requested transfer length and dest.payload.actual_len has held the received length, so the check is always false and can never detect a short response.

It also predates the addition of generic read CCC response length validation in i3c_master_send_ccc_cmd_locked(), which now performs the necessary check before returning success.

Remove the stale check.

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master.c | 5 -----
 1 file changed, 5 deletions(-)
drivers/i3c/master.c +0 -5
index 215a22f2c7b6..4296c51a9537 100644
@@ -1777,11 +1777,6 @@ static int i3c_master_getstatus_locked(struct i3c_master_controller *master,
 	if (ret)
 		goto out;
 
-	if (dest.payload.len != sizeof(*getstatus)) {
-		ret = -EIO;
-		goto out;
-	}
-
 	if (status)
 		*status = be16_to_cpu(getstatus->status);
 out:
Signature · 1 line
2.53.0
#6 lore raw

PATCH v3 6/17 i3c: mipi-i3c-hci: Restore controller state if i3c_hci_enable_ibi() returns an error

In reply to Adrian Hunter

i3c_hci_enable_ibi() performs controller-side setup before sending the ENEC CCC to enable IBIs on the target. If the ENEC CCC fails, the function returns an error but leaves the controller configured as if IBI enabling had succeeded.

Fix the error path by undoing the earlier setup when the ENEC CCC fails, ensuring the controller state remains consistent with the failed enable operation.

Fixes 9ad9a52cce282 ("i3c/master: introduce the mipi-i3c-hci driver")

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

Reworded the subject, which was previously "i3c: mipi-i3c-hci:
Fix i3c_hci_enable_ibi() error path", to say what the patch does rather than that it fixes something.

 drivers/i3c/master/mipi-i3c-hci/core.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +5 -1
index 8b3a87d8a8bb..166d9f70f435 100644
@@ -703,11 +703,15 @@ static int i3c_hci_enable_ibi(struct i3c_dev_desc *dev)
 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
 	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
+	int ret;
 
 	mipi_i3c_hci_dat_v1.clear_flags(hci, dev_data->dat_idx, DAT_0_SIR_REJECT, 0);
 	scoped_guard(spinlock_irqsave, &hci->lock)
 		hci->ibi_devs[dev_data->dat_idx] = dev;
-	return i3c_master_enec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR);
+	ret = i3c_master_enec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR);
+	if (ret)
+		__i3c_hci_disable_ibi(hci, dev);
+	return ret;
 }
 
 static int i3c_hci_disable_ibi(struct i3c_dev_desc *dev)
Signature · 1 line
2.53.0
#7 lore raw

PATCH v3 7/17 i3c: mipi-i3c-hci: Send DISEC before disabling IBIs in hardware

In reply to Adrian Hunter

Reorder i3c_hci_disable_ibi() so that DISEC is sent before IBIs are disabled at the controller.

This matches the sequence used by other I3C controller drivers. It also avoids dropping an IBI that races with the disable operation: an IBI received after DISEC is issued but before the controller disables IBIs can still be processed normally. Once DISEC has been sent, the hardware can be updated to NACK subsequent IBIs and complete the teardown.

No functional change is intended beyond aligning the ordering with other drivers and handling the race window consistently.

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/core.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +3 -2
index 166d9f70f435..f6a34ce7a295 100644
@@ -719,13 +719,14 @@ static int i3c_hci_disable_ibi(struct i3c_dev_desc *dev)
 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
 
-	__i3c_hci_disable_ibi(hci, dev);
 	/*
-	 * The DAT entry is now set to NACK and DISEC this target's IBIs, so
+	 * The DAT entry will be set to NACK and DISEC this target's IBIs, so
 	 * the IBI teardown can proceed even if DISEC below fails, so ignore
 	 * errors.
 	 */
 	i3c_master_disec_locked(m, dev->info.dyn_addr, I3C_CCC_EVENT_SIR);
+
+	__i3c_hci_disable_ibi(hci, dev);
 	return 0;
 }
 
Signature · 1 line
2.53.0
#8 lore raw

PATCH v3 8/17 i3c: mipi-i3c-hci: Fix runtime PM violation in i3c_hci_free_ibi()

In reply to Adrian Hunter

i3c_hci_free_ibi() calls __i3c_hci_disable_ibi(), but the latter requires runtime PM to be resumed. The ->free_ibi() callback does not guarantee that condition.

The cases where ->free_ibi() may be called without a preceding ->disable_ibi() do not require re-enabling runtime PM handling:

- runtime resume failed in i3c_dev_free_ibi_locked(), so it skips the IBI disable but still proceeds to call ->free_ibi()
- the device has been re-enumerated under a new dynamic address in __i3c_master_add_i3c_dev_locked(), which tears down the old device descriptor and deliberately clears olddev->ibi->enabled (to avoid sending a DISEC to the stale old address) before calling i3c_dev_free_ibi_locked(). As a result ->free_ibi() is reached with neither a hardware IBI disable nor runtime PM resumed.

In both cases the important requirement is to prevent further IBI processing for the device. That can be done without accessing hardware by removing the device from the ibi_devs[] table.

Replace the call to __i3c_hci_disable_ibi() with clearing the corresponding ibi_devs[] entry under the controller lock.

Fixes 650716f23eac4 ("i3c: mipi-i3c-hci: Fix race in i3c_hci_addr_to_dev()")

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

None

 drivers/i3c/master/mipi-i3c-hci/core.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +4 -2
index f6a34ce7a295..b9b20797d045 100644
@@ -675,11 +675,13 @@ static void __i3c_hci_disable_ibi(struct i3c_hci *hci, struct i3c_dev_desc *dev)
 
 static void i3c_hci_free_ibi(struct i3c_dev_desc *dev)
 {
+	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
 	struct i3c_hci *hci = to_i3c_hci(m);
 
-	/* Must ensure the IBI has been disabled */
-	__i3c_hci_disable_ibi(hci, dev);
+	/* Must ensure IBIs for this device will no longer be processed */
+	scoped_guard(spinlock_irqsave, &hci->lock)
+		hci->ibi_devs[dev_data->dat_idx] = NULL;
 	hci->io->free_ibi(hci, dev);
 }
 
Signature · 1 line
2.53.0
#9 lore raw

PATCH v3 9/17 i3c: mipi-i3c-hci: Process multiple IBIs per interrupt

In reply to Adrian Hunter

INTR_IBI_READY indicates that one or more IBI Status Descriptors are present in the IBI ring. After clearing interrupt status, the interrupt handler processes only a single IBI even though additional IBIs may already be queued.

The controller does not reassert INTR_IBI_READY solely because entries remain in the ring after interrupt status has been cleared. Consequently, the remaining IBIs are not processed until some later interrupt occurs, and can accumulate if IBIs arrive more often than the interrupt handler runs.

Process all IBIs that are pending when the handler runs: capture the IBI enqueue pointer up front and continue until the dequeue pointer reaches that position.

Fixes 9ad9a52cce28 ("i3c/master: introduce the mipi-i3c-hci driver")

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

None

 drivers/i3c/master/mipi-i3c-hci/dma.c | 39 +++++++++++++++++----------
 1 file changed, 25 insertions(+), 14 deletions(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +25 -14
index 5b195978f376..a798b0648922 100644
@@ -868,25 +868,24 @@ static void hci_dma_recycle_ibi_slot(struct i3c_hci *hci,
 	i3c_generic_ibi_recycle_slot(dev_ibi->pool, slot);
 }
 
-static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
+static bool hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh,
+				u32 *op1_val, unsigned int enq_ptr)
 {
 	struct hci_rings_data *rings = hci->io_data;
 	struct i3c_dev_desc *dev;
 	struct i3c_hci_dev_data *dev_data;
 	struct hci_dma_dev_ibi_data *dev_ibi;
 	struct i3c_ibi_slot *slot;
-	u32 op1_val, op2_val, ibi_status_error;
-	unsigned int ptr, enq_ptr, deq_ptr;
+	u32 ibi_status_error;
+	unsigned int ptr, deq_ptr;
 	unsigned int ibi_size, ibi_chunks, ibi_data_offset, first_part;
 	int ibi_addr, last_ptr;
 	void *ring_ibi_data;
 	dma_addr_t ring_ibi_data_dma;
 
-	op1_val = rh_reg_read(RING_OPERATION1);
-	deq_ptr = FIELD_GET(RING_OP1_IBI_DEQ_PTR, op1_val);
-
-	op2_val = rh_reg_read(RING_OPERATION2);
-	enq_ptr = FIELD_GET(RING_OP2_IBI_ENQ_PTR, op2_val);
+	deq_ptr = FIELD_GET(RING_OP1_IBI_DEQ_PTR, *op1_val);
+	if (deq_ptr == enq_ptr)
+		return false;
 
 	ibi_status_error = 0;
 	ibi_addr = -1;
@@ -936,7 +935,7 @@ static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
 		dev_dbg(&hci->master.dev,
 			"no LAST_STATUS available (e=%d d=%d)",
 			enq_ptr, deq_ptr);
-		return;
+		return false;
 	}
 	deq_ptr = last_ptr + 1;
 	deq_ptr %= rh->ibi_status_entries;
@@ -1015,10 +1014,9 @@ static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
 	i3c_master_queue_ibi(dev, slot);
 
 done:
-	op1_val = rh_reg_read(RING_OPERATION1);
-	op1_val &= ~RING_OP1_IBI_DEQ_PTR;
-	op1_val |= FIELD_PREP(RING_OP1_IBI_DEQ_PTR, deq_ptr);
-	rh_reg_write(RING_OPERATION1, op1_val);
+	*op1_val &= ~RING_OP1_IBI_DEQ_PTR;
+	*op1_val |= FIELD_PREP(RING_OP1_IBI_DEQ_PTR, deq_ptr);
+	rh_reg_write(RING_OPERATION1, *op1_val);
 
 	/* update the chunk pointer */
 	rh->ibi_chunk_ptr += ibi_chunks;
@@ -1026,6 +1024,19 @@ static void hci_dma_process_ibi(struct i3c_hci *hci, struct hci_rh_data *rh)
 
 	/* and tell the hardware about freed chunks */
 	rh_reg_write(CHUNK_CONTROL, rh_reg_read(CHUNK_CONTROL) + ibi_chunks);
+
+	return true;
+}
+
+static void hci_dma_drain_ibi_ring(struct i3c_hci *hci, struct hci_rh_data *rh)
+{
+	u32 op1_val = rh_reg_read(RING_OPERATION1);
+	u32 op2_val = rh_reg_read(RING_OPERATION2);
+	unsigned int enq_ptr = FIELD_GET(RING_OP2_IBI_ENQ_PTR, op2_val);
+
+	/* Loop is bounded by enq_ptr. Further IBIs will re-assert INTR_IBI_READY */
+	while (hci_dma_process_ibi(hci, rh, &op1_val, enq_ptr))
+		;
 }
 
 static bool hci_dma_irq_handler(struct i3c_hci *hci)
@@ -1047,7 +1058,7 @@ static bool hci_dma_irq_handler(struct i3c_hci *hci)
 		rh_reg_write(INTR_STATUS, status);
 
 		if (status & INTR_IBI_READY)
-			hci_dma_process_ibi(hci, rh);
+			hci_dma_drain_ibi_ring(hci, rh);
 		if (status & (INTR_TRANSFER_COMPLETION | INTR_TRANSFER_ERR))
 			hci_dma_xfer_done(hci, rh);
 		if (status & INTR_RING_OP)
Signature · 1 line
2.53.0
#10 lore raw

PATCH v3 10/17 i3c: mipi-i3c-hci: Move DMA suspend/resume callbacks

In reply to Adrian Hunter

Relocate hci_dma_suspend() and hci_dma_resume() closer to the DMA IO operations table in preparation for upcoming changes.

No functional change.

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/dma.c | 46 +++++++++++++--------------
 1 file changed, 23 insertions(+), 23 deletions(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +23 -23
index a798b0648922..ec4b469abd33 100644
@@ -273,29 +273,6 @@ static void hci_dma_init_rings(struct i3c_hci *hci)
 		hci_dma_init_rh(hci, &rings->headers[i], i);
 }
 
-static void hci_dma_suspend(struct i3c_hci *hci)
-{
-	struct hci_rings_data *rings = hci->io_data;
-	int n = rings ? rings->total : 0;
-
-	for (int i = 0; i < n; i++) {
-		struct hci_rh_data *rh = &rings->headers[i];
-
-		rh_reg_write(INTR_SIGNAL_ENABLE, 0);
-		rh_reg_write(RING_CONTROL, 0);
-	}
-
-	i3c_hci_sync_irq_inactive(hci);
-}
-
-static void hci_dma_resume(struct i3c_hci *hci)
-{
-	struct hci_rings_data *rings = hci->io_data;
-
-	if (rings)
-		hci_dma_init_rings(hci);
-}
-
 static int hci_dma_init(struct i3c_hci *hci)
 {
 	struct hci_rings_data *rings;
@@ -1075,6 +1052,29 @@ static bool hci_dma_irq_handler(struct i3c_hci *hci)
 	return handled;
 }
 
+static void hci_dma_suspend(struct i3c_hci *hci)
+{
+	struct hci_rings_data *rings = hci->io_data;
+	int n = rings ? rings->total : 0;
+
+	for (int i = 0; i < n; i++) {
+		struct hci_rh_data *rh = &rings->headers[i];
+
+		rh_reg_write(INTR_SIGNAL_ENABLE, 0);
+		rh_reg_write(RING_CONTROL, 0);
+	}
+
+	i3c_hci_sync_irq_inactive(hci);
+}
+
+static void hci_dma_resume(struct i3c_hci *hci)
+{
+	struct hci_rings_data *rings = hci->io_data;
+
+	if (rings)
+		hci_dma_init_rings(hci);
+}
+
 const struct hci_io_ops mipi_i3c_hci_dma = {
 	.init			= hci_dma_init,
 	.cleanup		= hci_dma_cleanup,
Signature · 1 line
2.53.0
#11 lore raw

PATCH v3 11/17 i3c: mipi-i3c-hci: Stop rings gracefully when suspending

In reply to Adrian Hunter

hci_dma_suspend() tore the rings down with a single write of zero to RH_RING_CONTROL, clearing the RS and ENABLE fields together and without waiting for the ring to stop. I3C HCI v1.1 section 6.1.2 separates those
steps: clear RS for all running Ring Bundles, and only then clear ENABLE for all enabled Ring Bundles.

The ring registers were also written outside hci->lock, while the interrupt handler, which takes that lock, could still be running on another CPU. i3c_hci_sync_irq_inactive() was called only afterwards.

Finally, an IBI can still be sitting in the IBI Status Ring when suspend runs. Clearing HC_CONTROL.BUS_ENABLE is deferred: per the description of that field, if a disable request occurs while receiving an IBI, the actual disabling does not occur until reception of the IBI is complete.

Instead, clear RS under hci->lock, wait for RING_STATUS_RUNNING to clear, and make the interrupt handler inactive. Only then disable the ring interrupt signals, drain anything left in the IBI ring, and clear ENABLE.

Fixes 816958720443 ("i3c: mipi-i3c-hci: Add DMA suspend and resume support")

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

Added comments explaining the choice of the RING_STOP_TIMEOUT_US and RING_STOP_SLEEP_US values.

 drivers/i3c/master/mipi-i3c-hci/dma.c | 57 +++++++++++++++++++++++++--
 1 file changed, 53 insertions(+), 4 deletions(-)
drivers/i3c/master/mipi-i3c-hci/dma.c +53 -4
index ec4b469abd33..b8fcea0870ab 100644
@@ -15,6 +15,7 @@
 #include <linux/errno.h>
 #include <linux/i3c/master.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 
 #include "hci.h"
 #include "cmd.h"
@@ -1052,19 +1053,67 @@ static bool hci_dma_irq_handler(struct i3c_hci *hci)
 	return handled;
 }
 
+/*
+ * With the bus disabled, a ring should stop within a few microseconds. The
+ * timeout is therefore only expected to expire if the hardware is stuck.
+ * Allow sufficient margin for slow systems, but keep the delay acceptable
+ * during suspend.
+ */
+#define RING_STOP_TIMEOUT_US	(100 * USEC_PER_MSEC)
+/*
+ * The ring is usually already stopped, so polling typically completes on the
+ * first iteration. Use a modest sleep interval to avoid busy-waiting without
+ * adding excessive latency.
+ */
+#define RING_STOP_SLEEP_US	100
+
 static void hci_dma_suspend(struct i3c_hci *hci)
 {
 	struct hci_rings_data *rings = hci->io_data;
 	int n = rings ? rings->total : 0;
+	struct hci_rh_data *rh;
+	u32 regval;
 
-	for (int i = 0; i < n; i++) {
-		struct hci_rh_data *rh = &rings->headers[i];
+	/* Gracefully stop the rings */
+	scoped_guard(spinlock_irqsave, &hci->lock) {
+		for (int i = 0; i < n; i++) {
+			rh = &rings->headers[i];
+			regval = rh_reg_read(RING_CONTROL);
+			if (regval & RING_CTRL_RUN_STOP)
+				rh_reg_write(RING_CONTROL, regval & ~RING_CTRL_RUN_STOP);
+		}
+	}
 
-		rh_reg_write(INTR_SIGNAL_ENABLE, 0);
-		rh_reg_write(RING_CONTROL, 0);
+	/* Wait for actual stop */
+	for (int i = 0; i < n; i++) {
+		rh = &rings->headers[i];
+		if (readx_poll_timeout(readl, rh->regs + RH_RING_STATUS, regval,
+				       !(regval & RING_STATUS_RUNNING),
+				       RING_STOP_SLEEP_US, RING_STOP_TIMEOUT_US))
+			dev_err(&hci->master.dev, "%s: Ring did not stop, status %#x\n",
+				__func__, regval);
 	}
 
+	/*
+	 * With the rings stopped, no more IBIs can be received. Flush and make
+	 * the interrupt handler inactive.
+	 */
 	i3c_hci_sync_irq_inactive(hci);
+
+	/* Disable interrupt signals and disable the rings */
+	scoped_guard(spinlock_irqsave, &hci->lock)
+		for (int i = 0; i < n; i++) {
+			rh = &rings->headers[i];
+			rh_reg_write(INTR_SIGNAL_ENABLE, 0);
+			/*
+			 * Be absolutely certain there is no unprocessed IBI.
+			 * hci_dma_drain_ibi_ring() will do nothing if there is
+			 * none.
+			 */
+			if (i < IBI_RINGS)
+				hci_dma_drain_ibi_ring(hci, rh);
+			rh_reg_write(RING_CONTROL, 0);
+		}
 }
 
 static void hci_dma_resume(struct i3c_hci *hci)
Signature · 1 line
2.53.0
#12 lore raw

PATCH v3 12/17 i3c: mipi-i3c-hci: Correct RESP_DATA_LENGTH to bits 15:0

In reply to Adrian Hunter

The I3C HCI Response Descriptor defines DATA_LENGTH in Bits[15:0], with Bits[23:16] reserved in all published HCI specification versions:
v1.0 (Table 91) and v1.1/v1.2 (Section 8.5). However, RESP_DATA_LENGTH extracts Bits[21:0], incorrectly including six reserved bits.

Update the mask to match the defined field width.

No functional change is expected because compliant controllers return zero in reserved fields.

---

Changes in V3:

Added Frank Li's Reviewed-by tag.

Changes in V2:

Reworded the subject, which was previously "i3c: mipi-i3c-hci:
Fix Response Descriptor DATA_LENGTH mask", to name the macro and the corrected bit range.

 drivers/i3c/master/mipi-i3c-hci/cmd.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/cmd.h +1 -1
index 7bada7b4b2de..e0d4a6e0e319 100644
@@ -24,7 +24,7 @@
  */
 #define RESP_STATUS(resp)		FIELD_GET(GENMASK(31, 28), resp)
 #define RESP_TID(resp)			FIELD_GET(GENMASK(27, 24), resp)
-#define RESP_DATA_LENGTH(resp)		FIELD_GET(GENMASK(21,  0), resp)
+#define RESP_DATA_LENGTH(resp)		FIELD_GET(GENMASK(15,  0), resp)
 
 #define RESP_ERR_FIELD			GENMASK(31, 28)
 
Signature · 1 line
2.53.0
#13 lore raw

PATCH v3 13/17 i3c: mipi-i3c-hci: Remove invalid transfer size limit

In reply to Adrian Hunter

The driver derives a maximum transfer size from HC_CAP_MAX_DATA_LENGTH in HC_CAPABILITIES, but no published version of the I3C HCI specification defines such a field.

HC_CAPABILITIES reserves Bits[31:8] in HCI v1.0 and Bits[27:22] in HCI v1.1 and v1.2, so the bits used by HC_CAP_MAX_DATA_LENGTH are reserved in all released HCI versions. Neither the HCI nor the I3C TCRI specifications define a maximum data length capability.

On compliant controllers reserved bits read as zero, making the computed limit 65536 bytes. Since struct i3c_xfer.len is u16, transfers can never reach that size and the resulting -EFBIG check can never trigger.

Remove the unused capability definition and the dead size check.

The driver's effective limit remains unchanged. HCI specifications define DATA_LENGTH as a 16-bit field and require larger transfers to be split across multiple Transfer Descriptors. The driver already relies on the core's 16-bit length types elsewhere when constructing descriptors.

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/core.c | 7 -------
 1 file changed, 7 deletions(-)
drivers/i3c/master/mipi-i3c-hci/core.c +0 -7
index b9b20797d045..4629a16debc9 100644
@@ -51,7 +51,6 @@
 #define HC_CAP_SG_DC_EN			BIT(30)
 #define HC_CAP_SG_IBI_EN		BIT(29)
 #define HC_CAP_SG_CR_EN			BIT(28)
-#define HC_CAP_MAX_DATA_LENGTH		GENMASK(24, 22)
 #define HC_CAP_CMD_SIZE			GENMASK(21, 20)
 #define HC_CAP_DIRECT_COMMANDS_EN	BIT(18)
 #define HC_CAP_MULTI_LANE_EN		BIT(15)
@@ -473,7 +472,6 @@ static int i3c_hci_i3c_xfers(struct i3c_dev_desc *dev,
 	struct i3c_hci *hci = to_i3c_hci(m);
 	struct hci_xfer *xfer;
 	DECLARE_COMPLETION_ONSTACK(done);
-	unsigned int size_limit;
 	int i, last, ret = 0;
 
 	dev_dbg(&hci->master.dev, "nxfers = %d", nxfers);
@@ -482,13 +480,8 @@ static int i3c_hci_i3c_xfers(struct i3c_dev_desc *dev,
 	if (!xfer)
 		return -ENOMEM;
 
-	size_limit = 1U << (16 + FIELD_GET(HC_CAP_MAX_DATA_LENGTH, hci->caps));
-
 	for (i = 0; i < nxfers; i++) {
 		xfer[i].data_len = i3c_xfers[i].len;
-		ret = -EFBIG;
-		if (xfer[i].data_len >= size_limit)
-			goto out;
 		xfer[i].rnw = i3c_xfers[i].rnw;
 		if (i3c_xfers[i].rnw) {
 			xfer[i].data = i3c_xfers[i].data.in;
Signature · 1 line
2.53.0
#14 lore raw

PATCH v3 14/17 i3c: mipi-i3c-hci: Remove invalid HDR-BT and Fm/Fm+ definitions

In reply to Adrian Hunter

Several HCI definitions describe capabilities or modes that are not defined by any released version of the I3C HCI specification.

HC_CAP_HDR_BT_EN uses a reserved HC_CAPABILITIES bit. Similarly, MODE_I3C_HDR_BT uses a MODE value reserved for I3C transfers, while MODE_I3C_Fm_FmP cannot be encoded in the 3-bit Command Descriptor MODE field.

These definitions are currently unused, so there is no functional change. Remove them to avoid implying support for non-existent HCI features and to prevent future misuse.

Assisted-by GitHub-Copilot:claude-opus-5

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/cmd_v1.c | 2 --
 drivers/i3c/master/mipi-i3c-hci/core.c   | 1 -
 2 files changed, 3 deletions(-)
drivers/i3c/master/mipi-i3c-hci/cmd_v1.c +0 -2
index 3b9345718d27..9910df53c013 100644
@@ -108,8 +108,6 @@ enum hci_cmd_mode {
 	MODE_I3C_SDR4		= 0x4,
 	MODE_I3C_HDR_TSx	= 0x5,
 	MODE_I3C_HDR_DDR	= 0x6,
-	MODE_I3C_HDR_BT		= 0x7,
-	MODE_I3C_Fm_FmP		= 0x8,
 	MODE_I2C_Fm		= 0x0,
 	MODE_I2C_FmP		= 0x1,
 	MODE_I2C_UD1		= 0x2,
drivers/i3c/master/mipi-i3c-hci/core.c +0 -1
index 4629a16debc9..7a39be64c4e1 100644
@@ -55,7 +55,6 @@
 #define HC_CAP_DIRECT_COMMANDS_EN	BIT(18)
 #define HC_CAP_MULTI_LANE_EN		BIT(15)
 #define HC_CAP_CMD_CCC_DEFBYTE		BIT(10)
-#define HC_CAP_HDR_BT_EN		BIT(8)
 #define HC_CAP_HDR_TS_EN		BIT(7)
 #define HC_CAP_HDR_DDR_EN		BIT(6)
 #define HC_CAP_NON_CURRENT_MASTER_CAP	BIT(5)	/* master handoff capable */
Signature · 1 line
2.53.0
#15 lore raw

PATCH v3 15/17 i3c: mipi-i3c-hci: Support configurable device NACK retries

In reply to Adrian Hunter

Implement the .set_dev_nack_retry() master operation using the DAT_0_DEV_NACK_RETRY_CNT field in Device Address Table entries.

Since the retry count is programmed per DAT entry, update all allocated entries when the setting changes. Also initialize newly allocated entries with the current retry count so that a consistent retry policy is applied across all devices.

Add a DAT helper to update the retry count of an individual entry. Return -ERANGE if the requested retry count exceeds the hardware field width and -EOPNOTSUPP when the active command descriptor model does not use DAT entries.

In addition, default the retry count to 1. The I3C specification mandates a retry when a Target NACKs its Dynamic Address. HCI v1.1 explicitly preserves that behaviour for Direct CCCs even when DEV_NACK_RETRY_CNT is programmed to 0, but HCI v1.0 defines the field only as a device-specific retry count and does not provide the same exception. As a result, a v1.0 controller left at the reset value of 0 may perform no retry whereas a v1.1 controller will retry once. Promoting 0 to 1 at probe time makes the behaviour consistent across controller versions. Userspace can still select 0 retries explicitly via dev_nack_retry_count.

---

Changes in V3:

Moved the retry count range check and the loop over allocated DAT entries from the DAT layer into i3c_hci_set_dev_nack_retry(), and removed the set_all_nack_retry() DAT operation. Moved the DAT_0_DEV_NACK_RETRY_CNT definition to dat.h so that it is visible to the caller.
Amended the commit message accordingly.

Changes in V2:

None

 drivers/i3c/master/mipi-i3c-hci/core.c   | 32 ++++++++++++++++++++++++
 drivers/i3c/master/mipi-i3c-hci/dat.h    |  2 ++
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 13 +++++++++-
 3 files changed, 46 insertions(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +32 -0
index 7a39be64c4e1..d6a8a7612d46 100644
@@ -555,6 +555,11 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 	return ret;
 }
 
+static void i3c_hci_dat_v1_set_curr_nack_retry(struct i3c_hci *hci, unsigned int dat_idx)
+{
+	mipi_i3c_hci_dat_v1.set_nack_retry(hci, dat_idx, hci->master.dev_nack_retry_count);
+}
+
 static int i3c_hci_attach_i3c_dev(struct i3c_dev_desc *dev)
 {
 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
@@ -573,6 +578,7 @@ static int i3c_hci_attach_i3c_dev(struct i3c_dev_desc *dev)
 		}
 		mipi_i3c_hci_dat_v1.set_dynamic_addr(hci, ret,
 						     dev->info.dyn_addr ?: dev->info.static_addr);
+		i3c_hci_dat_v1_set_curr_nack_retry(hci, ret);
 		dev_data->dat_idx = ret;
 	}
 	i3c_dev_set_master_data(dev, dev_data);
@@ -622,6 +628,7 @@ static int i3c_hci_attach_i2c_dev(struct i2c_dev_desc *dev)
 	}
 	mipi_i3c_hci_dat_v1.set_static_addr(hci, ret, dev->addr);
 	mipi_i3c_hci_dat_v1.set_flags(hci, ret, DAT_0_I2C_DEVICE, 0);
+	i3c_hci_dat_v1_set_curr_nack_retry(hci, ret);
 	dev_data->dat_idx = ret;
 	i2c_dev_set_master_data(dev, dev_data);
 	return 0;
@@ -733,6 +740,23 @@ static void i3c_hci_recycle_ibi_slot(struct i3c_dev_desc *dev,
 	hci->io->recycle_ibi_slot(hci, dev, slot);
 }
 
+static int i3c_hci_set_dev_nack_retry(struct i3c_master_controller *m, unsigned int cnt)
+{
+	struct i3c_hci *hci = to_i3c_hci(m);
+	unsigned int dat_idx;
+
+	if (hci->cmd != &mipi_i3c_hci_cmd_v1)
+		return -EOPNOTSUPP;
+
+	if (cnt > FIELD_MAX(DAT_0_DEV_NACK_RETRY_CNT))
+		return -ERANGE;
+
+	for_each_set_bit(dat_idx, hci->DAT_data, hci->DAT_entries)
+		mipi_i3c_hci_dat_v1.set_nack_retry(hci, dat_idx, cnt);
+
+	return 0;
+}
+
 static const struct i3c_master_controller_ops i3c_hci_ops = {
 	.bus_init		= i3c_hci_bus_init,
 	.bus_cleanup		= i3c_hci_bus_cleanup,
@@ -752,6 +776,7 @@ static const struct i3c_master_controller_ops i3c_hci_ops = {
 	.recycle_ibi_slot	= i3c_hci_recycle_ibi_slot,
 	.enable_hotjoin		= i3c_hci_enable_hotjoin,
 	.disable_hotjoin	= i3c_hci_disable_hotjoin,
+	.set_dev_nack_retry	= i3c_hci_set_dev_nack_retry,
 };
 
 static irqreturn_t i3c_hci_irq_handler(int irq, void *dev_id)
@@ -1186,6 +1211,13 @@ static int i3c_hci_probe(struct platform_device *pdev)
 	if (device_can_wakeup(i3c_hci_sysdev(&pdev->dev)))
 		hci->master.ibi_wakeup = true;
 
+	/*
+	 * HCI v1.1 onward does 1 retry for Direct CCCs anyway, so for v1.0 to
+	 * be consistent, promote 0 to 1.
+	 */
+	if (!hci->master.dev_nack_retry_count)
+		hci->master.dev_nack_retry_count = 1;
+
 	return i3c_master_register(&hci->master, &pdev->dev, &i3c_hci_ops, false);
 }
 
drivers/i3c/master/mipi-i3c-hci/dat.h +2 -0
index 6881f19da77f..31becc6f9aec 100644
@@ -12,6 +12,7 @@
 
 /* Global DAT flags */
 #define DAT_0_I2C_DEVICE		W0_BIT_(31)
+#define DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29)
 #define DAT_0_SIR_REJECT		W0_BIT_(13)
 #define DAT_0_IBI_PAYLOAD		W0_BIT_(12)
 
@@ -25,6 +26,7 @@ struct hci_dat_ops {
 	void (*clear_flags)(struct i3c_hci *hci, unsigned int dat_idx, u32 w0, u32 w1);
 	int (*get_index)(struct i3c_hci *hci, u8 address);
 	void (*restore)(struct i3c_hci *hci);
+	void (*set_nack_retry)(struct i3c_hci *hci, unsigned int dat_idx, unsigned int cnt);
 };
 
 extern const struct hci_dat_ops mipi_i3c_hci_dat_v1;
drivers/i3c/master/mipi-i3c-hci/dat_v1.c +12 -1
index 852966aa20d9..ec0764a27c62 100644
@@ -24,7 +24,7 @@
 #define DAT_1_AUTOCMD_VALUE		W1_MASK(47, 40)
 #define DAT_1_AUTOCMD_MASK		W1_MASK(39, 32)
 /*	DAT_0_I2C_DEVICE		W0_BIT_(31) */
-#define DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29)
+/*	DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29) */
 #define DAT_0_RING_ID			W0_MASK(28, 26)
 #define DAT_0_DYNADDR_PARITY		W0_BIT_(23)
 #define DAT_0_DYNAMIC_ADDRESS		W0_MASK(22, 16)
@@ -189,6 +189,16 @@ static void hci_dat_v1_restore(struct i3c_hci *hci)
 	}
 }
 
+static void hci_dat_v1_set_nack_retry(struct i3c_hci *hci, unsigned int dat_idx, unsigned int cnt)
+{
+	u32 dat_w0;
+
+	dat_w0 = dat_w0_read(dat_idx);
+	dat_w0 &= ~DAT_0_DEV_NACK_RETRY_CNT;
+	dat_w0 |= FIELD_PREP(DAT_0_DEV_NACK_RETRY_CNT, cnt);
+	dat_w0_write(dat_idx, dat_w0);
+}
+
 const struct hci_dat_ops mipi_i3c_hci_dat_v1 = {
 	.init			= hci_dat_v1_init,
 	.alloc_entry		= hci_dat_v1_alloc_entry,
@@ -199,4 +209,5 @@ const struct hci_dat_ops mipi_i3c_hci_dat_v1 = {
 	.clear_flags		= hci_dat_v1_clear_flags,
 	.get_index		= hci_dat_v1_get_index,
 	.restore		= hci_dat_v1_restore,
+	.set_nack_retry		= hci_dat_v1_set_nack_retry,
 };
Signature · 1 line
2.53.0
Reviewed-by on v3 15/17
#16 lore raw

Re: [PATCH V3 15/17] i3c: mipi-i3c-hci: Support configurable device NACK retries

Quoted 24 lines · Adrian Hunter

Implement the .set_dev_nack_retry() master operation using the DAT_0_DEV_NACK_RETRY_CNT field in Device Address Table entries.

Since the retry count is programmed per DAT entry, update all allocated entries when the setting changes. Also initialize newly allocated entries with the current retry count so that a consistent retry policy is applied across all devices.

Add a DAT helper to update the retry count of an individual entry. Return -ERANGE if the requested retry count exceeds the hardware field width and -EOPNOTSUPP when the active command descriptor model does not use DAT entries.

In addition, default the retry count to 1. The I3C specification mandates a retry when a Target NACKs its Dynamic Address. HCI v1.1 explicitly preserves that behaviour for Direct CCCs even when DEV_NACK_RETRY_CNT is programmed to 0, but HCI v1.0 defines the field only as a device-specific retry count and does not provide the same exception. As a result, a v1.0 controller left at the reset value of 0 may perform no retry whereas a v1.1 controller will retry once. Promoting 0 to 1 at probe time makes the behaviour consistent across controller versions. Userspace can still select 0 retries explicitly via dev_nack_retry_count.

Quoted 2 lines

---

Quoted 158 lines

Changes in V3:

Moved the retry count range check and the loop over allocated DAT entries from the DAT layer into i3c_hci_set_dev_nack_retry(), and removed the set_all_nack_retry() DAT operation. Moved the DAT_0_DEV_NACK_RETRY_CNT definition to dat.h so that it is visible to the caller.
Amended the commit message accordingly.

Changes in V2:

None

 drivers/i3c/master/mipi-i3c-hci/core.c   | 32 ++++++++++++++++++++++++
 drivers/i3c/master/mipi-i3c-hci/dat.h    |  2 ++
 drivers/i3c/master/mipi-i3c-hci/dat_v1.c | 13 +++++++++-
 3 files changed, 46 insertions(+), 1 deletion(-)
drivers/i3c/master/mipi-i3c-hci/core.c +32 -0
index 7a39be64c4e1..d6a8a7612d46 100644
@@ -555,6 +555,11 @@ static int i3c_hci_i2c_xfers(struct i2c_dev_desc *dev,
 	return ret;
 }
 
+static void i3c_hci_dat_v1_set_curr_nack_retry(struct i3c_hci *hci, unsigned int dat_idx)
+{
+	mipi_i3c_hci_dat_v1.set_nack_retry(hci, dat_idx, hci->master.dev_nack_retry_count);
+}
+
 static int i3c_hci_attach_i3c_dev(struct i3c_dev_desc *dev)
 {
 	struct i3c_master_controller *m = i3c_dev_get_master(dev);
@@ -573,6 +578,7 @@ static int i3c_hci_attach_i3c_dev(struct i3c_dev_desc *dev)
 		}
 		mipi_i3c_hci_dat_v1.set_dynamic_addr(hci, ret,
 						     dev->info.dyn_addr ?: dev->info.static_addr);
+		i3c_hci_dat_v1_set_curr_nack_retry(hci, ret);
 		dev_data->dat_idx = ret;
 	}
 	i3c_dev_set_master_data(dev, dev_data);
@@ -622,6 +628,7 @@ static int i3c_hci_attach_i2c_dev(struct i2c_dev_desc *dev)
 	}
 	mipi_i3c_hci_dat_v1.set_static_addr(hci, ret, dev->addr);
 	mipi_i3c_hci_dat_v1.set_flags(hci, ret, DAT_0_I2C_DEVICE, 0);
+	i3c_hci_dat_v1_set_curr_nack_retry(hci, ret);
 	dev_data->dat_idx = ret;
 	i2c_dev_set_master_data(dev, dev_data);
 	return 0;
@@ -733,6 +740,23 @@ static void i3c_hci_recycle_ibi_slot(struct i3c_dev_desc *dev,
 	hci->io->recycle_ibi_slot(hci, dev, slot);
 }
 
+static int i3c_hci_set_dev_nack_retry(struct i3c_master_controller *m, unsigned int cnt)
+{
+	struct i3c_hci *hci = to_i3c_hci(m);
+	unsigned int dat_idx;
+
+	if (hci->cmd != &mipi_i3c_hci_cmd_v1)
+		return -EOPNOTSUPP;
+
+	if (cnt > FIELD_MAX(DAT_0_DEV_NACK_RETRY_CNT))
+		return -ERANGE;
+
+	for_each_set_bit(dat_idx, hci->DAT_data, hci->DAT_entries)
+		mipi_i3c_hci_dat_v1.set_nack_retry(hci, dat_idx, cnt);
+
+	return 0;
+}
+
 static const struct i3c_master_controller_ops i3c_hci_ops = {
 	.bus_init		= i3c_hci_bus_init,
 	.bus_cleanup		= i3c_hci_bus_cleanup,
@@ -752,6 +776,7 @@ static const struct i3c_master_controller_ops i3c_hci_ops = {
 	.recycle_ibi_slot	= i3c_hci_recycle_ibi_slot,
 	.enable_hotjoin		= i3c_hci_enable_hotjoin,
 	.disable_hotjoin	= i3c_hci_disable_hotjoin,
+	.set_dev_nack_retry	= i3c_hci_set_dev_nack_retry,
 };
 
 static irqreturn_t i3c_hci_irq_handler(int irq, void *dev_id)
@@ -1186,6 +1211,13 @@ static int i3c_hci_probe(struct platform_device *pdev)
 	if (device_can_wakeup(i3c_hci_sysdev(&pdev->dev)))
 		hci->master.ibi_wakeup = true;
 
+	/*
+	 * HCI v1.1 onward does 1 retry for Direct CCCs anyway, so for v1.0 to
+	 * be consistent, promote 0 to 1.
+	 */
+	if (!hci->master.dev_nack_retry_count)
+		hci->master.dev_nack_retry_count = 1;
+
 	return i3c_master_register(&hci->master, &pdev->dev, &i3c_hci_ops, false);
 }
 
drivers/i3c/master/mipi-i3c-hci/dat.h +2 -0
index 6881f19da77f..31becc6f9aec 100644
@@ -12,6 +12,7 @@
 
 /* Global DAT flags */
 #define DAT_0_I2C_DEVICE		W0_BIT_(31)
+#define DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29)
 #define DAT_0_SIR_REJECT		W0_BIT_(13)
 #define DAT_0_IBI_PAYLOAD		W0_BIT_(12)
 
@@ -25,6 +26,7 @@ struct hci_dat_ops {
 	void (*clear_flags)(struct i3c_hci *hci, unsigned int dat_idx, u32 w0, u32 w1);
 	int (*get_index)(struct i3c_hci *hci, u8 address);
 	void (*restore)(struct i3c_hci *hci);
+	void (*set_nack_retry)(struct i3c_hci *hci, unsigned int dat_idx, unsigned int cnt);
 };
 
 extern const struct hci_dat_ops mipi_i3c_hci_dat_v1;
drivers/i3c/master/mipi-i3c-hci/dat_v1.c +12 -2
index 852966aa20d9..ec0764a27c62 100644
@@ -24,7 +24,7 @@
 #define DAT_1_AUTOCMD_VALUE		W1_MASK(47, 40)
 #define DAT_1_AUTOCMD_MASK		W1_MASK(39, 32)
 /*	DAT_0_I2C_DEVICE		W0_BIT_(31) */
-#define DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29)
+/*	DAT_0_DEV_NACK_RETRY_CNT	W0_MASK(30, 29) */
 #define DAT_0_RING_ID			W0_MASK(28, 26)
 #define DAT_0_DYNADDR_PARITY		W0_BIT_(23)
 #define DAT_0_DYNAMIC_ADDRESS		W0_MASK(22, 16)
@@ -189,6 +189,16 @@ static void hci_dat_v1_restore(struct i3c_hci *hci)
 	}
 }
 
+static void hci_dat_v1_set_nack_retry(struct i3c_hci *hci, unsigned int dat_idx, unsigned int cnt)
+{
+	u32 dat_w0;
+
+	dat_w0 = dat_w0_read(dat_idx);
+	dat_w0 &= ~DAT_0_DEV_NACK_RETRY_CNT;
+	dat_w0 |= FIELD_PREP(DAT_0_DEV_NACK_RETRY_CNT, cnt);
+	dat_w0_write(dat_idx, dat_w0);
+}
+
 const struct hci_dat_ops mipi_i3c_hci_dat_v1 = {
 	.init			= hci_dat_v1_init,
 	.alloc_entry		= hci_dat_v1_alloc_entry,
@@ -199,4 +209,5 @@ const struct hci_dat_ops mipi_i3c_hci_dat_v1 = {
 	.clear_flags		= hci_dat_v1_clear_flags,
 	.get_index		= hci_dat_v1_get_index,
 	.restore		= hci_dat_v1_restore,
+	.set_nack_retry		= hci_dat_v1_set_nack_retry,
 };
--

2.53.0

#17 lore raw

PATCH v3 16/17 i3c: Restrict HDR modes to those supported by the bus and target

In reply to Adrian Hunter

I3C v1.1.1 Table 9 defines which HDR Modes are permitted for each Bus Configuration. A Mixed Slow / Limited Bus permits no HDR Modes because Legacy I2C Devices without a 50 ns SCL spike filter cannot be shielded from HDR traffic. A Mixed Fast Bus permits HDR-DDR and HDR-TSL, but not HDR-TSP, since of the two Ternary Modes only HDR-TSL is defined for a Bus that also has Legacy I2C Devices.

However, both i3c_device_get_supported_xfer_mode() and the transfer validation in i3c_dev_do_xfers_locked() currently consider only the controller's hdr_cap. Since hdr_cap reflects controller capability rather than bus or target constraints, HDR Modes can be advertised and used even when the bus configuration forbids them. Likewise, HDR Modes unsupported by the target may be reported as available.

Add i3c_dev_supported_xfer_modes_locked(), which derives the usable transfer modes from the intersection of:

- HDR Modes supported by the controller
- HDR Modes allowed by the bus configuration
- HDR Modes supported by the target

Use this helper both when reporting supported transfer modes and when validating transfers, ensuring that the advertised and permitted modes remain consistent.

GETHDRCAP is queried only when I3C_BCR_HDR_CAP is set, so a target without that bit is treated as supporting no HDR Modes.

i3c_device_get_supported_xfer_mode() now takes the normal-use bus lock in order to access the device descriptor, matching i3c_device_get_info().

Also fix the kernel-doc reference to enum i3c_xfer_mode.

Fixes 256a21743d91 ("i3c: Add HDR API support")
Assisted-by GitHub-Copilot:claude-opus-5

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/device.c    | 15 ++++++++---
 drivers/i3c/internals.h |  2 ++
 drivers/i3c/master.c    | 57 ++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 69 insertions(+), 5 deletions(-)
drivers/i3c/device.c +11 -4
index f1ba363b22a1..4df4682af150 100644
@@ -301,16 +301,23 @@ i3c_device_match_id(struct i3c_device *i3cdev,
 EXPORT_SYMBOL_GPL(i3c_device_match_id);
 
 /**
- * i3c_device_get_supported_xfer_mode - Returns the supported transfer mode by
- *					connected master controller.
+ * i3c_device_get_supported_xfer_mode - Returns the transfer modes supported by
+ *					the connected master controller and @dev,
+ *					and allowed by the bus configuration.
  * @dev: I3C device
  *
  * Return: a bit mask, which supported transfer mode, bit position is defined at
- *	   enum i3c_hdr_mode
+ *	   enum i3c_xfer_mode
  */
 u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev)
 {
-	return i3c_bus_to_i3c_master(dev->bus)->this->info.hdr_cap | BIT(I3C_SDR);
+	u32 modes;
+
+	i3c_bus_normaluse_lock(dev->bus);
+	modes = i3c_dev_supported_xfer_modes_locked(dev->desc);
+	i3c_bus_normaluse_unlock(dev->bus);
+
+	return modes;
 }
 EXPORT_SYMBOL_GPL(i3c_device_get_supported_xfer_mode);
 
drivers/i3c/internals.h +2 -0
index 86a36b951e0d..9a6936e2c660 100644
@@ -18,6 +18,8 @@ bool i3c_bus_rpm_ibi_allowed(struct i3c_bus *bus);
 void i3c_bus_normaluse_lock(struct i3c_bus *bus);
 void i3c_bus_normaluse_unlock(struct i3c_bus *bus);
 
+u32 i3c_dev_supported_xfer_modes_locked(struct i3c_dev_desc *dev);
+
 int i3c_dev_setdasa_locked(struct i3c_dev_desc *dev);
 int i3c_dev_do_xfers_locked(struct i3c_dev_desc *dev,
 			    struct i3c_xfer *xfers,
drivers/i3c/master.c +56 -1

69 lines not on this page · raw message

Signature · 1 line
2.53.0
#18 lore raw

Re: [PATCH V3 16/17] i3c: Restrict HDR modes to those supported by the bus and target

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] i3c: missing NULL check for dev->desc in i3c_device_get_supported_xfer_mode --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] i3c: missing NULL check for dev->desc in i3c_device_get_supported_xfer_mode --

commit 62529bd655d4c18d69da34f93d179ce363aba85c
Author: Adrian Hunter <[email protected]>

i3c: Restrict HDR modes to those supported by the bus and target

This commit refactors i3c_device_get_supported_xfer_mode() to consider HDR modes allowed by the controller, bus configuration, and target. It introduces a helper i3c_dev_supported_xfer_modes_locked() and adds bus locking in normal use mode to access the device descriptor safely.

Quoted 4 lines
drivers/i3c/device.c +0 -0
index f1ba363b22a17..4df4682af1504 100644

[ ... ]

Quoted 12 lines
drivers/i3c/device.c +3 -2
@@ -301,16 +301,23 @@ i3c_device_match_id(struct i3c_device *i3cdev,
 EXPORT_SYMBOL_GPL(i3c_device_match_id);
 
 /**
- * i3c_device_get_supported_xfer_mode - Returns the supported transfer mode by
- *					connected master controller.
+ * i3c_device_get_supported_xfer_mode - Returns the transfer modes supported by
+ *					the connected master controller and @dev,
+ *					and allowed by the bus configuration.
  * @dev: I3C device
  *
  * Return: a bit mask, which supported transfer mode, bit position is defined at

drivers/i3c/device.c i3c_device_match_id(struct i3c_device *i3cdev, · line 317

Quoted 10 lines
drivers/i3c/device.c +5 -2
- *	   enum i3c_hdr_mode
+ *	   enum i3c_xfer_mode
  */
 u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev)
 {
-	return i3c_bus_to_i3c_master(dev->bus)->this->info.hdr_cap | BIT(I3C_SDR);
+	u32 modes;
+
+	i3c_bus_normaluse_lock(dev->bus);
+	modes = i3c_dev_supported_xfer_modes_locked(dev->desc);

[Severity: High]
Does this code risk a NULL pointer dereference during device unregistration?

If a device is being concurrently unregistered, i3c_master_unregister_i3c_devs() asynchronously sets dev->desc = NULL before driver unbinding completes. Other APIs in the I3C subsystem (such as i3c_device_get_info() and i3c_device_disable_ibi()) appear to guard against this teardown race condition by explicitly checking if (dev->desc) inside the bus lock.

Without a prior NULL check here, dev->desc is passed into i3c_dev_supported_xfer_modes_locked(), which unconditionally dereferences it:

include/linux/i3c/master.h:i3c_dev_get_master() {
    return dev->common.master;
}
drivers/i3c/master.c:i3c_dev_supported_xfer_modes_locked() {
    struct i3c_master_controller *master = i3c_dev_get_master(dev);
    ...
}

Could a check for dev->desc be added inside the lock before calling the helper?

Quoted 4 lines
drivers/i3c/device.c +3 -0
+	i3c_bus_normaluse_unlock(dev->bus);
+
+	return modes;
 }
Signature · 6 lines
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16

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

Re: [PATCH V3 16/17] i3c: Restrict HDR modes to those supported by the bus and target

Quoted 45 lines · On 20/09/2026 18:33, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] i3c: missing NULL check for dev->desc in i3c_device_get_supported_xfer_mode --

commit 62529bd655d4c18d69da34f93d179ce363aba85c
Author: Adrian Hunter <[email protected]>

i3c: Restrict HDR modes to those supported by the bus and target

This commit refactors i3c_device_get_supported_xfer_mode() to consider HDR modes allowed by the controller, bus configuration, and target. It introduces a helper i3c_dev_supported_xfer_modes_locked() and adds bus locking in normal use mode to access the device descriptor safely.

Quoted 4 lines
drivers/i3c/device.c +0 -0
index f1ba363b22a17..4df4682af1504 100644

[ ... ]

Quoted 12 lines
drivers/i3c/device.c +3 -2
@@ -301,16 +301,23 @@ i3c_device_match_id(struct i3c_device *i3cdev,
 EXPORT_SYMBOL_GPL(i3c_device_match_id);
 
 /**
- * i3c_device_get_supported_xfer_mode - Returns the supported transfer mode by
- *					connected master controller.
+ * i3c_device_get_supported_xfer_mode - Returns the transfer modes supported by
+ *					the connected master controller and @dev,
+ *					and allowed by the bus configuration.
  * @dev: I3C device
  *
  * Return: a bit mask, which supported transfer mode, bit position is defined at

drivers/i3c/device.c i3c_device_match_id(struct i3c_device *i3cdev, · line 317

Quoted 10 lines
drivers/i3c/device.c +5 -2
- *	   enum i3c_hdr_mode
+ *	   enum i3c_xfer_mode
  */
 u32 i3c_device_get_supported_xfer_mode(struct i3c_device *dev)
 {
-	return i3c_bus_to_i3c_master(dev->bus)->this->info.hdr_cap | BIT(I3C_SDR);
+	u32 modes;
+
+	i3c_bus_normaluse_lock(dev->bus);
+	modes = i3c_dev_supported_xfer_modes_locked(dev->desc);

[Severity: High]
Does this code risk a NULL pointer dereference during device unregistration?

Quoted 3 lines

If a device is being concurrently unregistered, i3c_master_unregister_i3c_devs() asynchronously sets dev->desc = NULL before driver unbinding completes. Other APIs in the I3C subsystem (such

FWIW: no it doesn't - that was fixed already, refer:

commit d2c743efd2d1ee64e94324664808f623dd865872

Author: Adrian Hunter <[email protected]>
Date:   Thu Jul 23 10:57:47 2026 +0300

i3c: master: Fix info leak and UAF in device unregister path

Quoted 25 lines

as i3c_device_get_info() and i3c_device_disable_ibi()) appear to guard against this teardown race condition by explicitly checking if (dev->desc) inside the bus lock.

Without a prior NULL check here, dev->desc is passed into i3c_dev_supported_xfer_modes_locked(), which unconditionally dereferences it:

include/linux/i3c/master.h:i3c_dev_get_master() {
    return dev->common.master;
}
drivers/i3c/master.c:i3c_dev_supported_xfer_modes_locked() {
    struct i3c_master_controller *master = i3c_dev_get_master(dev);
    ...
}

Could a check for dev->desc be added inside the lock before calling the helper?

Quoted 4 lines
drivers/i3c/device.c +3 -0
+	i3c_bus_normaluse_unlock(dev->bus);
+
+	return modes;
 }
#20 lore raw

PATCH v3 17/17 i3c: mipi-i3c-hci: Add HDR-DDR support

In reply to Adrian Hunter

Add support for HDR-DDR private transfers. Encode HDR-DDR Command Descriptors and advertise HDR-DDR capability when supported by the controller.

Carry the HDR Command Code in the descriptor CP, CMD and RNW fields. Use the Immediate Data Transfer Command for writes of up to four bytes, as is already done for SDR, and the Regular Data Transfer Command otherwise. The Immediate Data Transfer Command is write-only, which suits HDR because the eighth bit of the HDR Command Code is field RNW, so a write Command Code leaves that field zero as required.

Reject transfers shorter than one Data Word or with an odd byte count, as HDR-DDR transfers are defined in whole Data Words and the I3C specification requires at least one Data Word to follow the Command Word.

Only advertise HDR-DDR capability when the driver can generate the required descriptor format.

Assisted-by GitHub-Copilot:claude-opus-5

---

Changes in V3:

None

Changes in V2:

Added Frank Li's Reviewed-by tag.

 drivers/i3c/master/mipi-i3c-hci/cmd.h    |  2 +-
 drivers/i3c/master/mipi-i3c-hci/cmd_v1.c | 26 +++++++++++---
 drivers/i3c/master/mipi-i3c-hci/cmd_v2.c |  4 ++-
 drivers/i3c/master/mipi-i3c-hci/core.c   | 45 +++++++++++++++++++++---
 drivers/i3c/master/mipi-i3c-hci/hci.h    |  9 ++---
 include/linux/i3c/device.h               |  4 +++
 6 files changed, 76 insertions(+), 14 deletions(-)
drivers/i3c/master/mipi-i3c-hci/cmd.h +1 -1
index e0d4a6e0e319..fa3107df7c51 100644
@@ -55,7 +55,7 @@ struct hci_cmd_ops {
 	int (*prep_ccc)(struct i3c_hci *hci, struct hci_xfer *xfer,
 			u8 ccc_addr, u8 ccc_cmd, bool raw);
 	void (*prep_i3c_xfer)(struct i3c_hci *hci, struct i3c_dev_desc *dev,
-			      struct hci_xfer *xfer);
+			      struct hci_xfer *xfer, enum i3c_xfer_mode mode);
 	void (*prep_i2c_xfer)(struct i3c_hci *hci, struct i2c_dev_desc *dev,
 			      struct hci_xfer *xfer);
 	int (*perform_daa)(struct i3c_hci *hci);
drivers/i3c/master/mipi-i3c-hci/cmd_v1.c +22 -4
index 9910df53c013..78ad2ec2bd93 100644
@@ -215,14 +215,30 @@ static int hci_cmd_v1_prep_ccc(struct i3c_hci *hci,
 
 static void hci_cmd_v1_prep_i3c_xfer(struct i3c_hci *hci,
 				     struct i3c_dev_desc *dev,
-				     struct hci_xfer *xfer)
+				     struct hci_xfer *xfer,
+				     enum i3c_xfer_mode xfer_mode)
 {
 	struct i3c_hci_dev_data *dev_data = i3c_dev_get_master_data(dev);
 	unsigned int dat_idx = dev_data->dat_idx;
-	enum hci_cmd_mode mode = get_i3c_mode(hci);
+	enum hci_cmd_mode mode;
 	u8 *data = xfer->data;
 	unsigned int data_len = xfer->data_len;
 	bool rnw = xfer->rnw;
+	u32 cp_cmd = 0;
+
+	if (xfer_mode == I3C_SDR) {
+		mode = get_i3c_mode(hci);
+	} else {
+		/*
+		 * HDR-DDR is the only advertised HDR Mode, so a non-SDR
+		 * transfer uses the HDR-DDR Command Code encoding: CP marks CMD
+		 * as valid, CMD carries bits[6:0], and RNW carries bit[7]. CP
+		 * and CMD occupy the same descriptor bits in both command
+		 * formats.
+		 */
+		mode = MODE_I3C_HDR_DDR;
+		cp_cmd = CMD_R0_CP | CMD_R0_CMD(xfer->hdr_cmd & I3C_HDR_CMD_CODE);
+	}
 
 	xfer->cmd_tid = hci_get_tid();
 
@@ -233,7 +249,8 @@ static void hci_cmd_v1_prep_i3c_xfer(struct i3c_hci *hci,
 			CMD_I0_TID(xfer->cmd_tid) |
 			CMD_I0_DEV_INDEX(dat_idx) |
 			CMD_I0_DTT(data_len) |
-			CMD_I0_MODE(mode);
+			CMD_I0_MODE(mode) |
+			cp_cmd;
 		fill_data_bytes(xfer, data, data_len);
 	} else {
 		/* we use a Regular Data Transfer Command */
@@ -242,7 +259,8 @@ static void hci_cmd_v1_prep_i3c_xfer(struct i3c_hci *hci,
 			CMD_R0_TID(xfer->cmd_tid) |
 			CMD_R0_DEV_INDEX(dat_idx) |
 			CMD_R0_MODE(mode) |
-			(rnw ? CMD_R0_RNW : 0);
+			(rnw ? CMD_R0_RNW : 0) |
+			cp_cmd;
 		xfer->cmd_desc[1] =
 			CMD_R1_DATA_LENGTH(data_len);
 	}
drivers/i3c/master/mipi-i3c-hci/cmd_v2.c +3 -1
index 8d93748e858d..b5e25f05f03e 100644
@@ -212,9 +212,11 @@ static int hci_cmd_v2_prep_ccc(struct i3c_hci *hci, struct hci_xfer *xfer,
 	return 0;
 }
 
+/* For cmd_v2, only SDR capability is advertised */
 static void hci_cmd_v2_prep_i3c_xfer(struct i3c_hci *hci,
 				     struct i3c_dev_desc *dev,
-				     struct hci_xfer *xfer)
+				     struct hci_xfer *xfer,
+				     enum i3c_xfer_mode xfer_mode)
 {
 	unsigned int mode = XFERMODE_IDX_I3C_SDR;
 	unsigned int rate = get_i3c_rate_idx(hci);
drivers/i3c/master/mipi-i3c-hci/core.c +41 -4

86 lines not on this page · raw message

drivers/i3c/master/mipi-i3c-hci/hci.h +5 -4
index ee73f6e6756a..4288a1b5b491 100644
@@ -85,10 +85,10 @@ struct i3c_hci {
 
 /*
  * Structure to represent a master initiated transfer.
- * The rnw, data and data_len fields must be initialized before calling any
- * hci->cmd->*() method. The cmd method will initialize cmd_desc[] and
- * possibly modify (clear) the data field. Then xfer->cmd_desc[0] can
- * be augmented with CMD_0_ROC and/or CMD_0_TOC.
+ * The rnw, data and data_len (and hdr_cmd for HDR) fields must be initialized
+ * before calling any hci->cmd->*() method. The cmd method will initialize
+ * cmd_desc[] and possibly modify (clear) the data field. Then xfer->cmd_desc[0]
+ * can be augmented with CMD_0_ROC and/or CMD_0_TOC.
  * The completion field needs to be initialized before queueing with
  * hci->io->queue_xfer(), and requires CMD_0_ROC to be set.
  */
@@ -97,6 +97,7 @@ struct hci_xfer {
 	u32 response;
 	bool rnw;
 	bool started;
+	u8 hdr_cmd;
 	void *data;
 	unsigned int data_len;
 	unsigned int cmd_tid;
include/linux/i3c/device.h +4 -0
index 0f065b883ee0..e30ea2cb13cb 100644
@@ -79,6 +79,10 @@ struct i3c_xfer {
 	enum i3c_error_code err;
 };
 
+/* For splitting 'cmd' from struct i3c_xfer */
+#define I3C_HDR_CMD_RNW			BIT(7)
+#define I3C_HDR_CMD_CODE		GENMASK(6, 0)
+
 /**
  * enum i3c_dcr - I3C DCR values
  * @I3C_DCR_GENERIC_DEVICE: generic I3C device
Signature · 1 line
2.53.0
maintainer Applied
#21 lore raw
In reply to Adrian Hunter
Quoted 10 lines · Adrian Hunter

This series brings a collection of fixes, specification-conformance cleanups and feature additions to the I3C core and MIPI I3C HCI driver.

The first three patches are independent fixes tagged for stable. The remaining patches address a mixture of correctness issues in the core and HCI driver, improve conformance with the I3C and HCI specifications, add support for configurable device NACK retries, and finally add HDR-DDR support.

[...]

Applied, thanks!

[01/17] i3c: master: Fix out-of-bounds read in DMA bounce buffer setup

        https://git.kernel.org/i3c/c/35b028b25451
[02/17] i3c: mipi-i3c-hci: Bounce short reads irrespective of the IOMMU
        https://git.kernel.org/i3c/c/b1a25e8e143e
[03/17] i3c: mipi-i3c-hci-pci: Set drvdata before creating LTR sysfs attribute
        https://git.kernel.org/i3c/c/2bc2b1b89f65
[04/17] i3c: master: Match ACPI targets to the correct bus controller instance
        https://git.kernel.org/i3c/c/25648fb7cebb
[05/17] i3c: master: Remove stale GETSTATUS length check
        https://git.kernel.org/i3c/c/48d7187c98e8
[06/17] i3c: mipi-i3c-hci: Restore controller state if i3c_hci_enable_ibi() returns an error
        https://git.kernel.org/i3c/c/ad869331ac61
[07/17] i3c: mipi-i3c-hci: Send DISEC before disabling IBIs in hardware
        https://git.kernel.org/i3c/c/dcaabb080e0c
[08/17] i3c: mipi-i3c-hci: Fix runtime PM violation in i3c_hci_free_ibi()
        https://git.kernel.org/i3c/c/1570265b2257
[09/17] i3c: mipi-i3c-hci: Process multiple IBIs per interrupt
        https://git.kernel.org/i3c/c/5fbd0790af94
[10/17] i3c: mipi-i3c-hci: Move DMA suspend/resume callbacks
        https://git.kernel.org/i3c/c/ae307311a69e
[11/17] i3c: mipi-i3c-hci: Stop rings gracefully when suspending
        https://git.kernel.org/i3c/c/9796ccba1254
[12/17] i3c: mipi-i3c-hci: Correct RESP_DATA_LENGTH to bits 15:0
        https://git.kernel.org/i3c/c/de243c3dd2e5
[13/17] i3c: mipi-i3c-hci: Remove invalid transfer size limit
        https://git.kernel.org/i3c/c/f3bd76ecb075
[14/17] i3c: mipi-i3c-hci: Remove invalid HDR-BT and Fm/Fm+ definitions
        https://git.kernel.org/i3c/c/8fe4a1b93aa9
[15/17] i3c: mipi-i3c-hci: Support configurable device NACK retries
        https://git.kernel.org/i3c/c/b7e849313eba
[16/17] i3c: Restrict HDR modes to those supported by the bus and target
        https://git.kernel.org/i3c/c/b968be9e7193
[17/17] i3c: mipi-i3c-hci: Add HDR-DDR support
        https://git.kernel.org/i3c/c/9b347cbde81c

Best regards,

Signature · 3 lines
Alexandre Belloni, co-owner and COO, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com