Skip to contentSkip to conversation

net: enetc: use get/put_unaligned() for mac address handling

PATCH Superseded Michael Walle 3 participants 4 messages Jun 4, 2021

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: enetc: use get/put_unaligned() for mac address handling

The supplied buffer for the MAC address might not be aligned. Thus doing a 32bit (or 16bit) access could be on an unaligned address. For now, enetc is only used on aarch64 which can do unaligned accesses, thus there is no error. In any case, be correct and use the get/put_unaligned() helpers.

---
 drivers/net/ethernet/freescale/enetc/enetc_pf.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/freescale/enetc/enetc_pf.c +5 -4
index 31274325159a..a96d2acb5e11 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
 /* Copyright 2017-2019 NXP */
 
+#include <asm/unaligned.h>
 #include <linux/mdio.h>
 #include <linux/module.h>
 #include <linux/fsl/enetc_mdio.h>
@@ -17,15 +18,15 @@ static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr)
 	u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si));
 	u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si));
 
-	*(u32 *)addr = upper;
-	*(u16 *)(addr + 4) = lower;
+	put_unaligned(upper, (u32 *)addr);
+	put_unaligned(lower, (u16 *)(addr + 4));
 }
 
 static void enetc_pf_set_primary_mac_addr(struct enetc_hw *hw, int si,
 					  const u8 *addr)
 {
-	u32 upper = *(const u32 *)addr;
-	u16 lower = *(const u16 *)(addr + 4);
+	u32 upper = get_unaligned((const u32 *)addr);
+	u16 lower = get_unaligned((const u16 *)(addr + 4));
 
 	__raw_writel(upper, hw->port + ENETC_PSIPMAR0(si));
 	__raw_writew(lower, hw->port + ENETC_PSIPMAR1(si));
Signature · 1 line
2.20.1
#1 lore raw
Quoted 24 lines · Michael Walle

The supplied buffer for the MAC address might not be aligned. Thus doing a 32bit (or 16bit) access could be on an unaligned address. For now, enetc is only used on aarch64 which can do unaligned accesses, thus there is no error. In any case, be correct and use the get/put_unaligned() helpers.

---
 drivers/net/ethernet/freescale/enetc/enetc_pf.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/freescale/enetc/enetc_pf.c +1 -0
index 31274325159a..a96d2acb5e11 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
 /* Copyright 2017-2019 NXP */
 
+#include <asm/unaligned.h>
 #include <linux/mdio.h>
 #include <linux/module.h>
 #include <linux/fsl/enetc_mdio.h>

drivers/net/ethernet/freescale/enetc/enetc_pf.c static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr) · line 22

Quoted 7 lines
drivers/net/ethernet/freescale/enetc/enetc_pf.c +2 -2
@@ -17,15 +18,15 @@ static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr)
 	u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si));
 	u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si));
 
-	*(u32 *)addr = upper;
-	*(u16 *)(addr + 4) = lower;
+	put_unaligned(upper, (u32 *)addr);
+	put_unaligned(lower, (u16 *)(addr + 4));

I think you want to write little endian, therefore on a BE platform this code may be wrong. Better use put_unaligned_le32? By using these versions of the unaligned helpers you could also remove the pointer cast.

Quoted 13 lines
drivers/net/ethernet/freescale/enetc/enetc_pf.c +2 -2
 }
 
 static void enetc_pf_set_primary_mac_addr(struct enetc_hw *hw, int si,
 					  const u8 *addr)
 {
-	u32 upper = *(const u32 *)addr;
-	u16 lower = *(const u16 *)(addr + 4);
+	u32 upper = get_unaligned((const u32 *)addr);
+	u16 lower = get_unaligned((const u16 *)(addr + 4));
 
 	__raw_writel(upper, hw->port + ENETC_PSIPMAR0(si));
 	__raw_writew(lower, hw->port + ENETC_PSIPMAR1(si));
#2 lore raw

Am 2021-06-04 14:44, schrieb Heiner Kallweit:

Quoted 29 lines · Michael Walle
Quoted 28 lines

The supplied buffer for the MAC address might not be aligned. Thus doing a 32bit (or 16bit) access could be on an unaligned address. For now, enetc is only used on aarch64 which can do unaligned accesses, thus there is no error. In any case, be correct and use the get/put_unaligned() helpers.

---
 drivers/net/ethernet/freescale/enetc/enetc_pf.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
drivers/net/ethernet/freescale/enetc/enetc_pf.c +1 -0
index 31274325159a..a96d2acb5e11 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
 /* Copyright 2017-2019 NXP */
 
+#include <asm/unaligned.h>
 #include <linux/mdio.h>
 #include <linux/module.h>
 #include <linux/fsl/enetc_mdio.h>
@@ -17,15 +18,15 @@ static void enetc_pf_get_primary_mac_addr(struct 
 enetc_hw *hw, int si, u8 *addr)
 	u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si));
 	u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si));
 
