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