GNU Linux-libre 4.4.285-gnu1
[releases.git] / drivers / net / wireless / mwifiex / join.c
1 /*
2  * Marvell Wireless LAN device driver: association and ad-hoc start/join
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 "wmm.h"
26 #include "11n.h"
27 #include "11ac.h"
28
29 #define CAPINFO_MASK    (~(BIT(15) | BIT(14) | BIT(12) | BIT(11) | BIT(9)))
30
31 /*
32  * Append a generic IE as a pass through TLV to a TLV buffer.
33  *
34  * This function is called from the network join command preparation routine.
35  *
36  * If the IE buffer has been setup by the application, this routine appends
37  * the buffer as a pass through TLV type to the request.
38  */
39 static int
40 mwifiex_cmd_append_generic_ie(struct mwifiex_private *priv, u8 **buffer)
41 {
42         int ret_len = 0;
43         struct mwifiex_ie_types_header ie_header;
44
45         /* Null Checks */
46         if (!buffer)
47                 return 0;
48         if (!(*buffer))
49                 return 0;
50
51         /*
52          * If there is a generic ie buffer setup, append it to the return
53          *   parameter buffer pointer.
54          */
55         if (priv->gen_ie_buf_len) {
56                 mwifiex_dbg(priv->adapter, INFO,
57                             "info: %s: append generic ie len %d to %p\n",
58                             __func__, priv->gen_ie_buf_len, *buffer);
59
60                 /* Wrap the generic IE buffer with a pass through TLV type */
61                 ie_header.type = cpu_to_le16(TLV_TYPE_PASSTHROUGH);
62                 ie_header.len = cpu_to_le16(priv->gen_ie_buf_len);
63                 memcpy(*buffer, &ie_header, sizeof(ie_header));
64
65                 /* Increment the return size and the return buffer pointer
66                    param */
67                 *buffer += sizeof(ie_header);
68                 ret_len += sizeof(ie_header);
69
70                 /* Copy the generic IE buffer to the output buffer, advance
71                    pointer */
72                 memcpy(*buffer, priv->gen_ie_buf, priv->gen_ie_buf_len);
73
74                 /* Increment the return size and the return buffer pointer
75                    param */
76                 *buffer += priv->gen_ie_buf_len;
77                 ret_len += priv->gen_ie_buf_len;
78
79                 /* Reset the generic IE buffer */
80                 priv->gen_ie_buf_len = 0;
81         }
82
83         /* return the length appended to the buffer */
84         return ret_len;
85 }
86
87 /*
88  * Append TSF tracking info from the scan table for the target AP.
89  *
90  * This function is called from the network join command preparation routine.
91  *
92  * The TSF table TSF sent to the firmware contains two TSF values:
93  *      - The TSF of the target AP from its previous beacon/probe response
94  *      - The TSF timestamp of our local MAC at the time we observed the
95  *        beacon/probe response.
96  *
97  * The firmware uses the timestamp values to set an initial TSF value
98  * in the MAC for the new association after a reassociation attempt.
99  */
100 static int
101 mwifiex_cmd_append_tsf_tlv(struct mwifiex_private *priv, u8 **buffer,
102                            struct mwifiex_bssdescriptor *bss_desc)
103 {
104         struct mwifiex_ie_types_tsf_timestamp tsf_tlv;
105         __le64 tsf_val;
106
107         /* Null Checks */
108         if (buffer == NULL)
109                 return 0;
110         if (*buffer == NULL)
111                 return 0;
112
113         memset(&tsf_tlv, 0x00, sizeof(struct mwifiex_ie_types_tsf_timestamp));
114
115         tsf_tlv.header.type = cpu_to_le16(TLV_TYPE_TSFTIMESTAMP);
116         tsf_tlv.header.len = cpu_to_le16(2 * sizeof(tsf_val));
117
118         memcpy(*buffer, &tsf_tlv, sizeof(tsf_tlv.header));
119         *buffer += sizeof(tsf_tlv.header);
120
121         /* TSF at the time when beacon/probe_response was received */
122         tsf_val = cpu_to_le64(bss_desc->fw_tsf);
123         memcpy(*buffer, &tsf_val, sizeof(tsf_val));
124         *buffer += sizeof(tsf_val);
125
126         tsf_val = cpu_to_le64(bss_desc->timestamp);
127
128         mwifiex_dbg(priv->adapter, INFO,
129                     "info: %s: TSF offset calc: %016llx - %016llx\n",
130                     __func__, bss_desc->timestamp, bss_desc->fw_tsf);
131
132         memcpy(*buffer, &tsf_val, sizeof(tsf_val));
133         *buffer += sizeof(tsf_val);
134
135         return sizeof(tsf_tlv.header) + (2 * sizeof(tsf_val));
136 }
137
138 /*
139  * This function finds out the common rates between rate1 and rate2.
140  *
141  * It will fill common rates in rate1 as output if found.
142  *
143  * NOTE: Setting the MSB of the basic rates needs to be taken
144  * care of, either before or after calling this function.
145  */
146 static int mwifiex_get_common_rates(struct mwifiex_private *priv, u8 *rate1,
147                                     u32 rate1_size, u8 *rate2, u32 rate2_size)
148 {
149         int ret;
150         u8 *ptr = rate1, *tmp;
151         u32 i, j;
152
153         tmp = kmemdup(rate1, rate1_size, GFP_KERNEL);
154         if (!tmp) {
155                 mwifiex_dbg(priv->adapter, ERROR, "failed to alloc tmp buf\n");
156                 return -ENOMEM;
157         }
158
159         memset(rate1, 0, rate1_size);
160
161         for (i = 0; i < rate2_size && rate2[i]; i++) {
162                 for (j = 0; j < rate1_size && tmp[j]; j++) {
163                         /* Check common rate, excluding the bit for
164                            basic rate */
165                         if ((rate2[i] & 0x7F) == (tmp[j] & 0x7F)) {
166                                 *rate1++ = tmp[j];
167                                 break;
168                         }
169                 }
170         }
171
172         mwifiex_dbg(priv->adapter, INFO, "info: Tx data rate set to %#x\n",
173                     priv->data_rate);
174
175         if (!priv->is_data_rate_auto) {
176                 while (*ptr) {
177                         if ((*ptr & 0x7f) == priv->data_rate) {
178                                 ret = 0;
179                                 goto done;
180                         }
181                         ptr++;
182                 }
183                 mwifiex_dbg(priv->adapter, ERROR,
184                             "previously set fixed data rate %#x\t"
185                             "is not compatible with the network\n",
186                             priv->data_rate);
187
188                 ret = -1;
189                 goto done;
190         }
191
192         ret = 0;
193 done:
194         kfree(tmp);
195         return ret;
196 }
197
198 /*
199  * This function creates the intersection of the rates supported by a
200  * target BSS and our adapter settings for use in an assoc/join command.
201  */
202 static int
203 mwifiex_setup_rates_from_bssdesc(struct mwifiex_private *priv,
204                                  struct mwifiex_bssdescriptor *bss_desc,
205                                  u8 *out_rates, u32 *out_rates_size)
206 {
207         u8 card_rates[MWIFIEX_SUPPORTED_RATES];
208         u32 card_rates_size;
209
210         /* Copy AP supported rates */
211         memcpy(out_rates, bss_desc->supported_rates, MWIFIEX_SUPPORTED_RATES);
212         /* Get the STA supported rates */
213         card_rates_size = mwifiex_get_active_data_rates(priv, card_rates);
214         /* Get the common rates between AP and STA supported rates */
215         if (mwifiex_get_common_rates(priv, out_rates, MWIFIEX_SUPPORTED_RATES,
216                                      card_rates, card_rates_size)) {
217                 *out_rates_size = 0;
218                 mwifiex_dbg(priv->adapter, ERROR,
219                             "%s: cannot get common rates\n",
220                             __func__);
221                 return -1;
222         }
223
224         *out_rates_size =
225                 min_t(size_t, strlen(out_rates), MWIFIEX_SUPPORTED_RATES);
226
227         return 0;
228 }
229
230 /*
231  * This function appends a WPS IE. It is called from the network join command
232  * preparation routine.
233  *
234  * If the IE buffer has been setup by the application, this routine appends
235  * the buffer as a WPS TLV type to the request.
236  */
237 static int
238 mwifiex_cmd_append_wps_ie(struct mwifiex_private *priv, u8 **buffer)
239 {
240         int retLen = 0;
241         struct mwifiex_ie_types_header ie_header;
242
243         if (!buffer || !*buffer)
244                 return 0;
245
246         /*
247          * If there is a wps ie buffer setup, append it to the return
248          * parameter buffer pointer.
249          */
250         if (priv->wps_ie_len) {
251                 mwifiex_dbg(priv->adapter, CMD,
252                             "cmd: append wps ie %d to %p\n",
253                             priv->wps_ie_len, *buffer);
254
255                 /* Wrap the generic IE buffer with a pass through TLV type */
256                 ie_header.type = cpu_to_le16(TLV_TYPE_MGMT_IE);
257                 ie_header.len = cpu_to_le16(priv->wps_ie_len);
258                 memcpy(*buffer, &ie_header, sizeof(ie_header));
259                 *buffer += sizeof(ie_header);
260                 retLen += sizeof(ie_header);
261
262                 memcpy(*buffer, priv->wps_ie, priv->wps_ie_len);
263                 *buffer += priv->wps_ie_len;
264                 retLen += priv->wps_ie_len;
265
266         }
267
268         kfree(priv->wps_ie);
269         priv->wps_ie_len = 0;
270         return retLen;
271 }
272
273 /*
274  * This function appends a WAPI IE.
275  *
276  * This function is called from the network join command preparation routine.
277  *
278  * If the IE buffer has been setup by the application, this routine appends
279  * the buffer as a WAPI TLV type to the request.
280  */
281 static int
282 mwifiex_cmd_append_wapi_ie(struct mwifiex_private *priv, u8 **buffer)
283 {
284         int retLen = 0;
285         struct mwifiex_ie_types_header ie_header;
286
287         /* Null Checks */
288         if (buffer == NULL)
289                 return 0;
290         if (*buffer == NULL)
291                 return 0;
292
293         /*
294          * If there is a wapi ie buffer setup, append it to the return
295          *   parameter buffer pointer.
296          */
297         if (priv->wapi_ie_len) {
298                 mwifiex_dbg(priv->adapter, CMD,
299                             "cmd: append wapi ie %d to %p\n",
300                             priv->wapi_ie_len, *buffer);
301
302                 /* Wrap the generic IE buffer with a pass through TLV type */
303                 ie_header.type = cpu_to_le16(TLV_TYPE_WAPI_IE);
304                 ie_header.len = cpu_to_le16(priv->wapi_ie_len);
305                 memcpy(*buffer, &ie_header, sizeof(ie_header));
306
307                 /* Increment the return size and the return buffer pointer
308                    param */
309                 *buffer += sizeof(ie_header);
310                 retLen += sizeof(ie_header);
311
312                 /* Copy the wapi IE buffer to the output buffer, advance
313                    pointer */
314                 memcpy(*buffer, priv->wapi_ie, priv->wapi_ie_len);
315
316                 /* Increment the return size and the return buffer pointer
317                    param */
318                 *buffer += priv->wapi_ie_len;
319                 retLen += priv->wapi_ie_len;
320
321         }
322         /* return the length appended to the buffer */
323         return retLen;
324 }
325
326 /*
327  * This function appends rsn ie tlv for wpa/wpa2 security modes.
328  * It is called from the network join command preparation routine.
329  */
330 static int mwifiex_append_rsn_ie_wpa_wpa2(struct mwifiex_private *priv,
331                                           u8 **buffer)
332 {
333         struct mwifiex_ie_types_rsn_param_set *rsn_ie_tlv;
334         int rsn_ie_len;
335
336         if (!buffer || !(*buffer))
337                 return 0;
338
339         rsn_ie_tlv = (struct mwifiex_ie_types_rsn_param_set *) (*buffer);
340         rsn_ie_tlv->header.type = cpu_to_le16((u16) priv->wpa_ie[0]);
341         rsn_ie_tlv->header.type = cpu_to_le16(
342                                  le16_to_cpu(rsn_ie_tlv->header.type) & 0x00FF);
343         rsn_ie_tlv->header.len = cpu_to_le16((u16) priv->wpa_ie[1]);
344         rsn_ie_tlv->header.len = cpu_to_le16(le16_to_cpu(rsn_ie_tlv->header.len)
345                                                          & 0x00FF);
346         if (le16_to_cpu(rsn_ie_tlv->header.len) <= (sizeof(priv->wpa_ie) - 2))
347                 memcpy(rsn_ie_tlv->rsn_ie, &priv->wpa_ie[2],
348                        le16_to_cpu(rsn_ie_tlv->header.len));
349         else
350                 return -1;
351
352         rsn_ie_len = sizeof(rsn_ie_tlv->header) +
353                                         le16_to_cpu(rsn_ie_tlv->header.len);
354         *buffer += rsn_ie_len;
355
356         return rsn_ie_len;
357 }
358
359 /*
360  * This function prepares command for association.
361  *
362  * This sets the following parameters -
363  *      - Peer MAC address
364  *      - Listen interval
365  *      - Beacon interval
366  *      - Capability information
367  *
368  * ...and the following TLVs, as required -
369  *      - SSID TLV
370  *      - PHY TLV
371  *      - SS TLV
372  *      - Rates TLV
373  *      - Authentication TLV
374  *      - Channel TLV
375  *      - WPA/WPA2 IE
376  *      - 11n TLV
377  *      - Vendor specific TLV
378  *      - WMM TLV
379  *      - WAPI IE
380  *      - Generic IE
381  *      - TSF TLV
382  *
383  * Preparation also includes -
384  *      - Setting command ID and proper size
385  *      - Ensuring correct endian-ness
386  */
387 int mwifiex_cmd_802_11_associate(struct mwifiex_private *priv,
388                                  struct host_cmd_ds_command *cmd,
389                                  struct mwifiex_bssdescriptor *bss_desc)
390 {
391         struct host_cmd_ds_802_11_associate *assoc = &cmd->params.associate;
392         struct mwifiex_ie_types_ssid_param_set *ssid_tlv;
393         struct mwifiex_ie_types_phy_param_set *phy_tlv;
394         struct mwifiex_ie_types_ss_param_set *ss_tlv;
395         struct mwifiex_ie_types_rates_param_set *rates_tlv;
396         struct mwifiex_ie_types_auth_type *auth_tlv;
397         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
398         u8 rates[MWIFIEX_SUPPORTED_RATES];
399         u32 rates_size;
400         u16 tmp_cap;
401         u8 *pos;
402         int rsn_ie_len = 0;
403
404         pos = (u8 *) assoc;
405
406         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_ASSOCIATE);
407
408         /* Save so we know which BSS Desc to use in the response handler */
409         priv->attempted_bss_desc = bss_desc;
410
411         memcpy(assoc->peer_sta_addr,
412                bss_desc->mac_address, sizeof(assoc->peer_sta_addr));
413         pos += sizeof(assoc->peer_sta_addr);
414
415         /* Set the listen interval */
416         assoc->listen_interval = cpu_to_le16(priv->listen_interval);
417         /* Set the beacon period */
418         assoc->beacon_period = cpu_to_le16(bss_desc->beacon_period);
419
420         pos += sizeof(assoc->cap_info_bitmap);
421         pos += sizeof(assoc->listen_interval);
422         pos += sizeof(assoc->beacon_period);
423         pos += sizeof(assoc->dtim_period);
424
425         ssid_tlv = (struct mwifiex_ie_types_ssid_param_set *) pos;
426         ssid_tlv->header.type = cpu_to_le16(WLAN_EID_SSID);
427         ssid_tlv->header.len = cpu_to_le16((u16) bss_desc->ssid.ssid_len);
428         memcpy(ssid_tlv->ssid, bss_desc->ssid.ssid,
429                le16_to_cpu(ssid_tlv->header.len));
430         pos += sizeof(ssid_tlv->header) + le16_to_cpu(ssid_tlv->header.len);
431
432         phy_tlv = (struct mwifiex_ie_types_phy_param_set *) pos;
433         phy_tlv->header.type = cpu_to_le16(WLAN_EID_DS_PARAMS);
434         phy_tlv->header.len = cpu_to_le16(sizeof(phy_tlv->fh_ds.ds_param_set));
435         memcpy(&phy_tlv->fh_ds.ds_param_set,
436                &bss_desc->phy_param_set.ds_param_set.current_chan,
437                sizeof(phy_tlv->fh_ds.ds_param_set));
438         pos += sizeof(phy_tlv->header) + le16_to_cpu(phy_tlv->header.len);
439
440         ss_tlv = (struct mwifiex_ie_types_ss_param_set *) pos;
441         ss_tlv->header.type = cpu_to_le16(WLAN_EID_CF_PARAMS);
442         ss_tlv->header.len = cpu_to_le16(sizeof(ss_tlv->cf_ibss.cf_param_set));
443         pos += sizeof(ss_tlv->header) + le16_to_cpu(ss_tlv->header.len);
444
445         /* Get the common rates supported between the driver and the BSS Desc */
446         if (mwifiex_setup_rates_from_bssdesc
447             (priv, bss_desc, rates, &rates_size))
448                 return -1;
449
450         /* Save the data rates into Current BSS state structure */
451         priv->curr_bss_params.num_of_rates = rates_size;
452         memcpy(&priv->curr_bss_params.data_rates, rates, rates_size);
453
454         /* Setup the Rates TLV in the association command */
455         rates_tlv = (struct mwifiex_ie_types_rates_param_set *) pos;
456         rates_tlv->header.type = cpu_to_le16(WLAN_EID_SUPP_RATES);
457         rates_tlv->header.len = cpu_to_le16((u16) rates_size);
458         memcpy(rates_tlv->rates, rates, rates_size);
459         pos += sizeof(rates_tlv->header) + rates_size;
460         mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_CMD: rates size = %d\n",
461                     rates_size);
462
463         /* Add the Authentication type to be used for Auth frames */
464         auth_tlv = (struct mwifiex_ie_types_auth_type *) pos;
465         auth_tlv->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
466         auth_tlv->header.len = cpu_to_le16(sizeof(auth_tlv->auth_type));
467         if (priv->sec_info.wep_enabled)
468                 auth_tlv->auth_type = cpu_to_le16(
469                                 (u16) priv->sec_info.authentication_mode);
470         else
471                 auth_tlv->auth_type = cpu_to_le16(NL80211_AUTHTYPE_OPEN_SYSTEM);
472
473         pos += sizeof(auth_tlv->header) + le16_to_cpu(auth_tlv->header.len);
474
475         if (IS_SUPPORT_MULTI_BANDS(priv->adapter) &&
476             !(ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
477             (!bss_desc->disable_11n) &&
478             (priv->adapter->config_bands & BAND_GN ||
479              priv->adapter->config_bands & BAND_AN) &&
480             (bss_desc->bcn_ht_cap)
481             )
482                 ) {
483                 /* Append a channel TLV for the channel the attempted AP was
484                    found on */
485                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
486                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
487                 chan_tlv->header.len =
488                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
489
490                 memset(chan_tlv->chan_scan_param, 0x00,
491                        sizeof(struct mwifiex_chan_scan_param_set));
492                 chan_tlv->chan_scan_param[0].chan_number =
493                         (bss_desc->phy_param_set.ds_param_set.current_chan);
494                 mwifiex_dbg(priv->adapter, INFO, "info: Assoc: TLV Chan = %d\n",
495                             chan_tlv->chan_scan_param[0].chan_number);
496
497                 chan_tlv->chan_scan_param[0].radio_type =
498                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
499
500                 mwifiex_dbg(priv->adapter, INFO, "info: Assoc: TLV Band = %d\n",
501                             chan_tlv->chan_scan_param[0].radio_type);
502                 pos += sizeof(chan_tlv->header) +
503                         sizeof(struct mwifiex_chan_scan_param_set);
504         }
505
506         if (!priv->wps.session_enable) {
507                 if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
508                         rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
509
510                 if (rsn_ie_len == -1)
511                         return -1;
512         }
513
514         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info) &&
515             (!bss_desc->disable_11n) &&
516             (priv->adapter->config_bands & BAND_GN ||
517              priv->adapter->config_bands & BAND_AN))
518                 mwifiex_cmd_append_11n_tlv(priv, bss_desc, &pos);
519
520         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
521             !bss_desc->disable_11n && !bss_desc->disable_11ac &&
522             priv->adapter->config_bands & BAND_AAC)
523                 mwifiex_cmd_append_11ac_tlv(priv, bss_desc, &pos);
524
525         /* Append vendor specific IE TLV */
526         mwifiex_cmd_append_vsie_tlv(priv, MWIFIEX_VSIE_MASK_ASSOC, &pos);
527
528         mwifiex_wmm_process_association_req(priv, &pos, &bss_desc->wmm_ie,
529                                             bss_desc->bcn_ht_cap);
530         if (priv->sec_info.wapi_enabled && priv->wapi_ie_len)
531                 mwifiex_cmd_append_wapi_ie(priv, &pos);
532
533         if (priv->wps.session_enable && priv->wps_ie_len)
534                 mwifiex_cmd_append_wps_ie(priv, &pos);
535
536         mwifiex_cmd_append_generic_ie(priv, &pos);
537
538         mwifiex_cmd_append_tsf_tlv(priv, &pos, bss_desc);
539
540         mwifiex_11h_process_join(priv, &pos, bss_desc);
541
542         cmd->size = cpu_to_le16((u16) (pos - (u8 *) assoc) + S_DS_GEN);
543
544         /* Set the Capability info at last */
545         tmp_cap = bss_desc->cap_info_bitmap;
546
547         if (priv->adapter->config_bands == BAND_B)
548                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
549
550         tmp_cap &= CAPINFO_MASK;
551         mwifiex_dbg(priv->adapter, INFO,
552                     "info: ASSOC_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
553                     tmp_cap, CAPINFO_MASK);
554         assoc->cap_info_bitmap = cpu_to_le16(tmp_cap);
555
556         return 0;
557 }
558
559 static const char *assoc_failure_reason_to_str(u16 cap_info)
560 {
561         switch (cap_info) {
562         case CONNECT_ERR_AUTH_ERR_STA_FAILURE:
563                 return "CONNECT_ERR_AUTH_ERR_STA_FAILURE";
564         case CONNECT_ERR_AUTH_MSG_UNHANDLED:
565                 return "CONNECT_ERR_AUTH_MSG_UNHANDLED";
566         case CONNECT_ERR_ASSOC_ERR_TIMEOUT:
567                 return "CONNECT_ERR_ASSOC_ERR_TIMEOUT";
568         case CONNECT_ERR_ASSOC_ERR_AUTH_REFUSED:
569                 return "CONNECT_ERR_ASSOC_ERR_AUTH_REFUSED";
570         case CONNECT_ERR_STA_FAILURE:
571                 return "CONNECT_ERR_STA_FAILURE";
572         }
573
574         return "Unknown connect failure";
575 }
576 /*
577  * Association firmware command response handler
578  *
579  * The response buffer for the association command has the following
580  * memory layout.
581  *
582  * For cases where an association response was not received (indicated
583  * by the CapInfo and AId field):
584  *
585  *     .------------------------------------------------------------.
586  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
587  *     .------------------------------------------------------------.
588  *     |  cap_info/Error Return(t_u16):                             |
589  *     |           0xFFFF(-1): Internal error                       |
590  *     |           0xFFFE(-2): Authentication unhandled message     |
591  *     |           0xFFFD(-3): Authentication refused               |
592  *     |           0xFFFC(-4): Timeout waiting for AP response      |
593  *     .------------------------------------------------------------.
594  *     |  status_code(t_u16):                                       |
595  *     |        If cap_info is -1:                                  |
596  *     |           An internal firmware failure prevented the       |
597  *     |           command from being processed.  The status_code   |
598  *     |           will be set to 1.                                |
599  *     |                                                            |
600  *     |        If cap_info is -2:                                  |
601  *     |           An authentication frame was received but was     |
602  *     |           not handled by the firmware.  IEEE Status        |
603  *     |           code for the failure is returned.                |
604  *     |                                                            |
605  *     |        If cap_info is -3:                                  |
606  *     |           An authentication frame was received and the     |
607  *     |           status_code is the IEEE Status reported in the   |
608  *     |           response.                                        |
609  *     |                                                            |
610  *     |        If cap_info is -4:                                  |
611  *     |           (1) Association response timeout                 |
612  *     |           (2) Authentication response timeout              |
613  *     .------------------------------------------------------------.
614  *     |  a_id(t_u16): 0xFFFF                                       |
615  *     .------------------------------------------------------------.
616  *
617  *
618  * For cases where an association response was received, the IEEE
619  * standard association response frame is returned:
620  *
621  *     .------------------------------------------------------------.
622  *     |  Header(4 * sizeof(t_u16)):  Standard command response hdr |
623  *     .------------------------------------------------------------.
624  *     |  cap_info(t_u16): IEEE Capability                          |
625  *     .------------------------------------------------------------.
626  *     |  status_code(t_u16): IEEE Status Code                      |
627  *     .------------------------------------------------------------.
628  *     |  a_id(t_u16): IEEE Association ID                          |
629  *     .------------------------------------------------------------.
630  *     |  IEEE IEs(variable): Any received IEs comprising the       |
631  *     |                      remaining portion of a received       |
632  *     |                      association response frame.           |
633  *     .------------------------------------------------------------.
634  *
635  * For simplistic handling, the status_code field can be used to determine
636  * an association success (0) or failure (non-zero).
637  */
638 int mwifiex_ret_802_11_associate(struct mwifiex_private *priv,
639                              struct host_cmd_ds_command *resp)
640 {
641         struct mwifiex_adapter *adapter = priv->adapter;
642         int ret = 0;
643         struct ieee_types_assoc_rsp *assoc_rsp;
644         struct mwifiex_bssdescriptor *bss_desc;
645         bool enable_data = true;
646         u16 cap_info, status_code, aid;
647
648         assoc_rsp = (struct ieee_types_assoc_rsp *) &resp->params;
649
650         cap_info = le16_to_cpu(assoc_rsp->cap_info_bitmap);
651         status_code = le16_to_cpu(assoc_rsp->status_code);
652         aid = le16_to_cpu(assoc_rsp->a_id);
653
654         if ((aid & (BIT(15) | BIT(14))) != (BIT(15) | BIT(14)))
655                 dev_err(priv->adapter->dev,
656                         "invalid AID value 0x%x; bits 15:14 not set\n",
657                         aid);
658
659         aid &= ~(BIT(15) | BIT(14));
660
661         priv->assoc_rsp_size = min(le16_to_cpu(resp->size) - S_DS_GEN,
662                                    sizeof(priv->assoc_rsp_buf));
663
664         assoc_rsp->a_id = cpu_to_le16(aid);
665         memcpy(priv->assoc_rsp_buf, &resp->params, priv->assoc_rsp_size);
666
667         if (status_code) {
668                 priv->adapter->dbg.num_cmd_assoc_failure++;
669                 mwifiex_dbg(priv->adapter, ERROR,
670                             "ASSOC_RESP: failed,\t"
671                             "status code=%d err=%#x a_id=%#x\n",
672                             status_code, cap_info,
673                             le16_to_cpu(assoc_rsp->a_id));
674
675                 mwifiex_dbg(priv->adapter, ERROR, "assoc failure: reason %s\n",
676                             assoc_failure_reason_to_str(cap_info));
677                 if (cap_info == CONNECT_ERR_ASSOC_ERR_TIMEOUT) {
678                         if (status_code == MWIFIEX_ASSOC_CMD_FAILURE_AUTH) {
679                                 ret = WLAN_STATUS_AUTH_TIMEOUT;
680                                 mwifiex_dbg(priv->adapter, ERROR,
681                                             "ASSOC_RESP: AUTH timeout\n");
682                         } else {
683                                 ret = WLAN_STATUS_UNSPECIFIED_FAILURE;
684                                 mwifiex_dbg(priv->adapter, ERROR,
685                                             "ASSOC_RESP: UNSPECIFIED failure\n");
686                         }
687                 } else {
688                         ret = status_code;
689                 }
690
691                 goto done;
692         }
693
694         /* Send a Media Connected event, according to the Spec */
695         priv->media_connected = true;
696
697         priv->adapter->ps_state = PS_STATE_AWAKE;
698         priv->adapter->pps_uapsd_mode = false;
699         priv->adapter->tx_lock_flag = false;
700
701         /* Set the attempted BSSID Index to current */
702         bss_desc = priv->attempted_bss_desc;
703
704         mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_RESP: %s\n",
705                     bss_desc->ssid.ssid);
706
707         /* Make a copy of current BSSID descriptor */
708         memcpy(&priv->curr_bss_params.bss_descriptor,
709                bss_desc, sizeof(struct mwifiex_bssdescriptor));
710
711         /* Update curr_bss_params */
712         priv->curr_bss_params.bss_descriptor.channel
713                 = bss_desc->phy_param_set.ds_param_set.current_chan;
714
715         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
716
717         if (bss_desc->wmm_ie.vend_hdr.element_id == WLAN_EID_VENDOR_SPECIFIC)
718                 priv->curr_bss_params.wmm_enabled = true;
719         else
720                 priv->curr_bss_params.wmm_enabled = false;
721
722         if ((priv->wmm_required || bss_desc->bcn_ht_cap) &&
723             priv->curr_bss_params.wmm_enabled)
724                 priv->wmm_enabled = true;
725         else
726                 priv->wmm_enabled = false;
727
728         priv->curr_bss_params.wmm_uapsd_enabled = false;
729
730         if (priv->wmm_enabled)
731                 priv->curr_bss_params.wmm_uapsd_enabled
732                         = ((bss_desc->wmm_ie.qos_info_bitmap &
733                                 IEEE80211_WMM_IE_AP_QOSINFO_UAPSD) ? 1 : 0);
734
735         mwifiex_dbg(priv->adapter, INFO,
736                     "info: ASSOC_RESP: curr_pkt_filter is %#x\n",
737                     priv->curr_pkt_filter);
738         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
739                 priv->wpa_is_gtk_set = false;
740
741         if (priv->wmm_enabled) {
742                 /* Don't re-enable carrier until we get the WMM_GET_STATUS
743                    event */
744                 enable_data = false;
745         } else {
746                 /* Since WMM is not enabled, setup the queues with the
747                    defaults */
748                 mwifiex_wmm_setup_queue_priorities(priv, NULL);
749                 mwifiex_wmm_setup_ac_downgrade(priv);
750         }
751
752         if (enable_data)
753                 mwifiex_dbg(priv->adapter, INFO,
754                             "info: post association, re-enabling data flow\n");
755
756         /* Reset SNR/NF/RSSI values */
757         priv->data_rssi_last = 0;
758         priv->data_nf_last = 0;
759         priv->data_rssi_avg = 0;
760         priv->data_nf_avg = 0;
761         priv->bcn_rssi_last = 0;
762         priv->bcn_nf_last = 0;
763         priv->bcn_rssi_avg = 0;
764         priv->bcn_nf_avg = 0;
765         priv->rxpd_rate = 0;
766         priv->rxpd_htinfo = 0;
767
768         mwifiex_save_curr_bcn(priv);
769
770         priv->adapter->dbg.num_cmd_assoc_success++;
771
772         mwifiex_dbg(priv->adapter, INFO, "info: ASSOC_RESP: associated\n");
773
774         /* Add the ra_list here for infra mode as there will be only 1 ra
775            always */
776         mwifiex_ralist_add(priv,
777                            priv->curr_bss_params.bss_descriptor.mac_address);
778
779         if (!netif_carrier_ok(priv->netdev))
780                 netif_carrier_on(priv->netdev);
781         mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
782
783         if (priv->sec_info.wpa_enabled || priv->sec_info.wpa2_enabled)
784                 priv->scan_block = true;
785         else
786                 priv->port_open = true;
787
788 done:
789         /* Need to indicate IOCTL complete */
790         if (adapter->curr_cmd->wait_q_enabled) {
791                 if (ret)
792                         adapter->cmd_wait_q.status = -1;
793                 else
794                         adapter->cmd_wait_q.status = 0;
795         }
796
797         return ret;
798 }
799
800 /*
801  * This function prepares command for ad-hoc start.
802  *
803  * Driver will fill up SSID, BSS mode, IBSS parameters, physical
804  * parameters, probe delay, and capability information. Firmware
805  * will fill up beacon period, basic rates and operational rates.
806  *
807  * In addition, the following TLVs are added -
808  *      - Channel TLV
809  *      - Vendor specific IE
810  *      - WPA/WPA2 IE
811  *      - HT Capabilities IE
812  *      - HT Information IE
813  *
814  * Preparation also includes -
815  *      - Setting command ID and proper size
816  *      - Ensuring correct endian-ness
817  */
818 int
819 mwifiex_cmd_802_11_ad_hoc_start(struct mwifiex_private *priv,
820                                 struct host_cmd_ds_command *cmd,
821                                 struct cfg80211_ssid *req_ssid)
822 {
823         int rsn_ie_len = 0;
824         struct mwifiex_adapter *adapter = priv->adapter;
825         struct host_cmd_ds_802_11_ad_hoc_start *adhoc_start =
826                 &cmd->params.adhoc_start;
827         struct mwifiex_bssdescriptor *bss_desc;
828         u32 cmd_append_size = 0;
829         u32 i;
830         u16 tmp_cap;
831         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
832         u8 radio_type;
833
834         struct mwifiex_ie_types_htcap *ht_cap;
835         struct mwifiex_ie_types_htinfo *ht_info;
836         u8 *pos = (u8 *) adhoc_start +
837                         sizeof(struct host_cmd_ds_802_11_ad_hoc_start);
838
839         if (!adapter)
840                 return -1;
841
842         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_START);
843
844         bss_desc = &priv->curr_bss_params.bss_descriptor;
845         priv->attempted_bss_desc = bss_desc;
846
847         /*
848          * Fill in the parameters for 2 data structures:
849          *   1. struct host_cmd_ds_802_11_ad_hoc_start command
850          *   2. bss_desc
851          * Driver will fill up SSID, bss_mode,IBSS param, Physical Param,
852          * probe delay, and Cap info.
853          * Firmware will fill up beacon period, Basic rates
854          * and operational rates.
855          */
856
857         memset(adhoc_start->ssid, 0, IEEE80211_MAX_SSID_LEN);
858
859         if (req_ssid->ssid_len > IEEE80211_MAX_SSID_LEN)
860                 req_ssid->ssid_len = IEEE80211_MAX_SSID_LEN;
861         memcpy(adhoc_start->ssid, req_ssid->ssid, req_ssid->ssid_len);
862
863         mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: SSID = %s\n",
864                     adhoc_start->ssid);
865
866         memset(bss_desc->ssid.ssid, 0, IEEE80211_MAX_SSID_LEN);
867         memcpy(bss_desc->ssid.ssid, req_ssid->ssid, req_ssid->ssid_len);
868
869         bss_desc->ssid.ssid_len = req_ssid->ssid_len;
870
871         /* Set the BSS mode */
872         adhoc_start->bss_mode = HostCmd_BSS_MODE_IBSS;
873         bss_desc->bss_mode = NL80211_IFTYPE_ADHOC;
874         adhoc_start->beacon_period = cpu_to_le16(priv->beacon_period);
875         bss_desc->beacon_period = priv->beacon_period;
876
877         /* Set Physical param set */
878 /* Parameter IE Id */
879 #define DS_PARA_IE_ID   3
880 /* Parameter IE length */
881 #define DS_PARA_IE_LEN  1
882
883         adhoc_start->phy_param_set.ds_param_set.element_id = DS_PARA_IE_ID;
884         adhoc_start->phy_param_set.ds_param_set.len = DS_PARA_IE_LEN;
885
886         if (!mwifiex_get_cfp(priv, adapter->adhoc_start_band,
887                              (u16) priv->adhoc_channel, 0)) {
888                 struct mwifiex_chan_freq_power *cfp;
889                 cfp = mwifiex_get_cfp(priv, adapter->adhoc_start_band,
890                                       FIRST_VALID_CHANNEL, 0);
891                 if (cfp)
892                         priv->adhoc_channel = (u8) cfp->channel;
893         }
894
895         if (!priv->adhoc_channel) {
896                 mwifiex_dbg(adapter, ERROR,
897                             "ADHOC_S_CMD: adhoc_channel cannot be 0\n");
898                 return -1;
899         }
900
901         mwifiex_dbg(adapter, INFO,
902                     "info: ADHOC_S_CMD: creating ADHOC on channel %d\n",
903                     priv->adhoc_channel);
904
905         priv->curr_bss_params.bss_descriptor.channel = priv->adhoc_channel;
906         priv->curr_bss_params.band = adapter->adhoc_start_band;
907
908         bss_desc->channel = priv->adhoc_channel;
909         adhoc_start->phy_param_set.ds_param_set.current_chan =
910                 priv->adhoc_channel;
911
912         memcpy(&bss_desc->phy_param_set, &adhoc_start->phy_param_set,
913                sizeof(union ieee_types_phy_param_set));
914
915         /* Set IBSS param set */
916 /* IBSS parameter IE Id */
917 #define IBSS_PARA_IE_ID   6
918 /* IBSS parameter IE length */
919 #define IBSS_PARA_IE_LEN  2
920
921         adhoc_start->ss_param_set.ibss_param_set.element_id = IBSS_PARA_IE_ID;
922         adhoc_start->ss_param_set.ibss_param_set.len = IBSS_PARA_IE_LEN;
923         adhoc_start->ss_param_set.ibss_param_set.atim_window
924                                         = cpu_to_le16(priv->atim_window);
925         memcpy(&bss_desc->ss_param_set, &adhoc_start->ss_param_set,
926                sizeof(union ieee_types_ss_param_set));
927
928         /* Set Capability info */
929         bss_desc->cap_info_bitmap |= WLAN_CAPABILITY_IBSS;
930         tmp_cap = WLAN_CAPABILITY_IBSS;
931
932         /* Set up privacy in bss_desc */
933         if (priv->sec_info.encryption_mode) {
934                 /* Ad-Hoc capability privacy on */
935                 mwifiex_dbg(adapter, INFO,
936                             "info: ADHOC_S_CMD: wep_status set privacy to WEP\n");
937                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_8021X_WEP;
938                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
939         } else {
940                 mwifiex_dbg(adapter, INFO,
941                             "info: ADHOC_S_CMD: wep_status NOT set,\t"
942                             "setting privacy to ACCEPT ALL\n");
943                 bss_desc->privacy = MWIFIEX_802_11_PRIV_FILTER_ACCEPT_ALL;
944         }
945
946         memset(adhoc_start->data_rate, 0, sizeof(adhoc_start->data_rate));
947         mwifiex_get_active_data_rates(priv, adhoc_start->data_rate);
948         if ((adapter->adhoc_start_band & BAND_G) &&
949             (priv->curr_pkt_filter & HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON)) {
950                 if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
951                                      HostCmd_ACT_GEN_SET, 0,
952                                      &priv->curr_pkt_filter, false)) {
953                         mwifiex_dbg(adapter, ERROR,
954                                     "ADHOC_S_CMD: G Protection config failed\n");
955                         return -1;
956                 }
957         }
958         /* Find the last non zero */
959         for (i = 0; i < sizeof(adhoc_start->data_rate); i++)
960                 if (!adhoc_start->data_rate[i])
961                         break;
962
963         priv->curr_bss_params.num_of_rates = i;
964
965         /* Copy the ad-hoc creating rates into Current BSS rate structure */
966         memcpy(&priv->curr_bss_params.data_rates,
967                &adhoc_start->data_rate, priv->curr_bss_params.num_of_rates);
968
969         mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: rates=%4ph\n",
970                     adhoc_start->data_rate);
971
972         mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: AD-HOC Start command is ready\n");
973
974         if (IS_SUPPORT_MULTI_BANDS(adapter)) {
975                 /* Append a channel TLV */
976                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
977                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
978                 chan_tlv->header.len =
979                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
980
981                 memset(chan_tlv->chan_scan_param, 0x00,
982                        sizeof(struct mwifiex_chan_scan_param_set));
983                 chan_tlv->chan_scan_param[0].chan_number =
984                         (u8) priv->curr_bss_params.bss_descriptor.channel;
985
986                 mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Chan = %d\n",
987                             chan_tlv->chan_scan_param[0].chan_number);
988
989                 chan_tlv->chan_scan_param[0].radio_type
990                        = mwifiex_band_to_radio_type(priv->curr_bss_params.band);
991                 if (adapter->adhoc_start_band & BAND_GN ||
992                     adapter->adhoc_start_band & BAND_AN) {
993                         if (adapter->sec_chan_offset ==
994                                             IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
995                                 chan_tlv->chan_scan_param[0].radio_type |=
996                                         (IEEE80211_HT_PARAM_CHA_SEC_ABOVE << 4);
997                         else if (adapter->sec_chan_offset ==
998                                             IEEE80211_HT_PARAM_CHA_SEC_BELOW)
999                                 chan_tlv->chan_scan_param[0].radio_type |=
1000                                         (IEEE80211_HT_PARAM_CHA_SEC_BELOW << 4);
1001                 }
1002                 mwifiex_dbg(adapter, INFO, "info: ADHOC_S_CMD: TLV Band = %d\n",
1003                             chan_tlv->chan_scan_param[0].radio_type);
1004                 pos += sizeof(chan_tlv->header) +
1005                         sizeof(struct mwifiex_chan_scan_param_set);
1006                 cmd_append_size +=
1007                         sizeof(chan_tlv->header) +
1008                         sizeof(struct mwifiex_chan_scan_param_set);
1009         }
1010
1011         /* Append vendor specific IE TLV */
1012         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1013                                 MWIFIEX_VSIE_MASK_ADHOC, &pos);
1014
1015         if (priv->sec_info.wpa_enabled) {
1016                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1017                 if (rsn_ie_len == -1)
1018                         return -1;
1019                 cmd_append_size += rsn_ie_len;
1020         }
1021
1022         if (adapter->adhoc_11n_enabled) {
1023                 /* Fill HT CAPABILITY */
1024                 ht_cap = (struct mwifiex_ie_types_htcap *) pos;
1025                 memset(ht_cap, 0, sizeof(struct mwifiex_ie_types_htcap));
1026                 ht_cap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
1027                 ht_cap->header.len =
1028                        cpu_to_le16(sizeof(struct ieee80211_ht_cap));
1029                 radio_type = mwifiex_band_to_radio_type(
1030                                         priv->adapter->config_bands);
1031                 mwifiex_fill_cap_info(priv, radio_type, &ht_cap->ht_cap);
1032
1033                 if (adapter->sec_chan_offset ==
1034                                         IEEE80211_HT_PARAM_CHA_SEC_NONE) {
1035                         u16 tmp_ht_cap;
1036
1037                         tmp_ht_cap = le16_to_cpu(ht_cap->ht_cap.cap_info);
1038                         tmp_ht_cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
1039                         tmp_ht_cap &= ~IEEE80211_HT_CAP_SGI_40;
1040                         ht_cap->ht_cap.cap_info = cpu_to_le16(tmp_ht_cap);
1041                 }
1042
1043                 pos += sizeof(struct mwifiex_ie_types_htcap);
1044                 cmd_append_size += sizeof(struct mwifiex_ie_types_htcap);
1045
1046                 /* Fill HT INFORMATION */
1047                 ht_info = (struct mwifiex_ie_types_htinfo *) pos;
1048                 memset(ht_info, 0, sizeof(struct mwifiex_ie_types_htinfo));
1049                 ht_info->header.type = cpu_to_le16(WLAN_EID_HT_OPERATION);
1050                 ht_info->header.len =
1051                         cpu_to_le16(sizeof(struct ieee80211_ht_operation));
1052
1053                 ht_info->ht_oper.primary_chan =
1054                         (u8) priv->curr_bss_params.bss_descriptor.channel;
1055                 if (adapter->sec_chan_offset) {
1056                         ht_info->ht_oper.ht_param = adapter->sec_chan_offset;
1057                         ht_info->ht_oper.ht_param |=
1058                                         IEEE80211_HT_PARAM_CHAN_WIDTH_ANY;
1059                 }
1060                 ht_info->ht_oper.operation_mode =
1061                      cpu_to_le16(IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1062                 ht_info->ht_oper.basic_set[0] = 0xff;
1063                 pos += sizeof(struct mwifiex_ie_types_htinfo);
1064                 cmd_append_size +=
1065                                 sizeof(struct mwifiex_ie_types_htinfo);
1066         }
1067
1068         cmd->size =
1069                 cpu_to_le16((u16)(sizeof(struct host_cmd_ds_802_11_ad_hoc_start)
1070                                   + S_DS_GEN + cmd_append_size));
1071
1072         if (adapter->adhoc_start_band == BAND_B)
1073                 tmp_cap &= ~WLAN_CAPABILITY_SHORT_SLOT_TIME;
1074         else
1075                 tmp_cap |= WLAN_CAPABILITY_SHORT_SLOT_TIME;
1076
1077         adhoc_start->cap_info_bitmap = cpu_to_le16(tmp_cap);
1078
1079         return 0;
1080 }
1081
1082 /*
1083  * This function prepares command for ad-hoc join.
1084  *
1085  * Most of the parameters are set up by copying from the target BSS descriptor
1086  * from the scan response.
1087  *
1088  * In addition, the following TLVs are added -
1089  *      - Channel TLV
1090  *      - Vendor specific IE
1091  *      - WPA/WPA2 IE
1092  *      - 11n IE
1093  *
1094  * Preparation also includes -
1095  *      - Setting command ID and proper size
1096  *      - Ensuring correct endian-ness
1097  */
1098 int
1099 mwifiex_cmd_802_11_ad_hoc_join(struct mwifiex_private *priv,
1100                                struct host_cmd_ds_command *cmd,
1101                                struct mwifiex_bssdescriptor *bss_desc)
1102 {
1103         int rsn_ie_len = 0;
1104         struct host_cmd_ds_802_11_ad_hoc_join *adhoc_join =
1105                 &cmd->params.adhoc_join;
1106         struct mwifiex_ie_types_chan_list_param_set *chan_tlv;
1107         u32 cmd_append_size = 0;
1108         u16 tmp_cap;
1109         u32 i, rates_size = 0;
1110         u16 curr_pkt_filter;
1111         u8 *pos =
1112                 (u8 *) adhoc_join +
1113                 sizeof(struct host_cmd_ds_802_11_ad_hoc_join);
1114
1115 /* Use G protection */
1116 #define USE_G_PROTECTION        0x02
1117         if (bss_desc->erp_flags & USE_G_PROTECTION) {
1118                 curr_pkt_filter =
1119                         priv->
1120                         curr_pkt_filter | HostCmd_ACT_MAC_ADHOC_G_PROTECTION_ON;
1121
1122                 if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
1123                                      HostCmd_ACT_GEN_SET, 0,
1124                                      &curr_pkt_filter, false)) {
1125                         mwifiex_dbg(priv->adapter, ERROR,
1126                                     "ADHOC_J_CMD: G Protection config failed\n");
1127                         return -1;
1128                 }
1129         }
1130
1131         priv->attempted_bss_desc = bss_desc;
1132
1133         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_JOIN);
1134
1135         adhoc_join->bss_descriptor.bss_mode = HostCmd_BSS_MODE_IBSS;
1136
1137         adhoc_join->bss_descriptor.beacon_period
1138                 = cpu_to_le16(bss_desc->beacon_period);
1139
1140         memcpy(&adhoc_join->bss_descriptor.bssid,
1141                &bss_desc->mac_address, ETH_ALEN);
1142
1143         memcpy(&adhoc_join->bss_descriptor.ssid,
1144                &bss_desc->ssid.ssid, bss_desc->ssid.ssid_len);
1145
1146         memcpy(&adhoc_join->bss_descriptor.phy_param_set,
1147                &bss_desc->phy_param_set,
1148                sizeof(union ieee_types_phy_param_set));
1149
1150         memcpy(&adhoc_join->bss_descriptor.ss_param_set,
1151                &bss_desc->ss_param_set, sizeof(union ieee_types_ss_param_set));
1152
1153         tmp_cap = bss_desc->cap_info_bitmap;
1154
1155         tmp_cap &= CAPINFO_MASK;
1156
1157         mwifiex_dbg(priv->adapter, INFO,
1158                     "info: ADHOC_J_CMD: tmp_cap=%4X CAPINFO_MASK=%4lX\n",
1159                     tmp_cap, CAPINFO_MASK);
1160
1161         /* Information on BSSID descriptor passed to FW */
1162         mwifiex_dbg(priv->adapter, INFO,
1163                     "info: ADHOC_J_CMD: BSSID=%pM, SSID='%s'\n",
1164                     adhoc_join->bss_descriptor.bssid,
1165                     adhoc_join->bss_descriptor.ssid);
1166
1167         for (i = 0; i < MWIFIEX_SUPPORTED_RATES &&
1168                     bss_desc->supported_rates[i]; i++)
1169                 ;
1170         rates_size = i;
1171
1172         /* Copy Data Rates from the Rates recorded in scan response */
1173         memset(adhoc_join->bss_descriptor.data_rates, 0,
1174                sizeof(adhoc_join->bss_descriptor.data_rates));
1175         memcpy(adhoc_join->bss_descriptor.data_rates,
1176                bss_desc->supported_rates, rates_size);
1177
1178         /* Copy the adhoc join rates into Current BSS state structure */
1179         priv->curr_bss_params.num_of_rates = rates_size;
1180         memcpy(&priv->curr_bss_params.data_rates, bss_desc->supported_rates,
1181                rates_size);
1182
1183         /* Copy the channel information */
1184         priv->curr_bss_params.bss_descriptor.channel = bss_desc->channel;
1185         priv->curr_bss_params.band = (u8) bss_desc->bss_band;
1186
1187         if (priv->sec_info.wep_enabled || priv->sec_info.wpa_enabled)
1188                 tmp_cap |= WLAN_CAPABILITY_PRIVACY;
1189
1190         if (IS_SUPPORT_MULTI_BANDS(priv->adapter)) {
1191                 /* Append a channel TLV */
1192                 chan_tlv = (struct mwifiex_ie_types_chan_list_param_set *) pos;
1193                 chan_tlv->header.type = cpu_to_le16(TLV_TYPE_CHANLIST);
1194                 chan_tlv->header.len =
1195                         cpu_to_le16(sizeof(struct mwifiex_chan_scan_param_set));
1196
1197                 memset(chan_tlv->chan_scan_param, 0x00,
1198                        sizeof(struct mwifiex_chan_scan_param_set));
1199                 chan_tlv->chan_scan_param[0].chan_number =
1200                         (bss_desc->phy_param_set.ds_param_set.current_chan);
1201                 mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Chan=%d\n",
1202                             chan_tlv->chan_scan_param[0].chan_number);
1203
1204                 chan_tlv->chan_scan_param[0].radio_type =
1205                         mwifiex_band_to_radio_type((u8) bss_desc->bss_band);
1206
1207                 mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_J_CMD: TLV Band=%d\n",
1208                             chan_tlv->chan_scan_param[0].radio_type);
1209                 pos += sizeof(chan_tlv->header) +
1210                                 sizeof(struct mwifiex_chan_scan_param_set);
1211                 cmd_append_size += sizeof(chan_tlv->header) +
1212                                 sizeof(struct mwifiex_chan_scan_param_set);
1213         }
1214
1215         if (priv->sec_info.wpa_enabled)
1216                 rsn_ie_len = mwifiex_append_rsn_ie_wpa_wpa2(priv, &pos);
1217         if (rsn_ie_len == -1)
1218                 return -1;
1219         cmd_append_size += rsn_ie_len;
1220
1221         if (ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
1222                 cmd_append_size += mwifiex_cmd_append_11n_tlv(priv,
1223                         bss_desc, &pos);
1224
1225         /* Append vendor specific IE TLV */
1226         cmd_append_size += mwifiex_cmd_append_vsie_tlv(priv,
1227                         MWIFIEX_VSIE_MASK_ADHOC, &pos);
1228
1229         cmd->size = cpu_to_le16
1230                 ((u16) (sizeof(struct host_cmd_ds_802_11_ad_hoc_join)
1231                         + S_DS_GEN + cmd_append_size));
1232
1233         adhoc_join->bss_descriptor.cap_info_bitmap = cpu_to_le16(tmp_cap);
1234
1235         return 0;
1236 }
1237
1238 /*
1239  * This function handles the command response of ad-hoc start and
1240  * ad-hoc join.
1241  *
1242  * The function generates a device-connected event to notify
1243  * the applications, in case of successful ad-hoc start/join, and
1244  * saves the beacon buffer.
1245  */
1246 int mwifiex_ret_802_11_ad_hoc(struct mwifiex_private *priv,
1247                               struct host_cmd_ds_command *resp)
1248 {
1249         int ret = 0;
1250         struct mwifiex_adapter *adapter = priv->adapter;
1251         struct host_cmd_ds_802_11_ad_hoc_result *adhoc_result;
1252         struct mwifiex_bssdescriptor *bss_desc;
1253         u16 reason_code;
1254
1255         adhoc_result = &resp->params.adhoc_result;
1256
1257         bss_desc = priv->attempted_bss_desc;
1258
1259         /* Join result code 0 --> SUCCESS */
1260         reason_code = le16_to_cpu(resp->result);
1261         if (reason_code) {
1262                 mwifiex_dbg(priv->adapter, ERROR, "ADHOC_RESP: failed\n");
1263                 if (priv->media_connected)
1264                         mwifiex_reset_connect_state(priv, reason_code);
1265
1266                 memset(&priv->curr_bss_params.bss_descriptor,
1267                        0x00, sizeof(struct mwifiex_bssdescriptor));
1268
1269                 ret = -1;
1270                 goto done;
1271         }
1272
1273         /* Send a Media Connected event, according to the Spec */
1274         priv->media_connected = true;
1275
1276         if (le16_to_cpu(resp->command) == HostCmd_CMD_802_11_AD_HOC_START) {
1277                 mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_S_RESP %s\n",
1278                             bss_desc->ssid.ssid);
1279
1280                 /* Update the created network descriptor with the new BSSID */
1281                 memcpy(bss_desc->mac_address,
1282                        adhoc_result->bssid, ETH_ALEN);
1283
1284                 priv->adhoc_state = ADHOC_STARTED;
1285         } else {
1286                 /*
1287                  * Now the join cmd should be successful.
1288                  * If BSSID has changed use SSID to compare instead of BSSID
1289                  */
1290                 mwifiex_dbg(priv->adapter, INFO,
1291                             "info: ADHOC_J_RESP %s\n",
1292                             bss_desc->ssid.ssid);
1293
1294                 /*
1295                  * Make a copy of current BSSID descriptor, only needed for
1296                  * join since the current descriptor is already being used
1297                  * for adhoc start
1298                  */
1299                 memcpy(&priv->curr_bss_params.bss_descriptor,
1300                        bss_desc, sizeof(struct mwifiex_bssdescriptor));
1301
1302                 priv->adhoc_state = ADHOC_JOINED;
1303         }
1304
1305         mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: channel = %d\n",
1306                     priv->adhoc_channel);
1307         mwifiex_dbg(priv->adapter, INFO, "info: ADHOC_RESP: BSSID = %pM\n",
1308                     priv->curr_bss_params.bss_descriptor.mac_address);
1309
1310         if (!netif_carrier_ok(priv->netdev))
1311                 netif_carrier_on(priv->netdev);
1312         mwifiex_wake_up_net_dev_queue(priv->netdev, adapter);
1313
1314         mwifiex_save_curr_bcn(priv);
1315
1316 done:
1317         /* Need to indicate IOCTL complete */
1318         if (adapter->curr_cmd->wait_q_enabled) {
1319                 if (ret)
1320                         adapter->cmd_wait_q.status = -1;
1321                 else
1322                         adapter->cmd_wait_q.status = 0;
1323
1324         }
1325
1326         return ret;
1327 }
1328
1329 /*
1330  * This function associates to a specific BSS discovered in a scan.
1331  *
1332  * It clears any past association response stored for application
1333  * retrieval and calls the command preparation routine to send the
1334  * command to firmware.
1335  */
1336 int mwifiex_associate(struct mwifiex_private *priv,
1337                       struct mwifiex_bssdescriptor *bss_desc)
1338 {
1339         /* Return error if the adapter is not STA role or table entry
1340          * is not marked as infra.
1341          */
1342         if ((GET_BSS_ROLE(priv) != MWIFIEX_BSS_ROLE_STA) ||
1343             (bss_desc->bss_mode != NL80211_IFTYPE_STATION))
1344                 return -1;
1345
1346         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1347             !bss_desc->disable_11n && !bss_desc->disable_11ac &&
1348             priv->adapter->config_bands & BAND_AAC)
1349                 mwifiex_set_11ac_ba_params(priv);
1350         else
1351                 mwifiex_set_ba_params(priv);
1352
1353         /* Clear any past association response stored for application
1354            retrieval */
1355         priv->assoc_rsp_size = 0;
1356
1357         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_ASSOCIATE,
1358                                 HostCmd_ACT_GEN_SET, 0, bss_desc, true);
1359 }
1360
1361 /*
1362  * This function starts an ad-hoc network.
1363  *
1364  * It calls the command preparation routine to send the command to firmware.
1365  */
1366 int
1367 mwifiex_adhoc_start(struct mwifiex_private *priv,
1368                     struct cfg80211_ssid *adhoc_ssid)
1369 {
1370         mwifiex_dbg(priv->adapter, INFO, "info: Adhoc Channel = %d\n",
1371                     priv->adhoc_channel);
1372         mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.channel = %d\n",
1373                     priv->curr_bss_params.bss_descriptor.channel);
1374         mwifiex_dbg(priv->adapter, INFO, "info: curr_bss_params.band = %d\n",
1375                     priv->curr_bss_params.band);
1376
1377         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1378             priv->adapter->config_bands & BAND_AAC)
1379                 mwifiex_set_11ac_ba_params(priv);
1380         else
1381                 mwifiex_set_ba_params(priv);
1382
1383         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_START,
1384                                 HostCmd_ACT_GEN_SET, 0, adhoc_ssid, true);
1385 }
1386
1387 /*
1388  * This function joins an ad-hoc network found in a previous scan.
1389  *
1390  * It calls the command preparation routine to send the command to firmware,
1391  * if already not connected to the requested SSID.
1392  */
1393 int mwifiex_adhoc_join(struct mwifiex_private *priv,
1394                        struct mwifiex_bssdescriptor *bss_desc)
1395 {
1396         mwifiex_dbg(priv->adapter, INFO,
1397                     "info: adhoc join: curr_bss ssid =%s\n",
1398                     priv->curr_bss_params.bss_descriptor.ssid.ssid);
1399         mwifiex_dbg(priv->adapter, INFO,
1400                     "info: adhoc join: curr_bss ssid_len =%u\n",
1401                     priv->curr_bss_params.bss_descriptor.ssid.ssid_len);
1402         mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid =%s\n",
1403                     bss_desc->ssid.ssid);
1404         mwifiex_dbg(priv->adapter, INFO, "info: adhoc join: ssid_len =%u\n",
1405                     bss_desc->ssid.ssid_len);
1406
1407         /* Check if the requested SSID is already joined */
1408         if (priv->curr_bss_params.bss_descriptor.ssid.ssid_len &&
1409             !mwifiex_ssid_cmp(&bss_desc->ssid,
1410                               &priv->curr_bss_params.bss_descriptor.ssid) &&
1411             (priv->curr_bss_params.bss_descriptor.bss_mode ==
1412                                                         NL80211_IFTYPE_ADHOC)) {
1413                 mwifiex_dbg(priv->adapter, INFO,
1414                             "info: ADHOC_J_CMD: new ad-hoc SSID\t"
1415                             "is the same as current; not attempting to re-join\n");
1416                 return -1;
1417         }
1418
1419         if (ISSUPP_11ACENABLED(priv->adapter->fw_cap_info) &&
1420             !bss_desc->disable_11n && !bss_desc->disable_11ac &&
1421             priv->adapter->config_bands & BAND_AAC)
1422                 mwifiex_set_11ac_ba_params(priv);
1423         else
1424                 mwifiex_set_ba_params(priv);
1425
1426         mwifiex_dbg(priv->adapter, INFO,
1427                     "info: curr_bss_params.channel = %d\n",
1428                     priv->curr_bss_params.bss_descriptor.channel);
1429         mwifiex_dbg(priv->adapter, INFO,
1430                     "info: curr_bss_params.band = %c\n",
1431                     priv->curr_bss_params.band);
1432
1433         return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_JOIN,
1434                                 HostCmd_ACT_GEN_SET, 0, bss_desc, true);
1435 }
1436
1437 /*
1438  * This function deauthenticates/disconnects from infra network by sending
1439  * deauthentication request.
1440  */
1441 static int mwifiex_deauthenticate_infra(struct mwifiex_private *priv, u8 *mac)
1442 {
1443         u8 mac_address[ETH_ALEN];
1444         int ret;
1445
1446         if (!mac || is_zero_ether_addr(mac))
1447                 memcpy(mac_address,
1448                        priv->curr_bss_params.bss_descriptor.mac_address,
1449                        ETH_ALEN);
1450         else
1451                 memcpy(mac_address, mac, ETH_ALEN);
1452
1453         ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_DEAUTHENTICATE,
1454                                HostCmd_ACT_GEN_SET, 0, mac_address, true);
1455
1456         return ret;
1457 }
1458
1459 /*
1460  * This function deauthenticates/disconnects from a BSS.
1461  *
1462  * In case of infra made, it sends deauthentication request, and
1463  * in case of ad-hoc mode, a stop network request is sent to the firmware.
1464  * In AP mode, a command to stop bss is sent to firmware.
1465  */
1466 int mwifiex_deauthenticate(struct mwifiex_private *priv, u8 *mac)
1467 {
1468         int ret = 0;
1469
1470         if (!priv->media_connected)
1471                 return 0;
1472
1473         switch (priv->bss_mode) {
1474         case NL80211_IFTYPE_STATION:
1475         case NL80211_IFTYPE_P2P_CLIENT:
1476                 ret = mwifiex_deauthenticate_infra(priv, mac);
1477                 if (ret)
1478                         cfg80211_disconnected(priv->netdev, 0, NULL, 0,
1479                                               true, GFP_KERNEL);
1480                 break;
1481         case NL80211_IFTYPE_ADHOC:
1482                 return mwifiex_send_cmd(priv, HostCmd_CMD_802_11_AD_HOC_STOP,
1483                                         HostCmd_ACT_GEN_SET, 0, NULL, true);
1484         case NL80211_IFTYPE_AP:
1485                 return mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_STOP,
1486                                         HostCmd_ACT_GEN_SET, 0, NULL, true);
1487         default:
1488                 break;
1489         }
1490
1491         return ret;
1492 }
1493
1494 /* This function deauthenticates/disconnects from all BSS. */
1495 void mwifiex_deauthenticate_all(struct mwifiex_adapter *adapter)
1496 {
1497         struct mwifiex_private *priv;
1498         int i;
1499
1500         for (i = 0; i < adapter->priv_num; i++) {
1501                 priv = adapter->priv[i];
1502                 if (priv)
1503                         mwifiex_deauthenticate(priv, NULL);
1504         }
1505 }
1506 EXPORT_SYMBOL_GPL(mwifiex_deauthenticate_all);
1507
1508 /*
1509  * This function converts band to radio type used in channel TLV.
1510  */
1511 u8
1512 mwifiex_band_to_radio_type(u8 band)
1513 {
1514         switch (band) {
1515         case BAND_A:
1516         case BAND_AN:
1517         case BAND_A | BAND_AN:
1518         case BAND_A | BAND_AN | BAND_AAC:
1519                 return HostCmd_SCAN_RADIO_TYPE_A;
1520         case BAND_B:
1521         case BAND_G:
1522         case BAND_B | BAND_G:
1523         default:
1524                 return HostCmd_SCAN_RADIO_TYPE_BG;
1525         }
1526 }