[1/4] net/mlx5: add index allocate with up limit

Message ID 20210527093403.1153127-2-suanmingm@nvidia.com (mailing list archive)
State Superseded, archived
Delegated to: Raslan Darawsheh
Headers
Series net/mlx5: add indexed pool local cache |

Checks

Context Check Description
ci/checkpatch success coding style OK

Commit Message

Suanming Mou May 27, 2021, 9:34 a.m. UTC
  The index pool can be used as ID allocator. In the ID allocator case,
to support the maximum ID can be allocated is very useful since some
registers only want limited bits of ID.

This patch add the maximum ID configurable for the index pool.

Signed-off-by: Suanming Mou <suanmingm@nvidia.com>
---
 drivers/net/mlx5/mlx5_utils.c | 14 ++++++++++++--
 drivers/net/mlx5/mlx5_utils.h |  1 +
 2 files changed, 13 insertions(+), 2 deletions(-)
  

Patch

diff --git a/drivers/net/mlx5/mlx5_utils.c b/drivers/net/mlx5/mlx5_utils.c
index 18fe23e4fb..bf2b2ebc72 100644
--- a/drivers/net/mlx5/mlx5_utils.c
+++ b/drivers/net/mlx5/mlx5_utils.c
@@ -270,6 +270,9 @@  mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg)
 		if (i > 0)
 			pool->grow_tbl[i] += pool->grow_tbl[i - 1];
 	}
+	if (!pool->cfg.max_idx)
+		pool->cfg.max_idx =
+			mlx5_trunk_idx_offset_get(pool, TRUNK_MAX_IDX + 1);
 	return pool;
 }
 
@@ -282,9 +285,11 @@  mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
 	size_t trunk_size = 0;
 	size_t data_size;
 	size_t bmp_size;
-	uint32_t idx;
+	uint32_t idx, cur_max_idx, i;
 
-	if (pool->n_trunk_valid == TRUNK_MAX_IDX)
+	cur_max_idx = mlx5_trunk_idx_offset_get(pool, pool->n_trunk_valid);
+	if (pool->n_trunk_valid == TRUNK_MAX_IDX ||
+	    cur_max_idx >= pool->cfg.max_idx)
 		return -ENOMEM;
 	if (pool->n_trunk_valid == pool->n_trunk) {
 		/* No free trunk flags, expand trunk list. */
@@ -336,6 +341,11 @@  mlx5_ipool_grow(struct mlx5_indexed_pool *pool)
 	trunk->bmp = rte_bitmap_init_with_all_set(data_size, &trunk->data
 		     [RTE_CACHE_LINE_ROUNDUP(data_size * pool->cfg.size)],
 		     bmp_size);
+	/* Clear the overhead bits in the trunk if it happens. */
+	if (cur_max_idx + data_size > pool->cfg.max_idx) {
+		for (i = pool->cfg.max_idx - cur_max_idx; i < data_size; i++)
+			rte_bitmap_clear(trunk->bmp, i);
+	}
 	MLX5_ASSERT(trunk->bmp);
 	pool->n_trunk_valid++;
 #ifdef POOL_DEBUG
diff --git a/drivers/net/mlx5/mlx5_utils.h b/drivers/net/mlx5/mlx5_utils.h
index b54517c6df..15870e14c2 100644
--- a/drivers/net/mlx5/mlx5_utils.h
+++ b/drivers/net/mlx5/mlx5_utils.h
@@ -208,6 +208,7 @@  struct mlx5_indexed_pool_config {
 	uint32_t need_lock:1;
 	/* Lock is needed for multiple thread usage. */
 	uint32_t release_mem_en:1; /* Rlease trunk when it is free. */
+	uint32_t max_idx; /* The maximum index can be allocated. */
 	const char *type; /* Memory allocate type name. */
 	void *(*malloc)(uint32_t flags, size_t size, unsigned int align,
 			int socket);