mac.c 154 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
 * Copyright (c) 2005-2011 Atheros Communications Inc.
 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
 *
 * 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.
 */

#include "mac.h"

#include <net/mac80211.h>
#include <linux/etherdevice.h>

Michal Kazior's avatar
Michal Kazior committed
23
#include "hif.h"
24
25
26
27
28
#include "core.h"
#include "debug.h"
#include "wmi.h"
#include "htt.h"
#include "txrx.h"
Kalle Valo's avatar
Kalle Valo committed
29
#include "testmode.h"
Michal Kazior's avatar
Michal Kazior committed
30
31
#include "wmi.h"
#include "wmi-ops.h"
32
#include "wow.h"
33

34
35
36
37
38
/*********/
/* Rates */
/*********/

static struct ieee80211_rate ath10k_rates[] = {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	{ .bitrate = 10,
	  .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
	{ .bitrate = 20,
	  .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
	{ .bitrate = 55,
	  .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
	{ .bitrate = 110,
	  .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
	  .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
	  .flags = IEEE80211_RATE_SHORT_PREAMBLE },
53
54
55
56
57
58
59
60
61

	{ .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
	{ .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
	{ .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
	{ .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
	{ .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
	{ .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
	{ .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
	{ .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
62
63
};

64
65
66
67
68
#define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4

#define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
			     ATH10K_MAC_FIRST_OFDM_RATE_IDX)
69
70
71
#define ath10k_g_rates (ath10k_rates + 0)
#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static bool ath10k_mac_bitrate_is_cck(int bitrate)
{
	switch (bitrate) {
	case 10:
	case 20:
	case 55:
	case 110:
		return true;
	}

	return false;
}

static u8 ath10k_mac_bitrate_to_rate(int bitrate)
{
	return DIV_ROUND_UP(bitrate, 5) |
	       (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
}

91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
			     u8 hw_rate)
{
	const struct ieee80211_rate *rate;
	int i;

	for (i = 0; i < sband->n_bitrates; i++) {
		rate = &sband->bitrates[i];

		if (rate->hw_value == hw_rate)
			return i;
		else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
			 rate->hw_value_short == hw_rate)
			return i;
	}

	return 0;
}

110
111
112
113
114
115
116
117
118
119
120
121
u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
			     u32 bitrate)
{
	int i;

	for (i = 0; i < sband->n_bitrates; i++)
		if (sband->bitrates[i].bitrate == bitrate)
			return i;

	return 0;
}

122
123
124
125
126
127
128
/**********/
/* Crypto */
/**********/

static int ath10k_send_key(struct ath10k_vif *arvif,
			   struct ieee80211_key_conf *key,
			   enum set_key_cmd cmd,
129
			   const u8 *macaddr, u32 flags)
130
{
131
	struct ath10k *ar = arvif->ar;
132
133
134
135
136
	struct wmi_vdev_install_key_arg arg = {
		.vdev_id = arvif->vdev_id,
		.key_idx = key->keyidx,
		.key_len = key->keylen,
		.key_data = key->key,
137
		.key_flags = flags,
138
139
140
		.macaddr = macaddr,
	};

Michal Kazior's avatar
Michal Kazior committed
141
142
	lockdep_assert_held(&arvif->ar->conf_mutex);

143
144
145
	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
		arg.key_cipher = WMI_CIPHER_AES_CCM;
146
		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
147
148
149
150
151
152
153
154
155
156
		break;
	case WLAN_CIPHER_SUITE_TKIP:
		arg.key_cipher = WMI_CIPHER_TKIP;
		arg.key_txmic_len = 8;
		arg.key_rxmic_len = 8;
		break;
	case WLAN_CIPHER_SUITE_WEP40:
	case WLAN_CIPHER_SUITE_WEP104:
		arg.key_cipher = WMI_CIPHER_WEP;
		break;
157
	case WLAN_CIPHER_SUITE_AES_CMAC:
158
159
		WARN_ON(1);
		return -EINVAL;
160
	default:
161
		ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
162
163
164
165
166
167
168
169
170
171
172
173
174
175
		return -EOPNOTSUPP;
	}

	if (cmd == DISABLE_KEY) {
		arg.key_cipher = WMI_CIPHER_NONE;
		arg.key_data = NULL;
	}

	return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
}

static int ath10k_install_key(struct ath10k_vif *arvif,
			      struct ieee80211_key_conf *key,
			      enum set_key_cmd cmd,
176
			      const u8 *macaddr, u32 flags)
177
178
179
{
	struct ath10k *ar = arvif->ar;
	int ret;
180
	unsigned long time_left;
181

Michal Kazior's avatar
Michal Kazior committed
182
183
	lockdep_assert_held(&ar->conf_mutex);

184
	reinit_completion(&ar->install_key_done);
185

186
	ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
187
188
189
	if (ret)
		return ret;

190
191
	time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
	if (time_left == 0)
192
193
194
195
196
197
198
199
200
201
202
203
		return -ETIMEDOUT;

	return 0;
}

static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
					const u8 *addr)
{
	struct ath10k *ar = arvif->ar;
	struct ath10k_peer *peer;
	int ret;
	int i;
204
	u32 flags;
205
206
207
208
209
210
211
212
213
214
215
216
217

	lockdep_assert_held(&ar->conf_mutex);

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
	spin_unlock_bh(&ar->data_lock);

	if (!peer)
		return -ENOENT;

	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
		if (arvif->wep_keys[i] == NULL)
			continue;
218
219
220
221

		flags = 0;
		flags |= WMI_KEY_PAIRWISE;

222
223
		/* set TX_USAGE flag for default key id */
		if (arvif->def_wep_key_idx == i)
224
			flags |= WMI_KEY_TX_USAGE;
225
226

		ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
227
					 addr, flags);
228
229
230
		if (ret)
			return ret;

231
		spin_lock_bh(&ar->data_lock);
232
		peer->keys[i] = arvif->wep_keys[i];
233
		spin_unlock_bh(&ar->data_lock);
234
235
236
237
238
239
240
241
242
243
244
245
246
	}

	return 0;
}

static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
				  const u8 *addr)
{
	struct ath10k *ar = arvif->ar;
	struct ath10k_peer *peer;
	int first_errno = 0;
	int ret;
	int i;
247
	u32 flags = 0;
248
249
250
251
252
253
254
255
256
257
258
259
260
261

	lockdep_assert_held(&ar->conf_mutex);

	spin_lock_bh(&ar->data_lock);
	peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
	spin_unlock_bh(&ar->data_lock);

	if (!peer)
		return -ENOENT;

	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
		if (peer->keys[i] == NULL)
			continue;

262
		/* key flags are not required to delete the key */
263
		ret = ath10k_install_key(arvif, peer->keys[i],
264
					 DISABLE_KEY, addr, flags);
265
266
267
268
		if (ret && first_errno == 0)
			first_errno = ret;

		if (ret)
269
			ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
270
271
				    i, ret);

272
		spin_lock_bh(&ar->data_lock);
273
		peer->keys[i] = NULL;
274
		spin_unlock_bh(&ar->data_lock);
275
276
277
278
279
	}

