xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/wlan_osif_request_manager.c (revision 503663c6daafffe652fa360bde17243568cd6d2a) !
1 /*
2  * Copyright (c) 2017-2019 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <linux/kernel.h>
20 #include "qdf_mem.h"
21 #include "qdf_list.h"
22 #include "qdf_event.h"
23 #include "wlan_cfg80211.h"
24 #include "wlan_osif_request_manager.h"
25 
26 /* arbitrary value */
27 #define MAX_NUM_REQUESTS 20
28 
29 static bool is_initialized;
30 static qdf_list_t requests;
31 static qdf_spinlock_t spinlock;
32 static void *cookie;
33 
34 struct osif_request {
35 	qdf_list_node_t node;
36 	void *cookie;
37 	uint32_t reference_count;
38 	struct osif_request_params params;
39 	qdf_event_t completed;
40 };
41 
42 /* must be called with spinlock held */
43 static void osif_request_unlink(struct osif_request *request)
44 {
45 	qdf_list_remove_node(&requests, &request->node);
46 }
47 
48 static void osif_request_destroy(struct osif_request *request)
49 {
50 	struct osif_request_params *params;
51 
52 	params = &request->params;
53 	if (params->dealloc) {
54 		void *priv = osif_request_priv(request);
55 
56 		params->dealloc(priv);
57 	}
58 	qdf_event_destroy(&request->completed);
59 	qdf_mem_free(request);
60 }
61 
62 /* must be called with spinlock held */
63 static struct osif_request *osif_request_find(void *cookie)
64 {
65 	QDF_STATUS status;
66 	struct osif_request *request;
67 	qdf_list_node_t *node;
68 
69 	status = qdf_list_peek_front(&requests, &node);
70 	while (QDF_IS_STATUS_SUCCESS(status)) {
71 		request = qdf_container_of(node, struct osif_request, node);
72 		if (request->cookie == cookie)
73 			return request;
74 		status = qdf_list_peek_next(&requests, node, &node);
75 	}
76 
77 	return NULL;
78 }
79 
80 struct osif_request *osif_request_alloc(const struct osif_request_params *params)
81 {
82 	size_t length;
83 	struct osif_request *request;
84 
85 	if (!is_initialized) {
86 		osif_err("invoked when not initialized");
87 		return NULL;
88 	}
89 
90 	length = sizeof(*request) + params->priv_size;
91 	request = qdf_mem_malloc(length);
92 	if (!request)
93 		return NULL;
94 
95 	request->reference_count = 1;
96 	request->params = *params;
97 	qdf_event_create(&request->completed);
98 	qdf_spin_lock_bh(&spinlock);
99 	request->cookie = cookie++;
100 	qdf_list_insert_back(&requests, &request->node);
101 	qdf_spin_unlock_bh(&spinlock);
102 	osif_debug("request %pK, cookie %pK", request, request->cookie);
103 
104 	return request;
105 }
106 
107 void *osif_request_priv(struct osif_request *request)
108 {
109 	/* private data area immediately follows the struct osif_request */
110 	return request + 1;
111 }
112 
113 void *osif_request_cookie(struct osif_request *request)
114 {
115 	return request->cookie;
116 }
117 
118 struct osif_request *osif_request_get(void *cookie)
119 {
120 	struct osif_request *request;
121 
122 	if (!is_initialized) {
123 		osif_err("invoked when not initialized");
124 		return NULL;
125 	}
126 	qdf_spin_lock_bh(&spinlock);
127 	request = osif_request_find(cookie);
128 	if (request)
129 		request->reference_count++;
130 	qdf_spin_unlock_bh(&spinlock);
131 	osif_debug("cookie %pK, request %pK", cookie, request);
132 
133 	return request;
134 }
135 
136 void osif_request_put(struct osif_request *request)
137 {
138 	bool unlinked = false;
139 
140 	osif_debug("request %pK, cookie %pK", request, request->cookie);
141 	qdf_spin_lock_bh(&spinlock);
142 	request->reference_count--;
143 	if (0 == request->reference_count) {
144 		osif_request_unlink(request);
145 		unlinked = true;
146 	}
147 	qdf_spin_unlock_bh(&spinlock);
148 	if (unlinked)
149 		osif_request_destroy(request);
150 }
151 
152 int osif_request_wait_for_response(struct osif_request *request)
153 {
154 	QDF_STATUS status;
155 
156 	status = qdf_wait_for_event_completion(&request->completed,
157 				       request->params.timeout_ms);
158 
159 	return qdf_status_to_os_return(status);
160 }
161 
162 void osif_request_complete(struct osif_request *request)
163 {
164 	(void) qdf_event_set(&request->completed);
165 }
166 
167 void osif_request_manager_init(void)
168 {
169 	if (is_initialized)
170 		return;
171 
172 	qdf_list_create(&requests, MAX_NUM_REQUESTS);
173 	qdf_spinlock_create(&spinlock);
174 	is_initialized = true;
175 }
176 
177 /*
178  * osif_request_manager_deinit implementation note:
179  * It is intentional that we do not destroy the list or the spinlock.
180  * This allows threads to still access the infrastructure even when it
181  * has been deinitialized. Since neither lists nor spinlocks consume
182  * resources this does not result in a resource leak.
183  */
184 void osif_request_manager_deinit(void)
185 {
186 	is_initialized = false;
187 }
188