1 /*-
2  * Copyright (c) 2020 Mellanox Technologies, Ltd.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 #ifndef	_LINUXKPI_LINUX_XARRAY_H_
29 #define	_LINUXKPI_LINUX_XARRAY_H_
30 
31 #include <linux/gfp.h>
32 #include <linux/radix-tree.h>
33 #include <linux/err.h>
34 
35 #include <sys/lock.h>
36 #include <sys/mutex.h>
37 
38 #define	XA_LIMIT(min, max) \
39     ({ CTASSERT((min) == 0); (uint32_t)(max); })
40 
41 #define	XA_FLAGS_ALLOC (1U << 0)
42 #define	XA_FLAGS_LOCK_IRQ (1U << 1)
43 #define	XA_FLAGS_ALLOC1 (1U << 2)
44 
45 #define	XA_ERROR(x) \
46 	ERR_PTR(x)
47 
48 #define	xa_limit_32b XA_LIMIT(0, 0xFFFFFFFF)
49 
50 #define	XA_ASSERT_LOCKED(xa) mtx_assert(&(xa)->mtx, MA_OWNED)
51 #define	xa_lock(xa) mtx_lock(&(xa)->mtx)
52 #define	xa_unlock(xa) mtx_unlock(&(xa)->mtx)
53 
54 struct xarray {
55 	struct radix_tree_root root;
56 	struct mtx mtx;		/* internal mutex */
57 	uint32_t flags;		/* see XA_FLAGS_XXX */
58 };
59 
60 /*
61  * Extensible arrays API implemented as a wrapper
62  * around the radix tree implementation.
63  */
64 void *xa_erase(struct xarray *, uint32_t);
65 void *xa_load(struct xarray *, uint32_t);
66 int xa_alloc(struct xarray *, uint32_t *, void *, uint32_t, gfp_t);
67 int xa_alloc_cyclic(struct xarray *, uint32_t *, void *, uint32_t, uint32_t *, gfp_t);
68 int xa_insert(struct xarray *, uint32_t, void *, gfp_t);
69 void *xa_store(struct xarray *, uint32_t, void *, gfp_t);
70 void xa_init_flags(struct xarray *, uint32_t);
71 bool xa_empty(struct xarray *);
72 void xa_destroy(struct xarray *);
73 void *xa_next(struct xarray *, unsigned long *, bool);
74 
75 #define	xa_for_each(xa, index, entry) \
76 	for ((entry) = NULL, (index) = 0; \
77 	     ((entry) = xa_next(xa, &index, (entry) != NULL)) != NULL; )
78 
79 /*
80  * Unlocked version of functions above.
81  */
82 void *__xa_erase(struct xarray *, uint32_t);
83 int __xa_alloc(struct xarray *, uint32_t *, void *, uint32_t, gfp_t);
84 int __xa_alloc_cyclic(struct xarray *, uint32_t *, void *, uint32_t, uint32_t *, gfp_t);
85 int __xa_insert(struct xarray *, uint32_t, void *, gfp_t);
86 void *__xa_store(struct xarray *, uint32_t, void *, gfp_t);
87 bool __xa_empty(struct xarray *);
88 void *__xa_next(struct xarray *, unsigned long *, bool);
89 
90 static inline int
91 xa_err(void *ptr)
92 {
93 	return (PTR_ERR_OR_ZERO(ptr));
94 }
95 
96 static inline void
97 xa_init(struct xarray *xa)
98 {
99 	xa_init_flags(xa, 0);
100 }
101 
102 static inline void *
103 xa_mk_value(unsigned long v)
104 {
105 	unsigned long r = (v << 1) | 1;
106 
107 	return ((void *)r);
108 }
109 
110 static inline bool
111 xa_is_value(const void *e)
112 {
113 	unsigned long v = (unsigned long)e;
114 
115 	return (v & 1);
116 }
117 
118 static inline unsigned long
119 xa_to_value(const void *e)
120 {
121 	unsigned long v = (unsigned long)e;
122 
123 	return (v >> 1);
124 }
125 #endif		/* _LINUXKPI_LINUX_XARRAY_H_ */
126