cfg80211.c 168 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Copyright (c) 2010 Broadcom Corporation
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/* Toplevel file. Relies on dhd_linux.c to send commands to the dongle. */

#include <linux/kernel.h>
#include <linux/etherdevice.h>
21
#include <linux/module.h>
22
#include <linux/vmalloc.h>
23
#include <net/cfg80211.h>
24
#include <net/netlink.h>
25
26
27
28

#include <brcmu_utils.h>
#include <defs.h>
#include <brcmu_wifi.h>
29
#include "core.h"
30
#include "debug.h"
31
#include "tracepoint.h"
32
#include "fwil_types.h"
33
#include "p2p.h"
34
#include "btcoex.h"
35
#include "cfg80211.h"
36
#include "feature.h"
37
#include "fwil.h"
38
#include "proto.h"
39
#include "vendor.h"
40
#include "bus.h"
41
#include "common.h"
42

43
44
45
46
47
48
49
50
51
52
53
54
#define BRCMF_SCAN_IE_LEN_MAX		2048
#define BRCMF_PNO_VERSION		2
#define BRCMF_PNO_TIME			30
#define BRCMF_PNO_REPEAT		4
#define BRCMF_PNO_FREQ_EXPO_MAX		3
#define BRCMF_PNO_MAX_PFN_COUNT		16
#define BRCMF_PNO_ENABLE_ADAPTSCAN_BIT	6
#define BRCMF_PNO_HIDDEN_BIT		2
#define BRCMF_PNO_WPA_AUTH_ANY		0xFFFFFFFF
#define BRCMF_PNO_SCAN_COMPLETE		1
#define BRCMF_PNO_SCAN_INCOMPLETE	0

Hante Meuleman's avatar
Hante Meuleman committed
55
56
57
58
#define WPA_OUI				"\x00\x50\xF2"	/* WPA OUI */
#define WPA_OUI_TYPE			1
#define RSN_OUI				"\x00\x0F\xAC"	/* RSN OUI */
#define	WME_OUI_TYPE			2
59
#define WPS_OUI_TYPE			4
Hante Meuleman's avatar
Hante Meuleman committed
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

#define VS_IE_FIXED_HDR_LEN		6
#define WPA_IE_VERSION_LEN		2
#define WPA_IE_MIN_OUI_LEN		4
#define WPA_IE_SUITE_COUNT_LEN		2

#define WPA_CIPHER_NONE			0	/* None */
#define WPA_CIPHER_WEP_40		1	/* WEP (40-bit) */
#define WPA_CIPHER_TKIP			2	/* TKIP: default for WPA */
#define WPA_CIPHER_AES_CCM		4	/* AES (CCM) */
#define WPA_CIPHER_WEP_104		5	/* WEP (104-bit) */

#define RSN_AKM_NONE			0	/* None (IBSS) */
#define RSN_AKM_UNSPECIFIED		1	/* Over 802.1x */
#define RSN_AKM_PSK			2	/* Pre-shared Key */
#define RSN_CAP_LEN			2	/* Length of RSN capabilities */
#define RSN_CAP_PTK_REPLAY_CNTR_MASK	0x000C

#define VNDR_IE_CMD_LEN			4	/* length of the set command
						 * string :"add", "del" (+ NUL)
						 */
#define VNDR_IE_COUNT_OFFSET		4
#define VNDR_IE_PKTFLAG_OFFSET		8
#define VNDR_IE_VSIE_OFFSET		12
#define VNDR_IE_HDR_SIZE		12
85
#define VNDR_IE_PARSE_LIMIT		5
Hante Meuleman's avatar
Hante Meuleman committed
86
87
88

#define	DOT11_MGMT_HDR_LEN		24	/* d11 management header len */
#define	DOT11_BCN_PRB_FIXED_LEN		12	/* beacon/probe fixed length */
89

90
91
92
93
#define BRCMF_SCAN_JOIN_ACTIVE_DWELL_TIME_MS	320
#define BRCMF_SCAN_JOIN_PASSIVE_DWELL_TIME_MS	400
#define BRCMF_SCAN_JOIN_PROBE_INTERVAL_MS	20

94
95
96
#define BRCMF_ASSOC_PARAMS_FIXED_SIZE \
	(sizeof(struct brcmf_assoc_params_le) - sizeof(u16))

97
static bool check_vif_up(struct brcmf_cfg80211_vif *vif)
98
{
99
	if (!test_bit(BRCMF_VIF_STATUS_READY, &vif->sme_state)) {
100
101
		brcmf_dbg(INFO, "device is not ready : status (%lu)\n",
			  vif->sme_state);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		return false;
	}
	return true;
}

#define RATE_TO_BASE100KBPS(rate)   (((rate) * 10) / 2)
#define RATETAB_ENT(_rateid, _flags) \
	{                                                               \
		.bitrate        = RATE_TO_BASE100KBPS(_rateid),     \
		.hw_value       = (_rateid),                            \
		.flags          = (_flags),                             \
	}

static struct ieee80211_rate __wl_rates[] = {
	RATETAB_ENT(BRCM_RATE_1M, 0),
	RATETAB_ENT(BRCM_RATE_2M, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_5M5, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_11M, IEEE80211_RATE_SHORT_PREAMBLE),
	RATETAB_ENT(BRCM_RATE_6M, 0),
	RATETAB_ENT(BRCM_RATE_9M, 0),
	RATETAB_ENT(BRCM_RATE_12M, 0),
	RATETAB_ENT(BRCM_RATE_18M, 0),
	RATETAB_ENT(BRCM_RATE_24M, 0),
	RATETAB_ENT(BRCM_RATE_36M, 0),
	RATETAB_ENT(BRCM_RATE_48M, 0),
	RATETAB_ENT(BRCM_RATE_54M, 0),
};

#define wl_g_rates		(__wl_rates + 0)
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#define wl_g_rates_size		ARRAY_SIZE(__wl_rates)
#define wl_a_rates		(__wl_rates + 4)
#define wl_a_rates_size		(wl_g_rates_size - 4)

#define CHAN2G(_channel, _freq) {				\
	.band			= IEEE80211_BAND_2GHZ,		\
	.center_freq		= (_freq),			\
	.hw_value		= (_channel),			\
	.flags			= IEEE80211_CHAN_DISABLED,	\
	.max_antenna_gain	= 0,				\
	.max_power		= 30,				\
}

#define CHAN5G(_channel) {					\
	.band			= IEEE80211_BAND_5GHZ,		\
	.center_freq		= 5000 + (5 * (_channel)),	\
	.hw_value		= (_channel),			\
	.flags			= IEEE80211_CHAN_DISABLED,	\
	.max_antenna_gain	= 0,				\
	.max_power		= 30,				\
}

static struct ieee80211_channel __wl_2ghz_channels[] = {
	CHAN2G(1, 2412), CHAN2G(2, 2417), CHAN2G(3, 2422), CHAN2G(4, 2427),
	CHAN2G(5, 2432), CHAN2G(6, 2437), CHAN2G(7, 2442), CHAN2G(8, 2447),
	CHAN2G(9, 2452), CHAN2G(10, 2457), CHAN2G(11, 2462), CHAN2G(12, 2467),
	CHAN2G(13, 2472), CHAN2G(14, 2484)
};

