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  */
15 int 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  */
146 void
147 mwifiex_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  */
174 void 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  */
196 void 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  */
213 void 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  */
245 void
246 mwifiex_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  */
279 void 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  */
296 static void
297 mwifiex_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  */
373 void
374 mwifiex_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  */
408 static void
409 mwifiex_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  */
445 void 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 */
468 static int
469 mwifiex_uap_bss_param_prepare(u8 *tlv, void *cmd_buf, u16 *param_size)
470 {
471 	struct host_cmd_tlv_dtim_period *dtim_period;
472 	struct host_cmd_tlv_beacon_period *beacon_period;
473 	struct host_cmd_tlv_ssid *ssid;
474 	struct host_cmd_tlv_bcast_ssid *bcast_ssid;
475 	struct host_cmd_tlv_channel_band *chan_band;
476 	struct host_cmd_tlv_frag_threshold *frag_threshold;
477 	struct host_cmd_tlv_rts_threshold *rts_threshold;
478 	struct host_cmd_tlv_retry_limit *retry_limit;
479 	struct host_cmd_tlv_encrypt_protocol *encrypt_protocol;
480 	struct host_cmd_tlv_auth_type *auth_type;
481 	struct host_cmd_tlv_rates *tlv_rates;
482 	struct host_cmd_tlv_ageout_timer *ao_timer, *ps_ao_timer;
483 	struct host_cmd_tlv_power_constraint *pwr_ct;
484 	struct mwifiex_ie_types_htcap *htcap;
485 	struct mwifiex_ie_types_wmmcap *wmm_cap;
486 	struct mwifiex_uap_bss_param *bss_cfg = cmd_buf;
487 	int i;
488 	u16 cmd_size = *param_size;
489 
490 	if (bss_cfg->ssid.ssid_len) {
491 		ssid = (struct host_cmd_tlv_ssid *)tlv;
492 		ssid->header.type = cpu_to_le16(TLV_TYPE_UAP_SSID);
493 		ssid->header.len = cpu_to_le16((u16)bss_cfg->ssid.ssid_len);
494 		memcpy(ssid->ssid, bss_cfg->ssid.ssid, bss_cfg->ssid.ssid_len);
495 		cmd_size += sizeof(struct mwifiex_ie_types_header) +
496 			    bss_cfg->ssid.ssid_len;
497 		tlv += sizeof(struct mwifiex_ie_types_header) +
498 				bss_cfg->ssid.ssid_len;
499 
500 		bcast_ssid = (struct host_cmd_tlv_bcast_ssid *)tlv;
501 		bcast_ssid->header.type = cpu_to_le16(TLV_TYPE_UAP_BCAST_SSID);
502 		bcast_ssid->header.len =
503 				cpu_to_le16(sizeof(bcast_ssid->bcast_ctl));
504 		bcast_ssid->bcast_ctl = bss_cfg->bcast_ssid_ctl;
505 		cmd_size += sizeof(struct host_cmd_tlv_bcast_ssid);
506 		tlv += sizeof(struct host_cmd_tlv_bcast_ssid);
507 	}
508 	if (bss_cfg->rates[0]) {
509 		tlv_rates = (struct host_cmd_tlv_rates *)tlv;
510 		tlv_rates->header.type = cpu_to_le16(TLV_TYPE_UAP_RATES);
511 
512 		for (i = 0; i < MWIFIEX_SUPPORTED_RATES && bss_cfg->rates[i];
513 		     i++)
514 			tlv_rates->rates[i] = bss_cfg->rates[i];
515 
516 		tlv_rates->header.len = cpu_to_le16(i);
517 		cmd_size += sizeof(struct host_cmd_tlv_rates) + i;
518 		tlv += sizeof(struct host_cmd_tlv_rates) + i;
519 	}
520 	if (bss_cfg->channel &&
521 	    (((bss_cfg->band_cfg & BIT(0)) == BAND_CONFIG_BG &&
522 	      bss_cfg->channel <= MAX_CHANNEL_BAND_BG) ||
523 	    ((bss_cfg->band_cfg & BIT(0)) == BAND_CONFIG_A &&
524 	     bss_cfg->channel <= MAX_CHANNEL_BAND_A))) {
525 		chan_band = (struct host_cmd_tlv_channel_band *)tlv;
526 		chan_band->header.type = cpu_to_le16(TLV_TYPE_CHANNELBANDLIST);
527 		chan_band->header.len =
528 			cpu_to_le16(sizeof(struct host_cmd_tlv_channel_band) -
529 				    sizeof(struct mwifiex_ie_types_header));
530 		chan_band->band_config = bss_cfg->band_cfg;
531 		chan_band->channel = bss_cfg->channel;
532 		cmd_size += sizeof(struct host_cmd_tlv_channel_band);
533 		tlv += sizeof(struct host_cmd_tlv_channel_band);
534 	}
535 	if (bss_cfg->beacon_period >= MIN_BEACON_PERIOD &&
536 	    bss_cfg->beacon_period <= MAX_BEACON_PERIOD) {
537 		beacon_period = (struct host_cmd_tlv_beacon_period *)tlv;
538 		beacon_period->header.type =
539 					cpu_to_le16(TLV_TYPE_UAP_BEACON_PERIOD);
540 		beacon_period->header.len =
541 			cpu_to_le16(sizeof(struct host_cmd_tlv_beacon_period) -
542 				    sizeof(struct mwifiex_ie_types_header));
543 		beacon_period->period = cpu_to_le16(bss_cfg->beacon_period);
544 		cmd_size += sizeof(struct host_cmd_tlv_beacon_period);
545 		tlv += sizeof(struct host_cmd_tlv_beacon_period);
546 	}
547 	if (bss_cfg->dtim_period >= MIN_DTIM_PERIOD &&
548 	    bss_cfg->dtim_period <= MAX_DTIM_PERIOD) {
549 		dtim_period = (struct host_cmd_tlv_dtim_period *)tlv;
550 		dtim_period->header.type =
551 			cpu_to_le16(TLV_TYPE_UAP_DTIM_PERIOD);
552 		dtim_period->header.len =
553 			cpu_to_le16(sizeof(struct host_cmd_tlv_dtim_period) -
554 				    sizeof(struct mwifiex_ie_types_header));
555 		dtim_period->period = bss_cfg->dtim_period;
556 		cmd_size += sizeof(struct host_cmd_tlv_dtim_period);
557 		tlv += sizeof(struct host_cmd_tlv_dtim_period);
558 	}
559 	if (bss_cfg->rts_threshold <= MWIFIEX_RTS_MAX_VALUE) {
560 		rts_threshold = (struct host_cmd_tlv_rts_threshold *)tlv;
561 		rts_threshold->header.type =
562 					cpu_to_le16(TLV_TYPE_UAP_RTS_THRESHOLD);
563 		rts_threshold->header.len =
564 			cpu_to_le16(sizeof(struct host_cmd_tlv_rts_threshold) -
565 				    sizeof(struct mwifiex_ie_types_header));
566 		rts_threshold->rts_thr = cpu_to_le16(bss_cfg->rts_threshold);
567 		cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
568 		tlv += sizeof(struct host_cmd_tlv_frag_threshold);
569 	}
570 	if ((bss_cfg->frag_threshold >= MWIFIEX_FRAG_MIN_VALUE) &&
571 	    (bss_cfg->frag_threshold <= MWIFIEX_FRAG_MAX_VALUE)) {
572 		frag_threshold = (struct host_cmd_tlv_frag_threshold *)tlv;
573 		frag_threshold->header.type =
574 				cpu_to_le16(TLV_TYPE_UAP_FRAG_THRESHOLD);
575 		frag_threshold->header.len =
576 			cpu_to_le16(sizeof(struct host_cmd_tlv_frag_threshold) -
577 				    sizeof(struct mwifiex_ie_types_header));
578 		frag_threshold->frag_thr = cpu_to_le16(bss_cfg->frag_threshold);
579 		cmd_size += sizeof(struct host_cmd_tlv_frag_threshold);
580 		tlv += sizeof(struct host_cmd_tlv_frag_threshold);
581 	}
582 	if (bss_cfg->retry_limit <= MWIFIEX_RETRY_LIMIT) {
583 		retry_limit = (struct host_cmd_tlv_retry_limit *)tlv;
584 		retry_limit->header.type =
585 			cpu_to_le16(TLV_TYPE_UAP_RETRY_LIMIT);
586 		retry_limit->header.len =
587 			cpu_to_le16(sizeof(struct host_cmd_tlv_retry_limit) -
588 				    sizeof(struct mwifiex_ie_types_header));
589 		retry_limit->limit = (u8)bss_cfg->retry_limit;
590 		cmd_size += sizeof(struct host_cmd_tlv_retry_limit);
591 		tlv += sizeof(struct host_cmd_tlv_retry_limit);
592 	}
593 	if ((bss_cfg->protocol & PROTOCOL_WPA) ||
594 	    (bss_cfg->protocol & PROTOCOL_WPA2) ||
595 	    (bss_cfg->protocol & PROTOCOL_EAP))
596 		mwifiex_uap_bss_wpa(&tlv, cmd_buf, &cmd_size);
597 	else
598 		mwifiex_uap_bss_wep(&tlv, cmd_buf, &cmd_size);
599 
600 	if ((bss_cfg->auth_mode <= WLAN_AUTH_SHARED_KEY) ||
601 	    (bss_cfg->auth_mode == MWIFIEX_AUTH_MODE_AUTO)) {
602 		auth_type = (struct host_cmd_tlv_auth_type *)tlv;
603 		auth_type->header.type = cpu_to_le16(TLV_TYPE_AUTH_TYPE);
604 		auth_type->header.len =
605 			cpu_to_le16(sizeof(struct host_cmd_tlv_auth_type) -
606 			sizeof(struct mwifiex_ie_types_header));
607 		auth_type->auth_type = (u8)bss_cfg->auth_mode;
608 		cmd_size += sizeof(struct host_cmd_tlv_auth_type);
609 		tlv += sizeof(struct host_cmd_tlv_auth_type);
610 	}
611 	if (bss_cfg->protocol) {
612 		encrypt_protocol = (struct host_cmd_tlv_encrypt_protocol *)tlv;
613 		encrypt_protocol->header.type =
614 			cpu_to_le16(TLV_TYPE_UAP_ENCRY_PROTOCOL);
615 		encrypt_protocol->header.len =
616 			cpu_to_le16(sizeof(struct host_cmd_tlv_encrypt_protocol)
617 			- sizeof(struct mwifiex_ie_types_header));
618 		encrypt_protocol->proto = cpu_to_le16(bss_cfg->protocol);
619 		cmd_size += sizeof(struct host_cmd_tlv_encrypt_protocol);
620 		tlv += sizeof(struct host_cmd_tlv_encrypt_protocol);
621 	}
622 
623 	if (bss_cfg->ht_cap.cap_info) {
624 		htcap = (struct mwifiex_ie_types_htcap *)tlv;
625 		htcap->header.type = cpu_to_le16(WLAN_EID_HT_CAPABILITY);
626 		htcap->header.len =
627 				cpu_to_le16(sizeof(struct ieee80211_ht_cap));
628 		htcap->ht_cap.cap_info = bss_cfg->ht_cap.cap_info;
629 		htcap->ht_cap.ampdu_params_info =
630 					     bss_cfg->ht_cap.ampdu_params_info;
631 		memcpy(&htcap->ht_cap.mcs, &bss_cfg->ht_cap.mcs,
632 		       sizeof(struct ieee80211_mcs_info));
633 		htcap->ht_cap.extended_ht_cap_info =
634 					bss_cfg->ht_cap.extended_ht_cap_info;
635 		htcap->ht_cap.tx_BF_cap_info = bss_cfg->ht_cap.tx_BF_cap_info;
636 		htcap->ht_cap.antenna_selection_info =
637 					bss_cfg->ht_cap.antenna_selection_info;
638 		cmd_size += sizeof(struct mwifiex_ie_types_htcap);
639 		tlv += sizeof(struct mwifiex_ie_types_htcap);
640 	}
641 
642 	if (bss_cfg->wmm_info.qos_info != 0xFF) {
643 		wmm_cap = (struct mwifiex_ie_types_wmmcap *)tlv;
644 		wmm_cap->header.type = cpu_to_le16(WLAN_EID_VENDOR_SPECIFIC);
645 		wmm_cap->header.len = cpu_to_le16(sizeof(wmm_cap->wmm_info));
646 		memcpy(&wmm_cap->wmm_info, &bss_cfg->wmm_info,
647 		       sizeof(wmm_cap->wmm_info));
648 		cmd_size += sizeof(struct mwifiex_ie_types_wmmcap);
649 		tlv += sizeof(struct mwifiex_ie_types_wmmcap);
650 	}
651 
652 	if (bss_cfg->sta_ao_timer) {
653 		ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
654 		ao_timer->header.type = cpu_to_le16(TLV_TYPE_UAP_AO_TIMER);
655 		ao_timer->header.len = cpu_to_le16(sizeof(*ao_timer) -
656 					sizeof(struct mwifiex_ie_types_header));
657 		ao_timer->sta_ao_timer = cpu_to_le32(bss_cfg->sta_ao_timer);
658 		cmd_size += sizeof(*ao_timer);
659 		tlv += sizeof(*ao_timer);
660 	}
661 
662 	if (bss_cfg->power_constraint) {
663 		pwr_ct = (void *)tlv;
664 		pwr_ct->header.type = cpu_to_le16(TLV_TYPE_PWR_CONSTRAINT);
665 		pwr_ct->header.len = cpu_to_le16(sizeof(u8));
666 		pwr_ct->constraint = bss_cfg->power_constraint;
667 		cmd_size += sizeof(*pwr_ct);
668 		tlv += sizeof(*pwr_ct);
669 	}
670 
671 	if (bss_cfg->ps_sta_ao_timer) {
672 		ps_ao_timer = (struct host_cmd_tlv_ageout_timer *)tlv;
673 		ps_ao_timer->header.type =
674 				cpu_to_le16(TLV_TYPE_UAP_PS_AO_TIMER);
675 		ps_ao_timer->header.len = cpu_to_le16(sizeof(*ps_ao_timer) -
676 				sizeof(struct mwifiex_ie_types_header));
677 		ps_ao_timer->sta_ao_timer =
678 					cpu_to_le32(bss_cfg->ps_sta_ao_timer);
679 		cmd_size += sizeof(*ps_ao_timer);
680 		tlv += sizeof(*ps_ao_timer);
681 	}
682 
683 	*param_size = cmd_size;
684 
685 	return 0;
686 }
687 
688 /* This function parses custom IEs from IE list and prepares command buffer */
689 static int mwifiex_uap_custom_ie_prepare(u8 *tlv, void *cmd_buf, u16 *ie_size)
690 {
691 	struct mwifiex_ie_list *ap_ie = cmd_buf;
692 	struct mwifiex_ie_types_header *tlv_ie = (void *)tlv;
693 
694 	if (!ap_ie || !ap_ie->len)
695 		return -1;
696 
697 	*ie_size += le16_to_cpu(ap_ie->len) +
698 			sizeof(struct mwifiex_ie_types_header);
699 
700 	tlv_ie->type = cpu_to_le16(TLV_TYPE_MGMT_IE);
701 	tlv_ie->len = ap_ie->len;
702 	tlv += sizeof(struct mwifiex_ie_types_header);
703 
704 	memcpy(tlv, ap_ie->ie_list, le16_to_cpu(ap_ie->len));
705 
706 	return 0;
707 }
708 
709 /* Parse AP config structure and prepare TLV based command structure
710  * to be sent to FW for uAP configuration
711  */
712 static int
713 mwifiex_cmd_uap_sys_config(struct host_cmd_ds_command *cmd, u16 cmd_action,
714 			   u32 type, void *cmd_buf)
715 {
716 	u8 *tlv;
717 	u16 cmd_size, param_size, ie_size;
718 	struct host_cmd_ds_sys_config *sys_cfg;
719 
720 	cmd->command = cpu_to_le16(HostCmd_CMD_UAP_SYS_CONFIG);
721 	cmd_size = (u16)(sizeof(struct host_cmd_ds_sys_config) + S_DS_GEN);
722 	sys_cfg = (struct host_cmd_ds_sys_config *)&cmd->params.uap_sys_config;
723 	sys_cfg->action = cpu_to_le16(cmd_action);
724 	tlv = sys_cfg->tlv;
725 
726 	switch (type) {
727 	case UAP_BSS_PARAMS_I:
728 		param_size = cmd_size;
729 		if (mwifiex_uap_bss_param_prepare(tlv, cmd_buf, &param_size))
730 			return -1;
731 		cmd->size = cpu_to_le16(param_size);
732 		break;
733 	case UAP_CUSTOM_IE_I:
734 		ie_size = cmd_size;
735 		if (mwifiex_uap_custom_ie_prepare(tlv, cmd_buf, &ie_size))
736 			return -1;
737 		cmd->size = cpu_to_le16(ie_size);
738 		break;
739 	default:
740 		return -1;
741 	}
742 
743 	return 0;
744 }
745 
746 /* This function prepares AP specific deauth command with mac supplied in
747  * function parameter.
748  */
749 static int mwifiex_cmd_uap_sta_deauth(struct mwifiex_private *priv,
750 				      struct host_cmd_ds_command *cmd, u8 *mac)
751 {
752 	struct host_cmd_ds_sta_deauth *sta_deauth = &cmd->params.sta_deauth;
753 
754 	cmd->command = cpu_to_le16(HostCmd_CMD_UAP_STA_DEAUTH);
755 	memcpy(sta_deauth->mac, mac, ETH_ALEN);
756 	sta_deauth->reason = cpu_to_le16(WLAN_REASON_DEAUTH_LEAVING);
757 
758 	cmd->size = cpu_to_le16(sizeof(struct host_cmd_ds_sta_deauth) +
759 				S_DS_GEN);
760 	return 0;
761 }
762 
763 /* This function prepares the AP specific commands before sending them
764  * to the firmware.
765  * This is a generic function which calls specific command preparation
766  * routines based upon the command number.
767  */
768 int mwifiex_uap_prepare_cmd(struct mwifiex_private *priv, u16 cmd_no,
769 			    u16 cmd_action, u32 type,
770 			    void *data_buf, void *cmd_buf)
771 {
772 	struct host_cmd_ds_command *cmd = cmd_buf;
773 
774 	switch (cmd_no) {
775 	case HostCmd_CMD_UAP_SYS_CONFIG:
776 		if (mwifiex_cmd_uap_sys_config(cmd, cmd_action, type, data_buf))
777 			return -1;
778 		break;
779 	case HostCmd_CMD_UAP_BSS_START:
780 	case HostCmd_CMD_UAP_BSS_STOP:
781 	case HOST_CMD_APCMD_SYS_RESET:
782 	case HOST_CMD_APCMD_STA_LIST:
783 		cmd->command = cpu_to_le16(cmd_no);
784 		cmd->size = cpu_to_le16(S_DS_GEN);
785 		break;
786 	case HostCmd_CMD_UAP_STA_DEAUTH:
787 		if (mwifiex_cmd_uap_sta_deauth(priv, cmd, data_buf))
788 			return -1;
789 		break;
790 	case HostCmd_CMD_CHAN_REPORT_REQUEST:
791 		if (mwifiex_cmd_issue_chan_report_request(priv, cmd_buf,
792 							  data_buf))
793 			return -1;
794 		break;
795 	default:
796 		mwifiex_dbg(priv->adapter, ERROR,
797 			    "PREP_CMD: unknown cmd %#x\n", cmd_no);
798 		return -1;
799 	}
800 
801 	return 0;
802 }
803 
804 void mwifiex_uap_set_channel(struct mwifiex_private *priv,
805 			     struct mwifiex_uap_bss_param *bss_cfg,
806 			     struct cfg80211_chan_def chandef)
807 {
808 	u8 config_bands = 0, old_bands = priv->adapter->config_bands;
809 
810 	priv->bss_chandef = chandef;
811 
812 	bss_cfg->channel = ieee80211_frequency_to_channel(
813 						     chandef.chan->center_freq);
814 
815 	/* Set appropriate bands */
816 	if (chandef.chan->band == NL80211_BAND_2GHZ) {
817 		bss_cfg->band_cfg = BAND_CONFIG_BG;
818 		config_bands = BAND_B | BAND_G;
819 
820 		if (chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
821 			config_bands |= BAND_GN;
822 	} else {
823 		bss_cfg->band_cfg = BAND_CONFIG_A;
824 		config_bands = BAND_A;
825 
826 		if (chandef.width > NL80211_CHAN_WIDTH_20_NOHT)
827 			config_bands |= BAND_AN;
828 
829 		if (chandef.width > NL80211_CHAN_WIDTH_40)
830 			config_bands |= BAND_AAC;
831 	}
832 
833 	switch (chandef.width) {
834 	case NL80211_CHAN_WIDTH_5:
835 	case NL80211_CHAN_WIDTH_10:
836 	case NL80211_CHAN_WIDTH_20_NOHT:
837 	case NL80211_CHAN_WIDTH_20:
838 		break;
839 	case NL80211_CHAN_WIDTH_40:
840 		if (chandef.center_freq1 < chandef.chan->center_freq)
841 			bss_cfg->band_cfg |= MWIFIEX_SEC_CHAN_BELOW;
842 		else
843 			bss_cfg->band_cfg |= MWIFIEX_SEC_CHAN_ABOVE;
844 		break;
845 	case NL80211_CHAN_WIDTH_80:
846 	case NL80211_CHAN_WIDTH_80P80:
847 	case NL80211_CHAN_WIDTH_160:
848 		bss_cfg->band_cfg |=
849 		    mwifiex_get_sec_chan_offset(bss_cfg->channel) << 4;
850 		break;
851 	default:
852 		mwifiex_dbg(priv->adapter,
853 			    WARN, "Unknown channel width: %d\n",
854 			    chandef.width);
855 		break;
856 	}
857 
858 	priv->adapter->config_bands = config_bands;
859 
860 	if (old_bands != config_bands) {
861 		mwifiex_send_domain_info_cmd_fw(priv->adapter->wiphy);
862 		mwifiex_dnld_txpwr_table(priv);
863 	}
864 }
865 
866 int mwifiex_config_start_uap(struct mwifiex_private *priv,
867 			     struct mwifiex_uap_bss_param *bss_cfg)
868 {
869 	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_SYS_CONFIG,
870 			     HostCmd_ACT_GEN_SET,
871 			     UAP_BSS_PARAMS_I, bss_cfg, true)) {
872 		mwifiex_dbg(priv->adapter, ERROR,
873 			    "Failed to set AP configuration\n");
874 		return -1;
875 	}
876 
877 	if (mwifiex_send_cmd(priv, HostCmd_CMD_UAP_BSS_START,
878 			     HostCmd_ACT_GEN_SET, 0, NULL, true)) {
879 		mwifiex_dbg(priv->adapter, ERROR,
880 			    "Failed to start the BSS\n");
881 		return -1;
882 	}
883 
884 	if (priv->sec_info.wep_enabled)
885 		priv->curr_pkt_filter |= HostCmd_ACT_MAC_WEP_ENABLE;
886 	else
887 		priv->curr_pkt_filter &= ~HostCmd_ACT_MAC_WEP_ENABLE;
888 
889 	if (mwifiex_send_cmd(priv, HostCmd_CMD_MAC_CONTROL,
890 			     HostCmd_ACT_GEN_SET, 0,
891 			     &priv->curr_pkt_filter, true))
892 		return -1;
893 
894 	return 0;
895 }
896