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