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