mac.c 138 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
33
34
35
36
37
38
39

/**********/
/* Crypto */
/**********/

static int ath10k_send_key(struct ath10k_vif *arvif,
			   struct ieee80211_key_conf *key,
			   enum set_key_cmd cmd,
40
			   const u8 *macaddr, bool def_idx)
41
{
42
	struct ath10k *ar = arvif->ar;
43
44
45
46
47
48
49
50
	struct wmi_vdev_install_key_arg arg = {
		.vdev_id = arvif->vdev_id,
		.key_idx = key->keyidx,
		.key_len = key->keylen,
		.key_data = key->key,
		.macaddr = macaddr,
	};

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

53
54
55
56
57
58
59
60
	if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
		arg.key_flags = WMI_KEY_PAIRWISE;
	else
		arg.key_flags = WMI_KEY_GROUP;

	switch (key->cipher) {
	case WLAN_CIPHER_SUITE_CCMP:
		arg.key_cipher = WMI_CIPHER_AES_CCM;
61
		key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
62
63
64
65
66
67
68
69
70
71
72
73
74
		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;
		/* AP/IBSS mode requires self-key to be groupwise
		 * Otherwise pairwise key must be set */
		if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
			arg.key_flags = WMI_KEY_PAIRWISE;
75
76
77

		if (def_idx)
			arg.key_flags |= WMI_KEY_TX_USAGE;
78
		break;
79
80
81
	case WLAN_CIPHER_SUITE_AES_CMAC:
		/* this one needs to be done in software */
		return 1;
82
	default:
83
		ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		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,
98
			      const u8 *macaddr, bool def_idx)
99
100
101
102
{
	struct ath10k *ar = arvif->ar;
	int ret;

Michal Kazior's avatar
Michal Kazior committed
103
104
	lockdep_assert_held(&ar->conf_mutex);

105
	reinit_completion(&ar->install_key_done);
106

107
	ret = ath10k_send_key(arvif, key, cmd, macaddr, def_idx);
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
	if (ret)
		return ret;

	ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
	if (ret == 0)
		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;
125
	bool def_idx;
126
127
128
129
130
131
132
133
134
135
136
137
138

	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;
139
140
141
142
143
		/* set TX_USAGE flag for default key id */
		if (arvif->def_wep_key_idx == i)
			def_idx = true;
		else
			def_idx = false;
144
145

		ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
146
					 addr, def_idx);
147
148
149
		if (ret)
			return ret;

150
		spin_lock_bh(&ar->data_lock);
151
		peer->keys[i] = arvif->wep_keys[i];
152
		spin_unlock_bh(&ar->data_lock);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
	}

	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;

	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;

180
		/* key flags are not required to delete the key */
181
		ret = ath10k_install_key(arvif, peer->keys[i],
182
					 DISABLE_KEY, addr, false);
183
184
185
186
		if (ret && first_errno == 0)
			first_errno = ret;

		if (ret)
187
			ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
188
189
				    i, ret);

190
		spin_lock_bh(&ar->data_lock);
191
		peer->keys[i] = NULL;
192
		spin_unlock_bh(&ar->data_lock);
193
194
195
196
197
	}

	return first_errno;
}

Sujith Manoharan's avatar
Sujith Manoharan committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
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;
}

223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
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;

	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
243
					ether_addr_copy(addr, peer->addr);
244
245
246
247
248
249
250
251
252
253
254
255
					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;
256
257
		/* key flags are not required to delete the key */
		ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, false);
258
259
260
261
		if (ret && first_errno == 0)
			first_errno = ret;

		if (ret)
262
			ath10k_warn(ar, "failed to remove key for %pM: %d\n",
263
				    addr, ret);
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
	}

	return first_errno;
}

