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_pool_entry {
36 	struct rxe_pool		*pool;
37 	struct kref		ref_cnt;
38 	struct list_head	list;
39 
40 	/* only used if keyed */
41 	struct rb_node		key_node;
42 
43 	/* only used if indexed */
44 	struct rb_node		index_node;
45 	u32			index;
46 };
47 
48 struct rxe_pool {
49 	struct rxe_dev		*rxe;
50 	rwlock_t		pool_lock; /* protects pool add/del/search */
51 	size_t			elem_size;
52 	void			(*cleanup)(struct rxe_pool_entry *obj);
53 	enum rxe_pool_flags	flags;
54 	enum rxe_elem_type	type;
55 
56 	unsigned int		max_elem;
57 	atomic_t		num_elem;
58 
59 	/* only used if indexed */
60 	struct {
61 		struct rb_root		tree;
62 		unsigned long		*table;
63 		u32			last;
64 		u32			max_index;
65 		u32			min_index;
66 	} index;
67 
68 	/* only used if keyed */
69 	struct {
70 		struct rb_root		tree;
71 		size_t			key_offset;
72 		size_t			key_size;
73 	} key;
74 };
75 
76 /* initialize a pool of objects with given limit on
77  * number of elements. gets parameters from rxe_type_info
78  * pool elements will be allocated out of a slab cache
79  */
80 int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
81 		  enum rxe_elem_type type, u32 max_elem);
82 
83 /* free resources from object pool */
84 void rxe_pool_cleanup(struct rxe_pool *pool);
85 
86 /* allocate an object from pool holding and not holding the pool lock */
87 void *rxe_alloc_locked(struct rxe_pool *pool);
88 
89 void *rxe_alloc(struct rxe_pool *pool);
90 
91 /* connect already allocated object to pool */
92 int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
93 
94 #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem)
95 
96 /* assign an index to an indexed object and insert object into
97  *  pool's rb tree holding and not holding the pool_lock
98  */
99 int __rxe_add_index_locked(struct rxe_pool_entry *elem);
100 
101 #define rxe_add_index_locked(obj) __rxe_add_index_locked(&(obj)->pelem)
102 
103 int __rxe_add_index(struct rxe_pool_entry *elem);
104 
105 #define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem)
106 
107 /* drop an index and remove object from rb tree
108  * holding and not holding the pool_lock
109  */
110 void __rxe_drop_index_locked(struct rxe_pool_entry *elem);
111 
112 #define rxe_drop_index_locked(obj) __rxe_drop_index_locked(&(obj)->pelem)
113 
114 void __rxe_drop_index(struct rxe_pool_entry *elem);
115 
116 #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem)
117 
118 /* assign a key to a keyed object and insert object into
119  * pool's rb tree holding and not holding pool_lock
120  */
121 int __rxe_add_key_locked(struct rxe_pool_entry *elem, void *key);
122 
123 #define rxe_add_key_locked(obj, key) __rxe_add_key_locked(&(obj)->pelem, key)
124 
125 int __rxe_add_key(struct rxe_pool_entry *elem, void *key);
126 
127 #define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key)
128 
129 /* remove elem from rb tree holding and not holding the pool_lock */
130 void __rxe_drop_key_locked(struct rxe_pool_entry *elem);
131 
132 #define rxe_drop_key_locked(obj) __rxe_drop_key_locked(&(obj)->pelem)
133 
134 void __rxe_drop_key(struct rxe_pool_entry *elem);
135 
136 #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem)
137 
138 /* lookup an indexed object from index holding and not holding the pool_lock.
139  * takes a reference on object
140  */
141 void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index);
142 
143 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
144 
145 /* lookup keyed object from key holding and not holding the pool_lock.
146  * takes a reference on the objecti
147  */
148 void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key);
149 
150 void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
151 
152 /* cleanup an object when all references are dropped */
153 void rxe_elem_release(struct kref *kref);
154 
155 /* take a reference on an object */
156 #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
157 
158 /* drop a reference on an object */
159 #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
160 
161 #endif /* RXE_POOL_H */
162