static struct ieee80211_channel __wl_5ghz_channels[] = {
	CHAN5G(34), CHAN5G(36), CHAN5G(38), CHAN5G(40), CHAN5G(42),
	CHAN5G(44), CHAN5G(46), CHAN5G(48), CHAN5G(52), CHAN5G(56),
	CHAN5G(60), CHAN5G(64), CHAN5G(100), CHAN5G(104), CHAN5G(108),
	CHAN5G(112), CHAN5G(116), CHAN5G(120), CHAN5G(124), CHAN5G(128),
	CHAN5G(132), CHAN5G(136), CHAN5G(140), CHAN5G(144), CHAN5G(149),
	CHAN5G(153), CHAN5G(157), CHAN5G(161), CHAN5G(165)
};
168

169
/* Band templates duplicated per wiphy. The channel info
170
 * above is added to the band during setup.
171
172
 */
static const struct ieee80211_supported_band __wl_band_2ghz = {
173
174
175
176
177
	.band = IEEE80211_BAND_2GHZ,
	.bitrates = wl_g_rates,
	.n_bitrates = wl_g_rates_size,
};

178
static const struct ieee80211_supported_band __wl_band_5ghz = {
179
180
181
182
183
	.band = IEEE80211_BAND_5GHZ,
	.bitrates = wl_a_rates,
	.n_bitrates = wl_a_rates_size,
};

184
185
/* This is to override regulatory domains defined in cfg80211 module (reg.c)
 * By default world regulatory domain defined in reg.c puts the flags
186
187
188
 * NL80211_RRF_NO_IR for 5GHz channels (for * 36..48 and 149..165).
 * With respect to these flags, wpa_supplicant doesn't * start p2p
 * operations on 5GHz channels. All the changes in world regulatory
189
190
191
192
193
194
195
196
197
198
199
200
201
202
 * domain are to be done here.
 */
static const struct ieee80211_regdomain brcmf_regdom = {
	.n_reg_rules = 4,
	.alpha2 =  "99",
	.reg_rules = {
		/* IEEE 802.11b/g, channels 1..11 */
		REG_RULE(2412-10, 2472+10, 40, 6, 20, 0),
		/* If any */
		/* IEEE 802.11 channel 14 - Only JP enables
		 * this and for 802.11b only
		 */
		REG_RULE(2484-10, 2484+10, 20, 6, 20, 0),
		/* IEEE 802.11a, channel 36..64 */
203
		REG_RULE(5150-10, 5350+10, 80, 6, 20, 0),
204
		/* IEEE 802.11a, channel 100..165 */
205
		REG_RULE(5470-10, 5850+10, 80, 6, 20, 0), }
206
207
208
209
210
211
212
213
214
215
};

static const u32 __wl_cipher_suites[] = {
	WLAN_CIPHER_SUITE_WEP40,
	WLAN_CIPHER_SUITE_WEP104,
	WLAN_CIPHER_SUITE_TKIP,
	WLAN_CIPHER_SUITE_CCMP,
	WLAN_CIPHER_SUITE_AES_CMAC,
};

Hante Meuleman's avatar
Hante Meuleman committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* Vendor specific ie. id = 221, oui and type defines exact ie */
struct brcmf_vs_tlv {
	u8 id;
	u8 len;
	u8 oui[3];
	u8 oui_type;
};

struct parsed_vndr_ie_info {
	u8 *ie_ptr;
	u32 ie_len;	/* total length including id & length field */
	struct brcmf_vs_tlv vndrie;
};

struct parsed_vndr_ies {
	u32 count;
232
	struct parsed_vndr_ie_info ie_info[VNDR_IE_PARSE_LIMIT];
Hante Meuleman's avatar
Hante Meuleman committed
233
234
};

235
236
237
238
static int brcmf_roamoff;
module_param_named(roamoff, brcmf_roamoff, int, S_IRUSR);
MODULE_PARM_DESC(roamoff, "do not use internal roaming engine");

239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* Quarter dBm units to mW
 * Table starts at QDBM_OFFSET, so the first entry is mW for qdBm=153
 * Table is offset so the last entry is largest mW value that fits in
 * a u16.
 */

#define QDBM_OFFSET 153		/* Offset for first entry */
#define QDBM_TABLE_LEN 40	/* Table size */

/* Smallest mW value that will round up to the first table entry, QDBM_OFFSET.
 * Value is ( mW(QDBM_OFFSET - 1) + mW(QDBM_OFFSET) ) / 2
 */
#define QDBM_TABLE_LOW_BOUND 6493	/* Low bound */

/* Largest mW value that will round down to the last table entry,
 * QDBM_OFFSET + QDBM_TABLE_LEN-1.
 * Value is ( mW(QDBM_OFFSET + QDBM_TABLE_LEN - 1) +
 * mW(QDBM_OFFSET + QDBM_TABLE_LEN) ) / 2.
 */
#define QDBM_TABLE_HIGH_BOUND 64938	/* High bound */

static const u16 nqdBm_to_mW_map[QDBM_TABLE_LEN] = {
/* qdBm:	+0	+1	+2	+3	+4	+5	+6	+7 */
/* 153: */ 6683, 7079, 7499, 7943, 8414, 8913, 9441, 10000,
/* 161: */ 10593, 11220, 11885, 12589, 13335, 14125, 14962, 15849,
/* 169: */ 16788, 17783, 18836, 19953, 21135, 22387, 23714, 25119,
/* 177: */ 26607, 28184, 29854, 31623, 33497, 35481, 37584, 39811,
/* 185: */ 42170, 44668, 47315, 50119, 53088, 56234, 59566, 63096
};

static u16 brcmf_qdbm_to_mw(u8 qdbm)
{
	uint factor = 1;
	int idx = qdbm - QDBM_OFFSET;

	if (idx >= QDBM_TABLE_LEN)
		/* clamp to max u16 mW value */
		return 0xFFFF;

	/* scale the qdBm index up to the range of the table 0-40
	 * where an offset of 40 qdBm equals a factor of 10 mW.
	 */
	while (idx < 0) {
		idx += 40;
		factor *= 10;
	}

	/* return the mW value scaled down to the correct factor of 10,
	 * adding in factor/2 to get proper rounding.
	 */
	return (nqdBm_to_mW_map[idx] + factor / 2) / factor;
}

static u8 brcmf_mw_to_qdbm(u16 mw)
{
	u8 qdbm;
	int offset;
	uint mw_uint = mw;
	uint boundary;

	/* handle boundary case */
	if (mw_uint <= 1)
		return 0;

	offset = QDBM_OFFSET;

	/* move mw into the range of the table */
	while (mw_uint < QDBM_TABLE_LOW_BOUND) {
		mw_uint *= 10;
		offset -= 40;
	}

	for (qdbm = 0; qdbm < QDBM_TABLE_LEN - 1; qdbm++) {
		boundary = nqdBm_to_mW_map[qdbm] + (nqdBm_to_mW_map[qdbm + 1] -
						    nqdBm_to_mW_map[qdbm]) / 2;
		if (mw_uint < boundary)
			break;
	}

	qdbm += (u8) offset;

	return qdbm;
}

323
324
static u16 chandef_to_chanspec(struct brcmu_d11inf *d11inf,
			       struct cfg80211_chan_def *ch)
