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