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