1 /*
2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34 #ifndef RXE_POOL_H
35 #define RXE_POOL_H
36
37 #define RXE_POOL_ALIGN (16)
38 #define RXE_POOL_CACHE_FLAGS (0)
39
40 enum rxe_pool_flags {
41 RXE_POOL_ATOMIC = BIT(0),
42 RXE_POOL_INDEX = BIT(1),
43 RXE_POOL_KEY = BIT(2),
44 RXE_POOL_NO_ALLOC = BIT(4),
45 };
46
47 enum rxe_elem_type {
48 RXE_TYPE_UC,
49 RXE_TYPE_PD,
50 RXE_TYPE_AH,
51 RXE_TYPE_SRQ,
52 RXE_TYPE_QP,
53 RXE_TYPE_CQ,
54 RXE_TYPE_MR,
55 RXE_TYPE_MW,
56 RXE_TYPE_MC_GRP,
57 RXE_TYPE_MC_ELEM,
58 RXE_NUM_TYPES, /* keep me last */
59 };
60
61 struct rxe_pool_entry;
62
63 struct rxe_type_info {
64 const char *name;
65 size_t size;
66 void (*cleanup)(struct rxe_pool_entry *obj);
67 enum rxe_pool_flags flags;
68 u32 max_index;
69 u32 min_index;
70 size_t key_offset;
71 size_t key_size;
72 struct kmem_cache *cache;
73 };
74
75 extern struct rxe_type_info rxe_type_info[];
76
77 enum rxe_pool_state {
78 RXE_POOL_STATE_INVALID,
79 RXE_POOL_STATE_VALID,
80 };
81
82 struct rxe_pool_entry {
83 struct rxe_pool *pool;
84 struct kref ref_cnt;
85 struct list_head list;
86
87 /* only used if indexed or keyed */
88 struct rb_node node;
89 u32 index;
90 };
91
92 struct rxe_pool {
93 struct rxe_dev *rxe;
94 rwlock_t pool_lock; /* protects pool add/del/search */
95 size_t elem_size;
96 struct kref ref_cnt;
97 void (*cleanup)(struct rxe_pool_entry *obj);
98 enum rxe_pool_state state;
99 enum rxe_pool_flags flags;
100 enum rxe_elem_type type;
101
102 unsigned int max_elem;
103 atomic_t num_elem;
104
105 /* only used if indexed or keyed */
106 struct rb_root tree;
107 unsigned long *table;
108 size_t table_size;
109 u32 max_index;
110 u32 min_index;
111 u32 last;
112 size_t key_offset;
113 size_t key_size;
114 };
115
116 /* initialize slab caches for managed objects */
117 int rxe_cache_init(void);
118
119 /* cleanup slab caches for managed objects */
120 void rxe_cache_exit(void);
121
122 /* initialize a pool of objects with given limit on
123 * number of elements. gets parameters from rxe_type_info
124 * pool elements will be allocated out of a slab cache
125 */
126 int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
127 enum rxe_elem_type type, u32 max_elem);
128
129 /* free resources from object pool */
130 void rxe_pool_cleanup(struct rxe_pool *pool);
131
132 /* allocate an object from pool */
133 void *rxe_alloc(struct rxe_pool *pool);
134
135 /* connect already allocated object to pool */
136 int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
137
138 /* assign an index to an indexed object and insert object into
139 * pool's rb tree
140 */
141 void rxe_add_index(void *elem);
142
143 /* drop an index and remove object from rb tree */
144 void rxe_drop_index(void *elem);
145
146 /* assign a key to a keyed object and insert object into
147 * pool's rb tree
148 */
149 void rxe_add_key(void *elem, void *key);
150
151 /* remove elem from rb tree */
152 void rxe_drop_key(void *elem);
153
154 /* lookup an indexed object from index. takes a reference on object */
155 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
156
157 /* lookup keyed object from key. takes a reference on the object */
158 void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
159
160 /* cleanup an object when all references are dropped */
161 void rxe_elem_release(struct kref *kref);
162
163 /* take a reference on an object */
164 #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
165
166 /* drop a reference on an object */
167 #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
168
169 #endif /* RXE_POOL_H */