1 // SPDX-License-Identifier: ISC
2 /*
3  * Copyright (c) 2010 Broadcom Corporation
4  */
5 
6 /* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */
7 
8 #include <linux/kernel.h>
9 #include <linux/etherdevice.h>
10 #include <linux/module.h>
11 #include <linux/vmalloc.h>
12 #include <net/cfg80211.h>
13 #include <net/netlink.h>
14 #include <uapi/linux/if_arp.h>
15 
16 #include <brcmu_utils.h>
17 #include <defs.h>
18 #include <brcmu_wifi.h>
19 #include "core.h"
20 #include "debug.h"
21 #include "tracepoint.h"
22 #include "fwil_types.h"
23 #include "p2p.h"
24 #include "btcoex.h"
25 #include "pno.h"
26 #include "fwsignal.h"
27 #include "cfg80211.h"
28 #include "feature.h"
29 #include "fwil.h"
30 #include "proto.h"
31 #include "vendor.h"
32 #include "bus.h"
33 #include "common.h"
34 
35 #define BRCMF_SCAN_IE_LEN_MAX		2048
36 
37 #define WPA_OUI				"\x00\x50\xF2"	/* WPA OUI */
38 #define WPA_OUI_TYPE			1
39 #define RSN_OUI				"\x00\x0F\xAC"	/* RSN OUI */
40 #define	WME_OUI_TYPE			2
41 #define WPS_OUI_TYPE			4
42 
43 #define VS_IE_FIXED_HDR_LEN		6
44 #define WPA_IE_VERSION_LEN		2
45 #define WPA_IE_MIN_OUI_LEN		4
46 #define WPA_IE_SUITE_COUNT_LEN		2
47 
48 #define WPA_CIPHER_NONE			0	/* None */
49 #define WPA_CIPHER_WEP_40		1	/* WEP (40-bit) */
50 #define WPA_CIPHER_TKIP			2	/* TKIP: default for WPA */
51 #define WPA_CIPHER_AES_CCM		4	/* AES (CCM) */
52 #define WPA_CIPHER_WEP_104		5	/* WEP (104-bit) */
53 
54 #define RSN_AKM_NONE			0	/* None (IBSS) */
55 #define RSN_AKM_UNSPECIFIED		1	/* Over 802.1x */
56 #define RSN_AKM_PSK			2	/* Pre-shared Key */
57 #define RSN_AKM_SHA256_1X		5	/* SHA256, 802.1X */
58 #define RSN_AKM_SHA256_PSK		6	/* SHA256, Pre-shared Key */
59 #define RSN_CAP_LEN			2	/* Length of RSN capabilities */
60 #define RSN_CAP_PTK_REPLAY_CNTR_MASK	(BIT(2) | BIT(3))
61 #define RSN_CAP_MFPR_MASK		BIT(6)
62 #define RSN_CAP_MFPC_MASK		BIT(7)
63 #define RSN_PMKID_COUNT_LEN		2
64 
65 #define VNDR_IE_CMD_LEN			4	/* length of the set command
66 						 * string :"add", "del" (+ NUL)
67 						 */
68 #define VNDR_IE_COUNT_OFFSET		4
69 #define VNDR_IE_PKTFLAG_OFFSET		8
70 #define VNDR_IE_VSIE_OFFSET		12
71 #define VNDR_IE_HDR_SIZE		12
72 #define VNDR_IE_PARSE_LIMIT		5
73 
74 #define	DOT11_MGMT_HDR_LEN		24	/* d11 management header len */
75 #define	DOT11_BCN_PRB_FIXED_LEN		12	/* beacon/probe fixed length */
76 
77 #define BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS	320
78 #define BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS	400
79 #define BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS	20
80 
81 #define BRCMF_SCAN_CHANNEL_TIME		40
82 #define BRCMF_SCAN_UNASSOC_TIME		40
83 #define BRCMF_SCAN_PASSIVE_TIME		120
84 
85 #define BRCMF_ND_INFO_TIMEOUT		msecs_to_jiffies(2000)
86 
87 #define BRCMF_ASSOC_PARAMS_FIXED_SIZE \
88 	(sizeof(struct brcmf_assoc_params_le) - sizeof(u16))
89 
90 static bool check_vif_up(struct brcmf_cfg80211_vif *vif)
91 {
92 	if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state)) {
93 		brcmf_dbg(INFO, "device is not ready : status (%lu)\n",
94 			  vif->sme_state);
95 		return false;
96 	}
97 	return true;
98 }
99 
100 #define RATE_TO_BASE100KBPS(rate)   (((rate) * 10) / 2)
101 #define RATETAB_ENT(_rateid, _flags) \
102 	{                                                               \
103 		.bitrate        = RATE_TO_BASE100KBPS(_rateid),     \
104 		.hw_value       = (_rateid),                            \
105 		.flags          = (_flags),                             \
106 	}
107 
108 static struct ieee80211_rate __wl_rates[] = {
109 	RATETAB_ENT(BRCM_RATE_1M, 0),
110 	RATETAB_ENT(BRCM_RATE_2M, IEEE80211_RATE_SHORT_PREAMBLE),
111 	RATETAB_ENT(BRCM_RATE_5M5, IEEE80211_RATE_SHORT_PREAMBLE),
112 	RATETAB_ENT(BRCM_RATE_11M, IEEE80211_RATE_SHORT_PREAMBLE),
113 	RATETAB_ENT(BRCM_RATE_6M, 0),
114 	RATETAB_ENT(BRCM_RATE_9M, 0),
115 	RATETAB_ENT(BRCM_RATE_12M, 0),
116 	RATETAB_ENT(BRCM_RATE_18M, 0),
117 	RATETAB_ENT(BRCM_RATE_24M, 0),
118 	RATETAB_ENT(BRCM_RATE_36M, 0),
119 	RATETAB_ENT(BRCM_RATE_48M, 0),
120 	RATETAB_ENT(BRCM_RATE_54M, 0),
121 };
122 
123 #define wl_g_rates		(__wl_rates + 0)
124 #define wl_g_rates_size		ARRAY_SIZE(__wl_rates)
125 #define wl_a_rates		(__wl_rates + 4)
126 #define wl_a_rates_size		(wl_g_rates_size - 4)
127 
128 #define CHAN2G(_channel, _freq) {				\
129 	.band			= NL80211_BAND_2GHZ,		\
130 	.center_freq		= (_freq),			\
131 	.hw_value		= (_channel),			\
132 	.max_antenna_gain	= 0,				\
133 	.max_power		= 30,				\
134 }
135 
136 #define CHAN5G(_channel) {					\
137 	.band			= NL80211_BAND_5GHZ,		\
138 	.center_freq		= 5000 + (5 * (_channel)),	\
139 	.hw_value		= (_channel),			\
140 	.max_antenna_gain	= 0,				\
141 	.max_power		= 30,				\
142 }
143 
144 static struct ieee80211_channel __wl_2ghz_channels[] = {
145 	CHAN2G(1, 2412), CHAN2G(2, 2417), CHAN2G(3, 2422), CHAN2G(4, 2427),
146 	CHAN2G(5, 2432), CHAN2G(6, 2437), CHAN2G(7, 2442), CHAN2G(8, 2447),
147 	CHAN2G(9, 2452), CHAN2G(10, 2457), CHAN2G(11, 2462), CHAN2G(12, 2467),
148 	CHAN2G(13, 2472), CHAN2G(14, 2484)
149 };
150 
151 static struct ieee80211_channel __wl_5ghz_channels[] = {
152 	CHAN5G(34), CHAN5G(36), CHAN5G(38), CHAN5G(40), CHAN5G(42),
153 	CHAN5G(44), CHAN5G(46), CHAN5G(48), CHAN5G(52), CHAN5G(56),
154 	CHAN5G(60), CHAN5G(64), CHAN5G(100), CHAN5G(104), CHAN5G(108),
155 	CHAN5G(112), CHAN5G(116), CHAN5G(120), CHAN5G(124), CHAN5G(128),
156 	CHAN5G(132), CHAN5G(136), CHAN5G(140), CHAN5G(144), CHAN5G(149),
157 	CHAN5G(153), CHAN5G(157), CHAN5G(161), CHAN5G(165)
158 };
159 
160 /* Band templates duplicated per wiphy. The channel info
161  * above is added to the band during setup.
162  */
163 static const struct ieee80211_supported_band __wl_band_2ghz = {
164 	.band = NL80211_BAND_2GHZ,
165 	.bitrates = wl_g_rates,
166 	.n_bitrates = wl_g_rates_size,
167 };
168 
169 static const struct ieee80211_supported_band __wl_band_5ghz = {
170 	.band = NL80211_BAND_5GHZ,
171 	.bitrates = wl_a_rates,
172 	.n_bitrates = wl_a_rates_size,
173 };
174 
175 /* This is to override regulatory domains defined in cfg80211 module (reg.c)
176  * By default world regulatory domain defined in reg.c puts the flags
177  * NL80211_RRF_NO_IR for 5GHz channels (for * 36..48 and 149..165).
178  * With respect to these flags, wpa_supplicant doesn't * start p2p
179  * operations on 5GHz channels. All the changes in world regulatory
180  * domain are to be done here.
181  */
182 static const struct ieee80211_regdomain brcmf_regdom = {
183 	.n_reg_rules = 4,
184 	.alpha2 =  "99",
185 	.reg_rules = {
186 		/* IEEE 802.11b/g, channels 1..11 */
187 		REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
188 		/* If any */
189 		/* IEEE 802.11 channel 14 - Only JP enables
190 		 * this and for 802.11b only
191 		 */
192 		REG_RULE(2484-10, 2484+10, 20, 6, 20, 0),
193 		/* IEEE 802.11a, channel 36..64 */
194 		REG_RULE(5150-10, 5350+10, 160, 6, 20, 0),
195 		/* IEEE 802.11a, channel 100..165 */
196 		REG_RULE(5470-10, 5850+10, 160, 6, 20, 0), }
197 };
198 
199 /* Note: brcmf_cipher_suites is an array of int defining which cipher suites
200  * are supported. A pointer to this array and the number of entries is passed
201  * on to upper layers. AES_CMAC defines whether or not the driver supports MFP.
202  * So the cipher suite AES_CMAC has to be the last one in the array, and when
203  * device does not support MFP then the number of suites will be decreased by 1
204  */
205 static const u32 brcmf_cipher_suites[] = {
206 	WLAN_CIPHER_SUITE_WEP40,
207 	WLAN_CIPHER_SUITE_WEP104,
208 	WLAN_CIPHER_SUITE_TKIP,
209 	WLAN_CIPHER_SUITE_CCMP,
210 	/* Keep as last entry: */
211 	WLAN_CIPHER_SUITE_AES_CMAC
212 };
213 
214 /* Vendor specific ie. id = 221, oui and type defines exact ie */
215 struct brcmf_vs_tlv {
216 	u8 id;
217 	u8 len;
218 	u8 oui[3];
219 	u8 oui_type;
220 };
221 
222 struct parsed_vndr_ie_info {
223 	u8 *ie_ptr;
224 	u32 ie_len;	/* total length including id & length field */
225 	struct brcmf_vs_tlv vndrie;
226 };
227 
228 struct parsed_vndr_ies {
229 	u32 count;
230 	struct parsed_vndr_ie_info ie_info[VNDR_IE_PARSE_LIMIT];
231 };
232 
233 static u8 nl80211_band_to_fwil(enum nl80211_band band)
234 {
235 	switch (band) {
236 	case NL80211_BAND_2GHZ:
237 		return WLC_BAND_2G;
238 	case NL80211_BAND_5GHZ:
239 		return WLC_BAND_5G;
240 	default:
241 		WARN_ON(1);
242 		break;
243 	}
244 	return 0;
245 }
246 
247 static u16 chandef_to_chanspec(struct brcmu_d11inf *d11inf,
248 			       struct cfg80211_chan_def *ch)
249 {
250 	struct brcmu_chan ch_inf;
251 	s32 primary_offset;
252 
253 	brcmf_dbg(TRACE, "chandef: control %d center %d width %d\n",
254 		  ch->chan->center_freq, ch->center_freq1, ch->width);
255 	ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq1);
256 	primary_offset = ch->chan->center_freq - ch->center_freq1;
257 	switch (ch->width) {
258 	case NL80211_CHAN_WIDTH_20:
259 	case NL80211_CHAN_WIDTH_20_NOHT:
260 		ch_inf.bw = BRCMU_CHAN_BW_20;
261 		WARN_ON(primary_offset != 0);
262 		break;
263 	case NL80211_CHAN_WIDTH_40:
264 		ch_inf.bw = BRCMU_CHAN_BW_40;
265 		if (primary_offset > 0)
266 			ch_inf.sb = BRCMU_CHAN_SB_U;
267 		else
268 			ch_inf.sb = BRCMU_CHAN_SB_L;
269 		break;
270 	case NL80211_CHAN_WIDTH_80:
271 		ch_inf.bw = BRCMU_CHAN_BW_80;
272 		if (primary_offset == -30)
273 			ch_inf.sb = BRCMU_CHAN_SB_LL;
274 		else if (primary_offset == -10)
275 			ch_inf.sb = BRCMU_CHAN_SB_LU;
276 		else if (primary_offset == 10)
277 			ch_inf.sb = BRCMU_CHAN_SB_UL;
278 		else
279 			ch_inf.sb = BRCMU_CHAN_SB_UU;
280 		break;
281 	case NL80211_CHAN_WIDTH_160:
282 		ch_inf.bw = BRCMU_CHAN_BW_160;
283 		if (primary_offset == -70)
284 			ch_inf.sb = BRCMU_CHAN_SB_LLL;
285 		else if (primary_offset == -50)
286 			ch_inf.sb = BRCMU_CHAN_SB_LLU;
287 		else if (primary_offset == -30)
288 			ch_inf.sb = BRCMU_CHAN_SB_LUL;
289 		else if (primary_offset == -10)
290 			ch_inf.sb = BRCMU_CHAN_SB_LUU;
291 		else if (primary_offset == 10)
292 			ch_inf.sb = BRCMU_CHAN_SB_ULL;
293 		else if (primary_offset == 30)
294 			ch_inf.sb = BRCMU_CHAN_SB_ULU;
295 		else if (primary_offset == 50)
296 			ch_inf.sb = BRCMU_CHAN_SB_UUL;
297 		else
298 			ch_inf.sb = BRCMU_CHAN_SB_UUU;
299 		break;
300 	case NL80211_CHAN_WIDTH_80P80:
301 	case NL80211_CHAN_WIDTH_5:
302 	case NL80211_CHAN_WIDTH_10:
303 	default:
304 		WARN_ON_ONCE(1);
305 	}
306 	switch (ch->chan->band) {
307 	case NL80211_BAND_2GHZ:
308 		ch_inf.band = BRCMU_CHAN_BAND_2G;
309 		break;
310 	case NL80211_BAND_5GHZ:
311 		ch_inf.band = BRCMU_CHAN_BAND_5G;
312 		break;
313 	case NL80211_BAND_60GHZ:
314 	default:
315 		WARN_ON_ONCE(1);
316 	}
317 	d11inf->encchspec(&ch_inf);
318 
319 	brcmf_dbg(TRACE, "chanspec: 0x%x\n", ch_inf.chspec);
320 	return ch_inf.chspec;
321 }
322 
323 u16 channel_to_chanspec(struct brcmu_d11inf *d11inf,
324 			struct ieee80211_channel *ch)
325 {
326 	struct brcmu_chan ch_inf;
327 
328 	ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq);
329 	ch_inf.bw = BRCMU_CHAN_BW_20;
330 	d11inf->encchspec(&ch_inf);
331 
332 	return ch_inf.chspec;
333 }
334 
335 /* Traverse a string of 1-byte tag/1-byte length/variable-length value
336  * triples, returning a pointer to the substring whose first element
337  * matches tag
338  */
339 static const struct brcmf_tlv *
340 brcmf_parse_tlvs(const void *buf, int buflen, uint key)
341 {
342 	const struct brcmf_tlv *elt = buf;
343 	int totlen = buflen;
344 
345 	/* find tagged parameter */
346 	while (totlen >= TLV_HDR_LEN) {
347 		int len = elt->len;
348 
349 		/* validate remaining totlen */
350 		if ((elt->id == key) && (totlen >= (len + TLV_HDR_LEN)))
351 			return elt;
352 
353 		elt = (struct brcmf_tlv *)((u8 *)elt + (len + TLV_HDR_LEN));
354 		totlen -= (len + TLV_HDR_LEN);
355 	}
356 
357 	return NULL;
358 }
359 
360 /* Is any of the tlvs the expected entry? If
361  * not update the tlvs buffer pointer/length.
362  */
363 static bool
364 brcmf_tlv_has_ie(const u8 *ie, const u8 **tlvs, u32 *tlvs_len,
365 		 const u8 *oui, u32 oui_len, u8 type)
366 {
367 	/* If the contents match the OUI and the type */
368 	if (ie[TLV_LEN_OFF] >= oui_len + 1 &&
369 	    !memcmp(&ie[TLV_BODY_OFF], oui, oui_len) &&
370 	    type == ie[TLV_BODY_OFF + oui_len]) {
371 		return true;
372 	}
373 
374 	if (tlvs == NULL)
375 		return false;
376 	/* point to the next ie */
377 	ie += ie[TLV_LEN_OFF] + TLV_HDR_LEN;
378 	/* calculate the length of the rest of the buffer */
379 	*tlvs_len -= (int)(ie - *tlvs);
380 	/* update the pointer to the start of the buffer */
381 	*tlvs = ie;
382 
383 	return false;
384 }
385 
386 static struct brcmf_vs_tlv *
387 brcmf_find_wpaie(const u8 *parse, u32 len)
388 {
389 	const struct brcmf_tlv *ie;
390 
391 	while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
392 		if (brcmf_tlv_has_ie((const u8 *)ie, &parse, &len,
393 				     WPA_OUI, TLV_OUI_LEN, WPA_OUI_TYPE))
394 			return (struct brcmf_vs_tlv *)ie;
395 	}
396 	return NULL;
397 }
398 
399 static struct brcmf_vs_tlv *
400 brcmf_find_wpsie(const u8 *parse, u32 len)
401 {
402 	const struct brcmf_tlv *ie;
403 
404 	while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
405 		if (brcmf_tlv_has_ie((u8 *)ie, &parse, &len,
406 				     WPA_OUI, TLV_OUI_LEN, WPS_OUI_TYPE))
407 			return (struct brcmf_vs_tlv *)ie;
408 	}
409 	return NULL;
410 }
411 
412 static int brcmf_vif_change_validate(struct brcmf_cfg80211_info *cfg,
413 				     struct brcmf_cfg80211_vif *vif,
414 				     enum nl80211_iftype new_type)
415 {
416 	struct brcmf_cfg80211_vif *pos;
417 	bool check_combos = false;
418 	int ret = 0;
419 	struct iface_combination_params params = {
420 		.num_different_channels = 1,
421 	};
422 
423 	list_for_each_entry(pos, &cfg->vif_list, list)
424 		if (pos == vif) {
425 			params.iftype_num[new_type]++;
426 		} else {
427 			/* concurrent interfaces so need check combinations */
428 			check_combos = true;
429 			params.iftype_num[pos->wdev.iftype]++;
430 		}
431 
432 	if (check_combos)
433 		ret = cfg80211_check_combinations(cfg->wiphy, &params);
434 
435 	return ret;
436 }
437 
438 static int brcmf_vif_add_validate(struct brcmf_cfg80211_info *cfg,
439 				  enum nl80211_iftype new_type)
440 {
441 	struct brcmf_cfg80211_vif *pos;
442 	struct iface_combination_params params = {
443 		.num_different_channels = 1,
444 	};
445 
446 	list_for_each_entry(pos, &cfg->vif_list, list)
447 		params.iftype_num[pos->wdev.iftype]++;
448 
449 	params.iftype_num[new_type]++;
450 	return cfg80211_check_combinations(cfg->wiphy, &params);
451 }
452 
453 static void convert_key_from_CPU(struct brcmf_wsec_key *key,
454 				 struct brcmf_wsec_key_le *key_le)
455 {
456 	key_le->index = cpu_to_le32(key->index);
457 	key_le->len = cpu_to_le32(key->len);
458 	key_le->algo = cpu_to_le32(key->algo);
459 	key_le->flags = cpu_to_le32(key->flags);
460 	key_le->rxiv.hi = cpu_to_le32(key->rxiv.hi);
461 	key_le->rxiv.lo = cpu_to_le16(key->rxiv.lo);
462 	key_le->iv_initialized = cpu_to_le32(key->iv_initialized);
463 	memcpy(key_le->data, key->data, sizeof(key->data));
464 	memcpy(key_le->ea, key->ea, sizeof(key->ea));
465 }
466 
467 static int
468 send_key_to_dongle(struct brcmf_if *ifp, struct brcmf_wsec_key *key)
469 {
470 	struct brcmf_pub *drvr = ifp->drvr;
471 	int err;
472 	struct brcmf_wsec_key_le key_le;
473 
474 	convert_key_from_CPU(key, &key_le);
475 
476 	brcmf_netdev_wait_pend8021x(ifp);
477 
478 	err = brcmf_fil_bsscfg_data_set(ifp, "wsec_key", &key_le,
479 					sizeof(key_le));
480 
481 	if (err)
482 		bphy_err(drvr, "wsec_key error (%d)\n", err);
483 	return err;
484 }
485 
486 static void
487 brcmf_cfg80211_update_proto_addr_mode(struct wireless_dev *wdev)
488 {
489 	struct brcmf_cfg80211_vif *vif;
490 	struct brcmf_if *ifp;
491 
492 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
493 	ifp = vif->ifp;
494 
495 	if ((wdev->iftype == NL80211_IFTYPE_ADHOC) ||
496 	    (wdev->iftype == NL80211_IFTYPE_AP) ||
497 	    (wdev->iftype == NL80211_IFTYPE_P2P_GO))
498 		brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx,
499 						ADDR_DIRECT);
500 	else
501 		brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx,
502 						ADDR_INDIRECT);
503 }
504 
505 static int brcmf_get_first_free_bsscfgidx(struct brcmf_pub *drvr)
506 {
507 	int bsscfgidx;
508 
509 	for (bsscfgidx = 0; bsscfgidx < BRCMF_MAX_IFS; bsscfgidx++) {
510 		/* bsscfgidx 1 is reserved for legacy P2P */
511 		if (bsscfgidx == 1)
512 			continue;
513 		if (!drvr->iflist[bsscfgidx])
514 			return bsscfgidx;
515 	}
516 
517 	return -ENOMEM;
518 }
519 
520 static int brcmf_cfg80211_request_ap_if(struct brcmf_if *ifp)
521 {
522 	struct brcmf_pub *drvr = ifp->drvr;
523 	struct brcmf_mbss_ssid_le mbss_ssid_le;
524 	int bsscfgidx;
525 	int err;
526 
527 	memset(&mbss_ssid_le, 0, sizeof(mbss_ssid_le));
528 	bsscfgidx = brcmf_get_first_free_bsscfgidx(ifp->drvr);
529 	if (bsscfgidx < 0)
530 		return bsscfgidx;
531 
532 	mbss_ssid_le.bsscfgidx = cpu_to_le32(bsscfgidx);
533 	mbss_ssid_le.SSID_len = cpu_to_le32(5);
534 	sprintf(mbss_ssid_le.SSID, "ssid%d" , bsscfgidx);
535 
536 	err = brcmf_fil_bsscfg_data_set(ifp, "bsscfg:ssid", &mbss_ssid_le,
537 					sizeof(mbss_ssid_le));
538 	if (err < 0)
539 		bphy_err(drvr, "setting ssid failed %d\n", err);
540 
541 	return err;
542 }
543 
544 /**
545  * brcmf_ap_add_vif() - create a new AP virtual interface for multiple BSS
546  *
547  * @wiphy: wiphy device of new interface.
548  * @name: name of the new interface.
549  * @params: contains mac address for AP device.
550  */
551 static
552 struct wireless_dev *brcmf_ap_add_vif(struct wiphy *wiphy, const char *name,
553 				      struct vif_params *params)
554 {
555 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
556 	struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
557 	struct brcmf_pub *drvr = cfg->pub;
558 	struct brcmf_cfg80211_vif *vif;
559 	int err;
560 
561 	if (brcmf_cfg80211_vif_event_armed(cfg))
562 		return ERR_PTR(-EBUSY);
563 
564 	brcmf_dbg(INFO, "Adding vif \"%s\"\n", name);
565 
566 	vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_AP);
567 	if (IS_ERR(vif))
568 		return (struct wireless_dev *)vif;
569 
570 	brcmf_cfg80211_arm_vif_event(cfg, vif);
571 
572 	err = brcmf_cfg80211_request_ap_if(ifp);
573 	if (err) {
574 		brcmf_cfg80211_arm_vif_event(cfg, NULL);
575 		goto fail;
576 	}
577 
578 	/* wait for firmware event */
579 	err = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_ADD,
580 					    BRCMF_VIF_EVENT_TIMEOUT);
581 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
582 	if (!err) {
583 		bphy_err(drvr, "timeout occurred\n");
584 		err = -EIO;
585 		goto fail;
586 	}
587 
588 	/* interface created in firmware */
589 	ifp = vif->ifp;
590 	if (!ifp) {
591 		bphy_err(drvr, "no if pointer provided\n");
592 		err = -ENOENT;
593 		goto fail;
594 	}
595 
596 	strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
597 	err = brcmf_net_attach(ifp, true);
598 	if (err) {
599 		bphy_err(drvr, "Registering netdevice failed\n");
600 		free_netdev(ifp->ndev);
601 		goto fail;
602 	}
603 
604 	return &ifp->vif->wdev;
605 
606 fail:
607 	brcmf_free_vif(vif);
608 	return ERR_PTR(err);
609 }
610 
611 static bool brcmf_is_apmode(struct brcmf_cfg80211_vif *vif)
612 {
613 	enum nl80211_iftype iftype;
614 
615 	iftype = vif->wdev.iftype;
616 	return iftype == NL80211_IFTYPE_AP || iftype == NL80211_IFTYPE_P2P_GO;
617 }
618 
619 static bool brcmf_is_ibssmode(struct brcmf_cfg80211_vif *vif)
620 {
621 	return vif->wdev.iftype == NL80211_IFTYPE_ADHOC;
622 }
623 
624 /**
625  * brcmf_mon_add_vif() - create monitor mode virtual interface
626  *
627  * @wiphy: wiphy device of new interface.
628  * @name: name of the new interface.
629  */
630 static struct wireless_dev *brcmf_mon_add_vif(struct wiphy *wiphy,
631 					      const char *name)
632 {
633 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
634 	struct brcmf_cfg80211_vif *vif;
635 	struct net_device *ndev;
636 	struct brcmf_if *ifp;
637 	int err;
638 
639 	if (cfg->pub->mon_if) {
640 		err = -EEXIST;
641 		goto err_out;
642 	}
643 
644 	vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_MONITOR);
645 	if (IS_ERR(vif)) {
646 		err = PTR_ERR(vif);
647 		goto err_out;
648 	}
649 
650 	ndev = alloc_netdev(sizeof(*ifp), name, NET_NAME_UNKNOWN, ether_setup);
651 	if (!ndev) {
652 		err = -ENOMEM;
653 		goto err_free_vif;
654 	}
655 	ndev->type = ARPHRD_IEEE80211_RADIOTAP;
656 	ndev->ieee80211_ptr = &vif->wdev;
657 	ndev->needs_free_netdev = true;
658 	ndev->priv_destructor = brcmf_cfg80211_free_netdev;
659 	SET_NETDEV_DEV(ndev, wiphy_dev(cfg->wiphy));
660 
661 	ifp = netdev_priv(ndev);
662 	ifp->vif = vif;
663 	ifp->ndev = ndev;
664 	ifp->drvr = cfg->pub;
665 
666 	vif->ifp = ifp;
667 	vif->wdev.netdev = ndev;
668 
669 	err = brcmf_net_mon_attach(ifp);
670 	if (err) {
671 		brcmf_err("Failed to attach %s device\n", ndev->name);
672 		free_netdev(ndev);
673 		goto err_free_vif;
674 	}
675 
676 	cfg->pub->mon_if = ifp;
677 
678 	return &vif->wdev;
679 
680 err_free_vif:
681 	brcmf_free_vif(vif);
682 err_out:
683 	return ERR_PTR(err);
684 }
685 
686 static int brcmf_mon_del_vif(struct wiphy *wiphy, struct wireless_dev *wdev)
687 {
688 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
689 	struct net_device *ndev = wdev->netdev;
690 
691 	ndev->netdev_ops->ndo_stop(ndev);
692 
693 	brcmf_net_detach(ndev, true);
694 
695 	cfg->pub->mon_if = NULL;
696 
697 	return 0;
698 }
699 
700 static struct wireless_dev *brcmf_cfg80211_add_iface(struct wiphy *wiphy,
701 						     const char *name,
702 						     unsigned char name_assign_type,
703 						     enum nl80211_iftype type,
704 						     struct vif_params *params)
705 {
706 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
707 	struct brcmf_pub *drvr = cfg->pub;
708 	struct wireless_dev *wdev;
709 	int err;
710 
711 	brcmf_dbg(TRACE, "enter: %s type %d\n", name, type);
712 	err = brcmf_vif_add_validate(wiphy_to_cfg(wiphy), type);
713 	if (err) {
714 		bphy_err(drvr, "iface validation failed: err=%d\n", err);
715 		return ERR_PTR(err);
716 	}
717 	switch (type) {
718 	case NL80211_IFTYPE_ADHOC:
719 	case NL80211_IFTYPE_STATION:
720 	case NL80211_IFTYPE_AP_VLAN:
721 	case NL80211_IFTYPE_WDS:
722 	case NL80211_IFTYPE_MESH_POINT:
723 		return ERR_PTR(-EOPNOTSUPP);
724 	case NL80211_IFTYPE_MONITOR:
725 		return brcmf_mon_add_vif(wiphy, name);
726 	case NL80211_IFTYPE_AP:
727 		wdev = brcmf_ap_add_vif(wiphy, name, params);
728 		break;
729 	case NL80211_IFTYPE_P2P_CLIENT:
730 	case NL80211_IFTYPE_P2P_GO:
731 	case NL80211_IFTYPE_P2P_DEVICE:
732 		wdev = brcmf_p2p_add_vif(wiphy, name, name_assign_type, type, params);
733 		break;
734 	case NL80211_IFTYPE_UNSPECIFIED:
735 	default:
736 		return ERR_PTR(-EINVAL);
737 	}
738 
739 	if (IS_ERR(wdev))
740 		bphy_err(drvr, "add iface %s type %d failed: err=%d\n", name,
741 			 type, (int)PTR_ERR(wdev));
742 	else
743 		brcmf_cfg80211_update_proto_addr_mode(wdev);
744 
745 	return wdev;
746 }
747 
748 static void brcmf_scan_config_mpc(struct brcmf_if *ifp, int mpc)
749 {
750 	if (brcmf_feat_is_quirk_enabled(ifp, BRCMF_FEAT_QUIRK_NEED_MPC))
751 		brcmf_set_mpc(ifp, mpc);
752 }
753 
754 void brcmf_set_mpc(struct brcmf_if *ifp, int mpc)
755 {
756 	struct brcmf_pub *drvr = ifp->drvr;
757 	s32 err = 0;
758 
759 	if (check_vif_up(ifp->vif)) {
760 		err = brcmf_fil_iovar_int_set(ifp, "mpc", mpc);
761 		if (err) {
762 			bphy_err(drvr, "fail to set mpc\n");
763 			return;
764 		}
765 		brcmf_dbg(INFO, "MPC : %d\n", mpc);
766 	}
767 }
768 
769 s32 brcmf_notify_escan_complete(struct brcmf_cfg80211_info *cfg,
770 				struct brcmf_if *ifp, bool aborted,
771 				bool fw_abort)
772 {
773 	struct brcmf_pub *drvr = cfg->pub;
774 	struct brcmf_scan_params_le params_le;
775 	struct cfg80211_scan_request *scan_request;
776 	u64 reqid;
777 	u32 bucket;
778 	s32 err = 0;
779 
780 	brcmf_dbg(SCAN, "Enter\n");
781 
782 	/* clear scan request, because the FW abort can cause a second call */
783 	/* to this functon and might cause a double cfg80211_scan_done      */
784 	scan_request = cfg->scan_request;
785 	cfg->scan_request = NULL;
786 
787 	if (timer_pending(&cfg->escan_timeout))
788 		del_timer_sync(&cfg->escan_timeout);
789 
790 	if (fw_abort) {
791 		/* Do a scan abort to stop the driver's scan engine */
792 		brcmf_dbg(SCAN, "ABORT scan in firmware\n");
793 		memset(&params_le, 0, sizeof(params_le));
794 		eth_broadcast_addr(params_le.bssid);
795 		params_le.bss_type = DOT11_BSSTYPE_ANY;
796 		params_le.scan_type = 0;
797 		params_le.channel_num = cpu_to_le32(1);
798 		params_le.nprobes = cpu_to_le32(1);
799 		params_le.active_time = cpu_to_le32(-1);
800 		params_le.passive_time = cpu_to_le32(-1);
801 		params_le.home_time = cpu_to_le32(-1);
802 		/* Scan is aborted by setting channel_list[0] to -1 */
803 		params_le.channel_list[0] = cpu_to_le16(-1);
804 		/* E-Scan (or anyother type) can be aborted by SCAN */
805 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
806 					     &params_le, sizeof(params_le));
807 		if (err)
808 			bphy_err(drvr, "Scan abort failed\n");
809 	}
810 
811 	brcmf_scan_config_mpc(ifp, 1);
812 
813 	/*
814 	 * e-scan can be initiated internally
815 	 * which takes precedence.
816 	 */
817 	if (cfg->int_escan_map) {
818 		brcmf_dbg(SCAN, "scheduled scan completed (%x)\n",
819 			  cfg->int_escan_map);
820 		while (cfg->int_escan_map) {
821 			bucket = __ffs(cfg->int_escan_map);
822 			cfg->int_escan_map &= ~BIT(bucket);
823 			reqid = brcmf_pno_find_reqid_by_bucket(cfg->pno,
824 							       bucket);
825 			if (!aborted) {
826 				brcmf_dbg(SCAN, "report results: reqid=%llu\n",
827 					  reqid);
828 				cfg80211_sched_scan_results(cfg_to_wiphy(cfg),
829 							    reqid);
830 			}
831 		}
832 	} else if (scan_request) {
833 		struct cfg80211_scan_info info = {
834 			.aborted = aborted,
835 		};
836 
837 		brcmf_dbg(SCAN, "ESCAN Completed scan: %s\n",
838 			  aborted ? "Aborted" : "Done");
839 		cfg80211_scan_done(scan_request, &info);
840 	}
841 	if (!test_and_clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
842 		brcmf_dbg(SCAN, "Scan complete, probably P2P scan\n");
843 
844 	return err;
845 }
846 
847 static int brcmf_cfg80211_del_ap_iface(struct wiphy *wiphy,
848 				       struct wireless_dev *wdev)
849 {
850 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
851 	struct net_device *ndev = wdev->netdev;
852 	struct brcmf_if *ifp = netdev_priv(ndev);
853 	struct brcmf_pub *drvr = cfg->pub;
854 	int ret;
855 	int err;
856 
857 	brcmf_cfg80211_arm_vif_event(cfg, ifp->vif);
858 
859 	err = brcmf_fil_bsscfg_data_set(ifp, "interface_remove", NULL, 0);
860 	if (err) {
861 		bphy_err(drvr, "interface_remove failed %d\n", err);
862 		goto err_unarm;
863 	}
864 
865 	/* wait for firmware event */
866 	ret = brcmf_cfg80211_wait_vif_event(cfg, BRCMF_E_IF_DEL,
867 					    BRCMF_VIF_EVENT_TIMEOUT);
868 	if (!ret) {
869 		bphy_err(drvr, "timeout occurred\n");
870 		err = -EIO;
871 		goto err_unarm;
872 	}
873 
874 	brcmf_remove_interface(ifp, true);
875 
876 err_unarm:
877 	brcmf_cfg80211_arm_vif_event(cfg, NULL);
878 	return err;
879 }
880 
881 static
882 int brcmf_cfg80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
883 {
884 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
885 	struct net_device *ndev = wdev->netdev;
886 
887 	if (ndev && ndev == cfg_to_ndev(cfg))
888 		return -ENOTSUPP;
889 
890 	/* vif event pending in firmware */
891 	if (brcmf_cfg80211_vif_event_armed(cfg))
892 		return -EBUSY;
893 
894 	if (ndev) {
895 		if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status) &&
896 		    cfg->escan_info.ifp == netdev_priv(ndev))
897 			brcmf_notify_escan_complete(cfg, netdev_priv(ndev),
898 						    true, true);
899 
900 		brcmf_fil_iovar_int_set(netdev_priv(ndev), "mpc", 1);
901 	}
902 
903 	switch (wdev->iftype) {
904 	case NL80211_IFTYPE_ADHOC:
905 	case NL80211_IFTYPE_STATION:
906 	case NL80211_IFTYPE_AP_VLAN:
907 	case NL80211_IFTYPE_WDS:
908 	case NL80211_IFTYPE_MESH_POINT:
909 		return -EOPNOTSUPP;
910 	case NL80211_IFTYPE_MONITOR:
911 		return brcmf_mon_del_vif(wiphy, wdev);
912 	case NL80211_IFTYPE_AP:
913 		return brcmf_cfg80211_del_ap_iface(wiphy, wdev);
914 	case NL80211_IFTYPE_P2P_CLIENT:
915 	case NL80211_IFTYPE_P2P_GO:
916 	case NL80211_IFTYPE_P2P_DEVICE:
917 		return brcmf_p2p_del_vif(wiphy, wdev);
918 	case NL80211_IFTYPE_UNSPECIFIED:
919 	default:
920 		return -EINVAL;
921 	}
922 	return -EOPNOTSUPP;
923 }
924 
925 static s32
926 brcmf_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
927 			 enum nl80211_iftype type,
928 			 struct vif_params *params)
929 {
930 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
931 	struct brcmf_if *ifp = netdev_priv(ndev);
932 	struct brcmf_cfg80211_vif *vif = ifp->vif;
933 	struct brcmf_pub *drvr = cfg->pub;
934 	s32 infra = 0;
935 	s32 ap = 0;
936 	s32 err = 0;
937 
938 	brcmf_dbg(TRACE, "Enter, bsscfgidx=%d, type=%d\n", ifp->bsscfgidx,
939 		  type);
940 
941 	/* WAR: There are a number of p2p interface related problems which
942 	 * need to be handled initially (before doing the validate).
943 	 * wpa_supplicant tends to do iface changes on p2p device/client/go
944 	 * which are not always possible/allowed. However we need to return
945 	 * OK otherwise the wpa_supplicant wont start. The situation differs
946 	 * on configuration and setup (p2pon=1 module param). The first check
947 	 * is to see if the request is a change to station for p2p iface.
948 	 */
949 	if ((type == NL80211_IFTYPE_STATION) &&
950 	    ((vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT) ||
951 	     (vif->wdev.iftype == NL80211_IFTYPE_P2P_GO) ||
952 	     (vif->wdev.iftype == NL80211_IFTYPE_P2P_DEVICE))) {
953 		brcmf_dbg(TRACE, "Ignoring cmd for p2p if\n");
954 		/* Now depending on whether module param p2pon=1 was used the
955 		 * response needs to be either 0 or EOPNOTSUPP. The reason is
956 		 * that if p2pon=1 is used, but a newer supplicant is used then
957 		 * we should return an error, as this combination wont work.
958 		 * In other situations 0 is returned and supplicant will start
959 		 * normally. It will give a trace in cfg80211, but it is the
960 		 * only way to get it working. Unfortunately this will result
961 		 * in situation where we wont support new supplicant in
962 		 * combination with module param p2pon=1, but that is the way
963 		 * it is. If the user tries this then unloading of driver might
964 		 * fail/lock.
965 		 */
966 		if (cfg->p2p.p2pdev_dynamically)
967 			return -EOPNOTSUPP;
968 		else
969 			return 0;
970 	}
971 	err = brcmf_vif_change_validate(wiphy_to_cfg(wiphy), vif, type);
972 	if (err) {
973 		bphy_err(drvr, "iface validation failed: err=%d\n", err);
974 		return err;
975 	}
976 	switch (type) {
977 	case NL80211_IFTYPE_MONITOR:
978 	case NL80211_IFTYPE_WDS:
979 		bphy_err(drvr, "type (%d) : currently we do not support this type\n",
980 			 type);
981 		return -EOPNOTSUPP;
982 	case NL80211_IFTYPE_ADHOC:
983 		infra = 0;
984 		break;
985 	case NL80211_IFTYPE_STATION:
986 		infra = 1;
987 		break;
988 	case NL80211_IFTYPE_AP:
989 	case NL80211_IFTYPE_P2P_GO:
990 		ap = 1;
991 		break;
992 	default:
993 		err = -EINVAL;
994 		goto done;
995 	}
996 
997 	if (ap) {
998 		if (type == NL80211_IFTYPE_P2P_GO) {
999 			brcmf_dbg(INFO, "IF Type = P2P GO\n");
1000 			err = brcmf_p2p_ifchange(cfg, BRCMF_FIL_P2P_IF_GO);
1001 		}
1002 		if (!err) {
1003 			brcmf_dbg(INFO, "IF Type = AP\n");
1004 		}
1005 	} else {
1006 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, infra);
1007 		if (err) {
1008 			bphy_err(drvr, "WLC_SET_INFRA error (%d)\n", err);
1009 			err = -EAGAIN;
1010 			goto done;
1011 		}
1012 		brcmf_dbg(INFO, "IF Type = %s\n", brcmf_is_ibssmode(vif) ?
1013 			  "Adhoc" : "Infra");
1014 	}
1015 	ndev->ieee80211_ptr->iftype = type;
1016 
1017 	brcmf_cfg80211_update_proto_addr_mode(&vif->wdev);
1018 
1019 done:
1020 	brcmf_dbg(TRACE, "Exit\n");
1021 
1022 	return err;
1023 }
1024 
1025 static void brcmf_escan_prep(struct brcmf_cfg80211_info *cfg,
1026 			     struct brcmf_scan_params_le *params_le,
1027 			     struct cfg80211_scan_request *request)
1028 {
1029 	u32 n_ssids;
1030 	u32 n_channels;
1031 	s32 i;
1032 	s32 offset;
1033 	u16 chanspec;
1034 	char *ptr;
1035 	struct brcmf_ssid_le ssid_le;
1036 
1037 	eth_broadcast_addr(params_le->bssid);
1038 	params_le->bss_type = DOT11_BSSTYPE_ANY;
1039 	params_le->scan_type = BRCMF_SCANTYPE_ACTIVE;
1040 	params_le->channel_num = 0;
1041 	params_le->nprobes = cpu_to_le32(-1);
1042 	params_le->active_time = cpu_to_le32(-1);
1043 	params_le->passive_time = cpu_to_le32(-1);
1044 	params_le->home_time = cpu_to_le32(-1);
1045 	memset(&params_le->ssid_le, 0, sizeof(params_le->ssid_le));
1046 
1047 	n_ssids = request->n_ssids;
1048 	n_channels = request->n_channels;
1049 
1050 	/* Copy channel array if applicable */
1051 	brcmf_dbg(SCAN, "### List of channelspecs to scan ### %d\n",
1052 		  n_channels);
1053 	if (n_channels > 0) {
1054 		for (i = 0; i < n_channels; i++) {
1055 			chanspec = channel_to_chanspec(&cfg->d11inf,
1056 						       request->channels[i]);
1057 			brcmf_dbg(SCAN, "Chan : %d, Channel spec: %x\n",
1058 				  request->channels[i]->hw_value, chanspec);
1059 			params_le->channel_list[i] = cpu_to_le16(chanspec);
1060 		}
1061 	} else {
1062 		brcmf_dbg(SCAN, "Scanning all channels\n");
1063 	}
1064 	/* Copy ssid array if applicable */
1065 	brcmf_dbg(SCAN, "### List of SSIDs to scan ### %d\n", n_ssids);
1066 	if (n_ssids > 0) {
1067 		offset = offsetof(struct brcmf_scan_params_le, channel_list) +
1068 				n_channels * sizeof(u16);
1069 		offset = roundup(offset, sizeof(u32));
1070 		ptr = (char *)params_le + offset;
1071 		for (i = 0; i < n_ssids; i++) {
1072 			memset(&ssid_le, 0, sizeof(ssid_le));
1073 			ssid_le.SSID_len =
1074 					cpu_to_le32(request->ssids[i].ssid_len);
1075 			memcpy(ssid_le.SSID, request->ssids[i].ssid,
1076 			       request->ssids[i].ssid_len);
1077 			if (!ssid_le.SSID_len)
1078 				brcmf_dbg(SCAN, "%d: Broadcast scan\n", i);
1079 			else
1080 				brcmf_dbg(SCAN, "%d: scan for  %.32s size=%d\n",
1081 					  i, ssid_le.SSID, ssid_le.SSID_len);
1082 			memcpy(ptr, &ssid_le, sizeof(ssid_le));
1083 			ptr += sizeof(ssid_le);
1084 		}
1085 	} else {
1086 		brcmf_dbg(SCAN, "Performing passive scan\n");
1087 		params_le->scan_type = BRCMF_SCANTYPE_PASSIVE;
1088 	}
1089 	/* Adding mask to channel numbers */
1090 	params_le->channel_num =
1091 		cpu_to_le32((n_ssids << BRCMF_SCAN_PARAMS_NSSID_SHIFT) |
1092 			(n_channels & BRCMF_SCAN_PARAMS_COUNT_MASK));
1093 }
1094 
1095 static s32
1096 brcmf_run_escan(struct brcmf_cfg80211_info *cfg, struct brcmf_if *ifp,
1097 		struct cfg80211_scan_request *request)
1098 {
1099 	struct brcmf_pub *drvr = cfg->pub;
1100 	s32 params_size = BRCMF_SCAN_PARAMS_FIXED_SIZE +
1101 			  offsetof(struct brcmf_escan_params_le, params_le);
1102 	struct brcmf_escan_params_le *params;
1103 	s32 err = 0;
1104 
1105 	brcmf_dbg(SCAN, "E-SCAN START\n");
1106 
1107 	if (request != NULL) {
1108 		/* Allocate space for populating ssids in struct */
1109 		params_size += sizeof(u32) * ((request->n_channels + 1) / 2);
1110 
1111 		/* Allocate space for populating ssids in struct */
1112 		params_size += sizeof(struct brcmf_ssid_le) * request->n_ssids;
1113 	}
1114 
1115 	params = kzalloc(params_size, GFP_KERNEL);
1116 	if (!params) {
1117 		err = -ENOMEM;
1118 		goto exit;
1119 	}
1120 	BUG_ON(params_size + sizeof("escan") >= BRCMF_DCMD_MEDLEN);
1121 	brcmf_escan_prep(cfg, &params->params_le, request);
1122 	params->version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
1123 	params->action = cpu_to_le16(WL_ESCAN_ACTION_START);
1124 	params->sync_id = cpu_to_le16(0x1234);
1125 
1126 	err = brcmf_fil_iovar_data_set(ifp, "escan", params, params_size);
1127 	if (err) {
1128 		if (err == -EBUSY)
1129 			brcmf_dbg(INFO, "system busy : escan canceled\n");
1130 		else
1131 			bphy_err(drvr, "error (%d)\n", err);
1132 	}
1133 
1134 	kfree(params);
1135 exit:
1136 	return err;
1137 }
1138 
1139 static s32
1140 brcmf_do_escan(struct brcmf_if *ifp, struct cfg80211_scan_request *request)
1141 {
1142 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
1143 	s32 err;
1144 	struct brcmf_scan_results *results;
1145 	struct escan_info *escan = &cfg->escan_info;
1146 
1147 	brcmf_dbg(SCAN, "Enter\n");
1148 	escan->ifp = ifp;
1149 	escan->wiphy = cfg->wiphy;
1150 	escan->escan_state = WL_ESCAN_STATE_SCANNING;
1151 
1152 	brcmf_scan_config_mpc(ifp, 0);
1153 	results = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
1154 	results->version = 0;
1155 	results->count = 0;
1156 	results->buflen = WL_ESCAN_RESULTS_FIXED_SIZE;
1157 
1158 	err = escan->run(cfg, ifp, request);
1159 	if (err)
1160 		brcmf_scan_config_mpc(ifp, 1);
1161 	return err;
1162 }
1163 
1164 static s32
1165 brcmf_cfg80211_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
1166 {
1167 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1168 	struct brcmf_pub *drvr = cfg->pub;
1169 	struct brcmf_cfg80211_vif *vif;
1170 	s32 err = 0;
1171 
1172 	brcmf_dbg(TRACE, "Enter\n");
1173 	vif = container_of(request->wdev, struct brcmf_cfg80211_vif, wdev);
1174 	if (!check_vif_up(vif))
1175 		return -EIO;
1176 
1177 	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
1178 		bphy_err(drvr, "Scanning already: status (%lu)\n",
1179 			 cfg->scan_status);
1180 		return -EAGAIN;
1181 	}
1182 	if (test_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status)) {
1183 		bphy_err(drvr, "Scanning being aborted: status (%lu)\n",
1184 			 cfg->scan_status);
1185 		return -EAGAIN;
1186 	}
1187 	if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
1188 		bphy_err(drvr, "Scanning suppressed: status (%lu)\n",
1189 			 cfg->scan_status);
1190 		return -EAGAIN;
1191 	}
1192 	if (test_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state)) {
1193 		bphy_err(drvr, "Connecting: status (%lu)\n", vif->sme_state);
1194 		return -EAGAIN;
1195 	}
1196 
1197 	/* If scan req comes for p2p0, send it over primary I/F */
1198 	if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
1199 		vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1200 
1201 	brcmf_dbg(SCAN, "START ESCAN\n");
1202 
1203 	cfg->scan_request = request;
1204 	set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
1205 
1206 	cfg->escan_info.run = brcmf_run_escan;
1207 	err = brcmf_p2p_scan_prep(wiphy, request, vif);
1208 	if (err)
1209 		goto scan_out;
1210 
1211 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBREQ_FLAG,
1212 				    request->ie, request->ie_len);
1213 	if (err)
1214 		goto scan_out;
1215 
1216 	err = brcmf_do_escan(vif->ifp, request);
1217 	if (err)
1218 		goto scan_out;
1219 
1220 	/* Arm scan timeout timer */
1221 	mod_timer(&cfg->escan_timeout,
1222 		  jiffies + msecs_to_jiffies(BRCMF_ESCAN_TIMER_INTERVAL_MS));
1223 
1224 	return 0;
1225 
1226 scan_out:
1227 	bphy_err(drvr, "scan error (%d)\n", err);
1228 	clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
1229 	cfg->scan_request = NULL;
1230 	return err;
1231 }
1232 
1233 static s32 brcmf_set_rts(struct net_device *ndev, u32 rts_threshold)
1234 {
1235 	struct brcmf_if *ifp = netdev_priv(ndev);
1236 	struct brcmf_pub *drvr = ifp->drvr;
1237 	s32 err = 0;
1238 
1239 	err = brcmf_fil_iovar_int_set(ifp, "rtsthresh", rts_threshold);
1240 	if (err)
1241 		bphy_err(drvr, "Error (%d)\n", err);
1242 
1243 	return err;
1244 }
1245 
1246 static s32 brcmf_set_frag(struct net_device *ndev, u32 frag_threshold)
1247 {
1248 	struct brcmf_if *ifp = netdev_priv(ndev);
1249 	struct brcmf_pub *drvr = ifp->drvr;
1250 	s32 err = 0;
1251 
1252 	err = brcmf_fil_iovar_int_set(ifp, "fragthresh",
1253 				      frag_threshold);
1254 	if (err)
1255 		bphy_err(drvr, "Error (%d)\n", err);
1256 
1257 	return err;
1258 }
1259 
1260 static s32 brcmf_set_retry(struct net_device *ndev, u32 retry, bool l)
1261 {
1262 	struct brcmf_if *ifp = netdev_priv(ndev);
1263 	struct brcmf_pub *drvr = ifp->drvr;
1264 	s32 err = 0;
1265 	u32 cmd = (l ? BRCMF_C_SET_LRL : BRCMF_C_SET_SRL);
1266 
1267 	err = brcmf_fil_cmd_int_set(ifp, cmd, retry);
1268 	if (err) {
1269 		bphy_err(drvr, "cmd (%d) , error (%d)\n", cmd, err);
1270 		return err;
1271 	}
1272 	return err;
1273 }
1274 
1275 static s32 brcmf_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
1276 {
1277 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1278 	struct net_device *ndev = cfg_to_ndev(cfg);
1279 	struct brcmf_if *ifp = netdev_priv(ndev);
1280 	s32 err = 0;
1281 
1282 	brcmf_dbg(TRACE, "Enter\n");
1283 	if (!check_vif_up(ifp->vif))
1284 		return -EIO;
1285 
1286 	if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
1287 	    (cfg->conf->rts_threshold != wiphy->rts_threshold)) {
1288 		cfg->conf->rts_threshold = wiphy->rts_threshold;
1289 		err = brcmf_set_rts(ndev, cfg->conf->rts_threshold);
1290 		if (!err)
1291 			goto done;
1292 	}
1293 	if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
1294 	    (cfg->conf->frag_threshold != wiphy->frag_threshold)) {
1295 		cfg->conf->frag_threshold = wiphy->frag_threshold;
1296 		err = brcmf_set_frag(ndev, cfg->conf->frag_threshold);
1297 		if (!err)
1298 			goto done;
1299 	}
1300 	if (changed & WIPHY_PARAM_RETRY_LONG
1301 	    && (cfg->conf->retry_long != wiphy->retry_long)) {
1302 		cfg->conf->retry_long = wiphy->retry_long;
1303 		err = brcmf_set_retry(ndev, cfg->conf->retry_long, true);
1304 		if (!err)
1305 			goto done;
1306 	}
1307 	if (changed & WIPHY_PARAM_RETRY_SHORT
1308 	    && (cfg->conf->retry_short != wiphy->retry_short)) {
1309 		cfg->conf->retry_short = wiphy->retry_short;
1310 		err = brcmf_set_retry(ndev, cfg->conf->retry_short, false);
1311 		if (!err)
1312 			goto done;
1313 	}
1314 
1315 done:
1316 	brcmf_dbg(TRACE, "Exit\n");
1317 	return err;
1318 }
1319 
1320 static void brcmf_init_prof(struct brcmf_cfg80211_profile *prof)
1321 {
1322 	memset(prof, 0, sizeof(*prof));
1323 }
1324 
1325 static u16 brcmf_map_fw_linkdown_reason(const struct brcmf_event_msg *e)
1326 {
1327 	u16 reason;
1328 
1329 	switch (e->event_code) {
1330 	case BRCMF_E_DEAUTH:
1331 	case BRCMF_E_DEAUTH_IND:
1332 	case BRCMF_E_DISASSOC_IND:
1333 		reason = e->reason;
1334 		break;
1335 	case BRCMF_E_LINK:
1336 	default:
1337 		reason = 0;
1338 		break;
1339 	}
1340 	return reason;
1341 }
1342 
1343 static int brcmf_set_pmk(struct brcmf_if *ifp, const u8 *pmk_data, u16 pmk_len)
1344 {
1345 	struct brcmf_pub *drvr = ifp->drvr;
1346 	struct brcmf_wsec_pmk_le pmk;
1347 	int i, err;
1348 
1349 	/* convert to firmware key format */
1350 	pmk.key_len = cpu_to_le16(pmk_len << 1);
1351 	pmk.flags = cpu_to_le16(BRCMF_WSEC_PASSPHRASE);
1352 	for (i = 0; i < pmk_len; i++)
1353 		snprintf(&pmk.key[2 * i], 3, "%02x", pmk_data[i]);
1354 
1355 	/* store psk in firmware */
1356 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_WSEC_PMK,
1357 				     &pmk, sizeof(pmk));
1358 	if (err < 0)
1359 		bphy_err(drvr, "failed to change PSK in firmware (len=%u)\n",
1360 			 pmk_len);
1361 
1362 	return err;
1363 }
1364 
1365 static int brcmf_set_sae_password(struct brcmf_if *ifp, const u8 *pwd_data,
1366 				  u16 pwd_len)
1367 {
1368 	struct brcmf_pub *drvr = ifp->drvr;
1369 	struct brcmf_wsec_sae_pwd_le sae_pwd;
1370 	int err;
1371 
1372 	if (pwd_len > BRCMF_WSEC_MAX_SAE_PASSWORD_LEN) {
1373 		bphy_err(drvr, "sae_password must be less than %d\n",
1374 			 BRCMF_WSEC_MAX_SAE_PASSWORD_LEN);
1375 		return -EINVAL;
1376 	}
1377 
1378 	sae_pwd.key_len = cpu_to_le16(pwd_len);
1379 	memcpy(sae_pwd.key, pwd_data, pwd_len);
1380 
1381 	err = brcmf_fil_iovar_data_set(ifp, "sae_password", &sae_pwd,
1382 				       sizeof(sae_pwd));
1383 	if (err < 0)
1384 		bphy_err(drvr, "failed to set SAE password in firmware (len=%u)\n",
1385 			 pwd_len);
1386 
1387 	return err;
1388 }
1389 
1390 static void brcmf_link_down(struct brcmf_cfg80211_vif *vif, u16 reason,
1391 			    bool locally_generated)
1392 {
1393 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
1394 	struct brcmf_pub *drvr = cfg->pub;
1395 	bool bus_up = drvr->bus_if->state == BRCMF_BUS_UP;
1396 	s32 err = 0;
1397 
1398 	brcmf_dbg(TRACE, "Enter\n");
1399 
1400 	if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state)) {
1401 		if (bus_up) {
1402 			brcmf_dbg(INFO, "Call WLC_DISASSOC to stop excess roaming\n");
1403 			err = brcmf_fil_cmd_data_set(vif->ifp,
1404 						     BRCMF_C_DISASSOC, NULL, 0);
1405 			if (err)
1406 				bphy_err(drvr, "WLC_DISASSOC failed (%d)\n",
1407 					 err);
1408 		}
1409 
1410 		if ((vif->wdev.iftype == NL80211_IFTYPE_STATION) ||
1411 		    (vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT))
1412 			cfg80211_disconnected(vif->wdev.netdev, reason, NULL, 0,
1413 					      locally_generated, GFP_KERNEL);
1414 	}
1415 	clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state);
1416 	clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
1417 	brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
1418 	if (vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_NONE) {
1419 		if (bus_up)
1420 			brcmf_set_pmk(vif->ifp, NULL, 0);
1421 		vif->profile.use_fwsup = BRCMF_PROFILE_FWSUP_NONE;
1422 	}
1423 	brcmf_dbg(TRACE, "Exit\n");
1424 }
1425 
1426 static s32
1427 brcmf_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
1428 		      struct cfg80211_ibss_params *params)
1429 {
1430 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1431 	struct brcmf_if *ifp = netdev_priv(ndev);
1432 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
1433 	struct brcmf_pub *drvr = cfg->pub;
1434 	struct brcmf_join_params join_params;
1435 	size_t join_params_size = 0;
1436 	s32 err = 0;
1437 	s32 wsec = 0;
1438 	s32 bcnprd;
1439 	u16 chanspec;
1440 	u32 ssid_len;
1441 
1442 	brcmf_dbg(TRACE, "Enter\n");
1443 	if (!check_vif_up(ifp->vif))
1444 		return -EIO;
1445 
1446 	if (params->ssid)
1447 		brcmf_dbg(CONN, "SSID: %s\n", params->ssid);
1448 	else {
1449 		brcmf_dbg(CONN, "SSID: NULL, Not supported\n");
1450 		return -EOPNOTSUPP;
1451 	}
1452 
1453 	set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1454 
1455 	if (params->bssid)
1456 		brcmf_dbg(CONN, "BSSID: %pM\n", params->bssid);
1457 	else
1458 		brcmf_dbg(CONN, "No BSSID specified\n");
1459 
1460 	if (params->chandef.chan)
1461 		brcmf_dbg(CONN, "channel: %d\n",
1462 			  params->chandef.chan->center_freq);
1463 	else
1464 		brcmf_dbg(CONN, "no channel specified\n");
1465 
1466 	if (params->channel_fixed)
1467 		brcmf_dbg(CONN, "fixed channel required\n");
1468 	else
1469 		brcmf_dbg(CONN, "no fixed channel required\n");
1470 
1471 	if (params->ie && params->ie_len)
1472 		brcmf_dbg(CONN, "ie len: %d\n", params->ie_len);
1473 	else
1474 		brcmf_dbg(CONN, "no ie specified\n");
1475 
1476 	if (params->beacon_interval)
1477 		brcmf_dbg(CONN, "beacon interval: %d\n",
1478 			  params->beacon_interval);
1479 	else
1480 		brcmf_dbg(CONN, "no beacon interval specified\n");
1481 
1482 	if (params->basic_rates)
1483 		brcmf_dbg(CONN, "basic rates: %08X\n", params->basic_rates);
1484 	else
1485 		brcmf_dbg(CONN, "no basic rates specified\n");
1486 
1487 	if (params->privacy)
1488 		brcmf_dbg(CONN, "privacy required\n");
1489 	else
1490 		brcmf_dbg(CONN, "no privacy required\n");
1491 
1492 	/* Configure Privacy for starter */
1493 	if (params->privacy)
1494 		wsec |= WEP_ENABLED;
1495 
1496 	err = brcmf_fil_iovar_int_set(ifp, "wsec", wsec);
1497 	if (err) {
1498 		bphy_err(drvr, "wsec failed (%d)\n", err);
1499 		goto done;
1500 	}
1501 
1502 	/* Configure Beacon Interval for starter */
1503 	if (params->beacon_interval)
1504 		bcnprd = params->beacon_interval;
1505 	else
1506 		bcnprd = 100;
1507 
1508 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD, bcnprd);
1509 	if (err) {
1510 		bphy_err(drvr, "WLC_SET_BCNPRD failed (%d)\n", err);
1511 		goto done;
1512 	}
1513 
1514 	/* Configure required join parameter */
1515 	memset(&join_params, 0, sizeof(struct brcmf_join_params));
1516 
1517 	/* SSID */
1518 	ssid_len = min_t(u32, params->ssid_len, IEEE80211_MAX_SSID_LEN);
1519 	memcpy(join_params.ssid_le.SSID, params->ssid, ssid_len);
1520 	join_params.ssid_le.SSID_len = cpu_to_le32(ssid_len);
1521 	join_params_size = sizeof(join_params.ssid_le);
1522 
1523 	/* BSSID */
1524 	if (params->bssid) {
1525 		memcpy(join_params.params_le.bssid, params->bssid, ETH_ALEN);
1526 		join_params_size += BRCMF_ASSOC_PARAMS_FIXED_SIZE;
1527 		memcpy(profile->bssid, params->bssid, ETH_ALEN);
1528 	} else {
1529 		eth_broadcast_addr(join_params.params_le.bssid);
1530 		eth_zero_addr(profile->bssid);
1531 	}
1532 
1533 	/* Channel */
1534 	if (params->chandef.chan) {
1535 		u32 target_channel;
1536 
1537 		cfg->channel =
1538 			ieee80211_frequency_to_channel(
1539 				params->chandef.chan->center_freq);
1540 		if (params->channel_fixed) {
1541 			/* adding chanspec */
1542 			chanspec = chandef_to_chanspec(&cfg->d11inf,
1543 						       &params->chandef);
1544 			join_params.params_le.chanspec_list[0] =
1545 				cpu_to_le16(chanspec);
1546 			join_params.params_le.chanspec_num = cpu_to_le32(1);
1547 			join_params_size += sizeof(join_params.params_le);
1548 		}
1549 
1550 		/* set channel for starter */
1551 		target_channel = cfg->channel;
1552 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_CHANNEL,
1553 					    target_channel);
1554 		if (err) {
1555 			bphy_err(drvr, "WLC_SET_CHANNEL failed (%d)\n", err);
1556 			goto done;
1557 		}
1558 	} else
1559 		cfg->channel = 0;
1560 
1561 	cfg->ibss_starter = false;
1562 
1563 
1564 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
1565 				     &join_params, join_params_size);
1566 	if (err) {
1567 		bphy_err(drvr, "WLC_SET_SSID failed (%d)\n", err);
1568 		goto done;
1569 	}
1570 
1571 done:
1572 	if (err)
1573 		clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
1574 	brcmf_dbg(TRACE, "Exit\n");
1575 	return err;
1576 }
1577 
1578 static s32
1579 brcmf_cfg80211_leave_ibss(struct wiphy *wiphy, struct net_device *ndev)
1580 {
1581 	struct brcmf_if *ifp = netdev_priv(ndev);
1582 
1583 	brcmf_dbg(TRACE, "Enter\n");
1584 	if (!check_vif_up(ifp->vif)) {
1585 		/* When driver is being unloaded, it can end up here. If an
1586 		 * error is returned then later on a debug trace in the wireless
1587 		 * core module will be printed. To avoid this 0 is returned.
1588 		 */
1589 		return 0;
1590 	}
1591 
1592 	brcmf_link_down(ifp->vif, WLAN_REASON_DEAUTH_LEAVING, true);
1593 	brcmf_net_setcarrier(ifp, false);
1594 
1595 	brcmf_dbg(TRACE, "Exit\n");
1596 
1597 	return 0;
1598 }
1599 
1600 static s32 brcmf_set_wpa_version(struct net_device *ndev,
1601 				 struct cfg80211_connect_params *sme)
1602 {
1603 	struct brcmf_if *ifp = netdev_priv(ndev);
1604 	struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1605 	struct brcmf_pub *drvr = ifp->drvr;
1606 	struct brcmf_cfg80211_security *sec;
1607 	s32 val = 0;
1608 	s32 err = 0;
1609 
1610 	if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_1)
1611 		val = WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED;
1612 	else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_2)
1613 		val = WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED;
1614 	else if (sme->crypto.wpa_versions & NL80211_WPA_VERSION_3)
1615 		val = WPA3_AUTH_SAE_PSK;
1616 	else
1617 		val = WPA_AUTH_DISABLED;
1618 	brcmf_dbg(CONN, "setting wpa_auth to 0x%0x\n", val);
1619 	err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", val);
1620 	if (err) {
1621 		bphy_err(drvr, "set wpa_auth failed (%d)\n", err);
1622 		return err;
1623 	}
1624 	sec = &profile->sec;
1625 	sec->wpa_versions = sme->crypto.wpa_versions;
1626 	return err;
1627 }
1628 
1629 static s32 brcmf_set_auth_type(struct net_device *ndev,
1630 			       struct cfg80211_connect_params *sme)
1631 {
1632 	struct brcmf_if *ifp = netdev_priv(ndev);
1633 	struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1634 	struct brcmf_pub *drvr = ifp->drvr;
1635 	struct brcmf_cfg80211_security *sec;
1636 	s32 val = 0;
1637 	s32 err = 0;
1638 
1639 	switch (sme->auth_type) {
1640 	case NL80211_AUTHTYPE_OPEN_SYSTEM:
1641 		val = 0;
1642 		brcmf_dbg(CONN, "open system\n");
1643 		break;
1644 	case NL80211_AUTHTYPE_SHARED_KEY:
1645 		val = 1;
1646 		brcmf_dbg(CONN, "shared key\n");
1647 		break;
1648 	case NL80211_AUTHTYPE_SAE:
1649 		val = 3;
1650 		brcmf_dbg(CONN, "SAE authentication\n");
1651 		break;
1652 	default:
1653 		val = 2;
1654 		brcmf_dbg(CONN, "automatic, auth type (%d)\n", sme->auth_type);
1655 		break;
1656 	}
1657 
1658 	err = brcmf_fil_bsscfg_int_set(ifp, "auth", val);
1659 	if (err) {
1660 		bphy_err(drvr, "set auth failed (%d)\n", err);
1661 		return err;
1662 	}
1663 	sec = &profile->sec;
1664 	sec->auth_type = sme->auth_type;
1665 	return err;
1666 }
1667 
1668 static s32
1669 brcmf_set_wsec_mode(struct net_device *ndev,
1670 		    struct cfg80211_connect_params *sme)
1671 {
1672 	struct brcmf_if *ifp = netdev_priv(ndev);
1673 	struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1674 	struct brcmf_pub *drvr = ifp->drvr;
1675 	struct brcmf_cfg80211_security *sec;
1676 	s32 pval = 0;
1677 	s32 gval = 0;
1678 	s32 wsec;
1679 	s32 err = 0;
1680 
1681 	if (sme->crypto.n_ciphers_pairwise) {
1682 		switch (sme->crypto.ciphers_pairwise[0]) {
1683 		case WLAN_CIPHER_SUITE_WEP40:
1684 		case WLAN_CIPHER_SUITE_WEP104:
1685 			pval = WEP_ENABLED;
1686 			break;
1687 		case WLAN_CIPHER_SUITE_TKIP:
1688 			pval = TKIP_ENABLED;
1689 			break;
1690 		case WLAN_CIPHER_SUITE_CCMP:
1691 			pval = AES_ENABLED;
1692 			break;
1693 		case WLAN_CIPHER_SUITE_AES_CMAC:
1694 			pval = AES_ENABLED;
1695 			break;
1696 		default:
1697 			bphy_err(drvr, "invalid cipher pairwise (%d)\n",
1698 				 sme->crypto.ciphers_pairwise[0]);
1699 			return -EINVAL;
1700 		}
1701 	}
1702 	if (sme->crypto.cipher_group) {
1703 		switch (sme->crypto.cipher_group) {
1704 		case WLAN_CIPHER_SUITE_WEP40:
1705 		case WLAN_CIPHER_SUITE_WEP104:
1706 			gval = WEP_ENABLED;
1707 			break;
1708 		case WLAN_CIPHER_SUITE_TKIP:
1709 			gval = TKIP_ENABLED;
1710 			break;
1711 		case WLAN_CIPHER_SUITE_CCMP:
1712 			gval = AES_ENABLED;
1713 			break;
1714 		case WLAN_CIPHER_SUITE_AES_CMAC:
1715 			gval = AES_ENABLED;
1716 			break;
1717 		default:
1718 			bphy_err(drvr, "invalid cipher group (%d)\n",
1719 				 sme->crypto.cipher_group);
1720 			return -EINVAL;
1721 		}
1722 	}
1723 
1724 	brcmf_dbg(CONN, "pval (%d) gval (%d)\n", pval, gval);
1725 	/* In case of privacy, but no security and WPS then simulate */
1726 	/* setting AES. WPS-2.0 allows no security                   */
1727 	if (brcmf_find_wpsie(sme->ie, sme->ie_len) && !pval && !gval &&
1728 	    sme->privacy)
1729 		pval = AES_ENABLED;
1730 
1731 	wsec = pval | gval;
1732 	err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
1733 	if (err) {
1734 		bphy_err(drvr, "error (%d)\n", err);
1735 		return err;
1736 	}
1737 
1738 	sec = &profile->sec;
1739 	sec->cipher_pairwise = sme->crypto.ciphers_pairwise[0];
1740 	sec->cipher_group = sme->crypto.cipher_group;
1741 
1742 	return err;
1743 }
1744 
1745 static s32
1746 brcmf_set_key_mgmt(struct net_device *ndev, struct cfg80211_connect_params *sme)
1747 {
1748 	struct brcmf_if *ifp = netdev_priv(ndev);
1749 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
1750 	struct brcmf_pub *drvr = ifp->drvr;
1751 	s32 val;
1752 	s32 err;
1753 	const struct brcmf_tlv *rsn_ie;
1754 	const u8 *ie;
1755 	u32 ie_len;
1756 	u32 offset;
1757 	u16 rsn_cap;
1758 	u32 mfp;
1759 	u16 count;
1760 
1761 	profile->use_fwsup = BRCMF_PROFILE_FWSUP_NONE;
1762 	profile->is_ft = false;
1763 
1764 	if (!sme->crypto.n_akm_suites)
1765 		return 0;
1766 
1767 	err = brcmf_fil_bsscfg_int_get(netdev_priv(ndev), "wpa_auth", &val);
1768 	if (err) {
1769 		bphy_err(drvr, "could not get wpa_auth (%d)\n", err);
1770 		return err;
1771 	}
1772 	if (val & (WPA_AUTH_PSK | WPA_AUTH_UNSPECIFIED)) {
1773 		switch (sme->crypto.akm_suites[0]) {
1774 		case WLAN_AKM_SUITE_8021X:
1775 			val = WPA_AUTH_UNSPECIFIED;
1776 			if (sme->want_1x)
1777 				profile->use_fwsup = BRCMF_PROFILE_FWSUP_1X;
1778 			break;
1779 		case WLAN_AKM_SUITE_PSK:
1780 			val = WPA_AUTH_PSK;
1781 			break;
1782 		default:
1783 			bphy_err(drvr, "invalid cipher group (%d)\n",
1784 				 sme->crypto.cipher_group);
1785 			return -EINVAL;
1786 		}
1787 	} else if (val & (WPA2_AUTH_PSK | WPA2_AUTH_UNSPECIFIED)) {
1788 		switch (sme->crypto.akm_suites[0]) {
1789 		case WLAN_AKM_SUITE_8021X:
1790 			val = WPA2_AUTH_UNSPECIFIED;
1791 			if (sme->want_1x)
1792 				profile->use_fwsup = BRCMF_PROFILE_FWSUP_1X;
1793 			break;
1794 		case WLAN_AKM_SUITE_8021X_SHA256:
1795 			val = WPA2_AUTH_1X_SHA256;
1796 			if (sme->want_1x)
1797 				profile->use_fwsup = BRCMF_PROFILE_FWSUP_1X;
1798 			break;
1799 		case WLAN_AKM_SUITE_PSK_SHA256:
1800 			val = WPA2_AUTH_PSK_SHA256;
1801 			break;
1802 		case WLAN_AKM_SUITE_PSK:
1803 			val = WPA2_AUTH_PSK;
1804 			break;
1805 		case WLAN_AKM_SUITE_FT_8021X:
1806 			val = WPA2_AUTH_UNSPECIFIED | WPA2_AUTH_FT;
1807 			profile->is_ft = true;
1808 			if (sme->want_1x)
1809 				profile->use_fwsup = BRCMF_PROFILE_FWSUP_1X;
1810 			break;
1811 		case WLAN_AKM_SUITE_FT_PSK:
1812 			val = WPA2_AUTH_PSK | WPA2_AUTH_FT;
1813 			profile->is_ft = true;
1814 			break;
1815 		default:
1816 			bphy_err(drvr, "invalid cipher group (%d)\n",
1817 				 sme->crypto.cipher_group);
1818 			return -EINVAL;
1819 		}
1820 	} else if (val & WPA3_AUTH_SAE_PSK) {
1821 		switch (sme->crypto.akm_suites[0]) {
1822 		case WLAN_AKM_SUITE_SAE:
1823 			val = WPA3_AUTH_SAE_PSK;
1824 			if (sme->crypto.sae_pwd) {
1825 				brcmf_dbg(INFO, "using SAE offload\n");
1826 				profile->use_fwsup = BRCMF_PROFILE_FWSUP_SAE;
1827 			}
1828 			break;
1829 		default:
1830 			bphy_err(drvr, "invalid cipher group (%d)\n",
1831 				 sme->crypto.cipher_group);
1832 			return -EINVAL;
1833 		}
1834 	}
1835 
1836 	if (profile->use_fwsup == BRCMF_PROFILE_FWSUP_1X)
1837 		brcmf_dbg(INFO, "using 1X offload\n");
1838 
1839 	if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP))
1840 		goto skip_mfp_config;
1841 	/* The MFP mode (1 or 2) needs to be determined, parse IEs. The
1842 	 * IE will not be verified, just a quick search for MFP config
1843 	 */
1844 	rsn_ie = brcmf_parse_tlvs((const u8 *)sme->ie, sme->ie_len,
1845 				  WLAN_EID_RSN);
1846 	if (!rsn_ie)
1847 		goto skip_mfp_config;
1848 	ie = (const u8 *)rsn_ie;
1849 	ie_len = rsn_ie->len + TLV_HDR_LEN;
1850 	/* Skip unicast suite */
1851 	offset = TLV_HDR_LEN + WPA_IE_VERSION_LEN + WPA_IE_MIN_OUI_LEN;
1852 	if (offset + WPA_IE_SUITE_COUNT_LEN >= ie_len)
1853 		goto skip_mfp_config;
1854 	/* Skip multicast suite */
1855 	count = ie[offset] + (ie[offset + 1] << 8);
1856 	offset += WPA_IE_SUITE_COUNT_LEN + (count * WPA_IE_MIN_OUI_LEN);
1857 	if (offset + WPA_IE_SUITE_COUNT_LEN >= ie_len)
1858 		goto skip_mfp_config;
1859 	/* Skip auth key management suite(s) */
1860 	count = ie[offset] + (ie[offset + 1] << 8);
1861 	offset += WPA_IE_SUITE_COUNT_LEN + (count * WPA_IE_MIN_OUI_LEN);
1862 	if (offset + WPA_IE_SUITE_COUNT_LEN > ie_len)
1863 		goto skip_mfp_config;
1864 	/* Ready to read capabilities */
1865 	mfp = BRCMF_MFP_NONE;
1866 	rsn_cap = ie[offset] + (ie[offset + 1] << 8);
1867 	if (rsn_cap & RSN_CAP_MFPR_MASK)
1868 		mfp = BRCMF_MFP_REQUIRED;
1869 	else if (rsn_cap & RSN_CAP_MFPC_MASK)
1870 		mfp = BRCMF_MFP_CAPABLE;
1871 	brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "mfp", mfp);
1872 
1873 skip_mfp_config:
1874 	brcmf_dbg(CONN, "setting wpa_auth to %d\n", val);
1875 	err = brcmf_fil_bsscfg_int_set(netdev_priv(ndev), "wpa_auth", val);
1876 	if (err) {
1877 		bphy_err(drvr, "could not set wpa_auth (%d)\n", err);
1878 		return err;
1879 	}
1880 
1881 	return err;
1882 }
1883 
1884 static s32
1885 brcmf_set_sharedkey(struct net_device *ndev,
1886 		    struct cfg80211_connect_params *sme)
1887 {
1888 	struct brcmf_if *ifp = netdev_priv(ndev);
1889 	struct brcmf_pub *drvr = ifp->drvr;
1890 	struct brcmf_cfg80211_profile *profile = ndev_to_prof(ndev);
1891 	struct brcmf_cfg80211_security *sec;
1892 	struct brcmf_wsec_key key;
1893 	s32 val;
1894 	s32 err = 0;
1895 
1896 	brcmf_dbg(CONN, "key len (%d)\n", sme->key_len);
1897 
1898 	if (sme->key_len == 0)
1899 		return 0;
1900 
1901 	sec = &profile->sec;
1902 	brcmf_dbg(CONN, "wpa_versions 0x%x cipher_pairwise 0x%x\n",
1903 		  sec->wpa_versions, sec->cipher_pairwise);
1904 
1905 	if (sec->wpa_versions & (NL80211_WPA_VERSION_1 | NL80211_WPA_VERSION_2 |
1906 				 NL80211_WPA_VERSION_3))
1907 		return 0;
1908 
1909 	if (!(sec->cipher_pairwise &
1910 	    (WLAN_CIPHER_SUITE_WEP40 | WLAN_CIPHER_SUITE_WEP104)))
1911 		return 0;
1912 
1913 	memset(&key, 0, sizeof(key));
1914 	key.len = (u32) sme->key_len;
1915 	key.index = (u32) sme->key_idx;
1916 	if (key.len > sizeof(key.data)) {
1917 		bphy_err(drvr, "Too long key length (%u)\n", key.len);
1918 		return -EINVAL;
1919 	}
1920 	memcpy(key.data, sme->key, key.len);
1921 	key.flags = BRCMF_PRIMARY_KEY;
1922 	switch (sec->cipher_pairwise) {
1923 	case WLAN_CIPHER_SUITE_WEP40:
1924 		key.algo = CRYPTO_ALGO_WEP1;
1925 		break;
1926 	case WLAN_CIPHER_SUITE_WEP104:
1927 		key.algo = CRYPTO_ALGO_WEP128;
1928 		break;
1929 	default:
1930 		bphy_err(drvr, "Invalid algorithm (%d)\n",
1931 			 sme->crypto.ciphers_pairwise[0]);
1932 		return -EINVAL;
1933 	}
1934 	/* Set the new key/index */
1935 	brcmf_dbg(CONN, "key length (%d) key index (%d) algo (%d)\n",
1936 		  key.len, key.index, key.algo);
1937 	brcmf_dbg(CONN, "key \"%s\"\n", key.data);
1938 	err = send_key_to_dongle(ifp, &key);
1939 	if (err)
1940 		return err;
1941 
1942 	if (sec->auth_type == NL80211_AUTHTYPE_SHARED_KEY) {
1943 		brcmf_dbg(CONN, "set auth_type to shared key\n");
1944 		val = WL_AUTH_SHARED_KEY;	/* shared key */
1945 		err = brcmf_fil_bsscfg_int_set(ifp, "auth", val);
1946 		if (err)
1947 			bphy_err(drvr, "set auth failed (%d)\n", err);
1948 	}
1949 	return err;
1950 }
1951 
1952 static
1953 enum nl80211_auth_type brcmf_war_auth_type(struct brcmf_if *ifp,
1954 					   enum nl80211_auth_type type)
1955 {
1956 	if (type == NL80211_AUTHTYPE_AUTOMATIC &&
1957 	    brcmf_feat_is_quirk_enabled(ifp, BRCMF_FEAT_QUIRK_AUTO_AUTH)) {
1958 		brcmf_dbg(CONN, "WAR: use OPEN instead of AUTO\n");
1959 		type = NL80211_AUTHTYPE_OPEN_SYSTEM;
1960 	}
1961 	return type;
1962 }
1963 
1964 static void brcmf_set_join_pref(struct brcmf_if *ifp,
1965 				struct cfg80211_bss_selection *bss_select)
1966 {
1967 	struct brcmf_pub *drvr = ifp->drvr;
1968 	struct brcmf_join_pref_params join_pref_params[2];
1969 	enum nl80211_band band;
1970 	int err, i = 0;
1971 
1972 	join_pref_params[i].len = 2;
1973 	join_pref_params[i].rssi_gain = 0;
1974 
1975 	if (bss_select->behaviour != NL80211_BSS_SELECT_ATTR_BAND_PREF)
1976 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_ASSOC_PREFER, WLC_BAND_AUTO);
1977 
1978 	switch (bss_select->behaviour) {
1979 	case __NL80211_BSS_SELECT_ATTR_INVALID:
1980 		brcmf_c_set_joinpref_default(ifp);
1981 		return;
1982 	case NL80211_BSS_SELECT_ATTR_BAND_PREF:
1983 		join_pref_params[i].type = BRCMF_JOIN_PREF_BAND;
1984 		band = bss_select->param.band_pref;
1985 		join_pref_params[i].band = nl80211_band_to_fwil(band);
1986 		i++;
1987 		break;
1988 	case NL80211_BSS_SELECT_ATTR_RSSI_ADJUST:
1989 		join_pref_params[i].type = BRCMF_JOIN_PREF_RSSI_DELTA;
1990 		band = bss_select->param.adjust.band;
1991 		join_pref_params[i].band = nl80211_band_to_fwil(band);
1992 		join_pref_params[i].rssi_gain = bss_select->param.adjust.delta;
1993 		i++;
1994 		break;
1995 	case NL80211_BSS_SELECT_ATTR_RSSI:
1996 	default:
1997 		break;
1998 	}
1999 	join_pref_params[i].type = BRCMF_JOIN_PREF_RSSI;
2000 	join_pref_params[i].len = 2;
2001 	join_pref_params[i].rssi_gain = 0;
2002 	join_pref_params[i].band = 0;
2003 	err = brcmf_fil_iovar_data_set(ifp, "join_pref", join_pref_params,
2004 				       sizeof(join_pref_params));
2005 	if (err)
2006 		bphy_err(drvr, "Set join_pref error (%d)\n", err);
2007 }
2008 
2009 static s32
2010 brcmf_cfg80211_connect(struct wiphy *wiphy, struct net_device *ndev,
2011 		       struct cfg80211_connect_params *sme)
2012 {
2013 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2014 	struct brcmf_if *ifp = netdev_priv(ndev);
2015 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
2016 	struct ieee80211_channel *chan = sme->channel;
2017 	struct brcmf_pub *drvr = ifp->drvr;
2018 	struct brcmf_join_params join_params;
2019 	size_t join_params_size;
2020 	const struct brcmf_tlv *rsn_ie;
2021 	const struct brcmf_vs_tlv *wpa_ie;
2022 	const void *ie;
2023 	u32 ie_len;
2024 	struct brcmf_ext_join_params_le *ext_join_params;
2025 	u16 chanspec;
2026 	s32 err = 0;
2027 	u32 ssid_len;
2028 
2029 	brcmf_dbg(TRACE, "Enter\n");
2030 	if (!check_vif_up(ifp->vif))
2031 		return -EIO;
2032 
2033 	if (!sme->ssid) {
2034 		bphy_err(drvr, "Invalid ssid\n");
2035 		return -EOPNOTSUPP;
2036 	}
2037 
2038 	if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif) {
2039 		/* A normal (non P2P) connection request setup. */
2040 		ie = NULL;
2041 		ie_len = 0;
2042 		/* find the WPA_IE */
2043 		wpa_ie = brcmf_find_wpaie((u8 *)sme->ie, sme->ie_len);
2044 		if (wpa_ie) {
2045 			ie = wpa_ie;
2046 			ie_len = wpa_ie->len + TLV_HDR_LEN;
2047 		} else {
2048 			/* find the RSN_IE */
2049 			rsn_ie = brcmf_parse_tlvs((const u8 *)sme->ie,
2050 						  sme->ie_len,
2051 						  WLAN_EID_RSN);
2052 			if (rsn_ie) {
2053 				ie = rsn_ie;
2054 				ie_len = rsn_ie->len + TLV_HDR_LEN;
2055 			}
2056 		}
2057 		brcmf_fil_iovar_data_set(ifp, "wpaie", ie, ie_len);
2058 	}
2059 
2060 	err = brcmf_vif_set_mgmt_ie(ifp->vif, BRCMF_VNDR_IE_ASSOCREQ_FLAG,
2061 				    sme->ie, sme->ie_len);
2062 	if (err)
2063 		bphy_err(drvr, "Set Assoc REQ IE Failed\n");
2064 	else
2065 		brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc request\n");
2066 
2067 	set_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
2068 
2069 	if (chan) {
2070 		cfg->channel =
2071 			ieee80211_frequency_to_channel(chan->center_freq);
2072 		chanspec = channel_to_chanspec(&cfg->d11inf, chan);
2073 		brcmf_dbg(CONN, "channel=%d, center_req=%d, chanspec=0x%04x\n",
2074 			  cfg->channel, chan->center_freq, chanspec);
2075 	} else {
2076 		cfg->channel = 0;
2077 		chanspec = 0;
2078 	}
2079 
2080 	brcmf_dbg(INFO, "ie (%p), ie_len (%zd)\n", sme->ie, sme->ie_len);
2081 
2082 	err = brcmf_set_wpa_version(ndev, sme);
2083 	if (err) {
2084 		bphy_err(drvr, "wl_set_wpa_version failed (%d)\n", err);
2085 		goto done;
2086 	}
2087 
2088 	sme->auth_type = brcmf_war_auth_type(ifp, sme->auth_type);
2089 	err = brcmf_set_auth_type(ndev, sme);
2090 	if (err) {
2091 		bphy_err(drvr, "wl_set_auth_type failed (%d)\n", err);
2092 		goto done;
2093 	}
2094 
2095 	err = brcmf_set_wsec_mode(ndev, sme);
2096 	if (err) {
2097 		bphy_err(drvr, "wl_set_set_cipher failed (%d)\n", err);
2098 		goto done;
2099 	}
2100 
2101 	err = brcmf_set_key_mgmt(ndev, sme);
2102 	if (err) {
2103 		bphy_err(drvr, "wl_set_key_mgmt failed (%d)\n", err);
2104 		goto done;
2105 	}
2106 
2107 	err = brcmf_set_sharedkey(ndev, sme);
2108 	if (err) {
2109 		bphy_err(drvr, "brcmf_set_sharedkey failed (%d)\n", err);
2110 		goto done;
2111 	}
2112 
2113 	if (sme->crypto.psk &&
2114 	    profile->use_fwsup != BRCMF_PROFILE_FWSUP_SAE) {
2115 		if (WARN_ON(profile->use_fwsup != BRCMF_PROFILE_FWSUP_NONE)) {
2116 			err = -EINVAL;
2117 			goto done;
2118 		}
2119 		brcmf_dbg(INFO, "using PSK offload\n");
2120 		profile->use_fwsup = BRCMF_PROFILE_FWSUP_PSK;
2121 	}
2122 
2123 	if (profile->use_fwsup != BRCMF_PROFILE_FWSUP_NONE) {
2124 		/* enable firmware supplicant for this interface */
2125 		err = brcmf_fil_iovar_int_set(ifp, "sup_wpa", 1);
2126 		if (err < 0) {
2127 			bphy_err(drvr, "failed to enable fw supplicant\n");
2128 			goto done;
2129 		}
2130 	}
2131 
2132 	if (profile->use_fwsup == BRCMF_PROFILE_FWSUP_PSK)
2133 		err = brcmf_set_pmk(ifp, sme->crypto.psk,
2134 				    BRCMF_WSEC_MAX_PSK_LEN);
2135 	else if (profile->use_fwsup == BRCMF_PROFILE_FWSUP_SAE) {
2136 		/* clean up user-space RSNE */
2137 		if (brcmf_fil_iovar_data_set(ifp, "wpaie", NULL, 0)) {
2138 			bphy_err(drvr, "failed to clean up user-space RSNE\n");
2139 			goto done;
2140 		}
2141 		err = brcmf_set_sae_password(ifp, sme->crypto.sae_pwd,
2142 					     sme->crypto.sae_pwd_len);
2143 		if (!err && sme->crypto.psk)
2144 			err = brcmf_set_pmk(ifp, sme->crypto.psk,
2145 					    BRCMF_WSEC_MAX_PSK_LEN);
2146 	}
2147 	if (err)
2148 		goto done;
2149 
2150 	/* Join with specific BSSID and cached SSID
2151 	 * If SSID is zero join based on BSSID only
2152 	 */
2153 	join_params_size = offsetof(struct brcmf_ext_join_params_le, assoc_le) +
2154 		offsetof(struct brcmf_assoc_params_le, chanspec_list);
2155 	if (cfg->channel)
2156 		join_params_size += sizeof(u16);
2157 	ext_join_params = kzalloc(join_params_size, GFP_KERNEL);
2158 	if (ext_join_params == NULL) {
2159 		err = -ENOMEM;
2160 		goto done;
2161 	}
2162 	ssid_len = min_t(u32, sme->ssid_len, IEEE80211_MAX_SSID_LEN);
2163 	ext_join_params->ssid_le.SSID_len = cpu_to_le32(ssid_len);
2164 	memcpy(&ext_join_params->ssid_le.SSID, sme->ssid, ssid_len);
2165 	if (ssid_len < IEEE80211_MAX_SSID_LEN)
2166 		brcmf_dbg(CONN, "SSID \"%s\", len (%d)\n",
2167 			  ext_join_params->ssid_le.SSID, ssid_len);
2168 
2169 	/* Set up join scan parameters */
2170 	ext_join_params->scan_le.scan_type = -1;
2171 	ext_join_params->scan_le.home_time = cpu_to_le32(-1);
2172 
2173 	if (sme->bssid)
2174 		memcpy(&ext_join_params->assoc_le.bssid, sme->bssid, ETH_ALEN);
2175 	else
2176 		eth_broadcast_addr(ext_join_params->assoc_le.bssid);
2177 
2178 	if (cfg->channel) {
2179 		ext_join_params->assoc_le.chanspec_num = cpu_to_le32(1);
2180 
2181 		ext_join_params->assoc_le.chanspec_list[0] =
2182 			cpu_to_le16(chanspec);
2183 		/* Increase dwell time to receive probe response or detect
2184 		 * beacon from target AP at a noisy air only during connect
2185 		 * command.
2186 		 */
2187 		ext_join_params->scan_le.active_time =
2188 			cpu_to_le32(BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS);
2189 		ext_join_params->scan_le.passive_time =
2190 			cpu_to_le32(BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS);
2191 		/* To sync with presence period of VSDB GO send probe request
2192 		 * more frequently. Probe request will be stopped when it gets
2193 		 * probe response from target AP/GO.
2194 		 */
2195 		ext_join_params->scan_le.nprobes =
2196 			cpu_to_le32(BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS /
2197 				    BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS);
2198 	} else {
2199 		ext_join_params->scan_le.active_time = cpu_to_le32(-1);
2200 		ext_join_params->scan_le.passive_time = cpu_to_le32(-1);
2201 		ext_join_params->scan_le.nprobes = cpu_to_le32(-1);
2202 	}
2203 
2204 	brcmf_set_join_pref(ifp, &sme->bss_select);
2205 
2206 	err  = brcmf_fil_bsscfg_data_set(ifp, "join", ext_join_params,
2207 					 join_params_size);
2208 	kfree(ext_join_params);
2209 	if (!err)
2210 		/* This is it. join command worked, we are done */
2211 		goto done;
2212 
2213 	/* join command failed, fallback to set ssid */
2214 	memset(&join_params, 0, sizeof(join_params));
2215 	join_params_size = sizeof(join_params.ssid_le);
2216 
2217 	memcpy(&join_params.ssid_le.SSID, sme->ssid, ssid_len);
2218 	join_params.ssid_le.SSID_len = cpu_to_le32(ssid_len);
2219 
2220 	if (sme->bssid)
2221 		memcpy(join_params.params_le.bssid, sme->bssid, ETH_ALEN);
2222 	else
2223 		eth_broadcast_addr(join_params.params_le.bssid);
2224 
2225 	if (cfg->channel) {
2226 		join_params.params_le.chanspec_list[0] = cpu_to_le16(chanspec);
2227 		join_params.params_le.chanspec_num = cpu_to_le32(1);
2228 		join_params_size += sizeof(join_params.params_le);
2229 	}
2230 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
2231 				     &join_params, join_params_size);
2232 	if (err)
2233 		bphy_err(drvr, "BRCMF_C_SET_SSID failed (%d)\n", err);
2234 
2235 done:
2236 	if (err)
2237 		clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
2238 	brcmf_dbg(TRACE, "Exit\n");
2239 	return err;
2240 }
2241 
2242 static s32
2243 brcmf_cfg80211_disconnect(struct wiphy *wiphy, struct net_device *ndev,
2244 		       u16 reason_code)
2245 {
2246 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2247 	struct brcmf_if *ifp = netdev_priv(ndev);
2248 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
2249 	struct brcmf_pub *drvr = cfg->pub;
2250 	struct brcmf_scb_val_le scbval;
2251 	s32 err = 0;
2252 
2253 	brcmf_dbg(TRACE, "Enter. Reason code = %d\n", reason_code);
2254 	if (!check_vif_up(ifp->vif))
2255 		return -EIO;
2256 
2257 	clear_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
2258 	clear_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state);
2259 	cfg80211_disconnected(ndev, reason_code, NULL, 0, true, GFP_KERNEL);
2260 
2261 	memcpy(&scbval.ea, &profile->bssid, ETH_ALEN);
2262 	scbval.val = cpu_to_le32(reason_code);
2263 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_DISASSOC,
2264 				     &scbval, sizeof(scbval));
2265 	if (err)
2266 		bphy_err(drvr, "error (%d)\n", err);
2267 
2268 	brcmf_dbg(TRACE, "Exit\n");
2269 	return err;
2270 }
2271 
2272 static s32
2273 brcmf_cfg80211_set_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
2274 			    enum nl80211_tx_power_setting type, s32 mbm)
2275 {
2276 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2277 	struct net_device *ndev = cfg_to_ndev(cfg);
2278 	struct brcmf_if *ifp = netdev_priv(ndev);
2279 	struct brcmf_pub *drvr = cfg->pub;
2280 	s32 err;
2281 	s32 disable;
2282 	u32 qdbm = 127;
2283 
2284 	brcmf_dbg(TRACE, "Enter %d %d\n", type, mbm);
2285 	if (!check_vif_up(ifp->vif))
2286 		return -EIO;
2287 
2288 	switch (type) {
2289 	case NL80211_TX_POWER_AUTOMATIC:
2290 		break;
2291 	case NL80211_TX_POWER_LIMITED:
2292 	case NL80211_TX_POWER_FIXED:
2293 		if (mbm < 0) {
2294 			bphy_err(drvr, "TX_POWER_FIXED - dbm is negative\n");
2295 			err = -EINVAL;
2296 			goto done;
2297 		}
2298 		qdbm =  MBM_TO_DBM(4 * mbm);
2299 		if (qdbm > 127)
2300 			qdbm = 127;
2301 		qdbm |= WL_TXPWR_OVERRIDE;
2302 		break;
2303 	default:
2304 		bphy_err(drvr, "Unsupported type %d\n", type);
2305 		err = -EINVAL;
2306 		goto done;
2307 	}
2308 	/* Make sure radio is off or on as far as software is concerned */
2309 	disable = WL_RADIO_SW_DISABLE << 16;
2310 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_RADIO, disable);
2311 	if (err)
2312 		bphy_err(drvr, "WLC_SET_RADIO error (%d)\n", err);
2313 
2314 	err = brcmf_fil_iovar_int_set(ifp, "qtxpower", qdbm);
2315 	if (err)
2316 		bphy_err(drvr, "qtxpower error (%d)\n", err);
2317 
2318 done:
2319 	brcmf_dbg(TRACE, "Exit %d (qdbm)\n", qdbm & ~WL_TXPWR_OVERRIDE);
2320 	return err;
2321 }
2322 
2323 static s32
2324 brcmf_cfg80211_get_tx_power(struct wiphy *wiphy, struct wireless_dev *wdev,
2325 			    s32 *dbm)
2326 {
2327 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2328 	struct brcmf_cfg80211_vif *vif = wdev_to_vif(wdev);
2329 	struct brcmf_pub *drvr = cfg->pub;
2330 	s32 qdbm = 0;
2331 	s32 err;
2332 
2333 	brcmf_dbg(TRACE, "Enter\n");
2334 	if (!check_vif_up(vif))
2335 		return -EIO;
2336 
2337 	err = brcmf_fil_iovar_int_get(vif->ifp, "qtxpower", &qdbm);
2338 	if (err) {
2339 		bphy_err(drvr, "error (%d)\n", err);
2340 		goto done;
2341 	}
2342 	*dbm = (qdbm & ~WL_TXPWR_OVERRIDE) / 4;
2343 
2344 done:
2345 	brcmf_dbg(TRACE, "Exit (0x%x %d)\n", qdbm, *dbm);
2346 	return err;
2347 }
2348 
2349 static s32
2350 brcmf_cfg80211_config_default_key(struct wiphy *wiphy, struct net_device *ndev,
2351 				  u8 key_idx, bool unicast, bool multicast)
2352 {
2353 	struct brcmf_if *ifp = netdev_priv(ndev);
2354 	struct brcmf_pub *drvr = ifp->drvr;
2355 	u32 index;
2356 	u32 wsec;
2357 	s32 err = 0;
2358 
2359 	brcmf_dbg(TRACE, "Enter\n");
2360 	brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2361 	if (!check_vif_up(ifp->vif))
2362 		return -EIO;
2363 
2364 	err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2365 	if (err) {
2366 		bphy_err(drvr, "WLC_GET_WSEC error (%d)\n", err);
2367 		goto done;
2368 	}
2369 
2370 	if (wsec & WEP_ENABLED) {
2371 		/* Just select a new current key */
2372 		index = key_idx;
2373 		err = brcmf_fil_cmd_int_set(ifp,
2374 					    BRCMF_C_SET_KEY_PRIMARY, index);
2375 		if (err)
2376 			bphy_err(drvr, "error (%d)\n", err);
2377 	}
2378 done:
2379 	brcmf_dbg(TRACE, "Exit\n");
2380 	return err;
2381 }
2382 
2383 static s32
2384 brcmf_cfg80211_del_key(struct wiphy *wiphy, struct net_device *ndev,
2385 		       u8 key_idx, bool pairwise, const u8 *mac_addr)
2386 {
2387 	struct brcmf_if *ifp = netdev_priv(ndev);
2388 	struct brcmf_wsec_key *key;
2389 	s32 err;
2390 
2391 	brcmf_dbg(TRACE, "Enter\n");
2392 	brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2393 
2394 	if (!check_vif_up(ifp->vif))
2395 		return -EIO;
2396 
2397 	if (key_idx >= BRCMF_MAX_DEFAULT_KEYS) {
2398 		/* we ignore this key index in this case */
2399 		return -EINVAL;
2400 	}
2401 
2402 	key = &ifp->vif->profile.key[key_idx];
2403 
2404 	if (key->algo == CRYPTO_ALGO_OFF) {
2405 		brcmf_dbg(CONN, "Ignore clearing of (never configured) key\n");
2406 		return -EINVAL;
2407 	}
2408 
2409 	memset(key, 0, sizeof(*key));
2410 	key->index = (u32)key_idx;
2411 	key->flags = BRCMF_PRIMARY_KEY;
2412 
2413 	/* Clear the key/index */
2414 	err = send_key_to_dongle(ifp, key);
2415 
2416 	brcmf_dbg(TRACE, "Exit\n");
2417 	return err;
2418 }
2419 
2420 static s32
2421 brcmf_cfg80211_add_key(struct wiphy *wiphy, struct net_device *ndev,
2422 		       u8 key_idx, bool pairwise, const u8 *mac_addr,
2423 		       struct key_params *params)
2424 {
2425 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2426 	struct brcmf_if *ifp = netdev_priv(ndev);
2427 	struct brcmf_pub *drvr = cfg->pub;
2428 	struct brcmf_wsec_key *key;
2429 	s32 val;
2430 	s32 wsec;
2431 	s32 err;
2432 	u8 keybuf[8];
2433 	bool ext_key;
2434 
2435 	brcmf_dbg(TRACE, "Enter\n");
2436 	brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2437 	if (!check_vif_up(ifp->vif))
2438 		return -EIO;
2439 
2440 	if (key_idx >= BRCMF_MAX_DEFAULT_KEYS) {
2441 		/* we ignore this key index in this case */
2442 		bphy_err(drvr, "invalid key index (%d)\n", key_idx);
2443 		return -EINVAL;
2444 	}
2445 
2446 	if (params->key_len == 0)
2447 		return brcmf_cfg80211_del_key(wiphy, ndev, key_idx, pairwise,
2448 					      mac_addr);
2449 
2450 	if (params->key_len > sizeof(key->data)) {
2451 		bphy_err(drvr, "Too long key length (%u)\n", params->key_len);
2452 		return -EINVAL;
2453 	}
2454 
2455 	ext_key = false;
2456 	if (mac_addr && (params->cipher != WLAN_CIPHER_SUITE_WEP40) &&
2457 	    (params->cipher != WLAN_CIPHER_SUITE_WEP104)) {
2458 		brcmf_dbg(TRACE, "Ext key, mac %pM", mac_addr);
2459 		ext_key = true;
2460 	}
2461 
2462 	key = &ifp->vif->profile.key[key_idx];
2463 	memset(key, 0, sizeof(*key));
2464 	if ((ext_key) && (!is_multicast_ether_addr(mac_addr)))
2465 		memcpy((char *)&key->ea, (void *)mac_addr, ETH_ALEN);
2466 	key->len = params->key_len;
2467 	key->index = key_idx;
2468 	memcpy(key->data, params->key, key->len);
2469 	if (!ext_key)
2470 		key->flags = BRCMF_PRIMARY_KEY;
2471 
2472 	if (params->seq && params->seq_len == 6) {
2473 		/* rx iv */
2474 		u8 *ivptr;
2475 
2476 		ivptr = (u8 *)params->seq;
2477 		key->rxiv.hi = (ivptr[5] << 24) | (ivptr[4] << 16) |
2478 			(ivptr[3] << 8) | ivptr[2];
2479 		key->rxiv.lo = (ivptr[1] << 8) | ivptr[0];
2480 		key->iv_initialized = true;
2481 	}
2482 
2483 	switch (params->cipher) {
2484 	case WLAN_CIPHER_SUITE_WEP40:
2485 		key->algo = CRYPTO_ALGO_WEP1;
2486 		val = WEP_ENABLED;
2487 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
2488 		break;
2489 	case WLAN_CIPHER_SUITE_WEP104:
2490 		key->algo = CRYPTO_ALGO_WEP128;
2491 		val = WEP_ENABLED;
2492 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
2493 		break;
2494 	case WLAN_CIPHER_SUITE_TKIP:
2495 		if (!brcmf_is_apmode(ifp->vif)) {
2496 			brcmf_dbg(CONN, "Swapping RX/TX MIC key\n");
2497 			memcpy(keybuf, &key->data[24], sizeof(keybuf));
2498 			memcpy(&key->data[24], &key->data[16], sizeof(keybuf));
2499 			memcpy(&key->data[16], keybuf, sizeof(keybuf));
2500 		}
2501 		key->algo = CRYPTO_ALGO_TKIP;
2502 		val = TKIP_ENABLED;
2503 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
2504 		break;
2505 	case WLAN_CIPHER_SUITE_AES_CMAC:
2506 		key->algo = CRYPTO_ALGO_AES_CCM;
2507 		val = AES_ENABLED;
2508 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
2509 		break;
2510 	case WLAN_CIPHER_SUITE_CCMP:
2511 		key->algo = CRYPTO_ALGO_AES_CCM;
2512 		val = AES_ENABLED;
2513 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_CCMP\n");
2514 		break;
2515 	default:
2516 		bphy_err(drvr, "Invalid cipher (0x%x)\n", params->cipher);
2517 		err = -EINVAL;
2518 		goto done;
2519 	}
2520 
2521 	err = send_key_to_dongle(ifp, key);
2522 	if (ext_key || err)
2523 		goto done;
2524 
2525 	err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2526 	if (err) {
2527 		bphy_err(drvr, "get wsec error (%d)\n", err);
2528 		goto done;
2529 	}
2530 	wsec |= val;
2531 	err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
2532 	if (err) {
2533 		bphy_err(drvr, "set wsec error (%d)\n", err);
2534 		goto done;
2535 	}
2536 
2537 done:
2538 	brcmf_dbg(TRACE, "Exit\n");
2539 	return err;
2540 }
2541 
2542 static s32
2543 brcmf_cfg80211_get_key(struct wiphy *wiphy, struct net_device *ndev, u8 key_idx,
2544 		       bool pairwise, const u8 *mac_addr, void *cookie,
2545 		       void (*callback)(void *cookie,
2546 					struct key_params *params))
2547 {
2548 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2549 	struct key_params params;
2550 	struct brcmf_if *ifp = netdev_priv(ndev);
2551 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
2552 	struct brcmf_pub *drvr = cfg->pub;
2553 	struct brcmf_cfg80211_security *sec;
2554 	s32 wsec;
2555 	s32 err = 0;
2556 
2557 	brcmf_dbg(TRACE, "Enter\n");
2558 	brcmf_dbg(CONN, "key index (%d)\n", key_idx);
2559 	if (!check_vif_up(ifp->vif))
2560 		return -EIO;
2561 
2562 	memset(&params, 0, sizeof(params));
2563 
2564 	err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2565 	if (err) {
2566 		bphy_err(drvr, "WLC_GET_WSEC error (%d)\n", err);
2567 		/* Ignore this error, may happen during DISASSOC */
2568 		err = -EAGAIN;
2569 		goto done;
2570 	}
2571 	if (wsec & WEP_ENABLED) {
2572 		sec = &profile->sec;
2573 		if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP40) {
2574 			params.cipher = WLAN_CIPHER_SUITE_WEP40;
2575 			brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP40\n");
2576 		} else if (sec->cipher_pairwise & WLAN_CIPHER_SUITE_WEP104) {
2577 			params.cipher = WLAN_CIPHER_SUITE_WEP104;
2578 			brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_WEP104\n");
2579 		}
2580 	} else if (wsec & TKIP_ENABLED) {
2581 		params.cipher = WLAN_CIPHER_SUITE_TKIP;
2582 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_TKIP\n");
2583 	} else if (wsec & AES_ENABLED) {
2584 		params.cipher = WLAN_CIPHER_SUITE_AES_CMAC;
2585 		brcmf_dbg(CONN, "WLAN_CIPHER_SUITE_AES_CMAC\n");
2586 	} else  {
2587 		bphy_err(drvr, "Invalid algo (0x%x)\n", wsec);
2588 		err = -EINVAL;
2589 		goto done;
2590 	}
2591 	callback(cookie, &params);
2592 
2593 done:
2594 	brcmf_dbg(TRACE, "Exit\n");
2595 	return err;
2596 }
2597 
2598 static s32
2599 brcmf_cfg80211_config_default_mgmt_key(struct wiphy *wiphy,
2600 				       struct net_device *ndev, u8 key_idx)
2601 {
2602 	struct brcmf_if *ifp = netdev_priv(ndev);
2603 
2604 	brcmf_dbg(TRACE, "Enter key_idx %d\n", key_idx);
2605 
2606 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP))
2607 		return 0;
2608 
2609 	brcmf_dbg(INFO, "Not supported\n");
2610 
2611 	return -EOPNOTSUPP;
2612 }
2613 
2614 static void
2615 brcmf_cfg80211_reconfigure_wep(struct brcmf_if *ifp)
2616 {
2617 	struct brcmf_pub *drvr = ifp->drvr;
2618 	s32 err;
2619 	u8 key_idx;
2620 	struct brcmf_wsec_key *key;
2621 	s32 wsec;
2622 
2623 	for (key_idx = 0; key_idx < BRCMF_MAX_DEFAULT_KEYS; key_idx++) {
2624 		key = &ifp->vif->profile.key[key_idx];
2625 		if ((key->algo == CRYPTO_ALGO_WEP1) ||
2626 		    (key->algo == CRYPTO_ALGO_WEP128))
2627 			break;
2628 	}
2629 	if (key_idx == BRCMF_MAX_DEFAULT_KEYS)
2630 		return;
2631 
2632 	err = send_key_to_dongle(ifp, key);
2633 	if (err) {
2634 		bphy_err(drvr, "Setting WEP key failed (%d)\n", err);
2635 		return;
2636 	}
2637 	err = brcmf_fil_bsscfg_int_get(ifp, "wsec", &wsec);
2638 	if (err) {
2639 		bphy_err(drvr, "get wsec error (%d)\n", err);
2640 		return;
2641 	}
2642 	wsec |= WEP_ENABLED;
2643 	err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
2644 	if (err)
2645 		bphy_err(drvr, "set wsec error (%d)\n", err);
2646 }
2647 
2648 static void brcmf_convert_sta_flags(u32 fw_sta_flags, struct station_info *si)
2649 {
2650 	struct nl80211_sta_flag_update *sfu;
2651 
2652 	brcmf_dbg(TRACE, "flags %08x\n", fw_sta_flags);
2653 	si->filled |= BIT_ULL(NL80211_STA_INFO_STA_FLAGS);
2654 	sfu = &si->sta_flags;
2655 	sfu->mask = BIT(NL80211_STA_FLAG_WME) |
2656 		    BIT(NL80211_STA_FLAG_AUTHENTICATED) |
2657 		    BIT(NL80211_STA_FLAG_ASSOCIATED) |
2658 		    BIT(NL80211_STA_FLAG_AUTHORIZED);
2659 	if (fw_sta_flags & BRCMF_STA_WME)
2660 		sfu->set |= BIT(NL80211_STA_FLAG_WME);
2661 	if (fw_sta_flags & BRCMF_STA_AUTHE)
2662 		sfu->set |= BIT(NL80211_STA_FLAG_AUTHENTICATED);
2663 	if (fw_sta_flags & BRCMF_STA_ASSOC)
2664 		sfu->set |= BIT(NL80211_STA_FLAG_ASSOCIATED);
2665 	if (fw_sta_flags & BRCMF_STA_AUTHO)
2666 		sfu->set |= BIT(NL80211_STA_FLAG_AUTHORIZED);
2667 }
2668 
2669 static void brcmf_fill_bss_param(struct brcmf_if *ifp, struct station_info *si)
2670 {
2671 	struct brcmf_pub *drvr = ifp->drvr;
2672 	struct {
2673 		__le32 len;
2674 		struct brcmf_bss_info_le bss_le;
2675 	} *buf;
2676 	u16 capability;
2677 	int err;
2678 
2679 	buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
2680 	if (!buf)
2681 		return;
2682 
2683 	buf->len = cpu_to_le32(WL_BSS_INFO_MAX);
2684 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO, buf,
2685 				     WL_BSS_INFO_MAX);
2686 	if (err) {
2687 		bphy_err(drvr, "Failed to get bss info (%d)\n", err);
2688 		goto out_kfree;
2689 	}
2690 	si->filled |= BIT_ULL(NL80211_STA_INFO_BSS_PARAM);
2691 	si->bss_param.beacon_interval = le16_to_cpu(buf->bss_le.beacon_period);
2692 	si->bss_param.dtim_period = buf->bss_le.dtim_period;
2693 	capability = le16_to_cpu(buf->bss_le.capability);
2694 	if (capability & IEEE80211_HT_STBC_PARAM_DUAL_CTS_PROT)
2695 		si->bss_param.flags |= BSS_PARAM_FLAGS_CTS_PROT;
2696 	if (capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2697 		si->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_PREAMBLE;
2698 	if (capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2699 		si->bss_param.flags |= BSS_PARAM_FLAGS_SHORT_SLOT_TIME;
2700 
2701 out_kfree:
2702 	kfree(buf);
2703 }
2704 
2705 static s32
2706 brcmf_cfg80211_get_station_ibss(struct brcmf_if *ifp,
2707 				struct station_info *sinfo)
2708 {
2709 	struct brcmf_pub *drvr = ifp->drvr;
2710 	struct brcmf_scb_val_le scbval;
2711 	struct brcmf_pktcnt_le pktcnt;
2712 	s32 err;
2713 	u32 rate;
2714 	u32 rssi;
2715 
2716 	/* Get the current tx rate */
2717 	err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_RATE, &rate);
2718 	if (err < 0) {
2719 		bphy_err(drvr, "BRCMF_C_GET_RATE error (%d)\n", err);
2720 		return err;
2721 	}
2722 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2723 	sinfo->txrate.legacy = rate * 5;
2724 
2725 	memset(&scbval, 0, sizeof(scbval));
2726 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_RSSI, &scbval,
2727 				     sizeof(scbval));
2728 	if (err) {
2729 		bphy_err(drvr, "BRCMF_C_GET_RSSI error (%d)\n", err);
2730 		return err;
2731 	}
2732 	rssi = le32_to_cpu(scbval.val);
2733 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2734 	sinfo->signal = rssi;
2735 
2736 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_GET_PKTCNTS, &pktcnt,
2737 				     sizeof(pktcnt));
2738 	if (err) {
2739 		bphy_err(drvr, "BRCMF_C_GET_GET_PKTCNTS error (%d)\n", err);
2740 		return err;
2741 	}
2742 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS) |
2743 			 BIT_ULL(NL80211_STA_INFO_RX_DROP_MISC) |
2744 			 BIT_ULL(NL80211_STA_INFO_TX_PACKETS) |
2745 			 BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2746 	sinfo->rx_packets = le32_to_cpu(pktcnt.rx_good_pkt);
2747 	sinfo->rx_dropped_misc = le32_to_cpu(pktcnt.rx_bad_pkt);
2748 	sinfo->tx_packets = le32_to_cpu(pktcnt.tx_good_pkt);
2749 	sinfo->tx_failed  = le32_to_cpu(pktcnt.tx_bad_pkt);
2750 
2751 	return 0;
2752 }
2753 
2754 static s32
2755 brcmf_cfg80211_get_station(struct wiphy *wiphy, struct net_device *ndev,
2756 			   const u8 *mac, struct station_info *sinfo)
2757 {
2758 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2759 	struct brcmf_if *ifp = netdev_priv(ndev);
2760 	struct brcmf_pub *drvr = cfg->pub;
2761 	struct brcmf_scb_val_le scb_val;
2762 	s32 err = 0;
2763 	struct brcmf_sta_info_le sta_info_le;
2764 	u32 sta_flags;
2765 	u32 is_tdls_peer;
2766 	s32 total_rssi;
2767 	s32 count_rssi;
2768 	int rssi;
2769 	u32 i;
2770 
2771 	brcmf_dbg(TRACE, "Enter, MAC %pM\n", mac);
2772 	if (!check_vif_up(ifp->vif))
2773 		return -EIO;
2774 
2775 	if (brcmf_is_ibssmode(ifp->vif))
2776 		return brcmf_cfg80211_get_station_ibss(ifp, sinfo);
2777 
2778 	memset(&sta_info_le, 0, sizeof(sta_info_le));
2779 	memcpy(&sta_info_le, mac, ETH_ALEN);
2780 	err = brcmf_fil_iovar_data_get(ifp, "tdls_sta_info",
2781 				       &sta_info_le,
2782 				       sizeof(sta_info_le));
2783 	is_tdls_peer = !err;
2784 	if (err) {
2785 		err = brcmf_fil_iovar_data_get(ifp, "sta_info",
2786 					       &sta_info_le,
2787 					       sizeof(sta_info_le));
2788 		if (err < 0) {
2789 			bphy_err(drvr, "GET STA INFO failed, %d\n", err);
2790 			goto done;
2791 		}
2792 	}
2793 	brcmf_dbg(TRACE, "version %d\n", le16_to_cpu(sta_info_le.ver));
2794 	sinfo->filled = BIT_ULL(NL80211_STA_INFO_INACTIVE_TIME);
2795 	sinfo->inactive_time = le32_to_cpu(sta_info_le.idle) * 1000;
2796 	sta_flags = le32_to_cpu(sta_info_le.flags);
2797 	brcmf_convert_sta_flags(sta_flags, sinfo);
2798 	sinfo->sta_flags.mask |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2799 	if (is_tdls_peer)
2800 		sinfo->sta_flags.set |= BIT(NL80211_STA_FLAG_TDLS_PEER);
2801 	else
2802 		sinfo->sta_flags.set &= ~BIT(NL80211_STA_FLAG_TDLS_PEER);
2803 	if (sta_flags & BRCMF_STA_ASSOC) {
2804 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CONNECTED_TIME);
2805 		sinfo->connected_time = le32_to_cpu(sta_info_le.in);
2806 		brcmf_fill_bss_param(ifp, sinfo);
2807 	}
2808 	if (sta_flags & BRCMF_STA_SCBSTATS) {
2809 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
2810 		sinfo->tx_failed = le32_to_cpu(sta_info_le.tx_failures);
2811 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_PACKETS);
2812 		sinfo->tx_packets = le32_to_cpu(sta_info_le.tx_pkts);
2813 		sinfo->tx_packets += le32_to_cpu(sta_info_le.tx_mcast_pkts);
2814 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_PACKETS);
2815 		sinfo->rx_packets = le32_to_cpu(sta_info_le.rx_ucast_pkts);
2816 		sinfo->rx_packets += le32_to_cpu(sta_info_le.rx_mcast_pkts);
2817 		if (sinfo->tx_packets) {
2818 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2819 			sinfo->txrate.legacy =
2820 				le32_to_cpu(sta_info_le.tx_rate) / 100;
2821 		}
2822 		if (sinfo->rx_packets) {
2823 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
2824 			sinfo->rxrate.legacy =
2825 				le32_to_cpu(sta_info_le.rx_rate) / 100;
2826 		}
2827 		if (le16_to_cpu(sta_info_le.ver) >= 4) {
2828 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BYTES);
2829 			sinfo->tx_bytes = le64_to_cpu(sta_info_le.tx_tot_bytes);
2830 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BYTES);
2831 			sinfo->rx_bytes = le64_to_cpu(sta_info_le.rx_tot_bytes);
2832 		}
2833 		total_rssi = 0;
2834 		count_rssi = 0;
2835 		for (i = 0; i < BRCMF_ANT_MAX; i++) {
2836 			if (sta_info_le.rssi[i]) {
2837 				sinfo->chain_signal_avg[count_rssi] =
2838 					sta_info_le.rssi[i];
2839 				sinfo->chain_signal[count_rssi] =
2840 					sta_info_le.rssi[i];
2841 				total_rssi += sta_info_le.rssi[i];
2842 				count_rssi++;
2843 			}
2844 		}
2845 		if (count_rssi) {
2846 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
2847 			sinfo->chains = count_rssi;
2848 
2849 			sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2850 			total_rssi /= count_rssi;
2851 			sinfo->signal = total_rssi;
2852 		} else if (test_bit(BRCMF_VIF_STATUS_CONNECTED,
2853 			&ifp->vif->sme_state)) {
2854 			memset(&scb_val, 0, sizeof(scb_val));
2855 			err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_RSSI,
2856 						     &scb_val, sizeof(scb_val));
2857 			if (err) {
2858 				bphy_err(drvr, "Could not get rssi (%d)\n",
2859 					 err);
2860 				goto done;
2861 			} else {
2862 				rssi = le32_to_cpu(scb_val.val);
2863 				sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL);
2864 				sinfo->signal = rssi;
2865 				brcmf_dbg(CONN, "RSSI %d dBm\n", rssi);
2866 			}
2867 		}
2868 	}
2869 done:
2870 	brcmf_dbg(TRACE, "Exit\n");
2871 	return err;
2872 }
2873 
2874 static int
2875 brcmf_cfg80211_dump_station(struct wiphy *wiphy, struct net_device *ndev,
2876 			    int idx, u8 *mac, struct station_info *sinfo)
2877 {
2878 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2879 	struct brcmf_if *ifp = netdev_priv(ndev);
2880 	struct brcmf_pub *drvr = cfg->pub;
2881 	s32 err;
2882 
2883 	brcmf_dbg(TRACE, "Enter, idx %d\n", idx);
2884 
2885 	if (idx == 0) {
2886 		cfg->assoclist.count = cpu_to_le32(BRCMF_MAX_ASSOCLIST);
2887 		err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_ASSOCLIST,
2888 					     &cfg->assoclist,
2889 					     sizeof(cfg->assoclist));
2890 		if (err) {
2891 			bphy_err(drvr, "BRCMF_C_GET_ASSOCLIST unsupported, err=%d\n",
2892 				 err);
2893 			cfg->assoclist.count = 0;
2894 			return -EOPNOTSUPP;
2895 		}
2896 	}
2897 	if (idx < le32_to_cpu(cfg->assoclist.count)) {
2898 		memcpy(mac, cfg->assoclist.mac[idx], ETH_ALEN);
2899 		return brcmf_cfg80211_get_station(wiphy, ndev, mac, sinfo);
2900 	}
2901 	return -ENOENT;
2902 }
2903 
2904 static s32
2905 brcmf_cfg80211_set_power_mgmt(struct wiphy *wiphy, struct net_device *ndev,
2906 			   bool enabled, s32 timeout)
2907 {
2908 	s32 pm;
2909 	s32 err = 0;
2910 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
2911 	struct brcmf_if *ifp = netdev_priv(ndev);
2912 	struct brcmf_pub *drvr = cfg->pub;
2913 
2914 	brcmf_dbg(TRACE, "Enter\n");
2915 
2916 	/*
2917 	 * Powersave enable/disable request is coming from the
2918 	 * cfg80211 even before the interface is up. In that
2919 	 * scenario, driver will be storing the power save
2920 	 * preference in cfg struct to apply this to
2921 	 * FW later while initializing the dongle
2922 	 */
2923 	cfg->pwr_save = enabled;
2924 	if (!check_vif_up(ifp->vif)) {
2925 
2926 		brcmf_dbg(INFO, "Device is not ready, storing the value in cfg_info struct\n");
2927 		goto done;
2928 	}
2929 
2930 	pm = enabled ? PM_FAST : PM_OFF;
2931 	/* Do not enable the power save after assoc if it is a p2p interface */
2932 	if (ifp->vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT) {
2933 		brcmf_dbg(INFO, "Do not enable power save for P2P clients\n");
2934 		pm = PM_OFF;
2935 	}
2936 	brcmf_dbg(INFO, "power save %s\n", (pm ? "enabled" : "disabled"));
2937 
2938 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, pm);
2939 	if (err) {
2940 		if (err == -ENODEV)
2941 			bphy_err(drvr, "net_device is not ready yet\n");
2942 		else
2943 			bphy_err(drvr, "error (%d)\n", err);
2944 	}
2945 done:
2946 	brcmf_dbg(TRACE, "Exit\n");
2947 	return err;
2948 }
2949 
2950 static s32 brcmf_inform_single_bss(struct brcmf_cfg80211_info *cfg,
2951 				   struct brcmf_bss_info_le *bi)
2952 {
2953 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
2954 	struct brcmf_pub *drvr = cfg->pub;
2955 	struct cfg80211_bss *bss;
2956 	enum nl80211_band band;
2957 	struct brcmu_chan ch;
2958 	u16 channel;
2959 	u32 freq;
2960 	u16 notify_capability;
2961 	u16 notify_interval;
2962 	u8 *notify_ie;
2963 	size_t notify_ielen;
2964 	struct cfg80211_inform_bss bss_data = {};
2965 
2966 	if (le32_to_cpu(bi->length) > WL_BSS_INFO_MAX) {
2967 		bphy_err(drvr, "Bss info is larger than buffer. Discarding\n");
2968 		return -EINVAL;
2969 	}
2970 
2971 	if (!bi->ctl_ch) {
2972 		ch.chspec = le16_to_cpu(bi->chanspec);
2973 		cfg->d11inf.decchspec(&ch);
2974 		bi->ctl_ch = ch.control_ch_num;
2975 	}
2976 	channel = bi->ctl_ch;
2977 
2978 	if (channel <= CH_MAX_2G_CHANNEL)
2979 		band = NL80211_BAND_2GHZ;
2980 	else
2981 		band = NL80211_BAND_5GHZ;
2982 
2983 	freq = ieee80211_channel_to_frequency(channel, band);
2984 	bss_data.chan = ieee80211_get_channel(wiphy, freq);
2985 	bss_data.scan_width = NL80211_BSS_CHAN_WIDTH_20;
2986 	bss_data.boottime_ns = ktime_to_ns(ktime_get_boottime());
2987 
2988 	notify_capability = le16_to_cpu(bi->capability);
2989 	notify_interval = le16_to_cpu(bi->beacon_period);
2990 	notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
2991 	notify_ielen = le32_to_cpu(bi->ie_length);
2992 	bss_data.signal = (s16)le16_to_cpu(bi->RSSI) * 100;
2993 
2994 	brcmf_dbg(CONN, "bssid: %pM\n", bi->BSSID);
2995 	brcmf_dbg(CONN, "Channel: %d(%d)\n", channel, freq);
2996 	brcmf_dbg(CONN, "Capability: %X\n", notify_capability);
2997 	brcmf_dbg(CONN, "Beacon interval: %d\n", notify_interval);
2998 	brcmf_dbg(CONN, "Signal: %d\n", bss_data.signal);
2999 
3000 	bss = cfg80211_inform_bss_data(wiphy, &bss_data,
3001 				       CFG80211_BSS_FTYPE_UNKNOWN,
3002 				       (const u8 *)bi->BSSID,
3003 				       0, notify_capability,
3004 				       notify_interval, notify_ie,
3005 				       notify_ielen, GFP_KERNEL);
3006 
3007 	if (!bss)
3008 		return -ENOMEM;
3009 
3010 	cfg80211_put_bss(wiphy, bss);
3011 
3012 	return 0;
3013 }
3014 
3015 static struct brcmf_bss_info_le *
3016 next_bss_le(struct brcmf_scan_results *list, struct brcmf_bss_info_le *bss)
3017 {
3018 	if (bss == NULL)
3019 		return list->bss_info_le;
3020 	return (struct brcmf_bss_info_le *)((unsigned long)bss +
3021 					    le32_to_cpu(bss->length));
3022 }
3023 
3024 static s32 brcmf_inform_bss(struct brcmf_cfg80211_info *cfg)
3025 {
3026 	struct brcmf_pub *drvr = cfg->pub;
3027 	struct brcmf_scan_results *bss_list;
3028 	struct brcmf_bss_info_le *bi = NULL;	/* must be initialized */
3029 	s32 err = 0;
3030 	int i;
3031 
3032 	bss_list = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
3033 	if (bss_list->count != 0 &&
3034 	    bss_list->version != BRCMF_BSS_INFO_VERSION) {
3035 		bphy_err(drvr, "Version %d != WL_BSS_INFO_VERSION\n",
3036 			 bss_list->version);
3037 		return -EOPNOTSUPP;
3038 	}
3039 	brcmf_dbg(SCAN, "scanned AP count (%d)\n", bss_list->count);
3040 	for (i = 0; i < bss_list->count; i++) {
3041 		bi = next_bss_le(bss_list, bi);
3042 		err = brcmf_inform_single_bss(cfg, bi);
3043 		if (err)
3044 			break;
3045 	}
3046 	return err;
3047 }
3048 
3049 static s32 brcmf_inform_ibss(struct brcmf_cfg80211_info *cfg,
3050 			     struct net_device *ndev, const u8 *bssid)
3051 {
3052 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
3053 	struct brcmf_pub *drvr = cfg->pub;
3054 	struct ieee80211_channel *notify_channel;
3055 	struct brcmf_bss_info_le *bi = NULL;
3056 	struct ieee80211_supported_band *band;
3057 	struct cfg80211_bss *bss;
3058 	struct brcmu_chan ch;
3059 	u8 *buf = NULL;
3060 	s32 err = 0;
3061 	u32 freq;
3062 	u16 notify_capability;
3063 	u16 notify_interval;
3064 	u8 *notify_ie;
3065 	size_t notify_ielen;
3066 	s32 notify_signal;
3067 
3068 	brcmf_dbg(TRACE, "Enter\n");
3069 
3070 	buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
3071 	if (buf == NULL) {
3072 		err = -ENOMEM;
3073 		goto CleanUp;
3074 	}
3075 
3076 	*(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
3077 
3078 	err = brcmf_fil_cmd_data_get(netdev_priv(ndev), BRCMF_C_GET_BSS_INFO,
3079 				     buf, WL_BSS_INFO_MAX);
3080 	if (err) {
3081 		bphy_err(drvr, "WLC_GET_BSS_INFO failed: %d\n", err);
3082 		goto CleanUp;
3083 	}
3084 
3085 	bi = (struct brcmf_bss_info_le *)(buf + 4);
3086 
3087 	ch.chspec = le16_to_cpu(bi->chanspec);
3088 	cfg->d11inf.decchspec(&ch);
3089 
3090 	if (ch.band == BRCMU_CHAN_BAND_2G)
3091 		band = wiphy->bands[NL80211_BAND_2GHZ];
3092 	else
3093 		band = wiphy->bands[NL80211_BAND_5GHZ];
3094 
3095 	freq = ieee80211_channel_to_frequency(ch.control_ch_num, band->band);
3096 	cfg->channel = freq;
3097 	notify_channel = ieee80211_get_channel(wiphy, freq);
3098 
3099 	notify_capability = le16_to_cpu(bi->capability);
3100 	notify_interval = le16_to_cpu(bi->beacon_period);
3101 	notify_ie = (u8 *)bi + le16_to_cpu(bi->ie_offset);
3102 	notify_ielen = le32_to_cpu(bi->ie_length);
3103 	notify_signal = (s16)le16_to_cpu(bi->RSSI) * 100;
3104 
3105 	brcmf_dbg(CONN, "channel: %d(%d)\n", ch.control_ch_num, freq);
3106 	brcmf_dbg(CONN, "capability: %X\n", notify_capability);
3107 	brcmf_dbg(CONN, "beacon interval: %d\n", notify_interval);
3108 	brcmf_dbg(CONN, "signal: %d\n", notify_signal);
3109 
3110 	bss = cfg80211_inform_bss(wiphy, notify_channel,
3111 				  CFG80211_BSS_FTYPE_UNKNOWN, bssid, 0,
3112 				  notify_capability, notify_interval,
3113 				  notify_ie, notify_ielen, notify_signal,
3114 				  GFP_KERNEL);
3115 
3116 	if (!bss) {
3117 		err = -ENOMEM;
3118 		goto CleanUp;
3119 	}
3120 
3121 	cfg80211_put_bss(wiphy, bss);
3122 
3123 CleanUp:
3124 
3125 	kfree(buf);
3126 
3127 	brcmf_dbg(TRACE, "Exit\n");
3128 
3129 	return err;
3130 }
3131 
3132 static s32 brcmf_update_bss_info(struct brcmf_cfg80211_info *cfg,
3133 				 struct brcmf_if *ifp)
3134 {
3135 	struct brcmf_pub *drvr = cfg->pub;
3136 	struct brcmf_bss_info_le *bi;
3137 	const struct brcmf_tlv *tim;
3138 	size_t ie_len;
3139 	u8 *ie;
3140 	s32 err = 0;
3141 
3142 	brcmf_dbg(TRACE, "Enter\n");
3143 	if (brcmf_is_ibssmode(ifp->vif))
3144 		return err;
3145 
3146 	*(__le32 *)cfg->extra_buf = cpu_to_le32(WL_EXTRA_BUF_MAX);
3147 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
3148 				     cfg->extra_buf, WL_EXTRA_BUF_MAX);
3149 	if (err) {
3150 		bphy_err(drvr, "Could not get bss info %d\n", err);
3151 		goto update_bss_info_out;
3152 	}
3153 
3154 	bi = (struct brcmf_bss_info_le *)(cfg->extra_buf + 4);
3155 	err = brcmf_inform_single_bss(cfg, bi);
3156 	if (err)
3157 		goto update_bss_info_out;
3158 
3159 	ie = ((u8 *)bi) + le16_to_cpu(bi->ie_offset);
3160 	ie_len = le32_to_cpu(bi->ie_length);
3161 
3162 	tim = brcmf_parse_tlvs(ie, ie_len, WLAN_EID_TIM);
3163 	if (!tim) {
3164 		/*
3165 		* active scan was done so we could not get dtim
3166 		* information out of probe response.
3167 		* so we speficially query dtim information to dongle.
3168 		*/
3169 		u32 var;
3170 		err = brcmf_fil_iovar_int_get(ifp, "dtim_assoc", &var);
3171 		if (err) {
3172 			bphy_err(drvr, "wl dtim_assoc failed (%d)\n", err);
3173 			goto update_bss_info_out;
3174 		}
3175 	}
3176 
3177 update_bss_info_out:
3178 	brcmf_dbg(TRACE, "Exit");
3179 	return err;
3180 }
3181 
3182 void brcmf_abort_scanning(struct brcmf_cfg80211_info *cfg)
3183 {
3184 	struct escan_info *escan = &cfg->escan_info;
3185 
3186 	set_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
3187 	if (cfg->int_escan_map || cfg->scan_request) {
3188 		escan->escan_state = WL_ESCAN_STATE_IDLE;
3189 		brcmf_notify_escan_complete(cfg, escan->ifp, true, true);
3190 	}
3191 	clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
3192 	clear_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status);
3193 }
3194 
3195 static void brcmf_cfg80211_escan_timeout_worker(struct work_struct *work)
3196 {
3197 	struct brcmf_cfg80211_info *cfg =
3198 			container_of(work, struct brcmf_cfg80211_info,
3199 				     escan_timeout_work);
3200 
3201 	brcmf_inform_bss(cfg);
3202 	brcmf_notify_escan_complete(cfg, cfg->escan_info.ifp, true, true);
3203 }
3204 
3205 static void brcmf_escan_timeout(struct timer_list *t)
3206 {
3207 	struct brcmf_cfg80211_info *cfg =
3208 			from_timer(cfg, t, escan_timeout);
3209 	struct brcmf_pub *drvr = cfg->pub;
3210 
3211 	if (cfg->int_escan_map || cfg->scan_request) {
3212 		bphy_err(drvr, "timer expired\n");
3213 		schedule_work(&cfg->escan_timeout_work);
3214 	}
3215 }
3216 
3217 static s32
3218 brcmf_compare_update_same_bss(struct brcmf_cfg80211_info *cfg,
3219 			      struct brcmf_bss_info_le *bss,
3220 			      struct brcmf_bss_info_le *bss_info_le)
3221 {
3222 	struct brcmu_chan ch_bss, ch_bss_info_le;
3223 
3224 	ch_bss.chspec = le16_to_cpu(bss->chanspec);
3225 	cfg->d11inf.decchspec(&ch_bss);
3226 	ch_bss_info_le.chspec = le16_to_cpu(bss_info_le->chanspec);
3227 	cfg->d11inf.decchspec(&ch_bss_info_le);
3228 
3229 	if (!memcmp(&bss_info_le->BSSID, &bss->BSSID, ETH_ALEN) &&
3230 		ch_bss.band == ch_bss_info_le.band &&
3231 		bss_info_le->SSID_len == bss->SSID_len &&
3232 		!memcmp(bss_info_le->SSID, bss->SSID, bss_info_le->SSID_len)) {
3233 		if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) ==
3234 			(bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL)) {
3235 			s16 bss_rssi = le16_to_cpu(bss->RSSI);
3236 			s16 bss_info_rssi = le16_to_cpu(bss_info_le->RSSI);
3237 
3238 			/* preserve max RSSI if the measurements are
3239 			* both on-channel or both off-channel
3240 			*/
3241 			if (bss_info_rssi > bss_rssi)
3242 				bss->RSSI = bss_info_le->RSSI;
3243 		} else if ((bss->flags & BRCMF_BSS_RSSI_ON_CHANNEL) &&
3244 			(bss_info_le->flags & BRCMF_BSS_RSSI_ON_CHANNEL) == 0) {
3245 			/* preserve the on-channel rssi measurement
3246 			* if the new measurement is off channel
3247 			*/
3248 			bss->RSSI = bss_info_le->RSSI;
3249 			bss->flags |= BRCMF_BSS_RSSI_ON_CHANNEL;
3250 		}
3251 		return 1;
3252 	}
3253 	return 0;
3254 }
3255 
3256 static s32
3257 brcmf_cfg80211_escan_handler(struct brcmf_if *ifp,
3258 			     const struct brcmf_event_msg *e, void *data)
3259 {
3260 	struct brcmf_pub *drvr = ifp->drvr;
3261 	struct brcmf_cfg80211_info *cfg = drvr->config;
3262 	s32 status;
3263 	struct brcmf_escan_result_le *escan_result_le;
3264 	u32 escan_buflen;
3265 	struct brcmf_bss_info_le *bss_info_le;
3266 	struct brcmf_bss_info_le *bss = NULL;
3267 	u32 bi_length;
3268 	struct brcmf_scan_results *list;
3269 	u32 i;
3270 	bool aborted;
3271 
3272 	status = e->status;
3273 
3274 	if (status == BRCMF_E_STATUS_ABORT)
3275 		goto exit;
3276 
3277 	if (!test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
3278 		bphy_err(drvr, "scan not ready, bsscfgidx=%d\n",
3279 			 ifp->bsscfgidx);
3280 		return -EPERM;
3281 	}
3282 
3283 	if (status == BRCMF_E_STATUS_PARTIAL) {
3284 		brcmf_dbg(SCAN, "ESCAN Partial result\n");
3285 		if (e->datalen < sizeof(*escan_result_le)) {
3286 			bphy_err(drvr, "invalid event data length\n");
3287 			goto exit;
3288 		}
3289 		escan_result_le = (struct brcmf_escan_result_le *) data;
3290 		if (!escan_result_le) {
3291 			bphy_err(drvr, "Invalid escan result (NULL pointer)\n");
3292 			goto exit;
3293 		}
3294 		escan_buflen = le32_to_cpu(escan_result_le->buflen);
3295 		if (escan_buflen > BRCMF_ESCAN_BUF_SIZE ||
3296 		    escan_buflen > e->datalen ||
3297 		    escan_buflen < sizeof(*escan_result_le)) {
3298 			bphy_err(drvr, "Invalid escan buffer length: %d\n",
3299 				 escan_buflen);
3300 			goto exit;
3301 		}
3302 		if (le16_to_cpu(escan_result_le->bss_count) != 1) {
3303 			bphy_err(drvr, "Invalid bss_count %d: ignoring\n",
3304 				 escan_result_le->bss_count);
3305 			goto exit;
3306 		}
3307 		bss_info_le = &escan_result_le->bss_info_le;
3308 
3309 		if (brcmf_p2p_scan_finding_common_channel(cfg, bss_info_le))
3310 			goto exit;
3311 
3312 		if (!cfg->int_escan_map && !cfg->scan_request) {
3313 			brcmf_dbg(SCAN, "result without cfg80211 request\n");
3314 			goto exit;
3315 		}
3316 
3317 		bi_length = le32_to_cpu(bss_info_le->length);
3318 		if (bi_length != escan_buflen -	WL_ESCAN_RESULTS_FIXED_SIZE) {
3319 			bphy_err(drvr, "Ignoring invalid bss_info length: %d\n",
3320 				 bi_length);
3321 			goto exit;
3322 		}
3323 
3324 		if (!(cfg_to_wiphy(cfg)->interface_modes &
3325 					BIT(NL80211_IFTYPE_ADHOC))) {
3326 			if (le16_to_cpu(bss_info_le->capability) &
3327 						WLAN_CAPABILITY_IBSS) {
3328 				bphy_err(drvr, "Ignoring IBSS result\n");
3329 				goto exit;
3330 			}
3331 		}
3332 
3333 		list = (struct brcmf_scan_results *)
3334 				cfg->escan_info.escan_buf;
3335 		if (bi_length > BRCMF_ESCAN_BUF_SIZE - list->buflen) {
3336 			bphy_err(drvr, "Buffer is too small: ignoring\n");
3337 			goto exit;
3338 		}
3339 
3340 		for (i = 0; i < list->count; i++) {
3341 			bss = bss ? (struct brcmf_bss_info_le *)
3342 				((unsigned char *)bss +
3343 				le32_to_cpu(bss->length)) : list->bss_info_le;
3344 			if (brcmf_compare_update_same_bss(cfg, bss,
3345 							  bss_info_le))
3346 				goto exit;
3347 		}
3348 		memcpy(&cfg->escan_info.escan_buf[list->buflen], bss_info_le,
3349 		       bi_length);
3350 		list->version = le32_to_cpu(bss_info_le->version);
3351 		list->buflen += bi_length;
3352 		list->count++;
3353 	} else {
3354 		cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
3355 		if (brcmf_p2p_scan_finding_common_channel(cfg, NULL))
3356 			goto exit;
3357 		if (cfg->int_escan_map || cfg->scan_request) {
3358 			brcmf_inform_bss(cfg);
3359 			aborted = status != BRCMF_E_STATUS_SUCCESS;
3360 			brcmf_notify_escan_complete(cfg, ifp, aborted, false);
3361 		} else
3362 			brcmf_dbg(SCAN, "Ignored scan complete result 0x%x\n",
3363 				  status);
3364 	}
3365 exit:
3366 	return 0;
3367 }
3368 
3369 static void brcmf_init_escan(struct brcmf_cfg80211_info *cfg)
3370 {
3371 	brcmf_fweh_register(cfg->pub, BRCMF_E_ESCAN_RESULT,
3372 			    brcmf_cfg80211_escan_handler);
3373 	cfg->escan_info.escan_state = WL_ESCAN_STATE_IDLE;
3374 	/* Init scan_timeout timer */
3375 	timer_setup(&cfg->escan_timeout, brcmf_escan_timeout, 0);
3376 	INIT_WORK(&cfg->escan_timeout_work,
3377 		  brcmf_cfg80211_escan_timeout_worker);
3378 }
3379 
3380 static struct cfg80211_scan_request *
3381 brcmf_alloc_internal_escan_request(struct wiphy *wiphy, u32 n_netinfo) {
3382 	struct cfg80211_scan_request *req;
3383 	size_t req_size;
3384 
3385 	req_size = sizeof(*req) +
3386 		   n_netinfo * sizeof(req->channels[0]) +
3387 		   n_netinfo * sizeof(*req->ssids);
3388 
3389 	req = kzalloc(req_size, GFP_KERNEL);
3390 	if (req) {
3391 		req->wiphy = wiphy;
3392 		req->ssids = (void *)(&req->channels[0]) +
3393 			     n_netinfo * sizeof(req->channels[0]);
3394 	}
3395 	return req;
3396 }
3397 
3398 static int brcmf_internal_escan_add_info(struct cfg80211_scan_request *req,
3399 					 u8 *ssid, u8 ssid_len, u8 channel)
3400 {
3401 	struct ieee80211_channel *chan;
3402 	enum nl80211_band band;
3403 	int freq, i;
3404 
3405 	if (channel <= CH_MAX_2G_CHANNEL)
3406 		band = NL80211_BAND_2GHZ;
3407 	else
3408 		band = NL80211_BAND_5GHZ;
3409 
3410 	freq = ieee80211_channel_to_frequency(channel, band);
3411 	if (!freq)
3412 		return -EINVAL;
3413 
3414 	chan = ieee80211_get_channel(req->wiphy, freq);
3415 	if (!chan)
3416 		return -EINVAL;
3417 
3418 	for (i = 0; i < req->n_channels; i++) {
3419 		if (req->channels[i] == chan)
3420 			break;
3421 	}
3422 	if (i == req->n_channels)
3423 		req->channels[req->n_channels++] = chan;
3424 
3425 	for (i = 0; i < req->n_ssids; i++) {
3426 		if (req->ssids[i].ssid_len == ssid_len &&
3427 		    !memcmp(req->ssids[i].ssid, ssid, ssid_len))
3428 			break;
3429 	}
3430 	if (i == req->n_ssids) {
3431 		memcpy(req->ssids[req->n_ssids].ssid, ssid, ssid_len);
3432 		req->ssids[req->n_ssids++].ssid_len = ssid_len;
3433 	}
3434 	return 0;
3435 }
3436 
3437 static int brcmf_start_internal_escan(struct brcmf_if *ifp, u32 fwmap,
3438 				      struct cfg80211_scan_request *request)
3439 {
3440 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
3441 	int err;
3442 
3443 	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
3444 		if (cfg->int_escan_map)
3445 			brcmf_dbg(SCAN, "aborting internal scan: map=%u\n",
3446 				  cfg->int_escan_map);
3447 		/* Abort any on-going scan */
3448 		brcmf_abort_scanning(cfg);
3449 	}
3450 
3451 	brcmf_dbg(SCAN, "start internal scan: map=%u\n", fwmap);
3452 	set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
3453 	cfg->escan_info.run = brcmf_run_escan;
3454 	err = brcmf_do_escan(ifp, request);
3455 	if (err) {
3456 		clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
3457 		return err;
3458 	}
3459 	cfg->int_escan_map = fwmap;
3460 	return 0;
3461 }
3462 
3463 static struct brcmf_pno_net_info_le *
3464 brcmf_get_netinfo_array(struct brcmf_pno_scanresults_le *pfn_v1)
3465 {
3466 	struct brcmf_pno_scanresults_v2_le *pfn_v2;
3467 	struct brcmf_pno_net_info_le *netinfo;
3468 
3469 	switch (pfn_v1->version) {
3470 	default:
3471 		WARN_ON(1);
3472 		/* fall-thru */
3473 	case cpu_to_le32(1):
3474 		netinfo = (struct brcmf_pno_net_info_le *)(pfn_v1 + 1);
3475 		break;
3476 	case cpu_to_le32(2):
3477 		pfn_v2 = (struct brcmf_pno_scanresults_v2_le *)pfn_v1;
3478 		netinfo = (struct brcmf_pno_net_info_le *)(pfn_v2 + 1);
3479 		break;
3480 	}
3481 
3482 	return netinfo;
3483 }
3484 
3485 /* PFN result doesn't have all the info which are required by the supplicant
3486  * (For e.g IEs) Do a target Escan so that sched scan results are reported
3487  * via wl_inform_single_bss in the required format. Escan does require the
3488  * scan request in the form of cfg80211_scan_request. For timebeing, create
3489  * cfg80211_scan_request one out of the received PNO event.
3490  */
3491 static s32
3492 brcmf_notify_sched_scan_results(struct brcmf_if *ifp,
3493 				const struct brcmf_event_msg *e, void *data)
3494 {
3495 	struct brcmf_pub *drvr = ifp->drvr;
3496 	struct brcmf_cfg80211_info *cfg = drvr->config;
3497 	struct brcmf_pno_net_info_le *netinfo, *netinfo_start;
3498 	struct cfg80211_scan_request *request = NULL;
3499 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
3500 	int i, err = 0;
3501 	struct brcmf_pno_scanresults_le *pfn_result;
3502 	u32 bucket_map;
3503 	u32 result_count;
3504 	u32 status;
3505 	u32 datalen;
3506 
3507 	brcmf_dbg(SCAN, "Enter\n");
3508 
3509 	if (e->datalen < (sizeof(*pfn_result) + sizeof(*netinfo))) {
3510 		brcmf_dbg(SCAN, "Event data to small. Ignore\n");
3511 		return 0;
3512 	}
3513 
3514 	if (e->event_code == BRCMF_E_PFN_NET_LOST) {
3515 		brcmf_dbg(SCAN, "PFN NET LOST event. Do Nothing\n");
3516 		return 0;
3517 	}
3518 
3519 	pfn_result = (struct brcmf_pno_scanresults_le *)data;
3520 	result_count = le32_to_cpu(pfn_result->count);
3521 	status = le32_to_cpu(pfn_result->status);
3522 
3523 	/* PFN event is limited to fit 512 bytes so we may get
3524 	 * multiple NET_FOUND events. For now place a warning here.
3525 	 */
3526 	WARN_ON(status != BRCMF_PNO_SCAN_COMPLETE);
3527 	brcmf_dbg(SCAN, "PFN NET FOUND event. count: %d\n", result_count);
3528 	if (!result_count) {
3529 		bphy_err(drvr, "FALSE PNO Event. (pfn_count == 0)\n");
3530 		goto out_err;
3531 	}
3532 
3533 	netinfo_start = brcmf_get_netinfo_array(pfn_result);
3534 	datalen = e->datalen - ((void *)netinfo_start - (void *)pfn_result);
3535 	if (datalen < result_count * sizeof(*netinfo)) {
3536 		bphy_err(drvr, "insufficient event data\n");
3537 		goto out_err;
3538 	}
3539 
3540 	request = brcmf_alloc_internal_escan_request(wiphy,
3541 						     result_count);
3542 	if (!request) {
3543 		err = -ENOMEM;
3544 		goto out_err;
3545 	}
3546 
3547 	bucket_map = 0;
3548 	for (i = 0; i < result_count; i++) {
3549 		netinfo = &netinfo_start[i];
3550 
3551 		if (netinfo->SSID_len > IEEE80211_MAX_SSID_LEN)
3552 			netinfo->SSID_len = IEEE80211_MAX_SSID_LEN;
3553 		brcmf_dbg(SCAN, "SSID:%.32s Channel:%d\n",
3554 			  netinfo->SSID, netinfo->channel);
3555 		bucket_map |= brcmf_pno_get_bucket_map(cfg->pno, netinfo);
3556 		err = brcmf_internal_escan_add_info(request,
3557 						    netinfo->SSID,
3558 						    netinfo->SSID_len,
3559 						    netinfo->channel);
3560 		if (err)
3561 			goto out_err;
3562 	}
3563 
3564 	if (!bucket_map)
3565 		goto free_req;
3566 
3567 	err = brcmf_start_internal_escan(ifp, bucket_map, request);
3568 	if (!err)
3569 		goto free_req;
3570 
3571 out_err:
3572 	cfg80211_sched_scan_stopped(wiphy, 0);
3573 free_req:
3574 	kfree(request);
3575 	return err;
3576 }
3577 
3578 static int
3579 brcmf_cfg80211_sched_scan_start(struct wiphy *wiphy,
3580 				struct net_device *ndev,
3581 				struct cfg80211_sched_scan_request *req)
3582 {
3583 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3584 	struct brcmf_if *ifp = netdev_priv(ndev);
3585 	struct brcmf_pub *drvr = cfg->pub;
3586 
3587 	brcmf_dbg(SCAN, "Enter: n_match_sets=%d n_ssids=%d\n",
3588 		  req->n_match_sets, req->n_ssids);
3589 
3590 	if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
3591 		bphy_err(drvr, "Scanning suppressed: status=%lu\n",
3592 			 cfg->scan_status);
3593 		return -EAGAIN;
3594 	}
3595 
3596 	if (req->n_match_sets <= 0) {
3597 		brcmf_dbg(SCAN, "invalid number of matchsets specified: %d\n",
3598 			  req->n_match_sets);
3599 		return -EINVAL;
3600 	}
3601 
3602 	return brcmf_pno_start_sched_scan(ifp, req);
3603 }
3604 
3605 static int brcmf_cfg80211_sched_scan_stop(struct wiphy *wiphy,
3606 					  struct net_device *ndev, u64 reqid)
3607 {
3608 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3609 	struct brcmf_if *ifp = netdev_priv(ndev);
3610 
3611 	brcmf_dbg(SCAN, "enter\n");
3612 	brcmf_pno_stop_sched_scan(ifp, reqid);
3613 	if (cfg->int_escan_map)
3614 		brcmf_notify_escan_complete(cfg, ifp, true, true);
3615 	return 0;
3616 }
3617 
3618 static __always_inline void brcmf_delay(u32 ms)
3619 {
3620 	if (ms < 1000 / HZ) {
3621 		cond_resched();
3622 		mdelay(ms);
3623 	} else {
3624 		msleep(ms);
3625 	}
3626 }
3627 
3628 static s32 brcmf_config_wowl_pattern(struct brcmf_if *ifp, u8 cmd[4],
3629 				     u8 *pattern, u32 patternsize, u8 *mask,
3630 				     u32 packet_offset)
3631 {
3632 	struct brcmf_fil_wowl_pattern_le *filter;
3633 	u32 masksize;
3634 	u32 patternoffset;
3635 	u8 *buf;
3636 	u32 bufsize;
3637 	s32 ret;
3638 
3639 	masksize = (patternsize + 7) / 8;
3640 	patternoffset = sizeof(*filter) - sizeof(filter->cmd) + masksize;
3641 
3642 	bufsize = sizeof(*filter) + patternsize + masksize;
3643 	buf = kzalloc(bufsize, GFP_KERNEL);
3644 	if (!buf)
3645 		return -ENOMEM;
3646 	filter = (struct brcmf_fil_wowl_pattern_le *)buf;
3647 
3648 	memcpy(filter->cmd, cmd, 4);
3649 	filter->masksize = cpu_to_le32(masksize);
3650 	filter->offset = cpu_to_le32(packet_offset);
3651 	filter->patternoffset = cpu_to_le32(patternoffset);
3652 	filter->patternsize = cpu_to_le32(patternsize);
3653 	filter->type = cpu_to_le32(BRCMF_WOWL_PATTERN_TYPE_BITMAP);
3654 
3655 	if ((mask) && (masksize))
3656 		memcpy(buf + sizeof(*filter), mask, masksize);
3657 	if ((pattern) && (patternsize))
3658 		memcpy(buf + sizeof(*filter) + masksize, pattern, patternsize);
3659 
3660 	ret = brcmf_fil_iovar_data_set(ifp, "wowl_pattern", buf, bufsize);
3661 
3662 	kfree(buf);
3663 	return ret;
3664 }
3665 
3666 static s32
3667 brcmf_wowl_nd_results(struct brcmf_if *ifp, const struct brcmf_event_msg *e,
3668 		      void *data)
3669 {
3670 	struct brcmf_pub *drvr = ifp->drvr;
3671 	struct brcmf_cfg80211_info *cfg = drvr->config;
3672 	struct brcmf_pno_scanresults_le *pfn_result;
3673 	struct brcmf_pno_net_info_le *netinfo;
3674 
3675 	brcmf_dbg(SCAN, "Enter\n");
3676 
3677 	if (e->datalen < (sizeof(*pfn_result) + sizeof(*netinfo))) {
3678 		brcmf_dbg(SCAN, "Event data to small. Ignore\n");
3679 		return 0;
3680 	}
3681 
3682 	pfn_result = (struct brcmf_pno_scanresults_le *)data;
3683 
3684 	if (e->event_code == BRCMF_E_PFN_NET_LOST) {
3685 		brcmf_dbg(SCAN, "PFN NET LOST event. Ignore\n");
3686 		return 0;
3687 	}
3688 
3689 	if (le32_to_cpu(pfn_result->count) < 1) {
3690 		bphy_err(drvr, "Invalid result count, expected 1 (%d)\n",
3691 			 le32_to_cpu(pfn_result->count));
3692 		return -EINVAL;
3693 	}
3694 
3695 	netinfo = brcmf_get_netinfo_array(pfn_result);
3696 	if (netinfo->SSID_len > IEEE80211_MAX_SSID_LEN)
3697 		netinfo->SSID_len = IEEE80211_MAX_SSID_LEN;
3698 	memcpy(cfg->wowl.nd->ssid.ssid, netinfo->SSID, netinfo->SSID_len);
3699 	cfg->wowl.nd->ssid.ssid_len = netinfo->SSID_len;
3700 	cfg->wowl.nd->n_channels = 1;
3701 	cfg->wowl.nd->channels[0] =
3702 		ieee80211_channel_to_frequency(netinfo->channel,
3703 			netinfo->channel <= CH_MAX_2G_CHANNEL ?
3704 					NL80211_BAND_2GHZ : NL80211_BAND_5GHZ);
3705 	cfg->wowl.nd_info->n_matches = 1;
3706 	cfg->wowl.nd_info->matches[0] = cfg->wowl.nd;
3707 
3708 	/* Inform (the resume task) that the net detect information was recvd */
3709 	cfg->wowl.nd_data_completed = true;
3710 	wake_up(&cfg->wowl.nd_data_wait);
3711 
3712 	return 0;
3713 }
3714 
3715 #ifdef CONFIG_PM
3716 
3717 static void brcmf_report_wowl_wakeind(struct wiphy *wiphy, struct brcmf_if *ifp)
3718 {
3719 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3720 	struct brcmf_pub *drvr = cfg->pub;
3721 	struct brcmf_wowl_wakeind_le wake_ind_le;
3722 	struct cfg80211_wowlan_wakeup wakeup_data;
3723 	struct cfg80211_wowlan_wakeup *wakeup;
3724 	u32 wakeind;
3725 	s32 err;
3726 	int timeout;
3727 
3728 	err = brcmf_fil_iovar_data_get(ifp, "wowl_wakeind", &wake_ind_le,
3729 				       sizeof(wake_ind_le));
3730 	if (err) {
3731 		bphy_err(drvr, "Get wowl_wakeind failed, err = %d\n", err);
3732 		return;
3733 	}
3734 
3735 	wakeind = le32_to_cpu(wake_ind_le.ucode_wakeind);
3736 	if (wakeind & (BRCMF_WOWL_MAGIC | BRCMF_WOWL_DIS | BRCMF_WOWL_BCN |
3737 		       BRCMF_WOWL_RETR | BRCMF_WOWL_NET |
3738 		       BRCMF_WOWL_PFN_FOUND)) {
3739 		wakeup = &wakeup_data;
3740 		memset(&wakeup_data, 0, sizeof(wakeup_data));
3741 		wakeup_data.pattern_idx = -1;
3742 
3743 		if (wakeind & BRCMF_WOWL_MAGIC) {
3744 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_MAGIC\n");
3745 			wakeup_data.magic_pkt = true;
3746 		}
3747 		if (wakeind & BRCMF_WOWL_DIS) {
3748 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_DIS\n");
3749 			wakeup_data.disconnect = true;
3750 		}
3751 		if (wakeind & BRCMF_WOWL_BCN) {
3752 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_BCN\n");
3753 			wakeup_data.disconnect = true;
3754 		}
3755 		if (wakeind & BRCMF_WOWL_RETR) {
3756 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_RETR\n");
3757 			wakeup_data.disconnect = true;
3758 		}
3759 		if (wakeind & BRCMF_WOWL_NET) {
3760 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_NET\n");
3761 			/* For now always map to pattern 0, no API to get
3762 			 * correct information available at the moment.
3763 			 */
3764 			wakeup_data.pattern_idx = 0;
3765 		}
3766 		if (wakeind & BRCMF_WOWL_PFN_FOUND) {
3767 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_PFN_FOUND\n");
3768 			timeout = wait_event_timeout(cfg->wowl.nd_data_wait,
3769 				cfg->wowl.nd_data_completed,
3770 				BRCMF_ND_INFO_TIMEOUT);
3771 			if (!timeout)
3772 				bphy_err(drvr, "No result for wowl net detect\n");
3773 			else
3774 				wakeup_data.net_detect = cfg->wowl.nd_info;
3775 		}
3776 		if (wakeind & BRCMF_WOWL_GTK_FAILURE) {
3777 			brcmf_dbg(INFO, "WOWL Wake indicator: BRCMF_WOWL_GTK_FAILURE\n");
3778 			wakeup_data.gtk_rekey_failure = true;
3779 		}
3780 	} else {
3781 		wakeup = NULL;
3782 	}
3783 	cfg80211_report_wowlan_wakeup(&ifp->vif->wdev, wakeup, GFP_KERNEL);
3784 }
3785 
3786 #else
3787 
3788 static void brcmf_report_wowl_wakeind(struct wiphy *wiphy, struct brcmf_if *ifp)
3789 {
3790 }
3791 
3792 #endif /* CONFIG_PM */
3793 
3794 static s32 brcmf_cfg80211_resume(struct wiphy *wiphy)
3795 {
3796 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3797 	struct net_device *ndev = cfg_to_ndev(cfg);
3798 	struct brcmf_if *ifp = netdev_priv(ndev);
3799 
3800 	brcmf_dbg(TRACE, "Enter\n");
3801 
3802 	if (cfg->wowl.active) {
3803 		brcmf_report_wowl_wakeind(wiphy, ifp);
3804 		brcmf_fil_iovar_int_set(ifp, "wowl_clear", 0);
3805 		brcmf_config_wowl_pattern(ifp, "clr", NULL, 0, NULL, 0);
3806 		if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_ARP_ND))
3807 			brcmf_configure_arp_nd_offload(ifp, true);
3808 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM,
3809 				      cfg->wowl.pre_pmmode);
3810 		cfg->wowl.active = false;
3811 		if (cfg->wowl.nd_enabled) {
3812 			brcmf_cfg80211_sched_scan_stop(cfg->wiphy, ifp->ndev, 0);
3813 			brcmf_fweh_unregister(cfg->pub, BRCMF_E_PFN_NET_FOUND);
3814 			brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
3815 					    brcmf_notify_sched_scan_results);
3816 			cfg->wowl.nd_enabled = false;
3817 		}
3818 	}
3819 	return 0;
3820 }
3821 
3822 static void brcmf_configure_wowl(struct brcmf_cfg80211_info *cfg,
3823 				 struct brcmf_if *ifp,
3824 				 struct cfg80211_wowlan *wowl)
3825 {
3826 	u32 wowl_config;
3827 	struct brcmf_wowl_wakeind_le wowl_wakeind;
3828 	u32 i;
3829 
3830 	brcmf_dbg(TRACE, "Suspend, wowl config.\n");
3831 
3832 	if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_ARP_ND))
3833 		brcmf_configure_arp_nd_offload(ifp, false);
3834 	brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_PM, &cfg->wowl.pre_pmmode);
3835 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, PM_MAX);
3836 
3837 	wowl_config = 0;
3838 	if (wowl->disconnect)
3839 		wowl_config = BRCMF_WOWL_DIS | BRCMF_WOWL_BCN | BRCMF_WOWL_RETR;
3840 	if (wowl->magic_pkt)
3841 		wowl_config |= BRCMF_WOWL_MAGIC;
3842 	if ((wowl->patterns) && (wowl->n_patterns)) {
3843 		wowl_config |= BRCMF_WOWL_NET;
3844 		for (i = 0; i < wowl->n_patterns; i++) {
3845 			brcmf_config_wowl_pattern(ifp, "add",
3846 				(u8 *)wowl->patterns[i].pattern,
3847 				wowl->patterns[i].pattern_len,
3848 				(u8 *)wowl->patterns[i].mask,
3849 				wowl->patterns[i].pkt_offset);
3850 		}
3851 	}
3852 	if (wowl->nd_config) {
3853 		brcmf_cfg80211_sched_scan_start(cfg->wiphy, ifp->ndev,
3854 						wowl->nd_config);
3855 		wowl_config |= BRCMF_WOWL_PFN_FOUND;
3856 
3857 		cfg->wowl.nd_data_completed = false;
3858 		cfg->wowl.nd_enabled = true;
3859 		/* Now reroute the event for PFN to the wowl function. */
3860 		brcmf_fweh_unregister(cfg->pub, BRCMF_E_PFN_NET_FOUND);
3861 		brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
3862 				    brcmf_wowl_nd_results);
3863 	}
3864 	if (wowl->gtk_rekey_failure)
3865 		wowl_config |= BRCMF_WOWL_GTK_FAILURE;
3866 	if (!test_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state))
3867 		wowl_config |= BRCMF_WOWL_UNASSOC;
3868 
3869 	memcpy(&wowl_wakeind, "clear", 6);
3870 	brcmf_fil_iovar_data_set(ifp, "wowl_wakeind", &wowl_wakeind,
3871 				 sizeof(wowl_wakeind));
3872 	brcmf_fil_iovar_int_set(ifp, "wowl", wowl_config);
3873 	brcmf_fil_iovar_int_set(ifp, "wowl_activate", 1);
3874 	brcmf_bus_wowl_config(cfg->pub->bus_if, true);
3875 	cfg->wowl.active = true;
3876 }
3877 
3878 static s32 brcmf_cfg80211_suspend(struct wiphy *wiphy,
3879 				  struct cfg80211_wowlan *wowl)
3880 {
3881 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3882 	struct net_device *ndev = cfg_to_ndev(cfg);
3883 	struct brcmf_if *ifp = netdev_priv(ndev);
3884 	struct brcmf_cfg80211_vif *vif;
3885 
3886 	brcmf_dbg(TRACE, "Enter\n");
3887 
3888 	/* if the primary net_device is not READY there is nothing
3889 	 * we can do but pray resume goes smoothly.
3890 	 */
3891 	if (!check_vif_up(ifp->vif))
3892 		goto exit;
3893 
3894 	/* Stop scheduled scan */
3895 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_PNO))
3896 		brcmf_cfg80211_sched_scan_stop(wiphy, ndev, 0);
3897 
3898 	/* end any scanning */
3899 	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
3900 		brcmf_abort_scanning(cfg);
3901 
3902 	if (wowl == NULL) {
3903 		brcmf_bus_wowl_config(cfg->pub->bus_if, false);
3904 		list_for_each_entry(vif, &cfg->vif_list, list) {
3905 			if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state))
3906 				continue;
3907 			/* While going to suspend if associated with AP
3908 			 * disassociate from AP to save power while system is
3909 			 * in suspended state
3910 			 */
3911 			brcmf_link_down(vif, WLAN_REASON_UNSPECIFIED, true);
3912 			/* Make sure WPA_Supplicant receives all the event
3913 			 * generated due to DISASSOC call to the fw to keep
3914 			 * the state fw and WPA_Supplicant state consistent
3915 			 */
3916 			brcmf_delay(500);
3917 		}
3918 		/* Configure MPC */
3919 		brcmf_set_mpc(ifp, 1);
3920 
3921 	} else {
3922 		/* Configure WOWL paramaters */
3923 		brcmf_configure_wowl(cfg, ifp, wowl);
3924 	}
3925 
3926 exit:
3927 	brcmf_dbg(TRACE, "Exit\n");
3928 	/* clear any scanning activity */
3929 	cfg->scan_status = 0;
3930 	return 0;
3931 }
3932 
3933 static __used s32
3934 brcmf_update_pmklist(struct brcmf_cfg80211_info *cfg, struct brcmf_if *ifp)
3935 {
3936 	struct brcmf_pmk_list_le *pmk_list;
3937 	int i;
3938 	u32 npmk;
3939 	s32 err;
3940 
3941 	pmk_list = &cfg->pmk_list;
3942 	npmk = le32_to_cpu(pmk_list->npmk);
3943 
3944 	brcmf_dbg(CONN, "No of elements %d\n", npmk);
3945 	for (i = 0; i < npmk; i++)
3946 		brcmf_dbg(CONN, "PMK[%d]: %pM\n", i, &pmk_list->pmk[i].bssid);
3947 
3948 	err = brcmf_fil_iovar_data_set(ifp, "pmkid_info", pmk_list,
3949 				       sizeof(*pmk_list));
3950 
3951 	return err;
3952 }
3953 
3954 static s32
3955 brcmf_cfg80211_set_pmksa(struct wiphy *wiphy, struct net_device *ndev,
3956 			 struct cfg80211_pmksa *pmksa)
3957 {
3958 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
3959 	struct brcmf_if *ifp = netdev_priv(ndev);
3960 	struct brcmf_pmksa *pmk = &cfg->pmk_list.pmk[0];
3961 	struct brcmf_pub *drvr = cfg->pub;
3962 	s32 err;
3963 	u32 npmk, i;
3964 
3965 	brcmf_dbg(TRACE, "Enter\n");
3966 	if (!check_vif_up(ifp->vif))
3967 		return -EIO;
3968 
3969 	npmk = le32_to_cpu(cfg->pmk_list.npmk);
3970 	for (i = 0; i < npmk; i++)
3971 		if (!memcmp(pmksa->bssid, pmk[i].bssid, ETH_ALEN))
3972 			break;
3973 	if (i < BRCMF_MAXPMKID) {
3974 		memcpy(pmk[i].bssid, pmksa->bssid, ETH_ALEN);
3975 		memcpy(pmk[i].pmkid, pmksa->pmkid, WLAN_PMKID_LEN);
3976 		if (i == npmk) {
3977 			npmk++;
3978 			cfg->pmk_list.npmk = cpu_to_le32(npmk);
3979 		}
3980 	} else {
3981 		bphy_err(drvr, "Too many PMKSA entries cached %d\n", npmk);
3982 		return -EINVAL;
3983 	}
3984 
3985 	brcmf_dbg(CONN, "set_pmksa - PMK bssid: %pM =\n", pmk[npmk].bssid);
3986 	for (i = 0; i < WLAN_PMKID_LEN; i += 4)
3987 		brcmf_dbg(CONN, "%02x %02x %02x %02x\n", pmk[npmk].pmkid[i],
3988 			  pmk[npmk].pmkid[i + 1], pmk[npmk].pmkid[i + 2],
3989 			  pmk[npmk].pmkid[i + 3]);
3990 
3991 	err = brcmf_update_pmklist(cfg, ifp);
3992 
3993 	brcmf_dbg(TRACE, "Exit\n");
3994 	return err;
3995 }
3996 
3997 static s32
3998 brcmf_cfg80211_del_pmksa(struct wiphy *wiphy, struct net_device *ndev,
3999 			 struct cfg80211_pmksa *pmksa)
4000 {
4001 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4002 	struct brcmf_if *ifp = netdev_priv(ndev);
4003 	struct brcmf_pmksa *pmk = &cfg->pmk_list.pmk[0];
4004 	struct brcmf_pub *drvr = cfg->pub;
4005 	s32 err;
4006 	u32 npmk, i;
4007 
4008 	brcmf_dbg(TRACE, "Enter\n");
4009 	if (!check_vif_up(ifp->vif))
4010 		return -EIO;
4011 
4012 	brcmf_dbg(CONN, "del_pmksa - PMK bssid = %pM\n", pmksa->bssid);
4013 
4014 	npmk = le32_to_cpu(cfg->pmk_list.npmk);
4015 	for (i = 0; i < npmk; i++)
4016 		if (!memcmp(pmksa->bssid, pmk[i].bssid, ETH_ALEN))
4017 			break;
4018 
4019 	if ((npmk > 0) && (i < npmk)) {
4020 		for (; i < (npmk - 1); i++) {
4021 			memcpy(&pmk[i].bssid, &pmk[i + 1].bssid, ETH_ALEN);
4022 			memcpy(&pmk[i].pmkid, &pmk[i + 1].pmkid,
4023 			       WLAN_PMKID_LEN);
4024 		}
4025 		memset(&pmk[i], 0, sizeof(*pmk));
4026 		cfg->pmk_list.npmk = cpu_to_le32(npmk - 1);
4027 	} else {
4028 		bphy_err(drvr, "Cache entry not found\n");
4029 		return -EINVAL;
4030 	}
4031 
4032 	err = brcmf_update_pmklist(cfg, ifp);
4033 
4034 	brcmf_dbg(TRACE, "Exit\n");
4035 	return err;
4036 
4037 }
4038 
4039 static s32
4040 brcmf_cfg80211_flush_pmksa(struct wiphy *wiphy, struct net_device *ndev)
4041 {
4042 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4043 	struct brcmf_if *ifp = netdev_priv(ndev);
4044 	s32 err;
4045 
4046 	brcmf_dbg(TRACE, "Enter\n");
4047 	if (!check_vif_up(ifp->vif))
4048 		return -EIO;
4049 
4050 	memset(&cfg->pmk_list, 0, sizeof(cfg->pmk_list));
4051 	err = brcmf_update_pmklist(cfg, ifp);
4052 
4053 	brcmf_dbg(TRACE, "Exit\n");
4054 	return err;
4055 
4056 }
4057 
4058 static s32 brcmf_configure_opensecurity(struct brcmf_if *ifp)
4059 {
4060 	struct brcmf_pub *drvr = ifp->drvr;
4061 	s32 err;
4062 	s32 wpa_val;
4063 
4064 	/* set auth */
4065 	err = brcmf_fil_bsscfg_int_set(ifp, "auth", 0);
4066 	if (err < 0) {
4067 		bphy_err(drvr, "auth error %d\n", err);
4068 		return err;
4069 	}
4070 	/* set wsec */
4071 	err = brcmf_fil_bsscfg_int_set(ifp, "wsec", 0);
4072 	if (err < 0) {
4073 		bphy_err(drvr, "wsec error %d\n", err);
4074 		return err;
4075 	}
4076 	/* set upper-layer auth */
4077 	if (brcmf_is_ibssmode(ifp->vif))
4078 		wpa_val = WPA_AUTH_NONE;
4079 	else
4080 		wpa_val = WPA_AUTH_DISABLED;
4081 	err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_val);
4082 	if (err < 0) {
4083 		bphy_err(drvr, "wpa_auth error %d\n", err);
4084 		return err;
4085 	}
4086 
4087 	return 0;
4088 }
4089 
4090 static bool brcmf_valid_wpa_oui(u8 *oui, bool is_rsn_ie)
4091 {
4092 	if (is_rsn_ie)
4093 		return (memcmp(oui, RSN_OUI, TLV_OUI_LEN) == 0);
4094 
4095 	return (memcmp(oui, WPA_OUI, TLV_OUI_LEN) == 0);
4096 }
4097 
4098 static s32
4099 brcmf_configure_wpaie(struct brcmf_if *ifp,
4100 		      const struct brcmf_vs_tlv *wpa_ie,
4101 		      bool is_rsn_ie)
4102 {
4103 	struct brcmf_pub *drvr = ifp->drvr;
4104 	u32 auth = 0; /* d11 open authentication */
4105 	u16 count;
4106 	s32 err = 0;
4107 	s32 len;
4108 	u32 i;
4109 	u32 wsec;
4110 	u32 pval = 0;
4111 	u32 gval = 0;
4112 	u32 wpa_auth = 0;
4113 	u32 offset;
4114 	u8 *data;
4115 	u16 rsn_cap;
4116 	u32 wme_bss_disable;
4117 	u32 mfp;
4118 
4119 	brcmf_dbg(TRACE, "Enter\n");
4120 	if (wpa_ie == NULL)
4121 		goto exit;
4122 
4123 	len = wpa_ie->len + TLV_HDR_LEN;
4124 	data = (u8 *)wpa_ie;
4125 	offset = TLV_HDR_LEN;
4126 	if (!is_rsn_ie)
4127 		offset += VS_IE_FIXED_HDR_LEN;
4128 	else
4129 		offset += WPA_IE_VERSION_LEN;
4130 
4131 	/* check for multicast cipher suite */
4132 	if (offset + WPA_IE_MIN_OUI_LEN > len) {
4133 		err = -EINVAL;
4134 		bphy_err(drvr, "no multicast cipher suite\n");
4135 		goto exit;
4136 	}
4137 
4138 	if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
4139 		err = -EINVAL;
4140 		bphy_err(drvr, "ivalid OUI\n");
4141 		goto exit;
4142 	}
4143 	offset += TLV_OUI_LEN;
4144 
4145 	/* pick up multicast cipher */
4146 	switch (data[offset]) {
4147 	case WPA_CIPHER_NONE:
4148 		gval = 0;
4149 		break;
4150 	case WPA_CIPHER_WEP_40:
4151 	case WPA_CIPHER_WEP_104:
4152 		gval = WEP_ENABLED;
4153 		break;
4154 	case WPA_CIPHER_TKIP:
4155 		gval = TKIP_ENABLED;
4156 		break;
4157 	case WPA_CIPHER_AES_CCM:
4158 		gval = AES_ENABLED;
4159 		break;
4160 	default:
4161 		err = -EINVAL;
4162 		bphy_err(drvr, "Invalid multi cast cipher info\n");
4163 		goto exit;
4164 	}
4165 
4166 	offset++;
4167 	/* walk thru unicast cipher list and pick up what we recognize */
4168 	count = data[offset] + (data[offset + 1] << 8);
4169 	offset += WPA_IE_SUITE_COUNT_LEN;
4170 	/* Check for unicast suite(s) */
4171 	if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
4172 		err = -EINVAL;
4173 		bphy_err(drvr, "no unicast cipher suite\n");
4174 		goto exit;
4175 	}
4176 	for (i = 0; i < count; i++) {
4177 		if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
4178 			err = -EINVAL;
4179 			bphy_err(drvr, "ivalid OUI\n");
4180 			goto exit;
4181 		}
4182 		offset += TLV_OUI_LEN;
4183 		switch (data[offset]) {
4184 		case WPA_CIPHER_NONE:
4185 			break;
4186 		case WPA_CIPHER_WEP_40:
4187 		case WPA_CIPHER_WEP_104:
4188 			pval |= WEP_ENABLED;
4189 			break;
4190 		case WPA_CIPHER_TKIP:
4191 			pval |= TKIP_ENABLED;
4192 			break;
4193 		case WPA_CIPHER_AES_CCM:
4194 			pval |= AES_ENABLED;
4195 			break;
4196 		default:
4197 			bphy_err(drvr, "Invalid unicast security info\n");
4198 		}
4199 		offset++;
4200 	}
4201 	/* walk thru auth management suite list and pick up what we recognize */
4202 	count = data[offset] + (data[offset + 1] << 8);
4203 	offset += WPA_IE_SUITE_COUNT_LEN;
4204 	/* Check for auth key management suite(s) */
4205 	if (offset + (WPA_IE_MIN_OUI_LEN * count) > len) {
4206 		err = -EINVAL;
4207 		bphy_err(drvr, "no auth key mgmt suite\n");
4208 		goto exit;
4209 	}
4210 	for (i = 0; i < count; i++) {
4211 		if (!brcmf_valid_wpa_oui(&data[offset], is_rsn_ie)) {
4212 			err = -EINVAL;
4213 			bphy_err(drvr, "ivalid OUI\n");
4214 			goto exit;
4215 		}
4216 		offset += TLV_OUI_LEN;
4217 		switch (data[offset]) {
4218 		case RSN_AKM_NONE:
4219 			brcmf_dbg(TRACE, "RSN_AKM_NONE\n");
4220 			wpa_auth |= WPA_AUTH_NONE;
4221 			break;
4222 		case RSN_AKM_UNSPECIFIED:
4223 			brcmf_dbg(TRACE, "RSN_AKM_UNSPECIFIED\n");
4224 			is_rsn_ie ? (wpa_auth |= WPA2_AUTH_UNSPECIFIED) :
4225 				    (wpa_auth |= WPA_AUTH_UNSPECIFIED);
4226 			break;
4227 		case RSN_AKM_PSK:
4228 			brcmf_dbg(TRACE, "RSN_AKM_PSK\n");
4229 			is_rsn_ie ? (wpa_auth |= WPA2_AUTH_PSK) :
4230 				    (wpa_auth |= WPA_AUTH_PSK);
4231 			break;
4232 		case RSN_AKM_SHA256_PSK:
4233 			brcmf_dbg(TRACE, "RSN_AKM_MFP_PSK\n");
4234 			wpa_auth |= WPA2_AUTH_PSK_SHA256;
4235 			break;
4236 		case RSN_AKM_SHA256_1X:
4237 			brcmf_dbg(TRACE, "RSN_AKM_MFP_1X\n");
4238 			wpa_auth |= WPA2_AUTH_1X_SHA256;
4239 			break;
4240 		default:
4241 			bphy_err(drvr, "Invalid key mgmt info\n");
4242 		}
4243 		offset++;
4244 	}
4245 
4246 	mfp = BRCMF_MFP_NONE;
4247 	if (is_rsn_ie) {
4248 		wme_bss_disable = 1;
4249 		if ((offset + RSN_CAP_LEN) <= len) {
4250 			rsn_cap = data[offset] + (data[offset + 1] << 8);
4251 			if (rsn_cap & RSN_CAP_PTK_REPLAY_CNTR_MASK)
4252 				wme_bss_disable = 0;
4253 			if (rsn_cap & RSN_CAP_MFPR_MASK) {
4254 				brcmf_dbg(TRACE, "MFP Required\n");
4255 				mfp = BRCMF_MFP_REQUIRED;
4256 				/* Firmware only supports mfp required in
4257 				 * combination with WPA2_AUTH_PSK_SHA256 or
4258 				 * WPA2_AUTH_1X_SHA256.
4259 				 */
4260 				if (!(wpa_auth & (WPA2_AUTH_PSK_SHA256 |
4261 						  WPA2_AUTH_1X_SHA256))) {
4262 					err = -EINVAL;
4263 					goto exit;
4264 				}
4265 				/* Firmware has requirement that WPA2_AUTH_PSK/
4266 				 * WPA2_AUTH_UNSPECIFIED be set, if SHA256 OUI
4267 				 * is to be included in the rsn ie.
4268 				 */
4269 				if (wpa_auth & WPA2_AUTH_PSK_SHA256)
4270 					wpa_auth |= WPA2_AUTH_PSK;
4271 				else if (wpa_auth & WPA2_AUTH_1X_SHA256)
4272 					wpa_auth |= WPA2_AUTH_UNSPECIFIED;
4273 			} else if (rsn_cap & RSN_CAP_MFPC_MASK) {
4274 				brcmf_dbg(TRACE, "MFP Capable\n");
4275 				mfp = BRCMF_MFP_CAPABLE;
4276 			}
4277 		}
4278 		offset += RSN_CAP_LEN;
4279 		/* set wme_bss_disable to sync RSN Capabilities */
4280 		err = brcmf_fil_bsscfg_int_set(ifp, "wme_bss_disable",
4281 					       wme_bss_disable);
4282 		if (err < 0) {
4283 			bphy_err(drvr, "wme_bss_disable error %d\n", err);
4284 			goto exit;
4285 		}
4286 
4287 		/* Skip PMKID cnt as it is know to be 0 for AP. */
4288 		offset += RSN_PMKID_COUNT_LEN;
4289 
4290 		/* See if there is BIP wpa suite left for MFP */
4291 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP) &&
4292 		    ((offset + WPA_IE_MIN_OUI_LEN) <= len)) {
4293 			err = brcmf_fil_bsscfg_data_set(ifp, "bip",
4294 							&data[offset],
4295 							WPA_IE_MIN_OUI_LEN);
4296 			if (err < 0) {
4297 				bphy_err(drvr, "bip error %d\n", err);
4298 				goto exit;
4299 			}
4300 		}
4301 	}
4302 	/* FOR WPS , set SES_OW_ENABLED */
4303 	wsec = (pval | gval | SES_OW_ENABLED);
4304 
4305 	/* set auth */
4306 	err = brcmf_fil_bsscfg_int_set(ifp, "auth", auth);
4307 	if (err < 0) {
4308 		bphy_err(drvr, "auth error %d\n", err);
4309 		goto exit;
4310 	}
4311 	/* set wsec */
4312 	err = brcmf_fil_bsscfg_int_set(ifp, "wsec", wsec);
4313 	if (err < 0) {
4314 		bphy_err(drvr, "wsec error %d\n", err);
4315 		goto exit;
4316 	}
4317 	/* Configure MFP, this needs to go after wsec otherwise the wsec command
4318 	 * will overwrite the values set by MFP
4319 	 */
4320 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP)) {
4321 		err = brcmf_fil_bsscfg_int_set(ifp, "mfp", mfp);
4322 		if (err < 0) {
4323 			bphy_err(drvr, "mfp error %d\n", err);
4324 			goto exit;
4325 		}
4326 	}
4327 	/* set upper-layer auth */
4328 	err = brcmf_fil_bsscfg_int_set(ifp, "wpa_auth", wpa_auth);
4329 	if (err < 0) {
4330 		bphy_err(drvr, "wpa_auth error %d\n", err);
4331 		goto exit;
4332 	}
4333 
4334 exit:
4335 	return err;
4336 }
4337 
4338 static s32
4339 brcmf_parse_vndr_ies(const u8 *vndr_ie_buf, u32 vndr_ie_len,
4340 		     struct parsed_vndr_ies *vndr_ies)
4341 {
4342 	struct brcmf_vs_tlv *vndrie;
4343 	struct brcmf_tlv *ie;
4344 	struct parsed_vndr_ie_info *parsed_info;
4345 	s32 remaining_len;
4346 
4347 	remaining_len = (s32)vndr_ie_len;
4348 	memset(vndr_ies, 0, sizeof(*vndr_ies));
4349 
4350 	ie = (struct brcmf_tlv *)vndr_ie_buf;
4351 	while (ie) {
4352 		if (ie->id != WLAN_EID_VENDOR_SPECIFIC)
4353 			goto next;
4354 		vndrie = (struct brcmf_vs_tlv *)ie;
4355 		/* len should be bigger than OUI length + one */
4356 		if (vndrie->len < (VS_IE_FIXED_HDR_LEN - TLV_HDR_LEN + 1)) {
4357 			brcmf_err("invalid vndr ie. length is too small %d\n",
4358 				  vndrie->len);
4359 			goto next;
4360 		}
4361 		/* if wpa or wme ie, do not add ie */
4362 		if (!memcmp(vndrie->oui, (u8 *)WPA_OUI, TLV_OUI_LEN) &&
4363 		    ((vndrie->oui_type == WPA_OUI_TYPE) ||
4364 		    (vndrie->oui_type == WME_OUI_TYPE))) {
4365 			brcmf_dbg(TRACE, "Found WPA/WME oui. Do not add it\n");
4366 			goto next;
4367 		}
4368 
4369 		parsed_info = &vndr_ies->ie_info[vndr_ies->count];
4370 
4371 		/* save vndr ie information */
4372 		parsed_info->ie_ptr = (char *)vndrie;
4373 		parsed_info->ie_len = vndrie->len + TLV_HDR_LEN;
4374 		memcpy(&parsed_info->vndrie, vndrie, sizeof(*vndrie));
4375 
4376 		vndr_ies->count++;
4377 
4378 		brcmf_dbg(TRACE, "** OUI %3ph, type 0x%02x\n",
4379 			  parsed_info->vndrie.oui,
4380 			  parsed_info->vndrie.oui_type);
4381 
4382 		if (vndr_ies->count >= VNDR_IE_PARSE_LIMIT)
4383 			break;
4384 next:
4385 		remaining_len -= (ie->len + TLV_HDR_LEN);
4386 		if (remaining_len <= TLV_HDR_LEN)
4387 			ie = NULL;
4388 		else
4389 			ie = (struct brcmf_tlv *)(((u8 *)ie) + ie->len +
4390 				TLV_HDR_LEN);
4391 	}
4392 	return 0;
4393 }
4394 
4395 static u32
4396 brcmf_vndr_ie(u8 *iebuf, s32 pktflag, u8 *ie_ptr, u32 ie_len, s8 *add_del_cmd)
4397 {
4398 	strscpy(iebuf, add_del_cmd, VNDR_IE_CMD_LEN);
4399 
4400 	put_unaligned_le32(1, &iebuf[VNDR_IE_COUNT_OFFSET]);
4401 
4402 	put_unaligned_le32(pktflag, &iebuf[VNDR_IE_PKTFLAG_OFFSET]);
4403 
4404 	memcpy(&iebuf[VNDR_IE_VSIE_OFFSET], ie_ptr, ie_len);
4405 
4406 	return ie_len + VNDR_IE_HDR_SIZE;
4407 }
4408 
4409 s32 brcmf_vif_set_mgmt_ie(struct brcmf_cfg80211_vif *vif, s32 pktflag,
4410 			  const u8 *vndr_ie_buf, u32 vndr_ie_len)
4411 {
4412 	struct brcmf_pub *drvr;
4413 	struct brcmf_if *ifp;
4414 	struct vif_saved_ie *saved_ie;
4415 	s32 err = 0;
4416 	u8  *iovar_ie_buf;
4417 	u8  *curr_ie_buf;
4418 	u8  *mgmt_ie_buf = NULL;
4419 	int mgmt_ie_buf_len;
4420 	u32 *mgmt_ie_len;
4421 	u32 del_add_ie_buf_len = 0;
4422 	u32 total_ie_buf_len = 0;
4423 	u32 parsed_ie_buf_len = 0;
4424 	struct parsed_vndr_ies old_vndr_ies;
4425 	struct parsed_vndr_ies new_vndr_ies;
4426 	struct parsed_vndr_ie_info *vndrie_info;
4427 	s32 i;
4428 	u8 *ptr;
4429 	int remained_buf_len;
4430 
4431 	if (!vif)
4432 		return -ENODEV;
4433 	ifp = vif->ifp;
4434 	drvr = ifp->drvr;
4435 	saved_ie = &vif->saved_ie;
4436 
4437 	brcmf_dbg(TRACE, "bsscfgidx %d, pktflag : 0x%02X\n", ifp->bsscfgidx,
4438 		  pktflag);
4439 	iovar_ie_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
4440 	if (!iovar_ie_buf)
4441 		return -ENOMEM;
4442 	curr_ie_buf = iovar_ie_buf;
4443 	switch (pktflag) {
4444 	case BRCMF_VNDR_IE_PRBREQ_FLAG:
4445 		mgmt_ie_buf = saved_ie->probe_req_ie;
4446 		mgmt_ie_len = &saved_ie->probe_req_ie_len;
4447 		mgmt_ie_buf_len = sizeof(saved_ie->probe_req_ie);
4448 		break;
4449 	case BRCMF_VNDR_IE_PRBRSP_FLAG:
4450 		mgmt_ie_buf = saved_ie->probe_res_ie;
4451 		mgmt_ie_len = &saved_ie->probe_res_ie_len;
4452 		mgmt_ie_buf_len = sizeof(saved_ie->probe_res_ie);
4453 		break;
4454 	case BRCMF_VNDR_IE_BEACON_FLAG:
4455 		mgmt_ie_buf = saved_ie->beacon_ie;
4456 		mgmt_ie_len = &saved_ie->beacon_ie_len;
4457 		mgmt_ie_buf_len = sizeof(saved_ie->beacon_ie);
4458 		break;
4459 	case BRCMF_VNDR_IE_ASSOCREQ_FLAG:
4460 		mgmt_ie_buf = saved_ie->assoc_req_ie;
4461 		mgmt_ie_len = &saved_ie->assoc_req_ie_len;
4462 		mgmt_ie_buf_len = sizeof(saved_ie->assoc_req_ie);
4463 		break;
4464 	case BRCMF_VNDR_IE_ASSOCRSP_FLAG:
4465 		mgmt_ie_buf = saved_ie->assoc_res_ie;
4466 		mgmt_ie_len = &saved_ie->assoc_res_ie_len;
4467 		mgmt_ie_buf_len = sizeof(saved_ie->assoc_res_ie);
4468 		break;
4469 	default:
4470 		err = -EPERM;
4471 		bphy_err(drvr, "not suitable type\n");
4472 		goto exit;
4473 	}
4474 
4475 	if (vndr_ie_len > mgmt_ie_buf_len) {
4476 		err = -ENOMEM;
4477 		bphy_err(drvr, "extra IE size too big\n");
4478 		goto exit;
4479 	}
4480 
4481 	/* parse and save new vndr_ie in curr_ie_buff before comparing it */
4482 	if (vndr_ie_buf && vndr_ie_len && curr_ie_buf) {
4483 		ptr = curr_ie_buf;
4484 		brcmf_parse_vndr_ies(vndr_ie_buf, vndr_ie_len, &new_vndr_ies);
4485 		for (i = 0; i < new_vndr_ies.count; i++) {
4486 			vndrie_info = &new_vndr_ies.ie_info[i];
4487 			memcpy(ptr + parsed_ie_buf_len, vndrie_info->ie_ptr,
4488 			       vndrie_info->ie_len);
4489 			parsed_ie_buf_len += vndrie_info->ie_len;
4490 		}
4491 	}
4492 
4493 	if (mgmt_ie_buf && *mgmt_ie_len) {
4494 		if (parsed_ie_buf_len && (parsed_ie_buf_len == *mgmt_ie_len) &&
4495 		    (memcmp(mgmt_ie_buf, curr_ie_buf,
4496 			    parsed_ie_buf_len) == 0)) {
4497 			brcmf_dbg(TRACE, "Previous mgmt IE equals to current IE\n");
4498 			goto exit;
4499 		}
4500 
4501 		/* parse old vndr_ie */
4502 		brcmf_parse_vndr_ies(mgmt_ie_buf, *mgmt_ie_len, &old_vndr_ies);
4503 
4504 		/* make a command to delete old ie */
4505 		for (i = 0; i < old_vndr_ies.count; i++) {
4506 			vndrie_info = &old_vndr_ies.ie_info[i];
4507 
4508 			brcmf_dbg(TRACE, "DEL ID : %d, Len: %d , OUI:%3ph\n",
4509 				  vndrie_info->vndrie.id,
4510 				  vndrie_info->vndrie.len,
4511 				  vndrie_info->vndrie.oui);
4512 
4513 			del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
4514 							   vndrie_info->ie_ptr,
4515 							   vndrie_info->ie_len,
4516 							   "del");
4517 			curr_ie_buf += del_add_ie_buf_len;
4518 			total_ie_buf_len += del_add_ie_buf_len;
4519 		}
4520 	}
4521 
4522 	*mgmt_ie_len = 0;
4523 	/* Add if there is any extra IE */
4524 	if (mgmt_ie_buf && parsed_ie_buf_len) {
4525 		ptr = mgmt_ie_buf;
4526 
4527 		remained_buf_len = mgmt_ie_buf_len;
4528 
4529 		/* make a command to add new ie */
4530 		for (i = 0; i < new_vndr_ies.count; i++) {
4531 			vndrie_info = &new_vndr_ies.ie_info[i];
4532 
4533 			/* verify remained buf size before copy data */
4534 			if (remained_buf_len < (vndrie_info->vndrie.len +
4535 							VNDR_IE_VSIE_OFFSET)) {
4536 				bphy_err(drvr, "no space in mgmt_ie_buf: len left %d",
4537 					 remained_buf_len);
4538 				break;
4539 			}
4540 			remained_buf_len -= (vndrie_info->ie_len +
4541 					     VNDR_IE_VSIE_OFFSET);
4542 
4543 			brcmf_dbg(TRACE, "ADDED ID : %d, Len: %d, OUI:%3ph\n",
4544 				  vndrie_info->vndrie.id,
4545 				  vndrie_info->vndrie.len,
4546 				  vndrie_info->vndrie.oui);
4547 
4548 			del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
4549 							   vndrie_info->ie_ptr,
4550 							   vndrie_info->ie_len,
4551 							   "add");
4552 
4553 			/* save the parsed IE in wl struct */
4554 			memcpy(ptr + (*mgmt_ie_len), vndrie_info->ie_ptr,
4555 			       vndrie_info->ie_len);
4556 			*mgmt_ie_len += vndrie_info->ie_len;
4557 
4558 			curr_ie_buf += del_add_ie_buf_len;
4559 			total_ie_buf_len += del_add_ie_buf_len;
4560 		}
4561 	}
4562 	if (total_ie_buf_len) {
4563 		err  = brcmf_fil_bsscfg_data_set(ifp, "vndr_ie", iovar_ie_buf,
4564 						 total_ie_buf_len);
4565 		if (err)
4566 			bphy_err(drvr, "vndr ie set error : %d\n", err);
4567 	}
4568 
4569 exit:
4570 	kfree(iovar_ie_buf);
4571 	return err;
4572 }
4573 
4574 s32 brcmf_vif_clear_mgmt_ies(struct brcmf_cfg80211_vif *vif)
4575 {
4576 	s32 pktflags[] = {
4577 		BRCMF_VNDR_IE_PRBREQ_FLAG,
4578 		BRCMF_VNDR_IE_PRBRSP_FLAG,
4579 		BRCMF_VNDR_IE_BEACON_FLAG
4580 	};
4581 	int i;
4582 
4583 	for (i = 0; i < ARRAY_SIZE(pktflags); i++)
4584 		brcmf_vif_set_mgmt_ie(vif, pktflags[i], NULL, 0);
4585 
4586 	memset(&vif->saved_ie, 0, sizeof(vif->saved_ie));
4587 	return 0;
4588 }
4589 
4590 static s32
4591 brcmf_config_ap_mgmt_ie(struct brcmf_cfg80211_vif *vif,
4592 			struct cfg80211_beacon_data *beacon)
4593 {
4594 	struct brcmf_pub *drvr = vif->ifp->drvr;
4595 	s32 err;
4596 
4597 	/* Set Beacon IEs to FW */
4598 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_BEACON_FLAG,
4599 				    beacon->tail, beacon->tail_len);
4600 	if (err) {
4601 		bphy_err(drvr, "Set Beacon IE Failed\n");
4602 		return err;
4603 	}
4604 	brcmf_dbg(TRACE, "Applied Vndr IEs for Beacon\n");
4605 
4606 	/* Set Probe Response IEs to FW */
4607 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBRSP_FLAG,
4608 				    beacon->proberesp_ies,
4609 				    beacon->proberesp_ies_len);
4610 	if (err)
4611 		bphy_err(drvr, "Set Probe Resp IE Failed\n");
4612 	else
4613 		brcmf_dbg(TRACE, "Applied Vndr IEs for Probe Resp\n");
4614 
4615 	/* Set Assoc Response IEs to FW */
4616 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_ASSOCRSP_FLAG,
4617 				    beacon->assocresp_ies,
4618 				    beacon->assocresp_ies_len);
4619 	if (err)
4620 		brcmf_err("Set Assoc Resp IE Failed\n");
4621 	else
4622 		brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc Resp\n");
4623 
4624 	return err;
4625 }
4626 
4627 static s32
4628 brcmf_parse_configure_security(struct brcmf_if *ifp,
4629 			       struct cfg80211_ap_settings *settings,
4630 			       enum nl80211_iftype dev_role)
4631 {
4632 	const struct brcmf_tlv *rsn_ie;
4633 	const struct brcmf_vs_tlv *wpa_ie;
4634 	s32 err = 0;
4635 
4636 	/* find the RSN_IE */
4637 	rsn_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
4638 				  settings->beacon.tail_len, WLAN_EID_RSN);
4639 
4640 	/* find the WPA_IE */
4641 	wpa_ie = brcmf_find_wpaie((u8 *)settings->beacon.tail,
4642 				  settings->beacon.tail_len);
4643 
4644 	if (wpa_ie || rsn_ie) {
4645 		brcmf_dbg(TRACE, "WPA(2) IE is found\n");
4646 		if (wpa_ie) {
4647 			/* WPA IE */
4648 			err = brcmf_configure_wpaie(ifp, wpa_ie, false);
4649 			if (err < 0)
4650 				return err;
4651 		} else {
4652 			struct brcmf_vs_tlv *tmp_ie;
4653 
4654 			tmp_ie = (struct brcmf_vs_tlv *)rsn_ie;
4655 
4656 			/* RSN IE */
4657 			err = brcmf_configure_wpaie(ifp, tmp_ie, true);
4658 			if (err < 0)
4659 				return err;
4660 		}
4661 	} else {
4662 		brcmf_dbg(TRACE, "No WPA(2) IEs found\n");
4663 		brcmf_configure_opensecurity(ifp);
4664 	}
4665 
4666 	return err;
4667 }
4668 
4669 static s32
4670 brcmf_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *ndev,
4671 			struct cfg80211_ap_settings *settings)
4672 {
4673 	s32 ie_offset;
4674 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4675 	struct brcmf_if *ifp = netdev_priv(ndev);
4676 	struct brcmf_pub *drvr = cfg->pub;
4677 	const struct brcmf_tlv *ssid_ie;
4678 	const struct brcmf_tlv *country_ie;
4679 	struct brcmf_ssid_le ssid_le;
4680 	s32 err = -EPERM;
4681 	struct brcmf_join_params join_params;
4682 	enum nl80211_iftype dev_role;
4683 	struct brcmf_fil_bss_enable_le bss_enable;
4684 	u16 chanspec = chandef_to_chanspec(&cfg->d11inf, &settings->chandef);
4685 	bool mbss;
4686 	int is_11d;
4687 	bool supports_11d;
4688 
4689 	brcmf_dbg(TRACE, "ctrlchn=%d, center=%d, bw=%d, beacon_interval=%d, dtim_period=%d,\n",
4690 		  settings->chandef.chan->hw_value,
4691 		  settings->chandef.center_freq1, settings->chandef.width,
4692 		  settings->beacon_interval, settings->dtim_period);
4693 	brcmf_dbg(TRACE, "ssid=%s(%zu), auth_type=%d, inactivity_timeout=%d\n",
4694 		  settings->ssid, settings->ssid_len, settings->auth_type,
4695 		  settings->inactivity_timeout);
4696 	dev_role = ifp->vif->wdev.iftype;
4697 	mbss = ifp->vif->mbss;
4698 
4699 	/* store current 11d setting */
4700 	if (brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_REGULATORY,
4701 				  &ifp->vif->is_11d)) {
4702 		is_11d = supports_11d = false;
4703 	} else {
4704 		country_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
4705 					      settings->beacon.tail_len,
4706 					      WLAN_EID_COUNTRY);
4707 		is_11d = country_ie ? 1 : 0;
4708 		supports_11d = true;
4709 	}
4710 
4711 	memset(&ssid_le, 0, sizeof(ssid_le));
4712 	if (settings->ssid == NULL || settings->ssid_len == 0) {
4713 		ie_offset = DOT11_MGMT_HDR_LEN + DOT11_BCN_PRB_FIXED_LEN;
4714 		ssid_ie = brcmf_parse_tlvs(
4715 				(u8 *)&settings->beacon.head[ie_offset],
4716 				settings->beacon.head_len - ie_offset,
4717 				WLAN_EID_SSID);
4718 		if (!ssid_ie || ssid_ie->len > IEEE80211_MAX_SSID_LEN)
4719 			return -EINVAL;
4720 
4721 		memcpy(ssid_le.SSID, ssid_ie->data, ssid_ie->len);
4722 		ssid_le.SSID_len = cpu_to_le32(ssid_ie->len);
4723 		brcmf_dbg(TRACE, "SSID is (%s) in Head\n", ssid_le.SSID);
4724 	} else {
4725 		memcpy(ssid_le.SSID, settings->ssid, settings->ssid_len);
4726 		ssid_le.SSID_len = cpu_to_le32((u32)settings->ssid_len);
4727 	}
4728 
4729 	if (!mbss) {
4730 		brcmf_set_mpc(ifp, 0);
4731 		brcmf_configure_arp_nd_offload(ifp, false);
4732 	}
4733 
4734 	/* Parameters shared by all radio interfaces */
4735 	if (!mbss) {
4736 		if ((supports_11d) && (is_11d != ifp->vif->is_11d)) {
4737 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_REGULATORY,
4738 						    is_11d);
4739 			if (err < 0) {
4740 				bphy_err(drvr, "Regulatory Set Error, %d\n",
4741 					 err);
4742 				goto exit;
4743 			}
4744 		}
4745 		if (settings->beacon_interval) {
4746 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD,
4747 						    settings->beacon_interval);
4748 			if (err < 0) {
4749 				bphy_err(drvr, "Beacon Interval Set Error, %d\n",
4750 					 err);
4751 				goto exit;
4752 			}
4753 		}
4754 		if (settings->dtim_period) {
4755 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_DTIMPRD,
4756 						    settings->dtim_period);
4757 			if (err < 0) {
4758 				bphy_err(drvr, "DTIM Interval Set Error, %d\n",
4759 					 err);
4760 				goto exit;
4761 			}
4762 		}
4763 
4764 		if ((dev_role == NL80211_IFTYPE_AP) &&
4765 		    ((ifp->ifidx == 0) ||
4766 		     (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_RSDB) &&
4767 		      !brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN)))) {
4768 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4769 			if (err < 0) {
4770 				bphy_err(drvr, "BRCMF_C_DOWN error %d\n",
4771 					 err);
4772 				goto exit;
4773 			}
4774 			brcmf_fil_iovar_int_set(ifp, "apsta", 0);
4775 		}
4776 
4777 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 1);
4778 		if (err < 0) {
4779 			bphy_err(drvr, "SET INFRA error %d\n", err);
4780 			goto exit;
4781 		}
4782 	} else if (WARN_ON(supports_11d && (is_11d != ifp->vif->is_11d))) {
4783 		/* Multiple-BSS should use same 11d configuration */
4784 		err = -EINVAL;
4785 		goto exit;
4786 	}
4787 
4788 	/* Interface specific setup */
4789 	if (dev_role == NL80211_IFTYPE_AP) {
4790 		if ((brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS)) && (!mbss))
4791 			brcmf_fil_iovar_int_set(ifp, "mbss", 1);
4792 
4793 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 1);
4794 		if (err < 0) {
4795 			bphy_err(drvr, "setting AP mode failed %d\n",
4796 				 err);
4797 			goto exit;
4798 		}
4799 		if (!mbss) {
4800 			/* Firmware 10.x requires setting channel after enabling
4801 			 * AP and before bringing interface up.
4802 			 */
4803 			err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
4804 			if (err < 0) {
4805 				bphy_err(drvr, "Set Channel failed: chspec=%d, %d\n",
4806 					 chanspec, err);
4807 				goto exit;
4808 			}
4809 		}
4810 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
4811 		if (err < 0) {
4812 			bphy_err(drvr, "BRCMF_C_UP error (%d)\n", err);
4813 			goto exit;
4814 		}
4815 
4816 		err = brcmf_parse_configure_security(ifp, settings,
4817 						     NL80211_IFTYPE_AP);
4818 		if (err < 0) {
4819 			bphy_err(drvr, "brcmf_parse_configure_security error\n");
4820 			goto exit;
4821 		}
4822 
4823 		/* On DOWN the firmware removes the WEP keys, reconfigure
4824 		 * them if they were set.
4825 		 */
4826 		brcmf_cfg80211_reconfigure_wep(ifp);
4827 
4828 		memset(&join_params, 0, sizeof(join_params));
4829 		/* join parameters starts with ssid */
4830 		memcpy(&join_params.ssid_le, &ssid_le, sizeof(ssid_le));
4831 		/* create softap */
4832 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
4833 					     &join_params, sizeof(join_params));
4834 		if (err < 0) {
4835 			bphy_err(drvr, "SET SSID error (%d)\n", err);
4836 			goto exit;
4837 		}
4838 
4839 		err = brcmf_fil_iovar_int_set(ifp, "closednet",
4840 					      settings->hidden_ssid);
4841 		if (err) {
4842 			bphy_err(drvr, "%s closednet error (%d)\n",
4843 				 settings->hidden_ssid ?
4844 				 "enabled" : "disabled",
4845 				 err);
4846 			goto exit;
4847 		}
4848 
4849 		brcmf_dbg(TRACE, "AP mode configuration complete\n");
4850 	} else if (dev_role == NL80211_IFTYPE_P2P_GO) {
4851 		err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
4852 		if (err < 0) {
4853 			bphy_err(drvr, "Set Channel failed: chspec=%d, %d\n",
4854 				 chanspec, err);
4855 			goto exit;
4856 		}
4857 
4858 		err = brcmf_parse_configure_security(ifp, settings,
4859 						     NL80211_IFTYPE_P2P_GO);
4860 		if (err < 0) {
4861 			brcmf_err("brcmf_parse_configure_security error\n");
4862 			goto exit;
4863 		}
4864 
4865 		err = brcmf_fil_bsscfg_data_set(ifp, "ssid", &ssid_le,
4866 						sizeof(ssid_le));
4867 		if (err < 0) {
4868 			bphy_err(drvr, "setting ssid failed %d\n", err);
4869 			goto exit;
4870 		}
4871 		bss_enable.bsscfgidx = cpu_to_le32(ifp->bsscfgidx);
4872 		bss_enable.enable = cpu_to_le32(1);
4873 		err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
4874 					       sizeof(bss_enable));
4875 		if (err < 0) {
4876 			bphy_err(drvr, "bss_enable config failed %d\n", err);
4877 			goto exit;
4878 		}
4879 
4880 		brcmf_dbg(TRACE, "GO mode configuration complete\n");
4881 	} else {
4882 		WARN_ON(1);
4883 	}
4884 
4885 	brcmf_config_ap_mgmt_ie(ifp->vif, &settings->beacon);
4886 	set_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
4887 	brcmf_net_setcarrier(ifp, true);
4888 
4889 exit:
4890 	if ((err) && (!mbss)) {
4891 		brcmf_set_mpc(ifp, 1);
4892 		brcmf_configure_arp_nd_offload(ifp, true);
4893 	}
4894 	return err;
4895 }
4896 
4897 static int brcmf_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
4898 {
4899 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4900 	struct brcmf_if *ifp = netdev_priv(ndev);
4901 	struct brcmf_pub *drvr = cfg->pub;
4902 	s32 err;
4903 	struct brcmf_fil_bss_enable_le bss_enable;
4904 	struct brcmf_join_params join_params;
4905 
4906 	brcmf_dbg(TRACE, "Enter\n");
4907 
4908 	if (ifp->vif->wdev.iftype == NL80211_IFTYPE_AP) {
4909 		/* Due to most likely deauths outstanding we sleep */
4910 		/* first to make sure they get processed by fw. */
4911 		msleep(400);
4912 
4913 		if (ifp->vif->mbss) {
4914 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4915 			return err;
4916 		}
4917 
4918 		/* First BSS doesn't get a full reset */
4919 		if (ifp->bsscfgidx == 0)
4920 			brcmf_fil_iovar_int_set(ifp, "closednet", 0);
4921 
4922 		memset(&join_params, 0, sizeof(join_params));
4923 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
4924 					     &join_params, sizeof(join_params));
4925 		if (err < 0)
4926 			bphy_err(drvr, "SET SSID error (%d)\n", err);
4927 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4928 		if (err < 0)
4929 			bphy_err(drvr, "BRCMF_C_DOWN error %d\n", err);
4930 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 0);
4931 		if (err < 0)
4932 			bphy_err(drvr, "setting AP mode failed %d\n", err);
4933 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS))
4934 			brcmf_fil_iovar_int_set(ifp, "mbss", 0);
4935 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_REGULATORY,
4936 				      ifp->vif->is_11d);
4937 		/* Bring device back up so it can be used again */
4938 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
4939 		if (err < 0)
4940 			bphy_err(drvr, "BRCMF_C_UP error %d\n", err);
4941 
4942 		brcmf_vif_clear_mgmt_ies(ifp->vif);
4943 	} else {
4944 		bss_enable.bsscfgidx = cpu_to_le32(ifp->bsscfgidx);
4945 		bss_enable.enable = cpu_to_le32(0);
4946 		err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
4947 					       sizeof(bss_enable));
4948 		if (err < 0)
4949 			bphy_err(drvr, "bss_enable config failed %d\n", err);
4950 	}
4951 	brcmf_set_mpc(ifp, 1);
4952 	brcmf_configure_arp_nd_offload(ifp, true);
4953 	clear_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
4954 	brcmf_net_setcarrier(ifp, false);
4955 
4956 	return err;
4957 }
4958 
4959 static s32
4960 brcmf_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
4961 			     struct cfg80211_beacon_data *info)
4962 {
4963 	struct brcmf_if *ifp = netdev_priv(ndev);
4964 	s32 err;
4965 
4966 	brcmf_dbg(TRACE, "Enter\n");
4967 
4968 	err = brcmf_config_ap_mgmt_ie(ifp->vif, info);
4969 
4970 	return err;
4971 }
4972 
4973 static int
4974 brcmf_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
4975 			   struct station_del_parameters *params)
4976 {
4977 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4978 	struct brcmf_pub *drvr = cfg->pub;
4979 	struct brcmf_scb_val_le scbval;
4980 	struct brcmf_if *ifp = netdev_priv(ndev);
4981 	s32 err;
4982 
4983 	if (!params->mac)
4984 		return -EFAULT;
4985 
4986 	brcmf_dbg(TRACE, "Enter %pM\n", params->mac);
4987 
4988 	if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
4989 		ifp = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
4990 	if (!check_vif_up(ifp->vif))
4991 		return -EIO;
4992 
4993 	memcpy(&scbval.ea, params->mac, ETH_ALEN);
4994 	scbval.val = cpu_to_le32(params->reason_code);
4995 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCB_DEAUTHENTICATE_FOR_REASON,
4996 				     &scbval, sizeof(scbval));
4997 	if (err)
4998 		bphy_err(drvr, "SCB_DEAUTHENTICATE_FOR_REASON failed %d\n",
4999 			 err);
5000 
5001 	brcmf_dbg(TRACE, "Exit\n");
5002 	return err;
5003 }
5004 
5005 static int
5006 brcmf_cfg80211_change_station(struct wiphy *wiphy, struct net_device *ndev,
5007 			      const u8 *mac, struct station_parameters *params)
5008 {
5009 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5010 	struct brcmf_pub *drvr = cfg->pub;
5011 	struct brcmf_if *ifp = netdev_priv(ndev);
5012 	s32 err;
5013 
5014 	brcmf_dbg(TRACE, "Enter, MAC %pM, mask 0x%04x set 0x%04x\n", mac,
5015 		  params->sta_flags_mask, params->sta_flags_set);
5016 
5017 	/* Ignore all 00 MAC */
5018 	if (is_zero_ether_addr(mac))
5019 		return 0;
5020 
5021 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
5022 		return 0;
5023 
5024 	if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED))
5025 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SCB_AUTHORIZE,
5026 					     (void *)mac, ETH_ALEN);
5027 	else
5028 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SCB_DEAUTHORIZE,
5029 					     (void *)mac, ETH_ALEN);
5030 	if (err < 0)
5031 		bphy_err(drvr, "Setting SCB (de-)authorize failed, %d\n", err);
5032 
5033 	return err;
5034 }
5035 
5036 static void
5037 brcmf_cfg80211_update_mgmt_frame_registrations(struct wiphy *wiphy,
5038 					       struct wireless_dev *wdev,
5039 					       struct mgmt_frame_regs *upd)
5040 {
5041 	struct brcmf_cfg80211_vif *vif;
5042 
5043 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5044 
5045 	vif->mgmt_rx_reg = upd->interface_stypes;
5046 }
5047 
5048 
5049 static int
5050 brcmf_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
5051 		       struct cfg80211_mgmt_tx_params *params, u64 *cookie)
5052 {
5053 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5054 	struct ieee80211_channel *chan = params->chan;
5055 	struct brcmf_pub *drvr = cfg->pub;
5056 	const u8 *buf = params->buf;
5057 	size_t len = params->len;
5058 	const struct ieee80211_mgmt *mgmt;
5059 	struct brcmf_cfg80211_vif *vif;
5060 	s32 err = 0;
5061 	s32 ie_offset;
5062 	s32 ie_len;
5063 	struct brcmf_fil_action_frame_le *action_frame;
5064 	struct brcmf_fil_af_params_le *af_params;
5065 	bool ack;
5066 	s32 chan_nr;
5067 	u32 freq;
5068 
5069 	brcmf_dbg(TRACE, "Enter\n");
5070 
5071 	*cookie = 0;
5072 
5073 	mgmt = (const struct ieee80211_mgmt *)buf;
5074 
5075 	if (!ieee80211_is_mgmt(mgmt->frame_control)) {
5076 		bphy_err(drvr, "Driver only allows MGMT packet type\n");
5077 		return -EPERM;
5078 	}
5079 
5080 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5081 
5082 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
5083 		/* Right now the only reason to get a probe response */
5084 		/* is for p2p listen response or for p2p GO from     */
5085 		/* wpa_supplicant. Unfortunately the probe is send   */
5086 		/* on primary ndev, while dongle wants it on the p2p */
5087 		/* vif. Since this is only reason for a probe        */
5088 		/* response to be sent, the vif is taken from cfg.   */
5089 		/* If ever desired to send proberesp for non p2p     */
5090 		/* response then data should be checked for          */
5091 		/* "DIRECT-". Note in future supplicant will take    */
5092 		/* dedicated p2p wdev to do this and then this 'hack'*/
5093 		/* is not needed anymore.                            */
5094 		ie_offset =  DOT11_MGMT_HDR_LEN +
5095 			     DOT11_BCN_PRB_FIXED_LEN;
5096 		ie_len = len - ie_offset;
5097 		if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif)
5098 			vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
5099 		err = brcmf_vif_set_mgmt_ie(vif,
5100 					    BRCMF_VNDR_IE_PRBRSP_FLAG,
5101 					    &buf[ie_offset],
5102 					    ie_len);
5103 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
5104 					GFP_KERNEL);
5105 	} else if (ieee80211_is_action(mgmt->frame_control)) {
5106 		if (len > BRCMF_FIL_ACTION_FRAME_SIZE + DOT11_MGMT_HDR_LEN) {
5107 			bphy_err(drvr, "invalid action frame length\n");
5108 			err = -EINVAL;
5109 			goto exit;
5110 		}
5111 		af_params = kzalloc(sizeof(*af_params), GFP_KERNEL);
5112 		if (af_params == NULL) {
5113 			bphy_err(drvr, "unable to allocate frame\n");
5114 			err = -ENOMEM;
5115 			goto exit;
5116 		}
5117 		action_frame = &af_params->action_frame;
5118 		/* Add the packet Id */
5119 		action_frame->packet_id = cpu_to_le32(*cookie);
5120 		/* Add BSSID */
5121 		memcpy(&action_frame->da[0], &mgmt->da[0], ETH_ALEN);
5122 		memcpy(&af_params->bssid[0], &mgmt->bssid[0], ETH_ALEN);
5123 		/* Add the length exepted for 802.11 header  */
5124 		action_frame->len = cpu_to_le16(len - DOT11_MGMT_HDR_LEN);
5125 		/* Add the channel. Use the one specified as parameter if any or
5126 		 * the current one (got from the firmware) otherwise
5127 		 */
5128 		if (chan)
5129 			freq = chan->center_freq;
5130 		else
5131 			brcmf_fil_cmd_int_get(vif->ifp, BRCMF_C_GET_CHANNEL,
5132 					      &freq);
5133 		chan_nr = ieee80211_frequency_to_channel(freq);
5134 		af_params->channel = cpu_to_le32(chan_nr);
5135 		af_params->dwell_time = cpu_to_le32(params->wait);
5136 		memcpy(action_frame->data, &buf[DOT11_MGMT_HDR_LEN],
5137 		       le16_to_cpu(action_frame->len));
5138 
5139 		brcmf_dbg(TRACE, "Action frame, cookie=%lld, len=%d, freq=%d\n",
5140 			  *cookie, le16_to_cpu(action_frame->len), freq);
5141 
5142 		ack = brcmf_p2p_send_action_frame(cfg, cfg_to_ndev(cfg),
5143 						  af_params);
5144 
5145 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack,
5146 					GFP_KERNEL);
5147 		kfree(af_params);
5148 	} else {
5149 		brcmf_dbg(TRACE, "Unhandled, fc=%04x!!\n", mgmt->frame_control);
5150 		brcmf_dbg_hex_dump(true, buf, len, "payload, len=%zu\n", len);
5151 	}
5152 
5153 exit:
5154 	return err;
5155 }
5156 
5157 
5158 static int
5159 brcmf_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
5160 					struct wireless_dev *wdev,
5161 					u64 cookie)
5162 {
5163 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5164 	struct brcmf_pub *drvr = cfg->pub;
5165 	struct brcmf_cfg80211_vif *vif;
5166 	int err = 0;
5167 
5168 	brcmf_dbg(TRACE, "Enter p2p listen cancel\n");
5169 
5170 	vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
5171 	if (vif == NULL) {
5172 		bphy_err(drvr, "No p2p device available for probe response\n");
5173 		err = -ENODEV;
5174 		goto exit;
5175 	}
5176 	brcmf_p2p_cancel_remain_on_channel(vif->ifp);
5177 exit:
5178 	return err;
5179 }
5180 
5181 static int brcmf_cfg80211_get_channel(struct wiphy *wiphy,
5182 				      struct wireless_dev *wdev,
5183 				      struct cfg80211_chan_def *chandef)
5184 {
5185 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5186 	struct net_device *ndev = wdev->netdev;
5187 	struct brcmf_pub *drvr = cfg->pub;
5188 	struct brcmu_chan ch;
5189 	enum nl80211_band band = 0;
5190 	enum nl80211_chan_width width = 0;
5191 	u32 chanspec;
5192 	int freq, err;
5193 
5194 	if (!ndev || drvr->bus_if->state != BRCMF_BUS_UP)
5195 		return -ENODEV;
5196 
5197 	err = brcmf_fil_iovar_int_get(netdev_priv(ndev), "chanspec", &chanspec);
5198 	if (err) {
5199 		bphy_err(drvr, "chanspec failed (%d)\n", err);
5200 		return err;
5201 	}
5202 
5203 	ch.chspec = chanspec;
5204 	cfg->d11inf.decchspec(&ch);
5205 
5206 	switch (ch.band) {
5207 	case BRCMU_CHAN_BAND_2G:
5208 		band = NL80211_BAND_2GHZ;
5209 		break;
5210 	case BRCMU_CHAN_BAND_5G:
5211 		band = NL80211_BAND_5GHZ;
5212 		break;
5213 	}
5214 
5215 	switch (ch.bw) {
5216 	case BRCMU_CHAN_BW_80:
5217 		width = NL80211_CHAN_WIDTH_80;
5218 		break;
5219 	case BRCMU_CHAN_BW_40:
5220 		width = NL80211_CHAN_WIDTH_40;
5221 		break;
5222 	case BRCMU_CHAN_BW_20:
5223 		width = NL80211_CHAN_WIDTH_20;
5224 		break;
5225 	case BRCMU_CHAN_BW_80P80:
5226 		width = NL80211_CHAN_WIDTH_80P80;
5227 		break;
5228 	case BRCMU_CHAN_BW_160:
5229 		width = NL80211_CHAN_WIDTH_160;
5230 		break;
5231 	}
5232 
5233 	freq = ieee80211_channel_to_frequency(ch.control_ch_num, band);
5234 	chandef->chan = ieee80211_get_channel(wiphy, freq);
5235 	chandef->width = width;
5236 	chandef->center_freq1 = ieee80211_channel_to_frequency(ch.chnum, band);
5237 	chandef->center_freq2 = 0;
5238 
5239 	return 0;
5240 }
5241 
5242 static int brcmf_cfg80211_crit_proto_start(struct wiphy *wiphy,
5243 					   struct wireless_dev *wdev,
5244 					   enum nl80211_crit_proto_id proto,
5245 					   u16 duration)
5246 {
5247 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5248 	struct brcmf_cfg80211_vif *vif;
5249 
5250 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5251 
5252 	/* only DHCP support for now */
5253 	if (proto != NL80211_CRIT_PROTO_DHCP)
5254 		return -EINVAL;
5255 
5256 	/* suppress and abort scanning */
5257 	set_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
5258 	brcmf_abort_scanning(cfg);
5259 
5260 	return brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_DISABLED, duration);
5261 }
5262 
5263 static void brcmf_cfg80211_crit_proto_stop(struct wiphy *wiphy,
5264 					   struct wireless_dev *wdev)
5265 {
5266 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5267 	struct brcmf_cfg80211_vif *vif;
5268 
5269 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5270 
5271 	brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
5272 	clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
5273 }
5274 
5275 static s32
5276 brcmf_notify_tdls_peer_event(struct brcmf_if *ifp,
5277 			     const struct brcmf_event_msg *e, void *data)
5278 {
5279 	switch (e->reason) {
5280 	case BRCMF_E_REASON_TDLS_PEER_DISCOVERED:
5281 		brcmf_dbg(TRACE, "TDLS Peer Discovered\n");
5282 		break;
5283 	case BRCMF_E_REASON_TDLS_PEER_CONNECTED:
5284 		brcmf_dbg(TRACE, "TDLS Peer Connected\n");
5285 		brcmf_proto_add_tdls_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
5286 		break;
5287 	case BRCMF_E_REASON_TDLS_PEER_DISCONNECTED:
5288 		brcmf_dbg(TRACE, "TDLS Peer Disconnected\n");
5289 		brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
5290 		break;
5291 	}
5292 
5293 	return 0;
5294 }
5295 
5296 static int brcmf_convert_nl80211_tdls_oper(enum nl80211_tdls_operation oper)
5297 {
5298 	int ret;
5299 
5300 	switch (oper) {
5301 	case NL80211_TDLS_DISCOVERY_REQ:
5302 		ret = BRCMF_TDLS_MANUAL_EP_DISCOVERY;
5303 		break;
5304 	case NL80211_TDLS_SETUP:
5305 		ret = BRCMF_TDLS_MANUAL_EP_CREATE;
5306 		break;
5307 	case NL80211_TDLS_TEARDOWN:
5308 		ret = BRCMF_TDLS_MANUAL_EP_DELETE;
5309 		break;
5310 	default:
5311 		brcmf_err("unsupported operation: %d\n", oper);
5312 		ret = -EOPNOTSUPP;
5313 	}
5314 	return ret;
5315 }
5316 
5317 static int brcmf_cfg80211_tdls_oper(struct wiphy *wiphy,
5318 				    struct net_device *ndev, const u8 *peer,
5319 				    enum nl80211_tdls_operation oper)
5320 {
5321 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5322 	struct brcmf_pub *drvr = cfg->pub;
5323 	struct brcmf_if *ifp;
5324 	struct brcmf_tdls_iovar_le info;
5325 	int ret = 0;
5326 
5327 	ret = brcmf_convert_nl80211_tdls_oper(oper);
5328 	if (ret < 0)
5329 		return ret;
5330 
5331 	ifp = netdev_priv(ndev);
5332 	memset(&info, 0, sizeof(info));
5333 	info.mode = (u8)ret;
5334 	if (peer)
5335 		memcpy(info.ea, peer, ETH_ALEN);
5336 
5337 	ret = brcmf_fil_iovar_data_set(ifp, "tdls_endpoint",
5338 				       &info, sizeof(info));
5339 	if (ret < 0)
5340 		bphy_err(drvr, "tdls_endpoint iovar failed: ret=%d\n", ret);
5341 
5342 	return ret;
5343 }
5344 
5345 static int
5346 brcmf_cfg80211_update_conn_params(struct wiphy *wiphy,
5347 				  struct net_device *ndev,
5348 				  struct cfg80211_connect_params *sme,
5349 				  u32 changed)
5350 {
5351 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5352 	struct brcmf_pub *drvr = cfg->pub;
5353 	struct brcmf_if *ifp;
5354 	int err;
5355 
5356 	if (!(changed & UPDATE_ASSOC_IES))
5357 		return 0;
5358 
5359 	ifp = netdev_priv(ndev);
5360 	err = brcmf_vif_set_mgmt_ie(ifp->vif, BRCMF_VNDR_IE_ASSOCREQ_FLAG,
5361 				    sme->ie, sme->ie_len);
5362 	if (err)
5363 		bphy_err(drvr, "Set Assoc REQ IE Failed\n");
5364 	else
5365 		brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc request\n");
5366 
5367 	return err;
5368 }
5369 
5370 #ifdef CONFIG_PM
5371 static int
5372 brcmf_cfg80211_set_rekey_data(struct wiphy *wiphy, struct net_device *ndev,
5373 			      struct cfg80211_gtk_rekey_data *gtk)
5374 {
5375 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5376 	struct brcmf_pub *drvr = cfg->pub;
5377 	struct brcmf_if *ifp = netdev_priv(ndev);
5378 	struct brcmf_gtk_keyinfo_le gtk_le;
5379 	int ret;
5380 
5381 	brcmf_dbg(TRACE, "Enter, bssidx=%d\n", ifp->bsscfgidx);
5382 
5383 	memcpy(gtk_le.kck, gtk->kck, sizeof(gtk_le.kck));
5384 	memcpy(gtk_le.kek, gtk->kek, sizeof(gtk_le.kek));
5385 	memcpy(gtk_le.replay_counter, gtk->replay_ctr,
5386 	       sizeof(gtk_le.replay_counter));
5387 
5388 	ret = brcmf_fil_iovar_data_set(ifp, "gtk_key_info", &gtk_le,
5389 				       sizeof(gtk_le));
5390 	if (ret < 0)
5391 		bphy_err(drvr, "gtk_key_info iovar failed: ret=%d\n", ret);
5392 
5393 	return ret;
5394 }
5395 #endif
5396 
5397 static int brcmf_cfg80211_set_pmk(struct wiphy *wiphy, struct net_device *dev,
5398 				  const struct cfg80211_pmk_conf *conf)
5399 {
5400 	struct brcmf_if *ifp;
5401 
5402 	brcmf_dbg(TRACE, "enter\n");
5403 
5404 	/* expect using firmware supplicant for 1X */
5405 	ifp = netdev_priv(dev);
5406 	if (WARN_ON(ifp->vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_1X))
5407 		return -EINVAL;
5408 
5409 	if (conf->pmk_len > BRCMF_WSEC_MAX_PSK_LEN)
5410 		return -ERANGE;
5411 
5412 	return brcmf_set_pmk(ifp, conf->pmk, conf->pmk_len);
5413 }
5414 
5415 static int brcmf_cfg80211_del_pmk(struct wiphy *wiphy, struct net_device *dev,
5416 				  const u8 *aa)
5417 {
5418 	struct brcmf_if *ifp;
5419 
5420 	brcmf_dbg(TRACE, "enter\n");
5421 	ifp = netdev_priv(dev);
5422 	if (WARN_ON(ifp->vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_1X))
5423 		return -EINVAL;
5424 
5425 	return brcmf_set_pmk(ifp, NULL, 0);
5426 }
5427 
5428 static struct cfg80211_ops brcmf_cfg80211_ops = {
5429 	.add_virtual_intf = brcmf_cfg80211_add_iface,
5430 	.del_virtual_intf = brcmf_cfg80211_del_iface,
5431 	.change_virtual_intf = brcmf_cfg80211_change_iface,
5432 	.scan = brcmf_cfg80211_scan,
5433 	.set_wiphy_params = brcmf_cfg80211_set_wiphy_params,
5434 	.join_ibss = brcmf_cfg80211_join_ibss,
5435 	.leave_ibss = brcmf_cfg80211_leave_ibss,
5436 	.get_station = brcmf_cfg80211_get_station,
5437 	.dump_station = brcmf_cfg80211_dump_station,
5438 	.set_tx_power = brcmf_cfg80211_set_tx_power,
5439 	.get_tx_power = brcmf_cfg80211_get_tx_power,
5440 	.add_key = brcmf_cfg80211_add_key,
5441 	.del_key = brcmf_cfg80211_del_key,
5442 	.get_key = brcmf_cfg80211_get_key,
5443 	.set_default_key = brcmf_cfg80211_config_default_key,
5444 	.set_default_mgmt_key = brcmf_cfg80211_config_default_mgmt_key,
5445 	.set_power_mgmt = brcmf_cfg80211_set_power_mgmt,
5446 	.connect = brcmf_cfg80211_connect,
5447 	.disconnect = brcmf_cfg80211_disconnect,
5448 	.suspend = brcmf_cfg80211_suspend,
5449 	.resume = brcmf_cfg80211_resume,
5450 	.set_pmksa = brcmf_cfg80211_set_pmksa,
5451 	.del_pmksa = brcmf_cfg80211_del_pmksa,
5452 	.flush_pmksa = brcmf_cfg80211_flush_pmksa,
5453 	.start_ap = brcmf_cfg80211_start_ap,
5454 	.stop_ap = brcmf_cfg80211_stop_ap,
5455 	.change_beacon = brcmf_cfg80211_change_beacon,
5456 	.del_station = brcmf_cfg80211_del_station,
5457 	.change_station = brcmf_cfg80211_change_station,
5458 	.sched_scan_start = brcmf_cfg80211_sched_scan_start,
5459 	.sched_scan_stop = brcmf_cfg80211_sched_scan_stop,
5460 	.update_mgmt_frame_registrations =
5461 		brcmf_cfg80211_update_mgmt_frame_registrations,
5462 	.mgmt_tx = brcmf_cfg80211_mgmt_tx,
5463 	.remain_on_channel = brcmf_p2p_remain_on_channel,
5464 	.cancel_remain_on_channel = brcmf_cfg80211_cancel_remain_on_channel,
5465 	.get_channel = brcmf_cfg80211_get_channel,
5466 	.start_p2p_device = brcmf_p2p_start_device,
5467 	.stop_p2p_device = brcmf_p2p_stop_device,
5468 	.crit_proto_start = brcmf_cfg80211_crit_proto_start,
5469 	.crit_proto_stop = brcmf_cfg80211_crit_proto_stop,
5470 	.tdls_oper = brcmf_cfg80211_tdls_oper,
5471 	.update_connect_params = brcmf_cfg80211_update_conn_params,
5472 	.set_pmk = brcmf_cfg80211_set_pmk,
5473 	.del_pmk = brcmf_cfg80211_del_pmk,
5474 };
5475 
5476 struct cfg80211_ops *brcmf_cfg80211_get_ops(struct brcmf_mp_device *settings)
5477 {
5478 	struct cfg80211_ops *ops;
5479 
5480 	ops = kmemdup(&brcmf_cfg80211_ops, sizeof(brcmf_cfg80211_ops),
5481 		       GFP_KERNEL);
5482 
5483 	if (ops && settings->roamoff)
5484 		ops->update_connect_params = NULL;
5485 
5486 	return ops;
5487 }
5488 
5489 struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg,
5490 					   enum nl80211_iftype type)
5491 {
5492 	struct brcmf_cfg80211_vif *vif_walk;
5493 	struct brcmf_cfg80211_vif *vif;
5494 	bool mbss;
5495 	struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
5496 
5497 	brcmf_dbg(TRACE, "allocating virtual interface (size=%zu)\n",
5498 		  sizeof(*vif));
5499 	vif = kzalloc(sizeof(*vif), GFP_KERNEL);
5500 	if (!vif)
5501 		return ERR_PTR(-ENOMEM);
5502 
5503 	vif->wdev.wiphy = cfg->wiphy;
5504 	vif->wdev.iftype = type;
5505 
5506 	brcmf_init_prof(&vif->profile);
5507 
5508 	if (type == NL80211_IFTYPE_AP &&
5509 	    brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS)) {
5510 		mbss = false;
5511 		list_for_each_entry(vif_walk, &cfg->vif_list, list) {
5512 			if (vif_walk->wdev.iftype == NL80211_IFTYPE_AP) {
5513 				mbss = true;
5514 				break;
5515 			}
5516 		}
5517 		vif->mbss = mbss;
5518 	}
5519 
5520 	list_add_tail(&vif->list, &cfg->vif_list);
5521 	return vif;
5522 }
5523 
5524 void brcmf_free_vif(struct brcmf_cfg80211_vif *vif)
5525 {
5526 	list_del(&vif->list);
5527 	kfree(vif);
5528 }
5529 
5530 void brcmf_cfg80211_free_netdev(struct net_device *ndev)
5531 {
5532 	struct brcmf_cfg80211_vif *vif;
5533 	struct brcmf_if *ifp;
5534 
5535 	ifp = netdev_priv(ndev);
5536 	vif = ifp->vif;
5537 
5538 	if (vif)
5539 		brcmf_free_vif(vif);
5540 }
5541 
5542 static bool brcmf_is_linkup(struct brcmf_cfg80211_vif *vif,
5543 			    const struct brcmf_event_msg *e)
5544 {
5545 	u32 event = e->event_code;
5546 	u32 status = e->status;
5547 
5548 	if ((vif->profile.use_fwsup == BRCMF_PROFILE_FWSUP_PSK ||
5549 	     vif->profile.use_fwsup == BRCMF_PROFILE_FWSUP_SAE) &&
5550 	    event == BRCMF_E_PSK_SUP &&
5551 	    status == BRCMF_E_STATUS_FWSUP_COMPLETED)
5552 		set_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state);
5553 	if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
5554 		brcmf_dbg(CONN, "Processing set ssid\n");
5555 		memcpy(vif->profile.bssid, e->addr, ETH_ALEN);
5556 		if (vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_PSK &&
5557 		    vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_SAE)
5558 			return true;
5559 
5560 		set_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state);
5561 	}
5562 
5563 	if (test_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state) &&
5564 	    test_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state)) {
5565 		clear_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state);
5566 		clear_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state);
5567 		return true;
5568 	}
5569 	return false;
5570 }
5571 
5572 static bool brcmf_is_linkdown(const struct brcmf_event_msg *e)
5573 {
5574 	u32 event = e->event_code;
5575 	u16 flags = e->flags;
5576 
5577 	if ((event == BRCMF_E_DEAUTH) || (event == BRCMF_E_DEAUTH_IND) ||
5578 	    (event == BRCMF_E_DISASSOC_IND) ||
5579 	    ((event == BRCMF_E_LINK) && (!(flags & BRCMF_EVENT_MSG_LINK)))) {
5580 		brcmf_dbg(CONN, "Processing link down\n");
5581 		return true;
5582 	}
5583 	return false;
5584 }
5585 
5586 static bool brcmf_is_nonetwork(struct brcmf_cfg80211_info *cfg,
5587 			       const struct brcmf_event_msg *e)
5588 {
5589 	u32 event = e->event_code;
5590 	u32 status = e->status;
5591 
5592 	if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
5593 		brcmf_dbg(CONN, "Processing Link %s & no network found\n",
5594 			  e->flags & BRCMF_EVENT_MSG_LINK ? "up" : "down");
5595 		return true;
5596 	}
5597 
5598 	if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
5599 		brcmf_dbg(CONN, "Processing connecting & no network found\n");
5600 		return true;
5601 	}
5602 
5603 	if (event == BRCMF_E_PSK_SUP &&
5604 	    status != BRCMF_E_STATUS_FWSUP_COMPLETED) {
5605 		brcmf_dbg(CONN, "Processing failed supplicant state: %u\n",
5606 			  status);
5607 		return true;
5608 	}
5609 
5610 	return false;
5611 }
5612 
5613 static void brcmf_clear_assoc_ies(struct brcmf_cfg80211_info *cfg)
5614 {
5615 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5616 
5617 	kfree(conn_info->req_ie);
5618 	conn_info->req_ie = NULL;
5619 	conn_info->req_ie_len = 0;
5620 	kfree(conn_info->resp_ie);
5621 	conn_info->resp_ie = NULL;
5622 	conn_info->resp_ie_len = 0;
5623 }
5624 
5625 u8 brcmf_map_prio_to_prec(void *config, u8 prio)
5626 {
5627 	struct brcmf_cfg80211_info *cfg = (struct brcmf_cfg80211_info *)config;
5628 
5629 	if (!cfg)
5630 		return (prio == PRIO_8021D_NONE || prio == PRIO_8021D_BE) ?
5631 		       (prio ^ 2) : prio;
5632 
5633 	/* For those AC(s) with ACM flag set to 1, convert its 4-level priority
5634 	 * to an 8-level precedence which is the same as BE's
5635 	 */
5636 	if (prio > PRIO_8021D_EE &&
5637 	    cfg->ac_priority[prio] == cfg->ac_priority[PRIO_8021D_BE])
5638 		return cfg->ac_priority[prio] * 2;
5639 
5640 	/* Conversion of 4-level priority to 8-level precedence */
5641 	if (prio == PRIO_8021D_BE || prio == PRIO_8021D_BK ||
5642 	    prio == PRIO_8021D_CL || prio == PRIO_8021D_VO)
5643 		return cfg->ac_priority[prio] * 2;
5644 	else
5645 		return cfg->ac_priority[prio] * 2 + 1;
5646 }
5647 
5648 u8 brcmf_map_prio_to_aci(void *config, u8 prio)
5649 {
5650 	/* Prio here refers to the 802.1d priority in range of 0 to 7.
5651 	 * ACI here refers to the WLAN AC Index in range of 0 to 3.
5652 	 * This function will return ACI corresponding to input prio.
5653 	 */
5654 	struct brcmf_cfg80211_info *cfg = (struct brcmf_cfg80211_info *)config;
5655 
5656 	if (cfg)
5657 		return cfg->ac_priority[prio];
5658 
5659 	return prio;
5660 }
5661 
5662 static void brcmf_init_wmm_prio(u8 *priority)
5663 {
5664 	/* Initialize AC priority array to default
5665 	 * 802.1d priority as per following table:
5666 	 * 802.1d prio 0,3 maps to BE
5667 	 * 802.1d prio 1,2 maps to BK
5668 	 * 802.1d prio 4,5 maps to VI
5669 	 * 802.1d prio 6,7 maps to VO
5670 	 */
5671 	priority[0] = BRCMF_FWS_FIFO_AC_BE;
5672 	priority[3] = BRCMF_FWS_FIFO_AC_BE;
5673 	priority[1] = BRCMF_FWS_FIFO_AC_BK;
5674 	priority[2] = BRCMF_FWS_FIFO_AC_BK;
5675 	priority[4] = BRCMF_FWS_FIFO_AC_VI;
5676 	priority[5] = BRCMF_FWS_FIFO_AC_VI;
5677 	priority[6] = BRCMF_FWS_FIFO_AC_VO;
5678 	priority[7] = BRCMF_FWS_FIFO_AC_VO;
5679 }
5680 
5681 static void brcmf_wifi_prioritize_acparams(const
5682 	struct brcmf_cfg80211_edcf_acparam *acp, u8 *priority)
5683 {
5684 	u8 aci;
5685 	u8 aifsn;
5686 	u8 ecwmin;
5687 	u8 ecwmax;
5688 	u8 acm;
5689 	u8 ranking_basis[EDCF_AC_COUNT];
5690 	u8 aci_prio[EDCF_AC_COUNT]; /* AC_BE, AC_BK, AC_VI, AC_VO */
5691 	u8 index;
5692 
5693 	for (aci = 0; aci < EDCF_AC_COUNT; aci++, acp++) {
5694 		aifsn  = acp->ACI & EDCF_AIFSN_MASK;
5695 		acm = (acp->ACI & EDCF_ACM_MASK) ? 1 : 0;
5696 		ecwmin = acp->ECW & EDCF_ECWMIN_MASK;
5697 		ecwmax = (acp->ECW & EDCF_ECWMAX_MASK) >> EDCF_ECWMAX_SHIFT;
5698 		brcmf_dbg(CONN, "ACI %d aifsn %d acm %d ecwmin %d ecwmax %d\n",
5699 			  aci, aifsn, acm, ecwmin, ecwmax);
5700 		/* Default AC_VO will be the lowest ranking value */
5701 		ranking_basis[aci] = aifsn + ecwmin + ecwmax;
5702 		/* Initialise priority starting at 0 (AC_BE) */
5703 		aci_prio[aci] = 0;
5704 
5705 		/* If ACM is set, STA can't use this AC as per 802.11.
5706 		 * Change the ranking to BE
5707 		 */
5708 		if (aci != AC_BE && aci != AC_BK && acm == 1)
5709 			ranking_basis[aci] = ranking_basis[AC_BE];
5710 	}
5711 
5712 	/* Ranking method which works for AC priority
5713 	 * swapping when values for cwmin, cwmax and aifsn are varied
5714 	 * Compare each aci_prio against each other aci_prio
5715 	 */
5716 	for (aci = 0; aci < EDCF_AC_COUNT; aci++) {
5717 		for (index = 0; index < EDCF_AC_COUNT; index++) {
5718 			if (index != aci) {
5719 				/* Smaller ranking value has higher priority,
5720 				 * so increment priority for each ACI which has
5721 				 * a higher ranking value
5722 				 */
5723 				if (ranking_basis[aci] < ranking_basis[index])
5724 					aci_prio[aci]++;
5725 			}
5726 		}
5727 	}
5728 
5729 	/* By now, aci_prio[] will be in range of 0 to 3.
5730 	 * Use ACI prio to get the new priority value for
5731 	 * each 802.1d traffic type, in this range.
5732 	 */
5733 	if (!(aci_prio[AC_BE] == aci_prio[AC_BK] &&
5734 	      aci_prio[AC_BK] == aci_prio[AC_VI] &&
5735 	      aci_prio[AC_VI] == aci_prio[AC_VO])) {
5736 		/* 802.1d 0,3 maps to BE */
5737 		priority[0] = aci_prio[AC_BE];
5738 		priority[3] = aci_prio[AC_BE];
5739 
5740 		/* 802.1d 1,2 maps to BK */
5741 		priority[1] = aci_prio[AC_BK];
5742 		priority[2] = aci_prio[AC_BK];
5743 
5744 		/* 802.1d 4,5 maps to VO */
5745 		priority[4] = aci_prio[AC_VI];
5746 		priority[5] = aci_prio[AC_VI];
5747 
5748 		/* 802.1d 6,7 maps to VO */
5749 		priority[6] = aci_prio[AC_VO];
5750 		priority[7] = aci_prio[AC_VO];
5751 	} else {
5752 		/* Initialize to default priority */
5753 		brcmf_init_wmm_prio(priority);
5754 	}
5755 
5756 	brcmf_dbg(CONN, "Adj prio BE 0->%d, BK 1->%d, BK 2->%d, BE 3->%d\n",
5757 		  priority[0], priority[1], priority[2], priority[3]);
5758 
5759 	brcmf_dbg(CONN, "Adj prio VI 4->%d, VI 5->%d, VO 6->%d, VO 7->%d\n",
5760 		  priority[4], priority[5], priority[6], priority[7]);
5761 }
5762 
5763 static s32 brcmf_get_assoc_ies(struct brcmf_cfg80211_info *cfg,
5764 			       struct brcmf_if *ifp)
5765 {
5766 	struct brcmf_pub *drvr = cfg->pub;
5767 	struct brcmf_cfg80211_assoc_ielen_le *assoc_info;
5768 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5769 	struct brcmf_cfg80211_edcf_acparam edcf_acparam_info[EDCF_AC_COUNT];
5770 	u32 req_len;
5771 	u32 resp_len;
5772 	s32 err = 0;
5773 
5774 	brcmf_clear_assoc_ies(cfg);
5775 
5776 	err = brcmf_fil_iovar_data_get(ifp, "assoc_info",
5777 				       cfg->extra_buf, WL_ASSOC_INFO_MAX);
5778 	if (err) {
5779 		bphy_err(drvr, "could not get assoc info (%d)\n", err);
5780 		return err;
5781 	}
5782 	assoc_info =
5783 		(struct brcmf_cfg80211_assoc_ielen_le *)cfg->extra_buf;
5784 	req_len = le32_to_cpu(assoc_info->req_len);
5785 	resp_len = le32_to_cpu(assoc_info->resp_len);
5786 	if (req_len) {
5787 		err = brcmf_fil_iovar_data_get(ifp, "assoc_req_ies",
5788 					       cfg->extra_buf,
5789 					       WL_ASSOC_INFO_MAX);
5790 		if (err) {
5791 			bphy_err(drvr, "could not get assoc req (%d)\n", err);
5792 			return err;
5793 		}
5794 		conn_info->req_ie_len = req_len;
5795 		conn_info->req_ie =
5796 		    kmemdup(cfg->extra_buf, conn_info->req_ie_len,
5797 			    GFP_KERNEL);
5798 		if (!conn_info->req_ie)
5799 			conn_info->req_ie_len = 0;
5800 	} else {
5801 		conn_info->req_ie_len = 0;
5802 		conn_info->req_ie = NULL;
5803 	}
5804 	if (resp_len) {
5805 		err = brcmf_fil_iovar_data_get(ifp, "assoc_resp_ies",
5806 					       cfg->extra_buf,
5807 					       WL_ASSOC_INFO_MAX);
5808 		if (err) {
5809 			bphy_err(drvr, "could not get assoc resp (%d)\n", err);
5810 			return err;
5811 		}
5812 		conn_info->resp_ie_len = resp_len;
5813 		conn_info->resp_ie =
5814 		    kmemdup(cfg->extra_buf, conn_info->resp_ie_len,
5815 			    GFP_KERNEL);
5816 		if (!conn_info->resp_ie)
5817 			conn_info->resp_ie_len = 0;
5818 
5819 		err = brcmf_fil_iovar_data_get(ifp, "wme_ac_sta",
5820 					       edcf_acparam_info,
5821 					       sizeof(edcf_acparam_info));
5822 		if (err) {
5823 			brcmf_err("could not get wme_ac_sta (%d)\n", err);
5824 			return err;
5825 		}
5826 
5827 		brcmf_wifi_prioritize_acparams(edcf_acparam_info,
5828 					       cfg->ac_priority);
5829 	} else {
5830 		conn_info->resp_ie_len = 0;
5831 		conn_info->resp_ie = NULL;
5832 	}
5833 	brcmf_dbg(CONN, "req len (%d) resp len (%d)\n",
5834 		  conn_info->req_ie_len, conn_info->resp_ie_len);
5835 
5836 	return err;
5837 }
5838 
5839 static s32
5840 brcmf_bss_roaming_done(struct brcmf_cfg80211_info *cfg,
5841 		       struct net_device *ndev,
5842 		       const struct brcmf_event_msg *e)
5843 {
5844 	struct brcmf_if *ifp = netdev_priv(ndev);
5845 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5846 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5847 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
5848 	struct ieee80211_channel *notify_channel = NULL;
5849 	struct ieee80211_supported_band *band;
5850 	struct brcmf_bss_info_le *bi;
5851 	struct brcmu_chan ch;
5852 	struct cfg80211_roam_info roam_info = {};
5853 	u32 freq;
5854 	s32 err = 0;
5855 	u8 *buf;
5856 
5857 	brcmf_dbg(TRACE, "Enter\n");
5858 
5859 	brcmf_get_assoc_ies(cfg, ifp);
5860 	memcpy(profile->bssid, e->addr, ETH_ALEN);
5861 	brcmf_update_bss_info(cfg, ifp);
5862 
5863 	buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
5864 	if (buf == NULL) {
5865 		err = -ENOMEM;
5866 		goto done;
5867 	}
5868 
5869 	/* data sent to dongle has to be little endian */
5870 	*(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
5871 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
5872 				     buf, WL_BSS_INFO_MAX);
5873 
5874 	if (err)
5875 		goto done;
5876 
5877 	bi = (struct brcmf_bss_info_le *)(buf + 4);
5878 	ch.chspec = le16_to_cpu(bi->chanspec);
5879 	cfg->d11inf.decchspec(&ch);
5880 
5881 	if (ch.band == BRCMU_CHAN_BAND_2G)
5882 		band = wiphy->bands[NL80211_BAND_2GHZ];
5883 	else
5884 		band = wiphy->bands[NL80211_BAND_5GHZ];
5885 
5886 	freq = ieee80211_channel_to_frequency(ch.control_ch_num, band->band);
5887 	notify_channel = ieee80211_get_channel(wiphy, freq);
5888 
5889 done:
5890 	kfree(buf);
5891 
5892 	roam_info.channel = notify_channel;
5893 	roam_info.bssid = profile->bssid;
5894 	roam_info.req_ie = conn_info->req_ie;
5895 	roam_info.req_ie_len = conn_info->req_ie_len;
5896 	roam_info.resp_ie = conn_info->resp_ie;
5897 	roam_info.resp_ie_len = conn_info->resp_ie_len;
5898 
5899 	cfg80211_roamed(ndev, &roam_info, GFP_KERNEL);
5900 	brcmf_dbg(CONN, "Report roaming result\n");
5901 
5902 	if (profile->use_fwsup == BRCMF_PROFILE_FWSUP_1X && profile->is_ft) {
5903 		cfg80211_port_authorized(ndev, profile->bssid, GFP_KERNEL);
5904 		brcmf_dbg(CONN, "Report port authorized\n");
5905 	}
5906 
5907 	set_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
5908 	brcmf_dbg(TRACE, "Exit\n");
5909 	return err;
5910 }
5911 
5912 static s32
5913 brcmf_bss_connect_done(struct brcmf_cfg80211_info *cfg,
5914 		       struct net_device *ndev, const struct brcmf_event_msg *e,
5915 		       bool completed)
5916 {
5917 	struct brcmf_if *ifp = netdev_priv(ndev);
5918 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5919 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5920 	struct cfg80211_connect_resp_params conn_params;
5921 
5922 	brcmf_dbg(TRACE, "Enter\n");
5923 
5924 	if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING,
5925 			       &ifp->vif->sme_state)) {
5926 		memset(&conn_params, 0, sizeof(conn_params));
5927 		if (completed) {
5928 			brcmf_get_assoc_ies(cfg, ifp);
5929 			brcmf_update_bss_info(cfg, ifp);
5930 			set_bit(BRCMF_VIF_STATUS_CONNECTED,
5931 				&ifp->vif->sme_state);
5932 			conn_params.status = WLAN_STATUS_SUCCESS;
5933 		} else {
5934 			conn_params.status = WLAN_STATUS_AUTH_TIMEOUT;
5935 		}
5936 		conn_params.bssid = profile->bssid;
5937 		conn_params.req_ie = conn_info->req_ie;
5938 		conn_params.req_ie_len = conn_info->req_ie_len;
5939 		conn_params.resp_ie = conn_info->resp_ie;
5940 		conn_params.resp_ie_len = conn_info->resp_ie_len;
5941 		cfg80211_connect_done(ndev, &conn_params, GFP_KERNEL);
5942 		brcmf_dbg(CONN, "Report connect result - connection %s\n",
5943 			  completed ? "succeeded" : "failed");
5944 	}
5945 	brcmf_dbg(TRACE, "Exit\n");
5946 	return 0;
5947 }
5948 
5949 static s32
5950 brcmf_notify_connect_status_ap(struct brcmf_cfg80211_info *cfg,
5951 			       struct net_device *ndev,
5952 			       const struct brcmf_event_msg *e, void *data)
5953 {
5954 	struct brcmf_pub *drvr = cfg->pub;
5955 	static int generation;
5956 	u32 event = e->event_code;
5957 	u32 reason = e->reason;
5958 	struct station_info *sinfo;
5959 
5960 	brcmf_dbg(CONN, "event %s (%u), reason %d\n",
5961 		  brcmf_fweh_event_name(event), event, reason);
5962 	if (event == BRCMF_E_LINK && reason == BRCMF_E_REASON_LINK_BSSCFG_DIS &&
5963 	    ndev != cfg_to_ndev(cfg)) {
5964 		brcmf_dbg(CONN, "AP mode link down\n");
5965 		complete(&cfg->vif_disabled);
5966 		return 0;
5967 	}
5968 
5969 	if (((event == BRCMF_E_ASSOC_IND) || (event == BRCMF_E_REASSOC_IND)) &&
5970 	    (reason == BRCMF_E_STATUS_SUCCESS)) {
5971 		if (!data) {
5972 			bphy_err(drvr, "No IEs present in ASSOC/REASSOC_IND\n");
5973 			return -EINVAL;
5974 		}
5975 
5976 		sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
5977 		if (!sinfo)
5978 			return -ENOMEM;
5979 
5980 		sinfo->assoc_req_ies = data;
5981 		sinfo->assoc_req_ies_len = e->datalen;
5982 		generation++;
5983 		sinfo->generation = generation;
5984 		cfg80211_new_sta(ndev, e->addr, sinfo, GFP_KERNEL);
5985 
5986 		kfree(sinfo);
5987 	} else if ((event == BRCMF_E_DISASSOC_IND) ||
5988 		   (event == BRCMF_E_DEAUTH_IND) ||
5989 		   (event == BRCMF_E_DEAUTH)) {
5990 		cfg80211_del_sta(ndev, e->addr, GFP_KERNEL);
5991 	}
5992 	return 0;
5993 }
5994 
5995 static s32
5996 brcmf_notify_connect_status(struct brcmf_if *ifp,
5997 			    const struct brcmf_event_msg *e, void *data)
5998 {
5999 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
6000 	struct net_device *ndev = ifp->ndev;
6001 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
6002 	struct ieee80211_channel *chan;
6003 	s32 err = 0;
6004 
6005 	if ((e->event_code == BRCMF_E_DEAUTH) ||
6006 	    (e->event_code == BRCMF_E_DEAUTH_IND) ||
6007 	    (e->event_code == BRCMF_E_DISASSOC_IND) ||
6008 	    ((e->event_code == BRCMF_E_LINK) && (!e->flags))) {
6009 		brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
6010 	}
6011 
6012 	if (brcmf_is_apmode(ifp->vif)) {
6013 		err = brcmf_notify_connect_status_ap(cfg, ndev, e, data);
6014 	} else if (brcmf_is_linkup(ifp->vif, e)) {
6015 		brcmf_dbg(CONN, "Linkup\n");
6016 		if (brcmf_is_ibssmode(ifp->vif)) {
6017 			brcmf_inform_ibss(cfg, ndev, e->addr);
6018 			chan = ieee80211_get_channel(cfg->wiphy, cfg->channel);
6019 			memcpy(profile->bssid, e->addr, ETH_ALEN);
6020 			cfg80211_ibss_joined(ndev, e->addr, chan, GFP_KERNEL);
6021 			clear_bit(BRCMF_VIF_STATUS_CONNECTING,
6022 				  &ifp->vif->sme_state);
6023 			set_bit(BRCMF_VIF_STATUS_CONNECTED,
6024 				&ifp->vif->sme_state);
6025 		} else
6026 			brcmf_bss_connect_done(cfg, ndev, e, true);
6027 		brcmf_net_setcarrier(ifp, true);
6028 	} else if (brcmf_is_linkdown(e)) {
6029 		brcmf_dbg(CONN, "Linkdown\n");
6030 		if (!brcmf_is_ibssmode(ifp->vif) &&
6031 		    test_bit(BRCMF_VIF_STATUS_CONNECTED,
6032 			     &ifp->vif->sme_state)) {
6033 			if (memcmp(profile->bssid, e->addr, ETH_ALEN))
6034 				return err;
6035 
6036 			brcmf_bss_connect_done(cfg, ndev, e, false);
6037 			brcmf_link_down(ifp->vif,
6038 					brcmf_map_fw_linkdown_reason(e),
6039 					e->event_code &
6040 					(BRCMF_E_DEAUTH_IND |
6041 					BRCMF_E_DISASSOC_IND)
6042 					? false : true);
6043 			brcmf_init_prof(ndev_to_prof(ndev));
6044 			if (ndev != cfg_to_ndev(cfg))
6045 				complete(&cfg->vif_disabled);
6046 			brcmf_net_setcarrier(ifp, false);
6047 		}
6048 	} else if (brcmf_is_nonetwork(cfg, e)) {
6049 		if (brcmf_is_ibssmode(ifp->vif))
6050 			clear_bit(BRCMF_VIF_STATUS_CONNECTING,
6051 				  &ifp->vif->sme_state);
6052 		else
6053 			brcmf_bss_connect_done(cfg, ndev, e, false);
6054 	}
6055 
6056 	return err;
6057 }
6058 
6059 static s32
6060 brcmf_notify_roaming_status(struct brcmf_if *ifp,
6061 			    const struct brcmf_event_msg *e, void *data)
6062 {
6063 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
6064 	u32 event = e->event_code;
6065 	u32 status = e->status;
6066 
6067 	if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
6068 		if (test_bit(BRCMF_VIF_STATUS_CONNECTED,
6069 			     &ifp->vif->sme_state)) {
6070 			brcmf_bss_roaming_done(cfg, ifp->ndev, e);
6071 		} else {
6072 			brcmf_bss_connect_done(cfg, ifp->ndev, e, true);
6073 			brcmf_net_setcarrier(ifp, true);
6074 		}
6075 	}
6076 
6077 	return 0;
6078 }
6079 
6080 static s32
6081 brcmf_notify_mic_status(struct brcmf_if *ifp,
6082 			const struct brcmf_event_msg *e, void *data)
6083 {
6084 	u16 flags = e->flags;
6085 	enum nl80211_key_type key_type;
6086 
6087 	if (flags & BRCMF_EVENT_MSG_GROUP)
6088 		key_type = NL80211_KEYTYPE_GROUP;
6089 	else
6090 		key_type = NL80211_KEYTYPE_PAIRWISE;
6091 
6092 	cfg80211_michael_mic_failure(ifp->ndev, (u8 *)&e->addr, key_type, -1,
6093 				     NULL, GFP_KERNEL);
6094 
6095 	return 0;
6096 }
6097 
6098 static s32 brcmf_notify_vif_event(struct brcmf_if *ifp,
6099 				  const struct brcmf_event_msg *e, void *data)
6100 {
6101 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
6102 	struct brcmf_if_event *ifevent = (struct brcmf_if_event *)data;
6103 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
6104 	struct brcmf_cfg80211_vif *vif;
6105 
6106 	brcmf_dbg(TRACE, "Enter: action %u flags %u ifidx %u bsscfgidx %u\n",
6107 		  ifevent->action, ifevent->flags, ifevent->ifidx,
6108 		  ifevent->bsscfgidx);
6109 
6110 	spin_lock(&event->vif_event_lock);
6111 	event->action = ifevent->action;
6112 	vif = event->vif;
6113 
6114 	switch (ifevent->action) {
6115 	case BRCMF_E_IF_ADD:
6116 		/* waiting process may have timed out */
6117 		if (!cfg->vif_event.vif) {
6118 			spin_unlock(&event->vif_event_lock);
6119 			return -EBADF;
6120 		}
6121 
6122 		ifp->vif = vif;
6123 		vif->ifp = ifp;
6124 		if (ifp->ndev) {
6125 			vif->wdev.netdev = ifp->ndev;
6126 			ifp->ndev->ieee80211_ptr = &vif->wdev;
6127 			SET_NETDEV_DEV(ifp->ndev, wiphy_dev(cfg->wiphy));
6128 		}
6129 		spin_unlock(&event->vif_event_lock);
6130 		wake_up(&event->vif_wq);
6131 		return 0;
6132 
6133 	case BRCMF_E_IF_DEL:
6134 		spin_unlock(&event->vif_event_lock);
6135 		/* event may not be upon user request */
6136 		if (brcmf_cfg80211_vif_event_armed(cfg))
6137 			wake_up(&event->vif_wq);
6138 		return 0;
6139 
6140 	case BRCMF_E_IF_CHANGE:
6141 		spin_unlock(&event->vif_event_lock);
6142 		wake_up(&event->vif_wq);
6143 		return 0;
6144 
6145 	default:
6146 		spin_unlock(&event->vif_event_lock);
6147 		break;
6148 	}
6149 	return -EINVAL;
6150 }
6151 
6152 static void brcmf_init_conf(struct brcmf_cfg80211_conf *conf)
6153 {
6154 	conf->frag_threshold = (u32)-1;
6155 	conf->rts_threshold = (u32)-1;
6156 	conf->retry_short = (u32)-1;
6157 	conf->retry_long = (u32)-1;
6158 }
6159 
6160 static void brcmf_register_event_handlers(struct brcmf_cfg80211_info *cfg)
6161 {
6162 	brcmf_fweh_register(cfg->pub, BRCMF_E_LINK,
6163 			    brcmf_notify_connect_status);
6164 	brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH_IND,
6165 			    brcmf_notify_connect_status);
6166 	brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH,
6167 			    brcmf_notify_connect_status);
6168 	brcmf_fweh_register(cfg->pub, BRCMF_E_DISASSOC_IND,
6169 			    brcmf_notify_connect_status);
6170 	brcmf_fweh_register(cfg->pub, BRCMF_E_ASSOC_IND,
6171 			    brcmf_notify_connect_status);
6172 	brcmf_fweh_register(cfg->pub, BRCMF_E_REASSOC_IND,
6173 			    brcmf_notify_connect_status);
6174 	brcmf_fweh_register(cfg->pub, BRCMF_E_ROAM,
6175 			    brcmf_notify_roaming_status);
6176 	brcmf_fweh_register(cfg->pub, BRCMF_E_MIC_ERROR,
6177 			    brcmf_notify_mic_status);
6178 	brcmf_fweh_register(cfg->pub, BRCMF_E_SET_SSID,
6179 			    brcmf_notify_connect_status);
6180 	brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
6181 			    brcmf_notify_sched_scan_results);
6182 	brcmf_fweh_register(cfg->pub, BRCMF_E_IF,
6183 			    brcmf_notify_vif_event);
6184 	brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_PROBEREQ_MSG,
6185 			    brcmf_p2p_notify_rx_mgmt_p2p_probereq);
6186 	brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_DISC_LISTEN_COMPLETE,
6187 			    brcmf_p2p_notify_listen_complete);
6188 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_RX,
6189 			    brcmf_p2p_notify_action_frame_rx);
6190 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_COMPLETE,
6191 			    brcmf_p2p_notify_action_tx_complete);
6192 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE,
6193 			    brcmf_p2p_notify_action_tx_complete);
6194 	brcmf_fweh_register(cfg->pub, BRCMF_E_PSK_SUP,
6195 			    brcmf_notify_connect_status);
6196 }
6197 
6198 static void brcmf_deinit_priv_mem(struct brcmf_cfg80211_info *cfg)
6199 {
6200 	kfree(cfg->conf);
6201 	cfg->conf = NULL;
6202 	kfree(cfg->extra_buf);
6203 	cfg->extra_buf = NULL;
6204 	kfree(cfg->wowl.nd);
6205 	cfg->wowl.nd = NULL;
6206 	kfree(cfg->wowl.nd_info);
6207 	cfg->wowl.nd_info = NULL;
6208 	kfree(cfg->escan_info.escan_buf);
6209 	cfg->escan_info.escan_buf = NULL;
6210 }
6211 
6212 static s32 brcmf_init_priv_mem(struct brcmf_cfg80211_info *cfg)
6213 {
6214 	cfg->conf = kzalloc(sizeof(*cfg->conf), GFP_KERNEL);
6215 	if (!cfg->conf)
6216 		goto init_priv_mem_out;
6217 	cfg->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
6218 	if (!cfg->extra_buf)
6219 		goto init_priv_mem_out;
6220 	cfg->wowl.nd = kzalloc(sizeof(*cfg->wowl.nd) + sizeof(u32), GFP_KERNEL);
6221 	if (!cfg->wowl.nd)
6222 		goto init_priv_mem_out;
6223 	cfg->wowl.nd_info = kzalloc(sizeof(*cfg->wowl.nd_info) +
6224 				    sizeof(struct cfg80211_wowlan_nd_match *),
6225 				    GFP_KERNEL);
6226 	if (!cfg->wowl.nd_info)
6227 		goto init_priv_mem_out;
6228 	cfg->escan_info.escan_buf = kzalloc(BRCMF_ESCAN_BUF_SIZE, GFP_KERNEL);
6229 	if (!cfg->escan_info.escan_buf)
6230 		goto init_priv_mem_out;
6231 
6232 	return 0;
6233 
6234 init_priv_mem_out:
6235 	brcmf_deinit_priv_mem(cfg);
6236 
6237 	return -ENOMEM;
6238 }
6239 
6240 static s32 wl_init_priv(struct brcmf_cfg80211_info *cfg)
6241 {
6242 	s32 err = 0;
6243 
6244 	cfg->scan_request = NULL;
6245 	cfg->pwr_save = true;
6246 	cfg->dongle_up = false;		/* dongle is not up yet */
6247 	err = brcmf_init_priv_mem(cfg);
6248 	if (err)
6249 		return err;
6250 	brcmf_register_event_handlers(cfg);
6251 	mutex_init(&cfg->usr_sync);
6252 	brcmf_init_escan(cfg);
6253 	brcmf_init_conf(cfg->conf);
6254 	brcmf_init_wmm_prio(cfg->ac_priority);
6255 	init_completion(&cfg->vif_disabled);
6256 	return err;
6257 }
6258 
6259 static void wl_deinit_priv(struct brcmf_cfg80211_info *cfg)
6260 {
6261 	cfg->dongle_up = false;	/* dongle down */
6262 	brcmf_abort_scanning(cfg);
6263 	brcmf_deinit_priv_mem(cfg);
6264 }
6265 
6266 static void init_vif_event(struct brcmf_cfg80211_vif_event *event)
6267 {
6268 	init_waitqueue_head(&event->vif_wq);
6269 	spin_lock_init(&event->vif_event_lock);
6270 }
6271 
6272 static s32 brcmf_dongle_roam(struct brcmf_if *ifp)
6273 {
6274 	struct brcmf_pub *drvr = ifp->drvr;
6275 	s32 err;
6276 	u32 bcn_timeout;
6277 	__le32 roamtrigger[2];
6278 	__le32 roam_delta[2];
6279 
6280 	/* Configure beacon timeout value based upon roaming setting */
6281 	if (ifp->drvr->settings->roamoff)
6282 		bcn_timeout = BRCMF_DEFAULT_BCN_TIMEOUT_ROAM_OFF;
6283 	else
6284 		bcn_timeout = BRCMF_DEFAULT_BCN_TIMEOUT_ROAM_ON;
6285 	err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout", bcn_timeout);
6286 	if (err) {
6287 		bphy_err(drvr, "bcn_timeout error (%d)\n", err);
6288 		goto roam_setup_done;
6289 	}
6290 
6291 	/* Enable/Disable built-in roaming to allow supplicant to take care of
6292 	 * roaming.
6293 	 */
6294 	brcmf_dbg(INFO, "Internal Roaming = %s\n",
6295 		  ifp->drvr->settings->roamoff ? "Off" : "On");
6296 	err = brcmf_fil_iovar_int_set(ifp, "roam_off",
6297 				      ifp->drvr->settings->roamoff);
6298 	if (err) {
6299 		bphy_err(drvr, "roam_off error (%d)\n", err);
6300 		goto roam_setup_done;
6301 	}
6302 
6303 	roamtrigger[0] = cpu_to_le32(WL_ROAM_TRIGGER_LEVEL);
6304 	roamtrigger[1] = cpu_to_le32(BRCM_BAND_ALL);
6305 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_TRIGGER,
6306 				     (void *)roamtrigger, sizeof(roamtrigger));
6307 	if (err)
6308 		bphy_err(drvr, "WLC_SET_ROAM_TRIGGER error (%d)\n", err);
6309 
6310 	roam_delta[0] = cpu_to_le32(WL_ROAM_DELTA);
6311 	roam_delta[1] = cpu_to_le32(BRCM_BAND_ALL);
6312 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_DELTA,
6313 				     (void *)roam_delta, sizeof(roam_delta));
6314 	if (err)
6315 		bphy_err(drvr, "WLC_SET_ROAM_DELTA error (%d)\n", err);
6316 
6317 	return 0;
6318 
6319 roam_setup_done:
6320 	return err;
6321 }
6322 
6323 static s32
6324 brcmf_dongle_scantime(struct brcmf_if *ifp)
6325 {
6326 	struct brcmf_pub *drvr = ifp->drvr;
6327 	s32 err = 0;
6328 
6329 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
6330 				    BRCMF_SCAN_CHANNEL_TIME);
6331 	if (err) {
6332 		bphy_err(drvr, "Scan assoc time error (%d)\n", err);
6333 		goto dongle_scantime_out;
6334 	}
6335 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
6336 				    BRCMF_SCAN_UNASSOC_TIME);
6337 	if (err) {
6338 		bphy_err(drvr, "Scan unassoc time error (%d)\n", err);
6339 		goto dongle_scantime_out;
6340 	}
6341 
6342 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_PASSIVE_TIME,
6343 				    BRCMF_SCAN_PASSIVE_TIME);
6344 	if (err) {
6345 		bphy_err(drvr, "Scan passive time error (%d)\n", err);
6346 		goto dongle_scantime_out;
6347 	}
6348 
6349 dongle_scantime_out:
6350 	return err;
6351 }
6352 
6353 static void brcmf_update_bw40_channel_flag(struct ieee80211_channel *channel,
6354 					   struct brcmu_chan *ch)
6355 {
6356 	u32 ht40_flag;
6357 
6358 	ht40_flag = channel->flags & IEEE80211_CHAN_NO_HT40;
6359 	if (ch->sb == BRCMU_CHAN_SB_U) {
6360 		if (ht40_flag == IEEE80211_CHAN_NO_HT40)
6361 			channel->flags &= ~IEEE80211_CHAN_NO_HT40;
6362 		channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
6363 	} else {
6364 		/* It should be one of
6365 		 * IEEE80211_CHAN_NO_HT40 or
6366 		 * IEEE80211_CHAN_NO_HT40PLUS
6367 		 */
6368 		channel->flags &= ~IEEE80211_CHAN_NO_HT40;
6369 		if (ht40_flag == IEEE80211_CHAN_NO_HT40)
6370 			channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
6371 	}
6372 }
6373 
6374 static int brcmf_construct_chaninfo(struct brcmf_cfg80211_info *cfg,
6375 				    u32 bw_cap[])
6376 {
6377 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
6378 	struct brcmf_pub *drvr = cfg->pub;
6379 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6380 	struct ieee80211_supported_band *band;
6381 	struct ieee80211_channel *channel;
6382 	struct brcmf_chanspec_list *list;
6383 	struct brcmu_chan ch;
6384 	int err;
6385 	u8 *pbuf;
6386 	u32 i, j;
6387 	u32 total;
6388 	u32 chaninfo;
6389 
6390 	pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
6391 
6392 	if (pbuf == NULL)
6393 		return -ENOMEM;
6394 
6395 	list = (struct brcmf_chanspec_list *)pbuf;
6396 
6397 	err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
6398 				       BRCMF_DCMD_MEDLEN);
6399 	if (err) {
6400 		bphy_err(drvr, "get chanspecs error (%d)\n", err);
6401 		goto fail_pbuf;
6402 	}
6403 
6404 	band = wiphy->bands[NL80211_BAND_2GHZ];
6405 	if (band)
6406 		for (i = 0; i < band->n_channels; i++)
6407 			band->channels[i].flags = IEEE80211_CHAN_DISABLED;
6408 	band = wiphy->bands[NL80211_BAND_5GHZ];
6409 	if (band)
6410 		for (i = 0; i < band->n_channels; i++)
6411 			band->channels[i].flags = IEEE80211_CHAN_DISABLED;
6412 
6413 	total = le32_to_cpu(list->count);
6414 	for (i = 0; i < total; i++) {
6415 		ch.chspec = (u16)le32_to_cpu(list->element[i]);
6416 		cfg->d11inf.decchspec(&ch);
6417 
6418 		if (ch.band == BRCMU_CHAN_BAND_2G) {
6419 			band = wiphy->bands[NL80211_BAND_2GHZ];
6420 		} else if (ch.band == BRCMU_CHAN_BAND_5G) {
6421 			band = wiphy->bands[NL80211_BAND_5GHZ];
6422 		} else {
6423 			bphy_err(drvr, "Invalid channel Spec. 0x%x.\n",
6424 				 ch.chspec);
6425 			continue;
6426 		}
6427 		if (!band)
6428 			continue;
6429 		if (!(bw_cap[band->band] & WLC_BW_40MHZ_BIT) &&
6430 		    ch.bw == BRCMU_CHAN_BW_40)
6431 			continue;
6432 		if (!(bw_cap[band->band] & WLC_BW_80MHZ_BIT) &&
6433 		    ch.bw == BRCMU_CHAN_BW_80)
6434 			continue;
6435 
6436 		channel = NULL;
6437 		for (j = 0; j < band->n_channels; j++) {
6438 			if (band->channels[j].hw_value == ch.control_ch_num) {
6439 				channel = &band->channels[j];
6440 				break;
6441 			}
6442 		}
6443 		if (!channel) {
6444 			/* It seems firmware supports some channel we never
6445 			 * considered. Something new in IEEE standard?
6446 			 */
6447 			bphy_err(drvr, "Ignoring unexpected firmware channel %d\n",
6448 				 ch.control_ch_num);
6449 			continue;
6450 		}
6451 
6452 		if (channel->orig_flags & IEEE80211_CHAN_DISABLED)
6453 			continue;
6454 
6455 		/* assuming the chanspecs order is HT20,
6456 		 * HT40 upper, HT40 lower, and VHT80.
6457 		 */
6458 		switch (ch.bw) {
6459 		case BRCMU_CHAN_BW_160:
6460 			channel->flags &= ~IEEE80211_CHAN_NO_160MHZ;
6461 			break;
6462 		case BRCMU_CHAN_BW_80:
6463 			channel->flags &= ~IEEE80211_CHAN_NO_80MHZ;
6464 			break;
6465 		case BRCMU_CHAN_BW_40:
6466 			brcmf_update_bw40_channel_flag(channel, &ch);
6467 			break;
6468 		default:
6469 			wiphy_warn(wiphy, "Firmware reported unsupported bandwidth %d\n",
6470 				   ch.bw);
6471 			/* fall through */
6472 		case BRCMU_CHAN_BW_20:
6473 			/* enable the channel and disable other bandwidths
6474 			 * for now as mentioned order assure they are enabled
6475 			 * for subsequent chanspecs.
6476 			 */
6477 			channel->flags = IEEE80211_CHAN_NO_HT40 |
6478 					 IEEE80211_CHAN_NO_80MHZ |
6479 					 IEEE80211_CHAN_NO_160MHZ;
6480 			ch.bw = BRCMU_CHAN_BW_20;
6481 			cfg->d11inf.encchspec(&ch);
6482 			chaninfo = ch.chspec;
6483 			err = brcmf_fil_bsscfg_int_get(ifp, "per_chan_info",
6484 						       &chaninfo);
6485 			if (!err) {
6486 				if (chaninfo & WL_CHAN_RADAR)
6487 					channel->flags |=
6488 						(IEEE80211_CHAN_RADAR |
6489 						 IEEE80211_CHAN_NO_IR);
6490 				if (chaninfo & WL_CHAN_PASSIVE)
6491 					channel->flags |=
6492 						IEEE80211_CHAN_NO_IR;
6493 			}
6494 		}
6495 	}
6496 
6497 fail_pbuf:
6498 	kfree(pbuf);
6499 	return err;
6500 }
6501 
6502 static int brcmf_enable_bw40_2g(struct brcmf_cfg80211_info *cfg)
6503 {
6504 	struct brcmf_pub *drvr = cfg->pub;
6505 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6506 	struct ieee80211_supported_band *band;
6507 	struct brcmf_fil_bwcap_le band_bwcap;
6508 	struct brcmf_chanspec_list *list;
6509 	u8 *pbuf;
6510 	u32 val;
6511 	int err;
6512 	struct brcmu_chan ch;
6513 	u32 num_chan;
6514 	int i, j;
6515 
6516 	/* verify support for bw_cap command */
6517 	val = WLC_BAND_5G;
6518 	err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &val);
6519 
6520 	if (!err) {
6521 		/* only set 2G bandwidth using bw_cap command */
6522 		band_bwcap.band = cpu_to_le32(WLC_BAND_2G);
6523 		band_bwcap.bw_cap = cpu_to_le32(WLC_BW_CAP_40MHZ);
6524 		err = brcmf_fil_iovar_data_set(ifp, "bw_cap", &band_bwcap,
6525 					       sizeof(band_bwcap));
6526 	} else {
6527 		brcmf_dbg(INFO, "fallback to mimo_bw_cap\n");
6528 		val = WLC_N_BW_40ALL;
6529 		err = brcmf_fil_iovar_int_set(ifp, "mimo_bw_cap", val);
6530 	}
6531 
6532 	if (!err) {
6533 		/* update channel info in 2G band */
6534 		pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
6535 
6536 		if (pbuf == NULL)
6537 			return -ENOMEM;
6538 
6539 		ch.band = BRCMU_CHAN_BAND_2G;
6540 		ch.bw = BRCMU_CHAN_BW_40;
6541 		ch.sb = BRCMU_CHAN_SB_NONE;
6542 		ch.chnum = 0;
6543 		cfg->d11inf.encchspec(&ch);
6544 
6545 		/* pass encoded chanspec in query */
6546 		*(__le16 *)pbuf = cpu_to_le16(ch.chspec);
6547 
6548 		err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
6549 					       BRCMF_DCMD_MEDLEN);
6550 		if (err) {
6551 			bphy_err(drvr, "get chanspecs error (%d)\n", err);
6552 			kfree(pbuf);
6553 			return err;
6554 		}
6555 
6556 		band = cfg_to_wiphy(cfg)->bands[NL80211_BAND_2GHZ];
6557 		list = (struct brcmf_chanspec_list *)pbuf;
6558 		num_chan = le32_to_cpu(list->count);
6559 		for (i = 0; i < num_chan; i++) {
6560 			ch.chspec = (u16)le32_to_cpu(list->element[i]);
6561 			cfg->d11inf.decchspec(&ch);
6562 			if (WARN_ON(ch.band != BRCMU_CHAN_BAND_2G))
6563 				continue;
6564 			if (WARN_ON(ch.bw != BRCMU_CHAN_BW_40))
6565 				continue;
6566 			for (j = 0; j < band->n_channels; j++) {
6567 				if (band->channels[j].hw_value == ch.control_ch_num)
6568 					break;
6569 			}
6570 			if (WARN_ON(j == band->n_channels))
6571 				continue;
6572 
6573 			brcmf_update_bw40_channel_flag(&band->channels[j], &ch);
6574 		}
6575 		kfree(pbuf);
6576 	}
6577 	return err;
6578 }
6579 
6580 static void brcmf_get_bwcap(struct brcmf_if *ifp, u32 bw_cap[])
6581 {
6582 	struct brcmf_pub *drvr = ifp->drvr;
6583 	u32 band, mimo_bwcap;
6584 	int err;
6585 
6586 	band = WLC_BAND_2G;
6587 	err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
6588 	if (!err) {
6589 		bw_cap[NL80211_BAND_2GHZ] = band;
6590 		band = WLC_BAND_5G;
6591 		err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
6592 		if (!err) {
6593 			bw_cap[NL80211_BAND_5GHZ] = band;
6594 			return;
6595 		}
6596 		WARN_ON(1);
6597 		return;
6598 	}
6599 	brcmf_dbg(INFO, "fallback to mimo_bw_cap info\n");
6600 	mimo_bwcap = 0;
6601 	err = brcmf_fil_iovar_int_get(ifp, "mimo_bw_cap", &mimo_bwcap);
6602 	if (err)
6603 		/* assume 20MHz if firmware does not give a clue */
6604 		mimo_bwcap = WLC_N_BW_20ALL;
6605 
6606 	switch (mimo_bwcap) {
6607 	case WLC_N_BW_40ALL:
6608 		bw_cap[NL80211_BAND_2GHZ] |= WLC_BW_40MHZ_BIT;
6609 		/* fall-thru */
6610 	case WLC_N_BW_20IN2G_40IN5G:
6611 		bw_cap[NL80211_BAND_5GHZ] |= WLC_BW_40MHZ_BIT;
6612 		/* fall-thru */
6613 	case WLC_N_BW_20ALL:
6614 		bw_cap[NL80211_BAND_2GHZ] |= WLC_BW_20MHZ_BIT;
6615 		bw_cap[NL80211_BAND_5GHZ] |= WLC_BW_20MHZ_BIT;
6616 		break;
6617 	default:
6618 		bphy_err(drvr, "invalid mimo_bw_cap value\n");
6619 	}
6620 }
6621 
6622 static void brcmf_update_ht_cap(struct ieee80211_supported_band *band,
6623 				u32 bw_cap[2], u32 nchain)
6624 {
6625 	band->ht_cap.ht_supported = true;
6626 	if (bw_cap[band->band] & WLC_BW_40MHZ_BIT) {
6627 		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
6628 		band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
6629 	}
6630 	band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
6631 	band->ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
6632 	band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
6633 	band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
6634 	memset(band->ht_cap.mcs.rx_mask, 0xff, nchain);
6635 	band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
6636 }
6637 
6638 static __le16 brcmf_get_mcs_map(u32 nchain, enum ieee80211_vht_mcs_support supp)
6639 {
6640 	u16 mcs_map;
6641 	int i;
6642 
6643 	for (i = 0, mcs_map = 0xFFFF; i < nchain; i++)
6644 		mcs_map = (mcs_map << 2) | supp;
6645 
6646 	return cpu_to_le16(mcs_map);
6647 }
6648 
6649 static void brcmf_update_vht_cap(struct ieee80211_supported_band *band,
6650 				 u32 bw_cap[2], u32 nchain, u32 txstreams,
6651 				 u32 txbf_bfe_cap, u32 txbf_bfr_cap)
6652 {
6653 	__le16 mcs_map;
6654 
6655 	/* not allowed in 2.4G band */
6656 	if (band->band == NL80211_BAND_2GHZ)
6657 		return;
6658 
6659 	band->vht_cap.vht_supported = true;
6660 	/* 80MHz is mandatory */
6661 	band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_80;
6662 	if (bw_cap[band->band] & WLC_BW_160MHZ_BIT) {
6663 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
6664 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_160;
6665 	}
6666 	/* all support 256-QAM */
6667 	mcs_map = brcmf_get_mcs_map(nchain, IEEE80211_VHT_MCS_SUPPORT_0_9);
6668 	band->vht_cap.vht_mcs.rx_mcs_map = mcs_map;
6669 	band->vht_cap.vht_mcs.tx_mcs_map = mcs_map;
6670 
6671 	/* Beamforming support information */
6672 	if (txbf_bfe_cap & BRCMF_TXBF_SU_BFE_CAP)
6673 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
6674 	if (txbf_bfe_cap & BRCMF_TXBF_MU_BFE_CAP)
6675 		band->vht_cap.cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
6676 	if (txbf_bfr_cap & BRCMF_TXBF_SU_BFR_CAP)
6677 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
6678 	if (txbf_bfr_cap & BRCMF_TXBF_MU_BFR_CAP)
6679 		band->vht_cap.cap |= IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
6680 
6681 	if ((txbf_bfe_cap || txbf_bfr_cap) && (txstreams > 1)) {
6682 		band->vht_cap.cap |=
6683 			(2 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
6684 		band->vht_cap.cap |= ((txstreams - 1) <<
6685 				IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT);
6686 		band->vht_cap.cap |=
6687 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB;
6688 	}
6689 }
6690 
6691 static int brcmf_setup_wiphybands(struct brcmf_cfg80211_info *cfg)
6692 {
6693 	struct brcmf_pub *drvr = cfg->pub;
6694 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6695 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
6696 	u32 nmode = 0;
6697 	u32 vhtmode = 0;
6698 	u32 bw_cap[2] = { WLC_BW_20MHZ_BIT, WLC_BW_20MHZ_BIT };
6699 	u32 rxchain;
6700 	u32 nchain;
6701 	int err;
6702 	s32 i;
6703 	struct ieee80211_supported_band *band;
6704 	u32 txstreams = 0;
6705 	u32 txbf_bfe_cap = 0;
6706 	u32 txbf_bfr_cap = 0;
6707 
6708 	(void)brcmf_fil_iovar_int_get(ifp, "vhtmode", &vhtmode);
6709 	err = brcmf_fil_iovar_int_get(ifp, "nmode", &nmode);
6710 	if (err) {
6711 		bphy_err(drvr, "nmode error (%d)\n", err);
6712 	} else {
6713 		brcmf_get_bwcap(ifp, bw_cap);
6714 	}
6715 	brcmf_dbg(INFO, "nmode=%d, vhtmode=%d, bw_cap=(%d, %d)\n",
6716 		  nmode, vhtmode, bw_cap[NL80211_BAND_2GHZ],
6717 		  bw_cap[NL80211_BAND_5GHZ]);
6718 
6719 	err = brcmf_fil_iovar_int_get(ifp, "rxchain", &rxchain);
6720 	if (err) {
6721 		bphy_err(drvr, "rxchain error (%d)\n", err);
6722 		nchain = 1;
6723 	} else {
6724 		for (nchain = 0; rxchain; nchain++)
6725 			rxchain = rxchain & (rxchain - 1);
6726 	}
6727 	brcmf_dbg(INFO, "nchain=%d\n", nchain);
6728 
6729 	err = brcmf_construct_chaninfo(cfg, bw_cap);
6730 	if (err) {
6731 		bphy_err(drvr, "brcmf_construct_chaninfo failed (%d)\n", err);
6732 		return err;
6733 	}
6734 
6735 	if (vhtmode) {
6736 		(void)brcmf_fil_iovar_int_get(ifp, "txstreams", &txstreams);
6737 		(void)brcmf_fil_iovar_int_get(ifp, "txbf_bfe_cap",
6738 					      &txbf_bfe_cap);
6739 		(void)brcmf_fil_iovar_int_get(ifp, "txbf_bfr_cap",
6740 					      &txbf_bfr_cap);
6741 	}
6742 
6743 	for (i = 0; i < ARRAY_SIZE(wiphy->bands); i++) {
6744 		band = wiphy->bands[i];
6745 		if (band == NULL)
6746 			continue;
6747 
6748 		if (nmode)
6749 			brcmf_update_ht_cap(band, bw_cap, nchain);
6750 		if (vhtmode)
6751 			brcmf_update_vht_cap(band, bw_cap, nchain, txstreams,
6752 					     txbf_bfe_cap, txbf_bfr_cap);
6753 	}
6754 
6755 	return 0;
6756 }
6757 
6758 static const struct ieee80211_txrx_stypes
6759 brcmf_txrx_stypes[NUM_NL80211_IFTYPES] = {
6760 	[NL80211_IFTYPE_STATION] = {
6761 		.tx = 0xffff,
6762 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6763 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6764 	},
6765 	[NL80211_IFTYPE_P2P_CLIENT] = {
6766 		.tx = 0xffff,
6767 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6768 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6769 	},
6770 	[NL80211_IFTYPE_P2P_GO] = {
6771 		.tx = 0xffff,
6772 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
6773 		      BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
6774 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
6775 		      BIT(IEEE80211_STYPE_DISASSOC >> 4) |
6776 		      BIT(IEEE80211_STYPE_AUTH >> 4) |
6777 		      BIT(IEEE80211_STYPE_DEAUTH >> 4) |
6778 		      BIT(IEEE80211_STYPE_ACTION >> 4)
6779 	},
6780 	[NL80211_IFTYPE_P2P_DEVICE] = {
6781 		.tx = 0xffff,
6782 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6783 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6784 	},
6785 	[NL80211_IFTYPE_AP] = {
6786 		.tx = 0xffff,
6787 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
6788 		      BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
6789 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
6790 		      BIT(IEEE80211_STYPE_DISASSOC >> 4) |
6791 		      BIT(IEEE80211_STYPE_AUTH >> 4) |
6792 		      BIT(IEEE80211_STYPE_DEAUTH >> 4) |
6793 		      BIT(IEEE80211_STYPE_ACTION >> 4)
6794 	}
6795 };
6796 
6797 /**
6798  * brcmf_setup_ifmodes() - determine interface modes and combinations.
6799  *
6800  * @wiphy: wiphy object.
6801  * @ifp: interface object needed for feat module api.
6802  *
6803  * The interface modes and combinations are determined dynamically here
6804  * based on firmware functionality.
6805  *
6806  * no p2p and no mbss:
6807  *
6808  *	#STA <= 1, #AP <= 1, channels = 1, 2 total
6809  *
6810  * no p2p and mbss:
6811  *
6812  *	#STA <= 1, #AP <= 1, channels = 1, 2 total
6813  *	#AP <= 4, matching BI, channels = 1, 4 total
6814  *
6815  * no p2p and rsdb:
6816  *	#STA <= 1, #AP <= 2, channels = 2, 4 total
6817  *
6818  * p2p, no mchan, and mbss:
6819  *
6820  *	#STA <= 1, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 1, channels = 1, 3 total
6821  *	#STA <= 1, #P2P-DEV <= 1, #AP <= 1, #P2P-CL <= 1, channels = 1, 4 total
6822  *	#AP <= 4, matching BI, channels = 1, 4 total
6823  *
6824  * p2p, mchan, and mbss:
6825  *
6826  *	#STA <= 1, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 1, channels = 2, 3 total
6827  *	#STA <= 1, #P2P-DEV <= 1, #AP <= 1, #P2P-CL <= 1, channels = 1, 4 total
6828  *	#AP <= 4, matching BI, channels = 1, 4 total
6829  *
6830  * p2p, rsdb, and no mbss:
6831  *	#STA <= 1, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 2, AP <= 2,
6832  *	 channels = 2, 4 total
6833  */
6834 static int brcmf_setup_ifmodes(struct wiphy *wiphy, struct brcmf_if *ifp)
6835 {
6836 	struct ieee80211_iface_combination *combo = NULL;
6837 	struct ieee80211_iface_limit *c0_limits = NULL;
6838 	struct ieee80211_iface_limit *p2p_limits = NULL;
6839 	struct ieee80211_iface_limit *mbss_limits = NULL;
6840 	bool mon_flag, mbss, p2p, rsdb, mchan;
6841 	int i, c, n_combos, n_limits;
6842 
6843 	mon_flag = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FLAG);
6844 	mbss = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS);
6845 	p2p = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_P2P);
6846 	rsdb = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_RSDB);
6847 	mchan = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN);
6848 
6849 	n_combos = 1 + !!(p2p && !rsdb) + !!mbss;
6850 	combo = kcalloc(n_combos, sizeof(*combo), GFP_KERNEL);
6851 	if (!combo)
6852 		goto err;
6853 
6854 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
6855 				 BIT(NL80211_IFTYPE_ADHOC) |
6856 				 BIT(NL80211_IFTYPE_AP);
6857 	if (mon_flag)
6858 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
6859 	if (p2p)
6860 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
6861 					  BIT(NL80211_IFTYPE_P2P_GO) |
6862 					  BIT(NL80211_IFTYPE_P2P_DEVICE);
6863 
6864 	c = 0;
6865 	i = 0;
6866 	n_limits = 1 + mon_flag + (p2p ? 2 : 0) + (rsdb || !p2p);
6867 	c0_limits = kcalloc(n_limits, sizeof(*c0_limits), GFP_KERNEL);
6868 	if (!c0_limits)
6869 		goto err;
6870 
6871 	combo[c].num_different_channels = 1 + (rsdb || (p2p && mchan));
6872 	c0_limits[i].max = 1;
6873 	c0_limits[i++].types = BIT(NL80211_IFTYPE_STATION);
6874 	if (mon_flag) {
6875 		c0_limits[i].max = 1;
6876 		c0_limits[i++].types = BIT(NL80211_IFTYPE_MONITOR);
6877 	}
6878 	if (p2p) {
6879 		c0_limits[i].max = 1;
6880 		c0_limits[i++].types = BIT(NL80211_IFTYPE_P2P_DEVICE);
6881 		c0_limits[i].max = 1 + rsdb;
6882 		c0_limits[i++].types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
6883 				       BIT(NL80211_IFTYPE_P2P_GO);
6884 	}
6885 	if (p2p && rsdb) {
6886 		c0_limits[i].max = 2;
6887 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6888 		combo[c].max_interfaces = 4;
6889 	} else if (p2p) {
6890 		combo[c].max_interfaces = i;
6891 	} else if (rsdb) {
6892 		c0_limits[i].max = 2;
6893 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6894 		combo[c].max_interfaces = 3;
6895 	} else {
6896 		c0_limits[i].max = 1;
6897 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6898 		combo[c].max_interfaces = i;
6899 	}
6900 	combo[c].n_limits = i;
6901 	combo[c].limits = c0_limits;
6902 
6903 	if (p2p && !rsdb) {
6904 		c++;
6905 		i = 0;
6906 		p2p_limits = kcalloc(4, sizeof(*p2p_limits), GFP_KERNEL);
6907 		if (!p2p_limits)
6908 			goto err;
6909 		p2p_limits[i].max = 1;
6910 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_STATION);
6911 		p2p_limits[i].max = 1;
6912 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6913 		p2p_limits[i].max = 1;
6914 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_P2P_CLIENT);
6915 		p2p_limits[i].max = 1;
6916 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_P2P_DEVICE);
6917 		combo[c].num_different_channels = 1;
6918 		combo[c].max_interfaces = i;
6919 		combo[c].n_limits = i;
6920 		combo[c].limits = p2p_limits;
6921 	}
6922 
6923 	if (mbss) {
6924 		c++;
6925 		i = 0;
6926 		n_limits = 1 + mon_flag;
6927 		mbss_limits = kcalloc(n_limits, sizeof(*mbss_limits),
6928 				      GFP_KERNEL);
6929 		if (!mbss_limits)
6930 			goto err;
6931 		mbss_limits[i].max = 4;
6932 		mbss_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6933 		if (mon_flag) {
6934 			mbss_limits[i].max = 1;
6935 			mbss_limits[i++].types = BIT(NL80211_IFTYPE_MONITOR);
6936 		}
6937 		combo[c].beacon_int_infra_match = true;
6938 		combo[c].num_different_channels = 1;
6939 		combo[c].max_interfaces = 4 + mon_flag;
6940 		combo[c].n_limits = i;
6941 		combo[c].limits = mbss_limits;
6942 	}
6943 
6944 	wiphy->n_iface_combinations = n_combos;
6945 	wiphy->iface_combinations = combo;
6946 	return 0;
6947 
6948 err:
6949 	kfree(c0_limits);
6950 	kfree(p2p_limits);
6951 	kfree(mbss_limits);
6952 	kfree(combo);
6953 	return -ENOMEM;
6954 }
6955 
6956 #ifdef CONFIG_PM
6957 static const struct wiphy_wowlan_support brcmf_wowlan_support = {
6958 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
6959 	.n_patterns = BRCMF_WOWL_MAXPATTERNS,
6960 	.pattern_max_len = BRCMF_WOWL_MAXPATTERNSIZE,
6961 	.pattern_min_len = 1,
6962 	.max_pkt_offset = 1500,
6963 };
6964 #endif
6965 
6966 static void brcmf_wiphy_wowl_params(struct wiphy *wiphy, struct brcmf_if *ifp)
6967 {
6968 #ifdef CONFIG_PM
6969 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
6970 	struct brcmf_pub *drvr = cfg->pub;
6971 	struct wiphy_wowlan_support *wowl;
6972 
6973 	wowl = kmemdup(&brcmf_wowlan_support, sizeof(brcmf_wowlan_support),
6974 		       GFP_KERNEL);
6975 	if (!wowl) {
6976 		bphy_err(drvr, "only support basic wowlan features\n");
6977 		wiphy->wowlan = &brcmf_wowlan_support;
6978 		return;
6979 	}
6980 
6981 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_PNO)) {
6982 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_ND)) {
6983 			wowl->flags |= WIPHY_WOWLAN_NET_DETECT;
6984 			wowl->max_nd_match_sets = BRCMF_PNO_MAX_PFN_COUNT;
6985 			init_waitqueue_head(&cfg->wowl.nd_data_wait);
6986 		}
6987 	}
6988 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_GTK)) {
6989 		wowl->flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY;
6990 		wowl->flags |= WIPHY_WOWLAN_GTK_REKEY_FAILURE;
6991 	}
6992 
6993 	wiphy->wowlan = wowl;
6994 #endif
6995 }
6996 
6997 static int brcmf_setup_wiphy(struct wiphy *wiphy, struct brcmf_if *ifp)
6998 {
6999 	struct brcmf_pub *drvr = ifp->drvr;
7000 	const struct ieee80211_iface_combination *combo;
7001 	struct ieee80211_supported_band *band;
7002 	u16 max_interfaces = 0;
7003 	bool gscan;
7004 	__le32 bandlist[3];
7005 	u32 n_bands;
7006 	int err, i;
7007 
7008 	wiphy->max_scan_ssids = WL_NUM_SCAN_MAX;
7009 	wiphy->max_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
7010 	wiphy->max_num_pmkids = BRCMF_MAXPMKID;
7011 
7012 	err = brcmf_setup_ifmodes(wiphy, ifp);
7013 	if (err)
7014 		return err;
7015 
7016 	for (i = 0, combo = wiphy->iface_combinations;
7017 	     i < wiphy->n_iface_combinations; i++, combo++) {
7018 		max_interfaces = max(max_interfaces, combo->max_interfaces);
7019 	}
7020 
7021 	for (i = 0; i < max_interfaces && i < ARRAY_SIZE(drvr->addresses);
7022 	     i++) {
7023 		u8 *addr = drvr->addresses[i].addr;
7024 
7025 		memcpy(addr, drvr->mac, ETH_ALEN);
7026 		if (i) {
7027 			addr[0] |= BIT(1);
7028 			addr[ETH_ALEN - 1] ^= i;
7029 		}
7030 	}
7031 	wiphy->addresses = drvr->addresses;
7032 	wiphy->n_addresses = i;
7033 
7034 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
7035 	wiphy->cipher_suites = brcmf_cipher_suites;
7036 	wiphy->n_cipher_suites = ARRAY_SIZE(brcmf_cipher_suites);
7037 	if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP))
7038 		wiphy->n_cipher_suites--;
7039 	wiphy->bss_select_support = BIT(NL80211_BSS_SELECT_ATTR_RSSI) |
7040 				    BIT(NL80211_BSS_SELECT_ATTR_BAND_PREF) |
7041 				    BIT(NL80211_BSS_SELECT_ATTR_RSSI_ADJUST);
7042 
7043 	wiphy->flags |= WIPHY_FLAG_NETNS_OK |
7044 			WIPHY_FLAG_PS_ON_BY_DEFAULT |
7045 			WIPHY_FLAG_HAVE_AP_SME |
7046 			WIPHY_FLAG_OFFCHAN_TX |
7047 			WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
7048 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_TDLS))
7049 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
7050 	if (!ifp->drvr->settings->roamoff)
7051 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM;
7052 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_FWSUP)) {
7053 		wiphy_ext_feature_set(wiphy,
7054 				      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK);
7055 		wiphy_ext_feature_set(wiphy,
7056 				      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X);
7057 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_SAE))
7058 			wiphy_ext_feature_set(wiphy,
7059 					      NL80211_EXT_FEATURE_SAE_OFFLOAD);
7060 	}
7061 	wiphy->mgmt_stypes = brcmf_txrx_stypes;
7062 	wiphy->max_remain_on_channel_duration = 5000;
7063 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_PNO)) {
7064 		gscan = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_GSCAN);
7065 		brcmf_pno_wiphy_params(wiphy, gscan);
7066 	}
7067 	/* vendor commands/events support */
7068 	wiphy->vendor_commands = brcmf_vendor_cmds;
7069 	wiphy->n_vendor_commands = BRCMF_VNDR_CMDS_LAST - 1;
7070 
7071 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL))
7072 		brcmf_wiphy_wowl_params(wiphy, ifp);
7073 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BANDLIST, &bandlist,
7074 				     sizeof(bandlist));
7075 	if (err) {
7076 		bphy_err(drvr, "could not obtain band info: err=%d\n", err);
7077 		return err;
7078 	}
7079 	/* first entry in bandlist is number of bands */
7080 	n_bands = le32_to_cpu(bandlist[0]);
7081 	for (i = 1; i <= n_bands && i < ARRAY_SIZE(bandlist); i++) {
7082 		if (bandlist[i] == cpu_to_le32(WLC_BAND_2G)) {
7083 			band = kmemdup(&__wl_band_2ghz, sizeof(__wl_band_2ghz),
7084 				       GFP_KERNEL);
7085 			if (!band)
7086 				return -ENOMEM;
7087 
7088 			band->channels = kmemdup(&__wl_2ghz_channels,
7089 						 sizeof(__wl_2ghz_channels),
7090 						 GFP_KERNEL);
7091 			if (!band->channels) {
7092 				kfree(band);
7093 				return -ENOMEM;
7094 			}
7095 
7096 			band->n_channels = ARRAY_SIZE(__wl_2ghz_channels);
7097 			wiphy->bands[NL80211_BAND_2GHZ] = band;
7098 		}
7099 		if (bandlist[i] == cpu_to_le32(WLC_BAND_5G)) {
7100 			band = kmemdup(&__wl_band_5ghz, sizeof(__wl_band_5ghz),
7101 				       GFP_KERNEL);
7102 			if (!band)
7103 				return -ENOMEM;
7104 
7105 			band->channels = kmemdup(&__wl_5ghz_channels,
7106 						 sizeof(__wl_5ghz_channels),
7107 						 GFP_KERNEL);
7108 			if (!band->channels) {
7109 				kfree(band);
7110 				return -ENOMEM;
7111 			}
7112 
7113 			band->n_channels = ARRAY_SIZE(__wl_5ghz_channels);
7114 			wiphy->bands[NL80211_BAND_5GHZ] = band;
7115 		}
7116 	}
7117 
7118 	if (wiphy->bands[NL80211_BAND_5GHZ] &&
7119 	    brcmf_feat_is_enabled(ifp, BRCMF_FEAT_DOT11H))
7120 		wiphy_ext_feature_set(wiphy,
7121 				      NL80211_EXT_FEATURE_DFS_OFFLOAD);
7122 
7123 	wiphy_read_of_freq_limits(wiphy);
7124 
7125 	return 0;
7126 }
7127 
7128 static s32 brcmf_config_dongle(struct brcmf_cfg80211_info *cfg)
7129 {
7130 	struct brcmf_pub *drvr = cfg->pub;
7131 	struct net_device *ndev;
7132 	struct wireless_dev *wdev;
7133 	struct brcmf_if *ifp;
7134 	s32 power_mode;
7135 	s32 err = 0;
7136 
7137 	if (cfg->dongle_up)
7138 		return err;
7139 
7140 	ndev = cfg_to_ndev(cfg);
7141 	wdev = ndev->ieee80211_ptr;
7142 	ifp = netdev_priv(ndev);
7143 
7144 	/* make sure RF is ready for work */
7145 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
7146 
7147 	brcmf_dongle_scantime(ifp);
7148 
7149 	power_mode = cfg->pwr_save ? PM_FAST : PM_OFF;
7150 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, power_mode);
7151 	if (err)
7152 		goto default_conf_out;
7153 	brcmf_dbg(INFO, "power save set to %s\n",
7154 		  (power_mode ? "enabled" : "disabled"));
7155 
7156 	err = brcmf_dongle_roam(ifp);
7157 	if (err)
7158 		goto default_conf_out;
7159 	err = brcmf_cfg80211_change_iface(wdev->wiphy, ndev, wdev->iftype,
7160 					  NULL);
7161 	if (err)
7162 		goto default_conf_out;
7163 
7164 	brcmf_configure_arp_nd_offload(ifp, true);
7165 
7166 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_FAKEFRAG, 1);
7167 	if (err) {
7168 		bphy_err(drvr, "failed to set frameburst mode\n");
7169 		goto default_conf_out;
7170 	}
7171 
7172 	cfg->dongle_up = true;
7173 default_conf_out:
7174 
7175 	return err;
7176 
7177 }
7178 
7179 static s32 __brcmf_cfg80211_up(struct brcmf_if *ifp)
7180 {
7181 	set_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
7182 
7183 	return brcmf_config_dongle(ifp->drvr->config);
7184 }
7185 
7186 static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp)
7187 {
7188 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
7189 
7190 	/*
7191 	 * While going down, if associated with AP disassociate
7192 	 * from AP to save power
7193 	 */
7194 	if (check_vif_up(ifp->vif)) {
7195 		brcmf_link_down(ifp->vif, WLAN_REASON_UNSPECIFIED, true);
7196 
7197 		/* Make sure WPA_Supplicant receives all the event
7198 		   generated due to DISASSOC call to the fw to keep
7199 		   the state fw and WPA_Supplicant state consistent
7200 		 */
7201 		brcmf_delay(500);
7202 	}
7203 
7204 	brcmf_abort_scanning(cfg);
7205 	clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
7206 
7207 	return 0;
7208 }
7209 
7210 s32 brcmf_cfg80211_up(struct net_device *ndev)
7211 {
7212 	struct brcmf_if *ifp = netdev_priv(ndev);
7213 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
7214 	s32 err = 0;
7215 
7216 	mutex_lock(&cfg->usr_sync);
7217 	err = __brcmf_cfg80211_up(ifp);
7218 	mutex_unlock(&cfg->usr_sync);
7219 
7220 	return err;
7221 }
7222 
7223 s32 brcmf_cfg80211_down(struct net_device *ndev)
7224 {
7225 	struct brcmf_if *ifp = netdev_priv(ndev);
7226 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
7227 	s32 err = 0;
7228 
7229 	mutex_lock(&cfg->usr_sync);
7230 	err = __brcmf_cfg80211_down(ifp);
7231 	mutex_unlock(&cfg->usr_sync);
7232 
7233 	return err;
7234 }
7235 
7236 enum nl80211_iftype brcmf_cfg80211_get_iftype(struct brcmf_if *ifp)
7237 {
7238 	struct wireless_dev *wdev = &ifp->vif->wdev;
7239 
7240 	return wdev->iftype;
7241 }
7242 
7243 bool brcmf_get_vif_state_any(struct brcmf_cfg80211_info *cfg,
7244 			     unsigned long state)
7245 {
7246 	struct brcmf_cfg80211_vif *vif;
7247 
7248 	list_for_each_entry(vif, &cfg->vif_list, list) {
7249 		if (test_bit(state, &vif->sme_state))
7250 			return true;
7251 	}
7252 	return false;
7253 }
7254 
7255 static inline bool vif_event_equals(struct brcmf_cfg80211_vif_event *event,
7256 				    u8 action)
7257 {
7258 	u8 evt_action;
7259 
7260 	spin_lock(&event->vif_event_lock);
7261 	evt_action = event->action;
7262 	spin_unlock(&event->vif_event_lock);
7263 	return evt_action == action;
7264 }
7265 
7266 void brcmf_cfg80211_arm_vif_event(struct brcmf_cfg80211_info *cfg,
7267 				  struct brcmf_cfg80211_vif *vif)
7268 {
7269 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7270 
7271 	spin_lock(&event->vif_event_lock);
7272 	event->vif = vif;
7273 	event->action = 0;
7274 	spin_unlock(&event->vif_event_lock);
7275 }
7276 
7277 bool brcmf_cfg80211_vif_event_armed(struct brcmf_cfg80211_info *cfg)
7278 {
7279 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7280 	bool armed;
7281 
7282 	spin_lock(&event->vif_event_lock);
7283 	armed = event->vif != NULL;
7284 	spin_unlock(&event->vif_event_lock);
7285 
7286 	return armed;
7287 }
7288 
7289 int brcmf_cfg80211_wait_vif_event(struct brcmf_cfg80211_info *cfg,
7290 				  u8 action, ulong timeout)
7291 {
7292 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7293 
7294 	return wait_event_timeout(event->vif_wq,
7295 				  vif_event_equals(event, action), timeout);
7296 }
7297 
7298 static s32 brcmf_translate_country_code(struct brcmf_pub *drvr, char alpha2[2],
7299 					struct brcmf_fil_country_le *ccreq)
7300 {
7301 	struct brcmfmac_pd_cc *country_codes;
7302 	struct brcmfmac_pd_cc_entry *cc;
7303 	s32 found_index;
7304 	int i;
7305 
7306 	country_codes = drvr->settings->country_codes;
7307 	if (!country_codes) {
7308 		brcmf_dbg(TRACE, "No country codes configured for device\n");
7309 		return -EINVAL;
7310 	}
7311 
7312 	if ((alpha2[0] == ccreq->country_abbrev[0]) &&
7313 	    (alpha2[1] == ccreq->country_abbrev[1])) {
7314 		brcmf_dbg(TRACE, "Country code already set\n");
7315 		return -EAGAIN;
7316 	}
7317 
7318 	found_index = -1;
7319 	for (i = 0; i < country_codes->table_size; i++) {
7320 		cc = &country_codes->table[i];
7321 		if ((cc->iso3166[0] == '\0') && (found_index == -1))
7322 			found_index = i;
7323 		if ((cc->iso3166[0] == alpha2[0]) &&
7324 		    (cc->iso3166[1] == alpha2[1])) {
7325 			found_index = i;
7326 			break;
7327 		}
7328 	}
7329 	if (found_index == -1) {
7330 		brcmf_dbg(TRACE, "No country code match found\n");
7331 		return -EINVAL;
7332 	}
7333 	memset(ccreq, 0, sizeof(*ccreq));
7334 	ccreq->rev = cpu_to_le32(country_codes->table[found_index].rev);
7335 	memcpy(ccreq->ccode, country_codes->table[found_index].cc,
7336 	       BRCMF_COUNTRY_BUF_SZ);
7337 	ccreq->country_abbrev[0] = alpha2[0];
7338 	ccreq->country_abbrev[1] = alpha2[1];
7339 	ccreq->country_abbrev[2] = 0;
7340 
7341 	return 0;
7342 }
7343 
7344 static void brcmf_cfg80211_reg_notifier(struct wiphy *wiphy,
7345 					struct regulatory_request *req)
7346 {
7347 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
7348 	struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
7349 	struct brcmf_pub *drvr = cfg->pub;
7350 	struct brcmf_fil_country_le ccreq;
7351 	s32 err;
7352 	int i;
7353 
7354 	/* The country code gets set to "00" by default at boot, ignore */
7355 	if (req->alpha2[0] == '0' && req->alpha2[1] == '0')
7356 		return;
7357 
7358 	/* ignore non-ISO3166 country codes */
7359 	for (i = 0; i < 2; i++)
7360 		if (req->alpha2[i] < 'A' || req->alpha2[i] > 'Z') {
7361 			bphy_err(drvr, "not an ISO3166 code (0x%02x 0x%02x)\n",
7362 				 req->alpha2[0], req->alpha2[1]);
7363 			return;
7364 		}
7365 
7366 	brcmf_dbg(TRACE, "Enter: initiator=%d, alpha=%c%c\n", req->initiator,
7367 		  req->alpha2[0], req->alpha2[1]);
7368 
7369 	err = brcmf_fil_iovar_data_get(ifp, "country", &ccreq, sizeof(ccreq));
7370 	if (err) {
7371 		bphy_err(drvr, "Country code iovar returned err = %d\n", err);
7372 		return;
7373 	}
7374 
7375 	err = brcmf_translate_country_code(ifp->drvr, req->alpha2, &ccreq);
7376 	if (err)
7377 		return;
7378 
7379 	err = brcmf_fil_iovar_data_set(ifp, "country", &ccreq, sizeof(ccreq));
7380 	if (err) {
7381 		bphy_err(drvr, "Firmware rejected country setting\n");
7382 		return;
7383 	}
7384 	brcmf_setup_wiphybands(cfg);
7385 }
7386 
7387 static void brcmf_free_wiphy(struct wiphy *wiphy)
7388 {
7389 	int i;
7390 
7391 	if (!wiphy)
7392 		return;
7393 
7394 	if (wiphy->iface_combinations) {
7395 		for (i = 0; i < wiphy->n_iface_combinations; i++)
7396 			kfree(wiphy->iface_combinations[i].limits);
7397 	}
7398 	kfree(wiphy->iface_combinations);
7399 	if (wiphy->bands[NL80211_BAND_2GHZ]) {
7400 		kfree(wiphy->bands[NL80211_BAND_2GHZ]->channels);
7401 		kfree(wiphy->bands[NL80211_BAND_2GHZ]);
7402 	}
7403 	if (wiphy->bands[NL80211_BAND_5GHZ]) {
7404 		kfree(wiphy->bands[NL80211_BAND_5GHZ]->channels);
7405 		kfree(wiphy->bands[NL80211_BAND_5GHZ]);
7406 	}
7407 #if IS_ENABLED(CONFIG_PM)
7408 	if (wiphy->wowlan != &brcmf_wowlan_support)
7409 		kfree(wiphy->wowlan);
7410 #endif
7411 }
7412 
7413 struct brcmf_cfg80211_info *brcmf_cfg80211_attach(struct brcmf_pub *drvr,
7414 						  struct cfg80211_ops *ops,
7415 						  bool p2pdev_forced)
7416 {
7417 	struct wiphy *wiphy = drvr->wiphy;
7418 	struct net_device *ndev = brcmf_get_ifp(drvr, 0)->ndev;
7419 	struct brcmf_cfg80211_info *cfg;
7420 	struct brcmf_cfg80211_vif *vif;
7421 	struct brcmf_if *ifp;
7422 	s32 err = 0;
7423 	s32 io_type;
7424 	u16 *cap = NULL;
7425 
7426 	if (!ndev) {
7427 		bphy_err(drvr, "ndev is invalid\n");
7428 		return NULL;
7429 	}
7430 
7431 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
7432 	if (!cfg) {
7433 		bphy_err(drvr, "Could not allocate wiphy device\n");
7434 		return NULL;
7435 	}
7436 
7437 	cfg->wiphy = wiphy;
7438 	cfg->pub = drvr;
7439 	init_vif_event(&cfg->vif_event);
7440 	INIT_LIST_HEAD(&cfg->vif_list);
7441 
7442 	vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_STATION);
7443 	if (IS_ERR(vif))
7444 		goto wiphy_out;
7445 
7446 	ifp = netdev_priv(ndev);
7447 	vif->ifp = ifp;
7448 	vif->wdev.netdev = ndev;
7449 	ndev->ieee80211_ptr = &vif->wdev;
7450 	SET_NETDEV_DEV(ndev, wiphy_dev(cfg->wiphy));
7451 
7452 	err = wl_init_priv(cfg);
7453 	if (err) {
7454 		bphy_err(drvr, "Failed to init iwm_priv (%d)\n", err);
7455 		brcmf_free_vif(vif);
7456 		goto wiphy_out;
7457 	}
7458 	ifp->vif = vif;
7459 
7460 	/* determine d11 io type before wiphy setup */
7461 	err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_VERSION, &io_type);
7462 	if (err) {
7463 		bphy_err(drvr, "Failed to get D11 version (%d)\n", err);
7464 		goto priv_out;
7465 	}
7466 	cfg->d11inf.io_type = (u8)io_type;
7467 	brcmu_d11_attach(&cfg->d11inf);
7468 
7469 	/* regulatory notifer below needs access to cfg so
7470 	 * assign it now.
7471 	 */
7472 	drvr->config = cfg;
7473 
7474 	err = brcmf_setup_wiphy(wiphy, ifp);
7475 	if (err < 0)
7476 		goto priv_out;
7477 
7478 	brcmf_dbg(INFO, "Registering custom regulatory\n");
7479 	wiphy->reg_notifier = brcmf_cfg80211_reg_notifier;
7480 	wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
7481 	wiphy_apply_custom_regulatory(wiphy, &brcmf_regdom);
7482 
7483 	/* firmware defaults to 40MHz disabled in 2G band. We signal
7484 	 * cfg80211 here that we do and have it decide we can enable
7485 	 * it. But first check if device does support 2G operation.
7486 	 */
7487 	if (wiphy->bands[NL80211_BAND_2GHZ]) {
7488 		cap = &wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.cap;
7489 		*cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7490 	}
7491 #ifdef CONFIG_PM
7492 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_GTK))
7493 		ops->set_rekey_data = brcmf_cfg80211_set_rekey_data;
7494 #endif
7495 	err = wiphy_register(wiphy);
7496 	if (err < 0) {
7497 		bphy_err(drvr, "Could not register wiphy device (%d)\n", err);
7498 		goto priv_out;
7499 	}
7500 
7501 	err = brcmf_setup_wiphybands(cfg);
7502 	if (err) {
7503 		bphy_err(drvr, "Setting wiphy bands failed (%d)\n", err);
7504 		goto wiphy_unreg_out;
7505 	}
7506 
7507 	/* If cfg80211 didn't disable 40MHz HT CAP in wiphy_register(),
7508 	 * setup 40MHz in 2GHz band and enable OBSS scanning.
7509 	 */
7510 	if (cap && (*cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)) {
7511 		err = brcmf_enable_bw40_2g(cfg);
7512 		if (!err)
7513 			err = brcmf_fil_iovar_int_set(ifp, "obss_coex",
7514 						      BRCMF_OBSS_COEX_AUTO);
7515 		else
7516 			*cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7517 	}
7518 
7519 	err = brcmf_fweh_activate_events(ifp);
7520 	if (err) {
7521 		bphy_err(drvr, "FWEH activation failed (%d)\n", err);
7522 		goto wiphy_unreg_out;
7523 	}
7524 
7525 	err = brcmf_p2p_attach(cfg, p2pdev_forced);
7526 	if (err) {
7527 		bphy_err(drvr, "P2P initialisation failed (%d)\n", err);
7528 		goto wiphy_unreg_out;
7529 	}
7530 	err = brcmf_btcoex_attach(cfg);
7531 	if (err) {
7532 		bphy_err(drvr, "BT-coex initialisation failed (%d)\n", err);
7533 		brcmf_p2p_detach(&cfg->p2p);
7534 		goto wiphy_unreg_out;
7535 	}
7536 	err = brcmf_pno_attach(cfg);
7537 	if (err) {
7538 		bphy_err(drvr, "PNO initialisation failed (%d)\n", err);
7539 		brcmf_btcoex_detach(cfg);
7540 		brcmf_p2p_detach(&cfg->p2p);
7541 		goto wiphy_unreg_out;
7542 	}
7543 
7544 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_TDLS)) {
7545 		err = brcmf_fil_iovar_int_set(ifp, "tdls_enable", 1);
7546 		if (err) {
7547 			brcmf_dbg(INFO, "TDLS not enabled (%d)\n", err);
7548 			wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_TDLS;
7549 		} else {
7550 			brcmf_fweh_register(cfg->pub, BRCMF_E_TDLS_PEER_EVENT,
7551 					    brcmf_notify_tdls_peer_event);
7552 		}
7553 	}
7554 
7555 	/* (re-) activate FWEH event handling */
7556 	err = brcmf_fweh_activate_events(ifp);
7557 	if (err) {
7558 		bphy_err(drvr, "FWEH activation failed (%d)\n", err);
7559 		goto detach;
7560 	}
7561 
7562 	/* Fill in some of the advertised nl80211 supported features */
7563 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_SCAN_RANDOM_MAC)) {
7564 		wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
7565 #ifdef CONFIG_PM
7566 		if (wiphy->wowlan &&
7567 		    wiphy->wowlan->flags & WIPHY_WOWLAN_NET_DETECT)
7568 			wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7569 #endif
7570 	}
7571 
7572 	return cfg;
7573 
7574 detach:
7575 	brcmf_pno_detach(cfg);
7576 	brcmf_btcoex_detach(cfg);
7577 	brcmf_p2p_detach(&cfg->p2p);
7578 wiphy_unreg_out:
7579 	wiphy_unregister(cfg->wiphy);
7580 priv_out:
7581 	wl_deinit_priv(cfg);
7582 	brcmf_free_vif(vif);
7583 	ifp->vif = NULL;
7584 wiphy_out:
7585 	brcmf_free_wiphy(wiphy);
7586 	kfree(cfg);
7587 	return NULL;
7588 }
7589 
7590 void brcmf_cfg80211_detach(struct brcmf_cfg80211_info *cfg)
7591 {
7592 	if (!cfg)
7593 		return;
7594 
7595 	brcmf_pno_detach(cfg);
7596 	brcmf_btcoex_detach(cfg);
7597 	wiphy_unregister(cfg->wiphy);
7598 	wl_deinit_priv(cfg);
7599 	brcmf_free_wiphy(cfg->wiphy);
7600 	kfree(cfg);
7601 }
7602