From: sashiko-bot@kernel.org
To: "Alexey Charkov" <alchark@flipper.net>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time
Date: Fri, 27 Nov 2026 11:25:50 +0000 [thread overview]
Message-ID: <sashiko-outbox-237237@kernel.org> (raw)
In-Reply-To: <20261127-fusb302-getsourcecap-v2-6-6770eb1e7945@flipper.net>
Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] usb: typec: fusb302: zero header byte dropped in
fusb302_pd_rx_keep()
- [Medium] usb: typec: fusb302: missing rx_resync on error in
fusb302_pd_rx_fast_ack()
- [Medium] usb: typec: fusb302: stale COLLISION clears ack_check in
fusb302_irq_handle()
- [Medium] usb: typec: fusb302: defeated GoodCRC staging on role changes
and retries
- [Medium] usb: typec: fusb302: stranded rx_buf and resync over-read in
fast ack
- [Medium] usb: typec: fusb302: popped FIFO bytes lost in
fusb302_pd_rx_fast_ack()
- [Medium] usb: typec: fusb302: unacknowledged message after collision
in fusb302_pd_rx_fast_ack()
--
commit 4f7e49c60c6f94fddba0ebcedb0518c433748be5
Author: Alexey Charkov <alchark@flipper.net>
usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time
This commit pre-stages the next expected GoodCRC in the FUSB302 TX FIFO and
reads STATUS0, STATUS1, INTERRUPT and the head of the RX FIFO at the start
of the interrupt handler so Get_Source_Cap_Extended can be acknowledged
before its CRC finishes arriving.
> diff --git a/drivers/usb/typec/tcpm/fusb302.c b/drivers/usb/typec/tcpm/fusb302.c
> index c98a74fc7a2ac..bdba2169ec0fa 100644
> --- a/drivers/usb/typec/tcpm/fusb302.c
> +++ b/drivers/usb/typec/tcpm/fusb302.c
[ ... ]
> @@ -1707,6 +1803,50 @@ static int fusb302_pd_rx_find(struct fusb302_chip *chip)
> return 0;
> }
>
> +/*
> + * Append n header bytes that fusb302_pd_rx_fast_ack() read from the FIFO while
> + * a packet may have been landing to rx_buf. Reading an empty FIFO returns 0
> + * and takes nothing, so real bytes may come interleaved with such zeros. As
> + * neither the token nor header byte[0], which holds the message type, is ever
> + * 0, the real bytes are the non-zero ones. The only exception is header
> + * byte[1], which holds the power role or cable plug bit and is non-zero from
> + * a source or a cable plug, but may be 0 from a sink: leave that case to the
> + * CRC, see fusb302_pd_rx_resolve_h1().
> + */
> +static void fusb302_pd_rx_keep(struct fusb302_chip *chip, const u8 *fifo,
> + int n)
> +{
> + int i;
> +
> + for (i = 0; i < n; i++) {
> + if (fifo[i])
> + chip->rx_buf[chip->rx_len++] = fifo[i];
> + else if (chip->rx_len == 2 && chip->pwr_role == TYPEC_SOURCE &&
> + (chip->rx_buf[0] & FUSB302_RX_TKN_MASK) ==
> + FUSB302_RX_TKN_SOP)
> + chip->rx_h1_unsure = true;
> + }
> +}
[Severity: Medium]
Can checking chip->pwr_role == TYPEC_SOURCE here drop a valid zero second
header byte during a Power Role Swap or Fast Role Swap?
During PR_Swap and FR_Swap, both partners transiently operate with power
role TYPEC_SINK while exchanging PS_RDY and its GoodCRC: the initial source
calls tcpm_set_pwr_role(port, TYPEC_SINK) before sending PS_RDY, and the
initial sink stays TYPEC_SINK until after it receives PS_RDY and applies
VBUS. If PS_RDY or GoodCRC carries MessageID 0, its second header byte is
0x00 while chip->pwr_role is TYPEC_SINK. When fusb302_pd_rx_fast_ack()
sees FUSB_REG_STATUS0_ACTIVITY set (including while the chip is still
transmitting its own automatic GoodCRC to PS_RDY), fusb302_pd_rx_keep()
discards that 0x00 byte without setting chip->rx_h1_unsure, and the loop in
fusb302_pd_rx_fast_ack() then consumes the first CRC byte as header
byte[1], causing the message to fail CRC verification and aborting the role
swap.
[ ... ]
> @@ -1834,12 +1986,14 @@ static int fusb302_pd_read_message(struct fusb302_chip *chip)
> * TX_SUCCESS and GCRCSENT events out of order on occasion, so we need
> * to check the message type to ensure correct reporting to TCPM.
> */
> - if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC)
> + if (!len && pd_header_type_le(msg.header) == PD_CTRL_GOOD_CRC) {
> fusb302_pd_tx_complete(chip, TCPC_TX_SUCCESS);
> - else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1)
> + } else if ((token & FUSB302_RX_TKN_MASK) == FUSB302_RX_TKN_SOP1) {
> tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP_PRIME);
> - else
> + } else {
> + chip->rx_last_msgid = pd_header_msgid_le(msg.header);
> tcpm_pd_receive(chip->tcpm_port, &msg, TCPC_TX_SOP);
> + }
[Severity: Medium]
Does advancing chip->rx_last_msgid here even when Get_Source_Cap_Extended
went unacknowledged or its GoodCRC was delayed defeat GoodCRC staging for
the partner's retry?
If fusb302_pd_rx_ack() skipped sending GoodCRC because
fusb302_pd_tx_waiting(chip) was true, or if a Sink's
Get_Source_Cap_Extended with MessageID 0 had to wait for its CRC via
chip->rx_h1_unsure, updating chip->rx_last_msgid to M causes
fusb302_pd_stage_ack() to stage (M + 1) & 7 instead of M. When the partner
retries with MessageID M, chip->staged_msgid does not match and
fusb302_pd_rx_ack() falls back to flushing the TX FIFO and writing the full
GoodCRC over I2C. Similarly, tcpm_set_roles() calls
fusb302_pd_unstage_ack() without re-staging a GoodCRC for the new roles,
and tcpm_set_pd_rx() turns on PD RX without calling fusb302_pd_stage_ack(),
leaving the first message after a role change or PD RX start without a
staged GoodCRC.
[ ... ]
> @@ -2120,6 +2335,75 @@ static int fusb302_read_interrupts(struct fusb302_chip *chip, u8 *status0)
> return ret;
> }
>
> +static void fusb302_pd_rx_fast_ack(struct fusb302_chip *chip)
> +{
> + u8 regs[6];
> + int ret, n, tries;
> +
> + if (!chip->pd_rx_on)
> + return;
> +
> + if (!chip->rx_len) {
> + ret = fusb302_i2c_block_read(chip, FUSB_REG_STATUS0,
> + sizeof(regs), regs);
> + if (ret < 0)
> + return;
> + chip->pending_interrupt |= regs[2];
> + /* With ACTIVITY clear, the token is over 250us away */
> + if (regs[0] & FUSB_REG_STATUS0_ACTIVITY) {
> + fusb302_pd_rx_keep(chip, regs + 3, 3);
> + } else if (!(regs[1] & FUSB_REG_STATUS1_RX_EMPTY)) {
> + memcpy(chip->rx_buf, regs + 3, 3);
> + chip->rx_len = 3;
> + }
[Severity: Medium]
Can discarding regs[3..5] when ACTIVITY is clear in regs[0] and RX_EMPTY is
set in regs[1] drop the token and first header byte of an arriving packet?
At 300 kbps BMC signaling, the 64-bit preamble and 20-bit SOP token take
280 us. On a 100 kHz I2C bus (90 us per byte), or on a 400 kHz bus when
the I2C controller stretches or delays between bytes, more than 280 us
elapses between latching regs[0] and clocking out regs[4..5] from
FUSB_REG_FIFOS. If a packet begins on CC right after regs[0] is latched,
its SOP token and first header byte arrive in the RX FIFO in time to be
popped into regs[4..5], where neither branch runs and the bytes are lost
even though the hardware still sends an automatic GoodCRC. Passing regs +
3 to fusb302_pd_rx_keep() whenever RX_EMPTY is set would drop 0x00 bytes
from an empty FIFO while keeping any newly landed header bytes.
> + if (chip->rx_len && !fusb302_pd_rx_is_token(chip->rx_buf[0]))
> + chip->rx_resync = true;
> + }
[Severity: Medium]
What happens if a non-RX interrupt runs fusb302_pd_rx_fast_ack() while a
packet is still landing on CC?
When an interrupt such as BC_LVL, VBUSOK or COLLISION fires during an
incoming frame, fusb302_pd_rx_fast_ack() pops 1 to 3 bytes into
chip->rx_buf, but fusb302_irq_handle() only calls fusb302_pd_rx_drain()
when CRC_CHK, TX_SUCCESS or GCRCSENT is set. If the frame has not raised
one of those interrupts yet (or later fails its hardware CRC check or is
cut short by a collision), fusb302_irq_handle() exits with chip->rx_len > 0
and no rx_recheck scheduled. On the next interrupt,
fusb302_pd_rx_fast_ack() sees chip->rx_len != 0, skips the STATUS0..FIFOS
read, and splices the next packet onto the stale bytes in chip->rx_buf with
chip->rx_resync still false. In addition, when chip->rx_resync is already
true on entry and RX_EMPTY is clear with ACTIVITY clear, memcpy() copies 3
bytes unconditionally instead of reading 1 byte at a time like
fusb302_pd_rx_read(), pulling bogus 0x00 bytes from a 1- or 2-byte FIFO
tail into chip->rx_buf.
> + /*
> + * A read that catches the header as it lands gets only part of it,
> + * and the rest comes in within tens of microseconds: read the rest,
> + * but no further, as fusb302_pd_rx_keep() can only tell what zeros
> + * in the header mean. The packet is still landing then, so skip the
> + * status registers.
> + */
> + for (tries = 0; tries < 2; tries++) {
> + n = 3 - chip->rx_len;
> + if (!chip->rx_len || n <= 0 || chip->rx_resync ||
> + chip->rx_h1_unsure)
> + break;
> + ret = fusb302_i2c_block_read(chip, FUSB_REG_FIFOS, n, regs);
> + if (ret < 0)
> + return;
[Severity: Medium]
Should a failed fusb302_i2c_block_read() here (and on the 6-byte read from
FUSB_REG_STATUS0 above) set chip->rx_resync = true before returning, as
fusb302_pd_rx_read() does?
If the I2C block read fails partway through or returns a short read after
reaching FUSB_REG_FIFOS, some bytes may already have left the hardware RX
FIFO while chip->rx_resync remains false, leaving subsequent multi-byte
reads in fusb302_pd_read_message() out of step with the FIFO.
> + fusb302_pd_rx_keep(chip, regs, n);
> + }
> +
> + if (chip->rx_len >= 3 && !chip->rx_resync)
> + fusb302_pd_rx_ack(chip);
> +}
[Severity: Medium]
When fusb302_pd_rx_fast_ack() calls fusb302_pd_rx_ack() while the packet is
still landing on CC, won't FUSB302_TKN_TXON collide with the incoming CRC
and EOP?
When the second header byte enters the RX FIFO, the 4-byte CRC and EOP (45
bits, or 150 us at 300 kbps) are still active on CC, whereas writing the
single byte FUSB302_TKN_TXON over a 400 kHz I2C bus takes about 73 us. If
TXON reaches the chip while CC is still busy (either with the tail of the
message or with a partner retry), the chip aborts the transmission and
raises FUSB_REG_INTERRUPT_COLLISION. Because fusb302_pd_rx_ack() has
already set chip->rx_acked = true and the collision paths never clear it,
fusb302_pd_read_message() will not re-send the GoodCRC once CC goes idle,
yet still advances chip->rx_last_msgid and passes the unacknowledged
message to tcpm_pd_receive().
> +
> static void fusb302_irq_handle(struct fusb302_chip *chip)
> {
> int ret = 0;
> @@ -2134,6 +2418,7 @@ static void fusb302_irq_handle(struct fusb302_chip *chip)
> bool intr_comp_chng;
>
> mutex_lock(&chip->lock);
> + fusb302_pd_rx_fast_ack(chip);
> again:
[Severity: Medium]
Can a COLLISION from a GoodCRC sent in fusb302_pd_rx_fast_ack() clear
chip->ack_check for a second GoodCRC sent later in fusb302_pd_rx_drain()?
If a GoodCRC sent by fusb302_pd_rx_fast_ack() collides on CC,
fusb302_read_interrupts() latches FUSB_REG_INTERRUPT_COLLISION into
interrupt along with FUSB_REG_INTERRUPT_CRC_CHK. The CRC_CHK branch then
runs fusb302_pd_rx_drain() before the COLLISION branch is reached; if
fusb302_pd_rx_drain() reads a queued retry and sends a new GoodCRC, it sets
chip->ack_check = true for that second GoodCRC. When execution reaches the
FUSB_REG_INTERRUPT_COLLISION check further down in fusb302_irq_handle(),
the stale COLLISION bit from the first GoodCRC clears chip->ack_check =
false, so fusb302_pd_check_ack() is skipped at the done label and any
collision on the second GoodCRC is left pending in hardware to fail TCPM's
next transmission.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261127-fusb302-getsourcecap-v2-0-6770eb1e7945@flipper.net?part=6
next prev parent reply other threads:[~2026-11-27 11:25 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 10:40 [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 1/6] usb: typec: fusb302: Find the next message when the RX FIFO is out of step Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 2/6] usb: typec: fusb302: Drain the RX FIFO on each PD interrupt Alexey Charkov
2026-11-27 10:58 ` sashiko-bot
2026-11-27 10:40 ` [PATCH v2 3/6] usb: typec: fusb302: Read out the RX FIFO rather than flush it as PD RX goes on Alexey Charkov
2026-11-27 11:00 ` sashiko-bot
2026-11-27 10:40 ` [PATCH v2 4/6] usb: typec: fusb302: Handle interrupts in the IRQ thread Alexey Charkov
2026-11-27 10:40 ` [PATCH v2 5/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended from the driver Alexey Charkov
2026-11-27 11:01 ` sashiko-bot
2026-11-27 10:40 ` [PATCH v2 6/6] usb: typec: fusb302: Acknowledge Get_Source_Cap_Extended in time Alexey Charkov
2026-11-27 11:25 ` sashiko-bot [this message]
2026-11-27 11:25 ` [PATCH v2 0/6] usb: typec: fusb302: Make full PD 3.0 work on the 2.0-only FUSB302 sashiko-bot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=sashiko-outbox-237237@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=alchark@flipper.net \
--cc=linux-usb@vger.kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox