mlxsw: spectrum_buffers: Allocate prs & pms dynamically
authorPetr Machata <petrm@mellanox.com>
Wed, 20 Feb 2019 19:32:14 +0000 (19:32 +0000)
committerDavid S. Miller <davem@davemloft.net>
Thu, 21 Feb 2019 23:57:45 +0000 (15:57 -0800)
Spectrum-2 will be configured with a different set of pools than
Spectrum-1. The size of prs and pms buffers will therefore depend on the
chip type of the device.

Therefore, instead of reserving an array directly in a structure
definition, allocate the buffer in mlxsw_sp_sb_port{,s}_init().

Signed-off-by: Petr Machata <petrm@mellanox.com>
Signed-off-by: Ido Schimmel <idosch@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
drivers/net/ethernet/mellanox/mlxsw/spectrum_buffers.c

index cd23ec9268bc50023c3e79eee0e4b67e0c6b9fd8..7fa291ebcddf6fa9380402a739fe9b2cc9a0ce61 100644 (file)
@@ -63,11 +63,11 @@ static const struct mlxsw_sp_sb_pool_des mlxsw_sp_sb_pool_dess[] = {
 struct mlxsw_sp_sb_port {
        struct mlxsw_sp_sb_cm ing_cms[MLXSW_SP_SB_ING_TC_COUNT];
        struct mlxsw_sp_sb_cm eg_cms[MLXSW_SP_SB_EG_TC_COUNT];
-       struct mlxsw_sp_sb_pm pms[MLXSW_SP_SB_POOL_DESS_LEN];
+       struct mlxsw_sp_sb_pm *pms;
 };
 
 struct mlxsw_sp_sb {
-       struct mlxsw_sp_sb_pr prs[MLXSW_SP_SB_POOL_DESS_LEN];
+       struct mlxsw_sp_sb_pr *prs;
        struct mlxsw_sp_sb_port *ports;
        u32 cell_size;
        u64 sb_size;
@@ -283,20 +283,68 @@ static int mlxsw_sp_port_headroom_init(struct mlxsw_sp_port *mlxsw_sp_port)
        return mlxsw_sp_port_pb_prio_init(mlxsw_sp_port);
 }
 
+static int mlxsw_sp_sb_port_init(struct mlxsw_sp *mlxsw_sp,
+                                struct mlxsw_sp_sb_port *sb_port)
+{
+       struct mlxsw_sp_sb_pm *pms;
+
+       pms = kcalloc(MLXSW_SP_SB_POOL_DESS_LEN, sizeof(*pms), GFP_KERNEL);
+       if (!pms)
+               return -ENOMEM;
+       sb_port->pms = pms;
+       return 0;
+}
+
+static void mlxsw_sp_sb_port_fini(struct mlxsw_sp_sb_port *sb_port)
+{
+       kfree(sb_port->pms);
+}
+
 static int mlxsw_sp_sb_ports_init(struct mlxsw_sp *mlxsw_sp)
 {
        unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
+       struct mlxsw_sp_sb_pr *prs;
+       int i;
+       int err;
 
        mlxsw_sp->sb->ports = kcalloc(max_ports,
                                      sizeof(struct mlxsw_sp_sb_port),
                                      GFP_KERNEL);
        if (!mlxsw_sp->sb->ports)
                return -ENOMEM;
+
+       prs = kcalloc(MLXSW_SP_SB_POOL_DESS_LEN, sizeof(*prs), GFP_KERNEL);
+       if (!prs) {
+               err = -ENOMEM;
+               goto err_alloc_prs;
+       }
+       mlxsw_sp->sb->prs = prs;
+
+       for (i = 0; i < max_ports; i++) {
+               err = mlxsw_sp_sb_port_init(mlxsw_sp, &mlxsw_sp->sb->ports[i]);
+               if (err)
+                       goto err_sb_port_init;
+       }
+
        return 0;
+
+err_sb_port_init:
+       for (i--; i >= 0; i--)
+               mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
+       kfree(mlxsw_sp->sb->prs);
+err_alloc_prs:
+       kfree(mlxsw_sp->sb->ports);
+       return err;
 }
 
 static void mlxsw_sp_sb_ports_fini(struct mlxsw_sp *mlxsw_sp)
 {
+       int max_ports = mlxsw_core_max_ports(mlxsw_sp->core);
+       int i;
+
+       for (i = max_ports - 1; i >= 0; i--)
+               mlxsw_sp_sb_port_fini(&mlxsw_sp->sb->ports[i]);
+       kfree(mlxsw_sp->sb->prs);
        kfree(mlxsw_sp->sb->ports);
 }