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