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