Skip to contentSkip to conversation

i2c: imx: acknowledge the count byte of an atomic block read

PATCH In review Mehmet Fide 3 participants 3 messages Nov 16

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

PATCH 1/1 i2c: imx: acknowledge the count byte of an atomic block read

From: Mehmet Fide <[email protected]>

i2c_smbus_xfer_emulated() sends the read message of an SMBus block read with a length of 1 and I2C_M_RECV_LEN. i2c_imx_prepare_read() clears I2CR_TXAK only for a message longer than one byte, so the atomic read does not acknowledge the count byte. The target stops sending, the rest of the block is read as 0xff and the transfer reports success.

The test for I2C_M_RECV_LEN was dropped when the read was split into i2c_imx_prepare_read() and its callers. Commit a5d0b9e32745 ("i2c: imx:
fix emulated smbus block read") added it back to the interrupt driven read only. The DMA read never gets an I2C_M_RECV_LEN message.

Fixes b460b15b3cc2 ("i2c: imx: separate atomic, dma and non-dma use case")
Cc [email protected] # v6.13+

---

Notes:
Reported by the Sashiko review of "i2c: imx: test the block read flag of the message that is read":
https://sashiko.dev/#/bug/linux-09e3015b-fc03-4410-809b-82964cdfac9c

Tested on a Colibri VF50 on the Iris carrier, kernel at this base, with the M41T0 RTC at 0x68. The core takes the atomic path only after SYSTEM_RUNNING, so a test-only hook called xfer_atomic() with interrupts disabled, with the two messages that i2c_smbus_xfer_emulated() builds for a block read. Register 0x04 (0x13) as count byte: without the patch 0x13 and then 0xff (50 of 50, return 2); with it the next registers, the same bytes as the interrupt driven read (50 of 50). With a count byte of 1 (register 0x05): 01 ff before, 01 01 after (50 of 50 each).

 drivers/i2c/busses/i2c-imx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-imx.c +1 -1
index ccf08cd63151..1a3fc245d6ba 100644
@@ -1283,7 +1283,7 @@ static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx,
 	 * Reset the I2CR_TXAK flag initially for SMBus block read since the
 	 * length is unknown
 	 */
-	if (msgs->len - 1)
+	if ((msgs->len - 1) || (msgs->flags & I2C_M_RECV_LEN))
 		temp &= ~I2CR_TXAK;
 	if (use_dma)
 		temp |= I2CR_DMAEN;

base-commit: 25e7ab89f4774b0f842cee0e8a20c11f755924d5

Signature · 1 line
2.56.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [Medium] i2c: imx: missing TXAK for 1-byte…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [Medium] i2c: imx: missing TXAK for 1-byte block read in i2c_imx_isr_read_block_data_len() [1]
--

commit 0b842b7d356553521b725411a4e465612a28658b
Author: Mehmet Fide <[email protected]>

i2c: imx: acknowledge the count byte of an atomic block read

This commit clears I2CR_TXAK in i2c_imx_prepare_read() when I2C_M_RECV_LEN is set so that atomic SMBus block reads acknowledge the initial count byte.

drivers/i2c/busses/i2c-imx.c static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx, · line 1289

Quoted 13 lines
drivers/i2c/busses/i2c-imx.c +1 -1
index ccf08cd63151f..1a3fc245d6ba7 100644
@@ -1283,7 +1283,7 @@ static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx,
 	 * Reset the I2CR_TXAK flag initially for SMBus block read since the
 	 * length is unknown
 	 */
-	if (msgs->len - 1)
+	if ((msgs->len - 1) || (msgs->flags & I2C_M_RECV_LEN))
 		temp &= ~I2CR_TXAK;
 	if (use_dma)
 		temp |= I2CR_DMAEN;

[Severity: Medium]
This problem wasn't introduced by this patch, but on the interrupt-driven path, does i2c_imx_isr_read_block_data_len() fail to set I2CR_TXAK
[1] when an SMBus block read without PEC returns a block length of 1 byte?

Reading the count byte from IMX_I2C_I2DR immediately starts clocking in the single payload byte. When len is 1 and the initial msg->len is 1, i2c_imx_isr_read_block_data_len() updates msg->len to 2 and msg_buf_idx to 1 without setting I2CR_TXAK:

drivers/i2c/busses/i2c-imx.c:i2c_imx_isr_read_block_data_len() {
	...
	i2c_imx->msg->len += len;
	i2c_imx->msg->buf[i2c_imx->msg_buf_idx++] = len;
}

On the next interrupt, i2c_imx_isr_read_continue() sees msg_buf_idx == msg->len - 1 and misses the msg->len - 2 branch that sets I2CR_TXAK. The controller then acknowledges the final byte before generating a stop condition, which can cause the target device to hold SDA low and wedge the bus.

[1] https://sashiko.dev/#/bug/linux-8e52fb9c-78a7-426b-ad34-b729995ff1c3

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Reviewed-by on v1 1/1
#2 lore raw
In reply to Mehmet Fide
Quoted 49 lines · Mehmet Fide

From: Mehmet Fide <[email protected]>

i2c_smbus_xfer_emulated() sends the read message of an SMBus block read with a length of 1 and I2C_M_RECV_LEN. i2c_imx_prepare_read() clears I2CR_TXAK only for a message longer than one byte, so the atomic read does not acknowledge the count byte. The target stops sending, the rest of the block is read as 0xff and the transfer reports success.

The test for I2C_M_RECV_LEN was dropped when the read was split into i2c_imx_prepare_read() and its callers. Commit a5d0b9e32745 ("i2c: imx:
fix emulated smbus block read") added it back to the interrupt driven read only. The DMA read never gets an I2C_M_RECV_LEN message.

Fixes b460b15b3cc2 ("i2c: imx: separate atomic, dma and non-dma use case")
Cc [email protected] # v6.13+

---

Notes:
Reported by the Sashiko review of "i2c: imx: test the block read flag of the message that is read":
https://sashiko.dev/#/bug/linux-09e3015b-fc03-4410-809b-82964cdfac9c

Tested on a Colibri VF50 on the Iris carrier, kernel at this base, with the M41T0 RTC at 0x68. The core takes the atomic path only after SYSTEM_RUNNING, so a test-only hook called xfer_atomic() with interrupts disabled, with the two messages that i2c_smbus_xfer_emulated() builds for a block read. Register 0x04 (0x13) as count byte: without the patch 0x13 and then 0xff (50 of 50, return 2); with it the next registers, the same bytes as the interrupt driven read (50 of 50). With a count byte of 1 (register 0x05): 01 ff before, 01 01 after (50 of 50 each).

 drivers/i2c/busses/i2c-imx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/i2c/busses/i2c-imx.c +1 -1
index ccf08cd63151..1a3fc245d6ba 100644
@@ -1283,7 +1283,7 @@ static int i2c_imx_prepare_read(struct imx_i2c_struct *i2c_imx,
 	 * Reset the I2CR_TXAK flag initially for SMBus block read since the
 	 * length is unknown
 	 */
-	if (msgs->len - 1)
+	if ((msgs->len - 1) || (msgs->flags & I2C_M_RECV_LEN))
 		temp &= ~I2CR_TXAK;
 	if (use_dma)
 		temp |= I2CR_DMAEN;
Quoted 3 lines

base-commit: 25e7ab89f4774b0f842cee0e8a20c11f755924d5

Signature · 1 line
2.56.0

Thanks for the fix.