18fd92a66SOlivier Matz /* SPDX-License-Identifier: BSD-3-Clause 2771fa900SAdrien Mazarguil * Copyright 2015 6WIND S.A. 35feecc57SShahaf Shuler * Copyright 2015 Mellanox Technologies, Ltd 4771fa900SAdrien Mazarguil */ 5771fa900SAdrien Mazarguil 6771fa900SAdrien Mazarguil #ifndef RTE_PMD_MLX5_UTILS_H_ 7771fa900SAdrien Mazarguil #define RTE_PMD_MLX5_UTILS_H_ 8771fa900SAdrien Mazarguil 9771fa900SAdrien Mazarguil #include <stddef.h> 10b113cb5eSEdward Makarov #include <stdint.h> 11771fa900SAdrien Mazarguil #include <stdio.h> 12771fa900SAdrien Mazarguil #include <limits.h> 13771fa900SAdrien Mazarguil #include <errno.h> 14771fa900SAdrien Mazarguil 15a3cf59f5SSuanming Mou #include <rte_spinlock.h> 16e69a5922SXueming Li #include <rte_rwlock.h> 17a3cf59f5SSuanming Mou #include <rte_memory.h> 18a3cf59f5SSuanming Mou #include <rte_bitmap.h> 19a3cf59f5SSuanming Mou 207b4f1e6bSMatan Azrad #include <mlx5_common.h> 2125245d5dSShiri Kuzin #include <mlx5_common_utils.h> 227b4f1e6bSMatan Azrad 23771fa900SAdrien Mazarguil #include "mlx5_defs.h" 24771fa900SAdrien Mazarguil 25771fa900SAdrien Mazarguil /* Save and restore errno around argument evaluation. */ 26771fa900SAdrien Mazarguil #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0])) 27771fa900SAdrien Mazarguil 28a170a30dSNélio Laranjeiro extern int mlx5_logtype; 29a170a30dSNélio Laranjeiro 302d2546adSAsaf Penso #define MLX5_NET_LOG_PREFIX "mlx5_net" 312d2546adSAsaf Penso 32771fa900SAdrien Mazarguil /* Generic printf()-like logging macro with automatic line feed. */ 33a170a30dSNélio Laranjeiro #define DRV_LOG(level, ...) \ 342d2546adSAsaf Penso PMD_DRV_LOG_(level, mlx5_logtype, MLX5_NET_LOG_PREFIX, \ 35771fa900SAdrien Mazarguil __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \ 36771fa900SAdrien Mazarguil PMD_DRV_LOG_CPAREN) 37771fa900SAdrien Mazarguil 382e22920bSAdrien Mazarguil /* Convenience macros for accessing mbuf fields. */ 392e22920bSAdrien Mazarguil #define NEXT(m) ((m)->next) 402e22920bSAdrien Mazarguil #define DATA_LEN(m) ((m)->data_len) 412e22920bSAdrien Mazarguil #define PKT_LEN(m) ((m)->pkt_len) 422e22920bSAdrien Mazarguil #define DATA_OFF(m) ((m)->data_off) 432e22920bSAdrien Mazarguil #define SET_DATA_OFF(m, o) ((m)->data_off = (o)) 442e22920bSAdrien Mazarguil #define NB_SEGS(m) ((m)->nb_segs) 452e22920bSAdrien Mazarguil #define PORT(m) ((m)->port) 462e22920bSAdrien Mazarguil 4767fa62bcSAdrien Mazarguil /* Transpose flags. Useful to convert IBV to DPDK flags. */ 4867fa62bcSAdrien Mazarguil #define TRANSPOSE(val, from, to) \ 4967fa62bcSAdrien Mazarguil (((from) >= (to)) ? \ 5067fa62bcSAdrien Mazarguil (((val) & (from)) / ((from) / (to))) : \ 5167fa62bcSAdrien Mazarguil (((val) & (from)) * ((to) / (from)))) 5267fa62bcSAdrien Mazarguil 53a3cf59f5SSuanming Mou /* 54bd81eaebSSuanming Mou * For the case which data is linked with sequence increased index, the 557be78d02SJosh Soref * array table will be more efficient than hash table once need to search 56bd81eaebSSuanming Mou * one data entry in large numbers of entries. Since the traditional hash 57bd81eaebSSuanming Mou * tables has fixed table size, when huge numbers of data saved to the hash 58bd81eaebSSuanming Mou * table, it also comes lots of hash conflict. 59bd81eaebSSuanming Mou * 60bd81eaebSSuanming Mou * But simple array table also has fixed size, allocates all the needed 61bd81eaebSSuanming Mou * memory at once will waste lots of memory. For the case don't know the 62bd81eaebSSuanming Mou * exactly number of entries will be impossible to allocate the array. 63bd81eaebSSuanming Mou * 64bd81eaebSSuanming Mou * Then the multiple level table helps to balance the two disadvantages. 65bd81eaebSSuanming Mou * Allocate a global high level table with sub table entries at first, 66bd81eaebSSuanming Mou * the global table contains the sub table entries, and the sub table will 67bd81eaebSSuanming Mou * be allocated only once the corresponding index entry need to be saved. 68bd81eaebSSuanming Mou * e.g. for up to 32-bits index, three level table with 10-10-12 splitting, 69bd81eaebSSuanming Mou * with sequence increased index, the memory grows with every 4K entries. 70bd81eaebSSuanming Mou * 71bd81eaebSSuanming Mou * The currently implementation introduces 10-10-12 32-bits splitting 72bd81eaebSSuanming Mou * Three-Level table to help the cases which have millions of enties to 73bd81eaebSSuanming Mou * save. The index entries can be addressed directly by the index, no 74bd81eaebSSuanming Mou * search will be needed.q 75bd81eaebSSuanming Mou */ 76bd81eaebSSuanming Mou 77bd81eaebSSuanming Mou /* L3 table global table define. */ 78bd81eaebSSuanming Mou #define MLX5_L3T_GT_OFFSET 22 79bd81eaebSSuanming Mou #define MLX5_L3T_GT_SIZE (1 << 10) 80bd81eaebSSuanming Mou #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1) 81bd81eaebSSuanming Mou 82bd81eaebSSuanming Mou /* L3 table middle table define. */ 83bd81eaebSSuanming Mou #define MLX5_L3T_MT_OFFSET 12 84bd81eaebSSuanming Mou #define MLX5_L3T_MT_SIZE (1 << 10) 85bd81eaebSSuanming Mou #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1) 86bd81eaebSSuanming Mou 87bd81eaebSSuanming Mou /* L3 table entry table define. */ 88bd81eaebSSuanming Mou #define MLX5_L3T_ET_OFFSET 0 89bd81eaebSSuanming Mou #define MLX5_L3T_ET_SIZE (1 << 12) 90bd81eaebSSuanming Mou #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1) 91bd81eaebSSuanming Mou 92bd81eaebSSuanming Mou /* L3 table type. */ 93bd81eaebSSuanming Mou enum mlx5_l3t_type { 94bd81eaebSSuanming Mou MLX5_L3T_TYPE_WORD = 0, 95bd81eaebSSuanming Mou MLX5_L3T_TYPE_DWORD, 96bd81eaebSSuanming Mou MLX5_L3T_TYPE_QWORD, 97bd81eaebSSuanming Mou MLX5_L3T_TYPE_PTR, 98bd81eaebSSuanming Mou MLX5_L3T_TYPE_MAX, 99bd81eaebSSuanming Mou }; 100bd81eaebSSuanming Mou 101bd81eaebSSuanming Mou struct mlx5_indexed_pool; 102bd81eaebSSuanming Mou 103bd81eaebSSuanming Mou /* Generic data struct. */ 104bd81eaebSSuanming Mou union mlx5_l3t_data { 105bd81eaebSSuanming Mou uint16_t word; 106bd81eaebSSuanming Mou uint32_t dword; 107bd81eaebSSuanming Mou uint64_t qword; 108bd81eaebSSuanming Mou void *ptr; 109bd81eaebSSuanming Mou }; 110bd81eaebSSuanming Mou 111bd81eaebSSuanming Mou /* L3 level table data structure. */ 112bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl { 113bd81eaebSSuanming Mou uint64_t ref_cnt; /* Table ref_cnt. */ 114bd81eaebSSuanming Mou void *tbl[]; /* Table array. */ 115bd81eaebSSuanming Mou }; 116bd81eaebSSuanming Mou 117bd81eaebSSuanming Mou /* L3 word entry table data structure. */ 118*e7750639SAndre Muezerie struct __rte_packed_begin mlx5_l3t_entry_word { 119bd81eaebSSuanming Mou uint32_t idx; /* Table index. */ 120bd81eaebSSuanming Mou uint64_t ref_cnt; /* Table ref_cnt. */ 1210796c7b1SSuanming Mou struct { 1220796c7b1SSuanming Mou uint16_t data; 1230796c7b1SSuanming Mou uint32_t ref_cnt; 1240796c7b1SSuanming Mou } entry[MLX5_L3T_ET_SIZE]; /* Entry array */ 125*e7750639SAndre Muezerie } __rte_packed_end; 126bd81eaebSSuanming Mou 127bd81eaebSSuanming Mou /* L3 double word entry table data structure. */ 128*e7750639SAndre Muezerie struct __rte_packed_begin mlx5_l3t_entry_dword { 129bd81eaebSSuanming Mou uint32_t idx; /* Table index. */ 130bd81eaebSSuanming Mou uint64_t ref_cnt; /* Table ref_cnt. */ 1310796c7b1SSuanming Mou struct { 1320796c7b1SSuanming Mou uint32_t data; 1330796c7b1SSuanming Mou int32_t ref_cnt; 1340796c7b1SSuanming Mou } entry[MLX5_L3T_ET_SIZE]; /* Entry array */ 135*e7750639SAndre Muezerie } __rte_packed_end; 136bd81eaebSSuanming Mou 137bd81eaebSSuanming Mou /* L3 quad word entry table data structure. */ 138*e7750639SAndre Muezerie struct __rte_packed_begin mlx5_l3t_entry_qword { 139bd81eaebSSuanming Mou uint32_t idx; /* Table index. */ 140bd81eaebSSuanming Mou uint64_t ref_cnt; /* Table ref_cnt. */ 1410796c7b1SSuanming Mou struct { 1420796c7b1SSuanming Mou uint64_t data; 1430796c7b1SSuanming Mou uint32_t ref_cnt; 1440796c7b1SSuanming Mou } entry[MLX5_L3T_ET_SIZE]; /* Entry array */ 145*e7750639SAndre Muezerie } __rte_packed_end; 146bd81eaebSSuanming Mou 147bd81eaebSSuanming Mou /* L3 pointer entry table data structure. */ 148*e7750639SAndre Muezerie struct __rte_packed_begin mlx5_l3t_entry_ptr { 149bd81eaebSSuanming Mou uint32_t idx; /* Table index. */ 150bd81eaebSSuanming Mou uint64_t ref_cnt; /* Table ref_cnt. */ 1510796c7b1SSuanming Mou struct { 1520796c7b1SSuanming Mou void *data; 1530796c7b1SSuanming Mou uint32_t ref_cnt; 1540796c7b1SSuanming Mou } entry[MLX5_L3T_ET_SIZE]; /* Entry array */ 155*e7750639SAndre Muezerie } __rte_packed_end; 156bd81eaebSSuanming Mou 157bd81eaebSSuanming Mou /* L3 table data structure. */ 158bd81eaebSSuanming Mou struct mlx5_l3t_tbl { 159bd81eaebSSuanming Mou enum mlx5_l3t_type type; /* Table type. */ 160bd81eaebSSuanming Mou struct mlx5_indexed_pool *eip; 161bd81eaebSSuanming Mou /* Table index pool handles. */ 162bd81eaebSSuanming Mou struct mlx5_l3t_level_tbl *tbl; /* Global table index. */ 1630796c7b1SSuanming Mou rte_spinlock_t sl; /* The table lock. */ 164bd81eaebSSuanming Mou }; 165bd81eaebSSuanming Mou 1660796c7b1SSuanming Mou /** Type of function that is used to handle the data before freeing. */ 1670796c7b1SSuanming Mou typedef int32_t (*mlx5_l3t_alloc_callback_fn)(void *ctx, 1680796c7b1SSuanming Mou union mlx5_l3t_data *data); 1690796c7b1SSuanming Mou 170bd81eaebSSuanming Mou /* 17104a4de75SMichael Baum * The default ipool threshold value indicates which per_core_cache 17204a4de75SMichael Baum * value to set. 17304a4de75SMichael Baum */ 17404a4de75SMichael Baum #define MLX5_HW_IPOOL_SIZE_THRESHOLD (1 << 19) 17504a4de75SMichael Baum /* The default min local cache size. */ 17604a4de75SMichael Baum #define MLX5_HW_IPOOL_CACHE_MIN (1 << 9) 17704a4de75SMichael Baum 17804a4de75SMichael Baum /* 179a3cf59f5SSuanming Mou * The indexed memory entry index is made up of trunk index and offset of 180a3cf59f5SSuanming Mou * the entry in the trunk. Since the entry index is 32 bits, in case user 181a3cf59f5SSuanming Mou * prefers to have small trunks, user can change the macro below to a big 182a3cf59f5SSuanming Mou * number which helps the pool contains more trunks with lots of entries 183a3cf59f5SSuanming Mou * allocated. 184a3cf59f5SSuanming Mou */ 185a3cf59f5SSuanming Mou #define TRUNK_IDX_BITS 16 186a3cf59f5SSuanming Mou #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1) 187a3cf59f5SSuanming Mou #define TRUNK_INVALID TRUNK_MAX_IDX 188a3cf59f5SSuanming Mou #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS)) 189a3cf59f5SSuanming Mou #ifdef RTE_LIBRTE_MLX5_DEBUG 190a3cf59f5SSuanming Mou #define POOL_DEBUG 1 191a3cf59f5SSuanming Mou #endif 192a3cf59f5SSuanming Mou 193a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config { 194a3cf59f5SSuanming Mou uint32_t size; /* Pool entry size. */ 19562d7d519SSuanming Mou uint32_t trunk_size:22; 19662d7d519SSuanming Mou /* 19762d7d519SSuanming Mou * Trunk entry number. Must be power of 2. It can be increased 19862d7d519SSuanming Mou * if trunk_grow enable. The trunk entry number increases with 19962d7d519SSuanming Mou * left shift grow_shift. Trunks with index are after grow_trunk 20062d7d519SSuanming Mou * will keep the entry number same with the last grow trunk. 20162d7d519SSuanming Mou */ 20262d7d519SSuanming Mou uint32_t grow_trunk:4; 20362d7d519SSuanming Mou /* 20462d7d519SSuanming Mou * Trunks with entry number increase in the pool. Set it to 0 20562d7d519SSuanming Mou * to make the pool works as trunk entry fixed pool. It works 20662d7d519SSuanming Mou * only if grow_shift is not 0. 20762d7d519SSuanming Mou */ 20862d7d519SSuanming Mou uint32_t grow_shift:4; 20962d7d519SSuanming Mou /* 21062d7d519SSuanming Mou * Trunk entry number increase shift value, stop after grow_trunk. 21162d7d519SSuanming Mou * It works only if grow_trunk is not 0. 21262d7d519SSuanming Mou */ 21362d7d519SSuanming Mou uint32_t need_lock:1; 214a3cf59f5SSuanming Mou /* Lock is needed for multiple thread usage. */ 21504a4de75SMichael Baum uint32_t release_mem_en:1; /* Release trunk when it is free. */ 21658ecd3adSSuanming Mou uint32_t max_idx; /* The maximum index can be allocated. */ 217d15c0946SSuanming Mou uint32_t per_core_cache; 218d15c0946SSuanming Mou /* 219d15c0946SSuanming Mou * Cache entry number per core for performance. Should not be 220d15c0946SSuanming Mou * set with release_mem_en. 221d15c0946SSuanming Mou */ 222a3cf59f5SSuanming Mou const char *type; /* Memory allocate type name. */ 22383c2047cSSuanming Mou void *(*malloc)(uint32_t flags, size_t size, unsigned int align, 224a3cf59f5SSuanming Mou int socket); 225a3cf59f5SSuanming Mou /* User defined memory allocator. */ 226a3cf59f5SSuanming Mou void (*free)(void *addr); /* User defined memory release. */ 227a3cf59f5SSuanming Mou }; 228a3cf59f5SSuanming Mou 229a3cf59f5SSuanming Mou struct mlx5_indexed_trunk { 230a3cf59f5SSuanming Mou uint32_t idx; /* Trunk id. */ 231a3cf59f5SSuanming Mou uint32_t prev; /* Previous free trunk in free list. */ 232a3cf59f5SSuanming Mou uint32_t next; /* Next free trunk in free list. */ 233a3cf59f5SSuanming Mou uint32_t free; /* Free entries available */ 234a3cf59f5SSuanming Mou struct rte_bitmap *bmp; 23527595cd8STyler Retzlaff alignas(RTE_CACHE_LINE_SIZE) uint8_t data[]; /* Entry data start. */ 236a3cf59f5SSuanming Mou }; 237a3cf59f5SSuanming Mou 238d15c0946SSuanming Mou struct mlx5_indexed_cache { 239d15c0946SSuanming Mou struct mlx5_indexed_trunk **trunks; 240e12a0166STyler Retzlaff volatile RTE_ATOMIC(uint32_t) n_trunk_valid; /* Trunks allocated. */ 241d15c0946SSuanming Mou uint32_t n_trunk; /* Trunk pointer array size. */ 242d15c0946SSuanming Mou uint32_t ref_cnt; 243d15c0946SSuanming Mou uint32_t len; 244d15c0946SSuanming Mou uint32_t idx[]; 245d15c0946SSuanming Mou }; 246d15c0946SSuanming Mou 247d15c0946SSuanming Mou struct mlx5_ipool_per_lcore { 248d15c0946SSuanming Mou struct mlx5_indexed_cache *lc; 249d15c0946SSuanming Mou uint32_t len; /**< Current cache count. */ 250d15c0946SSuanming Mou uint32_t idx[]; /**< Cache objects. */ 251d15c0946SSuanming Mou }; 252d15c0946SSuanming Mou 253a3cf59f5SSuanming Mou struct mlx5_indexed_pool { 254a3cf59f5SSuanming Mou struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */ 255d15c0946SSuanming Mou rte_spinlock_t rsz_lock; /* Pool lock for multiple thread usage. */ 25642f46339SSuanming Mou rte_spinlock_t lcore_lock; 257d15c0946SSuanming Mou /* Dim of trunk pointer array. */ 258d15c0946SSuanming Mou union { 259d15c0946SSuanming Mou struct { 260a3cf59f5SSuanming Mou uint32_t n_trunk_valid; /* Trunks allocated. */ 261a3cf59f5SSuanming Mou uint32_t n_trunk; /* Trunk pointer array size. */ 262a3cf59f5SSuanming Mou struct mlx5_indexed_trunk **trunks; 263a3cf59f5SSuanming Mou uint32_t free_list; /* Index to first free trunk. */ 264d15c0946SSuanming Mou }; 265d15c0946SSuanming Mou struct { 266e12a0166STyler Retzlaff RTE_ATOMIC(struct mlx5_indexed_cache *) gc; 267d15c0946SSuanming Mou /* Global cache. */ 26842f46339SSuanming Mou struct mlx5_ipool_per_lcore *cache[RTE_MAX_LCORE + 1]; 269d15c0946SSuanming Mou /* Local cache. */ 27064a80f1cSSuanming Mou struct rte_bitmap *ibmp; 27164a80f1cSSuanming Mou void *bmp_mem; 27264a80f1cSSuanming Mou /* Allocate objects bitmap. Use during flush. */ 273d15c0946SSuanming Mou }; 274d15c0946SSuanming Mou }; 275a3cf59f5SSuanming Mou #ifdef POOL_DEBUG 276a3cf59f5SSuanming Mou uint32_t n_entry; 277a3cf59f5SSuanming Mou uint32_t trunk_new; 278a3cf59f5SSuanming Mou uint32_t trunk_avail; 279a3cf59f5SSuanming Mou uint32_t trunk_empty; 280a3cf59f5SSuanming Mou uint32_t trunk_free; 281a3cf59f5SSuanming Mou #endif 28262d7d519SSuanming Mou uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */ 283a3cf59f5SSuanming Mou }; 284a3cf59f5SSuanming Mou 285634efbc2SNelio Laranjeiro /** 28646287eacSBing Zhao * Return logarithm of the nearest power of two above input value. 287634efbc2SNelio Laranjeiro * 288634efbc2SNelio Laranjeiro * @param v 289634efbc2SNelio Laranjeiro * Input value. 290634efbc2SNelio Laranjeiro * 291634efbc2SNelio Laranjeiro * @return 29246287eacSBing Zhao * Logarithm of the nearest power of two above input value. 293634efbc2SNelio Laranjeiro */ 294634efbc2SNelio Laranjeiro static inline unsigned int 295634efbc2SNelio Laranjeiro log2above(unsigned int v) 296634efbc2SNelio Laranjeiro { 297634efbc2SNelio Laranjeiro unsigned int l; 298634efbc2SNelio Laranjeiro unsigned int r; 299634efbc2SNelio Laranjeiro 300634efbc2SNelio Laranjeiro for (l = 0, r = 0; (v >> 1); ++l, v >>= 1) 301634efbc2SNelio Laranjeiro r |= (v & 1); 302693f715dSHuawei Xie return l + r; 303634efbc2SNelio Laranjeiro } 304634efbc2SNelio Laranjeiro 3051ff37beeSXueming Li /********************************* indexed pool *************************/ 3061ff37beeSXueming Li 307a3cf59f5SSuanming Mou /** 308a3cf59f5SSuanming Mou * This function allocates non-initialized memory entry from pool. 309a3cf59f5SSuanming Mou * In NUMA systems, the memory entry allocated resides on the same 310a3cf59f5SSuanming Mou * NUMA socket as the core that calls this function. 311a3cf59f5SSuanming Mou * 312a3cf59f5SSuanming Mou * Memory entry is allocated from memory trunk, no alignment. 313a3cf59f5SSuanming Mou * 314a3cf59f5SSuanming Mou * @param pool 315a3cf59f5SSuanming Mou * Pointer to indexed memory entry pool. 316a3cf59f5SSuanming Mou * No initialization required. 317a3cf59f5SSuanming Mou * @param[out] idx 318a3cf59f5SSuanming Mou * Pointer to memory to save allocated index. 319a3cf59f5SSuanming Mou * Memory index always positive value. 320a3cf59f5SSuanming Mou * @return 321a3cf59f5SSuanming Mou * - Pointer to the allocated memory entry. 322a3cf59f5SSuanming Mou * - NULL on error. Not enough memory, or invalid arguments. 323a3cf59f5SSuanming Mou */ 324a3cf59f5SSuanming Mou void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx); 325a3cf59f5SSuanming Mou 326a3cf59f5SSuanming Mou /** 327a3cf59f5SSuanming Mou * This function allocates zero initialized memory entry from pool. 328a3cf59f5SSuanming Mou * In NUMA systems, the memory entry allocated resides on the same 329a3cf59f5SSuanming Mou * NUMA socket as the core that calls this function. 330a3cf59f5SSuanming Mou * 331a3cf59f5SSuanming Mou * Memory entry is allocated from memory trunk, no alignment. 332a3cf59f5SSuanming Mou * 333a3cf59f5SSuanming Mou * @param pool 334a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 335a3cf59f5SSuanming Mou * No initialization required. 336a3cf59f5SSuanming Mou * @param[out] idx 337a3cf59f5SSuanming Mou * Pointer to memory to save allocated index. 338a3cf59f5SSuanming Mou * Memory index always positive value. 339a3cf59f5SSuanming Mou * @return 340a3cf59f5SSuanming Mou * - Pointer to the allocated memory entry . 341a3cf59f5SSuanming Mou * - NULL on error. Not enough memory, or invalid arguments. 342a3cf59f5SSuanming Mou */ 343a3cf59f5SSuanming Mou void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx); 344a3cf59f5SSuanming Mou 345a3cf59f5SSuanming Mou /** 346a3cf59f5SSuanming Mou * This function frees indexed memory entry to pool. 347a3cf59f5SSuanming Mou * Caller has to make sure that the index is allocated from same pool. 348a3cf59f5SSuanming Mou * 349a3cf59f5SSuanming Mou * @param pool 350a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 351a3cf59f5SSuanming Mou * @param idx 352a3cf59f5SSuanming Mou * Allocated memory entry index. 353a3cf59f5SSuanming Mou */ 354a3cf59f5SSuanming Mou void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx); 355a3cf59f5SSuanming Mou 356a3cf59f5SSuanming Mou /** 357a3cf59f5SSuanming Mou * This function returns pointer of indexed memory entry from index. 358a3cf59f5SSuanming Mou * Caller has to make sure that the index is valid, and allocated 359a3cf59f5SSuanming Mou * from same pool. 360a3cf59f5SSuanming Mou * 361a3cf59f5SSuanming Mou * @param pool 362a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 363a3cf59f5SSuanming Mou * @param idx 364a3cf59f5SSuanming Mou * Allocated memory index. 365a3cf59f5SSuanming Mou * @return 366a3cf59f5SSuanming Mou * - Pointer to indexed memory entry. 367a3cf59f5SSuanming Mou */ 368a3cf59f5SSuanming Mou void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx); 369a3cf59f5SSuanming Mou 370a3cf59f5SSuanming Mou /** 371a3cf59f5SSuanming Mou * This function creates indexed memory pool. 372a3cf59f5SSuanming Mou * Caller has to configure the configuration accordingly. 373a3cf59f5SSuanming Mou * 374a3cf59f5SSuanming Mou * @param pool 375a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 376a3cf59f5SSuanming Mou * @param cfg 377a3cf59f5SSuanming Mou * Allocated memory index. 378a3cf59f5SSuanming Mou */ 379a3cf59f5SSuanming Mou struct mlx5_indexed_pool * 380a3cf59f5SSuanming Mou mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg); 381a3cf59f5SSuanming Mou 382a3cf59f5SSuanming Mou /** 383a3cf59f5SSuanming Mou * This function releases all resources of pool. 384a3cf59f5SSuanming Mou * Caller has to make sure that all indexes and memories allocated 385a3cf59f5SSuanming Mou * from this pool not referenced anymore. 386a3cf59f5SSuanming Mou * 387a3cf59f5SSuanming Mou * @param pool 388a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 389a3cf59f5SSuanming Mou * @return 390a3cf59f5SSuanming Mou * - non-zero value on error. 391a3cf59f5SSuanming Mou * - 0 on success. 392a3cf59f5SSuanming Mou */ 393a3cf59f5SSuanming Mou int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool); 394a3cf59f5SSuanming Mou 395a3cf59f5SSuanming Mou /** 396a3cf59f5SSuanming Mou * This function dumps debug info of pool. 397a3cf59f5SSuanming Mou * 398a3cf59f5SSuanming Mou * @param pool 399a3cf59f5SSuanming Mou * Pointer to indexed memory pool. 400a3cf59f5SSuanming Mou */ 401a3cf59f5SSuanming Mou void mlx5_ipool_dump(struct mlx5_indexed_pool *pool); 402a3cf59f5SSuanming Mou 403bd81eaebSSuanming Mou /** 404d15c0946SSuanming Mou * This function flushes all the cache index back to pool trunk. 405d15c0946SSuanming Mou * 406d15c0946SSuanming Mou * @param pool 407d15c0946SSuanming Mou * Pointer to the index memory pool handler. 408d15c0946SSuanming Mou * 409d15c0946SSuanming Mou */ 410d15c0946SSuanming Mou 411d15c0946SSuanming Mou void mlx5_ipool_flush_cache(struct mlx5_indexed_pool *pool); 412d15c0946SSuanming Mou 413d15c0946SSuanming Mou /** 414d15c0946SSuanming Mou * This function gets the available entry from pos. 415d15c0946SSuanming Mou * 416d15c0946SSuanming Mou * @param pool 417d15c0946SSuanming Mou * Pointer to the index memory pool handler. 418d15c0946SSuanming Mou * @param pos 419d15c0946SSuanming Mou * Pointer to the index position start from. 420d15c0946SSuanming Mou * 421d15c0946SSuanming Mou * @return 422d15c0946SSuanming Mou * - Pointer to the next available entry. 423d15c0946SSuanming Mou * 424d15c0946SSuanming Mou */ 425d15c0946SSuanming Mou void *mlx5_ipool_get_next(struct mlx5_indexed_pool *pool, uint32_t *pos); 426d15c0946SSuanming Mou 427d15c0946SSuanming Mou /** 42889578504SMaayan Kashani * This function resize the ipool. 42989578504SMaayan Kashani * 43089578504SMaayan Kashani * @param pool 43189578504SMaayan Kashani * Pointer to the index memory pool handler. 43289578504SMaayan Kashani * @param num_entries 43389578504SMaayan Kashani * Number of entries to be added to the pool. 43489578504SMaayan Kashani * This number should be divisible by trunk_size. 43589578504SMaayan Kashani * 43689578504SMaayan Kashani * @return 43789578504SMaayan Kashani * - non-zero value on error. 43889578504SMaayan Kashani * - 0 on success. 43989578504SMaayan Kashani * 44089578504SMaayan Kashani */ 441d54e82e1SGregory Etelson int mlx5_ipool_resize(struct mlx5_indexed_pool *pool, uint32_t num_entries, 442d54e82e1SGregory Etelson struct rte_flow_error *error); 44389578504SMaayan Kashani 44489578504SMaayan Kashani /** 445bd81eaebSSuanming Mou * This function allocates new empty Three-level table. 446bd81eaebSSuanming Mou * 447bd81eaebSSuanming Mou * @param type 448bd81eaebSSuanming Mou * The l3t can set as word, double word, quad word or pointer with index. 449bd81eaebSSuanming Mou * 450bd81eaebSSuanming Mou * @return 451bd81eaebSSuanming Mou * - Pointer to the allocated l3t. 452bd81eaebSSuanming Mou * - NULL on error. Not enough memory, or invalid arguments. 453bd81eaebSSuanming Mou */ 454bd81eaebSSuanming Mou struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type); 455bd81eaebSSuanming Mou 456bd81eaebSSuanming Mou /** 457bd81eaebSSuanming Mou * This function destroys Three-level table. 458bd81eaebSSuanming Mou * 459bd81eaebSSuanming Mou * @param tbl 460bd81eaebSSuanming Mou * Pointer to the l3t. 461bd81eaebSSuanming Mou */ 462bd81eaebSSuanming Mou void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl); 463bd81eaebSSuanming Mou 464bd81eaebSSuanming Mou /** 465bd81eaebSSuanming Mou * This function gets the index entry from Three-level table. 466bd81eaebSSuanming Mou * 467bd81eaebSSuanming Mou * @param tbl 468bd81eaebSSuanming Mou * Pointer to the l3t. 469bd81eaebSSuanming Mou * @param idx 470bd81eaebSSuanming Mou * Index to the entry. 471bd81eaebSSuanming Mou * @param data 472bd81eaebSSuanming Mou * Pointer to the memory which saves the entry data. 473bd81eaebSSuanming Mou * When function call returns 0, data contains the entry data get from 474bd81eaebSSuanming Mou * l3t. 475bd81eaebSSuanming Mou * When function call returns -1, data is not modified. 476bd81eaebSSuanming Mou * 477bd81eaebSSuanming Mou * @return 478bd81eaebSSuanming Mou * 0 if success, -1 on error. 479bd81eaebSSuanming Mou */ 480bd81eaebSSuanming Mou 4810796c7b1SSuanming Mou int32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx, 482bd81eaebSSuanming Mou union mlx5_l3t_data *data); 483bd81eaebSSuanming Mou 484bd81eaebSSuanming Mou /** 4850796c7b1SSuanming Mou * This function decreases and clear index entry if reference 4860796c7b1SSuanming Mou * counter is 0 from Three-level table. 4870796c7b1SSuanming Mou * 4880796c7b1SSuanming Mou * @param tbl 4890796c7b1SSuanming Mou * Pointer to the l3t. 4900796c7b1SSuanming Mou * @param idx 4910796c7b1SSuanming Mou * Index to the entry. 4920796c7b1SSuanming Mou * 4930796c7b1SSuanming Mou * @return 4940796c7b1SSuanming Mou * The remaining reference count, 0 means entry be cleared, -1 on error. 4950796c7b1SSuanming Mou */ 4960796c7b1SSuanming Mou int32_t mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx); 4970796c7b1SSuanming Mou 4980796c7b1SSuanming Mou /** 4990796c7b1SSuanming Mou * This function sets the index entry to Three-level table. 5000796c7b1SSuanming Mou * If the entry is already set, the EEXIST errno will be given, and 5010796c7b1SSuanming Mou * the set data will be filled to the data. 5020796c7b1SSuanming Mou * 5030796c7b1SSuanming Mou * @param tbl[in] 5040796c7b1SSuanming Mou * Pointer to the l3t. 5050796c7b1SSuanming Mou * @param idx[in] 5060796c7b1SSuanming Mou * Index to the entry. 5070796c7b1SSuanming Mou * @param data[in/out] 5080796c7b1SSuanming Mou * Pointer to the memory which contains the entry data save to l3t. 5090796c7b1SSuanming Mou * If the entry is already set, the set data will be filled. 5100796c7b1SSuanming Mou * 5110796c7b1SSuanming Mou * @return 5120796c7b1SSuanming Mou * 0 if success, -1 on error. 5130796c7b1SSuanming Mou */ 5140796c7b1SSuanming Mou int32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx, 515bd81eaebSSuanming Mou union mlx5_l3t_data *data); 516bd81eaebSSuanming Mou 517c123b821SSuanming Mou static inline void * 518c123b821SSuanming Mou mlx5_l3t_get_next(struct mlx5_l3t_tbl *tbl, uint32_t *pos) 519c123b821SSuanming Mou { 520c123b821SSuanming Mou struct mlx5_l3t_level_tbl *g_tbl, *m_tbl; 521c123b821SSuanming Mou uint32_t i, j, k, g_start, m_start, e_start; 522c123b821SSuanming Mou uint32_t idx = *pos; 523c123b821SSuanming Mou void *e_tbl; 524c123b821SSuanming Mou struct mlx5_l3t_entry_word *w_e_tbl; 525c123b821SSuanming Mou struct mlx5_l3t_entry_dword *dw_e_tbl; 526c123b821SSuanming Mou struct mlx5_l3t_entry_qword *qw_e_tbl; 527c123b821SSuanming Mou struct mlx5_l3t_entry_ptr *ptr_e_tbl; 528c123b821SSuanming Mou 529c123b821SSuanming Mou if (!tbl) 530c123b821SSuanming Mou return NULL; 531c123b821SSuanming Mou g_tbl = tbl->tbl; 532c123b821SSuanming Mou if (!g_tbl) 533c123b821SSuanming Mou return NULL; 534c123b821SSuanming Mou g_start = (idx >> MLX5_L3T_GT_OFFSET) & MLX5_L3T_GT_MASK; 535c123b821SSuanming Mou m_start = (idx >> MLX5_L3T_MT_OFFSET) & MLX5_L3T_MT_MASK; 536c123b821SSuanming Mou e_start = idx & MLX5_L3T_ET_MASK; 537c123b821SSuanming Mou for (i = g_start; i < MLX5_L3T_GT_SIZE; i++) { 538c123b821SSuanming Mou m_tbl = g_tbl->tbl[i]; 539c123b821SSuanming Mou if (!m_tbl) { 540c123b821SSuanming Mou /* Jump to new table, reset the sub table start. */ 541c123b821SSuanming Mou m_start = 0; 542c123b821SSuanming Mou e_start = 0; 543c123b821SSuanming Mou continue; 544c123b821SSuanming Mou } 545c123b821SSuanming Mou for (j = m_start; j < MLX5_L3T_MT_SIZE; j++) { 546c123b821SSuanming Mou if (!m_tbl->tbl[j]) { 547c123b821SSuanming Mou /* 548c123b821SSuanming Mou * Jump to new table, reset the sub table 549c123b821SSuanming Mou * start. 550c123b821SSuanming Mou */ 551c123b821SSuanming Mou e_start = 0; 552c123b821SSuanming Mou continue; 553c123b821SSuanming Mou } 554c123b821SSuanming Mou e_tbl = m_tbl->tbl[j]; 555c123b821SSuanming Mou switch (tbl->type) { 556c123b821SSuanming Mou case MLX5_L3T_TYPE_WORD: 557c123b821SSuanming Mou w_e_tbl = (struct mlx5_l3t_entry_word *)e_tbl; 558c123b821SSuanming Mou for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) { 559c123b821SSuanming Mou if (!w_e_tbl->entry[k].data) 560c123b821SSuanming Mou continue; 561c123b821SSuanming Mou *pos = (i << MLX5_L3T_GT_OFFSET) | 562c123b821SSuanming Mou (j << MLX5_L3T_MT_OFFSET) | k; 563c123b821SSuanming Mou return (void *)&w_e_tbl->entry[k].data; 564c123b821SSuanming Mou } 565c123b821SSuanming Mou break; 566c123b821SSuanming Mou case MLX5_L3T_TYPE_DWORD: 567c123b821SSuanming Mou dw_e_tbl = (struct mlx5_l3t_entry_dword *)e_tbl; 568c123b821SSuanming Mou for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) { 569c123b821SSuanming Mou if (!dw_e_tbl->entry[k].data) 570c123b821SSuanming Mou continue; 571c123b821SSuanming Mou *pos = (i << MLX5_L3T_GT_OFFSET) | 572c123b821SSuanming Mou (j << MLX5_L3T_MT_OFFSET) | k; 573c123b821SSuanming Mou return (void *)&dw_e_tbl->entry[k].data; 574c123b821SSuanming Mou } 575c123b821SSuanming Mou break; 576c123b821SSuanming Mou case MLX5_L3T_TYPE_QWORD: 577c123b821SSuanming Mou qw_e_tbl = (struct mlx5_l3t_entry_qword *)e_tbl; 578c123b821SSuanming Mou for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) { 579c123b821SSuanming Mou if (!qw_e_tbl->entry[k].data) 580c123b821SSuanming Mou continue; 581c123b821SSuanming Mou *pos = (i << MLX5_L3T_GT_OFFSET) | 582c123b821SSuanming Mou (j << MLX5_L3T_MT_OFFSET) | k; 583c123b821SSuanming Mou return (void *)&qw_e_tbl->entry[k].data; 584c123b821SSuanming Mou } 585c123b821SSuanming Mou break; 586c123b821SSuanming Mou default: 587c123b821SSuanming Mou ptr_e_tbl = (struct mlx5_l3t_entry_ptr *)e_tbl; 588c123b821SSuanming Mou for (k = e_start; k < MLX5_L3T_ET_SIZE; k++) { 589c123b821SSuanming Mou if (!ptr_e_tbl->entry[k].data) 590c123b821SSuanming Mou continue; 591c123b821SSuanming Mou *pos = (i << MLX5_L3T_GT_OFFSET) | 592c123b821SSuanming Mou (j << MLX5_L3T_MT_OFFSET) | k; 593c123b821SSuanming Mou return ptr_e_tbl->entry[k].data; 594c123b821SSuanming Mou } 595c123b821SSuanming Mou break; 596c123b821SSuanming Mou } 597c123b821SSuanming Mou } 598c123b821SSuanming Mou } 599c123b821SSuanming Mou return NULL; 600c123b821SSuanming Mou } 601c123b821SSuanming Mou 602a3cf59f5SSuanming Mou /* 603a3cf59f5SSuanming Mou * Macros for linked list based on indexed memory. 604a3cf59f5SSuanming Mou * Example data structure: 605a3cf59f5SSuanming Mou * struct Foo { 606a3cf59f5SSuanming Mou * ILIST_ENTRY(uint16_t) next; 607a3cf59f5SSuanming Mou * ... 608a3cf59f5SSuanming Mou * } 609a3cf59f5SSuanming Mou * 610a3cf59f5SSuanming Mou */ 611a3cf59f5SSuanming Mou #define ILIST_ENTRY(type) \ 612a3cf59f5SSuanming Mou struct { \ 613a3cf59f5SSuanming Mou type prev; /* Index of previous element. */ \ 614a3cf59f5SSuanming Mou type next; /* Index of next element. */ \ 615a3cf59f5SSuanming Mou } 616a3cf59f5SSuanming Mou 617a3cf59f5SSuanming Mou #define ILIST_INSERT(pool, head, idx, elem, field) \ 618a3cf59f5SSuanming Mou do { \ 619a3cf59f5SSuanming Mou typeof(elem) peer; \ 620a3cf59f5SSuanming Mou MLX5_ASSERT((elem) && (idx)); \ 621a3cf59f5SSuanming Mou (elem)->field.next = *(head); \ 622a3cf59f5SSuanming Mou (elem)->field.prev = 0; \ 623a3cf59f5SSuanming Mou if (*(head)) { \ 624a3cf59f5SSuanming Mou (peer) = mlx5_ipool_get(pool, *(head)); \ 625a3cf59f5SSuanming Mou if (peer) \ 626a3cf59f5SSuanming Mou (peer)->field.prev = (idx); \ 627a3cf59f5SSuanming Mou } \ 628a3cf59f5SSuanming Mou *(head) = (idx); \ 629a3cf59f5SSuanming Mou } while (0) 630a3cf59f5SSuanming Mou 631a3cf59f5SSuanming Mou #define ILIST_REMOVE(pool, head, idx, elem, field) \ 632a3cf59f5SSuanming Mou do { \ 633a3cf59f5SSuanming Mou typeof(elem) peer; \ 634a3cf59f5SSuanming Mou MLX5_ASSERT(elem); \ 635a3cf59f5SSuanming Mou MLX5_ASSERT(head); \ 636a3cf59f5SSuanming Mou if ((elem)->field.prev) { \ 637a3cf59f5SSuanming Mou (peer) = mlx5_ipool_get \ 638a3cf59f5SSuanming Mou (pool, (elem)->field.prev); \ 639a3cf59f5SSuanming Mou if (peer) \ 640a3cf59f5SSuanming Mou (peer)->field.next = (elem)->field.next;\ 641a3cf59f5SSuanming Mou } \ 642a3cf59f5SSuanming Mou if ((elem)->field.next) { \ 643a3cf59f5SSuanming Mou (peer) = mlx5_ipool_get \ 644a3cf59f5SSuanming Mou (pool, (elem)->field.next); \ 645a3cf59f5SSuanming Mou if (peer) \ 646a3cf59f5SSuanming Mou (peer)->field.prev = (elem)->field.prev;\ 647a3cf59f5SSuanming Mou } \ 648a3cf59f5SSuanming Mou if (*(head) == (idx)) \ 649a3cf59f5SSuanming Mou *(head) = (elem)->field.next; \ 650a3cf59f5SSuanming Mou } while (0) 651a3cf59f5SSuanming Mou 652a3cf59f5SSuanming Mou #define ILIST_FOREACH(pool, head, idx, elem, field) \ 653a3cf59f5SSuanming Mou for ((idx) = (head), (elem) = \ 654a3cf59f5SSuanming Mou (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \ 655a3cf59f5SSuanming Mou idx = (elem)->field.next, (elem) = \ 656a3cf59f5SSuanming Mou (idx) ? mlx5_ipool_get(pool, idx) : NULL) 657a3cf59f5SSuanming Mou 658a3cf59f5SSuanming Mou /* Single index list. */ 659a3cf59f5SSuanming Mou #define SILIST_ENTRY(type) \ 660a3cf59f5SSuanming Mou struct { \ 661a3cf59f5SSuanming Mou type next; /* Index of next element. */ \ 662a3cf59f5SSuanming Mou } 663a3cf59f5SSuanming Mou 664a3cf59f5SSuanming Mou #define SILIST_INSERT(head, idx, elem, field) \ 665a3cf59f5SSuanming Mou do { \ 666a3cf59f5SSuanming Mou MLX5_ASSERT((elem) && (idx)); \ 667a3cf59f5SSuanming Mou (elem)->field.next = *(head); \ 668a3cf59f5SSuanming Mou *(head) = (idx); \ 669a3cf59f5SSuanming Mou } while (0) 670a3cf59f5SSuanming Mou 671a3cf59f5SSuanming Mou #define SILIST_FOREACH(pool, head, idx, elem, field) \ 672a3cf59f5SSuanming Mou for ((idx) = (head), (elem) = \ 673a3cf59f5SSuanming Mou (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \ 674a3cf59f5SSuanming Mou idx = (elem)->field.next, (elem) = \ 675a3cf59f5SSuanming Mou (idx) ? mlx5_ipool_get(pool, idx) : NULL) 676a3cf59f5SSuanming Mou 677c123b821SSuanming Mou #define MLX5_L3T_FOREACH(tbl, idx, entry) \ 678c123b821SSuanming Mou for (idx = 0, (entry) = mlx5_l3t_get_next((tbl), &idx); \ 679c123b821SSuanming Mou (entry); \ 680c123b821SSuanming Mou idx++, (entry) = mlx5_l3t_get_next((tbl), &idx)) 681c123b821SSuanming Mou 68264a80f1cSSuanming Mou #define MLX5_IPOOL_FOREACH(ipool, idx, entry) \ 68364a80f1cSSuanming Mou for ((idx) = 0, mlx5_ipool_flush_cache((ipool)), \ 68464a80f1cSSuanming Mou (entry) = mlx5_ipool_get_next((ipool), &idx); \ 68564a80f1cSSuanming Mou (entry); idx++, (entry) = mlx5_ipool_get_next((ipool), &idx)) 68664a80f1cSSuanming Mou 687771fa900SAdrien Mazarguil #endif /* RTE_PMD_MLX5_UTILS_H_ */ 688