	return first_errno;
}

Sujith Manoharan's avatar
Sujith Manoharan committed
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
bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
				    u8 keyidx)
{
	struct ath10k_peer *peer;
	int i;

	lockdep_assert_held(&ar->data_lock);

	/* We don't know which vdev this peer belongs to,
	 * since WMI doesn't give us that information.
	 *
	 * FIXME: multi-bss needs to be handled.
	 */
	peer = ath10k_peer_find(ar, 0, addr);
	if (!peer)
		return false;

	for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
		if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
			return true;
	}

	return false;
}

305
306
307
308
309
310
311
312
313
static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
				 struct ieee80211_key_conf *key)
{
	struct ath10k *ar = arvif->ar;
	struct ath10k_peer *peer;
	u8 addr[ETH_ALEN];
	int first_errno = 0;
	int ret;
	int i;
314
	u32 flags = 0;
315
316
317
318
319
320
321
322
323
324
325

	lockdep_assert_held(&ar->conf_mutex);

	for (;;) {
		/* since ath10k_install_key we can't hold data_lock all the
		 * time, so we try to remove the keys incrementally */
		spin_lock_bh(&ar->data_lock);
		i = 0;
		list_for_each_entry(peer, &ar->peers, list) {
			for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
				if (peer->keys[i] == key) {
Kalle Valo's avatar
Kalle Valo committed
326
					ether_addr_copy(addr, peer->addr);
327
328
329
330
331
332
333
334
335
336
337
338
					peer->keys[i] = NULL;
					break;
				}
			}

			if (i < ARRAY_SIZE(peer->keys))
				break;
		}
		spin_unlock_bh(&ar->data_lock);

		if (i == ARRAY_SIZE(peer->keys))
			break;
339
		/* key flags are not required to delete the key */
340
		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
341
342
343
344
		if (ret && first_errno == 0)
			first_errno = ret;

		if (ret)
345
			ath10k_warn(ar, "failed to remove key for %pM: %d\n",
346
				    addr, ret);
347
348
349
350
351
	}

	return first_errno;
}

352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	enum nl80211_iftype iftype = arvif->vif->type;
	struct ieee80211_key_conf *key;
	u32 flags = 0;
	int num = 0;
	int i;
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	if (iftype != NL80211_IFTYPE_STATION)
		return 0;

	for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
		if (arvif->wep_keys[i]) {
			key = arvif->wep_keys[i];
			++num;
		}
	}

	if (num != 1)
		return 0;

	flags |= WMI_KEY_PAIRWISE;
	flags |= WMI_KEY_TX_USAGE;

	ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
	if (ret) {
		ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
			    key->keyidx, arvif->vdev_id, ret);
		return ret;
	}

	return 0;
}

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
					 struct ieee80211_key_conf *key)
{
	struct ath10k *ar = arvif->ar;
	struct ath10k_peer *peer;
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	list_for_each_entry(peer, &ar->peers, list) {
		if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
			continue;

		if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
			continue;

		if (peer->keys[key->keyidx] == key)
			continue;

		ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
			   arvif->vdev_id, key->keyidx);

		ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
		if (ret) {
			ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
				    arvif->vdev_id, peer->addr, ret);
			return ret;
		}
	}

	return 0;
}

423
424
425
426
427
428
429
430
431
432
433
434
435
/*********************/
/* General utilities */
/*********************/

