[net-next PATCH v2] net: phy: aquantia: drop wrong endianness conversion for addr and CRC

From: Christian Marangi
Date: Sun Nov 26 2023 - 19:37:31 EST


On further testing on BE target with kernel test robot, it was notice
that the endianness conversion for addr and CRC in fw_load_memory was
wrong and actually not needed. Values in define doesn't get converted
and are passed as is and hardcoded values are already in what the PHY
require, that is LE.

Use get_unaligned_le32 instead of get_unaligned for FW data word load to
correctly convert data in the correct order to follow system endian.

Also drop the cpu_to_be32 for CRC calculation as it's wrong and use
get_unaligned_be32 instead. The word is taken from firmware and is
always LE, the mailbox will emit a BE CRC from BE word hence the
word needs to be swapped on u8 to u32 cast on LE system.
This is needed as crc_ccitt_false will recast u32 to u8 and read order
changes between BE and LE system. By using get_unaligned_be32, word is
swapped only when needed resulting in the correct CRC calculated.

Reported-by: kernel test robot <lkp@xxxxxxxxx>
Closes: https://lore.kernel.org/oe-kbuild-all/202311210414.sEJZjlcD-lkp@xxxxxxxxx/
Fixes: e93984ebc1c8 ("net: phy: aquantia: add firmware load support")
Tested-by: Robert Marko <robimarko@xxxxxxxxx> # ipq8072 LE device
Signed-off-by: Christian Marangi <ansuelsmth@xxxxxxxxx>
---
Changes v2:
- Add further explaination in commit description
- Fix wrong CRC conversion and swap only when needed

drivers/net/phy/aquantia/aquantia_firmware.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/drivers/net/phy/aquantia/aquantia_firmware.c b/drivers/net/phy/aquantia/aquantia_firmware.c
index c5f292b1c4c8..c12e8a3acb77 100644
--- a/drivers/net/phy/aquantia/aquantia_firmware.c
+++ b/drivers/net/phy/aquantia/aquantia_firmware.c
@@ -93,9 +93,9 @@ static int aqr_fw_load_memory(struct phy_device *phydev, u32 addr,
u16 crc = 0, up_crc;
size_t pos;

- /* PHY expect addr in LE */
- addr = (__force u32)cpu_to_le32(addr);
-
+ /* PHY expect addr in LE. Hardcoded addr in defines are
+ * already in this format.
+ */
phy_write_mmd(phydev, MDIO_MMD_VEND1,
VEND1_GLOBAL_MAILBOX_INTERFACE1,
VEND1_GLOBAL_MAILBOX_INTERFACE1_CRC_RESET);
@@ -113,7 +113,7 @@ static int aqr_fw_load_memory(struct phy_device *phydev, u32 addr,
u32 word;

/* FW data is always stored in little-endian */
- word = get_unaligned((const u32 *)(data + pos));
+ word = get_unaligned_le32((const u32 *)(data + pos));

phy_write_mmd(phydev, MDIO_MMD_VEND1, VEND1_GLOBAL_MAILBOX_INTERFACE5,
VEND1_GLOBAL_MAILBOX_INTERFACE5_MSW_DATA(word));
@@ -125,10 +125,10 @@ static int aqr_fw_load_memory(struct phy_device *phydev, u32 addr,
VEND1_GLOBAL_MAILBOX_INTERFACE1_WRITE);

/* calculate CRC as we load data to the mailbox.
- * We convert word to big-endian as PHY is BE and mailbox will
+ * We read word as big-endian as PHY is BE and mailbox will
* return a BE CRC.
*/
- word = (__force u32)cpu_to_be32(word);
+ word = get_unaligned_be32((const u32 *)(data + pos));
crc = crc_ccitt_false(crc, (u8 *)&word, sizeof(word));
}

--
2.40.1