From nobody Tue Feb 10 02:43:33 2026 Received: from smtp-out3.simply.com (smtp-out3.simply.com [94.231.106.210]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 49323364EB2; Wed, 28 Jan 2026 14:49:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=94.231.106.210 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769611776; cv=none; b=lsgobcuGKdY2EY4rIiLAzqQvTddpFSkD17+0D83RCcmLmeTJEyVX6uaTOZgdCr8NMI1y+i1JS4Zmp9tEQZT53gpaZXRW8ejZs0/yctWc8zPhNRDCD1VurXcPqFU2oST44x6dKLkGNCvzCs2kOdTZWP3FIcErsvq/DToAhBCmeek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769611776; c=relaxed/simple; bh=Nb9C6mcqFX76VfmPbSiKsIalpLxwEFJMYdlOpZfRw0g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k4WlOwU/SqBhMnWZ4EP50fp7+px8eD0raHvL+S6KbEd0KCGog9VDNxz/ey94GWbWUTghfgPliAVfqCrW2LnuHJ1uu2HJbH0zE8pAS6LCLE513PkV9vSq2cubqlLOd75z3/aRf2C630jdzPiCsrlES/Sf/rq5Pe82G4LjBu1Rg/k= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gaisler.com; spf=pass smtp.mailfrom=gaisler.com; dkim=fail (0-bit key) header.d=gaisler.com header.i=@gaisler.com header.b=LrW0CCpE reason="key not found in DNS"; arc=none smtp.client-ip=94.231.106.210 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gaisler.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gaisler.com Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="key not found in DNS" (0-bit key) header.d=gaisler.com header.i=@gaisler.com header.b="LrW0CCpE" Received: from localhost (localhost [127.0.0.1]) by smtp.simply.com (Simply.com) with ESMTP id 4f1QCc0BXHz1DR2b; Wed, 28 Jan 2026 15:49:32 +0100 (CET) Received: from d-5xj5g74.got.gaisler.com.com (h-98-128-223-123.NA.cust.bahnhof.se [98.128.223.123]) by smtp.simply.com (Simply.com) with ESMTPA id 4f1QCb50VWz1FQVr; Wed, 28 Jan 2026 15:49:31 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gaisler.com; s=simplycom2; t=1769611771; bh=U7a/Lfd9ejcMVj7P5oPql/Qxige+KLxPk95VuUAYL5c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=LrW0CCpE8XBsr9L8qVx2z76vpqfOpBFgZskDOqjm7OeDts95RWrRZvuaj21iJZBuM kSCJ6RXGQBgl7ZQWZVP1P87qBL5bOcNaJmq8uS19ouQsskrzLZ1MnTshI8H6ga7jWH mDfQIV/k0/phDa4yv2JV6CLJZUuZ9669ULorSRmjBwUBJH0JM30SzcCUzNic4O8Psk F0XrG8TwY0EdwsS1doKGFppWLybSv6KY2U/TiMQ50ExJdhG7CkhW/9KSo+NmG+w1hI OM5cE+w0EzoAvnq875ZEmbr2vWzdq3TwSEyoM+Bv5uH9NpbAMJTOyHWp6dGOjbM+Vp lxhY+D7weMp/w== From: Arun Muthusamy To: robh@kernel.org, krzk+dt@kernel.org, conor+dt@kernel.org, mkl@pengutronix.de, mailhol@kernel.org Cc: devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-can@vger.kernel.org, Arun Muthusamy Subject: [PATCH v4 14/15] can: grcan: Update echo skb handling to match variable length CANFD frame Date: Wed, 28 Jan 2026 15:49:20 +0100 Message-ID: <20260128144921.5458-15-arun.muthusamy@gaisler.com> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260128144921.5458-1-arun.muthusamy@gaisler.com> References: <20260128144921.5458-1-arun.muthusamy@gaisler.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Refactor echo socket buffer management by introducing dedicated indices for current and next echo slots. - Introduce "echo_skb_idx" to keep track of the current packet index in the echo buffer, and "next_echo_idx" for the next available slot. - Adjust memory allocation for echo skb to calculate the number of slots based on slot size. - Enhance logic in catch_up_echo_skb() to correctly process and free echo skbs. - Initialize "next_echo_idx" in grcan_set_mode() to ensure proper starting conditions when the device enters proper modes. - Improve memory and index handling in grcan_start_xmit() and added a check to stop the network queue when necessary. Signed-off-by: Arun Muthusamy --- drivers/net/can/grcan.c | 53 ++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 14 deletions(-) diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c index d838fb0698f4..e6f3cd36ea66 100644 --- a/drivers/net/can/grcan.c +++ b/drivers/net/can/grcan.c @@ -298,6 +298,15 @@ struct grcan_priv { struct sk_buff **echo_skb; /* We allocate this on our own */ + /* + * Since the CAN FD frame has a variable length, this variable is used + * to keep track of the index of the CAN echo skb (socket buffer) frame. + */ + u32 echo_skb_idx; + + /* Next echo skb free slot index */ + u32 next_echo_idx; + /* The echo skb pointer, pointing into echo_skb and indicating which * frames can be echoed back. See the "Notes on the tx cyclic buffer * handling"-comment for grcan_start_xmit for more details. @@ -567,7 +576,7 @@ static int catch_up_echo_skb(struct net_device *dev, in= t budget, bool echo) struct grcan_registers __iomem *regs =3D priv->regs; struct grcan_dma *dma =3D &priv->dma; struct net_device_stats *stats =3D &dev->stats; - int i, work_done; + int work_done; /* Updates to priv->eskbp and wake-ups of the queue needs to * be atomic towards the reads of priv->eskbp and shut-downs @@ -578,19 +587,22 @@ static int catch_up_echo_skb(struct net_device *dev, = int budget, bool echo) for (work_done =3D 0; work_done < budget || budget < 0; work_done++) { if (priv->eskbp =3D=3D txrd) break; - i =3D priv->eskbp / GRCAN_MSG_SIZE; - if (echo) { - /* Normal echo of messages */ - stats->tx_packets++; - stats->tx_bytes +=3D can_get_echo_skb(dev, i, NULL); - } else { - /* For cleanup of untransmitted messages */ - can_free_echo_skb(dev, i, NULL); - } priv->eskbp =3D grcan_ring_add(priv->eskbp, GRCAN_MSG_SIZE, dma->tx.size); txrd =3D grcan_read_reg(®s->txrd); + + /* Grab the packet once the packet is send or free untransmitted packet = */ + if (priv->eskbp =3D=3D txrd) { + if (echo) { + /* Normal echo of messages */ + stats->tx_packets++; + stats->tx_bytes +=3D can_get_echo_skb(dev, priv->echo_skb_idx, NULL); + } else { + /* For cleanup of untransmitted messages */ + can_free_echo_skb(dev, priv->echo_skb_idx, NULL); + } + } } return work_done; } @@ -1098,6 +1110,7 @@ static int grcan_set_mode(struct net_device *dev, enu= m can_mode mode) if (!(priv->can.ctrlmode & CAN_CTRLMODE_LISTENONLY)) netif_wake_queue(dev); } + priv->next_echo_idx =3D 0; spin_unlock_irqrestore(&priv->lock, flags); return err; } @@ -1109,6 +1122,7 @@ static int grcan_open(struct net_device *dev) struct grcan_priv *priv =3D netdev_priv(dev); struct grcan_dma *dma =3D &priv->dma; unsigned long flags; + u32 nr_echo_slots; int err; /* Allocate memory */ @@ -1119,13 +1133,15 @@ static int grcan_open(struct net_device *dev) return err; } - priv->echo_skb =3D kcalloc(dma->tx.size, sizeof(*priv->echo_skb), + nr_echo_slots =3D dma->tx.size / GRCAN_MSG_SIZE; + + priv->echo_skb =3D kcalloc(nr_echo_slots, sizeof(*priv->echo_skb), GFP_KERNEL); if (!priv->echo_skb) { err =3D -ENOMEM; goto exit_free_dma_buffers; } - priv->can.echo_skb_max =3D dma->tx.size; + priv->can.echo_skb_max =3D nr_echo_slots; priv->can.echo_skb =3D priv->echo_skb; /* Get can device up */ @@ -1485,8 +1501,11 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *= skb, space =3D grcan_txspace(dma->tx.size, txwr, priv->eskbp); bds =3D grcan_numbds(len); - if (unlikely(space =3D=3D 1)) + if (unlikely(space < bds || priv->can.echo_skb[priv->echo_skb_idx])) { netif_stop_queue(dev); + spin_unlock_irqrestore(&priv->lock, flags); + return NETDEV_TX_BUSY; + } spin_unlock_irqrestore(&priv->lock, flags); /* End of critical section*/ @@ -1565,7 +1584,13 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *= skb, * can_put_echo_skb would be an error unless other measures are * taken. */ - can_put_echo_skb(skb, dev, slotindex, 0); + + priv->echo_skb_idx =3D priv->next_echo_idx; + + can_put_echo_skb(skb, dev, priv->next_echo_idx, 0); + + /* Move to the next index in the echo skb buffer */ + priv->next_echo_idx =3D (priv->next_echo_idx + 1) % priv->can.echo_skb_ma= x; /* Make sure everything is written before allowing hardware to * read from the memory -- 2.51.0