xref: /wlan-driver/qca-wifi-host-cmn/qdf/linux/src/qdf_idr.c (revision 5113495b16420b49004c444715d2daae2066e7dc)
1*5113495bSYour Name /*
2*5113495bSYour Name  * Copyright (c) 2018 The Linux Foundation. All rights reserved.
3*5113495bSYour Name  *
4*5113495bSYour Name  * Permission to use, copy, modify, and/or distribute this software for
5*5113495bSYour Name  * any purpose with or without fee is hereby granted, provided that the
6*5113495bSYour Name  * above copyright notice and this permission notice appear in all
7*5113495bSYour Name  * copies.
8*5113495bSYour Name  *
9*5113495bSYour Name  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10*5113495bSYour Name  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11*5113495bSYour Name  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12*5113495bSYour Name  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13*5113495bSYour Name  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14*5113495bSYour Name  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15*5113495bSYour Name  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16*5113495bSYour Name  * PERFORMANCE OF THIS SOFTWARE.
17*5113495bSYour Name  */
18*5113495bSYour Name 
19*5113495bSYour Name /**
20*5113495bSYour Name  * DOC: qdf_idr
21*5113495bSYour Name  * This file provides the ability to map an ID to a pointer
22*5113495bSYour Name  */
23*5113495bSYour Name 
24*5113495bSYour Name /* Include files */
25*5113495bSYour Name #include <qdf_idr.h>
26*5113495bSYour Name #include <qdf_module.h>
27*5113495bSYour Name 
28*5113495bSYour Name #define QDF_IDR_START     0x100
29*5113495bSYour Name #define QDF_IDR_END       0
30*5113495bSYour Name 
qdf_idr_gpf_flag(void)31*5113495bSYour Name static int qdf_idr_gpf_flag(void)
32*5113495bSYour Name {
33*5113495bSYour Name 	if (in_interrupt() || irqs_disabled() || in_atomic())
34*5113495bSYour Name 		return GFP_ATOMIC;
35*5113495bSYour Name 
36*5113495bSYour Name 	return GFP_KERNEL;
37*5113495bSYour Name }
38*5113495bSYour Name 
39*5113495bSYour Name #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0)
40*5113495bSYour Name /**
41*5113495bSYour Name  * __qdf_idr_alloc() - Allocates an unused ID
42*5113495bSYour Name  * @idp:   pointer to qdf idr
43*5113495bSYour Name  * @ptr:   pointer to be associated with the new ID
44*5113495bSYour Name  * @start: the minimum ID
45*5113495bSYour Name  * @end:   the maximum ID
46*5113495bSYour Name  *
47*5113495bSYour Name  * Return: new ID
48*5113495bSYour Name  */
49*5113495bSYour Name static inline int32_t
__qdf_idr_alloc(qdf_idr * idp,void * ptr,int32_t start,int32_t end)50*5113495bSYour Name __qdf_idr_alloc(qdf_idr *idp, void *ptr, int32_t start, int32_t end)
51*5113495bSYour Name {
52*5113495bSYour Name 	int32_t id = 0;
53*5113495bSYour Name 
54*5113495bSYour Name 	idr_get_new(&idp->idr, ptr, &id);
55*5113495bSYour Name 
56*5113495bSYour Name 	return id;
57*5113495bSYour Name }
58*5113495bSYour Name #else
59*5113495bSYour Name static inline int32_t
__qdf_idr_alloc(qdf_idr * idp,void * ptr,int32_t start,int32_t end)60*5113495bSYour Name __qdf_idr_alloc(qdf_idr *idp, void *ptr, int32_t start, int32_t end)
61*5113495bSYour Name {
62*5113495bSYour Name 	return idr_alloc(&idp->idr, ptr, start, end, qdf_idr_gpf_flag());
63*5113495bSYour Name }
64*5113495bSYour Name #endif
65*5113495bSYour Name 
qdf_idr_create(qdf_idr * idp)66*5113495bSYour Name QDF_STATUS qdf_idr_create(qdf_idr *idp)
67*5113495bSYour Name {
68*5113495bSYour Name 	if (!idp)
69*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
70*5113495bSYour Name 
71*5113495bSYour Name 	qdf_spinlock_create(&idp->lock);
72*5113495bSYour Name 
73*5113495bSYour Name 	idr_init(&idp->idr);
74*5113495bSYour Name 
75*5113495bSYour Name 	return QDF_STATUS_SUCCESS;
76*5113495bSYour Name }
77*5113495bSYour Name 
78*5113495bSYour Name qdf_export_symbol(qdf_idr_create);
79*5113495bSYour Name 
qdf_idr_destroy(qdf_idr * idp)80*5113495bSYour Name QDF_STATUS qdf_idr_destroy(qdf_idr *idp)
81*5113495bSYour Name {
82*5113495bSYour Name 	if (!idp)
83*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
84*5113495bSYour Name 
85*5113495bSYour Name 	qdf_spinlock_destroy(&idp->lock);
86*5113495bSYour Name 	idr_destroy(&idp->idr);
87*5113495bSYour Name 
88*5113495bSYour Name 	return QDF_STATUS_SUCCESS;
89*5113495bSYour Name }
90*5113495bSYour Name 
91*5113495bSYour Name qdf_export_symbol(qdf_idr_destroy);
92*5113495bSYour Name 
qdf_idr_alloc(qdf_idr * idp,void * ptr,int32_t * id)93*5113495bSYour Name QDF_STATUS qdf_idr_alloc(qdf_idr *idp, void *ptr, int32_t *id)
94*5113495bSYour Name {
95*5113495bSYour Name 	int local_id;
96*5113495bSYour Name 
97*5113495bSYour Name 	if (!idp || !ptr)
98*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
99*5113495bSYour Name 
100*5113495bSYour Name 	qdf_spinlock_acquire(&idp->lock);
101*5113495bSYour Name 	local_id = __qdf_idr_alloc(idp, ptr, QDF_IDR_START, QDF_IDR_END);
102*5113495bSYour Name 	qdf_spinlock_release(&idp->lock);
103*5113495bSYour Name 	if (local_id < QDF_IDR_START)
104*5113495bSYour Name 		return QDF_STATUS_E_FAILURE;
105*5113495bSYour Name 
106*5113495bSYour Name 	*id = local_id;
107*5113495bSYour Name 
108*5113495bSYour Name 	return QDF_STATUS_SUCCESS;
109*5113495bSYour Name }
110*5113495bSYour Name 
111*5113495bSYour Name qdf_export_symbol(qdf_idr_alloc);
112*5113495bSYour Name 
qdf_idr_remove(qdf_idr * idp,int32_t id)113*5113495bSYour Name QDF_STATUS qdf_idr_remove(qdf_idr *idp, int32_t id)
114*5113495bSYour Name {
115*5113495bSYour Name 	if (!idp || id < QDF_IDR_START)
116*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
117*5113495bSYour Name 
118*5113495bSYour Name 	qdf_spinlock_acquire(&idp->lock);
119*5113495bSYour Name 	if (idr_find(&idp->idr, id))
120*5113495bSYour Name 		idr_remove(&idp->idr, id);
121*5113495bSYour Name 	qdf_spinlock_release(&idp->lock);
122*5113495bSYour Name 
123*5113495bSYour Name 	return QDF_STATUS_SUCCESS;
124*5113495bSYour Name }
125*5113495bSYour Name 
126*5113495bSYour Name qdf_export_symbol(qdf_idr_remove);
127*5113495bSYour Name 
qdf_idr_find(qdf_idr * idp,int32_t id,void ** ptr)128*5113495bSYour Name QDF_STATUS qdf_idr_find(qdf_idr *idp, int32_t id, void **ptr)
129*5113495bSYour Name {
130*5113495bSYour Name 	if (!ptr || (id < QDF_IDR_START))
131*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
132*5113495bSYour Name 
133*5113495bSYour Name 	qdf_spinlock_acquire(&idp->lock);
134*5113495bSYour Name 	*ptr = idr_find(&idp->idr, id);
135*5113495bSYour Name 	qdf_spinlock_release(&idp->lock);
136*5113495bSYour Name 	if (!(*ptr))
137*5113495bSYour Name 		return QDF_STATUS_E_INVAL;
138*5113495bSYour Name 	else
139*5113495bSYour Name 		return QDF_STATUS_SUCCESS;
140*5113495bSYour Name }
141*5113495bSYour Name 
142*5113495bSYour Name qdf_export_symbol(qdf_idr_find);
143*5113495bSYour Name 
144