1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2015 6WIND S.A. 3 * Copyright 2015 Mellanox Technologies, Ltd 4 */ 5 6 #ifndef RTE_PMD_MLX5_UTILS_H_ 7 #define RTE_PMD_MLX5_UTILS_H_ 8 9 #include <stddef.h> 10 #include <stdint.h> 11 #include <stdio.h> 12 #include <limits.h> 13 #include <errno.h> 14 15 #include <rte_spinlock.h> 16 #include <rte_memory.h> 17 #include <rte_bitmap.h> 18 19 #include <mlx5_common.h> 20 21 #include "mlx5_defs.h" 22 23 24 /* Convert a bit number to the corresponding 64-bit mask */ 25 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v)) 26 27 /* Save and restore errno around argument evaluation. */ 28 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0])) 29 30 extern int mlx5_logtype; 31 32 /* Generic printf()-like logging macro with automatic line feed. */ 33 #define DRV_LOG(level, ...) \ 34 PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \ 35 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \ 36 PMD_DRV_LOG_CPAREN) 37 38 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__) 39 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__) 40 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__) 41 42 /* Convenience macros for accessing mbuf fields. */ 43 #define NEXT(m) ((m)->next) 44 #define DATA_LEN(m) ((m)->data_len) 45 #define PKT_LEN(m) ((m)->pkt_len) 46 #define DATA_OFF(m) ((m)->data_off) 47 #define SET_DATA_OFF(m, o) ((m)->data_off = (o)) 48 #define NB_SEGS(m) ((m)->nb_segs) 49 #define PORT(m) ((m)->port) 50 51 /* Transpose flags. Useful to convert IBV to DPDK flags. */ 52 #define TRANSPOSE(val, from, to) \ 53 (((from) >= (to)) ? \ 54 (((val) & (from)) / ((from) / (to))) : \ 55 (((val) & (from)) * ((to) / (from)))) 56 57 /* 58 * For the case which data is linked with sequence increased index, the 59 * array table will be more efficiect than hash table once need to serarch 60 * one data entry in large numbers of entries. Since the traditional hash 61 * tables has fixed table size, when huge numbers of data saved to the hash 62 * table, it also comes lots of hash conflict. 63 * 64 * But simple array table also has fixed size, allocates all the needed 65 * memory at once will waste lots of memory. For the case don't know the 66 * exactly number of entries will be impossible to allocate the array. 67 * 68 * Then the multiple level table helps to balance the two disadvantages. 69 * Allocate a global high level table with sub table entries at first, 70 * the global table contains the sub table entries, and the sub table will 71 * be allocated only once the corresponding index entry need to be saved. 72 * e.g. for up to 32-bits index, three level table with 10-10-12 splitting, 73 * with sequence increased index, the memory grows with every 4K entries. 74 * 75 * The currently implementation introduces 10-10-12 32-bits splitting 76 * Three-Level table to help the cases which have millions of enties to 77 * save. The index entries can be addressed directly by the index, no 78 * search will be needed.q 79 */ 80 81 /* L3 table global table define. */ 82 #define MLX5_L3T_GT_OFFSET 22 83 #define MLX5_L3T_GT_SIZE (1 << 10) 84 #define MLX5_L3T_GT_MASK (MLX5_L3T_GT_SIZE - 1) 85 86 /* L3 table middle table define. */ 87 #define MLX5_L3T_MT_OFFSET 12 88 #define MLX5_L3T_MT_SIZE (1 << 10) 89 #define MLX5_L3T_MT_MASK (MLX5_L3T_MT_SIZE - 1) 90 91 /* L3 table entry table define. */ 92 #define MLX5_L3T_ET_OFFSET 0 93 #define MLX5_L3T_ET_SIZE (1 << 12) 94 #define MLX5_L3T_ET_MASK (MLX5_L3T_ET_SIZE - 1) 95 96 /* L3 table type. */ 97 enum mlx5_l3t_type { 98 MLX5_L3T_TYPE_WORD = 0, 99 MLX5_L3T_TYPE_DWORD, 100 MLX5_L3T_TYPE_QWORD, 101 MLX5_L3T_TYPE_PTR, 102 MLX5_L3T_TYPE_MAX, 103 }; 104 105 struct mlx5_indexed_pool; 106 107 /* Generic data struct. */ 108 union mlx5_l3t_data { 109 uint16_t word; 110 uint32_t dword; 111 uint64_t qword; 112 void *ptr; 113 }; 114 115 /* L3 level table data structure. */ 116 struct mlx5_l3t_level_tbl { 117 uint64_t ref_cnt; /* Table ref_cnt. */ 118 void *tbl[]; /* Table array. */ 119 }; 120 121 /* L3 word entry table data structure. */ 122 struct mlx5_l3t_entry_word { 123 uint32_t idx; /* Table index. */ 124 uint64_t ref_cnt; /* Table ref_cnt. */ 125 uint16_t entry[]; /* Entry array. */ 126 }; 127 128 /* L3 double word entry table data structure. */ 129 struct mlx5_l3t_entry_dword { 130 uint32_t idx; /* Table index. */ 131 uint64_t ref_cnt; /* Table ref_cnt. */ 132 uint32_t entry[]; /* Entry array. */ 133 }; 134 135 /* L3 quad word entry table data structure. */ 136 struct mlx5_l3t_entry_qword { 137 uint32_t idx; /* Table index. */ 138 uint64_t ref_cnt; /* Table ref_cnt. */ 139 uint64_t entry[]; /* Entry array. */ 140 }; 141 142 /* L3 pointer entry table data structure. */ 143 struct mlx5_l3t_entry_ptr { 144 uint32_t idx; /* Table index. */ 145 uint64_t ref_cnt; /* Table ref_cnt. */ 146 void *entry[]; /* Entry array. */ 147 }; 148 149 /* L3 table data structure. */ 150 struct mlx5_l3t_tbl { 151 enum mlx5_l3t_type type; /* Table type. */ 152 struct mlx5_indexed_pool *eip; 153 /* Table index pool handles. */ 154 struct mlx5_l3t_level_tbl *tbl; /* Global table index. */ 155 }; 156 157 /* 158 * The indexed memory entry index is made up of trunk index and offset of 159 * the entry in the trunk. Since the entry index is 32 bits, in case user 160 * prefers to have small trunks, user can change the macro below to a big 161 * number which helps the pool contains more trunks with lots of entries 162 * allocated. 163 */ 164 #define TRUNK_IDX_BITS 16 165 #define TRUNK_MAX_IDX ((1 << TRUNK_IDX_BITS) - 1) 166 #define TRUNK_INVALID TRUNK_MAX_IDX 167 #define MLX5_IPOOL_DEFAULT_TRUNK_SIZE (1 << (28 - TRUNK_IDX_BITS)) 168 #ifdef RTE_LIBRTE_MLX5_DEBUG 169 #define POOL_DEBUG 1 170 #endif 171 172 struct mlx5_indexed_pool_config { 173 uint32_t size; /* Pool entry size. */ 174 uint32_t trunk_size:22; 175 /* 176 * Trunk entry number. Must be power of 2. It can be increased 177 * if trunk_grow enable. The trunk entry number increases with 178 * left shift grow_shift. Trunks with index are after grow_trunk 179 * will keep the entry number same with the last grow trunk. 180 */ 181 uint32_t grow_trunk:4; 182 /* 183 * Trunks with entry number increase in the pool. Set it to 0 184 * to make the pool works as trunk entry fixed pool. It works 185 * only if grow_shift is not 0. 186 */ 187 uint32_t grow_shift:4; 188 /* 189 * Trunk entry number increase shift value, stop after grow_trunk. 190 * It works only if grow_trunk is not 0. 191 */ 192 uint32_t need_lock:1; 193 /* Lock is needed for multiple thread usage. */ 194 uint32_t release_mem_en:1; /* Rlease trunk when it is free. */ 195 const char *type; /* Memory allocate type name. */ 196 void *(*malloc)(uint32_t flags, size_t size, unsigned int align, 197 int socket); 198 /* User defined memory allocator. */ 199 void (*free)(void *addr); /* User defined memory release. */ 200 }; 201 202 struct mlx5_indexed_trunk { 203 uint32_t idx; /* Trunk id. */ 204 uint32_t prev; /* Previous free trunk in free list. */ 205 uint32_t next; /* Next free trunk in free list. */ 206 uint32_t free; /* Free entries available */ 207 struct rte_bitmap *bmp; 208 uint8_t data[] __rte_cache_aligned; /* Entry data start. */ 209 }; 210 211 struct mlx5_indexed_pool { 212 struct mlx5_indexed_pool_config cfg; /* Indexed pool configuration. */ 213 rte_spinlock_t lock; /* Pool lock for multiple thread usage. */ 214 uint32_t n_trunk_valid; /* Trunks allocated. */ 215 uint32_t n_trunk; /* Trunk pointer array size. */ 216 /* Dim of trunk pointer array. */ 217 struct mlx5_indexed_trunk **trunks; 218 uint32_t free_list; /* Index to first free trunk. */ 219 #ifdef POOL_DEBUG 220 uint32_t n_entry; 221 uint32_t trunk_new; 222 uint32_t trunk_avail; 223 uint32_t trunk_empty; 224 uint32_t trunk_free; 225 #endif 226 uint32_t grow_tbl[]; /* Save the index offset for the grow trunks. */ 227 }; 228 229 /** 230 * Return logarithm of the nearest power of two above input value. 231 * 232 * @param v 233 * Input value. 234 * 235 * @return 236 * Logarithm of the nearest power of two above input value. 237 */ 238 static inline unsigned int 239 log2above(unsigned int v) 240 { 241 unsigned int l; 242 unsigned int r; 243 244 for (l = 0, r = 0; (v >> 1); ++l, v >>= 1) 245 r |= (v & 1); 246 return l + r; 247 } 248 249 /** Maximum size of string for naming the hlist table. */ 250 #define MLX5_HLIST_NAMESIZE 32 251 252 /** 253 * Structure of the entry in the hash list, user should define its own struct 254 * that contains this in order to store the data. The 'key' is 64-bits right 255 * now and its user's responsibility to guarantee there is no collision. 256 */ 257 struct mlx5_hlist_entry { 258 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */ 259 uint64_t key; /* user defined 'key', could be the hash signature. */ 260 }; 261 262 /** Structure for hash head. */ 263 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry); 264 265 /** Type of function that is used to handle the data before freeing. */ 266 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx); 267 268 /** hash list table structure */ 269 struct mlx5_hlist { 270 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */ 271 /**< number of heads, need to be power of 2. */ 272 uint32_t table_sz; 273 /**< mask to get the index of the list heads. */ 274 uint32_t mask; 275 struct mlx5_hlist_head heads[]; /**< list head arrays. */ 276 }; 277 278 /** 279 * Create a hash list table, the user can specify the list heads array size 280 * of the table, now the size should be a power of 2 in order to get better 281 * distribution for the entries. Each entry is a part of the whole data element 282 * and the caller should be responsible for the data element's allocation and 283 * cleanup / free. Key of each entry will be calculated with CRC in order to 284 * generate a little fairer distribution. 285 * 286 * @param name 287 * Name of the hash list(optional). 288 * @param size 289 * Heads array size of the hash list. 290 * 291 * @return 292 * Pointer of the hash list table created, NULL on failure. 293 */ 294 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size); 295 296 /** 297 * Search an entry matching the key. 298 * 299 * @param h 300 * Pointer to the hast list table. 301 * @param key 302 * Key for the searching entry. 303 * 304 * @return 305 * Pointer of the hlist entry if found, NULL otherwise. 306 */ 307 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key); 308 309 /** 310 * Insert an entry to the hash list table, the entry is only part of whole data 311 * element and a 64B key is used for matching. User should construct the key or 312 * give a calculated hash signature and guarantee there is no collision. 313 * 314 * @param h 315 * Pointer to the hast list table. 316 * @param entry 317 * Entry to be inserted into the hash list table. 318 * 319 * @return 320 * - zero for success. 321 * - -EEXIST if the entry is already inserted. 322 */ 323 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry); 324 325 /** 326 * Remove an entry from the hash list table. User should guarantee the validity 327 * of the entry. 328 * 329 * @param h 330 * Pointer to the hast list table. (not used) 331 * @param entry 332 * Entry to be removed from the hash list table. 333 */ 334 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused, 335 struct mlx5_hlist_entry *entry); 336 337 /** 338 * Destroy the hash list table, all the entries already inserted into the lists 339 * will be handled by the callback function provided by the user (including 340 * free if needed) before the table is freed. 341 * 342 * @param h 343 * Pointer to the hast list table. 344 * @param cb 345 * Callback function for each inserted entry when destroying the hash list. 346 * @param ctx 347 * Common context parameter used by callback function for each entry. 348 */ 349 void mlx5_hlist_destroy(struct mlx5_hlist *h, 350 mlx5_hlist_destroy_callback_fn cb, void *ctx); 351 352 /** 353 * This function allocates non-initialized memory entry from pool. 354 * In NUMA systems, the memory entry allocated resides on the same 355 * NUMA socket as the core that calls this function. 356 * 357 * Memory entry is allocated from memory trunk, no alignment. 358 * 359 * @param pool 360 * Pointer to indexed memory entry pool. 361 * No initialization required. 362 * @param[out] idx 363 * Pointer to memory to save allocated index. 364 * Memory index always positive value. 365 * @return 366 * - Pointer to the allocated memory entry. 367 * - NULL on error. Not enough memory, or invalid arguments. 368 */ 369 void *mlx5_ipool_malloc(struct mlx5_indexed_pool *pool, uint32_t *idx); 370 371 /** 372 * This function allocates zero initialized memory entry from pool. 373 * In NUMA systems, the memory entry allocated resides on the same 374 * NUMA socket as the core that calls this function. 375 * 376 * Memory entry is allocated from memory trunk, no alignment. 377 * 378 * @param pool 379 * Pointer to indexed memory pool. 380 * No initialization required. 381 * @param[out] idx 382 * Pointer to memory to save allocated index. 383 * Memory index always positive value. 384 * @return 385 * - Pointer to the allocated memory entry . 386 * - NULL on error. Not enough memory, or invalid arguments. 387 */ 388 void *mlx5_ipool_zmalloc(struct mlx5_indexed_pool *pool, uint32_t *idx); 389 390 /** 391 * This function frees indexed memory entry to pool. 392 * Caller has to make sure that the index is allocated from same pool. 393 * 394 * @param pool 395 * Pointer to indexed memory pool. 396 * @param idx 397 * Allocated memory entry index. 398 */ 399 void mlx5_ipool_free(struct mlx5_indexed_pool *pool, uint32_t idx); 400 401 /** 402 * This function returns pointer of indexed memory entry from index. 403 * Caller has to make sure that the index is valid, and allocated 404 * from same pool. 405 * 406 * @param pool 407 * Pointer to indexed memory pool. 408 * @param idx 409 * Allocated memory index. 410 * @return 411 * - Pointer to indexed memory entry. 412 */ 413 void *mlx5_ipool_get(struct mlx5_indexed_pool *pool, uint32_t idx); 414 415 /** 416 * This function creates indexed memory pool. 417 * Caller has to configure the configuration accordingly. 418 * 419 * @param pool 420 * Pointer to indexed memory pool. 421 * @param cfg 422 * Allocated memory index. 423 */ 424 struct mlx5_indexed_pool * 425 mlx5_ipool_create(struct mlx5_indexed_pool_config *cfg); 426 427 /** 428 * This function releases all resources of pool. 429 * Caller has to make sure that all indexes and memories allocated 430 * from this pool not referenced anymore. 431 * 432 * @param pool 433 * Pointer to indexed memory pool. 434 * @return 435 * - non-zero value on error. 436 * - 0 on success. 437 */ 438 int mlx5_ipool_destroy(struct mlx5_indexed_pool *pool); 439 440 /** 441 * This function dumps debug info of pool. 442 * 443 * @param pool 444 * Pointer to indexed memory pool. 445 */ 446 void mlx5_ipool_dump(struct mlx5_indexed_pool *pool); 447 448 /** 449 * This function allocates new empty Three-level table. 450 * 451 * @param type 452 * The l3t can set as word, double word, quad word or pointer with index. 453 * 454 * @return 455 * - Pointer to the allocated l3t. 456 * - NULL on error. Not enough memory, or invalid arguments. 457 */ 458 struct mlx5_l3t_tbl *mlx5_l3t_create(enum mlx5_l3t_type type); 459 460 /** 461 * This function destroys Three-level table. 462 * 463 * @param tbl 464 * Pointer to the l3t. 465 */ 466 void mlx5_l3t_destroy(struct mlx5_l3t_tbl *tbl); 467 468 /** 469 * This function gets the index entry from Three-level table. 470 * 471 * @param tbl 472 * Pointer to the l3t. 473 * @param idx 474 * Index to the entry. 475 * @param data 476 * Pointer to the memory which saves the entry data. 477 * When function call returns 0, data contains the entry data get from 478 * l3t. 479 * When function call returns -1, data is not modified. 480 * 481 * @return 482 * 0 if success, -1 on error. 483 */ 484 485 uint32_t mlx5_l3t_get_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx, 486 union mlx5_l3t_data *data); 487 /** 488 * This function clears the index entry from Three-level table. 489 * 490 * @param tbl 491 * Pointer to the l3t. 492 * @param idx 493 * Index to the entry. 494 */ 495 void mlx5_l3t_clear_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx); 496 497 /** 498 * This function gets the index entry from Three-level table. 499 * 500 * @param tbl 501 * Pointer to the l3t. 502 * @param idx 503 * Index to the entry. 504 * @param data 505 * Pointer to the memory which contains the entry data save to l3t. 506 * 507 * @return 508 * 0 if success, -1 on error. 509 */ 510 uint32_t mlx5_l3t_set_entry(struct mlx5_l3t_tbl *tbl, uint32_t idx, 511 union mlx5_l3t_data *data); 512 513 /* 514 * Macros for linked list based on indexed memory. 515 * Example data structure: 516 * struct Foo { 517 * ILIST_ENTRY(uint16_t) next; 518 * ... 519 * } 520 * 521 */ 522 #define ILIST_ENTRY(type) \ 523 struct { \ 524 type prev; /* Index of previous element. */ \ 525 type next; /* Index of next element. */ \ 526 } 527 528 #define ILIST_INSERT(pool, head, idx, elem, field) \ 529 do { \ 530 typeof(elem) peer; \ 531 MLX5_ASSERT((elem) && (idx)); \ 532 (elem)->field.next = *(head); \ 533 (elem)->field.prev = 0; \ 534 if (*(head)) { \ 535 (peer) = mlx5_ipool_get(pool, *(head)); \ 536 if (peer) \ 537 (peer)->field.prev = (idx); \ 538 } \ 539 *(head) = (idx); \ 540 } while (0) 541 542 #define ILIST_REMOVE(pool, head, idx, elem, field) \ 543 do { \ 544 typeof(elem) peer; \ 545 MLX5_ASSERT(elem); \ 546 MLX5_ASSERT(head); \ 547 if ((elem)->field.prev) { \ 548 (peer) = mlx5_ipool_get \ 549 (pool, (elem)->field.prev); \ 550 if (peer) \ 551 (peer)->field.next = (elem)->field.next;\ 552 } \ 553 if ((elem)->field.next) { \ 554 (peer) = mlx5_ipool_get \ 555 (pool, (elem)->field.next); \ 556 if (peer) \ 557 (peer)->field.prev = (elem)->field.prev;\ 558 } \ 559 if (*(head) == (idx)) \ 560 *(head) = (elem)->field.next; \ 561 } while (0) 562 563 #define ILIST_FOREACH(pool, head, idx, elem, field) \ 564 for ((idx) = (head), (elem) = \ 565 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \ 566 idx = (elem)->field.next, (elem) = \ 567 (idx) ? mlx5_ipool_get(pool, idx) : NULL) 568 569 /* Single index list. */ 570 #define SILIST_ENTRY(type) \ 571 struct { \ 572 type next; /* Index of next element. */ \ 573 } 574 575 #define SILIST_INSERT(head, idx, elem, field) \ 576 do { \ 577 MLX5_ASSERT((elem) && (idx)); \ 578 (elem)->field.next = *(head); \ 579 *(head) = (idx); \ 580 } while (0) 581 582 #define SILIST_FOREACH(pool, head, idx, elem, field) \ 583 for ((idx) = (head), (elem) = \ 584 (idx) ? mlx5_ipool_get(pool, (idx)) : NULL; (elem); \ 585 idx = (elem)->field.next, (elem) = \ 586 (idx) ? mlx5_ipool_get(pool, idx) : NULL) 587 588 #endif /* RTE_PMD_MLX5_UTILS_H_ */ 589