Quoted 9 lines

drivers/net/ethernet/freescale/enetc/enetc_pf.c static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr) · line 22

Quoted 4 lines
drivers/net/ethernet/freescale/enetc/enetc_pf.c +2 -2
-	*(u32 *)addr = upper;
-	*(u16 *)(addr + 4) = lower;
+	put_unaligned(upper, (u32 *)addr);
+	put_unaligned(lower, (u16 *)(addr + 4));

I think you want to write little endian, therefore on a BE platform this code may be wrong. Better use put_unaligned_le32? By using these versions of the unaligned helpers you could also remove the pointer cast.

I wasn't sure about the endianness. Might be the case, that on BE platforms, the endianess of the register will swap too. (OTOH I could use aarch64 with BE..)

But I'm fine with changing it. I'd presume this being the only platform for now it doesn't really matter.

-michael

maintainer
#3 lore raw
In reply to Heiner Kallweit
Quoted 4 lines

-----Original Message-----
From: Heiner Kallweit <[email protected]>
Sent: Friday, June 4, 2021 3:44 PM

[...]

Quoted 31 lines · Michael Walle
Quoted 6 lines

The supplied buffer for the MAC address might not be aligned. Thus doing a 32bit (or 16bit) access could be on an unaligned address. For now, enetc is only used on aarch64 which can do unaligned accesses, thus there is no error. In any case, be correct and use the get/put_unaligned() helpers.

Review

Quoted 6 lines
---
 drivers/net/ethernet/freescale/enetc/enetc_pf.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)
(quoted diff) +0 -0

b/drivers/net/ethernet/freescale/enetc/enetc_pf.c

drivers/net/ethernet/freescale/enetc/enetc_pf.c static void enetc_pf_get_primary_mac_addr(struct

Quoted 12 lines

index 31274325159a..a96d2acb5e11 100644

drivers/net/ethernet/freescale/enetc/enetc_pf.c +1 -0
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
 /* Copyright 2017-2019 NXP */
 
+#include <asm/unaligned.h>
 #include <linux/mdio.h>
 #include <linux/module.h>
 #include <linux/fsl/enetc_mdio.h>
@@ -17,15 +18,15 @@ static void enetc_pf_get_primary_mac_addr(struct

enetc_hw *hw, int si, u8 *addr)

Quoted 3 lines
drivers/net/ethernet/freescale/enetc/enetc_pf.c +0 -0
 	u32 upper = __raw_readl(hw->port + ENETC_PSIPMAR0(si));
 	u16 lower = __raw_readw(hw->port + ENETC_PSIPMAR1(si));
 
Quoted 10 lines

drivers/net/ethernet/freescale/enetc/enetc_pf.c static void enetc_pf_get_primary_mac_addr(struct enetc_hw *hw, int si, u8 *addr) · line 22

Quoted 4 lines
drivers/net/ethernet/freescale/enetc/enetc_pf.c +2 -2
-	*(u32 *)addr = upper;
-	*(u16 *)(addr + 4) = lower;
+	put_unaligned(upper, (u32 *)addr);
+	put_unaligned(lower, (u16 *)(addr + 4));

I think you want to write little endian, therefore on a BE platform this code may be wrong. Better use put_unaligned_le32? By using these versions of the unaligned helpers you could also remove the pointer cast.

+1