1*63fa15dbSBob Pearson /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */ 28700e3e7SMoni Shoua /* 38700e3e7SMoni Shoua * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved. 48700e3e7SMoni Shoua * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved. 58700e3e7SMoni Shoua */ 68700e3e7SMoni Shoua 78700e3e7SMoni Shoua #ifndef RXE_POOL_H 88700e3e7SMoni Shoua #define RXE_POOL_H 98700e3e7SMoni Shoua 108700e3e7SMoni Shoua #define RXE_POOL_ALIGN (16) 118700e3e7SMoni Shoua #define RXE_POOL_CACHE_FLAGS (0) 128700e3e7SMoni Shoua 138700e3e7SMoni Shoua enum rxe_pool_flags { 148700e3e7SMoni Shoua RXE_POOL_ATOMIC = BIT(0), 158700e3e7SMoni Shoua RXE_POOL_INDEX = BIT(1), 168700e3e7SMoni Shoua RXE_POOL_KEY = BIT(2), 1721a428a0SLeon Romanovsky RXE_POOL_NO_ALLOC = BIT(4), 188700e3e7SMoni Shoua }; 198700e3e7SMoni Shoua 208700e3e7SMoni Shoua enum rxe_elem_type { 218700e3e7SMoni Shoua RXE_TYPE_UC, 228700e3e7SMoni Shoua RXE_TYPE_PD, 238700e3e7SMoni Shoua RXE_TYPE_AH, 248700e3e7SMoni Shoua RXE_TYPE_SRQ, 258700e3e7SMoni Shoua RXE_TYPE_QP, 268700e3e7SMoni Shoua RXE_TYPE_CQ, 278700e3e7SMoni Shoua RXE_TYPE_MR, 288700e3e7SMoni Shoua RXE_TYPE_MW, 298700e3e7SMoni Shoua RXE_TYPE_MC_GRP, 308700e3e7SMoni Shoua RXE_TYPE_MC_ELEM, 318700e3e7SMoni Shoua RXE_NUM_TYPES, /* keep me last */ 328700e3e7SMoni Shoua }; 338700e3e7SMoni Shoua 3432404fb7SBart Van Assche struct rxe_pool_entry; 3532404fb7SBart Van Assche 368700e3e7SMoni Shoua struct rxe_type_info { 372bec3badSBart Van Assche const char *name; 388700e3e7SMoni Shoua size_t size; 3932404fb7SBart Van Assche void (*cleanup)(struct rxe_pool_entry *obj); 408700e3e7SMoni Shoua enum rxe_pool_flags flags; 418700e3e7SMoni Shoua u32 max_index; 428700e3e7SMoni Shoua u32 min_index; 438700e3e7SMoni Shoua size_t key_offset; 448700e3e7SMoni Shoua size_t key_size; 458700e3e7SMoni Shoua struct kmem_cache *cache; 468700e3e7SMoni Shoua }; 478700e3e7SMoni Shoua 488700e3e7SMoni Shoua extern struct rxe_type_info rxe_type_info[]; 498700e3e7SMoni Shoua 508700e3e7SMoni Shoua enum rxe_pool_state { 513ccf19e2SParav Pandit RXE_POOL_STATE_INVALID, 523ccf19e2SParav Pandit RXE_POOL_STATE_VALID, 538700e3e7SMoni Shoua }; 548700e3e7SMoni Shoua 558700e3e7SMoni Shoua struct rxe_pool_entry { 568700e3e7SMoni Shoua struct rxe_pool *pool; 578700e3e7SMoni Shoua struct kref ref_cnt; 588700e3e7SMoni Shoua struct list_head list; 598700e3e7SMoni Shoua 608700e3e7SMoni Shoua /* only used if indexed or keyed */ 618700e3e7SMoni Shoua struct rb_node node; 628700e3e7SMoni Shoua u32 index; 638700e3e7SMoni Shoua }; 648700e3e7SMoni Shoua 658700e3e7SMoni Shoua struct rxe_pool { 668700e3e7SMoni Shoua struct rxe_dev *rxe; 6766d0f207SParav Pandit rwlock_t pool_lock; /* protects pool add/del/search */ 688700e3e7SMoni Shoua size_t elem_size; 698700e3e7SMoni Shoua struct kref ref_cnt; 7032404fb7SBart Van Assche void (*cleanup)(struct rxe_pool_entry *obj); 718700e3e7SMoni Shoua enum rxe_pool_state state; 728700e3e7SMoni Shoua enum rxe_pool_flags flags; 738700e3e7SMoni Shoua enum rxe_elem_type type; 748700e3e7SMoni Shoua 758700e3e7SMoni Shoua unsigned int max_elem; 768700e3e7SMoni Shoua atomic_t num_elem; 778700e3e7SMoni Shoua 788700e3e7SMoni Shoua /* only used if indexed or keyed */ 798700e3e7SMoni Shoua struct rb_root tree; 808700e3e7SMoni Shoua unsigned long *table; 818700e3e7SMoni Shoua size_t table_size; 828700e3e7SMoni Shoua u32 max_index; 838700e3e7SMoni Shoua u32 min_index; 848700e3e7SMoni Shoua u32 last; 858700e3e7SMoni Shoua size_t key_offset; 868700e3e7SMoni Shoua size_t key_size; 878700e3e7SMoni Shoua }; 888700e3e7SMoni Shoua 898700e3e7SMoni Shoua /* initialize slab caches for managed objects */ 908700e3e7SMoni Shoua int rxe_cache_init(void); 918700e3e7SMoni Shoua 928700e3e7SMoni Shoua /* cleanup slab caches for managed objects */ 938700e3e7SMoni Shoua void rxe_cache_exit(void); 948700e3e7SMoni Shoua 958700e3e7SMoni Shoua /* initialize a pool of objects with given limit on 968700e3e7SMoni Shoua * number of elements. gets parameters from rxe_type_info 978700e3e7SMoni Shoua * pool elements will be allocated out of a slab cache 988700e3e7SMoni Shoua */ 998700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, 1008700e3e7SMoni Shoua enum rxe_elem_type type, u32 max_elem); 1018700e3e7SMoni Shoua 1028700e3e7SMoni Shoua /* free resources from object pool */ 1031ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool); 1048700e3e7SMoni Shoua 1058700e3e7SMoni Shoua /* allocate an object from pool */ 1068700e3e7SMoni Shoua void *rxe_alloc(struct rxe_pool *pool); 1078700e3e7SMoni Shoua 10821a428a0SLeon Romanovsky /* connect already allocated object to pool */ 10921a428a0SLeon Romanovsky int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem); 11021a428a0SLeon Romanovsky 1118700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into 1128700e3e7SMoni Shoua * pool's rb tree 1138700e3e7SMoni Shoua */ 1148700e3e7SMoni Shoua void rxe_add_index(void *elem); 1158700e3e7SMoni Shoua 1168700e3e7SMoni Shoua /* drop an index and remove object from rb tree */ 1178700e3e7SMoni Shoua void rxe_drop_index(void *elem); 1188700e3e7SMoni Shoua 1198700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into 1208700e3e7SMoni Shoua * pool's rb tree 1218700e3e7SMoni Shoua */ 1228700e3e7SMoni Shoua void rxe_add_key(void *elem, void *key); 1238700e3e7SMoni Shoua 1248700e3e7SMoni Shoua /* remove elem from rb tree */ 1258700e3e7SMoni Shoua void rxe_drop_key(void *elem); 1268700e3e7SMoni Shoua 1278700e3e7SMoni Shoua /* lookup an indexed object from index. takes a reference on object */ 1288700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); 1298700e3e7SMoni Shoua 1308700e3e7SMoni Shoua /* lookup keyed object from key. takes a reference on the object */ 1318700e3e7SMoni Shoua void *rxe_pool_get_key(struct rxe_pool *pool, void *key); 1328700e3e7SMoni Shoua 1338700e3e7SMoni Shoua /* cleanup an object when all references are dropped */ 1348700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref); 1358700e3e7SMoni Shoua 1368700e3e7SMoni Shoua /* take a reference on an object */ 1378700e3e7SMoni Shoua #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt) 1388700e3e7SMoni Shoua 1398700e3e7SMoni Shoua /* drop a reference on an object */ 1408700e3e7SMoni Shoua #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release) 1418700e3e7SMoni Shoua 1428700e3e7SMoni Shoua #endif /* RXE_POOL_H */ 143