xref: /linux/drivers/infiniband/sw/rxe/rxe_pool.h (revision 2bec3baded4ff46f51c1810e0dd8dfda55f05451)
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 
608700e3e7SMoni Shoua struct rxe_type_info {
61*2bec3badSBart Van Assche 	const char		*name;
628700e3e7SMoni Shoua 	size_t			size;
638700e3e7SMoni Shoua 	void			(*cleanup)(void *obj);
648700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
658700e3e7SMoni Shoua 	u32			max_index;
668700e3e7SMoni Shoua 	u32			min_index;
678700e3e7SMoni Shoua 	size_t			key_offset;
688700e3e7SMoni Shoua 	size_t			key_size;
698700e3e7SMoni Shoua 	struct kmem_cache	*cache;
708700e3e7SMoni Shoua };
718700e3e7SMoni Shoua 
728700e3e7SMoni Shoua extern struct rxe_type_info rxe_type_info[];
738700e3e7SMoni Shoua 
748700e3e7SMoni Shoua enum rxe_pool_state {
758700e3e7SMoni Shoua 	rxe_pool_invalid,
768700e3e7SMoni Shoua 	rxe_pool_valid,
778700e3e7SMoni Shoua };
788700e3e7SMoni Shoua 
798700e3e7SMoni Shoua struct rxe_pool_entry {
808700e3e7SMoni Shoua 	struct rxe_pool		*pool;
818700e3e7SMoni Shoua 	struct kref		ref_cnt;
828700e3e7SMoni Shoua 	struct list_head	list;
838700e3e7SMoni Shoua 
848700e3e7SMoni Shoua 	/* only used if indexed or keyed */
858700e3e7SMoni Shoua 	struct rb_node		node;
868700e3e7SMoni Shoua 	u32			index;
878700e3e7SMoni Shoua };
888700e3e7SMoni Shoua 
898700e3e7SMoni Shoua struct rxe_pool {
908700e3e7SMoni Shoua 	struct rxe_dev		*rxe;
918700e3e7SMoni Shoua 	spinlock_t              pool_lock; /* pool spinlock */
928700e3e7SMoni Shoua 	size_t			elem_size;
938700e3e7SMoni Shoua 	struct kref		ref_cnt;
948700e3e7SMoni Shoua 	void			(*cleanup)(void *obj);
958700e3e7SMoni Shoua 	enum rxe_pool_state	state;
968700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
978700e3e7SMoni Shoua 	enum rxe_elem_type	type;
988700e3e7SMoni Shoua 
998700e3e7SMoni Shoua 	unsigned int		max_elem;
1008700e3e7SMoni Shoua 	atomic_t		num_elem;
1018700e3e7SMoni Shoua 
1028700e3e7SMoni Shoua 	/* only used if indexed or keyed */
1038700e3e7SMoni Shoua 	struct rb_root		tree;
1048700e3e7SMoni Shoua 	unsigned long		*table;
1058700e3e7SMoni Shoua 	size_t			table_size;
1068700e3e7SMoni Shoua 	u32			max_index;
1078700e3e7SMoni Shoua 	u32			min_index;
1088700e3e7SMoni Shoua 	u32			last;
1098700e3e7SMoni Shoua 	size_t			key_offset;
1108700e3e7SMoni Shoua 	size_t			key_size;
1118700e3e7SMoni Shoua };
1128700e3e7SMoni Shoua 
1138700e3e7SMoni Shoua /* initialize slab caches for managed objects */
1148700e3e7SMoni Shoua int rxe_cache_init(void);
1158700e3e7SMoni Shoua 
1168700e3e7SMoni Shoua /* cleanup slab caches for managed objects */
1178700e3e7SMoni Shoua void rxe_cache_exit(void);
1188700e3e7SMoni Shoua 
1198700e3e7SMoni Shoua /* initialize a pool of objects with given limit on
1208700e3e7SMoni Shoua  * number of elements. gets parameters from rxe_type_info
1218700e3e7SMoni Shoua  * pool elements will be allocated out of a slab cache
1228700e3e7SMoni Shoua  */
1238700e3e7SMoni Shoua int rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
1248700e3e7SMoni Shoua 		  enum rxe_elem_type type, u32 max_elem);
1258700e3e7SMoni Shoua 
1268700e3e7SMoni Shoua /* free resources from object pool */
1278700e3e7SMoni Shoua int rxe_pool_cleanup(struct rxe_pool *pool);
1288700e3e7SMoni Shoua 
1298700e3e7SMoni Shoua /* allocate an object from pool */
1308700e3e7SMoni Shoua void *rxe_alloc(struct rxe_pool *pool);
1318700e3e7SMoni Shoua 
1328700e3e7SMoni Shoua /* assign an index to an indexed object and insert object into
1338700e3e7SMoni Shoua  *  pool's rb tree
1348700e3e7SMoni Shoua  */
1358700e3e7SMoni Shoua void rxe_add_index(void *elem);
1368700e3e7SMoni Shoua 
1378700e3e7SMoni Shoua /* drop an index and remove object from rb tree */
1388700e3e7SMoni Shoua void rxe_drop_index(void *elem);
1398700e3e7SMoni Shoua 
1408700e3e7SMoni Shoua /* assign a key to a keyed object and insert object into
1418700e3e7SMoni Shoua  *  pool's rb tree
1428700e3e7SMoni Shoua  */
1438700e3e7SMoni Shoua void rxe_add_key(void *elem, void *key);
1448700e3e7SMoni Shoua 
1458700e3e7SMoni Shoua /* remove elem from rb tree */
1468700e3e7SMoni Shoua void rxe_drop_key(void *elem);
1478700e3e7SMoni Shoua 
1488700e3e7SMoni Shoua /* lookup an indexed object from index. takes a reference on object */
1498700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
1508700e3e7SMoni Shoua 
1518700e3e7SMoni Shoua /* lookup keyed object from key. takes a reference on the object */
1528700e3e7SMoni Shoua void *rxe_pool_get_key(struct rxe_pool *pool, void *key);
1538700e3e7SMoni Shoua 
1548700e3e7SMoni Shoua /* cleanup an object when all references are dropped */
1558700e3e7SMoni Shoua void rxe_elem_release(struct kref *kref);
1568700e3e7SMoni Shoua 
1578700e3e7SMoni Shoua /* take a reference on an object */
1588700e3e7SMoni Shoua #define rxe_add_ref(elem) kref_get(&(elem)->pelem.ref_cnt)
1598700e3e7SMoni Shoua 
1608700e3e7SMoni Shoua /* drop a reference on an object */
1618700e3e7SMoni Shoua #define rxe_drop_ref(elem) kref_put(&(elem)->pelem.ref_cnt, rxe_elem_release)
1628700e3e7SMoni Shoua 
1638700e3e7SMoni Shoua #endif /* RXE_POOL_H */
164