162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
462306a36Sopenharmony_ci * Copyright (c) 2015 System Fabric Works, Inc. All rights reserved.
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#ifndef RXE_POOL_H
862306a36Sopenharmony_ci#define RXE_POOL_H
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cienum rxe_elem_type {
1162306a36Sopenharmony_ci	RXE_TYPE_UC,
1262306a36Sopenharmony_ci	RXE_TYPE_PD,
1362306a36Sopenharmony_ci	RXE_TYPE_AH,
1462306a36Sopenharmony_ci	RXE_TYPE_SRQ,
1562306a36Sopenharmony_ci	RXE_TYPE_QP,
1662306a36Sopenharmony_ci	RXE_TYPE_CQ,
1762306a36Sopenharmony_ci	RXE_TYPE_MR,
1862306a36Sopenharmony_ci	RXE_TYPE_MW,
1962306a36Sopenharmony_ci	RXE_NUM_TYPES,		/* keep me last */
2062306a36Sopenharmony_ci};
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_cistruct rxe_pool_elem {
2362306a36Sopenharmony_ci	struct rxe_pool		*pool;
2462306a36Sopenharmony_ci	void			*obj;
2562306a36Sopenharmony_ci	struct kref		ref_cnt;
2662306a36Sopenharmony_ci	struct list_head	list;
2762306a36Sopenharmony_ci	struct completion	complete;
2862306a36Sopenharmony_ci	u32			index;
2962306a36Sopenharmony_ci};
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_cistruct rxe_pool {
3262306a36Sopenharmony_ci	struct rxe_dev		*rxe;
3362306a36Sopenharmony_ci	const char		*name;
3462306a36Sopenharmony_ci	void			(*cleanup)(struct rxe_pool_elem *elem);
3562306a36Sopenharmony_ci	enum rxe_elem_type	type;
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci	unsigned int		max_elem;
3862306a36Sopenharmony_ci	atomic_t		num_elem;
3962306a36Sopenharmony_ci	size_t			elem_size;
4062306a36Sopenharmony_ci	size_t			elem_offset;
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci	struct xarray		xa;
4362306a36Sopenharmony_ci	struct xa_limit		limit;
4462306a36Sopenharmony_ci	u32			next;
4562306a36Sopenharmony_ci};
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci/* initialize a pool of objects with given limit on
4862306a36Sopenharmony_ci * number of elements. gets parameters from rxe_type_info
4962306a36Sopenharmony_ci * pool elements will be allocated out of a slab cache
5062306a36Sopenharmony_ci */
5162306a36Sopenharmony_civoid rxe_pool_init(struct rxe_dev *rxe, struct rxe_pool *pool,
5262306a36Sopenharmony_ci		  enum rxe_elem_type type);
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci/* free resources from object pool */
5562306a36Sopenharmony_civoid rxe_pool_cleanup(struct rxe_pool *pool);
5662306a36Sopenharmony_ci
5762306a36Sopenharmony_ci/* connect already allocated object to pool */
5862306a36Sopenharmony_ciint __rxe_add_to_pool(struct rxe_pool *pool, struct rxe_pool_elem *elem,
5962306a36Sopenharmony_ci				bool sleepable);
6062306a36Sopenharmony_ci#define rxe_add_to_pool(pool, obj) __rxe_add_to_pool(pool, &(obj)->elem, true)
6162306a36Sopenharmony_ci#define rxe_add_to_pool_ah(pool, obj, sleepable) __rxe_add_to_pool(pool, \
6262306a36Sopenharmony_ci				&(obj)->elem, sleepable)
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci/* lookup an indexed object from index. takes a reference on object */
6562306a36Sopenharmony_civoid *rxe_pool_get_index(struct rxe_pool *pool, u32 index);
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ciint __rxe_get(struct rxe_pool_elem *elem);
6862306a36Sopenharmony_ci#define rxe_get(obj) __rxe_get(&(obj)->elem)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ciint __rxe_put(struct rxe_pool_elem *elem);
7162306a36Sopenharmony_ci#define rxe_put(obj) __rxe_put(&(obj)->elem)
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciint __rxe_cleanup(struct rxe_pool_elem *elem, bool sleepable);
7462306a36Sopenharmony_ci#define rxe_cleanup(obj) __rxe_cleanup(&(obj)->elem, true)
7562306a36Sopenharmony_ci#define rxe_cleanup_ah(obj, sleepable) __rxe_cleanup(&(obj)->elem, sleepable)
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ci#define rxe_read(obj) kref_read(&(obj)->elem.ref_cnt)
7862306a36Sopenharmony_ci
7962306a36Sopenharmony_civoid __rxe_finalize(struct rxe_pool_elem *elem);
8062306a36Sopenharmony_ci#define rxe_finalize(obj) __rxe_finalize(&(obj)->elem)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#endif /* RXE_POOL_H */
83