/*********************/
/* 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:
282
283
284
285
			if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
				phymode = MODE_11B;
			else
				phymode = MODE_11G;
286
287
288
289
290
291
292
			break;
		case NL80211_CHAN_WIDTH_20:
			phymode = MODE_11NG_HT20;
			break;
		case NL80211_CHAN_WIDTH_40:
			phymode = MODE_11NG_HT40;
			break;
293
294
		case NL80211_CHAN_WIDTH_5:
		case NL80211_CHAN_WIDTH_10:
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
		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;
316
317
		case NL80211_CHAN_WIDTH_5:
		case NL80211_CHAN_WIDTH_10:
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
		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;
	}
}

static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

373
374
375
	if (ar->num_peers >= ar->max_num_peers)
		return -ENOBUFS;

376
	ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
377
	if (ret) {
378
		ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
379
			    addr, vdev_id, ret);
380
		return ret;
381
	}
382
383

	ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
384
	if (ret) {
385
		ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
386
			    addr, vdev_id, ret);
387
		return ret;
388
	}
389

390
	ar->num_peers++;
391
392
393
394

	return 0;
}

395
396
397
398
399
400
401
402
403
404
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) {
405
		ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
406
			    arvif->vdev_id, ret);
407
408
409
410
411
412
413
		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) {
414
		ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
415
			    arvif->vdev_id, ret);
416
417
418
419
420
421
422
		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) {
423
		ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
424
			    arvif->vdev_id, ret);
425
426
427
428
429
430
431
		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) {
432
		ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
433
			    arvif->vdev_id, ret);
434
435
436
437
438
439
		return ret;
	}

	return 0;
}

440
static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
441
{
442
443
444
445
446
	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);
447
448
449
450
}

static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
{
451
452
453
	struct ath10k *ar = arvif->ar;
	u32 vdev_param;

454
455
456
457
458
	if (value != 0xFFFFFFFF)
		value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
				ATH10K_FRAGMT_THRESHOLD_MIN,
				ATH10K_FRAGMT_THRESHOLD_MAX);

459
460
	vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
	return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
461
462
}

463
464
465
466
467
468
469
470
471
472
473
474
475
476
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;

477
478
	ar->num_peers--;

479
480
481
482
483
484
485
486
487
488
489
490
491
492
	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;

493
		ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
494
495
496
497
			    peer->addr, vdev_id);

		list_del(&peer->list);
		kfree(peer);
498
		ar->num_peers--;
499
500
501
502
	}
	spin_unlock_bh(&ar->data_lock);
}

503
504
505
506
507
508
509
510
511
512
513
514
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);
515
516

	ar->num_peers = 0;
517
	ar->num_stations = 0;
518
519
}

520
521
522
523
/************************/
/* Interface management */
/************************/

524
525
526
527
528
529
530
531
532
533
534
535
536
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
537
538
539
540
	if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
		    arvif->beacon_state != ATH10K_BEACON_SENT))
		return;

541
542
543
	dev_kfree_skb_any(arvif->beacon);

	arvif->beacon = NULL;
Michal Kazior's avatar
Michal Kazior committed
544
	arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
}

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;
	}
}

562
563
564
565
static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
{
	int ret;

Michal Kazior's avatar
Michal Kazior committed
566
567
	lockdep_assert_held(&ar->conf_mutex);

Michal Kazior's avatar
Michal Kazior committed
568
569
570
	if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
		return -ESHUTDOWN;

571
572
573
574
575
576
577
578
	ret = wait_for_completion_timeout(&ar->vdev_setup_done,
					  ATH10K_VDEV_SETUP_TIMEOUT_HZ);
	if (ret == 0)
		return -ETIMEDOUT;

	return 0;
}

Michal Kazior's avatar
Michal Kazior committed
579
static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
580
{
581
582
	struct cfg80211_chan_def *chandef = &ar->chandef;
	struct ieee80211_channel *channel = chandef->chan;
583
584
585
586
587
588
589
	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;
590
	arg.channel.band_center_freq1 = chandef->center_freq1;
591
592
593

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

598
	arg.channel.min_power = 0;
599
600
601
	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;
602

Michal Kazior's avatar
Michal Kazior committed
603
604
	reinit_completion(&ar->vdev_setup_done);

605
606
	ret = ath10k_wmi_vdev_start(ar, &arg);
	if (ret) {
607
		ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
608
			    vdev_id, ret);
609
610
611
612
613
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
614
		ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
615
			    vdev_id, ret);
616
617
618
619
620
		return ret;
	}

	ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
	if (ret) {
621
		ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
622
			    vdev_id, ret);
623
624
625
626
627
		goto vdev_stop;
	}

	ar->monitor_vdev_id = vdev_id;

628
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
Michal Kazior's avatar
Michal Kazior committed
629
		   ar->monitor_vdev_id);
630
631
632
633
634
	return 0;

vdev_stop:
	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
	if (ret)
635
		ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
636
			    ar->monitor_vdev_id, ret);
