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