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