]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/net/wireless/marvell/mwifiex/scan.c
mwifiex: drop 'add_tail' param from mwifiex_insert_cmd_to_pending_q()
[thirdparty/linux.git] / drivers / net / wireless / marvell / mwifiex / scan.c
CommitLineData
5e6e3a92
BZ
1/*
2 * Marvell Wireless LAN device driver: scan ioctl and command handling
3 *
65da33f5 4 * Copyright (C) 2011-2014, Marvell International Ltd.
5e6e3a92
BZ
5 *
6 * This software file (the "File") is distributed by Marvell International
7 * Ltd. under the terms of the GNU General Public License Version 2, June 1991
8 * (the "License"). You may use, redistribute and/or modify this File in
9 * accordance with the terms and conditions of the License, a copy of which
10 * is available by writing to the Free Software Foundation, Inc.,
11 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
12 * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
13 *
14 * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
16 * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
17 * this warranty disclaimer.
18 */
19
20#include "decl.h"
21#include "ioctl.h"
22#include "util.h"
23#include "fw.h"
24#include "main.h"
25#include "11n.h"
26#include "cfg80211.h"
27
28/* The maximum number of channels the firmware can scan per command */
29#define MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN 14
30
658f37b7 31#define MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD 4
5e6e3a92
BZ
32
33/* Memory needed to store a max sized Channel List TLV for a firmware scan */
34#define CHAN_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_header) \
35 + (MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN \
36 *sizeof(struct mwifiex_chan_scan_param_set)))
37
38/* Memory needed to store supported rate */
39#define RATE_TLV_MAX_SIZE (sizeof(struct mwifiex_ie_types_rates_param_set) \
40 + HOSTCMD_SUPPORTED_RATES)
41
42/* Memory needed to store a max number/size WildCard SSID TLV for a firmware
43 scan */
44#define WILDCARD_SSID_TLV_MAX_SIZE \
45 (MWIFIEX_MAX_SSID_LIST_LENGTH * \
46 (sizeof(struct mwifiex_ie_types_wildcard_ssid_params) \
47 + IEEE80211_MAX_SSID_LEN))
48
49/* Maximum memory needed for a mwifiex_scan_cmd_config with all TLVs at max */
50#define MAX_SCAN_CFG_ALLOC (sizeof(struct mwifiex_scan_cmd_config) \
51 + sizeof(struct mwifiex_ie_types_num_probes) \
52 + sizeof(struct mwifiex_ie_types_htcap) \
53 + CHAN_TLV_MAX_SIZE \
54 + RATE_TLV_MAX_SIZE \
55 + WILDCARD_SSID_TLV_MAX_SIZE)
56
57
58union mwifiex_scan_cmd_config_tlv {
59 /* Scan configuration (variable length) */
60 struct mwifiex_scan_cmd_config config;
61 /* Max allocated block */
62 u8 config_alloc_buf[MAX_SCAN_CFG_ALLOC];
63};
64
65enum cipher_suite {
66 CIPHER_SUITE_TKIP,
67 CIPHER_SUITE_CCMP,
68 CIPHER_SUITE_MAX
69};
70static u8 mwifiex_wpa_oui[CIPHER_SUITE_MAX][4] = {
71 { 0x00, 0x50, 0xf2, 0x02 }, /* TKIP */
72 { 0x00, 0x50, 0xf2, 0x04 }, /* AES */
73};
74static u8 mwifiex_rsn_oui[CIPHER_SUITE_MAX][4] = {
75 { 0x00, 0x0f, 0xac, 0x02 }, /* TKIP */
76 { 0x00, 0x0f, 0xac, 0x04 }, /* AES */
77};
78
679b687b
AF
79static void
80_dbg_security_flags(int log_level, const char *func, const char *desc,
81 struct mwifiex_private *priv,
82 struct mwifiex_bssdescriptor *bss_desc)
83{
84 _mwifiex_dbg(priv->adapter, log_level,
85 "info: %s: %s:\twpa_ie=%#x wpa2_ie=%#x WEP=%s WPA=%s WPA2=%s\tEncMode=%#x privacy=%#x\n",
86 func, desc,
87 bss_desc->bcn_wpa_ie ?
88 bss_desc->bcn_wpa_ie->vend_hdr.element_id : 0,
89 bss_desc->bcn_rsn_ie ?
90 bss_desc->bcn_rsn_ie->ieee_hdr.element_id : 0,
91 priv->sec_info.wep_enabled ? "e" : "d",
92 priv->sec_info.wpa_enabled ? "e" : "d",
93 priv->sec_info.wpa2_enabled ? "e" : "d",
94 priv->sec_info.encryption_mode,
95 bss_desc->privacy);
96}
97#define dbg_security_flags(mask, desc, priv, bss_desc) \
98 _dbg_security_flags(MWIFIEX_DBG_##mask, desc, __func__, priv, bss_desc)
99
38329568
AF
100static bool
101has_ieee_hdr(struct ieee_types_generic *ie, u8 key)
102{
103 return (ie && ie->ieee_hdr.element_id == key);
104}
105
106static bool
107has_vendor_hdr(struct ieee_types_vendor_specific *ie, u8 key)
108{
109 return (ie && ie->vend_hdr.element_id == key);
110}
111
5e6e3a92
BZ
112/*
113 * This function parses a given IE for a given OUI.
114 *
115 * This is used to parse a WPA/RSN IE to find if it has
116 * a given oui in PTK.
117 */
118static u8
119mwifiex_search_oui_in_ie(struct ie_body *iebody, u8 *oui)
120{
121 u8 count;
122
123 count = iebody->ptk_cnt[0];
124
125 /* There could be multiple OUIs for PTK hence
126 1) Take the length.
127 2) Check all the OUIs for AES.
128 3) If one of them is AES then pass success. */
129 while (count) {
130 if (!memcmp(iebody->ptk_body, oui, sizeof(iebody->ptk_body)))
131 return MWIFIEX_OUI_PRESENT;
132
133 --count;
134 if (count)
135 iebody = (struct ie_body *) ((u8 *) iebody +
136 sizeof(iebody->ptk_body));
137 }
138
139 pr_debug("info: %s: OUI is not found in PTK\n", __func__);
140 return MWIFIEX_OUI_NOT_PRESENT;
141}
142
143/*
144 * This function checks if a given OUI is present in a RSN IE.
145 *
146 * The function first checks if a RSN IE is present or not in the
147 * BSS descriptor. It tries to locate the OUI only if such an IE is
148 * present.
149 */
150static u8
151mwifiex_is_rsn_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
152{
270e58e8
YAP
153 u8 *oui;
154 struct ie_body *iebody;
5e6e3a92
BZ
155 u8 ret = MWIFIEX_OUI_NOT_PRESENT;
156
38329568 157 if (has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN)) {
5e6e3a92
BZ
158 iebody = (struct ie_body *)
159 (((u8 *) bss_desc->bcn_rsn_ie->data) +
cff23cec 160 RSN_GTK_OUI_OFFSET);
5e6e3a92
BZ
161 oui = &mwifiex_rsn_oui[cipher][0];
162 ret = mwifiex_search_oui_in_ie(iebody, oui);
163 if (ret)
164 return ret;
165 }
166 return ret;
167}
168
169/*
170 * This function checks if a given OUI is present in a WPA IE.
171 *
172 * The function first checks if a WPA IE is present or not in the
173 * BSS descriptor. It tries to locate the OUI only if such an IE is
174 * present.
175 */
176static u8
177mwifiex_is_wpa_oui_present(struct mwifiex_bssdescriptor *bss_desc, u32 cipher)
178{
270e58e8
YAP
179 u8 *oui;
180 struct ie_body *iebody;
5e6e3a92
BZ
181 u8 ret = MWIFIEX_OUI_NOT_PRESENT;
182
38329568 183 if (has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC)) {
5e6e3a92
BZ
184 iebody = (struct ie_body *) bss_desc->bcn_wpa_ie->data;
185 oui = &mwifiex_wpa_oui[cipher][0];
186 ret = mwifiex_search_oui_in_ie(iebody, oui);
187 if (ret)
188 return ret;
189 }
190 return ret;
191}
192
193/*
194 * This function compares two SSIDs and checks if they match.
195 */
196s32
b9be5f39 197mwifiex_ssid_cmp(struct cfg80211_ssid *ssid1, struct cfg80211_ssid *ssid2)
5e6e3a92
BZ
198{
199 if (!ssid1 || !ssid2 || (ssid1->ssid_len != ssid2->ssid_len))
200 return -1;
201 return memcmp(ssid1->ssid, ssid2->ssid, ssid1->ssid_len);
202}
203
5e6e3a92
BZ
204/*
205 * This function checks if wapi is enabled in driver and scanned network is
206 * compatible with it.
207 */
208static bool
cff23cec
YAP
209mwifiex_is_bss_wapi(struct mwifiex_private *priv,
210 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92
BZ
211{
212 if (priv->sec_info.wapi_enabled &&
38329568 213 has_ieee_hdr(bss_desc->bcn_wapi_ie, WLAN_EID_BSS_AC_ACCESS_DELAY))
5e6e3a92 214 return true;
5e6e3a92
BZ
215 return false;
216}
217
218/*
219 * This function checks if driver is configured with no security mode and
220 * scanned network is compatible with it.
221 */
222static bool
cff23cec
YAP
223mwifiex_is_bss_no_sec(struct mwifiex_private *priv,
224 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 225{
5eb02e44 226 if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
0a233c98 227 !priv->sec_info.wpa2_enabled &&
38329568
AF
228 !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
229 !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
cff23cec 230 !priv->sec_info.encryption_mode && !bss_desc->privacy) {
5e6e3a92
BZ
231 return true;
232 }
233 return false;
234}
235
236/*
237 * This function checks if static WEP is enabled in driver and scanned network
238 * is compatible with it.
239 */
240static bool
cff23cec
YAP
241mwifiex_is_bss_static_wep(struct mwifiex_private *priv,
242 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 243{
5eb02e44
AK
244 if (priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
245 !priv->sec_info.wpa2_enabled && bss_desc->privacy) {
5e6e3a92
BZ
246 return true;
247 }
248 return false;
249}
250
251/*
252 * This function checks if wpa is enabled in driver and scanned network is
253 * compatible with it.
254 */
255static bool
cff23cec
YAP
256mwifiex_is_bss_wpa(struct mwifiex_private *priv,
257 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 258{
5eb02e44 259 if (!priv->sec_info.wep_enabled && priv->sec_info.wpa_enabled &&
0a233c98 260 !priv->sec_info.wpa2_enabled &&
38329568 261 has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC)
5e6e3a92
BZ
262 /*
263 * Privacy bit may NOT be set in some APs like
264 * LinkSys WRT54G && bss_desc->privacy
265 */
266 ) {
679b687b 267 dbg_security_flags(INFO, "WPA", priv, bss_desc);
5e6e3a92
BZ
268 return true;
269 }
270 return false;
271}
272
273/*
274 * This function checks if wpa2 is enabled in driver and scanned network is
275 * compatible with it.
276 */
277static bool
cff23cec
YAP
278mwifiex_is_bss_wpa2(struct mwifiex_private *priv,
279 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 280{
0a233c98 281 if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
cff23cec 282 priv->sec_info.wpa2_enabled &&
38329568 283 has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN)) {
cff23cec
YAP
284 /*
285 * Privacy bit may NOT be set in some APs like
286 * LinkSys WRT54G && bss_desc->privacy
287 */
679b687b 288 dbg_security_flags(INFO, "WAP2", priv, bss_desc);
5e6e3a92
BZ
289 return true;
290 }
291 return false;
292}
293
294/*
295 * This function checks if adhoc AES is enabled in driver and scanned network is
296 * compatible with it.
297 */
298static bool
cff23cec
YAP
299mwifiex_is_bss_adhoc_aes(struct mwifiex_private *priv,
300 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 301{
5eb02e44 302 if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
cff23cec 303 !priv->sec_info.wpa2_enabled &&
38329568
AF
304 !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
305 !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
cff23cec 306 !priv->sec_info.encryption_mode && bss_desc->privacy) {
5e6e3a92
BZ
307 return true;
308 }
309 return false;
310}
311
312/*
313 * This function checks if dynamic WEP is enabled in driver and scanned network
314 * is compatible with it.
315 */
316static bool
cff23cec
YAP
317mwifiex_is_bss_dynamic_wep(struct mwifiex_private *priv,
318 struct mwifiex_bssdescriptor *bss_desc)
5e6e3a92 319{
5eb02e44 320 if (!priv->sec_info.wep_enabled && !priv->sec_info.wpa_enabled &&
cff23cec 321 !priv->sec_info.wpa2_enabled &&
38329568
AF
322 !has_vendor_hdr(bss_desc->bcn_wpa_ie, WLAN_EID_VENDOR_SPECIFIC) &&
323 !has_ieee_hdr(bss_desc->bcn_rsn_ie, WLAN_EID_RSN) &&
cff23cec 324 priv->sec_info.encryption_mode && bss_desc->privacy) {
679b687b 325 dbg_security_flags(INFO, "dynamic", priv, bss_desc);
5e6e3a92
BZ
326 return true;
327 }
328 return false;
329}
330
331/*
332 * This function checks if a scanned network is compatible with the driver
333 * settings.
334 *
335 * WEP WPA WPA2 ad-hoc encrypt Network
336 * enabled enabled enabled AES mode Privacy WPA WPA2 Compatible
337 * 0 0 0 0 NONE 0 0 0 yes No security
338 * 0 1 0 0 x 1x 1 x yes WPA (disable
339 * HT if no AES)
340 * 0 0 1 0 x 1x x 1 yes WPA2 (disable
341 * HT if no AES)
342 * 0 0 0 1 NONE 1 0 0 yes Ad-hoc AES
343 * 1 0 0 0 NONE 1 0 0 yes Static WEP
344 * (disable HT)
345 * 0 0 0 0 !=NONE 1 0 0 yes Dynamic WEP
346 *
347 * Compatibility is not matched while roaming, except for mode.
348 */
349static s32
7c6fa2a8
AK
350mwifiex_is_network_compatible(struct mwifiex_private *priv,
351 struct mwifiex_bssdescriptor *bss_desc, u32 mode)
5e6e3a92
BZ
352{
353 struct mwifiex_adapter *adapter = priv->adapter;
5e6e3a92 354
5e6e3a92
BZ
355 bss_desc->disable_11n = false;
356
357 /* Don't check for compatibility if roaming */
cff23cec
YAP
358 if (priv->media_connected &&
359 (priv->bss_mode == NL80211_IFTYPE_STATION) &&
360 (bss_desc->bss_mode == NL80211_IFTYPE_STATION))
7c6fa2a8 361 return 0;
5e6e3a92
BZ
362
363 if (priv->wps.session_enable) {
acebe8c1
ZL
364 mwifiex_dbg(adapter, IOCTL,
365 "info: return success directly in WPS period\n");
7c6fa2a8 366 return 0;
5e6e3a92
BZ
367 }
368
2a7305c8 369 if (bss_desc->chan_sw_ie_present) {
acebe8c1
ZL
370 mwifiex_dbg(adapter, INFO,
371 "Don't connect to AP with WLAN_EID_CHANNEL_SWITCH\n");
2a7305c8
AK
372 return -1;
373 }
374
cff23cec 375 if (mwifiex_is_bss_wapi(priv, bss_desc)) {
acebe8c1
ZL
376 mwifiex_dbg(adapter, INFO,
377 "info: return success for WAPI AP\n");
7c6fa2a8 378 return 0;
5e6e3a92
BZ
379 }
380
381 if (bss_desc->bss_mode == mode) {
cff23cec 382 if (mwifiex_is_bss_no_sec(priv, bss_desc)) {
5e6e3a92 383 /* No security */
7c6fa2a8 384 return 0;
cff23cec 385 } else if (mwifiex_is_bss_static_wep(priv, bss_desc)) {
5e6e3a92 386 /* Static WEP enabled */
acebe8c1
ZL
387 mwifiex_dbg(adapter, INFO,
388 "info: Disable 11n in WEP mode.\n");
5e6e3a92 389 bss_desc->disable_11n = true;
7c6fa2a8 390 return 0;
cff23cec 391 } else if (mwifiex_is_bss_wpa(priv, bss_desc)) {
5e6e3a92 392 /* WPA enabled */
cff23cec
YAP
393 if (((priv->adapter->config_bands & BAND_GN ||
394 priv->adapter->config_bands & BAND_AN) &&
395 bss_desc->bcn_ht_cap) &&
396 !mwifiex_is_wpa_oui_present(bss_desc,
397 CIPHER_SUITE_CCMP)) {
398
399 if (mwifiex_is_wpa_oui_present
400 (bss_desc, CIPHER_SUITE_TKIP)) {
acebe8c1
ZL
401 mwifiex_dbg(adapter, INFO,
402 "info: Disable 11n if AES\t"
403 "is not supported by AP\n");
5e6e3a92
BZ
404 bss_desc->disable_11n = true;
405 } else {
406 return -1;
407 }
408 }
7c6fa2a8 409 return 0;
cff23cec 410 } else if (mwifiex_is_bss_wpa2(priv, bss_desc)) {
5e6e3a92 411 /* WPA2 enabled */
cff23cec
YAP
412 if (((priv->adapter->config_bands & BAND_GN ||
413 priv->adapter->config_bands & BAND_AN) &&
414 bss_desc->bcn_ht_cap) &&
415 !mwifiex_is_rsn_oui_present(bss_desc,
416 CIPHER_SUITE_CCMP)) {
417
418 if (mwifiex_is_rsn_oui_present
419 (bss_desc, CIPHER_SUITE_TKIP)) {
acebe8c1
ZL
420 mwifiex_dbg(adapter, INFO,
421 "info: Disable 11n if AES\t"
422 "is not supported by AP\n");
5e6e3a92
BZ
423 bss_desc->disable_11n = true;
424 } else {
425 return -1;
426 }
427 }
7c6fa2a8 428 return 0;
cff23cec 429 } else if (mwifiex_is_bss_adhoc_aes(priv, bss_desc)) {
5e6e3a92 430 /* Ad-hoc AES enabled */
7c6fa2a8 431 return 0;
cff23cec 432 } else if (mwifiex_is_bss_dynamic_wep(priv, bss_desc)) {
5e6e3a92 433 /* Dynamic WEP enabled */
7c6fa2a8 434 return 0;
5e6e3a92
BZ
435 }
436
437 /* Security doesn't match */
679b687b 438 dbg_security_flags(ERROR, "failed", priv, bss_desc);
5e6e3a92
BZ
439 return -1;
440 }
441
442 /* Mode doesn't match */
443 return -1;
444}
445
5e6e3a92
BZ
446/*
447 * This function creates a channel list for the driver to scan, based
448 * on region/band information.
449 *
450 * This routine is used for any scan that is not provided with a
451 * specific channel list to scan.
452 */
658f37b7 453static int
5e6e3a92 454mwifiex_scan_create_channel_list(struct mwifiex_private *priv,
cff23cec
YAP
455 const struct mwifiex_user_scan_cfg
456 *user_scan_in,
457 struct mwifiex_chan_scan_param_set
458 *scan_chan_list,
459 u8 filtered_scan)
5e6e3a92 460{
57fbcce3 461 enum nl80211_band band;
5e6e3a92
BZ
462 struct ieee80211_supported_band *sband;
463 struct ieee80211_channel *ch;
464 struct mwifiex_adapter *adapter = priv->adapter;
465 int chan_idx = 0, i;
5e6e3a92 466
57fbcce3 467 for (band = 0; (band < NUM_NL80211_BANDS) ; band++) {
5e6e3a92 468
4facc34a 469 if (!priv->wdev.wiphy->bands[band])
5e6e3a92
BZ
470 continue;
471
4facc34a 472 sband = priv->wdev.wiphy->bands[band];
5e6e3a92 473
d06b7b9e 474 for (i = 0; (i < sband->n_channels) ; i++) {
5e6e3a92
BZ
475 ch = &sband->channels[i];
476 if (ch->flags & IEEE80211_CHAN_DISABLED)
477 continue;
478 scan_chan_list[chan_idx].radio_type = band;
186630c2 479
5e6e3a92 480 if (user_scan_in &&
cff23cec 481 user_scan_in->chan_list[0].scan_time)
5e6e3a92
BZ
482 scan_chan_list[chan_idx].max_scan_time =
483 cpu_to_le16((u16) user_scan_in->
484 chan_list[0].scan_time);
8fe02e16 485 else if (ch->flags & IEEE80211_CHAN_NO_IR)
5e6e3a92
BZ
486 scan_chan_list[chan_idx].max_scan_time =
487 cpu_to_le16(adapter->passive_scan_time);
488 else
489 scan_chan_list[chan_idx].max_scan_time =
490 cpu_to_le16(adapter->active_scan_time);
186630c2 491
8fe02e16 492 if (ch->flags & IEEE80211_CHAN_NO_IR)
5e6e3a92 493 scan_chan_list[chan_idx].chan_scan_mode_bitmap
2375fa2b 494 |= (MWIFIEX_PASSIVE_SCAN |
495 MWIFIEX_HIDDEN_SSID_REPORT);
5e6e3a92
BZ
496 else
497 scan_chan_list[chan_idx].chan_scan_mode_bitmap
498 &= ~MWIFIEX_PASSIVE_SCAN;
499 scan_chan_list[chan_idx].chan_number =
500 (u32) ch->hw_value;
1b499cb7
AK
501
502 scan_chan_list[chan_idx].chan_scan_mode_bitmap
503 |= MWIFIEX_DISABLE_CHAN_FILT;
504
5e6e3a92
BZ
505 if (filtered_scan) {
506 scan_chan_list[chan_idx].max_scan_time =
507 cpu_to_le16(adapter->specific_scan_time);
5e6e3a92 508 }
d06b7b9e 509 chan_idx++;
5e6e3a92
BZ
510 }
511
512 }
658f37b7 513 return chan_idx;
5e6e3a92
BZ
514}
515
0c9b7f22
XH
516/* This function creates a channel list tlv for bgscan config, based
517 * on region/band information.
518 */
519static int
520mwifiex_bgscan_create_channel_list(struct mwifiex_private *priv,
521 const struct mwifiex_bg_scan_cfg
522 *bgscan_cfg_in,
523 struct mwifiex_chan_scan_param_set
524 *scan_chan_list)
525{
57fbcce3 526 enum nl80211_band band;
0c9b7f22
XH
527 struct ieee80211_supported_band *sband;
528 struct ieee80211_channel *ch;
529 struct mwifiex_adapter *adapter = priv->adapter;
530 int chan_idx = 0, i;
531
57fbcce3 532 for (band = 0; (band < NUM_NL80211_BANDS); band++) {
0c9b7f22
XH
533 if (!priv->wdev.wiphy->bands[band])
534 continue;
535
536 sband = priv->wdev.wiphy->bands[band];
537
538 for (i = 0; (i < sband->n_channels) ; i++) {
539 ch = &sband->channels[i];
540 if (ch->flags & IEEE80211_CHAN_DISABLED)
541 continue;
542 scan_chan_list[chan_idx].radio_type = band;
543
544 if (bgscan_cfg_in->chan_list[0].scan_time)
545 scan_chan_list[chan_idx].max_scan_time =
546 cpu_to_le16((u16)bgscan_cfg_in->
547 chan_list[0].scan_time);
548 else if (ch->flags & IEEE80211_CHAN_NO_IR)
549 scan_chan_list[chan_idx].max_scan_time =
550 cpu_to_le16(adapter->passive_scan_time);
551 else
552 scan_chan_list[chan_idx].max_scan_time =
553 cpu_to_le16(adapter->
554 specific_scan_time);
555
556 if (ch->flags & IEEE80211_CHAN_NO_IR)
557 scan_chan_list[chan_idx].chan_scan_mode_bitmap
558 |= MWIFIEX_PASSIVE_SCAN;
559 else
560 scan_chan_list[chan_idx].chan_scan_mode_bitmap
561 &= ~MWIFIEX_PASSIVE_SCAN;
562
563 scan_chan_list[chan_idx].chan_number =
564 (u32)ch->hw_value;
565 chan_idx++;
566 }
567 }
568 return chan_idx;
569}
570
78dfca62
AP
571/* This function appends rate TLV to scan config command. */
572static int
573mwifiex_append_rate_tlv(struct mwifiex_private *priv,
574 struct mwifiex_scan_cmd_config *scan_cfg_out,
575 u8 radio)
576{
577 struct mwifiex_ie_types_rates_param_set *rates_tlv;
578 u8 rates[MWIFIEX_SUPPORTED_RATES], *tlv_pos;
579 u32 rates_size;
580
581 memset(rates, 0, sizeof(rates));
582
583 tlv_pos = (u8 *)scan_cfg_out->tlv_buf + scan_cfg_out->tlv_buf_len;
584
585 if (priv->scan_request)
586 rates_size = mwifiex_get_rates_from_cfg80211(priv, rates,
587 radio);
588 else
589 rates_size = mwifiex_get_supported_rates(priv, rates);
590
acebe8c1
ZL
591 mwifiex_dbg(priv->adapter, CMD,
592 "info: SCAN_CMD: Rates size = %d\n",
78dfca62
AP
593 rates_size);
594 rates_tlv = (struct mwifiex_ie_types_rates_param_set *)tlv_pos;
595 rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
596 rates_tlv->header.len = cpu_to_le16((u16) rates_size);
597 memcpy(rates_tlv->rates, rates, rates_size);
598 scan_cfg_out->tlv_buf_len += sizeof(rates_tlv->header) + rates_size;
599
600 return rates_size;
601}
602
5e6e3a92
BZ
603/*
604 * This function constructs and sends multiple scan config commands to
605 * the firmware.
606 *
607 * Previous routines in the code flow have created a scan command configuration
608 * with any requested TLVs. This function splits the channel TLV into maximum
609 * channels supported per scan lists and sends the portion of the channel TLV,
610 * along with the other TLVs, to the firmware.
611 */
612static int
600f5d90 613mwifiex_scan_channel_list(struct mwifiex_private *priv,
5e6e3a92
BZ
614 u32 max_chan_per_scan, u8 filtered_scan,
615 struct mwifiex_scan_cmd_config *scan_cfg_out,
616 struct mwifiex_ie_types_chan_list_param_set
617 *chan_tlv_out,
618 struct mwifiex_chan_scan_param_set *scan_chan_list)
619{
1845bd3a 620 struct mwifiex_adapter *adapter = priv->adapter;
5e6e3a92
BZ
621 int ret = 0;
622 struct mwifiex_chan_scan_param_set *tmp_chan_list;
623 struct mwifiex_chan_scan_param_set *start_chan;
21f58d20 624 u32 tlv_idx, rates_size, cmd_no;
5e6e3a92
BZ
625 u32 total_scan_time;
626 u32 done_early;
78dfca62 627 u8 radio_type;
5e6e3a92
BZ
628
629 if (!scan_cfg_out || !chan_tlv_out || !scan_chan_list) {
acebe8c1
ZL
630 mwifiex_dbg(priv->adapter, ERROR,
631 "info: Scan: Null detect: %p, %p, %p\n",
632 scan_cfg_out, chan_tlv_out, scan_chan_list);
5e6e3a92
BZ
633 return -1;
634 }
635
b887664d
AK
636 /* Check csa channel expiry before preparing scan list */
637 mwifiex_11h_get_csa_closed_channel(priv);
638
5e6e3a92
BZ
639 chan_tlv_out->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
640
641 /* Set the temp channel struct pointer to the start of the desired
642 list */
643 tmp_chan_list = scan_chan_list;
644
645 /* Loop through the desired channel list, sending a new firmware scan
646 commands for each max_chan_per_scan channels (or for 1,6,11
647 individually if configured accordingly) */
648 while (tmp_chan_list->chan_number) {
649
650 tlv_idx = 0;
651 total_scan_time = 0;
78dfca62 652 radio_type = 0;
5e6e3a92
BZ
653 chan_tlv_out->header.len = 0;
654 start_chan = tmp_chan_list;
655 done_early = false;
656
657 /*
658 * Construct the Channel TLV for the scan command. Continue to
659 * insert channel TLVs until:
660 * - the tlv_idx hits the maximum configured per scan command
661 * - the next channel to insert is 0 (end of desired channel
662 * list)
663 * - done_early is set (controlling individual scanning of
664 * 1,6,11)
665 */
cff23cec
YAP
666 while (tlv_idx < max_chan_per_scan &&
667 tmp_chan_list->chan_number && !done_early) {
5e6e3a92 668
b887664d
AK
669 if (tmp_chan_list->chan_number == priv->csa_chan) {
670 tmp_chan_list++;
671 continue;
672 }
673
78dfca62 674 radio_type = tmp_chan_list->radio_type;
acebe8c1
ZL
675 mwifiex_dbg(priv->adapter, INFO,
676 "info: Scan: Chan(%3d), Radio(%d),\t"
677 "Mode(%d, %d), Dur(%d)\n",
678 tmp_chan_list->chan_number,
679 tmp_chan_list->radio_type,
680 tmp_chan_list->chan_scan_mode_bitmap
681 & MWIFIEX_PASSIVE_SCAN,
682 (tmp_chan_list->chan_scan_mode_bitmap
683 & MWIFIEX_DISABLE_CHAN_FILT) >> 1,
684 le16_to_cpu(tmp_chan_list->max_scan_time));
5e6e3a92
BZ
685
686 /* Copy the current channel TLV to the command being
687 prepared */
688 memcpy(chan_tlv_out->chan_scan_param + tlv_idx,
689 tmp_chan_list,
690 sizeof(chan_tlv_out->chan_scan_param));
691
692 /* Increment the TLV header length by the size
693 appended */
5653c646
DM
694 le16_unaligned_add_cpu(&chan_tlv_out->header.len,
695 sizeof(
696 chan_tlv_out->chan_scan_param));
5e6e3a92
BZ
697
698 /*
699 * The tlv buffer length is set to the number of bytes
700 * of the between the channel tlv pointer and the start
701 * of the tlv buffer. This compensates for any TLVs
702 * that were appended before the channel list.
703 */
704 scan_cfg_out->tlv_buf_len = (u32) ((u8 *) chan_tlv_out -
705 scan_cfg_out->tlv_buf);
706
707 /* Add the size of the channel tlv header and the data
708 length */
709 scan_cfg_out->tlv_buf_len +=
710 (sizeof(chan_tlv_out->header)
711 + le16_to_cpu(chan_tlv_out->header.len));
712
713 /* Increment the index to the channel tlv we are
714 constructing */
715 tlv_idx++;
716
717 /* Count the total scan time per command */
718 total_scan_time +=
719 le16_to_cpu(tmp_chan_list->max_scan_time);
720
721 done_early = false;
722
723 /* Stop the loop if the *current* channel is in the
724 1,6,11 set and we are not filtering on a BSSID
725 or SSID. */
cff23cec
YAP
726 if (!filtered_scan &&
727 (tmp_chan_list->chan_number == 1 ||
728 tmp_chan_list->chan_number == 6 ||
729 tmp_chan_list->chan_number == 11))
5e6e3a92
BZ
730 done_early = true;
731
732 /* Increment the tmp pointer to the next channel to
733 be scanned */
734 tmp_chan_list++;
735
736 /* Stop the loop if the *next* channel is in the 1,6,11
737 set. This will cause it to be the only channel
738 scanned on the next interation */
cff23cec
YAP
739 if (!filtered_scan &&
740 (tmp_chan_list->chan_number == 1 ||
741 tmp_chan_list->chan_number == 6 ||
742 tmp_chan_list->chan_number == 11))
5e6e3a92
BZ
743 done_early = true;
744 }
745
746 /* The total scan time should be less than scan command timeout
747 value */
748 if (total_scan_time > MWIFIEX_MAX_TOTAL_SCAN_TIME) {
acebe8c1
ZL
749 mwifiex_dbg(priv->adapter, ERROR,
750 "total scan time %dms\t"
751 "is over limit (%dms), scan skipped\n",
752 total_scan_time,
753 MWIFIEX_MAX_TOTAL_SCAN_TIME);
5e6e3a92
BZ
754 ret = -1;
755 break;
756 }
757
78dfca62
AP
758 rates_size = mwifiex_append_rate_tlv(priv, scan_cfg_out,
759 radio_type);
760
5e6e3a92
BZ
761 priv->adapter->scan_channels = start_chan;
762
763 /* Send the scan command to the firmware with the specified
764 cfg */
21f58d20
AK
765 if (priv->adapter->ext_scan)
766 cmd_no = HostCmd_CMD_802_11_SCAN_EXT;
767 else
768 cmd_no = HostCmd_CMD_802_11_SCAN;
769
fa0ecbb9
BZ
770 ret = mwifiex_send_cmd(priv, cmd_no, HostCmd_ACT_GEN_SET,
771 0, scan_cfg_out, false);
78dfca62
AP
772
773 /* rate IE is updated per scan command but same starting
774 * pointer is used each time so that rate IE from earlier
775 * scan_cfg_out->buf is overwritten with new one.
776 */
777 scan_cfg_out->tlv_buf_len -=
778 sizeof(struct mwifiex_ie_types_header) + rates_size;
779
1845bd3a 780 if (ret) {
c70ca8cb 781 mwifiex_cancel_pending_scan_cmd(adapter);
5e6e3a92 782 break;
1845bd3a 783 }
5e6e3a92
BZ
784 }
785
786 if (ret)
787 return -1;
788
789 return 0;
790}
791
792/*
793 * This function constructs a scan command configuration structure to use
794 * in scan commands.
795 *
796 * Application layer or other functions can invoke network scanning
797 * with a scan configuration supplied in a user scan configuration structure.
798 * This structure is used as the basis of one or many scan command configuration
799 * commands that are sent to the command processing module and eventually to the
800 * firmware.
801 *
802 * This function creates a scan command configuration structure based on the
803 * following user supplied parameters (if present):
804 * - SSID filter
805 * - BSSID filter
806 * - Number of Probes to be sent
807 * - Channel list
808 *
809 * If the SSID or BSSID filter is not present, the filter is disabled/cleared.
810 * If the number of probes is not set, adapter default setting is used.
811 */
812static void
cff23cec
YAP
813mwifiex_config_scan(struct mwifiex_private *priv,
814 const struct mwifiex_user_scan_cfg *user_scan_in,
815 struct mwifiex_scan_cmd_config *scan_cfg_out,
816 struct mwifiex_ie_types_chan_list_param_set **chan_list_out,
817 struct mwifiex_chan_scan_param_set *scan_chan_list,
818 u8 *max_chan_per_scan, u8 *filtered_scan,
819 u8 *scan_current_only)
5e6e3a92
BZ
820{
821 struct mwifiex_adapter *adapter = priv->adapter;
822 struct mwifiex_ie_types_num_probes *num_probes_tlv;
cb91be87 823 struct mwifiex_ie_types_scan_chan_gap *chan_gap_tlv;
c2a8f0ff 824 struct mwifiex_ie_types_random_mac *random_mac_tlv;
5e6e3a92 825 struct mwifiex_ie_types_wildcard_ssid_params *wildcard_ssid_tlv;
21f58d20 826 struct mwifiex_ie_types_bssid_list *bssid_tlv;
5e6e3a92
BZ
827 u8 *tlv_pos;
828 u32 num_probes;
829 u32 ssid_len;
830 u32 chan_idx;
831 u32 scan_type;
832 u16 scan_dur;
833 u8 channel;
834 u8 radio_type;
be0b281e 835 int i;
5e6e3a92 836 u8 ssid_filter;
5e6e3a92 837 struct mwifiex_ie_types_htcap *ht_cap;
40d7412b 838 struct mwifiex_ie_types_bss_mode *bss_mode;
c2a8f0ff 839 const u8 zero_mac[6] = {0, 0, 0, 0, 0, 0};
5e6e3a92
BZ
840
841 /* The tlv_buf_len is calculated for each scan command. The TLVs added
842 in this routine will be preserved since the routine that sends the
843 command will append channelTLVs at *chan_list_out. The difference
844 between the *chan_list_out and the tlv_buf start will be used to
845 calculate the size of anything we add in this routine. */
846 scan_cfg_out->tlv_buf_len = 0;
847
848 /* Running tlv pointer. Assigned to chan_list_out at end of function
849 so later routines know where channels can be added to the command
850 buf */
851 tlv_pos = scan_cfg_out->tlv_buf;
852
853 /* Initialize the scan as un-filtered; the flag is later set to TRUE
854 below if a SSID or BSSID filter is sent in the command */
855 *filtered_scan = false;
856
857 /* Initialize the scan as not being only on the current channel. If
858 the channel list is customized, only contains one channel, and is
859 the active channel, this is set true and data flow is not halted. */
860 *scan_current_only = false;
861
862 if (user_scan_in) {
5653c646 863 u8 tmpaddr[ETH_ALEN];
5e6e3a92
BZ
864
865 /* Default the ssid_filter flag to TRUE, set false under
866 certain wildcard conditions and qualified by the existence
867 of an SSID list before marking the scan as filtered */
868 ssid_filter = true;
869
870 /* Set the BSS type scan filter, use Adapter setting if
871 unset */
872 scan_cfg_out->bss_mode =
2ccf7cef 873 (u8)(user_scan_in->bss_mode ?: adapter->scan_mode);
5e6e3a92
BZ
874
875 /* Set the number of probes to send, use Adapter setting
876 if unset */
2ccf7cef 877 num_probes = user_scan_in->num_probes ?: adapter->scan_probes;
5e6e3a92
BZ
878
879 /*
880 * Set the BSSID filter to the incoming configuration,
881 * if non-zero. If not set, it will remain disabled
882 * (all zeros).
883 */
884 memcpy(scan_cfg_out->specific_bssid,
885 user_scan_in->specific_bssid,
886 sizeof(scan_cfg_out->specific_bssid));
887
5653c646
DM
888 memcpy(tmpaddr, scan_cfg_out->specific_bssid, ETH_ALEN);
889
21f58d20 890 if (adapter->ext_scan &&
5653c646 891 !is_zero_ether_addr(tmpaddr)) {
21f58d20
AK
892 bssid_tlv =
893 (struct mwifiex_ie_types_bssid_list *)tlv_pos;
894 bssid_tlv->header.type = cpu_to_le16(TLV_TYPE_BSSID);
895 bssid_tlv->header.len = cpu_to_le16(ETH_ALEN);
896 memcpy(bssid_tlv->bssid, user_scan_in->specific_bssid,
897 ETH_ALEN);
898 tlv_pos += sizeof(struct mwifiex_ie_types_bssid_list);
899 }
900
be0b281e
AK
901 for (i = 0; i < user_scan_in->num_ssids; i++) {
902 ssid_len = user_scan_in->ssid_list[i].ssid_len;
5e6e3a92
BZ
903
904 wildcard_ssid_tlv =
905 (struct mwifiex_ie_types_wildcard_ssid_params *)
906 tlv_pos;
907 wildcard_ssid_tlv->header.type =
908 cpu_to_le16(TLV_TYPE_WILDCARDSSID);
909 wildcard_ssid_tlv->header.len = cpu_to_le16(
910 (u16) (ssid_len + sizeof(wildcard_ssid_tlv->
911 max_ssid_length)));
fada1058 912
be0b281e
AK
913 /*
914 * max_ssid_length = 0 tells firmware to perform
915 * specific scan for the SSID filled, whereas
916 * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
917 * wildcard scan.
918 */
919 if (ssid_len)
920 wildcard_ssid_tlv->max_ssid_length = 0;
921 else
922 wildcard_ssid_tlv->max_ssid_length =
923 IEEE80211_MAX_SSID_LEN;
5e6e3a92 924
0c6303cc
AN
925 if (!memcmp(user_scan_in->ssid_list[i].ssid,
926 "DIRECT-", 7))
927 wildcard_ssid_tlv->max_ssid_length = 0xfe;
928
5e6e3a92 929 memcpy(wildcard_ssid_tlv->ssid,
be0b281e 930 user_scan_in->ssid_list[i].ssid, ssid_len);
5e6e3a92
BZ
931
932 tlv_pos += (sizeof(wildcard_ssid_tlv->header)
933 + le16_to_cpu(wildcard_ssid_tlv->header.len));
934
acebe8c1
ZL
935 mwifiex_dbg(adapter, INFO,
936 "info: scan: ssid[%d]: %s, %d\n",
937 i, wildcard_ssid_tlv->ssid,
938 wildcard_ssid_tlv->max_ssid_length);
5e6e3a92
BZ
939
940 /* Empty wildcard ssid with a maxlen will match many or
941 potentially all SSIDs (maxlen == 32), therefore do
942 not treat the scan as
943 filtered. */
944 if (!ssid_len && wildcard_ssid_tlv->max_ssid_length)
945 ssid_filter = false;
5e6e3a92
BZ
946 }
947
948 /*
949 * The default number of channels sent in the command is low to
950 * ensure the response buffer from the firmware does not
951 * truncate scan results. That is not an issue with an SSID
952 * or BSSID filter applied to the scan results in the firmware.
953 */
5653c646 954 memcpy(tmpaddr, scan_cfg_out->specific_bssid, ETH_ALEN);
cff23cec 955 if ((i && ssid_filter) ||
5653c646 956 !is_zero_ether_addr(tmpaddr))
5e6e3a92 957 *filtered_scan = true;
8eda10ea
AP
958
959 if (user_scan_in->scan_chan_gap) {
acebe8c1
ZL
960 mwifiex_dbg(adapter, INFO,
961 "info: scan: channel gap = %d\n",
962 user_scan_in->scan_chan_gap);
8eda10ea
AP
963 *max_chan_per_scan =
964 MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN;
965
966 chan_gap_tlv = (void *)tlv_pos;
967 chan_gap_tlv->header.type =
968 cpu_to_le16(TLV_TYPE_SCAN_CHANNEL_GAP);
969 chan_gap_tlv->header.len =
970 cpu_to_le16(sizeof(chan_gap_tlv->chan_gap));
971 chan_gap_tlv->chan_gap =
972 cpu_to_le16((user_scan_in->scan_chan_gap));
973 tlv_pos +=
974 sizeof(struct mwifiex_ie_types_scan_chan_gap);
975 }
c2a8f0ff
GB
976
977 if (!ether_addr_equal(user_scan_in->random_mac, zero_mac)) {
978 random_mac_tlv = (void *)tlv_pos;
979 random_mac_tlv->header.type =
980 cpu_to_le16(TLV_TYPE_RANDOM_MAC);
981 random_mac_tlv->header.len =
982 cpu_to_le16(sizeof(random_mac_tlv->mac));
983 ether_addr_copy(random_mac_tlv->mac,
984 user_scan_in->random_mac);
985 tlv_pos +=
986 sizeof(struct mwifiex_ie_types_random_mac);
987 }
5e6e3a92
BZ
988 } else {
989 scan_cfg_out->bss_mode = (u8) adapter->scan_mode;
990 num_probes = adapter->scan_probes;
991 }
992
993 /*
994 * If a specific BSSID or SSID is used, the number of channels in the
995 * scan command will be increased to the absolute maximum.
996 */
0a5cc497 997 if (*filtered_scan) {
5e6e3a92 998 *max_chan_per_scan = MWIFIEX_MAX_CHANNELS_PER_SPECIFIC_SCAN;
0a5cc497
AK
999 } else {
1000 if (!priv->media_connected)
1001 *max_chan_per_scan = MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD;
1002 else
1003 *max_chan_per_scan =
1004 MWIFIEX_DEF_CHANNELS_PER_SCAN_CMD / 2;
1005 }
5e6e3a92 1006
40d7412b
AN
1007 if (adapter->ext_scan) {
1008 bss_mode = (struct mwifiex_ie_types_bss_mode *)tlv_pos;
1009 bss_mode->header.type = cpu_to_le16(TLV_TYPE_BSS_MODE);
1010 bss_mode->header.len = cpu_to_le16(sizeof(bss_mode->bss_mode));
1011 bss_mode->bss_mode = scan_cfg_out->bss_mode;
1012 tlv_pos += sizeof(bss_mode->header) +
1013 le16_to_cpu(bss_mode->header.len);
1014 }
1015
5e6e3a92
BZ
1016 /* If the input config or adapter has the number of Probes set,
1017 add tlv */
1018 if (num_probes) {
1019
acebe8c1
ZL
1020 mwifiex_dbg(adapter, INFO,
1021 "info: scan: num_probes = %d\n",
1022 num_probes);
5e6e3a92
BZ
1023
1024 num_probes_tlv = (struct mwifiex_ie_types_num_probes *) tlv_pos;
1025 num_probes_tlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
1026 num_probes_tlv->header.len =
1027 cpu_to_le16(sizeof(num_probes_tlv->num_probes));
1028 num_probes_tlv->num_probes = cpu_to_le16((u16) num_probes);
1029
1030 tlv_pos += sizeof(num_probes_tlv->header) +
1031 le16_to_cpu(num_probes_tlv->header.len);
1032
1033 }
1034
cff23cec
YAP
1035 if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
1036 (priv->adapter->config_bands & BAND_GN ||
1037 priv->adapter->config_bands & BAND_AN)) {
5e6e3a92
BZ
1038 ht_cap = (struct mwifiex_ie_types_htcap *) tlv_pos;
1039 memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
1040 ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
1041 ht_cap->header.len =
1042 cpu_to_le16(sizeof(struct ieee80211_ht_cap));
a46b7b5c
AK
1043 radio_type =
1044 mwifiex_band_to_radio_type(priv->adapter->config_bands);
341b8800 1045 mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
5e6e3a92
BZ
1046 tlv_pos += sizeof(struct mwifiex_ie_types_htcap);
1047 }
1048
1049 /* Append vendor specific IE TLV */
1050 mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_SCAN, &tlv_pos);
1051
1052 /*
1053 * Set the output for the channel TLV to the address in the tlv buffer
1054 * past any TLVs that were added in this function (SSID, num_probes).
1055 * Channel TLVs will be added past this for each scan command,
1056 * preserving the TLVs that were previously added.
1057 */
1058 *chan_list_out =
1059 (struct mwifiex_ie_types_chan_list_param_set *) tlv_pos;
1060
1061 if (user_scan_in && user_scan_in->chan_list[0].chan_number) {
1062
acebe8c1
ZL
1063 mwifiex_dbg(adapter, INFO,
1064 "info: Scan: Using supplied channel list\n");
5e6e3a92
BZ
1065
1066 for (chan_idx = 0;
cff23cec
YAP
1067 chan_idx < MWIFIEX_USER_SCAN_CHAN_MAX &&
1068 user_scan_in->chan_list[chan_idx].chan_number;
5e6e3a92
BZ
1069 chan_idx++) {
1070
1071 channel = user_scan_in->chan_list[chan_idx].chan_number;
948ad6b3 1072 scan_chan_list[chan_idx].chan_number = channel;
5e6e3a92
BZ
1073
1074 radio_type =
1075 user_scan_in->chan_list[chan_idx].radio_type;
948ad6b3 1076 scan_chan_list[chan_idx].radio_type = radio_type;
5e6e3a92
BZ
1077
1078 scan_type = user_scan_in->chan_list[chan_idx].scan_type;
1079
1080 if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
948ad6b3 1081 scan_chan_list[chan_idx].chan_scan_mode_bitmap
2375fa2b 1082 |= (MWIFIEX_PASSIVE_SCAN |
1083 MWIFIEX_HIDDEN_SSID_REPORT);
5e6e3a92 1084 else
948ad6b3 1085 scan_chan_list[chan_idx].chan_scan_mode_bitmap
5e6e3a92
BZ
1086 &= ~MWIFIEX_PASSIVE_SCAN;
1087
1b499cb7
AK
1088 scan_chan_list[chan_idx].chan_scan_mode_bitmap
1089 |= MWIFIEX_DISABLE_CHAN_FILT;
f3e1af3e 1090
5e6e3a92
BZ
1091 if (user_scan_in->chan_list[chan_idx].scan_time) {
1092 scan_dur = (u16) user_scan_in->
1093 chan_list[chan_idx].scan_time;
1094 } else {
1095 if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
1096 scan_dur = adapter->passive_scan_time;
1097 else if (*filtered_scan)
1098 scan_dur = adapter->specific_scan_time;
1099 else
1100 scan_dur = adapter->active_scan_time;
1101 }
1102
948ad6b3 1103 scan_chan_list[chan_idx].min_scan_time =
5e6e3a92 1104 cpu_to_le16(scan_dur);
948ad6b3 1105 scan_chan_list[chan_idx].max_scan_time =
5e6e3a92
BZ
1106 cpu_to_le16(scan_dur);
1107 }
1108
1109 /* Check if we are only scanning the current channel */
cff23cec
YAP
1110 if ((chan_idx == 1) &&
1111 (user_scan_in->chan_list[0].chan_number ==
1112 priv->curr_bss_params.bss_descriptor.channel)) {
5e6e3a92 1113 *scan_current_only = true;
acebe8c1
ZL
1114 mwifiex_dbg(adapter, INFO,
1115 "info: Scan: Scanning current channel only\n");
5e6e3a92 1116 }
5e6e3a92 1117 } else {
acebe8c1
ZL
1118 mwifiex_dbg(adapter, INFO,
1119 "info: Scan: Creating full region channel list\n");
8ac91341
KR
1120 mwifiex_scan_create_channel_list(priv, user_scan_in,
1121 scan_chan_list,
1122 *filtered_scan);
658f37b7
AK
1123 }
1124
5e6e3a92
BZ
1125}
1126
1127/*
1128 * This function inspects the scan response buffer for pointers to
1129 * expected TLVs.
1130 *
1131 * TLVs can be included at the end of the scan response BSS information.
1132 *
1133 * Data in the buffer is parsed pointers to TLVs that can potentially
1134 * be passed back in the response.
1135 */
1136static void
1137mwifiex_ret_802_11_scan_get_tlv_ptrs(struct mwifiex_adapter *adapter,
1138 struct mwifiex_ie_types_data *tlv,
1139 u32 tlv_buf_size, u32 req_tlv_type,
1140 struct mwifiex_ie_types_data **tlv_data)
1141{
1142 struct mwifiex_ie_types_data *current_tlv;
1143 u32 tlv_buf_left;
1144 u32 tlv_type;
1145 u32 tlv_len;
1146
1147 current_tlv = tlv;
1148 tlv_buf_left = tlv_buf_size;
1149 *tlv_data = NULL;
1150
acebe8c1
ZL
1151 mwifiex_dbg(adapter, INFO,
1152 "info: SCAN_RESP: tlv_buf_size = %d\n",
1153 tlv_buf_size);
5e6e3a92
BZ
1154
1155 while (tlv_buf_left >= sizeof(struct mwifiex_ie_types_header)) {
1156
1157 tlv_type = le16_to_cpu(current_tlv->header.type);
1158 tlv_len = le16_to_cpu(current_tlv->header.len);
1159
1160 if (sizeof(tlv->header) + tlv_len > tlv_buf_left) {
acebe8c1
ZL
1161 mwifiex_dbg(adapter, ERROR,
1162 "SCAN_RESP: TLV buffer corrupt\n");
5e6e3a92
BZ
1163 break;
1164 }
1165
1166 if (req_tlv_type == tlv_type) {
1167 switch (tlv_type) {
1168 case TLV_TYPE_TSFTIMESTAMP:
acebe8c1
ZL
1169 mwifiex_dbg(adapter, INFO,
1170 "info: SCAN_RESP: TSF\t"
1171 "timestamp TLV, len = %d\n",
1172 tlv_len);
2c208890 1173 *tlv_data = current_tlv;
5e6e3a92
BZ
1174 break;
1175 case TLV_TYPE_CHANNELBANDLIST:
acebe8c1
ZL
1176 mwifiex_dbg(adapter, INFO,
1177 "info: SCAN_RESP: channel\t"
1178 "band list TLV, len = %d\n",
1179 tlv_len);
2c208890 1180 *tlv_data = current_tlv;
5e6e3a92
BZ
1181 break;
1182 default:
acebe8c1
ZL
1183 mwifiex_dbg(adapter, ERROR,
1184 "SCAN_RESP: unhandled TLV = %d\n",
1185 tlv_type);
5e6e3a92
BZ
1186 /* Give up, this seems corrupted */
1187 return;
1188 }
1189 }
1190
1191 if (*tlv_data)
1192 break;
1193
1194
1195 tlv_buf_left -= (sizeof(tlv->header) + tlv_len);
1196 current_tlv =
1197 (struct mwifiex_ie_types_data *) (current_tlv->data +
1198 tlv_len);
1199
1200 } /* while */
1201}
1202
1203/*
7c6fa2a8
AK
1204 * This function parses provided beacon buffer and updates
1205 * respective fields in bss descriptor structure.
5e6e3a92 1206 */
9558a407
AK
1207int mwifiex_update_bss_desc_with_ie(struct mwifiex_adapter *adapter,
1208 struct mwifiex_bssdescriptor *bss_entry)
5e6e3a92
BZ
1209{
1210 int ret = 0;
1211 u8 element_id;
1212 struct ieee_types_fh_param_set *fh_param_set;
1213 struct ieee_types_ds_param_set *ds_param_set;
1214 struct ieee_types_cf_param_set *cf_param_set;
1215 struct ieee_types_ibss_param_set *ibss_param_set;
5e6e3a92
BZ
1216 u8 *current_ptr;
1217 u8 *rate;
1218 u8 element_len;
1219 u16 total_ie_len;
1220 u8 bytes_to_copy;
1221 u8 rate_size;
5e6e3a92 1222 u8 found_data_rate_ie;
7c6fa2a8 1223 u32 bytes_left;
5e6e3a92
BZ
1224 struct ieee_types_vendor_specific *vendor_ie;
1225 const u8 wpa_oui[4] = { 0x00, 0x50, 0xf2, 0x01 };
1226 const u8 wmm_oui[4] = { 0x00, 0x50, 0xf2, 0x02 };
1227
1228 found_data_rate_ie = false;
1229 rate_size = 0;
9558a407
AK
1230 current_ptr = bss_entry->beacon_buf;
1231 bytes_left = bss_entry->beacon_buf_size;
5e6e3a92
BZ
1232
1233 /* Process variable IE */
7c6fa2a8 1234 while (bytes_left >= 2) {
5e6e3a92
BZ
1235 element_id = *current_ptr;
1236 element_len = *(current_ptr + 1);
1237 total_ie_len = element_len + sizeof(struct ieee_types_header);
1238
7c6fa2a8 1239 if (bytes_left < total_ie_len) {
acebe8c1
ZL
1240 mwifiex_dbg(adapter, ERROR,
1241 "err: InterpretIE: in processing\t"
1242 "IE, bytes left < IE length\n");
7c6fa2a8 1243 return -1;
5e6e3a92
BZ
1244 }
1245 switch (element_id) {
1246 case WLAN_EID_SSID:
1247 bss_entry->ssid.ssid_len = element_len;
1248 memcpy(bss_entry->ssid.ssid, (current_ptr + 2),
1249 element_len);
acebe8c1
ZL
1250 mwifiex_dbg(adapter, INFO,
1251 "info: InterpretIE: ssid: %-32s\n",
1252 bss_entry->ssid.ssid);
5e6e3a92
BZ
1253 break;
1254
1255 case WLAN_EID_SUPP_RATES:
1256 memcpy(bss_entry->data_rates, current_ptr + 2,
1257 element_len);
1258 memcpy(bss_entry->supported_rates, current_ptr + 2,
1259 element_len);
1260 rate_size = element_len;
1261 found_data_rate_ie = true;
1262 break;
1263
1264 case WLAN_EID_FH_PARAMS:
1265 fh_param_set =
1266 (struct ieee_types_fh_param_set *) current_ptr;
1267 memcpy(&bss_entry->phy_param_set.fh_param_set,
1268 fh_param_set,
1269 sizeof(struct ieee_types_fh_param_set));
1270 break;
1271
1272 case WLAN_EID_DS_PARAMS:
1273 ds_param_set =
1274 (struct ieee_types_ds_param_set *) current_ptr;
1275
1276 bss_entry->channel = ds_param_set->current_chan;
1277
1278 memcpy(&bss_entry->phy_param_set.ds_param_set,
1279 ds_param_set,
1280 sizeof(struct ieee_types_ds_param_set));
1281 break;
1282
1283 case WLAN_EID_CF_PARAMS:
1284 cf_param_set =
1285 (struct ieee_types_cf_param_set *) current_ptr;
1286 memcpy(&bss_entry->ss_param_set.cf_param_set,
1287 cf_param_set,
1288 sizeof(struct ieee_types_cf_param_set));
1289 break;
1290
1291 case WLAN_EID_IBSS_PARAMS:
1292 ibss_param_set =
1293 (struct ieee_types_ibss_param_set *)
1294 current_ptr;
1295 memcpy(&bss_entry->ss_param_set.ibss_param_set,
1296 ibss_param_set,
1297 sizeof(struct ieee_types_ibss_param_set));
1298 break;
1299
1300 case WLAN_EID_ERP_INFO:
1301 bss_entry->erp_flags = *(current_ptr + 2);
1302 break;
1303
2a7305c8
AK
1304 case WLAN_EID_PWR_CONSTRAINT:
1305 bss_entry->local_constraint = *(current_ptr + 2);
1306 bss_entry->sensed_11h = true;
1307 break;
1308
1309 case WLAN_EID_CHANNEL_SWITCH:
1310 bss_entry->chan_sw_ie_present = true;
1311 case WLAN_EID_PWR_CAPABILITY:
1312 case WLAN_EID_TPC_REPORT:
1313 case WLAN_EID_QUIET:
1314 bss_entry->sensed_11h = true;
1315 break;
1316
5e6e3a92
BZ
1317 case WLAN_EID_EXT_SUPP_RATES:
1318 /*
1319 * Only process extended supported rate
1320 * if data rate is already found.
1321 * Data rate IE should come before
1322 * extended supported rate IE
1323 */
1324 if (found_data_rate_ie) {
1325 if ((element_len + rate_size) >
1326 MWIFIEX_SUPPORTED_RATES)
1327 bytes_to_copy =
1328 (MWIFIEX_SUPPORTED_RATES -
1329 rate_size);
1330 else
1331 bytes_to_copy = element_len;
1332
1333 rate = (u8 *) bss_entry->data_rates;
1334 rate += rate_size;
1335 memcpy(rate, current_ptr + 2, bytes_to_copy);
1336
1337 rate = (u8 *) bss_entry->supported_rates;
1338 rate += rate_size;
1339 memcpy(rate, current_ptr + 2, bytes_to_copy);
1340 }
1341 break;
1342
1343 case WLAN_EID_VENDOR_SPECIFIC:
1344 vendor_ie = (struct ieee_types_vendor_specific *)
1345 current_ptr;
1346
1347 if (!memcmp
1348 (vendor_ie->vend_hdr.oui, wpa_oui,
1349 sizeof(wpa_oui))) {
1350 bss_entry->bcn_wpa_ie =
1351 (struct ieee_types_vendor_specific *)
1352 current_ptr;
cff23cec
YAP
1353 bss_entry->wpa_offset = (u16)
1354 (current_ptr - bss_entry->beacon_buf);
5e6e3a92
BZ
1355 } else if (!memcmp(vendor_ie->vend_hdr.oui, wmm_oui,
1356 sizeof(wmm_oui))) {
1357 if (total_ie_len ==
cff23cec
YAP
1358 sizeof(struct ieee_types_wmm_parameter) ||
1359 total_ie_len ==
5e6e3a92
BZ
1360 sizeof(struct ieee_types_wmm_info))
1361 /*
1362 * Only accept and copy the WMM IE if
1363 * it matches the size expected for the
1364 * WMM Info IE or the WMM Parameter IE.
1365 */
1366 memcpy((u8 *) &bss_entry->wmm_ie,
1367 current_ptr, total_ie_len);
1368 }
1369 break;
1370 case WLAN_EID_RSN:
1371 bss_entry->bcn_rsn_ie =
1372 (struct ieee_types_generic *) current_ptr;
1373 bss_entry->rsn_offset = (u16) (current_ptr -
1374 bss_entry->beacon_buf);
1375 break;
1376 case WLAN_EID_BSS_AC_ACCESS_DELAY:
1377 bss_entry->bcn_wapi_ie =
1378 (struct ieee_types_generic *) current_ptr;
1379 bss_entry->wapi_offset = (u16) (current_ptr -
1380 bss_entry->beacon_buf);
1381 break;
1382 case WLAN_EID_HT_CAPABILITY:
1383 bss_entry->bcn_ht_cap = (struct ieee80211_ht_cap *)
1384 (current_ptr +
1385 sizeof(struct ieee_types_header));
1386 bss_entry->ht_cap_offset = (u16) (current_ptr +
1387 sizeof(struct ieee_types_header) -
1388 bss_entry->beacon_buf);
1389 break;
074d46d1
JB
1390 case WLAN_EID_HT_OPERATION:
1391 bss_entry->bcn_ht_oper =
1392 (struct ieee80211_ht_operation *)(current_ptr +
5e6e3a92
BZ
1393 sizeof(struct ieee_types_header));
1394 bss_entry->ht_info_offset = (u16) (current_ptr +
1395 sizeof(struct ieee_types_header) -
1396 bss_entry->beacon_buf);
1397 break;
a5f39056
YAP
1398 case WLAN_EID_VHT_CAPABILITY:
1399 bss_entry->disable_11ac = false;
1400 bss_entry->bcn_vht_cap =
1401 (void *)(current_ptr +
1402 sizeof(struct ieee_types_header));
1403 bss_entry->vht_cap_offset =
1404 (u16)((u8 *)bss_entry->bcn_vht_cap -
1405 bss_entry->beacon_buf);
1406 break;
1407 case WLAN_EID_VHT_OPERATION:
1408 bss_entry->bcn_vht_oper =
1409 (void *)(current_ptr +
1410 sizeof(struct ieee_types_header));
1411 bss_entry->vht_info_offset =
1412 (u16)((u8 *)bss_entry->bcn_vht_oper -
1413 bss_entry->beacon_buf);
1414 break;
5e6e3a92 1415 case WLAN_EID_BSS_COEX_2040:
cf831ffe
AK
1416 bss_entry->bcn_bss_co_2040 = current_ptr;
1417 bss_entry->bss_co_2040_offset =
1418 (u16) (current_ptr - bss_entry->beacon_buf);
5e6e3a92
BZ
1419 break;
1420 case WLAN_EID_EXT_CAPABILITY:
cf831ffe
AK
1421 bss_entry->bcn_ext_cap = current_ptr;
1422 bss_entry->ext_cap_offset =
1423 (u16) (current_ptr - bss_entry->beacon_buf);
5e6e3a92 1424 break;
a5f39056 1425 case WLAN_EID_OPMODE_NOTIF:
cf831ffe 1426 bss_entry->oper_mode = (void *)current_ptr;
a5f39056
YAP
1427 bss_entry->oper_mode_offset =
1428 (u16)((u8 *)bss_entry->oper_mode -
1429 bss_entry->beacon_buf);
1430 break;
5e6e3a92
BZ
1431 default:
1432 break;
1433 }
1434
1435 current_ptr += element_len + 2;
1436
1437 /* Need to account for IE ID and IE Len */
7c6fa2a8 1438 bytes_left -= (element_len + 2);
5e6e3a92 1439
7c6fa2a8 1440 } /* while (bytes_left > 2) */
5e6e3a92
BZ
1441 return ret;
1442}
1443
5e6e3a92
BZ
1444/*
1445 * This function converts radio type scan parameter to a band configuration
1446 * to be used in join command.
1447 */
1448static u8
1449mwifiex_radio_type_to_band(u8 radio_type)
1450{
5e6e3a92
BZ
1451 switch (radio_type) {
1452 case HostCmd_SCAN_RADIO_TYPE_A:
636c4598 1453 return BAND_A;
5e6e3a92
BZ
1454 case HostCmd_SCAN_RADIO_TYPE_BG:
1455 default:
636c4598 1456 return BAND_G;
5e6e3a92 1457 }
5e6e3a92
BZ
1458}
1459
5e6e3a92
BZ
1460/*
1461 * This is an internal function used to start a scan based on an input
1462 * configuration.
1463 *
1464 * This uses the input user scan configuration information when provided in
1465 * order to send the appropriate scan commands to firmware to populate or
1466 * update the internal driver scan table.
1467 */
1a1fb970
AK
1468int mwifiex_scan_networks(struct mwifiex_private *priv,
1469 const struct mwifiex_user_scan_cfg *user_scan_in)
5e6e3a92 1470{
c2476335 1471 int ret;
5e6e3a92 1472 struct mwifiex_adapter *adapter = priv->adapter;
270e58e8
YAP
1473 struct cmd_ctrl_node *cmd_node;
1474 union mwifiex_scan_cmd_config_tlv *scan_cfg_out;
5e6e3a92 1475 struct mwifiex_ie_types_chan_list_param_set *chan_list_out;
5e6e3a92 1476 struct mwifiex_chan_scan_param_set *scan_chan_list;
5e6e3a92
BZ
1477 u8 filtered_scan;
1478 u8 scan_current_chan_only;
1479 u8 max_chan_per_scan;
1480 unsigned long flags;
1481
600f5d90 1482 if (adapter->scan_processing) {
acebe8c1
ZL
1483 mwifiex_dbg(adapter, WARN,
1484 "cmd: Scan already in process...\n");
c2476335 1485 return -EBUSY;
5e6e3a92
BZ
1486 }
1487
600f5d90 1488 if (priv->scan_block) {
acebe8c1
ZL
1489 mwifiex_dbg(adapter, WARN,
1490 "cmd: Scan is blocked during association...\n");
c2476335 1491 return -EBUSY;
5e6e3a92
BZ
1492 }
1493
e00483f7 1494 if (adapter->surprise_removed || adapter->is_cmd_timedout) {
acebe8c1
ZL
1495 mwifiex_dbg(adapter, ERROR,
1496 "Ignore scan. Card removed or firmware in bad state\n");
e00483f7
AK
1497 return -EFAULT;
1498 }
1499
c2476335
BZ
1500 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
1501 adapter->scan_processing = true;
1502 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
1503
5e6e3a92 1504 scan_cfg_out = kzalloc(sizeof(union mwifiex_scan_cmd_config_tlv),
0d2e7a5c 1505 GFP_KERNEL);
5e6e3a92 1506 if (!scan_cfg_out) {
061f2e69
AK
1507 ret = -ENOMEM;
1508 goto done;
5e6e3a92
BZ
1509 }
1510
0d2e7a5c
JP
1511 scan_chan_list = kcalloc(MWIFIEX_USER_SCAN_CHAN_MAX,
1512 sizeof(struct mwifiex_chan_scan_param_set),
1513 GFP_KERNEL);
5e6e3a92 1514 if (!scan_chan_list) {
5e6e3a92 1515 kfree(scan_cfg_out);
061f2e69
AK
1516 ret = -ENOMEM;
1517 goto done;
5e6e3a92
BZ
1518 }
1519
cff23cec
YAP
1520 mwifiex_config_scan(priv, user_scan_in, &scan_cfg_out->config,
1521 &chan_list_out, scan_chan_list, &max_chan_per_scan,
1522 &filtered_scan, &scan_current_chan_only);
5e6e3a92 1523
600f5d90
AK
1524 ret = mwifiex_scan_channel_list(priv, max_chan_per_scan, filtered_scan,
1525 &scan_cfg_out->config, chan_list_out,
1526 scan_chan_list);
5e6e3a92
BZ
1527
1528 /* Get scan command from scan_pending_q and put to cmd_pending_q */
1529 if (!ret) {
1530 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1531 if (!list_empty(&adapter->scan_pending_q)) {
1532 cmd_node = list_first_entry(&adapter->scan_pending_q,
cff23cec 1533 struct cmd_ctrl_node, list);
5e6e3a92
BZ
1534 list_del(&cmd_node->list);
1535 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
cff23cec 1536 flags);
463df471 1537 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node);
1a1fb970 1538 queue_work(adapter->workqueue, &adapter->main_work);
00d7ea11
AK
1539
1540 /* Perform internal scan synchronously */
21de979e 1541 if (!priv->scan_request) {
acebe8c1
ZL
1542 mwifiex_dbg(adapter, INFO,
1543 "wait internal scan\n");
00d7ea11 1544 mwifiex_wait_queue_complete(adapter, cmd_node);
21de979e 1545 }
5e6e3a92
BZ
1546 } else {
1547 spin_unlock_irqrestore(&adapter->scan_pending_q_lock,
1548 flags);
1549 }
5e6e3a92
BZ
1550 }
1551
1552 kfree(scan_cfg_out);
1553 kfree(scan_chan_list);
061f2e69
AK
1554done:
1555 if (ret) {
1556 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
1557 adapter->scan_processing = false;
1558 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
1559 }
5e6e3a92
BZ
1560 return ret;
1561}
1562
1563/*
1564 * This function prepares a scan command to be sent to the firmware.
1565 *
1566 * This uses the scan command configuration sent to the command processing
1567 * module in command preparation stage to configure a scan command structure
1568 * to send to firmware.
1569 *
1570 * The fixed fields specifying the BSS type and BSSID filters as well as a
1571 * variable number/length of TLVs are sent in the command to firmware.
1572 *
1573 * Preparation also includes -
1574 * - Setting command ID, and proper size
1575 * - Ensuring correct endian-ness
1576 */
a5ffddb7
AK
1577int mwifiex_cmd_802_11_scan(struct host_cmd_ds_command *cmd,
1578 struct mwifiex_scan_cmd_config *scan_cfg)
5e6e3a92
BZ
1579{
1580 struct host_cmd_ds_802_11_scan *scan_cmd = &cmd->params.scan;
5e6e3a92
BZ
1581
1582 /* Set fixed field variables in scan command */
1583 scan_cmd->bss_mode = scan_cfg->bss_mode;
1584 memcpy(scan_cmd->bssid, scan_cfg->specific_bssid,
1585 sizeof(scan_cmd->bssid));
1586 memcpy(scan_cmd->tlv_buffer, scan_cfg->tlv_buf, scan_cfg->tlv_buf_len);
1587
1588 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SCAN);
1589
1590 /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
1591 cmd->size = cpu_to_le16((u16) (sizeof(scan_cmd->bss_mode)
1592 + sizeof(scan_cmd->bssid)
1593 + scan_cfg->tlv_buf_len + S_DS_GEN));
1594
1595 return 0;
1596}
1597
7c6fa2a8
AK
1598/*
1599 * This function checks compatibility of requested network with current
1600 * driver settings.
1601 */
1602int mwifiex_check_network_compatibility(struct mwifiex_private *priv,
1603 struct mwifiex_bssdescriptor *bss_desc)
1604{
1605 int ret = -1;
1606
1607 if (!bss_desc)
1608 return -1;
1609
6685d109
YAP
1610 if ((mwifiex_get_cfp(priv, (u8) bss_desc->bss_band,
1611 (u16) bss_desc->channel, 0))) {
7c6fa2a8
AK
1612 switch (priv->bss_mode) {
1613 case NL80211_IFTYPE_STATION:
1614 case NL80211_IFTYPE_ADHOC:
1615 ret = mwifiex_is_network_compatible(priv, bss_desc,
1616 priv->bss_mode);
1617 if (ret)
acebe8c1
ZL
1618 mwifiex_dbg(priv->adapter, ERROR,
1619 "Incompatible network settings\n");
2f9279b5 1620 break;
7c6fa2a8 1621 default:
2f9279b5 1622 ret = 0;
7c6fa2a8
AK
1623 }
1624 }
1625
1626 return ret;
1627}
1628
2375fa2b 1629/* This function checks if SSID string contains all zeroes or length is zero */
1630static bool mwifiex_is_hidden_ssid(struct cfg80211_ssid *ssid)
1631{
1632 int idx;
1633
1634 for (idx = 0; idx < ssid->ssid_len; idx++) {
1635 if (ssid->ssid[idx])
1636 return false;
1637 }
1638
1639 return true;
1640}
1641
1642/* This function checks if any hidden SSID found in passive scan channels
1643 * and save those channels for specific SSID active scan
1644 */
1645static int mwifiex_save_hidden_ssid_channels(struct mwifiex_private *priv,
1646 struct cfg80211_bss *bss)
1647{
1648 struct mwifiex_bssdescriptor *bss_desc;
1649 int ret;
1650 int chid;
1651
1652 /* Allocate and fill new bss descriptor */
1653 bss_desc = kzalloc(sizeof(*bss_desc), GFP_KERNEL);
1654 if (!bss_desc)
1655 return -ENOMEM;
1656
1657 ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
1658 if (ret)
1659 goto done;
1660
1661 if (mwifiex_is_hidden_ssid(&bss_desc->ssid)) {
1662 mwifiex_dbg(priv->adapter, INFO, "found hidden SSID\n");
1663 for (chid = 0 ; chid < MWIFIEX_USER_SCAN_CHAN_MAX; chid++) {
1664 if (priv->hidden_chan[chid].chan_number ==
1665 bss->channel->hw_value)
1666 break;
1667
1668 if (!priv->hidden_chan[chid].chan_number) {
1669 priv->hidden_chan[chid].chan_number =
1670 bss->channel->hw_value;
1671 priv->hidden_chan[chid].radio_type =
1672 bss->channel->band;
1673 priv->hidden_chan[chid].scan_type =
1674 MWIFIEX_SCAN_TYPE_ACTIVE;
1675 break;
1676 }
1677 }
1678 }
1679
1680done:
5ff26222
RL
1681 /* beacon_ie buffer was allocated in function
1682 * mwifiex_fill_new_bss_desc(). Free it now.
1683 */
1684 kfree(bss_desc->beacon_buf);
2375fa2b 1685 kfree(bss_desc);
1686 return 0;
1687}
1688
9558a407
AK
1689static int mwifiex_update_curr_bss_params(struct mwifiex_private *priv,
1690 struct cfg80211_bss *bss)
7c6fa2a8 1691{
db652e4b 1692 struct mwifiex_bssdescriptor *bss_desc;
7c6fa2a8
AK
1693 int ret;
1694 unsigned long flags;
7c6fa2a8
AK
1695
1696 /* Allocate and fill new bss descriptor */
0d2e7a5c
JP
1697 bss_desc = kzalloc(sizeof(struct mwifiex_bssdescriptor), GFP_KERNEL);
1698 if (!bss_desc)
7c6fa2a8 1699 return -ENOMEM;
5982b47a 1700
9558a407 1701 ret = mwifiex_fill_new_bss_desc(priv, bss, bss_desc);
7c6fa2a8
AK
1702 if (ret)
1703 goto done;
1704
1705 ret = mwifiex_check_network_compatibility(priv, bss_desc);
1706 if (ret)
1707 goto done;
1708
7c6fa2a8 1709 spin_lock_irqsave(&priv->curr_bcn_buf_lock, flags);
7c6fa2a8
AK
1710 /* Make a copy of current BSSID descriptor */
1711 memcpy(&priv->curr_bss_params.bss_descriptor, bss_desc,
cff23cec 1712 sizeof(priv->curr_bss_params.bss_descriptor));
d837a2ae
BZ
1713
1714 /* The contents of beacon_ie will be copied to its own buffer
1715 * in mwifiex_save_curr_bcn()
1716 */
7c6fa2a8
AK
1717 mwifiex_save_curr_bcn(priv);
1718 spin_unlock_irqrestore(&priv->curr_bcn_buf_lock, flags);
1719
1720done:
d837a2ae
BZ
1721 /* beacon_ie buffer was allocated in function
1722 * mwifiex_fill_new_bss_desc(). Free it now.
1723 */
1724 kfree(bss_desc->beacon_buf);
7c6fa2a8 1725 kfree(bss_desc);
7c6fa2a8
AK
1726 return 0;
1727}
1728
3b4d5c64
AK
1729static int
1730mwifiex_parse_single_response_buf(struct mwifiex_private *priv, u8 **bss_info,
1731 u32 *bytes_left, u64 fw_tsf, u8 *radio_type,
21f58d20 1732 bool ext_scan, s32 rssi_val)
3b4d5c64
AK
1733{
1734 struct mwifiex_adapter *adapter = priv->adapter;
1735 struct mwifiex_chan_freq_power *cfp;
1736 struct cfg80211_bss *bss;
1737 u8 bssid[ETH_ALEN];
1738 s32 rssi;
1739 const u8 *ie_buf;
1740 size_t ie_len;
1741 u16 channel = 0;
1742 u16 beacon_size = 0;
1743 u32 curr_bcn_bytes;
1744 u32 freq;
1745 u16 beacon_period;
1746 u16 cap_info_bitmap;
1747 u8 *current_ptr;
1748 u64 timestamp;
1749 struct mwifiex_fixed_bcn_param *bcn_param;
1750 struct mwifiex_bss_priv *bss_priv;
1751
1752 if (*bytes_left >= sizeof(beacon_size)) {
1753 /* Extract & convert beacon size from command buffer */
5653c646 1754 beacon_size = get_unaligned_le16((*bss_info));
3b4d5c64
AK
1755 *bytes_left -= sizeof(beacon_size);
1756 *bss_info += sizeof(beacon_size);
1757 }
1758
1759 if (!beacon_size || beacon_size > *bytes_left) {
1760 *bss_info += *bytes_left;
1761 *bytes_left = 0;
1762 return -EFAULT;
1763 }
1764
1765 /* Initialize the current working beacon pointer for this BSS
1766 * iteration
1767 */
1768 current_ptr = *bss_info;
1769
1770 /* Advance the return beacon pointer past the current beacon */
1771 *bss_info += beacon_size;
1772 *bytes_left -= beacon_size;
1773
1774 curr_bcn_bytes = beacon_size;
1775
1776 /* First 5 fields are bssid, RSSI(for legacy scan only),
1777 * time stamp, beacon interval, and capability information
1778 */
1779 if (curr_bcn_bytes < ETH_ALEN + sizeof(u8) +
1780 sizeof(struct mwifiex_fixed_bcn_param)) {
acebe8c1
ZL
1781 mwifiex_dbg(adapter, ERROR,
1782 "InterpretIE: not enough bytes left\n");
3b4d5c64
AK
1783 return -EFAULT;
1784 }
1785
1786 memcpy(bssid, current_ptr, ETH_ALEN);
1787 current_ptr += ETH_ALEN;
1788 curr_bcn_bytes -= ETH_ALEN;
1789
1790 if (!ext_scan) {
45d18c56 1791 rssi = (s32) *current_ptr;
3b4d5c64
AK
1792 rssi = (-rssi) * 100; /* Convert dBm to mBm */
1793 current_ptr += sizeof(u8);
1794 curr_bcn_bytes -= sizeof(u8);
acebe8c1
ZL
1795 mwifiex_dbg(adapter, INFO,
1796 "info: InterpretIE: RSSI=%d\n", rssi);
21f58d20
AK
1797 } else {
1798 rssi = rssi_val;
3b4d5c64
AK
1799 }
1800
1801 bcn_param = (struct mwifiex_fixed_bcn_param *)current_ptr;
1802 current_ptr += sizeof(*bcn_param);
1803 curr_bcn_bytes -= sizeof(*bcn_param);
1804
1805 timestamp = le64_to_cpu(bcn_param->timestamp);
1806 beacon_period = le16_to_cpu(bcn_param->beacon_period);
1807
1808 cap_info_bitmap = le16_to_cpu(bcn_param->cap_info_bitmap);
acebe8c1
ZL
1809 mwifiex_dbg(adapter, INFO,
1810 "info: InterpretIE: capabilities=0x%X\n",
1811 cap_info_bitmap);
3b4d5c64
AK
1812
1813 /* Rest of the current buffer are IE's */
1814 ie_buf = current_ptr;
1815 ie_len = curr_bcn_bytes;
acebe8c1
ZL
1816 mwifiex_dbg(adapter, INFO,
1817 "info: InterpretIE: IELength for this AP = %d\n",
1818 curr_bcn_bytes);
3b4d5c64
AK
1819
1820 while (curr_bcn_bytes >= sizeof(struct ieee_types_header)) {
1821 u8 element_id, element_len;
1822
1823 element_id = *current_ptr;
1824 element_len = *(current_ptr + 1);
1825 if (curr_bcn_bytes < element_len +
1826 sizeof(struct ieee_types_header)) {
acebe8c1
ZL
1827 mwifiex_dbg(adapter, ERROR,
1828 "%s: bytes left < IE length\n", __func__);
3b4d5c64
AK
1829 return -EFAULT;
1830 }
1831 if (element_id == WLAN_EID_DS_PARAMS) {
1832 channel = *(current_ptr +
1833 sizeof(struct ieee_types_header));
1834 break;
1835 }
1836
1837 current_ptr += element_len + sizeof(struct ieee_types_header);
1838 curr_bcn_bytes -= element_len +
1839 sizeof(struct ieee_types_header);
1840 }
1841
1842 if (channel) {
1843 struct ieee80211_channel *chan;
1844 u8 band;
1845
1846 /* Skip entry if on csa closed channel */
1847 if (channel == priv->csa_chan) {
acebe8c1
ZL
1848 mwifiex_dbg(adapter, WARN,
1849 "Dropping entry on csa closed channel\n");
3b4d5c64
AK
1850 return 0;
1851 }
1852
1853 band = BAND_G;
1854 if (radio_type)
1855 band = mwifiex_radio_type_to_band(*radio_type &
1856 (BIT(0) | BIT(1)));
1857
1858 cfp = mwifiex_get_cfp(priv, band, channel, 0);
1859
1860 freq = cfp ? cfp->freq : 0;
1861
4facc34a 1862 chan = ieee80211_get_channel(priv->wdev.wiphy, freq);
3b4d5c64
AK
1863
1864 if (chan && !(chan->flags & IEEE80211_CHAN_DISABLED)) {
4facc34a 1865 bss = cfg80211_inform_bss(priv->wdev.wiphy,
5bc8c1f2
JB
1866 chan, CFG80211_BSS_FTYPE_UNKNOWN,
1867 bssid, timestamp,
3b4d5c64
AK
1868 cap_info_bitmap, beacon_period,
1869 ie_buf, ie_len, rssi, GFP_KERNEL);
02421dd3
XH
1870 if (bss) {
1871 bss_priv = (struct mwifiex_bss_priv *)bss->priv;
1872 bss_priv->band = band;
1873 bss_priv->fw_tsf = fw_tsf;
1874 if (priv->media_connected &&
1875 !memcmp(bssid, priv->curr_bss_params.
1876 bss_descriptor.mac_address,
1877 ETH_ALEN))
1878 mwifiex_update_curr_bss_params(priv,
1879 bss);
1880 cfg80211_put_bss(priv->wdev.wiphy, bss);
1881 }
2375fa2b 1882
1883 if ((chan->flags & IEEE80211_CHAN_RADAR) ||
1884 (chan->flags & IEEE80211_CHAN_NO_IR)) {
1885 mwifiex_dbg(adapter, INFO,
1886 "radar or passive channel %d\n",
1887 channel);
1888 mwifiex_save_hidden_ssid_channels(priv, bss);
1889 }
3b4d5c64
AK
1890 }
1891 } else {
acebe8c1 1892 mwifiex_dbg(adapter, WARN, "missing BSS channel IE\n");
3b4d5c64
AK
1893 }
1894
1895 return 0;
1896}
1897
21445049
AP
1898static void mwifiex_complete_scan(struct mwifiex_private *priv)
1899{
1900 struct mwifiex_adapter *adapter = priv->adapter;
1901
bf354433 1902 adapter->survey_idx = 0;
21445049
AP
1903 if (adapter->curr_cmd->wait_q_enabled) {
1904 adapter->cmd_wait_q.status = 0;
1905 if (!priv->scan_request) {
acebe8c1
ZL
1906 mwifiex_dbg(adapter, INFO,
1907 "complete internal scan\n");
21445049
AP
1908 mwifiex_complete_cmd(adapter, adapter->curr_cmd);
1909 }
1910 }
1911}
1912
2375fa2b 1913/* This function checks if any hidden SSID found in passive scan channels
1914 * and do specific SSID active scan for those channels
1915 */
1916static int
1917mwifiex_active_scan_req_for_passive_chan(struct mwifiex_private *priv)
1918{
1919 int ret;
1920 struct mwifiex_adapter *adapter = priv->adapter;
1921 u8 id = 0;
1922 struct mwifiex_user_scan_cfg *user_scan_cfg;
1923
16d25da9
AK
1924 if (adapter->active_scan_triggered || !priv->scan_request ||
1925 priv->scan_aborting) {
2375fa2b 1926 adapter->active_scan_triggered = false;
1927 return 0;
1928 }
1929
1930 if (!priv->hidden_chan[0].chan_number) {
1931 mwifiex_dbg(adapter, INFO, "No BSS with hidden SSID found on DFS channels\n");
1932 return 0;
1933 }
1934 user_scan_cfg = kzalloc(sizeof(*user_scan_cfg), GFP_KERNEL);
1935
1936 if (!user_scan_cfg)
1937 return -ENOMEM;
1938
1939 memset(user_scan_cfg, 0, sizeof(*user_scan_cfg));
1940
1941 for (id = 0; id < MWIFIEX_USER_SCAN_CHAN_MAX; id++) {
1942 if (!priv->hidden_chan[id].chan_number)
1943 break;
1944 memcpy(&user_scan_cfg->chan_list[id],
1945 &priv->hidden_chan[id],
1946 sizeof(struct mwifiex_user_scan_chan));
1947 }
1948
1949 adapter->active_scan_triggered = true;
c2a8f0ff 1950 ether_addr_copy(user_scan_cfg->random_mac, priv->random_mac);
2375fa2b 1951 user_scan_cfg->num_ssids = priv->scan_request->n_ssids;
1952 user_scan_cfg->ssid_list = priv->scan_request->ssids;
1953
1954 ret = mwifiex_scan_networks(priv, user_scan_cfg);
1955 kfree(user_scan_cfg);
1956
1957 memset(&priv->hidden_chan, 0, sizeof(priv->hidden_chan));
1958
1959 if (ret) {
1960 dev_err(priv->adapter->dev, "scan failed: %d\n", ret);
1961 return ret;
1962 }
1963
1964 return 0;
1965}
d44b5c2f
AK
1966static void mwifiex_check_next_scan_command(struct mwifiex_private *priv)
1967{
1968 struct mwifiex_adapter *adapter = priv->adapter;
c70ca8cb 1969 struct cmd_ctrl_node *cmd_node;
d44b5c2f
AK
1970 unsigned long flags;
1971
1972 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
1973 if (list_empty(&adapter->scan_pending_q)) {
1974 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
c70ca8cb 1975
d44b5c2f
AK
1976 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
1977 adapter->scan_processing = false;
1978 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
1979
2375fa2b 1980 mwifiex_active_scan_req_for_passive_chan(priv);
1981
21445049
AP
1982 if (!adapter->ext_scan)
1983 mwifiex_complete_scan(priv);
1984
d44b5c2f 1985 if (priv->scan_request) {
1d76250b
AS
1986 struct cfg80211_scan_info info = {
1987 .aborted = false,
1988 };
1989
acebe8c1
ZL
1990 mwifiex_dbg(adapter, INFO,
1991 "info: notifying scan done\n");
1d76250b 1992 cfg80211_scan_done(priv->scan_request, &info);
d44b5c2f 1993 priv->scan_request = NULL;
09e672a1 1994 priv->scan_aborting = false;
d44b5c2f
AK
1995 } else {
1996 priv->scan_aborting = false;
acebe8c1
ZL
1997 mwifiex_dbg(adapter, INFO,
1998 "info: scan already aborted\n");
d44b5c2f 1999 }
d8d91253
AK
2000 } else if ((priv->scan_aborting && !priv->scan_request) ||
2001 priv->scan_block) {
d8d91253
AK
2002 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
2003
c70ca8cb
AF
2004 mwifiex_cancel_pending_scan_cmd(adapter);
2005
d8d91253
AK
2006 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, flags);
2007 adapter->scan_processing = false;
2008 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, flags);
2009
2375fa2b 2010 if (!adapter->active_scan_triggered) {
2011 if (priv->scan_request) {
1d76250b
AS
2012 struct cfg80211_scan_info info = {
2013 .aborted = true,
2014 };
2015
2375fa2b 2016 mwifiex_dbg(adapter, INFO,
2017 "info: aborting scan\n");
1d76250b 2018 cfg80211_scan_done(priv->scan_request, &info);
2375fa2b 2019 priv->scan_request = NULL;
09e672a1 2020 priv->scan_aborting = false;
2375fa2b 2021 } else {
2022 priv->scan_aborting = false;
2023 mwifiex_dbg(adapter, INFO,
2024 "info: scan already aborted\n");
2025 }
d44b5c2f 2026 }
d8d91253
AK
2027 } else {
2028 /* Get scan command from scan_pending_q and put to
2029 * cmd_pending_q
2030 */
2031 cmd_node = list_first_entry(&adapter->scan_pending_q,
2032 struct cmd_ctrl_node, list);
2033 list_del(&cmd_node->list);
2034 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
463df471 2035 mwifiex_insert_cmd_to_pending_q(adapter, cmd_node);
d44b5c2f
AK
2036 }
2037
2038 return;
2039}
2040
a9c790ba
XH
2041void mwifiex_cancel_scan(struct mwifiex_adapter *adapter)
2042{
2043 struct mwifiex_private *priv;
2044 unsigned long cmd_flags;
2045 int i;
2046
2047 mwifiex_cancel_pending_scan_cmd(adapter);
2048
2049 if (adapter->scan_processing) {
2050 spin_lock_irqsave(&adapter->mwifiex_cmd_lock, cmd_flags);
2051 adapter->scan_processing = false;
2052 spin_unlock_irqrestore(&adapter->mwifiex_cmd_lock, cmd_flags);
2053 for (i = 0; i < adapter->priv_num; i++) {
2054 priv = adapter->priv[i];
2055 if (!priv)
2056 continue;
2057 if (priv->scan_request) {
88b3ec52
DM
2058 struct cfg80211_scan_info info = {
2059 .aborted = true,
2060 };
2061
a9c790ba
XH
2062 mwifiex_dbg(adapter, INFO,
2063 "info: aborting scan\n");
88b3ec52 2064 cfg80211_scan_done(priv->scan_request, &info);
a9c790ba 2065 priv->scan_request = NULL;
09e672a1 2066 priv->scan_aborting = false;
a9c790ba
XH
2067 }
2068 }
2069 }
2070}
2071
5e6e3a92
BZ
2072/*
2073 * This function handles the command response of scan.
2074 *
2075 * The response buffer for the scan command has the following
2076 * memory layout:
2077 *
2078 * .-------------------------------------------------------------.
2079 * | Header (4 * sizeof(t_u16)): Standard command response hdr |
2080 * .-------------------------------------------------------------.
2081 * | BufSize (t_u16) : sizeof the BSS Description data |
2082 * .-------------------------------------------------------------.
2083 * | NumOfSet (t_u8) : Number of BSS Descs returned |
2084 * .-------------------------------------------------------------.
2085 * | BSSDescription data (variable, size given in BufSize) |
2086 * .-------------------------------------------------------------.
2087 * | TLV data (variable, size calculated using Header->Size, |
2088 * | BufSize and sizeof the fixed fields above) |
2089 * .-------------------------------------------------------------.
2090 */
2091int mwifiex_ret_802_11_scan(struct mwifiex_private *priv,
600f5d90 2092 struct host_cmd_ds_command *resp)
5e6e3a92
BZ
2093{
2094 int ret = 0;
2095 struct mwifiex_adapter *adapter = priv->adapter;
270e58e8 2096 struct host_cmd_ds_802_11_scan_rsp *scan_rsp;
5e6e3a92
BZ
2097 struct mwifiex_ie_types_data *tlv_data;
2098 struct mwifiex_ie_types_tsf_timestamp *tsf_tlv;
2099 u8 *bss_info;
2100 u32 scan_resp_size;
2101 u32 bytes_left;
5e6e3a92
BZ
2102 u32 idx;
2103 u32 tlv_buf_size;
5e6e3a92
BZ
2104 struct mwifiex_ie_types_chan_band_list_param_set *chan_band_tlv;
2105 struct chan_band_param_set *chan_band;
5e6e3a92 2106 u8 is_bgscan_resp;
3b4d5c64
AK
2107 __le64 fw_tsf = 0;
2108 u8 *radio_type;
7d7f07d8 2109 struct cfg80211_wowlan_nd_match *pmatch;
2110 struct cfg80211_sched_scan_request *nd_config = NULL;
5e6e3a92
BZ
2111
2112 is_bgscan_resp = (le16_to_cpu(resp->command)
cff23cec 2113 == HostCmd_CMD_802_11_BG_SCAN_QUERY);
5e6e3a92
BZ
2114 if (is_bgscan_resp)
2115 scan_rsp = &resp->params.bg_scan_query_resp.scan_resp;
2116 else
2117 scan_rsp = &resp->params.scan_resp;
2118
2119
67a50035 2120 if (scan_rsp->number_of_sets > MWIFIEX_MAX_AP) {
acebe8c1
ZL
2121 mwifiex_dbg(adapter, ERROR,
2122 "SCAN_RESP: too many AP returned (%d)\n",
2123 scan_rsp->number_of_sets);
5e6e3a92 2124 ret = -1;
8a7d7cbf 2125 goto check_next_scan;
5e6e3a92
BZ
2126 }
2127
b887664d
AK
2128 /* Check csa channel expiry before parsing scan response */
2129 mwifiex_11h_get_csa_closed_channel(priv);
2130
5e6e3a92 2131 bytes_left = le16_to_cpu(scan_rsp->bss_descript_size);
acebe8c1
ZL
2132 mwifiex_dbg(adapter, INFO,
2133 "info: SCAN_RESP: bss_descript_size %d\n",
2134 bytes_left);
5e6e3a92
BZ
2135
2136 scan_resp_size = le16_to_cpu(resp->size);
2137
acebe8c1
ZL
2138 mwifiex_dbg(adapter, INFO,
2139 "info: SCAN_RESP: returned %d APs before parsing\n",
2140 scan_rsp->number_of_sets);
5e6e3a92 2141
5e6e3a92
BZ
2142 bss_info = scan_rsp->bss_desc_and_tlv_buffer;
2143
2144 /*
2145 * The size of the TLV buffer is equal to the entire command response
2146 * size (scan_resp_size) minus the fixed fields (sizeof()'s), the
2147 * BSS Descriptions (bss_descript_size as bytesLef) and the command
2148 * response header (S_DS_GEN)
2149 */
2150 tlv_buf_size = scan_resp_size - (bytes_left
2151 + sizeof(scan_rsp->bss_descript_size)
2152 + sizeof(scan_rsp->number_of_sets)
2153 + S_DS_GEN);
2154
2155 tlv_data = (struct mwifiex_ie_types_data *) (scan_rsp->
2156 bss_desc_and_tlv_buffer +
2157 bytes_left);
2158
2159 /* Search the TLV buffer space in the scan response for any valid
2160 TLVs */
2161 mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
2162 TLV_TYPE_TSFTIMESTAMP,
2163 (struct mwifiex_ie_types_data **)
2164 &tsf_tlv);
2165
2166 /* Search the TLV buffer space in the scan response for any valid
2167 TLVs */
2168 mwifiex_ret_802_11_scan_get_tlv_ptrs(adapter, tlv_data, tlv_buf_size,
2169 TLV_TYPE_CHANNELBANDLIST,
2170 (struct mwifiex_ie_types_data **)
2171 &chan_band_tlv);
2172
7d7f07d8 2173#ifdef CONFIG_PM
2174 if (priv->wdev.wiphy->wowlan_config)
2175 nd_config = priv->wdev.wiphy->wowlan_config->nd_config;
2176#endif
2177
2178 if (nd_config) {
2179 adapter->nd_info =
2180 kzalloc(sizeof(struct cfg80211_wowlan_nd_match) +
2181 sizeof(struct cfg80211_wowlan_nd_match *) *
2182 scan_rsp->number_of_sets, GFP_ATOMIC);
2183
2184 if (adapter->nd_info)
2185 adapter->nd_info->n_matches = scan_rsp->number_of_sets;
2186 }
2187
5e6e3a92 2188 for (idx = 0; idx < scan_rsp->number_of_sets && bytes_left; idx++) {
5e6e3a92
BZ
2189 /*
2190 * If the TSF TLV was appended to the scan results, save this
b5abcf02
AK
2191 * entry's TSF value in the fw_tsf field. It is the firmware's
2192 * TSF value at the time the beacon or probe response was
2193 * received.
5e6e3a92 2194 */
7c6fa2a8 2195 if (tsf_tlv)
b5abcf02
AK
2196 memcpy(&fw_tsf, &tsf_tlv->tsf_data[idx * TSF_DATA_SIZE],
2197 sizeof(fw_tsf));
7c6fa2a8 2198
3b4d5c64
AK
2199 if (chan_band_tlv) {
2200 chan_band = &chan_band_tlv->chan_band_param[idx];
2201 radio_type = &chan_band->radio_type;
7c6fa2a8 2202 } else {
3b4d5c64 2203 radio_type = NULL;
7c6fa2a8 2204 }
3b4d5c64 2205
7d7f07d8 2206 if (chan_band_tlv && adapter->nd_info) {
2207 adapter->nd_info->matches[idx] =
b7116576
CJ
2208 kzalloc(sizeof(*pmatch) + sizeof(u32),
2209 GFP_ATOMIC);
7d7f07d8 2210
2211 pmatch = adapter->nd_info->matches[idx];
2212
efdf0e39 2213 if (pmatch) {
b7116576
CJ
2214 pmatch->n_channels = 1;
2215 pmatch->channels[0] = chan_band->chan_number;
7d7f07d8 2216 }
2217 }
2218
3b4d5c64
AK
2219 ret = mwifiex_parse_single_response_buf(priv, &bss_info,
2220 &bytes_left,
2221 le64_to_cpu(fw_tsf),
21f58d20 2222 radio_type, false, 0);
3b4d5c64
AK
2223 if (ret)
2224 goto check_next_scan;
7c6fa2a8 2225 }
5e6e3a92 2226
8a7d7cbf 2227check_next_scan:
d44b5c2f 2228 mwifiex_check_next_scan_command(priv);
5e6e3a92
BZ
2229 return ret;
2230}
2231
21f58d20
AK
2232/*
2233 * This function prepares an extended scan command to be sent to the firmware
2234 *
2235 * This uses the scan command configuration sent to the command processing
2236 * module in command preparation stage to configure a extended scan command
2237 * structure to send to firmware.
2238 */
2239int mwifiex_cmd_802_11_scan_ext(struct mwifiex_private *priv,
2240 struct host_cmd_ds_command *cmd,
2241 void *data_buf)
2242{
2243 struct host_cmd_ds_802_11_scan_ext *ext_scan = &cmd->params.ext_scan;
2244 struct mwifiex_scan_cmd_config *scan_cfg = data_buf;
2245
2246 memcpy(ext_scan->tlv_buffer, scan_cfg->tlv_buf, scan_cfg->tlv_buf_len);
2247
2248 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SCAN_EXT);
2249
2250 /* Size is equal to the sizeof(fixed portions) + the TLV len + header */
2251 cmd->size = cpu_to_le16((u16)(sizeof(ext_scan->reserved)
2252 + scan_cfg->tlv_buf_len + S_DS_GEN));
2253
2254 return 0;
2255}
2256
0c9b7f22
XH
2257/* This function prepares an background scan config command to be sent
2258 * to the firmware
2259 */
2260int mwifiex_cmd_802_11_bg_scan_config(struct mwifiex_private *priv,
2261 struct host_cmd_ds_command *cmd,
2262 void *data_buf)
2263{
2264 struct host_cmd_ds_802_11_bg_scan_config *bgscan_config =
2265 &cmd->params.bg_scan_config;
2266 struct mwifiex_bg_scan_cfg *bgscan_cfg_in = data_buf;
2267 u8 *tlv_pos = bgscan_config->tlv;
2268 u8 num_probes;
2269 u32 ssid_len, chan_idx, scan_type, scan_dur, chan_num;
2270 int i;
2271 struct mwifiex_ie_types_num_probes *num_probes_tlv;
2272 struct mwifiex_ie_types_repeat_count *repeat_count_tlv;
fdcab083 2273 struct mwifiex_ie_types_min_rssi_threshold *rssi_threshold_tlv;
0c9b7f22
XH
2274 struct mwifiex_ie_types_bgscan_start_later *start_later_tlv;
2275 struct mwifiex_ie_types_wildcard_ssid_params *wildcard_ssid_tlv;
2276 struct mwifiex_ie_types_chan_list_param_set *chan_list_tlv;
2277 struct mwifiex_chan_scan_param_set *temp_chan;
2278
2279 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_CONFIG);
2280 cmd->size = cpu_to_le16(sizeof(*bgscan_config) + S_DS_GEN);
2281
2282 bgscan_config->action = cpu_to_le16(bgscan_cfg_in->action);
2283 bgscan_config->enable = bgscan_cfg_in->enable;
2284 bgscan_config->bss_type = bgscan_cfg_in->bss_type;
2285 bgscan_config->scan_interval =
2286 cpu_to_le32(bgscan_cfg_in->scan_interval);
2287 bgscan_config->report_condition =
2288 cpu_to_le32(bgscan_cfg_in->report_condition);
2289
2290 /* stop sched scan */
2291 if (!bgscan_config->enable)
2292 return 0;
2293
2294 bgscan_config->chan_per_scan = bgscan_cfg_in->chan_per_scan;
2295
2296 num_probes = (bgscan_cfg_in->num_probes ? bgscan_cfg_in->
2297 num_probes : priv->adapter->scan_probes);
2298
2299 if (num_probes) {
2300 num_probes_tlv = (struct mwifiex_ie_types_num_probes *)tlv_pos;
2301 num_probes_tlv->header.type = cpu_to_le16(TLV_TYPE_NUMPROBES);
2302 num_probes_tlv->header.len =
2303 cpu_to_le16(sizeof(num_probes_tlv->num_probes));
2304 num_probes_tlv->num_probes = cpu_to_le16((u16)num_probes);
2305
2306 tlv_pos += sizeof(num_probes_tlv->header) +
2307 le16_to_cpu(num_probes_tlv->header.len);
2308 }
2309
2310 if (bgscan_cfg_in->repeat_count) {
2311 repeat_count_tlv =
2312 (struct mwifiex_ie_types_repeat_count *)tlv_pos;
2313 repeat_count_tlv->header.type =
2314 cpu_to_le16(TLV_TYPE_REPEAT_COUNT);
2315 repeat_count_tlv->header.len =
2316 cpu_to_le16(sizeof(repeat_count_tlv->repeat_count));
2317 repeat_count_tlv->repeat_count =
2318 cpu_to_le16(bgscan_cfg_in->repeat_count);
2319
2320 tlv_pos += sizeof(repeat_count_tlv->header) +
2321 le16_to_cpu(repeat_count_tlv->header.len);
2322 }
2323
fdcab083
GB
2324 if (bgscan_cfg_in->rssi_threshold) {
2325 rssi_threshold_tlv =
2326 (struct mwifiex_ie_types_min_rssi_threshold *)tlv_pos;
2327 rssi_threshold_tlv->header.type =
2328 cpu_to_le16(TLV_TYPE_RSSI_LOW);
2329 rssi_threshold_tlv->header.len =
2330 cpu_to_le16(sizeof(rssi_threshold_tlv->rssi_threshold));
2331 rssi_threshold_tlv->rssi_threshold =
2332 cpu_to_le16(bgscan_cfg_in->rssi_threshold);
2333
2334 tlv_pos += sizeof(rssi_threshold_tlv->header) +
2335 le16_to_cpu(rssi_threshold_tlv->header.len);
2336 }
2337
0c9b7f22
XH
2338 for (i = 0; i < bgscan_cfg_in->num_ssids; i++) {
2339 ssid_len = bgscan_cfg_in->ssid_list[i].ssid.ssid_len;
2340
2341 wildcard_ssid_tlv =
2342 (struct mwifiex_ie_types_wildcard_ssid_params *)tlv_pos;
2343 wildcard_ssid_tlv->header.type =
2344 cpu_to_le16(TLV_TYPE_WILDCARDSSID);
2345 wildcard_ssid_tlv->header.len = cpu_to_le16(
2346 (u16)(ssid_len + sizeof(wildcard_ssid_tlv->
2347 max_ssid_length)));
2348
2349 /* max_ssid_length = 0 tells firmware to perform
2350 * specific scan for the SSID filled, whereas
2351 * max_ssid_length = IEEE80211_MAX_SSID_LEN is for
2352 * wildcard scan.
2353 */
2354 if (ssid_len)
2355 wildcard_ssid_tlv->max_ssid_length = 0;
2356 else
2357 wildcard_ssid_tlv->max_ssid_length =
2358 IEEE80211_MAX_SSID_LEN;
2359
2360 memcpy(wildcard_ssid_tlv->ssid,
2361 bgscan_cfg_in->ssid_list[i].ssid.ssid, ssid_len);
2362
2363 tlv_pos += (sizeof(wildcard_ssid_tlv->header)
2364 + le16_to_cpu(wildcard_ssid_tlv->header.len));
2365 }
2366
2367 chan_list_tlv = (struct mwifiex_ie_types_chan_list_param_set *)tlv_pos;
2368
2369 if (bgscan_cfg_in->chan_list[0].chan_number) {
2370 dev_dbg(priv->adapter->dev, "info: bgscan: Using supplied channel list\n");
2371
2372 chan_list_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
2373
2374 for (chan_idx = 0;
2375 chan_idx < MWIFIEX_BG_SCAN_CHAN_MAX &&
2376 bgscan_cfg_in->chan_list[chan_idx].chan_number;
2377 chan_idx++) {
2378 temp_chan = chan_list_tlv->chan_scan_param + chan_idx;
2379
2380 /* Increment the TLV header length by size appended */
5653c646
DM
2381 le16_unaligned_add_cpu(&chan_list_tlv->header.len,
2382 sizeof(
2383 chan_list_tlv->chan_scan_param));
0c9b7f22
XH
2384
2385 temp_chan->chan_number =
2386 bgscan_cfg_in->chan_list[chan_idx].chan_number;
2387 temp_chan->radio_type =
2388 bgscan_cfg_in->chan_list[chan_idx].radio_type;
2389
2390 scan_type =
2391 bgscan_cfg_in->chan_list[chan_idx].scan_type;
2392
2393 if (scan_type == MWIFIEX_SCAN_TYPE_PASSIVE)
2394 temp_chan->chan_scan_mode_bitmap
2395 |= MWIFIEX_PASSIVE_SCAN;
2396 else
2397 temp_chan->chan_scan_mode_bitmap
2398 &= ~MWIFIEX_PASSIVE_SCAN;
2399
2400 if (bgscan_cfg_in->chan_list[chan_idx].scan_time) {
2401 scan_dur = (u16)bgscan_cfg_in->
2402 chan_list[chan_idx].scan_time;
2403 } else {
2404 scan_dur = (scan_type ==
2405 MWIFIEX_SCAN_TYPE_PASSIVE) ?
2406 priv->adapter->passive_scan_time :
2407 priv->adapter->specific_scan_time;
2408 }
2409
2410 temp_chan->min_scan_time = cpu_to_le16(scan_dur);
2411 temp_chan->max_scan_time = cpu_to_le16(scan_dur);
2412 }
2413 } else {
2414 dev_dbg(priv->adapter->dev,
2415 "info: bgscan: Creating full region channel list\n");
2416 chan_num =
2417 mwifiex_bgscan_create_channel_list(priv, bgscan_cfg_in,
2418 chan_list_tlv->
2419 chan_scan_param);
5653c646
DM
2420 le16_unaligned_add_cpu(&chan_list_tlv->header.len,
2421 chan_num *
0c9b7f22
XH
2422 sizeof(chan_list_tlv->chan_scan_param[0]));
2423 }
2424
2425 tlv_pos += (sizeof(chan_list_tlv->header)
2426 + le16_to_cpu(chan_list_tlv->header.len));
2427
2428 if (bgscan_cfg_in->start_later) {
2429 start_later_tlv =
2430 (struct mwifiex_ie_types_bgscan_start_later *)tlv_pos;
2431 start_later_tlv->header.type =
2432 cpu_to_le16(TLV_TYPE_BGSCAN_START_LATER);
2433 start_later_tlv->header.len =
2434 cpu_to_le16(sizeof(start_later_tlv->start_later));
2435 start_later_tlv->start_later =
2436 cpu_to_le16(bgscan_cfg_in->start_later);
2437
2438 tlv_pos += sizeof(start_later_tlv->header) +
2439 le16_to_cpu(start_later_tlv->header.len);
2440 }
2441
2442 /* Append vendor specific IE TLV */
2443 mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_BGSCAN, &tlv_pos);
2444
5653c646 2445 le16_unaligned_add_cpu(&cmd->size, tlv_pos - bgscan_config->tlv);
0c9b7f22
XH
2446
2447 return 0;
2448}
2449
2450int mwifiex_stop_bg_scan(struct mwifiex_private *priv)
2451{
2452 struct mwifiex_bg_scan_cfg *bgscan_cfg;
2453
2454 if (!priv->sched_scanning) {
2455 dev_dbg(priv->adapter->dev, "bgscan already stopped!\n");
2456 return 0;
2457 }
2458
2459 bgscan_cfg = kzalloc(sizeof(*bgscan_cfg), GFP_KERNEL);
2460 if (!bgscan_cfg)
2461 return -ENOMEM;
2462
2463 bgscan_cfg->bss_type = MWIFIEX_BSS_MODE_INFRA;
2464 bgscan_cfg->action = MWIFIEX_BGSCAN_ACT_SET;
2465 bgscan_cfg->enable = false;
2466
2467 if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_BG_SCAN_CONFIG,
2468 HostCmd_ACT_GEN_SET, 0, bgscan_cfg, true)) {
2469 kfree(bgscan_cfg);
2470 return -EFAULT;
2471 }
2472
2473 kfree(bgscan_cfg);
2474 priv->sched_scanning = false;
2475
2476 return 0;
2477}
2478
bf354433
AP
2479static void
2480mwifiex_update_chan_statistics(struct mwifiex_private *priv,
2481 struct mwifiex_ietypes_chanstats *tlv_stat)
2482{
2483 struct mwifiex_adapter *adapter = priv->adapter;
2484 u8 i, num_chan;
2485 struct mwifiex_fw_chan_stats *fw_chan_stats;
2486 struct mwifiex_chan_stats chan_stats;
2487
2488 fw_chan_stats = (void *)((u8 *)tlv_stat +
2489 sizeof(struct mwifiex_ie_types_header));
2490 num_chan = le16_to_cpu(tlv_stat->header.len) /
2491 sizeof(struct mwifiex_chan_stats);
2492
2493 for (i = 0 ; i < num_chan; i++) {
4b5dde2d
BN
2494 if (adapter->survey_idx >= adapter->num_in_chan_stats) {
2495 mwifiex_dbg(adapter, WARN,
2496 "FW reported too many channel results (max %d)\n",
2497 adapter->num_in_chan_stats);
2498 return;
2499 }
bf354433
AP
2500 chan_stats.chan_num = fw_chan_stats->chan_num;
2501 chan_stats.bandcfg = fw_chan_stats->bandcfg;
2502 chan_stats.flags = fw_chan_stats->flags;
2503 chan_stats.noise = fw_chan_stats->noise;
2504 chan_stats.total_bss = le16_to_cpu(fw_chan_stats->total_bss);
2505 chan_stats.cca_scan_dur =
2506 le16_to_cpu(fw_chan_stats->cca_scan_dur);
2507 chan_stats.cca_busy_dur =
2508 le16_to_cpu(fw_chan_stats->cca_busy_dur);
acebe8c1
ZL
2509 mwifiex_dbg(adapter, INFO,
2510 "chan=%d, noise=%d, total_network=%d scan_duration=%d, busy_duration=%d\n",
2511 chan_stats.chan_num,
2512 chan_stats.noise,
2513 chan_stats.total_bss,
2514 chan_stats.cca_scan_dur,
2515 chan_stats.cca_busy_dur);
bf354433
AP
2516 memcpy(&adapter->chan_stats[adapter->survey_idx++], &chan_stats,
2517 sizeof(struct mwifiex_chan_stats));
2518 fw_chan_stats++;
2519 }
2520}
2521
21f58d20 2522/* This function handles the command response of extended scan */
bf354433
AP
2523int mwifiex_ret_802_11_scan_ext(struct mwifiex_private *priv,
2524 struct host_cmd_ds_command *resp)
21f58d20 2525{
3223db20 2526 struct mwifiex_adapter *adapter = priv->adapter;
bf354433
AP
2527 struct host_cmd_ds_802_11_scan_ext *ext_scan_resp;
2528 struct mwifiex_ie_types_header *tlv;
2529 struct mwifiex_ietypes_chanstats *tlv_stat;
2530 u16 buf_left, type, len;
2531
3223db20
AP
2532 struct host_cmd_ds_command *cmd_ptr;
2533 struct cmd_ctrl_node *cmd_node;
2534 unsigned long cmd_flags, scan_flags;
2535 bool complete_scan = false;
2536
acebe8c1 2537 mwifiex_dbg(adapter, INFO, "info: EXT scan returns successfully\n");
21445049 2538
bf354433
AP
2539 ext_scan_resp = &resp->params.ext_scan;
2540
2541 tlv = (void *)ext_scan_resp->tlv_buffer;
2542 buf_left = le16_to_cpu(resp->size) - (sizeof(*ext_scan_resp) + S_DS_GEN
2543 - 1);
2544
2545 while (buf_left >= sizeof(struct mwifiex_ie_types_header)) {
2546 type = le16_to_cpu(tlv->type);
2547 len = le16_to_cpu(tlv->len);
2548
2549 if (buf_left < (sizeof(struct mwifiex_ie_types_header) + len)) {
acebe8c1
ZL
2550 mwifiex_dbg(adapter, ERROR,
2551 "error processing scan response TLVs");
bf354433
AP
2552 break;
2553 }
2554
2555 switch (type) {
2556 case TLV_TYPE_CHANNEL_STATS:
2557 tlv_stat = (void *)tlv;
2558 mwifiex_update_chan_statistics(priv, tlv_stat);
2559 break;
2560 default:
2561 break;
2562 }
2563
2564 buf_left -= len + sizeof(struct mwifiex_ie_types_header);
2565 tlv = (void *)((u8 *)tlv + len +
2566 sizeof(struct mwifiex_ie_types_header));
2567 }
2568
3223db20
AP
2569 spin_lock_irqsave(&adapter->cmd_pending_q_lock, cmd_flags);
2570 spin_lock_irqsave(&adapter->scan_pending_q_lock, scan_flags);
2571 if (list_empty(&adapter->scan_pending_q)) {
2572 complete_scan = true;
2573 list_for_each_entry(cmd_node, &adapter->cmd_pending_q, list) {
2574 cmd_ptr = (void *)cmd_node->cmd_skb->data;
2575 if (le16_to_cpu(cmd_ptr->command) ==
2576 HostCmd_CMD_802_11_SCAN_EXT) {
acebe8c1
ZL
2577 mwifiex_dbg(adapter, INFO,
2578 "Scan pending in command pending list");
3223db20
AP
2579 complete_scan = false;
2580 break;
2581 }
2582 }
2583 }
2584 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, scan_flags);
2585 spin_unlock_irqrestore(&adapter->cmd_pending_q_lock, cmd_flags);
2586
2587 if (complete_scan)
2588 mwifiex_complete_scan(priv);
21445049 2589
21f58d20
AK
2590 return 0;
2591}
2592
2593/* This function This function handles the event extended scan report. It
2594 * parses extended scan results and informs to cfg80211 stack.
2595 */
2596int mwifiex_handle_event_ext_scan_report(struct mwifiex_private *priv,
2597 void *buf)
2598{
2599 int ret = 0;
2600 struct mwifiex_adapter *adapter = priv->adapter;
2601 u8 *bss_info;
2602 u32 bytes_left, bytes_left_for_tlv, idx;
2603 u16 type, len;
2604 struct mwifiex_ie_types_data *tlv;
2605 struct mwifiex_ie_types_bss_scan_rsp *scan_rsp_tlv;
2606 struct mwifiex_ie_types_bss_scan_info *scan_info_tlv;
2607 u8 *radio_type;
2608 u64 fw_tsf = 0;
2609 s32 rssi = 0;
2610 struct mwifiex_event_scan_result *event_scan = buf;
2611 u8 num_of_set = event_scan->num_of_set;
2612 u8 *scan_resp = buf + sizeof(struct mwifiex_event_scan_result);
2613 u16 scan_resp_size = le16_to_cpu(event_scan->buf_size);
2614
2615 if (num_of_set > MWIFIEX_MAX_AP) {
acebe8c1
ZL
2616 mwifiex_dbg(adapter, ERROR,
2617 "EXT_SCAN: Invalid number of AP returned (%d)!!\n",
2618 num_of_set);
21f58d20
AK
2619 ret = -1;
2620 goto check_next_scan;
2621 }
2622
2623 bytes_left = scan_resp_size;
acebe8c1
ZL
2624 mwifiex_dbg(adapter, INFO,
2625 "EXT_SCAN: size %d, returned %d APs...",
2626 scan_resp_size, num_of_set);
868093a9
ZL
2627 mwifiex_dbg_dump(adapter, CMD_D, "EXT_SCAN buffer:", buf,
2628 scan_resp_size +
2629 sizeof(struct mwifiex_event_scan_result));
21f58d20
AK
2630
2631 tlv = (struct mwifiex_ie_types_data *)scan_resp;
2632
2633 for (idx = 0; idx < num_of_set && bytes_left; idx++) {
2634 type = le16_to_cpu(tlv->header.type);
2635 len = le16_to_cpu(tlv->header.len);
2636 if (bytes_left < sizeof(struct mwifiex_ie_types_header) + len) {
acebe8c1
ZL
2637 mwifiex_dbg(adapter, ERROR,
2638 "EXT_SCAN: Error bytes left < TLV length\n");
21f58d20
AK
2639 break;
2640 }
2641 scan_rsp_tlv = NULL;
2642 scan_info_tlv = NULL;
2643 bytes_left_for_tlv = bytes_left;
2644
2645 /* BSS response TLV with beacon or probe response buffer
2646 * at the initial position of each descriptor
2647 */
2648 if (type != TLV_TYPE_BSS_SCAN_RSP)
2649 break;
2650
2651 bss_info = (u8 *)tlv;
2652 scan_rsp_tlv = (struct mwifiex_ie_types_bss_scan_rsp *)tlv;
2653 tlv = (struct mwifiex_ie_types_data *)(tlv->data + len);
2654 bytes_left_for_tlv -=
2655 (len + sizeof(struct mwifiex_ie_types_header));
2656
2657 while (bytes_left_for_tlv >=
2658 sizeof(struct mwifiex_ie_types_header) &&
2659 le16_to_cpu(tlv->header.type) != TLV_TYPE_BSS_SCAN_RSP) {
2660 type = le16_to_cpu(tlv->header.type);
2661 len = le16_to_cpu(tlv->header.len);
2662 if (bytes_left_for_tlv <
2663 sizeof(struct mwifiex_ie_types_header) + len) {
acebe8c1
ZL
2664 mwifiex_dbg(adapter, ERROR,
2665 "EXT_SCAN: Error in processing TLV,\t"
2666 "bytes left < TLV length\n");
21f58d20
AK
2667 scan_rsp_tlv = NULL;
2668 bytes_left_for_tlv = 0;
2669 continue;
2670 }
2671 switch (type) {
2672 case TLV_TYPE_BSS_SCAN_INFO:
2673 scan_info_tlv =
2674 (struct mwifiex_ie_types_bss_scan_info *)tlv;
2675 if (len !=
2676 sizeof(struct mwifiex_ie_types_bss_scan_info) -
2677 sizeof(struct mwifiex_ie_types_header)) {
2678 bytes_left_for_tlv = 0;
2679 continue;
2680 }
2681 break;
2682 default:
2683 break;
2684 }
2685 tlv = (struct mwifiex_ie_types_data *)(tlv->data + len);
2686 bytes_left -=
2687 (len + sizeof(struct mwifiex_ie_types_header));
2688 bytes_left_for_tlv -=
2689 (len + sizeof(struct mwifiex_ie_types_header));
2690 }
2691
2692 if (!scan_rsp_tlv)
2693 break;
2694
2695 /* Advance pointer to the beacon buffer length and
2696 * update the bytes count so that the function
2697 * wlan_interpret_bss_desc_with_ie() can handle the
2698 * scan buffer withut any change
2699 */
2700 bss_info += sizeof(u16);
2701 bytes_left -= sizeof(u16);
2702
2703 if (scan_info_tlv) {
2704 rssi = (s32)(s16)(le16_to_cpu(scan_info_tlv->rssi));
2705 rssi *= 100; /* Convert dBm to mBm */
acebe8c1
ZL
2706 mwifiex_dbg(adapter, INFO,
2707 "info: InterpretIE: RSSI=%d\n", rssi);
21f58d20
AK
2708 fw_tsf = le64_to_cpu(scan_info_tlv->tsf);
2709 radio_type = &scan_info_tlv->radio_type;
2710 } else {
2711 radio_type = NULL;
2712 }
2713 ret = mwifiex_parse_single_response_buf(priv, &bss_info,
2714 &bytes_left, fw_tsf,
2715 radio_type, true, rssi);
2716 if (ret)
2717 goto check_next_scan;
2718 }
2719
2720check_next_scan:
2721 if (!event_scan->more_event)
2722 mwifiex_check_next_scan_command(priv);
2723
2724 return ret;
2725}
2726
5e6e3a92
BZ
2727/*
2728 * This function prepares command for background scan query.
2729 *
2730 * Preparation includes -
2731 * - Setting command ID and proper size
2732 * - Setting background scan flush parameter
2733 * - Ensuring correct endian-ness
2734 */
572e8f3e 2735int mwifiex_cmd_802_11_bg_scan_query(struct host_cmd_ds_command *cmd)
5e6e3a92
BZ
2736{
2737 struct host_cmd_ds_802_11_bg_scan_query *bg_query =
2738 &cmd->params.bg_scan_query;
2739
2740 cmd->command = cpu_to_le16(HostCmd_CMD_802_11_BG_SCAN_QUERY);
2741 cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_bg_scan_query)
2742 + S_DS_GEN);
2743
2744 bg_query->flush = 1;
2745
2746 return 0;
2747}
2748
5e6e3a92
BZ
2749/*
2750 * This function inserts scan command node to the scan pending queue.
2751 */
2752void
2753mwifiex_queue_scan_cmd(struct mwifiex_private *priv,
2754 struct cmd_ctrl_node *cmd_node)
2755{
2756 struct mwifiex_adapter *adapter = priv->adapter;
2757 unsigned long flags;
2758
600f5d90 2759 cmd_node->wait_q_enabled = true;
efaaa8b8 2760 cmd_node->condition = &adapter->scan_wait_q_woken;
5e6e3a92
BZ
2761 spin_lock_irqsave(&adapter->scan_pending_q_lock, flags);
2762 list_add_tail(&cmd_node->list, &adapter->scan_pending_q);
2763 spin_unlock_irqrestore(&adapter->scan_pending_q_lock, flags);
2764}
2765
5e6e3a92
BZ
2766/*
2767 * This function sends a scan command for all available channels to the
2768 * firmware, filtered on a specific SSID.
2769 */
2770static int mwifiex_scan_specific_ssid(struct mwifiex_private *priv,
b9be5f39 2771 struct cfg80211_ssid *req_ssid)
5e6e3a92
BZ
2772{
2773 struct mwifiex_adapter *adapter = priv->adapter;
dcd5c79c 2774 int ret;
5e6e3a92
BZ
2775 struct mwifiex_user_scan_cfg *scan_cfg;
2776
600f5d90 2777 if (adapter->scan_processing) {
acebe8c1
ZL
2778 mwifiex_dbg(adapter, WARN,
2779 "cmd: Scan already in process...\n");
dcd5c79c 2780 return -EBUSY;
5e6e3a92
BZ
2781 }
2782
600f5d90 2783 if (priv->scan_block) {
acebe8c1
ZL
2784 mwifiex_dbg(adapter, WARN,
2785 "cmd: Scan is blocked during association...\n");
dcd5c79c 2786 return -EBUSY;
5e6e3a92
BZ
2787 }
2788
5e6e3a92 2789 scan_cfg = kzalloc(sizeof(struct mwifiex_user_scan_cfg), GFP_KERNEL);
0d2e7a5c 2790 if (!scan_cfg)
b53575ec 2791 return -ENOMEM;
5e6e3a92 2792
c2a8f0ff 2793 ether_addr_copy(scan_cfg->random_mac, priv->random_mac);
be0b281e
AK
2794 scan_cfg->ssid_list = req_ssid;
2795 scan_cfg->num_ssids = 1;
5e6e3a92 2796
600f5d90 2797 ret = mwifiex_scan_networks(priv, scan_cfg);
5e6e3a92
BZ
2798
2799 kfree(scan_cfg);
2800 return ret;
2801}
2802
2803/*
2804 * Sends IOCTL request to start a scan.
2805 *
2806 * This function allocates the IOCTL request buffer, fills it
2807 * with requisite parameters and calls the IOCTL handler.
2808 *
2809 * Scan command can be issued for both normal scan and specific SSID
2810 * scan, depending upon whether an SSID is provided or not.
2811 */
600f5d90 2812int mwifiex_request_scan(struct mwifiex_private *priv,
b9be5f39 2813 struct cfg80211_ssid *req_ssid)
5e6e3a92 2814{
270e58e8 2815 int ret;
5e6e3a92 2816
1abf9ae7 2817 if (mutex_lock_interruptible(&priv->async_mutex)) {
acebe8c1
ZL
2818 mwifiex_dbg(priv->adapter, ERROR,
2819 "%s: acquire semaphore fail\n",
2820 __func__);
5e6e3a92
BZ
2821 return -1;
2822 }
5e6e3a92 2823
efaaa8b8 2824 priv->adapter->scan_wait_q_woken = false;
5e6e3a92
BZ
2825
2826 if (req_ssid && req_ssid->ssid_len != 0)
2827 /* Specific SSID scan */
600f5d90 2828 ret = mwifiex_scan_specific_ssid(priv, req_ssid);
5e6e3a92
BZ
2829 else
2830 /* Normal scan */
600f5d90
AK
2831 ret = mwifiex_scan_networks(priv, NULL);
2832
1abf9ae7 2833 mutex_unlock(&priv->async_mutex);
600f5d90 2834
5e6e3a92
BZ
2835 return ret;
2836}
2837
2838/*
2839 * This function appends the vendor specific IE TLV to a buffer.
2840 */
2841int
2842mwifiex_cmd_append_vsie_tlv(struct mwifiex_private *priv,
2843 u16 vsie_mask, u8 **buffer)
2844{
2845 int id, ret_len = 0;
2846 struct mwifiex_ie_types_vendor_param_set *vs_param_set;
2847
2848 if (!buffer)
2849 return 0;
2850 if (!(*buffer))
2851 return 0;
2852
2853 /*
2854 * Traverse through the saved vendor specific IE array and append
2855 * the selected(scan/assoc/adhoc) IE as TLV to the command
2856 */
2857 for (id = 0; id < MWIFIEX_MAX_VSIE_NUM; id++) {
2858 if (priv->vs_ie[id].mask & vsie_mask) {
2859 vs_param_set =
2860 (struct mwifiex_ie_types_vendor_param_set *)
2861 *buffer;
2862 vs_param_set->header.type =
2863 cpu_to_le16(TLV_TYPE_PASSTHROUGH);
2864 vs_param_set->header.len =
2865 cpu_to_le16((((u16) priv->vs_ie[id].ie[1])
2866 & 0x00FF) + 2);
2867 memcpy(vs_param_set->ie, priv->vs_ie[id].ie,
2868 le16_to_cpu(vs_param_set->header.len));
2869 *buffer += le16_to_cpu(vs_param_set->header.len) +
2870 sizeof(struct mwifiex_ie_types_header);
2871 ret_len += le16_to_cpu(vs_param_set->header.len) +
2872 sizeof(struct mwifiex_ie_types_header);
2873 }
2874 }
2875 return ret_len;
2876}
2877
2878/*
2879 * This function saves a beacon buffer of the current BSS descriptor.
2880 *
2881 * The current beacon buffer is saved so that it can be restored in the
2882 * following cases that makes the beacon buffer not to contain the current
2883 * ssid's beacon buffer.
2884 * - The current ssid was not found somehow in the last scan.
2885 * - The current ssid was the last entry of the scan table and overloaded.
2886 */
2887void
2888mwifiex_save_curr_bcn(struct mwifiex_private *priv)
2889{
2890 struct mwifiex_bssdescriptor *curr_bss =
2891 &priv->curr_bss_params.bss_descriptor;
2892
030fe797
AK
2893 if (!curr_bss->beacon_buf_size)
2894 return;
5e6e3a92 2895
030fe797
AK
2896 /* allocate beacon buffer at 1st time; or if it's size has changed */
2897 if (!priv->curr_bcn_buf ||
cff23cec 2898 priv->curr_bcn_size != curr_bss->beacon_buf_size) {
5e6e3a92 2899 priv->curr_bcn_size = curr_bss->beacon_buf_size;
5e6e3a92 2900
030fe797 2901 kfree(priv->curr_bcn_buf);
5982b47a 2902 priv->curr_bcn_buf = kmalloc(curr_bss->beacon_buf_size,
cff23cec 2903 GFP_ATOMIC);
0d2e7a5c 2904 if (!priv->curr_bcn_buf)
030fe797 2905 return;
5e6e3a92 2906 }
030fe797
AK
2907
2908 memcpy(priv->curr_bcn_buf, curr_bss->beacon_buf,
cff23cec 2909 curr_bss->beacon_buf_size);
acebe8c1
ZL
2910 mwifiex_dbg(priv->adapter, INFO,
2911 "info: current beacon saved %d\n",
2912 priv->curr_bcn_size);
7c6fa2a8
AK
2913
2914 curr_bss->beacon_buf = priv->curr_bcn_buf;
2915
2916 /* adjust the pointers in the current BSS descriptor */
2917 if (curr_bss->bcn_wpa_ie)
2918 curr_bss->bcn_wpa_ie =
2919 (struct ieee_types_vendor_specific *)
2920 (curr_bss->beacon_buf +
2921 curr_bss->wpa_offset);
2922
2923 if (curr_bss->bcn_rsn_ie)
2924 curr_bss->bcn_rsn_ie = (struct ieee_types_generic *)
2925 (curr_bss->beacon_buf +
2926 curr_bss->rsn_offset);
2927
2928 if (curr_bss->bcn_ht_cap)
2929 curr_bss->bcn_ht_cap = (struct ieee80211_ht_cap *)
2930 (curr_bss->beacon_buf +
2931 curr_bss->ht_cap_offset);
2932
074d46d1
JB
2933 if (curr_bss->bcn_ht_oper)
2934 curr_bss->bcn_ht_oper = (struct ieee80211_ht_operation *)
7c6fa2a8
AK
2935 (curr_bss->beacon_buf +
2936 curr_bss->ht_info_offset);
2937
a5f39056 2938 if (curr_bss->bcn_vht_cap)
d5124648
AK
2939 curr_bss->bcn_vht_cap = (void *)(curr_bss->beacon_buf +
2940 curr_bss->vht_cap_offset);
a5f39056
YAP
2941
2942 if (curr_bss->bcn_vht_oper)
d5124648
AK
2943 curr_bss->bcn_vht_oper = (void *)(curr_bss->beacon_buf +
2944 curr_bss->vht_info_offset);
a5f39056 2945
7c6fa2a8
AK
2946 if (curr_bss->bcn_bss_co_2040)
2947 curr_bss->bcn_bss_co_2040 =
2c208890 2948 (curr_bss->beacon_buf + curr_bss->bss_co_2040_offset);
7c6fa2a8
AK
2949
2950 if (curr_bss->bcn_ext_cap)
2c208890
JP
2951 curr_bss->bcn_ext_cap = curr_bss->beacon_buf +
2952 curr_bss->ext_cap_offset;
a5f39056
YAP
2953
2954 if (curr_bss->oper_mode)
2955 curr_bss->oper_mode = (void *)(curr_bss->beacon_buf +
2956 curr_bss->oper_mode_offset);
5e6e3a92
BZ
2957}
2958
2959/*
2960 * This function frees the current BSS descriptor beacon buffer.
2961 */
2962void
2963mwifiex_free_curr_bcn(struct mwifiex_private *priv)
2964{
2965 kfree(priv->curr_bcn_buf);
2966 priv->curr_bcn_buf = NULL;
2967}