🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #25889

[PATCH net] can: mcp251x: deliver the TX echo before RX frames of the same pass

#25889
From: Marc Kleine-Budde
Date: Wed, 02 Dec 2026 16:59
131 lines
15080 bytes
From: Nguyen Cong Thanh <congthanh.ml.2509@gmail.com>

mcp251x_can_ist() hands the frames of both receive buffers to the stack
first and returns the echo of a completed transmission only at the end
of the pass. When our frame has completed and the peer's reply is
already in a receive buffer, the reply reaches the stack before the echo
of the frame it answers. The handler returned the echo first until
commit bf66f3736a94 ("can: mcp251x: Move to threaded interrupts instead
of workqueues.") moved the receive buffers to the front of the loop.

Since commit 4b7fe92c0690 ("can: isotp: add local echo tx processing for
consecutive frames") can-isotp enters ISOTP_WAIT_FC only on the echo of
the last consecutive frame of a block, and isotp_rcv_fc() ignores a flow
control frame that arrives while the sender is still in ISOTP_SENDING.
With a non-zero block size and a peer that answers quickly, the transfer
stalls:

  bus                  mcp251x_can_ist()         can-isotp
  CF 8 sent (TXIF)
  FC in RXB0 (RX0IF)
                       read CANINTF
                       RX0: FC to the stack  ->  SENDING, FC ignored
                       TX: echo to the stack ->  WAIT_FC
                                                 FC timeout after 1 s

Seen with an MCP2515 (SPI at 1 MHz, CAN at 500 kbit/s) on an RK3566
board talking to an STM32 bootloader that answers each block of
consecutive frames with a flow control frame (BS 8, STmin 5 ms). In
candump on the gateway the flow control frame came 0.27 ms before the
echo and the UDS download timed out. With this change, in the gateway's
candump captures of 25 downloads, all 5225 flow control frames sent
after a full block came after the echo of the frame they answer.

Return the echo whenever TXIF has been seen, before the receive buffers
that follow: at the start of the pass, and again after the CANINTF
re-read that follows RXB0, before RXB1. In the second case the frame in
RXB0 was there before our frame completed and still goes first, while a
reply that rolled over into RXB1 now comes after the echo. The MCP251x
has no RX timestamps, so the bus order of a completed transmission and
a frame received around the same time is unknown either way. A reply
can only come after our frame; the cost is that an unrelated frame
received just before it may now be delivered after the echo. The TX
block at the end of the pass only wakes the queue.

As a side effect, the echo of a completed frame is now also returned
when the pass ends in bus-off with restart-ms 0, where the loop used to
break out before the TX block.

Fixes: bf66f3736a94 ("can: mcp251x: Move to threaded interrupts instead of workqueues.")
Cc: stable@vger.kernel.org
Signed-off-by: Nguyen Cong Thanh <congthanh.ml.2509@gmail.com>
Link: https://patch.msgid.link/20261128054815.1586-1-congthanh.ml.2509@gmail.com
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
---
 drivers/net/can/spi/mcp251x.c | 36 +++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/drivers/net/can/spi/mcp251x.c b/drivers/net/can/spi/mcp251x.c
index 0d0190ae094a..0acdab0cd0af 100644
--- a/drivers/net/can/spi/mcp251x.c
+++ b/drivers/net/can/spi/mcp251x.c
@@ -1067,6 +1067,18 @@ static void mcp251x_restart_work_handler(struct work_struct *ws)
 	mutex_unlock(&priv->mcp_lock);
 }
 
+static void mcp251x_tx_echo(struct mcp251x_priv *priv)
+{
+	struct net_device *net = priv->net;
+
+	if (!priv->tx_busy)
+		return;
+
+	net->stats.tx_packets++;
+	net->stats.tx_bytes += can_get_echo_skb(net, 0, NULL);
+	priv->tx_busy = false;
+}
+
 static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 {
 	struct mcp251x_priv *priv = dev_id;
@@ -1082,6 +1094,14 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 
 		mcp251x_read_2regs(spi, CANINTF, &intf, &eflag);
 
+		/* Return the echo of a completed transmission before the
+		 * frames received in this pass. Without RX timestamps their
+		 * bus order is unknown, but a reply to our frame, such as an
+		 * ISO-TP flow control frame, can only follow it.
+		 */
+		if (intf & CANINTF_TX)
+			mcp251x_tx_echo(priv);
+
 		/* receive buffer 0 */
 		if (intf & CANINTF_RX0IF) {
 			mcp251x_hw_rx(spi, 0);
@@ -1105,6 +1125,12 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 			}
 		}
 
+		/* Our frame may have completed while RXB0 was read, with the
+		 * reply rolled over into RXB1.
+		 */
+		if (intf & CANINTF_TX)
+			mcp251x_tx_echo(priv);
+
 		/* receive buffer 1 */
 		if (intf & CANINTF_RX1IF) {
 			mcp251x_hw_rx(spi, 1);
@@ -1196,15 +1222,9 @@ static irqreturn_t mcp251x_can_ist(int irq, void *dev_id)
 		if (intf == 0)
 			break;
 
-		if (intf & CANINTF_TX) {
-			if (priv->tx_busy) {
-				net->stats.tx_packets++;
-				net->stats.tx_bytes += can_get_echo_skb(net, 0,
-									NULL);
-				priv->tx_busy = false;
-			}
+		/* the echo has already been returned above */
+		if (intf & CANINTF_TX)
 			netif_wake_queue(net);
-		}
 	}
 	mutex_unlock(&priv->mcp_lock);
 	return IRQ_HANDLED;

base-commit: c1c39137dcc3134010566ff29a81c93124efa539
-- 
2.53.0
Technical Details
Message-ID: <20261202170513.2552933-2-mkl@pengutronix.de>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375946-linux-kernel-netdev=dewy.ru
References: <20261202170513.2552933-1-mkl@pengutronix.de>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!newsfeed.bofh.team!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-375946-linux-kernel-netdev=dewy.ru
From: mkl@pengutronix.de (Marc Kleine-Budde)
Newsgroups: linux.kernel.netdev
Subject: [PATCH net] can: mcp251x: deliver the TX echo before RX frames of the same pass
Date: Wed,  2 Dec 2026 17:59:06 +0100
Organization: Usenet Network
Message-ID: <20261202170513.2552933-2-mkl@pengutronix.de>
References: <20261202170513.2552933-1-mkl@pengutronix.de>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="2016038"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:qLpsV/mmE/Ln/I/YBgnCFoMPr3s= sha256:ySQPy8p5Hl7cDQwo10w3YPFkf6lpPOWDbuc5vr5Zbcg=
	sha1:ew8+XW+2V+zD/mIZfbsvDfxtmxw= sha256:Rs/zsLH83KBB8Ai1zA3ZE3jtLnwbaSSjgG+x7V43/o8=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 3536F1176C0; Wed,  2 Dec 2026 20:06:16 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.05
X-Spam-Status: No, score=-3.05 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=pengutronix.de
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id c14PO54j_yNL;
	Wed,  2 Dec 2026 20:06:11 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-375946-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 3DACB1176A7; Wed,  2 Dec 2026 20:05:58 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 1811E2611E; Wed,  2 Dec 2026 17:05:54 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=3; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796231154;
	b=PoWeJT8NeW2c24ENbTt9jzhG7227Yld8QpaSo/Z74w0Cewo+93Xh0m09M8qqus8FPBzs
	 tcGiCFOHvRzK7MPs08ohMMPvkyAPMfncx8mhgvTgxe7m6Q9p/0KHxLidDArqVLyTkTux3
	 8MccFuYooqX/WWZ5fKiiqa6Hs/ACrkYGnkHvLjj1ahCGFVE/EyCNOCnYyOR3xMnr/buQ0
	 spqQDkzKVd6g2+Nk6inuic74lGFhRYPCw1OTISwoTKW1yhkXZ4dsVjHxPy1vDk/kUxOBv
	 6IlQkkldukn2B6I9dxLeVHUkqhUixwtgunzTY/rFL4WaewoLbRsO1Dw5OpRcPrA92ig==
X-ARC-Message-Signature: i=3; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796231154;
	h=X-Original-To:Received:DMARC-Filter:Received:DKIM-Signature:From:To:
	 Cc:Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:
	 Content-Transfer-Encoding;
	bh=ghXLCL+wAsZDpND4kFRBa+j9D1H1nMJxRmJ4XxIh0cI=;
	b=tmIciUccduO+gNEWhclUjGrpLgTcu7QGJTofSTeEmCxl9upDMRlYw0wSm3eeV7znJRHi
	 ClTVRW+LXX6LoT/UPMK77+Lf+UInL7ScMreIV35bzdjDMEO6QKr8IOWXzLyYDMegUnrLN
	 hjuJwM3vx/mXwa7eyoxbRX38GxpGQLxNpykIWVGlPNc6qhMHMcKUV1gZXaGZ08QIKgmWT
	 jMQ9agRBKicHzWy1BWAqCqBwvo3908q19VthSesfjjU4voxOSIJduserX4IX7D+GZn0Sq
	 SYEsr5A1gM5H7DtotMv+O4qQD9wCJxiQo5KVIsvgoVg8/aSBorSTlDGQXHEFa2AEEGw==
X-ARC-Authentication-Results: i=3; smtp.subspace.kernel.org; 
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=BgkoUOO7;
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id DC324606A332;
	Wed,  2 Dec 2026 17:05:53 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=BgkoUOO7
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1796231153;
	b=fPy0ao0UqIZxn2C/+dgAQ84IDZuq29y3o5hUdllIniAZhvbWJt2aUG0yegIHl3eCoRnp
	 C5uZegstKAkdQUiyxdjjfY+TF4fxwODef349rBZORA/PzpH5S/dhnmfSiJv/N9mHyW5Dj
	 FeXJwpBwF0021gHC1Fajn3Q0pfOOloFIUVBknco/OcAR6AP80rEUxpiZqBQlg+UMphtpo
	 yQ3iqxPGopENSc4AhImrQLLyMD5Wy88SAS73FppAJ/GDNzNL9NFt37q45KFUtZiRZCzVY
	 SVHmPq/MIZeWBGliUfXOdosUpkK4GPOKlXIZesws3SgR7ojxIJIJ6q/4BH/EvgUjvYg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796231153;
	h=DMARC-Filter:Received:DKIM-Signature:From:To:Cc:Subject:Date:
	 Message-ID:X-Mailer:In-Reply-To:References:MIME-Version:
	 Content-Transfer-Encoding;
	bh=ghXLCL+wAsZDpND4kFRBa+j9D1H1nMJxRmJ4XxIh0cI=;
	b=evMqxGHuv/5GsXY34Wjc/zvDM0svESC7tNCDU38CNUxnpy1E1LAoEC7flo3jtzlUJky1
	 jpkbLNIWwZFr8DjnisQD7OiG/p8V5LTPnYBp/Mhj55fX8PmOLAu1BSiYHJk5zIIvaOeAp
	 Ow43WscR+t5Ql0FQ+krFg0g6umlPVLLzJ9ygwp3rkH+9xiOHRcb0DDCNmd8qvAOcp/gOw
	 /Q9VTmpeHwUYqodLXOeyD7O5LaSk4gXosxEumDRw0hzzxVSD9UmsyJSIbdPh8/YclIzWF
	 rCvf35PlpZbQIIwhiZ8EQIDDs2RM4TGKM6o6ti4QZKEidiI1dhkzdH/Ia9MFHpuzJ0g==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dmarc=none header.from=pengutronix.de;
	spf=pass smtp.mailfrom=pengutronix.de;
	dkim=pass header.d=pengutronix.de header.i=@pengutronix.de header.a=rsa-sha256 header.s=20260414 header.b=BgkoUOO7;
	arc=pass header.oldest-pass=0 smtp.remote-ip=185.203.200.13
X-Received: from mx1.white.stw.pengutronix.de (mx1.white.stw.pengutronix.de [185.203.200.13])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5646A606A327;
	Wed,  2 Dec 2026 17:05:52 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 5646A606A327
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=pengutronix.de
X-Received: from pengutronix.de (unknown [185.104.138.144])
	(Authenticated sender: mkl@pengutronix.de)
	by mx1.white.stw.pengutronix.de (Postfix) with ESMTPSA id C3755205BD5;
	Wed, 02 Dec 2026 18:05:45 +0100 (CET)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1796231146;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=ghXLCL+wAsZDpND4kFRBa+j9D1H1nMJxRmJ4XxIh0cI=;
	b=BgkoUOO7sxE0mk6A1wewWp72HPPyNuP2pHyhMwKtz5odyR+lEyxzfN4N8IGJEjvOJLdXH9
	i0N27NCFKkSx3Wh/pPqU0bcD8EEzH4V/JCD1pG+DZwFORLzSsqbgWLSBjf6LI9oqRRNnXr
	Yybtlf2IJBh3Skpi8gfCCB3XN5kI6PWcCnxucISOyttVdcJhjX3QonsPf+7TzbR6hE8fj2
	cjBDkyqIpVX3vY4GWfAEHpAZWoZeEqnFV/prAhlfWxgVCaXXH6qGLXC/GRXtvgphvegYGn
	7XsEz/3P/1tFhy4rf5uJFYiW859C3kxBvc8Isyb0QEzKC6yY1k1uVjKBVuRBQw==
X-ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=pengutronix.de;
	s=20260414; t=1796231146;
	h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
	 to:to:cc:cc:mime-version:mime-version:
	 content-transfer-encoding:content-transfer-encoding:
	 in-reply-to:in-reply-to:references:references;
	bh=ghXLCL+wAsZDpND4kFRBa+j9D1H1nMJxRmJ4XxIh0cI=;
	b=FFBNQEEOBT+isV1BvobPIU6gRjGySPb3OP1kyFsBh89yhdXg87Pku8Pg6hnBtnrgla7tJj
	DzjFRRDAhRqvGIXjcYpG/An5+hBuH0LT1R8ZGEuYTWwqgHznTPTbeOxz8vj2sME7E2dYjf
	0cKP+osOF961lXGlP6QcY53y/3GRZdT9iRZO3Onz4FGWRU8GhF19QCEWNI4nHiSc2m3c5m
	snz2hOxpL2aFOVE6O3GfnarJleQWKdqS5Y4/qMNxiKZ5u62kNk/itCDq863HFIEWPlHgId
	mfmICUmc0YtfD/cXOqIciCoS5h93DJ3tX/k4NvfT8NoWdx1F2/ZgmUOyvWE9/A==
X-ARC-Seal: i=1; s=20260414; d=pengutronix.de; t=1796231146; a=rsa-sha256;
	cv=none;
	b=QG4tRbkqbfrtLC8u52+vcKT9jbuPJ/aosXE6VR1q3jFpj7pk0bIS8npRAgGhWO0UJvI+uF
	L+w2Vxmfd8bDmTEJ8T82A/bRpk3Nxxk7C+MZr4FNJMuBAQiQ3m8WbYBRI6Pm0CcAuV3lEw
	cXtQPzJT83NRKLsPksIMTgJWCIRBIRxI+f/u1TIs5D0UjYw2J8YVaNFXGgU2x1BuGBdjUx
	HsHTKeYvrN3/6XevqZpDBNazU/LULnVVhLDH9pbbs1VqMORrWIdPtmtmsCr8Oj3vjbW/5b
	yLM5Cv/a3tAbDgQYUvnFTEsAMlgfnTFQBtqzk8wJ5QqPB292GAgVQ48lMF0zqw==
X-ARC-Authentication-Results: i=1;
	ORIGINATING;
	auth=pass smtp.auth=mkl@pengutronix.de smtp.mailfrom=mkl@pengutronix.de
X-Cc: davem@davemloft.net,
	kuba@kernel.org,
	linux-can@vger.kernel.org,
	kernel@pengutronix.de,
	Vincent Mailhol <mailhol@kernel.org>,
	Nguyen Cong Thanh <congthanh.ml.2509@gmail.com>,
	stable@vger.kernel.org,
	Marc Kleine-Budde <mkl@pengutronix.de>
X-Mailer: git-send-email 2.53.0
X-In-Reply-To: <20261202170513.2552933-1-mkl@pengutronix.de>
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list