xref: /linux/drivers/infiniband/sw/rxe/rxe_pool.h (revision 88cc77eb8bd05fd185a7d142dedc6e406c5c35b6)
163fa15dbSBob Pearson /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
28700e3e7SMoni Shoua /*
38700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
48700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
58700e3e7SMoni Shoua  */
68700e3e7SMoni Shoua 
78700e3e7SMoni Shoua #ifndef RXE_POOL_H
88700e3e7SMoni Shoua #define RXE_POOL_H
98700e3e7SMoni Shoua 
108700e3e7SMoni Shoua #define RXE_POOL_ALIGN		(16)
118700e3e7SMoni Shoua #define RXE_POOL_CACHE_FLAGS	(0)
128700e3e7SMoni Shoua 
138700e3e7SMoni Shoua enum rxe_pool_flags {
148700e3e7SMoni Shoua 	RXE_POOL_ATOMIC		= BIT(0),
158700e3e7SMoni Shoua 	RXE_POOL_INDEX		= BIT(1),
168700e3e7SMoni Shoua 	RXE_POOL_KEY		= BIT(2),
1721a428a0SLeon Romanovsky 	RXE_POOL_NO_ALLOC	= BIT(4),
188700e3e7SMoni Shoua };
198700e3e7SMoni Shoua 
208700e3e7SMoni Shoua enum rxe_elem_type {
218700e3e7SMoni Shoua 	RXE_TYPE_UC,
228700e3e7SMoni Shoua 	RXE_TYPE_PD,
238700e3e7SMoni Shoua 	RXE_TYPE_AH,
248700e3e7SMoni Shoua 	RXE_TYPE_SRQ,
258700e3e7SMoni Shoua 	RXE_TYPE_QP,
268700e3e7SMoni Shoua 	RXE_TYPE_CQ,
278700e3e7SMoni Shoua 	RXE_TYPE_MR,
288700e3e7SMoni Shoua 	RXE_TYPE_MW,
298700e3e7SMoni Shoua 	RXE_TYPE_MC_GRP,
308700e3e7SMoni Shoua 	RXE_TYPE_MC_ELEM,
318700e3e7SMoni Shoua 	RXE_NUM_TYPES,		/* keep me last */
328700e3e7SMoni Shoua };
338700e3e7SMoni Shoua 
3432404fb7SBart Van Assche struct rxe_pool_entry;
3532404fb7SBart Van Assche 
368700e3e7SMoni Shoua struct rxe_type_info {
372bec3badSBart Van Assche 	const char		*name;
388700e3e7SMoni Shoua 	size_t			size;
39b994d49eSBob Pearson 	size_t			elem_offset;
4032404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
418700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
428700e3e7SMoni Shoua 	u32			max_index;
438700e3e7SMoni Shoua 	u32			min_index;
448700e3e7SMoni Shoua 	size_t			key_offset;
458700e3e7SMoni Shoua 	size_t			key_size;
468700e3e7SMoni Shoua };
478700e3e7SMoni Shoua 
488700e3e7SMoni Shoua extern struct rxe_type_info rxe_type_info[];
498700e3e7SMoni Shoua 
508700e3e7SMoni Shoua enum rxe_pool_state {
513ccf19e2SParav Pandit 	RXE_POOL_STATE_INVALID,
523ccf19e2SParav Pandit 	RXE_POOL_STATE_VALID,
538700e3e7SMoni Shoua };
548700e3e7SMoni Shoua 
558700e3e7SMoni Shoua struct rxe_pool_entry {
568700e3e7SMoni Shoua 	struct rxe_pool		*pool;
578700e3e7SMoni Shoua 	struct kref		ref_cnt;
588700e3e7SMoni Shoua 	struct list_head	list;
598700e3e7SMoni Shoua 
60c06ee3a0SBob Pearson 	/* only used if keyed */
61c06ee3a0SBob Pearson 	struct rb_node		key_node;
62c06ee3a0SBob Pearson 
63c06ee3a0SBob Pearson 	/* only used if indexed */
64c06ee3a0SBob Pearson 	struct rb_node		index_node;
658700e3e7SMoni Shoua 	u32			index;
668700e3e7SMoni Shoua };
678700e3e7SMoni Shoua 
688700e3e7SMoni Shoua struct rxe_pool {
698700e3e7SMoni Shoua 	struct rxe_dev		*rxe;
7066d0f207SParav Pandit 	rwlock_t		pool_lock; /* protects pool add/del/search */
718700e3e7SMoni Shoua 	size_t			elem_size;
728700e3e7SMoni Shoua 	struct kref		ref_cnt;
7332404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
748700e3e7SMoni Shoua 	enum rxe_pool_state	state;
758700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
768700e3e7SMoni Shoua 	enum rxe_elem_type	type;
778700e3e7SMoni Shoua 
788700e3e7SMoni Shoua 	unsigned int		max_elem;
798700e3e7SMoni Shoua 	atomic_t		num_elem;
808700e3e7SMoni Shoua 
81c06ee3a0SBob Pearson 	/* only used if indexed */
82c06ee3a0SBob Pearson 	struct {
838700e3e7SMoni Shoua 		struct rb_root		tree;
848700e3e7SMoni Shoua 		unsigned long		*table;
858700e3e7SMoni Shoua 		size_t			table_size;
86c06ee3a0SBob Pearson 		u32			last;
878700e3e7SMoni Shoua 		u32			max_index;
888700e3e7SMoni Shoua 		u32			min_index;
89c06ee3a0SBob Pearson 	} index;
90c06ee3a0SBob Pearson 
91c06ee3a0SBob Pearson 	/* only used if keyed */
92c06ee3a0SBob Pearson 	struct {
93c06ee3a0SBob Pearson 		struct rb_root		tree;
948700e3e7SMoni Shoua 		size_t			key_offset;
958700e3e7SMoni Shoua 		size_t			key_size;
96c06ee3a0SBob Pearson 	} key;
978700e3e7SMoni Shoua };
988700e3e7SMoni Shoua 
998700e3e7SMoni Shoua /* initialize a pool of objects with given limit on
1008700e3e7SMoni Shoua  * number of elements. gets parameters from rxe_type_info
1018700e3e7SMoni Shoua  * pool elements will be allocated out of a slab cache
1028700e3e7SMoni Shoua  */
1038700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
1048700e3e7SMoni Shoua 		  enum rxe_elem_type type, u32 max_elem);
1058700e3e7SMoni Shoua 
1068700e3e7SMoni Shoua /* free resources from object pool */
1071ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool);
1088700e3e7SMoni Shoua 
109*88cc77ebSBob Pearson /* allocate an object from pool holding and not holding the pool lock */
110*88cc77ebSBob Pearson void *rxe_alloc_locked(struct rxe_pool *pool);
1118700e3e7SMoni Shoua 
112*88cc77ebSBob Pearson void *rxe_alloc(struct rxe_pool *pool);
1133853c35eSBob Pearson 
11421a428a0SLeon Romanovsky /* connect already allocated object to pool */
11591a42c5bSBob Pearson int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
11691a42c5bSBob Pearson 
11791a42c5bSBob Pearson #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->pelem)
11821a428a0SLeon Romanovsky 
1198700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into
120*88cc77ebSBob Pearson  *  pool's rb tree holding and not holding the pool_lock
1218700e3e7SMoni Shoua  */
122*88cc77ebSBob Pearson void __rxe_add_index_locked(struct rxe_pool_entry *elem);
123*88cc77ebSBob Pearson 
124*88cc77ebSBob Pearson #define rxe_add_index_locked(obj) __rxe_add_index_locked(&(obj)->pelem)
125*88cc77ebSBob Pearson 
12691a42c5bSBob Pearson void __rxe_add_index(struct rxe_pool_entry *elem);
12791a42c5bSBob Pearson 
12891a42c5bSBob Pearson #define rxe_add_index(obj) __rxe_add_index(&(obj)->pelem)
1298700e3e7SMoni Shoua 
1303853c35eSBob Pearson /* drop an index and remove object from rb tree
131*88cc77ebSBob Pearson  * holding and not holding the pool_lock
1323853c35eSBob Pearson  */
133*88cc77ebSBob Pearson void __rxe_drop_index_locked(struct rxe_pool_entry *elem);
134*88cc77ebSBob Pearson 
135*88cc77ebSBob Pearson #define rxe_drop_index_locked(obj) __rxe_drop_index_locked(&(obj)->pelem)
136*88cc77ebSBob Pearson 
13791a42c5bSBob Pearson void __rxe_drop_index(struct rxe_pool_entry *elem);
13891a42c5bSBob Pearson 
13991a42c5bSBob Pearson #define rxe_drop_index(obj) __rxe_drop_index(&(obj)->pelem)
1408700e3e7SMoni Shoua 
1418700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into
142*88cc77ebSBob Pearson  * pool's rb tree holding and not holding pool_lock
1438700e3e7SMoni Shoua  */
144*88cc77ebSBob Pearson void __rxe_add_key_locked(struct rxe_pool_entry *elem, void *key);
145*88cc77ebSBob Pearson 
146*88cc77ebSBob Pearson #define rxe_add_key_locked(obj, key) __rxe_add_key_locked(&(obj)->pelem, key)
147*88cc77ebSBob Pearson 
14891a42c5bSBob Pearson void __rxe_add_key(struct rxe_pool_entry *elem, void *key);
14991a42c5bSBob Pearson 
15091a42c5bSBob Pearson #define rxe_add_key(obj, key) __rxe_add_key(&(obj)->pelem, key)
1518700e3e7SMoni Shoua 
152*88cc77ebSBob Pearson /* remove elem from rb tree holding and not holding the pool_lock */
153*88cc77ebSBob Pearson void __rxe_drop_key_locked(struct rxe_pool_entry *elem);
1543853c35eSBob Pearson 
155*88cc77ebSBob Pearson #define rxe_drop_key_locked(obj) __rxe_drop_key_locked(&(obj)->pelem)
1563853c35eSBob Pearson 
15791a42c5bSBob Pearson void __rxe_drop_key(struct rxe_pool_entry *elem);
15891a42c5bSBob Pearson 
15991a42c5bSBob Pearson #define rxe_drop_key(obj) __rxe_drop_key(&(obj)->pelem)
1608700e3e7SMoni Shoua 
161*88cc77ebSBob Pearson /* lookup an indexed object from index holding and not holding the pool_lock.
1623853c35eSBob Pearson  * takes a reference on object
1633853c35eSBob Pearson  */
164*88cc77ebSBob Pearson void *rxe_pool_get_index_locked(struct rxe_pool *pool, u32 index);
165*88cc77ebSBob Pearson 
1668700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
1678700e3e7SMoni Shoua 
168*88cc77ebSBob Pearson /* lookup keyed object from key holding and not holding the pool_lock.
1693853c35eSBob Pearson  * takes a reference on the objecti
1703853c35eSBob Pearson  */
171*88cc77ebSBob Pearson void *rxe_pool_get_key_locked(struct rxe_pool *pool, void *key);
1728700e3e7SMoni Shoua 
173*88cc77ebSBob Pearson void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
1743853c35eSBob Pearson 
1758700e3e7SMoni Shoua /* cleanup an object when all references are dropped */
1768700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref);
1778700e3e7SMoni Shoua 
1788700e3e7SMoni Shoua /* take a reference on an object */
1798700e3e7SMoni Shoua #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
1808700e3e7SMoni Shoua 
1818700e3e7SMoni Shoua /* drop a reference on an object */
1828700e3e7SMoni Shoua #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
1838700e3e7SMoni Shoua 
1848700e3e7SMoni Shoua #endif /* RXE_POOL_H */
185