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