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