static inline enum wmi_phy_mode
chan_to_phymode(const struct cfg80211_chan_def *chandef)
{
	enum wmi_phy_mode phymode = MODE_UNKNOWN;

	switch (chandef->chan->band) {
	case IEEE80211_BAND_2GHZ:
		switch (chandef->width) {
		case NL80211_CHAN_WIDTH_20_NOHT:
436
437
438
439
			if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
				phymode = MODE_11B;
			else
				phymode = MODE_11G;
440
441
442
443
444
445
446
			break;
		case NL80211_CHAN_WIDTH_20:
			phymode = MODE_11NG_HT20;
			break;
		case NL80211_CHAN_WIDTH_40:
			phymode = MODE_11NG_HT40;
			break;
447
448
		case NL80211_CHAN_WIDTH_5:
		case NL80211_CHAN_WIDTH_10:
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
		case NL80211_CHAN_WIDTH_80:
		case NL80211_CHAN_WIDTH_80P80:
		case NL80211_CHAN_WIDTH_160:
			phymode = MODE_UNKNOWN;
			break;
		}
		break;
	case IEEE80211_BAND_5GHZ:
		switch (chandef->width) {
		case NL80211_CHAN_WIDTH_20_NOHT:
			phymode = MODE_11A;
			break;
		case NL80211_CHAN_WIDTH_20:
			phymode = MODE_11NA_HT20;
			break;
		case NL80211_CHAN_WIDTH_40:
			phymode = MODE_11NA_HT40;
			break;
		case NL80211_CHAN_WIDTH_80:
			phymode = MODE_11AC_VHT80;
			break;
470
471
		case NL80211_CHAN_WIDTH_5:
		case NL80211_CHAN_WIDTH_10:
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
		case NL80211_CHAN_WIDTH_80P80:
		case NL80211_CHAN_WIDTH_160:
			phymode = MODE_UNKNOWN;
			break;
		}
		break;
	default:
		break;
	}

	WARN_ON(phymode == MODE_UNKNOWN);
	return phymode;
}

static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
{
/*
 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
 *   0 for no restriction
 *   1 for 1/4 us
 *   2 for 1/2 us
 *   3 for 1 us
 *   4 for 2 us
 *   5 for 4 us
 *   6 for 8 us
 *   7 for 16 us
 */
	switch (mpdudensity) {
	case 0:
		return 0;
	case 1:
	case 2:
	case 3:
	/* Our lower layer calculations limit our precision to
	   1 microsecond */
		return 1;
	case 4:
		return 2;
	case 5:
		return 4;
	case 6:
		return 8;
	case 7:
		return 16;
	default:
		return 0;
	}
}

521
522
static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
			      enum wmi_peer_type peer_type)
523
524
525
526
527
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

528
529
530
	if (ar->num_peers >= ar->max_num_peers)
		return -ENOBUFS;

531
	ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
532
	if (ret) {
533
		ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
534
			    addr, vdev_id, ret);
535
		return ret;
536
	}
537
538

	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
539
	if (ret) {
540
		ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
541
			    addr, vdev_id, ret);
542
		return ret;
543
	}
544

545
	ar->num_peers++;
546
547
548
549

	return 0;
}

550
551
552
553
554
555
556
557
558
559
static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	u32 param;
	int ret;

	param = ar->wmi.pdev_param->sta_kickout_th;
	ret = ath10k_wmi_pdev_set_param(ar, param,
					ATH10K_KICKOUT_THRESHOLD);
	if (ret) {
560
		ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
561
			    arvif->vdev_id, ret);
562
563
564
565
566
567
568
		return ret;
	}

	param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
					ATH10K_KEEPALIVE_MIN_IDLE);
	if (ret) {
569
		ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
570
			    arvif->vdev_id, ret);
571
572
573
574
575
576
577
		return ret;
	}

	param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
					ATH10K_KEEPALIVE_MAX_IDLE);
	if (ret) {
578
		ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
579
			    arvif->vdev_id, ret);
580
581
582
583
584
585
586
		return ret;
	}

	param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
	ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
					ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
	if (ret) {
587
		ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
588
			    arvif->vdev_id, ret);
589
590
591
592
593
594
		return ret;
	}

	return 0;
}

595
static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
596
{
597
598
599
600
601
	struct ath10k *ar = arvif->ar;
	u32 vdev_param;

	vdev_param = ar->wmi.vdev_param->rts_threshold;
	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
602
603
604
605
}

static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
{
606
607
608
	struct ath10k *ar = arvif->ar;
	u32 vdev_param;

609
610
611
612
613
	if (value != 0xFFFFFFFF)
		value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
				ATH10K_FRAGMT_THRESHOLD_MIN,
				ATH10K_FRAGMT_THRESHOLD_MAX);

614
615
	vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
616
617
}

618
619
620
621
622
623
624
625
626
627
628
629
630
631
static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
	if (ret)
		return ret;

	ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
	if (ret)
		return ret;

632
633
	ar->num_peers--;

634
635
636
637
638
639
640
641
642
643
644
645
646
647
	return 0;
}

static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
{
	struct ath10k_peer *peer, *tmp;

	lockdep_assert_held(&ar->conf_mutex);

	spin_lock_bh(&ar->data_lock);
	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
		if (peer->vdev_id != vdev_id)
			continue;

648
		ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
649
650
651
652
			    peer->addr, vdev_id);

		list_del(&peer->list);
		kfree(peer);
653
		ar->num_peers--;
654
655
656
657
	}
	spin_unlock_bh(&ar->data_lock);
}

658
659
660
661
662
663
664
665
666
667
668
669
static void ath10k_peer_cleanup_all(struct ath10k *ar)
{
	struct ath10k_peer *peer, *tmp;

	lockdep_assert_held(&ar->conf_mutex);

	spin_lock_bh(&ar->data_lock);
	list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
		list_del(&peer->list);
		kfree(peer);
	}
	spin_unlock_bh(&ar->data_lock);
670
671

	ar->num_peers = 0;
672
	ar->num_stations = 0;
673
674
}

675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
				       struct ieee80211_sta *sta,
				       enum wmi_tdls_peer_state state)
{
	int ret;
	struct wmi_tdls_peer_update_cmd_arg arg = {};
	struct wmi_tdls_peer_capab_arg cap = {};
	struct wmi_channel_arg chan_arg = {};

