Skip to contentSkip to conversation

i2c: imx: fixes for a DMA write that is not acknowledged

PATCH New Mehmet Fide 1 participant 4 messages Nov 19

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

v2 cover i2c: imx: fixes for a DMA write that is not acknowledged

From: Mehmet Fide <[email protected]>

A write of DMA_THRESHOLD bytes or more to an address that does not answer leaves the interrupt status behind, so that the next DMA read returns 0xff for every byte without an error (patch 1). It also leaves its DMA buffer mapped, as every terminated DMA transfer does (patch 2), and it blocks for DMA_TIMEOUT before it fails with -ETIMEDOUT instead of -ENXIO (patch 3). Patch 3 needs patches 1 and 2.

Tested on a Colibri VF50 on the Iris carrier (i2c0 with DMA, M41T0 RTC at 0x68) with this base, the other i2c-imx fixes on the list [1-4], PROVE_LOCKING and DMA_API_DEBUG. Each round wrote 20 bytes to an unused address and read 16 bytes of the RTC:

without the series, 200 rounds: 124 writes failed with -ETIMEDOUT
after up to 1.08 s, none with -ENXIO, 163 reads failed, 128 more DMA mappings in use afterwards
with the series, 200 + 300 rounds: every write failed with -ENXIO
within 10 ms, every read was right, 4 more DMA mappings in use after the first 200 rounds and none more after the next 300

On a v6.12 product kernel with the series backported, 200 rounds that also wrote 18 bytes to a device that acknowledges them: every NACKed write failed with -ENXIO at once and every acknowledged write went through. A NACK of the last byte the DMA writes could not be provoked on either board, so that path is not tested.

For stable: 6.12.y and older need backports, as noted on v1; I can send them once the patches are in mainline.

Changes in v2:
- 2/3: new. Sashiko on v1 2/2: the new -ENXIO path left the DMA buffer mapped, as the timeout path already did
- 3/3 (v1 2/2): a NACK of the last byte the DMA writes was missed (Sashiko). Its acknowledge is now checked once the byte is out, before the final byte is sent
- 1/3: no change
- rebased on v7.4-rc3+ (4405c18c95d2) and tested on that base; v1 was compile tested on its base only

v1: https://lore.kernel.org/linux-i2c/[email protected]/

[1] https://lore.kernel.org/linux-i2c/[email protected]/
[2] https://lore.kernel.org/linux-i2c/[email protected]/
[3] https://lore.kernel.org/linux-i2c/[email protected]/
[4] https://lore.kernel.org/linux-i2c/[email protected]/

Mehmet Fide (3):
i2c: imx: do not carry the interrupt status over to the next transfer
i2c: imx: unmap the DMA buffer when a transfer is terminated
i2c: imx: return -ENXIO at once when a DMA write is not acknowledged

 drivers/i2c/busses/i2c-imx.c | 52 +++++++++++++++++++++++-------------
 1 file changed, 34 insertions(+), 18 deletions(-)

base-commit: 4405c18c95d2d0f99f6c581ef0ee8591a1aa6ccd

Signature · 1 line
2.56.0
#1 lore raw

PATCH v2 1/3 i2c: imx: do not carry the interrupt status over to the next transfer

From: Mehmet Fide <[email protected]>

In DMA mode the interrupt handler stores I2SR in i2c_imx->i2csr and only i2c_imx_trx_complete() clears it. A DMA transfer that fails returns without doing so, for example a DMA write whose address is not acknowledged.

The stale status stays until the next DMA read. There i2c_imx_prepare_read() calls i2c_imx_trx_complete(), which finds IIF set and returns before the address byte has been sent, so the read returns 0xff for every byte without an error:

  i2ctransfer -a -y 0 w20@0x03 0x00-     # unused address, -ETIMEDOUT
  i2ctransfer -y 0 w1@0x54 0x00 r32      # 32 times 0xff
  i2ctransfer -y 0 w1@0x54 0x00 r32      # correct data

Clear the status when a transfer starts.

Fixes ce1a78840ff7 ("i2c: imx: add DMA support for freescale i2c driver")
---
 drivers/i2c/busses/i2c-imx.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/busses/i2c-imx.c +2 -0
index 89f2b764bbc4..f20727ffb5ce 100644
@@ -1657,6 +1657,8 @@ static int i2c_imx_xfer_common(struct i2c_adapter *adapter,
 	if (result)
 		return result;
 
+	i2c_imx->i2csr = 0;
+
 	/* Start I2C transfer */
 	result = i2c_imx_start(i2c_imx, atomic);
 	if (result) {
Signature · 1 line
2.56.0
#2 lore raw

PATCH v2 2/3 i2c: imx: unmap the DMA buffer when a transfer is terminated

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

The buffer of a DMA transfer is unmapped in the DMA completion callback only. When the wait for the completion times out the transfer is terminated, the callback does not run and the mapping is never released.

Unmap the buffer where the transfer is waited for, after the transfer has completed or has been terminated.

Fixes ce1a78840ff7 ("i2c: imx: add DMA support for freescale i2c driver")
---
 drivers/i2c/busses/i2c-imx.c | 41 ++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 18 deletions(-)
drivers/i2c/busses/i2c-imx.c +23 -18
index f20727ffb5ce..fbfabceb29ac 100644
@@ -469,11 +469,26 @@ static void i2c_imx_dma_callback(void *arg)
 	struct imx_i2c_struct *i2c_imx = (struct imx_i2c_struct *)arg;
 	struct imx_i2c_dma *dma = i2c_imx->dma;
 
-	dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
-			dma->dma_len, dma->dma_data_dir);
 	complete(&dma->cmd_complete);
 }
 
