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