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 #define RXE_POOL_ALIGN		(16)
11 #define RXE_POOL_CACHE_FLAGS	(0)
12 
13 enum rxe_pool_flags {
14 	RXE_POOL_ATOMIC		= BIT(0),
15 	RXE_POOL_INDEX		= BIT(1),
16 	RXE_POOL_KEY		= BIT(2),
17 	RXE_POOL_NO_ALLOC	= BIT(4),
18 };
19 
20 enum rxe_elem_type {
21 	RXE_TYPE_UC,
22 	RXE_TYPE_PD,
23 	RXE_TYPE_AH,
24 	RXE_TYPE_SRQ,
25 	RXE_TYPE_QP,
26 	RXE_TYPE_CQ,
27 	RXE_TYPE_MR,
28 	RXE_TYPE_MW,
29 	RXE_TYPE_MC_GRP,
30 	RXE_TYPE_MC_ELEM,
31 	RXE_NUM_TYPES,		/* keep me last */
32 };
33 
34 struct rxe_pool_entry;
35 
36 struct rxe_type_info {
37 	const char		*name;
38 	size_t			size;
39 	void			(*cleanup)(struct rxe_pool_entry *obj);
40 	enum rxe_pool_flags	flags;
41 	u32			max_index;
42 	u32			min_index;
43 	size_t			key_offset;
44 	size_t			key_size;
45 };
46 
47 extern struct rxe_type_info rxe_type_info[];
48 
49 enum rxe_pool_state {
50 	RXE_POOL_STATE_INVALID,
51 	RXE_POOL_STATE_VALID,
52 };
53 
54 struct rxe_pool_entry {
55 	struct rxe_pool		*pool;
56 	struct kref		ref_cnt;
57 	struct list_head	list;
58 
59 	/* only used if indexed or keyed */
60 	struct rb_node		node;
61 	u32			index;
62 };
63 
64 struct rxe_pool {
65 	struct rxe_dev		*rxe;
66 	rwlock_t		pool_lock; /* protects pool add/del/search */
67 	size_t			elem_size;
68 	struct kref		ref_cnt;
69 	void			(*cleanup)(struct rxe_pool_entry *obj);
70 	enum rxe_pool_state	state;
71 	enum rxe_pool_flags	flags;
72 	enum rxe_elem_type	type;
73 
74 	unsigned int		max_elem;
75 	atomic_t		num_elem;
76 
77 	/* only used if indexed or keyed */
78 	struct rb_root		tree;
79 	unsigned long		*table;
80 	size_t			table_size;
81 	u32			max_index;
82 	u32			min_index;
83 	u32			last;
84 	size_t			key_offset;
85 	size_t			key_size;
86 };
87 
88 /* initialize a pool of objects with given limit on
89  * number of elements. gets parameters from rxe_type_info
90  * pool elements will be allocated out of a slab cache
91  */
92 int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
93 		  enum rxe_elem_type type, u32 max_elem);
94 
95 /* free resources from object pool */
96 void rxe_pool_cleanup(struct rxe_pool *pool);
97 
98 /* allocate an object from pool */
99 void *rxe_alloc(struct rxe_pool *pool);
100 
101 /* connect already allocated object to pool */
102 int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
103 
104 /* assign an index to an indexed object and insert object into
105  *  pool's rb tree
106  */
107 void rxe_add_index(void *elem);
108 
109 /* drop an index and remove object from rb tree */
110 void rxe_drop_index(void *elem);
111 
112 /* assign a key to a keyed object and insert object into
113  *  pool's rb tree
114  */
115 void rxe_add_key(void *elem, void *key);
116 
117 /* remove elem from rb tree */
118 void rxe_drop_key(void *elem);
119 
120 /* lookup an indexed object from index. takes a reference on object */
121 void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
122 
123 /* lookup keyed object from key. takes a reference on the object */
124 void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
125 
126 /* cleanup an object when all references are dropped */
127 void rxe_elem_release(struct kref *kref);
128 
129 /* take a reference on an object */
130 #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
131 
132 /* drop a reference on an object */
133 #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
134 
135 #endif /* RXE_POOL_H */
136