1d96e5996SHans Petter Selasky /*-
2d96e5996SHans Petter Selasky * Copyright (c) 2020 Mellanox Technologies, Ltd.
3d96e5996SHans Petter Selasky * All rights reserved.
4d96e5996SHans Petter Selasky *
5d96e5996SHans Petter Selasky * Redistribution and use in source and binary forms, with or without
6d96e5996SHans Petter Selasky * modification, are permitted provided that the following conditions
7d96e5996SHans Petter Selasky * are met:
8d96e5996SHans Petter Selasky * 1. Redistributions of source code must retain the above copyright
9d96e5996SHans Petter Selasky * notice unmodified, this list of conditions, and the following
10d96e5996SHans Petter Selasky * disclaimer.
11d96e5996SHans Petter Selasky * 2. Redistributions in binary form must reproduce the above copyright
12d96e5996SHans Petter Selasky * notice, this list of conditions and the following disclaimer in the
13d96e5996SHans Petter Selasky * documentation and/or other materials provided with the distribution.
14d96e5996SHans Petter Selasky *
15d96e5996SHans Petter Selasky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16d96e5996SHans Petter Selasky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17d96e5996SHans Petter Selasky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18d96e5996SHans Petter Selasky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19d96e5996SHans Petter Selasky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20d96e5996SHans Petter Selasky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21d96e5996SHans Petter Selasky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22d96e5996SHans Petter Selasky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23d96e5996SHans Petter Selasky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24d96e5996SHans Petter Selasky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25d96e5996SHans Petter Selasky */
26307f78f3SVladimir Kondratyev #ifndef _LINUXKPI_LINUX_XARRAY_H_
27307f78f3SVladimir Kondratyev #define _LINUXKPI_LINUX_XARRAY_H_
28d96e5996SHans Petter Selasky
29d96e5996SHans Petter Selasky #include <linux/gfp.h>
30d96e5996SHans Petter Selasky #include <linux/radix-tree.h>
31d96e5996SHans Petter Selasky #include <linux/err.h>
324b0552d5SJean-Sébastien Pédron #include <linux/kconfig.h>
33d96e5996SHans Petter Selasky
34d96e5996SHans Petter Selasky #include <sys/lock.h>
35d96e5996SHans Petter Selasky #include <sys/mutex.h>
36d96e5996SHans Petter Selasky
37d96e5996SHans Petter Selasky #define XA_LIMIT(min, max) \
38d96e5996SHans Petter Selasky ({ CTASSERT((min) == 0); (uint32_t)(max); })
39d96e5996SHans Petter Selasky
40d96e5996SHans Petter Selasky #define XA_FLAGS_ALLOC (1U << 0)
41d96e5996SHans Petter Selasky #define XA_FLAGS_LOCK_IRQ (1U << 1)
42e705066cSVladimir Kondratyev #define XA_FLAGS_ALLOC1 (1U << 2)
43d96e5996SHans Petter Selasky
44d96e5996SHans Petter Selasky #define XA_ERROR(x) \
45d96e5996SHans Petter Selasky ERR_PTR(x)
46d96e5996SHans Petter Selasky
475542309eSJean-Sébastien Pédron #define xa_is_err(x) \
485542309eSJean-Sébastien Pédron IS_ERR(x)
495542309eSJean-Sébastien Pédron
50d96e5996SHans Petter Selasky #define xa_limit_32b XA_LIMIT(0, 0xFFFFFFFF)
51d96e5996SHans Petter Selasky
52d96e5996SHans Petter Selasky #define XA_ASSERT_LOCKED(xa) mtx_assert(&(xa)->mtx, MA_OWNED)
53d96e5996SHans Petter Selasky #define xa_lock(xa) mtx_lock(&(xa)->mtx)
54d96e5996SHans Petter Selasky #define xa_unlock(xa) mtx_unlock(&(xa)->mtx)
55d96e5996SHans Petter Selasky
56d96e5996SHans Petter Selasky struct xarray {
57d96e5996SHans Petter Selasky struct radix_tree_root root;
58d96e5996SHans Petter Selasky struct mtx mtx; /* internal mutex */
59e705066cSVladimir Kondratyev uint32_t flags; /* see XA_FLAGS_XXX */
60d96e5996SHans Petter Selasky };
61d96e5996SHans Petter Selasky
62d96e5996SHans Petter Selasky /*
63d96e5996SHans Petter Selasky * Extensible arrays API implemented as a wrapper
64d96e5996SHans Petter Selasky * around the radix tree implementation.
65d96e5996SHans Petter Selasky */
66d96e5996SHans Petter Selasky void *xa_erase(struct xarray *, uint32_t);
67d96e5996SHans Petter Selasky void *xa_load(struct xarray *, uint32_t);
68d96e5996SHans Petter Selasky int xa_alloc(struct xarray *, uint32_t *, void *, uint32_t, gfp_t);
69d96e5996SHans Petter Selasky int xa_alloc_cyclic(struct xarray *, uint32_t *, void *, uint32_t, uint32_t *, gfp_t);
70d96e5996SHans Petter Selasky int xa_insert(struct xarray *, uint32_t, void *, gfp_t);
71d96e5996SHans Petter Selasky void *xa_store(struct xarray *, uint32_t, void *, gfp_t);
72d96e5996SHans Petter Selasky void xa_init_flags(struct xarray *, uint32_t);
73d96e5996SHans Petter Selasky bool xa_empty(struct xarray *);
74d96e5996SHans Petter Selasky void xa_destroy(struct xarray *);
75d96e5996SHans Petter Selasky void *xa_next(struct xarray *, unsigned long *, bool);
76d96e5996SHans Petter Selasky
77d96e5996SHans Petter Selasky #define xa_for_each(xa, index, entry) \
78d96e5996SHans Petter Selasky for ((entry) = NULL, (index) = 0; \
79d96e5996SHans Petter Selasky ((entry) = xa_next(xa, &index, (entry) != NULL)) != NULL; )
80d96e5996SHans Petter Selasky
81d96e5996SHans Petter Selasky /*
82d96e5996SHans Petter Selasky * Unlocked version of functions above.
83d96e5996SHans Petter Selasky */
84d96e5996SHans Petter Selasky void *__xa_erase(struct xarray *, uint32_t);
85d96e5996SHans Petter Selasky int __xa_alloc(struct xarray *, uint32_t *, void *, uint32_t, gfp_t);
86d96e5996SHans Petter Selasky int __xa_alloc_cyclic(struct xarray *, uint32_t *, void *, uint32_t, uint32_t *, gfp_t);
87d96e5996SHans Petter Selasky int __xa_insert(struct xarray *, uint32_t, void *, gfp_t);
88d96e5996SHans Petter Selasky void *__xa_store(struct xarray *, uint32_t, void *, gfp_t);
89d96e5996SHans Petter Selasky bool __xa_empty(struct xarray *);
90d96e5996SHans Petter Selasky void *__xa_next(struct xarray *, unsigned long *, bool);
91d96e5996SHans Petter Selasky
925542309eSJean-Sébastien Pédron #define xa_store_irq(xa, index, ptr, gfp) \
935542309eSJean-Sébastien Pédron xa_store((xa), (index), (ptr), (gfp))
945542309eSJean-Sébastien Pédron
955542309eSJean-Sébastien Pédron #define xa_erase_irq(xa, index) \
965542309eSJean-Sébastien Pédron xa_erase((xa), (index))
975542309eSJean-Sébastien Pédron
98*66f9a983SVladimir Kondratyev #define xa_lock_irq(xa) xa_lock(xa)
99*66f9a983SVladimir Kondratyev #define xa_unlock_irq(xa) xa_unlock(xa)
100*66f9a983SVladimir Kondratyev
1015542309eSJean-Sébastien Pédron #define xa_lock_irqsave(xa, flags) \
1025542309eSJean-Sébastien Pédron do { \
1035542309eSJean-Sébastien Pédron xa_lock((xa)); \
1045542309eSJean-Sébastien Pédron flags = 0; \
1055542309eSJean-Sébastien Pédron } while (0)
1065542309eSJean-Sébastien Pédron
1075542309eSJean-Sébastien Pédron #define xa_unlock_irqrestore(xa, flags) \
1085542309eSJean-Sébastien Pédron do { \
1095542309eSJean-Sébastien Pédron xa_unlock((xa)); \
1105542309eSJean-Sébastien Pédron flags == 0; \
1115542309eSJean-Sébastien Pédron } while (0)
1125542309eSJean-Sébastien Pédron
113d96e5996SHans Petter Selasky static inline int
xa_err(void * ptr)114d96e5996SHans Petter Selasky xa_err(void *ptr)
115d96e5996SHans Petter Selasky {
116d96e5996SHans Petter Selasky return (PTR_ERR_OR_ZERO(ptr));
117d96e5996SHans Petter Selasky }
118d96e5996SHans Petter Selasky
119ab79c906SHans Petter Selasky static inline void
xa_init(struct xarray * xa)120ab79c906SHans Petter Selasky xa_init(struct xarray *xa)
121ab79c906SHans Petter Selasky {
122ab79c906SHans Petter Selasky xa_init_flags(xa, 0);
123ab79c906SHans Petter Selasky }
124ab79c906SHans Petter Selasky
125b59ffedaSVladimir Kondratyev static inline void *
xa_mk_value(unsigned long v)126b59ffedaSVladimir Kondratyev xa_mk_value(unsigned long v)
127b59ffedaSVladimir Kondratyev {
128b59ffedaSVladimir Kondratyev unsigned long r = (v << 1) | 1;
129b59ffedaSVladimir Kondratyev
130b59ffedaSVladimir Kondratyev return ((void *)r);
131b59ffedaSVladimir Kondratyev }
132b59ffedaSVladimir Kondratyev
133b59ffedaSVladimir Kondratyev static inline bool
xa_is_value(const void * e)134b59ffedaSVladimir Kondratyev xa_is_value(const void *e)
135b59ffedaSVladimir Kondratyev {
136b59ffedaSVladimir Kondratyev unsigned long v = (unsigned long)e;
137b59ffedaSVladimir Kondratyev
138b59ffedaSVladimir Kondratyev return (v & 1);
139b59ffedaSVladimir Kondratyev }
140b59ffedaSVladimir Kondratyev
141b59ffedaSVladimir Kondratyev static inline unsigned long
xa_to_value(const void * e)142b59ffedaSVladimir Kondratyev xa_to_value(const void *e)
143b59ffedaSVladimir Kondratyev {
144b59ffedaSVladimir Kondratyev unsigned long v = (unsigned long)e;
145b59ffedaSVladimir Kondratyev
146b59ffedaSVladimir Kondratyev return (v >> 1);
147b59ffedaSVladimir Kondratyev }
148307f78f3SVladimir Kondratyev #endif /* _LINUXKPI_LINUX_XARRAY_H_ */
149