xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/core/src/wlan_scan_cache_db.c (revision d0c05845839e5f2ba5a8dcebe0cd3e4cd4e8dfcf)
1 /*
2  * Copyright (c) 2017-2021 The Linux Foundation. All rights reserved.
3  * Copyright (c) 2021-2022 Qualcomm Innovation Center, Inc. All rights reserved.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for
6  * any purpose with or without fee is hereby granted, provided that the
7  * above copyright notice and this permission notice appear in all
8  * copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17  * PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 /*
21  * DOC: contains scan cache api and functionality
22  * The Scan entries are protected by scan_db_lock. Holding the lock
23  * for whole scan operation during get/flush scan results may take
24  * more than 5 ms and thus ref count is used along with scan_db_lock.
25  * Below are the operation on scan cache entry:
26  * - While adding new node to the entry scan_db_lock is taken and ref_cnt
27  *   is initialized and incremented. Also the cookie will be set to valid value.
28  * - The ref count incremented during adding new node should be decremented only
29  *   by a delete operation on the node. But there can be multiple concurrent
30  *   delete operations on a node from different threads which may lead to ref
31  *   count being decremented multiple time and freeing the node even if node
32  *   is in use. So to maintain atomicity between multiple delete operations
33  *   on a same node from different threads, a cookie is used to check if node is
34  *   logically deleted or not. A delete operation will set the cookie to 0
35  *   making it invalid. So if the 2nd thread find the cookie as invalid it will
36  *   not try to delete and decrement the ref count of the node again.
37  * - This Cookie is also used to check if node is valid while iterating through
38  *   the scan cache to avoid duplicate entries.
39  * - Once ref_cnt become 0, i.e. it is logically deleted and no thread is using
40  *   it the node is physically deleted from the scan cache.
41  * - While reading the node the ref_cnt should be incremented. Once reading
42  *   operation is done ref_cnt is decremented.
43  */
44 #include <qdf_status.h>
45 #include <wlan_objmgr_psoc_obj.h>
46 #include <wlan_objmgr_pdev_obj.h>
47 #include <wlan_objmgr_vdev_obj.h>
48 #include <wlan_scan_public_structs.h>
49 #include <wlan_scan_utils_api.h>
50 #include "wlan_scan_main.h"
51 #include "wlan_scan_cache_db_i.h"
52 #include "wlan_reg_services_api.h"
53 #include "wlan_reg_ucfg_api.h"
54 #include <wlan_objmgr_vdev_obj.h>
55 #include <wlan_dfs_utils_api.h>
56 #include "wlan_crypto_global_def.h"
57 #include "wlan_crypto_global_api.h"
58 #include "wlan_cm_bss_score_param.h"
59 
60 #ifdef FEATURE_6G_SCAN_CHAN_SORT_ALGO
61 
62 struct channel_list_db *scm_get_rnr_channel_db(struct wlan_objmgr_psoc *psoc)
63 {
64 	struct wlan_scan_obj *scan_obj = NULL;
65 
66 	scan_obj = wlan_psoc_get_scan_obj(psoc);
67 
68 	if (!scan_obj)
69 		return NULL;
70 
71 	return &scan_obj->rnr_channel_db;
72 }
73 
74 struct meta_rnr_channel *scm_get_chan_meta(struct wlan_objmgr_psoc *psoc,
75 					   uint32_t chan_freq)
76 {
77 	int i;
78 	struct channel_list_db *rnr_channel_db;
79 
80 	if (!psoc || !chan_freq || !wlan_reg_is_6ghz_chan_freq(chan_freq))
81 		return NULL;
82 
83 	rnr_channel_db = scm_get_rnr_channel_db(psoc);
84 	if (!rnr_channel_db)
85 		return NULL;
86 
87 	for (i = 0; i < QDF_ARRAY_SIZE(rnr_channel_db->channel); i++)
88 		if (rnr_channel_db->channel[i].chan_freq == chan_freq)
89 			return &rnr_channel_db->channel[i];
90 
91 	return NULL;
92 }
93 
94 static void scm_add_rnr_channel_db(struct wlan_objmgr_psoc *psoc,
95 				   struct scan_cache_entry *entry)
96 {
97 	uint32_t chan_freq;
98 	uint8_t is_6g_bss, i;
99 	struct meta_rnr_channel *channel;
100 	struct rnr_bss_info *rnr_bss;
101 	struct scan_rnr_node *rnr_node;
102 
103 	chan_freq = entry->channel.chan_freq;
104 	is_6g_bss = wlan_reg_is_6ghz_chan_freq(chan_freq);
105 
106 	/* Return if the BSS is not 6G and RNR IE is not present */
107 	if (!(is_6g_bss || entry->ie_list.rnrie))
108 		return;
109 
110 	scm_debug("BSS freq %d BSSID: "QDF_MAC_ADDR_FMT, chan_freq,
111 		  QDF_MAC_ADDR_REF(entry->bssid.bytes));
112 	if (is_6g_bss) {
113 		channel = scm_get_chan_meta(psoc, chan_freq);
114 		if (!channel) {
115 			scm_debug("Failed to get chan Meta freq %d", chan_freq);
116 			return;
117 		}
118 		channel->bss_beacon_probe_count++;
119 		channel->beacon_probe_last_time_found = entry->scan_entry_time;
120 	}
121 
122 	/*
123 	 * If scan entry got RNR IE then loop through all
124 	 * entries and increase the BSS count in respective channels
125 	 */
126 	if (!entry->ie_list.rnrie)
127 		return;
128 
129 	for (i = 0; i < MAX_RNR_BSS; i++) {
130 		rnr_bss = &entry->rnr.bss_info[i];
131 		/* Skip if entry is not valid */
132 		if (!rnr_bss->channel_number)
133 			continue;
134 		chan_freq = wlan_reg_chan_opclass_to_freq(rnr_bss->channel_number,
135 							  rnr_bss->operating_class,
136 							  true);
137 		channel = scm_get_chan_meta(psoc, chan_freq);
138 		if (!channel) {
139 			scm_debug("Failed to get chan Meta freq %d", chan_freq);
140 			continue;
141 		}
142 		channel->bss_beacon_probe_count++;
143 		/* Don't add RNR entry if list is full */
144 		if (qdf_list_size(&channel->rnr_list) >= WLAN_MAX_RNR_COUNT) {
145 			scm_debug("List is full");
146 			return;
147 		}
148 
149 		rnr_node = qdf_mem_malloc(sizeof(struct scan_rnr_node));
150 		if (!rnr_node)
151 			return;
152 		rnr_node->entry.timestamp = entry->scan_entry_time;
153 		if (!qdf_is_macaddr_zero(&rnr_bss->bssid))
154 			qdf_mem_copy(&rnr_node->entry.bssid,
155 				     &rnr_bss->bssid,
156 				     QDF_MAC_ADDR_SIZE);
157 		if (rnr_bss->short_ssid)
158 			rnr_node->entry.short_ssid = rnr_bss->short_ssid;
159 		if (rnr_bss->bss_params)
160 			rnr_node->entry.bss_params = rnr_bss->bss_params;
161 		scm_debug("Add freq %d: "QDF_MAC_ADDR_FMT" short ssid %x", chan_freq,
162 			  QDF_MAC_ADDR_REF(rnr_bss->bssid.bytes),
163 			  rnr_bss->short_ssid);
164 		qdf_list_insert_back(&channel->rnr_list,
165 				     &rnr_node->node);
166 	}
167 }
168 
169 void scm_filter_rnr_flag_pno(struct wlan_objmgr_vdev *vdev,
170 			     uint32_t short_ssid,
171 			     struct chan_list *pno_chan_list)
172 {
173 	uint8_t i;
174 	uint32_t freq;
175 	struct meta_rnr_channel *chan;
176 	struct scan_rnr_node *rnr_node;
177 	enum scan_mode_6ghz scan_mode;
178 	struct wlan_scan_obj *scan_obj;
179 	struct wlan_objmgr_psoc *psoc;
180 
181 	psoc = wlan_vdev_get_psoc(vdev);
182 	if (!psoc)
183 		return;
184 
185 	scan_obj = wlan_vdev_get_scan_obj(vdev);
186 	if (!scan_obj) {
187 		scm_err("scan_obj is NULL");
188 		return;
189 	}
190 
191 	scan_mode = scan_obj->scan_def.scan_mode_6g;
192 	/* No Filteration required for below scan modes since
193 	 * no RNR flag marked.
194 	 */
195 	if (scan_mode == SCAN_MODE_6G_NO_CHANNEL ||
196 	    scan_mode == SCAN_MODE_6G_ALL_CHANNEL ||
197 	    scan_mode == SCAN_MODE_6G_ALL_DUTY_CYCLE)
198 		return;
199 
200 	for (i = 0; i < pno_chan_list->num_chan; i++) {
201 		freq = pno_chan_list->chan[i].freq;
202 
203 		chan = scm_get_chan_meta(psoc, freq);
204 		if (!chan || qdf_list_empty(&chan->rnr_list))
205 			continue;
206 
207 		qdf_list_for_each(&chan->rnr_list, rnr_node, node) {
208 			if (rnr_node->entry.short_ssid) {
209 				if (rnr_node->entry.short_ssid == short_ssid) {
210 			/* If short ssid entry present in RNR db cache, remove
211 			 * FLAG_SCAN_ONLY_IF_RNR_FOUND flag from the channel.
212 			 */
213 					pno_chan_list->chan[i].flags &=
214 						~FLAG_SCAN_ONLY_IF_RNR_FOUND;
215 					break;
216 				}
217 			}
218 		}
219 	}
220 }
221 #endif
222 
223 /**
224  * scm_del_scan_node() - API to remove scan node from the list
225  * @list: hash list
226  * @scan_node: node to be removed
227  *
228  * This should be called while holding scan_db_lock.
229  *
230  * Return: void
231  */
232 static void scm_del_scan_node(qdf_list_t *list,
233 	struct scan_cache_node *scan_node)
234 {
235 	QDF_STATUS status;
236 
237 	status = qdf_list_remove_node(list, &scan_node->node);
238 	if (QDF_IS_STATUS_SUCCESS(status)) {
239 		util_scan_free_cache_entry(scan_node->entry);
240 		qdf_mem_free(scan_node);
241 	}
242 }
243 
244 /**
245  * scm_del_scan_node_from_db() - API to del the scan entry
246  * @scan_db: scan database
247  * @scan_entry:entry scan_node
248  *
249  * API to flush the scan entry. This should be called while
250  * holding scan_db_lock.
251  *
252  * Return: QDF status.
253  */
254 static QDF_STATUS scm_del_scan_node_from_db(struct scan_dbs *scan_db,
255 	struct scan_cache_node *scan_node)
256 {
257 	QDF_STATUS status = QDF_STATUS_SUCCESS;
258 	uint8_t hash_idx;
259 
260 	if (!scan_node)
261 		return QDF_STATUS_E_INVAL;
262 
263 	hash_idx = SCAN_GET_HASH(scan_node->entry->bssid.bytes);
264 	scm_del_scan_node(&scan_db->scan_hash_tbl[hash_idx], scan_node);
265 	scan_db->num_entries--;
266 
267 	return status;
268 }
269 
270 /**
271  * scm_scan_entry_get_ref() - api to increase ref count of scan entry
272  * @scan_node: scan node
273  *
274  * Return: void
275  */
276 static void scm_scan_entry_get_ref(struct scan_cache_node *scan_node)
277 {
278 	if (!scan_node) {
279 		scm_err("scan_node is NULL");
280 		QDF_ASSERT(0);
281 		return;
282 	}
283 	qdf_atomic_inc(&scan_node->ref_cnt);
284 }
285 
286 /**
287  * scm_scan_entry_put_ref() - Api to decrease ref count of scan entry
288  * and free if it become 0
289  * @scan_db: scan database
290  * @scan_node: scan node
291  * @lock_needed: if scan_db_lock is needed
292  *
293  * Return: void
294  */
295 static void scm_scan_entry_put_ref(struct scan_dbs *scan_db,
296 	struct scan_cache_node *scan_node, bool lock_needed)
297 {
298 
299 	if (!scan_node) {
300 		scm_err("scan_node is NULL");
301 		QDF_ASSERT(0);
302 		return;
303 	}
304 
305 	if (lock_needed)
306 		qdf_spin_lock_bh(&scan_db->scan_db_lock);
307 
308 	if (!qdf_atomic_read(&scan_node->ref_cnt)) {
309 		if (lock_needed)
310 			qdf_spin_unlock_bh(&scan_db->scan_db_lock);
311 		scm_err("scan_node ref cnt is 0");
312 		QDF_ASSERT(0);
313 		return;
314 	}
315 
316 	/* Decrement ref count, free scan_node, if ref count == 0 */
317 	if (qdf_atomic_dec_and_test(&scan_node->ref_cnt))
318 		scm_del_scan_node_from_db(scan_db, scan_node);
319 
320 	if (lock_needed)
321 		qdf_spin_unlock_bh(&scan_db->scan_db_lock);
322 }
323 
324 /**
325  * scm_scan_entry_del() - API to delete scan node
326  * @scan_db: data base
327  * @scan_node: node to be deleted
328  *
329  * Call must be protected by scan_db->scan_db_lock
330  *
331  * Return: void
332  */
333 
334 static void scm_scan_entry_del(struct scan_dbs *scan_db,
335 			       struct scan_cache_node *scan_node)
336 {
337 	if (!scan_node) {
338 		scm_err("scan node is NULL");
339 		QDF_ASSERT(0);
340 		return;
341 	}
342 
343 	if (scan_node->cookie != SCAN_NODE_ACTIVE_COOKIE) {
344 		scm_debug("node is already deleted");
345 		return;
346 	}
347 	/* Seems node is already deleted */
348 	if (!qdf_atomic_read(&scan_node->ref_cnt)) {
349 		scm_debug("node is already deleted ref 0");
350 		return;
351 	}
352 	scan_node->cookie = 0;
353 	scm_scan_entry_put_ref(scan_db, scan_node, false);
354 }
355 
356 /**
357  * scm_add_scan_node() - API to add scan node
358  * @scan_db: data base
359  * @scan_node: node to be added
360  * @dup_node: node before which new node to be added
361  * if it's not NULL, otherwise add node to tail
362  *
363  * Call must be protected by scan_db->scan_db_lock
364  *
365  * Return: void
366  */
367 static void scm_add_scan_node(struct scan_dbs *scan_db,
368 	struct scan_cache_node *scan_node,
369 	struct scan_cache_node *dup_node)
370 {
371 	uint8_t hash_idx;
372 
373 	hash_idx =
374 		SCAN_GET_HASH(scan_node->entry->bssid.bytes);
375 
376 	qdf_atomic_init(&scan_node->ref_cnt);
377 	scan_node->cookie = SCAN_NODE_ACTIVE_COOKIE;
378 	scm_scan_entry_get_ref(scan_node);
379 	if (!dup_node)
380 		qdf_list_insert_back(&scan_db->scan_hash_tbl[hash_idx],
381 				     &scan_node->node);
382 	else
383 		qdf_list_insert_before(&scan_db->scan_hash_tbl[hash_idx],
384 				       &scan_node->node, &dup_node->node);
385 
386 	scan_db->num_entries++;
387 }
388 
389 
390 /**
391  * scm_get_next_valid_node() - API get the next valid scan node from
392  * the list
393  * @list: hash list
394  * @cur_node: current node pointer
395  *
396  * API to get next active node from the list. If cur_node is NULL
397  * it will return first node of the list.
398  * Call must be protected by scan_db->scan_db_lock
399  *
400  * Return: next scan node
401  */
402 static qdf_list_node_t *
403 scm_get_next_valid_node(qdf_list_t *list,
404 	qdf_list_node_t *cur_node)
405 {
406 	qdf_list_node_t *next_node = NULL;
407 	qdf_list_node_t *temp_node = NULL;
408 	struct scan_cache_node *scan_node;
409 
410 	if (cur_node)
411 		qdf_list_peek_next(list, cur_node, &next_node);
412 	else
413 		qdf_list_peek_front(list, &next_node);
414 
415 	while (next_node) {
416 		scan_node = qdf_container_of(next_node,
417 			struct scan_cache_node, node);
418 		if (scan_node->cookie == SCAN_NODE_ACTIVE_COOKIE)
419 			return next_node;
420 		/*
421 		 * If node is not valid check for next entry
422 		 * to get next valid node.
423 		 */
424 		qdf_list_peek_next(list, next_node, &temp_node);
425 		next_node = temp_node;
426 		temp_node = NULL;
427 	}
428 
429 	return next_node;
430 }
431 
432 /**
433  * scm_get_next_node() - API get the next scan node from
434  * the list
435  * @scan_db: scan data base
436  * @list: hash list
437  * @cur_node: current node pointer
438  *
439  * API get the next node from the list. If cur_node is NULL
440  * it will return first node of the list
441  *
442  * Return: next scan cache node
443  */
444 static struct scan_cache_node *
445 scm_get_next_node(struct scan_dbs *scan_db,
446 	qdf_list_t *list, struct scan_cache_node *cur_node)
447 {
448 	struct scan_cache_node *next_node = NULL;
449 	qdf_list_node_t *next_list = NULL;
450 
451 	qdf_spin_lock_bh(&scan_db->scan_db_lock);
452 	if (cur_node) {
453 		next_list = scm_get_next_valid_node(list, &cur_node->node);
454 		/* Decrement the ref count of the previous node */
455 		scm_scan_entry_put_ref(scan_db,
456 			cur_node, false);
457 	} else {
458 		next_list = scm_get_next_valid_node(list, NULL);
459 	}
460 	/* Increase the ref count of the obtained node */
461 	if (next_list) {
462 		next_node = qdf_container_of(next_list,
463 			struct scan_cache_node, node);
464 		scm_scan_entry_get_ref(next_node);
465 	}
466 	qdf_spin_unlock_bh(&scan_db->scan_db_lock);
467 
468 	return next_node;
469 }
470 
471 /**
472  * scm_check_and_age_out() - check and age out the old entries
473  * @scan_db: scan db
474  * @scan_node: node to check for age out
475  * @scan_aging_time: scan cache aging time
476  *
477  * Return: void
478  */
479 static void scm_check_and_age_out(struct scan_dbs *scan_db,
480 	struct scan_cache_node *node,
481 	qdf_time_t scan_aging_time)
482 {
483 	if (util_scan_entry_age(node->entry) >=
484 	   scan_aging_time) {
485 		scm_debug("Aging out BSSID: "QDF_MAC_ADDR_FMT" with age %lu ms",
486 			  QDF_MAC_ADDR_REF(node->entry->bssid.bytes),
487 			  util_scan_entry_age(node->entry));
488 		qdf_spin_lock_bh(&scan_db->scan_db_lock);
489 		scm_scan_entry_del(scan_db, node);
490 		qdf_spin_unlock_bh(&scan_db->scan_db_lock);
491 	}
492 }
493 
494 static bool scm_bss_is_connected(struct scan_cache_entry *entry)
495 {
496 	if (entry->mlme_info.assoc_state == SCAN_ENTRY_CON_STATE_ASSOC)
497 		return true;
498 	return false;
499 }
500 
501 /**
502  * scm_get_conn_node() - Get the scan cache entry node of the connected BSS
503  * @scan_db: scan DB pointer
504  *
505  * Return: scan cache entry node of connected BSS if exists, NULL otherwise
506  */
507 static
508 struct scan_cache_node *scm_get_conn_node(struct scan_dbs *scan_db)
509 {
510 	int i;
511 	struct scan_cache_node *cur_node = NULL;
512 	struct scan_cache_node *next_node = NULL;
513 
514 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
515 		cur_node = scm_get_next_node(scan_db,
516 			&scan_db->scan_hash_tbl[i], NULL);
517 		while (cur_node) {
518 			if (scm_bss_is_connected(cur_node->entry))
519 				return cur_node;
520 			next_node = scm_get_next_node(scan_db,
521 				&scan_db->scan_hash_tbl[i], cur_node);
522 			cur_node = next_node;
523 			next_node = NULL;
524 		}
525 	}
526 
527 	return NULL;
528 }
529 
530 static bool
531 scm_bss_is_nontx_of_conn_bss(struct scan_cache_node *conn_node,
532 			     struct scan_cache_node *cur_node)
533 {
534 	if (cur_node->entry->mbssid_info.profile_num &&
535 	    !memcmp(conn_node->entry->mbssid_info.trans_bssid,
536 		    cur_node->entry->mbssid_info.trans_bssid,
537 		    QDF_MAC_ADDR_SIZE))
538 		return true;
539 
540 	return false;
541 }
542 
543 void scm_age_out_entries(struct wlan_objmgr_psoc *psoc,
544 	struct scan_dbs *scan_db)
545 {
546 	int i;
547 	struct scan_cache_node *cur_node = NULL;
548 	struct scan_cache_node *next_node = NULL;
549 	struct scan_cache_node *conn_node = NULL;
550 	struct scan_default_params *def_param;
551 
552 	def_param = wlan_scan_psoc_get_def_params(psoc);
553 	if (!def_param) {
554 		scm_err("wlan_scan_psoc_get_def_params failed");
555 		return;
556 	}
557 
558 	conn_node = scm_get_conn_node(scan_db);
559 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
560 		cur_node = scm_get_next_node(scan_db,
561 			&scan_db->scan_hash_tbl[i], NULL);
562 		while (cur_node) {
563 			if (!conn_node /* if there is no connected node */ ||
564 			    /* OR cur_node is not part of the MBSSID of the
565 			     * connected node
566 			     */
567 			    (!scm_bss_is_connected(cur_node->entry) &&
568 			     !scm_bss_is_nontx_of_conn_bss(conn_node,
569 							  cur_node))) {
570 				scm_check_and_age_out(scan_db, cur_node,
571 					def_param->scan_cache_aging_time);
572 			}
573 			next_node = scm_get_next_node(scan_db,
574 				&scan_db->scan_hash_tbl[i], cur_node);
575 			cur_node = next_node;
576 			next_node = NULL;
577 		}
578 	}
579 
580 	if (conn_node)
581 		scm_scan_entry_put_ref(scan_db, conn_node, true);
582 }
583 
584 /**
585  * scm_flush_oldest_entry() - Iterate over scan db and flust out the
586  *  oldest entry
587  * @scan_db: scan db from which oldest entry needs to be flushed
588  *
589  * Return: QDF_STATUS
590  */
591 static QDF_STATUS scm_flush_oldest_entry(struct scan_dbs *scan_db)
592 {
593 	int i;
594 	struct scan_cache_node *oldest_node = NULL;
595 	struct scan_cache_node *cur_node;
596 
597 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
598 		/* Get the first valid node for the hash */
599 		cur_node = scm_get_next_node(scan_db,
600 					     &scan_db->scan_hash_tbl[i],
601 					     NULL);
602 		 /* Iterate scan db and flush out oldest node
603 		  * take ref_cnt for oldest_node
604 		  */
605 
606 		while (cur_node) {
607 			if (!oldest_node ||
608 			   (util_scan_entry_age(oldest_node->entry) <
609 			    util_scan_entry_age(cur_node->entry))) {
610 				if (oldest_node)
611 					scm_scan_entry_put_ref(scan_db,
612 							       oldest_node,
613 							       true);
614 				qdf_spin_lock_bh(&scan_db->scan_db_lock);
615 				oldest_node = cur_node;
616 				scm_scan_entry_get_ref(oldest_node);
617 				qdf_spin_unlock_bh(&scan_db->scan_db_lock);
618 			}
619 
620 			cur_node = scm_get_next_node(scan_db,
621 					&scan_db->scan_hash_tbl[i],
622 					cur_node);
623 		};
624 	}
625 
626 	if (oldest_node) {
627 		scm_debug("Flush oldest BSSID: "QDF_MAC_ADDR_FMT" with age %lu ms",
628 			  QDF_MAC_ADDR_REF(oldest_node->entry->bssid.bytes),
629 			  util_scan_entry_age(oldest_node->entry));
630 		/* Release ref_cnt taken for oldest_node and delete it */
631 		qdf_spin_lock_bh(&scan_db->scan_db_lock);
632 		scm_scan_entry_del(scan_db, oldest_node);
633 		scm_scan_entry_put_ref(scan_db, oldest_node, false);
634 		qdf_spin_unlock_bh(&scan_db->scan_db_lock);
635 	}
636 
637 	return QDF_STATUS_SUCCESS;
638 }
639 
640 /**
641  * scm_update_alt_wcn_ie() - update the alternate WCN IE
642  * @from: copy from
643  * @dst: copy to
644  *
645  * Return: void
646  */
647 static void scm_update_alt_wcn_ie(struct scan_cache_entry *from,
648 	struct scan_cache_entry *dst)
649 {
650 	uint32_t alt_wcn_ie_len;
651 
652 	if (from->frm_subtype == dst->frm_subtype)
653 		return;
654 
655 	if (!from->ie_list.wcn && !dst->ie_list.wcn)
656 		return;
657 
658 	/* Existing WCN IE is empty. */
659 	if (!from->ie_list.wcn)
660 		return;
661 
662 	alt_wcn_ie_len = 2 + from->ie_list.wcn[1];
663 	if (alt_wcn_ie_len > WLAN_MAX_IE_LEN + 2) {
664 		scm_err("invalid IE len");
665 		return;
666 	}
667 
668 	if (!dst->alt_wcn_ie.ptr) {
669 		/* allocate this additional buffer for alternate WCN IE */
670 		dst->alt_wcn_ie.ptr =
671 			qdf_mem_malloc_atomic(WLAN_MAX_IE_LEN + 2);
672 		if (!dst->alt_wcn_ie.ptr) {
673 			scm_err("failed to allocate memory");
674 			return;
675 		}
676 	}
677 	qdf_mem_copy(dst->alt_wcn_ie.ptr,
678 		from->ie_list.wcn, alt_wcn_ie_len);
679 	dst->alt_wcn_ie.len = alt_wcn_ie_len;
680 }
681 
682 /**
683  * scm_update_mlme_info() - update mlme info
684  * @src: source scan entry
685  * @dest: destination scan entry
686  *
687  * Return: void
688  */
689 static inline void
690 scm_update_mlme_info(struct scan_cache_entry *src,
691 	struct scan_cache_entry *dest)
692 {
693 	qdf_mem_copy(&dest->mlme_info, &src->mlme_info,
694 		sizeof(struct mlme_info));
695 }
696 
697 /**
698  * scm_copy_info_from_dup_entry() - copy duplicate node info
699  * to new scan entry
700  * @pdev: pdev ptr
701  * @scan_obj: scan obj ptr
702  * @scan_db: scan database
703  * @scan_params: new entry to be added
704  * @scan_node: duplicate entry
705  *
706  * Copy duplicate node info to new entry.
707  *
708  * Return: void
709  */
710 static void
711 scm_copy_info_from_dup_entry(struct wlan_objmgr_pdev *pdev,
712 			     struct wlan_scan_obj *scan_obj,
713 			     struct scan_dbs *scan_db,
714 			     struct scan_cache_entry *scan_params,
715 			     struct scan_cache_node *scan_node)
716 {
717 	struct scan_cache_entry *scan_entry;
718 	uint64_t time_gap;
719 
720 	scan_entry = scan_node->entry;
721 
722 	/* Update probe resp entry as well if AP is in hidden mode */
723 	if (scan_params->frm_subtype == MGMT_SUBTYPE_PROBE_RESP &&
724 	    scan_entry->is_hidden_ssid)
725 		scan_params->is_hidden_ssid = true;
726 
727 	/*
728 	 * If AP changed its beacon from not having an SSID to showing it the
729 	 * kernel will drop the entry asumming that something is wrong with AP.
730 	 * This can result in connection failure while updating the bss during
731 	 * connection. So flush the hidden entry from kernel before indicating
732 	 * the new entry.
733 	 */
734 	if (scan_entry->is_hidden_ssid &&
735 	    scan_params->frm_subtype == MGMT_SUBTYPE_BEACON &&
736 	    !util_scan_is_null_ssid(&scan_params->ssid)) {
737 		if (scan_obj->cb.unlink_bss) {
738 			scm_debug("Hidden AP "QDF_MAC_ADDR_FMT" switch to non-hidden SSID, So unlink the entry",
739 				  QDF_MAC_ADDR_REF(scan_entry->bssid.bytes));
740 			scan_obj->cb.unlink_bss(pdev, scan_entry);
741 		}
742 	}
743 
744 	/* If old entry have the ssid but new entry does not */
745 	if (util_scan_is_null_ssid(&scan_params->ssid) &&
746 	    scan_entry->ssid.length) {
747 		/*
748 		 * New entry has a hidden SSID and old one has the SSID.
749 		 * Add the entry by using the ssid of the old entry
750 		 * only if diff of saved SSID time and current time is
751 		 * less than HIDDEN_SSID_TIME time.
752 		 * This will avoid issues in case AP changes its SSID
753 		 * while remain hidden.
754 		 */
755 		time_gap =
756 			qdf_mc_timer_get_system_time() -
757 			scan_entry->hidden_ssid_timestamp;
758 		if (time_gap <= HIDDEN_SSID_TIME) {
759 			scan_params->hidden_ssid_timestamp =
760 				scan_entry->hidden_ssid_timestamp;
761 			scan_params->ssid.length =
762 				scan_entry->ssid.length;
763 			qdf_mem_copy(scan_params->ssid.ssid,
764 				scan_entry->ssid.ssid,
765 				scan_entry->ssid.length);
766 		}
767 	}
768 
769 	/*
770 	 * Due to Rx sensitivity issue, sometime beacons are seen on adjacent
771 	 * channel so workaround in software is needed. If DS params or HT info
772 	 * are present driver can get proper channel info from these IEs and set
773 	 * channel_mismatch so that the older RSSI values are used in new entry.
774 	 *
775 	 * For the cases where DS params and HT info is not present, driver
776 	 * needs to check below conditions to get proper channel and set
777 	 * channel_mismatch so that the older RSSI values are used in new entry:
778 	 *   -- The old entry channel and new entry channel are not same
779 	 *   -- RSSI is less than -80, this indicate that the signal has leaked
780 	 *       in adjacent channel.
781 	 */
782 	if ((scan_params->frm_subtype == MGMT_SUBTYPE_BEACON) &&
783 	    !util_scan_entry_htinfo(scan_params) &&
784 	    !util_scan_entry_ds_param(scan_params) &&
785 	    (scan_params->channel.chan_freq != scan_entry->channel.chan_freq) &&
786 	    (scan_params->rssi_raw  < ADJACENT_CHANNEL_RSSI_THRESHOLD)) {
787 		scan_params->channel.chan_freq = scan_entry->channel.chan_freq;
788 		scan_params->channel_mismatch = true;
789 	}
790 
791 	/* Use old value for rssi if beacon was heard on adjacent channel. */
792 	if (scan_params->channel_mismatch) {
793 		scan_params->snr = scan_entry->snr;
794 		scan_params->avg_snr = scan_entry->avg_snr;
795 		scan_params->rssi_raw = scan_entry->rssi_raw;
796 		scan_params->avg_rssi = scan_entry->avg_rssi;
797 		scan_params->rssi_timestamp =
798 			scan_entry->rssi_timestamp;
799 	} else {
800 		/* If elapsed time since last rssi and snr update for this
801 		 * entry is smaller than a thresold, calculate a
802 		 * running average of the RSSI and SNR values.
803 		 * Otherwise new frames RSSI and SNR are more representive
804 		 * of the signal strength.
805 		 */
806 		time_gap =
807 			scan_params->scan_entry_time -
808 			scan_entry->rssi_timestamp;
809 		if (time_gap > WLAN_RSSI_AVERAGING_TIME) {
810 			scan_params->avg_rssi =
811 				WLAN_RSSI_IN(scan_params->rssi_raw);
812 			scan_params->avg_snr =
813 				WLAN_SNR_IN(scan_params->snr);
814 		}
815 		else {
816 			/* Copy previous average rssi and snr to new entry */
817 			scan_params->avg_snr = scan_entry->avg_snr;
818 			scan_params->avg_rssi = scan_entry->avg_rssi;
819 			/* Average with previous samples */
820 			WLAN_RSSI_LPF(scan_params->avg_rssi,
821 				      scan_params->rssi_raw);
822 			WLAN_SNR_LPF(scan_params->avg_snr,
823 				     scan_params->snr);
824 		}
825 
826 		scan_params->rssi_timestamp = scan_params->scan_entry_time;
827 	}
828 
829 	/* copy wsn ie from scan_entry to scan_params*/
830 	scm_update_alt_wcn_ie(scan_entry, scan_params);
831 
832 	/* copy mlme info from scan_entry to scan_params*/
833 	scm_update_mlme_info(scan_entry, scan_params);
834 }
835 
836 /**
837  * scm_find_duplicate() - find duplicate entry,
838  * if present, add input scan entry before it and delete
839  * duplicate entry. otherwise add entry to tail
840  * @pdev: pdev ptr
841  * @scan_obj: scan obj ptr
842  * @scan_db: scan db
843  * @entry: input scan cache entry
844  * @dup_node: node before which new entry to be added
845  *
846  * ref_cnt is taken for dup_node, caller should release ref taken
847  * if returns true.
848  *
849  * Return: bool
850  */
851 static bool
852 scm_find_duplicate(struct wlan_objmgr_pdev *pdev,
853 		   struct wlan_scan_obj *scan_obj,
854 		   struct scan_dbs *scan_db,
855 		   struct scan_cache_entry *entry,
856 		   struct scan_cache_node **dup_node)
857 {
858 	uint8_t hash_idx;
859 	struct scan_cache_node *cur_node;
860 	struct scan_cache_node *next_node = NULL;
861 
862 	hash_idx = SCAN_GET_HASH(entry->bssid.bytes);
863 
864 	cur_node = scm_get_next_node(scan_db,
865 				     &scan_db->scan_hash_tbl[hash_idx],
866 				     NULL);
867 
868 	while (cur_node) {
869 		if (util_is_scan_entry_match(entry,
870 		   cur_node->entry)) {
871 			scm_copy_info_from_dup_entry(pdev, scan_obj, scan_db,
872 						     entry, cur_node);
873 			*dup_node = cur_node;
874 			return true;
875 		}
876 		next_node = scm_get_next_node(scan_db,
877 			 &scan_db->scan_hash_tbl[hash_idx], cur_node);
878 		cur_node = next_node;
879 		next_node = NULL;
880 	}
881 
882 	return false;
883 }
884 
885 /**
886  * scm_add_update_entry() - add or update scan entry
887  * @psoc: psoc ptr
888  * @pdev: pdev pointer
889  * @scan_params: new received entry
890  *
891  * Return: QDF_STATUS
892  */
893 static QDF_STATUS scm_add_update_entry(struct wlan_objmgr_psoc *psoc,
894 	struct wlan_objmgr_pdev *pdev, struct scan_cache_entry *scan_params)
895 {
896 	struct scan_cache_node *dup_node = NULL;
897 	struct scan_cache_node *scan_node = NULL;
898 	bool is_dup_found = false;
899 	QDF_STATUS status;
900 	struct scan_dbs *scan_db;
901 	struct wlan_scan_obj *scan_obj;
902 	uint8_t security_type;
903 
904 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
905 	if (!scan_db) {
906 		scm_err("scan_db is NULL");
907 		return QDF_STATUS_E_INVAL;
908 	}
909 
910 	scan_obj = wlan_psoc_get_scan_obj(psoc);
911 	if (!scan_obj) {
912 		scm_err("scan_obj is NULL");
913 		return QDF_STATUS_E_INVAL;
914 	}
915 
916 	if (scan_params->frm_subtype ==
917 	   MGMT_SUBTYPE_PROBE_RESP &&
918 	   !scan_params->ie_list.ssid)
919 		scm_debug("Probe resp doesn't contain SSID");
920 
921 
922 	if (scan_params->ie_list.csa ||
923 	   scan_params->ie_list.xcsa ||
924 	   scan_params->ie_list.cswrp)
925 		scm_debug("CSA IE present for BSSID: "QDF_MAC_ADDR_FMT,
926 			  QDF_MAC_ADDR_REF(scan_params->bssid.bytes));
927 
928 	is_dup_found = scm_find_duplicate(pdev, scan_obj, scan_db, scan_params,
929 					  &dup_node);
930 
931 	security_type = scan_params->security_type;
932 	scm_nofl_debug("Received %s: "QDF_MAC_ADDR_FMT" \"%.*s\" freq %d rssi %d tsf_delta %u seq %d snr %d phy %d hidden %d mismatch %d %s%s%s%s pdev %d boot_time %llu ns",
933 		       (scan_params->frm_subtype == MGMT_SUBTYPE_PROBE_RESP) ?
934 		       "prb rsp" : "bcn",
935 		       QDF_MAC_ADDR_REF(scan_params->bssid.bytes),
936 		       scan_params->ssid.length, scan_params->ssid.ssid,
937 		       scan_params->channel.chan_freq, scan_params->rssi_raw,
938 		       scan_params->tsf_delta, scan_params->seq_num,
939 		       scan_params->snr, scan_params->phy_mode,
940 		       scan_params->is_hidden_ssid,
941 		       scan_params->channel_mismatch,
942 		       security_type & SCAN_SECURITY_TYPE_WPA ? "[WPA]" : "",
943 		       security_type & SCAN_SECURITY_TYPE_RSN ? "[RSN]" : "",
944 		       security_type & SCAN_SECURITY_TYPE_WAPI ? "[WAPI]" : "",
945 		       security_type & SCAN_SECURITY_TYPE_WEP ? "[WEP]" : "",
946 		       wlan_objmgr_pdev_get_pdev_id(pdev),
947 		       scan_params->boottime_ns);
948 
949 	if (scan_obj->cb.inform_beacon)
950 		scan_obj->cb.inform_beacon(pdev, scan_params);
951 
952 	if (scan_db->num_entries >= MAX_SCAN_CACHE_SIZE) {
953 		status = scm_flush_oldest_entry(scan_db);
954 		if (QDF_IS_STATUS_ERROR(status)) {
955 			/* release ref taken for dup node */
956 			if (is_dup_found)
957 				scm_scan_entry_put_ref(scan_db, dup_node, true);
958 			return status;
959 		}
960 	}
961 
962 	scan_node = qdf_mem_malloc(sizeof(*scan_node));
963 	if (!scan_node) {
964 		/* release ref taken for dup node */
965 		if (is_dup_found)
966 			scm_scan_entry_put_ref(scan_db, dup_node, true);
967 		return QDF_STATUS_E_NOMEM;
968 	}
969 
970 	scan_node->entry = scan_params;
971 	qdf_spin_lock_bh(&scan_db->scan_db_lock);
972 	scm_add_scan_node(scan_db, scan_node, dup_node);
973 
974 	if (is_dup_found) {
975 		/* release ref taken for dup node and delete it */
976 		scm_scan_entry_del(scan_db, dup_node);
977 		scm_scan_entry_put_ref(scan_db, dup_node, false);
978 	}
979 	qdf_spin_unlock_bh(&scan_db->scan_db_lock);
980 
981 	return QDF_STATUS_SUCCESS;
982 }
983 
984 #ifdef CONFIG_REG_CLIENT
985 /**
986  * scm_is_bss_allowed_for_country() - Check if bss is allowed to start for a
987  * specific country and power mode (VLP?LPI/SP) for 6GHz.
988  * @psoc: psoc ptr
989  * @scan_entry: ptr to scan entry
990  *
991  * Return: True if allowed, False if not.
992  */
993 static bool scm_is_bss_allowed_for_country(struct wlan_objmgr_psoc *psoc,
994 					   struct scan_cache_entry *scan_entry)
995 {
996 	struct wlan_country_ie *cc_ie;
997 	uint8_t programmed_country[REG_ALPHA2_LEN + 1];
998 
999 	if (wlan_reg_is_6ghz_chan_freq(scan_entry->channel.chan_freq)) {
1000 		cc_ie = util_scan_entry_country(scan_entry);
1001 		if (!cc_ie)
1002 			return false;
1003 		wlan_reg_read_current_country(psoc, programmed_country);
1004 		if (cc_ie && qdf_mem_cmp(cc_ie->cc, programmed_country,
1005 					 REG_ALPHA2_LEN)) {
1006 			if (wlan_reg_is_us(programmed_country))
1007 				return false;
1008 		}
1009 	}
1010 	return true;
1011 }
1012 #else
1013 static bool scm_is_bss_allowed_for_country(struct wlan_objmgr_psoc *psoc,
1014 					   struct scan_cache_entry *scan_entry)
1015 {
1016 	return true;
1017 }
1018 #endif
1019 
1020 QDF_STATUS __scm_handle_bcn_probe(struct scan_bcn_probe_event *bcn)
1021 {
1022 	struct wlan_objmgr_psoc *psoc;
1023 	struct wlan_objmgr_pdev *pdev = NULL;
1024 	struct scan_cache_entry *scan_entry;
1025 	struct wlan_scan_obj *scan_obj;
1026 	qdf_list_t *scan_list = NULL;
1027 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1028 	uint32_t list_count, i;
1029 	qdf_list_node_t *next_node = NULL;
1030 	struct scan_cache_node *scan_node;
1031 	struct wlan_frame_hdr *hdr = NULL;
1032 	struct wlan_crypto_params sec_params;
1033 
1034 	if (!bcn) {
1035 		scm_err("bcn is NULL");
1036 		return QDF_STATUS_E_INVAL;
1037 	}
1038 	if (!bcn->rx_data) {
1039 		scm_err("rx_data iS NULL");
1040 		status = QDF_STATUS_E_INVAL;
1041 		goto free_nbuf;
1042 	}
1043 	if (!bcn->buf) {
1044 		scm_err("buf is NULL");
1045 		status = QDF_STATUS_E_INVAL;
1046 		goto free_nbuf;
1047 	}
1048 
1049 	hdr = (struct wlan_frame_hdr *)qdf_nbuf_data(bcn->buf);
1050 	psoc = bcn->psoc;
1051 	pdev = wlan_objmgr_get_pdev_by_id(psoc,
1052 			   bcn->rx_data->pdev_id, WLAN_SCAN_ID);
1053 	if (!pdev) {
1054 		scm_err("pdev is NULL");
1055 		status = QDF_STATUS_E_INVAL;
1056 		goto free_nbuf;
1057 	}
1058 	scan_obj = wlan_psoc_get_scan_obj(psoc);
1059 	if (!scan_obj) {
1060 		scm_err("scan_obj is NULL");
1061 		status = QDF_STATUS_E_INVAL;
1062 		goto free_nbuf;
1063 	}
1064 
1065 	if (qdf_nbuf_len(bcn->buf) <=
1066 	   (sizeof(struct wlan_frame_hdr) +
1067 	   offsetof(struct wlan_bcn_frame, ie))) {
1068 		scm_debug("invalid beacon/probe length");
1069 		status = QDF_STATUS_E_INVAL;
1070 		goto free_nbuf;
1071 	}
1072 
1073 	if (bcn->frm_type == MGMT_SUBTYPE_BEACON &&
1074 	    wlan_reg_is_dfs_for_freq(pdev, bcn->rx_data->chan_freq)) {
1075 		util_scan_add_hidden_ssid(pdev, bcn->buf);
1076 	}
1077 
1078 	scan_list =
1079 		 util_scan_unpack_beacon_frame(pdev, qdf_nbuf_data(bcn->buf),
1080 			qdf_nbuf_len(bcn->buf), bcn->frm_type,
1081 			bcn->rx_data);
1082 	if (!scan_list || qdf_list_empty(scan_list)) {
1083 		scm_debug("failed to unpack %d frame BSSID: "QDF_MAC_ADDR_FMT,
1084 			  bcn->frm_type, QDF_MAC_ADDR_REF(hdr->i_addr3));
1085 		status = QDF_STATUS_E_INVAL;
1086 		goto free_nbuf;
1087 	}
1088 
1089 	list_count = qdf_list_size(scan_list);
1090 	for (i = 0; i < list_count; i++) {
1091 		status = qdf_list_remove_front(scan_list, &next_node);
1092 		if (QDF_IS_STATUS_ERROR(status) || !next_node) {
1093 			scm_debug("list remove failure i:%d, lsize:%d, BSSID: "QDF_MAC_ADDR_FMT,
1094 				  i, list_count, QDF_MAC_ADDR_REF(hdr->i_addr3));
1095 			status = QDF_STATUS_E_INVAL;
1096 			goto free_nbuf;
1097 		}
1098 
1099 		scan_node = qdf_container_of(next_node,
1100 			struct scan_cache_node, node);
1101 
1102 		scan_entry = scan_node->entry;
1103 
1104 		if (scan_obj->drop_bcn_on_chan_mismatch &&
1105 		    scan_entry->channel_mismatch) {
1106 			scm_nofl_debug("Drop frame for chan mismatch "QDF_MAC_ADDR_FMT" Seq Num: %d freq %d RSSI %d",
1107 				       QDF_MAC_ADDR_REF(scan_entry->bssid.bytes),
1108 				       scan_entry->seq_num,
1109 				       scan_entry->channel.chan_freq,
1110 				       scan_entry->rssi_raw);
1111 			util_scan_free_cache_entry(scan_entry);
1112 			qdf_mem_free(scan_node);
1113 			continue;
1114 		}
1115 		/* Do not add invalid channel entry as kernel will reject it */
1116 		if (scan_obj->drop_bcn_on_invalid_freq &&
1117 		    wlan_reg_is_disable_for_pwrmode(
1118 					pdev,
1119 					scan_entry->channel.chan_freq,
1120 					REG_BEST_PWR_MODE)) {
1121 			scm_nofl_debug("Drop frame for invalid freq %d: "QDF_MAC_ADDR_FMT" Seq Num: %d RSSI %d",
1122 				       scan_entry->channel.chan_freq,
1123 				       QDF_MAC_ADDR_REF(scan_entry->bssid.bytes),
1124 				       scan_entry->seq_num,
1125 				       scan_entry->rssi_raw);
1126 			util_scan_free_cache_entry(scan_entry);
1127 			qdf_mem_free(scan_node);
1128 			continue;
1129 		}
1130 		if (util_scan_entry_rsn(scan_entry)) {
1131 			status = wlan_crypto_rsnie_check(
1132 					&sec_params,
1133 					util_scan_entry_rsn(scan_entry));
1134 			if (QDF_IS_STATUS_ERROR(status)) {
1135 				scm_nofl_debug("Drop frame from invalid RSN IE AP"
1136 					       QDF_MAC_ADDR_FMT
1137 					       ": RSN IE parse failed, status %d",
1138 					       QDF_MAC_ADDR_REF(
1139 					       scan_entry->bssid.bytes),
1140 					       status);
1141 				util_scan_free_cache_entry(scan_entry);
1142 				qdf_mem_free(scan_node);
1143 				continue;
1144 			}
1145 		}
1146 		if (wlan_cm_get_check_6ghz_security(psoc) &&
1147 		    wlan_reg_is_6ghz_chan_freq(scan_entry->channel.chan_freq)) {
1148 			if (!util_scan_entry_rsn(scan_entry)) {
1149 				scm_info_rl(
1150 					"Drop frame from "QDF_MAC_ADDR_FMT
1151 					": No RSN IE for 6GHz AP",
1152 					QDF_MAC_ADDR_REF(
1153 						scan_entry->bssid.bytes));
1154 				util_scan_free_cache_entry(scan_entry);
1155 				qdf_mem_free(scan_node);
1156 				continue;
1157 			}
1158 			status = wlan_crypto_rsnie_check(&sec_params,
1159 					util_scan_entry_rsn(scan_entry));
1160 			if (QDF_IS_STATUS_ERROR(status)) {
1161 				scm_info_rl(
1162 					"Drop frame from 6GHz AP "
1163 					QDF_MAC_ADDR_FMT
1164 					": RSN IE parse failed, status %d",
1165 					QDF_MAC_ADDR_REF(
1166 						scan_entry->bssid.bytes),
1167 					status);
1168 				util_scan_free_cache_entry(scan_entry);
1169 				qdf_mem_free(scan_node);
1170 				continue;
1171 			}
1172 			if ((QDF_HAS_PARAM(sec_params.ucastcipherset,
1173 					   WLAN_CRYPTO_CIPHER_NONE)) ||
1174 			    (QDF_HAS_PARAM(sec_params.ucastcipherset,
1175 					   WLAN_CRYPTO_CIPHER_TKIP)) ||
1176 			    (QDF_HAS_PARAM(sec_params.ucastcipherset,
1177 					   WLAN_CRYPTO_CIPHER_WEP_40)) ||
1178 			    (QDF_HAS_PARAM(sec_params.ucastcipherset,
1179 					   WLAN_CRYPTO_CIPHER_WEP_104))) {
1180 				scm_info_rl(
1181 					"Drop frame from "QDF_MAC_ADDR_FMT
1182 					": Invalid sec type %0X for 6GHz AP",
1183 					QDF_MAC_ADDR_REF(
1184 						scan_entry->bssid.bytes),
1185 					sec_params.ucastcipherset);
1186 				util_scan_free_cache_entry(scan_entry);
1187 				qdf_mem_free(scan_node);
1188 				continue;
1189 			}
1190 			if (!wlan_cm_6ghz_allowed_for_akm(psoc,
1191 					sec_params.key_mgmt,
1192 					sec_params.rsn_caps,
1193 					util_scan_entry_rsnxe(scan_entry),
1194 					0, false)) {
1195 				scm_info_rl(
1196 					"Drop frame from "QDF_MAC_ADDR_FMT
1197 					": Invalid AKM suite %0X for 6GHz AP",
1198 					QDF_MAC_ADDR_REF(
1199 						scan_entry->bssid.bytes),
1200 					sec_params.key_mgmt);
1201 				util_scan_free_cache_entry(scan_entry);
1202 				qdf_mem_free(scan_node);
1203 				continue;
1204 			}
1205 		}
1206 		if (scan_obj->cb.update_beacon)
1207 			scan_obj->cb.update_beacon(pdev, scan_entry);
1208 
1209 		/**
1210 		 * Do not drop the frame if Wi-Fi safe mode or RF test mode is
1211 		 * enabled. wlan_cm_get_check_6ghz_security API returns true if
1212 		 * neither Safe mode nor RF test mode are enabled.
1213 		 */
1214 		if (!scm_is_bss_allowed_for_country(psoc, scan_entry) &&
1215 		    wlan_cm_get_check_6ghz_security(psoc)) {
1216 			scm_info_rl(
1217 				"Drop frame from "QDF_MAC_ADDR_FMT
1218 				": AP in VLP mode not supported for US",
1219 				QDF_MAC_ADDR_REF(scan_entry->bssid.bytes));
1220 			util_scan_free_cache_entry(scan_entry);
1221 			qdf_mem_free(scan_node);
1222 			continue;
1223 		}
1224 
1225 		status = scm_add_update_entry(psoc, pdev, scan_entry);
1226 		if (QDF_IS_STATUS_ERROR(status)) {
1227 			scm_debug("failed to add entry for BSSID: "QDF_MAC_ADDR_FMT" Seq Num: %d",
1228 				  QDF_MAC_ADDR_REF(scan_entry->bssid.bytes),
1229 				  scan_entry->seq_num);
1230 			util_scan_free_cache_entry(scan_entry);
1231 			qdf_mem_free(scan_node);
1232 			continue;
1233 		}
1234 
1235 		qdf_mem_free(scan_node);
1236 	}
1237 
1238 free_nbuf:
1239 	if (scan_list)
1240 		qdf_mem_free(scan_list);
1241 	if (bcn->psoc)
1242 		wlan_objmgr_psoc_release_ref(bcn->psoc, WLAN_SCAN_ID);
1243 	if (pdev)
1244 		wlan_objmgr_pdev_release_ref(pdev, WLAN_SCAN_ID);
1245 	if (bcn->rx_data)
1246 		qdf_mem_free(bcn->rx_data);
1247 	if (bcn->buf)
1248 		qdf_nbuf_free(bcn->buf);
1249 	qdf_mem_free(bcn);
1250 
1251 	return status;
1252 }
1253 
1254 QDF_STATUS scm_handle_bcn_probe(struct scheduler_msg *msg)
1255 {
1256 	if (!msg) {
1257 		scm_err("msg is NULL");
1258 		return QDF_STATUS_E_NULL_VALUE;
1259 	}
1260 
1261 	return __scm_handle_bcn_probe(msg->bodyptr);
1262 }
1263 
1264 /**
1265  * scm_scan_apply_filter_get_entry() - apply filter and get the
1266  * scan entry
1267  * @psoc: psoc pointer
1268  * @db_entry: scan entry
1269  * @filter: filter to be applied
1270  * @scan_list: scan list to which entry is added
1271  *
1272  * Return: QDF_STATUS
1273  */
1274 static QDF_STATUS
1275 scm_scan_apply_filter_get_entry(struct wlan_objmgr_psoc *psoc,
1276 	struct scan_cache_entry *db_entry,
1277 	struct scan_filter *filter,
1278 	qdf_list_t *scan_list)
1279 {
1280 	struct scan_cache_node *scan_node = NULL;
1281 	struct security_info security = {0};
1282 	bool match;
1283 
1284 	if (!filter)
1285 		match = true;
1286 	else
1287 		match = scm_filter_match(psoc, db_entry,
1288 					filter, &security);
1289 
1290 	if (!match)
1291 		return QDF_STATUS_SUCCESS;
1292 
1293 	scan_node = qdf_mem_malloc_atomic(sizeof(*scan_node));
1294 	if (!scan_node)
1295 		return QDF_STATUS_E_NOMEM;
1296 
1297 	scan_node->entry =
1298 		util_scan_copy_cache_entry(db_entry);
1299 
1300 	if (!scan_node->entry) {
1301 		qdf_mem_free(scan_node);
1302 		return QDF_STATUS_E_NOMEM;
1303 	}
1304 
1305 	qdf_mem_copy(&scan_node->entry->neg_sec_info,
1306 		&security, sizeof(scan_node->entry->neg_sec_info));
1307 
1308 	qdf_list_insert_front(scan_list, &scan_node->node);
1309 
1310 	return QDF_STATUS_SUCCESS;
1311 }
1312 
1313 /**
1314  * scm_get_results() - Iterate and get scan results
1315  * @psoc: psoc ptr
1316  * @scan_db: scan db
1317  * @filter: filter to be applied
1318  * @scan_list: scan list to which entry is added
1319  *
1320  * Return: void
1321  */
1322 static void scm_get_results(struct wlan_objmgr_psoc *psoc,
1323 	struct scan_dbs *scan_db, struct scan_filter *filter,
1324 	qdf_list_t *scan_list)
1325 {
1326 	int i, count;
1327 	struct scan_cache_node *cur_node;
1328 	struct scan_cache_node *next_node = NULL;
1329 
1330 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
1331 		cur_node = scm_get_next_node(scan_db,
1332 			   &scan_db->scan_hash_tbl[i], NULL);
1333 		count = qdf_list_size(&scan_db->scan_hash_tbl[i]);
1334 		if (!count)
1335 			continue;
1336 		while (cur_node) {
1337 			scm_scan_apply_filter_get_entry(psoc,
1338 				cur_node->entry, filter, scan_list);
1339 			next_node = scm_get_next_node(scan_db,
1340 				&scan_db->scan_hash_tbl[i], cur_node);
1341 			cur_node = next_node;
1342 		}
1343 	}
1344 }
1345 
1346 QDF_STATUS scm_purge_scan_results(qdf_list_t *scan_list)
1347 {
1348 	QDF_STATUS status;
1349 	struct scan_cache_node *cur_node;
1350 	qdf_list_node_t *cur_lst = NULL, *next_lst = NULL;
1351 
1352 	if (!scan_list) {
1353 		scm_err("scan_result is NULL");
1354 		return QDF_STATUS_E_INVAL;
1355 	}
1356 
1357 	status = qdf_list_peek_front(scan_list, &cur_lst);
1358 
1359 	while (cur_lst) {
1360 		qdf_list_peek_next(
1361 			scan_list, cur_lst, &next_lst);
1362 		cur_node = qdf_container_of(cur_lst,
1363 			struct scan_cache_node, node);
1364 		status = qdf_list_remove_node(scan_list,
1365 					cur_lst);
1366 		if (QDF_IS_STATUS_SUCCESS(status)) {
1367 			util_scan_free_cache_entry(cur_node->entry);
1368 			qdf_mem_free(cur_node);
1369 		}
1370 		cur_lst = next_lst;
1371 		next_lst = NULL;
1372 	}
1373 
1374 	qdf_list_destroy(scan_list);
1375 	qdf_mem_free(scan_list);
1376 
1377 	return status;
1378 }
1379 
1380 qdf_list_t *scm_get_scan_result(struct wlan_objmgr_pdev *pdev,
1381 	struct scan_filter *filter)
1382 {
1383 	struct wlan_objmgr_psoc *psoc;
1384 	struct scan_dbs *scan_db;
1385 	qdf_list_t *tmp_list;
1386 
1387 	if (!pdev) {
1388 		scm_err("pdev is NULL");
1389 		return NULL;
1390 	}
1391 
1392 	psoc = wlan_pdev_get_psoc(pdev);
1393 	if (!psoc) {
1394 		scm_err("psoc is NULL");
1395 		return NULL;
1396 	}
1397 
1398 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1399 	if (!scan_db) {
1400 		scm_err("scan_db is NULL");
1401 		return NULL;
1402 	}
1403 
1404 	tmp_list = qdf_mem_malloc_atomic(sizeof(*tmp_list));
1405 	if (!tmp_list) {
1406 		scm_err("failed tp allocate scan_result");
1407 		return NULL;
1408 	}
1409 	qdf_list_create(tmp_list,
1410 			MAX_SCAN_CACHE_SIZE);
1411 	scm_age_out_entries(psoc, scan_db);
1412 	scm_get_results(psoc, scan_db, filter, tmp_list);
1413 
1414 	return tmp_list;
1415 }
1416 
1417 /**
1418  * scm_iterate_db_and_call_func() - iterate and call the func
1419  * @scan_db: scan db
1420  * @func: func to be called
1421  * @arg: func arg
1422  *
1423  * Return: QDF_STATUS
1424  */
1425 static QDF_STATUS
1426 scm_iterate_db_and_call_func(struct scan_dbs *scan_db,
1427 	scan_iterator_func func, void *arg)
1428 {
1429 	int i;
1430 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1431 	struct scan_cache_node *cur_node;
1432 	struct scan_cache_node *next_node = NULL;
1433 
1434 	if (!func)
1435 		return QDF_STATUS_E_INVAL;
1436 
1437 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
1438 		cur_node = scm_get_next_node(scan_db,
1439 			&scan_db->scan_hash_tbl[i], NULL);
1440 		while (cur_node) {
1441 			status = func(arg, cur_node->entry);
1442 			if (QDF_IS_STATUS_ERROR(status)) {
1443 				scm_scan_entry_put_ref(scan_db,
1444 					cur_node, true);
1445 				return status;
1446 			}
1447 			next_node = scm_get_next_node(scan_db,
1448 				&scan_db->scan_hash_tbl[i], cur_node);
1449 			cur_node = next_node;
1450 		}
1451 	}
1452 
1453 	return status;
1454 }
1455 
1456 QDF_STATUS
1457 scm_iterate_scan_db(struct wlan_objmgr_pdev *pdev,
1458 	scan_iterator_func func, void *arg)
1459 {
1460 	struct wlan_objmgr_psoc *psoc;
1461 	struct scan_dbs *scan_db;
1462 	QDF_STATUS status;
1463 
1464 	if (!func) {
1465 		scm_err("func is NULL");
1466 		return QDF_STATUS_E_INVAL;
1467 	}
1468 
1469 	if (!pdev) {
1470 		scm_err("pdev is NULL");
1471 		return QDF_STATUS_E_INVAL;
1472 	}
1473 
1474 	psoc = wlan_pdev_get_psoc(pdev);
1475 	if (!psoc) {
1476 		scm_err("psoc is NULL");
1477 		return QDF_STATUS_E_INVAL;
1478 	}
1479 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1480 	if (!scan_db) {
1481 		scm_err("scan_db is NULL");
1482 		return QDF_STATUS_E_INVAL;
1483 	}
1484 
1485 	scm_age_out_entries(psoc, scan_db);
1486 	status = scm_iterate_db_and_call_func(scan_db, func, arg);
1487 
1488 	return status;
1489 }
1490 
1491 /**
1492  * scm_scan_apply_filter_flush_entry() -flush scan entries depending
1493  * on filter
1494  * @psoc: psoc ptr
1495  * @scan_db: scan db
1496  * @db_node: node on which filters are applied
1497  * @filter: filter to be applied
1498  *
1499  * Return: QDF_STATUS
1500  */
1501 static QDF_STATUS
1502 scm_scan_apply_filter_flush_entry(struct wlan_objmgr_psoc *psoc,
1503 	struct scan_dbs *scan_db,
1504 	struct scan_cache_node *db_node,
1505 	struct scan_filter *filter)
1506 {
1507 	struct security_info security = {0};
1508 	bool match;
1509 
1510 	if (!filter)
1511 		match = true;
1512 	else
1513 		match = scm_filter_match(psoc, db_node->entry,
1514 					filter, &security);
1515 
1516 	if (!match)
1517 		return QDF_STATUS_SUCCESS;
1518 
1519 	qdf_spin_lock_bh(&scan_db->scan_db_lock);
1520 	scm_scan_entry_del(scan_db, db_node);
1521 	qdf_spin_unlock_bh(&scan_db->scan_db_lock);
1522 
1523 	return QDF_STATUS_SUCCESS;
1524 }
1525 
1526 /**
1527  * scm_flush_scan_entries() - API to flush scan entries depending on filters
1528  * @psoc: psoc ptr
1529  * @scan_db: scan db
1530  * @filter: filter
1531  *
1532  * Return: void
1533  */
1534 static void scm_flush_scan_entries(struct wlan_objmgr_psoc *psoc,
1535 	struct scan_dbs *scan_db,
1536 	struct scan_filter *filter)
1537 {
1538 	int i;
1539 	struct scan_cache_node *cur_node;
1540 	struct scan_cache_node *next_node = NULL;
1541 
1542 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
1543 		cur_node = scm_get_next_node(scan_db,
1544 			   &scan_db->scan_hash_tbl[i], NULL);
1545 		while (cur_node) {
1546 			scm_scan_apply_filter_flush_entry(psoc, scan_db,
1547 				cur_node, filter);
1548 			next_node = scm_get_next_node(scan_db,
1549 				&scan_db->scan_hash_tbl[i], cur_node);
1550 			cur_node = next_node;
1551 		}
1552 	}
1553 }
1554 
1555 QDF_STATUS scm_flush_results(struct wlan_objmgr_pdev *pdev,
1556 	struct scan_filter *filter)
1557 {
1558 	struct wlan_objmgr_psoc *psoc;
1559 	struct scan_dbs *scan_db;
1560 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1561 
1562 	if (!pdev) {
1563 		scm_err("pdev is NULL");
1564 		return QDF_STATUS_E_INVAL;
1565 	}
1566 
1567 	psoc = wlan_pdev_get_psoc(pdev);
1568 	if (!psoc) {
1569 		scm_err("psoc is NULL");
1570 		return QDF_STATUS_E_INVAL;
1571 	}
1572 
1573 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1574 	if (!scan_db) {
1575 		scm_err("scan_db is NULL");
1576 		return QDF_STATUS_E_INVAL;
1577 	}
1578 
1579 	scm_flush_scan_entries(psoc, scan_db, filter);
1580 
1581 	return status;
1582 }
1583 
1584 /**
1585  * scm_filter_channels() - Remove entries not belonging to channel list
1586  * @scan_db: scan db
1587  * @db_node: node on which filters are applied
1588  * @chan_freq_list: valid channel frequency (in MHz) list
1589  * @num_chan: number of channels
1590  *
1591  * Return: QDF_STATUS
1592  */
1593 static void scm_filter_channels(struct wlan_objmgr_pdev *pdev,
1594 				struct scan_dbs *scan_db,
1595 				struct scan_cache_node *db_node,
1596 				uint32_t *chan_freq_list, uint32_t num_chan)
1597 {
1598 	int i;
1599 	bool match = false;
1600 
1601 	for (i = 0; i < num_chan; i++) {
1602 		if (chan_freq_list[i] == util_scan_entry_channel_frequency(
1603 							db_node->entry)) {
1604 			match = true;
1605 			break;
1606 		}
1607 	}
1608 
1609 	if (!match) {
1610 		qdf_spin_lock_bh(&scan_db->scan_db_lock);
1611 		scm_scan_entry_del(scan_db, db_node);
1612 		qdf_spin_unlock_bh(&scan_db->scan_db_lock);
1613 	}
1614 }
1615 
1616 void scm_filter_valid_channel(struct wlan_objmgr_pdev *pdev,
1617 	uint32_t *chan_freq_list, uint32_t num_chan)
1618 {
1619 	int i;
1620 	struct wlan_objmgr_psoc *psoc;
1621 	struct scan_dbs *scan_db;
1622 	struct scan_cache_node *cur_node;
1623 	struct scan_cache_node *next_node = NULL;
1624 
1625 	scm_debug("num_chan = %d", num_chan);
1626 
1627 	if (!pdev) {
1628 		scm_err("pdev is NULL");
1629 		return;
1630 	}
1631 
1632 	psoc = wlan_pdev_get_psoc(pdev);
1633 	if (!psoc) {
1634 		scm_err("psoc is NULL");
1635 		return;
1636 	}
1637 
1638 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1639 	if (!scan_db) {
1640 		scm_err("scan_db is NULL");
1641 		return;
1642 	}
1643 
1644 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
1645 		cur_node = scm_get_next_node(scan_db,
1646 			   &scan_db->scan_hash_tbl[i], NULL);
1647 		while (cur_node) {
1648 			scm_filter_channels(pdev, scan_db,
1649 					    cur_node, chan_freq_list, num_chan);
1650 			next_node = scm_get_next_node(scan_db,
1651 				&scan_db->scan_hash_tbl[i], cur_node);
1652 			cur_node = next_node;
1653 		}
1654 	}
1655 }
1656 
1657 QDF_STATUS scm_scan_register_bcn_cb(struct wlan_objmgr_psoc *psoc,
1658 	update_beacon_cb cb, enum scan_cb_type type)
1659 {
1660 	struct wlan_scan_obj *scan_obj;
1661 
1662 	scan_obj = wlan_psoc_get_scan_obj(psoc);
1663 	if (!scan_obj) {
1664 		scm_err("scan obj is NULL");
1665 		return QDF_STATUS_E_INVAL;
1666 	}
1667 	switch (type) {
1668 	case SCAN_CB_TYPE_INFORM_BCN:
1669 		scan_obj->cb.inform_beacon = cb;
1670 		break;
1671 	case SCAN_CB_TYPE_UPDATE_BCN:
1672 		scan_obj->cb.update_beacon = cb;
1673 		break;
1674 	case SCAN_CB_TYPE_UNLINK_BSS:
1675 		scan_obj->cb.unlink_bss = cb;
1676 		break;
1677 	default:
1678 		scm_err("invalid cb type %d", type);
1679 	}
1680 
1681 	return QDF_STATUS_SUCCESS;
1682 }
1683 
1684 QDF_STATUS scm_db_init(struct wlan_objmgr_psoc *psoc)
1685 {
1686 	int i, j;
1687 	struct scan_dbs *scan_db;
1688 
1689 	if (!psoc) {
1690 		scm_err("psoc is NULL");
1691 		return QDF_STATUS_E_INVAL;
1692 	}
1693 
1694 	/* Initialize the scan database per pdev */
1695 	for (i = 0; i < WLAN_UMAC_MAX_PDEVS; i++) {
1696 		scan_db = wlan_pdevid_get_scan_db(psoc, i);
1697 		if (!scan_db) {
1698 			scm_err("scan_db is NULL %d", i);
1699 			continue;
1700 		}
1701 		scan_db->num_entries = 0;
1702 		qdf_spinlock_create(&scan_db->scan_db_lock);
1703 		for (j = 0; j < SCAN_HASH_SIZE; j++)
1704 			qdf_list_create(&scan_db->scan_hash_tbl[j],
1705 				MAX_SCAN_CACHE_SIZE);
1706 	}
1707 	return QDF_STATUS_SUCCESS;
1708 }
1709 
1710 QDF_STATUS scm_db_deinit(struct wlan_objmgr_psoc *psoc)
1711 {
1712 	int i, j;
1713 	struct scan_dbs *scan_db;
1714 
1715 	if (!psoc) {
1716 		scm_err("scan obj is NULL");
1717 		return QDF_STATUS_E_INVAL;
1718 	}
1719 
1720 	/* Initialize the scan database per pdev */
1721 	for (i = 0; i < WLAN_UMAC_MAX_PDEVS; i++) {
1722 		scan_db = wlan_pdevid_get_scan_db(psoc, i);
1723 		if (!scan_db) {
1724 			scm_err("scan_db is NULL %d", i);
1725 			continue;
1726 		}
1727 
1728 		scm_flush_scan_entries(psoc, scan_db, NULL);
1729 		for (j = 0; j < SCAN_HASH_SIZE; j++)
1730 			qdf_list_destroy(&scan_db->scan_hash_tbl[j]);
1731 		qdf_spinlock_destroy(&scan_db->scan_db_lock);
1732 	}
1733 
1734 	return QDF_STATUS_SUCCESS;
1735 }
1736 
1737 #ifdef FEATURE_6G_SCAN_CHAN_SORT_ALGO
1738 QDF_STATUS scm_channel_list_db_init(struct wlan_objmgr_psoc *psoc)
1739 {
1740 	uint32_t i, j;
1741 	uint32_t min_freq, max_freq;
1742 	struct channel_list_db *rnr_channel_db;
1743 
1744 	min_freq = wlan_reg_min_6ghz_chan_freq();
1745 	max_freq = wlan_reg_max_6ghz_chan_freq();
1746 
1747 	scm_info("min_freq %d max_freq %d", min_freq, max_freq);
1748 	i = min_freq;
1749 	rnr_channel_db = scm_get_rnr_channel_db(psoc);
1750 	if (!rnr_channel_db)
1751 		return QDF_STATUS_E_INVAL;
1752 
1753 	for (j = 0; j < QDF_ARRAY_SIZE(rnr_channel_db->channel); j++) {
1754 		if (i >= min_freq && i <= max_freq)
1755 			rnr_channel_db->channel[j].chan_freq = i;
1756 		i += 20;
1757 		/* init list for all to avoid uninitialized list */
1758 		qdf_list_create(&rnr_channel_db->channel[j].rnr_list,
1759 				WLAN_MAX_RNR_COUNT);
1760 	}
1761 	return QDF_STATUS_SUCCESS;
1762 }
1763 
1764 QDF_STATUS scm_channel_list_db_deinit(struct wlan_objmgr_psoc *psoc)
1765 {
1766 	int i;
1767 	qdf_list_node_t *cur_node, *next_node;
1768 	struct meta_rnr_channel *channel;
1769 	struct scan_rnr_node *rnr_node;
1770 	struct channel_list_db *rnr_channel_db;
1771 
1772 	rnr_channel_db = scm_get_rnr_channel_db(psoc);
1773 	if (!rnr_channel_db)
1774 		return QDF_STATUS_E_INVAL;
1775 
1776 	for (i = 0; i < QDF_ARRAY_SIZE(rnr_channel_db->channel); i++) {
1777 		channel = &rnr_channel_db->channel[i];
1778 		channel->chan_freq = 0;
1779 		channel->beacon_probe_last_time_found = 0;
1780 		channel->bss_beacon_probe_count = 0;
1781 		channel->saved_profile_count = 0;
1782 		cur_node = NULL;
1783 		qdf_list_peek_front(&channel->rnr_list, &cur_node);
1784 		while (cur_node) {
1785 			next_node = NULL;
1786 			qdf_list_peek_next(&channel->rnr_list, cur_node,
1787 					   &next_node);
1788 			rnr_node = qdf_container_of(cur_node,
1789 						    struct scan_rnr_node,
1790 						    node);
1791 			qdf_list_remove_node(&channel->rnr_list,
1792 					     &rnr_node->node);
1793 			qdf_mem_free(rnr_node);
1794 			cur_node = next_node;
1795 			next_node = NULL;
1796 		}
1797 		qdf_list_destroy(&channel->rnr_list);
1798 	}
1799 
1800 	return QDF_STATUS_SUCCESS;
1801 }
1802 
1803 QDF_STATUS scm_rnr_db_flush(struct wlan_objmgr_psoc *psoc)
1804 {
1805 	int i;
1806 	qdf_list_node_t *cur_node, *next_node;
1807 	struct meta_rnr_channel *channel;
1808 	struct scan_rnr_node *rnr_node;
1809 	struct channel_list_db *rnr_channel_db;
1810 
1811 	rnr_channel_db = scm_get_rnr_channel_db(psoc);
1812 	if (!rnr_channel_db)
1813 		return QDF_STATUS_E_INVAL;
1814 
1815 	for (i = 0; i < QDF_ARRAY_SIZE(rnr_channel_db->channel); i++) {
1816 		channel = &rnr_channel_db->channel[i];
1817 		cur_node = NULL;
1818 		qdf_list_peek_front(&channel->rnr_list, &cur_node);
1819 		while (cur_node) {
1820 			next_node = NULL;
1821 			qdf_list_peek_next(&channel->rnr_list, cur_node,
1822 					   &next_node);
1823 			rnr_node = qdf_container_of(cur_node,
1824 						    struct scan_rnr_node,
1825 						    node);
1826 			qdf_list_remove_node(&channel->rnr_list,
1827 					     &rnr_node->node);
1828 			qdf_mem_free(rnr_node);
1829 			cur_node = next_node;
1830 			next_node = NULL;
1831 		}
1832 		/* Reset beacon info */
1833 		channel->beacon_probe_last_time_found = 0;
1834 		channel->bss_beacon_probe_count = 0;
1835 	}
1836 
1837 	return QDF_STATUS_SUCCESS;
1838 }
1839 
1840 void scm_update_rnr_from_scan_cache(struct wlan_objmgr_pdev *pdev)
1841 {
1842 	uint8_t i;
1843 	struct scan_dbs *scan_db;
1844 	struct scan_cache_node *cur_node;
1845 	struct scan_cache_node *next_node = NULL;
1846 	struct wlan_objmgr_psoc *psoc;
1847 	struct scan_cache_entry *entry;
1848 
1849 	psoc = wlan_pdev_get_psoc(pdev);
1850 	if (!psoc) {
1851 		scm_err("psoc is NULL");
1852 		return;
1853 	}
1854 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1855 	if (!scan_db) {
1856 		scm_err("scan_db is NULL");
1857 		return;
1858 	}
1859 
1860 	for (i = 0 ; i < SCAN_HASH_SIZE; i++) {
1861 		cur_node = scm_get_next_node(scan_db,
1862 					     &scan_db->scan_hash_tbl[i], NULL);
1863 		while (cur_node) {
1864 			entry = cur_node->entry;
1865 			scm_add_rnr_channel_db(psoc, entry);
1866 			next_node =
1867 				scm_get_next_node(scan_db,
1868 						  &scan_db->scan_hash_tbl[i],
1869 						  cur_node);
1870 			cur_node = next_node;
1871 			next_node = NULL;
1872 		}
1873 	}
1874 }
1875 #endif
1876 
1877 QDF_STATUS scm_update_scan_mlme_info(struct wlan_objmgr_pdev *pdev,
1878 	struct scan_cache_entry *entry)
1879 {
1880 	uint8_t hash_idx;
1881 	struct scan_dbs *scan_db;
1882 	struct scan_cache_node *cur_node;
1883 	struct scan_cache_node *next_node = NULL;
1884 	struct wlan_objmgr_psoc *psoc;
1885 
1886 	psoc = wlan_pdev_get_psoc(pdev);
1887 	if (!psoc) {
1888 		scm_err("psoc is NULL");
1889 		return QDF_STATUS_E_INVAL;
1890 	}
1891 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1892 	if (!scan_db) {
1893 		scm_err("scan_db is NULL");
1894 		return QDF_STATUS_E_INVAL;
1895 	}
1896 
1897 	hash_idx = SCAN_GET_HASH(entry->bssid.bytes);
1898 
1899 	cur_node = scm_get_next_node(scan_db,
1900 			&scan_db->scan_hash_tbl[hash_idx], NULL);
1901 
1902 	while (cur_node) {
1903 		if (util_is_scan_entry_match(entry,
1904 					cur_node->entry)) {
1905 			/* Acquire db lock to prevent simultaneous update */
1906 			qdf_spin_lock_bh(&scan_db->scan_db_lock);
1907 			scm_update_mlme_info(entry, cur_node->entry);
1908 			qdf_spin_unlock_bh(&scan_db->scan_db_lock);
1909 			scm_scan_entry_put_ref(scan_db,
1910 					cur_node, true);
1911 			return QDF_STATUS_SUCCESS;
1912 		}
1913 		next_node = scm_get_next_node(scan_db,
1914 				&scan_db->scan_hash_tbl[hash_idx], cur_node);
1915 		cur_node = next_node;
1916 	}
1917 
1918 	return QDF_STATUS_E_INVAL;
1919 }
1920 
1921 QDF_STATUS scm_scan_update_mlme_by_bssinfo(struct wlan_objmgr_pdev *pdev,
1922 		struct bss_info *bss_info, struct mlme_info *mlme)
1923 {
1924 	uint8_t hash_idx;
1925 	struct scan_dbs *scan_db;
1926 	struct scan_cache_node *cur_node;
1927 	struct scan_cache_node *next_node = NULL;
1928 	struct wlan_objmgr_psoc *psoc;
1929 	struct scan_cache_entry *entry;
1930 
1931 	psoc = wlan_pdev_get_psoc(pdev);
1932 	if (!psoc) {
1933 		scm_err("psoc is NULL");
1934 		return QDF_STATUS_E_INVAL;
1935 	}
1936 	scan_db = wlan_pdev_get_scan_db(psoc, pdev);
1937 	if (!scan_db) {
1938 		scm_err("scan_db is NULL");
1939 		return QDF_STATUS_E_INVAL;
1940 	}
1941 
1942 	hash_idx = SCAN_GET_HASH(bss_info->bssid.bytes);
1943 	cur_node = scm_get_next_node(scan_db,
1944 			&scan_db->scan_hash_tbl[hash_idx], NULL);
1945 	while (cur_node) {
1946 		entry = cur_node->entry;
1947 		if (qdf_is_macaddr_equal(&bss_info->bssid, &entry->bssid) &&
1948 			(util_is_ssid_match(&bss_info->ssid, &entry->ssid)) &&
1949 			(bss_info->freq == entry->channel.chan_freq)) {
1950 			/* Acquire db lock to prevent simultaneous update */
1951 			qdf_spin_lock_bh(&scan_db->scan_db_lock);
1952 			qdf_mem_copy(&entry->mlme_info, mlme,
1953 					sizeof(struct mlme_info));
1954 			scm_scan_entry_put_ref(scan_db,
1955 					cur_node, false);
1956 			qdf_spin_unlock_bh(&scan_db->scan_db_lock);
1957 			return QDF_STATUS_SUCCESS;
1958 		}
1959 		next_node = scm_get_next_node(scan_db,
1960 				&scan_db->scan_hash_tbl[hash_idx], cur_node);
1961 		cur_node = next_node;
1962 	}
1963 
1964 	return QDF_STATUS_E_INVAL;
1965 }
1966 
1967 uint32_t scm_get_last_scan_time_per_channel(struct wlan_objmgr_vdev *vdev,
1968 					    uint32_t freq)
1969 {
1970 	struct wlan_scan_obj *scan;
1971 	struct chan_list_scan_info *chan_info;
1972 	uint8_t pdev_id;
1973 	int i;
1974 
1975 	scan = wlan_vdev_get_scan_obj(vdev);
1976 	if (!scan)
1977 		return 0;
1978 
1979 	pdev_id = wlan_scan_vdev_get_pdev_id(vdev);
1980 	chan_info = &scan->pdev_info[pdev_id].chan_scan_info;
1981 
1982 	for (i = 0; i < chan_info->num_chan ; i++) {
1983 		if (chan_info->ch_scan_info[i].freq == freq)
1984 			return chan_info->ch_scan_info[i].last_scan_time;
1985 	}
1986 
1987 	return 0;
1988 }
1989