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