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