637
638
639
640

	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
641
static int ath10k_monitor_vdev_stop(struct ath10k *ar)
642
643
644
645
646
{
	int ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

647
648
	ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
	if (ret)
649
		ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
650
			    ar->monitor_vdev_id, ret);
651

Michal Kazior's avatar
Michal Kazior committed
652
653
	reinit_completion(&ar->vdev_setup_done);

654
655
	ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
	if (ret)
656
		ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
657
			    ar->monitor_vdev_id, ret);
658
659
660

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

664
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
Michal Kazior's avatar
Michal Kazior committed
665
		   ar->monitor_vdev_id);
666
667
668
	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
669
static int ath10k_monitor_vdev_create(struct ath10k *ar)
670
671
672
673
674
{
	int bit, ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

Ben Greear's avatar
Ben Greear committed
675
	if (ar->free_vdev_map == 0) {
676
		ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
677
678
679
		return -ENOMEM;
	}

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

Ben Greear's avatar
Ben Greear committed
682
	ar->monitor_vdev_id = bit;
683
684
685
686
687

	ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
				     WMI_VDEV_TYPE_MONITOR,
				     0, ar->mac_addr);
	if (ret) {
688
		ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
689
			    ar->monitor_vdev_id, ret);
Ben Greear's avatar
Ben Greear committed
690
		return ret;
691
692
	}

Ben Greear's avatar
Ben Greear committed
693
	ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
694
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
695
696
697
698
699
		   ar->monitor_vdev_id);

	return 0;
}

Michal Kazior's avatar
Michal Kazior committed
700
static int ath10k_monitor_vdev_delete(struct ath10k *ar)
701
702
703
704
705
706
707
{
	int ret = 0;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
	if (ret) {
708
		ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
709
			    ar->monitor_vdev_id, ret);
710
711
712
		return ret;
	}

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

715
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
716
717
718
719
		   ar->monitor_vdev_id);
	return ret;
}

Michal Kazior's avatar
Michal Kazior committed
720
721
722
723
724
725
726
727
static int ath10k_monitor_start(struct ath10k *ar)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_monitor_vdev_create(ar);
	if (ret) {
728
		ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
Michal Kazior's avatar
Michal Kazior committed
729
730
731
732
733
		return ret;
	}

	ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
	if (ret) {
734
		ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
Michal Kazior's avatar
Michal Kazior committed
735
736
737
738
739
		ath10k_monitor_vdev_delete(ar);
		return ret;
	}

	ar->monitor_started = true;
740
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
Michal Kazior's avatar
Michal Kazior committed
741
742
743
744

	return 0;
}

745
static int ath10k_monitor_stop(struct ath10k *ar)
Michal Kazior's avatar
Michal Kazior committed
746
747
748
749
750
751
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ret = ath10k_monitor_vdev_stop(ar);
752
	if (ret) {
753
		ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
754
755
		return ret;
	}
Michal Kazior's avatar
Michal Kazior committed
756
757

	ret = ath10k_monitor_vdev_delete(ar);
758
	if (ret) {
759
		ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
760
761
		return ret;
	}
Michal Kazior's avatar
Michal Kazior committed
762
763

	ar->monitor_started = false;
764
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786

	return 0;
}

static int ath10k_monitor_recalc(struct ath10k *ar)
{
	bool should_start;

	lockdep_assert_held(&ar->conf_mutex);

	should_start = ar->monitor ||
		       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);
787
788

	return ath10k_monitor_stop(ar);
Michal Kazior's avatar
Michal Kazior committed
789
790
}

791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
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;

	if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
		rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);

	if (arvif->num_legacy_stations > 0)
		rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
			      WMI_RTSCTS_PROFILE);

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

811
812
813
814
815
816
817
818
static int ath10k_start_cac(struct ath10k *ar)
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);

819
	ret = ath10k_monitor_recalc(ar);
820
	if (ret) {
821
		ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
822
823
824
825
		clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
		return ret;
	}

826
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
827
828
829
830
831
832
833
834
835
836
837
838
839
840
		   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
841
	ath10k_monitor_stop(ar);
842

843
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
844
845
846
847

	return 0;
}