	lockdep_assert_held(&ar->conf_mutex);

	arg.vdev_id = vdev_id;
	arg.peer_state = state;
	ether_addr_copy(arg.addr, sta->addr);

	cap.peer_max_sp = sta->max_sp;
	cap.peer_uapsd_queues = sta->uapsd_queues;

	if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
	    !sta->tdls_initiator)
		cap.is_peer_responder = 1;

	ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
	if (ret) {
		ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
			    arg.addr, vdev_id, ret);
		return ret;
	}

	return 0;
}

707
708
709
710
/************************/
/* Interface management */
/************************/

711
712
713
714
715
716
717
718
719
720
721
722
723
void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;

	lockdep_assert_held(&ar->data_lock);

	if (!arvif->beacon)
		return;

	if (!arvif->beacon_buf)
		dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
				 arvif->beacon->len, DMA_TO_DEVICE);

Michal Kazior's avatar
Michal Kazior committed
724
725
726
727
	if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
		    arvif->beacon_state != ATH10K_BEACON_SENT))
		return;

728
729
730
	dev_kfree_skb_any(arvif->beacon);

	arvif->beacon = NULL;
Michal Kazior's avatar
Michal Kazior committed
731
	arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
}

static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;

	lockdep_assert_held(&ar->data_lock);

	ath10k_mac_vif_beacon_free(arvif);

	if (arvif->beacon_buf) {
		dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
				  arvif->beacon_buf, arvif->beacon_paddr);
		arvif->beacon_buf = NULL;
	}
}

749
750
static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
{
751
	unsigned long time_left;
752

Michal Kazior's avatar
Michal Kazior committed
753
754
	lockdep_assert_held(&ar->conf_mutex);

Michal Kazior's avatar
Michal Kazior committed
755
756
757
	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
		return -ESHUTDOWN;

758
759
760
	time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
						ATH10K_VDEV_SETUP_TIMEOUT_HZ);
	if (time_left == 0)
761
762
763
764
765
		return -ETIMEDOUT;

	return 0;
}

Michal Kazior's avatar
Michal Kazior committed
766
static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
767
{
768
769
	struct cfg80211_chan_def *chandef = &ar->chandef;
	struct ieee80211_channel *channel = chandef->chan;
770
771
772
773
774
775
776
	struct wmi_vdev_start_request_arg arg = {};
	int ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

	arg.vdev_id = vdev_id;
	arg.channel.freq = channel->center_freq;
777
	arg.channel.band_center_freq1 = chandef->center_freq1;
778
779
780

	/* TODO setup this dynamically, what in case we
	   don't have any vifs? */
781
	arg.channel.mode = chan_to_phymode(chandef);
782
783
	arg.channel.chan_radar =
			!!(channel->flags & IEEE80211_CHAN_RADAR);
784

785
	arg.channel.min_power = 0;
786
787
788
	arg.channel.max_power = channel->max_power * 2;
	arg.channel.max_reg_power = channel->max_reg_power * 2;
	arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
789

Michal Kazior's avatar
Michal Kazior committed
790
791
	reinit_completion(&ar->vdev_setup_done);

792
793
	ret = ath10k_wmi_vdev_start(ar, &arg);
	if (ret) {
794
		ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
795
			    vdev_id, ret);
796
797
798
799
800
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
801
		ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
802
			    vdev_id, ret);
803
804
805
806
807
		return ret;
	}

	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
	if (ret) {
808
		ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
809
			    vdev_id, ret);
810
811
812
813
814
		goto vdev_stop;
	}

	ar->monitor_vdev_id = vdev_id;

815
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior's avatar
Michal Kazior committed
816
		   ar->monitor_vdev_id);
817
818
819
820
821
	return 0;

vdev_stop:
	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
	if (ret)
822
		ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
823
			    ar->monitor_vdev_id, ret);
824
825
826
827

	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
828
static int ath10k_monitor_vdev_stop(struct ath10k *ar)
829
830
831
832
833
{
	int ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

834
835
	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
	if (ret)
836
		ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
837
			    ar->monitor_vdev_id, ret);
838

Michal Kazior's avatar
Michal Kazior committed
839
840
	reinit_completion(&ar->vdev_setup_done);

841
842
	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
	if (ret)
843
		ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
844
			    ar->monitor_vdev_id, ret);
845
846
847

	ret = ath10k_vdev_setup_sync(ar);
	if (ret)
848
		ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
849
			    ar->monitor_vdev_id, ret);
850

851
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior's avatar
Michal Kazior committed
852
		   ar->monitor_vdev_id);
853
854
855
	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
856
static int ath10k_monitor_vdev_create(struct ath10k *ar)
857
858
859
860
861
{
	int bit, ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

Ben Greear's avatar
Ben Greear committed
862
	if (ar->free_vdev_map == 0) {
863
		ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
864
865
866
		return -ENOMEM;
	}

Ben Greear's avatar
Ben Greear committed
867
	bit = __ffs64(ar->free_vdev_map);
Ben Greear's avatar
Ben Greear committed
868

Ben Greear's avatar
Ben Greear committed
869
	ar->monitor_vdev_id = bit;
870
871
872
873
874

	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
				     WMI_VDEV_TYPE_MONITOR,
				     0, ar->mac_addr);
	if (ret) {
875
		ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
876
			    ar->monitor_vdev_id, ret);
Ben Greear's avatar
Ben Greear committed
877
		return ret;
878
879
	}

Ben Greear's avatar
Ben Greear committed
880
	ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
881
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
882
883
884
885
886
		   ar->monitor_vdev_id);

	return 0;
}