325
326
327
328
329
330
331
332
333
334
{
	struct brcmu_chan ch_inf;
	s32 primary_offset;

	brcmf_dbg(TRACE, "chandef: control %d center %d width %d\n",
		  ch->chan->center_freq, ch->center_freq1, ch->width);
	ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq1);
	primary_offset = ch->center_freq1 - ch->chan->center_freq;
	switch (ch->width) {
	case NL80211_CHAN_WIDTH_20:
335
	case NL80211_CHAN_WIDTH_20_NOHT:
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
		ch_inf.bw = BRCMU_CHAN_BW_20;
		WARN_ON(primary_offset != 0);
		break;
	case NL80211_CHAN_WIDTH_40:
		ch_inf.bw = BRCMU_CHAN_BW_40;
		if (primary_offset < 0)
			ch_inf.sb = BRCMU_CHAN_SB_U;
		else
			ch_inf.sb = BRCMU_CHAN_SB_L;
		break;
	case NL80211_CHAN_WIDTH_80:
		ch_inf.bw = BRCMU_CHAN_BW_80;
		if (primary_offset < 0) {
			if (primary_offset < -CH_10MHZ_APART)
				ch_inf.sb = BRCMU_CHAN_SB_UU;
			else
				ch_inf.sb = BRCMU_CHAN_SB_UL;
		} else {
			if (primary_offset > CH_10MHZ_APART)
				ch_inf.sb = BRCMU_CHAN_SB_LL;
			else
				ch_inf.sb = BRCMU_CHAN_SB_LU;
		}
		break;
360
361
362
363
	case NL80211_CHAN_WIDTH_80P80:
	case NL80211_CHAN_WIDTH_160:
	case NL80211_CHAN_WIDTH_5:
	case NL80211_CHAN_WIDTH_10:
364
365
366
367
368
369
370
371
372
373
	default:
		WARN_ON_ONCE(1);
	}
	switch (ch->chan->band) {
	case IEEE80211_BAND_2GHZ:
		ch_inf.band = BRCMU_CHAN_BAND_2G;
		break;
	case IEEE80211_BAND_5GHZ:
		ch_inf.band = BRCMU_CHAN_BAND_5G;
		break;
374
	case IEEE80211_BAND_60GHZ:
375
376
377
378
379
380
381
382
	default:
		WARN_ON_ONCE(1);
	}
	d11inf->encchspec(&ch_inf);

	return ch_inf.chspec;
}

Franky Lin's avatar
Franky Lin committed
383
384
u16 channel_to_chanspec(struct brcmu_d11inf *d11inf,
			struct ieee80211_channel *ch)
385
{
Franky Lin's avatar
Franky Lin committed
386
	struct brcmu_chan ch_inf;
387

Franky Lin's avatar
Franky Lin committed
388
389
390
	ch_inf.chnum = ieee80211_frequency_to_channel(ch->center_freq);
	ch_inf.bw = BRCMU_CHAN_BW_20;
	d11inf->encchspec(&ch_inf);
391

Franky Lin's avatar
Franky Lin committed
392
	return ch_inf.chspec;
393
394
}

395
396
397
398
/* Traverse a string of 1-byte tag/1-byte length/variable-length value
 * triples, returning a pointer to the substring whose first element
 * matches tag
 */
399
400
const struct brcmf_tlv *
brcmf_parse_tlvs(const void *buf, int buflen, uint key)
401
{
402
403
	const struct brcmf_tlv *elt = buf;
	int totlen = buflen;
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423

	/* find tagged parameter */
	while (totlen >= TLV_HDR_LEN) {
		int len = elt->len;

		/* validate remaining totlen */
		if ((elt->id == key) && (totlen >= (len + TLV_HDR_LEN)))
			return elt;

		elt = (struct brcmf_tlv *)((u8 *)elt + (len + TLV_HDR_LEN));
		totlen -= (len + TLV_HDR_LEN);
	}

	return NULL;
}

/* Is any of the tlvs the expected entry? If
 * not update the tlvs buffer pointer/length.
 */
static bool
424
425
brcmf_tlv_has_ie(const u8 *ie, const u8 **tlvs, u32 *tlvs_len,
		 const u8 *oui, u32 oui_len, u8 type)
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
{
	/* If the contents match the OUI and the type */
	if (ie[TLV_LEN_OFF] >= oui_len + 1 &&
	    !memcmp(&ie[TLV_BODY_OFF], oui, oui_len) &&
	    type == ie[TLV_BODY_OFF + oui_len]) {
		return true;
	}

	if (tlvs == NULL)
		return false;
	/* point to the next ie */
	ie += ie[TLV_LEN_OFF] + TLV_HDR_LEN;
	/* calculate the length of the rest of the buffer */
	*tlvs_len -= (int)(ie - *tlvs);
	/* update the pointer to the start of the buffer */
	*tlvs = ie;

	return false;
}

static struct brcmf_vs_tlv *
447
brcmf_find_wpaie(const u8 *parse, u32 len)
448
{
449
	const struct brcmf_tlv *ie;
450
451

	while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
452
		if (brcmf_tlv_has_ie((const u8 *)ie, &parse, &len,
453
454
455
456
457
458
459
				     WPA_OUI, TLV_OUI_LEN, WPA_OUI_TYPE))
			return (struct brcmf_vs_tlv *)ie;
	}
	return NULL;
}

static struct brcmf_vs_tlv *
460
brcmf_find_wpsie(const u8 *parse, u32 len)
461
{
462
	const struct brcmf_tlv *ie;
463
464
465
466
467
468
469
470
471
472

	while ((ie = brcmf_parse_tlvs(parse, len, WLAN_EID_VENDOR_SPECIFIC))) {
		if (brcmf_tlv_has_ie((u8 *)ie, &parse, &len,
				     WPA_OUI, TLV_OUI_LEN, WPS_OUI_TYPE))
			return (struct brcmf_vs_tlv *)ie;
	}
	return NULL;
}


473
474
475
476
477
478
479
480
481
482
483
484
485
486
static void convert_key_from_CPU(struct brcmf_wsec_key *key,
				 struct brcmf_wsec_key_le *key_le)
{
	key_le->index = cpu_to_le32(key->index);
	key_le->len = cpu_to_le32(key->len);
	key_le->algo = cpu_to_le32(key->algo);
	key_le->flags = cpu_to_le32(key->flags);
	key_le->rxiv.hi = cpu_to_le32(key->rxiv.hi);
	key_le->rxiv.lo = cpu_to_le16(key->rxiv.lo);
	key_le->iv_initialized = cpu_to_le32(key->iv_initialized);
	memcpy(key_le->data, key->data, sizeof(key->data));
	memcpy(key_le->ea, key->ea, sizeof(key->ea));
}

487
static int
488
send_key_to_dongle(struct brcmf_if *ifp, struct brcmf_wsec_key *key)
489
490
491
492
493
{
	int err;
	struct brcmf_wsec_key_le key_le;

	convert_key_from_CPU(key, &key_le);
494

495
	brcmf_netdev_wait_pend8021x(ifp);
496

497
	err = brcmf_fil_bsscfg_data_set(ifp, "wsec_key", &key_le,
498
					sizeof(key_le));
499

500
	if (err)
501
		brcmf_err("wsec_key error (%d)\n", err);
502
503
504
	return err;
}

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
static s32
brcmf_configure_arp_offload(struct brcmf_if *ifp, bool enable)
{
	s32 err;
	u32 mode;

	if (enable)
		mode = BRCMF_ARP_OL_AGENT | BRCMF_ARP_OL_PEER_AUTO_REPLY;
	else
		mode = 0;

	/* Try to set and enable ARP offload feature, this may fail, then it  */
	/* is simply not supported and err 0 will be returned                 */
	err = brcmf_fil_iovar_int_set(ifp, "arp_ol", mode);
	if (err) {
		brcmf_dbg(TRACE, "failed to set ARP offload mode to 0x%x, err = %d\n",
			  mode, err);
		err = 0;
	} else {
		err = brcmf_fil_iovar_int_set(ifp, "arpoe", enable);
		if (err) {
			brcmf_dbg(TRACE, "failed to configure (%d) ARP offload err = %d\n",
				  enable, err);
			err = 0;
		} else
			brcmf_dbg(TRACE, "successfully configured (%d) ARP offload to 0x%x\n",
				  enable, mode);
	}

	return err;
}

