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_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;
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_unaligned_add_cpu(&cmd->size,
1361 				       sizeof(
1362 				       struct mwifiex_ie_types_rssi_threshold));
1363 	}
1364 
1365 	if (event_bitmap & BITMASK_BCN_RSSI_HIGH) {
1366 		rssi_tlv = (struct mwifiex_ie_types_rssi_threshold *) pos;
1367 
1368 		rssi_tlv->header.type = cpu_to_le16(TLV_TYPE_RSSI_HIGH);
1369 		rssi_tlv->header.len =
1370 		    cpu_to_le16(sizeof(struct mwifiex_ie_types_rssi_threshold) -
1371 				sizeof(struct mwifiex_ie_types_header));
1372 		rssi_tlv->abs_value = subsc_evt_cfg->bcn_h_rssi_cfg.abs_value;
1373 		rssi_tlv->evt_freq = subsc_evt_cfg->bcn_h_rssi_cfg.evt_freq;
1374 
1375 		mwifiex_dbg(priv->adapter, EVENT,
1376 			    "Cfg Beacon High Rssi event,\t"
1377 			    "RSSI:-%d dBm, Freq:%d\n",
1378 			    subsc_evt_cfg->bcn_h_rssi_cfg.abs_value,
1379 			    subsc_evt_cfg->bcn_h_rssi_cfg.evt_freq);
1380 
1381 		pos += sizeof(struct mwifiex_ie_types_rssi_threshold);
1382 		le16_unaligned_add_cpu(&cmd->size,
1383 				       sizeof(
1384 				       struct mwifiex_ie_types_rssi_threshold));
1385 	}
1386 
1387 	return 0;
1388 }
1389 
1390 static int
1391 mwifiex_cmd_append_rpn_expression(struct mwifiex_private *priv,
1392 				  struct mwifiex_mef_entry *mef_entry,
1393 				  u8 **buffer)
1394 {
1395 	struct mwifiex_mef_filter *filter = mef_entry->filter;
1396 	int i, byte_len;
1397 	u8 *stack_ptr = *buffer;
1398 
1399 	for (i = 0; i < MWIFIEX_MEF_MAX_FILTERS; i++) {
1400 		filter = &mef_entry->filter[i];
1401 		if (!filter->filt_type)
1402 			break;
1403 		put_unaligned_le32((u32)filter->repeat, stack_ptr);
1404 		stack_ptr += 4;
1405 		*stack_ptr = TYPE_DNUM;
1406 		stack_ptr += 1;
1407 
1408 		byte_len = filter->byte_seq[MWIFIEX_MEF_MAX_BYTESEQ];
1409 		memcpy(stack_ptr, filter->byte_seq, byte_len);
1410 		stack_ptr += byte_len;
1411 		*stack_ptr = byte_len;
1412 		stack_ptr += 1;
1413 		*stack_ptr = TYPE_BYTESEQ;
1414 		stack_ptr += 1;
1415 		put_unaligned_le32((u32)filter->offset, stack_ptr);
1416 		stack_ptr += 4;
1417 		*stack_ptr = TYPE_DNUM;
1418 		stack_ptr += 1;
1419 
1420 		*stack_ptr = filter->filt_type;
1421 		stack_ptr += 1;
1422 
1423 		if (filter->filt_action) {
1424 			*stack_ptr = filter->filt_action;
1425 			stack_ptr += 1;
1426 		}
1427 
1428 		if (stack_ptr - *buffer > STACK_NBYTES)
1429 			return -1;
1430 	}
1431 
1432 	*buffer = stack_ptr;
1433 	return 0;
1434 }
1435 
1436 static int
1437 mwifiex_cmd_mef_cfg(struct mwifiex_private *priv,
1438 		    struct host_cmd_ds_command *cmd,
1439 		    struct mwifiex_ds_mef_cfg *mef)
1440 {
1441 	struct host_cmd_ds_mef_cfg *mef_cfg = &cmd->params.mef_cfg;
1442 	struct mwifiex_fw_mef_entry *mef_entry = NULL;
1443 	u8 *pos = (u8 *)mef_cfg;
1444 	u16 i;
1445 
1446 	cmd->command = cpu_to_le16(HostCmd_CMD_MEF_CFG);
1447 
1448 	mef_cfg->criteria = cpu_to_le32(mef->criteria);
1449 	mef_cfg->num_entries = cpu_to_le16(mef->num_entries);
1450 	pos += sizeof(*mef_cfg);
1451 
1452 	for (i = 0; i < mef->num_entries; i++) {
1453 		mef_entry = (struct mwifiex_fw_mef_entry *)pos;
1454 		mef_entry->mode = mef->mef_entry[i].mode;
1455 		mef_entry->action = mef->mef_entry[i].action;
1456 		pos += sizeof(*mef_cfg->mef_entry);
1457 
1458 		if (mwifiex_cmd_append_rpn_expression(priv,
1459 						      &mef->mef_entry[i], &pos))
1460 			return -1;
1461 
1462 		mef_entry->exprsize =
1463 			cpu_to_le16(pos - mef_entry->expr);
1464 	}
1465 	cmd->size = cpu_to_le16((u16) (pos - (u8 *)mef_cfg) + S_DS_GEN);
1466 
1467 	return 0;
1468 }
1469 
1470 /* This function parse cal data from ASCII to hex */
1471 static u32 mwifiex_parse_cal_cfg(u8 *src, size_t len, u8 *dst)
1472 {
1473 	u8 *s = src, *d = dst;
1474 
1475 	while (s - src < len) {
1476 		if (*s && (isspace(*s) || *s == '\t')) {
1477 			s++;
1478 			continue;
1479 		}
1480 		if (isxdigit(*s)) {
1481 			*d++ = simple_strtol(s, NULL, 16);
1482 			s += 2;
1483 		} else {
1484 			s++;
1485 		}
1486 	}
1487 
1488 	return d - dst;
1489 }
1490 
1491 int mwifiex_dnld_dt_cfgdata(struct mwifiex_private *priv,
1492 			    struct device_node *node, const char *prefix)
1493 {
1494 #ifdef CONFIG_OF
1495 	struct property *prop;
1496 	size_t len = strlen(prefix);
1497 	int ret;
1498 
1499 	/* look for all matching property names */
1500 	for_each_property_of_node(node, prop) {
1501 		if (len > strlen(prop->name) ||
1502 		    strncmp(prop->name, prefix, len))
1503 			continue;
1504 
1505 		/* property header is 6 bytes, data must fit in cmd buffer */
1506 		if (prop->value && prop->length > 6 &&
1507 		    prop->length <= MWIFIEX_SIZE_OF_CMD_BUFFER - S_DS_GEN) {
1508 			ret = mwifiex_send_cmd(priv, HostCmd_CMD_CFG_DATA,
1509 					       HostCmd_ACT_GEN_SET, 0,
1510 					       prop, true);
1511 			if (ret)
1512 				return ret;
1513 		}
1514 	}
1515 #endif
1516 	return 0;
1517 }
1518 
1519 /* This function prepares command of set_cfg_data. */
1520 static int mwifiex_cmd_cfg_data(struct mwifiex_private *priv,
1521 				struct host_cmd_ds_command *cmd, void *data_buf)
1522 {
1523 	struct mwifiex_adapter *adapter = priv->adapter;
1524 	struct property *prop = data_buf;
1525 	u32 len;
1526 	u8 *data = (u8 *)cmd + S_DS_GEN;
1527 	int ret;
1528 
1529 	if (prop) {
1530 		len = prop->length;
1531 		ret = of_property_read_u8_array(adapter->dt_node, prop->name,
1532 						data, len);
1533 		if (ret)
1534 			return ret;
1535 		mwifiex_dbg(adapter, INFO,
1536 			    "download cfg_data from device tree: %s\n",
1537 			    prop->name);
1538 	} else if (adapter->cal_data->data && adapter->cal_data->size > 0) {
1539 		len = mwifiex_parse_cal_cfg((u8 *)adapter->cal_data->data,
1540 					    adapter->cal_data->size, data);
1541 		mwifiex_dbg(adapter, INFO,
1542 			    "download cfg_data from config file\n");
1543 	} else {
1544 		return -1;
1545 	}
1546 
1547 	cmd->command = cpu_to_le16(HostCmd_CMD_CFG_DATA);
1548 	cmd->size = cpu_to_le16(S_DS_GEN + len);
1549 
1550 	return 0;
1551 }
1552 
1553 static int
1554 mwifiex_cmd_set_mc_policy(struct mwifiex_private *priv,
1555 			  struct host_cmd_ds_command *cmd,
1556 			  u16 cmd_action, void *data_buf)
1557 {
1558 	struct host_cmd_ds_multi_chan_policy *mc_pol = &cmd->params.mc_policy;
1559 	const u16 *drcs_info = data_buf;
1560 
1561 	mc_pol->action = cpu_to_le16(cmd_action);
1562 	mc_pol->policy = cpu_to_le16(*drcs_info);
1563 	cmd->command = cpu_to_le16(HostCmd_CMD_MC_POLICY);
1564 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_multi_chan_policy) +
1565 				S_DS_GEN);
1566 	return 0;
1567 }
1568 
1569 static int mwifiex_cmd_robust_coex(struct mwifiex_private *priv,
1570 				   struct host_cmd_ds_command *cmd,
1571 				   u16 cmd_action, bool *is_timeshare)
1572 {
1573 	struct host_cmd_ds_robust_coex *coex = &cmd->params.coex;
1574 	struct mwifiex_ie_types_robust_coex *coex_tlv;
1575 
1576 	cmd->command = cpu_to_le16(HostCmd_CMD_ROBUST_COEX);
1577 	cmd->size = cpu_to_le16(sizeof(*coex) + sizeof(*coex_tlv) + S_DS_GEN);
1578 
1579 	coex->action = cpu_to_le16(cmd_action);
1580 	coex_tlv = (struct mwifiex_ie_types_robust_coex *)
1581 				((u8 *)coex + sizeof(*coex));
1582 	coex_tlv->header.type = cpu_to_le16(TLV_TYPE_ROBUST_COEX);
1583 	coex_tlv->header.len = cpu_to_le16(sizeof(coex_tlv->mode));
1584 
1585 	if (coex->action == HostCmd_ACT_GEN_GET)
1586 		return 0;
1587 
1588 	if (*is_timeshare)
1589 		coex_tlv->mode = cpu_to_le32(MWIFIEX_COEX_MODE_TIMESHARE);
1590 	else
1591 		coex_tlv->mode = cpu_to_le32(MWIFIEX_COEX_MODE_SPATIAL);
1592 
1593 	return 0;
1594 }
1595 
1596 static int mwifiex_cmd_gtk_rekey_offload(struct mwifiex_private *priv,
1597 					 struct host_cmd_ds_command *cmd,
1598 					 u16 cmd_action,
1599 					 struct cfg80211_gtk_rekey_data *data)
1600 {
1601 	struct host_cmd_ds_gtk_rekey_params *rekey = &cmd->params.rekey;
1602 	u64 rekey_ctr;
1603 
1604 	cmd->command = cpu_to_le16(HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG);
1605 	cmd->size = cpu_to_le16(sizeof(*rekey) + S_DS_GEN);
1606 
1607 	rekey->action = cpu_to_le16(cmd_action);
1608 	if (cmd_action == HostCmd_ACT_GEN_SET) {
1609 		memcpy(rekey->kek, data->kek, NL80211_KEK_LEN);
1610 		memcpy(rekey->kck, data->kck, NL80211_KCK_LEN);
1611 		rekey_ctr = be64_to_cpup((__be64 *)data->replay_ctr);
1612 		rekey->replay_ctr_low = cpu_to_le32((u32)rekey_ctr);
1613 		rekey->replay_ctr_high =
1614 			cpu_to_le32((u32)((u64)rekey_ctr >> 32));
1615 	}
1616 
1617 	return 0;
1618 }
1619 
1620 static int mwifiex_cmd_chan_region_cfg(struct mwifiex_private *priv,
1621 				       struct host_cmd_ds_command *cmd,
1622 				       u16 cmd_action)
1623 {
1624 	struct host_cmd_ds_chan_region_cfg *reg = &cmd->params.reg_cfg;
1625 
1626 	cmd->command = cpu_to_le16(HostCmd_CMD_CHAN_REGION_CFG);
1627 	cmd->size = cpu_to_le16(sizeof(*reg) + S_DS_GEN);
1628 
1629 	if (cmd_action == HostCmd_ACT_GEN_GET)
1630 		reg->action = cpu_to_le16(cmd_action);
1631 
1632 	return 0;
1633 }
1634 
1635 static int
1636 mwifiex_cmd_coalesce_cfg(struct mwifiex_private *priv,
1637 			 struct host_cmd_ds_command *cmd,
1638 			 u16 cmd_action, void *data_buf)
1639 {
1640 	struct host_cmd_ds_coalesce_cfg *coalesce_cfg =
1641 						&cmd->params.coalesce_cfg;
1642 	struct mwifiex_ds_coalesce_cfg *cfg = data_buf;
1643 	struct coalesce_filt_field_param *param;
1644 	u16 cnt, idx, length;
1645 	struct coalesce_receive_filt_rule *rule;
1646 
1647 	cmd->command = cpu_to_le16(HostCmd_CMD_COALESCE_CFG);
1648 	cmd->size = cpu_to_le16(S_DS_GEN);
1649 
1650 	coalesce_cfg->action = cpu_to_le16(cmd_action);
1651 	coalesce_cfg->num_of_rules = cpu_to_le16(cfg->num_of_rules);
1652 	rule = coalesce_cfg->rule;
1653 
1654 	for (cnt = 0; cnt < cfg->num_of_rules; cnt++) {
1655 		rule->header.type = cpu_to_le16(TLV_TYPE_COALESCE_RULE);
1656 		rule->max_coalescing_delay =
1657 			cpu_to_le16(cfg->rule[cnt].max_coalescing_delay);
1658 		rule->pkt_type = cfg->rule[cnt].pkt_type;
1659 		rule->num_of_fields = cfg->rule[cnt].num_of_fields;
1660 
1661 		length = 0;
1662 
1663 		param = rule->params;
1664 		for (idx = 0; idx < cfg->rule[cnt].num_of_fields; idx++) {
1665 			param->operation = cfg->rule[cnt].params[idx].operation;
1666 			param->operand_len =
1667 					cfg->rule[cnt].params[idx].operand_len;
1668 			param->offset =
1669 				cpu_to_le16(cfg->rule[cnt].params[idx].offset);
1670 			memcpy(param->operand_byte_stream,
1671 			       cfg->rule[cnt].params[idx].operand_byte_stream,
1672 			       param->operand_len);
1673 
1674 			length += sizeof(struct coalesce_filt_field_param);
1675 
1676 			param++;
1677 		}
1678 
1679 		/* Total rule length is sizeof max_coalescing_delay(u16),
1680 		 * num_of_fields(u8), pkt_type(u8) and total length of the all
1681 		 * params
1682 		 */
1683 		rule->header.len = cpu_to_le16(length + sizeof(u16) +
1684 					       sizeof(u8) + sizeof(u8));
1685 
1686 		/* Add the rule length to the command size*/
1687 		le16_unaligned_add_cpu(&cmd->size,
1688 				       le16_to_cpu(rule->header.len) +
1689 				       sizeof(struct mwifiex_ie_types_header));
1690 
1691 		rule = (void *)((u8 *)rule->params + length);
1692 	}
1693 
1694 	/* Add sizeof action, num_of_rules to total command length */
1695 	le16_unaligned_add_cpu(&cmd->size, sizeof(u16) + sizeof(u16));
1696 
1697 	return 0;
1698 }
1699 
1700 static int
1701 mwifiex_cmd_tdls_config(struct mwifiex_private *priv,
1702 			struct host_cmd_ds_command *cmd,
1703 			u16 cmd_action, void *data_buf)
1704 {
1705 	struct host_cmd_ds_tdls_config *tdls_config = &cmd->params.tdls_config;
1706 	struct mwifiex_tdls_init_cs_params *config;
1707 	struct mwifiex_tdls_config *init_config;
1708 	u16 len;
1709 
1710 	cmd->command = cpu_to_le16(HostCmd_CMD_TDLS_CONFIG);
1711 	cmd->size = cpu_to_le16(S_DS_GEN);
1712 	tdls_config->tdls_action = cpu_to_le16(cmd_action);
1713 	le16_unaligned_add_cpu(&cmd->size, sizeof(tdls_config->tdls_action));
1714 
1715 	switch (cmd_action) {
1716 	case ACT_TDLS_CS_ENABLE_CONFIG:
1717 		init_config = data_buf;
1718 		len = sizeof(*init_config);
1719 		memcpy(tdls_config->tdls_data, init_config, len);
1720 		break;
1721 	case ACT_TDLS_CS_INIT:
1722 		config = data_buf;
1723 		len = sizeof(*config);
1724 		memcpy(tdls_config->tdls_data, config, len);
1725 		break;
1726 	case ACT_TDLS_CS_STOP:
1727 		len = sizeof(struct mwifiex_tdls_stop_cs_params);
1728 		memcpy(tdls_config->tdls_data, data_buf, len);
1729 		break;
1730 	case ACT_TDLS_CS_PARAMS:
1731 		len = sizeof(struct mwifiex_tdls_config_cs_params);
1732 		memcpy(tdls_config->tdls_data, data_buf, len);
1733 		break;
1734 	default:
1735 		mwifiex_dbg(priv->adapter, ERROR,
1736 			    "Unknown TDLS configuration\n");
1737 		return -ENOTSUPP;
1738 	}
1739 
1740 	le16_unaligned_add_cpu(&cmd->size, len);
1741 	return 0;
1742 }
1743 
1744 static int
1745 mwifiex_cmd_tdls_oper(struct mwifiex_private *priv,
1746 		      struct host_cmd_ds_command *cmd,
1747 		      void *data_buf)
1748 {
1749 	struct host_cmd_ds_tdls_oper *tdls_oper = &cmd->params.tdls_oper;
1750 	struct mwifiex_ds_tdls_oper *oper = data_buf;
1751 	struct host_cmd_tlv_rates *tlv_rates;
1752 	struct mwifiex_ie_types_htcap *ht_capab;
1753 	struct mwifiex_ie_types_qos_info *wmm_qos_info;
1754 	struct mwifiex_ie_types_extcap *extcap;
1755 	struct mwifiex_ie_types_vhtcap *vht_capab;
1756 	struct mwifiex_ie_types_aid *aid;
1757 	struct mwifiex_ie_types_tdls_idle_timeout *timeout;
1758 	u8 *pos, qos_info;
1759 	u16 config_len = 0;
1760 	struct station_parameters *params = priv->sta_params;
1761 
1762 	cmd->command = cpu_to_le16(HostCmd_CMD_TDLS_OPER);
1763 	cmd->size = cpu_to_le16(S_DS_GEN);
1764 	le16_unaligned_add_cpu(&cmd->size,
1765 			       sizeof(struct host_cmd_ds_tdls_oper));
1766 
1767 	tdls_oper->reason = 0;
1768 	memcpy(tdls_oper->peer_mac, oper->peer_mac, ETH_ALEN);
1769 
1770 	pos = (u8 *)tdls_oper + sizeof(struct host_cmd_ds_tdls_oper);
1771 
1772 	switch (oper->tdls_action) {
1773 	case MWIFIEX_TDLS_DISABLE_LINK:
1774 		tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_DELETE);
1775 		break;
1776 	case MWIFIEX_TDLS_CREATE_LINK:
1777 		tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_CREATE);
1778 		break;
1779 	case MWIFIEX_TDLS_CONFIG_LINK:
1780 		tdls_oper->tdls_action = cpu_to_le16(ACT_TDLS_CONFIG);
1781 
1782 		if (!params) {
1783 			mwifiex_dbg(priv->adapter, ERROR,
1784 				    "TDLS config params not available for %pM\n",
1785 				    oper->peer_mac);
1786 			return -ENODATA;
1787 		}
1788 
1789 		put_unaligned_le16(params->capability, pos);
1790 		config_len += sizeof(params->capability);
1791 
1792 		qos_info = params->uapsd_queues | (params->max_sp << 5);
1793 		wmm_qos_info = (struct mwifiex_ie_types_qos_info *)(pos +
1794 								    config_len);
1795 		wmm_qos_info->header.type = cpu_to_le16(WLAN_EID_QOS_CAPA);
1796 		wmm_qos_info->header.len = cpu_to_le16(sizeof(qos_info));
1797 		wmm_qos_info->qos_info = qos_info;
1798 		config_len += sizeof(struct mwifiex_ie_types_qos_info);
1799 
1800 		if (params->ht_capa) {
1801 			ht_capab = (struct mwifiex_ie_types_htcap *)(pos +
1802 								    config_len);
1803 			ht_capab->header.type =
1804 					    cpu_to_le16(WLAN_EID_HT_CAPABILITY);
1805 			ht_capab->header.len =
1806 				   cpu_to_le16(sizeof(struct ieee80211_ht_cap));
1807 			memcpy(&ht_capab->ht_cap, params->ht_capa,
1808 			       sizeof(struct ieee80211_ht_cap));
1809 			config_len += sizeof(struct mwifiex_ie_types_htcap);
1810 		}
1811 
1812 		if (params->supported_rates && params->supported_rates_len) {
1813 			tlv_rates = (struct host_cmd_tlv_rates *)(pos +
1814 								  config_len);
1815 			tlv_rates->header.type =
1816 					       cpu_to_le16(WLAN_EID_SUPP_RATES);
1817 			tlv_rates->header.len =
1818 				       cpu_to_le16(params->supported_rates_len);
1819 			memcpy(tlv_rates->rates, params->supported_rates,
1820 			       params->supported_rates_len);
1821 			config_len += sizeof(struct host_cmd_tlv_rates) +
1822 				      params->supported_rates_len;
1823 		}
1824 
1825 		if (params->ext_capab && params->ext_capab_len) {
1826 			extcap = (struct mwifiex_ie_types_extcap *)(pos +
1827 								    config_len);
1828 			extcap->header.type =
1829 					   cpu_to_le16(WLAN_EID_EXT_CAPABILITY);
1830 			extcap->header.len = cpu_to_le16(params->ext_capab_len);
1831 			memcpy(extcap->ext_capab, params->ext_capab,
1832 			       params->ext_capab_len);
1833 			config_len += sizeof(struct mwifiex_ie_types_extcap) +
1834 				      params->ext_capab_len;
1835 		}
1836 		if (params->vht_capa) {
1837 			vht_capab = (struct mwifiex_ie_types_vhtcap *)(pos +
1838 								    config_len);
1839 			vht_capab->header.type =
1840 					   cpu_to_le16(WLAN_EID_VHT_CAPABILITY);
1841 			vht_capab->header.len =
1842 				  cpu_to_le16(sizeof(struct ieee80211_vht_cap));
1843 			memcpy(&vht_capab->vht_cap, params->vht_capa,
1844 			       sizeof(struct ieee80211_vht_cap));
1845 			config_len += sizeof(struct mwifiex_ie_types_vhtcap);
1846 		}
1847 		if (params->aid) {
1848 			aid = (struct mwifiex_ie_types_aid *)(pos + config_len);
1849 			aid->header.type = cpu_to_le16(WLAN_EID_AID);
1850 			aid->header.len = cpu_to_le16(sizeof(params->aid));
1851 			aid->aid = cpu_to_le16(params->aid);
1852 			config_len += sizeof(struct mwifiex_ie_types_aid);
1853 		}
1854 
1855 		timeout = (void *)(pos + config_len);
1856 		timeout->header.type = cpu_to_le16(TLV_TYPE_TDLS_IDLE_TIMEOUT);
1857 		timeout->header.len = cpu_to_le16(sizeof(timeout->value));
1858 		timeout->value = cpu_to_le16(MWIFIEX_TDLS_IDLE_TIMEOUT_IN_SEC);
1859 		config_len += sizeof(struct mwifiex_ie_types_tdls_idle_timeout);
1860 
1861 		break;
1862 	default:
1863 		mwifiex_dbg(priv->adapter, ERROR, "Unknown TDLS operation\n");
1864 		return -ENOTSUPP;
1865 	}
1866 
1867 	le16_unaligned_add_cpu(&cmd->size, config_len);
1868 
1869 	return 0;
1870 }
1871 
1872 /* This function prepares command of sdio rx aggr info. */
1873 static int mwifiex_cmd_sdio_rx_aggr_cfg(struct host_cmd_ds_command *cmd,
1874 					u16 cmd_action, void *data_buf)
1875 {
1876 	struct host_cmd_sdio_sp_rx_aggr_cfg *cfg =
1877 					&cmd->params.sdio_rx_aggr_cfg;
1878 
1879 	cmd->command = cpu_to_le16(HostCmd_CMD_SDIO_SP_RX_AGGR_CFG);
1880 	cmd->size =
1881 		cpu_to_le16(sizeof(struct host_cmd_sdio_sp_rx_aggr_cfg) +
1882 			    S_DS_GEN);
1883 	cfg->action = cmd_action;
1884 	if (cmd_action == HostCmd_ACT_GEN_SET)
1885 		cfg->enable = *(u8 *)data_buf;
1886 
1887 	return 0;
1888 }
1889 
1890 /* This function prepares command to get HS wakeup reason.
1891  *
1892  * Preparation includes -
1893  *      - Setting command ID, action and proper size
1894  *      - Ensuring correct endian-ness
1895  */
1896 static int mwifiex_cmd_get_wakeup_reason(struct mwifiex_private *priv,
1897 					 struct host_cmd_ds_command *cmd)
1898 {
1899 	cmd->command = cpu_to_le16(HostCmd_CMD_HS_WAKEUP_REASON);
1900 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_wakeup_reason) +
1901 				S_DS_GEN);
1902 
1903 	return 0;
1904 }
1905 
1906 /* This function check if the command is supported by firmware */
1907 static int mwifiex_is_cmd_supported(struct mwifiex_private *priv, u16 cmd_no)
1908 {
1909 	if (!ISSUPP_ADHOC_ENABLED(priv->adapter->fw_cap_info)) {
1910 		switch (cmd_no) {
1911 		case HostCmd_CMD_802_11_IBSS_COALESCING_STATUS:
1912 		case HostCmd_CMD_802_11_AD_HOC_START:
1913 		case HostCmd_CMD_802_11_AD_HOC_JOIN:
1914 		case HostCmd_CMD_802_11_AD_HOC_STOP:
1915 			return -EOPNOTSUPP;
1916 		default:
1917 			break;
1918 		}
1919 	}
1920 
1921 	return 0;
1922 }
1923 
1924 /*
1925  * This function prepares the commands before sending them to the firmware.
1926  *
1927  * This is a generic function which calls specific command preparation
1928  * routines based upon the command number.
1929  */
1930 int mwifiex_sta_prepare_cmd(struct mwifiex_private *priv, uint16_t cmd_no,
1931 			    u16 cmd_action, u32 cmd_oid,
1932 			    void *data_buf, void *cmd_buf)
1933 {
1934 	struct host_cmd_ds_command *cmd_ptr = cmd_buf;
1935 	int ret = 0;
1936 
1937 	if (mwifiex_is_cmd_supported(priv, cmd_no)) {
1938 		mwifiex_dbg(priv->adapter, ERROR,
1939 			    "0x%x command not supported by firmware\n",
1940 			    cmd_no);
1941 		return -EOPNOTSUPP;
1942 	}
1943 
1944 	/* Prepare command */
1945 	switch (cmd_no) {
1946 	case HostCmd_CMD_GET_HW_SPEC:
1947 		ret = mwifiex_cmd_get_hw_spec(priv, cmd_ptr);
1948 		break;
1949 	case HostCmd_CMD_CFG_DATA:
1950 		ret = mwifiex_cmd_cfg_data(priv, cmd_ptr, data_buf);
1951 		break;
1952 	case HostCmd_CMD_MAC_CONTROL:
1953 		ret = mwifiex_cmd_mac_control(priv, cmd_ptr, cmd_action,
1954 					      data_buf);
1955 		break;
1956 	case HostCmd_CMD_802_11_MAC_ADDRESS:
1957 		ret = mwifiex_cmd_802_11_mac_address(priv, cmd_ptr,
1958 						     cmd_action);
1959 		break;
1960 	case HostCmd_CMD_MAC_MULTICAST_ADR:
1961 		ret = mwifiex_cmd_mac_multicast_adr(cmd_ptr, cmd_action,
1962 						    data_buf);
1963 		break;
1964 	case HostCmd_CMD_TX_RATE_CFG:
1965 		ret = mwifiex_cmd_tx_rate_cfg(priv, cmd_ptr, cmd_action,
1966 					      data_buf);
1967 		break;
1968 	case HostCmd_CMD_TXPWR_CFG:
1969 		ret = mwifiex_cmd_tx_power_cfg(cmd_ptr, cmd_action,
1970 					       data_buf);
1971 		break;
1972 	case HostCmd_CMD_RF_TX_PWR:
1973 		ret = mwifiex_cmd_rf_tx_power(priv, cmd_ptr, cmd_action,
1974 					      data_buf);
1975 		break;
1976 	case HostCmd_CMD_RF_ANTENNA:
1977 		ret = mwifiex_cmd_rf_antenna(priv, cmd_ptr, cmd_action,
1978 					     data_buf);
1979 		break;
1980 	case HostCmd_CMD_802_11_PS_MODE_ENH:
1981 		ret = mwifiex_cmd_enh_power_mode(priv, cmd_ptr, cmd_action,
1982 						 (uint16_t)cmd_oid, data_buf);
1983 		break;
1984 	case HostCmd_CMD_802_11_HS_CFG_ENH:
1985 		ret = mwifiex_cmd_802_11_hs_cfg(priv, cmd_ptr, cmd_action,
1986 				(struct mwifiex_hs_config_param *) data_buf);
1987 		break;
1988 	case HostCmd_CMD_802_11_SCAN:
1989 		ret = mwifiex_cmd_802_11_scan(cmd_ptr, data_buf);
1990 		break;
1991 	case HostCmd_CMD_802_11_BG_SCAN_CONFIG:
1992 		ret = mwifiex_cmd_802_11_bg_scan_config(priv, cmd_ptr,
1993 							data_buf);
1994 		break;
1995 	case HostCmd_CMD_802_11_BG_SCAN_QUERY:
1996 		ret = mwifiex_cmd_802_11_bg_scan_query(cmd_ptr);
1997 		break;
1998 	case HostCmd_CMD_802_11_ASSOCIATE:
1999 		ret = mwifiex_cmd_802_11_associate(priv, cmd_ptr, data_buf);
2000 		break;
2001 	case HostCmd_CMD_802_11_DEAUTHENTICATE:
2002 		ret = mwifiex_cmd_802_11_deauthenticate(priv, cmd_ptr,
2003 							data_buf);
2004 		break;
2005 	case HostCmd_CMD_802_11_AD_HOC_START:
2006 		ret = mwifiex_cmd_802_11_ad_hoc_start(priv, cmd_ptr,
2007 						      data_buf);
2008 		break;
2009 	case HostCmd_CMD_802_11_GET_LOG:
2010 		ret = mwifiex_cmd_802_11_get_log(cmd_ptr);
2011 		break;
2012 	case HostCmd_CMD_802_11_AD_HOC_JOIN:
2013 		ret = mwifiex_cmd_802_11_ad_hoc_join(priv, cmd_ptr,
2014 						     data_buf);
2015 		break;
2016 	case HostCmd_CMD_802_11_AD_HOC_STOP:
2017 		ret = mwifiex_cmd_802_11_ad_hoc_stop(cmd_ptr);
2018 		break;
2019 	case HostCmd_CMD_RSSI_INFO:
2020 		ret = mwifiex_cmd_802_11_rssi_info(priv, cmd_ptr, cmd_action);
2021 		break;
2022 	case HostCmd_CMD_802_11_SNMP_MIB:
2023 		ret = mwifiex_cmd_802_11_snmp_mib(priv, cmd_ptr, cmd_action,
2024 						  cmd_oid, data_buf);
2025 		break;
2026 	case HostCmd_CMD_802_11_TX_RATE_QUERY:
2027 		cmd_ptr->command =
2028 			cpu_to_le16(HostCmd_CMD_802_11_TX_RATE_QUERY);
2029 		cmd_ptr->size =
2030 			cpu_to_le16(sizeof(struct host_cmd_ds_tx_rate_query) +
2031 				    S_DS_GEN);
2032 		priv->tx_rate = 0;
2033 		ret = 0;
2034 		break;
2035 	case HostCmd_CMD_VERSION_EXT:
2036 		cmd_ptr->command = cpu_to_le16(cmd_no);
2037 		cmd_ptr->params.verext.version_str_sel =
2038 			(u8)(get_unaligned((u32 *)data_buf));
2039 		memcpy(&cmd_ptr->params, data_buf,
2040 		       sizeof(struct host_cmd_ds_version_ext));
2041 		cmd_ptr->size =
2042 			cpu_to_le16(sizeof(struct host_cmd_ds_version_ext) +
2043 				    S_DS_GEN);
2044 		ret = 0;
2045 		break;
2046 	case HostCmd_CMD_MGMT_FRAME_REG:
2047 		cmd_ptr->command = cpu_to_le16(cmd_no);
2048 		cmd_ptr->params.reg_mask.action = cpu_to_le16(cmd_action);
2049 		cmd_ptr->params.reg_mask.mask = cpu_to_le32(
2050 						get_unaligned((u32 *)data_buf));
2051 		cmd_ptr->size =
2052 			cpu_to_le16(sizeof(struct host_cmd_ds_mgmt_frame_reg) +
2053 				    S_DS_GEN);
2054 		ret = 0;
2055 		break;
2056 	case HostCmd_CMD_REMAIN_ON_CHAN:
2057 		cmd_ptr->command = cpu_to_le16(cmd_no);
2058 		memcpy(&cmd_ptr->params, data_buf,
2059 		       sizeof(struct host_cmd_ds_remain_on_chan));
2060 		cmd_ptr->size =
2061 		      cpu_to_le16(sizeof(struct host_cmd_ds_remain_on_chan) +
2062 				  S_DS_GEN);
2063 		break;
2064 	case HostCmd_CMD_11AC_CFG:
2065 		ret = mwifiex_cmd_11ac_cfg(priv, cmd_ptr, cmd_action, data_buf);
2066 		break;
2067 	case HostCmd_CMD_PACKET_AGGR_CTRL:
2068 		cmd_ptr->command = cpu_to_le16(cmd_no);
2069 		cmd_ptr->params.pkt_aggr_ctrl.action = cpu_to_le16(cmd_action);
2070 		cmd_ptr->params.pkt_aggr_ctrl.enable =
2071 						cpu_to_le16(*(u16 *)data_buf);
2072 		cmd_ptr->size =
2073 			cpu_to_le16(sizeof(struct host_cmd_ds_pkt_aggr_ctrl) +
2074 				    S_DS_GEN);
2075 		break;
2076 	case HostCmd_CMD_P2P_MODE_CFG:
2077 		cmd_ptr->command = cpu_to_le16(cmd_no);
2078 		cmd_ptr->params.mode_cfg.action = cpu_to_le16(cmd_action);
2079 		cmd_ptr->params.mode_cfg.mode = cpu_to_le16(
2080 						get_unaligned((u16 *)data_buf));
2081 		cmd_ptr->size =
2082 			cpu_to_le16(sizeof(struct host_cmd_ds_p2p_mode_cfg) +
2083 				    S_DS_GEN);
2084 		break;
2085 	case HostCmd_CMD_FUNC_INIT:
2086 		if (priv->adapter->hw_status == MWIFIEX_HW_STATUS_RESET)
2087 			priv->adapter->hw_status = MWIFIEX_HW_STATUS_READY;
2088 		cmd_ptr->command = cpu_to_le16(cmd_no);
2089 		cmd_ptr->size = cpu_to_le16(S_DS_GEN);
2090 		break;
2091 	case HostCmd_CMD_FUNC_SHUTDOWN:
2092 		priv->adapter->hw_status = MWIFIEX_HW_STATUS_RESET;
2093 		cmd_ptr->command = cpu_to_le16(cmd_no);
2094 		cmd_ptr->size = cpu_to_le16(S_DS_GEN);
2095 		break;
2096 	case HostCmd_CMD_11N_ADDBA_REQ:
2097 		ret = mwifiex_cmd_11n_addba_req(cmd_ptr, data_buf);
2098 		break;
2099 	case HostCmd_CMD_11N_DELBA:
2100 		ret = mwifiex_cmd_11n_delba(cmd_ptr, data_buf);
2101 		break;
2102 	case HostCmd_CMD_11N_ADDBA_RSP:
2103 		ret = mwifiex_cmd_11n_addba_rsp_gen(priv, cmd_ptr, data_buf);
2104 		break;
2105 	case HostCmd_CMD_802_11_KEY_MATERIAL:
2106 		ret = mwifiex_cmd_802_11_key_material(priv, cmd_ptr,
2107 						      cmd_action, cmd_oid,
2108 						      data_buf);
2109 		break;
2110 	case HostCmd_CMD_802_11D_DOMAIN_INFO:
2111 		ret = mwifiex_cmd_802_11d_domain_info(priv, cmd_ptr,
2112 						      cmd_action);
2113 		break;
2114 	case HostCmd_CMD_RECONFIGURE_TX_BUFF:
2115 		ret = mwifiex_cmd_recfg_tx_buf(priv, cmd_ptr, cmd_action,
2116 					       data_buf);
2117 		break;
2118 	case HostCmd_CMD_AMSDU_AGGR_CTRL:
2119 		ret = mwifiex_cmd_amsdu_aggr_ctrl(cmd_ptr, cmd_action,
2120 						  data_buf);
2121 		break;
2122 	case HostCmd_CMD_11N_CFG:
2123 		ret = mwifiex_cmd_11n_cfg(priv, cmd_ptr, cmd_action, data_buf);
2124 		break;
2125 	case HostCmd_CMD_WMM_GET_STATUS:
2126 		mwifiex_dbg(priv->adapter, CMD,
2127 			    "cmd: WMM: WMM_GET_STATUS cmd sent\n");
2128 		cmd_ptr->command = cpu_to_le16(HostCmd_CMD_WMM_GET_STATUS);
2129 		cmd_ptr->size =
2130 			cpu_to_le16(sizeof(struct host_cmd_ds_wmm_get_status) +
2131 				    S_DS_GEN);
2132 		ret = 0;
2133 		break;
2134 	case HostCmd_CMD_802_11_IBSS_COALESCING_STATUS:
2135 		ret = mwifiex_cmd_ibss_coalescing_status(cmd_ptr, cmd_action,
2136 							 data_buf);
2137 		break;
2138 	case HostCmd_CMD_802_11_SCAN_EXT:
2139 		ret = mwifiex_cmd_802_11_scan_ext(priv, cmd_ptr, data_buf);
2140 		break;
2141 	case HostCmd_CMD_MEM_ACCESS:
2142 		ret = mwifiex_cmd_mem_access(cmd_ptr, cmd_action, data_buf);
2143 		break;
2144 	case HostCmd_CMD_MAC_REG_ACCESS:
2145 	case HostCmd_CMD_BBP_REG_ACCESS:
2146 	case HostCmd_CMD_RF_REG_ACCESS:
2147 	case HostCmd_CMD_PMIC_REG_ACCESS:
2148 	case HostCmd_CMD_CAU_REG_ACCESS:
2149 	case HostCmd_CMD_802_11_EEPROM_ACCESS:
2150 		ret = mwifiex_cmd_reg_access(cmd_ptr, cmd_action, data_buf);
2151 		break;
2152 	case HostCmd_CMD_SET_BSS_MODE:
2153 		cmd_ptr->command = cpu_to_le16(cmd_no);
2154 		if (priv->bss_mode == NL80211_IFTYPE_ADHOC)
2155 			cmd_ptr->params.bss_mode.con_type =
2156 				CONNECTION_TYPE_ADHOC;
2157 		else if (priv->bss_mode == NL80211_IFTYPE_STATION ||
2158 			 priv->bss_mode == NL80211_IFTYPE_P2P_CLIENT)
2159 			cmd_ptr->params.bss_mode.con_type =
2160 				CONNECTION_TYPE_INFRA;
2161 		else if (priv->bss_mode == NL80211_IFTYPE_AP ||
2162 			 priv->bss_mode == NL80211_IFTYPE_P2P_GO)
2163 			cmd_ptr->params.bss_mode.con_type = CONNECTION_TYPE_AP;
2164 		cmd_ptr->size = cpu_to_le16(sizeof(struct
2165 				host_cmd_ds_set_bss_mode) + S_DS_GEN);
2166 		ret = 0;
2167 		break;
2168 	case HostCmd_CMD_PCIE_DESC_DETAILS:
2169 		ret = mwifiex_cmd_pcie_host_spec(priv, cmd_ptr, cmd_action);
2170 		break;
2171 	case HostCmd_CMD_802_11_SUBSCRIBE_EVENT:
2172 		ret = mwifiex_cmd_802_11_subsc_evt(priv, cmd_ptr, data_buf);
2173 		break;
2174 	case HostCmd_CMD_MEF_CFG:
2175 		ret = mwifiex_cmd_mef_cfg(priv, cmd_ptr, data_buf);
2176 		break;
2177 	case HostCmd_CMD_COALESCE_CFG:
2178 		ret = mwifiex_cmd_coalesce_cfg(priv, cmd_ptr, cmd_action,
2179 					       data_buf);
2180 		break;
2181 	case HostCmd_CMD_TDLS_OPER:
2182 		ret = mwifiex_cmd_tdls_oper(priv, cmd_ptr, data_buf);
2183 		break;
2184 	case HostCmd_CMD_TDLS_CONFIG:
2185 		ret = mwifiex_cmd_tdls_config(priv, cmd_ptr, cmd_action,
2186 					      data_buf);
2187 		break;
2188 	case HostCmd_CMD_CHAN_REPORT_REQUEST:
2189 		ret = mwifiex_cmd_issue_chan_report_request(priv, cmd_ptr,
2190 							    data_buf);
2191 		break;
2192 	case HostCmd_CMD_SDIO_SP_RX_AGGR_CFG:
2193 		ret = mwifiex_cmd_sdio_rx_aggr_cfg(cmd_ptr, cmd_action,
2194 						   data_buf);
2195 		break;
2196 	case HostCmd_CMD_HS_WAKEUP_REASON:
2197 		ret = mwifiex_cmd_get_wakeup_reason(priv, cmd_ptr);
2198 		break;
2199 	case HostCmd_CMD_MC_POLICY:
2200 		ret = mwifiex_cmd_set_mc_policy(priv, cmd_ptr, cmd_action,
2201 						data_buf);
2202 		break;
2203 	case HostCmd_CMD_ROBUST_COEX:
2204 		ret = mwifiex_cmd_robust_coex(priv, cmd_ptr, cmd_action,
2205 					      data_buf);
2206 		break;
2207 	case HostCmd_CMD_GTK_REKEY_OFFLOAD_CFG:
2208 		ret = mwifiex_cmd_gtk_rekey_offload(priv, cmd_ptr, cmd_action,
2209 						    data_buf);
2210 		break;
2211 	case HostCmd_CMD_CHAN_REGION_CFG:
2212 		ret = mwifiex_cmd_chan_region_cfg(priv, cmd_ptr, cmd_action);
2213 		break;
2214 	default:
2215 		mwifiex_dbg(priv->adapter, ERROR,
2216 			    "PREP_CMD: unknown cmd- %#x\n", cmd_no);
2217 		ret = -1;
2218 		break;
2219 	}
2220 	return ret;
2221 }
2222 
2223 /*
2224  * This function issues commands to initialize firmware.
2225  *
2226  * This is called after firmware download to bring the card to
2227  * working state.
2228  * Function is also called during reinitialization of virtual
2229  * interfaces.
2230  *
2231  * The following commands are issued sequentially -
2232  *      - Set PCI-Express host buffer configuration (PCIE only)
2233  *      - Function init (for first interface only)
2234  *      - Read MAC address (for first interface only)
2235  *      - Reconfigure Tx buffer size (for first interface only)
2236  *      - Enable auto deep sleep (for first interface only)
2237  *      - Get Tx rate
2238  *      - Get Tx power
2239  *      - Set IBSS coalescing status
2240  *      - Set AMSDU aggregation control
2241  *      - Set 11d control
2242  *      - Set MAC control (this must be the last command to initialize firmware)
2243  */
2244 int mwifiex_sta_init_cmd(struct mwifiex_private *priv, u8 first_sta, bool init)
2245 {
2246 	struct mwifiex_adapter *adapter = priv->adapter;
2247 	int ret;
2248 	struct mwifiex_ds_11n_amsdu_aggr_ctrl amsdu_aggr_ctrl;
2249 	struct mwifiex_ds_auto_ds auto_ds;
2250 	enum state_11d_t state_11d;
2251 	struct mwifiex_ds_11n_tx_cfg tx_cfg;
2252 	u8 sdio_sp_rx_aggr_enable;
2253 	u16 packet_aggr_enable;
2254 	int data;
2255 
2256 	if (first_sta) {
2257 		if (priv->adapter->iface_type == MWIFIEX_PCIE) {
2258 			ret = mwifiex_send_cmd(priv,
2259 					       HostCmd_CMD_PCIE_DESC_DETAILS,
2260 					       HostCmd_ACT_GEN_SET, 0, NULL,
2261 					       true);
2262 			if (ret)
2263 				return -1;
2264 		}
2265 
2266 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_FUNC_INIT,
2267 				       HostCmd_ACT_GEN_SET, 0, NULL, true);
2268 		if (ret)
2269 			return -1;
2270 
2271 		/* Download calibration data to firmware.
2272 		 * The cal-data can be read from device tree and/or
2273 		 * a configuration file and downloaded to firmware.
2274 		 */
2275 		if (adapter->dt_node) {
2276 			if (of_property_read_u32(adapter->dt_node,
2277 						 "marvell,wakeup-pin",
2278 						 &data) == 0) {
2279 				pr_debug("Wakeup pin = 0x%x\n", data);
2280 				adapter->hs_cfg.gpio = data;
2281 			}
2282 
2283 			mwifiex_dnld_dt_cfgdata(priv, adapter->dt_node,
2284 						"marvell,caldata");
2285 		}
2286 
2287 		if (adapter->cal_data)
2288 			mwifiex_send_cmd(priv, HostCmd_CMD_CFG_DATA,
2289 					 HostCmd_ACT_GEN_SET, 0, NULL, true);
2290 
2291 		/* Read MAC address from HW */
2292 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_GET_HW_SPEC,
2293 				       HostCmd_ACT_GEN_GET, 0, NULL, true);
2294 		if (ret)
2295 			return -1;
2296 
2297 		/** Set SDIO Single Port RX Aggr Info */
2298 		if (priv->adapter->iface_type == MWIFIEX_SDIO &&
2299 		    ISSUPP_SDIO_SPA_ENABLED(priv->adapter->fw_cap_info) &&
2300 		    !priv->adapter->host_disable_sdio_rx_aggr) {
2301 			sdio_sp_rx_aggr_enable = true;
2302 			ret = mwifiex_send_cmd(priv,
2303 					       HostCmd_CMD_SDIO_SP_RX_AGGR_CFG,
2304 					       HostCmd_ACT_GEN_SET, 0,
2305 					       &sdio_sp_rx_aggr_enable,
2306 					       true);
2307 			if (ret) {
2308 				mwifiex_dbg(priv->adapter, ERROR,
2309 					    "error while enabling SP aggregation..disable it");
2310 				adapter->sdio_rx_aggr_enable = false;
2311 			}
2312 		}
2313 
2314 		/* Reconfigure tx buf size */
2315 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_RECONFIGURE_TX_BUFF,
2316 				       HostCmd_ACT_GEN_SET, 0,
2317 				       &priv->adapter->tx_buf_size, true);
2318 		if (ret)
2319 			return -1;
2320 
2321 		if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2322 			/* Enable IEEE PS by default */
2323 			priv->adapter->ps_mode = MWIFIEX_802_11_POWER_MODE_PSP;
2324 			ret = mwifiex_send_cmd(priv,
2325 					       HostCmd_CMD_802_11_PS_MODE_ENH,
2326 					       EN_AUTO_PS, BITMAP_STA_PS, NULL,
2327 					       true);
2328 			if (ret)
2329 				return -1;
2330 		}
2331 
2332 		if (drcs) {
2333 			adapter->drcs_enabled = true;
2334 			if (ISSUPP_DRCS_ENABLED(adapter->fw_cap_info))
2335 				ret = mwifiex_send_cmd(priv,
2336 						       HostCmd_CMD_MC_POLICY,
2337 						       HostCmd_ACT_GEN_SET, 0,
2338 						       &adapter->drcs_enabled,
2339 						       true);
2340 			if (ret)
2341 				return -1;
2342 		}
2343 
2344 		mwifiex_send_cmd(priv, HostCmd_CMD_CHAN_REGION_CFG,
2345 				 HostCmd_ACT_GEN_GET, 0, NULL, true);
2346 	}
2347 
2348 	/* get tx rate */
2349 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_TX_RATE_CFG,
2350 			       HostCmd_ACT_GEN_GET, 0, NULL, true);
2351 	if (ret)
2352 		return -1;
2353 	priv->data_rate = 0;
2354 
2355 	/* get tx power */
2356 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_RF_TX_PWR,
2357 			       HostCmd_ACT_GEN_GET, 0, NULL, true);
2358 	if (ret)
2359 		return -1;
2360 
2361 	memset(&amsdu_aggr_ctrl, 0, sizeof(amsdu_aggr_ctrl));
2362 	amsdu_aggr_ctrl.enable = true;
2363 	/* Send request to firmware */
2364 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_AMSDU_AGGR_CTRL,
2365 			       HostCmd_ACT_GEN_SET, 0,
2366 			       &amsdu_aggr_ctrl, true);
2367 	if (ret)
2368 		return -1;
2369 	/* MAC Control must be the last command in init_fw */
2370 	/* set MAC Control */
2371 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
2372 			       HostCmd_ACT_GEN_SET, 0,
2373 			       &priv->curr_pkt_filter, true);
2374 	if (ret)
2375 		return -1;
2376 
2377 	if (!disable_auto_ds && first_sta &&
2378 	    priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2379 		/* Enable auto deep sleep */
2380 		auto_ds.auto_ds = DEEP_SLEEP_ON;
2381 		auto_ds.idle_time = DEEP_SLEEP_IDLE_TIME;
2382 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_PS_MODE_ENH,
2383 				       EN_AUTO_PS, BITMAP_AUTO_DS,
2384 				       &auto_ds, true);
2385 		if (ret)
2386 			return -1;
2387 	}
2388 
2389 	if (priv->bss_type != MWIFIEX_BSS_TYPE_UAP) {
2390 		/* Send cmd to FW to enable/disable 11D function */
2391 		state_11d = ENABLE_11D;
2392 		ret = mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
2393 				       HostCmd_ACT_GEN_SET, DOT11D_I,
2394 				       &state_11d, true);
2395 		if (ret)
2396 			mwifiex_dbg(priv->adapter, ERROR,
2397 				    "11D: failed to enable 11D\n");
2398 	}
2399 
2400 	/* Pacekt aggregation handshake with firmware */
2401 	if (aggr_ctrl) {
2402 		packet_aggr_enable = true;
2403 		mwifiex_send_cmd(priv, HostCmd_CMD_PACKET_AGGR_CTRL,
2404 				 HostCmd_ACT_GEN_SET, 0,
2405 				 &packet_aggr_enable, true);
2406 	}
2407 
2408 	/* Send cmd to FW to configure 11n specific configuration
2409 	 * (Short GI, Channel BW, Green field support etc.) for transmit
2410 	 */
2411 	tx_cfg.tx_htcap = MWIFIEX_FW_DEF_HTTXCFG;
2412 	ret = mwifiex_send_cmd(priv, HostCmd_CMD_11N_CFG,
2413 			       HostCmd_ACT_GEN_SET, 0, &tx_cfg, true);
2414 
2415 	if (init) {
2416 		/* set last_init_cmd before sending the command */
2417 		priv->adapter->last_init_cmd = HostCmd_CMD_11N_CFG;
2418 		ret = -EINPROGRESS;
2419 	}
2420 
2421 	return ret;
2422 }
2423