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