537
538
539
static void
brcmf_cfg80211_update_proto_addr_mode(struct wireless_dev *wdev)
{
540
541
542
543
544
	struct brcmf_cfg80211_vif *vif;
	struct brcmf_if *ifp;

	vif = container_of(wdev, struct brcmf_cfg80211_vif, wdev);
	ifp = vif->ifp;
545
546
547
548
549
550
551
552
553
554
555

	if ((wdev->iftype == NL80211_IFTYPE_ADHOC) ||
	    (wdev->iftype == NL80211_IFTYPE_AP) ||
	    (wdev->iftype == NL80211_IFTYPE_P2P_GO))
		brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx,
						ADDR_DIRECT);
	else
		brcmf_proto_configure_addr_mode(ifp->drvr, ifp->ifidx,
						ADDR_INDIRECT);
}

556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
static int brcmf_cfg80211_request_ap_if(struct brcmf_if *ifp)
{
	struct brcmf_mbss_ssid_le mbss_ssid_le;
	int bsscfgidx;
	int err;

	memset(&mbss_ssid_le, 0, sizeof(mbss_ssid_le));
	bsscfgidx = brcmf_get_next_free_bsscfgidx(ifp->drvr);
	if (bsscfgidx < 0)
		return bsscfgidx;

	mbss_ssid_le.bsscfgidx = cpu_to_le32(bsscfgidx);
	mbss_ssid_le.SSID_len = cpu_to_le32(5);
	sprintf(mbss_ssid_le.SSID, "ssid%d" , bsscfgidx);

	err = brcmf_fil_bsscfg_data_set(ifp, "bsscfg:ssid", &mbss_ssid_le,
					sizeof(mbss_ssid_le));
	if (err < 0)
		brcmf_err("setting ssid failed %d\n", err);

	return err;
}

/**
 * brcmf_ap_add_vif() - create a new AP virtual interface for multiple BSS
 *
 * @wiphy: wiphy device of new interface.
 * @name: name of the new interface.
 * @flags: not used.
 * @params: contains mac address for AP device.
 */
static
struct wireless_dev *brcmf_ap_add_vif(struct wiphy *wiphy, const char *name,
				      u32 *flags, struct vif_params *params)
{
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
	struct brcmf_if *ifp = netdev_priv(cfg_to_ndev(cfg));
	struct brcmf_cfg80211_vif *vif;
	int err;

	if (brcmf_cfg80211_vif_event_armed(cfg))
		return ERR_PTR(-EBUSY);

	brcmf_dbg(INFO, "Adding vif \"%s\"\n", name);

	vif = brcmf_alloc_vif(cfg, NL80211_IFTYPE_AP, false);
	if (IS_ERR(vif))
		return (struct wireless_dev *)vif;

	brcmf_cfg80211_arm_vif_event(cfg, vif);

	err = brcmf_cfg80211_request_ap_if(ifp);
	if (err) {
		brcmf_cfg80211_arm_vif_event(cfg, NULL);
		goto fail;
	}

	/* wait for firmware event */
	err = brcmf_cfg80211_wait_vif_event_timeout(cfg, BRCMF_E_IF_ADD,
						    msecs_to_jiffies(1500));
	brcmf_cfg80211_arm_vif_event(cfg, NULL);
	if (!err) {
		brcmf_err("timeout occurred\n");
		err = -EIO;
		goto fail;
	}

	/* interface created in firmware */
	ifp = vif->ifp;
	if (!ifp) {
		brcmf_err("no if pointer provided\n");
		err = -ENOENT;
		goto fail;
	}

	strncpy(ifp->ndev->name, name, sizeof(ifp->ndev->name) - 1);
	err = brcmf_net_attach(ifp, true);
	if (err) {
		brcmf_err("Registering netdevice failed\n");
		goto fail;
	}

	return &ifp->vif->wdev;

fail:
	brcmf_free_vif(vif);
	return ERR_PTR(err);
}

645
646
647
648
649
650
651
652
653
654
655
656
657
static bool brcmf_is_apmode(struct brcmf_cfg80211_vif *vif)
{
	enum nl80211_iftype iftype;

	iftype = vif->wdev.iftype;
	return iftype == NL80211_IFTYPE_AP || iftype == NL80211_IFTYPE_P2P_GO;
}

static bool brcmf_is_ibssmode(struct brcmf_cfg80211_vif *vif)
{
	return vif->wdev.iftype == NL80211_IFTYPE_ADHOC;
}

658
659
static struct wireless_dev *brcmf_cfg80211_add_iface(struct wiphy *wiphy,
						     const char *name,
660
						     unsigned char name_assign_type,
661
662
663
664
						     enum nl80211_iftype type,
						     u32 *flags,
						     struct vif_params *params)
{
665
666
	struct wireless_dev *wdev;

667
668
669
670
671
672
673
674
675
	brcmf_dbg(TRACE, "enter: %s type %d\n", name, type);
	switch (type) {
	case NL80211_IFTYPE_ADHOC:
	case NL80211_IFTYPE_STATION:
	case NL80211_IFTYPE_AP_VLAN:
	case NL80211_IFTYPE_WDS:
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_MESH_POINT:
		return ERR_PTR(-EOPNOTSUPP);
676
677
678
679
680
	case NL80211_IFTYPE_AP:
		wdev = brcmf_ap_add_vif(wiphy, name, flags, params);
		if (!IS_ERR(wdev))
			brcmf_cfg80211_update_proto_addr_mode(wdev);
		return wdev;
681
682
	case NL80211_IFTYPE_P2P_CLIENT:
	case NL80211_IFTYPE_P2P_GO:
683
	case NL80211_IFTYPE_P2P_DEVICE:
684
		wdev = brcmf_p2p_add_vif(wiphy, name, name_assign_type, type, flags, params);
685
686
687
		if (!IS_ERR(wdev))
			brcmf_cfg80211_update_proto_addr_mode(wdev);
		return wdev;
688
689
690
691
692
693
	case NL80211_IFTYPE_UNSPECIFIED:
	default:
		return ERR_PTR(-EINVAL);
	}
}

694
695
static void brcmf_scan_config_mpc(struct brcmf_if *ifp, int mpc)
{
696
	if (brcmf_feat_is_quirk_enabled(ifp, BRCMF_FEAT_QUIRK_NEED_MPC))
697
698
699
		brcmf_set_mpc(ifp, mpc);
}

700
void brcmf_set_mpc(struct brcmf_if *ifp, int mpc)
701
702
703
704
705
706
707
708
709
710
711
712
713
{
	s32 err = 0;

	if (check_vif_up(ifp->vif)) {
		err = brcmf_fil_iovar_int_set(ifp, "mpc", mpc);
		if (err) {
			brcmf_err("fail to set mpc\n");
			return;
		}
		brcmf_dbg(INFO, "MPC : %d\n", mpc);
	}
}