848
static void ath10k_recalc_radar_detection(struct ath10k *ar)
849
850
851
852
853
854
855
{
	int ret;

	lockdep_assert_held(&ar->conf_mutex);

	ath10k_stop_cac(ar);

856
	if (!ar->radar_enabled)
857
858
		return;

859
	if (ar->num_started_vdevs > 0)
860
861
862
863
864
865
866
867
868
		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.
		 */
869
		ath10k_warn(ar, "failed to start CAC: %d\n", ret);
870
871
872
873
		ieee80211_radar_detected(ar->hw);
	}
}

874
static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
Michal Kazior's avatar
Michal Kazior committed
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
{
	struct ath10k *ar = arvif->ar;
	struct cfg80211_chan_def *chandef = &ar->chandef;
	struct wmi_vdev_start_request_arg arg = {};
	int ret = 0;

	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;
	}

911
	ath10k_dbg(ar, ATH10K_DBG_MAC,
Michal Kazior's avatar
Michal Kazior committed
912
913
914
915
		   "mac vdev %d start center_freq %d phymode %s\n",
		   arg.vdev_id, arg.channel.freq,
		   ath10k_wmi_phymode_str(arg.channel.mode));

916
917
918
919
920
	if (restart)
		ret = ath10k_wmi_vdev_restart(ar, &arg);
	else
		ret = ath10k_wmi_vdev_start(ar, &arg);

Michal Kazior's avatar
Michal Kazior committed
921
	if (ret) {
922
		ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
Michal Kazior's avatar
Michal Kazior committed
923
924
925
926
927
928
			    arg.vdev_id, ret);
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
929
930
931
		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
932
933
934
		return ret;
	}

935
936
937
	ar->num_started_vdevs++;
	ath10k_recalc_radar_detection(ar);

Michal Kazior's avatar
Michal Kazior committed
938
939
940
	return ret;
}

941
942
943
944
945
946
947
948
949
950
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);
}

Michal Kazior's avatar
Michal Kazior committed
951
952
953
954
955
956
957
958
959
960
961
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) {
962
		ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
Michal Kazior's avatar
Michal Kazior committed
963
964
965
966
967
968
			    arvif->vdev_id, ret);
		return ret;
	}

	ret = ath10k_vdev_setup_sync(ar);
	if (ret) {
969
		ath10k_warn(ar, "failed to synchronize setup for vdev %i stop: %d\n",
Michal Kazior's avatar
Michal Kazior committed
970
971
972
973
			    arvif->vdev_id, ret);
		return ret;
	}

974
975
976
977
978
979
980
	WARN_ON(ar->num_started_vdevs == 0);

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

Michal Kazior's avatar
Michal Kazior committed
981
982
983
	return ret;
}

984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
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
1116
1117
1118
1119
1120
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;

	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;

	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;
}

1121
static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1122
				     struct ieee80211_bss_conf *info)
1123
{
1124
	struct ath10k *ar = arvif->ar;
1125
1126
	int ret = 0;

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

1129
1130
	if (!info->enable_beacon) {
		ath10k_vdev_stop(arvif);
1131
1132
1133
1134

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

1135
		spin_lock_bh(&arvif->ar->data_lock);
1136
		ath10k_mac_vif_beacon_free(arvif);
1137
1138
		spin_unlock_bh(&arvif->ar->data_lock);

1139
1140
1141
1142
1143
1144
1145
1146
1147
		return;
	}

	arvif->tx_seq_no = 0x1000;

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

1148
	arvif->aid = 0;
Kalle Valo's avatar
Kalle Valo committed
1149
	ether_addr_copy(arvif->bssid, info->bssid);
1150
1151
1152

	ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
				 arvif->bssid);
1153
	if (ret) {
1154
		ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1155
			    arvif->vdev_id, ret);
1156
		ath10k_vdev_stop(arvif);
1157
1158
		return;
	}
1159
1160
1161
1162

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

1163
	ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1164
1165
1166
1167
1168
1169
}

static void ath10k_control_ibss(struct ath10k_vif *arvif,
				struct ieee80211_bss_conf *info,
				const u8 self_peer[ETH_ALEN])
{
1170
	struct ath10k *ar = arvif->ar;
1171
	u32 vdev_param;
1172
1173
	int ret = 0;

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

1176
1177
1178
	if (!info->ibss_joined) {
		ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
		if (ret)
1179
			ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1180
1181
				    self_peer, arvif->vdev_id, ret);

1182
		if (is_zero_ether_addr(arvif->bssid))
1183
1184
			return;

1185
		eth_zero_addr(arvif->bssid);
1186
1187
1188
1189
1190
1191

		return;
	}

	ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
	if (ret) {
1192
		ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1193
1194
1195
1196
			    self_peer, arvif->vdev_id, ret);
		return;
	}

