2 * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
3 * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
37 #define RXE_POOL_ALIGN (16)
38 #define RXE_POOL_CACHE_FLAGS (0)
41 RXE_POOL_ATOMIC = BIT(0),
42 RXE_POOL_INDEX = BIT(1),
43 RXE_POOL_KEY = BIT(2),
57 RXE_NUM_TYPES, /* keep me last */
60 struct rxe_type_info {
63 void (*cleanup)(void *obj);
64 enum rxe_pool_flags flags;
69 struct kmem_cache *cache;
72 extern struct rxe_type_info rxe_type_info[];
79 struct rxe_pool_entry {
80 struct rxe_pool *pool;
82 struct list_head list;
84 /* only used if indexed or keyed */
91 spinlock_t pool_lock; /* pool spinlock */
94 void (*cleanup)(void *obj);
95 enum rxe_pool_state state;
96 enum rxe_pool_flags flags;
97 enum rxe_elem_type type;
99 unsigned int max_elem;
102 /* only used if indexed or keyed */
104 unsigned long *table;
113 /* initialize slab caches for managed objects */
114 int rxe_cache_init(void);
116 /* cleanup slab caches for managed objects */
117 void rxe_cache_exit(void);
119 /* initialize a pool of objects with given limit on
120 * number of elements. gets parameters from rxe_type_info
121 * pool elements will be allocated out of a slab cache
123 int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
124 enum rxe_elem_type type, u32 max_elem);
126 /* free resources from object pool */
127 int rxe_pool_cleanup(struct rxe_pool *pool);
129 /* allocate an object from pool */
130 void *rxe_alloc(struct rxe_pool *pool);
132 /* assign an index to an indexed object and insert object into
135 void rxe_add_index(void *elem);
137 /* drop an index and remove object from rb tree */
138 void rxe_drop_index(void *elem);
140 /* assign a key to a keyed object and insert object into
143 void rxe_add_key(void *elem, void *key);
145 /* remove elem from rb tree */
146 void rxe_drop_key(void *elem);
148 /* lookup an indexed object from index. takes a reference on object */
149 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
151 /* lookup keyed object from key. takes a reference on the object */
152 void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
154 /* cleanup an object when all references are dropped */
155 void rxe_elem_release(struct kref *kref);
157 /* take a reference on an object */
158 #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
160 /* drop a reference on an object */
161 #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
163 #endif /* RXE_POOL_H */