714
715
716
s32 brcmf_notify_escan_complete(struct brcmf_cfg80211_info *cfg,
				struct brcmf_if *ifp, bool aborted,
				bool fw_abort)
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
{
	struct brcmf_scan_params_le params_le;
	struct cfg80211_scan_request *scan_request;
	s32 err = 0;

	brcmf_dbg(SCAN, "Enter\n");

	/* clear scan request, because the FW abort can cause a second call */
	/* to this functon and might cause a double cfg80211_scan_done      */
	scan_request = cfg->scan_request;
	cfg->scan_request = NULL;

	if (timer_pending(&cfg->escan_timeout))
		del_timer_sync(&cfg->escan_timeout);

	if (fw_abort) {
		/* Do a scan abort to stop the driver's scan engine */
		brcmf_dbg(SCAN, "ABORT scan in firmware\n");
		memset(&params_le, 0, sizeof(params_le));
736
		eth_broadcast_addr(params_le.bssid);
737
738
739
740
741
742
743
744
745
746
		params_le.bss_type = DOT11_BSSTYPE_ANY;
		params_le.scan_type = 0;
		params_le.channel_num = cpu_to_le32(1);
		params_le.nprobes = cpu_to_le32(1);
		params_le.active_time = cpu_to_le32(-1);
		params_le.passive_time = cpu_to_le32(-1);
		params_le.home_time = cpu_to_le32(-1);
		/* Scan is aborted by setting channel_list[0] to -1 */
		params_le.channel_list[0] = cpu_to_le16(-1);
		/* E-Scan (or anyother type) can be aborted by SCAN */
747
		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
748
749
750
751
					     &params_le, sizeof(params_le));
		if (err)
			brcmf_err("Scan abort  failed\n");
	}
752

753
	brcmf_scan_config_mpc(ifp, 1);
754

755
756
757
758
759
760
761
762
763
764
765
766
767
768
	/*
	 * e-scan can be initiated by scheduled scan
	 * which takes precedence.
	 */
	if (cfg->sched_escan) {
		brcmf_dbg(SCAN, "scheduled scan completed\n");
		cfg->sched_escan = false;
		if (!aborted)
			cfg80211_sched_scan_results(cfg_to_wiphy(cfg));
	} else if (scan_request) {
		brcmf_dbg(SCAN, "ESCAN Completed scan: %s\n",
			  aborted ? "Aborted" : "Done");
		cfg80211_scan_done(scan_request, aborted);
	}
769
770
	if (!test_and_clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status))
		brcmf_dbg(SCAN, "Scan complete, probably P2P scan\n");
771
772
773
774

	return err;
}

775
776
777
static
int brcmf_cfg80211_del_iface(struct wiphy *wiphy, struct wireless_dev *wdev)
{
778
779
780
781
782
783
784
785
786
	struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
	struct net_device *ndev = wdev->netdev;

	/* vif event pending in firmware */
	if (brcmf_cfg80211_vif_event_armed(cfg))
		return -EBUSY;

	if (ndev) {
		if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status) &&
787
788
789
		    cfg->escan_info.ifp == netdev_priv(ndev))
			brcmf_notify_escan_complete(cfg, netdev_priv(ndev),
						    true, true);
790
791
792
793

		brcmf_fil_iovar_int_set(netdev_priv(ndev), "mpc", 1);
	}

794
795
796
797
798
799
800
801
802
803
804
	switch (wdev->iftype) {
	case NL80211_IFTYPE_ADHOC:
	case NL80211_IFTYPE_STATION:
	case NL80211_IFTYPE_AP:
	case NL80211_IFTYPE_AP_VLAN:
	case NL80211_IFTYPE_WDS:
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_MESH_POINT:
		return -EOPNOTSUPP;
	case NL80211_IFTYPE_P2P_CLIENT:
	case NL80211_IFTYPE_P2P_GO:
805
	case NL80211_IFTYPE_P2P_DEVICE:
806
807
808
809
810
811
812
813
		return brcmf_p2p_del_vif(wiphy, wdev);
	case NL80211_IFTYPE_UNSPECIFIED:
	default:
		return -EINVAL;
	}
	return -EOPNOTSUPP;
}

814
815
816
817
818
static s32
brcmf_cfg80211_change_iface(struct wiphy *wiphy, struct net_device *ndev,
			 enum nl80211_iftype type, u32 *flags,
			 struct vif_params *params)
{
819
	struct brcmf_cfg80211_info *cfg = wiphy_priv(wiphy);
820
	struct brcmf_if *ifp = netdev_priv(ndev);
821
	struct brcmf_cfg80211_vif *vif = ifp->vif;
822
	s32 infra = 0;
Hante Meuleman's avatar
Hante Meuleman committed
823
	s32 ap = 0;
824
825
	s32 err = 0;

826
	brcmf_dbg(TRACE, "Enter, ndev=%p, type=%d\n", ndev, type);
827
828
829
830

	switch (type) {
	case NL80211_IFTYPE_MONITOR:
	case NL80211_IFTYPE_WDS:
831
832
		brcmf_err("type (%d) : currently we do not support this type\n",
			  type);
833
834
835
836
837
		return -EOPNOTSUPP;
	case NL80211_IFTYPE_ADHOC:
		infra = 0;
		break;
	case NL80211_IFTYPE_STATION:
838
839
840
841
842
843
844
845
846
847
848
849
		/* Ignore change for p2p IF. Unclear why supplicant does this */
		if ((vif->wdev.iftype == NL80211_IFTYPE_P2P_CLIENT) ||
		    (vif->wdev.iftype == NL80211_IFTYPE_P2P_GO)) {
			brcmf_dbg(TRACE, "Ignoring cmd for p2p if\n");
			/* WAR: It is unexpected to get a change of VIF for P2P
			 * IF, but it happens. The request can not be handled
			 * but returning EPERM causes a crash. Returning 0
			 * without setting ieee80211_ptr->iftype causes trace
			 * (WARN_ON) but it works with wpa_supplicant
			 */
			return 0;
		}
850
851
		infra = 1;
		break;
Hante Meuleman's avatar
Hante Meuleman committed
852
	case NL80211_IFTYPE_AP:
853
	case NL80211_IFTYPE_P2P_GO:
Hante Meuleman's avatar
Hante Meuleman committed
854
855
		ap = 1;
		break;
856
857
858
859
860
	default:
		err = -EINVAL;
		goto done;
	}

Hante Meuleman's avatar
Hante Meuleman committed
861
	if (ap) {
862
863
864
865
866
867
868
869
		if (type == NL80211_IFTYPE_P2P_GO) {
			brcmf_dbg(INFO, "IF Type = P2P GO\n");
			err = brcmf_p2p_ifchange(cfg, BRCMF_FIL_P2P_IF_GO);
		}
		if (!err) {
			set_bit(BRCMF_VIF_STATUS_AP_CREATING, &vif->sme_state);
			brcmf_dbg(INFO, "IF Type = AP\n");
		}
870
	} else {
871
		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_INFRA, infra);
Hante Meuleman's avatar
Hante Meuleman committed
872
		if (err) {
873
			brcmf_err("WLC_SET_INFRA error (%d)\n", err);
Hante Meuleman's avatar
Hante Meuleman committed
874
875
876
			err = -EAGAIN;
			goto done;
		}
877
		brcmf_dbg(INFO, "IF Type = %s\n", brcmf_is_ibssmode(vif) ?
878
			  "Adhoc" : "Infra");
879
	}
Hante Meuleman's avatar
Hante Meuleman committed
880
	ndev->ieee80211_ptr->iftype = type;
