163fa15dbSBob 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_INDEX = BIT(1), 158700e3e7SMoni Shoua RXE_POOL_KEY = BIT(2), 1621a428a0SLeon Romanovsky RXE_POOL_NO_ALLOC = BIT(4), 178700e3e7SMoni Shoua }; 188700e3e7SMoni Shoua 198700e3e7SMoni Shoua enum rxe_elem_type { 208700e3e7SMoni Shoua RXE_TYPE_UC, 218700e3e7SMoni Shoua RXE_TYPE_PD, 228700e3e7SMoni Shoua RXE_TYPE_AH, 238700e3e7SMoni Shoua RXE_TYPE_SRQ, 248700e3e7SMoni Shoua RXE_TYPE_QP, 258700e3e7SMoni Shoua RXE_TYPE_CQ, 268700e3e7SMoni Shoua RXE_TYPE_MR, 278700e3e7SMoni Shoua RXE_TYPE_MW, 288700e3e7SMoni Shoua RXE_TYPE_MC_GRP, 298700e3e7SMoni Shoua RXE_TYPE_MC_ELEM, 308700e3e7SMoni Shoua RXE_NUM_TYPES, /* keep me last */ 318700e3e7SMoni Shoua }; 328700e3e7SMoni Shoua 33*02827b67SBob Pearson struct rxe_pool_elem { 348700e3e7SMoni Shoua struct rxe_pool *pool; 358700e3e7SMoni Shoua struct kref ref_cnt; 368700e3e7SMoni Shoua struct list_head list; 378700e3e7SMoni Shoua 38c06ee3a0SBob Pearson /* only used if keyed */ 39c06ee3a0SBob Pearson struct rb_node key_node; 40c06ee3a0SBob Pearson 41c06ee3a0SBob Pearson /* only used if indexed */ 42c06ee3a0SBob Pearson struct rb_node index_node; 438700e3e7SMoni Shoua u32 index; 448700e3e7SMoni Shoua }; 458700e3e7SMoni Shoua 468700e3e7SMoni Shoua struct rxe_pool { 478700e3e7SMoni Shoua struct rxe_dev *rxe; 4866d0f207SParav Pandit rwlock_t pool_lock; /* protects pool add/del/search */ 498700e3e7SMoni Shoua size_t elem_size; 50*02827b67SBob Pearson void (*cleanup)(struct rxe_pool_elem *obj); 518700e3e7SMoni Shoua enum rxe_pool_flags flags; 528700e3e7SMoni Shoua enum rxe_elem_type type; 538700e3e7SMoni Shoua 548700e3e7SMoni Shoua unsigned int max_elem; 558700e3e7SMoni Shoua atomic_t num_elem; 568700e3e7SMoni Shoua 57c06ee3a0SBob Pearson /* only used if indexed */ 58c06ee3a0SBob Pearson struct { 598700e3e7SMoni Shoua struct rb_root tree; 608700e3e7SMoni Shoua unsigned long *table; 61c06ee3a0SBob Pearson u32 last; 628700e3e7SMoni Shoua u32 max_index; 638700e3e7SMoni Shoua u32 min_index; 64c06ee3a0SBob Pearson } index; 65c06ee3a0SBob Pearson 66c06ee3a0SBob Pearson /* only used if keyed */ 67c06ee3a0SBob Pearson struct { 68c06ee3a0SBob Pearson struct rb_root tree; 698700e3e7SMoni Shoua size_t key_offset; 708700e3e7SMoni Shoua size_t key_size; 71c06ee3a0SBob Pearson } key; 728700e3e7SMoni Shoua }; 738700e3e7SMoni Shoua 748700e3e7SMoni Shoua /* initialize a pool of objects with given limit on 758700e3e7SMoni Shoua * number of elements. gets parameters from rxe_type_info 768700e3e7SMoni Shoua * pool elements will be allocated out of a slab cache 778700e3e7SMoni Shoua */ 788700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, 798700e3e7SMoni Shoua enum rxe_elem_type type, u32 max_elem); 808700e3e7SMoni Shoua 818700e3e7SMoni Shoua /* free resources from object pool */ 821ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool); 838700e3e7SMoni Shoua 8488cc77ebSBob Pearson /* allocate an object from pool holding and not holding the pool lock */ 8588cc77ebSBob Pearson void *rxe_alloc_locked(struct rxe_pool *pool); 868700e3e7SMoni Shoua 8788cc77ebSBob Pearson void *rxe_alloc(struct rxe_pool *pool); 883853c35eSBob Pearson 8921a428a0SLeon Romanovsky /* connect already allocated object to pool */ 90*02827b67SBob Pearson int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem); 9191a42c5bSBob Pearson 92*02827b67SBob Pearson #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem) 9321a428a0SLeon Romanovsky 948700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into 9588cc77ebSBob Pearson * pool's rb tree holding and not holding the pool_lock 968700e3e7SMoni Shoua */ 97*02827b67SBob Pearson int __rxe_add_index_locked(struct rxe_pool_elem *elem); 9888cc77ebSBob Pearson 99*02827b67SBob Pearson #define rxe_add_index_locked(obj) __rxe_add_index_locked(&(obj)->elem) 10088cc77ebSBob Pearson 101*02827b67SBob Pearson int __rxe_add_index(struct rxe_pool_elem *elem); 10291a42c5bSBob Pearson 103*02827b67SBob Pearson #define rxe_add_index(obj) __rxe_add_index(&(obj)->elem) 1048700e3e7SMoni Shoua 1053853c35eSBob Pearson /* drop an index and remove object from rb tree 10688cc77ebSBob Pearson * holding and not holding the pool_lock 1073853c35eSBob Pearson */ 108*02827b67SBob Pearson void __rxe_drop_index_locked(struct rxe_pool_elem *elem); 10988cc77ebSBob Pearson 110*02827b67SBob Pearson #define rxe_drop_index_locked(obj) __rxe_drop_index_locked(&(obj)->elem) 11188cc77ebSBob Pearson 112*02827b67SBob Pearson void __rxe_drop_index(struct rxe_pool_elem *elem); 11391a42c5bSBob Pearson 114*02827b67SBob Pearson #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->elem) 1158700e3e7SMoni Shoua 1168700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into 11788cc77ebSBob Pearson * pool's rb tree holding and not holding pool_lock 1188700e3e7SMoni Shoua */ 119*02827b67SBob Pearson int __rxe_add_key_locked(struct rxe_pool_elem *elem, void *key); 12088cc77ebSBob Pearson 121*02827b67SBob Pearson #define rxe_add_key_locked(obj, key) __rxe_add_key_locked(&(obj)->elem, key) 12288cc77ebSBob Pearson 123*02827b67SBob Pearson int __rxe_add_key(struct rxe_pool_elem *elem, void *key); 12491a42c5bSBob Pearson 125*02827b67SBob Pearson #define rxe_add_key(obj, key) __rxe_add_key(&(obj)->elem, key) 1268700e3e7SMoni Shoua 12788cc77ebSBob Pearson /* remove elem from rb tree holding and not holding the pool_lock */ 128*02827b67SBob Pearson void __rxe_drop_key_locked(struct rxe_pool_elem *elem); 1293853c35eSBob Pearson 130*02827b67SBob Pearson #define rxe_drop_key_locked(obj) __rxe_drop_key_locked(&(obj)->elem) 1313853c35eSBob Pearson 132*02827b67SBob Pearson void __rxe_drop_key(struct rxe_pool_elem *elem); 13391a42c5bSBob Pearson 134*02827b67SBob Pearson #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->elem) 1358700e3e7SMoni Shoua 13688cc77ebSBob Pearson /* lookup an indexed object from index holding and not holding the pool_lock. 1373853c35eSBob Pearson * takes a reference on object 1383853c35eSBob Pearson */ 13988cc77ebSBob Pearson void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index); 14088cc77ebSBob Pearson 1418700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); 1428700e3e7SMoni Shoua 14388cc77ebSBob Pearson /* lookup keyed object from key holding and not holding the pool_lock. 1443853c35eSBob Pearson * takes a reference on the objecti 1453853c35eSBob Pearson */ 14688cc77ebSBob Pearson void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key); 1478700e3e7SMoni Shoua 14888cc77ebSBob Pearson void *rxe_pool_get_key(struct rxe_pool *pool, void *key); 1493853c35eSBob Pearson 1508700e3e7SMoni Shoua /* cleanup an object when all references are dropped */ 1518700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref); 1528700e3e7SMoni Shoua 1538700e3e7SMoni Shoua /* take a reference on an object */ 154*02827b67SBob Pearson #define rxe_add_ref(obj) kref_get(&(obj)->elem.ref_cnt) 1558700e3e7SMoni Shoua 1568700e3e7SMoni Shoua /* drop a reference on an object */ 157*02827b67SBob Pearson #define rxe_drop_ref(obj) kref_put(&(obj)->elem.ref_cnt, rxe_elem_release) 1588700e3e7SMoni Shoua 1598700e3e7SMoni Shoua #endif /* RXE_POOL_H */ 160