Michal Kazior's avatar
Michal Kazior committed
887
static int ath10k_monitor_vdev_delete(struct ath10k *ar)
888
889
890
891
892
893
894
{
	int ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
	if (ret) {
895
		ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
896
			    ar->monitor_vdev_id, ret);
897
898
899
		return ret;
	}

Ben Greear's avatar
Ben Greear committed
900
	ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
901

902
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
903
904
905
906
		   ar->monitor_vdev_id);
	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
907
908
909
910
911
912
913
914
static int ath10k_monitor_start(struct ath10k *ar)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_monitor_vdev_create(ar);
	if (ret) {
915
		ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior's avatar
Michal Kazior committed
916
917
918
919
920
		return ret;
	}

	ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
	if (ret) {
921
		ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior's avatar
Michal Kazior committed
922
923
924
925
926
		ath10k_monitor_vdev_delete(ar);
		return ret;
	}

	ar->monitor_started = true;
927
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior's avatar
Michal Kazior committed
928
929
930
931

	return 0;
}

932
static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior's avatar
Michal Kazior committed
933
934
935
936
937
938
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_monitor_vdev_stop(ar);
939
	if (ret) {
940
		ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
941
942
		return ret;
	}
Michal Kazior's avatar
Michal Kazior committed
943
944

	ret = ath10k_monitor_vdev_delete(ar);
945
	if (ret) {
946
		ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
947
948
		return ret;
	}
Michal Kazior's avatar
Michal Kazior committed
949
950

	ar->monitor_started = false;
951
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
952
953
954
955

	return 0;
}

956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
{
	struct ath10k_vif *arvif;

	if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
		return true;

	if (!ar->num_started_vdevs)
		return false;

	list_for_each_entry(arvif, &ar->arvifs, list)
		if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
			return false;

	ath10k_dbg(ar, ATH10K_DBG_MAC,
		   "mac disabling promiscuous mode because vdev is started\n");
	return true;
}

975
976
977
978
979
980
981
static int ath10k_monitor_recalc(struct ath10k *ar)
{
	bool should_start;

	lockdep_assert_held(&ar->conf_mutex);

	should_start = ar->monitor ||
Michal Kazior's avatar
Michal Kazior committed
982
		       !ath10k_mac_should_disable_promisc(ar) ||
983
984
985
986
987
988
989
990
991
992
993
		       test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);

	ath10k_dbg(ar, ATH10K_DBG_MAC,
		   "mac monitor recalc started? %d should? %d\n",
		   ar->monitor_started, should_start);

	if (should_start == ar->monitor_started)
		return 0;

	if (should_start)
		return ath10k_monitor_start(ar);
994
995

	return ath10k_monitor_stop(ar);
Michal Kazior's avatar
Michal Kazior committed
996
997
}

998
999
1000
1001
1002
1003
1004
1005
1006
static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	u32 vdev_param, rts_cts = 0;

	lockdep_assert_held(&ar->conf_mutex);

	vdev_param = ar->wmi.vdev_param->enable_rtscts;

1007
	rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1008
1009
1010
1011

	if (arvif->num_legacy_stations > 0)
		rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
			      WMI_RTSCTS_PROFILE);
1012
1013
1014
	else
		rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
			      WMI_RTSCTS_PROFILE);
1015
1016
1017
1018
1019

	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
					 rts_cts);
}

1020
1021
1022
1023
1024
1025
1026
1027
static int ath10k_start_cac(struct ath10k *ar)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);

1028
	ret = ath10k_monitor_recalc(ar);
1029
	if (ret) {
1030
		ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1031
1032
1033
1034
		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
		return ret;
	}

1035
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
		   ar->monitor_vdev_id);

	return 0;
}

static int ath10k_stop_cac(struct ath10k *ar)
{
	lockdep_assert_held(&ar->conf_mutex);

	/* CAC is not running - do nothing */
	if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
		return 0;

	clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
Michal Kazior's avatar
Michal Kazior committed
1050
	ath10k_monitor_stop(ar);
1051

1052
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1053
1054
1055
1056

	return 0;
}

1057
static void ath10k_recalc_radar_detection(struct ath10k *ar)
1058
1059
1060
1061
1062
1063
1064
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ath10k_stop_cac(ar);

1065
	if (!ar->radar_enabled)
1066
1067
		return;

1068
	if (ar->num_started_vdevs > 0)
1069
1070
1071
1072
1073
1074
1075
1076
1077
		return;

	ret = ath10k_start_cac(ar);
	if (ret) {
		/*
		 * Not possible to start CAC on current channel so starting
		 * radiation is not allowed, make this channel DFS_UNAVAILABLE
		 * by indicating that radar was detected.
		 */
1078
		ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1079
1080
1081
1082
		ieee80211_radar_detected(ar->hw);
	}
}

1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
static int ath10k_vdev_stop(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	reinit_completion(&ar->vdev_setup_done);

	ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
	if (ret) {
		ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
			    arvif->vdev_id, ret);
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
		ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
			    arvif->vdev_id, ret);
		return ret;
	}

	WARN_ON(ar->num_started_vdevs == 0);

	if (ar->num_started_vdevs != 0) {
		ar->num_started_vdevs--;
		ath10k_recalc_radar_detection(ar);
	}

	return ret;
}

