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