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