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