Skip to contentSkip to conversation

net: rnpgbe: fix mailbox endianness

PATCH Applied Dong Yibo 4 participants 5 messages Jul 1 – Jul 7

Subsystems (3)
lore
Share & tools
lore

Dong Yibo

author maintainer
#0 lore raw

v2 cover net: rnpgbe: fix mailbox endianness

The rnpgbe mailbox exchanges data through 32-bit MMIO registers in little-endian wire format. The original code had two problems:

1. FW structs with __le16/__le32 fields were cast to (u32 *) before reaching the transport, hiding the endian annotations from sparse.

2. No cpu_to_le32()/le32_to_cpu() conversion was performed between the CPU-endian MMIO values and the little-endian payload, causing data corruption on big-endian systems.

v2 fixes this by introducing union wrappers around the FW structs and adding the missing byte-order conversions in the transport layer. All pointer casts on the mailbox data path are eliminated.

Changelog:
v1 -> v2:
- Remove all pointer casts on the mailbox data path. Use union wrappers (mbx_fw_cmd_req_u, mbx_fw_cmd_reply_u) that overlay each FW struct with a __le32 dwords[] array. Callers fill named fields with cpu_to_le16/32() and pass dwords[] directly to the transport — no casts needed.
- Change transport signatures from u32 */void * to explicit __le32 * so sparse can verify endian correctness.
- Add comments in mucse_read_mbx_pf() and mucse_write_mbx_pf() explaining why memcpy_toio() cannot replace the readl()/writel() loop (the mailbox uses 32-bit MMIO registers, not byte- addressable RAM).

links:
---
v1: https://lore.kernel.org/netdev/[email protected]/

Dong Yibo (1):
net: rnpgbe: fix mailbox endianness and remove pointer casts

 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx.c    | 26 ++++--
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx.h    |  5 +-
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.c | 82 ++++++++++---------
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.h | 14 ++++
 4 files changed, 80 insertions(+), 47 deletions(-)
Signature · 1 line
2.25.1
author maintainer
#1 lore raw

PATCH v2 1/1 net: rnpgbe: fix mailbox endianness and remove pointer casts

The rnpgbe mailbox exchanges data through 32-bit MMIO registers in little-endian wire format. The original code had two problems:

1. FW structs (with __le16/__le32 fields) were cast to (u32 *) before reaching the mailbox transport, hiding the endian annotations from sparse.

2. No cpu_to_le32()/le32_to_cpu() conversion was done between CPU-endian MMIO values and the little-endian payload, causing data corruption on big-endian systems.

Fix by adding the missing byte-order conversions in the transport layer and introducing union wrappers (mbx_fw_cmd_req_u, mbx_fw_cmd_reply_u) that overlay each FW struct with a __le32 dwords[] array. Callers fill named fields using cpu_to_le16/32(), then pass dwords[] to the transport, which now takes explicit __le32 * instead of u32 *. This eliminates all pointer casts on the mailbox data path and lets sparse verify the conversions.

Fixes 4543534c3ef5 ("net: rnpgbe: Add basic mbx ops support")
---
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx.c    | 26 ++++--
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx.h    |  5 +-
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.c | 82 ++++++++++---------
 .../net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.h | 14 ++++
 4 files changed, 80 insertions(+), 47 deletions(-)
drivers/net/ethernet/mucse/rnpgbe/rnpgbe_mbx.c +18 -8
index de5e29230b3c..c46408698263 100644
@@ -166,18 +166,23 @@ static void mucse_mbx_inc_pf_ack(struct mucse_hw *hw)
  *
  * Return: 0 on success, negative errno on failure
  **/
