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