blob: 4d04830adcaefd2f0cc07d3b790d77a63e2c85c7 [file] [log] [blame]
Moni Shoua8700e3e2016-06-16 16:45:23 +03001/*
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
40enum rxe_pool_flags {
41 RXE_POOL_ATOMIC = BIT(0),
42 RXE_POOL_INDEX = BIT(1),
43 RXE_POOL_KEY = BIT(2),
44};
45
46enum rxe_elem_type {
47 RXE_TYPE_UC,
48 RXE_TYPE_PD,
49 RXE_TYPE_AH,
50 RXE_TYPE_SRQ,
51 RXE_TYPE_QP,
52 RXE_TYPE_CQ,
53 RXE_TYPE_MR,
54 RXE_TYPE_MW,
55 RXE_TYPE_MC_GRP,
56 RXE_TYPE_MC_ELEM,
57 RXE_NUM_TYPES, /* keep me last */
58};
59
60struct rxe_type_info {
61 char *name;
62 size_t size;
63 void (*cleanup)(void *obj);
64 enum rxe_pool_flags flags;
65 u32 max_index;
66 u32 min_index;
67 size_t key_offset;
68 size_t key_size;
69 struct kmem_cache *cache;
70};
71
72extern struct rxe_type_info rxe_type_info[];
73
74enum rxe_pool_state {
75 rxe_pool_invalid,
76 rxe_pool_valid,
77};
78
79struct rxe_pool_entry {
80 struct rxe_pool *pool;
81 struct kref ref_cnt;
82 struct list_head list;
83
84 /* only used if indexed or keyed */
85 struct rb_node node;
86 u32 index;
87};
88
89struct rxe_pool {
90 struct rxe_dev *rxe;
91 spinlock_t pool_lock; /* pool spinlock */
92 size_t elem_size;
93 struct kref ref_cnt;
94 void (*cleanup)(void *obj);
95 enum rxe_pool_state state;
96 enum rxe_pool_flags flags;
97 enum rxe_elem_type type;
98
99 unsigned int max_elem;
100 atomic_t num_elem;
101
102 /* only used if indexed or keyed */
103 struct rb_root tree;
104 unsigned long *table;
105 size_t table_size;
106 u32 max_index;
107 u32 min_index;
108 u32 last;
109 size_t key_offset;
110 size_t key_size;
111};
112
113/* initialize slab caches for managed objects */
114int rxe_cache_init(void);
115
116/* cleanup slab caches for managed objects */
117void rxe_cache_exit(void);
118
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
122 */
123int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
124 enum rxe_elem_type type, u32 max_elem);
125
126/* free resources from object pool */
127int rxe_pool_cleanup(struct rxe_pool *pool);
128
129/* allocate an object from pool */
130void *rxe_alloc(struct rxe_pool *pool);
131
132/* assign an index to an indexed object and insert object into
133 * pool's rb tree
134 */
135void rxe_add_index(void *elem);
136
137/* drop an index and remove object from rb tree */
138void rxe_drop_index(void *elem);
139
140/* assign a key to a keyed object and insert object into
141 * pool's rb tree
142 */
143void rxe_add_key(void *elem, void *key);
144
145/* remove elem from rb tree */
146void rxe_drop_key(void *elem);
147
148/* lookup an indexed object from index. takes a reference on object */
149void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
150
151/* lookup keyed object from key. takes a reference on the object */
152void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
153
154/* cleanup an object when all references are dropped */
155void rxe_elem_release(struct kref *kref);
156
157/* take a reference on an object */
158#define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
159
160/* drop a reference on an object */
161#define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
162
163#endif /* RXE_POOL_H */