xref: /wlan-dirver/qca-wifi-host-cmn/umac/dfs/core/src/filtering/dfs_process_radarevent.c (revision 87a8e4458319c60b618522e263ed900e36aab528)
1 /*
2  * Copyright (c) 2013, 2016-2018 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2002-2010, 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 contains the functionality to process the radar event generated
20  * for a pulse. This will group together pulses and call various detection
21  * functions to figure out whether a valid radar has been detected.
22  */
23 
24 #include "../dfs.h"
25 #include "../dfs_zero_cac.h"
26 #include "../dfs_channel.h"
27 #include "../dfs_internal.h"
28 #include "../dfs_process_radar_found_ind.h"
29 #include "wlan_dfs_utils_api.h"
30 #include "wlan_dfs_lmac_api.h"
31 #include "../dfs_partial_offload_radar.h"
32 
33 #define FREQ_5500_MHZ  5500
34 #define FREQ_5500_MHZ       5500
35 
36 #define DFS_MAX_FREQ_SPREAD            (1375 * 1)
37 #define DFS_LARGE_PRI_MULTIPLIER       4
38 #define DFS_W53_DEFAULT_PRI_MULTIPLIER 2
39 #define DFS_INVALID_PRI_LIMIT 100  /* should we use 135? */
40 #define DFS_BIG_SIDX          10000
41 
42 #define FRAC_PRI_SCORE_ARRAY_SIZE 40
43 
44 static char debug_dup[33];
45 static int debug_dup_cnt;
46 
47 /**
48  * dfs_process_pulse_dur() - Process pulse duration.
49  * @dfs: Pointer to wlan_dfs structure.
50  * @re_dur: Duration.
51  *
52  * Convert the hardware provided duration to TSF ticks (usecs) taking the clock
53  * (fast or normal) into account. Legacy (pre-11n, Owl, Sowl, Howl) operate
54  * 5GHz using a 40MHz clock.  Later 11n chips (Merlin, Osprey, etc) operate
55  * 5GHz using a 44MHz clock, so the reported pulse durations are different.
56  * Peregrine reports the pulse duration in microseconds regardless of the
57  * operating mode. (XXX TODO: verify this, obviously.)
58  *
59  * The hardware returns the duration in a variety of formats,
60  * so it's converted from the hardware format to TSF (usec)
61  * values here.
62  * XXX TODO: this should really be done when the PHY error
63  * is processed, rather than way out here..
64  *
65  *
66  * Return: Returns the duration.
67  */
68 static inline uint8_t dfs_process_pulse_dur(struct wlan_dfs *dfs,
69 		uint8_t re_dur)
70 {
71 	/*
72 	 * Short pulses are sometimes returned as having a duration of 0,
73 	 * so round those up to 1.
74 	 * XXX This holds true for BB TLV chips too, right?
75 	 */
76 	if (re_dur == 0)
77 		return 1;
78 
79 	/*
80 	 * For BB TLV chips, the hardware always returns microsecond pulse
81 	 * durations.
82 	 */
83 	if (dfs->dfs_caps.wlan_chip_is_bb_tlv)
84 		return re_dur;
85 
86 	/*
87 	 * This is for 11n and legacy chips, which may or may not use the 5GHz
88 	 * fast clock mode.
89 	 */
90 	/* Convert 0.8us durations to TSF ticks (usecs) */
91 	return (uint8_t)dfs_round((int32_t)((dfs->dur_multiplier)*re_dur));
92 }
93 
94 /*
95  * dfs_print_radar_events() - Prints the Radar events.
96  * @dfs: Pointer to wlan_dfs structure.
97  */
98 static void dfs_print_radar_events(struct wlan_dfs *dfs)
99 {
100 	int i;
101 
102 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "#Phyerr=%d, #false detect=%d, #queued=%d",
103 		 dfs->dfs_phyerr_count, dfs->dfs_phyerr_reject_count,
104 		dfs->dfs_phyerr_queued_count);
105 
106 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS, "dfs_phyerr_freq_min=%d, dfs_phyerr_freq_max=%d",
107 		 dfs->dfs_phyerr_freq_min, dfs->dfs_phyerr_freq_max);
108 
109 	dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS,
110 		"Total radar events detected=%d, entries in the radar queue follows:",
111 		 dfs->dfs_event_log_count);
112 
113 	for (i = 0; (i < DFS_EVENT_LOG_SIZE) && (i < dfs->dfs_event_log_count);
114 			i++) {
115 		dfs_debug(dfs, WLAN_DEBUG_DFS,
116 			"ts=%llu diff_ts=%u rssi=%u dur=%u, is_chirp=%d, seg_id=%d, sidx=%d, freq_offset=%d.%dMHz, peak_mag=%d, total_gain=%d, mb_gain=%d, relpwr_db=%d, delta_diff=%d, delta_peak=%d, psidx_diff=%d",
117 			dfs->radar_log[i].ts, dfs->radar_log[i].diff_ts,
118 			dfs->radar_log[i].rssi, dfs->radar_log[i].dur,
119 			dfs->radar_log[i].is_chirp, dfs->radar_log[i].seg_id,
120 			dfs->radar_log[i].sidx,
121 			(int)dfs->radar_log[i].freq_offset_khz/1000,
122 			(int)abs(dfs->radar_log[i].freq_offset_khz)%1000,
123 			dfs->radar_log[i].peak_mag,
124 			dfs->radar_log[i].total_gain,
125 			dfs->radar_log[i].mb_gain,
126 			dfs->radar_log[i].relpwr_db,
127 			dfs->radar_log[i].delta_diff,
128 			dfs->radar_log[i].delta_peak,
129 			dfs->radar_log[i].psidx_diff);
130 	}
131 	dfs->dfs_event_log_count = 0;
132 	dfs->dfs_phyerr_count = 0;
133 	dfs->dfs_phyerr_reject_count = 0;
134 	dfs->dfs_phyerr_queued_count = 0;
135 	dfs->dfs_phyerr_freq_min = 0x7fffffff;
136 	dfs->dfs_phyerr_freq_max = 0;
137 }
138 
139 /**
140  * dfs_confirm_radar() - This function checks for fractional PRI and jitter in
141  * sidx index to determine if the radar is real or not.
142  * @dfs: Pointer to dfs structure.
143  * @rf: Pointer to dfs_filter structure.
144  * @ext_chan_flag: ext chan flags.
145  */
146 static int dfs_confirm_radar(struct wlan_dfs *dfs,
147 		struct dfs_filter *rf,
148 		int ext_chan_flag)
149 {
150 	int i = 0;
151 	int index;
152 	struct dfs_delayline *dl = &rf->rf_dl;
153 	struct dfs_delayelem *de;
154 	uint64_t target_ts = 0;
155 	struct dfs_pulseline *pl;
156 	int start_index = 0, current_index, next_index;
157 	unsigned char scores[FRAC_PRI_SCORE_ARRAY_SIZE];
158 	uint32_t pri_margin;
159 	uint64_t this_diff_ts;
160 	uint32_t search_bin;
161 
162 	unsigned char max_score = 0;
163 	int max_score_index = 0;
164 
165 	pl = dfs->pulses;
166 
167 	OS_MEMZERO(scores, sizeof(scores));
168 	scores[0] = rf->rf_threshold;
169 
170 	pri_margin = dfs_get_pri_margin(dfs, ext_chan_flag,
171 			(rf->rf_patterntype == 1));
172 
173 	/*
174 	 * Look for the entry that matches dl_seq_num_second.
175 	 * we need the time stamp and diff_ts from there.
176 	 */
177 
178 	for (i = 0; i < dl->dl_numelems; i++) {
179 		index = (dl->dl_firstelem + i) & DFS_MAX_DL_MASK;
180 		de = &dl->dl_elems[index];
181 		if (dl->dl_seq_num_second == de->de_seq_num)
182 			target_ts = de->de_ts - de->de_time;
183 	}
184 
185 	if (dfs->dfs_debug_mask & WLAN_DEBUG_DFS2) {
186 		dfs_print_delayline(dfs, &rf->rf_dl);
187 
188 		/* print pulse line */
189 		dfs_debug(dfs, WLAN_DEBUG_DFS2,
190 			"%s: Pulse Line\n", __func__);
191 		for (i = 0; i < pl->pl_numelems; i++) {
192 			index =  (pl->pl_firstelem + i) &
193 				DFS_MAX_PULSE_BUFFER_MASK;
194 			dfs_debug(dfs, WLAN_DEBUG_DFS2,
195 					"Elem %u: ts=%llu dur=%u, seq_num=%d, delta_peak=%d, psidx_diff=%d\n",
196 					i, pl->pl_elems[index].p_time,
197 					pl->pl_elems[index].p_dur,
198 					pl->pl_elems[index].p_seq_num,
199 					pl->pl_elems[index].p_delta_peak,
200 					pl->pl_elems[index].p_psidx_diff);
201 		}
202 	}
203 
204 	/*
205 	 * Walk through the pulse line and find pulse with target_ts.
206 	 * Then continue until we find entry with seq_number dl_seq_num_stop.
207 	 */
208 
209 	for (i = 0; i < pl->pl_numelems; i++) {
210 		index =  (pl->pl_firstelem + i) & DFS_MAX_PULSE_BUFFER_MASK;
211 		if (pl->pl_elems[index].p_time == target_ts) {
212 			dl->dl_seq_num_start = pl->pl_elems[index].p_seq_num;
213 			start_index = index; /* save for future use */
214 		}
215 	}
216 
217 	dfs_debug(dfs, WLAN_DEBUG_DFS2,
218 			"%s: target_ts=%llu, dl_seq_num_start=%d, dl_seq_num_second=%d, dl_seq_num_stop=%d\n",
219 			__func__, target_ts, dl->dl_seq_num_start,
220 			dl->dl_seq_num_second, dl->dl_seq_num_stop);
221 
222 	current_index = start_index;
223 	while (pl->pl_elems[current_index].p_seq_num < dl->dl_seq_num_stop) {
224 		next_index = (current_index + 1) & DFS_MAX_PULSE_BUFFER_MASK;
225 		this_diff_ts = pl->pl_elems[next_index].p_time -
226 			pl->pl_elems[current_index].p_time;
227 
228 		/* Now update the score for this diff_ts */
229 		for (i = 1; i < FRAC_PRI_SCORE_ARRAY_SIZE; i++) {
230 			search_bin = dl->dl_search_pri / (i + 1);
231 
232 			/*
233 			 * We do not give score to PRI that is lower then the
234 			 * limit.
235 			 */
236 			if (search_bin < DFS_INVALID_PRI_LIMIT)
237 				break;
238 
239 			/*
240 			 * Increment the score if this_diff_ts belongs to this
241 			 * search_bin +/- margin.
242 			 */
243 			if ((this_diff_ts >= (search_bin - pri_margin)) &&
244 					(this_diff_ts <=
245 					 (search_bin + pri_margin))) {
246 				/*increment score */
247 				scores[i]++;
248 			}
249 		}
250 		current_index = next_index;
251 	}
252 
253 	for (i = 0; i < FRAC_PRI_SCORE_ARRAY_SIZE; i++)
254 		if (scores[i] > max_score) {
255 			max_score = scores[i];
256 			max_score_index = i;
257 		}
258 
259 	if (max_score_index != 0) {
260 		dfs_debug(dfs, WLAN_DEBUG_DFS_ALWAYS,
261 			  "Rejecting Radar since Fractional PRI detected: searchpri=%d, threshold=%d, fractional PRI=%d, Fractional PRI score=%d",
262 			  dl->dl_search_pri, scores[0],
263 			  dl->dl_search_pri/(max_score_index + 1),
264 			  max_score);
265 		return 0;
266 	}
267 
268 
269 	/* Check for frequency spread */
270 	if (dl->dl_min_sidx > pl->pl_elems[start_index].p_sidx)
271 		dl->dl_min_sidx = pl->pl_elems[start_index].p_sidx;
272 
273 	if (dl->dl_max_sidx < pl->pl_elems[start_index].p_sidx)
274 		dl->dl_max_sidx = pl->pl_elems[start_index].p_sidx;
275 
276 	if ((dl->dl_max_sidx - dl->dl_min_sidx) > rf->rf_sidx_spread) {
277 		dfs_debug(dfs, WLAN_DEBUG_DFS_ALWAYS,
278 			  "Rejecting Radar since frequency spread is too large : min_sidx=%d, max_sidx=%d, rf_sidx_spread=%d",
279 			  dl->dl_min_sidx, dl->dl_max_sidx,
280 			  rf->rf_sidx_spread);
281 		return 0;
282 	}
283 
284 	if ((rf->rf_check_delta_peak) &&
285 			((dl->dl_delta_peak_match_count +
286 			dl->dl_psidx_diff_match_count - 1) <
287 			rf->rf_threshold)) {
288 		dfs_debug(dfs, WLAN_DEBUG_DFS_ALWAYS,
289 			  "Rejecting Radar since delta peak values are invalid : dl_delta_peak_match_count=%d, dl_psidx_diff_match_count=%d, rf_threshold=%d",
290 			  dl->dl_delta_peak_match_count,
291 			  dl->dl_psidx_diff_match_count,
292 			  rf->rf_threshold);
293 		return 0;
294 	}
295 	dfs_debug(dfs, WLAN_DEBUG_DFS_FALSE_DET, "%s : dl->dl_min_sidx: %d , dl->dl_max_sidx :%d",
296 		  __func__, dl->dl_min_sidx, dl->dl_max_sidx);
297 
298 	dfs->dfs_freq_offset = DFS_SIDX_TO_FREQ_OFFSET((dl->dl_min_sidx +
299 							dl->dl_max_sidx) / 2);
300 	return 1;
301 }
302 
303 /*
304  * dfs_reject_on_pri() - Rejecting on individual filter based on min PRI .
305  * @dfs: Pointer to wlan_dfs structure.
306  * @rf: Pointer to dfs_filter structure.
307  * @deltaT: deltaT value.
308  * @this_ts: Timestamp.
309  */
310 static inline bool dfs_reject_on_pri(
311 		struct wlan_dfs *dfs,
312 		struct dfs_filter *rf,
313 		uint64_t deltaT,
314 		uint64_t this_ts)
315 {
316 	if ((deltaT < rf->rf_minpri) && (deltaT != 0)) {
317 		/* Second line of PRI filtering. */
318 		dfs_debug(dfs, WLAN_DEBUG_DFS2,
319 				"filterID %d : Rejecting on individual filter min PRI deltaT=%lld rf->rf_minpri=%u",
320 				rf->rf_pulseid, (uint64_t)deltaT,
321 				rf->rf_minpri);
322 		return 1;
323 	}
324 
325 	if (rf->rf_ignore_pri_window > 0) {
326 		if (deltaT < rf->rf_minpri) {
327 			dfs_debug(dfs, WLAN_DEBUG_DFS2,
328 					"filterID %d : Rejecting on individual filter max PRI deltaT=%lld rf->rf_minpri=%u",
329 					rf->rf_pulseid, (uint64_t)deltaT,
330 					rf->rf_minpri);
331 			/* But update the last time stamp. */
332 			rf->rf_dl.dl_last_ts = this_ts;
333 			return 1;
334 		}
335 	} else {
336 		/*
337 		 * The HW may miss some pulses especially with
338 		 * high channel loading. This is true for Japan
339 		 * W53 where channel loaoding is 50%. Also for
340 		 * ETSI where channel loading is 30% this can
341 		 * be an issue too. To take care of missing
342 		 * pulses, we introduce pri_margin multiplie.
343 		 * This is normally 2 but can be higher for W53.
344 		 */
345 
346 		if ((deltaT > (dfs->dfs_pri_multiplier * rf->rf_maxpri)) ||
347 				(deltaT < rf->rf_minpri)) {
348 			dfs_debug(dfs, WLAN_DEBUG_DFS2,
349 					"filterID %d : Rejecting on individual filter max PRI deltaT=%lld rf->rf_minpri=%u",
350 					rf->rf_pulseid, (uint64_t) deltaT,
351 					rf->rf_minpri);
352 			/* But update the last time stamp. */
353 			rf->rf_dl.dl_last_ts = this_ts;
354 			return 1;
355 		}
356 	}
357 
358 	return 0;
359 }
360 
361 /**
362  * dfs_confirm_radar_check() - Do additioal check to conirm radar except for
363  * the staggered, chirp FCC Bin 5, frequency hopping indicated by
364  * rf_patterntype == 1.
365  * @dfs: Pointer to wlan_dfs structure.
366  * @rf: Pointer to dfs_filter structure.
367  * @ext_chan_event_flag: Extension channel event flag
368  * @found: Pointer to radar found flag (return value).
369  * @false_radar_found: Pointer to false radar found (return value).
370  */
371 
372 static inline void dfs_confirm_radar_check(
373 		struct wlan_dfs *dfs,
374 		struct dfs_filter *rf,
375 		int ext_chan_event_flag,
376 		int *found,
377 		int *false_radar_found)
378 {
379 	if (rf->rf_patterntype != 1) {
380 		*found = dfs_confirm_radar(dfs, rf, ext_chan_event_flag);
381 		*false_radar_found = (*found == 1) ? 0 : 1;
382 	}
383 }
384 
385 void __dfs_process_radarevent(struct wlan_dfs *dfs,
386 		struct dfs_filtertype *ft,
387 		struct dfs_event *re,
388 		uint64_t this_ts,
389 		int *found,
390 		int *false_radar_found)
391 {
392 	int p;
393 	uint64_t deltaT = 0;
394 	int ext_chan_event_flag = 0;
395 	struct dfs_filter *rf = NULL;
396 	int8_t ori_rf_check_delta_peak = 0;
397 
398 	for (p = 0, *found = 0; (p < ft->ft_numfilters) &&
399 			(!(*found)) && !(*false_radar_found); p++) {
400 		rf = &(ft->ft_filters[p]);
401 		if ((re->re_dur >= rf->rf_mindur) &&
402 				(re->re_dur <= rf->rf_maxdur)) {
403 			/* The above check is probably not necessary. */
404 			deltaT = (this_ts < rf->rf_dl.dl_last_ts) ?
405 			    (int64_t)((DFS_TSF_WRAP - rf->rf_dl.dl_last_ts) +
406 				    this_ts + 1) :
407 			    this_ts - rf->rf_dl.dl_last_ts;
408 
409 			if (dfs_reject_on_pri(dfs, rf, deltaT, this_ts))
410 				continue;
411 
412 			dfs_add_pulse(dfs, rf, re, deltaT, this_ts);
413 
414 			/*
415 			 * If this is an extension channel event, flag it for
416 			 * false alarm reduction.
417 			 */
418 			if (re->re_chanindex == dfs->dfs_extchan_radindex)
419 				ext_chan_event_flag = 1;
420 
421 			if (rf->rf_patterntype == 2) {
422 				*found = dfs_staggered_check(dfs, rf,
423 					(uint32_t) deltaT, re->re_dur);
424 			} else {
425 				*found = dfs_bin_check(dfs, rf,
426 					(uint32_t) deltaT, re->re_dur,
427 					ext_chan_event_flag);
428 
429 				if (*found) {
430 					ori_rf_check_delta_peak =
431 						rf->rf_check_delta_peak;
432 					/*
433 					 * If FW does not send valid psidx_diff
434 					 * Do not do chirp check.
435 					 */
436 					if (rf->rf_check_delta_peak &&
437 						(!(re->re_flags &
438 						DFS_EVENT_VALID_PSIDX_DIFF)))
439 						rf->rf_check_delta_peak = false;
440 					dfs_confirm_radar_check(dfs,
441 							rf, ext_chan_event_flag,
442 							found,
443 							false_radar_found);
444 					rf->rf_check_delta_peak =
445 						ori_rf_check_delta_peak;
446 				}
447 			}
448 
449 			if (dfs->dfs_debug_mask & WLAN_DEBUG_DFS2)
450 				if (rf->rf_patterntype !=
451 						WLAN_DFS_RF_PATTERN_TYPE_1)
452 					dfs_print_delayline(dfs, &rf->rf_dl);
453 
454 			rf->rf_dl.dl_last_ts = this_ts;
455 		}
456 	}
457 
458 	if (*found) {
459 		dfs_info(dfs, WLAN_DEBUG_DFS_ALWAYS,
460 				"Found on channel minDur = %d, filterId = %d",
461 				ft->ft_mindur,
462 				rf != NULL ?  rf->rf_pulseid : -1);
463 	}
464 
465 	return;
466 }
467 
468 /**
469  * dfs_cal_average_radar_parameters() - Calculate the average radar parameters.
470  * @dfs: Pointer to wlan_dfs structure.
471  */
472 #if defined(WLAN_DFS_PARTIAL_OFFLOAD) && defined(HOST_DFS_SPOOF_TEST)
473 static void dfs_cal_average_radar_parameters(struct wlan_dfs *dfs)
474 {
475 	int i, count = 0;
476 	u_int32_t total_pri = 0;
477 	u_int32_t total_duration = 0;
478 	u_int32_t total_sidx = 0;
479 
480 	/* Calculating average PRI, Duration, SIDX from
481 	 * the 2nd pulse, ignoring the 1st pulse (radar_log[0]).
482 	 * This is because for the first pulse, the diff_ts will be
483 	 * (0 - current_ts) which will be a huge value.
484 	 * Average PRI computation will be wrong. FW returns a
485 	 * failure test result as PRI does not match their expected
486 	 * value.
487 	 */
488 
489 	for (i = 1; (i < DFS_EVENT_LOG_SIZE) && (i < dfs->dfs_event_log_count);
490 			i++) {
491 		total_pri +=  dfs->radar_log[i].diff_ts;
492 		total_duration += dfs->radar_log[i].dur;
493 		total_sidx +=  dfs->radar_log[i].sidx;
494 		count++;
495 	}
496 
497 	if (count > 0) {
498 		dfs->dfs_average_pri = total_pri / count;
499 		dfs->dfs_average_duration = total_duration / count;
500 		dfs->dfs_average_sidx = total_sidx / count;
501 
502 		dfs_info(dfs, WLAN_DEBUG_DFS2,
503 			 "Avg.PRI =%u, Avg.duration =%u Avg.sidx =%u",
504 			 dfs->dfs_average_pri,
505 			 dfs->dfs_average_duration,
506 			 dfs->dfs_average_sidx);
507 	}
508 }
509 #else
510 static void dfs_cal_average_radar_parameters(struct wlan_dfs *dfs)
511 {
512 }
513 #endif
514 
515 /**
516  * dfs_radarfound_reset_vars() - Reset dfs variables after radar found
517  * @dfs: Pointer to wlan_dfs structure.
518  * @rs: Pointer to dfs_state.
519  * @chan: Current  channel.
520  * @seg_id: Segment id.
521  */
522 static inline void dfs_radarfound_reset_vars(
523 		struct wlan_dfs *dfs,
524 		struct dfs_state *rs,
525 		struct dfs_channel *chan,
526 		uint8_t seg_id)
527 {
528 	struct dfs_channel *thischan;
529 
530 	/*
531 	 * TODO: Instead of discarding the radar, create a workqueue
532 	 * if the channel change is happenning through userspace and
533 	 * process the radar event once the channel change is completed.
534 	 */
535 
536 	/* Collect stats */
537 	dfs->wlan_dfs_stats.num_radar_detects++;
538 	thischan = &rs->rs_chan;
539 	if ((seg_id == SEG_ID_SECONDARY) &&
540 			(dfs_is_precac_timer_running(dfs)))
541 		dfs->is_radar_during_precac = 1;
542 
543 	/*
544 	 * If event log is on then dump the radar event queue on
545 	 * filter match. This can be used to collect information
546 	 * on false radar detection.
547 	 */
548 	if (dfs->dfs_event_log_on) {
549 		dfs_cal_average_radar_parameters(dfs);
550 		dfs_print_radar_events(dfs);
551 	}
552 
553 	dfs_reset_radarq(dfs);
554 	dfs_reset_alldelaylines(dfs);
555 
556 	dfs_debug(dfs, WLAN_DEBUG_DFS1,
557 			"Primary channel freq = %u flags=0x%x",
558 			chan->dfs_ch_freq, chan->dfs_ch_flagext);
559 
560 	if (chan->dfs_ch_freq != thischan->dfs_ch_freq)
561 		dfs_debug(dfs, WLAN_DEBUG_DFS1,
562 				"Ext channel freq = %u flags=0x%x",
563 				thischan->dfs_ch_freq,
564 				thischan->dfs_ch_flagext);
565 
566 	dfs->dfs_phyerr_freq_min = 0x7fffffff;
567 	dfs->dfs_phyerr_freq_max = 0;
568 	dfs->dfs_phyerr_w53_counter = 0;
569 
570 	if (seg_id == SEG_ID_SECONDARY) {
571 		dfs->wlan_dfs_stats.num_seg_two_radar_detects++;
572 		dfs->is_radar_found_on_secondary_seg = 1;
573 	}
574 }
575 
576 /**
577  * dfs_handle_bangradar - Handle the case of bangradar
578  * @dfs: Pointer to wlan_dfs structure.
579  * @chan: Current channel.
580  * @rs: Pointer to dfs_state.
581  * Return: if bangradar then  return 0.  Otherwise, return 1.
582  */
583 static inline int dfs_handle_bangradar(
584 	struct wlan_dfs *dfs,
585 	struct dfs_channel *chan,
586 	struct dfs_state **rs,
587 	uint8_t *seg_id,
588 	int *retval)
589 {
590 
591 	if (dfs->dfs_bangradar) {
592 		/*
593 		 * Bangradar will always simulate radar found on the primary
594 		 * channel.
595 		 */
596 		*rs = &dfs->dfs_radar[dfs->dfs_curchan_radindex];
597 		dfs->dfs_bangradar = 0; /* Reset */
598 		dfs_debug(dfs, WLAN_DEBUG_DFS, "bangradar");
599 		*retval = 1;
600 		return 1;
601 	}
602 
603 	if (dfs->dfs_second_segment_bangradar) {
604 		if (dfs_is_precac_timer_running(dfs) ||
605 				WLAN_IS_CHAN_11AC_VHT160(chan) ||
606 				WLAN_IS_CHAN_11AC_VHT80_80(chan)) {
607 			dfs->is_radar_found_on_secondary_seg = 1;
608 			*rs = &dfs->dfs_radar[dfs->dfs_curchan_radindex];
609 			dfs_debug(dfs, WLAN_DEBUG_DFS,
610 					"second segment bangradar on cfreq = %u",
611 					dfs->dfs_precac_secondary_freq);
612 			*retval = 1;
613 			*seg_id = SEG_ID_SECONDARY;
614 		} else {
615 			dfs_debug(dfs, WLAN_DEBUG_DFS,
616 					"Do not process the second segment bangradar");
617 		}
618 		dfs->dfs_second_segment_bangradar = 0; /* Reset */
619 		return 1;
620 	}
621 
622 	return 0;
623 }
624 
625 /**
626  * dfs_process_w53_pulses() - Prrocess w53 pulses
627  * @dfs: Pointer to wlan_dfs structure.
628  *
629  * For chips that support frequency information, we can relax PRI
630  * restriction if the frequency spread is narrow.
631  */
632 static inline void dfs_process_w53_pulses(
633 		struct wlan_dfs *dfs)
634 {
635 	if ((dfs->dfs_phyerr_freq_max - dfs->dfs_phyerr_freq_min) <
636 			DFS_MAX_FREQ_SPREAD)
637 		dfs->dfs_pri_multiplier = DFS_LARGE_PRI_MULTIPLIER;
638 
639 	dfs_debug(dfs, WLAN_DEBUG_DFS1,
640 			"w53_counter=%d, freq_max=%d, freq_min=%d, pri_multiplier=%d",
641 			 dfs->dfs_phyerr_w53_counter,
642 			dfs->dfs_phyerr_freq_max, dfs->dfs_phyerr_freq_min,
643 			dfs->dfs_pri_multiplier);
644 
645 	dfs->dfs_phyerr_freq_min = 0x7fffffff;
646 	dfs->dfs_phyerr_freq_max = 0;
647 }
648 
649 /**
650  * dfs_handle_missing_pulses - Handle the case of missing pulses
651  * @dfs: Pointer to wlan_dfs structure.
652  * @chan: Current channel.
653  *
654  * The HW may miss some pulses especially with high channel loading.
655  * This is true for Japan W53 where channel loaoding is 50%. Also
656  * for ETSI where channel loading is 30% this can be an issue too.
657  * To take care of missing pulses, we introduce pri_margin multiplie.
658  * This is normally 2 but can be higher for W53.
659  * Return: If not enough pulses return 0.  Otherwise, return 1.
660  */
661 static inline int dfs_handle_missing_pulses(
662 		struct wlan_dfs *dfs,
663 		struct dfs_channel *chan)
664 {
665 	if ((dfs->dfsdomain  == DFS_MKK4_DOMAIN) &&
666 			(dfs->dfs_caps.wlan_chip_is_bb_tlv) &&
667 			(chan->dfs_ch_freq < FREQ_5500_MHZ)) {
668 		dfs->dfs_pri_multiplier = DFS_W53_DEFAULT_PRI_MULTIPLIER;
669 		/*
670 		 * Do not process W53 pulses unless we have a minimum number
671 		 * of them.
672 		 */
673 		if (dfs->dfs_phyerr_w53_counter >= 5)
674 			dfs_process_w53_pulses(dfs);
675 		else
676 			return 0;
677 	}
678 
679 	dfs_debug(dfs, WLAN_DEBUG_DFS1, "pri_multiplier=%d",
680 			 dfs->dfs_pri_multiplier);
681 
682 	return 1;
683 }
684 
685 /**
686  * dfs_is_radarq_empty - check if radarq is empty
687  * @dfs: Pointer to wlan_dfs structure.
688  * @empty: Pointer to empty
689  */
690 static inline void dfs_is_radarq_empty(
691 		struct wlan_dfs *dfs,
692 		int *empty)
693 {
694 	WLAN_DFSQ_LOCK(dfs);
695 	*empty = STAILQ_EMPTY(&(dfs->dfs_radarq));
696 	WLAN_DFSQ_UNLOCK(dfs);
697 }
698 
699 /**
700  * dfs_remove_event_from_radarq - remove event from radarq
701  * @dfs: Pointer to wlan_dfs structure.
702  * @event: Double pointer to the event structure
703  */
704 static inline void dfs_remove_event_from_radarq(
705 		struct wlan_dfs *dfs,
706 		struct dfs_event **event)
707 {
708 	WLAN_DFSQ_LOCK(dfs);
709 	*event = STAILQ_FIRST(&(dfs->dfs_radarq));
710 	if (*event != NULL)
711 		STAILQ_REMOVE_HEAD(&(dfs->dfs_radarq), re_list);
712 	WLAN_DFSQ_UNLOCK(dfs);
713 }
714 
715 /**
716  * dfs_return_event_to_eventq - return event to eventq
717  * @dfs: Pointer to wlan_dfs structure.
718  * @event: Pointer to the event structure
719  */
720 static inline void dfs_return_event_to_eventq(
721 		struct wlan_dfs *dfs,
722 		struct dfs_event *event)
723 {
724 	qdf_mem_zero(event, sizeof(struct dfs_event));
725 	WLAN_DFSEVENTQ_LOCK(dfs);
726 	STAILQ_INSERT_TAIL(&(dfs->dfs_eventq), event, re_list);
727 	WLAN_DFSEVENTQ_UNLOCK(dfs);
728 }
729 
730 /**
731  * dfs_log_event - log dfs event
732  * @dfs: Pointer to wlan_dfs structure.
733  * @re:  Pointer to dfs_event re
734  * @this_ts: Current time stamp 64bit
735  * @diff_ts: Difference between 2 timestamps 32bit
736  * @index: Index value.
737  */
738 static inline void dfs_log_event(
739 		struct wlan_dfs *dfs,
740 		struct dfs_event *re,
741 		uint64_t this_ts,
742 		uint32_t diff_ts,
743 		uint32_t index)
744 {
745 	uint8_t i;
746 	struct dfs_pulseline *pl = dfs->pulses;
747 
748 	if (dfs->dfs_event_log_on) {
749 		i = dfs->dfs_event_log_count % DFS_EVENT_LOG_SIZE;
750 		dfs->radar_log[i].ts = this_ts;
751 		dfs->radar_log[i].diff_ts = diff_ts;
752 		dfs->radar_log[i].rssi = (*re).re_rssi;
753 		dfs->radar_log[i].dur = (*re).re_dur;
754 		dfs->radar_log[i].seg_id = (*re).re_seg_id;
755 		dfs->radar_log[i].sidx = (*re).re_sidx;
756 		dfs->radar_log[i].freq_offset_khz =
757 			(*re).re_freq_offset_khz;
758 		dfs->radar_log[i].peak_mag = (*re).re_peak_mag;
759 		dfs->radar_log[i].total_gain = (*re).re_total_gain;
760 		dfs->radar_log[i].mb_gain = (*re).re_mb_gain;
761 		dfs->radar_log[i].relpwr_db = (*re).re_relpwr_db;
762 		dfs->radar_log[i].delta_diff = (*re).re_delta_diff;
763 		dfs->radar_log[i].delta_peak = (*re).re_delta_peak;
764 		dfs->radar_log[i].psidx_diff = (*re).re_psidx_diff;
765 		dfs->radar_log[i].is_chirp = DFS_EVENT_NOTCHIRP(re) ?
766 			0 : 1;
767 		dfs->dfs_event_log_count++;
768 	}
769 
770 	dfs->dfs_seq_num++;
771 	pl->pl_elems[index].p_seq_num = dfs->dfs_seq_num;
772 }
773 
774 /**
775  * dfs_check_if_nonbin5 - Check if radar, other than bin5, is found
776  * @dfs: Pointer to wlan_dfs structure.
777  * @re: Pointer to re (radar event)
778  * @rs: Double Pointer to rs (radar state)
779  * @this_ts: Current time stamp 64bit
780  * @diff_ts: Difference between 2 timestamps 32bit
781  * @found: Pointer to found. If radar found or not.
782  * @retval: Pointer to retval(return value).
783  * @false_radar_found: Pointer to false_radar_found(return value).
784  */
785 static inline void dfs_check_if_nonbin5(
786 	struct wlan_dfs *dfs,
787 	struct dfs_event *re,
788 	struct dfs_state **rs,
789 	uint64_t this_ts,
790 	uint32_t diff_ts,
791 	int *found,
792 	int *retval,
793 	int *false_radar_found)
794 {
795 
796 	uint32_t tabledepth = 0;
797 	struct dfs_filtertype *ft;
798 	uint64_t deltaT;
799 
800 	dfs_debug(dfs, WLAN_DEBUG_DFS1,
801 			"  *** chan freq (%d): ts %llu dur %u rssi %u",
802 			(*rs)->rs_chan.dfs_ch_freq, (uint64_t)this_ts,
803 			(*re).re_dur, (*re).re_rssi);
804 
805 	while ((tabledepth < DFS_MAX_RADAR_OVERLAP) &&
806 			((dfs->dfs_ftindextable[(*re).re_dur])[tabledepth] !=
807 			 -1) && (!*retval) && !(*false_radar_found)) {
808 		ft = dfs->dfs_radarf[((dfs->dfs_ftindextable[(*re).re_dur])
809 				[tabledepth])];
810 		dfs_debug(dfs, WLAN_DEBUG_DFS2,
811 				"  ** RD (%d): ts %x dur %u rssi %u",
812 				(*rs)->rs_chan.dfs_ch_freq, (*re).re_ts,
813 				(*re).re_dur, (*re).re_rssi);
814 
815 		if ((*re).re_rssi < ft->ft_rssithresh &&
816 				(*re).re_dur > MAX_DUR_FOR_LOW_RSSI) {
817 			dfs_debug(dfs, WLAN_DEBUG_DFS2,
818 					"Rejecting on rssi rssi=%u thresh=%u",
819 					 (*re).re_rssi,
820 					ft->ft_rssithresh);
821 			tabledepth++;
822 			continue;
823 		}
824 		deltaT = this_ts - ft->ft_last_ts;
825 		dfs_debug(dfs, WLAN_DEBUG_DFS2,
826 				"deltaT = %lld (ts: 0x%llx) (last ts: 0x%llx)",
827 				(uint64_t)deltaT, (uint64_t)this_ts,
828 				(uint64_t)ft->ft_last_ts);
829 
830 		if ((deltaT < ft->ft_minpri) && (deltaT != 0)) {
831 			/*
832 			 * This check is for the whole filter type.
833 			 * Individual filters will check this again.
834 			 * This is first line of filtering.
835 			 */
836 			dfs_debug(dfs, WLAN_DEBUG_DFS2,
837 					"Rejecting on pri pri=%lld minpri=%u",
838 					 (uint64_t)deltaT, ft->ft_minpri);
839 			tabledepth++;
840 			continue;
841 		}
842 
843 		__dfs_process_radarevent(dfs, ft, re, this_ts, found,
844 				false_radar_found);
845 
846 		ft->ft_last_ts = this_ts;
847 		*retval |= *found;
848 		tabledepth++;
849 	}
850 }
851 
852 /**
853  * dfs_check_each_b5radar() - Check each bin5 radar
854  * @dfs: Pointer to wlan_dfs structure.
855  * @re:  Pointer to re(radar event).
856  * @br: Pointer to dfs_bin5radars structure.
857  * @this_ts: Current time stamp 64bit.
858  * @diff_ts: Difference between 2 timestamps 32bit.
859  * @found: Pointer to found. If radar found or not.
860  */
861 static inline void dfs_check_each_b5radar(
862 		struct wlan_dfs *dfs,
863 		struct dfs_event *re,
864 		struct dfs_bin5radars *br,
865 		uint64_t this_ts,
866 		uint32_t diff_ts,
867 		int *found)
868 {
869 	if (dfs_bin5_check_pulse(dfs, re, br)) {
870 		/*
871 		 * This is a valid Bin5 pulse, check if it belongs to a
872 		 * burst.
873 		 */
874 		(*re).re_dur = dfs_retain_bin5_burst_pattern(dfs, diff_ts,
875 				(*re).re_dur);
876 		/*
877 		 * Remember our computed duration for the next pulse in the
878 		 * burst (if needed).
879 		 */
880 		dfs->dfs_rinfo.dfs_bin5_chirp_ts = this_ts;
881 		dfs->dfs_rinfo.dfs_last_bin5_dur = (*re).re_dur;
882 
883 		if (dfs_bin5_addpulse(dfs, br, re, this_ts))
884 			*found |= dfs_bin5_check(dfs);
885 	} else {
886 		dfs_debug(dfs, WLAN_DEBUG_DFS_BIN5_PULSE,
887 				"not a BIN5 pulse (dur=%d)", (*re).re_dur);
888 	}
889 }
890 
891 /**
892  * dfs_check_if_bin5() - Check if bin5 radar is found
893  * @dfs: Pointer to wlan_dfs structure.
894  * @re:  Pointer to re(radar event).
895  * @this_ts: Current time stamp 64bit.
896  * @diff_ts: Difference between 2 timestamps 32bit.
897  * @found: Pointer to found. If radar found or not.
898  */
899 static inline void dfs_check_if_bin5(
900 	struct wlan_dfs *dfs,
901 	struct dfs_event *re,
902 	uint64_t this_ts,
903 	uint32_t diff_ts,
904 	int *found)
905 {
906 	int p;
907 
908 	/* BIN5 pulses are FCC and Japan specific. */
909 	if ((dfs->dfsdomain == DFS_FCC_DOMAIN) ||
910 			(dfs->dfsdomain == DFS_MKK4_DOMAIN)) {
911 		for (p = 0; (p < dfs->dfs_rinfo.rn_numbin5radars) && (!*found);
912 				p++) {
913 			struct dfs_bin5radars *br;
914 
915 			br = &(dfs->dfs_b5radars[p]);
916 			dfs_check_each_b5radar(dfs, re, br, this_ts, diff_ts,
917 					found);
918 		}
919 	}
920 
921 	if (*found)
922 		dfs_debug(dfs, WLAN_DEBUG_DFS, "Found bin5 radar");
923 }
924 
925 /**
926  * dfs_skip_the_event() - Skip the Radar event
927  * @dfs: Pointer to wlan_dfs structure.
928  * @re: Pointer to re(radar event).
929  * @rs: Pointer to dfs_state.
930  */
931 static inline bool dfs_skip_the_event(
932 	struct wlan_dfs *dfs,
933 	struct dfs_event *re,
934 	struct dfs_state **rs)
935 {
936 	if ((*re).re_chanindex < DFS_NUM_RADAR_STATES)
937 		(*rs) = &dfs->dfs_radar[(*re).re_chanindex];
938 	else
939 		return 1;
940 
941 	if ((*rs)->rs_chan.dfs_ch_flagext & CHANNEL_INTERFERENCE)
942 		return 1;
943 
944 	return 0;
945 }
946 
947 /**
948  * dfs_check_ts_wrap() - dfs check for timestamp wrap.
949  * @dfs: Pointer to wlan_dfs structure.
950  * @re: Pointer to re(radar event).
951  * @deltafull_ts: Deltafull ts.
952  *
953  * Return: Deltafull ts.
954  */
955 static inline uint64_t dfs_check_ts_wrap(
956 		struct wlan_dfs *dfs,
957 		struct dfs_event *re,
958 		uint64_t deltafull_ts)
959 {
960 	if (deltafull_ts >
961 			((uint64_t)((DFS_TSMASK -
962 					dfs->dfs_rinfo.rn_last_ts) +
963 				1 + (*re).re_ts)))
964 		deltafull_ts -=
965 			(DFS_TSMASK - dfs->dfs_rinfo.rn_last_ts) +
966 			1 + (*re).re_ts;
967 
968 	return deltafull_ts;
969 }
970 
971 /**
972  * dfs_calculate_ts_prefix() - Calculate deltafull ts value.
973  * @dfs: Pointer to wlan_dfs structure.
974  * @re: Pointer to re(radar event).
975  */
976 static inline void dfs_calculate_ts_prefix(
977 		struct wlan_dfs *dfs,
978 		struct dfs_event *re)
979 {
980 	uint64_t deltafull_ts;
981 
982 	if ((*re).re_ts <= dfs->dfs_rinfo.rn_last_ts) {
983 		dfs->dfs_rinfo.rn_ts_prefix += (((uint64_t) 1) << DFS_TSSHIFT);
984 		/* Now, see if it's been more than 1 wrap */
985 		deltafull_ts = (*re).re_full_ts - dfs->dfs_rinfo.rn_lastfull_ts;
986 		deltafull_ts = dfs_check_ts_wrap(dfs, re, deltafull_ts);
987 		deltafull_ts >>= DFS_TSSHIFT;
988 
989 		if (deltafull_ts > 1)
990 			dfs->dfs_rinfo.rn_ts_prefix +=
991 				((deltafull_ts - 1) << DFS_TSSHIFT);
992 	} else {
993 		deltafull_ts = (*re).re_full_ts -
994 			dfs->dfs_rinfo.rn_lastfull_ts;
995 		if (deltafull_ts > (uint64_t) DFS_TSMASK) {
996 			deltafull_ts >>= DFS_TSSHIFT;
997 			dfs->dfs_rinfo.rn_ts_prefix +=
998 				((deltafull_ts - 1) << DFS_TSSHIFT);
999 		}
1000 	}
1001 }
1002 
1003 /**
1004  * dfs_calculate_timestamps() - Calculate various timestamps
1005  * @dfs: Pointer to wlan_dfs structure.
1006  * @re: Pointer to re(radar event)
1007  * @this_ts : Pointer to  this_ts (this timestamp)
1008  */
1009 
1010 static inline void  dfs_calculate_timestamps(
1011 	struct wlan_dfs *dfs,
1012 	struct dfs_event *re,
1013 	uint64_t *this_ts)
1014 {
1015 	if (dfs->dfs_rinfo.rn_lastfull_ts == 0) {
1016 		/*
1017 		 * Either not started, or 64-bit rollover exactly to
1018 		 * zero Just prepend zeros to the 15-bit ts.
1019 		 */
1020 		dfs->dfs_rinfo.rn_ts_prefix = 0;
1021 	} else {
1022 		/* WAR 23031- patch duplicate ts on very short pulses.
1023 		 * This pacth has two problems in linux environment.
1024 		 * 1)The time stamp created and hence PRI depends
1025 		 * entirely on the latency. If the latency is high, it
1026 		 * possibly can split two consecutive pulses in the
1027 		 * same burst so far away (the same amount of latency)
1028 		 * that make them look like they are from differenct
1029 		 * bursts. It is observed to happen too often. It sure
1030 		 * makes the detection fail.
1031 		 * 2)Even if the latency is not that bad, it simply
1032 		 * shifts the duplicate timestamps to a new duplicate
1033 		 * timestamp based on how they are processed.
1034 		 * This is not worse but not good either.
1035 		 * Take this pulse as a good one and create a probable
1036 		 * PRI later.
1037 		 */
1038 		if ((*re).re_dur == 0 && (*re).re_ts ==
1039 				dfs->dfs_rinfo.rn_last_unique_ts) {
1040 			debug_dup[debug_dup_cnt++] = '1';
1041 			dfs_debug(dfs, WLAN_DEBUG_DFS1, "deltaT is 0");
1042 		} else {
1043 			dfs->dfs_rinfo.rn_last_unique_ts = (*re).re_ts;
1044 			debug_dup[debug_dup_cnt++] = '0';
1045 		}
1046 
1047 		if (debug_dup_cnt >= 32)
1048 			debug_dup_cnt = 0;
1049 
1050 		dfs_calculate_ts_prefix(dfs, re);
1051 	}
1052 
1053 	/*
1054 	 * At this stage rn_ts_prefix has either been blanked or
1055 	 * calculated, so it's safe to use.
1056 	 */
1057 	*this_ts = dfs->dfs_rinfo.rn_ts_prefix | ((uint64_t) (*re).re_ts);
1058 	dfs->dfs_rinfo.rn_lastfull_ts = (*re).re_full_ts;
1059 	dfs->dfs_rinfo.rn_last_ts = (*re).re_ts;
1060 }
1061 
1062 /**
1063  * dfs_add_to_pulseline - Extract necessary items from dfs_event and
1064  * add it as pulse in the pulseline
1065  * @dfs: Pointer to wlan_dfs structure.
1066  * @re:  Pointer to re(radar event)
1067  * @this_ts: Pointer to  this_ts (this timestamp)
1068  * @diff_ts: Diff ts.
1069  * @index: Pointer to get index value.
1070  */
1071 static inline void dfs_add_to_pulseline(
1072 	struct wlan_dfs *dfs,
1073 	struct dfs_event *re,
1074 	uint64_t *this_ts,
1075 	uint32_t *test_ts,
1076 	uint32_t *diff_ts,
1077 	uint32_t *index)
1078 {
1079 	struct dfs_pulseline *pl;
1080 
1081 	/*
1082 	 * Calculate the start of the radar pulse.
1083 	 *
1084 	 * The TSF is stamped by the MAC upon reception of the event,
1085 	 * which is (typically?) at the end of the event. But the
1086 	 * pattern matching code expects the event timestamps to be at
1087 	 * the start of the event. So to fake it, we subtract the pulse
1088 	 * duration from the given TSF. This is done after the 64-bit
1089 	 * timestamp has been calculated so long pulses correctly
1090 	 * under-wrap the counter.  Ie, if this was done on the 32
1091 	 * (or 15!) bit TSF when the TSF value is closed to 0, it will
1092 	 * underflow to 0xfffffXX, which would mess up the logical "OR"
1093 	 * operation done above.
1094 	 * This isn't valid for Peregrine as the hardware gives us the
1095 	 * actual TSF offset of the radar event, not just the MAC TSF
1096 	 * of the completed receive.
1097 	 *
1098 	 * XXX TODO: ensure that the TLV PHY error processing code will
1099 	 * correctly calculate the TSF to be the start of the radar
1100 	 * pulse.
1101 	 *
1102 	 * XXX TODO TODO: modify the TLV parsing code to subtract the
1103 	 * duration from the TSF, based on the current fast clock value.
1104 	 */
1105 	if ((!dfs->dfs_caps.wlan_chip_is_bb_tlv) && (*re).re_dur != 1)
1106 		*this_ts -= (*re).re_dur;
1107 
1108 	pl = dfs->pulses;
1109 	/* Save the pulse parameters in the pulse buffer(pulse line). */
1110 	*index = (pl->pl_lastelem + 1) & DFS_MAX_PULSE_BUFFER_MASK;
1111 
1112 	if (pl->pl_numelems == DFS_MAX_PULSE_BUFFER_SIZE)
1113 		pl->pl_firstelem = (pl->pl_firstelem+1) &
1114 			DFS_MAX_PULSE_BUFFER_MASK;
1115 	else
1116 		pl->pl_numelems++;
1117 
1118 	pl->pl_lastelem = *index;
1119 	pl->pl_elems[*index].p_time = *this_ts;
1120 	pl->pl_elems[*index].p_dur = (*re).re_dur;
1121 	pl->pl_elems[*index].p_rssi = (*re).re_rssi;
1122 	pl->pl_elems[*index].p_sidx = (*re).re_sidx;
1123 	pl->pl_elems[*index].p_delta_peak = (*re).re_delta_peak;
1124 	pl->pl_elems[*index].p_psidx_diff = (*re).re_psidx_diff;
1125 	*diff_ts = (uint32_t)*this_ts - *test_ts;
1126 	*test_ts = (uint32_t)*this_ts;
1127 
1128 	dfs_debug(dfs, WLAN_DEBUG_DFS1,
1129 			"ts%u %u %u diff %u pl->pl_lastelem.p_time=%llu",
1130 			(uint32_t)*this_ts, (*re).re_dur,
1131 			(*re).re_rssi, *diff_ts,
1132 			(uint64_t)pl->pl_elems[*index].p_time);
1133 }
1134 
1135 /**
1136  * dfs_conditional_clear_delaylines - Clear delay lines to remove  the
1137  * false pulses.
1138  * @dfs: Pointer to wlan_dfs structure.
1139  * @diff_ts: diff between timerstamps.
1140  * @this_ts: this timestamp value.
1141  * @re: Pointer to dfs_event structure.
1142  */
1143 static inline void dfs_conditional_clear_delaylines(
1144 	struct wlan_dfs *dfs,
1145 	uint32_t diff_ts,
1146 	uint64_t this_ts,
1147 	struct dfs_event re)
1148 {
1149 	struct dfs_pulseline *pl = dfs->pulses;
1150 	uint32_t index;
1151 
1152 	/* If diff_ts is very small, we might be getting false pulse
1153 	 * detects due to heavy interference. We might be getting
1154 	 * spectral splatter from adjacent channel. In order to prevent
1155 	 * false alarms we clear the delay-lines. This might impact
1156 	 * positive detections under harsh environments, but helps with
1157 	 * false detects.
1158 	 */
1159 
1160 	if (diff_ts < DFS_INVALID_PRI_LIMIT) {
1161 		dfs->dfs_seq_num = 0;
1162 		dfs_reset_alldelaylines(dfs);
1163 		dfs_reset_radarq(dfs);
1164 
1165 		index = (pl->pl_lastelem + 1) & DFS_MAX_PULSE_BUFFER_MASK;
1166 		if (pl->pl_numelems == DFS_MAX_PULSE_BUFFER_SIZE)
1167 			pl->pl_firstelem = (pl->pl_firstelem+1) &
1168 				DFS_MAX_PULSE_BUFFER_MASK;
1169 		else
1170 			pl->pl_numelems++;
1171 
1172 		pl->pl_lastelem = index;
1173 		pl->pl_elems[index].p_time = this_ts;
1174 		pl->pl_elems[index].p_dur = re.re_dur;
1175 		pl->pl_elems[index].p_rssi = re.re_rssi;
1176 		pl->pl_elems[index].p_sidx = re.re_sidx;
1177 		pl->pl_elems[index].p_delta_peak = re.re_delta_peak;
1178 		pl->pl_elems[index].p_psidx_diff = re.re_psidx_diff;
1179 		dfs->dfs_seq_num++;
1180 		pl->pl_elems[index].p_seq_num = dfs->dfs_seq_num;
1181 	}
1182 }
1183 
1184 /**
1185  * dfs_process_each_radarevent - remove each event from the dfs radar queue
1186  * and process it.
1187  * @dfs: Pointer to wlan_dfs structure.
1188  * @chan: Pointer to DFS current channel.
1189  * @rs: Pointer to dfs_state structure.
1190  * @seg_id: segment id.
1191  * @retval: pointer to retval.
1192  * @false_radar_found: pointer to false radar found.
1193  *
1194  * Return: If radar found then return 1 else return 0.
1195  */
1196 static inline int dfs_process_each_radarevent(
1197 	struct wlan_dfs *dfs,
1198 	struct dfs_channel *chan,
1199 	struct dfs_state **rs,
1200 	uint8_t *seg_id,
1201 	int *retval,
1202 	int *false_radar_found)
1203 {
1204 	struct dfs_event re, *event;
1205 	int found, empty;
1206 	int events_processed = 0;
1207 	uint64_t this_ts;
1208 	static uint32_t test_ts;
1209 	static uint32_t diff_ts;
1210 	uint32_t index;
1211 
1212 	dfs_is_radarq_empty(dfs, &empty);
1213 
1214 	while ((!empty) && (!*retval) && !(*false_radar_found) &&
1215 			(events_processed < MAX_EVENTS)) {
1216 		dfs_remove_event_from_radarq(dfs, &event);
1217 		if (!event) {
1218 			empty = 1;
1219 			break;
1220 		}
1221 		events_processed++;
1222 		re = *event;
1223 
1224 		dfs_return_event_to_eventq(dfs, event);
1225 
1226 		*seg_id = re.re_seg_id;
1227 		found = 0;
1228 		if (dfs_skip_the_event(dfs, &re, rs)) {
1229 			dfs_is_radarq_empty(dfs, &empty);
1230 			continue;
1231 		}
1232 
1233 		dfs_calculate_timestamps(dfs, &re, &this_ts);
1234 
1235 		re.re_dur = dfs_process_pulse_dur(dfs, re.re_dur);
1236 
1237 		dfs_add_to_pulseline(dfs, &re, &this_ts, &test_ts, &diff_ts,
1238 				&index);
1239 
1240 		dfs_log_event(dfs, &re, this_ts, diff_ts, index);
1241 
1242 		dfs_conditional_clear_delaylines(dfs, diff_ts, this_ts, re);
1243 
1244 		found = 0;
1245 		if (events_processed == 1) {
1246 			dfs->dfs_min_sidx = (re).re_sidx;
1247 			dfs->dfs_max_sidx = (re).re_sidx;
1248 		}
1249 
1250 		dfs_check_if_bin5(dfs, &re, this_ts, diff_ts, &found);
1251 		if (found) {
1252 			*retval |= found;
1253 			dfs->dfs_freq_offset = DFS_SIDX_TO_FREQ_OFFSET(
1254 				   (dfs->dfs_min_sidx + dfs->dfs_max_sidx) / 2);
1255 			return 1;
1256 		}
1257 
1258 		dfs_check_if_nonbin5(dfs, &re, rs, this_ts, diff_ts, &found,
1259 				retval, false_radar_found);
1260 
1261 		dfs_is_radarq_empty(dfs, &empty);
1262 	}
1263 
1264 	return 0;
1265 }
1266 
1267 /**
1268  * dfs_false_radarfound_reset_vars () - Reset dfs variables after false radar
1269  *                                      found.
1270  * @dfs: Pointer to wlan_dfs structure.
1271  */
1272 static inline void dfs_false_radarfound_reset_vars(
1273 	struct wlan_dfs *dfs)
1274 {
1275 	dfs->dfs_seq_num = 0;
1276 	dfs_reset_radarq(dfs);
1277 	dfs_reset_alldelaylines(dfs);
1278 	dfs->dfs_phyerr_freq_min     = 0x7fffffff;
1279 	dfs->dfs_phyerr_freq_max     = 0;
1280 	dfs->dfs_phyerr_w53_counter  = 0;
1281 }
1282 
1283 void dfs_radarfound_action_generic(struct wlan_dfs *dfs, uint8_t seg_id)
1284 {
1285 	struct radar_found_info *radar_found;
1286 
1287 	radar_found = qdf_mem_malloc(sizeof(*radar_found));
1288 	if (!radar_found) {
1289 		dfs_alert(dfs, WLAN_DEBUG_DFS_ALWAYS,
1290 			  "radar_found allocation failed");
1291 		return;
1292 	}
1293 
1294 	qdf_mem_zero(radar_found, sizeof(*radar_found));
1295 	radar_found->segment_id = seg_id;
1296 	radar_found->pdev_id =
1297 		wlan_objmgr_pdev_get_pdev_id(dfs->dfs_pdev_obj);
1298 
1299 	dfs_process_radar_ind(dfs, radar_found);
1300 	qdf_mem_free(radar_found);
1301 }
1302 
1303 void dfs_radar_found_action(struct wlan_dfs *dfs,
1304 			    bool bangradar,
1305 			    uint8_t seg_id)
1306 {
1307 	/* If Host DFS confirmation is supported, save the curchan as
1308 	 * radar found chan, send radar found indication along with
1309 	 * average radar parameters to FW and start the host status
1310 	 * wait timer.
1311 	 */
1312 	if (!bangradar &&
1313 	    (utils_get_dfsdomain(dfs->dfs_pdev_obj) == DFS_FCC_DOMAIN) &&
1314 	    lmac_is_host_dfs_check_support_enabled(dfs->dfs_pdev_obj)) {
1315 		dfs_radarfound_action_fcc(dfs, seg_id);
1316 	} else {
1317 		dfs_radarfound_action_generic(dfs, seg_id);
1318 	}
1319 }
1320 
1321 void dfs_process_radarevent(
1322 	struct wlan_dfs *dfs,
1323 	struct dfs_channel *chan)
1324 {
1325 	struct dfs_state *rs = NULL;
1326 	uint8_t   seg_id = 0;
1327 	int retval = 0;
1328 	int false_radar_found = 0;
1329 	bool bangradar = false;
1330 
1331 	if (!dfs_radarevent_basic_sanity(dfs, chan))
1332 		return;
1333 	/*
1334 	 * TEST : Simulate radar bang, make sure we add the channel to NOL
1335 	 * (bug 29968)
1336 	 */
1337 	if (dfs_handle_bangradar(dfs, chan, &rs, &seg_id, &retval)) {
1338 		if (retval)
1339 			bangradar = true;
1340 		goto dfsfound;
1341 	}
1342 
1343 	if (!dfs_handle_missing_pulses(dfs, chan))
1344 		return;
1345 
1346 	dfs_process_each_radarevent(dfs, chan, &rs, &seg_id, &retval,
1347 			&false_radar_found);
1348 
1349 dfsfound:
1350 	if (retval) {
1351 		dfs_radarfound_reset_vars(dfs, rs, chan, seg_id);
1352 		dfs_radar_found_action(dfs, bangradar, seg_id);
1353 	}
1354 
1355 	if (false_radar_found)
1356 		dfs_false_radarfound_reset_vars(dfs);
1357 }
1358