OSDN Git Service

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