881

882
883
	brcmf_cfg80211_update_proto_addr_mode(&vif->wdev);

884
done:
885
	brcmf_dbg(TRACE, "Exit\n");
886
887
888
889

	return err;
}

Franky Lin's avatar
Franky Lin committed
890
891
static void brcmf_escan_prep(struct brcmf_cfg80211_info *cfg,
			     struct brcmf_scan_params_le *params_le,
Hante Meuleman's avatar
Hante Meuleman committed
892
893
894
895
896
897
			     struct cfg80211_scan_request *request)
{
	u32 n_ssids;
	u32 n_channels;
	s32 i;
	s32 offset;
898
	u16 chanspec;
Hante Meuleman's avatar
Hante Meuleman committed
899
	char *ptr;
900
	struct brcmf_ssid_le ssid_le;
Hante Meuleman's avatar
Hante Meuleman committed
901

902
	eth_broadcast_addr(params_le->bssid);
Hante Meuleman's avatar
Hante Meuleman committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
	params_le->bss_type = DOT11_BSSTYPE_ANY;
	params_le->scan_type = 0;
	params_le->channel_num = 0;
	params_le->nprobes = cpu_to_le32(-1);
	params_le->active_time = cpu_to_le32(-1);
	params_le->passive_time = cpu_to_le32(-1);
	params_le->home_time = cpu_to_le32(-1);
	memset(&params_le->ssid_le, 0, sizeof(params_le->ssid_le));

	/* if request is null exit so it will be all channel broadcast scan */
	if (!request)
		return;

	n_ssids = request->n_ssids;
	n_channels = request->n_channels;
	/* Copy channel array if applicable */
919
920
	brcmf_dbg(SCAN, "### List of channelspecs to scan ### %d\n",
		  n_channels);
Hante Meuleman's avatar
Hante Meuleman committed
921
922
	if (n_channels > 0) {
		for (i = 0; i < n_channels; i++) {
Franky Lin's avatar
Franky Lin committed
923
924
			chanspec = channel_to_chanspec(&cfg->d11inf,
						       request->channels[i]);
925
926
			brcmf_dbg(SCAN, "Chan : %d, Channel spec: %x\n",
				  request->channels[i]->hw_value, chanspec);
927
			params_le->channel_list[i] = cpu_to_le16(chanspec);
Hante Meuleman's avatar
Hante Meuleman committed
928
929
		}
	} else {
930
		brcmf_dbg(SCAN, "Scanning all channels\n");
Hante Meuleman's avatar
Hante Meuleman committed
931
932
	}
	/* Copy ssid array if applicable */
933
	brcmf_dbg(SCAN, "### List of SSIDs to scan ### %d\n", n_ssids);
Hante Meuleman's avatar
Hante Meuleman committed
934
935
936
937
938
939
	if (n_ssids > 0) {
		offset = offsetof(struct brcmf_scan_params_le, channel_list) +
				n_channels * sizeof(u16);
		offset = roundup(offset, sizeof(u32));
		ptr = (char *)params_le + offset;
		for (i = 0; i < n_ssids; i++) {
940
941
942
943
944
945
			memset(&ssid_le, 0, sizeof(ssid_le));
			ssid_le.SSID_len =
					cpu_to_le32(request->ssids[i].ssid_len);
			memcpy(ssid_le.SSID, request->ssids[i].ssid,
			       request->ssids[i].ssid_len);
			if (!ssid_le.SSID_len)
946
				brcmf_dbg(SCAN, "%d: Broadcast scan\n", i);
Hante Meuleman's avatar
Hante Meuleman committed
947
			else
948
949
				brcmf_dbg(SCAN, "%d: scan for  %s size =%d\n",
					  i, ssid_le.SSID, ssid_le.SSID_len);
950
951
			memcpy(ptr, &ssid_le, sizeof(ssid_le));
			ptr += sizeof(ssid_le);
Hante Meuleman's avatar
Hante Meuleman committed
952
953
		}
	} else {
954
		brcmf_dbg(SCAN, "Broadcast scan %p\n", request->ssids);
Hante Meuleman's avatar
Hante Meuleman committed
955
		if ((request->ssids) && request->ssids->ssid_len) {
956
957
958
			brcmf_dbg(SCAN, "SSID %s len=%d\n",
				  params_le->ssid_le.SSID,
				  request->ssids->ssid_len);
Hante Meuleman's avatar
Hante Meuleman committed
959
960
961
962
963
964
965
966
967
968
969
970
971
			params_le->ssid_le.SSID_len =
				cpu_to_le32(request->ssids->ssid_len);
			memcpy(&params_le->ssid_le.SSID, request->ssids->ssid,
				request->ssids->ssid_len);
		}
	}
	/* Adding mask to channel numbers */
	params_le->channel_num =
		cpu_to_le32((n_ssids << BRCMF_SCAN_PARAMS_NSSID_SHIFT) |
			(n_channels & BRCMF_SCAN_PARAMS_COUNT_MASK));
}

static s32
972
brcmf_run_escan(struct brcmf_cfg80211_info *cfg, struct brcmf_if *ifp,
Hante Meuleman's avatar
Hante Meuleman committed
973
974
975
976
977
978
979
		struct cfg80211_scan_request *request, u16 action)
{
	s32 params_size = BRCMF_SCAN_PARAMS_FIXED_SIZE +
			  offsetof(struct brcmf_escan_params_le, params_le);
	struct brcmf_escan_params_le *params;
	s32 err = 0;

980
	brcmf_dbg(SCAN, "E-SCAN START\n");
Hante Meuleman's avatar
Hante Meuleman committed
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995

	if (request != NULL) {
		/* Allocate space for populating ssids in struct */
		params_size += sizeof(u32) * ((request->n_channels + 1) / 2);

		/* Allocate space for populating ssids in struct */
		params_size += sizeof(struct brcmf_ssid) * request->n_ssids;
	}

	params = kzalloc(params_size, GFP_KERNEL);
	if (!params) {
		err = -ENOMEM;
		goto exit;
	}
	BUG_ON(params_size + sizeof("escan") >= BRCMF_DCMD_MEDLEN);
Franky Lin's avatar
Franky Lin committed
996
	brcmf_escan_prep(cfg, &params->params_le, request);
Hante Meuleman's avatar
Hante Meuleman committed
997
998
999
1000
	params->version = cpu_to_le32(BRCMF_ESCAN_REQ_VERSION);
	params->action = cpu_to_le16(action);
	params->sync_id = cpu_to_le16(0x1234);

1001
	err = brcmf_fil_iovar_data_set(ifp, "escan", params, params_size);
Hante Meuleman's avatar
Hante Meuleman committed
1002
1003
	if (err) {
		if (err == -EBUSY)
1004
			brcmf_dbg(INFO, "system busy : escan canceled\n");
Hante Meuleman's avatar
Hante Meuleman committed
1005
		else
1006
			brcmf_err("error (%d)\n", err);
Hante Meuleman's avatar
Hante Meuleman committed
1007
1008
1009
1010
1011
1012
1013
1014
	}

	kfree(params);
exit:
	return err;
}

static s32
1015
brcmf_do_escan(struct brcmf_cfg80211_info *cfg, struct wiphy *wiphy,
1016
	       struct brcmf_if *ifp, struct cfg80211_scan_request *request)
