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