1116
static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior's avatar
Michal Kazior committed
1117
1118
1119
1120
{
	struct ath10k *ar = arvif->ar;
	struct cfg80211_chan_def *chandef = &ar->chandef;
	struct wmi_vdev_start_request_arg arg = {};
1121
	int ret = 0, ret2;
Michal Kazior's avatar
Michal Kazior committed
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152

	lockdep_assert_held(&ar->conf_mutex);

	reinit_completion(&ar->vdev_setup_done);

	arg.vdev_id = arvif->vdev_id;
	arg.dtim_period = arvif->dtim_period;
	arg.bcn_intval = arvif->beacon_interval;

	arg.channel.freq = chandef->chan->center_freq;
	arg.channel.band_center_freq1 = chandef->center_freq1;
	arg.channel.mode = chan_to_phymode(chandef);

	arg.channel.min_power = 0;
	arg.channel.max_power = chandef->chan->max_power * 2;
	arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
	arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;

	if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
		arg.ssid = arvif->u.ap.ssid;
		arg.ssid_len = arvif->u.ap.ssid_len;
		arg.hidden_ssid = arvif->u.ap.hidden_ssid;

		/* For now allow DFS for AP mode */
		arg.channel.chan_radar =
			!!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
	} else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
		arg.ssid = arvif->vif->bss_conf.ssid;
		arg.ssid_len = arvif->vif->bss_conf.ssid_len;
	}

1153
	ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior's avatar
Michal Kazior committed
1154
1155
1156
1157
		   "mac vdev %d start center_freq %d phymode %s\n",
		   arg.vdev_id, arg.channel.freq,
		   ath10k_wmi_phymode_str(arg.channel.mode));

1158
1159
1160
1161
1162
	if (restart)
		ret = ath10k_wmi_vdev_restart(ar, &arg);
	else
		ret = ath10k_wmi_vdev_start(ar, &arg);

Michal Kazior's avatar
Michal Kazior committed
1163
	if (ret) {
1164
		ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior's avatar
Michal Kazior committed
1165
1166
1167
1168
1169
1170
			    arg.vdev_id, ret);
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
1171
1172
1173
		ath10k_warn(ar,
			    "failed to synchronize setup for vdev %i restart %d: %d\n",
			    arg.vdev_id, restart, ret);
Michal Kazior's avatar
Michal Kazior committed
1174
1175
1176
		return ret;
	}

1177
1178
1179
	ar->num_started_vdevs++;
	ath10k_recalc_radar_detection(ar);

1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
	ret = ath10k_monitor_recalc(ar);
	if (ret) {
		ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
			    arg.vdev_id, restart, ret);
		ret2 = ath10k_vdev_stop(arvif);
		if (ret2)
			ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
				    arg.vdev_id, restart, ret2);
	}

Michal Kazior's avatar
Michal Kazior committed
1190
1191
1192
	return ret;
}

1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
static int ath10k_vdev_start(struct ath10k_vif *arvif)
{
	return ath10k_vdev_start_restart(arvif, false);
}

static int ath10k_vdev_restart(struct ath10k_vif *arvif)
{
	return ath10k_vdev_start_restart(arvif, true);
}

1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
				       struct sk_buff *bcn)
{
	struct ath10k *ar = arvif->ar;
	struct ieee80211_mgmt *mgmt;
	const u8 *p2p_ie;
	int ret;

	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
		return 0;

	if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
		return 0;

	mgmt = (void *)bcn->data;
	p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
					 mgmt->u.beacon.variable,
					 bcn->len - (mgmt->u.beacon.variable -
						     bcn->data));
	if (!p2p_ie)
		return -ENOENT;

	ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
	if (ret) {
		ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
			    arvif->vdev_id, ret);
		return ret;
	}

	return 0;
}

static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
				       u8 oui_type, size_t ie_offset)
{
	size_t len;
	const u8 *next;
	const u8 *end;
	u8 *ie;

	if (WARN_ON(skb->len < ie_offset))
		return -EINVAL;

	ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
					   skb->data + ie_offset,
					   skb->len - ie_offset);
	if (!ie)
		return -ENOENT;

	len = ie[1] + 2;
	end = skb->data + skb->len;
	next = ie + len;

	if (WARN_ON(next > end))
		return -EINVAL;

	memmove(ie, next, end - next);
	skb_trim(skb, skb->len - len);

	return 0;
}

static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	struct ieee80211_hw *hw = ar->hw;
	struct ieee80211_vif *vif = arvif->vif;
	struct ieee80211_mutable_offsets offs = {};
	struct sk_buff *bcn;
	int ret;

	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
		return 0;

1277
1278
1279
1280
	if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
	    arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
		return 0;

1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
	bcn = ieee80211_beacon_get_template(hw, vif, &offs);
	if (!bcn) {
		ath10k_warn(ar, "failed to get beacon template from mac80211\n");
		return -EPERM;
	}

	ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
	if (ret) {
		ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
		kfree_skb(bcn);
		return ret;
	}

	/* P2P IE is inserted by firmware automatically (as configured above)
	 * so remove it from the base beacon template to avoid duplicate P2P
	 * IEs in beacon frames.
	 */
	ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
				    offsetof(struct ieee80211_mgmt,
					     u.beacon.variable));

	ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
				  0, NULL, 0);
	kfree_skb(bcn);

	if (ret) {
		ath10k_warn(ar, "failed to submit beacon template command: %d\n",
			    ret);
		return ret;
	}

	return 0;
}

static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	struct ieee80211_hw *hw = ar->hw;
	struct ieee80211_vif *vif = arvif->vif;
	struct sk_buff *prb;
	int ret;

	if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
		return 0;

1326
1327
1328
	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
		return 0;

1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
	prb = ieee80211_proberesp_get(hw, vif);
	if (!prb) {
		ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
		return -EPERM;
	}

	ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
	kfree_skb(prb);

	if (ret) {
		ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
			    ret);
		return ret;
	}

	return 0;
}

1347
static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1348
				     struct ieee80211_bss_conf *info)
