xref: /wlan-dirver/qca-wifi-host-cmn/umac/dfs/core/src/misc/dfs_filter_init.c (revision 503663c6daafffe652fa360bde17243568cd6d2a)
1 /*
2  * Copyright (c) 2016-2019 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2002-2006, Atheros Communications Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /**
19  * DOC: This file contains the dfs_attach() and dfs_detach() functions as well
20  * as the dfs_control() function which is used to process ioctls related to DFS.
21  * For Linux/Mac,  "radartool" is the command line tool that can be used to call
22  * various ioctls to set and get radar detection thresholds.
23  */
24 
25 #include "../dfs_zero_cac.h"
26 #include "wlan_dfs_lmac_api.h"
27 #include "wlan_dfs_mlme_api.h"
28 #include "wlan_dfs_tgt_api.h"
29 #include "../dfs_internal.h"
30 #include "../dfs_filter_init.h"
31 #include "../dfs_partial_offload_radar.h"
32 
33 #ifndef WLAN_DFS_STATIC_MEM_ALLOC
34 /*
35  * dfs_alloc_dfs_events() - allocate dfs events buffer
36  *
37  * Return: events buffer, null on failure.
38  */
39 static inline struct dfs_event *dfs_alloc_dfs_events(void)
40 {
41 	return qdf_mem_malloc(sizeof(struct dfs_event) * DFS_MAX_EVENTS);
42 }
43 
44 /*
45  * dfs_free_dfs_events() - Free events buffer
46  * @events: Events buffer pointer
47  *
48  * Return: None
49  */
50 static inline void dfs_free_dfs_events(struct dfs_event *events)
51 {
52 	qdf_mem_free(events);
53 }
54 
55 /*
56  * dfs_alloc_dfs_pulseline() - allocate buffer for dfs pulses
57  *
58  * Return: events buffer, null on failure.
59  */
60 static inline struct dfs_pulseline *dfs_alloc_dfs_pulseline(void)
61 {
62 	return qdf_mem_malloc(sizeof(struct dfs_pulseline));
63 }
64 
65 /*
66  * dfs_free_dfs_pulseline() - Free pulse buffer
67  * @pulses: Pulses buffer pointer
68  *
69  * Return: None
70  */
71 static inline void dfs_free_dfs_pulseline(struct dfs_pulseline *pulses)
72 {
73 	qdf_mem_free(pulses);
74 }
75 #else
76 /* Static buffers for DFS objects */
77 static struct dfs_event global_dfs_event[DFS_MAX_EVENTS];
78 static struct dfs_pulseline global_dfs_pulseline;
79 
80 static inline struct dfs_event *dfs_alloc_dfs_events(void)
81 {
82 	return global_dfs_event;
83 }
84 
85 static inline void dfs_free_dfs_events(struct dfs_event *events)
86 {
87 }
88 
89 static inline struct dfs_pulseline *dfs_alloc_dfs_pulseline(void)
90 {
91 	return &global_dfs_pulseline;
92 }
93 
94 static inline void dfs_free_dfs_pulseline(struct dfs_pulseline *pulses)
95 {
96 }
97 #endif
98 
99 /*
100  * Channel switch announcement (CSA)
101  * usenol=1 (default) make CSA and switch to a new channel on radar detect
102  * usenol=0, make CSA with next channel same as current on radar detect
103  * usenol=2, no CSA and stay on the same channel on radar detect
104  */
105 
106 /**
107  * dfs_task() - The timer function to process the radar pulses.
108  */
109 static os_timer_func(dfs_task)
110 {
111 	struct wlan_dfs *dfs = NULL;
112 
113 	OS_GET_TIMER_ARG(dfs, struct wlan_dfs *);
114 
115 	if (!dfs) {
116 		dfs_err(dfs, WLAN_DEBUG_DFS_ALWAYS,  "dfs is NULL");
117 		return;
118 	}
119 
120 	/* Need to take a lock here since dfs filtering data structures are
121 	 * freed and re-allocated in dfs_init_radar_filters() during channel
122 	 * change which may happen in the middle of dfs pulse processing.
123 	 */
124 	WLAN_DFS_DATA_STRUCT_LOCK(dfs);
125 	dfs_process_radarevent(dfs, dfs->dfs_curchan);
126 	WLAN_DFS_DATA_STRUCT_UNLOCK(dfs);
127 
128 	dfs->wlan_radar_tasksched = 0;
129 }
130 
131 /**
132  * dfs_main_task_timer_init() - Initialize dfs task timer.
133  * @dfs: Pointer to wlan_dfs structure.
134  */
135 static void dfs_main_task_timer_init(struct wlan_dfs *dfs)
136 {
137 	qdf_timer_init(NULL,
138 			&(dfs->wlan_dfs_task_timer),
139 			dfs_task,
140 			(void *)(dfs),
141 			QDF_TIMER_TYPE_WAKE_APPS);
142 }
143 
144 /**
145  * dfs_free_filter() - free memory allocated for dfs ft_filters
146  * @radarf: pointer holding ft_filters.
147  *
148  * Return: None
149  */
150 static void dfs_free_filter(struct dfs_filtertype *radarf)
151 {
152 	uint8_t i;
153 
154 	for (i = 0; i < DFS_MAX_NUM_RADAR_FILTERS; i++) {
155 		if (radarf->ft_filters[i]) {
156 			qdf_mem_free(radarf->ft_filters[i]);
157 			radarf->ft_filters[i] = NULL;
158 		}
159 	}
160 }
161 
162 /**
163  * dfs_alloc_mem_filter() - allocate memory for dfs ft_filters
164  * @radarf: pointer holding ft_filters.
165  *
166  * Return: QDF_STATUS
167  */
168 static QDF_STATUS dfs_alloc_mem_filter(struct dfs_filtertype *radarf)
169 {
170 	uint8_t i;
171 
172 	for (i = 0; i < DFS_MAX_NUM_RADAR_FILTERS; i++) {
173 		radarf->ft_filters[i] = qdf_mem_malloc(sizeof(struct
174 							      dfs_filter));
175 		if (!radarf->ft_filters[i]) {
176 			/* Free all the filter if malloc failed */
177 			dfs_free_filter(radarf);
178 			return QDF_STATUS_E_FAILURE;
179 		}
180 	}
181 
182 	return QDF_STATUS_SUCCESS;
183 }
184 
185 int dfs_main_attach(struct wlan_dfs *dfs)
186 {
187 	int i, n;
188 	QDF_STATUS status;
189 	struct wlan_dfs_radar_tab_info radar_info;
190 
191 	if (!dfs) {
192 		dfs_err(dfs, WLAN_DEBUG_DFS_ALWAYS,  "dfs is NULL");
193 		return 0;
194 	}
195 
196 	/* If ignore_dfs is set to 1 then Radar detection is disabled. */
197 	if (dfs->dfs_ignore_dfs) {
198 		dfs_debug(dfs, WLAN_DEBUG_DFS1, "ignoring dfs");
199 		return 0;
200 	}
201 
202 	/*
203 	 * Zero out radar_info. It's possible that the attach function
204 	 * won't fetch an initial regulatory configuration; you really
205 	 * do want to ensure that the contents indicates there aren't
206 	 * any filters.
207 	 */
208 	qdf_mem_zero(&radar_info, sizeof(radar_info));
209 
210 	lmac_get_caps(dfs->dfs_pdev_obj, &(dfs->dfs_caps));
211 
212 	dfs_clear_stats(dfs);
213 	dfs->dfs_event_log_on = 1;
214 	dfs_debug(dfs, WLAN_DEBUG_DFS_ALWAYS, "event log enabled by default");
215 
216 	dfs->dfs_enable = 1;
217 
218 	/*Verify : Passing NULL to qdf_timer_init().*/
219 	dfs_main_task_timer_init(dfs);
220 
221 	dfs_allow_hw_pulses(dfs, true);
222 	dfs_host_wait_timer_init(dfs);
223 
224 	WLAN_DFSQ_LOCK_CREATE(dfs);
225 	STAILQ_INIT(&dfs->dfs_radarq);
226 	WLAN_ARQ_LOCK_CREATE(dfs);
227 	STAILQ_INIT(&dfs->dfs_arq);
228 	STAILQ_INIT(&(dfs->dfs_eventq));
229 	WLAN_DFSEVENTQ_LOCK_CREATE(dfs);
230 	WLAN_DFS_DATA_STRUCT_LOCK_CREATE(dfs);
231 
232 	dfs->events = dfs_alloc_dfs_events();
233 	if (!(dfs->events))
234 		return 1;
235 
236 	for (i = 0; i < DFS_MAX_EVENTS; i++)
237 		STAILQ_INSERT_TAIL(&(dfs->dfs_eventq), &dfs->events[i],
238 				re_list);
239 
240 	dfs->pulses = dfs_alloc_dfs_pulseline();
241 	if (!(dfs->pulses)) {
242 		dfs_free_dfs_events(dfs->events);
243 		dfs->events = NULL;
244 		return 1;
245 	}
246 
247 	dfs->pulses->pl_lastelem = DFS_MAX_PULSE_BUFFER_MASK;
248 
249 	/* Allocate memory for radar filters. */
250 	for (n = 0; n < DFS_MAX_RADAR_TYPES; n++) {
251 		dfs->dfs_radarf[n] = (struct dfs_filtertype *)
252 			qdf_mem_malloc(sizeof(struct dfs_filtertype));
253 		if (!(dfs->dfs_radarf[n]))
254 			goto bad1;
255 
256 		qdf_mem_zero(dfs->dfs_radarf[n],
257 			     sizeof(struct dfs_filtertype));
258 		status = dfs_alloc_mem_filter(dfs->dfs_radarf[n]);
259 		if (!QDF_IS_STATUS_SUCCESS(status)) {
260 			dfs_alert(dfs, WLAN_DEBUG_DFS_ALWAYS,
261 				  "mem alloc for dfs_filter failed");
262 			goto bad1;
263 		}
264 	}
265 
266 	/* Allocate memory for radar table. */
267 	dfs->dfs_ftindextable = (int8_t **)qdf_mem_malloc(
268 			DFS_NUM_FT_IDX_TBL_ROWS*sizeof(int8_t *));
269 	if (!(dfs->dfs_ftindextable))
270 		goto bad1;
271 
272 	for (n = 0; n < DFS_NUM_FT_IDX_TBL_ROWS; n++) {
273 		dfs->dfs_ftindextable[n] = qdf_mem_malloc(
274 				DFS_MAX_RADAR_OVERLAP*sizeof(int8_t));
275 		if (!(dfs->dfs_ftindextable[n]))
276 			goto bad2;
277 	}
278 
279 	dfs->dfs_use_nol = 1;
280 
281 	/* Init the cached extension channel busy for false alarm reduction */
282 	dfs->dfs_rinfo.ext_chan_busy_ts = lmac_get_tsf64(dfs->dfs_pdev_obj);
283 	dfs->dfs_rinfo.dfs_ext_chan_busy = 0;
284 	/* Init the Bin5 chirping related data */
285 	dfs->dfs_rinfo.dfs_bin5_chirp_ts = dfs->dfs_rinfo.ext_chan_busy_ts;
286 	dfs->dfs_rinfo.dfs_last_bin5_dur = MAX_BIN5_DUR;
287 	dfs->dfs_b5radars = NULL;
288 
289 	/*
290 	 * If dfs_init_radar_filters() fails, we can abort here and
291 	 * reconfigure when the first valid channel + radar config
292 	 * is available.
293 	 */
294 	if (dfs_init_radar_filters(dfs, &radar_info)) {
295 		dfs_err(dfs, WLAN_DEBUG_DFS_ALWAYS,  "Radar Filter Intialization Failed");
296 		return 1;
297 	}
298 
299 	dfs->wlan_dfs_false_rssi_thres = RSSI_POSSIBLY_FALSE;
300 	dfs->wlan_dfs_peak_mag = SEARCH_FFT_REPORT_PEAK_MAG_THRSH;
301 	dfs->dfs_phyerr_freq_min     = 0x7fffffff;
302 	dfs->dfs_phyerr_freq_max     = 0;
303 	dfs->dfs_phyerr_queued_count = 0;
304 	dfs->dfs_phyerr_w53_counter  = 0;
305 	dfs->dfs_pri_multiplier      = 2;
306 	dfs_get_radars(dfs);
307 
308 	return 0;
309 
310 bad2:
311 	qdf_mem_free(dfs->dfs_ftindextable);
312 	dfs->dfs_ftindextable = NULL;
313 bad1:
314 	for (n = 0; n < DFS_MAX_RADAR_TYPES; n++) {
315 		if (dfs->dfs_radarf[n]) {
316 			dfs_free_filter(dfs->dfs_radarf[n]);
317 			qdf_mem_free(dfs->dfs_radarf[n]);
318 			dfs->dfs_radarf[n] = NULL;
319 		}
320 	}
321 	if (dfs->pulses) {
322 		dfs_free_dfs_pulseline(dfs->pulses);
323 		dfs->pulses = NULL;
324 	}
325 	if (dfs->events) {
326 		dfs_free_dfs_events(dfs->events);
327 		dfs->events = NULL;
328 	}
329 
330 	return 1;
331 }
332 
333 void dfs_main_timer_reset(struct wlan_dfs *dfs)
334 {
335 	if (dfs->wlan_radar_tasksched) {
336 		qdf_timer_sync_cancel(&dfs->wlan_dfs_task_timer);
337 		dfs->wlan_radar_tasksched = 0;
338 	}
339 }
340 
341 void dfs_main_timer_detach(struct wlan_dfs *dfs)
342 {
343 	qdf_timer_free(&dfs->wlan_dfs_task_timer);
344 	dfs->wlan_radar_tasksched = 0;
345 }
346 
347 #if defined(WLAN_DFS_PARTIAL_OFFLOAD) && defined(HOST_DFS_SPOOF_TEST)
348 void dfs_host_wait_timer_detach(struct wlan_dfs *dfs)
349 {
350 	qdf_timer_free(&dfs->dfs_host_wait_timer);
351 }
352 #endif
353 
354 void dfs_main_detach(struct wlan_dfs *dfs)
355 {
356 	int n, empty;
357 
358 	if (!dfs->dfs_enable) {
359 		dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "Already detached");
360 		return;
361 	}
362 
363 	dfs->dfs_enable = 0;
364 
365 	dfs_reset_radarq(dfs);
366 	dfs_reset_alldelaylines(dfs);
367 
368 	if (dfs->pulses) {
369 		dfs_free_dfs_pulseline(dfs->pulses);
370 		dfs->pulses = NULL;
371 	}
372 
373 	for (n = 0; n < DFS_MAX_RADAR_TYPES; n++) {
374 		if (dfs->dfs_radarf[n]) {
375 			dfs_free_filter(dfs->dfs_radarf[n]);
376 			qdf_mem_free(dfs->dfs_radarf[n]);
377 			dfs->dfs_radarf[n] = NULL;
378 		}
379 	}
380 
381 	if (dfs->dfs_ftindextable) {
382 		for (n = 0; n < DFS_NUM_FT_IDX_TBL_ROWS; n++) {
383 			if (dfs->dfs_ftindextable[n]) {
384 				qdf_mem_free(dfs->dfs_ftindextable[n]);
385 				dfs->dfs_ftindextable[n] = NULL;
386 			}
387 		}
388 		qdf_mem_free(dfs->dfs_ftindextable);
389 		dfs->dfs_ftindextable = NULL;
390 		dfs->wlan_dfs_isdfsregdomain = 0;
391 	}
392 
393 	if (dfs->dfs_b5radars) {
394 		qdf_mem_free(dfs->dfs_b5radars);
395 		dfs->dfs_b5radars = NULL;
396 	}
397 
398 	dfs_reset_ar(dfs);
399 
400 	WLAN_ARQ_LOCK(dfs);
401 	empty = STAILQ_EMPTY(&(dfs->dfs_arq));
402 	WLAN_ARQ_UNLOCK(dfs);
403 	if (!empty)
404 		dfs_reset_arq(dfs);
405 
406 	if (dfs->events) {
407 		dfs_free_dfs_events(dfs->events);
408 		dfs->events = NULL;
409 	}
410 
411 	WLAN_DFS_DATA_STRUCT_LOCK_DESTROY(dfs);
412 	WLAN_DFSQ_LOCK_DESTROY(dfs);
413 	WLAN_ARQ_LOCK_DESTROY(dfs);
414 	WLAN_DFSEVENTQ_LOCK_DESTROY(dfs);
415 }
416 
417 int dfs_start_host_based_bangradar(struct wlan_dfs *dfs)
418 {
419 	dfs->wlan_radar_tasksched = 1;
420 	qdf_timer_mod(&dfs->wlan_dfs_task_timer, 0);
421 
422 	return 0;
423 }
424