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 enum rxe_pool_flags { 118700e3e7SMoni Shoua RXE_POOL_INDEX = BIT(1), 12c9f4c695SBob Pearson RXE_POOL_ALLOC = BIT(2), 138700e3e7SMoni Shoua }; 148700e3e7SMoni Shoua 158700e3e7SMoni Shoua enum rxe_elem_type { 168700e3e7SMoni Shoua RXE_TYPE_UC, 178700e3e7SMoni Shoua RXE_TYPE_PD, 188700e3e7SMoni Shoua RXE_TYPE_AH, 198700e3e7SMoni Shoua RXE_TYPE_SRQ, 208700e3e7SMoni Shoua RXE_TYPE_QP, 218700e3e7SMoni Shoua RXE_TYPE_CQ, 228700e3e7SMoni Shoua RXE_TYPE_MR, 238700e3e7SMoni Shoua RXE_TYPE_MW, 248700e3e7SMoni Shoua RXE_NUM_TYPES, /* keep me last */ 258700e3e7SMoni Shoua }; 268700e3e7SMoni Shoua 2702827b67SBob Pearson struct rxe_pool_elem { 288700e3e7SMoni Shoua struct rxe_pool *pool; 29b92d766cSBob Pearson void *obj; 308700e3e7SMoni Shoua struct kref ref_cnt; 318700e3e7SMoni Shoua struct list_head list; 328700e3e7SMoni Shoua 33c06ee3a0SBob Pearson /* only used if indexed */ 34c06ee3a0SBob Pearson struct rb_node index_node; 358700e3e7SMoni Shoua u32 index; 368700e3e7SMoni Shoua }; 378700e3e7SMoni Shoua 388700e3e7SMoni Shoua struct rxe_pool { 398700e3e7SMoni Shoua struct rxe_dev *rxe; 40c95acedbSBob Pearson const char *name; 4166d0f207SParav Pandit rwlock_t pool_lock; /* protects pool add/del/search */ 42*b4a47f68SBob Pearson void (*cleanup)(struct rxe_pool_elem *elem); 438700e3e7SMoni Shoua enum rxe_pool_flags flags; 448700e3e7SMoni Shoua enum rxe_elem_type type; 458700e3e7SMoni Shoua 468700e3e7SMoni Shoua unsigned int max_elem; 478700e3e7SMoni Shoua atomic_t num_elem; 48c95acedbSBob Pearson size_t elem_size; 49c95acedbSBob Pearson size_t elem_offset; 508700e3e7SMoni Shoua 51c06ee3a0SBob Pearson /* only used if indexed */ 52c06ee3a0SBob Pearson struct { 538700e3e7SMoni Shoua struct rb_root tree; 548700e3e7SMoni Shoua unsigned long *table; 55c06ee3a0SBob Pearson u32 last; 568700e3e7SMoni Shoua u32 max_index; 578700e3e7SMoni Shoua u32 min_index; 58c06ee3a0SBob Pearson } index; 598700e3e7SMoni Shoua }; 608700e3e7SMoni Shoua 618700e3e7SMoni Shoua /* initialize a pool of objects with given limit on 628700e3e7SMoni Shoua * number of elements. gets parameters from rxe_type_info 638700e3e7SMoni Shoua * pool elements will be allocated out of a slab cache 648700e3e7SMoni Shoua */ 658700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool, 668700e3e7SMoni Shoua enum rxe_elem_type type, u32 max_elem); 678700e3e7SMoni Shoua 688700e3e7SMoni Shoua /* free resources from object pool */ 691ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool); 708700e3e7SMoni Shoua 713c3e4d58SBob Pearson /* allocate an object from pool */ 7288cc77ebSBob Pearson void *rxe_alloc(struct rxe_pool *pool); 733853c35eSBob Pearson 7421a428a0SLeon Romanovsky /* connect already allocated object to pool */ 7502827b67SBob Pearson int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem); 7691a42c5bSBob Pearson 7702827b67SBob Pearson #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem) 7821a428a0SLeon Romanovsky 798700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into 803c3e4d58SBob Pearson * pool's rb tree 818700e3e7SMoni Shoua */ 8202827b67SBob Pearson int __rxe_add_index(struct rxe_pool_elem *elem); 8391a42c5bSBob Pearson 8402827b67SBob Pearson #define rxe_add_index(obj) __rxe_add_index(&(obj)->elem) 858700e3e7SMoni Shoua 863c3e4d58SBob Pearson /* drop an index and remove object from rb tree */ 8702827b67SBob Pearson void __rxe_drop_index(struct rxe_pool_elem *elem); 8891a42c5bSBob Pearson 8902827b67SBob Pearson #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->elem) 908700e3e7SMoni Shoua 913c3e4d58SBob Pearson /* lookup an indexed object from index. takes a reference on object */ 928700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index); 938700e3e7SMoni Shoua 948700e3e7SMoni Shoua /* cleanup an object when all references are dropped */ 958700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref); 968700e3e7SMoni Shoua 978700e3e7SMoni Shoua /* take a reference on an object */ 9802827b67SBob Pearson #define rxe_add_ref(obj) kref_get(&(obj)->elem.ref_cnt) 998700e3e7SMoni Shoua 1008700e3e7SMoni Shoua /* drop a reference on an object */ 10102827b67SBob Pearson #define rxe_drop_ref(obj) kref_put(&(obj)->elem.ref_cnt, rxe_elem_release) 1028700e3e7SMoni Shoua 103d5724055SBob Pearson #define rxe_read_ref(obj) kref_read(&(obj)->elem.ref_cnt) 104d5724055SBob Pearson 1058700e3e7SMoni Shoua #endif /* RXE_POOL_H */ 106