1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * NXP Wireless LAN device driver: AP specific command handling
4 *
5 * Copyright 2011-2020 NXP
6 */
7
8#include "main.h"
9#include "11ac.h"
10#include "11n.h"
11
12/* This function parses security related parameters from cfg80211_ap_settings
13 * and sets into FW understandable bss_config structure.
14 */
15int mwifiex_set_secure_params(struct mwifiex_private *priv,
16			      struct mwifiex_uap_bss_param *bss_config,
17			      struct cfg80211_ap_settings *params) {
18	int i;
19	struct mwifiex_wep_key wep_key;
20
21	if (!params->privacy) {
22		bss_config->protocol = PROTOCOL_NO_SECURITY;
23		bss_config->key_mgmt = KEY_MGMT_NONE;
24		bss_config->wpa_cfg.length = 0;
25		priv->sec_info.wep_enabled = 0;
26		priv->sec_info.wpa_enabled = 0;
27		priv->sec_info.wpa2_enabled = 0;
28
29		return 0;
30	}
31
32	switch (params->auth_type) {
33	case NL80211_AUTHTYPE_OPEN_SYSTEM:
34		bss_config->auth_mode = WLAN_AUTH_OPEN;
35		break;
36	case NL80211_AUTHTYPE_SHARED_KEY:
37		bss_config->auth_mode = WLAN_AUTH_SHARED_KEY;
38		break;
39	case NL80211_AUTHTYPE_NETWORK_EAP:
40		bss_config->auth_mode = WLAN_AUTH_LEAP;
41		break;
42	default:
43		bss_config->auth_mode = MWIFIEX_AUTH_MODE_AUTO;
44		break;
45	}
46
47	bss_config->key_mgmt_operation |= KEY_MGMT_ON_HOST;
48
49	for (i = 0; i < params->crypto.n_akm_suites; i++) {
50		switch (params->crypto.akm_suites[i]) {
51		case WLAN_AKM_SUITE_8021X:
52			if (params->crypto.wpa_versions &
53			    NL80211_WPA_VERSION_1) {
54				bss_config->protocol = PROTOCOL_WPA;
55				bss_config->key_mgmt = KEY_MGMT_EAP;
56			}
57			if (params->crypto.wpa_versions &
58			    NL80211_WPA_VERSION_2) {
59				bss_config->protocol |= PROTOCOL_WPA2;
60				bss_config->key_mgmt = KEY_MGMT_EAP;
61			}
62			break;
63		case WLAN_AKM_SUITE_PSK:
64			if (params->crypto.wpa_versions &
65			    NL80211_WPA_VERSION_1) {
66				bss_config->protocol = PROTOCOL_WPA;
67				bss_config->key_mgmt = KEY_MGMT_PSK;
68			}
69			if (params->crypto.wpa_versions &
70			    NL80211_WPA_VERSION_2) {
71				bss_config->protocol |= PROTOCOL_WPA2;
72				bss_config->key_mgmt = KEY_MGMT_PSK;
73			}
74			break;
75		default:
76			break;
77		}
78	}
79	for (i = 0; i < params->crypto.n_ciphers_pairwise; i++) {
80		switch (params->crypto.ciphers_pairwise[i]) {
81		case WLAN_CIPHER_SUITE_WEP40:
82		case WLAN_CIPHER_SUITE_WEP104:
83			break;
84		case WLAN_CIPHER_SUITE_TKIP:
85			if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
86				bss_config->wpa_cfg.pairwise_cipher_wpa |=
87								CIPHER_TKIP;
88			if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
89				bss_config->wpa_cfg.pairwise_cipher_wpa2 |=
90								CIPHER_TKIP;
91			break;
92		case WLAN_CIPHER_SUITE_CCMP:
93			if (params->crypto.wpa_versions & NL80211_WPA_VERSION_1)
94				bss_config->wpa_cfg.pairwise_cipher_wpa |=
95								CIPHER_AES_CCMP;
96			if (params->crypto.wpa_versions & NL80211_WPA_VERSION_2)
97				bss_config->wpa_cfg.pairwise_cipher_wpa2 |=
98								CIPHER_AES_CCMP;
99			break;
100		default:
101			break;
102		}
103	}
104
105	switch (params->crypto.cipher_group) {
106	case WLAN_CIPHER_SUITE_WEP40:
107	case WLAN_CIPHER_SUITE_WEP104:
108		if (priv->sec_info.wep_enabled) {
109			bss_config->protocol = PROTOCOL_STATIC_WEP;
110			bss_config->key_mgmt = KEY_MGMT_NONE;
111			bss_config->wpa_cfg.length = 0;
112
113			for (i = 0; i < NUM_WEP_KEYS; i++) {
114				wep_key = priv->wep_key[i];
115				bss_config->wep_cfg[i].key_index = i;
116
117				if (priv->wep_key_curr_index == i)
118					bss_config->wep_cfg[i].is_default = 1;
119				else
120					bss_config->wep_cfg[i].is_default = 0;
121
122				bss_config->wep_cfg[i].length =
123							     wep_key.key_length;
124				memcpy(&bss_config->wep_cfg[i].key,
125				       &wep_key.key_material,
126				       wep_key.key_length);
127			}
128		}
129		break;
130	case WLAN_CIPHER_SUITE_TKIP:
131		bss_config->wpa_cfg.group_cipher = CIPHER_TKIP;
132		break;
133	case WLAN_CIPHER_SUITE_CCMP:
134		bss_config->wpa_cfg.group_cipher = CIPHER_AES_CCMP;
135		break;
136	default:
137		break;
138	}
139
140	return 0;
141}
142
143/* This function updates 11n related parameters from IE and sets them into
144 * bss_config structure.
145 */
146void
147mwifiex_set_ht_params(struct mwifiex_private *priv,
148		      struct mwifiex_uap_bss_param *bss_cfg,
149		      struct cfg80211_ap_settings *params)
150{
151	const u8 *ht_ie;
152
153	if (!ISSUPP_11NENABLED(priv->adapter->fw_cap_info))
154		return;
155
156	ht_ie = cfg80211_find_ie(WLAN_EID_HT_CAPABILITY, params->beacon.tail,
157				 params->beacon.tail_len);
158	if (ht_ie) {
159		memcpy(&bss_cfg->ht_cap, ht_ie + 2,
160		       sizeof(struct ieee80211_ht_cap));
161		priv->ap_11n_enabled = 1;
162	} else {
163		memset(&bss_cfg->ht_cap, 0, sizeof(struct ieee80211_ht_cap));
164		bss_cfg->ht_cap.cap_info = cpu_to_le16(MWIFIEX_DEF_HT_CAP);
165		bss_cfg->ht_cap.ampdu_params_info = MWIFIEX_DEF_AMPDU;
166	}
167
168	return;
169}
170
171/* This function updates 11ac related parameters from IE
172 * and sets them into bss_config structure.
173 */
174void mwifiex_set_vht_params(struct mwifiex_private *priv,
175			    struct mwifiex_uap_bss_param *bss_cfg,
176			    struct cfg80211_ap_settings *params)
177{
178	const u8 *vht_ie;
179
180	vht_ie = cfg80211_find_ie(WLAN_EID_VHT_CAPABILITY, params->beacon.tail,
181				  params->beacon.tail_len);
182	if (vht_ie) {
183		memcpy(&bss_cfg->vht_cap, vht_ie + 2,
184		       sizeof(struct ieee80211_vht_cap));
185		priv->ap_11ac_enabled = 1;
186	} else {
187		priv->ap_11ac_enabled = 0;
188	}
189
190	return;
191}
192
193/* This function updates 11ac related parameters from IE
194 * and sets them into bss_config structure.
195 */
196void mwifiex_set_tpc_params(struct mwifiex_private *priv,
197			    struct mwifiex_uap_bss_param *bss_cfg,
198			    struct cfg80211_ap_settings *params)
199{
200	const u8 *tpc_ie;
201
202	tpc_ie = cfg80211_find_ie(WLAN_EID_TPC_REQUEST, params->beacon.tail,
203				  params->beacon.tail_len);
204	if (tpc_ie)
205		bss_cfg->power_constraint = *(tpc_ie + 2);
206	else
207		bss_cfg->power_constraint = 0;
208}
209
210/* Enable VHT only when cfg80211_ap_settings has VHT IE.
211 * Otherwise disable VHT.
212 */
213void mwifiex_set_vht_width(struct mwifiex_private *priv,
214			   enum nl80211_chan_width width,
215			   bool ap_11ac_enable)
216{
217	struct mwifiex_adapter *adapter = priv->adapter;
218	struct mwifiex_11ac_vht_cfg vht_cfg;
219
220	vht_cfg.band_config = VHT_CFG_5GHZ;
221	vht_cfg.cap_info = adapter->hw_dot_11ac_dev_cap;
222
223	if (!ap_11ac_enable) {
224		vht_cfg.mcs_tx_set = DISABLE_VHT_MCS_SET;
225		vht_cfg.mcs_rx_set = DISABLE_VHT_MCS_SET;
226	} else {
227		vht_cfg.mcs_tx_set = DEFAULT_VHT_MCS_SET;
228		vht_cfg.mcs_rx_set = DEFAULT_VHT_MCS_SET;
229	}
230
231	vht_cfg.misc_config  = VHT_CAP_UAP_ONLY;
232
233	if (ap_11ac_enable && width >= NL80211_CHAN_WIDTH_80)
234		vht_cfg.misc_config |= VHT_BW_80_160_80P80;
235
236	mwifiex_send_cmd(priv, HostCmd_CMD_11AC_CFG,
237			 HostCmd_ACT_GEN_SET, 0, &vht_cfg, true);
238
239	return;
240}
241
242/* This function finds supported rates IE from beacon parameter and sets
243 * these rates into bss_config structure.
244 */
245void
246mwifiex_set_uap_rates(struct mwifiex_uap_bss_param *bss_cfg,
247		      struct cfg80211_ap_settings *params)
248{
249	struct ieee_types_header *rate_ie;
250	int var_offset = offsetof(struct ieee80211_mgmt, u.beacon.variable);
251	const u8 *var_pos = params->beacon.head + var_offset;
252	int len = params->beacon.head_len - var_offset;
253	u8 rate_len = 0;
254
255	rate_ie = (void *)cfg80211_find_ie(WLAN_EID_SUPP_RATES, var_pos, len);
256	if (rate_ie) {
257		if (rate_ie->len > MWIFIEX_SUPPORTED_RATES)
258			return;
259		memcpy(bss_cfg->rates, rate_ie + 1, rate_ie->len);
260		rate_len = rate_ie->len;
261	}
262
263	rate_ie = (void *)cfg80211_find_ie(WLAN_EID_EXT_SUPP_RATES,
264					   params->beacon.tail,
265					   params->beacon.tail_len);
266	if (rate_ie) {
267		if (rate_ie->len > MWIFIEX_SUPPORTED_RATES - rate_len)
268			return;
269		memcpy(bss_cfg->rates + rate_len, rate_ie + 1, rate_ie->len);
270	}
271
272	return;
273}
274
275/* This function initializes some of mwifiex_uap_bss_param variables.
276 * This helps FW in ignoring invalid values. These values may or may not
277 * be get updated to valid ones at later stage.
278 */
279void mwifiex_set_sys_config_invalid_data(struct mwifiex_uap_bss_param *config)
280{
281	config->bcast_ssid_ctl = 0x7F;
282	config->radio_ctl = 0x7F;
283	config->dtim_period = 0x7F;
284	config->beacon_period = 0x7FFF;
285	config->auth_mode = 0x7F;
286	config->rts_threshold = 0x7FFF;
287	config->frag_threshold = 0x7FFF;
288	config->retry_limit = 0x7F;
289	config->qos_info = 0xFF;
290}
291
292/* This function parses BSS related parameters from structure
293 * and prepares TLVs specific to WPA/WPA2 security.
294 * These TLVs are appended to command buffer.
295 */
296static void
297mwifiex_uap_bss_wpa(u8 **tlv_buf, void *cmd_buf, u16 *param_size)
298{
299	struct host_cmd_tlv_pwk_cipher *pwk_cipher;
300	struct host_cmd_tlv_gwk_cipher *gwk_cipher;
301	struct host_cmd_tlv_passphrase *passphrase;
302	struct host_cmd_tlv_akmp *tlv_akmp;
303	struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
304	u16 cmd_size = *param_size;
305	u8 *tlv = *tlv_buf;
306
307	tlv_akmp = (struct host_cmd_tlv_akmp *)tlv;
308	tlv_akmp->header.type = cpu_to_le16(TLV_TYPE_UAP_AKMP);
309	tlv_akmp->header.len = cpu_to_le16(sizeof(struct host_cmd_tlv_akmp) -
310					sizeof(struct mwifiex_ie_types_header));
311	tlv_akmp->key_mgmt_operation = cpu_to_le16(bss_cfg->key_mgmt_operation);
312	tlv_akmp->key_mgmt = cpu_to_le16(bss_cfg->key_mgmt);
313	cmd_size += sizeof(struct host_cmd_tlv_akmp);
314	tlv += sizeof(struct host_cmd_tlv_akmp);
315
316	if (bss_cfg->wpa_cfg.pairwise_cipher_wpa & VALID_CIPHER_BITMAP) {
317		pwk_cipher = (struct host_cmd_tlv_pwk_cipher *)tlv;
318		pwk_cipher->header.type = cpu_to_le16(TLV_TYPE_PWK_CIPHER);
319		pwk_cipher->header.len =
320			cpu_to_le16(sizeof(struct host_cmd_tlv_pwk_cipher) -
321				    sizeof(struct mwifiex_ie_types_header));
322		pwk_cipher->proto = cpu_to_le16(PROTOCOL_WPA);
323		pwk_cipher->cipher = bss_cfg->wpa_cfg.pairwise_cipher_wpa;
324		cmd_size += sizeof(struct host_cmd_tlv_pwk_cipher);
325		tlv += sizeof(struct host_cmd_tlv_pwk_cipher);
326	}
327
328	if (bss_cfg->wpa_cfg.pairwise_cipher_wpa2 & VALID_CIPHER_BITMAP) {
329		pwk_cipher = (struct host_cmd_tlv_pwk_cipher *)tlv;
330		pwk_cipher->header.type = cpu_to_le16(TLV_TYPE_PWK_CIPHER);
331		pwk_cipher->header.len =
332			cpu_to_le16(sizeof(struct host_cmd_tlv_pwk_cipher) -
333				    sizeof(struct mwifiex_ie_types_header));
334		pwk_cipher->proto = cpu_to_le16(PROTOCOL_WPA2);
335		pwk_cipher->cipher = bss_cfg->wpa_cfg.pairwise_cipher_wpa2;
336		cmd_size += sizeof(struct host_cmd_tlv_pwk_cipher);
337		tlv += sizeof(struct host_cmd_tlv_pwk_cipher);
338	}
339
340	if (bss_cfg->wpa_cfg.group_cipher & VALID_CIPHER_BITMAP) {
341		gwk_cipher = (struct host_cmd_tlv_gwk_cipher *)tlv;
342		gwk_cipher->header.type = cpu_to_le16(TLV_TYPE_GWK_CIPHER);
343		gwk_cipher->header.len =
344			cpu_to_le16(sizeof(struct host_cmd_tlv_gwk_cipher) -
345				    sizeof(struct mwifiex_ie_types_header));
346		gwk_cipher->cipher = bss_cfg->wpa_cfg.group_cipher;
347		cmd_size += sizeof(struct host_cmd_tlv_gwk_cipher);
348		tlv += sizeof(struct host_cmd_tlv_gwk_cipher);
349	}
350
351	if (bss_cfg->wpa_cfg.length) {
352		passphrase = (struct host_cmd_tlv_passphrase *)tlv;
353		passphrase->header.type =
354				cpu_to_le16(TLV_TYPE_UAP_WPA_PASSPHRASE);
355		passphrase->header.len = cpu_to_le16(bss_cfg->wpa_cfg.length);
356		memcpy(passphrase->passphrase, bss_cfg->wpa_cfg.passphrase,
357		       bss_cfg->wpa_cfg.length);
358		cmd_size += sizeof(struct mwifiex_ie_types_header) +
359			    bss_cfg->wpa_cfg.length;
360		tlv += sizeof(struct mwifiex_ie_types_header) +
361				bss_cfg->wpa_cfg.length;
362	}
363
364	*param_size = cmd_size;
365	*tlv_buf = tlv;
366
367	return;
368}
369
370/* This function parses WMM related parameters from cfg80211_ap_settings
371 * structure and updates bss_config structure.
372 */
373void
374mwifiex_set_wmm_params(struct mwifiex_private *priv,
375		       struct mwifiex_uap_bss_param *bss_cfg,
376		       struct cfg80211_ap_settings *params)
377{
378	const u8 *vendor_ie;
379	const u8 *wmm_ie;
380	static const u8 wmm_oui[] = {0x00, 0x50, 0xf2, 0x02};
381
382	vendor_ie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
383					    WLAN_OUI_TYPE_MICROSOFT_WMM,
384					    params->beacon.tail,
385					    params->beacon.tail_len);
386	if (vendor_ie) {
387		wmm_ie = vendor_ie;
388		if (*(wmm_ie + 1) > sizeof(struct mwifiex_types_wmm_info))
389			return;
390		memcpy(&bss_cfg->wmm_info, wmm_ie +
391		       sizeof(struct ieee_types_header), *(wmm_ie + 1));
392		priv->wmm_enabled = 1;
393	} else {
394		memset(&bss_cfg->wmm_info, 0, sizeof(bss_cfg->wmm_info));
395		memcpy(&bss_cfg->wmm_info.oui, wmm_oui, sizeof(wmm_oui));
396		bss_cfg->wmm_info.subtype = MWIFIEX_WMM_SUBTYPE;
397		bss_cfg->wmm_info.version = MWIFIEX_WMM_VERSION;
398		priv->wmm_enabled = 0;
399	}
400
401	bss_cfg->qos_info = 0x00;
402	return;
403}
404/* This function parses BSS related parameters from structure
405 * and prepares TLVs specific to WEP encryption.
406 * These TLVs are appended to command buffer.
407 */
408static void
409mwifiex_uap_bss_wep(u8 **tlv_buf, void *cmd_buf, u16 *param_size)
410{
411	struct host_cmd_tlv_wep_key *wep_key;
412	u16 cmd_size = *param_size;
413	int i;
414	u8 *tlv = *tlv_buf;
415	struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
416
417	for (i = 0; i < NUM_WEP_KEYS; i++) {
418		if (bss_cfg->wep_cfg[i].length &&
419		    (bss_cfg->wep_cfg[i].length == WLAN_KEY_LEN_WEP40 ||
420		     bss_cfg->wep_cfg[i].length == WLAN_KEY_LEN_WEP104)) {
421			wep_key = (struct host_cmd_tlv_wep_key *)tlv;
422			wep_key->header.type =
423				cpu_to_le16(TLV_TYPE_UAP_WEP_KEY);
424			wep_key->header.len =
425				cpu_to_le16(bss_cfg->wep_cfg[i].length + 2);
426			wep_key->key_index = bss_cfg->wep_cfg[i].key_index;
427			wep_key->is_default = bss_cfg->wep_cfg[i].is_default;
428			memcpy(wep_key->key, bss_cfg->wep_cfg[i].key,
429			       bss_cfg->wep_cfg[i].length);
430			cmd_size += sizeof(struct mwifiex_ie_types_header) + 2 +
431				    bss_cfg->wep_cfg[i].length;
432			tlv += sizeof(struct mwifiex_ie_types_header) + 2 +
433				    bss_cfg->wep_cfg[i].length;
434		}
435	}
436
437	*param_size = cmd_size;
438	*tlv_buf = tlv;
439
440	return;
441}
442
443/* This function enable 11D if userspace set the country IE.
444 */
445void mwifiex_config_uap_11d(struct mwifiex_private *priv,
446			    struct cfg80211_beacon_data *beacon_data)
447{
448	enum state_11d_t state_11d;
449	const u8 *country_ie;
450
451	country_ie = cfg80211_find_ie(WLAN_EID_COUNTRY, beacon_data->tail,
452				      beacon_data->tail_len);
453	if (country_ie) {
454		/* Send cmd to FW to enable 11D function */
455		state_11d = ENABLE_11D;
456		if (mwifiex_send_cmd(priv, HostCmd_CMD_802_11_SNMP_MIB,
457				     HostCmd_ACT_GEN_SET, DOT11D_I,
458				     &state_11d, true)) {
459			mwifiex_dbg(priv->adapter, ERROR,
460				    "11D: failed to enable 11D\n");
461		}
462	}
463}
464
465/* This function parses BSS related parameters from structure
466 * and prepares TLVs. These TLVs are appended to command buffer.
467*/
468static int
469mwifiex_uap_bss_param_prepare(u8 *tlv, void *cmd_buf, u16 *param_size)
470{
471	struct host_cmd_tlv_mac_addr *mac_tlv;
472	struct host_cmd_tlv_dtim_period *dtim_period;
473	struct host_cmd_tlv_beacon_period *beacon_period;
474	struct host_cmd_tlv_ssid *ssid;
475	struct host_cmd_tlv_bcast_ssid *bcast_ssid;
476	struct host_cmd_tlv_channel_band *chan_band;
477	struct host_cmd_tlv_frag_threshold *frag_threshold;
478	struct host_cmd_tlv_rts_threshold *rts_threshold;
479	struct host_cmd_tlv_retry_limit *retry_limit;
480	struct host_cmd_tlv_encrypt_protocol *encrypt_protocol;
481	struct host_cmd_tlv_auth_type *auth_type;
482	struct host_cmd_tlv_rates *tlv_rates;
483	struct host_cmd_tlv_ageout_timer *ao_timer, *ps_ao_timer;
484	struct host_cmd_tlv_power_constraint *pwr_ct;
485	struct mwifiex_ie_types_htcap *htcap;
486	struct mwifiex_ie_types_wmmcap *wmm_cap;
487	struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
488	int i;
489	u16 cmd_size = *param_size;
490
491	mac_tlv = (struct host_cmd_tlv_mac_addr *)tlv;
492	mac_tlv->header.type = cpu_to_le16(TLV_TYPE_UAP_MAC_ADDRESS);
493	mac_tlv->header.len = cpu_to_le16(ETH_ALEN);
494	memcpy(mac_tlv->mac_addr, bss_cfg->mac_addr, ETH_ALEN);
495	cmd_size += sizeof(struct host_cmd_tlv_mac_addr);
496	tlv += sizeof(struct host_cmd_tlv_mac_addr);
497
498	if (bss_cfg->ssid.ssid_len) {
499		ssid = (struct host_cmd_tlv_ssid *)tlv;
500		ssid->header.type = cpu_to_le16(TLV_TYPE_UAP_SSID);
501		ssid->header.len = cpu_to_le16((u16)bss_cfg->ssid.ssid_len);
502		memcpy(ssid->ssid, bss_cfg->ssid.ssid, bss_cfg->ssid.ssid_len);
503		cmd_size += sizeof(struct mwifiex_ie_types_header) +
504			    bss_cfg->ssid.ssid_len;
505		tlv += sizeof(struct mwifiex_ie_types_header) +
506				bss_cfg->ssid.ssid_len;
507
508		bcast_ssid = (struct host_cmd_tlv_bcast_ssid *)tlv;
509		bcast_ssid->header.type = cpu_to_le16(TLV_TYPE_UAP_BCAST_SSID);
510		bcast_ssid->header.len =
511				cpu_to_le16(sizeof(bcast_ssid->bcast_ctl));
512		bcast_ssid->bcast_ctl = bss_cfg->bcast_ssid_ctl;
513		cmd_size += sizeof(struct host_cmd_tlv_bcast_ssid);
514		tlv += sizeof(struct host_cmd_tlv_bcast_ssid);
515	}
516	if (bss_cfg->rates[0]) {
517		tlv_rates = (struct host_cmd_tlv_rates *)tlv;
518		tlv_rates->header.type = cpu_to_le16(TLV_TYPE_UAP_RATES);
519
520		for (i = 0; i < MWIFIEX_SUPPORTED_RATES && bss_cfg->rates[i];
521		     i++)
522			tlv_rates->rates[i] = bss_cfg->rates[i];
523
524		tlv_rates->header.len = cpu_to_le16(i);
525		cmd_size += sizeof(struct host_cmd_tlv_rates) + i;
526		tlv += sizeof(struct host_cmd_tlv_rates) + i;
527	}
528	if (bss_cfg->channel &&
529	    (((bss_cfg->band_cfg & BIT(0)) == BAND_CONFIG_BG &&
530	      bss_cfg->channel <= MAX_CHANNEL_BAND_BG) ||
531	    ((bss_cfg->band_cfg & BIT(0)) == BAND_CONFIG_A &&
532	     bss_cfg->channel <= MAX_CHANNEL_BAND_A))) {
533		chan_band = (struct host_cmd_tlv_channel_band *)tlv;
534		chan_band->header.type = cpu_to_le16(TLV_TYPE_CHANNELBANDLIST);
535		chan_band->header.len =
536			cpu_to_le16(sizeof(struct host_cmd_tlv_channel_band) -
537				    sizeof(struct mwifiex_ie_types_header));
538		chan_band->band_config = bss_cfg->band_cfg;
539		chan_band->channel = bss_cfg->channel;
540		cmd_size += sizeof(struct host_cmd_tlv_channel_band);
541		tlv += sizeof(struct host_cmd_tlv_channel_band);
542	}
543	if (bss_cfg->beacon_period >= MIN_BEACON_PERIOD &&
544	    bss_cfg->beacon_period <= MAX_BEACON_PERIOD) {
545		beacon_period = (struct host_cmd_tlv_beacon_period *)tlv;
546		beacon_period->header.type =
547					cpu_to_le16(TLV_TYPE_UAP_BEACON_PERIOD);
548		beacon_period->header.len =
549			cpu_to_le16(sizeof(struct host_cmd_tlv_beacon_period) -
550				    sizeof(struct mwifiex_ie_types_header));
551		beacon_period->period = cpu_to_le16(bss_cfg->beacon_period);
552		cmd_size += sizeof(struct host_cmd_tlv_beacon_period);
553		tlv += sizeof(struct host_cmd_tlv_beacon_period);
554	}
555	if (bss_cfg->dtim_period >= MIN_DTIM_PERIOD &&
556	    bss_cfg->dtim_period <= MAX_DTIM_PERIOD) {
557		dtim_period = (struct host_cmd_tlv_dtim_period *)tlv;
558		dtim_period->header.type =
559			cpu_to_le16(TLV_TYPE_UAP_DTIM_PERIOD);
560		dtim_period->header.len =
561			cpu_to_le16(sizeof(struct host_cmd_tlv_dtim_period) -
562				    sizeof(struct mwifiex_ie_types_header));
563		dtim_period->period = bss_cfg->dtim_period;
564		cmd_size += sizeof(struct host_cmd_tlv_dtim_period);
565		tlv += sizeof(struct host_cmd_tlv_dtim_period);
566	}
567	if (bss_cfg->rts_threshold <= MWIFIEX_RTS_MAX_VALUE) {
568		rts_threshold = (struct host_cmd_tlv_rts_threshold *)tlv;
569		rts_threshold->header.type =
570					cpu_to_le16(TLV_TYPE_UAP_RTS_THRESHOLD);
571		rts_threshold->header.len =
572			cpu_to_le16(sizeof(struct host_cmd_tlv_rts_threshold) -
573				    sizeof(struct mwifiex_ie_types_header));
574		rts_threshold->rts_thr = cpu_to_le16(bss_cfg->rts_threshold);
575		cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
576		tlv += sizeof(struct host_cmd_tlv_frag_threshold);
577	}
578	if ((bss_cfg->frag_threshold >= MWIFIEX_FRAG_MIN_VALUE) &&
579	    (bss_cfg->frag_threshold <= MWIFIEX_FRAG_MAX_VALUE)) {
580		frag_threshold = (struct host_cmd_tlv_frag_threshold *)tlv;
581		frag_threshold->header.type =
582				cpu_to_le16(TLV_TYPE_UAP_FRAG_THRESHOLD);
583		frag_threshold->header.len =
584			cpu_to_le16(sizeof(struct host_cmd_tlv_frag_threshold) -
585				    sizeof(struct mwifiex_ie_types_header));
586		frag_threshold->frag_thr = cpu_to_le16(bss_cfg->frag_threshold);
587		cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
588		tlv += sizeof(struct host_cmd_tlv_frag_threshold);
589	}
590	if (bss_cfg->retry_limit <= MWIFIEX_RETRY_LIMIT) {
591		retry_limit = (struct host_cmd_tlv_retry_limit *)tlv;
592		retry_limit->header.type =
593			cpu_to_le16(TLV_TYPE_UAP_RETRY_LIMIT);
594		retry_limit->header.len =
595			cpu_to_le16(sizeof(struct host_cmd_tlv_retry_limit) -
596				    sizeof(struct mwifiex_ie_types_header));
597		retry_limit->limit = (u8)bss_cfg->retry_limit;
598		cmd_size += sizeof(struct host_cmd_tlv_retry_limit);
599		tlv += sizeof(struct host_cmd_tlv_retry_limit);
600	}
601	if ((bss_cfg->protocol & PROTOCOL_WPA) ||
602	    (bss_cfg->protocol & PROTOCOL_WPA2) ||
603	    (bss_cfg->protocol & PROTOCOL_EAP))
604		mwifiex_uap_bss_wpa(&tlv, cmd_buf, &cmd_size);
605	else
606		mwifiex_uap_bss_wep(&tlv, cmd_buf, &cmd_size);
607
608	if ((bss_cfg->auth_mode <= WLAN_AUTH_SHARED_KEY) ||
609	    (bss_cfg->auth_mode == MWIFIEX_AUTH_MODE_AUTO)) {
610		auth_type = (struct host_cmd_tlv_auth_type *)tlv;
611		auth_type->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
612		auth_type->header.len =
613			cpu_to_le16(sizeof(struct host_cmd_tlv_auth_type) -
614			sizeof(struct mwifiex_ie_types_header));
615		auth_type->auth_type = (u8)bss_cfg->auth_mode;
616		cmd_size += sizeof(struct host_cmd_tlv_auth_type);
617		tlv += sizeof(struct host_cmd_tlv_auth_type);
618	}
619	if (bss_cfg->protocol) {
620		encrypt_protocol = (struct host_cmd_tlv_encrypt_protocol *)tlv;
621		encrypt_protocol->header.type =
622			cpu_to_le16(TLV_TYPE_UAP_ENCRY_PROTOCOL);
623		encrypt_protocol->header.len =
624			cpu_to_le16(sizeof(struct host_cmd_tlv_encrypt_protocol)
625			- sizeof(struct mwifiex_ie_types_header));
626		encrypt_protocol->proto = cpu_to_le16(bss_cfg->protocol);
627		cmd_size += sizeof(struct host_cmd_tlv_encrypt_protocol);
628		tlv += sizeof(struct host_cmd_tlv_encrypt_protocol);
629	}
630
631	if (bss_cfg->ht_cap.cap_info) {
632		htcap = (struct mwifiex_ie_types_htcap *)tlv;
633		htcap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
634		htcap->header.len =
635				cpu_to_le16(sizeof(struct ieee80211_ht_cap));
636		htcap->ht_cap.cap_info = bss_cfg->ht_cap.cap_info;
637		htcap->ht_cap.ampdu_params_info =
638					     bss_cfg->ht_cap.ampdu_params_info;
639		memcpy(&htcap->ht_cap.mcs, &bss_cfg->ht_cap.mcs,
640		       sizeof(struct ieee80211_mcs_info));
641		htcap->ht_cap.extended_ht_cap_info =
642					bss_cfg->ht_cap.extended_ht_cap_info;
643		htcap->ht_cap.tx_BF_cap_info = bss_cfg->ht_cap.tx_BF_cap_info;
644		htcap->ht_cap.antenna_selection_info =
645					bss_cfg->ht_cap.antenna_selection_info;
646		cmd_size += sizeof(struct mwifiex_ie_types_htcap);
647		tlv += sizeof(struct mwifiex_ie_types_htcap);
648	}
649
650	if (bss_cfg->wmm_info.qos_info != 0xFF) {
651		wmm_cap = (struct mwifiex_ie_types_wmmcap *)tlv;
652		wmm_cap->header.type = cpu_to_le16(WLAN_EID_VENDOR_SPECIFIC);
653		wmm_cap->header.len = cpu_to_le16(sizeof(wmm_cap->wmm_info));
654		memcpy(&wmm_cap->wmm_info, &bss_cfg->wmm_info,
655		       sizeof(wmm_cap->wmm_info));
656		cmd_size += sizeof(struct mwifiex_ie_types_wmmcap);
657		tlv += sizeof(struct mwifiex_ie_types_wmmcap);
658	}
659
660	if (bss_cfg->sta_ao_timer) {
661		ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
662		ao_timer->header.type = cpu_to_le16(TLV_TYPE_UAP_AO_TIMER);
663		ao_timer->header.len = cpu_to_le16(sizeof(*ao_timer) -
664					sizeof(struct mwifiex_ie_types_header));
665		ao_timer->sta_ao_timer = cpu_to_le32(bss_cfg->sta_ao_timer);
666		cmd_size += sizeof(*ao_timer);
667		tlv += sizeof(*ao_timer);
668	}
669
670	if (bss_cfg->power_constraint) {
671		pwr_ct = (void *)tlv;
672		pwr_ct->header.type = cpu_to_le16(TLV_TYPE_PWR_CONSTRAINT);
673		pwr_ct->header.len = cpu_to_le16(sizeof(u8));
674		pwr_ct->constraint = bss_cfg->power_constraint;
675		cmd_size += sizeof(*pwr_ct);
676		tlv += sizeof(*pwr_ct);
677	}
678
679	if (bss_cfg->ps_sta_ao_timer) {
680		ps_ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
681		ps_ao_timer->header.type =
682				cpu_to_le16(TLV_TYPE_UAP_PS_AO_TIMER);
683		ps_ao_timer->header.len = cpu_to_le16(sizeof(*ps_ao_timer) -
684				sizeof(struct mwifiex_ie_types_header));
685		ps_ao_timer->sta_ao_timer =
686					cpu_to_le32(bss_cfg->ps_sta_ao_timer);
687		cmd_size += sizeof(*ps_ao_timer);
688		tlv += sizeof(*ps_ao_timer);
689	}
690
691	*param_size = cmd_size;
692
693	return 0;
694}
695
696/* This function parses custom IEs from IE list and prepares command buffer */
697static int mwifiex_uap_custom_ie_prepare(u8 *tlv, void *cmd_buf, u16 *ie_size)
698{
699	struct mwifiex_ie_list *ap_ie = cmd_buf;
700	struct mwifiex_ie_types_header *tlv_ie = (void *)tlv;
701
702	if (!ap_ie || !ap_ie->len)
703		return -1;
704
705	*ie_size += le16_to_cpu(ap_ie->len) +
706			sizeof(struct mwifiex_ie_types_header);
707
708	tlv_ie->type = cpu_to_le16(TLV_TYPE_MGMT_IE);
709	tlv_ie->len = ap_ie->len;
710	tlv += sizeof(struct mwifiex_ie_types_header);
711
712	memcpy(tlv, ap_ie->ie_list, le16_to_cpu(ap_ie->len));
713
714	return 0;
715}
716
717/* Parse AP config structure and prepare TLV based command structure
718 * to be sent to FW for uAP configuration
719 */
720static int
721mwifiex_cmd_uap_sys_config(struct host_cmd_ds_command *cmd, u16 cmd_action,
722			   u32 type, void *cmd_buf)
723{
724	u8 *tlv;
725	u16 cmd_size, param_size, ie_size;
726	struct host_cmd_ds_sys_config *sys_cfg;
727
728	cmd->command = cpu_to_le16(HostCmd_CMD_UAP_SYS_CONFIG);
729	cmd_size = (u16)(sizeof(struct host_cmd_ds_sys_config) + S_DS_GEN);
730	sys_cfg = (struct host_cmd_ds_sys_config *)&cmd->params.uap_sys_config;
731	sys_cfg->action = cpu_to_le16(cmd_action);
732	tlv = sys_cfg->tlv;
733
734	switch (type) {
735	case UAP_BSS_PARAMS_I:
736		param_size = cmd_size;
737		if (mwifiex_uap_bss_param_prepare(tlv, cmd_buf, &param_size))
738			return -1;
739		cmd->size = cpu_to_le16(param_size);
740		break;
741	case UAP_CUSTOM_IE_I:
742		ie_size = cmd_size;
743		if (mwifiex_uap_custom_ie_prepare(tlv, cmd_buf, &ie_size))
744			return -1;
745		cmd->size = cpu_to_le16(ie_size);
746		break;
747	default:
748		return -1;
749	}
750
751	return 0;
752}
753
754/* This function prepares AP specific deauth command with mac supplied in
755 * function parameter.
756 */
757static int mwifiex_cmd_uap_sta_deauth(struct mwifiex_private *priv,
758				      struct host_cmd_ds_command *cmd, u8 *mac)
759{
760	struct host_cmd_ds_sta_deauth *sta_deauth = &cmd->params.sta_deauth;
761
762	cmd->command = cpu_to_le16(HostCmd_CMD_UAP_STA_DEAUTH);
763	memcpy(sta_deauth->mac, mac, ETH_ALEN);
764	sta_deauth->reason = cpu_to_le16(WLAN_REASON_DEAUTH_LEAVING);
765
766	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_sta_deauth) +
767				S_DS_GEN);
768	return 0;
769}
770
771/* This function prepares the AP specific commands before sending them
772 * to the firmware.
773 * This is a generic function which calls specific command preparation
774 * routines based upon the command number.
775 */
776int mwifiex_uap_prepare_cmd(struct mwifiex_private *priv, u16 cmd_no,
777			    u16 cmd_action, u32 type,
778			    void *data_buf, void *cmd_buf)
779{
780	struct host_cmd_ds_command *cmd = cmd_buf;
781
782	switch (cmd_no) {
783	case HostCmd_CMD_UAP_SYS_CONFIG:
784		if (mwifiex_cmd_uap_sys_config(cmd, cmd_action, type, data_buf))
785			return -1;
786		break;
787	case HostCmd_CMD_UAP_BSS_START:
788	case HostCmd_CMD_UAP_BSS_STOP:
789	case HOST_CMD_APCMD_SYS_RESET:
790	case HOST_CMD_APCMD_STA_LIST:
791		cmd->command = cpu_to_le16(cmd_no);
792		cmd->size = cpu_to_le16(S_DS_GEN);
793		break;
794	case HostCmd_CMD_UAP_STA_DEAUTH:
795		if (mwifiex_cmd_uap_sta_deauth(priv, cmd, data_buf))
796			return -1;
797		break;
798	case HostCmd_CMD_CHAN_REPORT_REQUEST:
799		if (mwifiex_cmd_issue_chan_report_request(priv, cmd_buf,
800							  data_buf))
801			return -1;
802		break;
803	default:
804		mwifiex_dbg(priv->adapter, ERROR,
805			    "PREP_CMD: unknown cmd %#x\n", cmd_no);
806		return -1;
807	}
808
809	return 0;
810}
811
812void mwifiex_uap_set_channel(struct mwifiex_private *priv,
813			     struct mwifiex_uap_bss_param *bss_cfg,
814			     struct cfg80211_chan_def chandef)
815{
816	u8 config_bands = 0, old_bands = priv->adapter->config_bands;
817
818	priv->bss_chandef = chandef;
819
820	bss_cfg->channel = ieee80211_frequency_to_channel(
821						     chandef.chan->center_freq);
822
823	/* Set appropriate bands */
824	if (chandef.chan->band == NL80211_BAND_2GHZ) {
825		bss_cfg->band_cfg = BAND_CONFIG_BG;
826		config_bands = BAND_B | BAND_G;
827
828		if (chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
829			config_bands |= BAND_GN;
830	} else {
831		bss_cfg->band_cfg = BAND_CONFIG_A;
832		config_bands = BAND_A;
833
834		if (chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
835			config_bands |= BAND_AN;
836
837		if (chandef.width > NL80211_CHAN_WIDTH_40)
838			config_bands |= BAND_AAC;
839	}
840
841	switch (chandef.width) {
842	case NL80211_CHAN_WIDTH_5:
843	case NL80211_CHAN_WIDTH_10:
844	case NL80211_CHAN_WIDTH_20_NOHT:
845	case NL80211_CHAN_WIDTH_20:
846		break;
847	case NL80211_CHAN_WIDTH_40:
848		if (chandef.center_freq1 < chandef.chan->center_freq)
849			bss_cfg->band_cfg |= MWIFIEX_SEC_CHAN_BELOW;
850		else
851			bss_cfg->band_cfg |= MWIFIEX_SEC_CHAN_ABOVE;
852		break;
853	case NL80211_CHAN_WIDTH_80:
854	case NL80211_CHAN_WIDTH_80P80:
855	case NL80211_CHAN_WIDTH_160:
856		bss_cfg->band_cfg |=
857		    mwifiex_get_sec_chan_offset(bss_cfg->channel) << 4;
858		break;
859	default:
860		mwifiex_dbg(priv->adapter,
861			    WARN, "Unknown channel width: %d\n",
862			    chandef.width);
863		break;
864	}
865
866	priv->adapter->config_bands = config_bands;
867
868	if (old_bands != config_bands) {
869		mwifiex_send_domain_info_cmd_fw(priv->adapter->wiphy);
870		mwifiex_dnld_txpwr_table(priv);
871	}
872}
873
874int mwifiex_config_start_uap(struct mwifiex_private *priv,
875			     struct mwifiex_uap_bss_param *bss_cfg)
876{
877	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
878			     HostCmd_ACT_GEN_SET,
879			     UAP_BSS_PARAMS_I, bss_cfg, true)) {
880		mwifiex_dbg(priv->adapter, ERROR,
881			    "Failed to set AP configuration\n");
882		return -1;
883	}
884
885	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_START,
886			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
887		mwifiex_dbg(priv->adapter, ERROR,
888			    "Failed to start the BSS\n");
889		return -1;
890	}
891
892	if (priv->sec_info.wep_enabled)
893		priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
894	else
895		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
896
897	if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
898			     HostCmd_ACT_GEN_SET, 0,
899			     &priv->curr_pkt_filter, true))
900		return -1;
901
902	return 0;
903}
904