* [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup
2026-09-10 3:40 [PATCH v4 0/3] i2c: xiic: fix SMBus block read and PEC support Abdurrahman Hussain
@ 2026-09-10 3:40 ` Abdurrahman Hussain
2026-09-21 8:43 ` Andi Shyti
2026-09-10 3:40 ` [PATCH v4 2/3] i2c: xiic: defer RX_FULL until all trailing bytes are in FIFO Abdurrahman Hussain
2026-09-10 3:40 ` [PATCH v4 3/3] i2c: xiic: don't clobber msg->len to signal block-read completion Abdurrahman Hussain
2 siblings, 1 reply; 6+ messages in thread
From: Abdurrahman Hussain @ 2026-09-10 3:40 UTC (permalink / raw)
To: Michal Simek, Andi Shyti, Wolfram Sang
Cc: Shubhrajyoti Datta, linux-arm-kernel, linux-i2c, linux-kernel,
Abdurrahman Hussain
xiic_smbus_block_read_setup() recalculates i2c->rx_msg->len based on the
length byte returned by the device, but historically clobbered the PEC
byte expectation the SMBus core had baked into msg->len. That dropped
the PEC byte from the caller's buffer on the normal and chunked
receive-fifo branches.
Compute pec_len up-front as (i2c->rx_msg->len - 1) and add it to the
new length in every branch:
- chunked (rxmsg_len + pec_len > IIC_RX_FIFO_DEPTH): set the drain
target to rxmsg_len + 1 + pec_len.
- deferred (small enough to drain in one fill but >= MIN_LEN total):
same.
- padded (1 + rxmsg_len + pec_len < SMBUS_BLOCK_READ_MIN_LEN): the
hardware needs at least 3 bytes on the bus to exit the read
cleanly (the second byte is already being clocked in by the time
the ISR reads the length byte and is too late to NACK), so we
still pad rx_msg->len up to SMBUS_BLOCK_READ_MIN_LEN. The dummy
trailing byte that gets drained must then be trimmed off before
handing the message back to the SMBus core; otherwise
i2c_smbus_check_pec() reads buf[len-1] (= dummy) instead of the
real PEC byte at buf[1] and rejects every clean zero-length block
read with -EBADMSG.
Record the true valid byte count in a new field
i2c->smbus_actual_len and have xiic_process()'s RX_FULL completion
site trim rx_msg->len down to it before clearing rx_msg.
Widen the branch condition from the old "(rxmsg_len == 1) ||
(rxmsg_len == 0)" to "(1 + rxmsg_len + pec_len) < MIN_LEN" so that
user requests with multi-byte trailing bytes (e.g. pec_len == 2 on
a zero-length block) flow through the deferred branch instead of
getting truncated to MIN_LEN here.
Acked-by: Michal Simek <michal.simek@amd.com>
Signed-off-by: Abdurrahman Hussain <abdurrahman@nexthop.ai>
---
drivers/i2c/busses/i2c-xiic.c | 40 +++++++++++++++++++++++++++++++++-------
1 file changed, 33 insertions(+), 7 deletions(-)
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 3e7735e1dae0..d4308716d461 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -73,6 +73,11 @@ enum i2c_scl_freq {
* @prev_msg_tx: Previous message is Tx
* @quirks: To hold platform specific bug info
* @smbus_block_read: Flag to handle block read
+ * @smbus_actual_len: For SMBus block reads padded to SMBUS_BLOCK_READ_MIN_LEN,
+ * the number of bytes that are actually valid (length byte + payload +
+ * optional PEC). msg->len gets trimmed to this on transfer completion so
+ * the SMBus core sees the real PEC byte and not the trailing dummy.
+ * Zero when no trimming is needed.
* @input_clk: Input clock to I2C controller
* @i2c_clk: I2C SCL frequency
* @atomic: Mode of transfer
@@ -98,6 +103,7 @@ struct xiic_i2c {
bool prev_msg_tx;
u32 quirks;
bool smbus_block_read;
+ unsigned int smbus_actual_len;
unsigned long input_clk;
unsigned int i2c_clk;
bool atomic;
@@ -539,6 +545,8 @@ static void xiic_smbus_block_read_setup(struct xiic_i2c *i2c)
/* Check if received length is valid */
if (rxmsg_len <= I2C_SMBUS_BLOCK_MAX) {
+ unsigned int pec_len = i2c->rx_msg->len - 1;
+
/* Set Receive fifo depth */
if (rxmsg_len > IIC_RX_FIFO_DEPTH) {
/*
@@ -546,23 +554,30 @@ static void xiic_smbus_block_read_setup(struct xiic_i2c *i2c)
* Receive fifo depth should set to Rx fifo capacity minus 1
*/
rfd_set = IIC_RX_FIFO_DEPTH - 1;
- i2c->rx_msg->len = rxmsg_len + 1;
- } else if ((rxmsg_len == 1) ||
- (rxmsg_len == 0)) {
+ i2c->rx_msg->len = rxmsg_len + 1 + pec_len;
+ } else if (1 + rxmsg_len + pec_len < SMBUS_BLOCK_READ_MIN_LEN) {
/*
- * Minimum of 3 bytes required to exit cleanly. 1 byte
- * already received, Second byte is being received. Have
- * to set NACK in read_rx before receiving the last byte
+ * Hardware requires at least SMBUS_BLOCK_READ_MIN_LEN
+ * bytes on the bus to exit the read cleanly: by the
+ * time the ISR pulls the length byte from the FIFO,
+ * the second byte is already being clocked in and
+ * cannot be NACKed in time. Pad the drain target so
+ * the HW reaches the minimum, but remember the true
+ * valid byte count and trim msg->len back on transfer
+ * completion -- otherwise the SMBus core's PEC check
+ * reads the trailing dummy byte instead of the real
+ * PEC byte and rejects clean transfers with -EBADMSG.
*/
rfd_set = 0;
i2c->rx_msg->len = SMBUS_BLOCK_READ_MIN_LEN;
+ i2c->smbus_actual_len = 1 + rxmsg_len + pec_len;
} else {
/*
* When Rx msg len less than Rx fifo capacity
* Receive fifo depth should set to Rx msg len minus 2
*/
rfd_set = rxmsg_len - 2;
- i2c->rx_msg->len = rxmsg_len + 1;
+ i2c->rx_msg->len = rxmsg_len + 1 + pec_len;
}
xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rfd_set);
@@ -797,6 +812,17 @@ static irqreturn_t xiic_process(int irq, void *dev_id)
xiic_read_rx(i2c);
if (xiic_rx_space(i2c) == 0) {
+ /*
+ * If the setup path padded a short SMBus block read up
+ * to SMBUS_BLOCK_READ_MIN_LEN for the HW exit
+ * workaround, trim rx_msg->len back to the number of
+ * bytes that are actually valid so the SMBus core's
+ * PEC check reads the right index. Must happen before
+ * the rx_msg = NULL below.
+ */
+ if (i2c->rx_msg && i2c->smbus_actual_len)
+ i2c->rx_msg->len = i2c->smbus_actual_len;
+
/* this is the last part of the message */
i2c->rx_msg = NULL;
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup
2026-09-10 3:40 ` [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup Abdurrahman Hussain
@ 2026-09-21 8:43 ` Andi Shyti
2026-09-21 18:22 ` Abdurrahman Hussain
0 siblings, 1 reply; 6+ messages in thread
From: Andi Shyti @ 2026-09-21 8:43 UTC (permalink / raw)
To: Abdurrahman Hussain
Cc: Michal Simek, Wolfram Sang, Shubhrajyoti Datta, linux-arm-kernel,
linux-i2c, linux-kernel
Hi Abdurraham,
...
> xiic_read_rx(i2c);
> if (xiic_rx_space(i2c) == 0) {
> + /*
> + * If the setup path padded a short SMBus block read up
> + * to SMBUS_BLOCK_READ_MIN_LEN for the HW exit
> + * workaround, trim rx_msg->len back to the number of
> + * bytes that are actually valid so the SMBus core's
> + * PEC check reads the right index. Must happen before
> + * the rx_msg = NULL below.
> + */
> + if (i2c->rx_msg && i2c->smbus_actual_len)
> + i2c->rx_msg->len = i2c->smbus_actual_len;
We do need to reset smbus_actual_len at every xiic_start_recv(),
otherwise this would be true for every rx_msg. Right?
Andi
> /* this is the last part of the message */
> i2c->rx_msg = NULL;
>
>
> --
> 2.54.0
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup
2026-09-21 8:43 ` Andi Shyti
@ 2026-09-21 18:22 ` Abdurrahman Hussain
0 siblings, 0 replies; 6+ messages in thread
From: Abdurrahman Hussain @ 2026-09-21 18:22 UTC (permalink / raw)
To: Andi Shyti, Abdurrahman Hussain
Cc: Michal Simek, Wolfram Sang, Shubhrajyoti Datta, linux-arm-kernel,
linux-i2c, linux-kernel
On Mon Sep 21, 2026 at 1:43 AM PDT, Andi Shyti wrote:
> Hi Abdurraham,
>
> ...
>
>> xiic_read_rx(i2c);
>> if (xiic_rx_space(i2c) == 0) {
>> + /*
>> + * If the setup path padded a short SMBus block read up
>> + * to SMBUS_BLOCK_READ_MIN_LEN for the HW exit
>> + * workaround, trim rx_msg->len back to the number of
>> + * bytes that are actually valid so the SMBus core's
>> + * PEC check reads the right index. Must happen before
>> + * the rx_msg = NULL below.
>> + */
>> + if (i2c->rx_msg && i2c->smbus_actual_len)
>> + i2c->rx_msg->len = i2c->smbus_actual_len;
>
> We do need to reset smbus_actual_len at every xiic_start_recv(),
> otherwise this would be true for every rx_msg. Right?
>
> Andi
>
Right. Patch 3 clears it in the BNB handler, but the ARB_LOST /
TX_ERROR branch goes straight to out: and never reaches it, so an
aborted block read leaves it set for the next receive.
v5 clears it in xiic_start_recv() and drops the now-redundant reset
from patch 3. Patch 2 unchanged.
v5 also tags all three with Fixes: e4c1ff772e1a and Cc: stable - one
commit introduced all three, and a PEC block read needs all three.
Abdurrahman
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH v4 2/3] i2c: xiic: defer RX_FULL until all trailing bytes are in FIFO
2026-09-10 3:40 [PATCH v4 0/3] i2c: xiic: fix SMBus block read and PEC support Abdurrahman Hussain
2026-09-10 3:40 ` [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup Abdurrahman Hussain
@ 2026-09-10 3:40 ` Abdurrahman Hussain
2026-09-10 3:40 ` [PATCH v4 3/3] i2c: xiic: don't clobber msg->len to signal block-read completion Abdurrahman Hussain
2 siblings, 0 replies; 6+ messages in thread
From: Abdurrahman Hussain @ 2026-09-10 3:40 UTC (permalink / raw)
To: Michal Simek, Andi Shyti, Wolfram Sang
Cc: Shubhrajyoti Datta, linux-arm-kernel, linux-i2c, linux-kernel,
Abdurrahman Hussain
For the normal path of xiic_smbus_block_read_setup() (rxmsg_len less
than IIC_RX_FIFO_DEPTH), RFD was programmed to rxmsg_len - 2, which
fires the RX_FULL interrupt while the last payload byte is still in
flight. xiic_read_rx()'s bytes_rem == 1 branch then sets NACK on that
byte still on the wire, truncating the read in the PEC-enabled case.
Raise the threshold so RX_FULL fires only once every remaining byte
(payload plus optional PEC) is already buffered in the FIFO. That
routes the drain through xiic_read_rx()'s bytes_rem == 0 path, which
reads everything out and emits the stop cleanly. For the non-PEC path
the full payload is still read out through the same bytes_rem == 0
branch; the only user-visible change is that the controller waits one
extra byte-time before servicing the interrupt.
The deferred-fire formula is rxmsg_len + pec_len - 1, and the RFD
register at XIIC_RFD_REG_OFFSET is a 4-bit field. Widen the
chunk-vs-defer guard to (rxmsg_len + pec_len > IIC_RX_FIFO_DEPTH) so
the boundary case rxmsg_len == IIC_RX_FIFO_DEPTH with PEC enabled
cannot write 16 into that 4-bit register; it routes through the
chunked drain instead, which already caps RFD at IIC_RX_FIFO_DEPTH - 1.
Acked-by: Michal Simek <michal.simek@amd.com>
Signed-off-by: Abdurrahman Hussain <abdurrahman@nexthop.ai>
---
drivers/i2c/busses/i2c-xiic.c | 14 ++++++--------
1 file changed, 6 insertions(+), 8 deletions(-)
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index d4308716d461..2bdba6c0931f 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -548,10 +548,11 @@ static void xiic_smbus_block_read_setup(struct xiic_i2c *i2c)
unsigned int pec_len = i2c->rx_msg->len - 1;
/* Set Receive fifo depth */
- if (rxmsg_len > IIC_RX_FIFO_DEPTH) {
+ if (rxmsg_len + pec_len > IIC_RX_FIFO_DEPTH) {
/*
- * When Rx msg len greater than or equal to Rx fifo capacity
- * Receive fifo depth should set to Rx fifo capacity minus 1
+ * Trailing payload (data + optional PEC) exceeds Rx FIFO
+ * capacity; drain in chunks. Fire RX_FULL when the FIFO is
+ * full and let the ISR re-arm for the remainder.
*/
rfd_set = IIC_RX_FIFO_DEPTH - 1;
i2c->rx_msg->len = rxmsg_len + 1 + pec_len;
@@ -572,11 +573,8 @@ static void xiic_smbus_block_read_setup(struct xiic_i2c *i2c)
i2c->rx_msg->len = SMBUS_BLOCK_READ_MIN_LEN;
i2c->smbus_actual_len = 1 + rxmsg_len + pec_len;
} else {
- /*
- * When Rx msg len less than Rx fifo capacity
- * Receive fifo depth should set to Rx msg len minus 2
- */
- rfd_set = rxmsg_len - 2;
+ /* Defer RX_FULL until all trailing bytes are in FIFO. */
+ rfd_set = rxmsg_len + pec_len - 1;
i2c->rx_msg->len = rxmsg_len + 1 + pec_len;
}
xiic_setreg8(i2c, XIIC_RFD_REG_OFFSET, rfd_set);
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v4 3/3] i2c: xiic: don't clobber msg->len to signal block-read completion
2026-09-10 3:40 [PATCH v4 0/3] i2c: xiic: fix SMBus block read and PEC support Abdurrahman Hussain
2026-09-10 3:40 ` [PATCH v4 1/3] i2c: xiic: preserve PEC byte length in SMBus block read setup Abdurrahman Hussain
2026-09-10 3:40 ` [PATCH v4 2/3] i2c: xiic: defer RX_FULL until all trailing bytes are in FIFO Abdurrahman Hussain
@ 2026-09-10 3:40 ` Abdurrahman Hussain
2 siblings, 0 replies; 6+ messages in thread
From: Abdurrahman Hussain @ 2026-09-10 3:40 UTC (permalink / raw)
To: Michal Simek, Andi Shyti, Wolfram Sang
Cc: Shubhrajyoti Datta, linux-arm-kernel, linux-i2c, linux-kernel,
Abdurrahman Hussain
At the end of a SMBus block read the BNB handler force-set
tx_msg->len = 1 to push xiic_tx_space() to zero so the STATE_DONE
branch would fire. Two problems:
1. tx_msg and rx_msg alias the same i2c_msg struct during a receive
(see xiic_start_recv), so overwriting tx_msg->len also changes
rx_msg->len. The i2c core's i2c_smbus_check_pec() then reads the
PEC from the wrong offset -- buf[0] instead of buf[rxmsg_len + 1]
-- and either mis-validates or returns -EBADMSG.
2. xiic_start_recv sets tx_pos = msg->len (typically 2 when PEC is
enabled). xiic_tx_space() is unsigned msg->len - tx_pos, so
setting msg->len = 1 with tx_pos = 2 underflows to 0xFFFFFFFF and
xiic_tx_space() never compares equal to 0 -- the STATE_DONE check
falls through to STATE_ERROR, giving -EIO.
Instead, advance tx_pos up to msg->len. That drives tx_space to 0
without touching msg->len, preserving the buffer length that
xiic_smbus_block_read_setup() already grew to cover the length byte,
the payload and the optional PEC byte.
Also clear smbus_actual_len here so a subsequent non-SMBus transfer
does not see a stale trim value from this completed block read.
Acked-by: Michal Simek <michal.simek@amd.com>
Signed-off-by: Abdurrahman Hussain <abdurrahman@nexthop.ai>
---
drivers/i2c/busses/i2c-xiic.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/drivers/i2c/busses/i2c-xiic.c b/drivers/i2c/busses/i2c-xiic.c
index 2bdba6c0931f..8eaa411411c1 100644
--- a/drivers/i2c/busses/i2c-xiic.c
+++ b/drivers/i2c/busses/i2c-xiic.c
@@ -889,8 +889,17 @@ static irqreturn_t xiic_process(int irq, void *dev_id)
if (i2c->tx_msg && i2c->smbus_block_read) {
i2c->smbus_block_read = false;
- /* Set requested message len=1 to indicate STATE_DONE */
- i2c->tx_msg->len = 1;
+ /*
+ * Defensive: reset the per-transfer trim state in case
+ * the rx phase completed via an error path that
+ * skipped the trim site in the RX_FULL branch above.
+ */
+ i2c->smbus_actual_len = 0;
+ /*
+ * Drive xiic_tx_space() to 0 to signal STATE_DONE
+ * without truncating the rx_msg length.
+ */
+ i2c->tx_pos = i2c->tx_msg->len;
}
if (!i2c->tx_msg)
--
2.54.0
^ permalink raw reply [flat|nested] 6+ messages in thread