xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/wlan_osif_request_manager.c (revision 1397a33f48ea6455be40871470b286e535820eb8)
1 /*
2  * Copyright (c) 2017-2018 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 		cfg80211_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 		cfg80211_err("allocation failed");
94 		return NULL;
95 	}
96 	request->reference_count = 1;
97 	request->params = *params;
98 	qdf_event_create(&request->completed);
99 	qdf_spin_lock_bh(&spinlock);
100 	request->cookie = cookie++;
101 	qdf_list_insert_back(&requests, &request->node);
102 	qdf_spin_unlock_bh(&spinlock);
103 	cfg80211_debug("request %pK, cookie %pK",
104 		       request, request->cookie);
105 
106 	return request;
107 }
108 
109 void *osif_request_priv(struct osif_request *request)
110 {
111 	/* private data area immediately follows the struct osif_request */
112 	return request + 1;
113 }
114 
115 void *osif_request_cookie(struct osif_request *request)
116 {
117 	return request->cookie;
118 }
119 
120 struct osif_request *osif_request_get(void *cookie)
121 {
122 	struct osif_request *request;
123 
124 	if (!is_initialized) {
125 		cfg80211_err("invoked when not initialized");
126 		return NULL;
127 	}
128 	qdf_spin_lock_bh(&spinlock);
129 	request = osif_request_find(cookie);
130 	if (request)
131 		request->reference_count++;
132 	qdf_spin_unlock_bh(&spinlock);
133 	cfg80211_debug("cookie %pK, request %pK",
134 		       cookie, request);
135 
136 	return request;
137 }
138 
139 void osif_request_put(struct osif_request *request)
140 {
141 	bool unlinked = false;
142 
143 	cfg80211_debug("request %pK, cookie %pK",
144 		       request, request->cookie);
145 	qdf_spin_lock_bh(&spinlock);
146 	request->reference_count--;
147 	if (0 == request->reference_count) {
148 		osif_request_unlink(request);
149 		unlinked = true;
150 	}
151 	qdf_spin_unlock_bh(&spinlock);
152 	if (unlinked)
153 		osif_request_destroy(request);
154 }
155 
156 int osif_request_wait_for_response(struct osif_request *request)
157 {
158 	QDF_STATUS status;
159 
160 	status = qdf_wait_for_event_completion(&request->completed,
161 				       request->params.timeout_ms);
162 
163 	return qdf_status_to_os_return(status);
164 }
165 
166 void osif_request_complete(struct osif_request *request)
167 {
168 	(void) qdf_event_set(&request->completed);
169 }
170 
171 void osif_request_manager_init(void)
172 {
173 	if (is_initialized)
174 		return;
175 
176 	qdf_list_create(&requests, MAX_NUM_REQUESTS);
177 	qdf_spinlock_create(&spinlock);
178 	is_initialized = true;
179 }
180 
181 /*
182  * osif_request_manager_deinit implementation note:
183  * It is intentional that we do not destroy the list or the spinlock.
184  * This allows threads to still access the infrastructure even when it
185  * has been deinitialized. Since neither lists nor spinlocks consume
186  * resources this does not result in a resource leak.
187  */
188 void osif_request_manager_deinit(void)
189 {
190 	is_initialized = false;
191 }
192