net/mlx5: Refactor module EEPROM query
authorVladyslav Tarasiuk <vladyslavt@nvidia.com>
Fri, 9 Apr 2021 08:06:35 +0000 (11:06 +0300)
committerDavid S. Miller <davem@davemloft.net>
Sun, 11 Apr 2021 23:34:56 +0000 (16:34 -0700)
Prepare for ethtool_ops::get_module_eeprom_data() implementation by
extracting common part of mlx5_query_module_eeprom() into a separate
function.

Signed-off-by: Vladyslav Tarasiuk <vladyslavt@nvidia.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/mellanox/mlx5/core/port.c
include/linux/mlx5/port.h

index 4bb219565c58e3a8d5e265a5339260bee551d38a..9b9f870d67a42d6e8fbeefc6e0cf3f672f93f2c5 100644 (file)
@@ -353,67 +353,78 @@ static void mlx5_sfp_eeprom_params_set(u16 *i2c_addr, int *page_num, u16 *offset
        *offset -= MLX5_EEPROM_PAGE_LENGTH;
 }
 
-int mlx5_query_module_eeprom(struct mlx5_core_dev *dev,
-                            u16 offset, u16 size, u8 *data)
+static int mlx5_query_mcia(struct mlx5_core_dev *dev,
+                          struct mlx5_module_eeprom_query_params *params, u8 *data)
 {
-       int module_num, status, err, page_num = 0;
        u32 in[MLX5_ST_SZ_DW(mcia_reg)] = {};
        u32 out[MLX5_ST_SZ_DW(mcia_reg)];
-       u16 i2c_addr = 0;
-       u8 module_id;
+       int status, err;
        void *ptr;
+       u16 size;
+
+       size = min_t(int, params->size, MLX5_EEPROM_MAX_BYTES);
+
+       MLX5_SET(mcia_reg, in, l, 0);
+       MLX5_SET(mcia_reg, in, size, size);
+       MLX5_SET(mcia_reg, in, module, params->module_number);
+       MLX5_SET(mcia_reg, in, device_address, params->offset);
+       MLX5_SET(mcia_reg, in, page_number, params->page);
+       MLX5_SET(mcia_reg, in, i2c_device_address, params->i2c_address);
 
-       err = mlx5_query_module_num(dev, &module_num);
+       err = mlx5_core_access_reg(dev, in, sizeof(in), out,
+                                  sizeof(out), MLX5_REG_MCIA, 0, 0);
        if (err)
                return err;
 
-       err = mlx5_query_module_id(dev, module_num, &module_id);
+       status = MLX5_GET(mcia_reg, out, status);
+       if (status) {
+               mlx5_core_err(dev, "query_mcia_reg failed: status: 0x%x\n",
+                             status);
+               return -EIO;
+       }
+
+       ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0);
+       memcpy(data, ptr, size);
+
+       return size;
+}
+
+int mlx5_query_module_eeprom(struct mlx5_core_dev *dev,
+                            u16 offset, u16 size, u8 *data)
+{
+       struct mlx5_module_eeprom_query_params query = {0};
+       u8 module_id;
+       int err;
+
+       err = mlx5_query_module_num(dev, &query.module_number);
+       if (err)
+               return err;
+
+       err = mlx5_query_module_id(dev, query.module_number, &module_id);
        if (err)
                return err;
 
        switch (module_id) {
        case MLX5_MODULE_ID_SFP:
-               mlx5_sfp_eeprom_params_set(&i2c_addr, &page_num, &offset);
+               mlx5_sfp_eeprom_params_set(&query.i2c_address, &query.page, &query.offset);
                break;
        case MLX5_MODULE_ID_QSFP:
        case MLX5_MODULE_ID_QSFP_PLUS:
        case MLX5_MODULE_ID_QSFP28:
-               mlx5_qsfp_eeprom_params_set(&i2c_addr, &page_num, &offset);
+               mlx5_qsfp_eeprom_params_set(&query.i2c_address, &query.page, &query.offset);
                break;
        default:
                mlx5_core_err(dev, "Module ID not recognized: 0x%x\n", module_id);
                return -EINVAL;
        }
 
-       if (offset + size > MLX5_EEPROM_PAGE_LENGTH)
+       if (query.offset + size > MLX5_EEPROM_PAGE_LENGTH)
                /* Cross pages read, read until offset 256 in low page */
                size -= offset + size - MLX5_EEPROM_PAGE_LENGTH;
 
-       size = min_t(int, size, MLX5_EEPROM_MAX_BYTES);
+       query.size = size;
 
-       MLX5_SET(mcia_reg, in, l, 0);
-       MLX5_SET(mcia_reg, in, module, module_num);
-       MLX5_SET(mcia_reg, in, i2c_device_address, i2c_addr);
-       MLX5_SET(mcia_reg, in, page_number, page_num);
-       MLX5_SET(mcia_reg, in, device_address, offset);
-       MLX5_SET(mcia_reg, in, size, size);
-
-       err = mlx5_core_access_reg(dev, in, sizeof(in), out,
-                                  sizeof(out), MLX5_REG_MCIA, 0, 0);
-       if (err)
-               return err;
-
-       status = MLX5_GET(mcia_reg, out, status);
-       if (status) {
-               mlx5_core_err(dev, "query_mcia_reg failed: status: 0x%x\n",
-                             status);
-               return -EIO;
-       }
-
-       ptr = MLX5_ADDR_OF(mcia_reg, out, dword_0);
-       memcpy(data, ptr, size);
-
-       return size;
+       return mlx5_query_mcia(dev, &query, data);
 }
 EXPORT_SYMBOL_GPL(mlx5_query_module_eeprom);
 
index 23edd2db48038cfb347fe835ff3f5a43d0695baa..90b87aa82db318e308bb154ea3f066edf16bc197 100644 (file)
@@ -62,6 +62,15 @@ enum mlx5_an_status {
 #define MLX5_EEPROM_PAGE_LENGTH                256
 #define MLX5_EEPROM_HIGH_PAGE_LENGTH   128
 
+struct mlx5_module_eeprom_query_params {
+       u16 size;
+       u16 offset;
+       u16 i2c_address;
+       u32 page;
+       u32 bank;
+       u32 module_number;
+};
+
 enum mlx5e_link_mode {
        MLX5E_1000BASE_CX_SGMII  = 0,
        MLX5E_1000BASE_KX        = 1,