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 <mlx5_common.h> 16 17 #include "mlx5_defs.h" 18 19 20 /* 21 * Compilation workaround for PPC64 when AltiVec is fully enabled, e.g. std=c11. 22 * Otherwise there would be a type conflict between stdbool and altivec. 23 */ 24 #if defined(__PPC64__) && !defined(__APPLE_ALTIVEC__) 25 #undef bool 26 /* redefine as in stdbool.h */ 27 #define bool _Bool 28 #endif 29 30 /* Convert a bit number to the corresponding 64-bit mask */ 31 #define MLX5_BITSHIFT(v) (UINT64_C(1) << (v)) 32 33 /* Save and restore errno around argument evaluation. */ 34 #define ERRNO_SAFE(x) ((errno = (int []){ errno, ((x), 0) }[0])) 35 36 extern int mlx5_logtype; 37 38 /* Generic printf()-like logging macro with automatic line feed. */ 39 #define DRV_LOG(level, ...) \ 40 PMD_DRV_LOG_(level, mlx5_logtype, MLX5_DRIVER_NAME, \ 41 __VA_ARGS__ PMD_DRV_LOG_STRIP PMD_DRV_LOG_OPAREN, \ 42 PMD_DRV_LOG_CPAREN) 43 44 #define INFO(...) DRV_LOG(INFO, __VA_ARGS__) 45 #define WARN(...) DRV_LOG(WARNING, __VA_ARGS__) 46 #define ERROR(...) DRV_LOG(ERR, __VA_ARGS__) 47 48 /* Convenience macros for accessing mbuf fields. */ 49 #define NEXT(m) ((m)->next) 50 #define DATA_LEN(m) ((m)->data_len) 51 #define PKT_LEN(m) ((m)->pkt_len) 52 #define DATA_OFF(m) ((m)->data_off) 53 #define SET_DATA_OFF(m, o) ((m)->data_off = (o)) 54 #define NB_SEGS(m) ((m)->nb_segs) 55 #define PORT(m) ((m)->port) 56 57 /* Transpose flags. Useful to convert IBV to DPDK flags. */ 58 #define TRANSPOSE(val, from, to) \ 59 (((from) >= (to)) ? \ 60 (((val) & (from)) / ((from) / (to))) : \ 61 (((val) & (from)) * ((to) / (from)))) 62 63 /** 64 * Return logarithm of the nearest power of two above input value. 65 * 66 * @param v 67 * Input value. 68 * 69 * @return 70 * Logarithm of the nearest power of two above input value. 71 */ 72 static inline unsigned int 73 log2above(unsigned int v) 74 { 75 unsigned int l; 76 unsigned int r; 77 78 for (l = 0, r = 0; (v >> 1); ++l, v >>= 1) 79 r |= (v & 1); 80 return l + r; 81 } 82 83 /** Maximum size of string for naming the hlist table. */ 84 #define MLX5_HLIST_NAMESIZE 32 85 86 /** 87 * Structure of the entry in the hash list, user should define its own struct 88 * that contains this in order to store the data. The 'key' is 64-bits right 89 * now and its user's responsibility to guarantee there is no collision. 90 */ 91 struct mlx5_hlist_entry { 92 LIST_ENTRY(mlx5_hlist_entry) next; /* entry pointers in the list. */ 93 uint64_t key; /* user defined 'key', could be the hash signature. */ 94 }; 95 96 /** Structure for hash head. */ 97 LIST_HEAD(mlx5_hlist_head, mlx5_hlist_entry); 98 99 /** Type of function that is used to handle the data before freeing. */ 100 typedef void (*mlx5_hlist_destroy_callback_fn)(void *p, void *ctx); 101 102 /** hash list table structure */ 103 struct mlx5_hlist { 104 char name[MLX5_HLIST_NAMESIZE]; /**< Name of the hash list. */ 105 /**< number of heads, need to be power of 2. */ 106 uint32_t table_sz; 107 /**< mask to get the index of the list heads. */ 108 uint32_t mask; 109 struct mlx5_hlist_head heads[]; /**< list head arrays. */ 110 }; 111 112 /** 113 * Create a hash list table, the user can specify the list heads array size 114 * of the table, now the size should be a power of 2 in order to get better 115 * distribution for the entries. Each entry is a part of the whole data element 116 * and the caller should be responsible for the data element's allocation and 117 * cleanup / free. Key of each entry will be calculated with CRC in order to 118 * generate a little fairer distribution. 119 * 120 * @param name 121 * Name of the hash list(optional). 122 * @param size 123 * Heads array size of the hash list. 124 * 125 * @return 126 * Pointer of the hash list table created, NULL on failure. 127 */ 128 struct mlx5_hlist *mlx5_hlist_create(const char *name, uint32_t size); 129 130 /** 131 * Search an entry matching the key. 132 * 133 * @param h 134 * Pointer to the hast list table. 135 * @param key 136 * Key for the searching entry. 137 * 138 * @return 139 * Pointer of the hlist entry if found, NULL otherwise. 140 */ 141 struct mlx5_hlist_entry *mlx5_hlist_lookup(struct mlx5_hlist *h, uint64_t key); 142 143 /** 144 * Insert an entry to the hash list table, the entry is only part of whole data 145 * element and a 64B key is used for matching. User should construct the key or 146 * give a calculated hash signature and guarantee there is no collision. 147 * 148 * @param h 149 * Pointer to the hast list table. 150 * @param entry 151 * Entry to be inserted into the hash list table. 152 * 153 * @return 154 * - zero for success. 155 * - -EEXIST if the entry is already inserted. 156 */ 157 int mlx5_hlist_insert(struct mlx5_hlist *h, struct mlx5_hlist_entry *entry); 158 159 /** 160 * Remove an entry from the hash list table. User should guarantee the validity 161 * of the entry. 162 * 163 * @param h 164 * Pointer to the hast list table. (not used) 165 * @param entry 166 * Entry to be removed from the hash list table. 167 */ 168 void mlx5_hlist_remove(struct mlx5_hlist *h __rte_unused, 169 struct mlx5_hlist_entry *entry); 170 171 /** 172 * Destroy the hash list table, all the entries already inserted into the lists 173 * will be handled by the callback function provided by the user (including 174 * free if needed) before the table is freed. 175 * 176 * @param h 177 * Pointer to the hast list table. 178 * @param cb 179 * Callback function for each inserted entry when destroying the hash list. 180 * @param ctx 181 * Common context parameter used by callback function for each entry. 182 */ 183 void mlx5_hlist_destroy(struct mlx5_hlist *h, 184 mlx5_hlist_destroy_callback_fn cb, void *ctx); 185 186 #endif /* RTE_PMD_MLX5_UTILS_H_ */ 187