-static int mucse_read_mbx_pf(struct mucse_hw *hw, u32 *msg, u16 size)
+static int mucse_read_mbx_pf(struct mucse_hw *hw, __le32 *msg, u16 size)
 {
-	const int size_in_words = size / sizeof(u32);
+	const int size_in_words = size / sizeof(__le32);
 	struct mucse_mbx_info *mbx = &hw->mbx;
+	int off = MUCSE_MBX_FWPF_SHM;
 	int err;
 
 	err = mucse_obtain_mbx_lock_pf(hw);
 	if (err)
 		return err;
 
+	/* memcpy_fromio() is unsuitable: the mailbox uses 32-bit MMIO
+	 * registers, not byte-addressable RAM. readl() guarantees
+	 * the required 32-bit access width.
+	 */
 	for (int i = 0; i < size_in_words; i++)
-		msg[i] = mbx_data_rd32(mbx, MUCSE_MBX_FWPF_SHM + 4 * i);
+		msg[i] = cpu_to_le32(mbx_data_rd32(mbx, off + 4 * i));
 	/* Hw needs write data_reg at last */
 	mbx_data_wr32(mbx, MUCSE_MBX_FWPF_SHM, 0);
 	/* flush reqs as we have read this request data */
@@ -236,7 +241,7 @@ static int mucse_poll_for_msg(struct mucse_hw *hw)
  * Return: 0 if it successfully received a message notification and
  * copied it into the receive buffer, negative errno on failure
  **/
-int mucse_poll_and_read_mbx(struct mucse_hw *hw, u32 *msg, u16 size)
+int mucse_poll_and_read_mbx(struct mucse_hw *hw, __le32 *msg, u16 size)
 {
 	int err;
 
@@ -290,9 +295,9 @@ static void mucse_mbx_inc_pf_req(struct mucse_hw *hw)
  * Return: 0 if it successfully copied message into the buffer,
  * negative errno on failure
  **/
-static int mucse_write_mbx_pf(struct mucse_hw *hw, u32 *msg, u16 size)
+static int mucse_write_mbx_pf(struct mucse_hw *hw, const __le32 *msg, u16 size)
 {
-	const int size_in_words = size / sizeof(u32);
+	const int size_in_words = size / sizeof(__le32);
 	struct mucse_mbx_info *mbx = &hw->mbx;
 	int err;
 
@@ -300,8 +305,12 @@ static int mucse_write_mbx_pf(struct mucse_hw *hw, u32 *msg, u16 size)
 	if (err)
 		return err;
 
+	/* memcpy_toio() would decompose into arbitrary-width accesses;
+	 * the mailbox requires 32-bit MMIO writes via writel().
+	 */
 	for (int i = 0; i < size_in_words; i++)
-		mbx_data_wr32(mbx, MUCSE_MBX_FWPF_SHM + i * 4, msg[i]);
+		mbx_data_wr32(mbx, MUCSE_MBX_FWPF_SHM + i * 4,
+			      le32_to_cpu(msg[i]));
 
 	/* flush acks as we are overwriting the message buffer */
 	hw->mbx.fw_ack = mucse_mbx_get_fwack(mbx);
@@ -360,7 +369,8 @@ static int mucse_poll_for_ack(struct mucse_hw *hw)
  * Return: 0 if it successfully copied message into the buffer and
  * received an ack to that message within delay * timeout_cnt period
  **/
-int mucse_write_and_wait_ack_mbx(struct mucse_hw *hw, u32 *msg, u16 size)
+int mucse_write_and_wait_ack_mbx(struct mucse_hw *hw, const __le32 *msg,
+				 u16 size)
 {
 	int err;
 
drivers/net/ethernet/mucse/rnpgbe/rnpgbe_mbx.h +3 -2
index e6fcc8d1d3ca..75b88b18b04d 100644
@@ -14,7 +14,8 @@
 #define MUCSE_MBX_REQ             BIT(0) /* Request a req to mailbox */
 #define MUCSE_MBX_PFU             BIT(3) /* PF owns the mailbox buffer */
 
-int mucse_write_and_wait_ack_mbx(struct mucse_hw *hw, u32 *msg, u16 size);
+int mucse_write_and_wait_ack_mbx(struct mucse_hw *hw,
+				 const __le32 *msg, u16 size);
 void mucse_init_mbx_params_pf(struct mucse_hw *hw);
-int mucse_poll_and_read_mbx(struct mucse_hw *hw, u32 *msg, u16 size);
+int mucse_poll_and_read_mbx(struct mucse_hw *hw, __le32 *msg, u16 size);
 #endif /* _RNPGBE_MBX_H */
drivers/net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.c +45 -37

150 lines not on this page · raw message

drivers/net/ethernet/mucse/rnpgbe/rnpgbe_mbx_fw.h +14 -0
index fb24fc12b613..fe996aeffc4d 100644
@@ -80,6 +80,20 @@ struct mbx_fw_cmd_reply {
 	};
 } __packed;
 
+/* Union wrappers to expose struct as __le32 dword array for mailbox
+ * transport, eliminating the need for pointer casts.  The __packed
+ * structs have no padding, so dwords[] overlays the fields exactly.
+ */
+union mbx_fw_cmd_req_u {
+	struct mbx_fw_cmd_req r;
+	__le32 dwords[sizeof(struct mbx_fw_cmd_req) / sizeof(__le32)];
+};
+
+union mbx_fw_cmd_reply_u {
+	struct mbx_fw_cmd_reply r;
+	__le32 dwords[sizeof(struct mbx_fw_cmd_reply) / sizeof(__le32)];
+};
+
 int mucse_mbx_sync_fw(struct mucse_hw *hw);
 int mucse_mbx_powerup(struct mucse_hw *hw, bool is_powerup);
 int mucse_mbx_reset_hw(struct mucse_hw *hw);
Signature · 1 line
2.25.1
Reviewed-by on v2 1/1
#2 lore raw

Re: [PATCH net-next v2 1/1] net: rnpgbe: fix mailbox endianness and remove pointer casts

Quoted 19 lines · Dong Yibo

The rnpgbe mailbox exchanges data through 32-bit MMIO registers in little-endian wire format. The original code had two problems:

1. FW structs (with __le16/__le32 fields) were cast to (u32 *) before reaching the mailbox transport, hiding the endian annotations from sparse.

2. No cpu_to_le32()/le32_to_cpu() conversion was done between CPU-endian MMIO values and the little-endian payload, causing data corruption on big-endian systems.

Fix by adding the missing byte-order conversions in the transport layer and introducing union wrappers (mbx_fw_cmd_req_u, mbx_fw_cmd_reply_u) that overlay each FW struct with a __le32 dwords[] array. Callers fill named fields using cpu_to_le16/32(), then pass dwords[] to the transport, which now takes explicit __le32 * instead of u32 *. This eliminates all pointer casts on the mailbox data path and lets sparse verify the conversions.

Quoted 2 lines
Fixes 4543534c3ef5 ("net: rnpgbe: Add basic mbx ops support")

Andrew

maintainer
#3 lore raw
In reply to Dong Yibo
Quoted 28 lines · Dong Yibo

The rnpgbe mailbox exchanges data through 32-bit MMIO registers in little-endian wire format. The original code had two problems:

1. FW structs with __le16/__le32 fields were cast to (u32 *) before reaching the transport, hiding the endian annotations from sparse.

2. No cpu_to_le32()/le32_to_cpu() conversion was performed between the CPU-endian MMIO values and the little-endian payload, causing data corruption on big-endian systems.

v2 fixes this by introducing union wrappers around the FW structs and adding the missing byte-order conversions in the transport layer. All pointer casts on the mailbox data path are eliminated.

Changelog:
v1 -> v2:
- Remove all pointer casts on the mailbox data path. Use union wrappers (mbx_fw_cmd_req_u, mbx_fw_cmd_reply_u) that overlay each FW struct with a __le32 dwords[] array. Callers fill named fields with cpu_to_le16/32() and pass dwords[] directly to the transport — no casts needed.
- Change transport signatures from u32 */void * to explicit __le32 * so sparse can verify endian correctness.
- Add comments in mucse_read_mbx_pf() and mucse_write_mbx_pf() explaining why memcpy_toio() cannot replace the readl()/writel() loop (the mailbox uses 32-bit MMIO registers, not byte- addressable RAM).

Quoted 3 lines

links:
---
v1: https://lore.kernel.org/netdev/[email protected]/

A few process notes:
- this is 'net' material, I'm applying the patch there
- no needed for a cover letter for a one-patch series

/P

#4 lore raw
In reply to Dong Yibo
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:

Hello:

This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:

Quoted 11 lines · On Wed, 1 Jul 2026 11:22:07 +0800 you wrote:

The rnpgbe mailbox exchanges data through 32-bit MMIO registers in little-endian wire format. The original code had two problems:

1. FW structs with __le16/__le32 fields were cast to (u32 *) before reaching the transport, hiding the endian annotations from sparse.

2. No cpu_to_le32()/le32_to_cpu() conversion was performed between the CPU-endian MMIO values and the little-endian payload, causing data corruption on big-endian systems.

[...]

Here is the summary with links:
- [net-next,v2,1/1] net: rnpgbe: fix mailbox endianness and remove pointer casts

    https://git.kernel.org/netdev/net/c/d9d6d67f4c08

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html