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