Linux USB
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Alexey Charkov" <alchark@flipper.net>
Cc: linux-usb@vger.kernel.org
Subject: Re: [PATCH v2 4/4] usb: typec: tcpm: Keep cable plug messages away from the port partner
Date: Wed, 18 Nov 2026 07:51:45 +0000	[thread overview]
Message-ID: <sashiko-outbox-220369@kernel.org> (raw)
In-Reply-To: <20261118-tcpm-soft-reset-sop-v2-4-b6decdc95e66@flipper.net>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] usb: typec: tcpm: SOP' SVDMs in tcpm_cable_vdm_expected()
  still affect SOP
- [Medium] usb: typec: tcpm: tcpm_cable_ctrl_msg_expected() aborts
  unsent SOP' VDMs
- [Low] usb: typec: tcpm: stale soft_reset_sent_prime before
  run_state_machine()
--

commit 4c61156df43ba909af0892d17e05a01aa528ddf2
Author: Alexey Charkov <alchark@flipper.net>

usb: typec: tcpm: Keep cable plug messages away from the port partner

This commit filters incoming SOP' messages so that only expected VDM and
control responses from a cable plug are passed to the protocol handlers, and
drops messages on unsupported SOP* types before updating MessageID state.

> diff --git a/drivers/usb/typec/tcpm/tcpm.c b/drivers/usb/typec/tcpm/tcpm.c
> index cea9e6fc39c00..530186b51092f 100644
> --- a/drivers/usb/typec/tcpm/tcpm.c
> +++ b/drivers/usb/typec/tcpm/tcpm.c
> @@ -682,6 +682,8 @@ struct tcpm_port {
>  	 */
>  	enum tcpm_transmit_type vdm_sop_type;
>  	enum tcpm_transmit_type soft_reset_sop_type;
> +	/* The Soft_Reset in SOFT_RESET_SEND went to the cable plug */
> +	bool soft_reset_sent_prime;
>  	/*
>  	 * Prior to discovering the port partner's Specification Revision, the
>  	 * Vconn source and cable plug will use the lower of their two revisions.
> @@ -4179,6 +4181,64 @@ static void tcpm_pd_ext_msg_request(struct tcpm_port *port,
>  	}
>  }
>  
> +/* Whether a cable plug control message responds to what the port sent it */
> +static bool tcpm_cable_ctrl_msg_expected(struct tcpm_port *port,
> +					 enum pd_ctrl_msg_type type)
> +{
> +	switch (type) {
> +	case PD_CTRL_ACCEPT:
> +		/* the Soft_Reset after a VCONN Swap */
> +		return port->state == SOFT_RESET_SEND &&
> +		       port->soft_reset_sent_prime;
> +	case PD_CTRL_NOT_SUPP:
> +		/* a VDM, in the ready state, which takes it as the answer */
> +		return port->vdm_sop_type == TCPC_TX_SOP_PRIME &&
> +		       port->vdm_state > VDM_STATE_READY &&
> +		       port->state == ready_state(port);

[Severity: Medium]
Does checking port->vdm_state > VDM_STATE_READY allow an unsolicited SOP'
PD_CTRL_NOT_SUPP to cancel a VDM that has not been sent yet?

In enum vdm_states, VDM_STATE_WAIT_RSP_BUSY and VDM_STATE_SEND_MESSAGE are
both greater than VDM_STATE_READY.  When a PD 3.0 source queues an SOP' SVDM
in SRC_READY, vdm_run_state_machine() waits PD_T_SINK_TX in
VDM_STATE_SEND_MESSAGE before transmitting the message and clearing
port->vdo_data[0].  An unsolicited SOP' PD_CTRL_NOT_SUPP during that window
passes this check and reaches tcpm_pd_ctrl_request(), which sets
port->vdm_state to VDM_STATE_DONE, finishes the AMS without sending the VDM
or running the VDM_STATE_ERR_SEND fallback to partner discovery, and leaves
port->vdo_data[0] non-zero.

Checking port->vdm_state == VDM_STATE_BUSY, as tcpm_cable_vdm_expected()
does, would only accept Not_Supported while waiting for a response to a VDM
already on the wire.

> +	default:
> +		return false;
> +	}
> +}
> +
> +/* Whether a cable plug VDM answers the one the port is waiting on */
> +static bool tcpm_cable_vdm_expected(struct tcpm_port *port,
> +				    const struct pd_message *msg)
> +{
> +	u32 vdo = le32_to_cpu(msg->payload[0]);
> +
> +	return port->vdm_sop_type == TCPC_TX_SOP_PRIME &&
> +	       port->vdm_state == VDM_STATE_BUSY &&
> +	       PD_VDO_SVDM(vdo) && PD_VDO_CMDT(vdo) != CMDT_INIT;
> +}

[Severity: Medium]
Can a malformed SOP' SVDM response that passes tcpm_cable_vdm_expected()
still trigger transmissions to the port partner on SOP or corrupt partner
discovery state?

tcpm_cable_vdm_expected() checks neither the command nor the SVID against
 the pending SOP' request.  Once passed to tcpm_handle_vdm_request() ->
tcpm_pd_svdm(), response_tx_sop_type defaults to TCPC_TX_SOP:

drivers/usb/typec/tcpm/tcpm.c:tcpm_pd_svdm() {
    ...
		default:
			/* Unrecognized SVDM */
			response[0] = p[0] | VDO_CMDT(CMDT_RSP_NAK);
			rlen = 1;
			response[0] = (response[0] & ~VDO_SVDM_VERS_MASK) |
				      (VDO_SVDM_VERS(svdm_version));
			break;
		}
		break;
	case CMDT_RSP_NAK:
		tcpm_ams_finish(port);
		/* Go on with Source_Capabilities, as after an ACK */
		if (port->state == SRC_VDM_IDENTITY_REQUEST &&
		    rx_sop_type == TCPC_TX_SOP_PRIME) {
			tcpm_set_state(port, SRC_SEND_CAPABILITIES, 0);
			return 0;
		}
		switch (cmd) {
		/*
		 * The cable is not allowed to respond with NAK so this must've happened over SOP
		 */
		case CMD_DISCOVER_IDENT:
		case CMD_DISCOVER_SVID:
			tcpm_update_vdm_discovery_state(port, VDM_DISCOVERY_COMPLETE);
			break;
    ...
}

If the cable plug replies on SOP' with an unrecognized command (such as
CMD_ATTENTION with CMDT_RSP_ACK or CMD_EXIT_MODE with CMDT_RSP_NAK), the
default branches set rlen = 1 without updating response_tx_sop_type, so
tcpm_handle_vdm_request() queues a VDM NAK on TCPC_TX_SOP to the port
partner.  Similarly, if tcpm_vdm_ams(port) is false when the SOP' VDM
arrives (for instance, after a partner AMS interrupted port->ams), the else
branch in tcpm_handle_vdm_request() sends PD_MSG_CTRL_NOT_SUPP on SOP.

