xref: /wlan-dirver/qca-wifi-host-cmn/umac/dfs/core/src/misc/dfs_cac.c (revision 901120c066e139c7f8a2c8e4820561fdd83c67ef)
1 /*
2  * Copyright (c) 2016-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
4  * All rights reserved.
5  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 /**
29  * DOC: This file has the functions related to DFS CAC.
30  */
31 
32 #include "../dfs_channel.h"
33 #include "../dfs_zero_cac.h"
34 #include <wlan_objmgr_vdev_obj.h>
35 #include "wlan_dfs_utils_api.h"
36 #include "wlan_dfs_mlme_api.h"
37 #include "../dfs_internal.h"
38 #include "../dfs_process_radar_found_ind.h"
39 
40 #define IS_CHANNEL_WEATHER_RADAR(freq) ((freq >= 5600) && (freq <= 5650))
41 #define ADJACENT_WEATHER_RADAR_CHANNEL   5580
42 #define CH100_START_FREQ                 5490
43 #define CH100                            100
44 
45 /**
46  * dfs_cac_valid_timeout() - Timeout function for dfs_cac_valid_timer
47  *                           cac_valid bit will be reset in this function.
48  */
49 static os_timer_func(dfs_cac_valid_timeout)
50 {
51 	struct wlan_dfs *dfs = NULL;
52 
53 	OS_GET_TIMER_ARG(dfs, struct wlan_dfs *);
54 	dfs->dfs_cac_valid = 0;
55 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, ": Timed out!!");
56 }
57 
58 /**
59  * dfs_clear_cac_started_chan() - Clear dfs cac started channel.
60  * @dfs: Pointer to wlan_dfs structure.
61  */
62 void dfs_clear_cac_started_chan(struct wlan_dfs *dfs)
63 {
64 	qdf_mem_zero(&dfs->dfs_cac_started_chan,
65 		     sizeof(dfs->dfs_cac_started_chan));
66 }
67 
68 static void dfs_clear_nol_history_for_curchan(struct wlan_dfs *dfs)
69 {
70 	struct dfs_channel *chan = dfs->dfs_curchan;
71 	uint16_t sub_channels[MAX_20MHZ_SUBCHANS];
72 	uint8_t num_subchs;
73 
74 	num_subchs = dfs_get_bonding_channel_without_seg_info_for_freq(
75 				chan, sub_channels);
76 
77 	if (dfs->dfs_is_stadfs_enabled)
78 		if (dfs_mlme_is_opmode_sta(dfs->dfs_pdev_obj))
79 			utils_dfs_reg_update_nol_history_chan_for_freq(
80 				dfs->dfs_pdev_obj, sub_channels,
81 				num_subchs, DFS_NOL_HISTORY_RESET);
82 }
83 
84 void dfs_process_cac_completion(struct wlan_dfs *dfs)
85 {
86 	enum phy_ch_width ch_width = CH_WIDTH_INVALID;
87 	uint16_t primary_chan_freq = 0, sec_chan_freq = 0;
88 	struct dfs_channel *dfs_curchan;
89 
90 	dfs->dfs_cac_timer_running = 0;
91 	dfs_curchan = dfs->dfs_curchan;
92 
93 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "cac expired, chan %d cur time %d",
94 		 dfs->dfs_curchan->dfs_ch_freq,
95 		 (qdf_system_ticks_to_msecs(qdf_system_ticks()) / 1000));
96 
97 	/*
98 	 * When radar is detected during a CAC we are woken up prematurely to
99 	 * switch to a new channel. Check the channel to decide how to act.
100 	 */
101 	if (WLAN_IS_CHAN_RADAR(dfs, dfs->dfs_curchan)) {
102 		dfs_mlme_mark_dfs(dfs->dfs_pdev_obj,
103 				  dfs_curchan->dfs_ch_ieee,
104 				  dfs_curchan->dfs_ch_freq,
105 				  dfs_curchan->dfs_ch_mhz_freq_seg2,
106 				  dfs_curchan->dfs_ch_flags,
107 				  0);
108 		dfs_debug(dfs, WLAN_DEBUG_DFS,
109 			  "CAC timer on chan %u (%u MHz) stopped due to radar",
110 			  dfs_curchan->dfs_ch_ieee,
111 			  dfs_curchan->dfs_ch_freq);
112 	} else {
113 		dfs_debug(dfs, WLAN_DEBUG_DFS,
114 			  "CAC timer on channel %u (%u MHz) expired;"
115 			  "no radar detected",
116 			  dfs_curchan->dfs_ch_ieee,
117 			  dfs_curchan->dfs_ch_freq);
118 
119 		/* On CAC completion, set the bit 'cac_valid'.
120 		 * CAC will not be re-done if this bit is reset.
121 		 * The flag will be reset when dfs_cac_valid_timer
122 		 * timesout.
123 		 */
124 		if (dfs->dfs_cac_valid_time) {
125 			dfs->dfs_cac_valid = 1;
126 			qdf_timer_mod(&dfs->dfs_cac_valid_timer,
127 				      dfs->dfs_cac_valid_time * 1000);
128 		}
129 
130 		dfs_find_curchwidth_and_center_chan_for_freq(dfs,
131 							     &ch_width,
132 							     &primary_chan_freq,
133 							     &sec_chan_freq);
134 
135 		/* ETSI allows the driver to cache the CAC ( Once CAC done,
136 		 * it can be used in future).
137 		 * Therefore mark the current channel CAC done.
138 		 */
139 		if (utils_get_dfsdomain(dfs->dfs_pdev_obj) == DFS_ETSI_DOMAIN)
140 			dfs_mark_precac_done_for_freq(dfs,
141 						      primary_chan_freq,
142 						      sec_chan_freq,
143 						      ch_width);
144 	}
145 
146 	dfs_clear_cac_started_chan(dfs);
147 
148 	/* Clear NOL history for current channel on successful CAC completion */
149 	dfs_clear_nol_history_for_curchan(dfs);
150 	/* Iterate over the nodes, processing the CAC completion event. */
151 	dfs_mlme_proc_cac(dfs->dfs_pdev_obj, 0);
152 
153 	/* Send a CAC timeout, VAP up event to user space */
154 	dfs_mlme_deliver_event_up_after_cac(dfs->dfs_pdev_obj);
155 
156 	if (dfs->dfs_defer_precac_channel_change == 1) {
157 		dfs_mlme_channel_change_by_precac(dfs->dfs_pdev_obj);
158 		dfs->dfs_defer_precac_channel_change = 0;
159 	}
160 }
161 
162 /**
163  * dfs_cac_timeout() - DFS cactimeout function.
164  *
165  * Sets dfs_cac_timer_running to 0  and dfs_cac_valid_timer.
166  */
167 #ifdef CONFIG_CHAN_FREQ_API
168 static enum qdf_hrtimer_restart_status
169 dfs_cac_timeout(qdf_hrtimer_data_t *arg)
170 {
171 	struct wlan_dfs *dfs;
172 
173 	dfs = container_of(arg, struct wlan_dfs, dfs_cac_timer);
174 
175 	if (dfs_is_hw_mode_switch_in_progress(dfs))
176 		dfs->dfs_defer_params.is_cac_completed = true;
177 	else
178 		dfs_process_cac_completion(dfs);
179 
180 	return QDF_HRTIMER_NORESTART;
181 }
182 #endif
183 
184 #ifdef QCA_SUPPORT_DFS_CAC
185 void dfs_cac_timer_attach(struct wlan_dfs *dfs)
186 {
187 	dfs->dfs_cac_timeout_override = -1;
188 	dfs->wlan_dfs_cac_time = WLAN_DFS_WAIT_MS;
189 	qdf_hrtimer_init(&dfs->dfs_cac_timer,
190 			 dfs_cac_timeout,
191 			 QDF_CLOCK_MONOTONIC,
192 			 QDF_HRTIMER_MODE_REL,
193 			 QDF_CONTEXT_TASKLET);
194 	qdf_timer_init(NULL,
195 			&(dfs->dfs_cac_valid_timer),
196 			dfs_cac_valid_timeout,
197 			(void *)(dfs),
198 			QDF_TIMER_TYPE_WAKE_APPS);
199 }
200 
201 void dfs_cac_timer_reset(struct wlan_dfs *dfs)
202 {
203 	qdf_hrtimer_cancel(&dfs->dfs_cac_timer);
204 	dfs_get_override_cac_timeout(dfs,
205 			&(dfs->dfs_cac_timeout_override));
206 	dfs_clear_cac_started_chan(dfs);
207 }
208 
209 void dfs_cac_timer_detach(struct wlan_dfs *dfs)
210 {
211 	qdf_hrtimer_kill(&dfs->dfs_cac_timer);
212 	qdf_timer_free(&dfs->dfs_cac_valid_timer);
213 	dfs->dfs_cac_valid = 0;
214 }
215 
216 int dfs_is_ap_cac_timer_running(struct wlan_dfs *dfs)
217 {
218 	return dfs->dfs_cac_timer_running;
219 }
220 
221 #ifdef CONFIG_CHAN_FREQ_API
222 void dfs_start_cac_timer(struct wlan_dfs *dfs)
223 {
224 	int cac_timeout = 0;
225 	struct dfs_channel *chan = dfs->dfs_curchan;
226 
227 	cac_timeout =
228 	    dfs_mlme_get_cac_timeout_for_freq(dfs->dfs_pdev_obj,
229 					      chan->dfs_ch_freq,
230 					      chan->dfs_ch_mhz_freq_seg2,
231 					      chan->dfs_ch_flags);
232 
233 	dfs->dfs_cac_started_chan = *chan;
234 
235 	dfs_deliver_cac_state_events(dfs);
236 	dfs_debug(dfs, WLAN_DEBUG_DFS,
237 		  "chan = %d cfreq2 = %d timeout = %d sec, curr_time = %d sec",
238 		  chan->dfs_ch_ieee, chan->dfs_ch_vhtop_ch_freq_seg2,
239 		  cac_timeout,
240 		  qdf_system_ticks_to_msecs(qdf_system_ticks()) / 1000);
241 
242 	qdf_hrtimer_start(&dfs->dfs_cac_timer,
243 			  qdf_time_ms_to_ktime(cac_timeout * 1000),
244 			  QDF_HRTIMER_MODE_REL);
245 	dfs->dfs_cac_aborted = 0;
246 }
247 #endif
248 
249 void dfs_cancel_cac_timer(struct wlan_dfs *dfs)
250 {
251 	qdf_hrtimer_cancel(&dfs->dfs_cac_timer);
252 	dfs_clear_cac_started_chan(dfs);
253 }
254 
255 void dfs_send_dfs_events_for_chan(struct wlan_dfs *dfs,
256 				  struct dfs_channel *chan,
257 				  enum WLAN_DFS_EVENTS event)
258 {
259 	uint8_t nchannels, i;
260 	qdf_freq_t freq_list[MAX_20MHZ_SUBCHANS];
261 
262 	nchannels =
263 		dfs_get_bonding_channel_without_seg_info_for_freq(chan,
264 								  freq_list);
265 	for (i = 0; i < nchannels; i++)
266 		utils_dfs_deliver_event(dfs->dfs_pdev_obj,
267 					freq_list[i],
268 					event);
269 }
270 
271 void dfs_cac_stop(struct wlan_dfs *dfs)
272 {
273 	uint32_t phyerr;
274 	struct dfs_channel *chan;
275 
276 	chan = &dfs->dfs_cac_started_chan;
277 	dfs_get_debug_info(dfs, (void *)&phyerr);
278 	dfs_debug(dfs, WLAN_DEBUG_DFS,
279 		"Stopping CAC Timer %d procphyerr 0x%08x",
280 		 dfs->dfs_curchan->dfs_ch_freq, phyerr);
281 	qdf_hrtimer_cancel(&dfs->dfs_cac_timer);
282 
283 	dfs_send_dfs_events_for_chan(dfs, chan, WLAN_EV_CAC_RESET);
284 
285 	if (dfs->dfs_cac_timer_running)
286 		dfs->dfs_cac_aborted = 1;
287 	dfs_clear_cac_started_chan(dfs);
288 	dfs->dfs_cac_timer_running = 0;
289 }
290 
291 void dfs_stacac_stop(struct wlan_dfs *dfs)
292 {
293 	uint32_t phyerr;
294 
295 	dfs_get_debug_info(dfs, (void *)&phyerr);
296 	dfs_debug(dfs, WLAN_DEBUG_DFS,
297 		"Stopping STA CAC Timer %d procphyerr 0x%08x",
298 		 dfs->dfs_curchan->dfs_ch_freq, phyerr);
299 	dfs_clear_cac_started_chan(dfs);
300 }
301 
302 int dfs_override_cac_timeout(struct wlan_dfs *dfs, int cac_timeout)
303 {
304 	if (!dfs)
305 		return -EIO;
306 
307 	dfs->dfs_cac_timeout_override = cac_timeout;
308 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "CAC timeout is now %s %d",
309 		 (cac_timeout == -1) ? "default" : "overridden",
310 		 cac_timeout);
311 
312 	return 0;
313 }
314 
315 int dfs_get_override_cac_timeout(struct wlan_dfs *dfs, int *cac_timeout)
316 {
317 	if (!dfs)
318 		return -EIO;
319 
320 	(*cac_timeout) = dfs->dfs_cac_timeout_override;
321 
322 	return 0;
323 }
324 
325 #ifdef CONFIG_CHAN_FREQ_API
326 void dfs_cac_valid_reset_for_freq(struct wlan_dfs *dfs,
327 				  uint16_t prevchan_freq,
328 				  uint32_t prevchan_flags)
329 {
330 	if (dfs->dfs_cac_valid_time) {
331 		if ((prevchan_freq != dfs->dfs_curchan->dfs_ch_freq) ||
332 		    (prevchan_flags != dfs->dfs_curchan->dfs_ch_flags)) {
333 			dfs_err(dfs, WLAN_DEBUG_DFS_ALWAYS,
334 				"Cancelling timer & clearing cac_valid");
335 			qdf_timer_stop(&dfs->dfs_cac_valid_timer);
336 			dfs->dfs_cac_valid = 0;
337 		}
338 	}
339 }
340 #endif
341 #endif
342 
343 #ifdef CONFIG_CHAN_FREQ_API
344 bool
345 dfs_is_subset_channel_for_freq(uint16_t *old_subchans_freq,
346 			       uint8_t old_n_chans,
347 			       uint16_t *new_subchans_freq,
348 			       uint8_t new_n_chans)
349 {
350 	bool is_found;
351 	int i, j;
352 
353 	if (!new_n_chans)
354 		return true;
355 
356 	if (new_n_chans > old_n_chans)
357 		return false;
358 
359 	for (i = 0; i < new_n_chans; i++) {
360 		is_found = false;
361 		for (j = 0; j < old_n_chans; j++) {
362 			if (new_subchans_freq[i] == old_subchans_freq[j]) {
363 				is_found = true;
364 				break;
365 			}
366 		}
367 
368 		/* If new_subchans[i] is not found in old_subchans, then,
369 		 * new_chan is not subset of old_chan.
370 		 */
371 		if (!is_found)
372 			break;
373 	}
374 
375 	return is_found;
376 }
377 #endif
378 
379 #ifdef CONFIG_CHAN_FREQ_API
380 uint8_t
381 dfs_find_dfs_sub_channels_for_freq(struct wlan_dfs *dfs,
382 				   struct dfs_channel *chan,
383 				   uint16_t *subchan_arr)
384 {
385 	if (WLAN_IS_CHAN_MODE_160(chan) || WLAN_IS_CHAN_MODE_80_80(chan)) {
386 		if (WLAN_IS_CHAN_DFS(chan) && WLAN_IS_CHAN_DFS_CFREQ2(chan))
387 			return dfs_get_bonding_channel_without_seg_info_for_freq
388 				(chan, subchan_arr);
389 		if (WLAN_IS_CHAN_DFS(chan))
390 			return dfs_get_bonding_channels_for_freq(dfs,
391 								 chan,
392 								 SEG_ID_PRIMARY,
393 								 DETECTOR_ID_0,
394 								 subchan_arr);
395 		if (WLAN_IS_CHAN_DFS_CFREQ2(chan))
396 			return dfs_get_bonding_channels_for_freq
397 				(dfs, chan, SEG_ID_SECONDARY,
398 				 DETECTOR_ID_0, subchan_arr);
399 		/* All channels in 160/80_80 BW are non DFS, return 0
400 		 * as number of subchannels
401 		 */
402 		return 0;
403 	} else if (WLAN_IS_CHAN_DFS(chan)) {
404 		return dfs_get_bonding_channel_without_seg_info_for_freq
405 			(chan, subchan_arr);
406 	}
407 	/* All channels are non DFS, return 0 as number of subchannels*/
408 	return 0;
409 }
410 #endif
411 
412 #ifdef CONFIG_CHAN_FREQ_API
413 bool
414 dfs_is_new_chan_subset_of_old_chan(struct wlan_dfs *dfs,
415 				   struct dfs_channel *new_chan,
416 				   struct dfs_channel *old_chan)
417 {
418 	uint16_t new_subchans[MAX_20MHZ_SUBCHANS];
419 	uint16_t old_subchans[MAX_20MHZ_SUBCHANS];
420 	uint8_t n_new_subchans = 0;
421 	uint8_t n_old_subchans = 0;
422 
423 	/* Given channel is the old channel. i.e. The channel which
424 	 * should have the new channel as subset.
425 	 */
426 	n_old_subchans = dfs_find_dfs_sub_channels_for_freq(dfs, old_chan,
427 							    old_subchans);
428 	/* cur_chan is the new channel to be check if subset of old channel */
429 	n_new_subchans = dfs_find_dfs_sub_channels_for_freq(dfs, new_chan,
430 							    new_subchans);
431 
432 	return dfs_is_subset_channel_for_freq(old_subchans,
433 					      n_old_subchans,
434 					      new_subchans,
435 					      n_new_subchans);
436 }
437 #endif
438 
439 #ifdef QCA_SUPPORT_DFS_CAC
440 bool dfs_is_cac_required(struct wlan_dfs *dfs,
441 			 struct dfs_channel *cur_chan,
442 			 struct dfs_channel *prev_chan,
443 			 bool *continue_current_cac,
444 			 bool is_vap_restart)
445 {
446 	struct dfs_channel *cac_started_chan = &dfs->dfs_cac_started_chan;
447 
448 	if (!WLAN_IS_PRIMARY_OR_SECONDARY_CHAN_DFS(cur_chan)) {
449 		dfs_debug(dfs, WLAN_DEBUG_DFS, "Skip CAC on non-DFS channel");
450 		return false;
451 	}
452 
453 	if (dfs->dfs_ignore_dfs || dfs->dfs_cac_valid || dfs->dfs_ignore_cac) {
454 		dfs_debug(dfs, WLAN_DEBUG_DFS,
455 			  "Skip CAC, ignore_dfs = %d cac_valid = %d ignore_cac = %d",
456 			  dfs->dfs_ignore_dfs, dfs->dfs_cac_valid,
457 			  dfs->dfs_ignore_cac);
458 		return false;
459 	}
460 
461 	/* In case of RCAC, check if CAC is completed only on the RCAC channel
462 	 * and do not check the CAC info on current operating channel.
463 	 */
464 	if (dfs_is_agile_rcac_enabled(dfs) &&
465 	    dfs_is_rcac_cac_done(dfs, cur_chan, prev_chan))
466 		return false;
467 
468 	/* If the channel has completed PRE-CAC then CAC can be skipped here. */
469 	if (dfs_is_precac_done(dfs, cur_chan)) {
470 		dfs_debug(dfs, WLAN_DEBUG_DFS,
471 			  "PRE-CAC already done on this channel %d",
472 			  cur_chan->dfs_ch_ieee);
473 		return false;
474 	}
475 
476 	if (dfs_is_ap_cac_timer_running(dfs)) {
477 		/* Check if we should continue the existing CAC or
478 		 * cancel the existing CAC.
479 		 * For example: - if an existing VAP(0) is already in
480 		 * DFS wait state (which means the radio(wifi) is
481 		 * running the CAC) and it is in channel A and another
482 		 * VAP(1) comes up in the same channel then instead of
483 		 * cancelling the CAC we can let the CAC continue.
484 		 */
485 		if (dfs_is_new_chan_subset_of_old_chan(dfs,
486 						       cur_chan,
487 						       cac_started_chan)) {
488 			if (continue_current_cac)
489 				*continue_current_cac = true;
490 		} else {
491 			/* New CAC is needed, cancel the running CAC
492 			 * timer.
493 			 * 1) When AP is in DFS_WAIT state and it is in
494 			 *    channel A and user restarts the AP vap in
495 			 *    channel B, then cancel the running CAC in
496 			 *    channel A and start new CAC in channel B.
497 			 *
498 			 * 2) When AP detects the RADAR during CAC in
499 			 *    channel A, it cancels the running CAC and
500 			 *    tries to find channel B with the reduced
501 			 *    bandwidth with of channel A.
502 			 *    In this case, since the CAC is aborted by
503 			 *    the RADAR, AP should start the CAC again.
504 			 */
505 			dfs_cancel_cac_timer(dfs);
506 		}
507 	} else { /* CAC timer is not running. */
508 		/* If channel change happens via VAP DOWN/UP on subset channels,
509 		 * (eg: from 52 HT80 to 64 HT80) CAC done information
510 		 * (of 52 HT80) based on subset logic
511 		 * (as 52 and 64 HT80 are subsets of each other)
512 		 * is not expected to be preserved as VAP has come up
513 		 * from DOWN state. Hence do not skip CAC on 64 HT80.
514 		 * is_vap_restart flag is used as an identifier to indicate if
515 		 * vap has come up from a DOWN state or UP state (vap restart).
516 		 */
517 		if (!is_vap_restart) {
518 			dfs_debug(dfs, WLAN_DEBUG_DFS, "CAC is needed");
519 			return true;
520 		}
521 		if (dfs_is_new_chan_subset_of_old_chan(dfs,
522 						       cur_chan,
523 						       prev_chan)) {
524 			/* AP bandwidth reduce case:
525 			 * When AP detects the RADAR in in-service monitoring
526 			 * mode in channel A, it cancels the running CAC and
527 			 * tries to find the channel B with the reduced
528 			 * bandwidth of channel A.
529 			 * If the new channel B is subset of the channel A
530 			 * then AP skips the CAC.
531 			 */
532 			if (!dfs->dfs_cac_aborted) {
533 				dfs_debug(dfs, WLAN_DEBUG_DFS, "Skip CAC");
534 				return false;
535 			}
536 		}
537 	}
538 
539 	return true;
540 }
541 #endif
542