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 	struct completion	complete;
28 	u32			index;
29 };
30 
31 struct rxe_pool {
32 	struct rxe_dev		*rxe;
33 	const char		*name;
34 	void			(*cleanup)(struct rxe_pool_elem *elem);
35 	enum rxe_elem_type	type;
36 
37 	unsigned int		max_elem;
38 	atomic_t		num_elem;
39 	size_t			elem_size;
40 	size_t			elem_offset;
41 
42 	struct xarray		xa;
43 	struct xa_limit		limit;
44 	u32			next;
45 };
46 
47 /* initialize a pool of objects with given limit on
48  * number of elements. gets parameters from rxe_type_info
49  * pool elements will be allocated out of a slab cache
50  */
51 void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
52 		  enum rxe_elem_type type);
53 
54 /* free resources from object pool */
55 void rxe_pool_cleanup(struct rxe_pool *pool);
56 
57 /* connect already allocated object to pool */
58 int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem,
59 				bool sleepable);
60 #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem, true)
61 #define rxe_add_to_pool_ah(pool, obj, sleepable) __rxe_add_to_pool(pool, \
62 				&(obj)->elem, sleepable)
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 #define rxe_get(obj) __rxe_get(&(obj)->elem)
69 
70 int __rxe_put(struct rxe_pool_elem *elem);
71 #define rxe_put(obj) __rxe_put(&(obj)->elem)
72 
73 int __rxe_cleanup(struct rxe_pool_elem *elem, bool sleepable);
74 #define rxe_cleanup(obj) __rxe_cleanup(&(obj)->elem, true)
75 #define rxe_cleanup_ah(obj, sleepable) __rxe_cleanup(&(obj)->elem, sleepable)
76 
77 #define rxe_read(obj) kref_read(&(obj)->elem.ref_cnt)
78 
79 void __rxe_finalize(struct rxe_pool_elem *elem);
80 #define rxe_finalize(obj) __rxe_finalize(&(obj)->elem)
81 
82 #endif /* RXE_POOL_H */
83