xref: /linux/drivers/net/wireless/broadcom/brcm80211/brcmfmac/cfg80211.c (revision 05ee19c18c2bb3dea69e29219017367c4a77e65a)
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 -EINVAL;
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 	case BRCMF_VNDR_IE_ASSOCRSP_FLAG:
4453 		mgmt_ie_buf = saved_ie->assoc_res_ie;
4454 		mgmt_ie_len = &saved_ie->assoc_res_ie_len;
4455 		mgmt_ie_buf_len = sizeof(saved_ie->assoc_res_ie);
4456 		break;
4457 	default:
4458 		err = -EPERM;
4459 		bphy_err(drvr, "not suitable type\n");
4460 		goto exit;
4461 	}
4462 
4463 	if (vndr_ie_len > mgmt_ie_buf_len) {
4464 		err = -ENOMEM;
4465 		bphy_err(drvr, "extra IE size too big\n");
4466 		goto exit;
4467 	}
4468 
4469 	/* parse and save new vndr_ie in curr_ie_buff before comparing it */
4470 	if (vndr_ie_buf && vndr_ie_len && curr_ie_buf) {
4471 		ptr = curr_ie_buf;
4472 		brcmf_parse_vndr_ies(vndr_ie_buf, vndr_ie_len, &new_vndr_ies);
4473 		for (i = 0; i < new_vndr_ies.count; i++) {
4474 			vndrie_info = &new_vndr_ies.ie_info[i];
4475 			memcpy(ptr + parsed_ie_buf_len, vndrie_info->ie_ptr,
4476 			       vndrie_info->ie_len);
4477 			parsed_ie_buf_len += vndrie_info->ie_len;
4478 		}
4479 	}
4480 
4481 	if (mgmt_ie_buf && *mgmt_ie_len) {
4482 		if (parsed_ie_buf_len && (parsed_ie_buf_len == *mgmt_ie_len) &&
4483 		    (memcmp(mgmt_ie_buf, curr_ie_buf,
4484 			    parsed_ie_buf_len) == 0)) {
4485 			brcmf_dbg(TRACE, "Previous mgmt IE equals to current IE\n");
4486 			goto exit;
4487 		}
4488 
4489 		/* parse old vndr_ie */
4490 		brcmf_parse_vndr_ies(mgmt_ie_buf, *mgmt_ie_len, &old_vndr_ies);
4491 
4492 		/* make a command to delete old ie */
4493 		for (i = 0; i < old_vndr_ies.count; i++) {
4494 			vndrie_info = &old_vndr_ies.ie_info[i];
4495 
4496 			brcmf_dbg(TRACE, "DEL ID : %d, Len: %d , OUI:%3ph\n",
4497 				  vndrie_info->vndrie.id,
4498 				  vndrie_info->vndrie.len,
4499 				  vndrie_info->vndrie.oui);
4500 
4501 			del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
4502 							   vndrie_info->ie_ptr,
4503 							   vndrie_info->ie_len,
4504 							   "del");
4505 			curr_ie_buf += del_add_ie_buf_len;
4506 			total_ie_buf_len += del_add_ie_buf_len;
4507 		}
4508 	}
4509 
4510 	*mgmt_ie_len = 0;
4511 	/* Add if there is any extra IE */
4512 	if (mgmt_ie_buf && parsed_ie_buf_len) {
4513 		ptr = mgmt_ie_buf;
4514 
4515 		remained_buf_len = mgmt_ie_buf_len;
4516 
4517 		/* make a command to add new ie */
4518 		for (i = 0; i < new_vndr_ies.count; i++) {
4519 			vndrie_info = &new_vndr_ies.ie_info[i];
4520 
4521 			/* verify remained buf size before copy data */
4522 			if (remained_buf_len < (vndrie_info->vndrie.len +
4523 							VNDR_IE_VSIE_OFFSET)) {
4524 				bphy_err(drvr, "no space in mgmt_ie_buf: len left %d",
4525 					 remained_buf_len);
4526 				break;
4527 			}
4528 			remained_buf_len -= (vndrie_info->ie_len +
4529 					     VNDR_IE_VSIE_OFFSET);
4530 
4531 			brcmf_dbg(TRACE, "ADDED ID : %d, Len: %d, OUI:%3ph\n",
4532 				  vndrie_info->vndrie.id,
4533 				  vndrie_info->vndrie.len,
4534 				  vndrie_info->vndrie.oui);
4535 
4536 			del_add_ie_buf_len = brcmf_vndr_ie(curr_ie_buf, pktflag,
4537 							   vndrie_info->ie_ptr,
4538 							   vndrie_info->ie_len,
4539 							   "add");
4540 
4541 			/* save the parsed IE in wl struct */
4542 			memcpy(ptr + (*mgmt_ie_len), vndrie_info->ie_ptr,
4543 			       vndrie_info->ie_len);
4544 			*mgmt_ie_len += vndrie_info->ie_len;
4545 
4546 			curr_ie_buf += del_add_ie_buf_len;
4547 			total_ie_buf_len += del_add_ie_buf_len;
4548 		}
4549 	}
4550 	if (total_ie_buf_len) {
4551 		err  = brcmf_fil_bsscfg_data_set(ifp, "vndr_ie", iovar_ie_buf,
4552 						 total_ie_buf_len);
4553 		if (err)
4554 			bphy_err(drvr, "vndr ie set error : %d\n", err);
4555 	}
4556 
4557 exit:
4558 	kfree(iovar_ie_buf);
4559 	return err;
4560 }
4561 
4562 s32 brcmf_vif_clear_mgmt_ies(struct brcmf_cfg80211_vif *vif)
4563 {
4564 	s32 pktflags[] = {
4565 		BRCMF_VNDR_IE_PRBREQ_FLAG,
4566 		BRCMF_VNDR_IE_PRBRSP_FLAG,
4567 		BRCMF_VNDR_IE_BEACON_FLAG
4568 	};
4569 	int i;
4570 
4571 	for (i = 0; i < ARRAY_SIZE(pktflags); i++)
4572 		brcmf_vif_set_mgmt_ie(vif, pktflags[i], NULL, 0);
4573 
4574 	memset(&vif->saved_ie, 0, sizeof(vif->saved_ie));
4575 	return 0;
4576 }
4577 
4578 static s32
4579 brcmf_config_ap_mgmt_ie(struct brcmf_cfg80211_vif *vif,
4580 			struct cfg80211_beacon_data *beacon)
4581 {
4582 	struct brcmf_pub *drvr = vif->ifp->drvr;
4583 	s32 err;
4584 
4585 	/* Set Beacon IEs to FW */
4586 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_BEACON_FLAG,
4587 				    beacon->tail, beacon->tail_len);
4588 	if (err) {
4589 		bphy_err(drvr, "Set Beacon IE Failed\n");
4590 		return err;
4591 	}
4592 	brcmf_dbg(TRACE, "Applied Vndr IEs for Beacon\n");
4593 
4594 	/* Set Probe Response IEs to FW */
4595 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_PRBRSP_FLAG,
4596 				    beacon->proberesp_ies,
4597 				    beacon->proberesp_ies_len);
4598 	if (err)
4599 		bphy_err(drvr, "Set Probe Resp IE Failed\n");
4600 	else
4601 		brcmf_dbg(TRACE, "Applied Vndr IEs for Probe Resp\n");
4602 
4603 	/* Set Assoc Response IEs to FW */
4604 	err = brcmf_vif_set_mgmt_ie(vif, BRCMF_VNDR_IE_ASSOCRSP_FLAG,
4605 				    beacon->assocresp_ies,
4606 				    beacon->assocresp_ies_len);
4607 	if (err)
4608 		brcmf_err("Set Assoc Resp IE Failed\n");
4609 	else
4610 		brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc Resp\n");
4611 
4612 	return err;
4613 }
4614 
4615 static s32
4616 brcmf_cfg80211_start_ap(struct wiphy *wiphy, struct net_device *ndev,
4617 			struct cfg80211_ap_settings *settings)
4618 {
4619 	s32 ie_offset;
4620 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4621 	struct brcmf_if *ifp = netdev_priv(ndev);
4622 	struct brcmf_pub *drvr = cfg->pub;
4623 	const struct brcmf_tlv *ssid_ie;
4624 	const struct brcmf_tlv *country_ie;
4625 	struct brcmf_ssid_le ssid_le;
4626 	s32 err = -EPERM;
4627 	const struct brcmf_tlv *rsn_ie;
4628 	const struct brcmf_vs_tlv *wpa_ie;
4629 	struct brcmf_join_params join_params;
4630 	enum nl80211_iftype dev_role;
4631 	struct brcmf_fil_bss_enable_le bss_enable;
4632 	u16 chanspec = chandef_to_chanspec(&cfg->d11inf, &settings->chandef);
4633 	bool mbss;
4634 	int is_11d;
4635 	bool supports_11d;
4636 
4637 	brcmf_dbg(TRACE, "ctrlchn=%d, center=%d, bw=%d, beacon_interval=%d, dtim_period=%d,\n",
4638 		  settings->chandef.chan->hw_value,
4639 		  settings->chandef.center_freq1, settings->chandef.width,
4640 		  settings->beacon_interval, settings->dtim_period);
4641 	brcmf_dbg(TRACE, "ssid=%s(%zu), auth_type=%d, inactivity_timeout=%d\n",
4642 		  settings->ssid, settings->ssid_len, settings->auth_type,
4643 		  settings->inactivity_timeout);
4644 	dev_role = ifp->vif->wdev.iftype;
4645 	mbss = ifp->vif->mbss;
4646 
4647 	/* store current 11d setting */
4648 	if (brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_REGULATORY,
4649 				  &ifp->vif->is_11d)) {
4650 		is_11d = supports_11d = false;
4651 	} else {
4652 		country_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
4653 					      settings->beacon.tail_len,
4654 					      WLAN_EID_COUNTRY);
4655 		is_11d = country_ie ? 1 : 0;
4656 		supports_11d = true;
4657 	}
4658 
4659 	memset(&ssid_le, 0, sizeof(ssid_le));
4660 	if (settings->ssid == NULL || settings->ssid_len == 0) {
4661 		ie_offset = DOT11_MGMT_HDR_LEN + DOT11_BCN_PRB_FIXED_LEN;
4662 		ssid_ie = brcmf_parse_tlvs(
4663 				(u8 *)&settings->beacon.head[ie_offset],
4664 				settings->beacon.head_len - ie_offset,
4665 				WLAN_EID_SSID);
4666 		if (!ssid_ie || ssid_ie->len > IEEE80211_MAX_SSID_LEN)
4667 			return -EINVAL;
4668 
4669 		memcpy(ssid_le.SSID, ssid_ie->data, ssid_ie->len);
4670 		ssid_le.SSID_len = cpu_to_le32(ssid_ie->len);
4671 		brcmf_dbg(TRACE, "SSID is (%s) in Head\n", ssid_le.SSID);
4672 	} else {
4673 		memcpy(ssid_le.SSID, settings->ssid, settings->ssid_len);
4674 		ssid_le.SSID_len = cpu_to_le32((u32)settings->ssid_len);
4675 	}
4676 
4677 	if (!mbss) {
4678 		brcmf_set_mpc(ifp, 0);
4679 		brcmf_configure_arp_nd_offload(ifp, false);
4680 	}
4681 
4682 	/* find the RSN_IE */
4683 	rsn_ie = brcmf_parse_tlvs((u8 *)settings->beacon.tail,
4684 				  settings->beacon.tail_len, WLAN_EID_RSN);
4685 
4686 	/* find the WPA_IE */
4687 	wpa_ie = brcmf_find_wpaie((u8 *)settings->beacon.tail,
4688 				  settings->beacon.tail_len);
4689 
4690 	if ((wpa_ie != NULL || rsn_ie != NULL)) {
4691 		brcmf_dbg(TRACE, "WPA(2) IE is found\n");
4692 		if (wpa_ie != NULL) {
4693 			/* WPA IE */
4694 			err = brcmf_configure_wpaie(ifp, wpa_ie, false);
4695 			if (err < 0)
4696 				goto exit;
4697 		} else {
4698 			struct brcmf_vs_tlv *tmp_ie;
4699 
4700 			tmp_ie = (struct brcmf_vs_tlv *)rsn_ie;
4701 
4702 			/* RSN IE */
4703 			err = brcmf_configure_wpaie(ifp, tmp_ie, true);
4704 			if (err < 0)
4705 				goto exit;
4706 		}
4707 	} else {
4708 		brcmf_dbg(TRACE, "No WPA(2) IEs found\n");
4709 		brcmf_configure_opensecurity(ifp);
4710 	}
4711 
4712 	/* Parameters shared by all radio interfaces */
4713 	if (!mbss) {
4714 		if ((supports_11d) && (is_11d != ifp->vif->is_11d)) {
4715 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_REGULATORY,
4716 						    is_11d);
4717 			if (err < 0) {
4718 				bphy_err(drvr, "Regulatory Set Error, %d\n",
4719 					 err);
4720 				goto exit;
4721 			}
4722 		}
4723 		if (settings->beacon_interval) {
4724 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_BCNPRD,
4725 						    settings->beacon_interval);
4726 			if (err < 0) {
4727 				bphy_err(drvr, "Beacon Interval Set Error, %d\n",
4728 					 err);
4729 				goto exit;
4730 			}
4731 		}
4732 		if (settings->dtim_period) {
4733 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_DTIMPRD,
4734 						    settings->dtim_period);
4735 			if (err < 0) {
4736 				bphy_err(drvr, "DTIM Interval Set Error, %d\n",
4737 					 err);
4738 				goto exit;
4739 			}
4740 		}
4741 
4742 		if ((dev_role == NL80211_IFTYPE_AP) &&
4743 		    ((ifp->ifidx == 0) ||
4744 		     (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_RSDB) &&
4745 		      !brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN)))) {
4746 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4747 			if (err < 0) {
4748 				bphy_err(drvr, "BRCMF_C_DOWN error %d\n",
4749 					 err);
4750 				goto exit;
4751 			}
4752 			brcmf_fil_iovar_int_set(ifp, "apsta", 0);
4753 		}
4754 
4755 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, 1);
4756 		if (err < 0) {
4757 			bphy_err(drvr, "SET INFRA error %d\n", err);
4758 			goto exit;
4759 		}
4760 	} else if (WARN_ON(supports_11d && (is_11d != ifp->vif->is_11d))) {
4761 		/* Multiple-BSS should use same 11d configuration */
4762 		err = -EINVAL;
4763 		goto exit;
4764 	}
4765 
4766 	/* Interface specific setup */
4767 	if (dev_role == NL80211_IFTYPE_AP) {
4768 		if ((brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS)) && (!mbss))
4769 			brcmf_fil_iovar_int_set(ifp, "mbss", 1);
4770 
4771 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 1);
4772 		if (err < 0) {
4773 			bphy_err(drvr, "setting AP mode failed %d\n",
4774 				 err);
4775 			goto exit;
4776 		}
4777 		if (!mbss) {
4778 			/* Firmware 10.x requires setting channel after enabling
4779 			 * AP and before bringing interface up.
4780 			 */
4781 			err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
4782 			if (err < 0) {
4783 				bphy_err(drvr, "Set Channel failed: chspec=%d, %d\n",
4784 					 chanspec, err);
4785 				goto exit;
4786 			}
4787 		}
4788 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
4789 		if (err < 0) {
4790 			bphy_err(drvr, "BRCMF_C_UP error (%d)\n", err);
4791 			goto exit;
4792 		}
4793 		/* On DOWN the firmware removes the WEP keys, reconfigure
4794 		 * them if they were set.
4795 		 */
4796 		brcmf_cfg80211_reconfigure_wep(ifp);
4797 
4798 		memset(&join_params, 0, sizeof(join_params));
4799 		/* join parameters starts with ssid */
4800 		memcpy(&join_params.ssid_le, &ssid_le, sizeof(ssid_le));
4801 		/* create softap */
4802 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
4803 					     &join_params, sizeof(join_params));
4804 		if (err < 0) {
4805 			bphy_err(drvr, "SET SSID error (%d)\n", err);
4806 			goto exit;
4807 		}
4808 
4809 		if (settings->hidden_ssid) {
4810 			err = brcmf_fil_iovar_int_set(ifp, "closednet", 1);
4811 			if (err) {
4812 				bphy_err(drvr, "closednet error (%d)\n", err);
4813 				goto exit;
4814 			}
4815 		}
4816 
4817 		brcmf_dbg(TRACE, "AP mode configuration complete\n");
4818 	} else if (dev_role == NL80211_IFTYPE_P2P_GO) {
4819 		err = brcmf_fil_iovar_int_set(ifp, "chanspec", chanspec);
4820 		if (err < 0) {
4821 			bphy_err(drvr, "Set Channel failed: chspec=%d, %d\n",
4822 				 chanspec, err);
4823 			goto exit;
4824 		}
4825 		err = brcmf_fil_bsscfg_data_set(ifp, "ssid", &ssid_le,
4826 						sizeof(ssid_le));
4827 		if (err < 0) {
4828 			bphy_err(drvr, "setting ssid failed %d\n", err);
4829 			goto exit;
4830 		}
4831 		bss_enable.bsscfgidx = cpu_to_le32(ifp->bsscfgidx);
4832 		bss_enable.enable = cpu_to_le32(1);
4833 		err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
4834 					       sizeof(bss_enable));
4835 		if (err < 0) {
4836 			bphy_err(drvr, "bss_enable config failed %d\n", err);
4837 			goto exit;
4838 		}
4839 
4840 		brcmf_dbg(TRACE, "GO mode configuration complete\n");
4841 	} else {
4842 		WARN_ON(1);
4843 	}
4844 
4845 	brcmf_config_ap_mgmt_ie(ifp->vif, &settings->beacon);
4846 	set_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
4847 	brcmf_net_setcarrier(ifp, true);
4848 
4849 exit:
4850 	if ((err) && (!mbss)) {
4851 		brcmf_set_mpc(ifp, 1);
4852 		brcmf_configure_arp_nd_offload(ifp, true);
4853 	}
4854 	return err;
4855 }
4856 
4857 static int brcmf_cfg80211_stop_ap(struct wiphy *wiphy, struct net_device *ndev)
4858 {
4859 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4860 	struct brcmf_if *ifp = netdev_priv(ndev);
4861 	struct brcmf_pub *drvr = cfg->pub;
4862 	s32 err;
4863 	struct brcmf_fil_bss_enable_le bss_enable;
4864 	struct brcmf_join_params join_params;
4865 
4866 	brcmf_dbg(TRACE, "Enter\n");
4867 
4868 	if (ifp->vif->wdev.iftype == NL80211_IFTYPE_AP) {
4869 		/* Due to most likely deauths outstanding we sleep */
4870 		/* first to make sure they get processed by fw. */
4871 		msleep(400);
4872 
4873 		if (ifp->vif->mbss) {
4874 			err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4875 			return err;
4876 		}
4877 
4878 		/* First BSS doesn't get a full reset */
4879 		if (ifp->bsscfgidx == 0)
4880 			brcmf_fil_iovar_int_set(ifp, "closednet", 0);
4881 
4882 		memset(&join_params, 0, sizeof(join_params));
4883 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SSID,
4884 					     &join_params, sizeof(join_params));
4885 		if (err < 0)
4886 			bphy_err(drvr, "SET SSID error (%d)\n", err);
4887 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_DOWN, 1);
4888 		if (err < 0)
4889 			bphy_err(drvr, "BRCMF_C_DOWN error %d\n", err);
4890 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_AP, 0);
4891 		if (err < 0)
4892 			bphy_err(drvr, "setting AP mode failed %d\n", err);
4893 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS))
4894 			brcmf_fil_iovar_int_set(ifp, "mbss", 0);
4895 		brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_REGULATORY,
4896 				      ifp->vif->is_11d);
4897 		/* Bring device back up so it can be used again */
4898 		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 1);
4899 		if (err < 0)
4900 			bphy_err(drvr, "BRCMF_C_UP error %d\n", err);
4901 
4902 		brcmf_vif_clear_mgmt_ies(ifp->vif);
4903 	} else {
4904 		bss_enable.bsscfgidx = cpu_to_le32(ifp->bsscfgidx);
4905 		bss_enable.enable = cpu_to_le32(0);
4906 		err = brcmf_fil_iovar_data_set(ifp, "bss", &bss_enable,
4907 					       sizeof(bss_enable));
4908 		if (err < 0)
4909 			bphy_err(drvr, "bss_enable config failed %d\n", err);
4910 	}
4911 	brcmf_set_mpc(ifp, 1);
4912 	brcmf_configure_arp_nd_offload(ifp, true);
4913 	clear_bit(BRCMF_VIF_STATUS_AP_CREATED, &ifp->vif->sme_state);
4914 	brcmf_net_setcarrier(ifp, false);
4915 
4916 	return err;
4917 }
4918 
4919 static s32
4920 brcmf_cfg80211_change_beacon(struct wiphy *wiphy, struct net_device *ndev,
4921 			     struct cfg80211_beacon_data *info)
4922 {
4923 	struct brcmf_if *ifp = netdev_priv(ndev);
4924 	s32 err;
4925 
4926 	brcmf_dbg(TRACE, "Enter\n");
4927 
4928 	err = brcmf_config_ap_mgmt_ie(ifp->vif, info);
4929 
4930 	return err;
4931 }
4932 
4933 static int
4934 brcmf_cfg80211_del_station(struct wiphy *wiphy, struct net_device *ndev,
4935 			   struct station_del_parameters *params)
4936 {
4937 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4938 	struct brcmf_pub *drvr = cfg->pub;
4939 	struct brcmf_scb_val_le scbval;
4940 	struct brcmf_if *ifp = netdev_priv(ndev);
4941 	s32 err;
4942 
4943 	if (!params->mac)
4944 		return -EFAULT;
4945 
4946 	brcmf_dbg(TRACE, "Enter %pM\n", params->mac);
4947 
4948 	if (ifp->vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
4949 		ifp = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif->ifp;
4950 	if (!check_vif_up(ifp->vif))
4951 		return -EIO;
4952 
4953 	memcpy(&scbval.ea, params->mac, ETH_ALEN);
4954 	scbval.val = cpu_to_le32(params->reason_code);
4955 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCB_DEAUTHENTICATE_FOR_REASON,
4956 				     &scbval, sizeof(scbval));
4957 	if (err)
4958 		bphy_err(drvr, "SCB_DEAUTHENTICATE_FOR_REASON failed %d\n",
4959 			 err);
4960 
4961 	brcmf_dbg(TRACE, "Exit\n");
4962 	return err;
4963 }
4964 
4965 static int
4966 brcmf_cfg80211_change_station(struct wiphy *wiphy, struct net_device *ndev,
4967 			      const u8 *mac, struct station_parameters *params)
4968 {
4969 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
4970 	struct brcmf_pub *drvr = cfg->pub;
4971 	struct brcmf_if *ifp = netdev_priv(ndev);
4972 	s32 err;
4973 
4974 	brcmf_dbg(TRACE, "Enter, MAC %pM, mask 0x%04x set 0x%04x\n", mac,
4975 		  params->sta_flags_mask, params->sta_flags_set);
4976 
4977 	/* Ignore all 00 MAC */
4978 	if (is_zero_ether_addr(mac))
4979 		return 0;
4980 
4981 	if (!(params->sta_flags_mask & BIT(NL80211_STA_FLAG_AUTHORIZED)))
4982 		return 0;
4983 
4984 	if (params->sta_flags_set & BIT(NL80211_STA_FLAG_AUTHORIZED))
4985 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SCB_AUTHORIZE,
4986 					     (void *)mac, ETH_ALEN);
4987 	else
4988 		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_SCB_DEAUTHORIZE,
4989 					     (void *)mac, ETH_ALEN);
4990 	if (err < 0)
4991 		bphy_err(drvr, "Setting SCB (de-)authorize failed, %d\n", err);
4992 
4993 	return err;
4994 }
4995 
4996 static void
4997 brcmf_cfg80211_mgmt_frame_register(struct wiphy *wiphy,
4998 				   struct wireless_dev *wdev,
4999 				   u16 frame_type, bool reg)
5000 {
5001 	struct brcmf_cfg80211_vif *vif;
5002 	u16 mgmt_type;
5003 
5004 	brcmf_dbg(TRACE, "Enter, frame_type %04x, reg=%d\n", frame_type, reg);
5005 
5006 	mgmt_type = (frame_type & IEEE80211_FCTL_STYPE) >> 4;
5007 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5008 	if (reg)
5009 		vif->mgmt_rx_reg |= BIT(mgmt_type);
5010 	else
5011 		vif->mgmt_rx_reg &= ~BIT(mgmt_type);
5012 }
5013 
5014 
5015 static int
5016 brcmf_cfg80211_mgmt_tx(struct wiphy *wiphy, struct wireless_dev *wdev,
5017 		       struct cfg80211_mgmt_tx_params *params, u64 *cookie)
5018 {
5019 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5020 	struct ieee80211_channel *chan = params->chan;
5021 	struct brcmf_pub *drvr = cfg->pub;
5022 	const u8 *buf = params->buf;
5023 	size_t len = params->len;
5024 	const struct ieee80211_mgmt *mgmt;
5025 	struct brcmf_cfg80211_vif *vif;
5026 	s32 err = 0;
5027 	s32 ie_offset;
5028 	s32 ie_len;
5029 	struct brcmf_fil_action_frame_le *action_frame;
5030 	struct brcmf_fil_af_params_le *af_params;
5031 	bool ack;
5032 	s32 chan_nr;
5033 	u32 freq;
5034 
5035 	brcmf_dbg(TRACE, "Enter\n");
5036 
5037 	*cookie = 0;
5038 
5039 	mgmt = (const struct ieee80211_mgmt *)buf;
5040 
5041 	if (!ieee80211_is_mgmt(mgmt->frame_control)) {
5042 		bphy_err(drvr, "Driver only allows MGMT packet type\n");
5043 		return -EPERM;
5044 	}
5045 
5046 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5047 
5048 	if (ieee80211_is_probe_resp(mgmt->frame_control)) {
5049 		/* Right now the only reason to get a probe response */
5050 		/* is for p2p listen response or for p2p GO from     */
5051 		/* wpa_supplicant. Unfortunately the probe is send   */
5052 		/* on primary ndev, while dongle wants it on the p2p */
5053 		/* vif. Since this is only reason for a probe        */
5054 		/* response to be sent, the vif is taken from cfg.   */
5055 		/* If ever desired to send proberesp for non p2p     */
5056 		/* response then data should be checked for          */
5057 		/* "DIRECT-". Note in future supplicant will take    */
5058 		/* dedicated p2p wdev to do this and then this 'hack'*/
5059 		/* is not needed anymore.                            */
5060 		ie_offset =  DOT11_MGMT_HDR_LEN +
5061 			     DOT11_BCN_PRB_FIXED_LEN;
5062 		ie_len = len - ie_offset;
5063 		if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif)
5064 			vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
5065 		err = brcmf_vif_set_mgmt_ie(vif,
5066 					    BRCMF_VNDR_IE_PRBRSP_FLAG,
5067 					    &buf[ie_offset],
5068 					    ie_len);
5069 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, true,
5070 					GFP_KERNEL);
5071 	} else if (ieee80211_is_action(mgmt->frame_control)) {
5072 		if (len > BRCMF_FIL_ACTION_FRAME_SIZE + DOT11_MGMT_HDR_LEN) {
5073 			bphy_err(drvr, "invalid action frame length\n");
5074 			err = -EINVAL;
5075 			goto exit;
5076 		}
5077 		af_params = kzalloc(sizeof(*af_params), GFP_KERNEL);
5078 		if (af_params == NULL) {
5079 			bphy_err(drvr, "unable to allocate frame\n");
5080 			err = -ENOMEM;
5081 			goto exit;
5082 		}
5083 		action_frame = &af_params->action_frame;
5084 		/* Add the packet Id */
5085 		action_frame->packet_id = cpu_to_le32(*cookie);
5086 		/* Add BSSID */
5087 		memcpy(&action_frame->da[0], &mgmt->da[0], ETH_ALEN);
5088 		memcpy(&af_params->bssid[0], &mgmt->bssid[0], ETH_ALEN);
5089 		/* Add the length exepted for 802.11 header  */
5090 		action_frame->len = cpu_to_le16(len - DOT11_MGMT_HDR_LEN);
5091 		/* Add the channel. Use the one specified as parameter if any or
5092 		 * the current one (got from the firmware) otherwise
5093 		 */
5094 		if (chan)
5095 			freq = chan->center_freq;
5096 		else
5097 			brcmf_fil_cmd_int_get(vif->ifp, BRCMF_C_GET_CHANNEL,
5098 					      &freq);
5099 		chan_nr = ieee80211_frequency_to_channel(freq);
5100 		af_params->channel = cpu_to_le32(chan_nr);
5101 
5102 		memcpy(action_frame->data, &buf[DOT11_MGMT_HDR_LEN],
5103 		       le16_to_cpu(action_frame->len));
5104 
5105 		brcmf_dbg(TRACE, "Action frame, cookie=%lld, len=%d, freq=%d\n",
5106 			  *cookie, le16_to_cpu(action_frame->len), freq);
5107 
5108 		ack = brcmf_p2p_send_action_frame(cfg, cfg_to_ndev(cfg),
5109 						  af_params);
5110 
5111 		cfg80211_mgmt_tx_status(wdev, *cookie, buf, len, ack,
5112 					GFP_KERNEL);
5113 		kfree(af_params);
5114 	} else {
5115 		brcmf_dbg(TRACE, "Unhandled, fc=%04x!!\n", mgmt->frame_control);
5116 		brcmf_dbg_hex_dump(true, buf, len, "payload, len=%zu\n", len);
5117 	}
5118 
5119 exit:
5120 	return err;
5121 }
5122 
5123 
5124 static int
5125 brcmf_cfg80211_cancel_remain_on_channel(struct wiphy *wiphy,
5126 					struct wireless_dev *wdev,
5127 					u64 cookie)
5128 {
5129 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5130 	struct brcmf_pub *drvr = cfg->pub;
5131 	struct brcmf_cfg80211_vif *vif;
5132 	int err = 0;
5133 
5134 	brcmf_dbg(TRACE, "Enter p2p listen cancel\n");
5135 
5136 	vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif;
5137 	if (vif == NULL) {
5138 		bphy_err(drvr, "No p2p device available for probe response\n");
5139 		err = -ENODEV;
5140 		goto exit;
5141 	}
5142 	brcmf_p2p_cancel_remain_on_channel(vif->ifp);
5143 exit:
5144 	return err;
5145 }
5146 
5147 static int brcmf_cfg80211_get_channel(struct wiphy *wiphy,
5148 				      struct wireless_dev *wdev,
5149 				      struct cfg80211_chan_def *chandef)
5150 {
5151 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5152 	struct net_device *ndev = wdev->netdev;
5153 	struct brcmf_pub *drvr = cfg->pub;
5154 	struct brcmu_chan ch;
5155 	enum nl80211_band band = 0;
5156 	enum nl80211_chan_width width = 0;
5157 	u32 chanspec;
5158 	int freq, err;
5159 
5160 	if (!ndev || drvr->bus_if->state != BRCMF_BUS_UP)
5161 		return -ENODEV;
5162 
5163 	err = brcmf_fil_iovar_int_get(netdev_priv(ndev), "chanspec", &chanspec);
5164 	if (err) {
5165 		bphy_err(drvr, "chanspec failed (%d)\n", err);
5166 		return err;
5167 	}
5168 
5169 	ch.chspec = chanspec;
5170 	cfg->d11inf.decchspec(&ch);
5171 
5172 	switch (ch.band) {
5173 	case BRCMU_CHAN_BAND_2G:
5174 		band = NL80211_BAND_2GHZ;
5175 		break;
5176 	case BRCMU_CHAN_BAND_5G:
5177 		band = NL80211_BAND_5GHZ;
5178 		break;
5179 	}
5180 
5181 	switch (ch.bw) {
5182 	case BRCMU_CHAN_BW_80:
5183 		width = NL80211_CHAN_WIDTH_80;
5184 		break;
5185 	case BRCMU_CHAN_BW_40:
5186 		width = NL80211_CHAN_WIDTH_40;
5187 		break;
5188 	case BRCMU_CHAN_BW_20:
5189 		width = NL80211_CHAN_WIDTH_20;
5190 		break;
5191 	case BRCMU_CHAN_BW_80P80:
5192 		width = NL80211_CHAN_WIDTH_80P80;
5193 		break;
5194 	case BRCMU_CHAN_BW_160:
5195 		width = NL80211_CHAN_WIDTH_160;
5196 		break;
5197 	}
5198 
5199 	freq = ieee80211_channel_to_frequency(ch.control_ch_num, band);
5200 	chandef->chan = ieee80211_get_channel(wiphy, freq);
5201 	chandef->width = width;
5202 	chandef->center_freq1 = ieee80211_channel_to_frequency(ch.chnum, band);
5203 	chandef->center_freq2 = 0;
5204 
5205 	return 0;
5206 }
5207 
5208 static int brcmf_cfg80211_crit_proto_start(struct wiphy *wiphy,
5209 					   struct wireless_dev *wdev,
5210 					   enum nl80211_crit_proto_id proto,
5211 					   u16 duration)
5212 {
5213 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5214 	struct brcmf_cfg80211_vif *vif;
5215 
5216 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5217 
5218 	/* only DHCP support for now */
5219 	if (proto != NL80211_CRIT_PROTO_DHCP)
5220 		return -EINVAL;
5221 
5222 	/* suppress and abort scanning */
5223 	set_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
5224 	brcmf_abort_scanning(cfg);
5225 
5226 	return brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_DISABLED, duration);
5227 }
5228 
5229 static void brcmf_cfg80211_crit_proto_stop(struct wiphy *wiphy,
5230 					   struct wireless_dev *wdev)
5231 {
5232 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5233 	struct brcmf_cfg80211_vif *vif;
5234 
5235 	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
5236 
5237 	brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
5238 	clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
5239 }
5240 
5241 static s32
5242 brcmf_notify_tdls_peer_event(struct brcmf_if *ifp,
5243 			     const struct brcmf_event_msg *e, void *data)
5244 {
5245 	switch (e->reason) {
5246 	case BRCMF_E_REASON_TDLS_PEER_DISCOVERED:
5247 		brcmf_dbg(TRACE, "TDLS Peer Discovered\n");
5248 		break;
5249 	case BRCMF_E_REASON_TDLS_PEER_CONNECTED:
5250 		brcmf_dbg(TRACE, "TDLS Peer Connected\n");
5251 		brcmf_proto_add_tdls_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
5252 		break;
5253 	case BRCMF_E_REASON_TDLS_PEER_DISCONNECTED:
5254 		brcmf_dbg(TRACE, "TDLS Peer Disconnected\n");
5255 		brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
5256 		break;
5257 	}
5258 
5259 	return 0;
5260 }
5261 
5262 static int brcmf_convert_nl80211_tdls_oper(enum nl80211_tdls_operation oper)
5263 {
5264 	int ret;
5265 
5266 	switch (oper) {
5267 	case NL80211_TDLS_DISCOVERY_REQ:
5268 		ret = BRCMF_TDLS_MANUAL_EP_DISCOVERY;
5269 		break;
5270 	case NL80211_TDLS_SETUP:
5271 		ret = BRCMF_TDLS_MANUAL_EP_CREATE;
5272 		break;
5273 	case NL80211_TDLS_TEARDOWN:
5274 		ret = BRCMF_TDLS_MANUAL_EP_DELETE;
5275 		break;
5276 	default:
5277 		brcmf_err("unsupported operation: %d\n", oper);
5278 		ret = -EOPNOTSUPP;
5279 	}
5280 	return ret;
5281 }
5282 
5283 static int brcmf_cfg80211_tdls_oper(struct wiphy *wiphy,
5284 				    struct net_device *ndev, const u8 *peer,
5285 				    enum nl80211_tdls_operation oper)
5286 {
5287 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5288 	struct brcmf_pub *drvr = cfg->pub;
5289 	struct brcmf_if *ifp;
5290 	struct brcmf_tdls_iovar_le info;
5291 	int ret = 0;
5292 
5293 	ret = brcmf_convert_nl80211_tdls_oper(oper);
5294 	if (ret < 0)
5295 		return ret;
5296 
5297 	ifp = netdev_priv(ndev);
5298 	memset(&info, 0, sizeof(info));
5299 	info.mode = (u8)ret;
5300 	if (peer)
5301 		memcpy(info.ea, peer, ETH_ALEN);
5302 
5303 	ret = brcmf_fil_iovar_data_set(ifp, "tdls_endpoint",
5304 				       &info, sizeof(info));
5305 	if (ret < 0)
5306 		bphy_err(drvr, "tdls_endpoint iovar failed: ret=%d\n", ret);
5307 
5308 	return ret;
5309 }
5310 
5311 static int
5312 brcmf_cfg80211_update_conn_params(struct wiphy *wiphy,
5313 				  struct net_device *ndev,
5314 				  struct cfg80211_connect_params *sme,
5315 				  u32 changed)
5316 {
5317 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5318 	struct brcmf_pub *drvr = cfg->pub;
5319 	struct brcmf_if *ifp;
5320 	int err;
5321 
5322 	if (!(changed & UPDATE_ASSOC_IES))
5323 		return 0;
5324 
5325 	ifp = netdev_priv(ndev);
5326 	err = brcmf_vif_set_mgmt_ie(ifp->vif, BRCMF_VNDR_IE_ASSOCREQ_FLAG,
5327 				    sme->ie, sme->ie_len);
5328 	if (err)
5329 		bphy_err(drvr, "Set Assoc REQ IE Failed\n");
5330 	else
5331 		brcmf_dbg(TRACE, "Applied Vndr IEs for Assoc request\n");
5332 
5333 	return err;
5334 }
5335 
5336 #ifdef CONFIG_PM
5337 static int
5338 brcmf_cfg80211_set_rekey_data(struct wiphy *wiphy, struct net_device *ndev,
5339 			      struct cfg80211_gtk_rekey_data *gtk)
5340 {
5341 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
5342 	struct brcmf_pub *drvr = cfg->pub;
5343 	struct brcmf_if *ifp = netdev_priv(ndev);
5344 	struct brcmf_gtk_keyinfo_le gtk_le;
5345 	int ret;
5346 
5347 	brcmf_dbg(TRACE, "Enter, bssidx=%d\n", ifp->bsscfgidx);
5348 
5349 	memcpy(gtk_le.kck, gtk->kck, sizeof(gtk_le.kck));
5350 	memcpy(gtk_le.kek, gtk->kek, sizeof(gtk_le.kek));
5351 	memcpy(gtk_le.replay_counter, gtk->replay_ctr,
5352 	       sizeof(gtk_le.replay_counter));
5353 
5354 	ret = brcmf_fil_iovar_data_set(ifp, "gtk_key_info", &gtk_le,
5355 				       sizeof(gtk_le));
5356 	if (ret < 0)
5357 		bphy_err(drvr, "gtk_key_info iovar failed: ret=%d\n", ret);
5358 
5359 	return ret;
5360 }
5361 #endif
5362 
5363 static int brcmf_cfg80211_set_pmk(struct wiphy *wiphy, struct net_device *dev,
5364 				  const struct cfg80211_pmk_conf *conf)
5365 {
5366 	struct brcmf_if *ifp;
5367 
5368 	brcmf_dbg(TRACE, "enter\n");
5369 
5370 	/* expect using firmware supplicant for 1X */
5371 	ifp = netdev_priv(dev);
5372 	if (WARN_ON(ifp->vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_1X))
5373 		return -EINVAL;
5374 
5375 	if (conf->pmk_len > BRCMF_WSEC_MAX_PSK_LEN)
5376 		return -ERANGE;
5377 
5378 	return brcmf_set_pmk(ifp, conf->pmk, conf->pmk_len);
5379 }
5380 
5381 static int brcmf_cfg80211_del_pmk(struct wiphy *wiphy, struct net_device *dev,
5382 				  const u8 *aa)
5383 {
5384 	struct brcmf_if *ifp;
5385 
5386 	brcmf_dbg(TRACE, "enter\n");
5387 	ifp = netdev_priv(dev);
5388 	if (WARN_ON(ifp->vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_1X))
5389 		return -EINVAL;
5390 
5391 	return brcmf_set_pmk(ifp, NULL, 0);
5392 }
5393 
5394 static struct cfg80211_ops brcmf_cfg80211_ops = {
5395 	.add_virtual_intf = brcmf_cfg80211_add_iface,
5396 	.del_virtual_intf = brcmf_cfg80211_del_iface,
5397 	.change_virtual_intf = brcmf_cfg80211_change_iface,
5398 	.scan = brcmf_cfg80211_scan,
5399 	.set_wiphy_params = brcmf_cfg80211_set_wiphy_params,
5400 	.join_ibss = brcmf_cfg80211_join_ibss,
5401 	.leave_ibss = brcmf_cfg80211_leave_ibss,
5402 	.get_station = brcmf_cfg80211_get_station,
5403 	.dump_station = brcmf_cfg80211_dump_station,
5404 	.set_tx_power = brcmf_cfg80211_set_tx_power,
5405 	.get_tx_power = brcmf_cfg80211_get_tx_power,
5406 	.add_key = brcmf_cfg80211_add_key,
5407 	.del_key = brcmf_cfg80211_del_key,
5408 	.get_key = brcmf_cfg80211_get_key,
5409 	.set_default_key = brcmf_cfg80211_config_default_key,
5410 	.set_default_mgmt_key = brcmf_cfg80211_config_default_mgmt_key,
5411 	.set_power_mgmt = brcmf_cfg80211_set_power_mgmt,
5412 	.connect = brcmf_cfg80211_connect,
5413 	.disconnect = brcmf_cfg80211_disconnect,
5414 	.suspend = brcmf_cfg80211_suspend,
5415 	.resume = brcmf_cfg80211_resume,
5416 	.set_pmksa = brcmf_cfg80211_set_pmksa,
5417 	.del_pmksa = brcmf_cfg80211_del_pmksa,
5418 	.flush_pmksa = brcmf_cfg80211_flush_pmksa,
5419 	.start_ap = brcmf_cfg80211_start_ap,
5420 	.stop_ap = brcmf_cfg80211_stop_ap,
5421 	.change_beacon = brcmf_cfg80211_change_beacon,
5422 	.del_station = brcmf_cfg80211_del_station,
5423 	.change_station = brcmf_cfg80211_change_station,
5424 	.sched_scan_start = brcmf_cfg80211_sched_scan_start,
5425 	.sched_scan_stop = brcmf_cfg80211_sched_scan_stop,
5426 	.mgmt_frame_register = brcmf_cfg80211_mgmt_frame_register,
5427 	.mgmt_tx = brcmf_cfg80211_mgmt_tx,
5428 	.remain_on_channel = brcmf_p2p_remain_on_channel,
5429 	.cancel_remain_on_channel = brcmf_cfg80211_cancel_remain_on_channel,
5430 	.get_channel = brcmf_cfg80211_get_channel,
5431 	.start_p2p_device = brcmf_p2p_start_device,
5432 	.stop_p2p_device = brcmf_p2p_stop_device,
5433 	.crit_proto_start = brcmf_cfg80211_crit_proto_start,
5434 	.crit_proto_stop = brcmf_cfg80211_crit_proto_stop,
5435 	.tdls_oper = brcmf_cfg80211_tdls_oper,
5436 	.update_connect_params = brcmf_cfg80211_update_conn_params,
5437 	.set_pmk = brcmf_cfg80211_set_pmk,
5438 	.del_pmk = brcmf_cfg80211_del_pmk,
5439 };
5440 
5441 struct cfg80211_ops *brcmf_cfg80211_get_ops(struct brcmf_mp_device *settings)
5442 {
5443 	struct cfg80211_ops *ops;
5444 
5445 	ops = kmemdup(&brcmf_cfg80211_ops, sizeof(brcmf_cfg80211_ops),
5446 		       GFP_KERNEL);
5447 
5448 	if (ops && settings->roamoff)
5449 		ops->update_connect_params = NULL;
5450 
5451 	return ops;
5452 }
5453 
5454 struct brcmf_cfg80211_vif *brcmf_alloc_vif(struct brcmf_cfg80211_info *cfg,
5455 					   enum nl80211_iftype type)
5456 {
5457 	struct brcmf_cfg80211_vif *vif_walk;
5458 	struct brcmf_cfg80211_vif *vif;
5459 	bool mbss;
5460 	struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
5461 
5462 	brcmf_dbg(TRACE, "allocating virtual interface (size=%zu)\n",
5463 		  sizeof(*vif));
5464 	vif = kzalloc(sizeof(*vif), GFP_KERNEL);
5465 	if (!vif)
5466 		return ERR_PTR(-ENOMEM);
5467 
5468 	vif->wdev.wiphy = cfg->wiphy;
5469 	vif->wdev.iftype = type;
5470 
5471 	brcmf_init_prof(&vif->profile);
5472 
5473 	if (type == NL80211_IFTYPE_AP &&
5474 	    brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS)) {
5475 		mbss = false;
5476 		list_for_each_entry(vif_walk, &cfg->vif_list, list) {
5477 			if (vif_walk->wdev.iftype == NL80211_IFTYPE_AP) {
5478 				mbss = true;
5479 				break;
5480 			}
5481 		}
5482 		vif->mbss = mbss;
5483 	}
5484 
5485 	list_add_tail(&vif->list, &cfg->vif_list);
5486 	return vif;
5487 }
5488 
5489 void brcmf_free_vif(struct brcmf_cfg80211_vif *vif)
5490 {
5491 	list_del(&vif->list);
5492 	kfree(vif);
5493 }
5494 
5495 void brcmf_cfg80211_free_netdev(struct net_device *ndev)
5496 {
5497 	struct brcmf_cfg80211_vif *vif;
5498 	struct brcmf_if *ifp;
5499 
5500 	ifp = netdev_priv(ndev);
5501 	vif = ifp->vif;
5502 
5503 	if (vif)
5504 		brcmf_free_vif(vif);
5505 }
5506 
5507 static bool brcmf_is_linkup(struct brcmf_cfg80211_vif *vif,
5508 			    const struct brcmf_event_msg *e)
5509 {
5510 	u32 event = e->event_code;
5511 	u32 status = e->status;
5512 
5513 	if (vif->profile.use_fwsup == BRCMF_PROFILE_FWSUP_PSK &&
5514 	    event == BRCMF_E_PSK_SUP &&
5515 	    status == BRCMF_E_STATUS_FWSUP_COMPLETED)
5516 		set_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state);
5517 	if (event == BRCMF_E_SET_SSID && status == BRCMF_E_STATUS_SUCCESS) {
5518 		brcmf_dbg(CONN, "Processing set ssid\n");
5519 		memcpy(vif->profile.bssid, e->addr, ETH_ALEN);
5520 		if (vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_PSK &&
5521 		    vif->profile.use_fwsup != BRCMF_PROFILE_FWSUP_SAE)
5522 			return true;
5523 
5524 		set_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state);
5525 	}
5526 
5527 	if (test_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state) &&
5528 	    test_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state)) {
5529 		clear_bit(BRCMF_VIF_STATUS_EAP_SUCCESS, &vif->sme_state);
5530 		clear_bit(BRCMF_VIF_STATUS_ASSOC_SUCCESS, &vif->sme_state);
5531 		return true;
5532 	}
5533 	return false;
5534 }
5535 
5536 static bool brcmf_is_linkdown(const struct brcmf_event_msg *e)
5537 {
5538 	u32 event = e->event_code;
5539 	u16 flags = e->flags;
5540 
5541 	if ((event == BRCMF_E_DEAUTH) || (event == BRCMF_E_DEAUTH_IND) ||
5542 	    (event == BRCMF_E_DISASSOC_IND) ||
5543 	    ((event == BRCMF_E_LINK) && (!(flags & BRCMF_EVENT_MSG_LINK)))) {
5544 		brcmf_dbg(CONN, "Processing link down\n");
5545 		return true;
5546 	}
5547 	return false;
5548 }
5549 
5550 static bool brcmf_is_nonetwork(struct brcmf_cfg80211_info *cfg,
5551 			       const struct brcmf_event_msg *e)
5552 {
5553 	u32 event = e->event_code;
5554 	u32 status = e->status;
5555 
5556 	if (event == BRCMF_E_LINK && status == BRCMF_E_STATUS_NO_NETWORKS) {
5557 		brcmf_dbg(CONN, "Processing Link %s & no network found\n",
5558 			  e->flags & BRCMF_EVENT_MSG_LINK ? "up" : "down");
5559 		return true;
5560 	}
5561 
5562 	if (event == BRCMF_E_SET_SSID && status != BRCMF_E_STATUS_SUCCESS) {
5563 		brcmf_dbg(CONN, "Processing connecting & no network found\n");
5564 		return true;
5565 	}
5566 
5567 	if (event == BRCMF_E_PSK_SUP &&
5568 	    status != BRCMF_E_STATUS_FWSUP_COMPLETED) {
5569 		brcmf_dbg(CONN, "Processing failed supplicant state: %u\n",
5570 			  status);
5571 		return true;
5572 	}
5573 
5574 	return false;
5575 }
5576 
5577 static void brcmf_clear_assoc_ies(struct brcmf_cfg80211_info *cfg)
5578 {
5579 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5580 
5581 	kfree(conn_info->req_ie);
5582 	conn_info->req_ie = NULL;
5583 	conn_info->req_ie_len = 0;
5584 	kfree(conn_info->resp_ie);
5585 	conn_info->resp_ie = NULL;
5586 	conn_info->resp_ie_len = 0;
5587 }
5588 
5589 static s32 brcmf_get_assoc_ies(struct brcmf_cfg80211_info *cfg,
5590 			       struct brcmf_if *ifp)
5591 {
5592 	struct brcmf_pub *drvr = cfg->pub;
5593 	struct brcmf_cfg80211_assoc_ielen_le *assoc_info;
5594 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5595 	u32 req_len;
5596 	u32 resp_len;
5597 	s32 err = 0;
5598 
5599 	brcmf_clear_assoc_ies(cfg);
5600 
5601 	err = brcmf_fil_iovar_data_get(ifp, "assoc_info",
5602 				       cfg->extra_buf, WL_ASSOC_INFO_MAX);
5603 	if (err) {
5604 		bphy_err(drvr, "could not get assoc info (%d)\n", err);
5605 		return err;
5606 	}
5607 	assoc_info =
5608 		(struct brcmf_cfg80211_assoc_ielen_le *)cfg->extra_buf;
5609 	req_len = le32_to_cpu(assoc_info->req_len);
5610 	resp_len = le32_to_cpu(assoc_info->resp_len);
5611 	if (req_len) {
5612 		err = brcmf_fil_iovar_data_get(ifp, "assoc_req_ies",
5613 					       cfg->extra_buf,
5614 					       WL_ASSOC_INFO_MAX);
5615 		if (err) {
5616 			bphy_err(drvr, "could not get assoc req (%d)\n", err);
5617 			return err;
5618 		}
5619 		conn_info->req_ie_len = req_len;
5620 		conn_info->req_ie =
5621 		    kmemdup(cfg->extra_buf, conn_info->req_ie_len,
5622 			    GFP_KERNEL);
5623 		if (!conn_info->req_ie)
5624 			conn_info->req_ie_len = 0;
5625 	} else {
5626 		conn_info->req_ie_len = 0;
5627 		conn_info->req_ie = NULL;
5628 	}
5629 	if (resp_len) {
5630 		err = brcmf_fil_iovar_data_get(ifp, "assoc_resp_ies",
5631 					       cfg->extra_buf,
5632 					       WL_ASSOC_INFO_MAX);
5633 		if (err) {
5634 			bphy_err(drvr, "could not get assoc resp (%d)\n", err);
5635 			return err;
5636 		}
5637 		conn_info->resp_ie_len = resp_len;
5638 		conn_info->resp_ie =
5639 		    kmemdup(cfg->extra_buf, conn_info->resp_ie_len,
5640 			    GFP_KERNEL);
5641 		if (!conn_info->resp_ie)
5642 			conn_info->resp_ie_len = 0;
5643 	} else {
5644 		conn_info->resp_ie_len = 0;
5645 		conn_info->resp_ie = NULL;
5646 	}
5647 	brcmf_dbg(CONN, "req len (%d) resp len (%d)\n",
5648 		  conn_info->req_ie_len, conn_info->resp_ie_len);
5649 
5650 	return err;
5651 }
5652 
5653 static s32
5654 brcmf_bss_roaming_done(struct brcmf_cfg80211_info *cfg,
5655 		       struct net_device *ndev,
5656 		       const struct brcmf_event_msg *e)
5657 {
5658 	struct brcmf_if *ifp = netdev_priv(ndev);
5659 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5660 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5661 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
5662 	struct ieee80211_channel *notify_channel = NULL;
5663 	struct ieee80211_supported_band *band;
5664 	struct brcmf_bss_info_le *bi;
5665 	struct brcmu_chan ch;
5666 	struct cfg80211_roam_info roam_info = {};
5667 	u32 freq;
5668 	s32 err = 0;
5669 	u8 *buf;
5670 
5671 	brcmf_dbg(TRACE, "Enter\n");
5672 
5673 	brcmf_get_assoc_ies(cfg, ifp);
5674 	memcpy(profile->bssid, e->addr, ETH_ALEN);
5675 	brcmf_update_bss_info(cfg, ifp);
5676 
5677 	buf = kzalloc(WL_BSS_INFO_MAX, GFP_KERNEL);
5678 	if (buf == NULL) {
5679 		err = -ENOMEM;
5680 		goto done;
5681 	}
5682 
5683 	/* data sent to dongle has to be little endian */
5684 	*(__le32 *)buf = cpu_to_le32(WL_BSS_INFO_MAX);
5685 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BSS_INFO,
5686 				     buf, WL_BSS_INFO_MAX);
5687 
5688 	if (err)
5689 		goto done;
5690 
5691 	bi = (struct brcmf_bss_info_le *)(buf + 4);
5692 	ch.chspec = le16_to_cpu(bi->chanspec);
5693 	cfg->d11inf.decchspec(&ch);
5694 
5695 	if (ch.band == BRCMU_CHAN_BAND_2G)
5696 		band = wiphy->bands[NL80211_BAND_2GHZ];
5697 	else
5698 		band = wiphy->bands[NL80211_BAND_5GHZ];
5699 
5700 	freq = ieee80211_channel_to_frequency(ch.control_ch_num, band->band);
5701 	notify_channel = ieee80211_get_channel(wiphy, freq);
5702 
5703 done:
5704 	kfree(buf);
5705 
5706 	roam_info.channel = notify_channel;
5707 	roam_info.bssid = profile->bssid;
5708 	roam_info.req_ie = conn_info->req_ie;
5709 	roam_info.req_ie_len = conn_info->req_ie_len;
5710 	roam_info.resp_ie = conn_info->resp_ie;
5711 	roam_info.resp_ie_len = conn_info->resp_ie_len;
5712 
5713 	cfg80211_roamed(ndev, &roam_info, GFP_KERNEL);
5714 	brcmf_dbg(CONN, "Report roaming result\n");
5715 
5716 	if (profile->use_fwsup == BRCMF_PROFILE_FWSUP_1X && profile->is_ft) {
5717 		cfg80211_port_authorized(ndev, profile->bssid, GFP_KERNEL);
5718 		brcmf_dbg(CONN, "Report port authorized\n");
5719 	}
5720 
5721 	set_bit(BRCMF_VIF_STATUS_CONNECTED, &ifp->vif->sme_state);
5722 	brcmf_dbg(TRACE, "Exit\n");
5723 	return err;
5724 }
5725 
5726 static s32
5727 brcmf_bss_connect_done(struct brcmf_cfg80211_info *cfg,
5728 		       struct net_device *ndev, const struct brcmf_event_msg *e,
5729 		       bool completed)
5730 {
5731 	struct brcmf_if *ifp = netdev_priv(ndev);
5732 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5733 	struct brcmf_cfg80211_connect_info *conn_info = cfg_to_conn(cfg);
5734 	struct cfg80211_connect_resp_params conn_params;
5735 
5736 	brcmf_dbg(TRACE, "Enter\n");
5737 
5738 	if (test_and_clear_bit(BRCMF_VIF_STATUS_CONNECTING,
5739 			       &ifp->vif->sme_state)) {
5740 		memset(&conn_params, 0, sizeof(conn_params));
5741 		if (completed) {
5742 			brcmf_get_assoc_ies(cfg, ifp);
5743 			brcmf_update_bss_info(cfg, ifp);
5744 			set_bit(BRCMF_VIF_STATUS_CONNECTED,
5745 				&ifp->vif->sme_state);
5746 			conn_params.status = WLAN_STATUS_SUCCESS;
5747 		} else {
5748 			conn_params.status = WLAN_STATUS_AUTH_TIMEOUT;
5749 		}
5750 		conn_params.bssid = profile->bssid;
5751 		conn_params.req_ie = conn_info->req_ie;
5752 		conn_params.req_ie_len = conn_info->req_ie_len;
5753 		conn_params.resp_ie = conn_info->resp_ie;
5754 		conn_params.resp_ie_len = conn_info->resp_ie_len;
5755 		cfg80211_connect_done(ndev, &conn_params, GFP_KERNEL);
5756 		brcmf_dbg(CONN, "Report connect result - connection %s\n",
5757 			  completed ? "succeeded" : "failed");
5758 	}
5759 	brcmf_dbg(TRACE, "Exit\n");
5760 	return 0;
5761 }
5762 
5763 static s32
5764 brcmf_notify_connect_status_ap(struct brcmf_cfg80211_info *cfg,
5765 			       struct net_device *ndev,
5766 			       const struct brcmf_event_msg *e, void *data)
5767 {
5768 	struct brcmf_pub *drvr = cfg->pub;
5769 	static int generation;
5770 	u32 event = e->event_code;
5771 	u32 reason = e->reason;
5772 	struct station_info *sinfo;
5773 
5774 	brcmf_dbg(CONN, "event %s (%u), reason %d\n",
5775 		  brcmf_fweh_event_name(event), event, reason);
5776 	if (event == BRCMF_E_LINK && reason == BRCMF_E_REASON_LINK_BSSCFG_DIS &&
5777 	    ndev != cfg_to_ndev(cfg)) {
5778 		brcmf_dbg(CONN, "AP mode link down\n");
5779 		complete(&cfg->vif_disabled);
5780 		return 0;
5781 	}
5782 
5783 	if (((event == BRCMF_E_ASSOC_IND) || (event == BRCMF_E_REASSOC_IND)) &&
5784 	    (reason == BRCMF_E_STATUS_SUCCESS)) {
5785 		if (!data) {
5786 			bphy_err(drvr, "No IEs present in ASSOC/REASSOC_IND\n");
5787 			return -EINVAL;
5788 		}
5789 
5790 		sinfo = kzalloc(sizeof(*sinfo), GFP_KERNEL);
5791 		if (!sinfo)
5792 			return -ENOMEM;
5793 
5794 		sinfo->assoc_req_ies = data;
5795 		sinfo->assoc_req_ies_len = e->datalen;
5796 		generation++;
5797 		sinfo->generation = generation;
5798 		cfg80211_new_sta(ndev, e->addr, sinfo, GFP_KERNEL);
5799 
5800 		kfree(sinfo);
5801 	} else if ((event == BRCMF_E_DISASSOC_IND) ||
5802 		   (event == BRCMF_E_DEAUTH_IND) ||
5803 		   (event == BRCMF_E_DEAUTH)) {
5804 		cfg80211_del_sta(ndev, e->addr, GFP_KERNEL);
5805 	}
5806 	return 0;
5807 }
5808 
5809 static s32
5810 brcmf_notify_connect_status(struct brcmf_if *ifp,
5811 			    const struct brcmf_event_msg *e, void *data)
5812 {
5813 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5814 	struct net_device *ndev = ifp->ndev;
5815 	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
5816 	struct ieee80211_channel *chan;
5817 	s32 err = 0;
5818 
5819 	if ((e->event_code == BRCMF_E_DEAUTH) ||
5820 	    (e->event_code == BRCMF_E_DEAUTH_IND) ||
5821 	    (e->event_code == BRCMF_E_DISASSOC_IND) ||
5822 	    ((e->event_code == BRCMF_E_LINK) && (!e->flags))) {
5823 		brcmf_proto_delete_peer(ifp->drvr, ifp->ifidx, (u8 *)e->addr);
5824 	}
5825 
5826 	if (brcmf_is_apmode(ifp->vif)) {
5827 		err = brcmf_notify_connect_status_ap(cfg, ndev, e, data);
5828 	} else if (brcmf_is_linkup(ifp->vif, e)) {
5829 		brcmf_dbg(CONN, "Linkup\n");
5830 		if (brcmf_is_ibssmode(ifp->vif)) {
5831 			brcmf_inform_ibss(cfg, ndev, e->addr);
5832 			chan = ieee80211_get_channel(cfg->wiphy, cfg->channel);
5833 			memcpy(profile->bssid, e->addr, ETH_ALEN);
5834 			cfg80211_ibss_joined(ndev, e->addr, chan, GFP_KERNEL);
5835 			clear_bit(BRCMF_VIF_STATUS_CONNECTING,
5836 				  &ifp->vif->sme_state);
5837 			set_bit(BRCMF_VIF_STATUS_CONNECTED,
5838 				&ifp->vif->sme_state);
5839 		} else
5840 			brcmf_bss_connect_done(cfg, ndev, e, true);
5841 		brcmf_net_setcarrier(ifp, true);
5842 	} else if (brcmf_is_linkdown(e)) {
5843 		brcmf_dbg(CONN, "Linkdown\n");
5844 		if (!brcmf_is_ibssmode(ifp->vif)) {
5845 			brcmf_bss_connect_done(cfg, ndev, e, false);
5846 			brcmf_link_down(ifp->vif,
5847 					brcmf_map_fw_linkdown_reason(e));
5848 			brcmf_init_prof(ndev_to_prof(ndev));
5849 			if (ndev != cfg_to_ndev(cfg))
5850 				complete(&cfg->vif_disabled);
5851 			brcmf_net_setcarrier(ifp, false);
5852 		}
5853 	} else if (brcmf_is_nonetwork(cfg, e)) {
5854 		if (brcmf_is_ibssmode(ifp->vif))
5855 			clear_bit(BRCMF_VIF_STATUS_CONNECTING,
5856 				  &ifp->vif->sme_state);
5857 		else
5858 			brcmf_bss_connect_done(cfg, ndev, e, false);
5859 	}
5860 
5861 	return err;
5862 }
5863 
5864 static s32
5865 brcmf_notify_roaming_status(struct brcmf_if *ifp,
5866 			    const struct brcmf_event_msg *e, void *data)
5867 {
5868 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5869 	u32 event = e->event_code;
5870 	u32 status = e->status;
5871 
5872 	if (event == BRCMF_E_ROAM && status == BRCMF_E_STATUS_SUCCESS) {
5873 		if (test_bit(BRCMF_VIF_STATUS_CONNECTED,
5874 			     &ifp->vif->sme_state)) {
5875 			brcmf_bss_roaming_done(cfg, ifp->ndev, e);
5876 		} else {
5877 			brcmf_bss_connect_done(cfg, ifp->ndev, e, true);
5878 			brcmf_net_setcarrier(ifp, true);
5879 		}
5880 	}
5881 
5882 	return 0;
5883 }
5884 
5885 static s32
5886 brcmf_notify_mic_status(struct brcmf_if *ifp,
5887 			const struct brcmf_event_msg *e, void *data)
5888 {
5889 	u16 flags = e->flags;
5890 	enum nl80211_key_type key_type;
5891 
5892 	if (flags & BRCMF_EVENT_MSG_GROUP)
5893 		key_type = NL80211_KEYTYPE_GROUP;
5894 	else
5895 		key_type = NL80211_KEYTYPE_PAIRWISE;
5896 
5897 	cfg80211_michael_mic_failure(ifp->ndev, (u8 *)&e->addr, key_type, -1,
5898 				     NULL, GFP_KERNEL);
5899 
5900 	return 0;
5901 }
5902 
5903 static s32 brcmf_notify_vif_event(struct brcmf_if *ifp,
5904 				  const struct brcmf_event_msg *e, void *data)
5905 {
5906 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
5907 	struct brcmf_if_event *ifevent = (struct brcmf_if_event *)data;
5908 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
5909 	struct brcmf_cfg80211_vif *vif;
5910 
5911 	brcmf_dbg(TRACE, "Enter: action %u flags %u ifidx %u bsscfgidx %u\n",
5912 		  ifevent->action, ifevent->flags, ifevent->ifidx,
5913 		  ifevent->bsscfgidx);
5914 
5915 	spin_lock(&event->vif_event_lock);
5916 	event->action = ifevent->action;
5917 	vif = event->vif;
5918 
5919 	switch (ifevent->action) {
5920 	case BRCMF_E_IF_ADD:
5921 		/* waiting process may have timed out */
5922 		if (!cfg->vif_event.vif) {
5923 			spin_unlock(&event->vif_event_lock);
5924 			return -EBADF;
5925 		}
5926 
5927 		ifp->vif = vif;
5928 		vif->ifp = ifp;
5929 		if (ifp->ndev) {
5930 			vif->wdev.netdev = ifp->ndev;
5931 			ifp->ndev->ieee80211_ptr = &vif->wdev;
5932 			SET_NETDEV_DEV(ifp->ndev, wiphy_dev(cfg->wiphy));
5933 		}
5934 		spin_unlock(&event->vif_event_lock);
5935 		wake_up(&event->vif_wq);
5936 		return 0;
5937 
5938 	case BRCMF_E_IF_DEL:
5939 		spin_unlock(&event->vif_event_lock);
5940 		/* event may not be upon user request */
5941 		if (brcmf_cfg80211_vif_event_armed(cfg))
5942 			wake_up(&event->vif_wq);
5943 		return 0;
5944 
5945 	case BRCMF_E_IF_CHANGE:
5946 		spin_unlock(&event->vif_event_lock);
5947 		wake_up(&event->vif_wq);
5948 		return 0;
5949 
5950 	default:
5951 		spin_unlock(&event->vif_event_lock);
5952 		break;
5953 	}
5954 	return -EINVAL;
5955 }
5956 
5957 static void brcmf_init_conf(struct brcmf_cfg80211_conf *conf)
5958 {
5959 	conf->frag_threshold = (u32)-1;
5960 	conf->rts_threshold = (u32)-1;
5961 	conf->retry_short = (u32)-1;
5962 	conf->retry_long = (u32)-1;
5963 }
5964 
5965 static void brcmf_register_event_handlers(struct brcmf_cfg80211_info *cfg)
5966 {
5967 	brcmf_fweh_register(cfg->pub, BRCMF_E_LINK,
5968 			    brcmf_notify_connect_status);
5969 	brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH_IND,
5970 			    brcmf_notify_connect_status);
5971 	brcmf_fweh_register(cfg->pub, BRCMF_E_DEAUTH,
5972 			    brcmf_notify_connect_status);
5973 	brcmf_fweh_register(cfg->pub, BRCMF_E_DISASSOC_IND,
5974 			    brcmf_notify_connect_status);
5975 	brcmf_fweh_register(cfg->pub, BRCMF_E_ASSOC_IND,
5976 			    brcmf_notify_connect_status);
5977 	brcmf_fweh_register(cfg->pub, BRCMF_E_REASSOC_IND,
5978 			    brcmf_notify_connect_status);
5979 	brcmf_fweh_register(cfg->pub, BRCMF_E_ROAM,
5980 			    brcmf_notify_roaming_status);
5981 	brcmf_fweh_register(cfg->pub, BRCMF_E_MIC_ERROR,
5982 			    brcmf_notify_mic_status);
5983 	brcmf_fweh_register(cfg->pub, BRCMF_E_SET_SSID,
5984 			    brcmf_notify_connect_status);
5985 	brcmf_fweh_register(cfg->pub, BRCMF_E_PFN_NET_FOUND,
5986 			    brcmf_notify_sched_scan_results);
5987 	brcmf_fweh_register(cfg->pub, BRCMF_E_IF,
5988 			    brcmf_notify_vif_event);
5989 	brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_PROBEREQ_MSG,
5990 			    brcmf_p2p_notify_rx_mgmt_p2p_probereq);
5991 	brcmf_fweh_register(cfg->pub, BRCMF_E_P2P_DISC_LISTEN_COMPLETE,
5992 			    brcmf_p2p_notify_listen_complete);
5993 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_RX,
5994 			    brcmf_p2p_notify_action_frame_rx);
5995 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_COMPLETE,
5996 			    brcmf_p2p_notify_action_tx_complete);
5997 	brcmf_fweh_register(cfg->pub, BRCMF_E_ACTION_FRAME_OFF_CHAN_COMPLETE,
5998 			    brcmf_p2p_notify_action_tx_complete);
5999 	brcmf_fweh_register(cfg->pub, BRCMF_E_PSK_SUP,
6000 			    brcmf_notify_connect_status);
6001 }
6002 
6003 static void brcmf_deinit_priv_mem(struct brcmf_cfg80211_info *cfg)
6004 {
6005 	kfree(cfg->conf);
6006 	cfg->conf = NULL;
6007 	kfree(cfg->extra_buf);
6008 	cfg->extra_buf = NULL;
6009 	kfree(cfg->wowl.nd);
6010 	cfg->wowl.nd = NULL;
6011 	kfree(cfg->wowl.nd_info);
6012 	cfg->wowl.nd_info = NULL;
6013 	kfree(cfg->escan_info.escan_buf);
6014 	cfg->escan_info.escan_buf = NULL;
6015 }
6016 
6017 static s32 brcmf_init_priv_mem(struct brcmf_cfg80211_info *cfg)
6018 {
6019 	cfg->conf = kzalloc(sizeof(*cfg->conf), GFP_KERNEL);
6020 	if (!cfg->conf)
6021 		goto init_priv_mem_out;
6022 	cfg->extra_buf = kzalloc(WL_EXTRA_BUF_MAX, GFP_KERNEL);
6023 	if (!cfg->extra_buf)
6024 		goto init_priv_mem_out;
6025 	cfg->wowl.nd = kzalloc(sizeof(*cfg->wowl.nd) + sizeof(u32), GFP_KERNEL);
6026 	if (!cfg->wowl.nd)
6027 		goto init_priv_mem_out;
6028 	cfg->wowl.nd_info = kzalloc(sizeof(*cfg->wowl.nd_info) +
6029 				    sizeof(struct cfg80211_wowlan_nd_match *),
6030 				    GFP_KERNEL);
6031 	if (!cfg->wowl.nd_info)
6032 		goto init_priv_mem_out;
6033 	cfg->escan_info.escan_buf = kzalloc(BRCMF_ESCAN_BUF_SIZE, GFP_KERNEL);
6034 	if (!cfg->escan_info.escan_buf)
6035 		goto init_priv_mem_out;
6036 
6037 	return 0;
6038 
6039 init_priv_mem_out:
6040 	brcmf_deinit_priv_mem(cfg);
6041 
6042 	return -ENOMEM;
6043 }
6044 
6045 static s32 wl_init_priv(struct brcmf_cfg80211_info *cfg)
6046 {
6047 	s32 err = 0;
6048 
6049 	cfg->scan_request = NULL;
6050 	cfg->pwr_save = true;
6051 	cfg->dongle_up = false;		/* dongle is not up yet */
6052 	err = brcmf_init_priv_mem(cfg);
6053 	if (err)
6054 		return err;
6055 	brcmf_register_event_handlers(cfg);
6056 	mutex_init(&cfg->usr_sync);
6057 	brcmf_init_escan(cfg);
6058 	brcmf_init_conf(cfg->conf);
6059 	init_completion(&cfg->vif_disabled);
6060 	return err;
6061 }
6062 
6063 static void wl_deinit_priv(struct brcmf_cfg80211_info *cfg)
6064 {
6065 	cfg->dongle_up = false;	/* dongle down */
6066 	brcmf_abort_scanning(cfg);
6067 	brcmf_deinit_priv_mem(cfg);
6068 }
6069 
6070 static void init_vif_event(struct brcmf_cfg80211_vif_event *event)
6071 {
6072 	init_waitqueue_head(&event->vif_wq);
6073 	spin_lock_init(&event->vif_event_lock);
6074 }
6075 
6076 static s32 brcmf_dongle_roam(struct brcmf_if *ifp)
6077 {
6078 	struct brcmf_pub *drvr = ifp->drvr;
6079 	s32 err;
6080 	u32 bcn_timeout;
6081 	__le32 roamtrigger[2];
6082 	__le32 roam_delta[2];
6083 
6084 	/* Configure beacon timeout value based upon roaming setting */
6085 	if (ifp->drvr->settings->roamoff)
6086 		bcn_timeout = BRCMF_DEFAULT_BCN_TIMEOUT_ROAM_OFF;
6087 	else
6088 		bcn_timeout = BRCMF_DEFAULT_BCN_TIMEOUT_ROAM_ON;
6089 	err = brcmf_fil_iovar_int_set(ifp, "bcn_timeout", bcn_timeout);
6090 	if (err) {
6091 		bphy_err(drvr, "bcn_timeout error (%d)\n", err);
6092 		goto roam_setup_done;
6093 	}
6094 
6095 	/* Enable/Disable built-in roaming to allow supplicant to take care of
6096 	 * roaming.
6097 	 */
6098 	brcmf_dbg(INFO, "Internal Roaming = %s\n",
6099 		  ifp->drvr->settings->roamoff ? "Off" : "On");
6100 	err = brcmf_fil_iovar_int_set(ifp, "roam_off",
6101 				      ifp->drvr->settings->roamoff);
6102 	if (err) {
6103 		bphy_err(drvr, "roam_off error (%d)\n", err);
6104 		goto roam_setup_done;
6105 	}
6106 
6107 	roamtrigger[0] = cpu_to_le32(WL_ROAM_TRIGGER_LEVEL);
6108 	roamtrigger[1] = cpu_to_le32(BRCM_BAND_ALL);
6109 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_TRIGGER,
6110 				     (void *)roamtrigger, sizeof(roamtrigger));
6111 	if (err)
6112 		bphy_err(drvr, "WLC_SET_ROAM_TRIGGER error (%d)\n", err);
6113 
6114 	roam_delta[0] = cpu_to_le32(WL_ROAM_DELTA);
6115 	roam_delta[1] = cpu_to_le32(BRCM_BAND_ALL);
6116 	err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SET_ROAM_DELTA,
6117 				     (void *)roam_delta, sizeof(roam_delta));
6118 	if (err)
6119 		bphy_err(drvr, "WLC_SET_ROAM_DELTA error (%d)\n", err);
6120 
6121 	return 0;
6122 
6123 roam_setup_done:
6124 	return err;
6125 }
6126 
6127 static s32
6128 brcmf_dongle_scantime(struct brcmf_if *ifp)
6129 {
6130 	struct brcmf_pub *drvr = ifp->drvr;
6131 	s32 err = 0;
6132 
6133 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_CHANNEL_TIME,
6134 				    BRCMF_SCAN_CHANNEL_TIME);
6135 	if (err) {
6136 		bphy_err(drvr, "Scan assoc time error (%d)\n", err);
6137 		goto dongle_scantime_out;
6138 	}
6139 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_UNASSOC_TIME,
6140 				    BRCMF_SCAN_UNASSOC_TIME);
6141 	if (err) {
6142 		bphy_err(drvr, "Scan unassoc time error (%d)\n", err);
6143 		goto dongle_scantime_out;
6144 	}
6145 
6146 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_SCAN_PASSIVE_TIME,
6147 				    BRCMF_SCAN_PASSIVE_TIME);
6148 	if (err) {
6149 		bphy_err(drvr, "Scan passive time error (%d)\n", err);
6150 		goto dongle_scantime_out;
6151 	}
6152 
6153 dongle_scantime_out:
6154 	return err;
6155 }
6156 
6157 static void brcmf_update_bw40_channel_flag(struct ieee80211_channel *channel,
6158 					   struct brcmu_chan *ch)
6159 {
6160 	u32 ht40_flag;
6161 
6162 	ht40_flag = channel->flags & IEEE80211_CHAN_NO_HT40;
6163 	if (ch->sb == BRCMU_CHAN_SB_U) {
6164 		if (ht40_flag == IEEE80211_CHAN_NO_HT40)
6165 			channel->flags &= ~IEEE80211_CHAN_NO_HT40;
6166 		channel->flags |= IEEE80211_CHAN_NO_HT40PLUS;
6167 	} else {
6168 		/* It should be one of
6169 		 * IEEE80211_CHAN_NO_HT40 or
6170 		 * IEEE80211_CHAN_NO_HT40PLUS
6171 		 */
6172 		channel->flags &= ~IEEE80211_CHAN_NO_HT40;
6173 		if (ht40_flag == IEEE80211_CHAN_NO_HT40)
6174 			channel->flags |= IEEE80211_CHAN_NO_HT40MINUS;
6175 	}
6176 }
6177 
6178 static int brcmf_construct_chaninfo(struct brcmf_cfg80211_info *cfg,
6179 				    u32 bw_cap[])
6180 {
6181 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
6182 	struct brcmf_pub *drvr = cfg->pub;
6183 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6184 	struct ieee80211_supported_band *band;
6185 	struct ieee80211_channel *channel;
6186 	struct brcmf_chanspec_list *list;
6187 	struct brcmu_chan ch;
6188 	int err;
6189 	u8 *pbuf;
6190 	u32 i, j;
6191 	u32 total;
6192 	u32 chaninfo;
6193 
6194 	pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
6195 
6196 	if (pbuf == NULL)
6197 		return -ENOMEM;
6198 
6199 	list = (struct brcmf_chanspec_list *)pbuf;
6200 
6201 	err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
6202 				       BRCMF_DCMD_MEDLEN);
6203 	if (err) {
6204 		bphy_err(drvr, "get chanspecs error (%d)\n", err);
6205 		goto fail_pbuf;
6206 	}
6207 
6208 	band = wiphy->bands[NL80211_BAND_2GHZ];
6209 	if (band)
6210 		for (i = 0; i < band->n_channels; i++)
6211 			band->channels[i].flags = IEEE80211_CHAN_DISABLED;
6212 	band = wiphy->bands[NL80211_BAND_5GHZ];
6213 	if (band)
6214 		for (i = 0; i < band->n_channels; i++)
6215 			band->channels[i].flags = IEEE80211_CHAN_DISABLED;
6216 
6217 	total = le32_to_cpu(list->count);
6218 	for (i = 0; i < total; i++) {
6219 		ch.chspec = (u16)le32_to_cpu(list->element[i]);
6220 		cfg->d11inf.decchspec(&ch);
6221 
6222 		if (ch.band == BRCMU_CHAN_BAND_2G) {
6223 			band = wiphy->bands[NL80211_BAND_2GHZ];
6224 		} else if (ch.band == BRCMU_CHAN_BAND_5G) {
6225 			band = wiphy->bands[NL80211_BAND_5GHZ];
6226 		} else {
6227 			bphy_err(drvr, "Invalid channel Spec. 0x%x.\n",
6228 				 ch.chspec);
6229 			continue;
6230 		}
6231 		if (!band)
6232 			continue;
6233 		if (!(bw_cap[band->band] & WLC_BW_40MHZ_BIT) &&
6234 		    ch.bw == BRCMU_CHAN_BW_40)
6235 			continue;
6236 		if (!(bw_cap[band->band] & WLC_BW_80MHZ_BIT) &&
6237 		    ch.bw == BRCMU_CHAN_BW_80)
6238 			continue;
6239 
6240 		channel = NULL;
6241 		for (j = 0; j < band->n_channels; j++) {
6242 			if (band->channels[j].hw_value == ch.control_ch_num) {
6243 				channel = &band->channels[j];
6244 				break;
6245 			}
6246 		}
6247 		if (!channel) {
6248 			/* It seems firmware supports some channel we never
6249 			 * considered. Something new in IEEE standard?
6250 			 */
6251 			bphy_err(drvr, "Ignoring unexpected firmware channel %d\n",
6252 				 ch.control_ch_num);
6253 			continue;
6254 		}
6255 
6256 		if (channel->orig_flags & IEEE80211_CHAN_DISABLED)
6257 			continue;
6258 
6259 		/* assuming the chanspecs order is HT20,
6260 		 * HT40 upper, HT40 lower, and VHT80.
6261 		 */
6262 		switch (ch.bw) {
6263 		case BRCMU_CHAN_BW_160:
6264 			channel->flags &= ~IEEE80211_CHAN_NO_160MHZ;
6265 			break;
6266 		case BRCMU_CHAN_BW_80:
6267 			channel->flags &= ~IEEE80211_CHAN_NO_80MHZ;
6268 			break;
6269 		case BRCMU_CHAN_BW_40:
6270 			brcmf_update_bw40_channel_flag(channel, &ch);
6271 			break;
6272 		default:
6273 			wiphy_warn(wiphy, "Firmware reported unsupported bandwidth %d\n",
6274 				   ch.bw);
6275 			/* fall through */
6276 		case BRCMU_CHAN_BW_20:
6277 			/* enable the channel and disable other bandwidths
6278 			 * for now as mentioned order assure they are enabled
6279 			 * for subsequent chanspecs.
6280 			 */
6281 			channel->flags = IEEE80211_CHAN_NO_HT40 |
6282 					 IEEE80211_CHAN_NO_80MHZ |
6283 					 IEEE80211_CHAN_NO_160MHZ;
6284 			ch.bw = BRCMU_CHAN_BW_20;
6285 			cfg->d11inf.encchspec(&ch);
6286 			chaninfo = ch.chspec;
6287 			err = brcmf_fil_bsscfg_int_get(ifp, "per_chan_info",
6288 						       &chaninfo);
6289 			if (!err) {
6290 				if (chaninfo & WL_CHAN_RADAR)
6291 					channel->flags |=
6292 						(IEEE80211_CHAN_RADAR |
6293 						 IEEE80211_CHAN_NO_IR);
6294 				if (chaninfo & WL_CHAN_PASSIVE)
6295 					channel->flags |=
6296 						IEEE80211_CHAN_NO_IR;
6297 			}
6298 		}
6299 	}
6300 
6301 fail_pbuf:
6302 	kfree(pbuf);
6303 	return err;
6304 }
6305 
6306 static int brcmf_enable_bw40_2g(struct brcmf_cfg80211_info *cfg)
6307 {
6308 	struct brcmf_pub *drvr = cfg->pub;
6309 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6310 	struct ieee80211_supported_band *band;
6311 	struct brcmf_fil_bwcap_le band_bwcap;
6312 	struct brcmf_chanspec_list *list;
6313 	u8 *pbuf;
6314 	u32 val;
6315 	int err;
6316 	struct brcmu_chan ch;
6317 	u32 num_chan;
6318 	int i, j;
6319 
6320 	/* verify support for bw_cap command */
6321 	val = WLC_BAND_5G;
6322 	err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &val);
6323 
6324 	if (!err) {
6325 		/* only set 2G bandwidth using bw_cap command */
6326 		band_bwcap.band = cpu_to_le32(WLC_BAND_2G);
6327 		band_bwcap.bw_cap = cpu_to_le32(WLC_BW_CAP_40MHZ);
6328 		err = brcmf_fil_iovar_data_set(ifp, "bw_cap", &band_bwcap,
6329 					       sizeof(band_bwcap));
6330 	} else {
6331 		brcmf_dbg(INFO, "fallback to mimo_bw_cap\n");
6332 		val = WLC_N_BW_40ALL;
6333 		err = brcmf_fil_iovar_int_set(ifp, "mimo_bw_cap", val);
6334 	}
6335 
6336 	if (!err) {
6337 		/* update channel info in 2G band */
6338 		pbuf = kzalloc(BRCMF_DCMD_MEDLEN, GFP_KERNEL);
6339 
6340 		if (pbuf == NULL)
6341 			return -ENOMEM;
6342 
6343 		ch.band = BRCMU_CHAN_BAND_2G;
6344 		ch.bw = BRCMU_CHAN_BW_40;
6345 		ch.sb = BRCMU_CHAN_SB_NONE;
6346 		ch.chnum = 0;
6347 		cfg->d11inf.encchspec(&ch);
6348 
6349 		/* pass encoded chanspec in query */
6350 		*(__le16 *)pbuf = cpu_to_le16(ch.chspec);
6351 
6352 		err = brcmf_fil_iovar_data_get(ifp, "chanspecs", pbuf,
6353 					       BRCMF_DCMD_MEDLEN);
6354 		if (err) {
6355 			bphy_err(drvr, "get chanspecs error (%d)\n", err);
6356 			kfree(pbuf);
6357 			return err;
6358 		}
6359 
6360 		band = cfg_to_wiphy(cfg)->bands[NL80211_BAND_2GHZ];
6361 		list = (struct brcmf_chanspec_list *)pbuf;
6362 		num_chan = le32_to_cpu(list->count);
6363 		for (i = 0; i < num_chan; i++) {
6364 			ch.chspec = (u16)le32_to_cpu(list->element[i]);
6365 			cfg->d11inf.decchspec(&ch);
6366 			if (WARN_ON(ch.band != BRCMU_CHAN_BAND_2G))
6367 				continue;
6368 			if (WARN_ON(ch.bw != BRCMU_CHAN_BW_40))
6369 				continue;
6370 			for (j = 0; j < band->n_channels; j++) {
6371 				if (band->channels[j].hw_value == ch.control_ch_num)
6372 					break;
6373 			}
6374 			if (WARN_ON(j == band->n_channels))
6375 				continue;
6376 
6377 			brcmf_update_bw40_channel_flag(&band->channels[j], &ch);
6378 		}
6379 		kfree(pbuf);
6380 	}
6381 	return err;
6382 }
6383 
6384 static void brcmf_get_bwcap(struct brcmf_if *ifp, u32 bw_cap[])
6385 {
6386 	struct brcmf_pub *drvr = ifp->drvr;
6387 	u32 band, mimo_bwcap;
6388 	int err;
6389 
6390 	band = WLC_BAND_2G;
6391 	err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
6392 	if (!err) {
6393 		bw_cap[NL80211_BAND_2GHZ] = band;
6394 		band = WLC_BAND_5G;
6395 		err = brcmf_fil_iovar_int_get(ifp, "bw_cap", &band);
6396 		if (!err) {
6397 			bw_cap[NL80211_BAND_5GHZ] = band;
6398 			return;
6399 		}
6400 		WARN_ON(1);
6401 		return;
6402 	}
6403 	brcmf_dbg(INFO, "fallback to mimo_bw_cap info\n");
6404 	mimo_bwcap = 0;
6405 	err = brcmf_fil_iovar_int_get(ifp, "mimo_bw_cap", &mimo_bwcap);
6406 	if (err)
6407 		/* assume 20MHz if firmware does not give a clue */
6408 		mimo_bwcap = WLC_N_BW_20ALL;
6409 
6410 	switch (mimo_bwcap) {
6411 	case WLC_N_BW_40ALL:
6412 		bw_cap[NL80211_BAND_2GHZ] |= WLC_BW_40MHZ_BIT;
6413 		/* fall-thru */
6414 	case WLC_N_BW_20IN2G_40IN5G:
6415 		bw_cap[NL80211_BAND_5GHZ] |= WLC_BW_40MHZ_BIT;
6416 		/* fall-thru */
6417 	case WLC_N_BW_20ALL:
6418 		bw_cap[NL80211_BAND_2GHZ] |= WLC_BW_20MHZ_BIT;
6419 		bw_cap[NL80211_BAND_5GHZ] |= WLC_BW_20MHZ_BIT;
6420 		break;
6421 	default:
6422 		bphy_err(drvr, "invalid mimo_bw_cap value\n");
6423 	}
6424 }
6425 
6426 static void brcmf_update_ht_cap(struct ieee80211_supported_band *band,
6427 				u32 bw_cap[2], u32 nchain)
6428 {
6429 	band->ht_cap.ht_supported = true;
6430 	if (bw_cap[band->band] & WLC_BW_40MHZ_BIT) {
6431 		band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
6432 		band->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
6433 	}
6434 	band->ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
6435 	band->ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
6436 	band->ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
6437 	band->ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_16;
6438 	memset(band->ht_cap.mcs.rx_mask, 0xff, nchain);
6439 	band->ht_cap.mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
6440 }
6441 
6442 static __le16 brcmf_get_mcs_map(u32 nchain, enum ieee80211_vht_mcs_support supp)
6443 {
6444 	u16 mcs_map;
6445 	int i;
6446 
6447 	for (i = 0, mcs_map = 0xFFFF; i < nchain; i++)
6448 		mcs_map = (mcs_map << 2) | supp;
6449 
6450 	return cpu_to_le16(mcs_map);
6451 }
6452 
6453 static void brcmf_update_vht_cap(struct ieee80211_supported_band *band,
6454 				 u32 bw_cap[2], u32 nchain, u32 txstreams,
6455 				 u32 txbf_bfe_cap, u32 txbf_bfr_cap)
6456 {
6457 	__le16 mcs_map;
6458 
6459 	/* not allowed in 2.4G band */
6460 	if (band->band == NL80211_BAND_2GHZ)
6461 		return;
6462 
6463 	band->vht_cap.vht_supported = true;
6464 	/* 80MHz is mandatory */
6465 	band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_80;
6466 	if (bw_cap[band->band] & WLC_BW_160MHZ_BIT) {
6467 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ;
6468 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SHORT_GI_160;
6469 	}
6470 	/* all support 256-QAM */
6471 	mcs_map = brcmf_get_mcs_map(nchain, IEEE80211_VHT_MCS_SUPPORT_0_9);
6472 	band->vht_cap.vht_mcs.rx_mcs_map = mcs_map;
6473 	band->vht_cap.vht_mcs.tx_mcs_map = mcs_map;
6474 
6475 	/* Beamforming support information */
6476 	if (txbf_bfe_cap & BRCMF_TXBF_SU_BFE_CAP)
6477 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE;
6478 	if (txbf_bfe_cap & BRCMF_TXBF_MU_BFE_CAP)
6479 		band->vht_cap.cap |= IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE;
6480 	if (txbf_bfr_cap & BRCMF_TXBF_SU_BFR_CAP)
6481 		band->vht_cap.cap |= IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
6482 	if (txbf_bfr_cap & BRCMF_TXBF_MU_BFR_CAP)
6483 		band->vht_cap.cap |= IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE;
6484 
6485 	if ((txbf_bfe_cap || txbf_bfr_cap) && (txstreams > 1)) {
6486 		band->vht_cap.cap |=
6487 			(2 << IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT);
6488 		band->vht_cap.cap |= ((txstreams - 1) <<
6489 				IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT);
6490 		band->vht_cap.cap |=
6491 			IEEE80211_VHT_CAP_VHT_LINK_ADAPTATION_VHT_MRQ_MFB;
6492 	}
6493 }
6494 
6495 static int brcmf_setup_wiphybands(struct brcmf_cfg80211_info *cfg)
6496 {
6497 	struct brcmf_pub *drvr = cfg->pub;
6498 	struct brcmf_if *ifp = brcmf_get_ifp(drvr, 0);
6499 	struct wiphy *wiphy = cfg_to_wiphy(cfg);
6500 	u32 nmode = 0;
6501 	u32 vhtmode = 0;
6502 	u32 bw_cap[2] = { WLC_BW_20MHZ_BIT, WLC_BW_20MHZ_BIT };
6503 	u32 rxchain;
6504 	u32 nchain;
6505 	int err;
6506 	s32 i;
6507 	struct ieee80211_supported_band *band;
6508 	u32 txstreams = 0;
6509 	u32 txbf_bfe_cap = 0;
6510 	u32 txbf_bfr_cap = 0;
6511 
6512 	(void)brcmf_fil_iovar_int_get(ifp, "vhtmode", &vhtmode);
6513 	err = brcmf_fil_iovar_int_get(ifp, "nmode", &nmode);
6514 	if (err) {
6515 		bphy_err(drvr, "nmode error (%d)\n", err);
6516 	} else {
6517 		brcmf_get_bwcap(ifp, bw_cap);
6518 	}
6519 	brcmf_dbg(INFO, "nmode=%d, vhtmode=%d, bw_cap=(%d, %d)\n",
6520 		  nmode, vhtmode, bw_cap[NL80211_BAND_2GHZ],
6521 		  bw_cap[NL80211_BAND_5GHZ]);
6522 
6523 	err = brcmf_fil_iovar_int_get(ifp, "rxchain", &rxchain);
6524 	if (err) {
6525 		bphy_err(drvr, "rxchain error (%d)\n", err);
6526 		nchain = 1;
6527 	} else {
6528 		for (nchain = 0; rxchain; nchain++)
6529 			rxchain = rxchain & (rxchain - 1);
6530 	}
6531 	brcmf_dbg(INFO, "nchain=%d\n", nchain);
6532 
6533 	err = brcmf_construct_chaninfo(cfg, bw_cap);
6534 	if (err) {
6535 		bphy_err(drvr, "brcmf_construct_chaninfo failed (%d)\n", err);
6536 		return err;
6537 	}
6538 
6539 	if (vhtmode) {
6540 		(void)brcmf_fil_iovar_int_get(ifp, "txstreams", &txstreams);
6541 		(void)brcmf_fil_iovar_int_get(ifp, "txbf_bfe_cap",
6542 					      &txbf_bfe_cap);
6543 		(void)brcmf_fil_iovar_int_get(ifp, "txbf_bfr_cap",
6544 					      &txbf_bfr_cap);
6545 	}
6546 
6547 	for (i = 0; i < ARRAY_SIZE(wiphy->bands); i++) {
6548 		band = wiphy->bands[i];
6549 		if (band == NULL)
6550 			continue;
6551 
6552 		if (nmode)
6553 			brcmf_update_ht_cap(band, bw_cap, nchain);
6554 		if (vhtmode)
6555 			brcmf_update_vht_cap(band, bw_cap, nchain, txstreams,
6556 					     txbf_bfe_cap, txbf_bfr_cap);
6557 	}
6558 
6559 	return 0;
6560 }
6561 
6562 static const struct ieee80211_txrx_stypes
6563 brcmf_txrx_stypes[NUM_NL80211_IFTYPES] = {
6564 	[NL80211_IFTYPE_STATION] = {
6565 		.tx = 0xffff,
6566 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6567 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6568 	},
6569 	[NL80211_IFTYPE_P2P_CLIENT] = {
6570 		.tx = 0xffff,
6571 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6572 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6573 	},
6574 	[NL80211_IFTYPE_P2P_GO] = {
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 	[NL80211_IFTYPE_P2P_DEVICE] = {
6585 		.tx = 0xffff,
6586 		.rx = BIT(IEEE80211_STYPE_ACTION >> 4) |
6587 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4)
6588 	},
6589 	[NL80211_IFTYPE_AP] = {
6590 		.tx = 0xffff,
6591 		.rx = BIT(IEEE80211_STYPE_ASSOC_REQ >> 4) |
6592 		      BIT(IEEE80211_STYPE_REASSOC_REQ >> 4) |
6593 		      BIT(IEEE80211_STYPE_PROBE_REQ >> 4) |
6594 		      BIT(IEEE80211_STYPE_DISASSOC >> 4) |
6595 		      BIT(IEEE80211_STYPE_AUTH >> 4) |
6596 		      BIT(IEEE80211_STYPE_DEAUTH >> 4) |
6597 		      BIT(IEEE80211_STYPE_ACTION >> 4)
6598 	}
6599 };
6600 
6601 /**
6602  * brcmf_setup_ifmodes() - determine interface modes and combinations.
6603  *
6604  * @wiphy: wiphy object.
6605  * @ifp: interface object needed for feat module api.
6606  *
6607  * The interface modes and combinations are determined dynamically here
6608  * based on firmware functionality.
6609  *
6610  * no p2p and no mbss:
6611  *
6612  *	#STA <= 1, #AP <= 1, channels = 1, 2 total
6613  *
6614  * no p2p and mbss:
6615  *
6616  *	#STA <= 1, #AP <= 1, channels = 1, 2 total
6617  *	#AP <= 4, matching BI, channels = 1, 4 total
6618  *
6619  * no p2p and rsdb:
6620  *	#STA <= 2, #AP <= 2, channels = 2, 4 total
6621  *
6622  * p2p, no mchan, and mbss:
6623  *
6624  *	#STA <= 1, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 1, channels = 1, 3 total
6625  *	#STA <= 1, #P2P-DEV <= 1, #AP <= 1, #P2P-CL <= 1, channels = 1, 4 total
6626  *	#AP <= 4, matching BI, channels = 1, 4 total
6627  *
6628  * p2p, mchan, and mbss:
6629  *
6630  *	#STA <= 1, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 1, channels = 2, 3 total
6631  *	#STA <= 1, #P2P-DEV <= 1, #AP <= 1, #P2P-CL <= 1, channels = 1, 4 total
6632  *	#AP <= 4, matching BI, channels = 1, 4 total
6633  *
6634  * p2p, rsdb, and no mbss:
6635  *	#STA <= 2, #P2P-DEV <= 1, #{P2P-CL, P2P-GO} <= 2, AP <= 2,
6636  *	 channels = 2, 4 total
6637  */
6638 static int brcmf_setup_ifmodes(struct wiphy *wiphy, struct brcmf_if *ifp)
6639 {
6640 	struct ieee80211_iface_combination *combo = NULL;
6641 	struct ieee80211_iface_limit *c0_limits = NULL;
6642 	struct ieee80211_iface_limit *p2p_limits = NULL;
6643 	struct ieee80211_iface_limit *mbss_limits = NULL;
6644 	bool mon_flag, mbss, p2p, rsdb, mchan;
6645 	int i, c, n_combos, n_limits;
6646 
6647 	mon_flag = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MONITOR_FLAG);
6648 	mbss = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MBSS);
6649 	p2p = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_P2P);
6650 	rsdb = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_RSDB);
6651 	mchan = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MCHAN);
6652 
6653 	n_combos = 1 + !!(p2p && !rsdb) + !!mbss;
6654 	combo = kcalloc(n_combos, sizeof(*combo), GFP_KERNEL);
6655 	if (!combo)
6656 		goto err;
6657 
6658 	wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
6659 				 BIT(NL80211_IFTYPE_ADHOC) |
6660 				 BIT(NL80211_IFTYPE_AP);
6661 	if (mon_flag)
6662 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_MONITOR);
6663 	if (p2p)
6664 		wiphy->interface_modes |= BIT(NL80211_IFTYPE_P2P_CLIENT) |
6665 					  BIT(NL80211_IFTYPE_P2P_GO) |
6666 					  BIT(NL80211_IFTYPE_P2P_DEVICE);
6667 
6668 	c = 0;
6669 	i = 0;
6670 	n_limits = 1 + mon_flag + (p2p ? 2 : 0) + (rsdb || !p2p);
6671 	c0_limits = kcalloc(n_limits, sizeof(*c0_limits), GFP_KERNEL);
6672 	if (!c0_limits)
6673 		goto err;
6674 
6675 	combo[c].num_different_channels = 1 + (rsdb || (p2p && mchan));
6676 	c0_limits[i].max = 1 + rsdb;
6677 	c0_limits[i++].types = BIT(NL80211_IFTYPE_STATION);
6678 	if (mon_flag) {
6679 		c0_limits[i].max = 1;
6680 		c0_limits[i++].types = BIT(NL80211_IFTYPE_MONITOR);
6681 	}
6682 	if (p2p) {
6683 		c0_limits[i].max = 1;
6684 		c0_limits[i++].types = BIT(NL80211_IFTYPE_P2P_DEVICE);
6685 		c0_limits[i].max = 1 + rsdb;
6686 		c0_limits[i++].types = BIT(NL80211_IFTYPE_P2P_CLIENT) |
6687 				       BIT(NL80211_IFTYPE_P2P_GO);
6688 	}
6689 	if (p2p && rsdb) {
6690 		c0_limits[i].max = 2;
6691 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6692 		combo[c].max_interfaces = 5;
6693 	} else if (p2p) {
6694 		combo[c].max_interfaces = i;
6695 	} else if (rsdb) {
6696 		c0_limits[i].max = 2;
6697 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6698 		combo[c].max_interfaces = 3;
6699 	} else {
6700 		c0_limits[i].max = 1;
6701 		c0_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6702 		combo[c].max_interfaces = i;
6703 	}
6704 	combo[c].n_limits = i;
6705 	combo[c].limits = c0_limits;
6706 
6707 	if (p2p && !rsdb) {
6708 		c++;
6709 		i = 0;
6710 		p2p_limits = kcalloc(4, sizeof(*p2p_limits), GFP_KERNEL);
6711 		if (!p2p_limits)
6712 			goto err;
6713 		p2p_limits[i].max = 1;
6714 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_STATION);
6715 		p2p_limits[i].max = 1;
6716 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6717 		p2p_limits[i].max = 1;
6718 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_P2P_CLIENT);
6719 		p2p_limits[i].max = 1;
6720 		p2p_limits[i++].types = BIT(NL80211_IFTYPE_P2P_DEVICE);
6721 		combo[c].num_different_channels = 1;
6722 		combo[c].max_interfaces = i;
6723 		combo[c].n_limits = i;
6724 		combo[c].limits = p2p_limits;
6725 	}
6726 
6727 	if (mbss) {
6728 		c++;
6729 		i = 0;
6730 		n_limits = 1 + mon_flag;
6731 		mbss_limits = kcalloc(n_limits, sizeof(*mbss_limits),
6732 				      GFP_KERNEL);
6733 		if (!mbss_limits)
6734 			goto err;
6735 		mbss_limits[i].max = 4;
6736 		mbss_limits[i++].types = BIT(NL80211_IFTYPE_AP);
6737 		if (mon_flag) {
6738 			mbss_limits[i].max = 1;
6739 			mbss_limits[i++].types = BIT(NL80211_IFTYPE_MONITOR);
6740 		}
6741 		combo[c].beacon_int_infra_match = true;
6742 		combo[c].num_different_channels = 1;
6743 		combo[c].max_interfaces = 4 + mon_flag;
6744 		combo[c].n_limits = i;
6745 		combo[c].limits = mbss_limits;
6746 	}
6747 
6748 	wiphy->n_iface_combinations = n_combos;
6749 	wiphy->iface_combinations = combo;
6750 	return 0;
6751 
6752 err:
6753 	kfree(c0_limits);
6754 	kfree(p2p_limits);
6755 	kfree(mbss_limits);
6756 	kfree(combo);
6757 	return -ENOMEM;
6758 }
6759 
6760 #ifdef CONFIG_PM
6761 static const struct wiphy_wowlan_support brcmf_wowlan_support = {
6762 	.flags = WIPHY_WOWLAN_MAGIC_PKT | WIPHY_WOWLAN_DISCONNECT,
6763 	.n_patterns = BRCMF_WOWL_MAXPATTERNS,
6764 	.pattern_max_len = BRCMF_WOWL_MAXPATTERNSIZE,
6765 	.pattern_min_len = 1,
6766 	.max_pkt_offset = 1500,
6767 };
6768 #endif
6769 
6770 static void brcmf_wiphy_wowl_params(struct wiphy *wiphy, struct brcmf_if *ifp)
6771 {
6772 #ifdef CONFIG_PM
6773 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
6774 	struct brcmf_pub *drvr = cfg->pub;
6775 	struct wiphy_wowlan_support *wowl;
6776 
6777 	wowl = kmemdup(&brcmf_wowlan_support, sizeof(brcmf_wowlan_support),
6778 		       GFP_KERNEL);
6779 	if (!wowl) {
6780 		bphy_err(drvr, "only support basic wowlan features\n");
6781 		wiphy->wowlan = &brcmf_wowlan_support;
6782 		return;
6783 	}
6784 
6785 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_PNO)) {
6786 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_ND)) {
6787 			wowl->flags |= WIPHY_WOWLAN_NET_DETECT;
6788 			wowl->max_nd_match_sets = BRCMF_PNO_MAX_PFN_COUNT;
6789 			init_waitqueue_head(&cfg->wowl.nd_data_wait);
6790 		}
6791 	}
6792 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_GTK)) {
6793 		wowl->flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY;
6794 		wowl->flags |= WIPHY_WOWLAN_GTK_REKEY_FAILURE;
6795 	}
6796 
6797 	wiphy->wowlan = wowl;
6798 #endif
6799 }
6800 
6801 static int brcmf_setup_wiphy(struct wiphy *wiphy, struct brcmf_if *ifp)
6802 {
6803 	struct brcmf_pub *drvr = ifp->drvr;
6804 	const struct ieee80211_iface_combination *combo;
6805 	struct ieee80211_supported_band *band;
6806 	u16 max_interfaces = 0;
6807 	bool gscan;
6808 	__le32 bandlist[3];
6809 	u32 n_bands;
6810 	int err, i;
6811 
6812 	wiphy->max_scan_ssids = WL_NUM_SCAN_MAX;
6813 	wiphy->max_scan_ie_len = BRCMF_SCAN_IE_LEN_MAX;
6814 	wiphy->max_num_pmkids = BRCMF_MAXPMKID;
6815 
6816 	err = brcmf_setup_ifmodes(wiphy, ifp);
6817 	if (err)
6818 		return err;
6819 
6820 	for (i = 0, combo = wiphy->iface_combinations;
6821 	     i < wiphy->n_iface_combinations; i++, combo++) {
6822 		max_interfaces = max(max_interfaces, combo->max_interfaces);
6823 	}
6824 
6825 	for (i = 0; i < max_interfaces && i < ARRAY_SIZE(drvr->addresses);
6826 	     i++) {
6827 		u8 *addr = drvr->addresses[i].addr;
6828 
6829 		memcpy(addr, drvr->mac, ETH_ALEN);
6830 		if (i) {
6831 			addr[0] |= BIT(1);
6832 			addr[ETH_ALEN - 1] ^= i;
6833 		}
6834 	}
6835 	wiphy->addresses = drvr->addresses;
6836 	wiphy->n_addresses = i;
6837 
6838 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
6839 	wiphy->cipher_suites = brcmf_cipher_suites;
6840 	wiphy->n_cipher_suites = ARRAY_SIZE(brcmf_cipher_suites);
6841 	if (!brcmf_feat_is_enabled(ifp, BRCMF_FEAT_MFP))
6842 		wiphy->n_cipher_suites--;
6843 	wiphy->bss_select_support = BIT(NL80211_BSS_SELECT_ATTR_RSSI) |
6844 				    BIT(NL80211_BSS_SELECT_ATTR_BAND_PREF) |
6845 				    BIT(NL80211_BSS_SELECT_ATTR_RSSI_ADJUST);
6846 
6847 	wiphy->flags |= WIPHY_FLAG_NETNS_OK |
6848 			WIPHY_FLAG_PS_ON_BY_DEFAULT |
6849 			WIPHY_FLAG_HAVE_AP_SME |
6850 			WIPHY_FLAG_OFFCHAN_TX |
6851 			WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
6852 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_TDLS))
6853 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
6854 	if (!ifp->drvr->settings->roamoff)
6855 		wiphy->flags |= WIPHY_FLAG_SUPPORTS_FW_ROAM;
6856 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_FWSUP)) {
6857 		wiphy_ext_feature_set(wiphy,
6858 				      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_PSK);
6859 		wiphy_ext_feature_set(wiphy,
6860 				      NL80211_EXT_FEATURE_4WAY_HANDSHAKE_STA_1X);
6861 		if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_SAE))
6862 			wiphy_ext_feature_set(wiphy,
6863 					      NL80211_EXT_FEATURE_SAE_OFFLOAD);
6864 	}
6865 	wiphy->mgmt_stypes = brcmf_txrx_stypes;
6866 	wiphy->max_remain_on_channel_duration = 5000;
6867 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_PNO)) {
6868 		gscan = brcmf_feat_is_enabled(ifp, BRCMF_FEAT_GSCAN);
6869 		brcmf_pno_wiphy_params(wiphy, gscan);
6870 	}
6871 	/* vendor commands/events support */
6872 	wiphy->vendor_commands = brcmf_vendor_cmds;
6873 	wiphy->n_vendor_commands = BRCMF_VNDR_CMDS_LAST - 1;
6874 
6875 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL))
6876 		brcmf_wiphy_wowl_params(wiphy, ifp);
6877 	err = brcmf_fil_cmd_data_get(ifp, BRCMF_C_GET_BANDLIST, &bandlist,
6878 				     sizeof(bandlist));
6879 	if (err) {
6880 		bphy_err(drvr, "could not obtain band info: err=%d\n", err);
6881 		return err;
6882 	}
6883 	/* first entry in bandlist is number of bands */
6884 	n_bands = le32_to_cpu(bandlist[0]);
6885 	for (i = 1; i <= n_bands && i < ARRAY_SIZE(bandlist); i++) {
6886 		if (bandlist[i] == cpu_to_le32(WLC_BAND_2G)) {
6887 			band = kmemdup(&__wl_band_2ghz, sizeof(__wl_band_2ghz),
6888 				       GFP_KERNEL);
6889 			if (!band)
6890 				return -ENOMEM;
6891 
6892 			band->channels = kmemdup(&__wl_2ghz_channels,
6893 						 sizeof(__wl_2ghz_channels),
6894 						 GFP_KERNEL);
6895 			if (!band->channels) {
6896 				kfree(band);
6897 				return -ENOMEM;
6898 			}
6899 
6900 			band->n_channels = ARRAY_SIZE(__wl_2ghz_channels);
6901 			wiphy->bands[NL80211_BAND_2GHZ] = band;
6902 		}
6903 		if (bandlist[i] == cpu_to_le32(WLC_BAND_5G)) {
6904 			band = kmemdup(&__wl_band_5ghz, sizeof(__wl_band_5ghz),
6905 				       GFP_KERNEL);
6906 			if (!band)
6907 				return -ENOMEM;
6908 
6909 			band->channels = kmemdup(&__wl_5ghz_channels,
6910 						 sizeof(__wl_5ghz_channels),
6911 						 GFP_KERNEL);
6912 			if (!band->channels) {
6913 				kfree(band);
6914 				return -ENOMEM;
6915 			}
6916 
6917 			band->n_channels = ARRAY_SIZE(__wl_5ghz_channels);
6918 			wiphy->bands[NL80211_BAND_5GHZ] = band;
6919 		}
6920 	}
6921 
6922 	if (wiphy->bands[NL80211_BAND_5GHZ] &&
6923 	    brcmf_feat_is_enabled(ifp, BRCMF_FEAT_DOT11H))
6924 		wiphy_ext_feature_set(wiphy,
6925 				      NL80211_EXT_FEATURE_DFS_OFFLOAD);
6926 
6927 	wiphy_read_of_freq_limits(wiphy);
6928 
6929 	return 0;
6930 }
6931 
6932 static s32 brcmf_config_dongle(struct brcmf_cfg80211_info *cfg)
6933 {
6934 	struct brcmf_pub *drvr = cfg->pub;
6935 	struct net_device *ndev;
6936 	struct wireless_dev *wdev;
6937 	struct brcmf_if *ifp;
6938 	s32 power_mode;
6939 	s32 err = 0;
6940 
6941 	if (cfg->dongle_up)
6942 		return err;
6943 
6944 	ndev = cfg_to_ndev(cfg);
6945 	wdev = ndev->ieee80211_ptr;
6946 	ifp = netdev_priv(ndev);
6947 
6948 	/* make sure RF is ready for work */
6949 	brcmf_fil_cmd_int_set(ifp, BRCMF_C_UP, 0);
6950 
6951 	brcmf_dongle_scantime(ifp);
6952 
6953 	power_mode = cfg->pwr_save ? PM_FAST : PM_OFF;
6954 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PM, power_mode);
6955 	if (err)
6956 		goto default_conf_out;
6957 	brcmf_dbg(INFO, "power save set to %s\n",
6958 		  (power_mode ? "enabled" : "disabled"));
6959 
6960 	err = brcmf_dongle_roam(ifp);
6961 	if (err)
6962 		goto default_conf_out;
6963 	err = brcmf_cfg80211_change_iface(wdev->wiphy, ndev, wdev->iftype,
6964 					  NULL);
6965 	if (err)
6966 		goto default_conf_out;
6967 
6968 	brcmf_configure_arp_nd_offload(ifp, true);
6969 
6970 	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_FAKEFRAG, 1);
6971 	if (err) {
6972 		bphy_err(drvr, "failed to set frameburst mode\n");
6973 		goto default_conf_out;
6974 	}
6975 
6976 	cfg->dongle_up = true;
6977 default_conf_out:
6978 
6979 	return err;
6980 
6981 }
6982 
6983 static s32 __brcmf_cfg80211_up(struct brcmf_if *ifp)
6984 {
6985 	set_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
6986 
6987 	return brcmf_config_dongle(ifp->drvr->config);
6988 }
6989 
6990 static s32 __brcmf_cfg80211_down(struct brcmf_if *ifp)
6991 {
6992 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
6993 
6994 	/*
6995 	 * While going down, if associated with AP disassociate
6996 	 * from AP to save power
6997 	 */
6998 	if (check_vif_up(ifp->vif)) {
6999 		brcmf_link_down(ifp->vif, WLAN_REASON_UNSPECIFIED);
7000 
7001 		/* Make sure WPA_Supplicant receives all the event
7002 		   generated due to DISASSOC call to the fw to keep
7003 		   the state fw and WPA_Supplicant state consistent
7004 		 */
7005 		brcmf_delay(500);
7006 	}
7007 
7008 	brcmf_abort_scanning(cfg);
7009 	clear_bit(BRCMF_VIF_STATUS_READY, &ifp->vif->sme_state);
7010 
7011 	return 0;
7012 }
7013 
7014 s32 brcmf_cfg80211_up(struct net_device *ndev)
7015 {
7016 	struct brcmf_if *ifp = netdev_priv(ndev);
7017 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
7018 	s32 err = 0;
7019 
7020 	mutex_lock(&cfg->usr_sync);
7021 	err = __brcmf_cfg80211_up(ifp);
7022 	mutex_unlock(&cfg->usr_sync);
7023 
7024 	return err;
7025 }
7026 
7027 s32 brcmf_cfg80211_down(struct net_device *ndev)
7028 {
7029 	struct brcmf_if *ifp = netdev_priv(ndev);
7030 	struct brcmf_cfg80211_info *cfg = ifp->drvr->config;
7031 	s32 err = 0;
7032 
7033 	mutex_lock(&cfg->usr_sync);
7034 	err = __brcmf_cfg80211_down(ifp);
7035 	mutex_unlock(&cfg->usr_sync);
7036 
7037 	return err;
7038 }
7039 
7040 enum nl80211_iftype brcmf_cfg80211_get_iftype(struct brcmf_if *ifp)
7041 {
7042 	struct wireless_dev *wdev = &ifp->vif->wdev;
7043 
7044 	return wdev->iftype;
7045 }
7046 
7047 bool brcmf_get_vif_state_any(struct brcmf_cfg80211_info *cfg,
7048 			     unsigned long state)
7049 {
7050 	struct brcmf_cfg80211_vif *vif;
7051 
7052 	list_for_each_entry(vif, &cfg->vif_list, list) {
7053 		if (test_bit(state, &vif->sme_state))
7054 			return true;
7055 	}
7056 	return false;
7057 }
7058 
7059 static inline bool vif_event_equals(struct brcmf_cfg80211_vif_event *event,
7060 				    u8 action)
7061 {
7062 	u8 evt_action;
7063 
7064 	spin_lock(&event->vif_event_lock);
7065 	evt_action = event->action;
7066 	spin_unlock(&event->vif_event_lock);
7067 	return evt_action == action;
7068 }
7069 
7070 void brcmf_cfg80211_arm_vif_event(struct brcmf_cfg80211_info *cfg,
7071 				  struct brcmf_cfg80211_vif *vif)
7072 {
7073 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7074 
7075 	spin_lock(&event->vif_event_lock);
7076 	event->vif = vif;
7077 	event->action = 0;
7078 	spin_unlock(&event->vif_event_lock);
7079 }
7080 
7081 bool brcmf_cfg80211_vif_event_armed(struct brcmf_cfg80211_info *cfg)
7082 {
7083 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7084 	bool armed;
7085 
7086 	spin_lock(&event->vif_event_lock);
7087 	armed = event->vif != NULL;
7088 	spin_unlock(&event->vif_event_lock);
7089 
7090 	return armed;
7091 }
7092 
7093 int brcmf_cfg80211_wait_vif_event(struct brcmf_cfg80211_info *cfg,
7094 				  u8 action, ulong timeout)
7095 {
7096 	struct brcmf_cfg80211_vif_event *event = &cfg->vif_event;
7097 
7098 	return wait_event_timeout(event->vif_wq,
7099 				  vif_event_equals(event, action), timeout);
7100 }
7101 
7102 static s32 brcmf_translate_country_code(struct brcmf_pub *drvr, char alpha2[2],
7103 					struct brcmf_fil_country_le *ccreq)
7104 {
7105 	struct brcmfmac_pd_cc *country_codes;
7106 	struct brcmfmac_pd_cc_entry *cc;
7107 	s32 found_index;
7108 	int i;
7109 
7110 	country_codes = drvr->settings->country_codes;
7111 	if (!country_codes) {
7112 		brcmf_dbg(TRACE, "No country codes configured for device\n");
7113 		return -EINVAL;
7114 	}
7115 
7116 	if ((alpha2[0] == ccreq->country_abbrev[0]) &&
7117 	    (alpha2[1] == ccreq->country_abbrev[1])) {
7118 		brcmf_dbg(TRACE, "Country code already set\n");
7119 		return -EAGAIN;
7120 	}
7121 
7122 	found_index = -1;
7123 	for (i = 0; i < country_codes->table_size; i++) {
7124 		cc = &country_codes->table[i];
7125 		if ((cc->iso3166[0] == '\0') && (found_index == -1))
7126 			found_index = i;
7127 		if ((cc->iso3166[0] == alpha2[0]) &&
7128 		    (cc->iso3166[1] == alpha2[1])) {
7129 			found_index = i;
7130 			break;
7131 		}
7132 	}
7133 	if (found_index == -1) {
7134 		brcmf_dbg(TRACE, "No country code match found\n");
7135 		return -EINVAL;
7136 	}
7137 	memset(ccreq, 0, sizeof(*ccreq));
7138 	ccreq->rev = cpu_to_le32(country_codes->table[found_index].rev);
7139 	memcpy(ccreq->ccode, country_codes->table[found_index].cc,
7140 	       BRCMF_COUNTRY_BUF_SZ);
7141 	ccreq->country_abbrev[0] = alpha2[0];
7142 	ccreq->country_abbrev[1] = alpha2[1];
7143 	ccreq->country_abbrev[2] = 0;
7144 
7145 	return 0;
7146 }
7147 
7148 static void brcmf_cfg80211_reg_notifier(struct wiphy *wiphy,
7149 					struct regulatory_request *req)
7150 {
7151 	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
7152 	struct brcmf_if *ifp = brcmf_get_ifp(cfg->pub, 0);
7153 	struct brcmf_pub *drvr = cfg->pub;
7154 	struct brcmf_fil_country_le ccreq;
7155 	s32 err;
7156 	int i;
7157 
7158 	/* The country code gets set to "00" by default at boot, ignore */
7159 	if (req->alpha2[0] == '0' && req->alpha2[1] == '0')
7160 		return;
7161 
7162 	/* ignore non-ISO3166 country codes */
7163 	for (i = 0; i < 2; i++)
7164 		if (req->alpha2[i] < 'A' || req->alpha2[i] > 'Z') {
7165 			bphy_err(drvr, "not an ISO3166 code (0x%02x 0x%02x)\n",
7166 				 req->alpha2[0], req->alpha2[1]);
7167 			return;
7168 		}
7169 
7170 	brcmf_dbg(TRACE, "Enter: initiator=%d, alpha=%c%c\n", req->initiator,
7171 		  req->alpha2[0], req->alpha2[1]);
7172 
7173 	err = brcmf_fil_iovar_data_get(ifp, "country", &ccreq, sizeof(ccreq));
7174 	if (err) {
7175 		bphy_err(drvr, "Country code iovar returned err = %d\n", err);
7176 		return;
7177 	}
7178 
7179 	err = brcmf_translate_country_code(ifp->drvr, req->alpha2, &ccreq);
7180 	if (err)
7181 		return;
7182 
7183 	err = brcmf_fil_iovar_data_set(ifp, "country", &ccreq, sizeof(ccreq));
7184 	if (err) {
7185 		bphy_err(drvr, "Firmware rejected country setting\n");
7186 		return;
7187 	}
7188 	brcmf_setup_wiphybands(cfg);
7189 }
7190 
7191 static void brcmf_free_wiphy(struct wiphy *wiphy)
7192 {
7193 	int i;
7194 
7195 	if (!wiphy)
7196 		return;
7197 
7198 	if (wiphy->iface_combinations) {
7199 		for (i = 0; i < wiphy->n_iface_combinations; i++)
7200 			kfree(wiphy->iface_combinations[i].limits);
7201 	}
7202 	kfree(wiphy->iface_combinations);
7203 	if (wiphy->bands[NL80211_BAND_2GHZ]) {
7204 		kfree(wiphy->bands[NL80211_BAND_2GHZ]->channels);
7205 		kfree(wiphy->bands[NL80211_BAND_2GHZ]);
7206 	}
7207 	if (wiphy->bands[NL80211_BAND_5GHZ]) {
7208 		kfree(wiphy->bands[NL80211_BAND_5GHZ]->channels);
7209 		kfree(wiphy->bands[NL80211_BAND_5GHZ]);
7210 	}
7211 #if IS_ENABLED(CONFIG_PM)
7212 	if (wiphy->wowlan != &brcmf_wowlan_support)
7213 		kfree(wiphy->wowlan);
7214 #endif
7215 }
7216 
7217 struct brcmf_cfg80211_info *brcmf_cfg80211_attach(struct brcmf_pub *drvr,
7218 						  struct cfg80211_ops *ops,
7219 						  bool p2pdev_forced)
7220 {
7221 	struct wiphy *wiphy = drvr->wiphy;
7222 	struct net_device *ndev = brcmf_get_ifp(drvr, 0)->ndev;
7223 	struct brcmf_cfg80211_info *cfg;
7224 	struct brcmf_cfg80211_vif *vif;
7225 	struct brcmf_if *ifp;
7226 	s32 err = 0;
7227 	s32 io_type;
7228 	u16 *cap = NULL;
7229 
7230 	if (!ndev) {
7231 		bphy_err(drvr, "ndev is invalid\n");
7232 		return NULL;
7233 	}
7234 
7235 	cfg = kzalloc(sizeof(*cfg), GFP_KERNEL);
7236 	if (!cfg) {
7237 		bphy_err(drvr, "Could not allocate wiphy device\n");
7238 		return NULL;
7239 	}
7240 
7241 	cfg->wiphy = wiphy;
7242 	cfg->pub = drvr;
7243 	init_vif_event(&cfg->vif_event);
7244 	INIT_LIST_HEAD(&cfg->vif_list);
7245 
7246 	vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_STATION);
7247 	if (IS_ERR(vif))
7248 		goto wiphy_out;
7249 
7250 	ifp = netdev_priv(ndev);
7251 	vif->ifp = ifp;
7252 	vif->wdev.netdev = ndev;
7253 	ndev->ieee80211_ptr = &vif->wdev;
7254 	SET_NETDEV_DEV(ndev, wiphy_dev(cfg->wiphy));
7255 
7256 	err = wl_init_priv(cfg);
7257 	if (err) {
7258 		bphy_err(drvr, "Failed to init iwm_priv (%d)\n", err);
7259 		brcmf_free_vif(vif);
7260 		goto wiphy_out;
7261 	}
7262 	ifp->vif = vif;
7263 
7264 	/* determine d11 io type before wiphy setup */
7265 	err = brcmf_fil_cmd_int_get(ifp, BRCMF_C_GET_VERSION, &io_type);
7266 	if (err) {
7267 		bphy_err(drvr, "Failed to get D11 version (%d)\n", err);
7268 		goto priv_out;
7269 	}
7270 	cfg->d11inf.io_type = (u8)io_type;
7271 	brcmu_d11_attach(&cfg->d11inf);
7272 
7273 	/* regulatory notifer below needs access to cfg so
7274 	 * assign it now.
7275 	 */
7276 	drvr->config = cfg;
7277 
7278 	err = brcmf_setup_wiphy(wiphy, ifp);
7279 	if (err < 0)
7280 		goto priv_out;
7281 
7282 	brcmf_dbg(INFO, "Registering custom regulatory\n");
7283 	wiphy->reg_notifier = brcmf_cfg80211_reg_notifier;
7284 	wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG;
7285 	wiphy_apply_custom_regulatory(wiphy, &brcmf_regdom);
7286 
7287 	/* firmware defaults to 40MHz disabled in 2G band. We signal
7288 	 * cfg80211 here that we do and have it decide we can enable
7289 	 * it. But first check if device does support 2G operation.
7290 	 */
7291 	if (wiphy->bands[NL80211_BAND_2GHZ]) {
7292 		cap = &wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.cap;
7293 		*cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7294 	}
7295 #ifdef CONFIG_PM
7296 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_WOWL_GTK))
7297 		ops->set_rekey_data = brcmf_cfg80211_set_rekey_data;
7298 #endif
7299 	err = wiphy_register(wiphy);
7300 	if (err < 0) {
7301 		bphy_err(drvr, "Could not register wiphy device (%d)\n", err);
7302 		goto priv_out;
7303 	}
7304 
7305 	err = brcmf_setup_wiphybands(cfg);
7306 	if (err) {
7307 		bphy_err(drvr, "Setting wiphy bands failed (%d)\n", err);
7308 		goto wiphy_unreg_out;
7309 	}
7310 
7311 	/* If cfg80211 didn't disable 40MHz HT CAP in wiphy_register(),
7312 	 * setup 40MHz in 2GHz band and enable OBSS scanning.
7313 	 */
7314 	if (cap && (*cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)) {
7315 		err = brcmf_enable_bw40_2g(cfg);
7316 		if (!err)
7317 			err = brcmf_fil_iovar_int_set(ifp, "obss_coex",
7318 						      BRCMF_OBSS_COEX_AUTO);
7319 		else
7320 			*cap &= ~IEEE80211_HT_CAP_SUP_WIDTH_20_40;
7321 	}
7322 
7323 	err = brcmf_fweh_activate_events(ifp);
7324 	if (err) {
7325 		bphy_err(drvr, "FWEH activation failed (%d)\n", err);
7326 		goto wiphy_unreg_out;
7327 	}
7328 
7329 	err = brcmf_p2p_attach(cfg, p2pdev_forced);
7330 	if (err) {
7331 		bphy_err(drvr, "P2P initialisation failed (%d)\n", err);
7332 		goto wiphy_unreg_out;
7333 	}
7334 	err = brcmf_btcoex_attach(cfg);
7335 	if (err) {
7336 		bphy_err(drvr, "BT-coex initialisation failed (%d)\n", err);
7337 		brcmf_p2p_detach(&cfg->p2p);
7338 		goto wiphy_unreg_out;
7339 	}
7340 	err = brcmf_pno_attach(cfg);
7341 	if (err) {
7342 		bphy_err(drvr, "PNO initialisation failed (%d)\n", err);
7343 		brcmf_btcoex_detach(cfg);
7344 		brcmf_p2p_detach(&cfg->p2p);
7345 		goto wiphy_unreg_out;
7346 	}
7347 
7348 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_TDLS)) {
7349 		err = brcmf_fil_iovar_int_set(ifp, "tdls_enable", 1);
7350 		if (err) {
7351 			brcmf_dbg(INFO, "TDLS not enabled (%d)\n", err);
7352 			wiphy->flags &= ~WIPHY_FLAG_SUPPORTS_TDLS;
7353 		} else {
7354 			brcmf_fweh_register(cfg->pub, BRCMF_E_TDLS_PEER_EVENT,
7355 					    brcmf_notify_tdls_peer_event);
7356 		}
7357 	}
7358 
7359 	/* (re-) activate FWEH event handling */
7360 	err = brcmf_fweh_activate_events(ifp);
7361 	if (err) {
7362 		bphy_err(drvr, "FWEH activation failed (%d)\n", err);
7363 		goto detach;
7364 	}
7365 
7366 	/* Fill in some of the advertised nl80211 supported features */
7367 	if (brcmf_feat_is_enabled(ifp, BRCMF_FEAT_SCAN_RANDOM_MAC)) {
7368 		wiphy->features |= NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR;
7369 #ifdef CONFIG_PM
7370 		if (wiphy->wowlan &&
7371 		    wiphy->wowlan->flags & WIPHY_WOWLAN_NET_DETECT)
7372 			wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
7373 #endif
7374 	}
7375 
7376 	return cfg;
7377 
7378 detach:
7379 	brcmf_pno_detach(cfg);
7380 	brcmf_btcoex_detach(cfg);
7381 	brcmf_p2p_detach(&cfg->p2p);
7382 wiphy_unreg_out:
7383 	wiphy_unregister(cfg->wiphy);
7384 priv_out:
7385 	wl_deinit_priv(cfg);
7386 	brcmf_free_vif(vif);
7387 	ifp->vif = NULL;
7388 wiphy_out:
7389 	brcmf_free_wiphy(wiphy);
7390 	kfree(cfg);
7391 	return NULL;
7392 }
7393 
7394 void brcmf_cfg80211_detach(struct brcmf_cfg80211_info *cfg)
7395 {
7396 	if (!cfg)
7397 		return;
7398 
7399 	brcmf_pno_detach(cfg);
7400 	brcmf_btcoex_detach(cfg);
7401 	wiphy_unregister(cfg->wiphy);
7402 	wl_deinit_priv(cfg);
7403 	brcmf_free_wiphy(cfg->wiphy);
7404 	kfree(cfg);
7405 }
7406