[RFC net-next 4/4] net/mlx5e: Implement set_module_eeprom_by_page ethtool callback
From: Björn Töpel <bjorn@kernel.org>
Date: 2026-02-19 13:01:22
Also in:
linux-rdma, lkml
Subsystem:
mellanox ethernet driver (mlx5e), mellanox mlx5 core vpi driver, networking drivers, the rest · Maintainers:
Saeed Mahameed, Tariq Toukan, Mark Bloch, Leon Romanovsky, Andrew Lunn, "David S. Miller", Eric Dumazet, Jakub Kicinski, Paolo Abeni, Linus Torvalds
Add EEPROM write support by implementing the set_module_eeprom_by_page ethtool_ops callback, mirroring the existing get_module_eeprom_by_page read path. This enables the kernel's module loopback SET path which requires both get and set callbacks. The write path reuses the MCIA register (MLX5_REG_MCIA) via mlx5_core_access_reg() with write=1 instead of write=0. This seems to work, but I'd like some proper feedback from the mlx5 folks! ;-) Signed-off-by: Björn Töpel <bjorn@kernel.org> --- .../ethernet/mellanox/mlx5/core/en_ethtool.c | 52 +++++++++++++------ .../ethernet/mellanox/mlx5/core/mlx5_core.h | 6 +-- .../net/ethernet/mellanox/mlx5/core/port.c | 34 ++++++++---- 3 files changed, 64 insertions(+), 28 deletions(-)
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
index 4a8dc85d5924..1dce67485651 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/en_ethtool.c@@ -2103,22 +2103,24 @@ static int mlx5e_get_module_eeprom(struct net_device *netdev, return 0; } -static int mlx5e_get_module_eeprom_by_page(struct net_device *netdev, - const struct ethtool_module_eeprom *page_data, - struct netlink_ext_ack *extack) +static int __mlx5e_access_module_eeprom_by_page(struct net_device *netdev, + const struct ethtool_module_eeprom *page_data, + struct netlink_ext_ack *extack, + bool write) { struct mlx5e_priv *priv = netdev_priv(netdev); struct mlx5_module_eeprom_query_params query; struct mlx5_core_dev *mdev = priv->mdev; u8 *data = page_data->data; - int size_read; + int size_xfered; u8 status = 0; int i = 0; if (!page_data->length) return -EINVAL; - memset(data, 0, page_data->length); + if (!write) + memset(data, 0, page_data->length); query.offset = page_data->offset; query.i2c_address = page_data->i2c_address;
@@ -2126,28 +2128,47 @@ static int mlx5e_get_module_eeprom_by_page(struct net_device *netdev, query.page = page_data->page; while (i < page_data->length) { query.size = page_data->length - i; - size_read = mlx5_query_module_eeprom_by_page(mdev, &query, - data + i, &status); + size_xfered = mlx5_access_module_eeprom_by_page(mdev, &query, + data + i, &status, + write); - /* Done reading, return how many bytes was read */ - if (!size_read) + if (!size_xfered) return i; - if (size_read < 0) { + if (size_xfered < 0) { NL_SET_ERR_MSG_FMT_MOD( extack, - "Query module eeprom by page failed, read %u bytes, err %d, status %u", - i, size_read, status); - return size_read; + "%s module eeprom by page failed, %s %u bytes, err %d, status %u", + write ? "Set" : "Query", + write ? "wrote" : "read", + i, size_xfered, status); + return size_xfered; } - i += size_read; - query.offset += size_read; + i += size_xfered; + query.offset += size_xfered; } return i; } +static int mlx5e_get_module_eeprom_by_page(struct net_device *netdev, + const struct ethtool_module_eeprom *page_data, + struct netlink_ext_ack *extack) +{ + return __mlx5e_access_module_eeprom_by_page(netdev, page_data, extack, false); +} + +static int mlx5e_set_module_eeprom_by_page(struct net_device *netdev, + const struct ethtool_module_eeprom *page_data, + struct netlink_ext_ack *extack) +{ + int err; + + err = __mlx5e_access_module_eeprom_by_page(netdev, page_data, extack, true); + return err < 0 ? err : 0; +} + int mlx5e_ethtool_flash_device(struct mlx5e_priv *priv, struct ethtool_flash *flash) {
@@ -2776,6 +2797,7 @@ const struct ethtool_ops mlx5e_ethtool_ops = { .get_module_info = mlx5e_get_module_info, .get_module_eeprom = mlx5e_get_module_eeprom, .get_module_eeprom_by_page = mlx5e_get_module_eeprom_by_page, + .set_module_eeprom_by_page = mlx5e_set_module_eeprom_by_page, .flash_device = mlx5e_flash_device, .get_priv_flags = mlx5e_get_priv_flags, .set_priv_flags = mlx5e_set_priv_flags,
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h b/drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h
index b635b423d972..b69730727764 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h
+++ b/drivers/net/ethernet/mellanox/mlx5/core/mlx5_core.h@@ -361,9 +361,9 @@ void mlx5_query_port_fcs(struct mlx5_core_dev *mdev, bool *supported, int mlx5_query_module_eeprom(struct mlx5_core_dev *dev, u16 offset, u16 size, u8 *data, u8 *status); int -mlx5_query_module_eeprom_by_page(struct mlx5_core_dev *dev, - struct mlx5_module_eeprom_query_params *params, - u8 *data, u8 *status); +mlx5_access_module_eeprom_by_page(struct mlx5_core_dev *dev, + struct mlx5_module_eeprom_query_params *params, + u8 *data, u8 *status, bool write); int mlx5_query_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *out); int mlx5_set_port_dcbx_param(struct mlx5_core_dev *mdev, u32 *in);
diff --git a/drivers/net/ethernet/mellanox/mlx5/core/port.c b/drivers/net/ethernet/mellanox/mlx5/core/port.c
index ee8b9765c5ba..d68f9f9d7a80 100644
--- a/drivers/net/ethernet/mellanox/mlx5/core/port.c
+++ b/drivers/net/ethernet/mellanox/mlx5/core/port.c@@ -369,9 +369,9 @@ static int mlx5_mcia_max_bytes(struct mlx5_core_dev *dev) return (MLX5_CAP_MCAM_FEATURE(dev, mcia_32dwords) ? 32 : 12) * sizeof(u32); } -static int mlx5_query_mcia(struct mlx5_core_dev *dev, - struct mlx5_module_eeprom_query_params *params, - u8 *data, u8 *status) +static int __mlx5_access_mcia(struct mlx5_core_dev *dev, + struct mlx5_module_eeprom_query_params *params, + u8 *data, u8 *status, bool write) { u32 in[MLX5_ST_SZ_DW(mcia_reg)] = {}; u32 out[MLX5_ST_SZ_DW(mcia_reg)];
@@ -388,8 +388,13 @@ static int mlx5_query_mcia(struct mlx5_core_dev *dev, MLX5_SET(mcia_reg, in, page_number, params->page); MLX5_SET(mcia_reg, in, i2c_device_address, params->i2c_address); + if (write) { + ptr = MLX5_ADDR_OF(mcia_reg, in, dword_0); + memcpy(ptr, data, size); + } + err = mlx5_core_access_reg(dev, in, sizeof(in), out, - sizeof(out), MLX5_REG_MCIA, 0, 0); + sizeof(out), MLX5_REG_MCIA, 0, write); if (err) return err;
@@ -399,12 +404,21 @@ static int mlx5_query_mcia(struct mlx5_core_dev *dev, return -EIO; } - ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0); - memcpy(data, ptr, size); + if (!write) { + ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0); + memcpy(data, ptr, size); + } return size; } +static int mlx5_query_mcia(struct mlx5_core_dev *dev, + struct mlx5_module_eeprom_query_params *params, + u8 *data, u8 *status) +{ + return __mlx5_access_mcia(dev, params, data, status, false); +} + int mlx5_query_module_eeprom(struct mlx5_core_dev *dev, u16 offset, u16 size, u8 *data, u8 *status) {
@@ -446,9 +460,9 @@ int mlx5_query_module_eeprom(struct mlx5_core_dev *dev, return mlx5_query_mcia(dev, &query, data, status); } -int mlx5_query_module_eeprom_by_page(struct mlx5_core_dev *dev, - struct mlx5_module_eeprom_query_params *params, - u8 *data, u8 *status) +int mlx5_access_module_eeprom_by_page(struct mlx5_core_dev *dev, + struct mlx5_module_eeprom_query_params *params, + u8 *data, u8 *status, bool write) { int err;
@@ -462,7 +476,7 @@ int mlx5_query_module_eeprom_by_page(struct mlx5_core_dev *dev, return -EINVAL; } - return mlx5_query_mcia(dev, params, data, status); + return __mlx5_access_mcia(dev, params, data, status, write); } static int mlx5_query_port_pvlc(struct mlx5_core_dev *dev, u32 *pvlc,
--
2.53.0