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