xref: /wlan-dirver/qca-wifi-host-cmn/os_if/linux/scan/src/wlan_cfg80211_scan.c (revision 11f5a63a6cbdda84849a730de22f0a71e635d58c)
1 /*
2  * Copyright (c) 2017-2019 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 <linux/wireless.h>
26 #include <linux/netdevice.h>
27 #include <net/cfg80211.h>
28 #include <wlan_scan_utils_api.h>
29 #include <wlan_cfg80211.h>
30 #include <wlan_cfg80211_scan.h>
31 #include <wlan_osif_priv.h>
32 #include <wlan_scan_public_structs.h>
33 #include <wlan_scan_ucfg_api.h>
34 #include <wlan_cfg80211_scan.h>
35 #include <qdf_mem.h>
36 #include <wlan_utility.h>
37 #include "cfg_ucfg_api.h"
38 #ifdef WLAN_POLICY_MGR_ENABLE
39 #include <wlan_policy_mgr_api.h>
40 #endif
41 #include <wlan_reg_services_api.h>
42 #ifdef FEATURE_WLAN_DIAG_SUPPORT
43 #include "host_diag_core_event.h"
44 #endif
45 
46 static const
47 struct nla_policy scan_policy[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1] = {
48 	[QCA_WLAN_VENDOR_ATTR_SCAN_FLAGS] = {.type = NLA_U32},
49 	[QCA_WLAN_VENDOR_ATTR_SCAN_TX_NO_CCK_RATE] = {.type = NLA_FLAG},
50 	[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE] = {.type = NLA_U64},
51 };
52 
53 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \
54 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
55 /**
56  * wlan_fill_scan_rand_attrs() - Populate the scan randomization attrs
57  * @vdev: pointer to objmgr vdev
58  * @flags: cfg80211 scan flags
59  * @mac_addr: random mac addr from cfg80211
60  * @mac_addr_mask: mac addr mask from cfg80211
61  * @randomize: output variable to check scan randomization status
62  * @addr: output variable to hold random addr
63  * @mask: output variable to hold mac mask
64  *
65  * Return: None
66  */
67 static void wlan_fill_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
68 				      uint32_t flags,
69 				      uint8_t *mac_addr,
70 				      uint8_t *mac_addr_mask,
71 				      bool *randomize,
72 				      uint8_t *addr,
73 				      uint8_t *mask)
74 {
75 	*randomize = false;
76 	if (!(flags & NL80211_SCAN_FLAG_RANDOM_ADDR))
77 		return;
78 
79 	if (wlan_vdev_mlme_get_opmode(vdev) != QDF_STA_MODE)
80 		return;
81 
82 	if (wlan_vdev_is_up(vdev) == QDF_STATUS_SUCCESS)
83 		return;
84 
85 	*randomize = true;
86 	memcpy(addr, mac_addr, QDF_MAC_ADDR_SIZE);
87 	memcpy(mask, mac_addr_mask, QDF_MAC_ADDR_SIZE);
88 	osif_debug("Random mac addr: %pM and Random mac mask: %pM",
89 		   addr, mask);
90 }
91 
92 /**
93  * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs
94  * @vdev: pointer to objmgr vdev
95  * @request: pointer to cfg80211 scan request
96  * @req: pointer to cmn module scan request
97  *
98  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
99  * to fill random attributes of internal scan request with cfg80211_scan_request
100  *
101  * Return: None
102  */
103 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
104 				 struct cfg80211_scan_request *request,
105 				 struct scan_start_request *req)
106 {
107 	bool *randomize = &req->scan_req.scan_random.randomize;
108 	uint8_t *mac_addr = req->scan_req.scan_random.mac_addr;
109 	uint8_t *mac_mask = req->scan_req.scan_random.mac_mask;
110 
111 	wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr,
112 				  request->mac_addr_mask, randomize, mac_addr,
113 				  mac_mask);
114 	if (!*randomize)
115 		return;
116 
117 	req->scan_req.scan_f_add_spoofed_mac_in_probe = true;
118 	req->scan_req.scan_f_add_rand_seq_in_probe = true;
119 }
120 #else
121 /**
122  * wlan_scan_rand_attrs() - Wrapper function to fill scan random attrs
123  * @vdev: pointer to objmgr vdev
124  * @request: pointer to cfg80211 scan request
125  * @req: pointer to cmn module scan request
126  *
127  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
128  * to fill random attributes of internal scan request with cfg80211_scan_request
129  *
130  * Return: None
131  */
132 static void wlan_scan_rand_attrs(struct wlan_objmgr_vdev *vdev,
133 				 struct cfg80211_scan_request *request,
134 				 struct scan_start_request *req)
135 {
136 }
137 #endif
138 
139 #ifdef FEATURE_WLAN_SCAN_PNO
140 #if ((LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \
141 	defined(CFG80211_MULTI_SCAN_PLAN_BACKPORT))
142 
143 /**
144  * wlan_config_sched_scan_plan() - configures the sched scan plans
145  *   from the framework.
146  * @pno_req: pointer to PNO scan request
147  * @request: pointer to scan request from framework
148  *
149  * Return: None
150  */
151 static void
152 wlan_config_sched_scan_plan(struct pno_scan_req_params *pno_req,
153 			    struct cfg80211_sched_scan_request *request)
154 {
155 	/*
156 	 * As of now max 2 scan plans were supported by firmware
157 	 * if number of scan plan supported by firmware increased below logic
158 	 * must change.
159 	 */
160 	if (request->n_scan_plans == SCAN_PNO_MAX_PLAN_REQUEST) {
161 		pno_req->fast_scan_period =
162 			request->scan_plans[0].interval * MSEC_PER_SEC;
163 		pno_req->fast_scan_max_cycles =
164 			request->scan_plans[0].iterations;
165 		pno_req->slow_scan_period =
166 			request->scan_plans[1].interval * MSEC_PER_SEC;
167 	} else if (request->n_scan_plans == 1) {
168 		pno_req->fast_scan_period =
169 			request->scan_plans[0].interval * MSEC_PER_SEC;
170 		/*
171 		 * if only one scan plan is configured from framework
172 		 * then both fast and slow scan should be configured with the
173 		 * same value that is why fast scan cycles are hardcoded to one
174 		 */
175 		pno_req->fast_scan_max_cycles = 1;
176 		pno_req->slow_scan_period =
177 			request->scan_plans[0].interval * MSEC_PER_SEC;
178 	} else {
179 		osif_err("Invalid number of scan plans %d !!",
180 			 request->n_scan_plans);
181 	}
182 }
183 #else
184 #define wlan_config_sched_scan_plan(pno_req, request) \
185 	__wlan_config_sched_scan_plan(pno_req, request, psoc)
186 
187 static void
188 __wlan_config_sched_scan_plan(struct pno_scan_req_params *pno_req,
189 			      struct cfg80211_sched_scan_request *request,
190 			      struct wlan_objmgr_psoc *psoc)
191 {
192 	uint32_t scan_timer_repeat_value, slow_scan_multiplier;
193 
194 	scan_timer_repeat_value = ucfg_scan_get_scan_timer_repeat_value(psoc);
195 	slow_scan_multiplier = ucfg_scan_get_slow_scan_multiplier(psoc);
196 
197 	pno_req->fast_scan_period = request->interval;
198 	pno_req->fast_scan_max_cycles = scan_timer_repeat_value;
199 	pno_req->slow_scan_period =
200 		(slow_scan_multiplier * pno_req->fast_scan_period);
201 	osif_debug("Base scan interval: %d sec PNO Scan Timer Repeat Value: %d",
202 		   (request->interval / 1000), scan_timer_repeat_value);
203 }
204 #endif
205 
206 #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0)
207 static inline void
208 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid)
209 {
210 	cfg80211_sched_scan_results(wiphy);
211 }
212 #else
213 static inline void
214 wlan_cfg80211_sched_scan_results(struct wiphy *wiphy, uint64_t reqid)
215 {
216 	cfg80211_sched_scan_results(wiphy, reqid);
217 }
218 #endif
219 
220 /**
221  * wlan_cfg80211_pno_callback() - pno callback function to handle
222  * pno events.
223  * @vdev: vdev ptr
224  * @event: scan events
225  * @args: argument
226  *
227  * Return: void
228  */
229 static void wlan_cfg80211_pno_callback(struct wlan_objmgr_vdev *vdev,
230 	struct scan_event *event,
231 	void *args)
232 {
233 	struct wlan_objmgr_pdev *pdev;
234 	struct pdev_osif_priv *pdev_ospriv;
235 
236 	if (event->type != SCAN_EVENT_TYPE_NLO_COMPLETE)
237 		return;
238 
239 	osif_debug("vdev id = %d", event->vdev_id);
240 
241 	pdev = wlan_vdev_get_pdev(vdev);
242 	if (!pdev) {
243 		osif_err("pdev is NULL");
244 		return;
245 	}
246 
247 	pdev_ospriv = wlan_pdev_get_ospriv(pdev);
248 	if (!pdev_ospriv) {
249 		osif_err("pdev_ospriv is NULL");
250 		return;
251 	}
252 	wlan_cfg80211_sched_scan_results(pdev_ospriv->wiphy, 0);
253 }
254 
255 #ifdef WLAN_POLICY_MGR_ENABLE
256 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc)
257 {
258 	return policy_mgr_mode_specific_connection_count(psoc,
259 							  PM_SAP_MODE,
260 							  NULL) ||
261 		policy_mgr_mode_specific_connection_count(psoc,
262 							  PM_P2P_GO_MODE,
263 							  NULL);
264 }
265 
266 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs(
267 			struct wlan_objmgr_psoc *psoc,
268 			u8 channel, bool *ok)
269 {
270 	QDF_STATUS status = policy_mgr_is_chan_ok_for_dnbs(
271 				psoc, wlan_chan_to_freq(channel), ok);
272 
273 	if (QDF_IS_STATUS_ERROR(status)) {
274 		osif_err("DNBS check failed");
275 		return status;
276 	}
277 
278 	return QDF_STATUS_SUCCESS;
279 }
280 #else
281 static bool wlan_cfg80211_is_ap_go_present(struct wlan_objmgr_psoc *psoc)
282 {
283 	return false;
284 }
285 
286 static QDF_STATUS wlan_cfg80211_is_chan_ok_for_dnbs(
287 			struct wlan_objmgr_psoc *psoc,
288 			u8 channel,
289 			bool *ok)
290 {
291 	if (!ok)
292 		return QDF_STATUS_E_INVAL;
293 
294 	*ok = true;
295 	return QDF_STATUS_SUCCESS;
296 }
297 #endif
298 
299 #if defined(CFG80211_SCAN_RANDOM_MAC_ADDR) || \
300 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
301 /**
302  * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs
303  * @vdev: pointer to objmgr vdev
304  * @request: pointer to cfg80211 sched scan request
305  * @req: pointer to cmn module pno scan request
306  *
307  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
308  * to fill random attributes of internal pno scan
309  * with cfg80211_sched_scan_request
310  *
311  * Return: None
312  */
313 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev,
314 				    struct cfg80211_sched_scan_request *request,
315 				    struct pno_scan_req_params *req)
316 {
317 	bool *randomize = &req->scan_random.randomize;
318 	uint8_t *mac_addr = req->scan_random.mac_addr;
319 	uint8_t *mac_mask = req->scan_random.mac_mask;
320 
321 	wlan_fill_scan_rand_attrs(vdev, request->flags, request->mac_addr,
322 				  request->mac_addr_mask, randomize, mac_addr,
323 				  mac_mask);
324 }
325 #else
326 /**
327  * wlan_pno_scan_rand_attr() - Wrapper function to fill sched scan random attrs
328  * @vdev: pointer to objmgr vdev
329  * @request: pointer to cfg80211 sched scan request
330  * @req: pointer to cmn module pno scan request
331  *
332  * This is a wrapper function which invokes wlan_fill_scan_rand_attrs()
333  * to fill random attributes of internal pno scan
334  * with cfg80211_sched_scan_request
335  *
336  * Return: None
337  */
338 static void wlan_pno_scan_rand_attr(struct wlan_objmgr_vdev *vdev,
339 				    struct cfg80211_sched_scan_request *request,
340 				    struct pno_scan_req_params *req)
341 {
342 }
343 #endif
344 
345 /**
346  * wlan_hdd_sched_scan_update_relative_rssi() - update CPNO params
347  * @pno_request: pointer to PNO scan request
348  * @request: Pointer to cfg80211 scheduled scan start request
349  *
350  * This function is used to update Connected PNO params sent by kernel
351  *
352  * Return: None
353  */
354 #if defined(CFG80211_REPORT_BETTER_BSS_IN_SCHED_SCAN) || \
355 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
356 static inline void wlan_hdd_sched_scan_update_relative_rssi(
357 			struct pno_scan_req_params *pno_request,
358 			struct cfg80211_sched_scan_request *request)
359 {
360 	pno_request->relative_rssi_set = request->relative_rssi_set;
361 	pno_request->relative_rssi = request->relative_rssi;
362 	if (NL80211_BAND_2GHZ == request->rssi_adjust.band)
363 		pno_request->band_rssi_pref.band = WLAN_BAND_2_4_GHZ;
364 	else if (NL80211_BAND_5GHZ == request->rssi_adjust.band)
365 		pno_request->band_rssi_pref.band = WLAN_BAND_5_GHZ;
366 	pno_request->band_rssi_pref.rssi = request->rssi_adjust.delta;
367 }
368 #else
369 static inline void wlan_hdd_sched_scan_update_relative_rssi(
370 			struct pno_scan_req_params *pno_request,
371 			struct cfg80211_sched_scan_request *request)
372 {
373 }
374 #endif
375 
376 #ifdef FEATURE_WLAN_SCAN_PNO
377 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0))
378 static uint32_t wlan_config_sched_scan_start_delay(
379 		struct cfg80211_sched_scan_request *request)
380 {
381 	return request->delay;
382 }
383 #else
384 static uint32_t wlan_config_sched_scan_start_delay(
385 		struct cfg80211_sched_scan_request *request)
386 {
387 	return 0;
388 }
389 #endif /*(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) */
390 #endif /* FEATURE_WLAN_SCAN_PNO */
391 
392 int wlan_cfg80211_sched_scan_start(struct wlan_objmgr_vdev *vdev,
393 				   struct cfg80211_sched_scan_request *request,
394 				   uint8_t scan_backoff_multiplier)
395 {
396 	struct pno_scan_req_params *req;
397 	int i, j, ret = 0;
398 	QDF_STATUS status;
399 	uint8_t num_chan = 0, channel;
400 	struct wlan_objmgr_pdev *pdev = wlan_vdev_get_pdev(vdev);
401 	struct wlan_objmgr_psoc *psoc;
402 	uint32_t valid_ch[SCAN_PNO_MAX_NETW_CHANNELS_EX] = {0};
403 	bool enable_dfs_pno_chnl_scan;
404 
405 	if (ucfg_scan_get_pno_in_progress(vdev)) {
406 		osif_debug("pno is already in progress");
407 		return -EBUSY;
408 	}
409 
410 	if (ucfg_scan_get_pdev_status(pdev) !=
411 	   SCAN_NOT_IN_PROGRESS) {
412 		status = wlan_abort_scan(pdev,
413 				wlan_objmgr_pdev_get_pdev_id(pdev),
414 				INVAL_VDEV_ID, INVAL_SCAN_ID, true);
415 		if (QDF_IS_STATUS_ERROR(status)) {
416 			osif_err("aborting the existing scan is unsuccessful");
417 			return -EBUSY;
418 		}
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 			channel = request->channels[i]->hw_value;
459 			if ((!enable_dfs_pno_chnl_scan) &&
460 			    (wlan_reg_is_dfs_ch(pdev, channel))) {
461 				osif_debug("Dropping DFS channel :%d",
462 					   channel);
463 				continue;
464 			}
465 			if (wlan_reg_is_dsrc_chan(pdev, channel))
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 								  channel,
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 ", channel);
486 			valid_ch[num_chan++] = wlan_chan_to_freq(channel);
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 	osif_debug("Dequeue Scan id: %d", scan_id);
721 
722 	if ((!source) || (!req)) {
723 		osif_err("source or request is NULL");
724 		return QDF_STATUS_E_NULL_VALUE;
725 	}
726 
727 	/* Get NL global context from objmgr*/
728 	osif_ctx = wlan_pdev_get_ospriv(pdev);
729 	if (!osif_ctx) {
730 		osif_err("Failed to retrieve osif context");
731 		return status;
732 	}
733 	scan_priv = osif_ctx->osif_scan;
734 
735 	if (qdf_list_empty(&scan_priv->scan_req_q)) {
736 		osif_info("Scan List is empty");
737 		return QDF_STATUS_E_FAILURE;
738 	}
739 
740 	qdf_mutex_acquire(&scan_priv->scan_req_q_lock);
741 	if (QDF_STATUS_SUCCESS !=
742 		qdf_list_peek_front(&scan_priv->scan_req_q, &next_node)) {
743 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
744 		osif_err("Failed to remove Scan Req from queue");
745 		return QDF_STATUS_E_FAILURE;
746 	}
747 
748 	do {
749 		node = next_node;
750 		scan_req = qdf_container_of(node, struct scan_req, node);
751 		if (scan_req->scan_id == scan_id) {
752 			status = qdf_list_remove_node(&scan_priv->scan_req_q,
753 						      node);
754 			if (status == QDF_STATUS_SUCCESS) {
755 				*req = scan_req->scan_request;
756 				*source = scan_req->source;
757 				*dev = scan_req->dev;
758 				qdf_mem_free(scan_req);
759 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
760 				osif_debug("removed Scan id: %d, req = %pK, pending scans %d",
761 					   scan_id, req,
762 					   qdf_list_size(&scan_priv->scan_req_q));
763 				return QDF_STATUS_SUCCESS;
764 			} else {
765 				qdf_mutex_release(&scan_priv->scan_req_q_lock);
766 				osif_err("Failed to remove node scan id %d, pending scans %d",
767 					 scan_id,
768 					 qdf_list_size(&scan_priv->scan_req_q));
769 				return status;
770 			}
771 		}
772 	} while (QDF_STATUS_SUCCESS ==
773 		qdf_list_peek_next(&scan_priv->scan_req_q, node, &next_node));
774 	qdf_mutex_release(&scan_priv->scan_req_q_lock);
775 	osif_err("Failed to find scan id %d", scan_id);
776 
777 	return status;
778 }
779 
780 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0))
781 /**
782  * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211
783  * @netdev: Net device
784  * @req : Scan request
785  * @aborted : true scan aborted false scan success
786  *
787  * This function notifies scan done to cfg80211
788  *
789  * Return: none
790  */
791 static void wlan_cfg80211_scan_done(struct net_device *netdev,
792 				    struct cfg80211_scan_request *req,
793 				    bool aborted)
794 {
795 	struct cfg80211_scan_info info = {
796 		.aborted = aborted
797 	};
798 
799 	if (netdev->flags & IFF_UP)
800 		cfg80211_scan_done(req, &info);
801 }
802 #elif (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 14, 0))
803 /**
804  * wlan_cfg80211_scan_done() - Scan completed callback to cfg80211
805  * @netdev: Net device
806  * @req : Scan request
807  * @aborted : true scan aborted false scan success
808  *
809  * This function notifies scan done to cfg80211
810  *
811  * Return: none
812  */
813 static void wlan_cfg80211_scan_done(struct net_device *netdev,
814 				    struct cfg80211_scan_request *req,
815 				    bool aborted)
816 {
817 	if (netdev->flags & IFF_UP)
818 		cfg80211_scan_done(req, aborted);
819 }
820 #endif
821 
822 /**
823  * wlan_vendor_scan_callback() - Scan completed callback event
824  *
825  * @req : Scan request
826  * @aborted : true scan aborted false scan success
827  *
828  * This function sends scan completed callback event to NL.
829  *
830  * Return: none
831  */
832 static void wlan_vendor_scan_callback(struct cfg80211_scan_request *req,
833 					bool aborted)
834 {
835 	struct sk_buff *skb;
836 	struct nlattr *attr;
837 	int i;
838 	uint8_t scan_status;
839 	uint64_t cookie;
840 
841 	skb = cfg80211_vendor_event_alloc(req->wdev->wiphy, req->wdev,
842 			SCAN_DONE_EVENT_BUF_SIZE + 4 + NLMSG_HDRLEN,
843 			QCA_NL80211_VENDOR_SUBCMD_SCAN_DONE_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 	cfg80211_vendor_event(skb, GFP_ATOMIC);
891 	qdf_mem_free(req);
892 
893 	return;
894 
895 nla_put_failure:
896 	kfree_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("scan ID = %d vdev id = %d, event type %s(%d) reason = %s(%d)",
983 		   scan_id, event->vdev_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 	uint32_t extra_ie_len = 0;
1295 
1296 	psoc = wlan_pdev_get_psoc(pdev);
1297 	if (!psoc) {
1298 		osif_err("Invalid psoc object");
1299 		return -EINVAL;
1300 	}
1301 
1302 	/* Get NL global context from objmgr*/
1303 	osif_priv = wlan_pdev_get_ospriv(pdev);
1304 	if (!osif_priv) {
1305 		osif_err("Invalid osif priv object");
1306 		return -EINVAL;
1307 	}
1308 
1309 	/*
1310 	 * For a non-SAP vdevs, if a scan is already going on i.e the scan queue
1311 	 * is not empty, and the simultaneous scan is disabled, dont allow 2nd
1312 	 * scan.
1313 	 */
1314 	if (!wlan_cfg80211_allow_simultaneous_scan(psoc) &&
1315 	    !qdf_list_empty(&osif_priv->osif_scan->scan_req_q) &&
1316 	    wlan_vdev_mlme_get_opmode(vdev) != QDF_SAP_MODE) {
1317 		osif_err("Simultaneous scan disabled, reject scan");
1318 		return -EBUSY;
1319 	}
1320 
1321 	req = qdf_mem_malloc(sizeof(*req));
1322 	if (!req)
1323 		return -EINVAL;
1324 
1325 	/* Initialize the scan global params */
1326 	ucfg_scan_init_default_params(vdev, req);
1327 
1328 	req_id = osif_priv->osif_scan->req_id;
1329 	scan_id = ucfg_scan_get_scan_id(psoc);
1330 	if (!scan_id) {
1331 		osif_err("Invalid scan id");
1332 		qdf_mem_free(req);
1333 		return -EINVAL;
1334 	}
1335 
1336 	/* fill the scan request structure */
1337 	req->vdev = vdev;
1338 	req->scan_req.vdev_id = wlan_vdev_get_id(vdev);
1339 	req->scan_req.scan_id = scan_id;
1340 	req->scan_req.scan_req_id = req_id;
1341 
1342 	/* Enqueue the scan request */
1343 	ret = wlan_scan_request_enqueue(pdev, request, params->source,
1344 					req->scan_req.scan_id);
1345 	if (ret) {
1346 		qdf_mem_free(req);
1347 		return ret;
1348 	}
1349 
1350 	/* Update scan policy type flags according to cfg scan request */
1351 	wlan_cfg80211_update_scan_policy_type_flags(request,
1352 					     &req->scan_req);
1353 	/*
1354 	 * Even though supplicant doesn't provide any SSIDs, n_ssids is
1355 	 * set to 1.  Because of this, driver is assuming that this is not
1356 	 * wildcard scan and so is not aging out the scan results.
1357 	 */
1358 	if ((request->ssids) && (request->n_ssids == 1) &&
1359 	    ('\0' == request->ssids->ssid[0])) {
1360 		request->n_ssids = 0;
1361 	}
1362 
1363 	if ((request->ssids) && (0 < request->n_ssids)) {
1364 		int j;
1365 		req->scan_req.num_ssids = request->n_ssids;
1366 
1367 		if (req->scan_req.num_ssids > WLAN_SCAN_MAX_NUM_SSID) {
1368 			osif_info("number of ssid received %d is greater than MAX %d so copy only MAX nuber of SSIDs",
1369 				  req->scan_req.num_ssids,
1370 				  WLAN_SCAN_MAX_NUM_SSID);
1371 			req->scan_req.num_ssids = WLAN_SCAN_MAX_NUM_SSID;
1372 		}
1373 		/* copy all the ssid's and their length */
1374 		for (j = 0; j < req->scan_req.num_ssids; j++)  {
1375 			pssid = &req->scan_req.ssid[j];
1376 			/* get the ssid length */
1377 			pssid->length = request->ssids[j].ssid_len;
1378 			if (pssid->length > WLAN_SSID_MAX_LEN)
1379 				pssid->length = WLAN_SSID_MAX_LEN;
1380 			qdf_mem_copy(pssid->ssid,
1381 				     &request->ssids[j].ssid[0],
1382 				     pssid->length);
1383 			osif_info("SSID number %d: %.*s", j, pssid->length,
1384 				  pssid->ssid);
1385 		}
1386 	}
1387 	if (request->ssids ||
1388 	   (wlan_vdev_mlme_get_opmode(vdev) == QDF_P2P_GO_MODE))
1389 		req->scan_req.scan_f_passive = false;
1390 
1391 	if (params->half_rate)
1392 		req->scan_req.scan_f_half_rate = true;
1393 	else if (params->quarter_rate)
1394 		req->scan_req.scan_f_quarter_rate = true;
1395 
1396 	if (params->strict_pscan)
1397 		req->scan_req.scan_f_strict_passive_pch = true;
1398 
1399 	if ((request->n_ssids == 1) && request->ssids &&
1400 	   !qdf_mem_cmp(&request->ssids[0], "DIRECT-", 7))
1401 		is_p2p_scan = true;
1402 
1403 	if (is_p2p_scan && request->no_cck)
1404 		req->scan_req.scan_type = SCAN_TYPE_P2P_SEARCH;
1405 
1406 	/* Set dwell time mode according to scan policy type flags */
1407 	if (ucfg_scan_cfg_honour_nl_scan_policy_flags(psoc)) {
1408 		if (req->scan_req.scan_policy_high_accuracy)
1409 			req->scan_req.adaptive_dwell_time_mode =
1410 						SCAN_DWELL_MODE_STATIC;
1411 		if (req->scan_req.scan_policy_low_power ||
1412 		    req->scan_req.scan_policy_low_span)
1413 			req->scan_req.adaptive_dwell_time_mode =
1414 						SCAN_DWELL_MODE_AGGRESSIVE;
1415 	}
1416 
1417 	/*
1418 	 * FW require at least 1 MAC to send probe request.
1419 	 * If MAC is all 0 set it to BC addr as this is the address on
1420 	 * which fw will send probe req.
1421 	 */
1422 	req->scan_req.num_bssid = 1;
1423 	wlan_copy_bssid_scan_request(req, request);
1424 	if (qdf_is_macaddr_zero(&req->scan_req.bssid_list[0]))
1425 		qdf_set_macaddr_broadcast(&req->scan_req.bssid_list[0]);
1426 
1427 	if (request->n_channels) {
1428 		uint32_t buff_len = (request->n_channels * 5) + 1;
1429 		char *chl = qdf_mem_malloc(buff_len);
1430 		int len = 0;
1431 #ifdef WLAN_POLICY_MGR_ENABLE
1432 		bool ap_or_go_present =
1433 			policy_mgr_mode_specific_connection_count(
1434 			     psoc, PM_SAP_MODE, NULL) ||
1435 			     policy_mgr_mode_specific_connection_count(
1436 			     psoc, PM_P2P_GO_MODE, NULL);
1437 #endif
1438 		if (!chl) {
1439 			ret = -ENOMEM;
1440 			goto err;
1441 		}
1442 		for (i = 0; i < request->n_channels; i++) {
1443 			c_freq = request->channels[i]->center_freq;
1444 			if (wlan_reg_is_dsrc_freq(c_freq))
1445 				continue;
1446 #ifdef WLAN_POLICY_MGR_ENABLE
1447 			if (ap_or_go_present) {
1448 				bool ok;
1449 
1450 				qdf_status = policy_mgr_is_chan_ok_for_dnbs(
1451 							psoc, c_freq, &ok);
1452 
1453 				if (QDF_IS_STATUS_ERROR(qdf_status)) {
1454 					osif_err("DNBS check failed");
1455 					qdf_mem_free(chl);
1456 					chl = NULL;
1457 					ret = -EINVAL;
1458 					goto err;
1459 				}
1460 				if (!ok)
1461 					continue;
1462 			}
1463 #endif
1464 			len += snprintf(chl + len, buff_len - len, "%d ",
1465 					c_freq);
1466 			req->scan_req.chan_list.chan[num_chan].freq = c_freq;
1467 			band = util_scan_scm_freq_to_band(c_freq);
1468 			if (band == WLAN_BAND_2_4_GHZ)
1469 				req->scan_req.chan_list.chan[num_chan].phymode =
1470 					SCAN_PHY_MODE_11G;
1471 			else
1472 				req->scan_req.chan_list.chan[num_chan].phymode =
1473 					SCAN_PHY_MODE_11A;
1474 			num_chan++;
1475 			if (num_chan >= NUM_CHANNELS)
1476 				break;
1477 		}
1478 		osif_info("Channel-List: %s", chl);
1479 		qdf_mem_free(chl);
1480 		chl = NULL;
1481 		osif_info("No. of Scan Channels: %d", num_chan);
1482 	}
1483 	if (!num_chan) {
1484 		osif_err("Received zero non-dsrc channels");
1485 		ret = -EINVAL;
1486 		goto err;
1487 	}
1488 	req->scan_req.chan_list.num_chan = num_chan;
1489 
1490 	/* P2P increase the scan priority */
1491 	if (is_p2p_scan)
1492 		req->scan_req.scan_priority = SCAN_PRIORITY_HIGH;
1493 
1494 	if (params->priority != SCAN_PRIORITY_COUNT)
1495 		req->scan_req.scan_priority = params->priority;
1496 
1497 	if (request->ie_len)
1498 		extra_ie_len = request->ie_len;
1499 	else if (params->default_ie.ptr && params->default_ie.len)
1500 		extra_ie_len = params->default_ie.len;
1501 
1502 	if (params->vendor_ie.ptr && params->vendor_ie.len)
1503 		extra_ie_len += params->vendor_ie.len;
1504 
1505 	if (extra_ie_len) {
1506 		req->scan_req.extraie.ptr = qdf_mem_malloc(extra_ie_len);
1507 		if (!req->scan_req.extraie.ptr) {
1508 			ret = -ENOMEM;
1509 			goto err;
1510 		}
1511 	}
1512 
1513 	if (request->ie_len) {
1514 		req->scan_req.extraie.len = request->ie_len;
1515 		qdf_mem_copy(req->scan_req.extraie.ptr, request->ie,
1516 			     request->ie_len);
1517 	} else if (params->default_ie.ptr && params->default_ie.len) {
1518 		req->scan_req.extraie.len = params->default_ie.len;
1519 		qdf_mem_copy(req->scan_req.extraie.ptr, params->default_ie.ptr,
1520 			     params->default_ie.len);
1521 	}
1522 
1523 	if (params->vendor_ie.ptr && params->vendor_ie.len) {
1524 		qdf_mem_copy((req->scan_req.extraie.ptr +
1525 			      req->scan_req.extraie.len),
1526 			     params->vendor_ie.ptr, params->vendor_ie.len);
1527 
1528 		req->scan_req.extraie.len += params->vendor_ie.len;
1529 	}
1530 
1531 	if (!is_p2p_scan) {
1532 		if (req->scan_req.scan_random.randomize)
1533 			wlan_scan_rand_attrs(vdev, request, req);
1534 		if (ucfg_ie_whitelist_enabled(psoc, vdev) &&
1535 		    ucfg_copy_ie_whitelist_attrs(psoc,
1536 					&req->scan_req.ie_whitelist))
1537 			req->scan_req.scan_f_en_ie_whitelist_in_probe = true;
1538 	}
1539 
1540 	if (request->flags & NL80211_SCAN_FLAG_FLUSH)
1541 		ucfg_scan_flush_results(pdev, NULL);
1542 
1543 	/*
1544 	 * Acquire wakelock to handle the case where APP's send scan to connect.
1545 	 * If suspend is received during scan scan will be aborted and APP will
1546 	 * not get scan result and not connect. eg if PNO is implemented in
1547 	 * framework.
1548 	 */
1549 	wlan_scan_acquire_wake_lock_timeout(psoc,
1550 					&osif_priv->osif_scan->scan_wake_lock,
1551 					SCAN_WAKE_LOCK_SCAN_DURATION);
1552 
1553 	qdf_runtime_pm_prevent_suspend(
1554 		&osif_priv->osif_scan->runtime_pm_lock);
1555 
1556 	qdf_status = ucfg_scan_start(req);
1557 	if (QDF_IS_STATUS_ERROR(qdf_status)) {
1558 		osif_err("ucfg_scan_start returned error %d", qdf_status);
1559 		if (qdf_status == QDF_STATUS_E_RESOURCES)
1560 			osif_err("HO is in progress.So defer the scan by informing busy");
1561 		wlan_scan_request_dequeue(pdev, scan_id, &request,
1562 					  &params->source, &netdev);
1563 		if (qdf_list_empty(&osif_priv->osif_scan->scan_req_q)) {
1564 			qdf_runtime_pm_allow_suspend(
1565 				&osif_priv->osif_scan->runtime_pm_lock);
1566 			wlan_scan_release_wake_lock(psoc,
1567 				&osif_priv->osif_scan->scan_wake_lock);
1568 		}
1569 	}
1570 
1571 	return qdf_status_to_os_return(qdf_status);
1572 
1573 err:
1574 	qdf_mem_free(req);
1575 	wlan_scan_request_dequeue(pdev, scan_id, &request,
1576 				  &params->source, &netdev);
1577 	return ret;
1578 }
1579 
1580 /**
1581  * wlan_get_scanid() - API to get the scan id
1582  * from the scan cookie attribute.
1583  * @pdev: Pointer to pdev object
1584  * @scan_id: Pointer to scan id
1585  * @cookie : Scan cookie attribute
1586  *
1587  * API to get the scan id from the scan cookie attribute
1588  * sent from supplicant by matching scan request.
1589  *
1590  * Return: 0 for success, non zero for failure
1591  */
1592 static int wlan_get_scanid(struct wlan_objmgr_pdev *pdev,
1593 			       uint32_t *scan_id, uint64_t cookie)
1594 {
1595 	struct scan_req *scan_req;
1596 	qdf_list_node_t *node = NULL;
1597 	qdf_list_node_t *ptr_node = NULL;
1598 	int ret = -EINVAL;
1599 	struct pdev_osif_priv *osif_ctx;
1600 	struct osif_scan_pdev *scan_priv;
1601 
1602 	/* Get NL global context from objmgr*/
1603 	osif_ctx = wlan_pdev_get_ospriv(pdev);
1604 	if (!osif_ctx) {
1605 		osif_err("Failed to retrieve osif context");
1606 		return ret;
1607 	}
1608 	scan_priv = osif_ctx->osif_scan;
1609 	qdf_mutex_acquire(&scan_priv->scan_req_q_lock);
1610 	if (qdf_list_empty(&scan_priv->scan_req_q)) {
1611 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
1612 		osif_err("Failed to retrieve scan id");
1613 		return ret;
1614 	}
1615 
1616 	if (QDF_STATUS_SUCCESS !=
1617 			    qdf_list_peek_front(&scan_priv->scan_req_q,
1618 			    &ptr_node)) {
1619 		qdf_mutex_release(&scan_priv->scan_req_q_lock);
1620 		return ret;
1621 	}
1622 
1623 	do {
1624 		node = ptr_node;
1625 		scan_req = qdf_container_of(node, struct scan_req, node);
1626 		if (cookie ==
1627 		    (uintptr_t)(scan_req->scan_request)) {
1628 			*scan_id = scan_req->scan_id;
1629 			ret = 0;
1630 			break;
1631 		}
1632 	} while (QDF_STATUS_SUCCESS ==
1633 		 qdf_list_peek_next(&scan_priv->scan_req_q,
1634 		 node, &ptr_node));
1635 
1636 	qdf_mutex_release(&scan_priv->scan_req_q_lock);
1637 
1638 	return ret;
1639 }
1640 
1641 QDF_STATUS wlan_abort_scan(struct wlan_objmgr_pdev *pdev,
1642 				   uint32_t pdev_id, uint32_t vdev_id,
1643 				   wlan_scan_id scan_id, bool sync)
1644 {
1645 	struct scan_cancel_request *req;
1646 	struct pdev_osif_priv *osif_ctx;
1647 	struct osif_scan_pdev *scan_priv;
1648 	QDF_STATUS status;
1649 	struct wlan_objmgr_vdev *vdev;
1650 
1651 	req = qdf_mem_malloc(sizeof(*req));
1652 	if (!req)
1653 		return QDF_STATUS_E_NOMEM;
1654 
1655 	/* Get NL global context from objmgr*/
1656 	osif_ctx = wlan_pdev_get_ospriv(pdev);
1657 	if (!osif_ctx) {
1658 		osif_err("Failed to retrieve osif context");
1659 		qdf_mem_free(req);
1660 		return QDF_STATUS_E_FAILURE;
1661 	}
1662 	if (vdev_id == INVAL_VDEV_ID)
1663 		vdev = wlan_objmgr_pdev_get_first_vdev(pdev, WLAN_OSIF_ID);
1664 	else
1665 		vdev = wlan_objmgr_get_vdev_by_id_from_pdev(pdev,
1666 				vdev_id, WLAN_OSIF_ID);
1667 
1668 	if (!vdev) {
1669 		osif_err("Failed get vdev");
1670 		qdf_mem_free(req);
1671 		return QDF_STATUS_E_INVAL;
1672 	}
1673 	scan_priv = osif_ctx->osif_scan;
1674 	req->cancel_req.requester = scan_priv->req_id;
1675 	req->vdev = vdev;
1676 	req->cancel_req.scan_id = scan_id;
1677 	req->cancel_req.pdev_id = pdev_id;
1678 	req->cancel_req.vdev_id = vdev_id;
1679 	if (scan_id != INVAL_SCAN_ID)
1680 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_SINGLE;
1681 	else if (vdev_id == INVAL_VDEV_ID)
1682 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_PDEV_ALL;
1683 	else
1684 		req->cancel_req.req_type = WLAN_SCAN_CANCEL_VDEV_ALL;
1685 
1686 	if (sync)
1687 		status = ucfg_scan_cancel_sync(req);
1688 	else
1689 		status = ucfg_scan_cancel(req);
1690 	if (QDF_IS_STATUS_ERROR(status))
1691 		osif_err("Cancel scan request failed");
1692 
1693 	wlan_objmgr_vdev_release_ref(vdev, WLAN_OSIF_ID);
1694 
1695 	return status;
1696 }
1697 
1698 int wlan_cfg80211_abort_scan(struct wlan_objmgr_pdev *pdev)
1699 {
1700 	uint8_t pdev_id;
1701 
1702 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1703 
1704 	if (ucfg_scan_get_pdev_status(pdev) !=
1705 	   SCAN_NOT_IN_PROGRESS)
1706 		wlan_abort_scan(pdev, pdev_id,
1707 			INVAL_VDEV_ID, INVAL_SCAN_ID, true);
1708 
1709 	return 0;
1710 }
1711 
1712 int wlan_vendor_abort_scan(struct wlan_objmgr_pdev *pdev,
1713 			const void *data, int data_len)
1714 {
1715 	struct nlattr *tb[QCA_WLAN_VENDOR_ATTR_SCAN_MAX + 1];
1716 	int ret = -EINVAL;
1717 	wlan_scan_id scan_id;
1718 	uint64_t cookie;
1719 	uint8_t pdev_id;
1720 
1721 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1722 	if (wlan_cfg80211_nla_parse(tb, QCA_WLAN_VENDOR_ATTR_SCAN_MAX, data,
1723 				    data_len, scan_policy)) {
1724 		osif_err("Invalid ATTR");
1725 		return ret;
1726 	}
1727 
1728 	if (tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]) {
1729 		cookie = nla_get_u64(
1730 			    tb[QCA_WLAN_VENDOR_ATTR_SCAN_COOKIE]);
1731 		ret = wlan_get_scanid(pdev, &scan_id, cookie);
1732 		if (ret != 0)
1733 			return ret;
1734 		if (ucfg_scan_get_pdev_status(pdev) !=
1735 		   SCAN_NOT_IN_PROGRESS)
1736 			wlan_abort_scan(pdev, INVAL_PDEV_ID,
1737 					INVAL_VDEV_ID, scan_id, true);
1738 	}
1739 	return 0;
1740 }
1741 
1742 static inline struct ieee80211_channel *
1743 wlan_get_ieee80211_channel(struct wiphy *wiphy,
1744 		struct wlan_objmgr_pdev *pdev,
1745 		int chan_freq)
1746 {
1747 	struct ieee80211_channel *chan;
1748 
1749 	chan = ieee80211_get_channel(wiphy, chan_freq);
1750 	if (!chan)
1751 		osif_err("chan is NULL, freq: %d", chan_freq);
1752 
1753 	return chan;
1754 }
1755 
1756 #ifdef WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS
1757 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params)
1758 {
1759 	return util_scan_entry_frame_len(scan_params) + sizeof(qcom_ie_age);
1760 }
1761 
1762 static inline void wlan_add_age_ie(uint8_t *mgmt_frame,
1763 	struct scan_cache_entry *scan_params)
1764 {
1765 	qcom_ie_age *qie_age = NULL;
1766 
1767 	/* GPS Requirement: need age ie per entry. Using vendor specific. */
1768 	/* Assuming this is the last IE, copy at the end */
1769 	qie_age = (qcom_ie_age *) (mgmt_frame +
1770 		   util_scan_entry_frame_len(scan_params));
1771 	qie_age->element_id = QCOM_VENDOR_IE_ID;
1772 	qie_age->len = QCOM_VENDOR_IE_AGE_LEN;
1773 	qie_age->oui_1 = QCOM_OUI1;
1774 	qie_age->oui_2 = QCOM_OUI2;
1775 	qie_age->oui_3 = QCOM_OUI3;
1776 	qie_age->type = QCOM_VENDOR_IE_AGE_TYPE;
1777 	/*
1778 	 * Lowi expects the timestamp of bss in units of 1/10 ms. In driver
1779 	 * all bss related timestamp is in units of ms. Due to this when scan
1780 	 * results are sent to lowi the scan age is high.To address this,
1781 	 * send age in units of 1/10 ms.
1782 	 */
1783 	qie_age->age =
1784 		(uint32_t)(qdf_mc_timer_get_system_time() -
1785 		  scan_params->scan_entry_time)/10;
1786 	qie_age->tsf_delta = scan_params->tsf_delta;
1787 	memcpy(&qie_age->beacon_tsf, scan_params->tsf_info.data,
1788 		  sizeof(qie_age->beacon_tsf));
1789 	memcpy(&qie_age->seq_ctrl, &scan_params->seq_num,
1790 	       sizeof(qie_age->seq_ctrl));
1791 }
1792 #else
1793 static inline int wlan_get_frame_len(struct scan_cache_entry *scan_params)
1794 {
1795 	return util_scan_entry_frame_len(scan_params);
1796 }
1797 
1798 static inline void wlan_add_age_ie(uint8_t *mgmt_frame,
1799 	struct scan_cache_entry *scan_params)
1800 {
1801 }
1802 #endif /* WLAN_ENABLE_AGEIE_ON_SCAN_RESULTS */
1803 
1804 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 4, 0)) || \
1805 	defined(CFG80211_INFORM_BSS_FRAME_DATA)
1806 /**
1807  * wlan_fill_per_chain_rssi() - fill per chain RSSI in inform bss
1808  * @data: bss data
1809  * @per_chain_snr: per chain RSSI
1810  *
1811  * Return: void
1812  */
1813 #if defined(CFG80211_SCAN_PER_CHAIN_RSSI_SUPPORT) || \
1814 	   (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 16, 0))
1815 static void wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data,
1816 	struct wlan_cfg80211_inform_bss *bss)
1817 {
1818 
1819 	uint32_t i;
1820 
1821 	if (!bss || !data) {
1822 		osif_err("Received bss is NULL");
1823 		return;
1824 	}
1825 	for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) {
1826 		if (!bss->per_chain_rssi[i] ||
1827 		    (bss->per_chain_rssi[i] == WLAN_INVALID_PER_CHAIN_RSSI))
1828 			continue;
1829 		data->chain_signal[i] = bss->per_chain_rssi[i];
1830 		data->chains |= BIT(i);
1831 	}
1832 }
1833 #else
1834 static inline void
1835 wlan_fill_per_chain_rssi(struct cfg80211_inform_bss *data,
1836 	struct wlan_cfg80211_inform_bss *bss)
1837 {
1838 }
1839 #endif
1840 
1841 struct cfg80211_bss *
1842 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1843 		struct wlan_cfg80211_inform_bss *bss)
1844 {
1845 	struct cfg80211_inform_bss data  = {0};
1846 
1847 	if (!bss) {
1848 		osif_err("bss is null");
1849 		return NULL;
1850 	}
1851 	wlan_fill_per_chain_rssi(&data, bss);
1852 
1853 	data.chan = bss->chan;
1854 	data.boottime_ns = bss->boottime_ns;
1855 	data.signal = bss->rssi;
1856 	return cfg80211_inform_bss_frame_data(wiphy, &data, bss->mgmt,
1857 					      bss->frame_len, GFP_ATOMIC);
1858 }
1859 #else
1860 struct cfg80211_bss *
1861 wlan_cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
1862 		struct wlan_cfg80211_inform_bss *bss)
1863 
1864 {
1865 	return cfg80211_inform_bss_frame(wiphy, bss->chan, bss->mgmt,
1866 					 bss->frame_len,
1867 					 bss->rssi, GFP_ATOMIC);
1868 }
1869 #endif
1870 
1871 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0))
1872 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy,
1873 		struct cfg80211_bss *bss)
1874 {
1875 	cfg80211_put_bss(wiphy, bss);
1876 }
1877 #else
1878 static inline void wlan_cfg80211_put_bss(struct wiphy *wiphy,
1879 		struct cfg80211_bss *bss)
1880 {
1881 	cfg80211_put_bss(bss);
1882 }
1883 #endif
1884 
1885 void wlan_cfg80211_inform_bss_frame(struct wlan_objmgr_pdev *pdev,
1886 		struct scan_cache_entry *scan_params)
1887 {
1888 	struct pdev_osif_priv *pdev_ospriv = wlan_pdev_get_ospriv(pdev);
1889 	struct wiphy *wiphy;
1890 	struct cfg80211_bss *bss = NULL;
1891 	struct wlan_cfg80211_inform_bss bss_data = {0};
1892 
1893 	if (!pdev_ospriv) {
1894 		osif_err("os_priv is NULL");
1895 		return;
1896 	}
1897 
1898 	wiphy = pdev_ospriv->wiphy;
1899 
1900 	bss_data.frame_len = wlan_get_frame_len(scan_params);
1901 	bss_data.mgmt = qdf_mem_malloc_atomic(bss_data.frame_len);
1902 	if (!bss_data.mgmt) {
1903 		osif_err("mem alloc failed for bss %pM seq %d",
1904 			 bss_data.mgmt->bssid, scan_params->seq_num);
1905 		return;
1906 	}
1907 	qdf_mem_copy(bss_data.mgmt,
1908 		 util_scan_entry_frame_ptr(scan_params),
1909 		 util_scan_entry_frame_len(scan_params));
1910 	/*
1911 	 * Android does not want the timestamp from the frame.
1912 	 * Instead it wants a monotonic increasing value
1913 	 */
1914 	bss_data.mgmt->u.probe_resp.timestamp = qdf_get_monotonic_boottime();
1915 	wlan_add_age_ie((uint8_t *)bss_data.mgmt, scan_params);
1916 	/*
1917 	 * Based on .ini configuration, raw rssi can be reported for bss.
1918 	 * Raw rssi is typically used for estimating power.
1919 	 */
1920 	bss_data.rssi = scan_params->rssi_raw;
1921 
1922 	bss_data.chan = wlan_get_ieee80211_channel(wiphy, pdev,
1923 		scan_params->channel.chan_freq);
1924 	if (!bss_data.chan) {
1925 		osif_err("Channel not found for bss %pM seq %d chan_freq %d",
1926 			 bss_data.mgmt->bssid, scan_params->seq_num,
1927 			 scan_params->channel.chan_freq);
1928 		qdf_mem_free(bss_data.mgmt);
1929 		return;
1930 	}
1931 
1932 	/*
1933 	 * Supplicant takes the signal strength in terms of
1934 	 * mBm (1 dBm = 100 mBm).
1935 	 */
1936 	bss_data.rssi = QDF_MIN(bss_data.rssi, 0) * 100;
1937 
1938 	bss_data.boottime_ns = scan_params->boottime_ns;
1939 
1940 	qdf_mem_copy(bss_data.per_chain_rssi, scan_params->per_chain_rssi,
1941 		     WLAN_MGMT_TXRX_HOST_MAX_ANTENNA);
1942 
1943 	bss = wlan_cfg80211_inform_bss_frame_data(wiphy, &bss_data);
1944 	if (!bss)
1945 		osif_err("failed to inform bss %pM seq %d",
1946 			 bss_data.mgmt->bssid, scan_params->seq_num);
1947 	else
1948 		wlan_cfg80211_put_bss(wiphy, bss);
1949 
1950 	qdf_mem_free(bss_data.mgmt);
1951 }
1952 
1953 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)) && \
1954 	!defined(WITH_BACKPORTS) && !defined(IEEE80211_PRIVACY)
1955 struct cfg80211_bss *wlan_cfg80211_get_bss(struct wiphy *wiphy,
1956 					   struct ieee80211_channel *channel,
1957 					   const u8 *bssid, const u8 *ssid,
1958 					   size_t ssid_len)
1959 {
1960 	return cfg80211_get_bss(wiphy, channel, bssid,
1961 				ssid, ssid_len,
1962 				WLAN_CAPABILITY_ESS,
1963 				WLAN_CAPABILITY_ESS);
1964 }
1965 #else
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 				IEEE80211_BSS_TYPE_ESS,
1974 				IEEE80211_PRIVACY_ANY);
1975 }
1976 #endif
1977 
1978 void __wlan_cfg80211_unlink_bss_list(struct wiphy *wiphy, uint8_t *bssid,
1979 				     uint8_t *ssid, uint8_t ssid_len)
1980 {
1981 	struct cfg80211_bss *bss = NULL;
1982 
1983 	bss = wlan_cfg80211_get_bss(wiphy, NULL, bssid,
1984 				    ssid, ssid_len);
1985 	if (!bss) {
1986 		osif_info("BSS %pM not found", bssid);
1987 	} else {
1988 		osif_debug("unlink entry for ssid:%.*s and BSSID %pM",
1989 			   ssid_len, ssid, bssid);
1990 		cfg80211_unlink_bss(wiphy, bss);
1991 		wlan_cfg80211_put_bss(wiphy, bss);
1992 	}
1993 
1994 	/*
1995 	 * Kernel creates separate entries into it's bss list for probe resp
1996 	 * and beacon for hidden AP. Both have separate ref count and thus
1997 	 * deleting one will not delete other entry.
1998 	 * If beacon entry of the hidden AP is not deleted and AP switch to
1999 	 * broadcasting SSID from Hiding SSID, kernel will reject the beacon
2000 	 * entry. So unlink the hidden beacon entry (if present) as well from
2001 	 * kernel, to avoid such issue.
2002 	 */
2003 	bss = wlan_cfg80211_get_bss(wiphy, NULL, bssid, NULL, 0);
2004 	if (!bss) {
2005 		osif_debug("Hidden bss not found for Ssid:%.*s BSSID: %pM sid_len %d",
2006 			   ssid_len, ssid, bssid, ssid_len);
2007 	} else {
2008 		osif_debug("unlink entry for Hidden ssid:%.*s and BSSID %pM",
2009 			   ssid_len, ssid, bssid);
2010 
2011 		cfg80211_unlink_bss(wiphy, bss);
2012 		/* cfg80211_get_bss get bss with ref count so release it */
2013 		wlan_cfg80211_put_bss(wiphy, bss);
2014 	}
2015 }
2016 void wlan_cfg80211_unlink_bss_list(struct wlan_objmgr_pdev *pdev,
2017 				   struct scan_cache_entry *scan_entry)
2018 {
2019 	struct pdev_osif_priv *pdev_ospriv = wlan_pdev_get_ospriv(pdev);
2020 	struct wiphy *wiphy;
2021 
2022 	if (!pdev_ospriv) {
2023 		osif_err("os_priv is NULL");
2024 		return;
2025 	}
2026 
2027 	wiphy = pdev_ospriv->wiphy;
2028 
2029 	__wlan_cfg80211_unlink_bss_list(wiphy, scan_entry->bssid.bytes,
2030 					scan_entry->ssid.ssid,
2031 					scan_entry->ssid.length);
2032 }
2033 
2034 #if (LINUX_VERSION_CODE < KERNEL_VERSION(4, 12, 0))
2035 /*
2036  * wlan_scan_wiphy_set_max_sched_scans() - set maximum number of scheduled scans
2037  * to wiphy.
2038  * @wiphy: pointer to wiphy
2039  * @max_scans: max num scans to be configured
2040  *
2041  */
2042 static inline void
2043 wlan_scan_wiphy_set_max_sched_scans(struct wiphy *wiphy, uint8_t max_scans)
2044 {
2045 	if (max_scans == 0)
2046 		wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
2047 	else
2048 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_SCHED_SCAN;
2049 }
2050 #else
2051 static inline void
2052 wlan_scan_wiphy_set_max_sched_scans(struct wiphy *wiphy, uint8_t max_scans)
2053 {
2054 	wiphy->max_sched_scan_reqs = max_scans;
2055 }
2056 #endif /* KERNEL_VERSION(4, 12, 0) */
2057 
2058 #if defined(CFG80211_REPORT_BETTER_BSS_IN_SCHED_SCAN) || \
2059 	(LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0))
2060 void wlan_scan_cfg80211_add_connected_pno_support(struct wiphy *wiphy)
2061 {
2062 	wiphy_ext_feature_set(wiphy,
2063 			      NL80211_EXT_FEATURE_SCHED_SCAN_RELATIVE_RSSI);
2064 }
2065 #endif
2066 
2067 #if ((LINUX_VERSION_CODE > KERNEL_VERSION(4, 4, 0)) || \
2068 		defined(CFG80211_MULTI_SCAN_PLAN_BACKPORT)) && \
2069 		defined(FEATURE_WLAN_SCAN_PNO)
2070 void wlan_config_sched_scan_plans_to_wiphy(struct wiphy *wiphy,
2071 					   struct wlan_objmgr_psoc *psoc)
2072 {
2073 	if (ucfg_scan_get_pno_scan_support(psoc)) {
2074 		wlan_scan_wiphy_set_max_sched_scans(wiphy, 1);
2075 		wiphy->max_sched_scan_ssids = SCAN_PNO_MAX_SUPP_NETWORKS;
2076 		wiphy->max_match_sets = SCAN_PNO_MAX_SUPP_NETWORKS;
2077 		wiphy->max_sched_scan_ie_len = SCAN_MAX_IE_LENGTH;
2078 		wiphy->max_sched_scan_plans = SCAN_PNO_MAX_PLAN_REQUEST;
2079 
2080 		wiphy->max_sched_scan_plan_interval =
2081 			ucfg_scan_get_max_sched_scan_plan_interval(psoc);
2082 
2083 		wiphy->max_sched_scan_plan_iterations =
2084 			ucfg_scan_get_max_sched_scan_plan_iterations(psoc);
2085 	}
2086 }
2087 #endif
2088