In addition, a CMDT_RSP_NAK on SOP' for CMD_DISCOVER_IDENT or
CMD_DISCOVER_SVID in SRC_READY/SNK_READY marks VDM_DISCOVERY_COMPLETE and
skips partner SVID discovery or partner altmode registration, and an
unexpected CMD_DISCOVER_MODES ACK on SOP' before CMD_DISCOVER_IDENT
registers port->plug_prime reaches tcpm_register_plug_altmodes() with
port->plug_prime == NULL.

[ ... ]

> @@ -6256,7 +6320,9 @@ static void run_state_machine(struct tcpm_port *port)
>  		 * A Soft_Reset Message Shall be targeted at a specific entity
>  		 * depending on the type of SOP* packet used.
>  		 */
> -		if (port->soft_reset_sop_type == TCPC_TX_SOP_PRIME) {
> +		port->soft_reset_sent_prime =
> +			port->soft_reset_sop_type == TCPC_TX_SOP_PRIME;
> +		if (port->soft_reset_sent_prime) {
>  			port->soft_reset_sop_type = TCPC_TX_SOP;
>  			port->message_id_prime = 0;
>  			port->rx_msgid_prime = -1;

[Severity: Low]
Can a stale port->soft_reset_sent_prime allow an unsolicited SOP'
PD_CTRL_ACCEPT to cancel a later SOP SOFT_RESET_SEND before
run_state_machine() runs?

After an SOP' Soft_Reset following a VCONN swap, port->soft_reset_sent_prime
remains true until the next time run_state_machine() executes
SOFT_RESET_SEND.  If tcpm_pd_ext_msg_request() later calls
tcpm_set_state(port, SOFT_RESET_SEND, 0) for a malformed SOP
PD_EXT_GET_BATT_STATUS or PD_EXT_GET_BATT_CAP message, port->state becomes
SOFT_RESET_SEND immediately while port->state_machine is queued on port->wq.
An SOP' PD_CTRL_ACCEPT already queued ahead of it on port->wq will then pass
tcpm_cable_ctrl_msg_expected() and transition the port back to
ready_state(port) before the SOP Soft_Reset is sent.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261118-tcpm-soft-reset-sop-v2-0-b6decdc95e66@flipper.net?part=4

      reply	other threads:[~2026-11-18  7:51 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  7:26 [PATCH v2 0/4] usb: typec: tcpm: Fixes for talking to the cable plug as a VCONN source Alexey Charkov
2026-11-18  7:26 ` [PATCH v2 1/4] usb: typec: tcpm: Keep the Soft_Reset target apart from the VDM one Alexey Charkov
2026-11-18  7:32   ` sashiko-bot
2026-11-18  7:26 ` [PATCH v2 2/4] usb: typec: tcpm: Stop replaying Source_Capabilities after cable discovery Alexey Charkov
2026-11-18  7:39   ` sashiko-bot
2026-11-18  7:26 ` [PATCH v2 3/4] usb: typec: tcpm: Leave SRC_VDM_IDENTITY_REQUEST whatever the cable answers Alexey Charkov
2026-11-18  8:08   ` sashiko-bot
2026-11-18  7:26 ` [PATCH v2 4/4] usb: typec: tcpm: Keep cable plug messages away from the port partner Alexey Charkov
2026-11-18  7:51   ` sashiko-bot [this message]

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-220369@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