+static int i2c_imx_dma_wait(struct imx_i2c_struct *i2c_imx)
+{
+	struct imx_i2c_dma *dma = i2c_imx->dma;
+	int result = 0;
+
+	if (!wait_for_completion_timeout(&dma->cmd_complete,
+					 msecs_to_jiffies(DMA_TIMEOUT)))
+		result = -ETIMEDOUT;
+
+	if (result)
+		dmaengine_terminate_sync(dma->chan_using);
+	dma_unmap_single(dma->chan_using->device->dev, dma->dma_buf,
+			 dma->dma_len, dma->dma_data_dir);
+
+	return result;
+}
+
 static int i2c_imx_dma_xfer(struct imx_i2c_struct *i2c_imx,
 					struct i2c_msg *msgs)
 {
@@ -1192,7 +1207,6 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
 					struct i2c_msg *msgs)
 {
 	int result;
-	unsigned long time_left;
 	unsigned int temp = 0;
 	unsigned long orig_jiffies = jiffies;
 	struct imx_i2c_dma *dma = i2c_imx->dma;
@@ -1217,13 +1231,9 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
 	 * The first byte must be transmitted by the CPU.
 	 */
 	imx_i2c_write_reg(i2c_imx_addr_byte(msgs), i2c_imx, IMX_I2C_I2DR);
-	time_left = wait_for_completion_timeout(
-				&i2c_imx->dma->cmd_complete,
-				msecs_to_jiffies(DMA_TIMEOUT));
-	if (time_left == 0) {
-		dmaengine_terminate_sync(dma->chan_using);
-		return -ETIMEDOUT;
-	}
+	result = i2c_imx_dma_wait(i2c_imx);
+	if (result)
+		return result;
 
 	/* Waiting for transfer complete. */
 	while (1) {
@@ -1298,7 +1308,6 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 			struct i2c_msg *msgs, bool is_lastmsg)
 {
 	int result;
-	unsigned long time_left;
 	unsigned int temp;
 	unsigned long orig_jiffies = jiffies;
 	struct imx_i2c_dma *dma = i2c_imx->dma;
@@ -1321,13 +1330,9 @@ static int i2c_imx_dma_read(struct imx_i2c_struct *i2c_imx,
 	if (result)
 		return result;
 
-	time_left = wait_for_completion_timeout(
-				&i2c_imx->dma->cmd_complete,
-				msecs_to_jiffies(DMA_TIMEOUT));
-	if (time_left == 0) {
-		dmaengine_terminate_sync(dma->chan_using);
-		return -ETIMEDOUT;
-	}
+	result = i2c_imx_dma_wait(i2c_imx);
+	if (result)
+		return result;
 
 	/* waiting for transfer complete. */
 	while (1) {
Signature · 1 line
2.56.0
#3 lore raw

PATCH v2 3/3 i2c: imx: return -ENXIO at once when a DMA write is not acknowledged

In reply to Mehmet Fide

From: Mehmet Fide <[email protected]>

When a byte of a DMA write is not acknowledged the controller raises its interrupt and no DMA request, so i2c_imx_dma_write() blocks for DMA_TIMEOUT (1 s) and returns -ETIMEDOUT, where the interrupt driven write returns -ENXIO.

Let the interrupt handler end the wait when it sees a missing acknowledge during a DMA write, and return -ENXIO. The last byte the DMA writes is still on the bus when its completion arrives: look at its acknowledge once the byte is out, before the final byte is sent.

Fixes ce1a78840ff7 ("i2c: imx: add DMA support for freescale i2c driver")
---
 drivers/i2c/busses/i2c-imx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/i2c/busses/i2c-imx.c +9 -0
index fbfabceb29ac..06b082c677a3 100644
@@ -480,6 +480,9 @@ static int i2c_imx_dma_wait(struct imx_i2c_struct *i2c_imx)
 	if (!wait_for_completion_timeout(&dma->cmd_complete,
 					 msecs_to_jiffies(DMA_TIMEOUT)))
 		result = -ETIMEDOUT;
+	else if (dma->dma_transfer_dir == DMA_MEM_TO_DEV &&
+		 (i2c_imx->i2csr & I2SR_RXAK))
+		result = -ENXIO;
 
 	if (result)
 		dmaengine_terminate_sync(dma->chan_using);
@@ -1123,6 +1126,9 @@ static irqreturn_t i2c_imx_master_isr(struct imx_i2c_struct *i2c_imx, unsigned i
 	switch (i2c_imx->state) {
 	case IMX_I2C_STATE_DMA:
 		i2c_imx->i2csr = status;
+		if ((status & I2SR_RXAK) &&
+		    i2c_imx->dma->dma_transfer_dir == DMA_MEM_TO_DEV)
+			complete(&i2c_imx->dma->cmd_complete);
 		wake_up(&i2c_imx->queue);
 		break;
 
@@ -1248,6 +1254,9 @@ static int i2c_imx_dma_write(struct imx_i2c_struct *i2c_imx,
 		schedule();
 	}
 
+	if (temp & I2SR_RXAK)
+		return -ENXIO;
+
 	temp = imx_i2c_read_reg(i2c_imx, IMX_I2C_I2CR);
 	temp &= ~I2CR_DMAEN;
 	imx_i2c_write_reg(temp, i2c_imx, IMX_I2C_I2CR);
Signature · 1 line
2.56.0