xref: /wlan-dirver/qca-wifi-host-cmn/umac/scan/dispatcher/src/wlan_scan_utils_api.c (revision 45a38684b07295822dc8eba39e293408f203eec8)
1 /*
2  * Copyright (c) 2017-2020 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 /*
20  * DOC: Defines scan utility functions
21  */
22 
23 #include <wlan_cmn.h>
24 #include <wlan_scan_ucfg_api.h>
25 #include <wlan_scan_utils_api.h>
26 #include <../../core/src/wlan_scan_cache_db.h>
27 #include <../../core/src/wlan_scan_main.h>
28 #include <wlan_reg_services_api.h>
29 
30 #define MAX_IE_LEN 1024
31 #define SHORT_SSID_LEN 4
32 #define NEIGHBOR_AP_LEN 1
33 #define BSS_PARAMS_LEN 1
34 
35 const char*
36 util_scan_get_ev_type_name(enum scan_event_type type)
37 {
38 	static const char * const event_name[] = {
39 		[SCAN_EVENT_TYPE_STARTED] = "STARTED",
40 		[SCAN_EVENT_TYPE_COMPLETED] = "COMPLETED",
41 		[SCAN_EVENT_TYPE_BSS_CHANNEL] = "HOME_CHANNEL",
42 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL] = "FOREIGN_CHANNEL",
43 		[SCAN_EVENT_TYPE_DEQUEUED] = "DEQUEUED",
44 		[SCAN_EVENT_TYPE_PREEMPTED] = "PREEMPTED",
45 		[SCAN_EVENT_TYPE_START_FAILED] = "START_FAILED",
46 		[SCAN_EVENT_TYPE_RESTARTED] = "RESTARTED",
47 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_EXIT] = "FOREIGN_CHANNEL_EXIT",
48 		[SCAN_EVENT_TYPE_SUSPENDED] = "SUSPENDED",
49 		[SCAN_EVENT_TYPE_RESUMED] = "RESUMED",
50 		[SCAN_EVENT_TYPE_NLO_COMPLETE] = "NLO_COMPLETE",
51 		[SCAN_EVENT_TYPE_NLO_MATCH] = "NLO_MATCH",
52 		[SCAN_EVENT_TYPE_INVALID] = "INVALID",
53 		[SCAN_EVENT_TYPE_GPIO_TIMEOUT] = "GPIO_TIMEOUT",
54 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_START] =
55 			"RADIO_MEASUREMENT_START",
56 		[SCAN_EVENT_TYPE_RADIO_MEASUREMENT_END] =
57 			"RADIO_MEASUREMENT_END",
58 		[SCAN_EVENT_TYPE_BSSID_MATCH] = "BSSID_MATCH",
59 		[SCAN_EVENT_TYPE_FOREIGN_CHANNEL_GET_NF] =
60 			"FOREIGN_CHANNEL_GET_NF",
61 	};
62 
63 	if (type >= SCAN_EVENT_TYPE_MAX)
64 		return "UNKNOWN";
65 
66 	return event_name[type];
67 }
68 
69 
70 const char*
71 util_scan_get_ev_reason_name(enum scan_completion_reason reason)
72 {
73 	static const char * const reason_name[] = {
74 		[SCAN_REASON_NONE] = "NONE",
75 		[SCAN_REASON_COMPLETED] = "COMPLETED",
76 		[SCAN_REASON_CANCELLED] = "CANCELLED",
77 		[SCAN_REASON_PREEMPTED] = "PREEMPTED",
78 		[SCAN_REASON_TIMEDOUT] = "TIMEDOUT",
79 		[SCAN_REASON_INTERNAL_FAILURE] = "INTERNAL_FAILURE",
80 		[SCAN_REASON_SUSPENDED] = "SUSPENDED",
81 		[SCAN_REASON_RUN_FAILED] = "RUN_FAILED",
82 		[SCAN_REASON_TERMINATION_FUNCTION] = "TERMINATION_FUNCTION",
83 		[SCAN_REASON_MAX_OFFCHAN_RETRIES] = "MAX_OFFCHAN_RETRIES",
84 		[SCAN_REASON_DFS_VIOLATION] = "DFS_NOL_VIOLATION",
85 	};
86 
87 	if (reason >= SCAN_REASON_MAX)
88 		return "UNKNOWN";
89 
90 	return reason_name[reason];
91 }
92 
93 qdf_time_t
94 util_get_last_scan_time(struct wlan_objmgr_vdev *vdev)
95 {
96 	uint8_t pdev_id;
97 	struct wlan_scan_obj *scan_obj;
98 
99 	if (!vdev) {
100 		scm_warn("null vdev");
101 		QDF_ASSERT(0);
102 		return 0;
103 	}
104 	pdev_id = wlan_scan_vdev_get_pdev_id(vdev);
105 	scan_obj = wlan_vdev_get_scan_obj(vdev);
106 
107 	if (scan_obj)
108 		return scan_obj->pdev_info[pdev_id].last_scan_time;
109 	else
110 		return 0;
111 }
112 
113 enum wlan_band util_scan_scm_chan_to_band(uint32_t chan)
114 {
115 	if (WLAN_CHAN_IS_2GHZ(chan))
116 		return WLAN_BAND_2_4_GHZ;
117 
118 	return WLAN_BAND_5_GHZ;
119 }
120 
121 enum wlan_band util_scan_scm_freq_to_band(uint16_t freq)
122 {
123 	if (WLAN_REG_IS_24GHZ_CH_FREQ(freq))
124 		return WLAN_BAND_2_4_GHZ;
125 
126 	return WLAN_BAND_5_GHZ;
127 }
128 
129 bool util_is_scan_entry_match(
130 	struct scan_cache_entry *entry1,
131 	struct scan_cache_entry *entry2)
132 {
133 
134 	if (entry1->cap_info.wlan_caps.ess !=
135 	   entry2->cap_info.wlan_caps.ess)
136 		return false;
137 
138 	if (entry1->cap_info.wlan_caps.ess &&
139 	   !qdf_mem_cmp(entry1->bssid.bytes,
140 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
141 		/* Check for BSS */
142 		if (util_is_ssid_match(&entry1->ssid, &entry2->ssid) ||
143 		    util_scan_is_null_ssid(&entry1->ssid) ||
144 		    util_scan_is_null_ssid(&entry2->ssid))
145 			return true;
146 	} else if (entry1->cap_info.wlan_caps.ibss &&
147 	   (entry1->channel.chan_freq ==
148 	   entry2->channel.chan_freq)) {
149 		/*
150 		 * Same channel cannot have same SSID for
151 		 * different IBSS, so no need to check BSSID
152 		 */
153 		if (util_is_ssid_match(
154 		   &entry1->ssid, &entry2->ssid))
155 			return true;
156 	} else if (!entry1->cap_info.wlan_caps.ibss &&
157 	   !entry1->cap_info.wlan_caps.ess &&
158 	   !qdf_mem_cmp(entry1->bssid.bytes,
159 	   entry2->bssid.bytes, QDF_MAC_ADDR_SIZE)) {
160 		/* In case of P2P devices, ess and ibss will be set to zero */
161 		return true;
162 	}
163 
164 	return false;
165 }
166 
167 static bool util_is_pureg_rate(uint8_t *rates, uint8_t nrates)
168 {
169 	static const uint8_t g_rates[] = {12, 18, 24, 36, 48, 72, 96, 108};
170 	bool pureg = false;
171 	uint8_t i, j;
172 
173 	for (i = 0; i < nrates; i++) {
174 		for (j = 0; j < QDF_ARRAY_SIZE(g_rates); j++) {
175 			if (WLAN_RV(rates[i]) == g_rates[j]) {
176 				pureg = true;
177 				break;
178 			}
179 		}
180 		if (pureg)
181 			break;
182 	}
183 
184 	return pureg;
185 }
186 
187 #ifdef CONFIG_BAND_6GHZ
188 static struct he_oper_6g_param *util_scan_get_he_6g_params(uint8_t *he_ops)
189 {
190 	uint8_t len;
191 	uint32_t he_oper_params;
192 
193 	if (!he_ops)
194 		return NULL;
195 
196 	len = he_ops[1];
197 	he_ops += sizeof(struct ie_header);
198 
199 	if (len < WLAN_HEOP_FIXED_PARAM_LENGTH)
200 		return NULL;
201 
202 	/* element id extension */
203 	he_ops++;
204 	len--;
205 
206 	he_oper_params = LE_READ_4(he_ops);
207 	if (!(he_oper_params & WLAN_HEOP_6GHZ_INFO_PRESENT_MASK))
208 		return NULL;
209 
210 	/* fixed params - element id extension */
211 	he_ops += WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
212 	len -= WLAN_HEOP_FIXED_PARAM_LENGTH - 1;
213 
214 	if (!len)
215 		return NULL;
216 
217 	/* vht oper params */
218 	if (he_oper_params & WLAN_HEOP_VHTOP_PRESENT_MASK) {
219 		if (len < WLAN_HEOP_VHTOP_LENGTH)
220 			return NULL;
221 		he_ops += WLAN_HEOP_VHTOP_LENGTH;
222 		len -= WLAN_HEOP_VHTOP_LENGTH;
223 	}
224 
225 	if (!len)
226 		return NULL;
227 
228 	if (he_oper_params & WLAN_HEOP_CO_LOCATED_BSS_MASK) {
229 		he_ops += WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
230 		len -= WLAN_HEOP_CO_LOCATED_BSS_LENGTH;
231 	}
232 
233 	if (len < sizeof(struct he_oper_6g_param))
234 		return NULL;
235 
236 	return (struct he_oper_6g_param *)he_ops;
237 }
238 
239 static QDF_STATUS
240 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
241 				     struct scan_cache_entry *scan_params,
242 				     qdf_freq_t *chan_freq,
243 				     bool *he_6g_dup_bcon, uint8_t band_mask)
244 {
245 	struct he_oper_6g_param *he_6g_params;
246 	uint8_t *he_ops;
247 	struct wlan_scan_obj *scan_obj;
248 	struct wlan_objmgr_psoc *psoc;
249 
250 	psoc = wlan_pdev_get_psoc(pdev);
251 	if (!psoc) {
252 		scm_err("psoc is NULL");
253 		return QDF_STATUS_E_INVAL;
254 	}
255 
256 	scan_obj = wlan_psoc_get_scan_obj(psoc);
257 	if (!scan_obj) {
258 		scm_err("scan_obj is NULL");
259 		return QDF_STATUS_E_INVAL;
260 	}
261 
262 	*he_6g_dup_bcon = false;
263 
264 	he_ops = util_scan_entry_heop(scan_params);
265 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
266 		return QDF_STATUS_SUCCESS;
267 
268 	he_6g_params = util_scan_get_he_6g_params(he_ops);
269 	if (!he_6g_params)
270 		return QDF_STATUS_SUCCESS;
271 
272 	*chan_freq = wlan_reg_chan_band_to_freq(pdev,
273 						he_6g_params->primary_channel,
274 						band_mask);
275 	if (scan_obj->drop_bcn_on_invalid_freq &&
276 	    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
277 		scm_debug_rl("%pM: Drop as invalid channel %d freq %d in HE 6Ghz params",
278 			     scan_params->bssid.bytes,
279 			     he_6g_params->primary_channel, *chan_freq);
280 		return QDF_STATUS_E_INVAL;
281 	}
282 	*he_6g_dup_bcon = he_6g_params->duplicate_beacon ? true : false;
283 
284 	return QDF_STATUS_SUCCESS;
285 }
286 
287 static enum wlan_phymode
288 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
289 			 struct scan_cache_entry *scan_params)
290 {
291 	struct he_oper_6g_param *he_6g_params;
292 	enum wlan_phymode phymode = WLAN_PHYMODE_11AXA_HE20;
293 	uint8_t *he_ops;
294 	uint8_t band_mask = BIT(REG_BAND_6G);
295 
296 	he_ops = util_scan_entry_heop(scan_params);
297 	if (!util_scan_entry_hecap(scan_params) || !he_ops)
298 		return phymode;
299 
300 	he_6g_params = util_scan_get_he_6g_params(he_ops);
301 	if (!he_6g_params)
302 		return phymode;
303 
304 	switch (he_6g_params->width) {
305 	case WLAN_HE_6GHZ_CHWIDTH_20:
306 		phymode = WLAN_PHYMODE_11AXA_HE20;
307 		break;
308 	case WLAN_HE_6GHZ_CHWIDTH_40:
309 		phymode = WLAN_PHYMODE_11AXA_HE40;
310 		break;
311 	case WLAN_HE_6GHZ_CHWIDTH_80:
312 		phymode = WLAN_PHYMODE_11AXA_HE80;
313 		break;
314 	case WLAN_HE_6GHZ_CHWIDTH_160_80_80:
315 		if (WLAN_IS_HE80_80(he_6g_params))
316 			phymode = WLAN_PHYMODE_11AXA_HE80_80;
317 		else if (WLAN_IS_HE160(he_6g_params))
318 			phymode = WLAN_PHYMODE_11AXA_HE160;
319 		else
320 			phymode = WLAN_PHYMODE_11AXA_HE80;
321 		break;
322 	default:
323 		scm_err("Invalid he_6g_params width: %d", he_6g_params->width);
324 		phymode = WLAN_PHYMODE_11AXA_HE20;
325 		break;
326 	}
327 
328 	if (he_6g_params->chan_freq_seg0)
329 		scan_params->channel.cfreq0 =
330 			wlan_reg_chan_band_to_freq(pdev,
331 					he_6g_params->chan_freq_seg0,
332 					band_mask);
333 	if (he_6g_params->chan_freq_seg1)
334 		scan_params->channel.cfreq1 =
335 			wlan_reg_chan_band_to_freq(pdev,
336 					he_6g_params->chan_freq_seg1,
337 					band_mask);
338 
339 	return phymode;
340 }
341 #else
342 static QDF_STATUS
343 util_scan_get_chan_from_he_6g_params(struct wlan_objmgr_pdev *pdev,
344 				     struct scan_cache_entry *scan_params,
345 				     qdf_freq_t *chan_freq,
346 				     bool *he_6g_dup_bcon,
347 				     uint8_t band_mask)
348 {
349 	return QDF_STATUS_SUCCESS;
350 }
351 static inline enum wlan_phymode
352 util_scan_get_phymode_6g(struct wlan_objmgr_pdev *pdev,
353 			 struct scan_cache_entry *scan_params)
354 {
355 	return WLAN_PHYMODE_AUTO;
356 }
357 #endif
358 
359 static inline
360 uint32_t util_scan_sec_chan_freq_from_htinfo(struct wlan_ie_htinfo_cmn *htinfo,
361 					     uint32_t primary_chan_freq)
362 {
363 	if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE)
364 		return primary_chan_freq + WLAN_CHAN_SPACING_20MHZ;
365 	else if (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW)
366 		return primary_chan_freq - WLAN_CHAN_SPACING_20MHZ;
367 
368 	return 0;
369 }
370 
371 static enum wlan_phymode
372 util_scan_get_phymode_5g(struct wlan_objmgr_pdev *pdev,
373 			 struct scan_cache_entry *scan_params)
374 {
375 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
376 	uint16_t ht_cap = 0;
377 	struct htcap_cmn_ie *htcap;
378 	struct wlan_ie_htinfo_cmn *htinfo;
379 	struct wlan_ie_vhtop *vhtop;
380 	uint8_t band_mask = BIT(REG_BAND_5G);
381 
382 	htcap = (struct htcap_cmn_ie *)
383 		util_scan_entry_htcap(scan_params);
384 	htinfo = (struct wlan_ie_htinfo_cmn *)
385 		util_scan_entry_htinfo(scan_params);
386 	vhtop = (struct wlan_ie_vhtop *)
387 		util_scan_entry_vhtop(scan_params);
388 
389 	if (!(htcap && htinfo))
390 		return WLAN_PHYMODE_11A;
391 
392 	if (htcap)
393 		ht_cap = le16toh(htcap->hc_cap);
394 
395 	if (ht_cap & WLAN_HTCAP_C_CHWIDTH40)
396 		phymode = WLAN_PHYMODE_11NA_HT40;
397 	else
398 		phymode = WLAN_PHYMODE_11NA_HT20;
399 
400 	scan_params->channel.cfreq0 =
401 		util_scan_sec_chan_freq_from_htinfo(htinfo,
402 						scan_params->channel.chan_freq);
403 
404 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
405 		switch (vhtop->vht_op_chwidth) {
406 		case WLAN_VHTOP_CHWIDTH_2040:
407 			if (ht_cap & WLAN_HTCAP_C_CHWIDTH40)
408 				phymode = WLAN_PHYMODE_11AC_VHT40;
409 			else
410 				phymode = WLAN_PHYMODE_11AC_VHT20;
411 			break;
412 		case WLAN_VHTOP_CHWIDTH_80:
413 			if (WLAN_IS_REVSIG_VHT80_80(vhtop))
414 				phymode = WLAN_PHYMODE_11AC_VHT80_80;
415 			else if (WLAN_IS_REVSIG_VHT160(vhtop))
416 				phymode = WLAN_PHYMODE_11AC_VHT160;
417 			else
418 				phymode = WLAN_PHYMODE_11AC_VHT80;
419 			break;
420 		case WLAN_VHTOP_CHWIDTH_160:
421 			phymode = WLAN_PHYMODE_11AC_VHT160;
422 			break;
423 		case WLAN_VHTOP_CHWIDTH_80_80:
424 			phymode = WLAN_PHYMODE_11AC_VHT80_80;
425 			break;
426 		default:
427 			scm_err("bad channel: %d",
428 					vhtop->vht_op_chwidth);
429 			phymode = WLAN_PHYMODE_11AC_VHT20;
430 			break;
431 		}
432 		if (vhtop->vht_op_ch_freq_seg1)
433 			scan_params->channel.cfreq0 =
434 				wlan_reg_chan_band_to_freq(pdev,
435 						vhtop->vht_op_ch_freq_seg1,
436 						band_mask);
437 		if (vhtop->vht_op_ch_freq_seg2)
438 			scan_params->channel.cfreq1 =
439 				wlan_reg_chan_band_to_freq(pdev,
440 						vhtop->vht_op_ch_freq_seg2,
441 						band_mask);
442 	}
443 
444 	if (!util_scan_entry_hecap(scan_params))
445 		return phymode;
446 
447 	/* for 5Ghz Check for HE, only if VHT cap and HE cap are present */
448 	if (!IS_WLAN_PHYMODE_VHT(phymode))
449 		return phymode;
450 
451 	switch (phymode) {
452 	case WLAN_PHYMODE_11AC_VHT20:
453 		phymode = WLAN_PHYMODE_11AXA_HE20;
454 		break;
455 	case WLAN_PHYMODE_11AC_VHT40:
456 		phymode = WLAN_PHYMODE_11AXA_HE40;
457 		break;
458 	case WLAN_PHYMODE_11AC_VHT80:
459 		phymode = WLAN_PHYMODE_11AXA_HE80;
460 		break;
461 	case WLAN_PHYMODE_11AC_VHT160:
462 		phymode = WLAN_PHYMODE_11AXA_HE160;
463 		break;
464 	case WLAN_PHYMODE_11AC_VHT80_80:
465 		phymode = WLAN_PHYMODE_11AXA_HE80_80;
466 		break;
467 	default:
468 		phymode = WLAN_PHYMODE_11AXA_HE20;
469 		break;
470 	}
471 
472 	return phymode;
473 }
474 
475 static enum wlan_phymode
476 util_scan_get_phymode_2g(struct scan_cache_entry *scan_params)
477 {
478 	enum wlan_phymode phymode = WLAN_PHYMODE_AUTO;
479 	uint16_t ht_cap = 0;
480 	struct htcap_cmn_ie *htcap;
481 	struct wlan_ie_htinfo_cmn *htinfo;
482 	struct wlan_ie_vhtop *vhtop;
483 
484 	htcap = (struct htcap_cmn_ie *)
485 		util_scan_entry_htcap(scan_params);
486 	htinfo = (struct wlan_ie_htinfo_cmn *)
487 		util_scan_entry_htinfo(scan_params);
488 	vhtop = (struct wlan_ie_vhtop *)
489 		util_scan_entry_vhtop(scan_params);
490 
491 	if (htcap)
492 		ht_cap = le16toh(htcap->hc_cap);
493 
494 	if (htcap && htinfo) {
495 		if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
496 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_ABOVE))
497 			phymode = WLAN_PHYMODE_11NG_HT40PLUS;
498 		else if ((ht_cap & WLAN_HTCAP_C_CHWIDTH40) &&
499 		   (htinfo->hi_extchoff == WLAN_HTINFO_EXTOFFSET_BELOW))
500 			phymode = WLAN_PHYMODE_11NG_HT40MINUS;
501 		else
502 			phymode = WLAN_PHYMODE_11NG_HT20;
503 	} else if (util_scan_entry_xrates(scan_params)) {
504 		/* only 11G stations will have more than 8 rates */
505 		phymode = WLAN_PHYMODE_11G;
506 	} else {
507 		/* Some mischievous g-only APs do not set extended rates */
508 		if (util_scan_entry_rates(scan_params)) {
509 			if (util_is_pureg_rate(&scan_params->ie_list.rates[2],
510 			   scan_params->ie_list.rates[1]))
511 				phymode = WLAN_PHYMODE_11G;
512 			else
513 				phymode = WLAN_PHYMODE_11B;
514 		} else {
515 			phymode = WLAN_PHYMODE_11B;
516 		}
517 	}
518 
519 	/* Check for VHT only if HT cap is present */
520 	if (!IS_WLAN_PHYMODE_HT(phymode))
521 		return phymode;
522 
523 	scan_params->channel.cfreq0 =
524 		util_scan_sec_chan_freq_from_htinfo(htinfo,
525 						scan_params->channel.chan_freq);
526 
527 	if (util_scan_entry_vhtcap(scan_params) && vhtop) {
528 		switch (vhtop->vht_op_chwidth) {
529 		case WLAN_VHTOP_CHWIDTH_2040:
530 			if (phymode == WLAN_PHYMODE_11NG_HT40PLUS)
531 				phymode = WLAN_PHYMODE_11AC_VHT40PLUS_2G;
532 			else if (phymode == WLAN_PHYMODE_11NG_HT40MINUS)
533 				phymode = WLAN_PHYMODE_11AC_VHT40MINUS_2G;
534 			else
535 				phymode = WLAN_PHYMODE_11AC_VHT20_2G;
536 
537 			break;
538 		default:
539 			scm_info("bad vht_op_chwidth: %d",
540 				 vhtop->vht_op_chwidth);
541 			phymode = WLAN_PHYMODE_11AC_VHT20_2G;
542 			break;
543 		}
544 	}
545 
546 	if (!util_scan_entry_hecap(scan_params))
547 		return phymode;
548 
549 	if (phymode == WLAN_PHYMODE_11AC_VHT40PLUS_2G ||
550 	    phymode == WLAN_PHYMODE_11NG_HT40PLUS)
551 		phymode = WLAN_PHYMODE_11AXG_HE40PLUS;
552 	else if (phymode == WLAN_PHYMODE_11AC_VHT40MINUS_2G ||
553 		 phymode == WLAN_PHYMODE_11NG_HT40MINUS)
554 		phymode = WLAN_PHYMODE_11AXG_HE40MINUS;
555 	else
556 		phymode = WLAN_PHYMODE_11AXG_HE20;
557 
558 	return phymode;
559 }
560 
561 static enum wlan_phymode
562 util_scan_get_phymode(struct wlan_objmgr_pdev *pdev,
563 		      struct scan_cache_entry *scan_params)
564 {
565 	if (WLAN_REG_IS_24GHZ_CH_FREQ(scan_params->channel.chan_freq))
566 		return util_scan_get_phymode_2g(scan_params);
567 	else if (WLAN_REG_IS_6GHZ_CHAN_FREQ(scan_params->channel.chan_freq))
568 		return util_scan_get_phymode_6g(pdev, scan_params);
569 	else
570 		return util_scan_get_phymode_5g(pdev, scan_params);
571 }
572 
573 static QDF_STATUS
574 util_scan_parse_chan_switch_wrapper_ie(struct scan_cache_entry *scan_params,
575 	struct ie_header *sub_ie, qdf_size_t sub_ie_len)
576 {
577 	/* Walk through to check nothing is malformed */
578 	while (sub_ie_len >= sizeof(struct ie_header)) {
579 		/* At least one more header is present */
580 		sub_ie_len -= sizeof(struct ie_header);
581 
582 		if (sub_ie->ie_len == 0) {
583 			sub_ie += 1;
584 			continue;
585 		}
586 		if (sub_ie_len < sub_ie->ie_len) {
587 			scm_err("Incomplete corrupted IE:%x",
588 				WLAN_ELEMID_CHAN_SWITCH_WRAP);
589 			return QDF_STATUS_E_INVAL;
590 		}
591 		switch (sub_ie->ie_id) {
592 		case WLAN_ELEMID_COUNTRY:
593 			scan_params->ie_list.country = (uint8_t *)sub_ie;
594 			break;
595 		case WLAN_ELEMID_WIDE_BAND_CHAN_SWITCH:
596 			scan_params->ie_list.widebw = (uint8_t *)sub_ie;
597 			break;
598 		case WLAN_ELEMID_VHT_TX_PWR_ENVLP:
599 			scan_params->ie_list.txpwrenvlp = (uint8_t *)sub_ie;
600 			break;
601 		}
602 		/* Consume sub info element */
603 		sub_ie_len -= sub_ie->ie_len;
604 		/* go to next Sub IE */
605 		sub_ie = (struct ie_header *)
606 			(((uint8_t *) sub_ie) +
607 			sizeof(struct ie_header) + sub_ie->ie_len);
608 	}
609 
610 	return QDF_STATUS_SUCCESS;
611 }
612 
613 bool
614 util_scan_is_hidden_ssid(struct ie_ssid *ssid)
615 {
616 	uint8_t i;
617 
618 	/*
619 	 * We flag this as Hidden SSID if the Length is 0
620 	 * of the SSID only contains 0's
621 	 */
622 	if (!ssid || !ssid->ssid_len)
623 		return true;
624 
625 	for (i = 0; i < ssid->ssid_len; i++)
626 		if (ssid->ssid[i] != 0)
627 			return false;
628 
629 	/* All 0's */
630 	return true;
631 }
632 
633 static QDF_STATUS
634 util_scan_update_rnr(struct rnr_bss_info *rnr,
635 		     struct neighbor_ap_info_field *ap_info,
636 		     uint8_t *data)
637 {
638 	uint8_t tbtt_info_length;
639 
640 	tbtt_info_length = ap_info->tbtt_header.tbtt_info_length;
641 
642 	switch (tbtt_info_length) {
643 	case TBTT_NEIGHBOR_AP_OFFSET_ONLY:
644 		/* Dont store it skip*/
645 		break;
646 
647 	case TBTT_NEIGHBOR_AP_BSS_PARAM:
648 		/* Dont store it skip*/
649 		break;
650 
651 	case TBTT_NEIGHBOR_AP_SHORTSSID:
652 		rnr->channel_number = ap_info->channel_number;
653 		rnr->operating_class = ap_info->operting_class;
654 		qdf_mem_copy(&rnr->short_ssid, &data[1], SHORT_SSID_LEN);
655 		break;
656 
657 	case TBTT_NEIGHBOR_AP_S_SSID_BSS_PARAM:
658 		rnr->channel_number = ap_info->channel_number;
659 		rnr->operating_class = ap_info->operting_class;
660 		qdf_mem_copy(&rnr->short_ssid, &data[1], SHORT_SSID_LEN);
661 		rnr->bss_params = data[5];
662 		break;
663 
664 	case TBTT_NEIGHBOR_AP_BSSID:
665 		rnr->channel_number = ap_info->channel_number;
666 		rnr->operating_class = ap_info->operting_class;
667 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
668 		break;
669 
670 	case TBTT_NEIGHBOR_AP_BSSID_BSS_PARAM:
671 		rnr->channel_number = ap_info->channel_number;
672 		rnr->operating_class = ap_info->operting_class;
673 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
674 		rnr->bss_params = data[7];
675 		break;
676 
677 	case TBTT_NEIGHBOR_AP_BSSID_BSS_PARAM_20MHZ_PSD:
678 		rnr->channel_number = ap_info->channel_number;
679 		rnr->operating_class = ap_info->operting_class;
680 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
681 		rnr->bss_params = data[7];
682 		rnr->psd_20mhz = data[8];
683 		break;
684 
685 	case TBTT_NEIGHBOR_AP_BSSSID_S_SSID:
686 		rnr->channel_number = ap_info->channel_number;
687 		rnr->operating_class = ap_info->operting_class;
688 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
689 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
690 		break;
691 
692 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM:
693 		rnr->channel_number = ap_info->channel_number;
694 		rnr->operating_class = ap_info->operting_class;
695 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
696 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
697 		rnr->bss_params = data[11];
698 		break;
699 
700 	case TBTT_NEIGHBOR_AP_BSSID_S_SSID_BSS_PARAM_20MHZ_PSD:
701 		rnr->channel_number = ap_info->channel_number;
702 		rnr->operating_class = ap_info->operting_class;
703 		qdf_mem_copy(&rnr->bssid, &data[1], QDF_MAC_ADDR_SIZE);
704 		qdf_mem_copy(&rnr->short_ssid, &data[7], SHORT_SSID_LEN);
705 		rnr->bss_params = data[11];
706 		rnr->psd_20mhz = data[12];
707 		break;
708 
709 	default:
710 		scm_debug("Wrong fieldtype");
711 	}
712 
713 	return QDF_STATUS_SUCCESS;
714 }
715 
716 static QDF_STATUS
717 util_scan_parse_rnr_ie(struct scan_cache_entry *scan_entry,
718 		       struct ie_header *ie)
719 {
720 	uint32_t rnr_ie_len;
721 	uint16_t tbtt_count, tbtt_length, i, fieldtype;
722 	uint8_t *data;
723 	struct neighbor_ap_info_field *neighbor_ap_info;
724 
725 	rnr_ie_len = ie->ie_len;
726 	data = (uint8_t *)ie + sizeof(struct ie_header);
727 
728 	while (data < ((uint8_t *)ie + rnr_ie_len + 2)) {
729 		neighbor_ap_info = (struct neighbor_ap_info_field *)data;
730 		tbtt_count = neighbor_ap_info->tbtt_header.tbtt_info_count;
731 		tbtt_length = neighbor_ap_info->tbtt_header.tbtt_info_length;
732 		fieldtype = neighbor_ap_info->tbtt_header.tbbt_info_fieldtype;
733 		scm_debug("channel number %d, op class %d",
734 			  neighbor_ap_info->channel_number,
735 			  neighbor_ap_info->operting_class);
736 		scm_debug("tbtt_count %d, tbtt_length %d, fieldtype %d",
737 			  tbtt_count, tbtt_length, fieldtype);
738 		data += sizeof(struct neighbor_ap_info_field);
739 		for (i = 0; i < (tbtt_count + 1) ; i++) {
740 			if (i < MAX_RNR_BSS)
741 				util_scan_update_rnr(
742 					&scan_entry->rnr.bss_info[i],
743 					neighbor_ap_info,
744 					data);
745 			data += tbtt_length;
746 		}
747 	}
748 
749 	return QDF_STATUS_SUCCESS;
750 }
751 
752 static QDF_STATUS
753 util_scan_parse_extn_ie(struct scan_cache_entry *scan_params,
754 	struct ie_header *ie)
755 {
756 	struct extn_ie_header *extn_ie = (struct extn_ie_header *) ie;
757 
758 	switch (extn_ie->ie_extn_id) {
759 	case WLAN_EXTN_ELEMID_MAX_CHAN_SWITCH_TIME:
760 		scan_params->ie_list.mcst  = (uint8_t *)ie;
761 		break;
762 	case WLAN_EXTN_ELEMID_SRP:
763 		scan_params->ie_list.srp   = (uint8_t *)ie;
764 		break;
765 	case WLAN_EXTN_ELEMID_HECAP:
766 		scan_params->ie_list.hecap = (uint8_t *)ie;
767 		break;
768 	case WLAN_EXTN_ELEMID_HEOP:
769 		scan_params->ie_list.heop  = (uint8_t *)ie;
770 		break;
771 	case WLAN_EXTN_ELEMID_ESP:
772 		scan_params->ie_list.esp = (uint8_t *)ie;
773 		break;
774 	case WLAN_EXTN_ELEMID_MUEDCA:
775 		scan_params->ie_list.muedca = (uint8_t *)ie;
776 		break;
777 	case WLAN_EXTN_ELEMID_HE_6G_CAP:
778 		scan_params->ie_list.hecap_6g = (uint8_t *)ie;
779 		break;
780 	default:
781 		break;
782 	}
783 	return QDF_STATUS_SUCCESS;
784 }
785 
786 static QDF_STATUS
787 util_scan_parse_vendor_ie(struct scan_cache_entry *scan_params,
788 	struct ie_header *ie)
789 {
790 	if (!scan_params->ie_list.vendor)
791 		scan_params->ie_list.vendor = (uint8_t *)ie;
792 
793 	if (is_wpa_oui((uint8_t *)ie)) {
794 		scan_params->ie_list.wpa = (uint8_t *)ie;
795 	} else if (is_wps_oui((uint8_t *)ie)) {
796 		scan_params->ie_list.wps = (uint8_t *)ie;
797 		/* WCN IE should be a subset of WPS IE */
798 		if (is_wcn_oui((uint8_t *)ie))
799 			scan_params->ie_list.wcn = (uint8_t *)ie;
800 	} else if (is_wme_param((uint8_t *)ie)) {
801 		scan_params->ie_list.wmeparam = (uint8_t *)ie;
802 	} else if (is_wme_info((uint8_t *)ie)) {
803 		scan_params->ie_list.wmeinfo = (uint8_t *)ie;
804 	} else if (is_atheros_oui((uint8_t *)ie)) {
805 		scan_params->ie_list.athcaps = (uint8_t *)ie;
806 	} else if (is_atheros_extcap_oui((uint8_t *)ie)) {
807 		scan_params->ie_list.athextcaps = (uint8_t *)ie;
808 	} else if (is_sfa_oui((uint8_t *)ie)) {
809 		scan_params->ie_list.sfa = (uint8_t *)ie;
810 	} else if (is_p2p_oui((uint8_t *)ie)) {
811 		scan_params->ie_list.p2p = (uint8_t *)ie;
812 	} else if (is_qca_son_oui((uint8_t *)ie,
813 				  QCA_OUI_WHC_AP_INFO_SUBTYPE)) {
814 		scan_params->ie_list.sonadv = (uint8_t *)ie;
815 	} else if (is_ht_cap((uint8_t *)ie)) {
816 		/* we only care if there isn't already an HT IE (ANA) */
817 		if (!scan_params->ie_list.htcap) {
818 			if (ie->ie_len != (WLAN_VENDOR_HT_IE_OFFSET_LEN +
819 					   sizeof(struct htcap_cmn_ie)))
820 				return QDF_STATUS_E_INVAL;
821 			scan_params->ie_list.htcap =
822 			 (uint8_t *)&(((struct wlan_vendor_ie_htcap *)ie)->ie);
823 		}
824 	} else if (is_ht_info((uint8_t *)ie)) {
825 		/* we only care if there isn't already an HT IE (ANA) */
826 		if (!scan_params->ie_list.htinfo) {
827 			if (ie->ie_len != WLAN_VENDOR_HT_IE_OFFSET_LEN +
828 					  sizeof(struct wlan_ie_htinfo_cmn))
829 				return QDF_STATUS_E_INVAL;
830 			scan_params->ie_list.htinfo =
831 			  (uint8_t *)&(((struct wlan_vendor_ie_htinfo *)
832 			  ie)->hi_ie);
833 		}
834 	} else if (is_interop_vht((uint8_t *)ie) &&
835 	    !(scan_params->ie_list.vhtcap)) {
836 		uint8_t *vendor_ie = (uint8_t *)(ie);
837 
838 		if (ie->ie_len < ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
839 				 sizeof(struct wlan_ie_vhtcaps)) -
840 				 sizeof(struct ie_header)))
841 			return QDF_STATUS_E_INVAL;
842 		vendor_ie = ((uint8_t *)(ie)) + WLAN_VENDOR_VHTCAP_IE_OFFSET;
843 		if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtcaps)) -
844 				      sizeof(struct ie_header))
845 			return QDF_STATUS_E_INVAL;
846 		/* location where Interop Vht Cap IE and VHT OP IE Present */
847 		scan_params->ie_list.vhtcap = (((uint8_t *)(ie)) +
848 						WLAN_VENDOR_VHTCAP_IE_OFFSET);
849 		if (ie->ie_len > ((WLAN_VENDOR_VHTCAP_IE_OFFSET +
850 				 sizeof(struct wlan_ie_vhtcaps)) -
851 				 sizeof(struct ie_header))) {
852 			if (ie->ie_len < ((WLAN_VENDOR_VHTOP_IE_OFFSET +
853 					  sizeof(struct wlan_ie_vhtop)) -
854 					  sizeof(struct ie_header)))
855 				return QDF_STATUS_E_INVAL;
856 			vendor_ie = ((uint8_t *)(ie)) +
857 				    WLAN_VENDOR_VHTOP_IE_OFFSET;
858 			if (vendor_ie[1] != (sizeof(struct wlan_ie_vhtop) -
859 					     sizeof(struct ie_header)))
860 				return QDF_STATUS_E_INVAL;
861 			scan_params->ie_list.vhtop = (((uint8_t *)(ie)) +
862 						   WLAN_VENDOR_VHTOP_IE_OFFSET);
863 		}
864 	} else if (is_bwnss_oui((uint8_t *)ie)) {
865 		/*
866 		 * Bandwidth-NSS map has sub-type & version.
867 		 * hence copy data just after version byte
868 		 */
869 		scan_params->ie_list.bwnss_map = (((uint8_t *)ie) + 8);
870 	} else if (is_mbo_oce_oui((uint8_t *)ie)) {
871 		scan_params->ie_list.mbo_oce = (uint8_t *)ie;
872 	} else if (is_extender_oui((uint8_t *)ie)) {
873 		scan_params->ie_list.extender = (uint8_t *)ie;
874 	} else if (is_adaptive_11r_oui((uint8_t *)ie)) {
875 		if ((ie->ie_len < OUI_LENGTH) ||
876 		    (ie->ie_len > MAX_ADAPTIVE_11R_IE_LEN))
877 			return QDF_STATUS_E_INVAL;
878 
879 		scan_params->ie_list.adaptive_11r = (uint8_t *)ie +
880 						sizeof(struct ie_header);
881 	} else if (is_sae_single_pmk_oui((uint8_t *)ie)) {
882 		if ((ie->ie_len < OUI_LENGTH) ||
883 		    (ie->ie_len > MAX_SAE_SINGLE_PMK_IE_LEN)) {
884 			scm_debug("Invalid sae single pmk OUI");
885 			return QDF_STATUS_E_INVAL;
886 		}
887 		scan_params->ie_list.single_pmk = (uint8_t *)ie +
888 						sizeof(struct ie_header);
889 	}
890 	return QDF_STATUS_SUCCESS;
891 }
892 
893 static QDF_STATUS
894 util_scan_populate_bcn_ie_list(struct wlan_objmgr_pdev *pdev,
895 			       struct scan_cache_entry *scan_params,
896 			       qdf_freq_t *chan_freq, uint8_t band_mask)
897 {
898 	struct ie_header *ie, *sub_ie;
899 	uint32_t ie_len, sub_ie_len;
900 	QDF_STATUS status;
901 	uint8_t chan_idx;
902 	struct wlan_scan_obj *scan_obj;
903 	struct wlan_objmgr_psoc *psoc;
904 
905 	psoc = wlan_pdev_get_psoc(pdev);
906 	if (!psoc) {
907 		scm_err("psoc is NULL");
908 		return QDF_STATUS_E_INVAL;
909 	}
910 
911 	scan_obj = wlan_psoc_get_scan_obj(psoc);
912 	if (!scan_obj) {
913 		scm_err("scan_obj is NULL");
914 		return QDF_STATUS_E_INVAL;
915 	}
916 
917 	ie_len = util_scan_entry_ie_len(scan_params);
918 	ie = (struct ie_header *)
919 		  util_scan_entry_ie_data(scan_params);
920 
921 	while (ie_len >= sizeof(struct ie_header)) {
922 		ie_len -= sizeof(struct ie_header);
923 
924 		if (!ie->ie_len) {
925 			ie += 1;
926 			continue;
927 		}
928 
929 		if (ie_len < ie->ie_len) {
930 			scm_debug("Incomplete corrupted IE:%x",
931 				ie->ie_id);
932 			return QDF_STATUS_E_INVAL;
933 		}
934 
935 		switch (ie->ie_id) {
936 		case WLAN_ELEMID_SSID:
937 			if (ie->ie_len > (sizeof(struct ie_ssid) -
938 					  sizeof(struct ie_header)))
939 				goto err;
940 			scan_params->ie_list.ssid = (uint8_t *)ie;
941 			break;
942 		case WLAN_ELEMID_RATES:
943 			if (ie->ie_len > WLAN_SUPPORTED_RATES_IE_MAX_LEN)
944 				goto err;
945 			scan_params->ie_list.rates = (uint8_t *)ie;
946 			break;
947 		case WLAN_ELEMID_DSPARMS:
948 			if (ie->ie_len != WLAN_DS_PARAM_IE_MAX_LEN)
949 				return QDF_STATUS_E_INVAL;
950 			scan_params->ie_list.ds_param = (uint8_t *)ie;
951 			chan_idx =
952 				((struct ds_ie *)ie)->cur_chan;
953 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
954 								band_mask);
955 			/* Drop if invalid freq */
956 			if (scan_obj->drop_bcn_on_invalid_freq &&
957 			    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
958 				scm_debug_rl("%pM: Drop as invalid channel %d freq %d in DS IE",
959 					     scan_params->bssid.bytes,
960 					     chan_idx, *chan_freq);
961 				return QDF_STATUS_E_INVAL;
962 			}
963 			break;
964 		case WLAN_ELEMID_TIM:
965 			if (ie->ie_len < WLAN_TIM_IE_MIN_LENGTH)
966 				goto err;
967 			scan_params->ie_list.tim = (uint8_t *)ie;
968 			scan_params->dtim_period =
969 				((struct wlan_tim_ie *)ie)->tim_period;
970 			break;
971 		case WLAN_ELEMID_COUNTRY:
972 			if (ie->ie_len < WLAN_COUNTRY_IE_MIN_LEN)
973 				goto err;
974 			scan_params->ie_list.country = (uint8_t *)ie;
975 			break;
976 		case WLAN_ELEMID_QBSS_LOAD:
977 			if (ie->ie_len != sizeof(struct qbss_load_ie) -
978 					  sizeof(struct ie_header)) {
979 				/*
980 				 * Expected QBSS IE length is 5Bytes; For some
981 				 * old cisco AP, QBSS IE length is 4Bytes, which
982 				 * doesn't match with latest spec, So ignore
983 				 * QBSS IE in such case.
984 				 */
985 				break;
986 			}
987 			scan_params->ie_list.qbssload = (uint8_t *)ie;
988 			break;
989 		case WLAN_ELEMID_CHANSWITCHANN:
990 			if (ie->ie_len != WLAN_CSA_IE_MAX_LEN)
991 				goto err;
992 			scan_params->ie_list.csa = (uint8_t *)ie;
993 			break;
994 		case WLAN_ELEMID_IBSSDFS:
995 			if (ie->ie_len < WLAN_IBSSDFS_IE_MIN_LEN)
996 				goto err;
997 			scan_params->ie_list.ibssdfs = (uint8_t *)ie;
998 			break;
999 		case WLAN_ELEMID_QUIET:
1000 			if (ie->ie_len != WLAN_QUIET_IE_MAX_LEN)
1001 				goto err;
1002 			scan_params->ie_list.quiet = (uint8_t *)ie;
1003 			break;
1004 		case WLAN_ELEMID_ERP:
1005 			if (ie->ie_len != (sizeof(struct erp_ie) -
1006 					    sizeof(struct ie_header)))
1007 				goto err;
1008 			scan_params->erp = ((struct erp_ie *)ie)->value;
1009 			break;
1010 		case WLAN_ELEMID_HTCAP_ANA:
1011 			if (ie->ie_len != sizeof(struct htcap_cmn_ie))
1012 				goto err;
1013 			scan_params->ie_list.htcap =
1014 				(uint8_t *)&(((struct htcap_ie *)ie)->ie);
1015 			break;
1016 		case WLAN_ELEMID_RSN:
1017 			if (ie->ie_len < WLAN_RSN_IE_MIN_LEN)
1018 				goto err;
1019 			scan_params->ie_list.rsn = (uint8_t *)ie;
1020 			break;
1021 		case WLAN_ELEMID_XRATES:
1022 			scan_params->ie_list.xrates = (uint8_t *)ie;
1023 			break;
1024 		case WLAN_ELEMID_EXTCHANSWITCHANN:
1025 			if (ie->ie_len != WLAN_XCSA_IE_MAX_LEN)
1026 				goto err;
1027 			scan_params->ie_list.xcsa = (uint8_t *)ie;
1028 			break;
1029 		case WLAN_ELEMID_SECCHANOFFSET:
1030 			if (ie->ie_len != WLAN_SECCHANOFF_IE_MAX_LEN)
1031 				goto err;
1032 			scan_params->ie_list.secchanoff = (uint8_t *)ie;
1033 			break;
1034 		case WLAN_ELEMID_HTINFO_ANA:
1035 			if (ie->ie_len != sizeof(struct wlan_ie_htinfo_cmn))
1036 				goto err;
1037 			scan_params->ie_list.htinfo =
1038 			  (uint8_t *)&(((struct wlan_ie_htinfo *) ie)->hi_ie);
1039 			chan_idx = ((struct wlan_ie_htinfo_cmn *)
1040 				 (scan_params->ie_list.htinfo))->hi_ctrlchannel;
1041 			*chan_freq = wlan_reg_chan_band_to_freq(pdev, chan_idx,
1042 								band_mask);
1043 			/* Drop if invalid freq */
1044 			if (scan_obj->drop_bcn_on_invalid_freq &&
1045 			    wlan_reg_is_disable_for_freq(pdev, *chan_freq)) {
1046 				scm_debug_rl("%pM: Drop as invalid channel %d freq %d in HT_INFO IE",
1047 					     scan_params->bssid.bytes,
1048 					     chan_idx, *chan_freq);
1049 				return QDF_STATUS_E_INVAL;
1050 			}
1051 			break;
1052 		case WLAN_ELEMID_WAPI:
1053 			if (ie->ie_len < WLAN_WAPI_IE_MIN_LEN)
1054 				goto err;
1055 			scan_params->ie_list.wapi = (uint8_t *)ie;
1056 			break;
1057 		case WLAN_ELEMID_XCAPS:
1058 			if (ie->ie_len > WLAN_EXTCAP_IE_MAX_LEN)
1059 				goto err;
1060 			scan_params->ie_list.extcaps = (uint8_t *)ie;
1061 			break;
1062 		case WLAN_ELEMID_VHTCAP:
1063 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtcaps) -
1064 					   sizeof(struct ie_header)))
1065 				goto err;
1066 			scan_params->ie_list.vhtcap = (uint8_t *)ie;
1067 			break;
1068 		case WLAN_ELEMID_VHTOP:
1069 			if (ie->ie_len != (sizeof(struct wlan_ie_vhtop) -
1070 					   sizeof(struct ie_header)))
1071 				goto err;
1072 			scan_params->ie_list.vhtop = (uint8_t *)ie;
1073 			break;
1074 		case WLAN_ELEMID_OP_MODE_NOTIFY:
1075 			if (ie->ie_len != WLAN_OPMODE_IE_MAX_LEN)
1076 				goto err;
1077 			scan_params->ie_list.opmode = (uint8_t *)ie;
1078 			break;
1079 		case WLAN_ELEMID_MOBILITY_DOMAIN:
1080 			if (ie->ie_len != WLAN_MOBILITY_DOMAIN_IE_MAX_LEN)
1081 				goto err;
1082 			scan_params->ie_list.mdie = (uint8_t *)ie;
1083 			break;
1084 		case WLAN_ELEMID_VENDOR:
1085 			status = util_scan_parse_vendor_ie(scan_params,
1086 							   ie);
1087 			if (QDF_IS_STATUS_ERROR(status))
1088 				goto err_status;
1089 			break;
1090 		case WLAN_ELEMID_CHAN_SWITCH_WRAP:
1091 			scan_params->ie_list.cswrp = (uint8_t *)ie;
1092 			/* Go to next sub IE */
1093 			sub_ie = (struct ie_header *)
1094 			(((uint8_t *)ie) + sizeof(struct ie_header));
1095 			sub_ie_len = ie->ie_len;
1096 			status =
1097 				util_scan_parse_chan_switch_wrapper_ie(
1098 					scan_params, sub_ie, sub_ie_len);
1099 			if (QDF_IS_STATUS_ERROR(status)) {
1100 				goto err_status;
1101 			}
1102 			break;
1103 		case WLAN_ELEMID_FILS_INDICATION:
1104 			if (ie->ie_len < WLAN_FILS_INDICATION_IE_MIN_LEN)
1105 				goto err;
1106 			scan_params->ie_list.fils_indication = (uint8_t *)ie;
1107 			break;
1108 		case WLAN_ELEMID_EXTN_ELEM:
1109 			status = util_scan_parse_extn_ie(scan_params, ie);
1110 			if (QDF_IS_STATUS_ERROR(status))
1111 				goto err_status;
1112 			break;
1113 		case WLAN_ELEMID_REDUCED_NEIGHBOR_REPORT:
1114 			if (ie->ie_len < WLAN_RNR_IE_MIN_LEN)
1115 				goto err;
1116 			scan_params->ie_list.rnrie = (uint8_t *)ie;
1117 			status = util_scan_parse_rnr_ie(scan_params, ie);
1118 			if (QDF_IS_STATUS_ERROR(status))
1119 				goto err_status;
1120 			break;
1121 		default:
1122 			break;
1123 		}
1124 
1125 		/* Consume info element */
1126 		ie_len -= ie->ie_len;
1127 		/* Go to next IE */
1128 		ie = (struct ie_header *)
1129 			(((uint8_t *) ie) +
1130 			sizeof(struct ie_header) +
1131 			ie->ie_len);
1132 	}
1133 
1134 	return QDF_STATUS_SUCCESS;
1135 
1136 err:
1137 	status = QDF_STATUS_E_INVAL;
1138 err_status:
1139 	scm_debug("failed to parse IE - id: %d, len: %d",
1140 		  ie->ie_id, ie->ie_len);
1141 
1142 	return status;
1143 }
1144 
1145 /**
1146  * util_scan_update_esp_data: update ESP params from beacon/probe response
1147  * @esp_information: pointer to wlan_esp_information
1148  * @scan_entry: new received entry
1149  *
1150  * The Estimated Service Parameters element is
1151  * used by a AP to provide information to another STA which
1152  * can then use the information as input to an algorithm to
1153  * generate an estimate of throughput between the two STAs.
1154  * The ESP Information List field contains from 1 to 4 ESP
1155  * Information fields(each field 24 bits), each corresponding
1156  * to an access category for which estimated service parameters
1157  * information is provided.
1158  *
1159  * Return: None
1160  */
1161 static void util_scan_update_esp_data(struct wlan_esp_ie *esp_information,
1162 		struct scan_cache_entry *scan_entry)
1163 {
1164 
1165 	uint8_t *data;
1166 	int i = 0;
1167 	uint64_t total_elements;
1168 	struct wlan_esp_info *esp_info;
1169 	struct wlan_esp_ie *esp_ie;
1170 
1171 	esp_ie = (struct wlan_esp_ie *)
1172 		util_scan_entry_esp_info(scan_entry);
1173 
1174 	total_elements  = esp_ie->esp_len;
1175 	data = (uint8_t *)esp_ie + 3;
1176 	do_div(total_elements, ESP_INFORMATION_LIST_LENGTH);
1177 
1178 	if (total_elements > MAX_ESP_INFORMATION_FIELD) {
1179 		scm_err("No of Air time fractions are greater than supported");
1180 		return;
1181 	}
1182 
1183 	for (i = 0; i < total_elements; i++) {
1184 		esp_info = (struct wlan_esp_info *)data;
1185 		if (esp_info->access_category == ESP_AC_BK) {
1186 			qdf_mem_copy(&esp_information->esp_info_AC_BK,
1187 					data, 3);
1188 			data = data + ESP_INFORMATION_LIST_LENGTH;
1189 			continue;
1190 		}
1191 		if (esp_info->access_category == ESP_AC_BE) {
1192 			qdf_mem_copy(&esp_information->esp_info_AC_BE,
1193 					data, 3);
1194 			data = data + ESP_INFORMATION_LIST_LENGTH;
1195 			continue;
1196 		}
1197 		if (esp_info->access_category == ESP_AC_VI) {
1198 			qdf_mem_copy(&esp_information->esp_info_AC_VI,
1199 					data, 3);
1200 			data = data + ESP_INFORMATION_LIST_LENGTH;
1201 			continue;
1202 		}
1203 		if (esp_info->access_category == ESP_AC_VO) {
1204 			qdf_mem_copy(&esp_information->esp_info_AC_VO,
1205 					data, 3);
1206 			data = data + ESP_INFORMATION_LIST_LENGTH;
1207 			break;
1208 		}
1209 	}
1210 }
1211 
1212 /**
1213  * util_scan_scm_update_bss_with_esp_dataa: calculate estimated air time
1214  * fraction
1215  * @scan_entry: new received entry
1216  *
1217  * This function process all Access category ESP params and provide
1218  * best effort air time fraction.
1219  * If best effort is not available, it will choose VI, VO and BK in sequence
1220  *
1221  */
1222 static void util_scan_scm_update_bss_with_esp_data(
1223 		struct scan_cache_entry *scan_entry)
1224 {
1225 	uint8_t air_time_fraction = 0;
1226 	struct wlan_esp_ie esp_information;
1227 
1228 	if (!scan_entry->ie_list.esp)
1229 		return;
1230 
1231 	util_scan_update_esp_data(&esp_information, scan_entry);
1232 
1233 	/*
1234 	 * If the ESP metric is transmitting multiple airtime fractions, then
1235 	 * follow the sequence AC_BE, AC_VI, AC_VO, AC_BK and pick whichever is
1236 	 * the first one available
1237 	 */
1238 	if (esp_information.esp_info_AC_BE.access_category
1239 			== ESP_AC_BE)
1240 		air_time_fraction =
1241 			esp_information.esp_info_AC_BE.
1242 			estimated_air_fraction;
1243 	else if (esp_information.esp_info_AC_VI.access_category
1244 			== ESP_AC_VI)
1245 		air_time_fraction =
1246 			esp_information.esp_info_AC_VI.
1247 			estimated_air_fraction;
1248 	else if (esp_information.esp_info_AC_VO.access_category
1249 			== ESP_AC_VO)
1250 		air_time_fraction =
1251 			esp_information.esp_info_AC_VO.
1252 			estimated_air_fraction;
1253 	else if (esp_information.esp_info_AC_BK.access_category
1254 			== ESP_AC_BK)
1255 		air_time_fraction =
1256 			esp_information.esp_info_AC_BK.
1257 				estimated_air_fraction;
1258 	scan_entry->air_time_fraction = air_time_fraction;
1259 }
1260 
1261 /**
1262  * util_scan_scm_calc_nss_supported_by_ap() - finds out nss from AP
1263  * @scan_entry: new received entry
1264  *
1265  * Return: number of nss advertised by AP
1266  */
1267 static int util_scan_scm_calc_nss_supported_by_ap(
1268 		struct scan_cache_entry *scan_params)
1269 {
1270 	struct htcap_cmn_ie *htcap;
1271 	struct wlan_ie_vhtcaps *vhtcaps;
1272 	struct wlan_ie_hecaps *hecaps;
1273 	uint16_t rx_mcs_map = 0;
1274 
1275 	htcap = (struct htcap_cmn_ie *)
1276 		util_scan_entry_htcap(scan_params);
1277 	vhtcaps = (struct wlan_ie_vhtcaps *)
1278 		util_scan_entry_vhtcap(scan_params);
1279 	hecaps = (struct wlan_ie_hecaps *)
1280 		util_scan_entry_hecap(scan_params);
1281 
1282 	if (hecaps) {
1283 		/* Using rx mcs map related to 80MHz or lower as in some
1284 		 * cases higher mcs may suuport lesser NSS than that
1285 		 * of lowe mcs. Thus giving max NSS capability.
1286 		 */
1287 		rx_mcs_map =
1288 			qdf_cpu_to_le16(hecaps->mcs_bw_map[0].rx_mcs_map);
1289 	} else if (vhtcaps) {
1290 		rx_mcs_map = vhtcaps->rx_mcs_map;
1291 	}
1292 
1293 	if (hecaps || vhtcaps) {
1294 		if ((rx_mcs_map & 0xC000) != 0xC000)
1295 			return 8;
1296 
1297 		if ((rx_mcs_map & 0x3000) != 0x3000)
1298 			return 7;
1299 
1300 		if ((rx_mcs_map & 0x0C00) != 0x0C00)
1301 			return 6;
1302 
1303 		if ((rx_mcs_map & 0x0300) != 0x0300)
1304 			return 5;
1305 
1306 		if ((rx_mcs_map & 0x00C0) != 0x00C0)
1307 			return 4;
1308 
1309 		if ((rx_mcs_map & 0x0030) != 0x0030)
1310 			return 3;
1311 
1312 		if ((rx_mcs_map & 0x000C) != 0x000C)
1313 			return 2;
1314 	} else if (htcap) {
1315 		if (htcap->mcsset[3])
1316 			return 4;
1317 
1318 		if (htcap->mcsset[2])
1319 			return 3;
1320 
1321 		if (htcap->mcsset[1])
1322 			return 2;
1323 
1324 	}
1325 	return 1;
1326 }
1327 
1328 #ifdef WLAN_DFS_CHAN_HIDDEN_SSID
1329 QDF_STATUS
1330 util_scan_add_hidden_ssid(struct wlan_objmgr_pdev *pdev, qdf_nbuf_t bcnbuf)
1331 {
1332 	struct wlan_frame_hdr *hdr;
1333 	struct wlan_bcn_frame *bcn;
1334 	struct wlan_scan_obj *scan_obj;
1335 	struct wlan_ssid *conf_ssid;
1336 	struct  ie_header *ie;
1337 	uint32_t frame_len = qdf_nbuf_len(bcnbuf);
1338 	uint16_t bcn_ie_offset, ssid_ie_start_offset, ssid_ie_end_offset;
1339 	uint16_t tmplen, ie_length;
1340 	uint8_t *pbeacon, *tmp;
1341 	bool     set_ssid_flag = false;
1342 	struct ie_ssid *ssid;
1343 	uint8_t pdev_id;
1344 
1345 	if (!pdev) {
1346 		scm_warn("pdev: 0x%pK is NULL", pdev);
1347 		return QDF_STATUS_E_NULL_VALUE;
1348 	}
1349 	pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1350 	scan_obj = wlan_pdev_get_scan_obj(pdev);
1351 	if (!scan_obj) {
1352 		scm_warn("null scan_obj");
1353 		return QDF_STATUS_E_NULL_VALUE;
1354 	}
1355 
1356 	conf_ssid = &scan_obj->pdev_info[pdev_id].conf_ssid;
1357 
1358 	hdr = (struct wlan_frame_hdr *)qdf_nbuf_data(bcnbuf);
1359 
1360 	/* received bssid does not match configured bssid */
1361 	if (qdf_mem_cmp(hdr->i_addr3, scan_obj->pdev_info[pdev_id].conf_bssid,
1362 			QDF_MAC_ADDR_SIZE) ||
1363 			conf_ssid->length == 0) {
1364 		return QDF_STATUS_SUCCESS;
1365 	}
1366 
1367 	bcn = (struct wlan_bcn_frame *)(qdf_nbuf_data(bcnbuf) + sizeof(*hdr));
1368 	pbeacon = (uint8_t *)bcn;
1369 
1370 	ie = (struct ie_header *)(pbeacon +
1371 				  offsetof(struct wlan_bcn_frame, ie));
1372 
1373 	bcn_ie_offset = offsetof(struct wlan_bcn_frame, ie);
1374 	ie_length = (uint16_t)(frame_len - sizeof(*hdr) -
1375 			       bcn_ie_offset);
1376 
1377 	while (ie_length >=  sizeof(struct ie_header)) {
1378 		ie_length -= sizeof(struct ie_header);
1379 
1380 		bcn_ie_offset += sizeof(struct ie_header);
1381 
1382 		if (ie_length < ie->ie_len) {
1383 			scm_debug("Incomplete corrupted IE:%x", ie->ie_id);
1384 			return QDF_STATUS_E_INVAL;
1385 		}
1386 		if (ie->ie_id == WLAN_ELEMID_SSID) {
1387 			if (ie->ie_len > (sizeof(struct ie_ssid) -
1388 						 sizeof(struct ie_header))) {
1389 				return QDF_STATUS_E_INVAL;
1390 			}
1391 			ssid = (struct ie_ssid *)ie;
1392 			if (util_scan_is_hidden_ssid(ssid)) {
1393 				set_ssid_flag  = true;
1394 				ssid_ie_start_offset = bcn_ie_offset -
1395 					sizeof(struct ie_header);
1396 				ssid_ie_end_offset = bcn_ie_offset +
1397 					ie->ie_len;
1398 			}
1399 		}
1400 		if (ie->ie_len == 0) {
1401 			ie += 1;    /* next IE */
1402 			continue;
1403 		}
1404 		if (ie->ie_id == WLAN_ELEMID_VENDOR &&
1405 		    is_wps_oui((uint8_t *)ie)) {
1406 			set_ssid_flag = false;
1407 			break;
1408 		}
1409 		/* Consume info element */
1410 		ie_length -=  ie->ie_len;
1411 		/* Go to next IE */
1412 		ie = (struct ie_header *)(((uint8_t *)ie) +
1413 				sizeof(struct ie_header) +
1414 				ie->ie_len);
1415 	}
1416 
1417 	if (set_ssid_flag) {
1418 		/* Hidden SSID if the Length is 0 */
1419 		if (!ssid->ssid_len) {
1420 			/* increase the taillength by length of ssid */
1421 			if (qdf_nbuf_put_tail(bcnbuf,
1422 					      conf_ssid->length) == NULL) {
1423 				scm_debug("No enough tailroom");
1424 				return  QDF_STATUS_E_NOMEM;
1425 			}
1426 			/* length of the buffer to be copied */
1427 			tmplen = frame_len -
1428 				sizeof(*hdr) - ssid_ie_end_offset;
1429 			/*
1430 			 * tmp memory to copy the beacon info
1431 			 * after ssid ie.
1432 			 */
1433 			tmp = qdf_mem_malloc(tmplen * sizeof(u_int8_t));
1434 			if (!tmp)
1435 				return  QDF_STATUS_E_NOMEM;
1436 
1437 			/* Copy beacon data after ssid ie to tmp */
1438 			qdf_nbuf_copy_bits(bcnbuf, (sizeof(*hdr) +
1439 					   ssid_ie_end_offset), tmplen, tmp);
1440 			/* Add ssid length */
1441 			*(pbeacon + (ssid_ie_start_offset + 1))
1442 				= conf_ssid->length;
1443 			/* Insert the  SSID string */
1444 			qdf_mem_copy((pbeacon + ssid_ie_end_offset),
1445 				     conf_ssid->ssid, conf_ssid->length);
1446 			/* Copy rest of the beacon data */
1447 			qdf_mem_copy((pbeacon + ssid_ie_end_offset +
1448 				      conf_ssid->length), tmp, tmplen);
1449 			qdf_mem_free(tmp);
1450 
1451 			/* Hidden ssid with all 0's */
1452 		} else if (ssid->ssid_len == conf_ssid->length) {
1453 			/* Insert the  SSID string */
1454 			qdf_mem_copy((pbeacon + ssid_ie_start_offset +
1455 				      sizeof(struct ie_header)),
1456 				      conf_ssid->ssid, conf_ssid->length);
1457 		} else {
1458 			scm_debug("mismatch in hidden ssid length");
1459 			return QDF_STATUS_E_INVAL;
1460 		}
1461 	}
1462 	return QDF_STATUS_SUCCESS;
1463 }
1464 #endif /* WLAN_DFS_CHAN_HIDDEN_SSID */
1465 
1466 #ifdef WLAN_ADAPTIVE_11R
1467 /**
1468  * scm_fill_adaptive_11r_cap() - Check if the AP supports adaptive 11r
1469  * @scan_entry: Pointer to the scan entry
1470  *
1471  * Return: true if adaptive 11r is advertised else false
1472  */
1473 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
1474 {
1475 	uint8_t *ie;
1476 	uint8_t data;
1477 	bool adaptive_11r;
1478 
1479 	ie = util_scan_entry_adaptive_11r(scan_entry);
1480 	if (!ie)
1481 		return;
1482 
1483 	data = *(ie + OUI_LENGTH);
1484 	adaptive_11r = (data & 0x1) ? true : false;
1485 
1486 	scan_entry->adaptive_11r_ap = adaptive_11r;
1487 }
1488 #else
1489 static void scm_fill_adaptive_11r_cap(struct scan_cache_entry *scan_entry)
1490 {
1491 	scan_entry->adaptive_11r_ap = false;
1492 }
1493 #endif
1494 
1495 static void util_scan_set_security(struct scan_cache_entry *scan_params)
1496 {
1497 	if (util_scan_entry_wpa(scan_params))
1498 		scan_params->security_type |= SCAN_SECURITY_TYPE_WPA;
1499 
1500 	if (util_scan_entry_rsn(scan_params))
1501 		scan_params->security_type |= SCAN_SECURITY_TYPE_RSN;
1502 	if (util_scan_entry_wapi(scan_params))
1503 		scan_params->security_type |= SCAN_SECURITY_TYPE_WAPI;
1504 
1505 	if (!scan_params->security_type &&
1506 	    scan_params->cap_info.wlan_caps.privacy)
1507 		scan_params->security_type |= SCAN_SECURITY_TYPE_WEP;
1508 }
1509 
1510 static QDF_STATUS
1511 util_scan_gen_scan_entry(struct wlan_objmgr_pdev *pdev,
1512 			 uint8_t *frame, qdf_size_t frame_len,
1513 			 uint32_t frm_subtype,
1514 			 struct mgmt_rx_event_params *rx_param,
1515 			 struct scan_mbssid_info *mbssid_info,
1516 			 qdf_list_t *scan_list)
1517 {
1518 	struct wlan_frame_hdr *hdr;
1519 	struct wlan_bcn_frame *bcn;
1520 	QDF_STATUS status = QDF_STATUS_SUCCESS;
1521 	struct ie_ssid *ssid;
1522 	struct scan_cache_entry *scan_entry;
1523 	struct qbss_load_ie *qbss_load;
1524 	struct scan_cache_node *scan_node;
1525 	uint8_t i;
1526 	qdf_freq_t chan_freq = 0;
1527 	bool he_6g_dup_bcon = false;
1528 	uint8_t band_mask;
1529 
1530 	scan_entry = qdf_mem_malloc_atomic(sizeof(*scan_entry));
1531 	if (!scan_entry) {
1532 		scm_err("failed to allocate memory for scan_entry");
1533 		return QDF_STATUS_E_NOMEM;
1534 	}
1535 	scan_entry->raw_frame.ptr =
1536 			qdf_mem_malloc_atomic(frame_len);
1537 	if (!scan_entry->raw_frame.ptr) {
1538 		scm_err("failed to allocate memory for frame");
1539 		qdf_mem_free(scan_entry);
1540 		return QDF_STATUS_E_NOMEM;
1541 	}
1542 
1543 	bcn = (struct wlan_bcn_frame *)
1544 			   (frame + sizeof(*hdr));
1545 	hdr = (struct wlan_frame_hdr *)frame;
1546 
1547 	/* update timestamp in nanoseconds needed by kernel layers */
1548 	scan_entry->boottime_ns = qdf_get_bootbased_boottime_ns();
1549 
1550 	scan_entry->frm_subtype = frm_subtype;
1551 	qdf_mem_copy(scan_entry->bssid.bytes,
1552 		hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1553 	/* Scr addr */
1554 	qdf_mem_copy(scan_entry->mac_addr.bytes,
1555 		hdr->i_addr2, QDF_MAC_ADDR_SIZE);
1556 	scan_entry->seq_num =
1557 		(le16toh(*(uint16_t *)hdr->i_seq) >> WLAN_SEQ_SEQ_SHIFT);
1558 
1559 	scan_entry->snr = rx_param->snr;
1560 	scan_entry->avg_snr = WLAN_SNR_IN(scan_entry->snr);
1561 	scan_entry->rssi_raw = rx_param->rssi;
1562 	scan_entry->avg_rssi = WLAN_RSSI_IN(scan_entry->rssi_raw);
1563 	scan_entry->tsf_delta = rx_param->tsf_delta;
1564 	scan_entry->pdev_id = wlan_objmgr_pdev_get_pdev_id(pdev);
1565 
1566 	/* Copy per chain rssi to scan entry */
1567 	qdf_mem_copy(scan_entry->per_chain_rssi, rx_param->rssi_ctl,
1568 		     WLAN_MGMT_TXRX_HOST_MAX_ANTENNA);
1569 	band_mask = BIT(wlan_reg_freq_to_band(rx_param->chan_freq));
1570 
1571 	if (!wlan_psoc_nif_fw_ext_cap_get(wlan_pdev_get_psoc(pdev),
1572 					  WLAN_SOC_CEXT_HW_DB2DBM)) {
1573 		for (i = 0; i < WLAN_MGMT_TXRX_HOST_MAX_ANTENNA; i++) {
1574 			if (scan_entry->per_chain_rssi[i] !=
1575 			    WLAN_INVALID_PER_CHAIN_SNR)
1576 				scan_entry->per_chain_rssi[i] +=
1577 						WLAN_NOISE_FLOOR_DBM_DEFAULT;
1578 			else
1579 				scan_entry->per_chain_rssi[i] =
1580 						WLAN_INVALID_PER_CHAIN_RSSI;
1581 		}
1582 	}
1583 
1584 	/* store jiffies */
1585 	scan_entry->rrm_parent_tsf = (uint32_t)qdf_system_ticks();
1586 
1587 	scan_entry->bcn_int = le16toh(bcn->beacon_interval);
1588 
1589 	/*
1590 	 * In case if the beacon dosnt have
1591 	 * valid beacon interval falback to def
1592 	 */
1593 	if (!scan_entry->bcn_int)
1594 		scan_entry->bcn_int = 100;
1595 	scan_entry->cap_info.value = le16toh(bcn->capability.value);
1596 	qdf_mem_copy(scan_entry->tsf_info.data,
1597 		bcn->timestamp, 8);
1598 	scan_entry->erp = ERP_NON_ERP_PRESENT;
1599 
1600 	scan_entry->scan_entry_time =
1601 		qdf_mc_timer_get_system_time();
1602 
1603 	scan_entry->raw_frame.len = frame_len;
1604 	qdf_mem_copy(scan_entry->raw_frame.ptr,
1605 		frame, frame_len);
1606 	status = util_scan_populate_bcn_ie_list(pdev, scan_entry, &chan_freq,
1607 						band_mask);
1608 	if (QDF_IS_STATUS_ERROR(status)) {
1609 		scm_debug("%pM: failed to parse beacon IE",
1610 			  scan_entry->bssid.bytes);
1611 		qdf_mem_free(scan_entry->raw_frame.ptr);
1612 		qdf_mem_free(scan_entry);
1613 		return QDF_STATUS_E_FAILURE;
1614 	}
1615 
1616 	ssid = (struct ie_ssid *)
1617 		scan_entry->ie_list.ssid;
1618 
1619 	if (ssid && (ssid->ssid_len > WLAN_SSID_MAX_LEN)) {
1620 		qdf_mem_free(scan_entry->raw_frame.ptr);
1621 		qdf_mem_free(scan_entry);
1622 		return QDF_STATUS_E_FAILURE;
1623 	}
1624 
1625 	if (scan_entry->ie_list.p2p)
1626 		scan_entry->is_p2p = true;
1627 
1628 	if (!chan_freq && util_scan_entry_hecap(scan_entry)) {
1629 		status = util_scan_get_chan_from_he_6g_params(pdev, scan_entry,
1630 							      &chan_freq,
1631 							      &he_6g_dup_bcon,
1632 							      band_mask);
1633 		if (QDF_IS_STATUS_ERROR(status)) {
1634 			qdf_mem_free(scan_entry->raw_frame.ptr);
1635 			qdf_mem_free(scan_entry);
1636 			return QDF_STATUS_E_FAILURE;
1637 		}
1638 	}
1639 
1640 	if (chan_freq)
1641 		scan_entry->channel.chan_freq = chan_freq;
1642 
1643 	/* If no channel info is present in beacon use meta channel */
1644 	if (!scan_entry->channel.chan_freq) {
1645 		scan_entry->channel.chan_freq = rx_param->chan_freq;
1646 	} else if (rx_param->chan_freq !=
1647 	   scan_entry->channel.chan_freq) {
1648 		if (!wlan_reg_is_49ghz_freq(scan_entry->channel.chan_freq) &&
1649 		    !he_6g_dup_bcon)
1650 			scan_entry->channel_mismatch = true;
1651 	}
1652 
1653 	if (util_scan_is_hidden_ssid(ssid)) {
1654 		scan_entry->ie_list.ssid = NULL;
1655 		scan_entry->is_hidden_ssid = true;
1656 	} else {
1657 		qdf_mem_copy(scan_entry->ssid.ssid,
1658 				ssid->ssid, ssid->ssid_len);
1659 		scan_entry->ssid.length = ssid->ssid_len;
1660 		scan_entry->hidden_ssid_timestamp =
1661 			scan_entry->scan_entry_time;
1662 	}
1663 	qdf_mem_copy(&scan_entry->mbssid_info, mbssid_info,
1664 		     sizeof(scan_entry->mbssid_info));
1665 
1666 	scan_entry->phy_mode = util_scan_get_phymode(pdev, scan_entry);
1667 
1668 	scan_entry->nss = util_scan_scm_calc_nss_supported_by_ap(scan_entry);
1669 	scm_fill_adaptive_11r_cap(scan_entry);
1670 	util_scan_set_security(scan_entry);
1671 
1672 	util_scan_scm_update_bss_with_esp_data(scan_entry);
1673 	qbss_load = (struct qbss_load_ie *)
1674 			util_scan_entry_qbssload(scan_entry);
1675 	if (qbss_load)
1676 		scan_entry->qbss_chan_load = qbss_load->qbss_chan_load;
1677 
1678 	scan_node = qdf_mem_malloc_atomic(sizeof(*scan_node));
1679 	if (!scan_node) {
1680 		qdf_mem_free(scan_entry->raw_frame.ptr);
1681 		qdf_mem_free(scan_entry);
1682 		return QDF_STATUS_E_FAILURE;
1683 	}
1684 
1685 	scan_node->entry = scan_entry;
1686 	qdf_list_insert_front(scan_list, &scan_node->node);
1687 
1688 	return status;
1689 }
1690 
1691 /**
1692  * util_scan_find_ie() - find information element
1693  * @eid: element id
1694  * @ies: pointer consisting of IEs
1695  * @len: IE length
1696  *
1697  * Return: NULL if the element ID is not found or if IE pointer is NULL else
1698  * pointer to the first byte of the requested element
1699  */
1700 static uint8_t *util_scan_find_ie(uint8_t eid, uint8_t *ies,
1701 				  int32_t len)
1702 {
1703 	if (!ies)
1704 		return NULL;
1705 
1706 	while (len >= 2 && len >= ies[1] + 2) {
1707 		if (ies[0] == eid)
1708 			return ies;
1709 		len -= ies[1] + 2;
1710 		ies += ies[1] + 2;
1711 	}
1712 
1713 	return NULL;
1714 }
1715 
1716 #ifdef WLAN_FEATURE_MBSSID
1717 static void util_gen_new_bssid(uint8_t *bssid, uint8_t max_bssid,
1718 			       uint8_t mbssid_index,
1719 			       uint8_t *new_bssid_addr)
1720 {
1721 	uint8_t lsb_n;
1722 	int i;
1723 
1724 	for (i = 0; i < QDF_MAC_ADDR_SIZE; i++)
1725 		new_bssid_addr[i] = bssid[i];
1726 
1727 	lsb_n = new_bssid_addr[5] & ((1 << max_bssid) - 1);
1728 
1729 	new_bssid_addr[5] &= ~((1 << max_bssid) - 1);
1730 	new_bssid_addr[5] |= (lsb_n + mbssid_index) % (1 << max_bssid);
1731 }
1732 
1733 static uint32_t util_gen_new_ie(uint8_t *ie, uint32_t ielen,
1734 				uint8_t *subelement,
1735 				size_t subie_len, uint8_t *new_ie)
1736 {
1737 	uint8_t *pos, *tmp;
1738 	const uint8_t *tmp_old, *tmp_new;
1739 	uint8_t *sub_copy;
1740 
1741 	/* copy subelement as we need to change its content to
1742 	 * mark an ie after it is processed.
1743 	 */
1744 	sub_copy = qdf_mem_malloc(subie_len);
1745 	if (!sub_copy)
1746 		return 0;
1747 	qdf_mem_copy(sub_copy, subelement, subie_len);
1748 
1749 	pos = &new_ie[0];
1750 
1751 	/* new ssid */
1752 	tmp_new = util_scan_find_ie(WLAN_ELEMID_SSID, sub_copy, subie_len);
1753 	if (tmp_new) {
1754 		qdf_mem_copy(pos, tmp_new, tmp_new[1] + 2);
1755 		pos += (tmp_new[1] + 2);
1756 	}
1757 
1758 	/* go through IEs in ie (skip SSID) and subelement,
1759 	 * merge them into new_ie
1760 	 */
1761 	tmp_old = util_scan_find_ie(WLAN_ELEMID_SSID, ie, ielen);
1762 	tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
1763 
1764 	while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
1765 		if (tmp_old[0] == 0) {
1766 			tmp_old++;
1767 			continue;
1768 		}
1769 
1770 		tmp = (uint8_t *)util_scan_find_ie(tmp_old[0], sub_copy,
1771 				subie_len);
1772 		if (!tmp) {
1773 			/* ie in old ie but not in subelement */
1774 			if (tmp_old[0] != WLAN_ELEMID_MULTIPLE_BSSID) {
1775 				qdf_mem_copy(pos, tmp_old, tmp_old[1] + 2);
1776 				pos += tmp_old[1] + 2;
1777 			}
1778 		} else {
1779 			/* ie in transmitting ie also in subelement,
1780 			 * copy from subelement and flag the ie in subelement
1781 			 * as copied (by setting eid field to 0xff). For
1782 			 * vendor ie, compare OUI + type + subType to
1783 			 * determine if they are the same ie.
1784 			 */
1785 			if (tmp_old[0] == WLAN_ELEMID_VENDOR) {
1786 				if (!qdf_mem_cmp(tmp_old + 2, tmp + 2, 5)) {
1787 					/* same vendor ie, copy from
1788 					 * subelement
1789 					 */
1790 					qdf_mem_copy(pos, tmp, tmp[1] + 2);
1791 					pos += tmp[1] + 2;
1792 					tmp[0] = 0xff;
1793 				} else {
1794 					qdf_mem_copy(pos, tmp_old,
1795 						     tmp_old[1] + 2);
1796 					pos += tmp_old[1] + 2;
1797 				}
1798 			} else {
1799 				/* copy ie from subelement into new ie */
1800 				qdf_mem_copy(pos, tmp, tmp[1] + 2);
1801 				pos += tmp[1] + 2;
1802 				tmp[0] = 0xff;
1803 			}
1804 		}
1805 
1806 		if (tmp_old + tmp_old[1] + 2 - ie == ielen)
1807 			break;
1808 
1809 		tmp_old += tmp_old[1] + 2;
1810 	}
1811 
1812 	/* go through subelement again to check if there is any ie not
1813 	 * copied to new ie, skip ssid, capability, bssid-index ie
1814 	 */
1815 	tmp_new = sub_copy;
1816 	while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
1817 		if (!(tmp_new[0] == WLAN_ELEMID_NONTX_BSSID_CAP ||
1818 		      tmp_new[0] == WLAN_ELEMID_SSID ||
1819 		      tmp_new[0] == WLAN_ELEMID_MULTI_BSSID_IDX ||
1820 		      tmp_new[0] == 0xff)) {
1821 			qdf_mem_copy(pos, tmp_new, tmp_new[1] + 2);
1822 			pos += tmp_new[1] + 2;
1823 		}
1824 		if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
1825 			break;
1826 		tmp_new += tmp_new[1] + 2;
1827 	}
1828 
1829 	qdf_mem_free(sub_copy);
1830 	return pos - new_ie;
1831 }
1832 
1833 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
1834 					 uint8_t *frame, qdf_size_t frame_len,
1835 					 uint32_t frm_subtype,
1836 					 struct mgmt_rx_event_params *rx_param,
1837 					 qdf_list_t *scan_list)
1838 {
1839 	struct wlan_bcn_frame *bcn;
1840 	struct wlan_frame_hdr *hdr;
1841 	struct scan_mbssid_info mbssid_info;
1842 	QDF_STATUS status;
1843 	uint8_t *pos, *subelement, *mbssid_end_pos;
1844 	uint8_t *tmp, *mbssid_index_ie;
1845 	uint32_t subie_len, new_ie_len;
1846 	uint8_t new_bssid[QDF_MAC_ADDR_SIZE], bssid[QDF_MAC_ADDR_SIZE];
1847 	uint8_t *new_ie;
1848 	uint8_t *ie, *new_frame = NULL;
1849 	uint64_t ielen, new_frame_len;
1850 
1851 	hdr = (struct wlan_frame_hdr *)frame;
1852 	bcn = (struct wlan_bcn_frame *)(frame + sizeof(struct wlan_frame_hdr));
1853 	ie = (uint8_t *)&bcn->ie;
1854 	ielen = (uint16_t)(frame_len -
1855 		sizeof(struct wlan_frame_hdr) -
1856 		offsetof(struct wlan_bcn_frame, ie));
1857 	qdf_mem_copy(bssid, hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1858 
1859 	if (!util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, ie, ielen))
1860 		return QDF_STATUS_E_FAILURE;
1861 
1862 	pos = ie;
1863 
1864 	new_ie = qdf_mem_malloc(MAX_IE_LEN);
1865 	if (!new_ie)
1866 		return QDF_STATUS_E_NOMEM;
1867 
1868 	while (pos < ie + ielen + 2) {
1869 		tmp = util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID, pos,
1870 					ielen - (pos - ie));
1871 		if (!tmp)
1872 			break;
1873 
1874 		mbssid_info.profile_count = 1 << tmp[2];
1875 		mbssid_end_pos = tmp + tmp[1] + 2;
1876 		/* Skip Element ID, Len, MaxBSSID Indicator */
1877 		if (tmp[1] < 4)
1878 			break;
1879 		for (subelement = tmp + 3; subelement < mbssid_end_pos - 1;
1880 		     subelement += 2 + subelement[1]) {
1881 			subie_len = subelement[1];
1882 			if (mbssid_end_pos - subelement < 2 + subie_len)
1883 				break;
1884 			if (subelement[0] != 0 || subelement[1] < 4) {
1885 				/* not a valid BSS profile */
1886 				continue;
1887 			}
1888 
1889 			if (subelement[2] != WLAN_ELEMID_NONTX_BSSID_CAP ||
1890 			    subelement[3] != 2) {
1891 				/* The first element within the Nontransmitted
1892 				 * BSSID Profile is not the Nontransmitted
1893 				 * BSSID Capability element.
1894 				 */
1895 				continue;
1896 			}
1897 
1898 			/* found a Nontransmitted BSSID Profile */
1899 			mbssid_index_ie =
1900 				util_scan_find_ie(WLAN_ELEMID_MULTI_BSSID_IDX,
1901 						  subelement + 2, subie_len);
1902 			if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
1903 			    mbssid_index_ie[2] == 0) {
1904 				/* No valid Multiple BSSID-Index element */
1905 				continue;
1906 			}
1907 			qdf_mem_copy(&mbssid_info.trans_bssid, bssid,
1908 				     QDF_MAC_ADDR_SIZE);
1909 			mbssid_info.profile_num = mbssid_index_ie[2];
1910 			util_gen_new_bssid(bssid, tmp[2], mbssid_index_ie[2],
1911 					   new_bssid);
1912 			new_ie_len = util_gen_new_ie(ie, ielen, subelement + 2,
1913 						     subie_len, new_ie);
1914 			if (!new_ie_len)
1915 				continue;
1916 
1917 			new_frame_len = frame_len - ielen + new_ie_len;
1918 			new_frame = qdf_mem_malloc(new_frame_len);
1919 			if (!new_frame) {
1920 				qdf_mem_free(new_ie);
1921 				return QDF_STATUS_E_NOMEM;
1922 			}
1923 
1924 			/*
1925 			 * Copy the header(24byte), timestamp(8 byte),
1926 			 * beaconinterval(2byte) and capability(2byte)
1927 			 */
1928 			qdf_mem_copy(new_frame, frame, 36);
1929 			/* Copy the new ie generated from MBSSID profile*/
1930 			hdr = (struct wlan_frame_hdr *)new_frame;
1931 			qdf_mem_copy(hdr->i_addr2, new_bssid,
1932 				     QDF_MAC_ADDR_SIZE);
1933 			qdf_mem_copy(hdr->i_addr3, new_bssid,
1934 				     QDF_MAC_ADDR_SIZE);
1935 			/* Copy the new ie generated from MBSSID profile*/
1936 			qdf_mem_copy(new_frame +
1937 					offsetof(struct wlan_bcn_frame, ie) +
1938 					sizeof(struct wlan_frame_hdr),
1939 					new_ie, new_ie_len);
1940 			status = util_scan_gen_scan_entry(pdev, new_frame,
1941 							  new_frame_len,
1942 							  frm_subtype,
1943 							  rx_param,
1944 							  &mbssid_info,
1945 							  scan_list);
1946 			if (QDF_IS_STATUS_ERROR(status)) {
1947 				qdf_mem_free(new_frame);
1948 				scm_err("failed to generate a scan entry");
1949 				break;
1950 			}
1951 			/* scan entry makes its own copy so free the frame*/
1952 			qdf_mem_free(new_frame);
1953 		}
1954 
1955 		pos = mbssid_end_pos;
1956 	}
1957 	qdf_mem_free(new_ie);
1958 
1959 	return QDF_STATUS_SUCCESS;
1960 }
1961 #else
1962 static QDF_STATUS util_scan_parse_mbssid(struct wlan_objmgr_pdev *pdev,
1963 					 uint8_t *frame, qdf_size_t frame_len,
1964 					 uint32_t frm_subtype,
1965 					 struct mgmt_rx_event_params *rx_param,
1966 					 qdf_list_t *scan_list)
1967 {
1968 	return QDF_STATUS_SUCCESS;
1969 }
1970 #endif
1971 
1972 static QDF_STATUS
1973 util_scan_parse_beacon_frame(struct wlan_objmgr_pdev *pdev,
1974 			     uint8_t *frame,
1975 			     qdf_size_t frame_len,
1976 			     uint32_t frm_subtype,
1977 			     struct mgmt_rx_event_params *rx_param,
1978 			     qdf_list_t *scan_list)
1979 {
1980 	struct wlan_bcn_frame *bcn;
1981 	struct wlan_frame_hdr *hdr;
1982 	uint8_t *mbssid_ie = NULL;
1983 	uint32_t ie_len = 0;
1984 	QDF_STATUS status;
1985 	struct scan_mbssid_info mbssid_info = { 0 };
1986 
1987 	hdr = (struct wlan_frame_hdr *)frame;
1988 	bcn = (struct wlan_bcn_frame *)
1989 			   (frame + sizeof(struct wlan_frame_hdr));
1990 	ie_len = (uint16_t)(frame_len -
1991 		sizeof(struct wlan_frame_hdr) -
1992 		offsetof(struct wlan_bcn_frame, ie));
1993 
1994 	mbssid_ie = util_scan_find_ie(WLAN_ELEMID_MULTIPLE_BSSID,
1995 				      (uint8_t *)&bcn->ie, ie_len);
1996 	if (mbssid_ie) {
1997 		qdf_mem_copy(&mbssid_info.trans_bssid,
1998 			     hdr->i_addr3, QDF_MAC_ADDR_SIZE);
1999 		mbssid_info.profile_count = 1 << mbssid_ie[2];
2000 	}
2001 
2002 	status = util_scan_gen_scan_entry(pdev, frame, frame_len,
2003 					  frm_subtype, rx_param,
2004 					  &mbssid_info,
2005 					  scan_list);
2006 
2007 	/*
2008 	 * IF MBSSID IE is present in the beacon then
2009 	 * scan component will create a new entry for
2010 	 * each BSSID found in the MBSSID
2011 	 */
2012 	if (mbssid_ie)
2013 		status = util_scan_parse_mbssid(pdev, frame, frame_len,
2014 						frm_subtype, rx_param,
2015 						scan_list);
2016 
2017 	if (QDF_IS_STATUS_ERROR(status))
2018 		scm_debug_rl("Failed to create a scan entry");
2019 
2020 	return status;
2021 }
2022 
2023 qdf_list_t *
2024 util_scan_unpack_beacon_frame(struct wlan_objmgr_pdev *pdev, uint8_t *frame,
2025 			      qdf_size_t frame_len, uint32_t frm_subtype,
2026 			      struct mgmt_rx_event_params *rx_param)
2027 {
2028 	qdf_list_t *scan_list;
2029 	QDF_STATUS status;
2030 
2031 	scan_list = qdf_mem_malloc_atomic(sizeof(*scan_list));
2032 	if (!scan_list) {
2033 		scm_err("failed to allocate scan_list");
2034 		return NULL;
2035 	}
2036 	qdf_list_create(scan_list, MAX_SCAN_CACHE_SIZE);
2037 
2038 	status = util_scan_parse_beacon_frame(pdev, frame, frame_len,
2039 					      frm_subtype, rx_param,
2040 					      scan_list);
2041 	if (QDF_IS_STATUS_ERROR(status)) {
2042 		ucfg_scan_purge_results(scan_list);
2043 		return NULL;
2044 	}
2045 
2046 	return scan_list;
2047 }
2048 
2049 QDF_STATUS
2050 util_scan_entry_update_mlme_info(struct wlan_objmgr_pdev *pdev,
2051 	struct scan_cache_entry *scan_entry)
2052 {
2053 
2054 	if (!pdev || !scan_entry) {
2055 		scm_err("pdev 0x%pK, scan_entry: 0x%pK", pdev, scan_entry);
2056 		return QDF_STATUS_E_INVAL;
2057 	}
2058 
2059 	return scm_update_scan_mlme_info(pdev, scan_entry);
2060 }
2061 
2062 bool util_is_scan_completed(struct scan_event *event, bool *success)
2063 {
2064 	if ((event->type == SCAN_EVENT_TYPE_COMPLETED) ||
2065 	    (event->type == SCAN_EVENT_TYPE_DEQUEUED) ||
2066 	    (event->type == SCAN_EVENT_TYPE_START_FAILED)) {
2067 		if ((event->type == SCAN_EVENT_TYPE_COMPLETED) &&
2068 		    (event->reason == SCAN_REASON_COMPLETED))
2069 			*success = true;
2070 		else
2071 			*success = false;
2072 
2073 		return true;
2074 	}
2075 
2076 	*success = false;
2077 	return false;
2078 }
2079 
2080