Hante Meuleman's avatar
Hante Meuleman committed
1017
1018
{
	s32 err;
1019
	u32 passive_scan;
Hante Meuleman's avatar
Hante Meuleman committed
1020
	struct brcmf_scan_results *results;
1021
	struct escan_info *escan = &cfg->escan_info;
Hante Meuleman's avatar
Hante Meuleman committed
1022

1023
	brcmf_dbg(SCAN, "Enter\n");
1024
	escan->ifp = ifp;
1025
1026
	escan->wiphy = wiphy;
	escan->escan_state = WL_ESCAN_STATE_SCANNING;
1027
	passive_scan = cfg->active_scan ? 0 : 1;
1028
	err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
1029
				    passive_scan);
Hante Meuleman's avatar
Hante Meuleman committed
1030
	if (err) {
1031
		brcmf_err("error (%d)\n", err);
Hante Meuleman's avatar
Hante Meuleman committed
1032
1033
		return err;
	}
1034
	brcmf_scan_config_mpc(ifp, 0);
1035
	results = (struct brcmf_scan_results *)cfg->escan_info.escan_buf;
Hante Meuleman's avatar
Hante Meuleman committed
1036
1037
1038
1039
	results->version = 0;
	results->count = 0;
	results->buflen = WL_ESCAN_RESULTS_FIXED_SIZE;

1040
	err = escan->run(cfg, ifp, request, WL_ESCAN_ACTION_START);
Hante Meuleman's avatar
Hante Meuleman committed
1041
	if (err)
1042
		brcmf_scan_config_mpc(ifp, 1);
Hante Meuleman's avatar
Hante Meuleman committed
1043
1044
1045
1046
	return err;
}

static s32
1047
brcmf_cfg80211_escan(struct wiphy *wiphy, struct brcmf_cfg80211_vif *vif,
Hante Meuleman's avatar
Hante Meuleman committed
1048
1049
1050
		     struct cfg80211_scan_request *request,
		     struct cfg80211_ssid *this_ssid)
{
1051
1052
	struct brcmf_if *ifp = vif->ifp;
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
Hante Meuleman's avatar
Hante Meuleman committed
1053
	struct cfg80211_ssid *ssids;
1054
	struct brcmf_cfg80211_scan_req *sr = &cfg->scan_req_int;
1055
	u32 passive_scan;
Hante Meuleman's avatar
Hante Meuleman committed
1056
1057
1058
1059
1060
	bool escan_req;
	bool spec_scan;
	s32 err;
	u32 SSID_len;

1061
	brcmf_dbg(SCAN, "START ESCAN\n");
Hante Meuleman's avatar
Hante Meuleman committed
1062

1063
	if (test_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status)) {
1064
		brcmf_err("Scanning already: status (%lu)\n", cfg->scan_status);
Hante Meuleman's avatar
Hante Meuleman committed
1065
1066
		return -EAGAIN;
	}
1067
	if (test_bit(BRCMF_SCAN_STATUS_ABORT, &cfg->scan_status)) {
1068
1069
		brcmf_err("Scanning being aborted: status (%lu)\n",
			  cfg->scan_status);
Hante Meuleman's avatar
Hante Meuleman committed
1070
1071
		return -EAGAIN;
	}
1072
1073
1074
1075
1076
	if (test_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status)) {
		brcmf_err("Scanning suppressed: status (%lu)\n",
			  cfg->scan_status);
		return -EAGAIN;
	}
1077
	if (test_bit(BRCMF_VIF_STATUS_CONNECTING, &ifp->vif->sme_state)) {
1078
		brcmf_err("Connecting: status (%lu)\n", ifp->vif->sme_state);
Hante Meuleman's avatar
Hante Meuleman committed
1079
1080
1081
		return -EAGAIN;
	}

1082
	/* If scan req comes for p2p0, send it over primary I/F */
1083
1084
	if (vif == cfg->p2p.bss_idx[P2PAPI_BSSCFG_DEVICE].vif)
		vif = cfg->p2p.bss_idx[P2PAPI_BSSCFG_PRIMARY].vif;
1085

Hante Meuleman's avatar
Hante Meuleman committed
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
	escan_req = false;
	if (request) {
		/* scan bss */
		ssids = request->ssids;
		escan_req = true;
	} else {
		/* scan in ibss */
		/* we don't do escan in ibss */
		ssids = this_ssid;
	}

1097
	cfg->scan_request = request;
1098
	set_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
Hante Meuleman's avatar
Hante Meuleman committed
1099
	if (escan_req) {
1100
		cfg->escan_info.run = brcmf_run_escan;
1101
		err = brcmf_p2p_scan_prep(wiphy, request, vif);
1102
1103
1104
		if (err)
			goto scan_out;

1105
		err = brcmf_do_escan(cfg, wiphy, vif->ifp, request);
1106
		if (err)
Hante Meuleman's avatar
Hante Meuleman committed
1107
1108
			goto scan_out;
	} else {
1109
1110
		brcmf_dbg(SCAN, "ssid \"%s\", ssid_len (%d)\n",
			  ssids->ssid, ssids->ssid_len);
Hante Meuleman's avatar
Hante Meuleman committed
1111
1112
1113
1114
1115
1116
1117
1118
1119
		memset(&sr->ssid_le, 0, sizeof(sr->ssid_le));
		SSID_len = min_t(u8, sizeof(sr->ssid_le.SSID), ssids->ssid_len);
		sr->ssid_le.SSID_len = cpu_to_le32(0);
		spec_scan = false;
		if (SSID_len) {
			memcpy(sr->ssid_le.SSID, ssids->ssid, SSID_len);
			sr->ssid_le.SSID_len = cpu_to_le32(SSID_len);
			spec_scan = true;
		} else
1120
			brcmf_dbg(SCAN, "Broadcast scan\n");
Hante Meuleman's avatar
Hante Meuleman committed
1121

1122
		passive_scan = cfg->active_scan ? 0 : 1;
1123
		err = brcmf_fil_cmd_int_set(ifp, BRCMF_C_SET_PASSIVE_SCAN,
1124
					    passive_scan);
Hante Meuleman's avatar
Hante Meuleman committed
1125
		if (err) {
1126
			brcmf_err("WLC_SET_PASSIVE_SCAN error (%d)\n", err);
Hante Meuleman's avatar
Hante Meuleman committed
1127
1128
			goto scan_out;
		}
1129
		brcmf_scan_config_mpc(ifp, 0);
1130
		err = brcmf_fil_cmd_data_set(ifp, BRCMF_C_SCAN,
1131
					     &sr->ssid_le, sizeof(sr->ssid_le));
Hante Meuleman's avatar
Hante Meuleman committed
1132
1133
		if (err) {
			if (err == -EBUSY)
1134
1135
				brcmf_dbg(INFO, "BUSY: scan for \"%s\" canceled\n",
					  sr->ssid_le.SSID);
Hante Meuleman's avatar
Hante Meuleman committed
1136
			else
1137
				brcmf_err("WLC_SCAN error (%d)\n", err);
Hante Meuleman's avatar
Hante Meuleman committed
1138

1139
			brcmf_scan_config_mpc(ifp, 1);
Hante Meuleman's avatar
Hante Meuleman committed
1140
1141
1142
1143
			goto scan_out;
		}
	}

1144
1145
1146
1147
	/* Arm scan timeout timer */
	mod_timer(&cfg->escan_timeout, jiffies +
			WL_ESCAN_TIMER_INTERVAL_MS * HZ / 1000);

Hante Meuleman's avatar
Hante Meuleman committed
1148
1149
1150
	return 0;

