xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/scan/src/wlan_cfg80211_scan.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: defines driver functions interfacing with linux kernel
22  */
23 
24 #include <qdf_list.h>
25 #include <qdf_status.h>
26 #include <qdf_module.h>
27 #include <linux/wireless.h>
28 #include <linux/netdevice.h>
29 #include <net/cfg80211.h>
30 #include <wlan_scan_utils_api.h>
31 #include <wlan_cfg80211.h>
32 #include <wlan_cfg80211_scan.h>
33 #include <wlan_osif_priv.h>
34 #include <wlan_scan_public_structs.h>
35 #include <wlan_scan_ucfg_api.h>
36 #include <wlan_cfg80211_scan.h>
37 #include <qdf_mem.h>
38 #include <wlan_utility.h>
39 #include "cfg_ucfg_api.h"
40 #ifdef WLAN_POLICY_MGR_ENABLE
41 #include <wlan_policy_mgr_api.h>
42 #endif
43 #include <wlan_reg_services_api.h>
44 #ifdef FEATURE_WLAN_DIAG_SUPPORT
45 #include "host_diag_core_event.h"
46 #endif
47 
48 const struct nla_policy cfg80211_scan_policy[
49 			QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1] = {
50 	[QCA_WLAN_VENDOR_ATTR_SCAN_FLAGS] = {.type = NLA_U32},
51 	[QCA_WLAN_VENDOR_ATTR_SCAN_TX_NO_CCK_RATE] = {.type = NLA_FLAG},
52 	[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE] = {.type = NLA_U64},
53 };
54 
55 /**
56  * wlan_cfg80211_is_colocated_6ghz_scan_supported() - Check whether colocated
57  * 6ghz scan flag present in scan request or not
58  * @scan_flag: Flags bitmap comming from kernel
59  *
60  * Return: True if colocated 6ghz scan flag present in scan req
61  */
62 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 10, 0))
63 static bool
64 wlan_cfg80211_is_colocated_6ghz_scan_supported(uint32_t scan_flag)
65 {
66 	return !!(scan_flag & NL80211_SCAN_FLAG_COLOCATED_6GHZ);
67 }
68 #else
69 static inline bool
70 wlan_cfg80211_is_colocated_6ghz_scan_supported(uint32_t scan_flag)
71 {
72 	return false;
73 }
74 #endif
75 
76 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \
77 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
78 /**
79  * wlan_fill_scan_rand_attrs() - Populate the scan randomization attrs
80  * @vdev: pointer to objmgr vdev
81  * @flags: cfg80211 scan flags
82  * @mac_addr: random mac addr from cfg80211
83  * @mac_addr_mask: mac addr mask from cfg80211
84  * @randomize: output variable to check scan randomization status
85  * @addr: output variable to hold random addr
86  * @mask: output variable to hold mac mask
87  *
88  * Return: None
89  */
90 static void wlan_fill_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
91 				      uint32_t flags,
92 				      uint8_t *mac_addr,
93 				      uint8_t *mac_addr_mask,
94 				      bool *randomize,
95 				      uint8_t *addr,
96 				      uint8_t *mask)
97 {
98 	*randomize = false;
99 	if (!(flags & NL80211_SCAN_FLAG_RANDOM_ADDR))
100 		return;
101 
102 	if (wlan_vdev_mlme_get_opmode(vdev) != QDF_STA_MODE)
103 		return;
104 
105 	if (wlan_vdev_is_up(vdev) == QDF_STATUS_SUCCESS)
106 		return;
107 
108 	*randomize = true;
109 	memcpy(addr, mac_addr, QDF_MAC_ADDR_SIZE);
110 	memcpy(mask, mac_addr_mask, QDF_MAC_ADDR_SIZE);
111 	osif_debug("Random mac addr: "QDF_MAC_ADDR_FMT" and Random mac mask: "QDF_FULL_MAC_FMT,
112 		   QDF_MAC_ADDR_REF(addr), QDF_FULL_MAC_REF(mask));
113 }
114 
115 /**
116  * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs
117  * @vdev: pointer to objmgr vdev
118  * @request: pointer to cfg80211 scan request
119  * @req: pointer to cmn module scan request
120  *
121  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
122  * to fill random attributes of internal scan request with cfg80211_scan_request
123  *
124  * Return: None
125  */
126 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
127 				 struct cfg80211_scan_request *request,
128 				 struct scan_start_request *req)
129 {
130 	bool *randomize = &req->scan_req.scan_random.randomize;
131 	uint8_t *mac_addr = req->scan_req.scan_random.mac_addr;
132 	uint8_t *mac_mask = req->scan_req.scan_random.mac_mask;
133 
134 	wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr,
135 				  request->mac_addr_mask, randomize, mac_addr,
136 				  mac_mask);
137 	if (!*randomize)
138 		return;
139 
140 	req->scan_req.scan_f_add_spoofed_mac_in_probe = true;
141 	req->scan_req.scan_f_add_rand_seq_in_probe = true;
142 }
143 #else
144 /**
145  * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs
146  * @vdev: pointer to objmgr vdev
147  * @request: pointer to cfg80211 scan request
148  * @req: pointer to cmn module scan request
149  *
150  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
151  * to fill random attributes of internal scan request with cfg80211_scan_request
152  *
153  * Return: None
154  */
155 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
156 				 struct cfg80211_scan_request *request,
157 				 struct scan_start_request *req)
158 {
159 }
160 #endif
161 
162 #ifdef FEATURE_WLAN_SCAN_PNO
163 #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \
164 	defined(CFG80211_MULTI_SCAN_PLAN_BACKPORT))
165 
166 /**
167  * wlan_config_sched_scan_plan() - configures the sched scan plans
168  *   from the framework.
169  * @pno_req: pointer to PNO scan request
170  * @request: pointer to scan request from framework
171  *
172  * Return: None
173  */
174 static void
175 wlan_config_sched_scan_plan(struct wlan_objmgr_psoc *psoc,
176 			    struct pno_scan_req_params *pno_req,
177 			    struct cfg80211_sched_scan_request *request)
178 {
179 	if (!ucfg_scan_get_user_config_sched_scan_plan(psoc) ||
180 	    request->n_scan_plans == 1) {
181 		pno_req->fast_scan_period =
182 		request->scan_plans[0].interval * MSEC_PER_SEC;
183 		/*
184 		 * if only one scan plan is configured from framework
185 		 * then both fast and slow scan should be configured with the
186 		 * same value that is why fast scan cycles are hardcoded to one
187 		 */
188 		pno_req->fast_scan_max_cycles = 1;
189 		pno_req->slow_scan_period =
190 			request->scan_plans[0].interval * MSEC_PER_SEC;
191 	}
192 	/*
193 	 * As of now max 2 scan plans were supported by firmware
194 	 * if number of scan plan supported by firmware increased below logic
195 	 * must change.
196 	 */
197 	else if (request->n_scan_plans == SCAN_PNO_MAX_PLAN_REQUEST) {
198 		pno_req->fast_scan_period =
199 			request->scan_plans[0].interval * MSEC_PER_SEC;
200 		pno_req->fast_scan_max_cycles =
201 			request->scan_plans[0].iterations;
202 		pno_req->slow_scan_period =
203 			request->scan_plans[1].interval * MSEC_PER_SEC;
204 	} else {
205 		osif_err("Invalid number of scan plans %d !!",
206 			 request->n_scan_plans);
207 	}
208 }
209 #else
210 #define wlan_config_sched_scan_plan(psoc, pno_req, request) \
211 	__wlan_config_sched_scan_plan(pno_req, request, psoc)
212 
213 static void
214 __wlan_config_sched_scan_plan(struct pno_scan_req_params *pno_req,
215 			      struct cfg80211_sched_scan_request *request,
216 			      struct wlan_objmgr_psoc *psoc)
217 {
218 	uint32_t scan_timer_repeat_value, slow_scan_multiplier;
219 
220 	scan_timer_repeat_value = ucfg_scan_get_scan_timer_repeat_value(psoc);
221 	slow_scan_multiplier = ucfg_scan_get_slow_scan_multiplier(psoc);
222 
223 	pno_req->fast_scan_period = request->interval;
224 	pno_req->fast_scan_max_cycles = scan_timer_repeat_value;
225 	pno_req->slow_scan_period =
226 		(slow_scan_multiplier * pno_req->fast_scan_period);
227 }
228 #endif
229 
230 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
231 static inline void
232 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid)
233 {
234 	cfg80211_sched_scan_results(wiphy);
235 }
236 #else
237 static inline void
238 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid)
239 {
240 	cfg80211_sched_scan_results(wiphy, reqid);
241 }
242 #endif
243 
244 /**
245  * wlan_cfg80211_pno_callback() - pno callback function to handle
246  * pno events.
247  * @vdev: vdev ptr
248  * @event: scan events
249  * @args: argument
250  *
251  * Return: void
252  */
253 static void wlan_cfg80211_pno_callback(struct wlan_objmgr_vdev *vdev,
254 	struct scan_event *event,
255 	void *args)
256 {
257 	struct wlan_objmgr_pdev *pdev;
258 	struct pdev_osif_priv *pdev_ospriv;
259 
260 	if (event->type != SCAN_EVENT_TYPE_NLO_COMPLETE)
261 		return;
262 
263 	osif_debug("vdev id = %d", event->vdev_id);
264 
265 	pdev = wlan_vdev_get_pdev(vdev);
266 	if (!pdev) {
267 		osif_err("pdev is NULL");
268 		return;
269 	}
270 
271 	pdev_ospriv = wlan_pdev_get_ospriv(pdev);
272 	if (!pdev_ospriv) {
273 		osif_err("pdev_ospriv is NULL");
274 		return;
275 	}
276 	wlan_cfg80211_sched_scan_results(pdev_ospriv->wiphy, 0);
277 }
278 
279 #ifdef WLAN_POLICY_MGR_ENABLE
280 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc)
281 {
282 	return policy_mgr_mode_specific_connection_count(psoc,
283 							  PM_SAP_MODE,
284 							  NULL) ||
285 		policy_mgr_mode_specific_connection_count(psoc,
286 							  PM_P2P_GO_MODE,
287 							  NULL);
288 }
289 
290 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs(
291 			struct wlan_objmgr_psoc *psoc,
292 			u16 chan_freq, bool *ok)
293 {
294 	QDF_STATUS status = policy_mgr_is_chan_ok_for_dnbs(
295 				psoc, chan_freq, ok);
296 
297 	if (QDF_IS_STATUS_ERROR(status)) {
298 		osif_err("DNBS check failed");
299 		return status;
300 	}
301 
302 	return QDF_STATUS_SUCCESS;
303 }
304 #else
305 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc)
306 {
307 	return false;
308 }
309 
310 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs(
311 			struct wlan_objmgr_psoc *psoc,
312 			u16 chan_freq,
313 			bool *ok)
314 {
315 	if (!ok)
316 		return QDF_STATUS_E_INVAL;
317 
318 	*ok = true;
319 	return QDF_STATUS_SUCCESS;
320 }
321 #endif
322 
323 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \
324 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
325 /**
326  * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs
327  * @vdev: pointer to objmgr vdev
328  * @request: pointer to cfg80211 sched scan request
329  * @req: pointer to cmn module pno scan request
330  *
331  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
332  * to fill random attributes of internal pno scan
333  * with cfg80211_sched_scan_request
334  *
335  * Return: None
336  */
337 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev,
338 				    struct cfg80211_sched_scan_request *request,
339 				    struct pno_scan_req_params *req)
340 {
341 	bool *randomize = &req->scan_random.randomize;
342 	uint8_t *mac_addr = req->scan_random.mac_addr;
343 	uint8_t *mac_mask = req->scan_random.mac_mask;
344 
345 	wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr,
346 				  request->mac_addr_mask, randomize, mac_addr,
347 				  mac_mask);
348 }
349 #else
350 /**
351  * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs
352  * @vdev: pointer to objmgr vdev
353  * @request: pointer to cfg80211 sched scan request
354  * @req: pointer to cmn module pno scan request
355  *
356  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
357  * to fill random attributes of internal pno scan
358  * with cfg80211_sched_scan_request
359  *
360  * Return: None
361  */
362 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev,
363 				    struct cfg80211_sched_scan_request *request,
364 				    struct pno_scan_req_params *req)
365 {
366 }
367 #endif
368 
369 /**
370  * wlan_hdd_sched_scan_update_relative_rssi() - update CPNO params
371  * @pno_request: pointer to PNO scan request
372  * @request: Pointer to cfg80211 scheduled scan start request
373  *
374  * This function is used to update Connected PNO params sent by kernel
375  *
376  * Return: None
377  */
378 #if defined(CFG80211_REPORT_BETTER_BSS_IN_SCHED_SCAN) || \
379 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
380 static inline void wlan_hdd_sched_scan_update_relative_rssi(
381 			struct pno_scan_req_params *pno_request,
382 			struct cfg80211_sched_scan_request *request)
383 {
384 	pno_request->relative_rssi_set = request->relative_rssi_set;
385 	pno_request->relative_rssi = request->relative_rssi;
386 	if (NL80211_BAND_2GHZ == request->rssi_adjust.band)
387 		pno_request->band_rssi_pref.band = WLAN_BAND_2_4_GHZ;
388 	else if (NL80211_BAND_5GHZ == request->rssi_adjust.band)
389 		pno_request->band_rssi_pref.band = WLAN_BAND_5_GHZ;
390 	pno_request->band_rssi_pref.rssi = request->rssi_adjust.delta;
391 }
392 #else
393 static inline void wlan_hdd_sched_scan_update_relative_rssi(
394 			struct pno_scan_req_params *pno_request,
395 			struct cfg80211_sched_scan_request *request)
396 {
397 }
398 #endif
399 
400 #ifdef FEATURE_WLAN_SCAN_PNO
401 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
402 static uint32_t wlan_config_sched_scan_start_delay(
403 		struct cfg80211_sched_scan_request *request)
404 {
405 	return request->delay;
406 }
407 #else
408 static uint32_t wlan_config_sched_scan_start_delay(
409 		struct cfg80211_sched_scan_request *request)
410 {
411 	return 0;
412 }
413 #endif /*(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) */
414 #endif /* FEATURE_WLAN_SCAN_PNO */
415 
416 int wlan_cfg80211_sched_scan_start(struct wlan_objmgr_vdev *vdev,
417 				   struct cfg80211_sched_scan_request *request,
418 				   uint8_t scan_backoff_multiplier)
419 {
420 	struct pno_scan_req_params *req;
421 	int i, j, ret = 0;
422 	QDF_STATUS status;
423 	uint8_t num_chan = 0;
424 	uint16_t chan_freq;
425 	struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev);
426 	struct wlan_objmgr_psoc *psoc;
427 	uint32_t valid_ch[SCAN_PNO_MAX_NETW_CHANNELS_EX] = {0};
428 	bool enable_dfs_pno_chnl_scan;
429 
430 	if (ucfg_scan_get_pno_in_progress(vdev)) {
431 		osif_debug("pno is already in progress");
432 		return -EBUSY;
433 	}
434 
435 	if (ucfg_scan_get_pdev_status(pdev) !=
436 	   SCAN_NOT_IN_PROGRESS) {
437 		status = wlan_abort_scan(pdev,
438 				wlan_objmgr_pdev_get_pdev_id(pdev),
439 				INVAL_VDEV_ID, INVAL_SCAN_ID, true);
440 		if (QDF_IS_STATUS_ERROR(status))
441 			return -EBUSY;
442 	}
443 
444 	req = qdf_mem_malloc(sizeof(*req));
445 	if (!req)
446 		return -ENOMEM;
447 
448 	wlan_pdev_obj_lock(pdev);
449 	psoc = wlan_pdev_get_psoc(pdev);
450 	wlan_pdev_obj_unlock(pdev);
451 
452 	req->networks_cnt = request->n_match_sets;
453 	req->vdev_id = wlan_vdev_get_id(vdev);
454 	req->vdev = vdev;
455 	req->scan_policy_colocated_6ghz =
456 		wlan_cfg80211_is_colocated_6ghz_scan_supported(request->flags);
457 
458 	if ((!req->networks_cnt) ||
459 	    (req->networks_cnt > SCAN_PNO_MAX_SUPP_NETWORKS)) {
460 		osif_err("Network input is not correct %d",
461 			 req->networks_cnt);
462 		ret = -EINVAL;
463 		goto error;
464 	}
465 
466 	if (request->n_channels > SCAN_PNO_MAX_NETW_CHANNELS_EX) {
467 		osif_err("Incorrect number of channels %d",
468 			 request->n_channels);
469 		ret = -EINVAL;
470 		goto error;
471 	}
472 
473 	enable_dfs_pno_chnl_scan = ucfg_scan_is_dfs_chnl_scan_enabled(psoc);
474 	if (request->n_channels) {
475 		uint32_t buff_len;
476 		char *chl;
477 		int len = 0;
478 		bool ap_or_go_present = wlan_cfg80211_is_ap_go_present(psoc);
479 
480 		buff_len = (request->n_channels * 5) + 1;
481 		chl = qdf_mem_malloc(buff_len);
482 		if (!chl) {
483 			ret = -ENOMEM;
484 			goto error;
485 		}
486 		for (i = 0; i < request->n_channels; i++) {
487 			chan_freq = request->channels[i]->center_freq;
488 			if ((!enable_dfs_pno_chnl_scan) &&
489 			    (wlan_reg_is_dfs_for_freq(pdev, chan_freq))) {
490 				osif_debug("Dropping DFS channel freq :%d",
491 					   chan_freq);
492 				continue;
493 			}
494 			if (wlan_reg_is_dsrc_freq(chan_freq))
495 				continue;
496 
497 			if (ap_or_go_present) {
498 				bool ok;
499 
500 				status =
501 				wlan_cfg80211_is_chan_ok_for_dnbs(psoc,
502 								  chan_freq,
503 								  &ok);
504 				if (QDF_IS_STATUS_ERROR(status)) {
505 					osif_err("DNBS check failed");
506 					qdf_mem_free(chl);
507 					chl = NULL;
508 					ret = -EINVAL;
509 					goto error;
510 				}
511 				if (!ok)
512 					continue;
513 			}
514 			len += qdf_scnprintf(chl + len, buff_len - len, " %d", chan_freq);
515 			valid_ch[num_chan++] = chan_freq;
516 		}
517 		osif_debug("Channel-List[%d]:%s", num_chan, chl);
518 		qdf_mem_free(chl);
519 		chl = NULL;
520 		/* If all channels are DFS and dropped,
521 		 * then ignore the PNO request
522 		 */
523 		if (!num_chan) {
524 			osif_notice("Channel list empty due to filtering of DSRC");
525 			ret = -EINVAL;
526 			goto error;
527 		}
528 	}
529 
530 	/* Filling per profile  params */
531 	for (i = 0; i < req->networks_cnt; i++) {
532 		struct cfg80211_match_set *user_req = &request->match_sets[i];
533 		struct pno_nw_type *tgt_req = &req->networks_list[i];
534 
535 		tgt_req->ssid.length = user_req->ssid.ssid_len;
536 
537 		if (!tgt_req->ssid.length ||
538 		    tgt_req->ssid.length > WLAN_SSID_MAX_LEN) {
539 			osif_err(" SSID Len %d is not correct for network %d",
540 				 tgt_req->ssid.length, i);
541 			ret = -EINVAL;
542 			goto error;
543 		}
544 
545 		qdf_mem_copy(tgt_req->ssid.ssid, user_req->ssid.ssid,
546 			     tgt_req->ssid.length);
547 		tgt_req->authentication = 0;   /*eAUTH_TYPE_ANY */
548 		tgt_req->encryption = 0;       /*eED_ANY */
549 		tgt_req->bc_new_type = 0;    /*eBCAST_UNKNOWN */
550 
551 
552 		/*Copying list of valid channel into request */
553 		for (j = 0; j < num_chan; j++)
554 			tgt_req->pno_chan_list.chan[j].freq = valid_ch[j];
555 		tgt_req->pno_chan_list.num_chan = num_chan;
556 
557 		if (ucfg_is_6ghz_pno_scan_optimization_supported(psoc)) {
558 			uint32_t short_ssid =
559 				wlan_construct_shortssid(tgt_req->ssid.ssid,
560 							 tgt_req->ssid.length);
561 
562 			ucfg_scan_add_flags_to_pno_chan_list(vdev, req,
563 							     &num_chan,
564 							     short_ssid, i);
565 		}
566 
567 		tgt_req->rssi_thresh = user_req->rssi_thold;
568 	}
569 
570 	/* set scan to passive if no SSIDs are specified in the request */
571 	if (0 == request->n_ssids)
572 		req->do_passive_scan = true;
573 	else
574 		req->do_passive_scan = false;
575 
576 	for (i = 0; i < request->n_ssids; i++) {
577 		j = 0;
578 		while (j < req->networks_cnt) {
579 			if ((req->networks_list[j].ssid.length ==
580 			     request->ssids[i].ssid_len) &&
581 			    (!qdf_mem_cmp(req->networks_list[j].ssid.ssid,
582 					 request->ssids[i].ssid,
583 					 req->networks_list[j].ssid.length))) {
584 				req->networks_list[j].bc_new_type =
585 					SSID_BC_TYPE_HIDDEN;
586 				break;
587 			}
588 			j++;
589 		}
590 	}
591 
592 	/*
593 	 * Before Kernel 4.4
594 	 *   Driver gets only one time interval which is hard coded in
595 	 *   supplicant for 10000ms.
596 	 *
597 	 * After Kernel 4.4
598 	 *   User can configure multiple scan_plans, each scan would have
599 	 *   separate scan cycle and interval. (interval is in unit of second.)
600 	 *   For our use case, we would only have supplicant set one scan_plan,
601 	 *   and firmware also support only one as well, so pick up the first
602 	 *   index.
603 	 *
604 	 *   Taking power consumption into account
605 	 *   firmware after gPNOScanTimerRepeatValue times fast_scan_period
606 	 *   switches slow_scan_period. This is less frequent scans and firmware
607 	 *   shall be in slow_scan_period mode until next PNO Start.
608 	 */
609 	wlan_config_sched_scan_plan(psoc, req, request);
610 	req->delay_start_time = wlan_config_sched_scan_start_delay(request);
611 	req->scan_backoff_multiplier = scan_backoff_multiplier;
612 
613 	wlan_hdd_sched_scan_update_relative_rssi(req, request);
614 
615 	psoc = wlan_pdev_get_psoc(pdev);
616 	ucfg_scan_register_pno_cb(psoc,
617 		wlan_cfg80211_pno_callback, NULL);
618 	ucfg_scan_get_pno_def_params(vdev, req);
619 
620 	if (req->scan_random.randomize)
621 		wlan_pno_scan_rand_attr(vdev, request, req);
622 
623 	if (ucfg_ie_allowlist_enabled(psoc, vdev))
624 		ucfg_copy_ie_allowlist_attrs(psoc, &req->ie_allowlist);
625 
626 	osif_debug("Network count %d n_ssids %d fast_scan_period: %d msec slow_scan_period: %d msec, fast_scan_max_cycles: %d, relative_rssi %d band_pref %d, rssi_pref %d",
627 		   req->networks_cnt, request->n_ssids, req->fast_scan_period,
628 		   req->slow_scan_period, req->fast_scan_max_cycles,
629 		   req->relative_rssi, req->band_rssi_pref.band,
630 		   req->band_rssi_pref.rssi);
631 
632 	for (i = 0; i < req->networks_cnt; i++)
633 		osif_debug("[%d] ssid: %.*s, RSSI th %d bc NW type %u",
634 			   i, req->networks_list[i].ssid.length,
635 			   req->networks_list[i].ssid.ssid,
636 			   req->networks_list[i].rssi_thresh,
637 			   req->networks_list[i].bc_new_type);
638 
639 	status = ucfg_scan_pno_start(vdev, req);
640 	if (QDF_IS_STATUS_ERROR(status)) {
641 		osif_err("Failed to enable PNO");
642 		ret = -EINVAL;
643 		goto error;
644 	}
645 
646 error:
647 	qdf_mem_free(req);
648 	return ret;
649 }
650 
651 int wlan_cfg80211_sched_scan_stop(struct wlan_objmgr_vdev *vdev)
652 {
653 	QDF_STATUS status;
654 
655 	status = ucfg_scan_pno_stop(vdev);
656 	if (QDF_IS_STATUS_ERROR(status))
657 		osif_debug("Failed to disable PNO");
658 
659 	return 0;
660 }
661 #endif /*FEATURE_WLAN_SCAN_PNO */
662 
663 /**
664  * wlan_copy_bssid_scan_request() - API to copy the bssid to Scan request
665  * @scan_req: Pointer to scan_start_request
666  * @request: scan request from Supplicant
667  *
668  * This API copies the BSSID in scan request from Supplicant and copies it to
669  * the scan_start_request
670  *
671  * Return: None
672  */
673 #if defined(CFG80211_SCAN_BSSID) || \
674 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
675 static inline void
676 wlan_copy_bssid_scan_request(struct scan_start_request *scan_req,
677 		struct cfg80211_scan_request *request)
678 {
679 	qdf_mem_copy(scan_req->scan_req.bssid_list[0].bytes,
680 				request->bssid, QDF_MAC_ADDR_SIZE);
681 }
682 #else
683 static inline void
684 wlan_copy_bssid_scan_request(struct scan_start_request *scan_req,
685 		struct cfg80211_scan_request *request)
686 {
687 
688 }
689 #endif
690 
691 /**
692  * wlan_schedule_scan_start_request() - Schedule scan start request
693  * @pdev: pointer to pdev object
694  * @req: Pointer to the scan request
695  * @source: source of the scan request
696  * @scan_start_req: pointer to scan start request
697  *
698  * Schedule scan start request and enqueue scan request in the global scan
699  * list. This list stores the active scan request information.
700  *
701  * Return: QDF_STATUS
702  */
703 static QDF_STATUS
704 wlan_schedule_scan_start_request(struct wlan_objmgr_pdev *pdev,
705 				 struct cfg80211_scan_request *req,
706 				 uint8_t source,
707 				 struct scan_start_request *scan_start_req)
708 {
709 	struct scan_req *scan_req;
710 	QDF_STATUS status;
711 	struct pdev_osif_priv *osif_ctx;
712 	struct osif_scan_pdev *osif_scan;
713 
714 	scan_req = qdf_mem_malloc(sizeof(*scan_req));
715 	if (!scan_req) {
716 		ucfg_scm_scan_free_scan_request_mem(scan_start_req);
717 		return QDF_STATUS_E_NOMEM;
718 	}
719 
720 	/* Get NL global context from objmgr*/
721 	osif_ctx = wlan_pdev_get_ospriv(pdev);
722 	osif_scan = osif_ctx->osif_scan;
723 	scan_req->scan_request = req;
724 	scan_req->source = source;
725 	scan_req->scan_id = scan_start_req->scan_req.scan_id;
726 	scan_req->dev = req->wdev->netdev;
727 	scan_req->scan_start_timestamp = qdf_get_time_of_the_day_ms();
728 
729 	qdf_mutex_acquire(&osif_scan->scan_req_q_lock);
730 	if (qdf_list_size(&osif_scan->scan_req_q) < WLAN_MAX_SCAN_COUNT) {
731 		status = ucfg_scan_start(scan_start_req);
732 		if (QDF_IS_STATUS_SUCCESS(status)) {
733 			qdf_list_insert_back(&osif_scan->scan_req_q,
734 					     &scan_req->node);
735 		} else {
736 			osif_err("scan req failed with error %d", status);
737 			if (status == QDF_STATUS_E_RESOURCES)
738 				osif_err("HO is in progress.So defer the scan by informing busy");
739 		}
740 	} else {
741 		ucfg_scm_scan_free_scan_request_mem(scan_start_req);
742 		status = QDF_STATUS_E_RESOURCES;
743 	}
744 
745 	qdf_mutex_release(&osif_scan->scan_req_q_lock);
746 	if (QDF_IS_STATUS_ERROR(status)) {
747 		osif_rl_debug("Failed to enqueue Scan Req as max scan %d already queued",
748 			      qdf_list_size(&osif_scan->scan_req_q));
749 		qdf_mem_free(scan_req);
750 	}
751 
752 	return status;
753 }
754 
755 /**
756  * wlan_scan_request_dequeue() - dequeue scan request
757  * @nl_ctx: Global HDD context
758  * @scan_id: scan id
759  * @req: scan request
760  * @dev: net device
761  * @source : returns source of the scan request
762  *
763  * Return: QDF_STATUS
764  */
765 static QDF_STATUS wlan_scan_request_dequeue(
766 	struct wlan_objmgr_pdev *pdev,
767 	uint32_t scan_id, struct cfg80211_scan_request **req,
768 	uint8_t *source, struct net_device **dev,
769 	qdf_time_t *scan_start_timestamp)
770 {
771 	QDF_STATUS status = QDF_STATUS_E_FAILURE;
772 	struct scan_req *scan_req;
773 	qdf_list_node_t *node = NULL, *next_node = NULL;
774 	struct pdev_osif_priv *osif_ctx;
775 	struct osif_scan_pdev *scan_priv;
776 
777 	if ((!source) || (!req)) {
778 		osif_err("source or request is NULL");
779 		return QDF_STATUS_E_NULL_VALUE;
780 	}
781 
782 	/* Get NL global context from objmgr*/
783 	osif_ctx = wlan_pdev_get_ospriv(pdev);
784 	if (!osif_ctx) {
785 		osif_err("Failed to retrieve osif context");
786 		return status;
787 	}
788 	scan_priv = osif_ctx->osif_scan;
789 
790 	qdf_mutex_acquire(&scan_priv->scan_req_q_lock);
791 	if (qdf_list_empty(&scan_priv->scan_req_q)) {
792 		osif_info("Scan List is empty");
793 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
794 		return QDF_STATUS_E_FAILURE;
795 	}
796 
797 	if (QDF_STATUS_SUCCESS !=
798 		qdf_list_peek_front(&scan_priv->scan_req_q, &next_node)) {
799 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
800 		osif_err("Failed to remove Scan Req from queue");
801 		return QDF_STATUS_E_FAILURE;
802 	}
803 
804 	do {
805 		node = next_node;
806 		scan_req = qdf_container_of(node, struct scan_req, node);
807 		if (scan_req->scan_id == scan_id) {
808 			status = qdf_list_remove_node(&scan_priv->scan_req_q,
809 						      node);
810 			if (status == QDF_STATUS_SUCCESS) {
811 				*req = scan_req->scan_request;
812 				*source = scan_req->source;
813 				*dev = scan_req->dev;
814 				*scan_start_timestamp =
815 					scan_req->scan_start_timestamp;
816 				qdf_mem_free(scan_req);
817 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
818 				osif_debug("removed Scan id: %d, req = %pK, pending scans %d",
819 					   scan_id, req,
820 					   qdf_list_size(&scan_priv->scan_req_q));
821 				return QDF_STATUS_SUCCESS;
822 			} else {
823 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
824 				osif_err("Failed to remove scan id %d, pending scans %d",
825 					 scan_id,
826 					 qdf_list_size(&scan_priv->scan_req_q));
827 				return status;
828 			}
829 		}
830 	} while (QDF_STATUS_SUCCESS ==
831 		qdf_list_peek_next(&scan_priv->scan_req_q, node, &next_node));
832 	qdf_mutex_release(&scan_priv->scan_req_q_lock);
833 	osif_debug("Failed to find scan id %d", scan_id);
834 
835 	return status;
836 }
837 
838 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
839 /**
840  * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211
841  * @netdev: Net device
842  * @req : Scan request
843  * @aborted : true scan aborted false scan success
844  * @osif_priv: OS private structure
845  *
846  * This function notifies scan done to cfg80211
847  *
848  * Return: none
849  */
850 void wlan_cfg80211_scan_done(struct net_device *netdev,
851 			     struct cfg80211_scan_request *req,
852 			     bool aborted, struct pdev_osif_priv *osif_priv)
853 {
854 	struct cfg80211_scan_info info = {
855 		.aborted = aborted
856 	};
857 	bool driver_internal_netdev_state;
858 
859 	driver_internal_netdev_state = netdev->flags & IFF_UP;
860 	if (osif_priv->osif_check_netdev_state)
861 		driver_internal_netdev_state =
862 			osif_priv->osif_check_netdev_state(netdev);
863 
864 	if (driver_internal_netdev_state)
865 		cfg80211_scan_done(req, &info);
866 	else
867 		osif_debug("scan done callback has been dropped :%s",
868 			   (netdev)->name);
869 }
870 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0))
871 /**
872  * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211
873  * @netdev: Net device
874  * @req : Scan request
875  * @aborted : true scan aborted false scan success
876  * @osif_priv - OS private structure
877  *
878  * This function notifies scan done to cfg80211
879  *
880  * Return: none
881  */
882 void wlan_cfg80211_scan_done(struct net_device *netdev,
883 			     struct cfg80211_scan_request *req,
884 			     bool aborted, struct pdev_osif_priv *osif_priv)
885 {
886 	bool driver_internal_net_state;
887 
888 	driver_internal_netdev_state = netdev->flags & IFF_UP;
889 	if (osif_priv->osif_check_netdev_state)
890 		driver_internal_net_state =
891 			osif_priv->osif_check_netdev_state(netdev);
892 
893 	if (driver_internal_netdev_state)
894 		cfg80211_scan_done(req, aborted);
895 	else
896 		osif_debug("scan request has been dropped :%s", (netdev)->name);
897 }
898 #endif
899 
900 /**
901  * wlan_vendor_scan_callback() - Scan completed callback event
902  *
903  * @req : Scan request
904  * @aborted : true scan aborted false scan success
905  *
906  * This function sends scan completed callback event to NL.
907  *
908  * Return: none
909  */
910 static void wlan_vendor_scan_callback(struct cfg80211_scan_request *req,
911 					bool aborted)
912 {
913 	struct sk_buff *skb;
914 	struct nlattr *attr;
915 	int i;
916 	uint8_t scan_status;
917 	uint64_t cookie;
918 	int index = QCA_NL80211_VENDOR_SUBCMD_SCAN_DONE_INDEX;
919 
920 	skb = wlan_cfg80211_vendor_event_alloc(req->wdev->wiphy, req->wdev,
921 					       SCAN_DONE_EVENT_BUF_SIZE + 4 +
922 					       NLMSG_HDRLEN,
923 					       index,
924 					       GFP_ATOMIC);
925 
926 	if (!skb) {
927 		osif_err("skb alloc failed");
928 		qdf_mem_free(req);
929 		return;
930 	}
931 
932 	cookie = (uintptr_t)req;
933 
934 	attr = nla_nest_start(skb, QCA_WLAN_VENDOR_ATTR_SCAN_SSIDS);
935 	if (!attr)
936 		goto nla_put_failure;
937 	for (i = 0; i < req->n_ssids; i++) {
938 		if (nla_put(skb, i, req->ssids[i].ssid_len, req->ssids[i].ssid))
939 			goto nla_put_failure;
940 	}
941 	nla_nest_end(skb, attr);
942 
943 	attr = nla_nest_start(skb, QCA_WLAN_VENDOR_ATTR_SCAN_FREQUENCIES);
944 	if (!attr)
945 		goto nla_put_failure;
946 	for (i = 0; i < req->n_channels; i++) {
947 		if (nla_put_u32(skb, i, req->channels[i]->center_freq))
948 			goto nla_put_failure;
949 	}
950 	nla_nest_end(skb, attr);
951 
952 	if (req->ie &&
953 		nla_put(skb, QCA_WLAN_VENDOR_ATTR_SCAN_IE, req->ie_len,
954 			req->ie))
955 		goto nla_put_failure;
956 
957 	if (req->flags &&
958 		nla_put_u32(skb, QCA_WLAN_VENDOR_ATTR_SCAN_FLAGS, req->flags))
959 		goto nla_put_failure;
960 
961 	if (wlan_cfg80211_nla_put_u64(skb, QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE,
962 					cookie))
963 		goto nla_put_failure;
964 
965 	scan_status = (aborted == true) ? VENDOR_SCAN_STATUS_ABORTED :
966 		VENDOR_SCAN_STATUS_NEW_RESULTS;
967 	if (nla_put_u8(skb, QCA_WLAN_VENDOR_ATTR_SCAN_STATUS, scan_status))
968 		goto nla_put_failure;
969 
970 	wlan_cfg80211_vendor_event(skb, GFP_ATOMIC);
971 	qdf_mem_free(req);
972 
973 	return;
974 
975 nla_put_failure:
976 	wlan_cfg80211_vendor_free_skb(skb);
977 	qdf_mem_free(req);
978 }
979 
980 /**
981  * wlan_scan_acquire_wake_lock_timeout() - acquire scan wake lock
982  * @psoc: psoc ptr
983  * @scan_wake_lock: Scan wake lock
984  * @timeout: timeout in ms
985  *
986  * Return: void
987  */
988 static inline
989 void wlan_scan_acquire_wake_lock_timeout(struct wlan_objmgr_psoc *psoc,
990 					 qdf_wake_lock_t *scan_wake_lock,
991 					 uint32_t timeout)
992 {
993 	if (!psoc || !scan_wake_lock)
994 		return;
995 
996 	if (ucfg_scan_wake_lock_in_user_scan(psoc))
997 		qdf_wake_lock_timeout_acquire(scan_wake_lock, timeout);
998 }
999 
1000 
1001 /**
1002  * wlan_scan_release_wake_lock() - release scan wake lock
1003  * @psoc: psoc ptr
1004  * @scan_wake_lock: Scan wake lock
1005  *
1006  * Return: void
1007  */
1008 #ifdef FEATURE_WLAN_DIAG_SUPPORT
1009 static inline
1010 void wlan_scan_release_wake_lock(struct wlan_objmgr_psoc *psoc,
1011 				 qdf_wake_lock_t *scan_wake_lock)
1012 {
1013 	if (!psoc || !scan_wake_lock)
1014 		return;
1015 
1016 	if (ucfg_scan_wake_lock_in_user_scan(psoc))
1017 		qdf_wake_lock_release(scan_wake_lock,
1018 				      WIFI_POWER_EVENT_WAKELOCK_SCAN);
1019 }
1020 #else
1021 static inline
1022 void wlan_scan_release_wake_lock(struct wlan_objmgr_psoc *psoc,
1023 				 qdf_wake_lock_t *scan_wake_lock)
1024 {
1025 	if (!psoc || !scan_wake_lock)
1026 		return;
1027 
1028 	if (ucfg_scan_wake_lock_in_user_scan(psoc))
1029 		qdf_wake_lock_release(scan_wake_lock, 0);
1030 }
1031 #endif
1032 
1033 static
1034 uint32_t wlan_scan_get_bss_count_for_scan(struct wlan_objmgr_pdev *pdev,
1035 					  qdf_time_t scan_start_ts)
1036 {
1037 	struct scan_filter *filter;
1038 	qdf_list_t *list = NULL;
1039 	uint32_t count = 0;
1040 
1041 	if (!scan_start_ts)
1042 		return count;
1043 
1044 	filter = qdf_mem_malloc(sizeof(*filter));
1045 	if (!filter)
1046 		return count;
1047 
1048 	filter->ignore_auth_enc_type = true;
1049 	filter->age_threshold = qdf_get_time_of_the_day_ms() - scan_start_ts;
1050 
1051 	list = ucfg_scan_get_result(pdev, filter);
1052 
1053 	qdf_mem_free(filter);
1054 
1055 	if (list) {
1056 		count = qdf_list_size(list);
1057 		ucfg_scan_purge_results(list);
1058 	}
1059 
1060 	return count;
1061 }
1062 
1063 /**
1064  * wlan_cfg80211_scan_done_callback() - scan done callback function called after
1065  * scan is finished
1066  * @vdev: vdev ptr
1067  * @event: Scan event
1068  * @args: Scan cb arg
1069  *
1070  * Return: void
1071  */
1072 static void wlan_cfg80211_scan_done_callback(
1073 					struct wlan_objmgr_vdev *vdev,
1074 					struct scan_event *event,
1075 					void *args)
1076 {
1077 	struct cfg80211_scan_request *req = NULL;
1078 	bool success = false;
1079 	uint32_t scan_id;
1080 	uint8_t source = NL_SCAN;
1081 	struct wlan_objmgr_pdev *pdev;
1082 	struct pdev_osif_priv *osif_priv;
1083 	struct net_device *netdev = NULL;
1084 	QDF_STATUS status;
1085 	qdf_time_t scan_start_timestamp = 0;
1086 	uint32_t unique_bss_count = 0;
1087 
1088 	if (!event) {
1089 		osif_nofl_err("Invalid scan event received");
1090 		return;
1091 	}
1092 
1093 	scan_id = event->scan_id;
1094 
1095 	qdf_mtrace(QDF_MODULE_ID_SCAN, QDF_MODULE_ID_OS_IF, event->type,
1096 		   event->vdev_id, scan_id);
1097 
1098 	if (event->type == SCAN_EVENT_TYPE_STARTED)
1099 		osif_nofl_info("scan start scan id %d", scan_id);
1100 
1101 	if (!util_is_scan_completed(event, &success))
1102 		return;
1103 
1104 	pdev = wlan_vdev_get_pdev(vdev);
1105 	osif_priv = wlan_pdev_get_ospriv(pdev);
1106 	status = wlan_scan_request_dequeue(
1107 			pdev, scan_id, &req, &source, &netdev,
1108 			&scan_start_timestamp);
1109 	if (QDF_IS_STATUS_ERROR(status)) {
1110 		osif_err("Dequeue of scan request failed ID: %d", scan_id);
1111 		goto allow_suspend;
1112 	}
1113 
1114 	if (!netdev) {
1115 		osif_err("net dev is NULL,Drop scan event Id: %d", scan_id);
1116 		/*
1117 		 * Free scan request in case of VENDOR_SCAN as it is
1118 		 * allocated in driver.
1119 		 */
1120 		if (source == VENDOR_SCAN)
1121 			qdf_mem_free(req);
1122 		goto allow_suspend;
1123 	}
1124 
1125 	/* Make sure vdev is active */
1126 	status = wlan_objmgr_vdev_try_get_ref(vdev, WLAN_OSIF_ID);
1127 	if (QDF_IS_STATUS_ERROR(status)) {
1128 		osif_err("Failed to get vdev reference: scan Id: %d", scan_id);
1129 		/*
1130 		 * Free scan request in case of VENDOR_SCAN as it is
1131 		 * allocated in driver.
1132 		 */
1133 		if (source == VENDOR_SCAN)
1134 			qdf_mem_free(req);
1135 		goto allow_suspend;
1136 	}
1137 
1138 	/*
1139 	 * Scan can be triggred from NL or vendor scan
1140 	 * - If scan is triggered from NL then cfg80211 scan done should be
1141 	 * called to updated scan completion to NL.
1142 	 * - If scan is triggred through vendor command then
1143 	 * scan done event will be posted
1144 	 */
1145 	if (NL_SCAN == source)
1146 		wlan_cfg80211_scan_done(netdev, req, !success, osif_priv);
1147 	else
1148 		wlan_vendor_scan_callback(req, !success);
1149 
1150 	wlan_objmgr_vdev_release_ref(vdev, WLAN_OSIF_ID);
1151 
1152 	unique_bss_count = wlan_scan_get_bss_count_for_scan(pdev,
1153 							  scan_start_timestamp);
1154 	osif_nofl_info("vdev %d, scan id %d type %s(%d) reason %s(%d) scan found %d bss",
1155 		       event->vdev_id, scan_id,
1156 		       util_scan_get_ev_type_name(event->type), event->type,
1157 		       util_scan_get_ev_reason_name(event->reason),
1158 		       event->reason, unique_bss_count);
1159 allow_suspend:
1160 	qdf_mutex_acquire(&osif_priv->osif_scan->scan_req_q_lock);
1161 	if (qdf_list_empty(&osif_priv->osif_scan->scan_req_q)) {
1162 		struct wlan_objmgr_psoc *psoc;
1163 
1164 		qdf_mutex_release(&osif_priv->osif_scan->scan_req_q_lock);
1165 		qdf_runtime_pm_allow_suspend(
1166 			&osif_priv->osif_scan->runtime_pm_lock);
1167 
1168 		psoc = wlan_pdev_get_psoc(pdev);
1169 		wlan_scan_release_wake_lock(psoc,
1170 					&osif_priv->osif_scan->scan_wake_lock);
1171 		/*
1172 		 * Acquire wakelock to handle the case where APP's tries
1173 		 * to suspend immediately after the driver gets connect
1174 		 * request(i.e after scan) from supplicant, this result in
1175 		 * app's is suspending and not able to process the connect
1176 		 * request to AP
1177 		 */
1178 		wlan_scan_acquire_wake_lock_timeout(psoc,
1179 					&osif_priv->osif_scan->scan_wake_lock,
1180 					SCAN_WAKE_LOCK_CONNECT_DURATION);
1181 	} else {
1182 		qdf_mutex_release(&osif_priv->osif_scan->scan_req_q_lock);
1183 	}
1184 
1185 }
1186 
1187 QDF_STATUS wlan_scan_runtime_pm_init(struct wlan_objmgr_pdev *pdev)
1188 {
1189 	struct pdev_osif_priv *osif_priv;
1190 	struct osif_scan_pdev *scan_priv;
1191 
1192 	wlan_pdev_obj_lock(pdev);
1193 	osif_priv = wlan_pdev_get_ospriv(pdev);
1194 	wlan_pdev_obj_unlock(pdev);
1195 
1196 	scan_priv = osif_priv->osif_scan;
1197 
1198 	return qdf_runtime_lock_init(&scan_priv->runtime_pm_lock);
1199 }
1200 
1201 void wlan_scan_runtime_pm_deinit(struct wlan_objmgr_pdev *pdev)
1202 {
1203 	struct pdev_osif_priv *osif_priv;
1204 	struct osif_scan_pdev *scan_priv;
1205 
1206 	wlan_pdev_obj_lock(pdev);
1207 	osif_priv = wlan_pdev_get_ospriv(pdev);
1208 	wlan_pdev_obj_unlock(pdev);
1209 
1210 	scan_priv = osif_priv->osif_scan;
1211 	qdf_runtime_lock_deinit(&scan_priv->runtime_pm_lock);
1212 }
1213 
1214 QDF_STATUS wlan_cfg80211_scan_priv_init(struct wlan_objmgr_pdev *pdev)
1215 {
1216 	struct pdev_osif_priv *osif_priv;
1217 	struct osif_scan_pdev *scan_priv;
1218 	struct wlan_objmgr_psoc *psoc;
1219 	wlan_scan_requester req_id;
1220 
1221 	psoc = wlan_pdev_get_psoc(pdev);
1222 
1223 	req_id = ucfg_scan_register_requester(psoc, "CFG",
1224 		wlan_cfg80211_scan_done_callback, NULL);
1225 
1226 	osif_priv = wlan_pdev_get_ospriv(pdev);
1227 	scan_priv = qdf_mem_malloc(sizeof(*scan_priv));
1228 	if (!scan_priv)
1229 		return QDF_STATUS_E_NOMEM;
1230 
1231 	/* Initialize the scan request queue */
1232 	osif_priv->osif_scan = scan_priv;
1233 	scan_priv->req_id = req_id;
1234 	qdf_list_create(&scan_priv->scan_req_q, WLAN_MAX_SCAN_COUNT);
1235 	qdf_mutex_create(&scan_priv->scan_req_q_lock);
1236 	qdf_wake_lock_create(&scan_priv->scan_wake_lock, "scan_wake_lock");
1237 
1238 	return QDF_STATUS_SUCCESS;
1239 }
1240 
1241 QDF_STATUS wlan_cfg80211_scan_priv_deinit(struct wlan_objmgr_pdev *pdev)
1242 {
1243 	struct pdev_osif_priv *osif_priv;
1244 	struct osif_scan_pdev *scan_priv;
1245 	struct wlan_objmgr_psoc *psoc;
1246 
1247 	psoc = wlan_pdev_get_psoc(pdev);
1248 	osif_priv = wlan_pdev_get_ospriv(pdev);
1249 
1250 	wlan_cfg80211_cleanup_scan_queue(pdev, NULL);
1251 	scan_priv = osif_priv->osif_scan;
1252 	qdf_wake_lock_destroy(&scan_priv->scan_wake_lock);
1253 	qdf_mutex_destroy(&scan_priv->scan_req_q_lock);
1254 	qdf_list_destroy(&scan_priv->scan_req_q);
1255 	ucfg_scan_unregister_requester(psoc, scan_priv->req_id);
1256 	osif_priv->osif_scan = NULL;
1257 	qdf_mem_free(scan_priv);
1258 
1259 	return QDF_STATUS_SUCCESS;
1260 }
1261 
1262 /**
1263  * wlan_cfg80211_enqueue_for_cleanup() - Function to populate scan cleanup queue
1264  * @scan_cleanup_q: Scan cleanup queue to be populated
1265  * @scan_priv: Pointer to scan related data used by cfg80211 scan
1266  * @dev: Netdevice pointer
1267  *
1268  * The function synchrounously iterates through the global scan queue to
1269  * identify entries that have to be cleaned up, copies identified entries
1270  * to another queue(to send scan complete event to NL later) and removes the
1271  * entry from the global scan queue.
1272  *
1273  * Return: None
1274  */
1275 static void
1276 wlan_cfg80211_enqueue_for_cleanup(qdf_list_t *scan_cleanup_q,
1277 				  struct osif_scan_pdev *scan_priv,
1278 				  struct net_device *dev)
1279 {
1280 	struct scan_req *scan_req, *scan_cleanup;
1281 	qdf_list_node_t *node = NULL, *next_node = NULL;
1282 
1283 	qdf_mutex_acquire(&scan_priv->scan_req_q_lock);
1284 	if (QDF_STATUS_SUCCESS !=
1285 		qdf_list_peek_front(&scan_priv->scan_req_q,
1286 				    &node)) {
1287 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
1288 		return;
1289 	}
1290 
1291 	while (node) {
1292 		/*
1293 		 * Keep track of the next node, to traverse through the list
1294 		 * in the event of the current node being deleted.
1295 		 */
1296 		qdf_list_peek_next(&scan_priv->scan_req_q,
1297 				   node, &next_node);
1298 		scan_req = qdf_container_of(node, struct scan_req, node);
1299 		if (!dev || (dev == scan_req->dev)) {
1300 			scan_cleanup = qdf_mem_malloc(sizeof(struct scan_req));
1301 			if (!scan_cleanup) {
1302 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
1303 				return;
1304 			}
1305 			scan_cleanup->scan_request = scan_req->scan_request;
1306 			scan_cleanup->scan_id = scan_req->scan_id;
1307 			scan_cleanup->source = scan_req->source;
1308 			scan_cleanup->dev = scan_req->dev;
1309 			qdf_list_insert_back(scan_cleanup_q,
1310 					     &scan_cleanup->node);
1311 			if (QDF_STATUS_SUCCESS !=
1312 				qdf_list_remove_node(&scan_priv->scan_req_q,
1313 						     node)) {
1314 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
1315 				osif_err("Failed to remove scan request");
1316 				return;
1317 			}
1318 			qdf_mem_free(scan_req);
1319 		}
1320 		node = next_node;
1321 		next_node = NULL;
1322 	}
1323 	qdf_mutex_release(&scan_priv->scan_req_q_lock);
1324 }
1325 
1326 void wlan_cfg80211_cleanup_scan_queue(struct wlan_objmgr_pdev *pdev,
1327 				      struct net_device *dev)
1328 {
1329 	struct scan_req *scan_req;
1330 	struct cfg80211_scan_request *req;
1331 	uint8_t source;
1332 	bool aborted = true;
1333 	struct pdev_osif_priv *osif_priv;
1334 	qdf_list_t scan_cleanup_q;
1335 	qdf_list_node_t *node = NULL;
1336 
1337 	if (!pdev) {
1338 		osif_err("pdev is Null");
1339 		return;
1340 	}
1341 
1342 	osif_priv = wlan_pdev_get_ospriv(pdev);
1343 
1344 	/*
1345 	 * To avoid any race conditions, create a local list to copy all the
1346 	 * scan entries to be removed and then send scan complete for each of
1347 	 * the identified entries to NL.
1348 	 */
1349 	qdf_list_create(&scan_cleanup_q, WLAN_MAX_SCAN_COUNT);
1350 	wlan_cfg80211_enqueue_for_cleanup(&scan_cleanup_q,
1351 					  osif_priv->osif_scan, dev);
1352 
1353 	while (!qdf_list_empty(&scan_cleanup_q)) {
1354 		if (QDF_STATUS_SUCCESS != qdf_list_remove_front(&scan_cleanup_q,
1355 								&node)) {
1356 			osif_err("Failed to remove scan request");
1357 			return;
1358 		}
1359 		scan_req = container_of(node, struct scan_req, node);
1360 		req = scan_req->scan_request;
1361 		source = scan_req->source;
1362 		if (NL_SCAN == source)
1363 			wlan_cfg80211_scan_done(scan_req->dev, req,
1364 						aborted, osif_priv);
1365 		else
1366 			wlan_vendor_scan_callback(req, aborted);
1367 
1368 		qdf_mem_free(scan_req);
1369 	}
1370 	qdf_list_destroy(&scan_cleanup_q);
1371 
1372 	return;
1373 }
1374 
1375 /**
1376  * wlan_cfg80211_update_scan_policy_type_flags() - Set scan flags according to
1377  * scan request
1378  * @scan_req: Pointer to csr scan req
1379  *
1380  * Return: None
1381  */
1382 #if defined(CFG80211_SCAN_DBS_CONTROL_SUPPORT) || \
1383 	   (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
1384 static void wlan_cfg80211_update_scan_policy_type_flags(
1385 	struct cfg80211_scan_request *req,
1386 	struct scan_req_params *scan_req)
1387 {
1388 	if (req->flags & NL80211_SCAN_FLAG_HIGH_ACCURACY)
1389 		scan_req->scan_policy_high_accuracy = true;
1390 	if (req->flags & NL80211_SCAN_FLAG_LOW_SPAN)
1391 		scan_req->scan_policy_low_span = true;
1392 	if (req->flags & NL80211_SCAN_FLAG_LOW_POWER)
1393 		scan_req->scan_policy_low_power = true;
1394 
1395 	if (wlan_cfg80211_is_colocated_6ghz_scan_supported(req->flags))
1396 		scan_req->scan_policy_colocated_6ghz = true;
1397 }
1398 #else
1399 static inline void wlan_cfg80211_update_scan_policy_type_flags(
1400 		struct cfg80211_scan_request *req,
1401 		struct scan_req_params *scan_req)
1402 {
1403 }
1404 #endif
1405 
1406 #ifdef WLAN_POLICY_MGR_ENABLE
1407 static bool
1408 wlan_cfg80211_allow_simultaneous_scan(struct wlan_objmgr_psoc *psoc)
1409 {
1410 	return policy_mgr_is_scan_simultaneous_capable(psoc);
1411 }
1412 #else
1413 static bool
1414 wlan_cfg80211_allow_simultaneous_scan(struct wlan_objmgr_psoc *psoc)
1415 {
1416 	return true;
1417 }
1418 #endif
1419 
1420 enum scan_priority convert_nl_scan_priority_to_internal(
1421 	enum qca_wlan_vendor_scan_priority nl_scan_priority)
1422 {
1423 	switch (nl_scan_priority) {
1424 	case QCA_WLAN_VENDOR_SCAN_PRIORITY_VERY_LOW:
1425 		return SCAN_PRIORITY_VERY_LOW;
1426 
1427 	case QCA_WLAN_VENDOR_SCAN_PRIORITY_LOW:
1428 		return SCAN_PRIORITY_LOW;
1429 
1430 	case QCA_WLAN_VENDOR_SCAN_PRIORITY_MEDIUM:
1431 		return SCAN_PRIORITY_MEDIUM;
1432 
1433 	case QCA_WLAN_VENDOR_SCAN_PRIORITY_HIGH:
1434 		return SCAN_PRIORITY_HIGH;
1435 
1436 	case QCA_WLAN_VENDOR_SCAN_PRIORITY_VERY_HIGH:
1437 		return SCAN_PRIORITY_VERY_HIGH;
1438 
1439 	default:
1440 		return SCAN_PRIORITY_COUNT;
1441 	}
1442 }
1443 
1444 int wlan_cfg80211_scan(struct wlan_objmgr_vdev *vdev,
1445 		       struct cfg80211_scan_request *request,
1446 		       struct scan_params *params)
1447 {
1448 	struct scan_start_request *req;
1449 	struct wlan_ssid *pssid;
1450 	uint8_t i;
1451 	int ret = 0;
1452 	uint8_t num_chan = 0;
1453 	uint32_t c_freq;
1454 	struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev);
1455 	wlan_scan_requester req_id;
1456 	struct pdev_osif_priv *osif_priv;
1457 	struct wlan_objmgr_psoc *psoc;
1458 	wlan_scan_id scan_id;
1459 	bool is_p2p_scan = false;
1460 	enum wlan_band band;
1461 	QDF_STATUS qdf_status;
1462 	enum QDF_OPMODE opmode;
1463 	uint32_t extra_ie_len = 0;
1464 
1465 	psoc = wlan_pdev_get_psoc(pdev);
1466 	if (!psoc) {
1467 		osif_err("Invalid psoc object");
1468 		return -EINVAL;
1469 	}
1470 	opmode = wlan_vdev_mlme_get_opmode(vdev);
1471 
1472 	osif_debug("%s(vdev%d): mode %d", request->wdev->netdev->name,
1473 		   wlan_vdev_get_id(vdev), opmode);
1474 
1475 	/* Get NL global context from objmgr*/
1476 	osif_priv = wlan_pdev_get_ospriv(pdev);
1477 	if (!osif_priv) {
1478 		osif_err("Invalid osif priv object");
1479 		return -EINVAL;
1480 	}
1481 
1482 	/*
1483 	 * For a non-SAP vdevs, if a scan is already going on i.e the scan queue
1484 	 * is not empty, and the simultaneous scan is disabled, dont allow 2nd
1485 	 * scan.
1486 	 */
1487 	qdf_mutex_acquire(&osif_priv->osif_scan->scan_req_q_lock);
1488 	if (!wlan_cfg80211_allow_simultaneous_scan(psoc) &&
1489 	    !qdf_list_empty(&osif_priv->osif_scan->scan_req_q) &&
1490 	    opmode != QDF_SAP_MODE) {
1491 		qdf_mutex_release(&osif_priv->osif_scan->scan_req_q_lock);
1492 		osif_err("Simultaneous scan disabled, reject scan");
1493 		return -EBUSY;
1494 	}
1495 	qdf_mutex_release(&osif_priv->osif_scan->scan_req_q_lock);
1496 
1497 	req = qdf_mem_malloc(sizeof(*req));
1498 	if (!req)
1499 		return -EINVAL;
1500 
1501 	/* Initialize the scan global params */
1502 	ucfg_scan_init_default_params(vdev, req);
1503 
1504 	req_id = osif_priv->osif_scan->req_id;
1505 	scan_id = ucfg_scan_get_scan_id(psoc);
1506 	if (!scan_id) {
1507 		osif_err("Invalid scan id");
1508 		qdf_mem_free(req);
1509 		return -EINVAL;
1510 	}
1511 
1512 	/* fill the scan request structure */
1513 	req->vdev = vdev;
1514 	req->scan_req.vdev_id = wlan_vdev_get_id(vdev);
1515 	req->scan_req.scan_id = scan_id;
1516 	req->scan_req.scan_req_id = req_id;
1517 
1518 	/* Update scan policy type flags according to cfg scan request */
1519 	wlan_cfg80211_update_scan_policy_type_flags(request,
1520 					     &req->scan_req);
1521 	/*
1522 	 * Even though supplicant doesn't provide any SSIDs, n_ssids is
1523 	 * set to 1.  Because of this, driver is assuming that this is not
1524 	 * wildcard scan and so is not aging out the scan results.
1525 	 */
1526 	if ((request->ssids) && (request->n_ssids == 1) &&
1527 	    ('\0' == request->ssids->ssid[0])) {
1528 		request->n_ssids = 0;
1529 	}
1530 
1531 	if ((request->ssids) && (0 < request->n_ssids)) {
1532 		int j;
1533 		req->scan_req.num_ssids = request->n_ssids;
1534 
1535 		if (req->scan_req.num_ssids > WLAN_SCAN_MAX_NUM_SSID) {
1536 			osif_info("number of ssid %d greater than MAX %d",
1537 				  req->scan_req.num_ssids,
1538 				  WLAN_SCAN_MAX_NUM_SSID);
1539 			req->scan_req.num_ssids = WLAN_SCAN_MAX_NUM_SSID;
1540 		}
1541 		/* copy all the ssid's and their length */
1542 		for (j = 0; j < req->scan_req.num_ssids; j++)  {
1543 			pssid = &req->scan_req.ssid[j];
1544 			/* get the ssid length */
1545 			pssid->length = request->ssids[j].ssid_len;
1546 			if (pssid->length > WLAN_SSID_MAX_LEN)
1547 				pssid->length = WLAN_SSID_MAX_LEN;
1548 			qdf_mem_copy(pssid->ssid,
1549 				     &request->ssids[j].ssid[0],
1550 				     pssid->length);
1551 		}
1552 	}
1553 	if (request->ssids ||
1554 	   (opmode == QDF_P2P_GO_MODE) || (opmode == QDF_P2P_DEVICE_MODE))
1555 		req->scan_req.scan_f_passive = false;
1556 
1557 	if (params->half_rate)
1558 		req->scan_req.scan_f_half_rate = true;
1559 	else if (params->quarter_rate)
1560 		req->scan_req.scan_f_quarter_rate = true;
1561 
1562 	if (params->strict_pscan)
1563 		req->scan_req.scan_f_strict_passive_pch = true;
1564 
1565 	if ((request->n_ssids == 1) && request->ssids &&
1566 	   !qdf_mem_cmp(&request->ssids[0], "DIRECT-", 7))
1567 		is_p2p_scan = true;
1568 
1569 	if (is_p2p_scan && request->no_cck)
1570 		req->scan_req.scan_type = SCAN_TYPE_P2P_SEARCH;
1571 
1572 	if (params->dwell_time_active)
1573 		req->scan_req.dwell_time_active = params->dwell_time_active;
1574 
1575 	if (params->dwell_time_active_2g)
1576 		req->scan_req.dwell_time_active_2g =
1577 			params->dwell_time_active_2g;
1578 
1579 	if (params->dwell_time_passive)
1580 		req->scan_req.dwell_time_passive = params->dwell_time_passive;
1581 
1582 	if (params->dwell_time_active_6g)
1583 		req->scan_req.dwell_time_active_6g =
1584 			params->dwell_time_active_6g;
1585 
1586 	if (params->dwell_time_passive_6g)
1587 		req->scan_req.dwell_time_passive_6g =
1588 			params->dwell_time_passive_6g;
1589 
1590 	/* Set dwell time mode according to scan policy type flags */
1591 	if (ucfg_scan_cfg_honour_nl_scan_policy_flags(psoc)) {
1592 		if (req->scan_req.scan_policy_high_accuracy)
1593 			req->scan_req.adaptive_dwell_time_mode =
1594 						SCAN_DWELL_MODE_STATIC;
1595 		if (req->scan_req.scan_policy_low_power)
1596 			req->scan_req.adaptive_dwell_time_mode =
1597 						SCAN_DWELL_MODE_AGGRESSIVE;
1598 	}
1599 
1600 	/*
1601 	 * FW require at least 1 MAC to send probe request.
1602 	 * If MAC is all 0 set it to BC addr as this is the address on
1603 	 * which fw will send probe req.
1604 	 */
1605 	req->scan_req.num_bssid = 1;
1606 	wlan_copy_bssid_scan_request(req, request);
1607 	if (qdf_is_macaddr_zero(&req->scan_req.bssid_list[0]))
1608 		qdf_set_macaddr_broadcast(&req->scan_req.bssid_list[0]);
1609 
1610 	if (params->scan_f_2ghz && !params->scan_f_5ghz) {
1611 		req->scan_req.scan_f_2ghz = true;
1612 		req->scan_req.scan_f_5ghz = false;
1613 	} else if (!params->scan_f_2ghz && params->scan_f_5ghz) {
1614 		req->scan_req.scan_f_2ghz = false;
1615 		req->scan_req.scan_f_5ghz = true;
1616 	}
1617 
1618 	if (request->n_channels) {
1619 #ifdef WLAN_POLICY_MGR_ENABLE
1620 		bool ap_or_go_present =
1621 			policy_mgr_mode_specific_connection_count(
1622 			     psoc, PM_SAP_MODE, NULL) ||
1623 			     policy_mgr_mode_specific_connection_count(
1624 			     psoc, PM_P2P_GO_MODE, NULL);
1625 #endif
1626 		for (i = 0; i < request->n_channels; i++) {
1627 			c_freq = request->channels[i]->center_freq;
1628 			if (wlan_reg_is_dsrc_freq(c_freq))
1629 				continue;
1630 #ifdef WLAN_POLICY_MGR_ENABLE
1631 			if (ap_or_go_present) {
1632 				bool ok;
1633 
1634 				qdf_status = policy_mgr_is_chan_ok_for_dnbs(
1635 							psoc, c_freq, &ok);
1636 
1637 				if (QDF_IS_STATUS_ERROR(qdf_status)) {
1638 					osif_err("DNBS check failed");
1639 					ret = -EINVAL;
1640 					goto err;
1641 				}
1642 				if (!ok)
1643 					continue;
1644 			}
1645 #endif
1646 
1647 			if ((req->scan_req.scan_f_2ghz &&
1648 			     WLAN_REG_IS_24GHZ_CH_FREQ(c_freq)) ||
1649 			    (req->scan_req.scan_f_5ghz &&
1650 			     (WLAN_REG_IS_5GHZ_CH_FREQ(c_freq) ||
1651 			      WLAN_REG_IS_49GHZ_FREQ(c_freq) ||
1652 			      WLAN_REG_IS_6GHZ_CHAN_FREQ(c_freq)))) {
1653 				req->scan_req.chan_list.chan[num_chan].freq =
1654 									c_freq;
1655 				band = util_scan_scm_freq_to_band(c_freq);
1656 				if (band == WLAN_BAND_2_4_GHZ)
1657 					req->scan_req.chan_list.chan[num_chan].phymode =
1658 						SCAN_PHY_MODE_11G;
1659 				else
1660 					req->scan_req.chan_list.chan[num_chan].phymode =
1661 						SCAN_PHY_MODE_11A;
1662 				num_chan++;
1663 				if (num_chan >= NUM_CHANNELS)
1664 					break;
1665 			}
1666 		}
1667 	}
1668 	if (!num_chan) {
1669 		osif_err("Received zero non-dsrc channels");
1670 		ret = -EINVAL;
1671 		goto err;
1672 	}
1673 	req->scan_req.chan_list.num_chan = num_chan;
1674 
1675 	/* P2P increase the scan priority */
1676 	if (is_p2p_scan)
1677 		req->scan_req.scan_priority = SCAN_PRIORITY_HIGH;
1678 
1679 	if (params->priority != SCAN_PRIORITY_COUNT)
1680 		req->scan_req.scan_priority = params->priority;
1681 
1682 	if (request->ie_len)
1683 		extra_ie_len = request->ie_len;
1684 	else if (params->default_ie.ptr && params->default_ie.len)
1685 		extra_ie_len = params->default_ie.len;
1686 
1687 	if (params->vendor_ie.ptr && params->vendor_ie.len)
1688 		extra_ie_len += params->vendor_ie.len;
1689 
1690 	if (extra_ie_len) {
1691 		req->scan_req.extraie.ptr = qdf_mem_malloc(extra_ie_len);
1692 		if (!req->scan_req.extraie.ptr) {
1693 			ret = -ENOMEM;
1694 			goto err;
1695 		}
1696 	}
1697 
1698 	if (request->ie_len) {
1699 		req->scan_req.extraie.len = request->ie_len;
1700 		qdf_mem_copy(req->scan_req.extraie.ptr, request->ie,
1701 			     request->ie_len);
1702 	} else if (params->default_ie.ptr && params->default_ie.len) {
1703 		req->scan_req.extraie.len = params->default_ie.len;
1704 		qdf_mem_copy(req->scan_req.extraie.ptr, params->default_ie.ptr,
1705 			     params->default_ie.len);
1706 	}
1707 
1708 	if (params->vendor_ie.ptr && params->vendor_ie.len) {
1709 		qdf_mem_copy((req->scan_req.extraie.ptr +
1710 			      req->scan_req.extraie.len),
1711 			     params->vendor_ie.ptr, params->vendor_ie.len);
1712 
1713 		req->scan_req.extraie.len += params->vendor_ie.len;
1714 	}
1715 
1716 	if (!is_p2p_scan) {
1717 		if (req->scan_req.scan_random.randomize)
1718 			wlan_scan_rand_attrs(vdev, request, req);
1719 		if (ucfg_ie_allowlist_enabled(psoc, vdev) &&
1720 		    ucfg_copy_ie_allowlist_attrs(psoc,
1721 						 &req->scan_req.ie_allowlist))
1722 			req->scan_req.scan_f_en_ie_allowlist_in_probe = true;
1723 	}
1724 
1725 	if (request->flags & NL80211_SCAN_FLAG_FLUSH)
1726 		ucfg_scan_flush_results(pdev, NULL);
1727 
1728 	if (params->scan_probe_unicast_ra)
1729 		req->scan_req.scan_ctrl_flags_ext |=
1730 				SCAN_FLAG_EXT_FORCE_UNICAST_RA;
1731 
1732 	osif_debug("scan_ctrl_flags_ext %0x",
1733 		   req->scan_req.scan_ctrl_flags_ext);
1734 
1735 	/*
1736 	 * Acquire wakelock to handle the case where APP's send scan to connect.
1737 	 * If suspend is received during scan scan will be aborted and APP will
1738 	 * not get scan result and not connect. eg if PNO is implemented in
1739 	 * framework.
1740 	 */
1741 	wlan_scan_acquire_wake_lock_timeout(psoc,
1742 					&osif_priv->osif_scan->scan_wake_lock,
1743 					SCAN_WAKE_LOCK_SCAN_DURATION);
1744 
1745 	qdf_runtime_pm_prevent_suspend(
1746 		&osif_priv->osif_scan->runtime_pm_lock);
1747 
1748 	qdf_status = wlan_schedule_scan_start_request(pdev, request,
1749 						      params->source, req);
1750 	if (QDF_IS_STATUS_ERROR(qdf_status)) {
1751 		qdf_mutex_acquire(&osif_priv->osif_scan->scan_req_q_lock);
1752 		if (qdf_list_empty(&osif_priv->osif_scan->scan_req_q)) {
1753 			qdf_mutex_release(
1754 				&osif_priv->osif_scan->scan_req_q_lock);
1755 			qdf_runtime_pm_allow_suspend(
1756 					&osif_priv->osif_scan->runtime_pm_lock);
1757 			wlan_scan_release_wake_lock(
1758 					psoc,
1759 					&osif_priv->osif_scan->scan_wake_lock);
1760 		} else {
1761 			qdf_mutex_release(
1762 				&osif_priv->osif_scan->scan_req_q_lock);
1763 		}
1764 	}
1765 
1766 	return qdf_status_to_os_return(qdf_status);
1767 
1768 err:
1769 	qdf_mem_free(req);
1770 	return ret;
1771 }
1772 
1773 /**
1774  * wlan_get_scanid() - API to get the scan id
1775  * from the scan cookie attribute.
1776  * @pdev: Pointer to pdev object
1777  * @scan_id: Pointer to scan id
1778  * @cookie : Scan cookie attribute
1779  *
1780  * API to get the scan id from the scan cookie attribute
1781  * sent from supplicant by matching scan request.
1782  *
1783  * Return: 0 for success, non zero for failure
1784  */
1785 static int wlan_get_scanid(struct wlan_objmgr_pdev *pdev,
1786 			       uint32_t *scan_id, uint64_t cookie)
1787 {
1788 	struct scan_req *scan_req;
1789 	qdf_list_node_t *node = NULL;
1790 	qdf_list_node_t *ptr_node = NULL;
1791 	int ret = -EINVAL;
1792 	struct pdev_osif_priv *osif_ctx;
1793 	struct osif_scan_pdev *scan_priv;
1794 
1795 	/* Get NL global context from objmgr*/
1796 	osif_ctx = wlan_pdev_get_ospriv(pdev);
1797 	if (!osif_ctx) {
1798 		osif_err("Failed to retrieve osif context");
1799 		return ret;
1800 	}
1801 	scan_priv = osif_ctx->osif_scan;
1802 	qdf_mutex_acquire(&scan_priv->scan_req_q_lock);
1803 	if (qdf_list_empty(&scan_priv->scan_req_q)) {
1804 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
1805 		osif_err("Failed to retrieve scan id");
1806 		return ret;
1807 	}
1808 
1809 	if (QDF_STATUS_SUCCESS !=
1810 			    qdf_list_peek_front(&scan_priv->scan_req_q,
1811 			    &ptr_node)) {
1812 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
1813 		return ret;
1814 	}
1815 
1816 	do {
1817 		node = ptr_node;
1818 		scan_req = qdf_container_of(node, struct scan_req, node);
1819 		if (cookie ==
1820 		    (uintptr_t)(scan_req->scan_request)) {
1821 			*scan_id = scan_req->scan_id;
1822 			ret = 0;
1823 			break;
1824 		}
1825 	} while (QDF_STATUS_SUCCESS ==
1826 		 qdf_list_peek_next(&scan_priv->scan_req_q,
1827 		 node, &ptr_node));
1828 
1829 	qdf_mutex_release(&scan_priv->scan_req_q_lock);
1830 
1831 	return ret;
1832 }
1833 
1834 QDF_STATUS wlan_abort_scan(struct wlan_objmgr_pdev *pdev,
1835 				   uint32_t pdev_id, uint32_t vdev_id,
1836 				   wlan_scan_id scan_id, bool sync)
1837 {
1838 	struct scan_cancel_request *req;
1839 	struct pdev_osif_priv *osif_ctx;
1840 	struct osif_scan_pdev *scan_priv;
1841 	QDF_STATUS status;
1842 	struct wlan_objmgr_vdev *vdev;
1843 
1844 	req = qdf_mem_malloc(sizeof(*req));
1845 	if (!req)
1846 		return QDF_STATUS_E_NOMEM;
1847 
1848 	/* Get NL global context from objmgr*/
1849 	osif_ctx = wlan_pdev_get_ospriv(pdev);
1850 	if (!osif_ctx) {
1851 		osif_err("Failed to retrieve osif context");
1852 		qdf_mem_free(req);
1853 		return QDF_STATUS_E_FAILURE;
1854 	}
1855 	if (vdev_id == INVAL_VDEV_ID)
1856 		vdev = wlan_objmgr_pdev_get_first_vdev(pdev, WLAN_OSIF_ID);
1857 	else
1858 		vdev = wlan_objmgr_get_vdev_by_id_from_pdev(pdev,
1859 				vdev_id, WLAN_OSIF_ID);
1860 
1861 	if (!vdev) {
1862 		qdf_mem_free(req);
1863 		return QDF_STATUS_E_INVAL;
1864 	}
1865 	scan_priv = osif_ctx->osif_scan;
1866 	req->cancel_req.requester = scan_priv->req_id;
1867 	req->vdev = vdev;
1868 	req->cancel_req.scan_id = scan_id;
1869 	req->cancel_req.pdev_id = pdev_id;
1870 	req->cancel_req.vdev_id = vdev_id;
1871 	if (scan_id != INVAL_SCAN_ID && scan_id != CANCEL_HOST_SCAN_ID)
1872 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_SINGLE;
1873 	else if (scan_id == CANCEL_HOST_SCAN_ID)
1874 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_HOST_VDEV_ALL;
1875 	else if (vdev_id == INVAL_VDEV_ID)
1876 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_PDEV_ALL;
1877 	else
1878 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_VDEV_ALL;
1879 
1880 	osif_debug("Type %d Vdev %d pdev %d scan id %d sync %d",
1881 		   req->cancel_req.req_type, req->cancel_req.vdev_id,
1882 		   req->cancel_req.pdev_id, req->cancel_req.scan_id, sync);
1883 
1884 	if (sync)
1885 		status = ucfg_scan_cancel_sync(req);
1886 	else
1887 		status = ucfg_scan_cancel(req);
1888 	if (QDF_IS_STATUS_ERROR(status))
1889 		osif_err("Cancel scan request failed");
1890 
1891 	wlan_objmgr_vdev_release_ref(vdev, WLAN_OSIF_ID);
1892 
1893 	return status;
1894 }
1895 
1896 qdf_export_symbol(wlan_abort_scan);
1897 
1898 int wlan_cfg80211_abort_scan(struct wlan_objmgr_pdev *pdev)
1899 {
1900 	uint8_t pdev_id;
1901 
1902 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1903 
1904 	if (ucfg_scan_get_pdev_status(pdev) !=
1905 	   SCAN_NOT_IN_PROGRESS)
1906 		wlan_abort_scan(pdev, pdev_id,
1907 			INVAL_VDEV_ID, INVAL_SCAN_ID, true);
1908 
1909 	return 0;
1910 }
1911 
1912 int wlan_vendor_abort_scan(struct wlan_objmgr_pdev *pdev,
1913 			const void *data, int data_len)
1914 {
1915 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1];
1916 	int ret = -EINVAL;
1917 	wlan_scan_id scan_id;
1918 	uint64_t cookie;
1919 	uint8_t pdev_id;
1920 
1921 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1922 	if (wlan_cfg80211_nla_parse(tb, QCA_WLAN_VENDOR_ATTR_SCAN_MAX, data,
1923 				    data_len, cfg80211_scan_policy)) {
1924 		osif_err("Invalid ATTR");
1925 		return ret;
1926 	}
1927 
1928 	if (tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]) {
1929 		cookie = nla_get_u64(
1930 			    tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
1931 		ret = wlan_get_scanid(pdev, &scan_id, cookie);
1932 		if (ret != 0)
1933 			return ret;
1934 		if (ucfg_scan_get_pdev_status(pdev) !=
1935 		   SCAN_NOT_IN_PROGRESS)
1936 			wlan_abort_scan(pdev, INVAL_PDEV_ID,
1937 					INVAL_VDEV_ID, scan_id, true);
1938 	}
1939 	return 0;
1940 }
1941 
1942 static inline struct ieee80211_channel *
1943 wlan_get_ieee80211_channel(struct wiphy *wiphy,
1944 		struct wlan_objmgr_pdev *pdev,
1945 		int chan_freq)
1946 {
1947 	struct ieee80211_channel *chan;
1948 
1949 	chan = ieee80211_get_channel(wiphy, chan_freq);
1950 	if (!chan)
1951 		osif_err_rl("chan is NULL, freq: %d", chan_freq);
1952 
1953 	return chan;
1954 }
1955 
1956 #ifdef WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS
1957 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params)
1958 {
1959 	return util_scan_entry_frame_len(scan_params) + sizeof(qcom_ie_age);
1960 }
1961 
1962 static inline void wlan_add_age_ie(uint8_t *mgmt_frame,
1963 	struct scan_cache_entry *scan_params)
1964 {
1965 	qcom_ie_age *qie_age = NULL;
1966 
1967 	/* GPS Requirement: need age ie per entry. Using vendor specific. */
1968 	/* Assuming this is the last IE, copy at the end */
1969 	qie_age = (qcom_ie_age *) (mgmt_frame +
1970 		   util_scan_entry_frame_len(scan_params));
1971 	qie_age->element_id = QCOM_VENDOR_IE_ID;
1972 	qie_age->len = QCOM_VENDOR_IE_AGE_LEN;
1973 	qie_age->oui_1 = QCOM_OUI1;
1974 	qie_age->oui_2 = QCOM_OUI2;
1975 	qie_age->oui_3 = QCOM_OUI3;
1976 	qie_age->type = QCOM_VENDOR_IE_AGE_TYPE;
1977 	/*
1978 	 * Lowi expects the timestamp of bss in units of 1/10 ms. In driver
1979 	 * all bss related timestamp is in units of ms. Due to this when scan
1980 	 * results are sent to lowi the scan age is high.To address this,
1981 	 * send age in units of 1/10 ms.
1982 	 */
1983 	qie_age->age =
1984 		(uint32_t)(qdf_mc_timer_get_system_time() -
1985 		  scan_params->scan_entry_time)/10;
1986 	qie_age->tsf_delta = scan_params->tsf_delta;
1987 	memcpy(&qie_age->beacon_tsf, scan_params->tsf_info.data,
1988 		  sizeof(qie_age->beacon_tsf));
1989 	memcpy(&qie_age->seq_ctrl, &scan_params->seq_num,
1990 	       sizeof(qie_age->seq_ctrl));
1991 }
1992 #else
1993 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params)
1994 {
1995 	return util_scan_entry_frame_len(scan_params);
1996 }
1997 
1998 static inline void wlan_add_age_ie(uint8_t *mgmt_frame,
1999 	struct scan_cache_entry *scan_params)
2000 {
2001 }
2002 #endif /* WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS */
2003 
2004 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \
2005 	defined(CFG80211_INFORM_BSS_FRAME_DATA)
2006 /**
2007  * wlan_fill_per_chain_rssi() - fill per chain RSSI in inform bss
2008  * @data: bss data
2009  * @per_chain_snr: per chain RSSI
2010  *
2011  * Return: void
2012  */
2013 #if defined(CFG80211_SCAN_PER_CHAIN_RSSI_SUPPORT) || \
2014 	   (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
2015 static void wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data,
2016 	struct wlan_cfg80211_inform_bss *bss)
2017 {
2018 
2019 	uint32_t i;
2020 
2021 	if (!bss || !data) {
2022 		osif_err("Received bss is NULL");
2023 		return;
2024 	}
2025 	for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) {
2026 		if (!bss->per_chain_rssi[i] ||
2027 		    (bss->per_chain_rssi[i] == WLAN_INVALID_PER_CHAIN_RSSI))
2028 			continue;
2029 		data->chain_signal[i] = bss->per_chain_rssi[i];
2030 		data->chains |= BIT(i);
2031 	}
2032 }
2033 #else
2034 static inline void
2035 wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data,
2036 	struct wlan_cfg80211_inform_bss *bss)
2037 {
2038 }
2039 #endif
2040 
2041 struct cfg80211_bss *
2042 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2043 		struct wlan_cfg80211_inform_bss *bss)
2044 {
2045 	struct cfg80211_inform_bss data  = {0};
2046 
2047 	if (!bss) {
2048 		osif_err("bss is null");
2049 		return NULL;
2050 	}
2051 	wlan_fill_per_chain_rssi(&data, bss);
2052 
2053 	data.chan = bss->chan;
2054 	data.boottime_ns = bss->boottime_ns;
2055 	data.signal = bss->rssi;
2056 	return cfg80211_inform_bss_frame_data(wiphy, &data, bss->mgmt,
2057 					      bss->frame_len, GFP_ATOMIC);
2058 }
2059 #else
2060 struct cfg80211_bss *
2061 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
2062 		struct wlan_cfg80211_inform_bss *bss)
2063 
2064 {
2065 	return cfg80211_inform_bss_frame(wiphy, bss->chan, bss->mgmt,
2066 					 bss->frame_len,
2067 					 bss->rssi, GFP_ATOMIC);
2068 }
2069 #endif
2070 
2071 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
2072 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy,
2073 		struct cfg80211_bss *bss)
2074 {
2075 	cfg80211_put_bss(wiphy, bss);
2076 }
2077 #else
2078 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy,
2079 		struct cfg80211_bss *bss)
2080 {
2081 	cfg80211_put_bss(bss);
2082 }
2083 #endif
2084 
2085 void wlan_cfg80211_inform_bss_frame(struct wlan_objmgr_pdev *pdev,
2086 		struct scan_cache_entry *scan_params)
2087 {
2088 	struct pdev_osif_priv *pdev_ospriv = wlan_pdev_get_ospriv(pdev);
2089 	struct wiphy *wiphy;
2090 	struct cfg80211_bss *bss = NULL;
2091 	struct wlan_cfg80211_inform_bss bss_data = {0};
2092 
2093 	if (!pdev_ospriv) {
2094 		osif_err("os_priv is NULL");
2095 		return;
2096 	}
2097 
2098 	wiphy = pdev_ospriv->wiphy;
2099 
2100 	bss_data.frame_len = wlan_get_frame_len(scan_params);
2101 	bss_data.mgmt = qdf_mem_malloc_atomic(bss_data.frame_len);
2102 	if (!bss_data.mgmt) {
2103 		osif_err("bss mem alloc failed for seq %d",
2104 			 scan_params->seq_num);
2105 		return;
2106 	}
2107 	qdf_mem_copy(bss_data.mgmt,
2108 		 util_scan_entry_frame_ptr(scan_params),
2109 		 util_scan_entry_frame_len(scan_params));
2110 	/*
2111 	 * Android does not want the timestamp from the frame.
2112 	 * Instead it wants a monotonic increasing value
2113 	 */
2114 	bss_data.mgmt->u.probe_resp.timestamp = qdf_get_monotonic_boottime();
2115 	wlan_add_age_ie((uint8_t *)bss_data.mgmt, scan_params);
2116 	/*
2117 	 * Based on .ini configuration, raw rssi can be reported for bss.
2118 	 * Raw rssi is typically used for estimating power.
2119 	 */
2120 	bss_data.rssi = scan_params->rssi_raw;
2121 
2122 	bss_data.chan = wlan_get_ieee80211_channel(wiphy, pdev,
2123 		scan_params->channel.chan_freq);
2124 	if (!bss_data.chan) {
2125 		osif_err_rl("Channel not found for bss " QDF_MAC_ADDR_FMT " seq %d chan_freq %d",
2126 			    QDF_MAC_ADDR_REF(bss_data.mgmt->bssid),
2127 			    scan_params->seq_num,
2128 			    scan_params->channel.chan_freq);
2129 		qdf_mem_free(bss_data.mgmt);
2130 		return;
2131 	}
2132 
2133 	/*
2134 	 * Supplicant takes the signal strength in terms of
2135 	 * mBm (1 dBm = 100 mBm).
2136 	 */
2137 	bss_data.rssi = QDF_MIN(bss_data.rssi, 0) * 100;
2138 
2139 	bss_data.boottime_ns = scan_params->boottime_ns;
2140 
2141 	qdf_mem_copy(bss_data.per_chain_rssi, scan_params->per_chain_rssi,
2142 		     WLAN_MGMT_TXRX_HOST_MAX_ANTENNA);
2143 
2144 	bss = wlan_cfg80211_inform_bss_frame_data(wiphy, &bss_data);
2145 	if (!bss)
2146 		osif_err("failed to inform bss "QDF_MAC_ADDR_FMT" seq %d",
2147 			 QDF_MAC_ADDR_REF(bss_data.mgmt->bssid),
2148 			 scan_params->seq_num);
2149 	else
2150 		wlan_cfg80211_put_bss(wiphy, bss);
2151 
2152 	qdf_mem_free(bss_data.mgmt);
2153 }
2154 
2155 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)) && \
2156 	!defined(WITH_BACKPORTS) && !defined(IEEE80211_PRIVACY)
2157 struct cfg80211_bss *wlan_cfg80211_get_bss(struct wiphy *wiphy,
2158 					   struct ieee80211_channel *channel,
2159 					   const u8 *bssid, const u8 *ssid,
2160 					   size_t ssid_len)
2161 {
2162 	return cfg80211_get_bss(wiphy, channel, bssid,
2163 				ssid, ssid_len,
2164 				WLAN_CAPABILITY_ESS,
2165 				WLAN_CAPABILITY_ESS);
2166 }
2167 #else
2168 struct cfg80211_bss *wlan_cfg80211_get_bss(struct wiphy *wiphy,
2169 					   struct ieee80211_channel *channel,
2170 					   const u8 *bssid, const u8 *ssid,
2171 					   size_t ssid_len)
2172 {
2173 	return cfg80211_get_bss(wiphy, channel, bssid,
2174 				ssid, ssid_len,
2175 				IEEE80211_BSS_TYPE_ESS,
2176 				IEEE80211_PRIVACY_ANY);
2177 }
2178 #endif
2179 
2180 QDF_STATUS  __wlan_cfg80211_unlink_bss_list(struct wiphy *wiphy,
2181 					    struct wlan_objmgr_pdev *pdev,
2182 					    uint8_t *bssid, uint8_t *ssid,
2183 					    uint8_t ssid_len)
2184 {
2185 	struct cfg80211_bss *bss = NULL;
2186 	uint8_t vdev_id;
2187 
2188 	if (bssid && wlan_get_connected_vdev_by_bssid(pdev, bssid, &vdev_id)) {
2189 		osif_debug("BSS "QDF_MAC_ADDR_FMT" connected on vdev %d dont unlink",
2190 			   QDF_MAC_ADDR_REF(bssid), vdev_id);
2191 		return QDF_STATUS_E_FAILURE;
2192 	}
2193 
2194 	bss = wlan_cfg80211_get_bss(wiphy, NULL, bssid,
2195 				    ssid, ssid_len);
2196 	if (!bss) {
2197 		osif_info("BSS "QDF_MAC_ADDR_FMT" not found",
2198 			  QDF_MAC_ADDR_REF(bssid));
2199 	} else {
2200 		osif_debug("unlink entry for ssid:%.*s and BSSID "QDF_MAC_ADDR_FMT,
2201 			   ssid_len, ssid, QDF_MAC_ADDR_REF(bssid));
2202 		cfg80211_unlink_bss(wiphy, bss);
2203 		wlan_cfg80211_put_bss(wiphy, bss);
2204 	}
2205 
2206 	/*
2207 	 * Kernel creates separate entries into it's bss list for probe resp
2208 	 * and beacon for hidden AP. Both have separate ref count and thus
2209 	 * deleting one will not delete other entry.
2210 	 * If beacon entry of the hidden AP is not deleted and AP switch to
2211 	 * broadcasting SSID from Hiding SSID, kernel will reject the beacon
2212 	 * entry. So unlink the hidden beacon entry (if present) as well from
2213 	 * kernel, to avoid such issue.
2214 	 */
2215 	bss = wlan_cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0);
2216 	if (!bss) {
2217 		osif_debug("Hidden bss not found for Ssid:%.*s BSSID: "QDF_MAC_ADDR_FMT" sid_len %d",
2218 			   ssid_len, ssid, QDF_MAC_ADDR_REF(bssid), ssid_len);
2219 	} else {
2220 		osif_debug("unlink entry for Hidden ssid:%.*s and BSSID "QDF_MAC_ADDR_FMT,
2221 			   ssid_len, ssid, QDF_MAC_ADDR_REF(bssid));
2222 
2223 		cfg80211_unlink_bss(wiphy, bss);
2224 		/* cfg80211_get_bss get bss with ref count so release it */
2225 		wlan_cfg80211_put_bss(wiphy, bss);
2226 	}
2227 
2228 	return QDF_STATUS_SUCCESS;
2229 }
2230 void wlan_cfg80211_unlink_bss_list(struct wlan_objmgr_pdev *pdev,
2231 				   struct scan_cache_entry *scan_entry)
2232 {
2233 	struct pdev_osif_priv *pdev_ospriv = wlan_pdev_get_ospriv(pdev);
2234 	struct wiphy *wiphy;
2235 
2236 	if (!pdev_ospriv) {
2237 		osif_err("os_priv is NULL");
2238 		return;
2239 	}
2240 
2241 	wiphy = pdev_ospriv->wiphy;
2242 
2243 	__wlan_cfg80211_unlink_bss_list(wiphy, pdev, scan_entry->bssid.bytes,
2244 					scan_entry->ssid.ssid,
2245 					scan_entry->ssid.length);
2246 }
2247 
2248 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0))
2249 /*
2250  * wlan_scan_wiphy_set_max_sched_scans() - set maximum number of scheduled scans
2251  * to wiphy.
2252  * @wiphy: pointer to wiphy
2253  * @max_scans: max num scans to be configured
2254  *
2255  */
2256 static inline void
2257 wlan_scan_wiphy_set_max_sched_scans(struct wiphy *wiphy, uint8_t max_scans)
2258 {
2259 	if (max_scans == 0)
2260 		wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
2261 	else
2262 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
2263 }
2264 #else
2265 static inline void
2266 wlan_scan_wiphy_set_max_sched_scans(struct wiphy *wiphy, uint8_t max_scans)
2267 {
2268 	wiphy->max_sched_scan_reqs = max_scans;
2269 }
2270 #endif /* KERNEL_VERSION(4, 12, 0) */
2271 
2272 #if defined(CFG80211_REPORT_BETTER_BSS_IN_SCHED_SCAN) || \
2273 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
2274 void wlan_scan_cfg80211_add_connected_pno_support(struct wiphy *wiphy)
2275 {
2276 	wiphy_ext_feature_set(wiphy,
2277 			      NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI);
2278 }
2279 #endif
2280 
2281 #if ((LINUX_VERSION_CODE > KERNEL_VERSION(4, 4, 0)) || \
2282 		defined(CFG80211_MULTI_SCAN_PLAN_BACKPORT)) && \
2283 		defined(FEATURE_WLAN_SCAN_PNO)
2284 void wlan_config_sched_scan_plans_to_wiphy(struct wiphy *wiphy,
2285 					   struct wlan_objmgr_psoc *psoc)
2286 {
2287 	if (ucfg_scan_get_pno_scan_support(psoc)) {
2288 		wlan_scan_wiphy_set_max_sched_scans(wiphy, 1);
2289 		wiphy->max_sched_scan_ssids = SCAN_PNO_MAX_SUPP_NETWORKS;
2290 		wiphy->max_match_sets = SCAN_PNO_MAX_SUPP_NETWORKS;
2291 		wiphy->max_sched_scan_ie_len = SCAN_MAX_IE_LENGTH;
2292 		wiphy->max_sched_scan_plans = SCAN_PNO_MAX_PLAN_REQUEST;
2293 
2294 		wiphy->max_sched_scan_plan_interval =
2295 			ucfg_scan_get_max_sched_scan_plan_interval(psoc);
2296 
2297 		wiphy->max_sched_scan_plan_iterations =
2298 			ucfg_scan_get_max_sched_scan_plan_iterations(psoc);
2299 	}
2300 }
2301 #endif
2302