From: Arun Muthusamy <arun.muthusamy@gaisler.com>
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 <arun.muthusamy@gaisler.com>
Subject: [PATCH v7 11/15] can: grcan: Refactor GRCAN DMA buffer to use structured memory layout
Date: Fri, 8 May 2026 09:01:17 +0200 [thread overview]
Message-ID: <20260508070121.6918-12-arun.muthusamy@gaisler.com> (raw)
In-Reply-To: <20260508070121.6918-1-arun.muthusamy@gaisler.com>
- Introduce a structured layout to represent hardware memory using
struct grcan_msg_header and struct grcan_msg_fragment.
- Updated the GRCAN receive and transmit functions to utilize this
new structured approach.
- Implemented helper functions grcan_msg_header_at for accessing
header structures within DMA buffers.
- Replaced array and bitwise operations with memcpy for copying
between CAN frames and header structures.
Signed-off-by: Arun Muthusamy <arun.muthusamy@gaisler.com>
---
drivers/net/can/grcan.c | 80 +++++++++++++++++++++++++----------------
1 file changed, 49 insertions(+), 31 deletions(-)
diff --git a/drivers/net/can/grcan.c b/drivers/net/can/grcan.c
index b5527fd9e02e..4921da349a0e 100644
--- a/drivers/net/can/grcan.c
+++ b/drivers/net/can/grcan.c
@@ -195,9 +195,6 @@ struct grcan_registers {
#define GRCAN_MSG_OFF 0x00000002
#define GRCAN_MSG_PASS 0x00000001
-#define GRCAN_MSG_DATA_SLOT_INDEX(i) (2 + (i) / 4)
-#define GRCAN_MSG_DATA_SHIFT(i) ((3 - (i) % 4) * 8)
-
#define GRCAN_BUFFER_ALIGNMENT 1024
#define GRCAN_DEFAULT_BUFFER_SIZE 1024
#define GRCAN_VALID_TR_SIZE_MASK 0x001fffc0
@@ -242,9 +239,23 @@ struct grcan_hwcap {
bool fd;
};
+struct grcan_msg_header {
+ u32 id;
+ u32 ctrl;
+ u8 data[CAN_MAX_DLEN];
+} __packed;
+
+struct grcan_msg_fragment {
+ u8 data[GRCAN_MSG_SIZE];
+} __packed;
+
struct grcan_dma_buffer {
size_t size;
void *buf;
+ union {
+ struct grcan_msg_header *header;
+ struct grcan_msg_fragment *frag;
+ };
dma_addr_t handle;
};
@@ -417,6 +428,12 @@ static inline u32 grcan_txspace(size_t txsize, u32 txwr, u32 eskbp)
return slots - used;
}
+static inline struct grcan_msg_header *
+grcan_msg_header_at(struct grcan_dma_buffer *dbuf, u32 offset)
+{
+ return (struct grcan_msg_header *)((u8 *)dbuf->buf + offset);
+}
+
/* Configuration parameters that can be set via module parameters */
static struct grcan_device_config grcan_module_config =
GRCAN_DEFAULT_DEVICE_CONFIG;
@@ -1215,11 +1232,11 @@ static int grcan_receive(struct net_device *dev, int budget)
struct grcan_registers __iomem *regs = priv->regs;
struct grcan_dma *dma = &priv->dma;
struct net_device_stats *stats = &dev->stats;
+ struct grcan_msg_header *hdr;
struct can_frame *cf;
struct sk_buff *skb;
u32 wr, rd, startrd;
- u32 *slot;
- u32 i, rtr, eff, j, shift;
+ u32 rtr, eff;
int work_done = 0;
rd = grcan_read_reg(®s->rxrd);
@@ -1239,30 +1256,33 @@ static int grcan_receive(struct net_device *dev, int budget)
continue;
}
- slot = dma->rx.buf + rd;
- eff = slot[0] & GRCAN_MSG_IDE;
- rtr = slot[0] & GRCAN_MSG_RTR;
+ hdr = grcan_msg_header_at(&dma->rx, rd);
+
+ eff = hdr->id & GRCAN_MSG_IDE;
+ rtr = hdr->id & GRCAN_MSG_RTR;
+
if (eff) {
- cf->can_id = ((slot[0] & GRCAN_MSG_EID)
+ cf->can_id = ((hdr->id & GRCAN_MSG_EID)
>> GRCAN_MSG_EID_BIT);
cf->can_id |= CAN_EFF_FLAG;
} else {
- cf->can_id = ((slot[0] & GRCAN_MSG_BID)
+ cf->can_id = ((hdr->id & GRCAN_MSG_BID)
>> GRCAN_MSG_BID_BIT);
}
- cf->len = can_cc_dlc2len((slot[1] & GRCAN_MSG_DLC)
- >> GRCAN_MSG_DLC_BIT);
+
+ cf->len = can_cc_dlc2len((hdr->ctrl & GRCAN_MSG_DLC)
+ >> GRCAN_MSG_DLC_BIT);
+
if (rtr) {
cf->can_id |= CAN_RTR_FLAG;
} else {
- for (i = 0; i < cf->len; i++) {
- j = GRCAN_MSG_DATA_SLOT_INDEX(i);
- shift = GRCAN_MSG_DATA_SHIFT(i);
- cf->data[i] = (u8)(slot[j] >> shift);
- }
+ if (cf->len > 0)
+ memcpy(cf->data, hdr->data,
+ min_t(u32, cf->len, CAN_MAX_DLEN));
stats->rx_bytes += cf->len;
}
+
stats->rx_packets++;
netif_receive_skb(skb);
@@ -1395,11 +1415,10 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
struct grcan_registers __iomem *regs = priv->regs;
struct grcan_dma *dma = &priv->dma;
struct can_frame *cf = (struct can_frame *)skb->data;
+ struct grcan_msg_header *hdr;
u32 id, txwr, txrd, space, txctrl;
int slotindex;
- u32 *slot;
- u32 i, rtr, eff, dlc, tmp, err;
- int j, shift;
+ u32 rtr, eff, dlc, tmp, err;
unsigned long flags;
u32 oneshotmode = priv->can.ctrlmode & CAN_CTRLMODE_ONE_SHOT;
@@ -1422,7 +1441,6 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
space = grcan_txspace(dma->tx.size, txwr, priv->eskbp);
slotindex = txwr / GRCAN_MSG_SIZE;
- slot = dma->tx.buf + txwr;
if (unlikely(space == 1))
netif_stop_queue(dev);
@@ -1438,6 +1456,9 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
return NETDEV_TX_BUSY;
}
+ hdr = grcan_msg_header_at(&dma->tx, txwr);
+ memset(hdr, 0, sizeof(*hdr));
+
/* Convert and write CAN message to DMA buffer */
eff = cf->can_id & CAN_EFF_FLAG;
rtr = cf->can_id & CAN_RTR_FLAG;
@@ -1447,16 +1468,13 @@ static netdev_tx_t grcan_start_xmit(struct sk_buff *skb,
tmp = (id << GRCAN_MSG_EID_BIT) & GRCAN_MSG_EID;
else
tmp = (id << GRCAN_MSG_BID_BIT) & GRCAN_MSG_BID;
- slot[0] = (eff ? GRCAN_MSG_IDE : 0) | (rtr ? GRCAN_MSG_RTR : 0) | tmp;
-
- slot[1] = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
- slot[2] = 0;
- slot[3] = 0;
- for (i = 0; i < dlc; i++) {
- j = GRCAN_MSG_DATA_SLOT_INDEX(i);
- shift = GRCAN_MSG_DATA_SHIFT(i);
- slot[j] |= cf->data[i] << shift;
- }
+
+ hdr->id = (eff ? GRCAN_MSG_IDE : 0) | (rtr ? GRCAN_MSG_RTR : 0) | tmp;
+
+ hdr->ctrl = ((dlc << GRCAN_MSG_DLC_BIT) & GRCAN_MSG_DLC);
+
+ if (dlc > 0)
+ memcpy(hdr->data, cf->data, min_t(u32, cf->len, CAN_MAX_DLEN));
/* Checking that channel has not been disabled. These cases
* should never happen
--
2.51.0
next prev parent reply other threads:[~2026-05-08 7:01 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-08 7:01 [PATCH v7 RESEND 00/15] can: grcan: Enhance driver with CANFD Support and Improvements Arun Muthusamy
2026-05-08 7:01 ` [PATCH v7 01/15] dt-bindings: Add vendor prefix for Frontgrade Gaisler AB Arun Muthusamy
2026-05-08 7:01 ` [PATCH v7 02/15] dt-bindings: net: can: gaisler,grcan: Convert to DT schema Arun Muthusamy
2026-05-08 18:37 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 03/15] MAINTAINERS: Add maintainers for GRCAN CAN network driver Arun Muthusamy
2026-05-08 7:01 ` [PATCH v7 04/15] can: grcan: Add clock handling Arun Muthusamy
2026-05-08 18:50 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 05/15] can: grcan: Replace bit timing macros with literal values Arun Muthusamy
2026-05-08 7:01 ` [PATCH v7 06/15] can: grcan: Simplify timing configuration Arun Muthusamy
2026-05-08 19:28 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 07/15] can: grcan: add FD capability detection and nominal bit-timing Arun Muthusamy
2026-05-08 19:45 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 08/15] can: grcan: set DMA mask for GRCAN and GRCANFD to 32-bit Arun Muthusamy
2026-05-08 19:53 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 09/15] can: grcan: Add saving and restoring of CAN FD baud-rate registers Arun Muthusamy
2026-05-08 21:35 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 10/15] can: grcan: Reserve space between cap and next register to align with address layout Arun Muthusamy
2026-05-08 7:01 ` Arun Muthusamy [this message]
2026-05-08 22:03 ` [PATCH v7 11/15] can: grcan: Refactor GRCAN DMA buffer to use structured memory layout sashiko-bot
2026-05-08 7:01 ` [PATCH v7 12/15] can: grcan: Add CANFD TX support alongside legacy CAN Arun Muthusamy
2026-05-08 22:40 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 13/15] can: grcan: Add CANFD RX " Arun Muthusamy
2026-05-08 23:08 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 14/15] can: grcan: Update echo skb handling to match variable length CANFD frame Arun Muthusamy
2026-05-08 23:25 ` sashiko-bot
2026-05-08 7:01 ` [PATCH v7 15/15] can: grcan: Advertise CANFD capability Arun Muthusamy
2026-05-08 23:50 ` 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=20260508070121.6918-12-arun.muthusamy@gaisler.com \
--to=arun.muthusamy@gaisler.com \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=krzk+dt@kernel.org \
--cc=linux-can@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mailhol@kernel.org \
--cc=mkl@pengutronix.de \
--cc=robh@kernel.org \
/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