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