1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 2 /* 3 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 4 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 5 */ 6 7 #ifndef RXE_POOL_H 8 #define RXE_POOL_H 9 10 #define RXE_POOL_ALIGN (16) 11 #define RXE_POOL_CACHE_FLAGS (0) 12 13 enum rxe_pool_flags { 14 RXE_POOL_INDEX = BIT(1), 15 RXE_POOL_KEY = BIT(2), 16 RXE_POOL_NO_ALLOC = BIT(4), 17 }; 18 19 enum rxe_elem_type { 20 RXE_TYPE_UC, 21 RXE_TYPE_PD, 22 RXE_TYPE_AH, 23 RXE_TYPE_SRQ, 24 RXE_TYPE_QP, 25 RXE_TYPE_CQ, 26 RXE_TYPE_MR, 27 RXE_TYPE_MW, 28 RXE_TYPE_MC_GRP, 29 RXE_TYPE_MC_ELEM, 30 RXE_NUM_TYPES, /* keep me last */ 31 }; 32 33 struct rxe_pool_entry; 34 35 struct rxe_type_info { 36 const char *name; 37 size_t size; 38 size_t elem_offset; 39 void (*cleanup)(struct rxe_pool_entry *obj); 40 enum rxe_pool_flags flags; 41 u32 max_index; 42 u32 min_index; 43 size_t key_offset; 44 size_t key_size; 45 }; 46 47 extern struct rxe_type_info rxe_type_info[]; 48 49 struct rxe_pool_entry { 50 struct rxe_pool *pool; 51 struct kref ref_cnt; 52 struct list_head list; 53 54 /* only used if keyed */ 55 struct rb_node key_node; 56 57 /* only used if indexed */ 58 struct rb_node index_node; 59 u32 index; 60 }; 61 62 struct rxe_pool { 63 struct rxe_dev *rxe; 64 rwlock_t pool_lock; /* protects pool add/del/search */ 65 size_t elem_size; 66 void (*cleanup)(struct rxe_pool_entry *obj); 67 enum rxe_pool_flags flags; 68 enum rxe_elem_type type; 69 70 unsigned int max_elem; 71 atomic_t num_elem; 72 73 /* only used if indexed */ 74 struct { 75 struct rb_root tree; 76 unsigned long *table; 77 size_t table_size; 78 u32 last; 79 u32 max_index; 80 u32 min_index; 81 } index; 82 83 /* only used if keyed */ 84 struct { 85 struct rb_root tree; 86 size_t key_offset; 87 size_t key_size; 88 } key; 89 }; 90 91 /* initialize a pool of objects with given limit on 92 * number of elements. gets parameters from rxe_type_info 93 * pool elements will be allocated out of a slab cache 94 */ 95 int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, 96 enum rxe_elem_type type, u32 max_elem); 97 98 /* free resources from object pool */ 99 void rxe_pool_cleanup(struct rxe_pool *pool); 100 101 /* allocate an object from pool holding and not holding the pool lock */ 102 void *rxe_alloc_locked(struct rxe_pool *pool); 103 104 void *rxe_alloc(struct rxe_pool *pool); 105 106 /* connect already allocated object to pool */ 107 int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem); 108 109 #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem) 110 111 /* assign an index to an indexed object and insert object into 112 * pool's rb tree holding and not holding the pool_lock 113 */ 114 int __rxe_add_index_locked(struct rxe_pool_entry *elem); 115 116 #define rxe_add_index_locked(obj) __rxe_add_index_locked(&(obj)->pelem) 117 118 int __rxe_add_index(struct rxe_pool_entry *elem); 119 120 #define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem) 121 122 /* drop an index and remove object from rb tree 123 * holding and not holding the pool_lock 124 */ 125 void __rxe_drop_index_locked(struct rxe_pool_entry *elem); 126 127 #define rxe_drop_index_locked(obj) __rxe_drop_index_locked(&(obj)->pelem) 128 129 void __rxe_drop_index(struct rxe_pool_entry *elem); 130 131 #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem) 132 133 /* assign a key to a keyed object and insert object into 134 * pool's rb tree holding and not holding pool_lock 135 */ 136 int __rxe_add_key_locked(struct rxe_pool_entry *elem, void *key); 137 138 #define rxe_add_key_locked(obj, key) __rxe_add_key_locked(&(obj)->pelem, key) 139 140 int __rxe_add_key(struct rxe_pool_entry *elem, void *key); 141 142 #define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key) 143 144 /* remove elem from rb tree holding and not holding the pool_lock */ 145 void __rxe_drop_key_locked(struct rxe_pool_entry *elem); 146 147 #define rxe_drop_key_locked(obj) __rxe_drop_key_locked(&(obj)->pelem) 148 149 void __rxe_drop_key(struct rxe_pool_entry *elem); 150 151 #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem) 152 153 /* lookup an indexed object from index holding and not holding the pool_lock. 154 * takes a reference on object 155 */ 156 void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index); 157 158 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); 159 160 /* lookup keyed object from key holding and not holding the pool_lock. 161 * takes a reference on the objecti 162 */ 163 void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key); 164 165 void *rxe_pool_get_key(struct rxe_pool *pool, void *key); 166 167 /* cleanup an object when all references are dropped */ 168 void rxe_elem_release(struct kref *kref); 169 170 /* take a reference on an object */ 171 #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt) 172 173 /* drop a reference on an object */ 174 #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release) 175 176 #endif /* RXE_POOL_H */ 177