xref: /wlan-dirver/qca-wifi-host-cmn/umac/regulatory/core/src/reg_callbacks.c (revision 8ddef7dd9a290d4a9b1efd5d3efacf51d78a1a0d)
1 /*
2  * Copyright (c) 2014-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 /**
20  * DOC: reg_callbacks.c
21  * This file defines regulatory callback functions
22  */
23 
24 #include <wlan_cmn.h>
25 #include <reg_services_public_struct.h>
26 #include <wlan_objmgr_psoc_obj.h>
27 #include <wlan_objmgr_pdev_obj.h>
28 #include "reg_priv_objs.h"
29 #include "reg_utils.h"
30 #include <scheduler_api.h>
31 #include "reg_callbacks.h"
32 #include "reg_services_common.h"
33 #include "reg_build_chan_list.h"
34 
35 /**
36  * reg_call_chan_change_cbks() - Call registered callback functions on channel
37  * change.
38  * @psoc: Pointer to global psoc structure.
39  * @pdev: Pointer to global pdev structure.
40  */
41 static void reg_call_chan_change_cbks(struct wlan_objmgr_psoc *psoc,
42 				      struct wlan_objmgr_pdev *pdev)
43 {
44 	struct chan_change_cbk_entry *cbk_list;
45 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
46 	struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj;
47 	struct regulatory_channel *cur_chan_list;
48 	uint32_t ctr;
49 	struct avoid_freq_ind_data *avoid_freq_ind = NULL;
50 	reg_chan_change_callback callback;
51 
52 	psoc_priv_obj = reg_get_psoc_obj(psoc);
53 	if (!IS_VALID_PSOC_REG_OBJ(psoc_priv_obj)) {
54 		reg_alert("psoc reg component is NULL");
55 		return;
56 	}
57 
58 	pdev_priv_obj = reg_get_pdev_obj(pdev);
59 	if (!IS_VALID_PDEV_REG_OBJ(pdev_priv_obj)) {
60 		reg_alert("pdev reg component is NULL");
61 		return;
62 	}
63 
64 	cur_chan_list = qdf_mem_malloc(NUM_CHANNELS * sizeof(*cur_chan_list));
65 	if (!cur_chan_list) {
66 		reg_alert("Mem alloc failed for current channel list");
67 		return;
68 	}
69 
70 	qdf_mem_copy(cur_chan_list,
71 		     pdev_priv_obj->cur_chan_list,
72 		     NUM_CHANNELS *
73 		     sizeof(struct regulatory_channel));
74 
75 	if (psoc_priv_obj->ch_avoid_ind) {
76 		avoid_freq_ind = qdf_mem_malloc(sizeof(*avoid_freq_ind));
77 		if (!avoid_freq_ind) {
78 			reg_alert("Mem alloc failed for avoid freq ind");
79 			goto skip_ch_avoid_ind;
80 		}
81 		qdf_mem_copy(&avoid_freq_ind->freq_list,
82 			     &psoc_priv_obj->avoid_freq_list,
83 				sizeof(struct ch_avoid_ind_type));
84 		qdf_mem_copy(&avoid_freq_ind->chan_list,
85 			     &psoc_priv_obj->unsafe_chan_list,
86 				sizeof(struct unsafe_ch_list));
87 		psoc_priv_obj->ch_avoid_ind = false;
88 	}
89 
90 skip_ch_avoid_ind:
91 	cbk_list = psoc_priv_obj->cbk_list;
92 
93 	for (ctr = 0; ctr < REG_MAX_CHAN_CHANGE_CBKS; ctr++) {
94 		callback  = NULL;
95 		qdf_spin_lock_bh(&psoc_priv_obj->cbk_list_lock);
96 		if (cbk_list[ctr].cbk)
97 			callback = cbk_list[ctr].cbk;
98 		qdf_spin_unlock_bh(&psoc_priv_obj->cbk_list_lock);
99 		if (callback)
100 			callback(psoc, pdev, cur_chan_list, avoid_freq_ind,
101 				 cbk_list[ctr].arg);
102 	}
103 	qdf_mem_free(cur_chan_list);
104 	if (avoid_freq_ind)
105 		qdf_mem_free(avoid_freq_ind);
106 }
107 
108 /**
109  * reg_alloc_and_fill_payload() - Alloc and fill payload structure.
110  * @psoc: Pointer to global psoc structure.
111  * @pdev: Pointer to global pdev structure.
112  */
113 static void reg_alloc_and_fill_payload(struct wlan_objmgr_psoc *psoc,
114 				       struct wlan_objmgr_pdev *pdev,
115 				       struct reg_sched_payload **payload)
116 {
117 	*payload = qdf_mem_malloc(sizeof(**payload));
118 	if (*payload) {
119 		(*payload)->psoc = psoc;
120 		(*payload)->pdev = pdev;
121 	}
122 }
123 
124 /**
125  * reg_chan_change_flush_cbk_sb() - Flush south bound channel change callbacks.
126  * @msg: Pointer to scheduler msg structure.
127  */
128 static QDF_STATUS reg_chan_change_flush_cbk_sb(struct scheduler_msg *msg)
129 {
130 	struct reg_sched_payload *load = msg->bodyptr;
131 	struct wlan_objmgr_psoc *psoc = load->psoc;
132 	struct wlan_objmgr_pdev *pdev = load->pdev;
133 
134 	wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_SB_ID);
135 	wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_SB_ID);
136 	qdf_mem_free(load);
137 
138 	return QDF_STATUS_SUCCESS;
139 }
140 
141 /**
142  * reg_sched_chan_change_cbks_sb() - Schedule south bound channel change
143  * callbacks.
144  * @msg: Pointer to scheduler msg structure.
145  */
146 static QDF_STATUS reg_sched_chan_change_cbks_sb(struct scheduler_msg *msg)
147 {
148 	struct reg_sched_payload *load = msg->bodyptr;
149 	struct wlan_objmgr_psoc *psoc = load->psoc;
150 	struct wlan_objmgr_pdev *pdev = load->pdev;
151 
152 	reg_call_chan_change_cbks(psoc, pdev);
153 
154 	wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_SB_ID);
155 	wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_SB_ID);
156 	qdf_mem_free(load);
157 
158 	return QDF_STATUS_SUCCESS;
159 }
160 
161 /**
162  * reg_chan_change_flush_cbk_nb() - Flush north bound channel change callbacks.
163  * @msg: Pointer to scheduler msg structure.
164  */
165 static QDF_STATUS reg_chan_change_flush_cbk_nb(struct scheduler_msg *msg)
166 {
167 	struct reg_sched_payload *load = msg->bodyptr;
168 	struct wlan_objmgr_psoc *psoc = load->psoc;
169 	struct wlan_objmgr_pdev *pdev = load->pdev;
170 
171 	wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_NB_ID);
172 	wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_NB_ID);
173 	qdf_mem_free(load);
174 
175 	return QDF_STATUS_SUCCESS;
176 }
177 
178 /**
179  * reg_sched_chan_change_cbks_nb() - Schedule north bound channel change
180  * callbacks.
181  * @msg: Pointer to scheduler msg structure.
182  */
183 static QDF_STATUS reg_sched_chan_change_cbks_nb(struct scheduler_msg *msg)
184 {
185 	struct reg_sched_payload *load = msg->bodyptr;
186 	struct wlan_objmgr_psoc *psoc = load->psoc;
187 	struct wlan_objmgr_pdev *pdev = load->pdev;
188 
189 	reg_call_chan_change_cbks(psoc, pdev);
190 
191 	wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_NB_ID);
192 	wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_NB_ID);
193 	qdf_mem_free(load);
194 
195 	return QDF_STATUS_SUCCESS;
196 }
197 
198 QDF_STATUS reg_send_scheduler_msg_sb(struct wlan_objmgr_psoc *psoc,
199 				     struct wlan_objmgr_pdev *pdev)
200 {
201 	struct scheduler_msg msg = {0};
202 	struct reg_sched_payload *payload;
203 	QDF_STATUS status;
204 
205 	status = wlan_objmgr_psoc_try_get_ref(psoc, WLAN_REGULATORY_SB_ID);
206 	if (QDF_IS_STATUS_ERROR(status)) {
207 		reg_err("error taking psoc ref cnt");
208 		return status;
209 	}
210 
211 	status = wlan_objmgr_pdev_try_get_ref(pdev, WLAN_REGULATORY_SB_ID);
212 	if (QDF_IS_STATUS_ERROR(status)) {
213 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_SB_ID);
214 		reg_err("error taking pdev ref cnt");
215 		return status;
216 	}
217 
218 	reg_alloc_and_fill_payload(psoc, pdev, &payload);
219 	if (!payload) {
220 		reg_err("payload memory alloc failed");
221 		wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_SB_ID);
222 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_SB_ID);
223 		return QDF_STATUS_E_NOMEM;
224 	}
225 
226 	msg.bodyptr = payload;
227 	msg.callback = reg_sched_chan_change_cbks_sb;
228 	msg.flush_callback = reg_chan_change_flush_cbk_sb;
229 
230 	status = scheduler_post_message(QDF_MODULE_ID_REGULATORY,
231 					QDF_MODULE_ID_REGULATORY,
232 					QDF_MODULE_ID_TARGET_IF, &msg);
233 	if (QDF_IS_STATUS_ERROR(status)) {
234 		wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_SB_ID);
235 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_SB_ID);
236 		reg_err("scheduler msg posting failed");
237 		qdf_mem_free(payload);
238 	}
239 
240 	return status;
241 }
242 
243 QDF_STATUS reg_send_scheduler_msg_nb(struct wlan_objmgr_psoc *psoc,
244 				     struct wlan_objmgr_pdev *pdev)
245 {
246 	struct scheduler_msg msg = {0};
247 	struct reg_sched_payload *payload;
248 	QDF_STATUS status;
249 
250 	status = wlan_objmgr_psoc_try_get_ref(psoc, WLAN_REGULATORY_NB_ID);
251 	if (QDF_IS_STATUS_ERROR(status)) {
252 		reg_err("error taking psoc ref cnt");
253 		return status;
254 	}
255 
256 	status = wlan_objmgr_pdev_try_get_ref(pdev, WLAN_REGULATORY_NB_ID);
257 	if (QDF_IS_STATUS_ERROR(status)) {
258 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_NB_ID);
259 		reg_err("error taking pdev ref cnt");
260 		return status;
261 	}
262 
263 	reg_alloc_and_fill_payload(psoc, pdev, &payload);
264 	if (!payload) {
265 		reg_err("payload memory alloc failed");
266 		wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_NB_ID);
267 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_NB_ID);
268 		return QDF_STATUS_E_NOMEM;
269 	}
270 	msg.bodyptr = payload;
271 	msg.callback = reg_sched_chan_change_cbks_nb;
272 	msg.flush_callback = reg_chan_change_flush_cbk_nb;
273 
274 	status = scheduler_post_message(QDF_MODULE_ID_REGULATORY,
275 					QDF_MODULE_ID_REGULATORY,
276 					QDF_MODULE_ID_OS_IF, &msg);
277 	if (QDF_IS_STATUS_ERROR(status)) {
278 		wlan_objmgr_pdev_release_ref(pdev, WLAN_REGULATORY_NB_ID);
279 		wlan_objmgr_psoc_release_ref(psoc, WLAN_REGULATORY_NB_ID);
280 		reg_err("scheduler msg posting failed");
281 		qdf_mem_free(payload);
282 	}
283 
284 	return status;
285 }
286 
287 QDF_STATUS reg_notify_sap_event(struct wlan_objmgr_pdev *pdev,
288 				bool sap_state)
289 {
290 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
291 	struct wlan_regulatory_pdev_priv_obj *pdev_priv_obj;
292 	struct wlan_objmgr_psoc *psoc;
293 	QDF_STATUS status;
294 
295 	pdev_priv_obj = reg_get_pdev_obj(pdev);
296 
297 	if (!IS_VALID_PDEV_REG_OBJ(pdev_priv_obj)) {
298 		reg_err("pdev reg component is NULL");
299 		return QDF_STATUS_E_INVAL;
300 	}
301 
302 	psoc = wlan_pdev_get_psoc(pdev);
303 	if (!psoc) {
304 		reg_err("psoc is NULL");
305 		return QDF_STATUS_E_INVAL;
306 	}
307 
308 	psoc_priv_obj = reg_get_psoc_obj(psoc);
309 	if (!IS_VALID_PSOC_REG_OBJ(psoc_priv_obj)) {
310 		reg_err("psoc reg component is NULL");
311 		return QDF_STATUS_E_INVAL;
312 	}
313 
314 	reg_info("sap_state: %d", sap_state);
315 
316 	if (pdev_priv_obj->sap_state == sap_state)
317 		return QDF_STATUS_SUCCESS;
318 
319 	pdev_priv_obj->sap_state = sap_state;
320 	set_disable_channel_state(pdev_priv_obj);
321 
322 	reg_compute_pdev_current_chan_list(pdev_priv_obj);
323 	status = reg_send_scheduler_msg_sb(psoc, pdev);
324 
325 	return status;
326 }
327 
328 void reg_register_chan_change_callback(struct wlan_objmgr_psoc *psoc,
329 				       reg_chan_change_callback cbk, void *arg)
330 {
331 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
332 	uint32_t count;
333 
334 	psoc_priv_obj = reg_get_psoc_obj(psoc);
335 	if (!psoc_priv_obj) {
336 		reg_err("reg psoc private obj is NULL");
337 		return;
338 	}
339 
340 	qdf_spin_lock_bh(&psoc_priv_obj->cbk_list_lock);
341 	for (count = 0; count < REG_MAX_CHAN_CHANGE_CBKS; count++)
342 		if (!psoc_priv_obj->cbk_list[count].cbk) {
343 			psoc_priv_obj->cbk_list[count].cbk = cbk;
344 			psoc_priv_obj->cbk_list[count].arg = arg;
345 			psoc_priv_obj->num_chan_change_cbks++;
346 			break;
347 		}
348 	qdf_spin_unlock_bh(&psoc_priv_obj->cbk_list_lock);
349 
350 	if (count == REG_MAX_CHAN_CHANGE_CBKS)
351 		reg_err("callback list is full, could not add the cbk");
352 }
353 
354 void reg_unregister_chan_change_callback(struct wlan_objmgr_psoc *psoc,
355 					 reg_chan_change_callback cbk)
356 {
357 	struct wlan_regulatory_psoc_priv_obj *psoc_priv_obj;
358 	uint32_t count;
359 
360 	psoc_priv_obj = reg_get_psoc_obj(psoc);
361 	if (!psoc_priv_obj) {
362 		reg_err("reg psoc private obj is NULL");
363 		return;
364 	}
365 
366 	qdf_spin_lock_bh(&psoc_priv_obj->cbk_list_lock);
367 	for (count = 0; count < REG_MAX_CHAN_CHANGE_CBKS; count++)
368 		if (psoc_priv_obj->cbk_list[count].cbk == cbk) {
369 			psoc_priv_obj->cbk_list[count].cbk = NULL;
370 			psoc_priv_obj->num_chan_change_cbks--;
371 			break;
372 		}
373 	qdf_spin_unlock_bh(&psoc_priv_obj->cbk_list_lock);
374 
375 	if (count == REG_MAX_CHAN_CHANGE_CBKS)
376 		reg_err("callback not found in the list");
377 }
378 
379