1197
1198
	vdev_param = arvif->ar->wmi.vdev_param->atim_window;
	ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1199
1200
					ATH10K_DEFAULT_ATIM);
	if (ret)
1201
		ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1202
1203
1204
			    arvif->vdev_id, ret);
}

Michal Kazior's avatar
Michal Kazior committed
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
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;
}

1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
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;
}

1270
static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1271
{
1272
	struct ath10k *ar = arvif->ar;
1273
	struct ieee80211_vif *vif = arvif->vif;
1274
	struct ieee80211_conf *conf = &ar->hw->conf;
1275
1276
1277
	enum wmi_sta_powersave_param param;
	enum wmi_sta_ps_mode psmode;
	int ret;
1278
	int ps_timeout;
1279
	bool enable_ps;
1280

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

1283
1284
	if (arvif->vif->type != NL80211_IFTYPE_STATION)
		return 0;
1285

1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
	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) {
1297
1298
1299
		psmode = WMI_STA_PS_MODE_ENABLED;
		param = WMI_STA_PS_PARAM_INACTIVITY_TIME;

1300
1301
1302
1303
1304
1305
1306
		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;
		}

1307
		ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1308
						  ps_timeout);
1309
		if (ret) {
1310
			ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1311
				    arvif->vdev_id, ret);
1312
			return ret;
1313
1314
1315
1316
1317
		}
	} else {
		psmode = WMI_STA_PS_MODE_DISABLED;
	}

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

1321
1322
	ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
	if (ret) {
1323
		ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1324
			    psmode, arvif->vdev_id, ret);
1325
1326
1327
1328
		return ret;
	}

	return 0;
1329
1330
}

Michal Kazior's avatar
Michal Kazior committed
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
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;
}

1363
1364
1365
1366
/**********************/
/* Station management */
/**********************/

Michal Kazior's avatar
Michal Kazior committed
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
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;
}

1384
static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
Michal Kazior's avatar
Michal Kazior committed
1385
				      struct ieee80211_vif *vif,
1386
1387
1388
				      struct ieee80211_sta *sta,
				      struct wmi_peer_assoc_complete_arg *arg)
{
Michal Kazior's avatar
Michal Kazior committed
1389
1390
	struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);

Michal Kazior's avatar
Michal Kazior committed
1391
1392
	lockdep_assert_held(&ar->conf_mutex);

Kalle Valo's avatar
Kalle Valo committed
1393
	ether_addr_copy(arg->addr, sta->addr);
1394
1395
1396
	arg->vdev_id = arvif->vdev_id;
	arg->peer_aid = sta->aid;
	arg->peer_flags |= WMI_PEER_AUTH;
Michal Kazior's avatar
Michal Kazior committed
1397
	arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1398
	arg->peer_num_spatial_streams = 1;
Michal Kazior's avatar
Michal Kazior committed
1399
	arg->peer_caps = vif->bss_conf.assoc_capability;
1400
1401
1402
}

static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
Michal Kazior's avatar
Michal Kazior committed
1403
				       struct ieee80211_vif *vif,
1404
1405
1406
1407
1408
1409
1410
				       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;

Michal Kazior's avatar
Michal Kazior committed
1411
1412
	lockdep_assert_held(&ar->conf_mutex);

1413
	bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1414
1415
			       info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
			       IEEE80211_PRIVACY_ANY);
1416
1417
1418
1419
1420
1421
1422
1423
1424
	if (bss) {
		const struct cfg80211_bss_ies *ies;

		rcu_read_lock();
		rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);

		ies = rcu_dereference(bss->ies);

		wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1425
1426
1427
						WLAN_OUI_TYPE_MICROSOFT_WPA,
						ies->data,
						ies->len);
1428
1429
1430
1431
1432
1433
		rcu_read_unlock();
		cfg80211_put_bss(ar->hw->wiphy, bss);
	}

	/* FIXME: base on RSN IE/WPA IE is a correct idea? */
	if (rsnie || wpaie) {
1434
		ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1435
1436
1437
1438
		arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
	}

	if (wpaie) {