xref: /linux/drivers/infiniband/sw/rxe/rxe_pool.h (revision 3197706abd053275d2a561cfb7dc8f6cfaf7d02c)
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 enum rxe_pool_flags {
113225717fSBob Pearson 	RXE_POOL_ALLOC		= BIT(1),
128700e3e7SMoni Shoua };
138700e3e7SMoni Shoua 
148700e3e7SMoni Shoua enum rxe_elem_type {
158700e3e7SMoni Shoua 	RXE_TYPE_UC,
168700e3e7SMoni Shoua 	RXE_TYPE_PD,
178700e3e7SMoni Shoua 	RXE_TYPE_AH,
188700e3e7SMoni Shoua 	RXE_TYPE_SRQ,
198700e3e7SMoni Shoua 	RXE_TYPE_QP,
208700e3e7SMoni Shoua 	RXE_TYPE_CQ,
218700e3e7SMoni Shoua 	RXE_TYPE_MR,
228700e3e7SMoni Shoua 	RXE_TYPE_MW,
238700e3e7SMoni Shoua 	RXE_NUM_TYPES,		/* keep me last */
248700e3e7SMoni Shoua };
258700e3e7SMoni Shoua 
2602827b67SBob Pearson struct rxe_pool_elem {
278700e3e7SMoni Shoua 	struct rxe_pool		*pool;
28b92d766cSBob Pearson 	void			*obj;
298700e3e7SMoni Shoua 	struct kref		ref_cnt;
308700e3e7SMoni Shoua 	struct list_head	list;
318700e3e7SMoni Shoua 	u32			index;
328700e3e7SMoni Shoua };
338700e3e7SMoni Shoua 
348700e3e7SMoni Shoua struct rxe_pool {
358700e3e7SMoni Shoua 	struct rxe_dev		*rxe;
36c95acedbSBob Pearson 	const char		*name;
37b4a47f68SBob Pearson 	void			(*cleanup)(struct rxe_pool_elem *elem);
388700e3e7SMoni Shoua 	enum rxe_pool_flags	flags;
398700e3e7SMoni Shoua 	enum rxe_elem_type	type;
408700e3e7SMoni Shoua 
418700e3e7SMoni Shoua 	unsigned int		max_elem;
428700e3e7SMoni Shoua 	atomic_t		num_elem;
43c95acedbSBob Pearson 	size_t			elem_size;
44c95acedbSBob Pearson 	size_t			elem_offset;
458700e3e7SMoni Shoua 
463225717fSBob Pearson 	struct xarray		xa;
473225717fSBob Pearson 	struct xa_limit		limit;
483225717fSBob Pearson 	u32			next;
498700e3e7SMoni Shoua };
508700e3e7SMoni Shoua 
518700e3e7SMoni Shoua /* initialize a pool of objects with given limit on
528700e3e7SMoni Shoua  * number of elements. gets parameters from rxe_type_info
538700e3e7SMoni Shoua  * pool elements will be allocated out of a slab cache
548700e3e7SMoni Shoua  */
553225717fSBob Pearson void rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
563ccffe8aSBob Pearson 		  enum rxe_elem_type type);
578700e3e7SMoni Shoua 
588700e3e7SMoni Shoua /* free resources from object pool */
591ceb25c8SYuval Shaia void rxe_pool_cleanup(struct rxe_pool *pool);
608700e3e7SMoni Shoua 
613c3e4d58SBob Pearson /* allocate an object from pool */
6288cc77ebSBob Pearson void *rxe_alloc(struct rxe_pool *pool);
633853c35eSBob Pearson 
6421a428a0SLeon Romanovsky /* connect already allocated object to pool */
6502827b67SBob Pearson int __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem);
6691a42c5bSBob Pearson 
6702827b67SBob Pearson #define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem)
6821a428a0SLeon Romanovsky 
693c3e4d58SBob Pearson /* lookup an indexed object from index. takes a reference on object */
708700e3e7SMoni Shoua void *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
718700e3e7SMoni Shoua 
723225717fSBob Pearson int __rxe_get(struct rxe_pool_elem *elem);
733225717fSBob Pearson 
74*3197706aSBob Pearson #define rxe_get(obj) __rxe_get(&(obj)->elem)
758700e3e7SMoni Shoua 
763225717fSBob Pearson int __rxe_put(struct rxe_pool_elem *elem);
773225717fSBob Pearson 
78*3197706aSBob Pearson #define rxe_put(obj) __rxe_put(&(obj)->elem)
798700e3e7SMoni Shoua 
80*3197706aSBob Pearson #define rxe_read(obj) kref_read(&(obj)->elem.ref_cnt)
81d5724055SBob Pearson 
828700e3e7SMoni Shoua #endif /* RXE_POOL_H */
83