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 enum rxe_elem_type {
11 	RXE_TYPE_UC,
12 	RXE_TYPE_PD,
13 	RXE_TYPE_AH,
14 	RXE_TYPE_SRQ,
15 	RXE_TYPE_QP,
16 	RXE_TYPE_CQ,
17 	RXE_TYPE_MR,
18 	RXE_TYPE_MW,
19 	RXE_NUM_TYPES,		/* keep me last */
20 };
21 
22 struct rxe_pool_elem {
23 	struct rxe_pool		*pool;
24 	void			*obj;
25 	struct kref		ref_cnt;
26 	struct list_head	list;
27 	u32			index;
28 };
29 
30 struct rxe_pool {
31 	struct rxe_dev		*rxe;
32 	const char		*name;
33 	void			(*cleanup)(struct rxe_pool_elem *elem);
34 	enum rxe_elem_type	type;
35 
36 	unsigned int		max_elem;
37 	atomic_t		num_elem;
38 	size_t			elem_size;
39 	size_t			elem_offset;
40 
41 	struct xarray		xa;
42 	struct xa_limit		limit;
43 	u32			next;
44 };
45 
46 /* initialize a pool of objects with given limit on
47  * number of elements. gets parameters from rxe_type_info
48  * pool elements will be allocated out of a slab cache
49  */
50 void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
51 		  enum rxe_elem_type type);
52 
53 /* free resources from object pool */
54 void rxe_pool_cleanup(struct rxe_pool *pool);
55 
56 /* allocate an object from pool */
57 void *rxe_alloc(struct rxe_pool *pool);
58 
59 /* connect already allocated object to pool */
60 int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem);
61 
62 #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem)
63 
64 /* lookup an indexed object from index. takes a reference on object */
65 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
66 
67 int __rxe_get(struct rxe_pool_elem *elem);
68 
69 #define rxe_get(obj) __rxe_get(&(obj)->elem)
70 
71 int __rxe_put(struct rxe_pool_elem *elem);
72 
73 #define rxe_put(obj) __rxe_put(&(obj)->elem)
74 
75 #define rxe_read(obj) kref_read(&(obj)->elem.ref_cnt)
76 
77 #endif /* RXE_POOL_H */
78