xref: /linux/drivers/infiniband/sw/rxe/rxe_pool.h (revision 21a428a019c9a6d133e745b529b9bf18c1187e70)
18700e3e7SMoni Shoua /*
28700e3e7SMoni Shoua  * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
38700e3e7SMoni Shoua  * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
48700e3e7SMoni Shoua  *
58700e3e7SMoni Shoua  * This software is available to you under a choice of one of two
68700e3e7SMoni Shoua  * licenses.  You may choose to be licensed under the terms of the GNU
78700e3e7SMoni Shoua  * General Public License (GPL) Version 2, available from the file
88700e3e7SMoni Shoua  * COPYING in the main directory of this source tree, or the
98700e3e7SMoni Shoua  * OpenIB.org BSD license below:
108700e3e7SMoni Shoua  *
118700e3e7SMoni Shoua  *	   Redistribution and use in source and binary forms, with or
128700e3e7SMoni Shoua  *	   without modification, are permitted provided that the following
138700e3e7SMoni Shoua  *	   conditions are met:
148700e3e7SMoni Shoua  *
158700e3e7SMoni Shoua  *		- Redistributions of source code must retain the above
168700e3e7SMoni Shoua  *		  copyright notice, this list of conditions and the following
178700e3e7SMoni Shoua  *		  disclaimer.
188700e3e7SMoni Shoua  *
198700e3e7SMoni Shoua  *		- Redistributions in binary form must reproduce the above
208700e3e7SMoni Shoua  *		  copyright notice, this list of conditions and the following
218700e3e7SMoni Shoua  *		  disclaimer in the documentation and/or other materials
228700e3e7SMoni Shoua  *		  provided with the distribution.
238700e3e7SMoni Shoua  *
248700e3e7SMoni Shoua  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
258700e3e7SMoni Shoua  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
268700e3e7SMoni Shoua  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
278700e3e7SMoni Shoua  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
288700e3e7SMoni Shoua  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
298700e3e7SMoni Shoua  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
308700e3e7SMoni Shoua  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
318700e3e7SMoni Shoua  * SOFTWARE.
328700e3e7SMoni Shoua  */
338700e3e7SMoni Shoua 
348700e3e7SMoni Shoua #ifndef RXE_POOL_H
358700e3e7SMoni Shoua #define RXE_POOL_H
368700e3e7SMoni Shoua 
378700e3e7SMoni Shoua #define RXE_POOL_ALIGN		(16)
388700e3e7SMoni Shoua #define RXE_POOL_CACHE_FLAGS	(0)
398700e3e7SMoni Shoua 
408700e3e7SMoni Shoua enum rxe_pool_flags {
418700e3e7SMoni Shoua 	RXE_POOL_ATOMIC		= BIT(0),
428700e3e7SMoni Shoua 	RXE_POOL_INDEX		= BIT(1),
438700e3e7SMoni Shoua 	RXE_POOL_KEY		= BIT(2),
44*21a428a0SLeon Romanovsky 	RXE_POOL_NO_ALLOC	= BIT(4),
458700e3e7SMoni Shoua };
468700e3e7SMoni Shoua 
478700e3e7SMoni Shoua enum rxe_elem_type {
488700e3e7SMoni Shoua 	RXE_TYPE_UC,
498700e3e7SMoni Shoua 	RXE_TYPE_PD,
508700e3e7SMoni Shoua 	RXE_TYPE_AH,
518700e3e7SMoni Shoua 	RXE_TYPE_SRQ,
528700e3e7SMoni Shoua 	RXE_TYPE_QP,
538700e3e7SMoni Shoua 	RXE_TYPE_CQ,
548700e3e7SMoni Shoua 	RXE_TYPE_MR,
558700e3e7SMoni Shoua 	RXE_TYPE_MW,
568700e3e7SMoni Shoua 	RXE_TYPE_MC_GRP,
578700e3e7SMoni Shoua 	RXE_TYPE_MC_ELEM,
588700e3e7SMoni Shoua 	RXE_NUM_TYPES,		/* keep me last */
598700e3e7SMoni Shoua };
608700e3e7SMoni Shoua 
6132404fb7SBart Van Assche struct rxe_pool_entry;
6232404fb7SBart Van Assche 
638700e3e7SMoni Shoua struct rxe_type_info {
642bec3badSBart Van Assche 	const char		*name;
658700e3e7SMoni Shoua 	size_t			size;
6632404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
678700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
688700e3e7SMoni Shoua 	u32			max_index;
698700e3e7SMoni Shoua 	u32			min_index;
708700e3e7SMoni Shoua 	size_t			key_offset;
718700e3e7SMoni Shoua 	size_t			key_size;
728700e3e7SMoni Shoua 	struct kmem_cache	*cache;
738700e3e7SMoni Shoua };
748700e3e7SMoni Shoua 
758700e3e7SMoni Shoua extern struct rxe_type_info rxe_type_info[];
768700e3e7SMoni Shoua 
778700e3e7SMoni Shoua enum rxe_pool_state {
783ccf19e2SParav Pandit 	RXE_POOL_STATE_INVALID,
793ccf19e2SParav Pandit 	RXE_POOL_STATE_VALID,
808700e3e7SMoni Shoua };
818700e3e7SMoni Shoua 
828700e3e7SMoni Shoua struct rxe_pool_entry {
838700e3e7SMoni Shoua 	struct rxe_pool		*pool;
848700e3e7SMoni Shoua 	struct kref		ref_cnt;
858700e3e7SMoni Shoua 	struct list_head	list;
868700e3e7SMoni Shoua 
878700e3e7SMoni Shoua 	/* only used if indexed or keyed */
888700e3e7SMoni Shoua 	struct rb_node		node;
898700e3e7SMoni Shoua 	u32			index;
908700e3e7SMoni Shoua };
918700e3e7SMoni Shoua 
928700e3e7SMoni Shoua struct rxe_pool {
938700e3e7SMoni Shoua 	struct rxe_dev		*rxe;
9466d0f207SParav Pandit 	rwlock_t		pool_lock; /* protects pool add/del/search */
958700e3e7SMoni Shoua 	size_t			elem_size;
968700e3e7SMoni Shoua 	struct kref		ref_cnt;
9732404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
988700e3e7SMoni Shoua 	enum rxe_pool_state	state;
998700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
1008700e3e7SMoni Shoua 	enum rxe_elem_type	type;
1018700e3e7SMoni Shoua 
1028700e3e7SMoni Shoua 	unsigned int		max_elem;
1038700e3e7SMoni Shoua 	atomic_t		num_elem;
1048700e3e7SMoni Shoua 
1058700e3e7SMoni Shoua 	/* only used if indexed or keyed */
1068700e3e7SMoni Shoua 	struct rb_root		tree;
1078700e3e7SMoni Shoua 	unsigned long		*table;
1088700e3e7SMoni Shoua 	size_t			table_size;
1098700e3e7SMoni Shoua 	u32			max_index;
1108700e3e7SMoni Shoua 	u32			min_index;
1118700e3e7SMoni Shoua 	u32			last;
1128700e3e7SMoni Shoua 	size_t			key_offset;
1138700e3e7SMoni Shoua 	size_t			key_size;
1148700e3e7SMoni Shoua };
1158700e3e7SMoni Shoua 
1168700e3e7SMoni Shoua /* initialize slab caches for managed objects */
1178700e3e7SMoni Shoua int rxe_cache_init(void);
1188700e3e7SMoni Shoua 
1198700e3e7SMoni Shoua /* cleanup slab caches for managed objects */
1208700e3e7SMoni Shoua void rxe_cache_exit(void);
1218700e3e7SMoni Shoua 
1228700e3e7SMoni Shoua /* initialize a pool of objects with given limit on
1238700e3e7SMoni Shoua  * number of elements. gets parameters from rxe_type_info
1248700e3e7SMoni Shoua  * pool elements will be allocated out of a slab cache
1258700e3e7SMoni Shoua  */
1268700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
1278700e3e7SMoni Shoua 		  enum rxe_elem_type type, u32 max_elem);
1288700e3e7SMoni Shoua 
1298700e3e7SMoni Shoua /* free resources from object pool */
1301ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool);
1318700e3e7SMoni Shoua 
1328700e3e7SMoni Shoua /* allocate an object from pool */
1338700e3e7SMoni Shoua void *rxe_alloc(struct rxe_pool *pool);
1348700e3e7SMoni Shoua 
135*21a428a0SLeon Romanovsky /* connect already allocated object to pool */
136*21a428a0SLeon Romanovsky int rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_entry *elem);
137*21a428a0SLeon Romanovsky 
1388700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into
1398700e3e7SMoni Shoua  *  pool's rb tree
1408700e3e7SMoni Shoua  */
1418700e3e7SMoni Shoua void rxe_add_index(void *elem);
1428700e3e7SMoni Shoua 
1438700e3e7SMoni Shoua /* drop an index and remove object from rb tree */
1448700e3e7SMoni Shoua void rxe_drop_index(void *elem);
1458700e3e7SMoni Shoua 
1468700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into
1478700e3e7SMoni Shoua  *  pool's rb tree
1488700e3e7SMoni Shoua  */
1498700e3e7SMoni Shoua void rxe_add_key(void *elem, void *key);
1508700e3e7SMoni Shoua 
1518700e3e7SMoni Shoua /* remove elem from rb tree */
1528700e3e7SMoni Shoua void rxe_drop_key(void *elem);
1538700e3e7SMoni Shoua 
1548700e3e7SMoni Shoua /* lookup an indexed object from index. takes a reference on object */
1558700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
1568700e3e7SMoni Shoua 
1578700e3e7SMoni Shoua /* lookup keyed object from key. takes a reference on the object */
1588700e3e7SMoni Shoua void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
1598700e3e7SMoni Shoua 
1608700e3e7SMoni Shoua /* cleanup an object when all references are dropped */
1618700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref);
1628700e3e7SMoni Shoua 
1638700e3e7SMoni Shoua /* take a reference on an object */
1648700e3e7SMoni Shoua #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
1658700e3e7SMoni Shoua 
1668700e3e7SMoni Shoua /* drop a reference on an object */
1678700e3e7SMoni Shoua #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
1688700e3e7SMoni Shoua 
1698700e3e7SMoni Shoua #endif /* RXE_POOL_H */
170