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