Merge ath-next from ath.git
[firefly-linux-kernel-4.4.55.git] / drivers / net / wireless / mwifiex / sta_cmd.c
1 /*
2  * Marvell Wireless LAN device driver: station 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 "wmm.h"
26 #include "11n.h"
27 #include "11ac.h"
28
29 static bool disable_auto_ds;
30 module_param(disable_auto_ds, bool, 0);
31 MODULE_PARM_DESC(disable_auto_ds,
32                  "deepsleep enabled=0(default), deepsleep disabled=1");
33 /*
34  * This function prepares command to set/get RSSI information.
35  *
36  * Preparation includes -
37  *      - Setting command ID, action and proper size
38  *      - Setting data/beacon average factors
39  *      - Resetting SNR/NF/RSSI values in private structure
40  *      - Ensuring correct endian-ness
41  */
42 static int
43 mwifiex_cmd_802_11_rssi_info(struct mwifiex_private *priv,
44                              struct host_cmd_ds_command *cmd, u16 cmd_action)
45 {
46         cmd->command = cpu_to_le16(HostCmd_CMD_RSSI_INFO);
47         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_rssi_info) +
48                                 S_DS_GEN);
49         cmd->params.rssi_info.action = cpu_to_le16(cmd_action);
50         cmd->params.rssi_info.ndata = cpu_to_le16(priv->data_avg_factor);
51         cmd->params.rssi_info.nbcn = cpu_to_le16(priv->bcn_avg_factor);
52
53         /* Reset SNR/NF/RSSI values in private structure */
54         priv->data_rssi_last = 0;
55         priv->data_nf_last = 0;
56         priv->data_rssi_avg = 0;
57         priv->data_nf_avg = 0;
58         priv->bcn_rssi_last = 0;
59         priv->bcn_nf_last = 0;
60         priv->bcn_rssi_avg = 0;
61         priv->bcn_nf_avg = 0;
62
63         return 0;
64 }
65
66 /*
67  * This function prepares command to set MAC control.
68  *
69  * Preparation includes -
70  *      - Setting command ID, action and proper size
71  *      - Ensuring correct endian-ness
72  */
73 static int mwifiex_cmd_mac_control(struct mwifiex_private *priv,
74                                    struct host_cmd_ds_command *cmd,
75                                    u16 cmd_action, u16 *action)
76 {
77         struct host_cmd_ds_mac_control *mac_ctrl = &cmd->params.mac_ctrl;
78
79         if (cmd_action != HostCmd_ACT_GEN_SET) {
80                 mwifiex_dbg(priv->adapter, ERROR,
81                             "mac_control: only support set cmd\n");
82                 return -1;
83         }
84
85         cmd->command = cpu_to_le16(HostCmd_CMD_MAC_CONTROL);
86         cmd->size =
87                 cpu_to_le16(sizeof(struct host_cmd_ds_mac_control) + S_DS_GEN);
88         mac_ctrl->action = cpu_to_le16(*action);
89
90         return 0;
91 }
92
93 /*
94  * This function prepares command to set/get SNMP MIB.
95  *
96  * Preparation includes -
97  *      - Setting command ID, action and proper size
98  *      - Setting SNMP MIB OID number and value
99  *        (as required)
100  *      - Ensuring correct endian-ness
101  *
102  * The following SNMP MIB OIDs are supported -
103  *      - FRAG_THRESH_I     : Fragmentation threshold
104  *      - RTS_THRESH_I      : RTS threshold
105  *      - SHORT_RETRY_LIM_I : Short retry limit
106  *      - DOT11D_I          : 11d support
107  */
108 static int mwifiex_cmd_802_11_snmp_mib(struct mwifiex_private *priv,
109                                        struct host_cmd_ds_command *cmd,
110                                        u16 cmd_action, u32 cmd_oid,
111                                        u16 *ul_temp)
112 {
113         struct host_cmd_ds_802_11_snmp_mib *snmp_mib = &cmd->params.smib;
114
115         mwifiex_dbg(priv->adapter, CMD,
116                     "cmd: SNMP_CMD: cmd_oid = 0x%x\n", cmd_oid);
117         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SNMP_MIB);
118         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_snmp_mib)
119                                 - 1 + S_DS_GEN);
120
121         snmp_mib->oid = cpu_to_le16((u16)cmd_oid);
122         if (cmd_action == HostCmd_ACT_GEN_GET) {
123                 snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_GET);
124                 snmp_mib->buf_size = cpu_to_le16(MAX_SNMP_BUF_SIZE);
125                 le16_add_cpu(&cmd->size, MAX_SNMP_BUF_SIZE);
126         } else if (cmd_action == HostCmd_ACT_GEN_SET) {
127                 snmp_mib->query_type = cpu_to_le16(HostCmd_ACT_GEN_SET);
128                 snmp_mib->buf_size = cpu_to_le16(sizeof(u16));
129                 *((__le16 *) (snmp_mib->value)) = cpu_to_le16(*ul_temp);
130                 le16_add_cpu(&cmd->size, sizeof(u16));
131         }
132
133         mwifiex_dbg(priv->adapter, CMD,
134                     "cmd: SNMP_CMD: Action=0x%x, OID=0x%x,\t"
135                     "OIDSize=0x%x, Value=0x%x\n",
136                     cmd_action, cmd_oid, le16_to_cpu(snmp_mib->buf_size),
137                     le16_to_cpu(*(__le16 *)snmp_mib->value));
138         return 0;
139 }
140
141 /*
142  * This function prepares command to get log.
143  *
144  * Preparation includes -
145  *      - Setting command ID and proper size
146  *      - Ensuring correct endian-ness
147  */
148 static int
149 mwifiex_cmd_802_11_get_log(struct host_cmd_ds_command *cmd)
150 {
151         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_GET_LOG);
152         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_get_log) +
153                                 S_DS_GEN);
154         return 0;
155 }
156
157 /*
158  * This function prepares command to set/get Tx data rate configuration.
159  *
160  * Preparation includes -
161  *      - Setting command ID, action and proper size
162  *      - Setting configuration index, rate scope and rate drop pattern
163  *        parameters (as required)
164  *      - Ensuring correct endian-ness
165  */
166 static int mwifiex_cmd_tx_rate_cfg(struct mwifiex_private *priv,
167                                    struct host_cmd_ds_command *cmd,
168                                    u16 cmd_action, u16 *pbitmap_rates)
169 {
170         struct host_cmd_ds_tx_rate_cfg *rate_cfg = &cmd->params.tx_rate_cfg;
171         struct mwifiex_rate_scope *rate_scope;
172         struct mwifiex_rate_drop_pattern *rate_drop;
173         u32 i;
174
175         cmd->command = cpu_to_le16(HostCmd_CMD_TX_RATE_CFG);
176
177         rate_cfg->action = cpu_to_le16(cmd_action);
178         rate_cfg->cfg_index = 0;
179
180         rate_scope = (struct mwifiex_rate_scope *) ((u8 *) rate_cfg +
181                       sizeof(struct host_cmd_ds_tx_rate_cfg));
182         rate_scope->type = cpu_to_le16(TLV_TYPE_RATE_SCOPE);
183         rate_scope->length = cpu_to_le16
184                 (sizeof(*rate_scope) - sizeof(struct mwifiex_ie_types_header));
185         if (pbitmap_rates != NULL) {
186                 rate_scope->hr_dsss_rate_bitmap = cpu_to_le16(pbitmap_rates[0]);
187                 rate_scope->ofdm_rate_bitmap = cpu_to_le16(pbitmap_rates[1]);
188                 for (i = 0;
189                      i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(u16);
190                      i++)
191                         rate_scope->ht_mcs_rate_bitmap[i] =
192                                 cpu_to_le16(pbitmap_rates[2 + i]);
193                 if (priv->adapter->fw_api_ver == MWIFIEX_FW_V15) {
194                         for (i = 0;
195                              i < ARRAY_SIZE(rate_scope->vht_mcs_rate_bitmap);
196                              i++)
197                                 rate_scope->vht_mcs_rate_bitmap[i] =
198                                         cpu_to_le16(pbitmap_rates[10 + i]);
199                 }
200         } else {
201                 rate_scope->hr_dsss_rate_bitmap =
202                         cpu_to_le16(priv->bitmap_rates[0]);
203                 rate_scope->ofdm_rate_bitmap =
204                         cpu_to_le16(priv->bitmap_rates[1]);
205                 for (i = 0;
206                      i < sizeof(rate_scope->ht_mcs_rate_bitmap) / sizeof(u16);
207                      i++)
208                         rate_scope->ht_mcs_rate_bitmap[i] =
209                                 cpu_to_le16(priv->bitmap_rates[2 + i]);
210                 if (priv->adapter->fw_api_ver == MWIFIEX_FW_V15) {
211                         for (i = 0;
212                              i < ARRAY_SIZE(rate_scope->vht_mcs_rate_bitmap);
213                              i++)
214                                 rate_scope->vht_mcs_rate_bitmap[i] =
215                                         cpu_to_le16(priv->bitmap_rates[10 + i]);
216                 }
217         }
218
219         rate_drop = (struct mwifiex_rate_drop_pattern *) ((u8 *) rate_scope +
220                                              sizeof(struct mwifiex_rate_scope));
221         rate_drop->type = cpu_to_le16(TLV_TYPE_RATE_DROP_CONTROL);
222         rate_drop->length = cpu_to_le16(sizeof(rate_drop->rate_drop_mode));
223         rate_drop->rate_drop_mode = 0;
224
225         cmd->size =
226                 cpu_to_le16(S_DS_GEN + sizeof(struct host_cmd_ds_tx_rate_cfg) +
227                             sizeof(struct mwifiex_rate_scope) +
228                             sizeof(struct mwifiex_rate_drop_pattern));
229
230         return 0;
231 }
232
233 /*
234  * This function prepares command to set/get Tx power configuration.
235  *
236  * Preparation includes -
237  *      - Setting command ID, action and proper size
238  *      - Setting Tx power mode, power group TLV
239  *        (as required)
240  *      - Ensuring correct endian-ness
241  */
242 static int mwifiex_cmd_tx_power_cfg(struct host_cmd_ds_command *cmd,
243                                     u16 cmd_action,
244                                     struct host_cmd_ds_txpwr_cfg *txp)
245 {
246         struct mwifiex_types_power_group *pg_tlv;
247         struct host_cmd_ds_txpwr_cfg *cmd_txp_cfg = &cmd->params.txp_cfg;
248
249         cmd->command = cpu_to_le16(HostCmd_CMD_TXPWR_CFG);
250         cmd->size =
251                 cpu_to_le16(S_DS_GEN + sizeof(struct host_cmd_ds_txpwr_cfg));
252         switch (cmd_action) {
253         case HostCmd_ACT_GEN_SET:
254                 if (txp->mode) {
255                         pg_tlv = (struct mwifiex_types_power_group
256                                   *) ((unsigned long) txp +
257                                      sizeof(struct host_cmd_ds_txpwr_cfg));
258                         memmove(cmd_txp_cfg, txp,
259                                 sizeof(struct host_cmd_ds_txpwr_cfg) +
260                                 sizeof(struct mwifiex_types_power_group) +
261                                 le16_to_cpu(pg_tlv->length));
262
263                         pg_tlv = (struct mwifiex_types_power_group *) ((u8 *)
264                                   cmd_txp_cfg +
265                                   sizeof(struct host_cmd_ds_txpwr_cfg));
266                         cmd->size = cpu_to_le16(le16_to_cpu(cmd->size) +
267                                   sizeof(struct mwifiex_types_power_group) +
268                                   le16_to_cpu(pg_tlv->length));
269                 } else {
270                         memmove(cmd_txp_cfg, txp, sizeof(*txp));
271                 }
272                 cmd_txp_cfg->action = cpu_to_le16(cmd_action);
273                 break;
274         case HostCmd_ACT_GEN_GET:
275                 cmd_txp_cfg->action = cpu_to_le16(cmd_action);
276                 break;
277         }
278
279         return 0;
280 }
281
282 /*
283  * This function prepares command to get RF Tx power.
284  */
285 static int mwifiex_cmd_rf_tx_power(struct mwifiex_private *priv,
286                                    struct host_cmd_ds_command *cmd,
287                                    u16 cmd_action, void *data_buf)
288 {
289         struct host_cmd_ds_rf_tx_pwr *txp = &cmd->params.txp;
290
291         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_rf_tx_pwr)
292                                 + S_DS_GEN);
293         cmd->command = cpu_to_le16(HostCmd_CMD_RF_TX_PWR);
294         txp->action = cpu_to_le16(cmd_action);
295
296         return 0;
297 }
298
299 /*
300  * This function prepares command to set rf antenna.
301  */
302 static int mwifiex_cmd_rf_antenna(struct mwifiex_private *priv,
303                                   struct host_cmd_ds_command *cmd,
304                                   u16 cmd_action,
305                                   struct mwifiex_ds_ant_cfg *ant_cfg)
306 {
307         struct host_cmd_ds_rf_ant_mimo *ant_mimo = &cmd->params.ant_mimo;
308         struct host_cmd_ds_rf_ant_siso *ant_siso = &cmd->params.ant_siso;
309
310         cmd->command = cpu_to_le16(HostCmd_CMD_RF_ANTENNA);
311
312         if (cmd_action != HostCmd_ACT_GEN_SET)
313                 return 0;
314
315         if (priv->adapter->hw_dev_mcs_support == HT_STREAM_2X2) {
316                 cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_rf_ant_mimo) +
317                                         S_DS_GEN);
318                 ant_mimo->action_tx = cpu_to_le16(HostCmd_ACT_SET_TX);
319                 ant_mimo->tx_ant_mode = cpu_to_le16((u16)ant_cfg->tx_ant);
320                 ant_mimo->action_rx = cpu_to_le16(HostCmd_ACT_SET_RX);
321                 ant_mimo->rx_ant_mode = cpu_to_le16((u16)ant_cfg->rx_ant);
322         } else {
323                 cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_rf_ant_siso) +
324                                         S_DS_GEN);
325                 ant_siso->action = cpu_to_le16(HostCmd_ACT_SET_BOTH);
326                 ant_siso->ant_mode = cpu_to_le16((u16)ant_cfg->tx_ant);
327         }
328
329         return 0;
330 }
331
332 /*
333  * This function prepares command to set Host Sleep configuration.
334  *
335  * Preparation includes -
336  *      - Setting command ID and proper size
337  *      - Setting Host Sleep action, conditions, ARP filters
338  *        (as required)
339  *      - Ensuring correct endian-ness
340  */
341 static int
342 mwifiex_cmd_802_11_hs_cfg(struct mwifiex_private *priv,
343                           struct host_cmd_ds_command *cmd,
344                           u16 cmd_action,
345                           struct mwifiex_hs_config_param *hscfg_param)
346 {
347         struct mwifiex_adapter *adapter = priv->adapter;
348         struct host_cmd_ds_802_11_hs_cfg_enh *hs_cfg = &cmd->params.opt_hs_cfg;
349         bool hs_activate = false;
350
351         if (!hscfg_param)
352                 /* New Activate command */
353                 hs_activate = true;
354         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_HS_CFG_ENH);
355
356         if (!hs_activate &&
357             (hscfg_param->conditions != cpu_to_le32(HS_CFG_CANCEL)) &&
358             ((adapter->arp_filter_size > 0) &&
359              (adapter->arp_filter_size <= ARP_FILTER_MAX_BUF_SIZE))) {
360                 mwifiex_dbg(adapter, CMD,
361                             "cmd: Attach %d bytes ArpFilter to HSCfg cmd\n",
362                             adapter->arp_filter_size);
363                 memcpy(((u8 *) hs_cfg) +
364                        sizeof(struct host_cmd_ds_802_11_hs_cfg_enh),
365                        adapter->arp_filter, adapter->arp_filter_size);
366                 cmd->size = cpu_to_le16
367                                 (adapter->arp_filter_size +
368                                  sizeof(struct host_cmd_ds_802_11_hs_cfg_enh)
369                                 + S_DS_GEN);
370         } else {
371                 cmd->size = cpu_to_le16(S_DS_GEN + sizeof(struct
372                                                 host_cmd_ds_802_11_hs_cfg_enh));
373         }
374         if (hs_activate) {
375                 hs_cfg->action = cpu_to_le16(HS_ACTIVATE);
376                 hs_cfg->params.hs_activate.resp_ctrl = cpu_to_le16(RESP_NEEDED);
377         } else {
378                 hs_cfg->action = cpu_to_le16(HS_CONFIGURE);
379                 hs_cfg->params.hs_config.conditions = hscfg_param->conditions;
380                 hs_cfg->params.hs_config.gpio = hscfg_param->gpio;
381                 hs_cfg->params.hs_config.gap = hscfg_param->gap;
382                 mwifiex_dbg(adapter, CMD,
383                             "cmd: HS_CFG_CMD: condition:0x%x gpio:0x%x gap:0x%x\n",
384                             hs_cfg->params.hs_config.conditions,
385                             hs_cfg->params.hs_config.gpio,
386                             hs_cfg->params.hs_config.gap);
387         }
388
389         return 0;
390 }
391
392 /*
393  * This function prepares command to set/get MAC address.
394  *
395  * Preparation includes -
396  *      - Setting command ID, action and proper size
397  *      - Setting MAC address (for SET only)
398  *      - Ensuring correct endian-ness
399  */
400 static int mwifiex_cmd_802_11_mac_address(struct mwifiex_private *priv,
401                                           struct host_cmd_ds_command *cmd,
402                                           u16 cmd_action)
403 {
404         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_MAC_ADDRESS);
405         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_mac_address) +
406                                 S_DS_GEN);
407         cmd->result = 0;
408
409         cmd->params.mac_addr.action = cpu_to_le16(cmd_action);
410
411         if (cmd_action == HostCmd_ACT_GEN_SET)
412                 memcpy(cmd->params.mac_addr.mac_addr, priv->curr_addr,
413                        ETH_ALEN);
414         return 0;
415 }
416
417 /*
418  * This function prepares command to set MAC multicast address.
419  *
420  * Preparation includes -
421  *      - Setting command ID, action and proper size
422  *      - Setting MAC multicast address
423  *      - Ensuring correct endian-ness
424  */
425 static int
426 mwifiex_cmd_mac_multicast_adr(struct host_cmd_ds_command *cmd,
427                               u16 cmd_action,
428                               struct mwifiex_multicast_list *mcast_list)
429 {
430         struct host_cmd_ds_mac_multicast_adr *mcast_addr = &cmd->params.mc_addr;
431
432         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_mac_multicast_adr) +
433                                 S_DS_GEN);
434         cmd->command = cpu_to_le16(HostCmd_CMD_MAC_MULTICAST_ADR);
435
436         mcast_addr->action = cpu_to_le16(cmd_action);
437         mcast_addr->num_of_adrs =
438                 cpu_to_le16((u16) mcast_list->num_multicast_addr);
439         memcpy(mcast_addr->mac_list, mcast_list->mac_list,
440                mcast_list->num_multicast_addr * ETH_ALEN);
441
442         return 0;
443 }
444
445 /*
446  * This function prepares command to deauthenticate.
447  *
448  * Preparation includes -
449  *      - Setting command ID and proper size
450  *      - Setting AP MAC address and reason code
451  *      - Ensuring correct endian-ness
452  */
453 static int mwifiex_cmd_802_11_deauthenticate(struct mwifiex_private *priv,
454                                              struct host_cmd_ds_command *cmd,
455                                              u8 *mac)
456 {
457         struct host_cmd_ds_802_11_deauthenticate *deauth = &cmd->params.deauth;
458
459         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_DEAUTHENTICATE);
460         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_deauthenticate)
461                                 + S_DS_GEN);
462
463         /* Set AP MAC address */
464         memcpy(deauth->mac_addr, mac, ETH_ALEN);
465
466         mwifiex_dbg(priv->adapter, CMD, "cmd: Deauth: %pM\n", deauth->mac_addr);
467
468         deauth->reason_code = cpu_to_le16(WLAN_REASON_DEAUTH_LEAVING);
469
470         return 0;
471 }
472
473 /*
474  * This function prepares command to stop Ad-Hoc network.
475  *
476  * Preparation includes -
477  *      - Setting command ID and proper size
478  *      - Ensuring correct endian-ness
479  */
480 static int mwifiex_cmd_802_11_ad_hoc_stop(struct host_cmd_ds_command *cmd)
481 {
482         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_AD_HOC_STOP);
483         cmd->size = cpu_to_le16(S_DS_GEN);
484         return 0;
485 }
486
487 /*
488  * This function sets WEP key(s) to key parameter TLV(s).
489  *
490  * Multi-key parameter TLVs are supported, so we can send multiple
491  * WEP keys in a single buffer.
492  */
493 static int
494 mwifiex_set_keyparamset_wep(struct mwifiex_private *priv,
495                             struct mwifiex_ie_type_key_param_set *key_param_set,
496                             u16 *key_param_len)
497 {
498         int cur_key_param_len;
499         u8 i;
500
501         /* Multi-key_param_set TLV is supported */
502         for (i = 0; i < NUM_WEP_KEYS; i++) {
503                 if ((priv->wep_key[i].key_length == WLAN_KEY_LEN_WEP40) ||
504                     (priv->wep_key[i].key_length == WLAN_KEY_LEN_WEP104)) {
505                         key_param_set->type =
506                                 cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
507 /* Key_param_set WEP fixed length */
508 #define KEYPARAMSET_WEP_FIXED_LEN 8
509                         key_param_set->length = cpu_to_le16((u16)
510                                         (priv->wep_key[i].
511                                          key_length +
512                                          KEYPARAMSET_WEP_FIXED_LEN));
513                         key_param_set->key_type_id =
514                                 cpu_to_le16(KEY_TYPE_ID_WEP);
515                         key_param_set->key_info =
516                                 cpu_to_le16(KEY_ENABLED | KEY_UNICAST |
517                                             KEY_MCAST);
518                         key_param_set->key_len =
519                                 cpu_to_le16(priv->wep_key[i].key_length);
520                         /* Set WEP key index */
521                         key_param_set->key[0] = i;
522                         /* Set default Tx key flag */
523                         if (i ==
524                             (priv->
525                              wep_key_curr_index & HostCmd_WEP_KEY_INDEX_MASK))
526                                 key_param_set->key[1] = 1;
527                         else
528                                 key_param_set->key[1] = 0;
529                         memmove(&key_param_set->key[2],
530                                 priv->wep_key[i].key_material,
531                                 priv->wep_key[i].key_length);
532
533                         cur_key_param_len = priv->wep_key[i].key_length +
534                                 KEYPARAMSET_WEP_FIXED_LEN +
535                                 sizeof(struct mwifiex_ie_types_header);
536                         *key_param_len += (u16) cur_key_param_len;
537                         key_param_set =
538                                 (struct mwifiex_ie_type_key_param_set *)
539                                                 ((u8 *)key_param_set +
540                                                  cur_key_param_len);
541                 } else if (!priv->wep_key[i].key_length) {
542                         continue;
543                 } else {
544                         mwifiex_dbg(priv->adapter, ERROR,
545                                     "key%d Length = %d is incorrect\n",
546                                     (i + 1), priv->wep_key[i].key_length);
547                         return -1;
548                 }
549         }
550
551         return 0;
552 }
553
554 /* This function populates key material v2 command
555  * to set network key for AES & CMAC AES.
556  */
557 static int mwifiex_set_aes_key_v2(struct mwifiex_private *priv,
558                                   struct host_cmd_ds_command *cmd,
559                                   struct mwifiex_ds_encrypt_key *enc_key,
560                                   struct host_cmd_ds_802_11_key_material_v2 *km)
561 {
562         struct mwifiex_adapter *adapter = priv->adapter;
563         u16 size, len = KEY_PARAMS_FIXED_LEN;
564
565         if (enc_key->is_igtk_key) {
566                 mwifiex_dbg(adapter, INFO,
567                             "%s: Set CMAC AES Key\n", __func__);
568                 if (enc_key->is_rx_seq_valid)
569                         memcpy(km->key_param_set.key_params.cmac_aes.ipn,
570                                enc_key->pn, enc_key->pn_len);
571                 km->key_param_set.key_info &= cpu_to_le16(~KEY_MCAST);
572                 km->key_param_set.key_info |= cpu_to_le16(KEY_IGTK);
573                 km->key_param_set.key_type = KEY_TYPE_ID_AES_CMAC;
574                 km->key_param_set.key_params.cmac_aes.key_len =
575                                           cpu_to_le16(enc_key->key_len);
576                 memcpy(km->key_param_set.key_params.cmac_aes.key,
577                        enc_key->key_material, enc_key->key_len);
578                 len += sizeof(struct mwifiex_cmac_aes_param);
579         } else {
580                 mwifiex_dbg(adapter, INFO,
581                             "%s: Set AES Key\n", __func__);
582                 if (enc_key->is_rx_seq_valid)
583                         memcpy(km->key_param_set.key_params.aes.pn,
584                                enc_key->pn, enc_key->pn_len);
585                 km->key_param_set.key_type = KEY_TYPE_ID_AES;
586                 km->key_param_set.key_params.aes.key_len =
587                                           cpu_to_le16(enc_key->key_len);
588                 memcpy(km->key_param_set.key_params.aes.key,
589                        enc_key->key_material, enc_key->key_len);
590                 len += sizeof(struct mwifiex_aes_param);
591         }
592
593         km->key_param_set.len = cpu_to_le16(len);
594         size = len + sizeof(struct mwifiex_ie_types_header) +
595                sizeof(km->action) + S_DS_GEN;
596         cmd->size = cpu_to_le16(size);
597
598         return 0;
599 }
600
601 /* This function prepares command to set/get/reset network key(s).
602  * This function prepares key material command for V2 format.
603  * Preparation includes -
604  *      - Setting command ID, action and proper size
605  *      - Setting WEP keys, WAPI keys or WPA keys along with required
606  *        encryption (TKIP, AES) (as required)
607  *      - Ensuring correct endian-ness
608  */
609 static int
610 mwifiex_cmd_802_11_key_material_v2(struct mwifiex_private *priv,
611                                    struct host_cmd_ds_command *cmd,
612                                    u16 cmd_action, u32 cmd_oid,
613                                    struct mwifiex_ds_encrypt_key *enc_key)
614 {
615         struct mwifiex_adapter *adapter = priv->adapter;
616         u8 *mac = enc_key->mac_addr;
617         u16 key_info, len = KEY_PARAMS_FIXED_LEN;
618         struct host_cmd_ds_802_11_key_material_v2 *km =
619                                                 &cmd->params.key_material_v2;
620
621         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_KEY_MATERIAL);
622         km->action = cpu_to_le16(cmd_action);
623
624         if (cmd_action == HostCmd_ACT_GEN_GET) {
625                 mwifiex_dbg(adapter, INFO, "%s: Get key\n", __func__);
626                 km->key_param_set.key_idx =
627                                         enc_key->key_index & KEY_INDEX_MASK;
628                 km->key_param_set.type = cpu_to_le16(TLV_TYPE_KEY_PARAM_V2);
629                 km->key_param_set.len = cpu_to_le16(KEY_PARAMS_FIXED_LEN);
630                 memcpy(km->key_param_set.mac_addr, mac, ETH_ALEN);
631
632                 if (enc_key->key_index & MWIFIEX_KEY_INDEX_UNICAST)
633                         key_info = KEY_UNICAST;
634                 else
635                         key_info = KEY_MCAST;
636
637                 if (enc_key->is_igtk_key)
638                         key_info |= KEY_IGTK;
639
640                 km->key_param_set.key_info = cpu_to_le16(key_info);
641
642                 cmd->size = cpu_to_le16(sizeof(struct mwifiex_ie_types_header) +
643                                         S_DS_GEN + KEY_PARAMS_FIXED_LEN +
644                                         sizeof(km->action));
645                 return 0;
646         }
647
648         memset(&km->key_param_set, 0,
649                sizeof(struct mwifiex_ie_type_key_param_set_v2));
650
651         if (enc_key->key_disable) {
652                 mwifiex_dbg(adapter, INFO, "%s: Remove key\n", __func__);
653                 km->action = cpu_to_le16(HostCmd_ACT_GEN_REMOVE);
654                 km->key_param_set.type = cpu_to_le16(TLV_TYPE_KEY_PARAM_V2);
655                 km->key_param_set.len = cpu_to_le16(KEY_PARAMS_FIXED_LEN);
656                 km->key_param_set.key_idx = enc_key->key_index & KEY_INDEX_MASK;
657                 key_info = KEY_MCAST | KEY_UNICAST;
658                 km->key_param_set.key_info = cpu_to_le16(key_info);
659                 memcpy(km->key_param_set.mac_addr, mac, ETH_ALEN);
660                 cmd->size = cpu_to_le16(sizeof(struct mwifiex_ie_types_header) +
661                                         S_DS_GEN + KEY_PARAMS_FIXED_LEN +
662                                         sizeof(km->action));
663                 return 0;
664         }
665
666         km->action = cpu_to_le16(HostCmd_ACT_GEN_SET);
667         km->key_param_set.key_idx = enc_key->key_index & KEY_INDEX_MASK;
668         km->key_param_set.type = cpu_to_le16(TLV_TYPE_KEY_PARAM_V2);
669         key_info = KEY_ENABLED;
670         memcpy(km->key_param_set.mac_addr, mac, ETH_ALEN);
671
672         if (enc_key->key_len <= WLAN_KEY_LEN_WEP104) {
673                 mwifiex_dbg(adapter, INFO, "%s: Set WEP Key\n", __func__);
674                 len += sizeof(struct mwifiex_wep_param);
675                 km->key_param_set.len = cpu_to_le16(len);
676                 km->key_param_set.key_type = KEY_TYPE_ID_WEP;
677
678                 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
679                                 key_info |= KEY_MCAST | KEY_UNICAST;
680                 } else {
681                         if (enc_key->is_current_wep_key) {
682                                 key_info |= KEY_MCAST | KEY_UNICAST;
683                                 if (km->key_param_set.key_idx ==
684                                     (priv->wep_key_curr_index & KEY_INDEX_MASK))
685                                         key_info |= KEY_DEFAULT;
686                         } else {
687                                 if (mac) {
688                                         if (is_broadcast_ether_addr(mac))
689                                                 key_info |= KEY_MCAST;
690                                         else
691                                                 key_info |= KEY_UNICAST |
692                                                             KEY_DEFAULT;
693                                 } else {
694                                         key_info |= KEY_MCAST;
695                                 }
696                         }
697                 }
698                 km->key_param_set.key_info = cpu_to_le16(key_info);
699
700                 km->key_param_set.key_params.wep.key_len =
701                                                   cpu_to_le16(enc_key->key_len);
702                 memcpy(km->key_param_set.key_params.wep.key,
703                        enc_key->key_material, enc_key->key_len);
704
705                 cmd->size = cpu_to_le16(sizeof(struct mwifiex_ie_types_header) +
706                                         len + sizeof(km->action) + S_DS_GEN);
707                 return 0;
708         }
709
710         if (is_broadcast_ether_addr(mac))
711                 key_info |= KEY_MCAST | KEY_RX_KEY;
712         else
713                 key_info |= KEY_UNICAST | KEY_TX_KEY | KEY_RX_KEY;
714
715         if (enc_key->is_wapi_key) {
716                 mwifiex_dbg(adapter, INFO, "%s: Set WAPI Key\n", __func__);
717                 km->key_param_set.key_type = KEY_TYPE_ID_WAPI;
718                 memcpy(km->key_param_set.key_params.wapi.pn, enc_key->pn,
719                        PN_LEN);
720                 km->key_param_set.key_params.wapi.key_len =
721                                                 cpu_to_le16(enc_key->key_len);
722                 memcpy(km->key_param_set.key_params.wapi.key,
723                        enc_key->key_material, enc_key->key_len);
724                 if (is_broadcast_ether_addr(mac))
725                         priv->sec_info.wapi_key_on = true;
726
727                 if (!priv->sec_info.wapi_key_on)
728                         key_info |= KEY_DEFAULT;
729                 km->key_param_set.key_info = cpu_to_le16(key_info);
730
731                 len += sizeof(struct mwifiex_wapi_param);
732                 km->key_param_set.len = cpu_to_le16(len);
733                 cmd->size = cpu_to_le16(sizeof(struct mwifiex_ie_types_header) +
734                                         len + sizeof(km->action) + S_DS_GEN);
735                 return 0;
736         }
737
738         if (priv->bss_mode == NL80211_IFTYPE_ADHOC) {
739                 key_info |= KEY_DEFAULT;
740                 /* Enable unicast bit for WPA-NONE/ADHOC_AES */
741                 if (!priv->sec_info.wpa2_enabled &&
742                     !is_broadcast_ether_addr(mac))
743                         key_info |= KEY_UNICAST;
744         } else {
745                 /* Enable default key for WPA/WPA2 */
746                 if (!priv->wpa_is_gtk_set)
747                         key_info |= KEY_DEFAULT;
748         }
749
750         km->key_param_set.key_info = cpu_to_le16(key_info);
751
752         if (enc_key->key_len == WLAN_KEY_LEN_CCMP)
753                 return mwifiex_set_aes_key_v2(priv, cmd, enc_key, km);
754
755         if (enc_key->key_len == WLAN_KEY_LEN_TKIP) {
756                 mwifiex_dbg(adapter, INFO,
757                             "%s: Set TKIP Key\n", __func__);
758                 if (enc_key->is_rx_seq_valid)
759                         memcpy(km->key_param_set.key_params.tkip.pn,
760                                enc_key->pn, enc_key->pn_len);
761                 km->key_param_set.key_type = KEY_TYPE_ID_TKIP;
762                 km->key_param_set.key_params.tkip.key_len =
763                                                 cpu_to_le16(enc_key->key_len);
764                 memcpy(km->key_param_set.key_params.tkip.key,
765                        enc_key->key_material, enc_key->key_len);
766
767                 len += sizeof(struct mwifiex_tkip_param);
768                 km->key_param_set.len = cpu_to_le16(len);
769                 cmd->size = cpu_to_le16(sizeof(struct mwifiex_ie_types_header) +
770                                         len + sizeof(km->action) + S_DS_GEN);
771         }
772
773         return 0;
774 }
775
776 /*
777  * This function prepares command to set/get/reset network key(s).
778  * This function prepares key material command for V1 format.
779  *
780  * Preparation includes -
781  *      - Setting command ID, action and proper size
782  *      - Setting WEP keys, WAPI keys or WPA keys along with required
783  *        encryption (TKIP, AES) (as required)
784  *      - Ensuring correct endian-ness
785  */
786 static int
787 mwifiex_cmd_802_11_key_material_v1(struct mwifiex_private *priv,
788                                    struct host_cmd_ds_command *cmd,
789                                    u16 cmd_action, u32 cmd_oid,
790                                    struct mwifiex_ds_encrypt_key *enc_key)
791 {
792         struct host_cmd_ds_802_11_key_material *key_material =
793                 &cmd->params.key_material;
794         struct host_cmd_tlv_mac_addr *tlv_mac;
795         u16 key_param_len = 0, cmd_size;
796         int ret = 0;
797
798         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_KEY_MATERIAL);
799         key_material->action = cpu_to_le16(cmd_action);
800
801         if (cmd_action == HostCmd_ACT_GEN_GET) {
802                 cmd->size =
803                         cpu_to_le16(sizeof(key_material->action) + S_DS_GEN);
804                 return ret;
805         }
806
807         if (!enc_key) {
808                 memset(&key_material->key_param_set, 0,
809                        (NUM_WEP_KEYS *
810                         sizeof(struct mwifiex_ie_type_key_param_set)));
811                 ret = mwifiex_set_keyparamset_wep(priv,
812                                                   &key_material->key_param_set,
813                                                   &key_param_len);
814                 cmd->size = cpu_to_le16(key_param_len +
815                                     sizeof(key_material->action) + S_DS_GEN);
816                 return ret;
817         } else
818                 memset(&key_material->key_param_set, 0,
819                        sizeof(struct mwifiex_ie_type_key_param_set));
820         if (enc_key->is_wapi_key) {
821                 mwifiex_dbg(priv->adapter, INFO, "info: Set WAPI Key\n");
822                 key_material->key_param_set.key_type_id =
823                                                 cpu_to_le16(KEY_TYPE_ID_WAPI);
824                 if (cmd_oid == KEY_INFO_ENABLED)
825                         key_material->key_param_set.key_info =
826                                                 cpu_to_le16(KEY_ENABLED);
827                 else
828                         key_material->key_param_set.key_info =
829                                                 cpu_to_le16(!KEY_ENABLED);
830
831                 key_material->key_param_set.key[0] = enc_key->key_index;
832                 if (!priv->sec_info.wapi_key_on)
833                         key_material->key_param_set.key[1] = 1;
834                 else
835                         /* set 0 when re-key */
836                         key_material->key_param_set.key[1] = 0;
837
838                 if (!is_broadcast_ether_addr(enc_key->mac_addr)) {
839                         /* WAPI pairwise key: unicast */
840                         key_material->key_param_set.key_info |=
841                                 cpu_to_le16(KEY_UNICAST);
842                 } else {        /* WAPI group key: multicast */
843                         key_material->key_param_set.key_info |=
844                                 cpu_to_le16(KEY_MCAST);
845                         priv->sec_info.wapi_key_on = true;
846                 }
847
848                 key_material->key_param_set.type =
849                                         cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
850                 key_material->key_param_set.key_len =
851                                                 cpu_to_le16(WAPI_KEY_LEN);
852                 memcpy(&key_material->key_param_set.key[2],
853                        enc_key->key_material, enc_key->key_len);
854                 memcpy(&key_material->key_param_set.key[2 + enc_key->key_len],
855                        enc_key->pn, PN_LEN);
856                 key_material->key_param_set.length =
857                         cpu_to_le16(WAPI_KEY_LEN + KEYPARAMSET_FIXED_LEN);
858
859                 key_param_len = (WAPI_KEY_LEN + KEYPARAMSET_FIXED_LEN) +
860                                  sizeof(struct mwifiex_ie_types_header);
861                 cmd->size = cpu_to_le16(sizeof(key_material->action)
862                                         + S_DS_GEN +  key_param_len);
863                 return ret;
864         }
865         if (enc_key->key_len == WLAN_KEY_LEN_CCMP) {
866                 if (enc_key->is_igtk_key) {
867                         mwifiex_dbg(priv->adapter, CMD, "cmd: CMAC_AES\n");
868                         key_material->key_param_set.key_type_id =
869                                         cpu_to_le16(KEY_TYPE_ID_AES_CMAC);
870                         if (cmd_oid == KEY_INFO_ENABLED)
871                                 key_material->key_param_set.key_info =
872                                                 cpu_to_le16(KEY_ENABLED);
873                         else
874                                 key_material->key_param_set.key_info =
875                                                 cpu_to_le16(!KEY_ENABLED);
876
877                         key_material->key_param_set.key_info |=
878                                                         cpu_to_le16(KEY_IGTK);
879                 } else {
880                         mwifiex_dbg(priv->adapter, CMD, "cmd: WPA_AES\n");
881                         key_material->key_param_set.key_type_id =
882                                                 cpu_to_le16(KEY_TYPE_ID_AES);
883                         if (cmd_oid == KEY_INFO_ENABLED)
884                                 key_material->key_param_set.key_info =
885                                                 cpu_to_le16(KEY_ENABLED);
886                         else
887                                 key_material->key_param_set.key_info =
888                                                 cpu_to_le16(!KEY_ENABLED);
889
890                         if (enc_key->key_index & MWIFIEX_KEY_INDEX_UNICAST)
891                                 /* AES pairwise key: unicast */
892                                 key_material->key_param_set.key_info |=
893                                                 cpu_to_le16(KEY_UNICAST);
894                         else    /* AES group key: multicast */
895                                 key_material->key_param_set.key_info |=
896                                                         cpu_to_le16(KEY_MCAST);
897                 }
898         } else if (enc_key->key_len == WLAN_KEY_LEN_TKIP) {
899                 mwifiex_dbg(priv->adapter, CMD, "cmd: WPA_TKIP\n");
900                 key_material->key_param_set.key_type_id =
901                                                 cpu_to_le16(KEY_TYPE_ID_TKIP);
902                 key_material->key_param_set.key_info =
903                                                 cpu_to_le16(KEY_ENABLED);
904
905                 if (enc_key->key_index & MWIFIEX_KEY_INDEX_UNICAST)
906                                 /* TKIP pairwise key: unicast */
907                         key_material->key_param_set.key_info |=
908                                                 cpu_to_le16(KEY_UNICAST);
909                 else            /* TKIP group key: multicast */
910                         key_material->key_param_set.key_info |=
911                                                         cpu_to_le16(KEY_MCAST);
912         }
913
914         if (key_material->key_param_set.key_type_id) {
915                 key_material->key_param_set.type =
916                                         cpu_to_le16(TLV_TYPE_KEY_MATERIAL);
917                 key_material->key_param_set.key_len =
918                                         cpu_to_le16((u16) enc_key->key_len);
919                 memcpy(key_material->key_param_set.key, enc_key->key_material,
920                        enc_key->key_len);
921                 key_material->key_param_set.length =
922                         cpu_to_le16((u16) enc_key->key_len +
923                                     KEYPARAMSET_FIXED_LEN);
924
925                 key_param_len = (u16)(enc_key->key_len + KEYPARAMSET_FIXED_LEN)
926                                 + sizeof(struct mwifiex_ie_types_header);
927
928                 if (le16_to_cpu(key_material->key_param_set.key_type_id) ==
929                                                         KEY_TYPE_ID_AES_CMAC) {
930                         struct mwifiex_cmac_param *param =
931                                         (void *)key_material->key_param_set.key;
932
933                         memcpy(param->ipn, enc_key->pn, IGTK_PN_LEN);
934                         memcpy(param->key, enc_key->key_material,
935                                WLAN_KEY_LEN_AES_CMAC);
936
937                         key_param_len = sizeof(struct mwifiex_cmac_param);
938                         key_material->key_param_set.key_len =
939                                                 cpu_to_le16(key_param_len);
940                         key_param_len += KEYPARAMSET_FIXED_LEN;
941                         key_material->key_param_set.length =
942                                                 cpu_to_le16(key_param_len);
943                         key_param_len += sizeof(struct mwifiex_ie_types_header);
944                 }
945
946                 cmd->size = cpu_to_le16(sizeof(key_material->action) + S_DS_GEN
947                                         + key_param_len);
948
949                 if (GET_BSS_ROLE(priv) == MWIFIEX_BSS_ROLE_UAP) {
950                         tlv_mac = (void *)((u8 *)&key_material->key_param_set +
951                                            key_param_len);
952                         tlv_mac->header.type =
953                                         cpu_to_le16(TLV_TYPE_STA_MAC_ADDR);
954                         tlv_mac->header.len = cpu_to_le16(ETH_ALEN);
955                         memcpy(tlv_mac->mac_addr, enc_key->mac_addr, ETH_ALEN);
956                         cmd_size = key_param_len + S_DS_GEN +
957                                    sizeof(key_material->action) +
958                                    sizeof(struct host_cmd_tlv_mac_addr);
959                 } else {
960                         cmd_size = key_param_len + S_DS_GEN +
961                                    sizeof(key_material->action);
962                 }
963                 cmd->size = cpu_to_le16(cmd_size);
964         }
965
966         return ret;
967 }
968
969 /* Wrapper function for setting network key depending upon FW KEY API version */
970 static int
971 mwifiex_cmd_802_11_key_material(struct mwifiex_private *priv,
972                                 struct host_cmd_ds_command *cmd,
973                                 u16 cmd_action, u32 cmd_oid,
974                                 struct mwifiex_ds_encrypt_key *enc_key)
975 {
976         if (priv->adapter->key_api_major_ver == KEY_API_VER_MAJOR_V2)
977                 return mwifiex_cmd_802_11_key_material_v2(priv, cmd,
978                                                           cmd_action, cmd_oid,
979                                                           enc_key);
980
981         else
982                 return mwifiex_cmd_802_11_key_material_v1(priv, cmd,
983                                                           cmd_action, cmd_oid,
984                                                           enc_key);
985 }
986
987 /*
988  * This function prepares command to set/get 11d domain information.
989  *
990  * Preparation includes -
991  *      - Setting command ID, action and proper size
992  *      - Setting domain information fields (for SET only)
993  *      - Ensuring correct endian-ness
994  */
995 static int mwifiex_cmd_802_11d_domain_info(struct mwifiex_private *priv,
996                                            struct host_cmd_ds_command *cmd,
997                                            u16 cmd_action)
998 {
999         struct mwifiex_adapter *adapter = priv->adapter;
1000         struct host_cmd_ds_802_11d_domain_info *domain_info =
1001                 &cmd->params.domain_info;
1002         struct mwifiex_ietypes_domain_param_set *domain =
1003                 &domain_info->domain;
1004         u8 no_of_triplet = adapter->domain_reg.no_of_triplet;
1005
1006         mwifiex_dbg(adapter, INFO,
1007                     "info: 11D: no_of_triplet=0x%x\n", no_of_triplet);
1008
1009         cmd->command = cpu_to_le16(HostCmd_CMD_802_11D_DOMAIN_INFO);
1010         domain_info->action = cpu_to_le16(cmd_action);
1011         if (cmd_action == HostCmd_ACT_GEN_GET) {
1012                 cmd->size = cpu_to_le16(sizeof(domain_info->action) + S_DS_GEN);
1013                 return 0;
1014         }
1015
1016         /* Set domain info fields */
1017         domain->header.type = cpu_to_le16(WLAN_EID_COUNTRY);
1018         memcpy(domain->country_code, adapter->domain_reg.country_code,
1019                sizeof(domain->country_code));
1020
1021         domain->header.len =
1022                 cpu_to_le16((no_of_triplet *
1023                              sizeof(struct ieee80211_country_ie_triplet))
1024                             + sizeof(domain->country_code));
1025
1026         if (no_of_triplet) {
1027                 memcpy(domain->triplet, adapter->domain_reg.triplet,
1028                        no_of_triplet * sizeof(struct
1029                                               ieee80211_country_ie_triplet));
1030
1031                 cmd->size = cpu_to_le16(sizeof(domain_info->action) +
1032                                         le16_to_cpu(domain->header.len) +
1033                                         sizeof(struct mwifiex_ie_types_header)
1034                                         + S_DS_GEN);
1035         } else {
1036                 cmd->size = cpu_to_le16(sizeof(domain_info->action) + S_DS_GEN);
1037         }
1038
1039         return 0;
1040 }
1041
1042 /*
1043  * This function prepares command to set/get IBSS coalescing status.
1044  *
1045  * Preparation includes -
1046  *      - Setting command ID, action and proper size
1047  *      - Setting status to enable or disable (for SET only)
1048  *      - Ensuring correct endian-ness
1049  */
1050 static int mwifiex_cmd_ibss_coalescing_status(struct host_cmd_ds_command *cmd,
1051                                               u16 cmd_action, u16 *enable)
1052 {
1053         struct host_cmd_ds_802_11_ibss_status *ibss_coal =
1054                 &(cmd->params.ibss_coalescing);
1055
1056         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_IBSS_COALESCING_STATUS);
1057         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_ibss_status) +
1058                                 S_DS_GEN);
1059         cmd->result = 0;
1060         ibss_coal->action = cpu_to_le16(cmd_action);
1061
1062         switch (cmd_action) {
1063         case HostCmd_ACT_GEN_SET:
1064                 if (enable)
1065                         ibss_coal->enable = cpu_to_le16(*enable);
1066                 else
1067                         ibss_coal->enable = 0;
1068                 break;
1069
1070                 /* In other case.. Nothing to do */
1071         case HostCmd_ACT_GEN_GET:
1072         default:
1073                 break;
1074         }
1075
1076         return 0;
1077 }
1078
1079 /* This function prepares command buffer to get/set memory location value.
1080  */
1081 static int
1082 mwifiex_cmd_mem_access(struct host_cmd_ds_command *cmd, u16 cmd_action,
1083                        void *pdata_buf)
1084 {
1085         struct mwifiex_ds_mem_rw *mem_rw = (void *)pdata_buf;
1086         struct host_cmd_ds_mem_access *mem_access = (void *)&cmd->params.mem;
1087
1088         cmd->command = cpu_to_le16(HostCmd_CMD_MEM_ACCESS);
1089         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_mem_access) +
1090                                 S_DS_GEN);
1091
1092         mem_access->action = cpu_to_le16(cmd_action);
1093         mem_access->addr = cpu_to_le32(mem_rw->addr);
1094         mem_access->value = cpu_to_le32(mem_rw->value);
1095
1096         return 0;
1097 }
1098
1099 /*
1100  * This function prepares command to set/get register value.
1101  *
1102  * Preparation includes -
1103  *      - Setting command ID, action and proper size
1104  *      - Setting register offset (for both GET and SET) and
1105  *        register value (for SET only)
1106  *      - Ensuring correct endian-ness
1107  *
1108  * The following type of registers can be accessed with this function -
1109  *      - MAC register
1110  *      - BBP register
1111  *      - RF register
1112  *      - PMIC register
1113  *      - CAU register
1114  *      - EEPROM
1115  */
1116 static int mwifiex_cmd_reg_access(struct host_cmd_ds_command *cmd,
1117                                   u16 cmd_action, void *data_buf)
1118 {
1119         struct mwifiex_ds_reg_rw *reg_rw = data_buf;
1120
1121         switch (le16_to_cpu(cmd->command)) {
1122         case HostCmd_CMD_MAC_REG_ACCESS:
1123         {
1124                 struct host_cmd_ds_mac_reg_access *mac_reg;
1125
1126                 cmd->size = cpu_to_le16(sizeof(*mac_reg) + S_DS_GEN);
1127                 mac_reg = &cmd->params.mac_reg;
1128                 mac_reg->action = cpu_to_le16(cmd_action);
1129                 mac_reg->offset =
1130                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
1131                 mac_reg->value = reg_rw->value;
1132                 break;
1133         }
1134         case HostCmd_CMD_BBP_REG_ACCESS:
1135         {
1136                 struct host_cmd_ds_bbp_reg_access *bbp_reg;
1137
1138                 cmd->size = cpu_to_le16(sizeof(*bbp_reg) + S_DS_GEN);
1139                 bbp_reg = &cmd->params.bbp_reg;
1140                 bbp_reg->action = cpu_to_le16(cmd_action);
1141                 bbp_reg->offset =
1142                         cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
1143                 bbp_reg->value = (u8) le32_to_cpu(reg_rw->value);
1144                 break;
1145         }
1146         case HostCmd_CMD_RF_REG_ACCESS:
1147         {
1148                 struct host_cmd_ds_rf_reg_access *rf_reg;
1149
1150                 cmd->size = cpu_to_le16(sizeof(*rf_reg) + S_DS_GEN);
1151                 rf_reg = &cmd->params.rf_reg;
1152                 rf_reg->action = cpu_to_le16(cmd_action);
1153                 rf_reg->offset = cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
1154                 rf_reg->value = (u8) le32_to_cpu(reg_rw->value);
1155                 break;
1156         }
1157         case HostCmd_CMD_PMIC_REG_ACCESS:
1158         {
1159                 struct host_cmd_ds_pmic_reg_access *pmic_reg;
1160
1161                 cmd->size = cpu_to_le16(sizeof(*pmic_reg) + S_DS_GEN);
1162                 pmic_reg = &cmd->params.pmic_reg;
1163                 pmic_reg->action = cpu_to_le16(cmd_action);
1164                 pmic_reg->offset =
1165                                 cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
1166                 pmic_reg->value = (u8) le32_to_cpu(reg_rw->value);
1167                 break;
1168         }
1169         case HostCmd_CMD_CAU_REG_ACCESS:
1170         {
1171                 struct host_cmd_ds_rf_reg_access *cau_reg;
1172
1173                 cmd->size = cpu_to_le16(sizeof(*cau_reg) + S_DS_GEN);
1174                 cau_reg = &cmd->params.rf_reg;
1175                 cau_reg->action = cpu_to_le16(cmd_action);
1176                 cau_reg->offset =
1177                                 cpu_to_le16((u16) le32_to_cpu(reg_rw->offset));
1178                 cau_reg->value = (u8) le32_to_cpu(reg_rw->value);
1179                 break;
1180         }
1181         case HostCmd_CMD_802_11_EEPROM_ACCESS:
1182         {
1183                 struct mwifiex_ds_read_eeprom *rd_eeprom = data_buf;
1184                 struct host_cmd_ds_802_11_eeprom_access *cmd_eeprom =
1185                         &cmd->params.eeprom;
1186
1187                 cmd->size = cpu_to_le16(sizeof(*cmd_eeprom) + S_DS_GEN);
1188                 cmd_eeprom->action = cpu_to_le16(cmd_action);
1189                 cmd_eeprom->offset = rd_eeprom->offset;
1190                 cmd_eeprom->byte_count = rd_eeprom->byte_count;
1191                 cmd_eeprom->value = 0;
1192                 break;
1193         }
1194         default:
1195                 return -1;
1196         }
1197
1198         return 0;
1199 }
1200
1201 /*
1202  * This function prepares command to set PCI-Express
1203  * host buffer configuration
1204  *
1205  * Preparation includes -
1206  *      - Setting command ID, action and proper size
1207  *      - Setting host buffer configuration
1208  *      - Ensuring correct endian-ness
1209  */
1210 static int
1211 mwifiex_cmd_pcie_host_spec(struct mwifiex_private *priv,
1212                            struct host_cmd_ds_command *cmd, u16 action)
1213 {
1214         struct host_cmd_ds_pcie_details *host_spec =
1215                                         &cmd->params.pcie_host_spec;
1216         struct pcie_service_card *card = priv->adapter->card;
1217
1218         cmd->command = cpu_to_le16(HostCmd_CMD_PCIE_DESC_DETAILS);
1219         cmd->size = cpu_to_le16(sizeof(struct
1220                                         host_cmd_ds_pcie_details) + S_DS_GEN);
1221         cmd->result = 0;
1222
1223         memset(host_spec, 0, sizeof(struct host_cmd_ds_pcie_details));
1224
1225         if (action != HostCmd_ACT_GEN_SET)
1226                 return 0;
1227
1228         /* Send the ring base addresses and count to firmware */
1229         host_spec->txbd_addr_lo = (u32)(card->txbd_ring_pbase);
1230         host_spec->txbd_addr_hi = (u32)(((u64)card->txbd_ring_pbase)>>32);
1231         host_spec->txbd_count = MWIFIEX_MAX_TXRX_BD;
1232         host_spec->rxbd_addr_lo = (u32)(card->rxbd_ring_pbase);
1233         host_spec->rxbd_addr_hi = (u32)(((u64)card->rxbd_ring_pbase)>>32);
1234         host_spec->rxbd_count = MWIFIEX_MAX_TXRX_BD;
1235         host_spec->evtbd_addr_lo = (u32)(card->evtbd_ring_pbase);
1236         host_spec->evtbd_addr_hi = (u32)(((u64)card->evtbd_ring_pbase)>>32);
1237         host_spec->evtbd_count = MWIFIEX_MAX_EVT_BD;
1238         if (card->sleep_cookie_vbase) {
1239                 host_spec->sleep_cookie_addr_lo =
1240                                                 (u32)(card->sleep_cookie_pbase);
1241                 host_spec->sleep_cookie_addr_hi =
1242                                  (u32)(((u64)(card->sleep_cookie_pbase)) >> 32);
1243                 mwifiex_dbg(priv->adapter, INFO,
1244                             "sleep_cook_lo phy addr: 0x%x\n",
1245                             host_spec->sleep_cookie_addr_lo);
1246         }
1247
1248         return 0;
1249 }
1250
1251 /*
1252  * This function prepares command for event subscription, configuration
1253  * and query. Events can be subscribed or unsubscribed. Current subscribed
1254  * events can be queried. Also, current subscribed events are reported in
1255  * every FW response.
1256  */
1257 static int
1258 mwifiex_cmd_802_11_subsc_evt(struct mwifiex_private *priv,
1259                              struct host_cmd_ds_command *cmd,
1260                              struct mwifiex_ds_misc_subsc_evt *subsc_evt_cfg)
1261 {
1262         struct host_cmd_ds_802_11_subsc_evt *subsc_evt = &cmd->params.subsc_evt;
1263         struct mwifiex_ie_types_rssi_threshold *rssi_tlv;
1264         u16 event_bitmap;
1265         u8 *pos;
1266
1267         cmd->command = cpu_to_le16(HostCmd_CMD_802_11_SUBSCRIBE_EVENT);
1268         cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_802_11_subsc_evt) +
1269                                 S_DS_GEN);
1270
1271         subsc_evt->action = cpu_to_le16(subsc_evt_cfg->action);
1272         mwifiex_dbg(priv->adapter, CMD,
1273                     "cmd: action: %d\n", subsc_evt_cfg->action);
1274
1275         /*For query requests, no configuration TLV structures are to be added.*/
1276         if (subsc_evt_cfg->action == HostCmd_ACT_GEN_GET)
1277                 return 0;
1278
1279         subsc_evt->events = cpu_to_le16(subsc_evt_cfg->events);
1280
1281         event_bitmap = subsc_evt_cfg->events;
1282         mwifiex_dbg(priv->adapter, CMD, "cmd: event bitmap : %16x\n",
1283                     event_bitmap);
1284
1285         if (((subsc_evt_cfg->action == HostCmd_ACT_BITWISE_CLR) ||
1286              (subsc_evt_cfg->action == HostCmd_ACT_BITWISE_SET)) &&
1287             (event_bitmap == 0)) {
1288                 mwifiex_dbg(priv->adapter, ERROR,
1289                             "Error: No event specified\t"
1290                             "for bitwise action type\n");
1291                 return -EINVAL;
1292         }
1293
1294         /*
1295          * Append TLV structures for each of the specified events for
1296          * subscribing or re-configuring. This is not required for
1297          * bitwise unsubscribing request.
1298          */
1299         if (subsc_evt_cfg->action == HostCmd_ACT_BITWISE_CLR)
1300                 return 0;
1301
1302         pos = ((u8 *)subsc_evt) +
1303                         sizeof(struct host_cmd_ds_802_11_subsc_evt);
1304
1305         if (event_bitmap & BITMASK_BCN_RSSI_LOW) {
1306                 rssi_tlv = (struct mwifiex_ie_types_rssi_threshold *) pos;
1307
1308                 rssi_tlv->header.type = cpu_to_le16(TLV_TYPE_RSSI_LOW);
1309                 rssi_tlv->header.len =
1310                     cpu_to_le16(sizeof(struct mwifiex_ie_types_rssi_threshold) -
1311                                 sizeof(struct mwifiex_ie_types_header));
1312                 rssi_tlv->abs_value = subsc_evt_cfg->bcn_l_rssi_cfg.abs_value;
1313                 rssi_tlv->evt_freq = subsc_evt_cfg->bcn_l_rssi_cfg.evt_freq;
1314
1315                 mwifiex_dbg(priv->adapter, EVENT,
1316                             "Cfg Beacon Low Rssi event,\t"
1317                             "RSSI:-%d dBm, Freq:%d\n",
1318                             subsc_evt_cfg->bcn_l_rssi_cfg.abs_value,
1319                             subsc_evt_cfg->bcn_l_rssi_cfg.evt_freq);
1320
1321                 pos += sizeof(struct mwifiex_ie_types_rssi_threshold);
1322                 le16_add_cpu(&cmd->size,
1323                              sizeof(struct mwifiex_ie_types_rssi_threshold));
1324         }
1325
1326         if (event_bitmap & BITMASK_BCN_RSSI_HIGH) {
1327                 rssi_tlv = (struct mwifiex_ie_types_rssi_threshold *) pos;
1328
1329                 rssi_tlv->header.type = cpu_to_le16(TLV_TYPE_RSSI_HIGH);
1330                 rssi_tlv->header.len =
1331                     cpu_to_le16(sizeof(struct mwifiex_ie_types_rssi_threshold) -
1332                                 sizeof(struct mwifiex_ie_types_header));
1333                 rssi_tlv->abs_value = subsc_evt_cfg->bcn_h_rssi_cfg.abs_value;
1334                 rssi_tlv->evt_freq = subsc_evt_cfg->bcn_h_rssi_cfg.evt_freq;
1335
1336                 mwifiex_dbg(priv->adapter, EVENT,
1337                             "Cfg Beacon High Rssi event,\t"
1338                             "RSSI:-%d dBm, Freq:%d\n",
1339                             subsc_evt_cfg->bcn_h_rssi_cfg.abs_value,
1340                             subsc_evt_cfg->bcn_h_rssi_cfg.evt_freq);
1341
1342                 pos += sizeof(struct mwifiex_ie_types_rssi_threshold);
1343                 le16_add_cpu(&cmd->size,
1344                              sizeof(struct mwifiex_ie_types_rssi_threshold));
1345         }
1346
1347         return 0;
1348 }
1349
1350 static int
1351 mwifiex_cmd_append_rpn_expression(struct mwifiex_private *priv,
1352                                   struct mwifiex_mef_entry *mef_entry,
1353                                   u8 **buffer)
1354 {
1355         struct mwifiex_mef_filter *filter = mef_entry->filter;
1356         int i, byte_len;
1357         u8 *stack_ptr = *buffer;
1358
1359         for (i = 0; i < MWIFIEX_MEF_MAX_FILTERS; i++) {
1360                 filter = &mef_entry->filter[i];
1361                 if (!filter->filt_type)
1362                         break;
1363                 *(__le32 *)stack_ptr = cpu_to_le32((u32)filter->repeat);
1364                 stack_ptr += 4;
1365                 *stack_ptr = TYPE_DNUM;
1366                 stack_ptr += 1;
1367
1368                 byte_len = filter->byte_seq[MWIFIEX_MEF_MAX_BYTESEQ];
1369                 memcpy(stack_ptr, filter->byte_seq, byte_len);
1370                 stack_ptr += byte_len;
1371                 *stack_ptr = byte_len;
1372                 stack_ptr += 1;
1373                 *stack_ptr = TYPE_BYTESEQ;
1374                 stack_ptr += 1;
1375
1376                 *(__le32 *)stack_ptr = cpu_to_le32((u32)filter->offset);
1377                 stack_ptr += 4;
1378                 *stack_ptr = TYPE_DNUM;
1379                 stack_ptr += 1;
1380
1381                 *stack_ptr = filter->filt_type;
1382                 stack_ptr += 1;
1383
1384                 if (filter->filt_action) {
1385                         *stack_ptr = filter->filt_action;
1386                         stack_ptr += 1;
1387                 }
1388
1389                 if (stack_ptr - *buffer > STACK_NBYTES)
1390                         return -1;
1391         }
1392
1393         *buffer = stack_ptr;
1394         return 0;
1395 }
1396
1397 static int
1398 mwifiex_cmd_mef_cfg(struct mwifiex_private *priv,
1399                     struct host_cmd_ds_command *cmd,
1400                     struct mwifiex_ds_mef_cfg *mef)
1401 {
1402         struct host_cmd_ds_mef_cfg *mef_cfg = &cmd->params.mef_cfg;
1403         struct mwifiex_fw_mef_entry *mef_entry = NULL;
1404         u8 *pos = (u8 *)mef_cfg;
1405         u16 i;
1406
1407         cmd->command = cpu_to_le16(HostCmd_CMD_MEF_CFG);
1408
1409         mef_cfg->criteria = cpu_to_le32(mef->criteria);
1410         mef_cfg->num_entries = cpu_to_le16(mef->num_entries);
1411         pos += sizeof(*mef_cfg);
1412
1413         for (i = 0; i < mef->num_entries; i++) {
1414                 mef_entry = (struct mwifiex_fw_mef_entry *)pos;
1415                 mef_entry->mode = mef->mef_entry[i].mode;
1416                 mef_entry->action = mef->mef_entry[i].action;
1417                 pos += sizeof(*mef_cfg->mef_entry);
1418
1419                 if (mwifiex_cmd_append_rpn_expression(priv,
1420                                                       &mef->mef_entry[i], &pos))
1421                         return -1;
1422
1423                 mef_entry->exprsize =
1424                         cpu_to_le16(pos - mef_entry->expr);
1425         }
1426         cmd->size = cpu_to_le16((u16) (pos - (u8 *)mef_cfg) + S_DS_GEN);
1427
1428         return 0;
1429 }
1430
1431 /* This function parse cal data from ASCII to hex */
1432 static u32 mwifiex_parse_cal_cfg(u8 *src, size_t len, u8 *dst)
1433 {
1434         u8 *s = src, *d = dst;
1435
1436         while (s - src < len) {
1437                 if (*s && (isspace(*s) || *s == '\t')) {
1438                         s++;
1439                         continue;
1440                 }
1441                 if (isxdigit(*s)) {
1442                         *d++ = simple_strtol(s, NULL, 16);
1443                         s += 2;
1444                 } else {
1445                         s++;
1446                 }
1447         }
1448
1449         return d - dst;
1450 }
1451
1452 int mwifiex_dnld_dt_cfgdata(struct mwifiex_private *priv,
1453                             struct device_node *node, const char *prefix)
1454 {
1455 #ifdef CONFIG_OF
1456         struct property *prop;
1457         size_t len = strlen(prefix);
1458         int ret;
1459
1460         /* look for all matching property names */
1461         for_each_property_of_node(node, prop) {
1462                 if (len > strlen(prop->name) ||
1463                     strncmp(prop->name, prefix, len))
1464                         continue;
1465
1466                 /* property header is 6 bytes, data must fit in cmd buffer */
1467                 if (prop && prop->value && prop->length > 6 &&
1468                     prop->length <= MWIFIEX_SIZE_OF_CMD_BUFFER - S_DS_GEN) {
1469                         ret = mwifiex_send_cmd(priv, HostCmd_CMD_CFG_DATA,
1470                                                HostCmd_ACT_GEN_SET, 0,
1471                                                prop, true);
1472                         if (ret)
1473                                 return ret;
1474                 }
1475         }
1476 #endif
1477         return 0;
1478 }
1479
1480 /* This function prepares command of set_cfg_data. */
1481 static int mwifiex_cmd_cfg_data(struct mwifiex_private *priv,
1482                                 struct host_cmd_ds_command *cmd, void *data_buf)
1483 {
1484         struct mwifiex_adapter *adapter = priv->adapter;
1485         struct property *prop = data_buf;
1486         u32 len;
1487         u8 *data = (u8 *)cmd + S_DS_GEN;
1488         int ret;
1489
1490         if (prop) {
1491                 len = prop->length;
1492                 ret = of_property_read_u8_array(adapter->dt_node, prop->name,
1493                                                 data, len);
1494                 if (ret)
1495                         return ret;
1496                 mwifiex_dbg(adapter, INFO,
1497                             "download cfg_data from device tree: %s\n",
1498                             prop->name);
1499         } else if (adapter->cal_data->data && adapter->cal_data->size > 0) {
1500                 len = mwifiex_parse_cal_cfg((u8 *)adapter->cal_data->data,
1501                                             adapter->cal_data->size, data);
1502                 mwifiex_dbg(adapter, INFO,
1503                             "download cfg_data from config file\n");
1504         } else {
1505                 return -1;
1506         }
1507
1508         cmd->command = cpu_to_le16(HostCmd_CMD_CFG_DATA);
1509         cmd->size = cpu_to_le16(S_DS_GEN + len);
1510
1511         return 0;
1512 }
1513
1514 static int
1515 mwifiex_cmd_coalesce_cfg(struct mwifiex_private *priv,
1516                          struct host_cmd_ds_command *cmd,
1517                          u16 cmd_action, void *data_buf)
1518 {
1519         struct host_cmd_ds_coalesce_cfg *coalesce_cfg =
1520                                                 &cmd->params.coalesce_cfg;
1521         struct mwifiex_ds_coalesce_cfg *cfg = data_buf;
1522         struct coalesce_filt_field_param *param;
1523         u16 cnt, idx, length;
1524         struct coalesce_receive_filt_rule *rule;
1525
1526         cmd->command = cpu_to_le16(HostCmd_CMD_COALESCE_CFG);
1527         cmd->size = cpu_to_le16(S_DS_GEN);
1528
1529         coalesce_cfg->action = cpu_to_le16(cmd_action);
1530         coalesce_cfg->num_of_rules = cpu_to_le16(cfg->num_of_rules);
1531         rule = coalesce_cfg->rule;
1532
1533         for (cnt = 0; cnt < cfg->num_of_rules; cnt++) {
1534                 rule->header.type = cpu_to_le16(TLV_TYPE_COALESCE_RULE);
1535                 rule->max_coalescing_delay =
1536                         cpu_to_le16(cfg->rule[cnt].max_coalescing_delay);
1537                 rule->pkt_type = cfg->rule[cnt].pkt_type;
1538                 rule->num_of_fields = cfg->rule[cnt].num_of_fields;
1539
1540                 length = 0;
1541
1542                 param = rule->params;
1543                 for (idx = 0; idx < cfg->rule[cnt].num_of_fields; idx++) {
1544                         param->operation = cfg->rule[cnt].params[idx].operation;
1545                         param->operand_len =
1546                                         cfg->rule[cnt].params[idx].operand_len;
1547                         param->offset =
1548                                 cpu_to_le16(cfg->rule[cnt].params[idx].offset);
1549                         memcpy(param->operand_byte_stream,
1550                                cfg->rule[cnt].params[idx].operand_byte_stream,
1551                                param->operand_len);
1552
1553                         length += sizeof(struct coalesce_filt_field_param);
1554
1555                         param++;
1556                 }
1557
1558                 /* Total rule length is sizeof max_coalescing_delay(u16),
1559                  * num_of_fields(u8), pkt_type(u8) and total length of the all
1560                  * params
1561                  */
1562                 rule->header.len = cpu_to_le16(length + sizeof(u16) +
1563                                                sizeof(u8) + sizeof(u8));
1564
1565                 /* Add the rule length to the command size*/
1566                 le16_add_cpu(&cmd->size, le16_to_cpu(rule->header.len) +
1567                              sizeof(struct mwifiex_ie_types_header));
1568
1569                 rule = (void *)((u8 *)rule->params + length);
1570         }
1571
1572         /* Add sizeof action, num_of_rules to total command length */
1573         le16_add_cpu(&cmd->size, sizeof(u16) + sizeof(u16));
1574
1575         return 0;
1576 }
1577
1578 static int
1579 mwifiex_cmd_tdls_oper(struct mwifiex_private *priv,
1580                       struct host_cmd_ds_command *cmd,
1581                       void *data_buf)
1582 {
1583         struct host_cmd_ds_tdls_oper *tdls_oper = &cmd->params.tdls_oper;
1584         struct mwifiex_ds_tdls_oper *oper = data_buf;
1585         struct mwifiex_sta_node *sta_ptr;
1586         struct host_cmd_tlv_rates *tlv_rates;
1587         struct mwifiex_ie_types_htcap *ht_capab;
1588         struct mwifiex_ie_types_qos_info *wmm_qos_info;
1589         struct mwifiex_ie_types_extcap *extcap;
1590         struct mwifiex_ie_types_vhtcap *vht_capab;
1591         struct mwifiex_ie_types_aid *aid;
1592         struct mwifiex_ie_types_tdls_idle_timeout *timeout;
1593         u8 *pos, qos_info;
1594         u16 config_len = 0;
1595         struct station_parameters *params = priv->sta_params;
1596
1597         cmd->command = cpu_to_le16(HostCmd_CMD_TDLS_OPER);
1598         cmd->size = cpu_to_le16(S_DS_GEN);
1599         le16_add_cpu(&cmd->size, sizeof(struct host_cmd_ds_tdls_oper));
1600
1601         tdls_oper->reason = 0;
1602         memcpy(tdls_oper->peer_mac, oper->peer_mac, ETH_ALEN);
1603         sta_ptr = mwifiex_get_sta_entry(priv, oper->peer_mac);
1604
1605         pos = (u8 *)tdls_oper + sizeof(struct host_cmd_ds_tdls_oper);
1606
1607         switch (oper->tdls_action) {
1608         case MWIFIEX_TDLS_DISABLE_LINK:
1609                 tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_DELETE);
1610                 break;
1611         case MWIFIEX_TDLS_CREATE_LINK:
1612                 tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_CREATE);
1613                 break;
1614         case MWIFIEX_TDLS_CONFIG_LINK:
1615                 tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_CONFIG);
1616
1617                 if (!params) {
1618                         mwifiex_dbg(priv->adapter, ERROR,
1619                                     "TDLS config params not available for %pM\n",
1620                                     oper->peer_mac);
1621                         return -ENODATA;
1622                 }
1623
1624                 *(__le16 *)pos = cpu_to_le16(params->capability);
1625                 config_len += sizeof(params->capability);
1626
1627                 qos_info = params->uapsd_queues | (params->max_sp << 5);
1628                 wmm_qos_info = (struct mwifiex_ie_types_qos_info *)(pos +
1629                                                                     config_len);
1630                 wmm_qos_info->header.type = cpu_to_le16(WLAN_EID_QOS_CAPA);
1631                 wmm_qos_info->header.len = cpu_to_le16(sizeof(qos_info));
1632                 wmm_qos_info->qos_info = qos_info;
1633                 config_len += sizeof(struct mwifiex_ie_types_qos_info);
1634
1635                 if (params->ht_capa) {
1636                         ht_capab = (struct mwifiex_ie_types_htcap *)(pos +
1637                                                                     config_len);
1638                         ht_capab->header.type =
1639                                             cpu_to_le16(WLAN_EID_HT_CAPABILITY);
1640                         ht_capab->header.len =
1641                                    cpu_to_le16(sizeof(struct ieee80211_ht_cap));
1642                         memcpy(&ht_capab->ht_cap, params->ht_capa,
1643                                sizeof(struct ieee80211_ht_cap));
1644                         config_len += sizeof(struct mwifiex_ie_types_htcap);
1645                 }
1646
1647                 if (params->supported_rates && params->supported_rates_len) {
1648                         tlv_rates = (struct host_cmd_tlv_rates *)(pos +
1649                                                                   config_len);
1650                         tlv_rates->header.type =
1651                                                cpu_to_le16(WLAN_EID_SUPP_RATES);
1652                         tlv_rates->header.len =
1653                                        cpu_to_le16(params->supported_rates_len);
1654                         memcpy(tlv_rates->rates, params->supported_rates,
1655                                params->supported_rates_len);
1656                         config_len += sizeof(struct host_cmd_tlv_rates) +
1657                                       params->supported_rates_len;
1658                 }
1659
1660                 if (params->ext_capab && params->ext_capab_len) {
1661                         extcap = (struct mwifiex_ie_types_extcap *)(pos +
1662                                                                     config_len);
1663                         extcap->header.type =
1664                                            cpu_to_le16(WLAN_EID_EXT_CAPABILITY);
1665                         extcap->header.len = cpu_to_le16(params->ext_capab_len);
1666                         memcpy(extcap->ext_capab, params->ext_capab,
1667                                params->ext_capab_len);
1668                         config_len += sizeof(struct mwifiex_ie_types_extcap) +
1669                                       params->ext_capab_len;
1670                 }
1671                 if (params->vht_capa) {
1672                         vht_capab = (struct mwifiex_ie_types_vhtcap *)(pos +
1673                                                                     config_len);
1674                         vht_capab->header.type =
1675                                            cpu_to_le16(WLAN_EID_VHT_CAPABILITY);
1676                         vht_capab->header.len =
1677                                   cpu_to_le16(sizeof(struct ieee80211_vht_cap));
1678                         memcpy(&vht_capab->vht_cap, params->vht_capa,
1679                                sizeof(struct ieee80211_vht_cap));
1680                         config_len += sizeof(struct mwifiex_ie_types_vhtcap);
1681                 }
1682                 if (params->aid) {
1683                         aid = (struct mwifiex_ie_types_aid *)(pos + config_len);
1684                         aid->header.type = cpu_to_le16(WLAN_EID_AID);
1685                         aid->header.len = cpu_to_le16(sizeof(params->aid));
1686                         aid->aid = cpu_to_le16(params->aid);
1687                         config_len += sizeof(struct mwifiex_ie_types_aid);
1688                 }
1689
1690                 timeout = (void *)(pos + config_len);
1691                 timeout->header.type = cpu_to_le16(TLV_TYPE_TDLS_IDLE_TIMEOUT);
1692                 timeout->header.len = cpu_to_le16(sizeof(timeout->value));
1693                 timeout->value = cpu_to_le16(MWIFIEX_TDLS_IDLE_TIMEOUT_IN_SEC);
1694                 config_len += sizeof(struct mwifiex_ie_types_tdls_idle_timeout);
1695
1696                 break;
1697         default:
1698                 mwifiex_dbg(priv->adapter, ERROR, "Unknown TDLS operation\n");
1699                 return -ENOTSUPP;
1700         }
1701
1702         le16_add_cpu(&cmd->size, config_len);
1703
1704         return 0;
1705 }
1706
1707 /* This function prepares command of sdio rx aggr info. */
1708 static int mwifiex_cmd_sdio_rx_aggr_cfg(struct host_cmd_ds_command *cmd,
1709                                         u16 cmd_action, void *data_buf)
1710 {
1711         struct host_cmd_sdio_sp_rx_aggr_cfg *cfg =
1712                                         &cmd->params.sdio_rx_aggr_cfg;
1713
1714         cmd->command = cpu_to_le16(HostCmd_CMD_SDIO_SP_RX_AGGR_CFG);
1715         cmd->size =
1716                 cpu_to_le16(sizeof(struct host_cmd_sdio_sp_rx_aggr_cfg) +
1717                             S_DS_GEN);
1718         cfg->action = cmd_action;
1719         if (cmd_action == HostCmd_ACT_GEN_SET)
1720                 cfg->enable = *(u8 *)data_buf;
1721
1722         return 0;
1723 }
1724
1725 /*
1726  * This function prepares the commands before sending them to the firmware.
1727  *
1728  * This is a generic function which calls specific command preparation
1729  * routines based upon the command number.
1730  */
1731 int mwifiex_sta_prepare_cmd(struct mwifiex_private *priv, uint16_t cmd_no,
1732                             u16 cmd_action, u32 cmd_oid,
1733                             void *data_buf, void *cmd_buf)
1734 {
1735         struct host_cmd_ds_command *cmd_ptr = cmd_buf;
1736         int ret = 0;
1737
1738         /* Prepare command */
1739         switch (cmd_no) {
1740         case HostCmd_CMD_GET_HW_SPEC:
1741                 ret = mwifiex_cmd_get_hw_spec(priv, cmd_ptr);
1742                 break;
1743         case HostCmd_CMD_CFG_DATA:
1744                 ret = mwifiex_cmd_cfg_data(priv, cmd_ptr, data_buf);
1745                 break;
1746         case HostCmd_CMD_MAC_CONTROL:
1747                 ret = mwifiex_cmd_mac_control(priv, cmd_ptr, cmd_action,
1748                                               data_buf);
1749                 break;
1750         case HostCmd_CMD_802_11_MAC_ADDRESS:
1751                 ret = mwifiex_cmd_802_11_mac_address(priv, cmd_ptr,
1752                                                      cmd_action);
1753                 break;
1754         case HostCmd_CMD_MAC_MULTICAST_ADR:
1755                 ret = mwifiex_cmd_mac_multicast_adr(cmd_ptr, cmd_action,
1756                                                     data_buf);
1757                 break;
1758         case HostCmd_CMD_TX_RATE_CFG:
1759                 ret = mwifiex_cmd_tx_rate_cfg(priv, cmd_ptr, cmd_action,
1760                                               data_buf);
1761                 break;
1762         case HostCmd_CMD_TXPWR_CFG:
1763                 ret = mwifiex_cmd_tx_power_cfg(cmd_ptr, cmd_action,
1764                                                data_buf);
1765                 break;
1766         case HostCmd_CMD_RF_TX_PWR:
1767                 ret = mwifiex_cmd_rf_tx_power(priv, cmd_ptr, cmd_action,
1768                                               data_buf);
1769                 break;
1770         case HostCmd_CMD_RF_ANTENNA:
1771                 ret = mwifiex_cmd_rf_antenna(priv, cmd_ptr, cmd_action,
1772                                              data_buf);
1773                 break;
1774         case HostCmd_CMD_802_11_PS_MODE_ENH:
1775                 ret = mwifiex_cmd_enh_power_mode(priv, cmd_ptr, cmd_action,
1776                                                  (uint16_t)cmd_oid, data_buf);
1777                 break;
1778         case HostCmd_CMD_802_11_HS_CFG_ENH:
1779                 ret = mwifiex_cmd_802_11_hs_cfg(priv, cmd_ptr, cmd_action,
1780                                 (struct mwifiex_hs_config_param *) data_buf);
1781                 break;
1782         case HostCmd_CMD_802_11_SCAN:
1783                 ret = mwifiex_cmd_802_11_scan(cmd_ptr, data_buf);
1784                 break;
1785         case HostCmd_CMD_802_11_BG_SCAN_QUERY:
1786                 ret = mwifiex_cmd_802_11_bg_scan_query(cmd_ptr);
1787                 break;
1788         case HostCmd_CMD_802_11_ASSOCIATE:
1789                 ret = mwifiex_cmd_802_11_associate(priv, cmd_ptr, data_buf);
1790                 break;
1791         case HostCmd_CMD_802_11_DEAUTHENTICATE:
1792                 ret = mwifiex_cmd_802_11_deauthenticate(priv, cmd_ptr,
1793                                                         data_buf);
1794                 break;
1795         case HostCmd_CMD_802_11_AD_HOC_START:
1796                 ret = mwifiex_cmd_802_11_ad_hoc_start(priv, cmd_ptr,
1797                                                       data_buf);
1798                 break;
1799         case HostCmd_CMD_802_11_GET_LOG:
1800                 ret = mwifiex_cmd_802_11_get_log(cmd_ptr);
1801                 break;
1802         case HostCmd_CMD_802_11_AD_HOC_JOIN:
1803                 ret = mwifiex_cmd_802_11_ad_hoc_join(priv, cmd_ptr,
1804                                                      data_buf);
1805                 break;
1806         case HostCmd_CMD_802_11_AD_HOC_STOP:
1807                 ret = mwifiex_cmd_802_11_ad_hoc_stop(cmd_ptr);
1808                 break;
1809         case HostCmd_CMD_RSSI_INFO:
1810                 ret = mwifiex_cmd_802_11_rssi_info(priv, cmd_ptr, cmd_action);
1811                 break;
1812         case HostCmd_CMD_802_11_SNMP_MIB:
1813                 ret = mwifiex_cmd_802_11_snmp_mib(priv, cmd_ptr, cmd_action,
1814                                                   cmd_oid, data_buf);
1815                 break;
1816         case HostCmd_CMD_802_11_TX_RATE_QUERY:
1817                 cmd_ptr->command =
1818                         cpu_to_le16(HostCmd_CMD_802_11_TX_RATE_QUERY);
1819                 cmd_ptr->size =
1820                         cpu_to_le16(sizeof(struct host_cmd_ds_tx_rate_query) +
1821                                     S_DS_GEN);
1822                 priv->tx_rate = 0;
1823                 ret = 0;
1824                 break;
1825         case HostCmd_CMD_VERSION_EXT:
1826                 cmd_ptr->command = cpu_to_le16(cmd_no);
1827                 cmd_ptr->params.verext.version_str_sel =
1828                         (u8) (*((u32 *) data_buf));
1829                 memcpy(&cmd_ptr->params, data_buf,
1830                        sizeof(struct host_cmd_ds_version_ext));
1831                 cmd_ptr->size =
1832                         cpu_to_le16(sizeof(struct host_cmd_ds_version_ext) +
1833                                     S_DS_GEN);
1834                 ret = 0;
1835                 break;
1836         case HostCmd_CMD_MGMT_FRAME_REG:
1837                 cmd_ptr->command = cpu_to_le16(cmd_no);
1838                 cmd_ptr->params.reg_mask.action = cpu_to_le16(cmd_action);
1839                 cmd_ptr->params.reg_mask.mask = cpu_to_le32(*(u32 *)data_buf);
1840                 cmd_ptr->size =
1841                         cpu_to_le16(sizeof(struct host_cmd_ds_mgmt_frame_reg) +
1842                                     S_DS_GEN);
1843                 ret = 0;
1844                 break;
1845         case HostCmd_CMD_REMAIN_ON_CHAN:
1846                 cmd_ptr->command = cpu_to_le16(cmd_no);
1847                 memcpy(&cmd_ptr->params, data_buf,
1848                        sizeof(struct host_cmd_ds_remain_on_chan));
1849                 cmd_ptr->size =
1850                       cpu_to_le16(sizeof(struct host_cmd_ds_remain_on_chan) +
1851                                   S_DS_GEN);
1852                 break;
1853         case HostCmd_CMD_11AC_CFG:
1854                 ret = mwifiex_cmd_11ac_cfg(priv, cmd_ptr, cmd_action, data_buf);
1855                 break;
1856         case HostCmd_CMD_P2P_MODE_CFG:
1857                 cmd_ptr->command = cpu_to_le16(cmd_no);
1858                 cmd_ptr->params.mode_cfg.action = cpu_to_le16(cmd_action);
1859                 cmd_ptr->params.mode_cfg.mode = cpu_to_le16(*(u16 *)data_buf);
1860                 cmd_ptr->size =
1861                         cpu_to_le16(sizeof(struct host_cmd_ds_p2p_mode_cfg) +
1862                                     S_DS_GEN);
1863                 break;
1864         case HostCmd_CMD_FUNC_INIT:
1865                 if (priv->adapter->hw_status == MWIFIEX_HW_STATUS_RESET)
1866                         priv->adapter->hw_status = MWIFIEX_HW_STATUS_READY;
1867                 cmd_ptr->command = cpu_to_le16(cmd_no);
1868                 cmd_ptr->size = cpu_to_le16(S_DS_GEN);
1869                 break;
1870         case HostCmd_CMD_FUNC_SHUTDOWN:
1871                 priv->adapter->hw_status = MWIFIEX_HW_STATUS_RESET;
1872                 cmd_ptr->command = cpu_to_le16(cmd_no);
1873                 cmd_ptr->size = cpu_to_le16(S_DS_GEN);
1874                 break;
1875         case HostCmd_CMD_11N_ADDBA_REQ:
1876                 ret = mwifiex_cmd_11n_addba_req(cmd_ptr, data_buf);
1877                 break;
1878         case HostCmd_CMD_11N_DELBA:
1879                 ret = mwifiex_cmd_11n_delba(cmd_ptr, data_buf);
1880                 break;
1881         case HostCmd_CMD_11N_ADDBA_RSP:
1882                 ret = mwifiex_cmd_11n_addba_rsp_gen(priv, cmd_ptr, data_buf);
1883                 break;
1884         case HostCmd_CMD_802_11_KEY_MATERIAL:
1885                 ret = mwifiex_cmd_802_11_key_material(priv, cmd_ptr,
1886                                                       cmd_action, cmd_oid,
1887                                                       data_buf);
1888                 break;
1889         case HostCmd_CMD_802_11D_DOMAIN_INFO:
1890                 ret = mwifiex_cmd_802_11d_domain_info(priv, cmd_ptr,
1891                                                       cmd_action);
1892                 break;
1893         case HostCmd_CMD_RECONFIGURE_TX_BUFF:
1894                 ret = mwifiex_cmd_recfg_tx_buf(priv, cmd_ptr, cmd_action,
1895                                                data_buf);
1896                 break;
1897         case HostCmd_CMD_AMSDU_AGGR_CTRL:
1898                 ret = mwifiex_cmd_amsdu_aggr_ctrl(cmd_ptr, cmd_action,
1899                                                   data_buf);
1900                 break;
1901         case HostCmd_CMD_11N_CFG:
1902                 ret = mwifiex_cmd_11n_cfg(priv, cmd_ptr, cmd_action, data_buf);
1903                 break;
1904         case HostCmd_CMD_WMM_GET_STATUS:
1905                 mwifiex_dbg(priv->adapter, CMD,
1906                             "cmd: WMM: WMM_GET_STATUS cmd sent\n");
1907                 cmd_ptr->command = cpu_to_le16(HostCmd_CMD_WMM_GET_STATUS);
1908                 cmd_ptr->size =
1909                         cpu_to_le16(sizeof(struct host_cmd_ds_wmm_get_status) +
1910                                     S_DS_GEN);
1911                 ret = 0;
1912                 break;
1913         case HostCmd_CMD_802_11_IBSS_COALESCING_STATUS:
1914                 ret = mwifiex_cmd_ibss_coalescing_status(cmd_ptr, cmd_action,
1915                                                          data_buf);
1916                 break;
1917         case HostCmd_CMD_802_11_SCAN_EXT:
1918                 ret = mwifiex_cmd_802_11_scan_ext(priv, cmd_ptr, data_buf);
1919                 break;
1920         case HostCmd_CMD_MEM_ACCESS:
1921                 ret = mwifiex_cmd_mem_access(cmd_ptr, cmd_action, data_buf);
1922                 break;
1923         case HostCmd_CMD_MAC_REG_ACCESS:
1924         case HostCmd_CMD_BBP_REG_ACCESS:
1925         case HostCmd_CMD_RF_REG_ACCESS:
1926         case HostCmd_CMD_PMIC_REG_ACCESS:
1927         case HostCmd_CMD_CAU_REG_ACCESS:
1928         case HostCmd_CMD_802_11_EEPROM_ACCESS:
1929                 ret = mwifiex_cmd_reg_access(cmd_ptr, cmd_action, data_buf);
1930                 break;
1931         case HostCmd_CMD_SET_BSS_MODE:
1932                 cmd_ptr->command = cpu_to_le16(cmd_no);
1933                 if (priv->bss_mode == NL80211_IFTYPE_ADHOC)
1934                         cmd_ptr->params.bss_mode.con_type =
1935                                 CONNECTION_TYPE_ADHOC;
1936                 else if (priv->bss_mode == NL80211_IFTYPE_STATION)
1937                         cmd_ptr->params.bss_mode.con_type =
1938                                 CONNECTION_TYPE_INFRA;
1939                 else if (priv->bss_mode == NL80211_IFTYPE_AP)
1940                         cmd_ptr->params.bss_mode.con_type = CONNECTION_TYPE_AP;
1941                 cmd_ptr->size = cpu_to_le16(sizeof(struct
1942                                 host_cmd_ds_set_bss_mode) + S_DS_GEN);
1943                 ret = 0;
1944                 break;
1945         case HostCmd_CMD_PCIE_DESC_DETAILS:
1946                 ret = mwifiex_cmd_pcie_host_spec(priv, cmd_ptr, cmd_action);
1947                 break;
1948         case HostCmd_CMD_802_11_SUBSCRIBE_EVENT:
1949                 ret = mwifiex_cmd_802_11_subsc_evt(priv, cmd_ptr, data_buf);
1950                 break;
1951         case HostCmd_CMD_MEF_CFG:
1952                 ret = mwifiex_cmd_mef_cfg(priv, cmd_ptr, data_buf);
1953                 break;
1954         case HostCmd_CMD_COALESCE_CFG:
1955                 ret = mwifiex_cmd_coalesce_cfg(priv, cmd_ptr, cmd_action,
1956                                                data_buf);
1957                 break;
1958         case HostCmd_CMD_TDLS_OPER:
1959                 ret = mwifiex_cmd_tdls_oper(priv, cmd_ptr, data_buf);
1960                 break;
1961         case HostCmd_CMD_CHAN_REPORT_REQUEST:
1962                 ret = mwifiex_cmd_issue_chan_report_request(priv, cmd_ptr,
1963                                                             data_buf);
1964                 break;
1965         case HostCmd_CMD_SDIO_SP_RX_AGGR_CFG:
1966                 ret = mwifiex_cmd_sdio_rx_aggr_cfg(cmd_ptr, cmd_action,
1967                                                    data_buf);
1968                 break;
1969         default:
1970                 mwifiex_dbg(priv->adapter, ERROR,
1971                             "PREP_CMD: unknown cmd- %#x\n", cmd_no);
1972                 ret = -1;
1973                 break;
1974         }
1975         return ret;
1976 }
1977
1978 /*
1979  * This function issues commands to initialize firmware.
1980  *
1981  * This is called after firmware download to bring the card to
1982  * working state.
1983  * Function is also called during reinitialization of virtual
1984  * interfaces.
1985  *
1986  * The following commands are issued sequentially -
1987  *      - Set PCI-Express host buffer configuration (PCIE only)
1988  *      - Function init (for first interface only)
1989  *      - Read MAC address (for first interface only)
1990  *      - Reconfigure Tx buffer size (for first interface only)
1991  *      - Enable auto deep sleep (for first interface only)
1992  *      - Get Tx rate
1993  *      - Get Tx power
1994  *      - Set IBSS coalescing status
1995  *      - Set AMSDU aggregation control
1996  *      - Set 11d control
1997  *      - Set MAC control (this must be the last command to initialize firmware)
1998  */
1999 int mwifiex_sta_init_cmd(struct mwifiex_private *priv, u8 first_sta, bool init)
2000 {
2001         struct mwifiex_adapter *adapter = priv->adapter;
2002         int ret;
2003         u16 enable = true;
2004         struct mwifiex_ds_11n_amsdu_aggr_ctrl amsdu_aggr_ctrl;
2005         struct mwifiex_ds_auto_ds auto_ds;
2006         enum state_11d_t state_11d;
2007         struct mwifiex_ds_11n_tx_cfg tx_cfg;
2008         u8 sdio_sp_rx_aggr_enable;
2009
2010         if (first_sta) {
2011                 if (priv->adapter->iface_type == MWIFIEX_PCIE) {
2012                         ret = mwifiex_send_cmd(priv,
2013                                                HostCmd_CMD_PCIE_DESC_DETAILS,
2014                                                HostCmd_ACT_GEN_SET, 0, NULL,
2015                                                true);
2016                         if (ret)
2017                                 return -1;
2018                 }
2019
2020                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_FUNC_INIT,
2021                                        HostCmd_ACT_GEN_SET, 0, NULL, true);
2022                 if (ret)
2023                         return -1;
2024
2025                 /* Download calibration data to firmware.
2026                  * The cal-data can be read from device tree and/or
2027                  * a configuration file and downloaded to firmware.
2028                  */
2029                 adapter->dt_node =
2030                                 of_find_node_by_name(NULL, "marvell_cfgdata");
2031                 if (adapter->dt_node) {
2032                         ret = mwifiex_dnld_dt_cfgdata(priv, adapter->dt_node,
2033                                                       "marvell,caldata");
2034                         if (ret)
2035                                 return -1;
2036                 }
2037
2038                 if (adapter->cal_data) {
2039                         ret = mwifiex_send_cmd(priv, HostCmd_CMD_CFG_DATA,
2040                                                HostCmd_ACT_GEN_SET, 0, NULL,
2041                                                true);
2042                         if (ret)
2043                                 return -1;
2044                 }
2045
2046                 /* Read MAC address from HW */
2047                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_GET_HW_SPEC,
2048                                        HostCmd_ACT_GEN_GET, 0, NULL, true);
2049                 if (ret)
2050                         return -1;
2051
2052                 /** Set SDIO Single Port RX Aggr Info */
2053                 if (priv->adapter->iface_type == MWIFIEX_SDIO &&
2054                     ISSUPP_SDIO_SPA_ENABLED(priv->adapter->fw_cap_info)) {
2055                         sdio_sp_rx_aggr_enable = true;
2056                         ret = mwifiex_send_cmd(priv,
2057                                                HostCmd_CMD_SDIO_SP_RX_AGGR_CFG,
2058                                                HostCmd_ACT_GEN_SET, 0,
2059                                                &sdio_sp_rx_aggr_enable,
2060                                                true);
2061                         if (ret) {
2062                                 mwifiex_dbg(priv->adapter, ERROR,
2063                                             "error while enabling SP aggregation..disable it");
2064                                 adapter->sdio_rx_aggr_enable = false;
2065                         }
2066                 }
2067
2068                 /* Reconfigure tx buf size */
2069                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
2070                                        HostCmd_ACT_GEN_SET, 0,
2071                                        &priv->adapter->tx_buf_size, true);
2072                 if (ret)
2073                         return -1;
2074
2075                 if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2076                         /* Enable IEEE PS by default */
2077                         priv->adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
2078                         ret = mwifiex_send_cmd(priv,
2079                                                HostCmd_CMD_802_11_PS_MODE_ENH,
2080                                                EN_AUTO_PS, BITMAP_STA_PS, NULL,
2081                                                true);
2082                         if (ret)
2083                                 return -1;
2084                 }
2085         }
2086
2087         /* get tx rate */
2088         ret = mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
2089                                HostCmd_ACT_GEN_GET, 0, NULL, true);
2090         if (ret)
2091                 return -1;
2092         priv->data_rate = 0;
2093
2094         /* get tx power */
2095         ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
2096                                HostCmd_ACT_GEN_GET, 0, NULL, true);
2097         if (ret)
2098                 return -1;
2099
2100         if (priv->bss_type == MWIFIEX_BSS_TYPE_STA) {
2101                 /* set ibss coalescing_status */
2102                 ret = mwifiex_send_cmd(
2103                                 priv,
2104                                 HostCmd_CMD_802_11_IBSS_COALESCING_STATUS,
2105                                 HostCmd_ACT_GEN_SET, 0, &enable, true);
2106                 if (ret)
2107                         return -1;
2108         }
2109
2110         memset(&amsdu_aggr_ctrl, 0, sizeof(amsdu_aggr_ctrl));
2111         amsdu_aggr_ctrl.enable = true;
2112         /* Send request to firmware */
2113         ret = mwifiex_send_cmd(priv, HostCmd_CMD_AMSDU_AGGR_CTRL,
2114                                HostCmd_ACT_GEN_SET, 0,
2115                                &amsdu_aggr_ctrl, true);
2116         if (ret)
2117                 return -1;
2118         /* MAC Control must be the last command in init_fw */
2119         /* set MAC Control */
2120         ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
2121                                HostCmd_ACT_GEN_SET, 0,
2122                                &priv->curr_pkt_filter, true);
2123         if (ret)
2124                 return -1;
2125
2126         if (!disable_auto_ds &&
2127             first_sta && priv->adapter->iface_type != MWIFIEX_USB &&
2128             priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2129                 /* Enable auto deep sleep */
2130                 auto_ds.auto_ds = DEEP_SLEEP_ON;
2131                 auto_ds.idle_time = DEEP_SLEEP_IDLE_TIME;
2132                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
2133                                        EN_AUTO_PS, BITMAP_AUTO_DS,
2134                                        &auto_ds, true);
2135                 if (ret)
2136                         return -1;
2137         }
2138
2139         if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2140                 /* Send cmd to FW to enable/disable 11D function */
2141                 state_11d = ENABLE_11D;
2142                 ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
2143                                        HostCmd_ACT_GEN_SET, DOT11D_I,
2144                                        &state_11d, true);
2145                 if (ret)
2146                         mwifiex_dbg(priv->adapter, ERROR,
2147                                     "11D: failed to enable 11D\n");
2148         }
2149
2150         /* Send cmd to FW to configure 11n specific configuration
2151          * (Short GI, Channel BW, Green field support etc.) for transmit
2152          */
2153         tx_cfg.tx_htcap = MWIFIEX_FW_DEF_HTTXCFG;
2154         ret = mwifiex_send_cmd(priv, HostCmd_CMD_11N_CFG,
2155                                HostCmd_ACT_GEN_SET, 0, &tx_cfg, true);
2156
2157         if (init) {
2158                 /* set last_init_cmd before sending the command */
2159                 priv->adapter->last_init_cmd = HostCmd_CMD_11N_CFG;
2160                 ret = -EINPROGRESS;
2161         }
2162
2163         return ret;
2164 }