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