scan_out:
1151
	clear_bit(BRCMF_SCAN_STATUS_BUSY, &cfg->scan_status);
1152
	cfg->scan_request = NULL;
Hante Meuleman's avatar
Hante Meuleman committed
1153
1154
1155
	return err;
}

1156
static s32
1157
brcmf_cfg80211_scan(struct wiphy *wiphy, struct cfg80211_scan_request *request)
1158
{
1159
	struct brcmf_cfg80211_vif *vif;
1160
1161
	s32 err = 0;

1162
	brcmf_dbg(TRACE, "Enter\n");
1163
1164
	vif = container_of(request->wdev, struct brcmf_cfg80211_vif, wdev);
	if (!check_vif_up(vif))
1165
1166
		return -EIO;

1167
	err = brcmf_cfg80211_escan(wiphy, vif, request, NULL);
Hante Meuleman's avatar
Hante Meuleman committed
1168

1169
	if (err)
1170
		brcmf_err("scan error (%d)\n", err);
1171

1172
	brcmf_dbg(TRACE, "Exit\n");
1173
1174
1175
1176
1177
1178
1179
	return err;
}

static s32 brcmf_set_rts(struct net_device *ndev, u32 rts_threshold)
{
	s32 err = 0;

1180
1181
	err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "rtsthresh",
				      rts_threshold);
1182
	if (err)
1183
		brcmf_err("Error (%d)\n", err);
1184
1185
1186
1187
1188
1189
1190
1191

	return err;
}

static s32 brcmf_set_frag(struct net_device *ndev, u32 frag_threshold)
{
	s32 err = 0;

1192
1193
	err = brcmf_fil_iovar_int_set(netdev_priv(ndev), "fragthresh",
				      frag_threshold);
1194
	if (err)
1195
		brcmf_err("Error (%d)\n", err);
1196
1197
1198
1199
1200
1201
1202

	return err;
}

static s32 brcmf_set_retry(struct net_device *ndev, u32 retry, bool l)
{
	s32 err = 0;
Hante Meuleman's avatar
Hante Meuleman committed
1203
	u32 cmd = (l ? BRCMF_C_SET_LRL : BRCMF_C_SET_SRL);
1204

1205
	err = brcmf_fil_cmd_int_set(netdev_priv(ndev), cmd, retry);
1206
	if (err) {
1207
		brcmf_err("cmd (%d) , error (%d)\n", cmd, err);
1208
1209
1210
1211
1212
1213
1214
		return err;
	}
	return err;
}

static s32 brcmf_cfg80211_set_wiphy_params(struct wiphy *wiphy, u32 changed)
{
1215
1216
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
	struct net_device *ndev = cfg_to_ndev(cfg);
1217
	struct brcmf_if *ifp = netdev_priv(ndev);
1218
1219
	s32 err = 0;

1220
	brcmf_dbg(TRACE, "Enter\n");
1221
	if (!check_vif_up(ifp->vif))
1222
1223
1224
		return -EIO;

	if (changed & WIPHY_PARAM_RTS_THRESHOLD &&
1225
1226
1227
	    (cfg->conf->rts_threshold != wiphy->rts_threshold)) {
		cfg->conf->rts_threshold = wiphy->rts_threshold;
		err = brcmf_set_rts(ndev, cfg->conf->rts_threshold);
1228
1229
1230
1231
		if (!err)
			goto done;
	}
	if (changed & WIPHY_PARAM_FRAG_THRESHOLD &&
1232
1233
1234
	    (cfg->conf->frag_threshold != wiphy->frag_threshold)) {
		cfg->conf->frag_threshold = wiphy->frag_threshold;
		err = brcmf_set_frag(ndev, cfg->conf->frag_threshold);
1235
1236
1237
1238
		if (!err)
			goto done;
	}
	if (changed & WIPHY_PARAM_RETRY_LONG
1239
1240
1241
	    && (cfg->conf->retry_long != wiphy->retry_long)) {
		cfg->conf->retry_long = wiphy->retry_long;
		err = brcmf_set_retry(ndev, cfg->conf->retry_long, true);
1242
1243
1244
1245
		if (!err)
			goto done;
	}
	if (changed & WIPHY_PARAM_RETRY_SHORT
1246
1247
1248
	    && (cfg->conf->retry_short != wiphy->retry_short)) {
		cfg->conf->retry_short = wiphy->retry_short;
		err = brcmf_set_retry(ndev, cfg->conf->retry_short, false);
1249
1250
1251
1252
1253
		if (!err)
			goto done;
	}

done:
1254
	brcmf_dbg(TRACE, "Exit\n");
1255
1256
1257
1258
1259
1260
1261
1262
	return err;
}

static void brcmf_init_prof(struct brcmf_cfg80211_profile *prof)
{
	memset(prof, 0, sizeof(*prof));
}

1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
static u16 brcmf_map_fw_linkdown_reason(const struct brcmf_event_msg *e)
{
	u16 reason;

	switch (e->event_code) {
	case BRCMF_E_DEAUTH:
	case BRCMF_E_DEAUTH_IND:
	case BRCMF_E_DISASSOC_IND:
		reason = e->reason;
		break;
	case BRCMF_E_LINK:
	default:
		reason = 0;
		break;
	}
	return reason;
}

static void brcmf_link_down(struct brcmf_cfg80211_vif *vif, u16 reason)
1282
{
1283
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(vif->wdev.wiphy);
1284
1285
	s32 err = 0;

1286
	brcmf_dbg(TRACE, "Enter\n");
1287

1288
	if (test_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state)) {
1289
		brcmf_dbg(INFO, "Call WLC_DISASSOC to stop excess roaming\n ");
1290
		err = brcmf_fil_cmd_data_set(vif->ifp,
1291
					     BRCMF_C_DISASSOC, NULL, 0);
1292
		if (err) {
1293
			brcmf_err("WLC_DISASSOC failed (%d)\n", err);
1294
		}
1295
		clear_bit(BRCMF_VIF_STATUS_CONNECTED, &vif->sme_state);
1296
		cfg80211_disconnected(vif->wdev.netdev, reason, NULL, 0,
1297
				      true, GFP_KERNEL);
1298

1299
	}
1300
	clear_bit(BRCMF_VIF_STATUS_CONNECTING, &vif->sme_state);
1301
1302
	clear_bit(BRCMF_SCAN_STATUS_SUPPRESS, &cfg->scan_status);
	brcmf_btcoex_set_mode(vif, BRCMF_BTCOEX_ENABLED, 0);
1303
	brcmf_dbg(TRACE, "Exit\n");
1304
1305
1306
1307
1308
1309
}

static s32
brcmf_cfg80211_join_ibss(struct wiphy *wiphy, struct net_device *ndev,
		      struct cfg80211_ibss_params *params)
{
1310
	struct brcmf_cfg80211_info *cfg = wiphy_to_cfg(wiphy);
1311
1312
	struct brcmf_if *ifp = netdev_priv(ndev);
	struct brcmf_cfg80211_profile *profile = &ifp->vif->profile;
1313
1314
1315
1316
1317
	struct brcmf_join_params join_params;
	size_t join_params_size = 0;
	s32 err = 0;
	s32 wsec = 0;
	s32 bcnprd;
1318
	u16 chanspec;
1319

1320
	brcmf_dbg(TRACE, "Enter\n");
1321
	if (!check_vif_up(ifp->vif))
1322
1323
1324
		return -EIO;

	if (params->ssid)
1325
		brcmf_dbg(CONN, "SSID: %s\n", params->ssid