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