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