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