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