1349
{
1350
	struct ath10k *ar = arvif->ar;
1351
1352
	int ret = 0;

Michal Kazior's avatar
Michal Kazior committed
1353
1354
	lockdep_assert_held(&arvif->ar->conf_mutex);

1355
1356
	if (!info->enable_beacon) {
		ath10k_vdev_stop(arvif);
1357
1358
1359

		arvif->is_started = false;
		arvif->is_up = false;
1360
1361

		spin_lock_bh(&arvif->ar->data_lock);
1362
		ath10k_mac_vif_beacon_free(arvif);
1363
1364
		spin_unlock_bh(&arvif->ar->data_lock);

1365
1366
1367
1368
1369
1370
1371
1372
1373
		return;
	}

	arvif->tx_seq_no = 0x1000;

	ret = ath10k_vdev_start(arvif);
	if (ret)
		return;

1374
	arvif->aid = 0;
Kalle Valo's avatar
Kalle Valo committed
1375
	ether_addr_copy(arvif->bssid, info->bssid);
1376
1377
1378

	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
				 arvif->bssid);
1379
	if (ret) {
1380
		ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1381
			    arvif->vdev_id, ret);
1382
		ath10k_vdev_stop(arvif);
1383
1384
		return;
	}
1385
1386
1387
1388

	arvif->is_started = true;
	arvif->is_up = true;

1389
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1390
1391
1392
1393
1394
1395
}

static void ath10k_control_ibss(struct ath10k_vif *arvif,
				struct ieee80211_bss_conf *info,
				const u8 self_peer[ETH_ALEN])
{
1396
	struct ath10k *ar = arvif->ar;
1397
	u32 vdev_param;
1398
1399
	int ret = 0;

Michal Kazior's avatar
Michal Kazior committed
1400
1401
	lockdep_assert_held(&arvif->ar->conf_mutex);

1402
1403
1404
	if (!info->ibss_joined) {
		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
		if (ret)
1405
			ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1406
1407
				    self_peer, arvif->vdev_id, ret);

1408
		if (is_zero_ether_addr(arvif->bssid))
1409
1410
			return;

1411
		memset(arvif->bssid, 0, ETH_ALEN);
1412
1413
1414
1415

		return;
	}

1416
1417
	ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer,
				 WMI_PEER_TYPE_DEFAULT);
1418
	if (ret) {
1419
		ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1420
1421
1422
1423
			    self_peer, arvif->vdev_id, ret);
		return;
	}

1424
1425
	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1426
1427
					ATH10K_DEFAULT_ATIM);
	if (ret)
1428
		ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1429
1430
1431
			    arvif->vdev_id, ret);
}

Michal Kazior's avatar
Michal Kazior committed
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	u32 param;
	u32 value;
	int ret;

	lockdep_assert_held(&arvif->ar->conf_mutex);

	if (arvif->u.sta.uapsd)
		value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
	else
		value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;

	param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
	if (ret) {
		ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
			    value, arvif->vdev_id, ret);
		return ret;
	}

	return 0;
}

static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	u32 param;
	u32 value;
	int ret;

	lockdep_assert_held(&arvif->ar->conf_mutex);

	if (arvif->u.sta.uapsd)
		value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
	else
		value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;

	param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
	ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
					  param, value);
	if (ret) {
		ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
			    value, arvif->vdev_id, ret);
		return ret;
	}

	return 0;
}

1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
static int ath10k_mac_ps_vif_count(struct ath10k *ar)
{
	struct ath10k_vif *arvif;
	int num = 0;

	lockdep_assert_held(&ar->conf_mutex);

	list_for_each_entry(arvif, &ar->arvifs, list)
		if (arvif->ps)
			num++;

	return num;
}

1497
static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1498
{
1499
	struct ath10k *ar = arvif->ar;
1500
	struct ieee80211_vif *vif = arvif->vif;
1501
	struct ieee80211_conf *conf = &ar->hw->conf;
1502
1503
1504
	enum wmi_sta_powersave_param param;
	enum wmi_sta_ps_mode psmode;
	int ret;
1505
	int ps_timeout;
1506
	bool enable_ps;
1507

Michal Kazior's avatar
Michal Kazior committed
1508
1509
	lockdep_assert_held(&arvif->ar->conf_mutex);

1510
1511
	if (arvif->vif->type != NL80211_IFTYPE_STATION)
		return 0;
1512

1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
	enable_ps = arvif->ps;

	if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
	    !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
		      ar->fw_features)) {
		ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
			    arvif->vdev_id);
		enable_ps = false;
	}

	if (enable_ps) {
1524
1525
1526
		psmode = WMI_STA_PS_MODE_ENABLED;
		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;

1527
1528
1529
1530
1531
1532
1533
		ps_timeout = conf->dynamic_ps_timeout;
		if (ps_timeout == 0) {
			/* Firmware doesn't like 0 */
			ps_timeout = ieee80211_tu_to_usec(
				vif->bss_conf.beacon_int) / 1000;
		}

1534
		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1535
						  ps_timeout);
1536
		if (ret) {
1537
			ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1538
				    arvif->vdev_id, ret);
1539
			return ret;
1540
1541
1542
1543
1544
		}
	} else {
		psmode = WMI_STA_PS_MODE_DISABLED;
	}

1545
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1546
1547
		   arvif->vdev_id, psmode ? "enable" : "disable");

1548
1549
	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
	if (ret) {
1550
		ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1551
			    psmode, arvif->vdev_id, ret);
1552
1553
1554
1555
		return ret;
	}

	return 0;
1556
1557
}

