xref: /linux/drivers/infiniband/sw/rxe/rxe_pool.h (revision 3ccf19e25adf2d3b9c5f1e65b950b6d223e1c4c0)
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),
448700e3e7SMoni Shoua };
458700e3e7SMoni Shoua 
468700e3e7SMoni Shoua enum rxe_elem_type {
478700e3e7SMoni Shoua 	RXE_TYPE_UC,
488700e3e7SMoni Shoua 	RXE_TYPE_PD,
498700e3e7SMoni Shoua 	RXE_TYPE_AH,
508700e3e7SMoni Shoua 	RXE_TYPE_SRQ,
518700e3e7SMoni Shoua 	RXE_TYPE_QP,
528700e3e7SMoni Shoua 	RXE_TYPE_CQ,
538700e3e7SMoni Shoua 	RXE_TYPE_MR,
548700e3e7SMoni Shoua 	RXE_TYPE_MW,
558700e3e7SMoni Shoua 	RXE_TYPE_MC_GRP,
568700e3e7SMoni Shoua 	RXE_TYPE_MC_ELEM,
578700e3e7SMoni Shoua 	RXE_NUM_TYPES,		/* keep me last */
588700e3e7SMoni Shoua };
598700e3e7SMoni Shoua 
6032404fb7SBart Van Assche struct rxe_pool_entry;
6132404fb7SBart Van Assche 
628700e3e7SMoni Shoua struct rxe_type_info {
632bec3badSBart Van Assche 	const char		*name;
648700e3e7SMoni Shoua 	size_t			size;
6532404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
668700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
678700e3e7SMoni Shoua 	u32			max_index;
688700e3e7SMoni Shoua 	u32			min_index;
698700e3e7SMoni Shoua 	size_t			key_offset;
708700e3e7SMoni Shoua 	size_t			key_size;
718700e3e7SMoni Shoua 	struct kmem_cache	*cache;
728700e3e7SMoni Shoua };
738700e3e7SMoni Shoua 
748700e3e7SMoni Shoua extern struct rxe_type_info rxe_type_info[];
758700e3e7SMoni Shoua 
768700e3e7SMoni Shoua enum rxe_pool_state {
77*3ccf19e2SParav Pandit 	RXE_POOL_STATE_INVALID,
78*3ccf19e2SParav Pandit 	RXE_POOL_STATE_VALID,
798700e3e7SMoni Shoua };
808700e3e7SMoni Shoua 
818700e3e7SMoni Shoua struct rxe_pool_entry {
828700e3e7SMoni Shoua 	struct rxe_pool		*pool;
838700e3e7SMoni Shoua 	struct kref		ref_cnt;
848700e3e7SMoni Shoua 	struct list_head	list;
858700e3e7SMoni Shoua 
868700e3e7SMoni Shoua 	/* only used if indexed or keyed */
878700e3e7SMoni Shoua 	struct rb_node		node;
888700e3e7SMoni Shoua 	u32			index;
898700e3e7SMoni Shoua };
908700e3e7SMoni Shoua 
918700e3e7SMoni Shoua struct rxe_pool {
928700e3e7SMoni Shoua 	struct rxe_dev		*rxe;
9366d0f207SParav Pandit 	rwlock_t		pool_lock; /* protects pool add/del/search */
948700e3e7SMoni Shoua 	size_t			elem_size;
958700e3e7SMoni Shoua 	struct kref		ref_cnt;
9632404fb7SBart Van Assche 	void			(*cleanup)(struct rxe_pool_entry *obj);
978700e3e7SMoni Shoua 	enum rxe_pool_state	state;
988700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
998700e3e7SMoni Shoua 	enum rxe_elem_type	type;
1008700e3e7SMoni Shoua 
1018700e3e7SMoni Shoua 	unsigned int		max_elem;
1028700e3e7SMoni Shoua 	atomic_t		num_elem;
1038700e3e7SMoni Shoua 
1048700e3e7SMoni Shoua 	/* only used if indexed or keyed */
1058700e3e7SMoni Shoua 	struct rb_root		tree;
1068700e3e7SMoni Shoua 	unsigned long		*table;
1078700e3e7SMoni Shoua 	size_t			table_size;
1088700e3e7SMoni Shoua 	u32			max_index;
1098700e3e7SMoni Shoua 	u32			min_index;
1108700e3e7SMoni Shoua 	u32			last;
1118700e3e7SMoni Shoua 	size_t			key_offset;
1128700e3e7SMoni Shoua 	size_t			key_size;
1138700e3e7SMoni Shoua };
1148700e3e7SMoni Shoua 
1158700e3e7SMoni Shoua /* initialize slab caches for managed objects */
1168700e3e7SMoni Shoua int rxe_cache_init(void);
1178700e3e7SMoni Shoua 
1188700e3e7SMoni Shoua /* cleanup slab caches for managed objects */
1198700e3e7SMoni Shoua void rxe_cache_exit(void);
1208700e3e7SMoni Shoua 
1218700e3e7SMoni Shoua /* initialize a pool of objects with given limit on
1228700e3e7SMoni Shoua  * number of elements. gets parameters from rxe_type_info
1238700e3e7SMoni Shoua  * pool elements will be allocated out of a slab cache
1248700e3e7SMoni Shoua  */
1258700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
1268700e3e7SMoni Shoua 		  enum rxe_elem_type type, u32 max_elem);
1278700e3e7SMoni Shoua 
1288700e3e7SMoni Shoua /* free resources from object pool */
1298700e3e7SMoni Shoua int rxe_pool_cleanup(struct rxe_pool *pool);
1308700e3e7SMoni Shoua 
1318700e3e7SMoni Shoua /* allocate an object from pool */
1328700e3e7SMoni Shoua void *rxe_alloc(struct rxe_pool *pool);
1338700e3e7SMoni Shoua 
1348700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into
1358700e3e7SMoni Shoua  *  pool's rb tree
1368700e3e7SMoni Shoua  */
1378700e3e7SMoni Shoua void rxe_add_index(void *elem);
1388700e3e7SMoni Shoua 
1398700e3e7SMoni Shoua /* drop an index and remove object from rb tree */
1408700e3e7SMoni Shoua void rxe_drop_index(void *elem);
1418700e3e7SMoni Shoua 
1428700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into
1438700e3e7SMoni Shoua  *  pool's rb tree
1448700e3e7SMoni Shoua  */
1458700e3e7SMoni Shoua void rxe_add_key(void *elem, void *key);
1468700e3e7SMoni Shoua 
1478700e3e7SMoni Shoua /* remove elem from rb tree */
1488700e3e7SMoni Shoua void rxe_drop_key(void *elem);
1498700e3e7SMoni Shoua 
1508700e3e7SMoni Shoua /* lookup an indexed object from index. takes a reference on object */
1518700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
1528700e3e7SMoni Shoua 
1538700e3e7SMoni Shoua /* lookup keyed object from key. takes a reference on the object */
1548700e3e7SMoni Shoua void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
1558700e3e7SMoni Shoua 
1568700e3e7SMoni Shoua /* cleanup an object when all references are dropped */
1578700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref);
1588700e3e7SMoni Shoua 
1598700e3e7SMoni Shoua /* take a reference on an object */
1608700e3e7SMoni Shoua #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
1618700e3e7SMoni Shoua 
1628700e3e7SMoni Shoua /* drop a reference on an object */
1638700e3e7SMoni Shoua #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
1648700e3e7SMoni Shoua 
1658700e3e7SMoni Shoua #endif /* RXE_POOL_H */
166