Michal Kazior's avatar
Michal Kazior committed
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	struct wmi_sta_keepalive_arg arg = {};
	int ret;

	lockdep_assert_held(&arvif->ar->conf_mutex);

	if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
		return 0;

	if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
		return 0;

	/* Some firmware revisions have a bug and ignore the `enabled` field.
	 * Instead use the interval to disable the keepalive.
	 */
	arg.vdev_id = arvif->vdev_id;
	arg.enabled = 1;
	arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
	arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;

	ret = ath10k_wmi_sta_keepalive(ar, &arg);
	if (ret) {
		ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
			    arvif->vdev_id, ret);
		return ret;
	}

	return 0;
}

1590
1591
1592
1593
1594
1595
static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
{
	struct ath10k *ar = arvif->ar;
	struct ieee80211_vif *vif = arvif->vif;
	int ret;

1596
1597
1598
1599
1600
	lockdep_assert_held(&arvif->ar->conf_mutex);

	if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
		return;

1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
	if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
		return;

	if (!vif->csa_active)
		return;

	if (!arvif->is_up)
		return;

	if (!ieee80211_csa_is_complete(vif)) {
		ieee80211_csa_update_counter(vif);

		ret = ath10k_mac_setup_bcn_tmpl(arvif);
		if (ret)
			ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
				    ret);

		ret = ath10k_mac_setup_prb_tmpl(arvif);
		if (ret)
			ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
				    ret);
	} else {
		ieee80211_csa_finish(vif);
	}
}

static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
{
	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
						ap_csa_work);
	struct ath10k *ar = arvif->ar;

	mutex_lock(&ar->conf_mutex);
	ath10k_mac_vif_ap_csa_count_down(arvif);
	mutex_unlock(&ar->conf_mutex);
}

1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
					  struct ieee80211_vif *vif)
{
	struct sk_buff *skb = data;
	struct ieee80211_mgmt *mgmt = (void *)skb->data;
	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);

	if (vif->type != NL80211_IFTYPE_STATION)
		return;

	if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
		return;

	cancel_delayed_work(&arvif->connection_loss_work);
}

void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
{
	ieee80211_iterate_active_interfaces_atomic(ar->hw,
						   IEEE80211_IFACE_ITER_NORMAL,
						   ath10k_mac_handle_beacon_iter,
						   skb);
}

static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
					       struct ieee80211_vif *vif)
{
	u32 *vdev_id = data;
	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
	struct ath10k *ar = arvif->ar;
	struct ieee80211_hw *hw = ar->hw;

	if (arvif->vdev_id != *vdev_id)
		return;

	if (!arvif->is_up)
		return;

	ieee80211_beacon_loss(vif);

	/* Firmware doesn't report beacon loss events repeatedly. If AP probe
	 * (done by mac80211) succeeds but beacons do not resume then it
	 * doesn't make sense to continue operation. Queue connection loss work
	 * which can be cancelled when beacon is received.
	 */
	ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
				     ATH10K_CONNECTION_LOSS_HZ);
}

void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
{
	ieee80211_iterate_active_interfaces_atomic(ar->hw,
						   IEEE80211_IFACE_ITER_NORMAL,
						   ath10k_mac_handle_beacon_miss_iter,
						   &vdev_id);
}

static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
{
	struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
						connection_loss_work.work);
	struct ieee80211_vif *vif = arvif->vif;

	if (!arvif->is_up)
		return;

	ieee80211_connection_loss(vif);
}

1707
1708
1709
1710
/**********************/
/* Station management */
/**********************/

Michal Kazior's avatar
Michal Kazior committed
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
					     struct ieee80211_vif *vif)
{
	/* Some firmware revisions have unstable STA powersave when listen
	 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
	 * generate NullFunc frames properly even if buffered frames have been
	 * indicated in Beacon TIM. Firmware would seldom wake up to pull
	 * buffered frames. Often pinging the device from AP would simply fail.
	 *
	 * As a workaround set it to 1.
	 */
	if (vif->type == NL80211_IFTYPE_STATION)
		return 1;

	return ar->hw->conf.listen_interval;
}

1728
static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior's avatar
Michal Kazior committed
1729
				      struct ieee80211_vif *vif,
1730
1731
1732
				      struct ieee80211_sta *sta,
				      struct wmi_peer_assoc_complete_arg *arg)
{
Michal Kazior's avatar
Michal Kazior committed
1733
	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1734
	u32 aid;
Michal Kazior's avatar
Michal Kazior committed
1735

Michal Kazior's avatar
Michal Kazior committed
1736
1737
	lockdep_assert_held(&ar->conf_mutex);

1738
1739
1740
1741
1742
	if (vif->type == NL80211_IFTYPE_STATION)
		aid = vif->bss_conf.aid;
	else
		aid = sta->aid;

Kalle Valo's avatar
Kalle Valo committed
1743
	ether_addr_copy(arg->addr, sta->addr);
1744
	arg->vdev_id = arvif->vdev_id;
1745
	arg->peer_aid = aid;
1746
	arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior's avatar
Michal Kazior committed
1747
	arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1748
	arg->peer_num_spatial_streams = 1;
Michal Kazior's avatar
Michal Kazior committed
1749
	arg->peer_caps = vif->bss_conf.assoc_capability;
1750
1751
1752
}

static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior's avatar
Michal Kazior committed
1753
				       struct ieee80211_vif *vif,
1754
1755
1756
1757
1758
1759
1760
				       struct wmi_peer_assoc_complete_arg *arg)
{
	struct ieee80211_bss_conf *info = &vif->bss_conf;
	struct cfg80211_bss *bss;
	const u8 *rsnie = NULL;
	const u8 *wpaie = NULL;