]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/wireless/ath/ath10k/mac.c
ath10k: prevent fw reg dump spam
[mirror_ubuntu-artful-kernel.git] / drivers / net / wireless / ath / ath10k / mac.c
CommitLineData
5e3dd157
KV
1/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "mac.h"
19
20#include <net/mac80211.h>
21#include <linux/etherdevice.h>
22
8cd13cad 23#include "hif.h"
5e3dd157
KV
24#include "core.h"
25#include "debug.h"
26#include "wmi.h"
27#include "htt.h"
28#include "txrx.h"
43d2a30f 29#include "testmode.h"
d7579d12
MK
30#include "wmi.h"
31#include "wmi-ops.h"
5e3dd157
KV
32
33/**********/
34/* Crypto */
35/**********/
36
37static int ath10k_send_key(struct ath10k_vif *arvif,
38 struct ieee80211_key_conf *key,
39 enum set_key_cmd cmd,
40 const u8 *macaddr)
41{
7aa7a72a 42 struct ath10k *ar = arvif->ar;
5e3dd157
KV
43 struct wmi_vdev_install_key_arg arg = {
44 .vdev_id = arvif->vdev_id,
45 .key_idx = key->keyidx,
46 .key_len = key->keylen,
47 .key_data = key->key,
48 .macaddr = macaddr,
49 };
50
548db54c
MK
51 lockdep_assert_held(&arvif->ar->conf_mutex);
52
5e3dd157
KV
53 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
54 arg.key_flags = WMI_KEY_PAIRWISE;
55 else
56 arg.key_flags = WMI_KEY_GROUP;
57
58 switch (key->cipher) {
59 case WLAN_CIPHER_SUITE_CCMP:
60 arg.key_cipher = WMI_CIPHER_AES_CCM;
eeab266c
MK
61 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
62 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
63 else
64 key->flags |= IEEE80211_KEY_FLAG_SW_MGMT_TX;
5e3dd157
KV
65 break;
66 case WLAN_CIPHER_SUITE_TKIP:
5e3dd157
KV
67 arg.key_cipher = WMI_CIPHER_TKIP;
68 arg.key_txmic_len = 8;
69 arg.key_rxmic_len = 8;
70 break;
71 case WLAN_CIPHER_SUITE_WEP40:
72 case WLAN_CIPHER_SUITE_WEP104:
73 arg.key_cipher = WMI_CIPHER_WEP;
74 /* AP/IBSS mode requires self-key to be groupwise
75 * Otherwise pairwise key must be set */
76 if (memcmp(macaddr, arvif->vif->addr, ETH_ALEN))
77 arg.key_flags = WMI_KEY_PAIRWISE;
78 break;
79 default:
7aa7a72a 80 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
5e3dd157
KV
81 return -EOPNOTSUPP;
82 }
83
84 if (cmd == DISABLE_KEY) {
85 arg.key_cipher = WMI_CIPHER_NONE;
86 arg.key_data = NULL;
87 }
88
89 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
90}
91
92static int ath10k_install_key(struct ath10k_vif *arvif,
93 struct ieee80211_key_conf *key,
94 enum set_key_cmd cmd,
95 const u8 *macaddr)
96{
97 struct ath10k *ar = arvif->ar;
98 int ret;
99
548db54c
MK
100 lockdep_assert_held(&ar->conf_mutex);
101
16735d02 102 reinit_completion(&ar->install_key_done);
5e3dd157
KV
103
104 ret = ath10k_send_key(arvif, key, cmd, macaddr);
105 if (ret)
106 return ret;
107
108 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
109 if (ret == 0)
110 return -ETIMEDOUT;
111
112 return 0;
113}
114
115static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
116 const u8 *addr)
117{
118 struct ath10k *ar = arvif->ar;
119 struct ath10k_peer *peer;
120 int ret;
121 int i;
122
123 lockdep_assert_held(&ar->conf_mutex);
124
125 spin_lock_bh(&ar->data_lock);
126 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
127 spin_unlock_bh(&ar->data_lock);
128
129 if (!peer)
130 return -ENOENT;
131
132 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
133 if (arvif->wep_keys[i] == NULL)
134 continue;
135
136 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
137 addr);
138 if (ret)
139 return ret;
140
ae167131 141 spin_lock_bh(&ar->data_lock);
5e3dd157 142 peer->keys[i] = arvif->wep_keys[i];
ae167131 143 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
144 }
145
146 return 0;
147}
148
149static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
150 const u8 *addr)
151{
152 struct ath10k *ar = arvif->ar;
153 struct ath10k_peer *peer;
154 int first_errno = 0;
155 int ret;
156 int i;
157
158 lockdep_assert_held(&ar->conf_mutex);
159
160 spin_lock_bh(&ar->data_lock);
161 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
162 spin_unlock_bh(&ar->data_lock);
163
164 if (!peer)
165 return -ENOENT;
166
167 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
168 if (peer->keys[i] == NULL)
169 continue;
170
171 ret = ath10k_install_key(arvif, peer->keys[i],
172 DISABLE_KEY, addr);
173 if (ret && first_errno == 0)
174 first_errno = ret;
175
176 if (ret)
7aa7a72a 177 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
5e3dd157
KV
178 i, ret);
179
ae167131 180 spin_lock_bh(&ar->data_lock);
5e3dd157 181 peer->keys[i] = NULL;
ae167131 182 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
183 }
184
185 return first_errno;
186}
187
504f6cdf
SM
188bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
189 u8 keyidx)
190{
191 struct ath10k_peer *peer;
192 int i;
193
194 lockdep_assert_held(&ar->data_lock);
195
196 /* We don't know which vdev this peer belongs to,
197 * since WMI doesn't give us that information.
198 *
199 * FIXME: multi-bss needs to be handled.
200 */
201 peer = ath10k_peer_find(ar, 0, addr);
202 if (!peer)
203 return false;
204
205 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
206 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
207 return true;
208 }
209
210 return false;
211}
212
5e3dd157
KV
213static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
214 struct ieee80211_key_conf *key)
215{
216 struct ath10k *ar = arvif->ar;
217 struct ath10k_peer *peer;
218 u8 addr[ETH_ALEN];
219 int first_errno = 0;
220 int ret;
221 int i;
222
223 lockdep_assert_held(&ar->conf_mutex);
224
225 for (;;) {
226 /* since ath10k_install_key we can't hold data_lock all the
227 * time, so we try to remove the keys incrementally */
228 spin_lock_bh(&ar->data_lock);
229 i = 0;
230 list_for_each_entry(peer, &ar->peers, list) {
231 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
232 if (peer->keys[i] == key) {
b25f32cb 233 ether_addr_copy(addr, peer->addr);
5e3dd157
KV
234 peer->keys[i] = NULL;
235 break;
236 }
237 }
238
239 if (i < ARRAY_SIZE(peer->keys))
240 break;
241 }
242 spin_unlock_bh(&ar->data_lock);
243
244 if (i == ARRAY_SIZE(peer->keys))
245 break;
246
247 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr);
248 if (ret && first_errno == 0)
249 first_errno = ret;
250
251 if (ret)
7aa7a72a 252 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
be6546fc 253 addr, ret);
5e3dd157
KV
254 }
255
256 return first_errno;
257}
258
5e3dd157
KV
259/*********************/
260/* General utilities */
261/*********************/
262
263static inline enum wmi_phy_mode
264chan_to_phymode(const struct cfg80211_chan_def *chandef)
265{
266 enum wmi_phy_mode phymode = MODE_UNKNOWN;
267
268 switch (chandef->chan->band) {
269 case IEEE80211_BAND_2GHZ:
270 switch (chandef->width) {
271 case NL80211_CHAN_WIDTH_20_NOHT:
6faab127
PO
272 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
273 phymode = MODE_11B;
274 else
275 phymode = MODE_11G;
5e3dd157
KV
276 break;
277 case NL80211_CHAN_WIDTH_20:
278 phymode = MODE_11NG_HT20;
279 break;
280 case NL80211_CHAN_WIDTH_40:
281 phymode = MODE_11NG_HT40;
282 break;
0f817ed5
JL
283 case NL80211_CHAN_WIDTH_5:
284 case NL80211_CHAN_WIDTH_10:
5e3dd157
KV
285 case NL80211_CHAN_WIDTH_80:
286 case NL80211_CHAN_WIDTH_80P80:
287 case NL80211_CHAN_WIDTH_160:
288 phymode = MODE_UNKNOWN;
289 break;
290 }
291 break;
292 case IEEE80211_BAND_5GHZ:
293 switch (chandef->width) {
294 case NL80211_CHAN_WIDTH_20_NOHT:
295 phymode = MODE_11A;
296 break;
297 case NL80211_CHAN_WIDTH_20:
298 phymode = MODE_11NA_HT20;
299 break;
300 case NL80211_CHAN_WIDTH_40:
301 phymode = MODE_11NA_HT40;
302 break;
303 case NL80211_CHAN_WIDTH_80:
304 phymode = MODE_11AC_VHT80;
305 break;
0f817ed5
JL
306 case NL80211_CHAN_WIDTH_5:
307 case NL80211_CHAN_WIDTH_10:
5e3dd157
KV
308 case NL80211_CHAN_WIDTH_80P80:
309 case NL80211_CHAN_WIDTH_160:
310 phymode = MODE_UNKNOWN;
311 break;
312 }
313 break;
314 default:
315 break;
316 }
317
318 WARN_ON(phymode == MODE_UNKNOWN);
319 return phymode;
320}
321
322static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
323{
324/*
325 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
326 * 0 for no restriction
327 * 1 for 1/4 us
328 * 2 for 1/2 us
329 * 3 for 1 us
330 * 4 for 2 us
331 * 5 for 4 us
332 * 6 for 8 us
333 * 7 for 16 us
334 */
335 switch (mpdudensity) {
336 case 0:
337 return 0;
338 case 1:
339 case 2:
340 case 3:
341 /* Our lower layer calculations limit our precision to
342 1 microsecond */
343 return 1;
344 case 4:
345 return 2;
346 case 5:
347 return 4;
348 case 6:
349 return 8;
350 case 7:
351 return 16;
352 default:
353 return 0;
354 }
355}
356
357static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
358{
359 int ret;
360
361 lockdep_assert_held(&ar->conf_mutex);
362
cfd1061e
MK
363 if (ar->num_peers >= ar->max_num_peers)
364 return -ENOBUFS;
365
5e3dd157 366 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
479398b0 367 if (ret) {
7aa7a72a 368 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
69244e56 369 addr, vdev_id, ret);
5e3dd157 370 return ret;
479398b0 371 }
5e3dd157
KV
372
373 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
479398b0 374 if (ret) {
7aa7a72a 375 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
69244e56 376 addr, vdev_id, ret);
5e3dd157 377 return ret;
479398b0 378 }
292a753d 379
0e759f36 380 ar->num_peers++;
5e3dd157
KV
381
382 return 0;
383}
384
5a13e76e
KV
385static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
386{
387 struct ath10k *ar = arvif->ar;
388 u32 param;
389 int ret;
390
391 param = ar->wmi.pdev_param->sta_kickout_th;
392 ret = ath10k_wmi_pdev_set_param(ar, param,
393 ATH10K_KICKOUT_THRESHOLD);
394 if (ret) {
7aa7a72a 395 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
69244e56 396 arvif->vdev_id, ret);
5a13e76e
KV
397 return ret;
398 }
399
400 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
401 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
402 ATH10K_KEEPALIVE_MIN_IDLE);
403 if (ret) {
7aa7a72a 404 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
69244e56 405 arvif->vdev_id, ret);
5a13e76e
KV
406 return ret;
407 }
408
409 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
410 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
411 ATH10K_KEEPALIVE_MAX_IDLE);
412 if (ret) {
7aa7a72a 413 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
69244e56 414 arvif->vdev_id, ret);
5a13e76e
KV
415 return ret;
416 }
417
418 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
419 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
420 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
421 if (ret) {
7aa7a72a 422 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
69244e56 423 arvif->vdev_id, ret);
5a13e76e
KV
424 return ret;
425 }
426
427 return 0;
428}
429
acab6400 430static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
424121c3 431{
6d1506e7
BM
432 struct ath10k *ar = arvif->ar;
433 u32 vdev_param;
434
6d1506e7
BM
435 vdev_param = ar->wmi.vdev_param->rts_threshold;
436 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
424121c3
MK
437}
438
439static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
440{
6d1506e7
BM
441 struct ath10k *ar = arvif->ar;
442 u32 vdev_param;
443
424121c3
MK
444 if (value != 0xFFFFFFFF)
445 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
446 ATH10K_FRAGMT_THRESHOLD_MIN,
447 ATH10K_FRAGMT_THRESHOLD_MAX);
448
6d1506e7
BM
449 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
450 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
424121c3
MK
451}
452
5e3dd157
KV
453static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
454{
455 int ret;
456
457 lockdep_assert_held(&ar->conf_mutex);
458
459 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
460 if (ret)
461 return ret;
462
463 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
464 if (ret)
465 return ret;
466
0e759f36 467 ar->num_peers--;
0e759f36 468
5e3dd157
KV
469 return 0;
470}
471
472static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
473{
474 struct ath10k_peer *peer, *tmp;
475
476 lockdep_assert_held(&ar->conf_mutex);
477
478 spin_lock_bh(&ar->data_lock);
479 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
480 if (peer->vdev_id != vdev_id)
481 continue;
482
7aa7a72a 483 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
5e3dd157
KV
484 peer->addr, vdev_id);
485
486 list_del(&peer->list);
487 kfree(peer);
0e759f36 488 ar->num_peers--;
5e3dd157
KV
489 }
490 spin_unlock_bh(&ar->data_lock);
491}
492
a96d7745
MK
493static void ath10k_peer_cleanup_all(struct ath10k *ar)
494{
495 struct ath10k_peer *peer, *tmp;
496
497 lockdep_assert_held(&ar->conf_mutex);
498
499 spin_lock_bh(&ar->data_lock);
500 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
501 list_del(&peer->list);
502 kfree(peer);
503 }
504 spin_unlock_bh(&ar->data_lock);
292a753d
MK
505
506 ar->num_peers = 0;
cfd1061e 507 ar->num_stations = 0;
a96d7745
MK
508}
509
5e3dd157
KV
510/************************/
511/* Interface management */
512/************************/
513
64badcb6
MK
514void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
515{
516 struct ath10k *ar = arvif->ar;
517
518 lockdep_assert_held(&ar->data_lock);
519
520 if (!arvif->beacon)
521 return;
522
523 if (!arvif->beacon_buf)
524 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
525 arvif->beacon->len, DMA_TO_DEVICE);
526
527 dev_kfree_skb_any(arvif->beacon);
528
529 arvif->beacon = NULL;
530 arvif->beacon_sent = false;
531}
532
533static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
534{
535 struct ath10k *ar = arvif->ar;
536
537 lockdep_assert_held(&ar->data_lock);
538
539 ath10k_mac_vif_beacon_free(arvif);
540
541 if (arvif->beacon_buf) {
542 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
543 arvif->beacon_buf, arvif->beacon_paddr);
544 arvif->beacon_buf = NULL;
545 }
546}
547
5e3dd157
KV
548static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
549{
550 int ret;
551
548db54c
MK
552 lockdep_assert_held(&ar->conf_mutex);
553
7962b0d8
MK
554 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
555 return -ESHUTDOWN;
556
5e3dd157
KV
557 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
558 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
559 if (ret == 0)
560 return -ETIMEDOUT;
561
562 return 0;
563}
564
1bbc0975 565static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
5e3dd157 566{
c930f744
MK
567 struct cfg80211_chan_def *chandef = &ar->chandef;
568 struct ieee80211_channel *channel = chandef->chan;
5e3dd157 569 struct wmi_vdev_start_request_arg arg = {};
5e3dd157
KV
570 int ret = 0;
571
572 lockdep_assert_held(&ar->conf_mutex);
573
5e3dd157
KV
574 arg.vdev_id = vdev_id;
575 arg.channel.freq = channel->center_freq;
c930f744 576 arg.channel.band_center_freq1 = chandef->center_freq1;
5e3dd157
KV
577
578 /* TODO setup this dynamically, what in case we
579 don't have any vifs? */
c930f744 580 arg.channel.mode = chan_to_phymode(chandef);
e8a50f8b
MP
581 arg.channel.chan_radar =
582 !!(channel->flags & IEEE80211_CHAN_RADAR);
5e3dd157 583
89c5c843 584 arg.channel.min_power = 0;
02256930
MK
585 arg.channel.max_power = channel->max_power * 2;
586 arg.channel.max_reg_power = channel->max_reg_power * 2;
587 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157 588
7962b0d8
MK
589 reinit_completion(&ar->vdev_setup_done);
590
5e3dd157
KV
591 ret = ath10k_wmi_vdev_start(ar, &arg);
592 if (ret) {
7aa7a72a 593 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
69244e56 594 vdev_id, ret);
5e3dd157
KV
595 return ret;
596 }
597
598 ret = ath10k_vdev_setup_sync(ar);
599 if (ret) {
7aa7a72a 600 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i: %d\n",
69244e56 601 vdev_id, ret);
5e3dd157
KV
602 return ret;
603 }
604
605 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
606 if (ret) {
7aa7a72a 607 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
69244e56 608 vdev_id, ret);
5e3dd157
KV
609 goto vdev_stop;
610 }
611
612 ar->monitor_vdev_id = vdev_id;
5e3dd157 613
7aa7a72a 614 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1bbc0975 615 ar->monitor_vdev_id);
5e3dd157
KV
616 return 0;
617
618vdev_stop:
619 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
620 if (ret)
7aa7a72a 621 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
69244e56 622 ar->monitor_vdev_id, ret);
5e3dd157
KV
623
624 return ret;
625}
626
1bbc0975 627static int ath10k_monitor_vdev_stop(struct ath10k *ar)
5e3dd157
KV
628{
629 int ret = 0;
630
631 lockdep_assert_held(&ar->conf_mutex);
632
52fa0191
MP
633 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
634 if (ret)
7aa7a72a 635 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
69244e56 636 ar->monitor_vdev_id, ret);
5e3dd157 637
7962b0d8
MK
638 reinit_completion(&ar->vdev_setup_done);
639
5e3dd157
KV
640 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
641 if (ret)
7aa7a72a 642 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
69244e56 643 ar->monitor_vdev_id, ret);
5e3dd157
KV
644
645 ret = ath10k_vdev_setup_sync(ar);
646 if (ret)
7aa7a72a 647 ath10k_warn(ar, "failed to synchronise monitor vdev %i: %d\n",
69244e56 648 ar->monitor_vdev_id, ret);
5e3dd157 649
7aa7a72a 650 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1bbc0975 651 ar->monitor_vdev_id);
5e3dd157
KV
652 return ret;
653}
654
1bbc0975 655static int ath10k_monitor_vdev_create(struct ath10k *ar)
5e3dd157
KV
656{
657 int bit, ret = 0;
658
659 lockdep_assert_held(&ar->conf_mutex);
660
a9aefb3b 661 if (ar->free_vdev_map == 0) {
7aa7a72a 662 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
5e3dd157
KV
663 return -ENOMEM;
664 }
665
16c11176 666 bit = __ffs64(ar->free_vdev_map);
a9aefb3b 667
16c11176 668 ar->monitor_vdev_id = bit;
5e3dd157
KV
669
670 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
671 WMI_VDEV_TYPE_MONITOR,
672 0, ar->mac_addr);
673 if (ret) {
7aa7a72a 674 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
69244e56 675 ar->monitor_vdev_id, ret);
a9aefb3b 676 return ret;
5e3dd157
KV
677 }
678
16c11176 679 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
7aa7a72a 680 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
5e3dd157
KV
681 ar->monitor_vdev_id);
682
5e3dd157 683 return 0;
5e3dd157
KV
684}
685
1bbc0975 686static int ath10k_monitor_vdev_delete(struct ath10k *ar)
5e3dd157
KV
687{
688 int ret = 0;
689
690 lockdep_assert_held(&ar->conf_mutex);
691
5e3dd157
KV
692 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
693 if (ret) {
7aa7a72a 694 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
69244e56 695 ar->monitor_vdev_id, ret);
5e3dd157
KV
696 return ret;
697 }
698
16c11176 699 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
5e3dd157 700
7aa7a72a 701 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
5e3dd157
KV
702 ar->monitor_vdev_id);
703 return ret;
704}
705
1bbc0975
MK
706static int ath10k_monitor_start(struct ath10k *ar)
707{
708 int ret;
709
710 lockdep_assert_held(&ar->conf_mutex);
711
1bbc0975
MK
712 ret = ath10k_monitor_vdev_create(ar);
713 if (ret) {
7aa7a72a 714 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1bbc0975
MK
715 return ret;
716 }
717
718 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
719 if (ret) {
7aa7a72a 720 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1bbc0975
MK
721 ath10k_monitor_vdev_delete(ar);
722 return ret;
723 }
724
725 ar->monitor_started = true;
7aa7a72a 726 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1bbc0975
MK
727
728 return 0;
729}
730
1933747f 731static int ath10k_monitor_stop(struct ath10k *ar)
1bbc0975
MK
732{
733 int ret;
734
735 lockdep_assert_held(&ar->conf_mutex);
736
1bbc0975 737 ret = ath10k_monitor_vdev_stop(ar);
1933747f 738 if (ret) {
7aa7a72a 739 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1933747f
MK
740 return ret;
741 }
1bbc0975
MK
742
743 ret = ath10k_monitor_vdev_delete(ar);
1933747f 744 if (ret) {
7aa7a72a 745 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1933747f
MK
746 return ret;
747 }
1bbc0975
MK
748
749 ar->monitor_started = false;
7aa7a72a 750 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1933747f
MK
751
752 return 0;
753}
754
755static int ath10k_monitor_recalc(struct ath10k *ar)
756{
757 bool should_start;
758
759 lockdep_assert_held(&ar->conf_mutex);
760
761 should_start = ar->monitor ||
762 ar->filter_flags & FIF_PROMISC_IN_BSS ||
763 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
764
765 ath10k_dbg(ar, ATH10K_DBG_MAC,
766 "mac monitor recalc started? %d should? %d\n",
767 ar->monitor_started, should_start);
768
769 if (should_start == ar->monitor_started)
770 return 0;
771
772 if (should_start)
773 return ath10k_monitor_start(ar);
d8bb26b9
KV
774
775 return ath10k_monitor_stop(ar);
1bbc0975
MK
776}
777
e81bd104
MK
778static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
779{
780 struct ath10k *ar = arvif->ar;
781 u32 vdev_param, rts_cts = 0;
782
783 lockdep_assert_held(&ar->conf_mutex);
784
785 vdev_param = ar->wmi.vdev_param->enable_rtscts;
786
787 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
788 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
789
790 if (arvif->num_legacy_stations > 0)
791 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
792 WMI_RTSCTS_PROFILE);
793
794 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
795 rts_cts);
796}
797
e8a50f8b
MP
798static int ath10k_start_cac(struct ath10k *ar)
799{
800 int ret;
801
802 lockdep_assert_held(&ar->conf_mutex);
803
804 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
805
1933747f 806 ret = ath10k_monitor_recalc(ar);
e8a50f8b 807 if (ret) {
7aa7a72a 808 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
e8a50f8b
MP
809 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
810 return ret;
811 }
812
7aa7a72a 813 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
e8a50f8b
MP
814 ar->monitor_vdev_id);
815
816 return 0;
817}
818
819static int ath10k_stop_cac(struct ath10k *ar)
820{
821 lockdep_assert_held(&ar->conf_mutex);
822
823 /* CAC is not running - do nothing */
824 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
825 return 0;
826
e8a50f8b 827 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1bbc0975 828 ath10k_monitor_stop(ar);
e8a50f8b 829
7aa7a72a 830 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
e8a50f8b
MP
831
832 return 0;
833}
834
d650097b 835static void ath10k_recalc_radar_detection(struct ath10k *ar)
e8a50f8b 836{
e8a50f8b
MP
837 int ret;
838
839 lockdep_assert_held(&ar->conf_mutex);
840
e8a50f8b
MP
841 ath10k_stop_cac(ar);
842
d650097b 843 if (!ar->radar_enabled)
e8a50f8b
MP
844 return;
845
d650097b 846 if (ar->num_started_vdevs > 0)
e8a50f8b
MP
847 return;
848
849 ret = ath10k_start_cac(ar);
850 if (ret) {
851 /*
852 * Not possible to start CAC on current channel so starting
853 * radiation is not allowed, make this channel DFS_UNAVAILABLE
854 * by indicating that radar was detected.
855 */
7aa7a72a 856 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
e8a50f8b
MP
857 ieee80211_radar_detected(ar->hw);
858 }
859}
860
dc55e307 861static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
72654fa7
MK
862{
863 struct ath10k *ar = arvif->ar;
864 struct cfg80211_chan_def *chandef = &ar->chandef;
865 struct wmi_vdev_start_request_arg arg = {};
866 int ret = 0;
867
868 lockdep_assert_held(&ar->conf_mutex);
869
870 reinit_completion(&ar->vdev_setup_done);
871
872 arg.vdev_id = arvif->vdev_id;
873 arg.dtim_period = arvif->dtim_period;
874 arg.bcn_intval = arvif->beacon_interval;
875
876 arg.channel.freq = chandef->chan->center_freq;
877 arg.channel.band_center_freq1 = chandef->center_freq1;
878 arg.channel.mode = chan_to_phymode(chandef);
879
880 arg.channel.min_power = 0;
881 arg.channel.max_power = chandef->chan->max_power * 2;
882 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
883 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
884
885 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
886 arg.ssid = arvif->u.ap.ssid;
887 arg.ssid_len = arvif->u.ap.ssid_len;
888 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
889
890 /* For now allow DFS for AP mode */
891 arg.channel.chan_radar =
892 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
893 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
894 arg.ssid = arvif->vif->bss_conf.ssid;
895 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
896 }
897
7aa7a72a 898 ath10k_dbg(ar, ATH10K_DBG_MAC,
72654fa7
MK
899 "mac vdev %d start center_freq %d phymode %s\n",
900 arg.vdev_id, arg.channel.freq,
901 ath10k_wmi_phymode_str(arg.channel.mode));
902
dc55e307
MK
903 if (restart)
904 ret = ath10k_wmi_vdev_restart(ar, &arg);
905 else
906 ret = ath10k_wmi_vdev_start(ar, &arg);
907
72654fa7 908 if (ret) {
7aa7a72a 909 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
72654fa7
MK
910 arg.vdev_id, ret);
911 return ret;
912 }
913
914 ret = ath10k_vdev_setup_sync(ar);
915 if (ret) {
7aa7a72a 916 ath10k_warn(ar, "failed to synchronise setup for vdev %i: %d\n",
72654fa7
MK
917 arg.vdev_id, ret);
918 return ret;
919 }
920
d650097b
MK
921 ar->num_started_vdevs++;
922 ath10k_recalc_radar_detection(ar);
923
72654fa7
MK
924 return ret;
925}
926
dc55e307
MK
927static int ath10k_vdev_start(struct ath10k_vif *arvif)
928{
929 return ath10k_vdev_start_restart(arvif, false);
930}
931
932static int ath10k_vdev_restart(struct ath10k_vif *arvif)
933{
934 return ath10k_vdev_start_restart(arvif, true);
935}
936
72654fa7
MK
937static int ath10k_vdev_stop(struct ath10k_vif *arvif)
938{
939 struct ath10k *ar = arvif->ar;
940 int ret;
941
942 lockdep_assert_held(&ar->conf_mutex);
943
944 reinit_completion(&ar->vdev_setup_done);
945
946 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
947 if (ret) {
7aa7a72a 948 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
72654fa7
MK
949 arvif->vdev_id, ret);
950 return ret;
951 }
952
953 ret = ath10k_vdev_setup_sync(ar);
954 if (ret) {
7aa7a72a 955 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
72654fa7
MK
956 arvif->vdev_id, ret);
957 return ret;
958 }
959
d650097b
MK
960 WARN_ON(ar->num_started_vdevs == 0);
961
962 if (ar->num_started_vdevs != 0) {
963 ar->num_started_vdevs--;
964 ath10k_recalc_radar_detection(ar);
965 }
966
72654fa7
MK
967 return ret;
968}
969
fbb8f1b7
MK
970static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
971 struct sk_buff *bcn)
972{
973 struct ath10k *ar = arvif->ar;
974 struct ieee80211_mgmt *mgmt;
975 const u8 *p2p_ie;
976 int ret;
977
978 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
979 return 0;
980
981 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
982 return 0;
983
984 mgmt = (void *)bcn->data;
985 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
986 mgmt->u.beacon.variable,
987 bcn->len - (mgmt->u.beacon.variable -
988 bcn->data));
989 if (!p2p_ie)
990 return -ENOENT;
991
992 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
993 if (ret) {
994 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
995 arvif->vdev_id, ret);
996 return ret;
997 }
998
999 return 0;
1000}
1001
1002static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1003 u8 oui_type, size_t ie_offset)
1004{
1005 size_t len;
1006 const u8 *next;
1007 const u8 *end;
1008 u8 *ie;
1009
1010 if (WARN_ON(skb->len < ie_offset))
1011 return -EINVAL;
1012
1013 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1014 skb->data + ie_offset,
1015 skb->len - ie_offset);
1016 if (!ie)
1017 return -ENOENT;
1018
1019 len = ie[1] + 2;
1020 end = skb->data + skb->len;
1021 next = ie + len;
1022
1023 if (WARN_ON(next > end))
1024 return -EINVAL;
1025
1026 memmove(ie, next, end - next);
1027 skb_trim(skb, skb->len - len);
1028
1029 return 0;
1030}
1031
1032static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1033{
1034 struct ath10k *ar = arvif->ar;
1035 struct ieee80211_hw *hw = ar->hw;
1036 struct ieee80211_vif *vif = arvif->vif;
1037 struct ieee80211_mutable_offsets offs = {};
1038 struct sk_buff *bcn;
1039 int ret;
1040
1041 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1042 return 0;
1043
1044 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1045 if (!bcn) {
1046 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1047 return -EPERM;
1048 }
1049
1050 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1051 if (ret) {
1052 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1053 kfree_skb(bcn);
1054 return ret;
1055 }
1056
1057 /* P2P IE is inserted by firmware automatically (as configured above)
1058 * so remove it from the base beacon template to avoid duplicate P2P
1059 * IEs in beacon frames.
1060 */
1061 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1062 offsetof(struct ieee80211_mgmt,
1063 u.beacon.variable));
1064
1065 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1066 0, NULL, 0);
1067 kfree_skb(bcn);
1068
1069 if (ret) {
1070 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1071 ret);
1072 return ret;
1073 }
1074
1075 return 0;
1076}
1077
1078static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1079{
1080 struct ath10k *ar = arvif->ar;
1081 struct ieee80211_hw *hw = ar->hw;
1082 struct ieee80211_vif *vif = arvif->vif;
1083 struct sk_buff *prb;
1084 int ret;
1085
1086 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1087 return 0;
1088
1089 prb = ieee80211_proberesp_get(hw, vif);
1090 if (!prb) {
1091 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1092 return -EPERM;
1093 }
1094
1095 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1096 kfree_skb(prb);
1097
1098 if (ret) {
1099 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1100 ret);
1101 return ret;
1102 }
1103
1104 return 0;
1105}
1106
5e3dd157 1107static void ath10k_control_beaconing(struct ath10k_vif *arvif,
5b07e07f 1108 struct ieee80211_bss_conf *info)
5e3dd157 1109{
7aa7a72a 1110 struct ath10k *ar = arvif->ar;
5e3dd157
KV
1111 int ret = 0;
1112
548db54c
MK
1113 lockdep_assert_held(&arvif->ar->conf_mutex);
1114
5e3dd157
KV
1115 if (!info->enable_beacon) {
1116 ath10k_vdev_stop(arvif);
c930f744
MK
1117
1118 arvif->is_started = false;
1119 arvif->is_up = false;
1120
748afc47 1121 spin_lock_bh(&arvif->ar->data_lock);
64badcb6 1122 ath10k_mac_vif_beacon_free(arvif);
748afc47
MK
1123 spin_unlock_bh(&arvif->ar->data_lock);
1124
5e3dd157
KV
1125 return;
1126 }
1127
1128 arvif->tx_seq_no = 0x1000;
1129
1130 ret = ath10k_vdev_start(arvif);
1131 if (ret)
1132 return;
1133
c930f744 1134 arvif->aid = 0;
b25f32cb 1135 ether_addr_copy(arvif->bssid, info->bssid);
c930f744
MK
1136
1137 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1138 arvif->bssid);
5e3dd157 1139 if (ret) {
7aa7a72a 1140 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
69244e56 1141 arvif->vdev_id, ret);
c930f744 1142 ath10k_vdev_stop(arvif);
5e3dd157
KV
1143 return;
1144 }
c930f744
MK
1145
1146 arvif->is_started = true;
1147 arvif->is_up = true;
1148
7aa7a72a 1149 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
5e3dd157
KV
1150}
1151
1152static void ath10k_control_ibss(struct ath10k_vif *arvif,
1153 struct ieee80211_bss_conf *info,
1154 const u8 self_peer[ETH_ALEN])
1155{
7aa7a72a 1156 struct ath10k *ar = arvif->ar;
6d1506e7 1157 u32 vdev_param;
5e3dd157
KV
1158 int ret = 0;
1159
548db54c
MK
1160 lockdep_assert_held(&arvif->ar->conf_mutex);
1161
5e3dd157
KV
1162 if (!info->ibss_joined) {
1163 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1164 if (ret)
7aa7a72a 1165 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
5e3dd157
KV
1166 self_peer, arvif->vdev_id, ret);
1167
c930f744 1168 if (is_zero_ether_addr(arvif->bssid))
5e3dd157
KV
1169 return;
1170
c930f744 1171 memset(arvif->bssid, 0, ETH_ALEN);
5e3dd157
KV
1172
1173 return;
1174 }
1175
1176 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1177 if (ret) {
7aa7a72a 1178 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
5e3dd157
KV
1179 self_peer, arvif->vdev_id, ret);
1180 return;
1181 }
1182
6d1506e7
BM
1183 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1184 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
1185 ATH10K_DEFAULT_ATIM);
1186 if (ret)
7aa7a72a 1187 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
5e3dd157
KV
1188 arvif->vdev_id, ret);
1189}
1190
9f9b5746
MK
1191static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1192{
1193 struct ath10k *ar = arvif->ar;
1194 u32 param;
1195 u32 value;
1196 int ret;
1197
1198 lockdep_assert_held(&arvif->ar->conf_mutex);
1199
1200 if (arvif->u.sta.uapsd)
1201 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1202 else
1203 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1204
1205 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1206 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1207 if (ret) {
1208 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1209 value, arvif->vdev_id, ret);
1210 return ret;
1211 }
1212
1213 return 0;
1214}
1215
1216static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1217{
1218 struct ath10k *ar = arvif->ar;
1219 u32 param;
1220 u32 value;
1221 int ret;
1222
1223 lockdep_assert_held(&arvif->ar->conf_mutex);
1224
1225 if (arvif->u.sta.uapsd)
1226 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1227 else
1228 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1229
1230 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1231 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1232 param, value);
1233 if (ret) {
1234 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1235 value, arvif->vdev_id, ret);
1236 return ret;
1237 }
1238
1239 return 0;
1240}
1241
ad088bfa 1242static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
5e3dd157 1243{
ad088bfa 1244 struct ath10k *ar = arvif->ar;
526549a8 1245 struct ieee80211_vif *vif = arvif->vif;
ad088bfa 1246 struct ieee80211_conf *conf = &ar->hw->conf;
5e3dd157
KV
1247 enum wmi_sta_powersave_param param;
1248 enum wmi_sta_ps_mode psmode;
1249 int ret;
526549a8 1250 int ps_timeout;
5e3dd157 1251
548db54c
MK
1252 lockdep_assert_held(&arvif->ar->conf_mutex);
1253
ad088bfa
MK
1254 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1255 return 0;
5e3dd157 1256
bf14e65c 1257 if (vif->bss_conf.ps) {
5e3dd157
KV
1258 psmode = WMI_STA_PS_MODE_ENABLED;
1259 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1260
526549a8
MK
1261 ps_timeout = conf->dynamic_ps_timeout;
1262 if (ps_timeout == 0) {
1263 /* Firmware doesn't like 0 */
1264 ps_timeout = ieee80211_tu_to_usec(
1265 vif->bss_conf.beacon_int) / 1000;
1266 }
1267
ad088bfa 1268 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
526549a8 1269 ps_timeout);
5e3dd157 1270 if (ret) {
7aa7a72a 1271 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
69244e56 1272 arvif->vdev_id, ret);
ad088bfa 1273 return ret;
5e3dd157 1274 }
5e3dd157
KV
1275 } else {
1276 psmode = WMI_STA_PS_MODE_DISABLED;
1277 }
1278
7aa7a72a 1279 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
60c3daa8
KV
1280 arvif->vdev_id, psmode ? "enable" : "disable");
1281
ad088bfa
MK
1282 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1283 if (ret) {
7aa7a72a 1284 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
be6546fc 1285 psmode, arvif->vdev_id, ret);
ad088bfa
MK
1286 return ret;
1287 }
1288
1289 return 0;
5e3dd157
KV
1290}
1291
1292/**********************/
1293/* Station management */
1294/**********************/
1295
590922a8
MK
1296static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1297 struct ieee80211_vif *vif)
1298{
1299 /* Some firmware revisions have unstable STA powersave when listen
1300 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1301 * generate NullFunc frames properly even if buffered frames have been
1302 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1303 * buffered frames. Often pinging the device from AP would simply fail.
1304 *
1305 * As a workaround set it to 1.
1306 */
1307 if (vif->type == NL80211_IFTYPE_STATION)
1308 return 1;
1309
1310 return ar->hw->conf.listen_interval;
1311}
1312
5e3dd157 1313static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
590922a8 1314 struct ieee80211_vif *vif,
5e3dd157 1315 struct ieee80211_sta *sta,
5e3dd157
KV
1316 struct wmi_peer_assoc_complete_arg *arg)
1317{
590922a8
MK
1318 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1319
548db54c
MK
1320 lockdep_assert_held(&ar->conf_mutex);
1321
b25f32cb 1322 ether_addr_copy(arg->addr, sta->addr);
5e3dd157
KV
1323 arg->vdev_id = arvif->vdev_id;
1324 arg->peer_aid = sta->aid;
1325 arg->peer_flags |= WMI_PEER_AUTH;
590922a8 1326 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
5e3dd157 1327 arg->peer_num_spatial_streams = 1;
590922a8 1328 arg->peer_caps = vif->bss_conf.assoc_capability;
5e3dd157
KV
1329}
1330
1331static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
590922a8 1332 struct ieee80211_vif *vif,
5e3dd157
KV
1333 struct wmi_peer_assoc_complete_arg *arg)
1334{
5e3dd157
KV
1335 struct ieee80211_bss_conf *info = &vif->bss_conf;
1336 struct cfg80211_bss *bss;
1337 const u8 *rsnie = NULL;
1338 const u8 *wpaie = NULL;
1339
548db54c
MK
1340 lockdep_assert_held(&ar->conf_mutex);
1341
5e3dd157
KV
1342 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1343 info->bssid, NULL, 0, 0, 0);
1344 if (bss) {
1345 const struct cfg80211_bss_ies *ies;
1346
1347 rcu_read_lock();
1348 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1349
1350 ies = rcu_dereference(bss->ies);
1351
1352 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
5b07e07f
KV
1353 WLAN_OUI_TYPE_MICROSOFT_WPA,
1354 ies->data,
1355 ies->len);
5e3dd157
KV
1356 rcu_read_unlock();
1357 cfg80211_put_bss(ar->hw->wiphy, bss);
1358 }
1359
1360 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1361 if (rsnie || wpaie) {
7aa7a72a 1362 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
5e3dd157
KV
1363 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1364 }
1365
1366 if (wpaie) {
7aa7a72a 1367 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
5e3dd157
KV
1368 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1369 }
1370}
1371
1372static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1373 struct ieee80211_sta *sta,
1374 struct wmi_peer_assoc_complete_arg *arg)
1375{
1376 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1377 const struct ieee80211_supported_band *sband;
1378 const struct ieee80211_rate *rates;
1379 u32 ratemask;
1380 int i;
1381
548db54c
MK
1382 lockdep_assert_held(&ar->conf_mutex);
1383
5e3dd157
KV
1384 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1385 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1386 rates = sband->bitrates;
1387
1388 rateset->num_rates = 0;
1389
1390 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1391 if (!(ratemask & 1))
1392 continue;
1393
1394 rateset->rates[rateset->num_rates] = rates->hw_value;
1395 rateset->num_rates++;
1396 }
1397}
1398
1399static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1400 struct ieee80211_sta *sta,
1401 struct wmi_peer_assoc_complete_arg *arg)
1402{
1403 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
5e3dd157 1404 int i, n;
af762c0b 1405 u32 stbc;
5e3dd157 1406
548db54c
MK
1407 lockdep_assert_held(&ar->conf_mutex);
1408
5e3dd157
KV
1409 if (!ht_cap->ht_supported)
1410 return;
1411
1412 arg->peer_flags |= WMI_PEER_HT;
1413 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1414 ht_cap->ampdu_factor)) - 1;
1415
1416 arg->peer_mpdu_density =
1417 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1418
1419 arg->peer_ht_caps = ht_cap->cap;
1420 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1421
1422 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1423 arg->peer_flags |= WMI_PEER_LDPC;
1424
1425 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1426 arg->peer_flags |= WMI_PEER_40MHZ;
1427 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1428 }
1429
1430 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1431 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1432
1433 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1434 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1435
1436 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1437 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1438 arg->peer_flags |= WMI_PEER_STBC;
1439 }
1440
1441 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
5e3dd157
KV
1442 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1443 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1444 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1445 arg->peer_rate_caps |= stbc;
1446 arg->peer_flags |= WMI_PEER_STBC;
1447 }
1448
5e3dd157
KV
1449 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1450 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1451 else if (ht_cap->mcs.rx_mask[1])
1452 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1453
1454 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1455 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1456 arg->peer_ht_rates.rates[n++] = i;
1457
fd71f807
BM
1458 /*
1459 * This is a workaround for HT-enabled STAs which break the spec
1460 * and have no HT capabilities RX mask (no HT RX MCS map).
1461 *
1462 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1463 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1464 *
1465 * Firmware asserts if such situation occurs.
1466 */
1467 if (n == 0) {
1468 arg->peer_ht_rates.num_rates = 8;
1469 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1470 arg->peer_ht_rates.rates[i] = i;
1471 } else {
1472 arg->peer_ht_rates.num_rates = n;
1473 arg->peer_num_spatial_streams = sta->rx_nss;
1474 }
5e3dd157 1475
7aa7a72a 1476 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
60c3daa8 1477 arg->addr,
5e3dd157
KV
1478 arg->peer_ht_rates.num_rates,
1479 arg->peer_num_spatial_streams);
1480}
1481
d3d3ff42
JD
1482static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1483 struct ath10k_vif *arvif,
1484 struct ieee80211_sta *sta)
5e3dd157
KV
1485{
1486 u32 uapsd = 0;
1487 u32 max_sp = 0;
d3d3ff42 1488 int ret = 0;
5e3dd157 1489
548db54c
MK
1490 lockdep_assert_held(&ar->conf_mutex);
1491
5e3dd157 1492 if (sta->wme && sta->uapsd_queues) {
7aa7a72a 1493 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
5e3dd157
KV
1494 sta->uapsd_queues, sta->max_sp);
1495
5e3dd157
KV
1496 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1497 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1498 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1499 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1500 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1501 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1502 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1503 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1504 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1505 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1506 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1507 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1508
5e3dd157
KV
1509 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1510 max_sp = sta->max_sp;
1511
d3d3ff42
JD
1512 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1513 sta->addr,
1514 WMI_AP_PS_PEER_PARAM_UAPSD,
1515 uapsd);
1516 if (ret) {
7aa7a72a 1517 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
69244e56 1518 arvif->vdev_id, ret);
d3d3ff42
JD
1519 return ret;
1520 }
5e3dd157 1521
d3d3ff42
JD
1522 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1523 sta->addr,
1524 WMI_AP_PS_PEER_PARAM_MAX_SP,
1525 max_sp);
1526 if (ret) {
7aa7a72a 1527 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
69244e56 1528 arvif->vdev_id, ret);
d3d3ff42
JD
1529 return ret;
1530 }
5e3dd157
KV
1531
1532 /* TODO setup this based on STA listen interval and
1533 beacon interval. Currently we don't know
1534 sta->listen_interval - mac80211 patch required.
1535 Currently use 10 seconds */
d3d3ff42 1536 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
5b07e07f
KV
1537 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1538 10);
d3d3ff42 1539 if (ret) {
7aa7a72a 1540 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
69244e56 1541 arvif->vdev_id, ret);
d3d3ff42
JD
1542 return ret;
1543 }
5e3dd157 1544 }
5e3dd157 1545
d3d3ff42 1546 return 0;
5e3dd157
KV
1547}
1548
1549static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1550 struct ieee80211_sta *sta,
1551 struct wmi_peer_assoc_complete_arg *arg)
1552{
1553 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
a24b88b5 1554 u8 ampdu_factor;
5e3dd157
KV
1555
1556 if (!vht_cap->vht_supported)
1557 return;
1558
1559 arg->peer_flags |= WMI_PEER_VHT;
5e3dd157
KV
1560 arg->peer_vht_caps = vht_cap->cap;
1561
a24b88b5
SM
1562 ampdu_factor = (vht_cap->cap &
1563 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1564 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1565
1566 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1567 * zero in VHT IE. Using it would result in degraded throughput.
1568 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1569 * it if VHT max_mpdu is smaller. */
1570 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1571 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1572 ampdu_factor)) - 1);
1573
5e3dd157
KV
1574 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1575 arg->peer_flags |= WMI_PEER_80MHZ;
1576
1577 arg->peer_vht_rates.rx_max_rate =
1578 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1579 arg->peer_vht_rates.rx_mcs_set =
1580 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1581 arg->peer_vht_rates.tx_max_rate =
1582 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1583 arg->peer_vht_rates.tx_mcs_set =
1584 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1585
7aa7a72a 1586 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
60c3daa8 1587 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
5e3dd157
KV
1588}
1589
1590static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
590922a8 1591 struct ieee80211_vif *vif,
5e3dd157 1592 struct ieee80211_sta *sta,
5e3dd157
KV
1593 struct wmi_peer_assoc_complete_arg *arg)
1594{
590922a8
MK
1595 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1596
5e3dd157
KV
1597 switch (arvif->vdev_type) {
1598 case WMI_VDEV_TYPE_AP:
d3d3ff42
JD
1599 if (sta->wme)
1600 arg->peer_flags |= WMI_PEER_QOS;
1601
1602 if (sta->wme && sta->uapsd_queues) {
1603 arg->peer_flags |= WMI_PEER_APSD;
1604 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1605 }
5e3dd157
KV
1606 break;
1607 case WMI_VDEV_TYPE_STA:
590922a8 1608 if (vif->bss_conf.qos)
d3d3ff42 1609 arg->peer_flags |= WMI_PEER_QOS;
5e3dd157 1610 break;
627d9841
JD
1611 case WMI_VDEV_TYPE_IBSS:
1612 if (sta->wme)
1613 arg->peer_flags |= WMI_PEER_QOS;
1614 break;
5e3dd157
KV
1615 default:
1616 break;
1617 }
627d9841
JD
1618
1619 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1620 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
5e3dd157
KV
1621}
1622
91b12089
MK
1623static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1624{
1625 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1626 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1627}
1628
5e3dd157 1629static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
590922a8 1630 struct ieee80211_vif *vif,
5e3dd157
KV
1631 struct ieee80211_sta *sta,
1632 struct wmi_peer_assoc_complete_arg *arg)
1633{
1634 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1635
5e3dd157
KV
1636 switch (ar->hw->conf.chandef.chan->band) {
1637 case IEEE80211_BAND_2GHZ:
1638 if (sta->ht_cap.ht_supported) {
1639 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1640 phymode = MODE_11NG_HT40;
1641 else
1642 phymode = MODE_11NG_HT20;
91b12089 1643 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
5e3dd157 1644 phymode = MODE_11G;
91b12089
MK
1645 } else {
1646 phymode = MODE_11B;
5e3dd157
KV
1647 }
1648
1649 break;
1650 case IEEE80211_BAND_5GHZ:
7cc45e98
SM
1651 /*
1652 * Check VHT first.
1653 */
1654 if (sta->vht_cap.vht_supported) {
1655 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1656 phymode = MODE_11AC_VHT80;
1657 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1658 phymode = MODE_11AC_VHT40;
1659 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1660 phymode = MODE_11AC_VHT20;
1661 } else if (sta->ht_cap.ht_supported) {
5e3dd157
KV
1662 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1663 phymode = MODE_11NA_HT40;
1664 else
1665 phymode = MODE_11NA_HT20;
1666 } else {
1667 phymode = MODE_11A;
1668 }
1669
1670 break;
1671 default:
1672 break;
1673 }
1674
7aa7a72a 1675 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
38a1d47e 1676 sta->addr, ath10k_wmi_phymode_str(phymode));
60c3daa8 1677
5e3dd157
KV
1678 arg->peer_phymode = phymode;
1679 WARN_ON(phymode == MODE_UNKNOWN);
1680}
1681
b9ada65d 1682static int ath10k_peer_assoc_prepare(struct ath10k *ar,
590922a8 1683 struct ieee80211_vif *vif,
b9ada65d 1684 struct ieee80211_sta *sta,
b9ada65d 1685 struct wmi_peer_assoc_complete_arg *arg)
5e3dd157 1686{
548db54c
MK
1687 lockdep_assert_held(&ar->conf_mutex);
1688
b9ada65d 1689 memset(arg, 0, sizeof(*arg));
5e3dd157 1690
590922a8
MK
1691 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1692 ath10k_peer_assoc_h_crypto(ar, vif, arg);
b9ada65d
KV
1693 ath10k_peer_assoc_h_rates(ar, sta, arg);
1694 ath10k_peer_assoc_h_ht(ar, sta, arg);
1695 ath10k_peer_assoc_h_vht(ar, sta, arg);
590922a8
MK
1696 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1697 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
5e3dd157 1698
b9ada65d 1699 return 0;
5e3dd157
KV
1700}
1701
90046f50
MK
1702static const u32 ath10k_smps_map[] = {
1703 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
1704 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
1705 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
1706 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
1707};
1708
1709static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
1710 const u8 *addr,
1711 const struct ieee80211_sta_ht_cap *ht_cap)
1712{
1713 int smps;
1714
1715 if (!ht_cap->ht_supported)
1716 return 0;
1717
1718 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
1719 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
1720
1721 if (smps >= ARRAY_SIZE(ath10k_smps_map))
1722 return -EINVAL;
1723
1724 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
1725 WMI_PEER_SMPS_STATE,
1726 ath10k_smps_map[smps]);
1727}
1728
5e3dd157
KV
1729/* can be called only in mac80211 callbacks due to `key_count` usage */
1730static void ath10k_bss_assoc(struct ieee80211_hw *hw,
1731 struct ieee80211_vif *vif,
1732 struct ieee80211_bss_conf *bss_conf)
1733{
1734 struct ath10k *ar = hw->priv;
1735 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
90046f50 1736 struct ieee80211_sta_ht_cap ht_cap;
b9ada65d 1737 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
1738 struct ieee80211_sta *ap_sta;
1739 int ret;
1740
548db54c
MK
1741 lockdep_assert_held(&ar->conf_mutex);
1742
077efc8c
MK
1743 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
1744 arvif->vdev_id, arvif->bssid, arvif->aid);
1745
5e3dd157
KV
1746 rcu_read_lock();
1747
1748 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
1749 if (!ap_sta) {
7aa7a72a 1750 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
69244e56 1751 bss_conf->bssid, arvif->vdev_id);
5e3dd157
KV
1752 rcu_read_unlock();
1753 return;
1754 }
1755
90046f50
MK
1756 /* ap_sta must be accessed only within rcu section which must be left
1757 * before calling ath10k_setup_peer_smps() which might sleep. */
1758 ht_cap = ap_sta->ht_cap;
1759
590922a8 1760 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
5e3dd157 1761 if (ret) {
7aa7a72a 1762 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
69244e56 1763 bss_conf->bssid, arvif->vdev_id, ret);
5e3dd157
KV
1764 rcu_read_unlock();
1765 return;
1766 }
1767
1768 rcu_read_unlock();
1769
b9ada65d
KV
1770 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
1771 if (ret) {
7aa7a72a 1772 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
69244e56 1773 bss_conf->bssid, arvif->vdev_id, ret);
b9ada65d
KV
1774 return;
1775 }
1776
90046f50
MK
1777 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
1778 if (ret) {
7aa7a72a 1779 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
69244e56 1780 arvif->vdev_id, ret);
90046f50
MK
1781 return;
1782 }
1783
7aa7a72a 1784 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
1785 "mac vdev %d up (associated) bssid %pM aid %d\n",
1786 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
1787
077efc8c
MK
1788 WARN_ON(arvif->is_up);
1789
c930f744 1790 arvif->aid = bss_conf->aid;
b25f32cb 1791 ether_addr_copy(arvif->bssid, bss_conf->bssid);
c930f744
MK
1792
1793 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
1794 if (ret) {
7aa7a72a 1795 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
5e3dd157 1796 arvif->vdev_id, ret);
c930f744
MK
1797 return;
1798 }
1799
1800 arvif->is_up = true;
5e3dd157
KV
1801}
1802
5e3dd157
KV
1803static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
1804 struct ieee80211_vif *vif)
1805{
1806 struct ath10k *ar = hw->priv;
1807 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1808 int ret;
1809
548db54c
MK
1810 lockdep_assert_held(&ar->conf_mutex);
1811
077efc8c
MK
1812 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
1813 arvif->vdev_id, arvif->bssid);
60c3daa8 1814
5e3dd157 1815 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
077efc8c
MK
1816 if (ret)
1817 ath10k_warn(ar, "faield to down vdev %i: %d\n",
1818 arvif->vdev_id, ret);
5e3dd157 1819
cc4827b9 1820 arvif->def_wep_key_idx = 0;
c930f744 1821 arvif->is_up = false;
5e3dd157
KV
1822}
1823
590922a8
MK
1824static int ath10k_station_assoc(struct ath10k *ar,
1825 struct ieee80211_vif *vif,
1826 struct ieee80211_sta *sta,
1827 bool reassoc)
5e3dd157 1828{
590922a8 1829 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
b9ada65d 1830 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
1831 int ret = 0;
1832
548db54c
MK
1833 lockdep_assert_held(&ar->conf_mutex);
1834
590922a8 1835 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
b9ada65d 1836 if (ret) {
7aa7a72a 1837 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
69244e56 1838 sta->addr, arvif->vdev_id, ret);
b9ada65d
KV
1839 return ret;
1840 }
1841
44d6fa90 1842 peer_arg.peer_reassoc = reassoc;
b9ada65d 1843 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
5e3dd157 1844 if (ret) {
7aa7a72a 1845 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
69244e56 1846 sta->addr, arvif->vdev_id, ret);
5e3dd157
KV
1847 return ret;
1848 }
1849
b1ecde36
MK
1850 /* Re-assoc is run only to update supported rates for given station. It
1851 * doesn't make much sense to reconfigure the peer completely.
1852 */
1853 if (!reassoc) {
1854 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
1855 &sta->ht_cap);
e81bd104 1856 if (ret) {
b1ecde36 1857 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
e81bd104
MK
1858 arvif->vdev_id, ret);
1859 return ret;
1860 }
e81bd104 1861
b1ecde36
MK
1862 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
1863 if (ret) {
1864 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
1865 sta->addr, arvif->vdev_id, ret);
1866 return ret;
1867 }
5e3dd157 1868
b1ecde36
MK
1869 if (!sta->wme) {
1870 arvif->num_legacy_stations++;
1871 ret = ath10k_recalc_rtscts_prot(arvif);
1872 if (ret) {
1873 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
1874 arvif->vdev_id, ret);
1875 return ret;
1876 }
1877 }
1878
1879 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
1880 if (ret) {
1881 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
1882 arvif->vdev_id, ret);
1883 return ret;
1884 }
d3d3ff42
JD
1885 }
1886
5e3dd157
KV
1887 return ret;
1888}
1889
590922a8
MK
1890static int ath10k_station_disassoc(struct ath10k *ar,
1891 struct ieee80211_vif *vif,
5e3dd157
KV
1892 struct ieee80211_sta *sta)
1893{
590922a8 1894 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5e3dd157
KV
1895 int ret = 0;
1896
548db54c
MK
1897 lockdep_assert_held(&ar->conf_mutex);
1898
e81bd104
MK
1899 if (!sta->wme) {
1900 arvif->num_legacy_stations--;
1901 ret = ath10k_recalc_rtscts_prot(arvif);
1902 if (ret) {
7aa7a72a 1903 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
e81bd104
MK
1904 arvif->vdev_id, ret);
1905 return ret;
1906 }
1907 }
1908
5e3dd157
KV
1909 ret = ath10k_clear_peer_keys(arvif, sta->addr);
1910 if (ret) {
7aa7a72a 1911 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
69244e56 1912 arvif->vdev_id, ret);
5e3dd157
KV
1913 return ret;
1914 }
1915
1916 return ret;
1917}
1918
1919/**************/
1920/* Regulatory */
1921/**************/
1922
1923static int ath10k_update_channel_list(struct ath10k *ar)
1924{
1925 struct ieee80211_hw *hw = ar->hw;
1926 struct ieee80211_supported_band **bands;
1927 enum ieee80211_band band;
1928 struct ieee80211_channel *channel;
1929 struct wmi_scan_chan_list_arg arg = {0};
1930 struct wmi_channel_arg *ch;
1931 bool passive;
1932 int len;
1933 int ret;
1934 int i;
1935
548db54c
MK
1936 lockdep_assert_held(&ar->conf_mutex);
1937
5e3dd157
KV
1938 bands = hw->wiphy->bands;
1939 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1940 if (!bands[band])
1941 continue;
1942
1943 for (i = 0; i < bands[band]->n_channels; i++) {
1944 if (bands[band]->channels[i].flags &
1945 IEEE80211_CHAN_DISABLED)
1946 continue;
1947
1948 arg.n_channels++;
1949 }
1950 }
1951
1952 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
1953 arg.channels = kzalloc(len, GFP_KERNEL);
1954 if (!arg.channels)
1955 return -ENOMEM;
1956
1957 ch = arg.channels;
1958 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
1959 if (!bands[band])
1960 continue;
1961
1962 for (i = 0; i < bands[band]->n_channels; i++) {
1963 channel = &bands[band]->channels[i];
1964
1965 if (channel->flags & IEEE80211_CHAN_DISABLED)
1966 continue;
1967
1968 ch->allow_ht = true;
1969
1970 /* FIXME: when should we really allow VHT? */
1971 ch->allow_vht = true;
1972
1973 ch->allow_ibss =
8fe02e16 1974 !(channel->flags & IEEE80211_CHAN_NO_IR);
5e3dd157
KV
1975
1976 ch->ht40plus =
1977 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
1978
e8a50f8b
MP
1979 ch->chan_radar =
1980 !!(channel->flags & IEEE80211_CHAN_RADAR);
1981
8fe02e16 1982 passive = channel->flags & IEEE80211_CHAN_NO_IR;
5e3dd157
KV
1983 ch->passive = passive;
1984
1985 ch->freq = channel->center_freq;
2d66721c 1986 ch->band_center_freq1 = channel->center_freq;
89c5c843 1987 ch->min_power = 0;
02256930
MK
1988 ch->max_power = channel->max_power * 2;
1989 ch->max_reg_power = channel->max_reg_power * 2;
1990 ch->max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157
KV
1991 ch->reg_class_id = 0; /* FIXME */
1992
1993 /* FIXME: why use only legacy modes, why not any
1994 * HT/VHT modes? Would that even make any
1995 * difference? */
1996 if (channel->band == IEEE80211_BAND_2GHZ)
1997 ch->mode = MODE_11G;
1998 else
1999 ch->mode = MODE_11A;
2000
2001 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2002 continue;
2003
7aa7a72a 2004 ath10k_dbg(ar, ATH10K_DBG_WMI,
60c3daa8
KV
2005 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2006 ch - arg.channels, arg.n_channels,
5e3dd157
KV
2007 ch->freq, ch->max_power, ch->max_reg_power,
2008 ch->max_antenna_gain, ch->mode);
2009
2010 ch++;
2011 }
2012 }
2013
2014 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2015 kfree(arg.channels);
2016
2017 return ret;
2018}
2019
821af6ae
MP
2020static enum wmi_dfs_region
2021ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2022{
2023 switch (dfs_region) {
2024 case NL80211_DFS_UNSET:
2025 return WMI_UNINIT_DFS_DOMAIN;
2026 case NL80211_DFS_FCC:
2027 return WMI_FCC_DFS_DOMAIN;
2028 case NL80211_DFS_ETSI:
2029 return WMI_ETSI_DFS_DOMAIN;
2030 case NL80211_DFS_JP:
2031 return WMI_MKK4_DFS_DOMAIN;
2032 }
2033 return WMI_UNINIT_DFS_DOMAIN;
2034}
2035
f7843d7f 2036static void ath10k_regd_update(struct ath10k *ar)
5e3dd157 2037{
5e3dd157 2038 struct reg_dmn_pair_mapping *regpair;
5e3dd157 2039 int ret;
821af6ae
MP
2040 enum wmi_dfs_region wmi_dfs_reg;
2041 enum nl80211_dfs_regions nl_dfs_reg;
5e3dd157 2042
f7843d7f 2043 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
2044
2045 ret = ath10k_update_channel_list(ar);
2046 if (ret)
7aa7a72a 2047 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
5e3dd157
KV
2048
2049 regpair = ar->ath_common.regulatory.regpair;
f7843d7f 2050
821af6ae
MP
2051 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2052 nl_dfs_reg = ar->dfs_detector->region;
2053 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2054 } else {
2055 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2056 }
2057
5e3dd157
KV
2058 /* Target allows setting up per-band regdomain but ath_common provides
2059 * a combined one only */
2060 ret = ath10k_wmi_pdev_set_regdomain(ar,
ef8c0017
KV
2061 regpair->reg_domain,
2062 regpair->reg_domain, /* 2ghz */
2063 regpair->reg_domain, /* 5ghz */
5e3dd157 2064 regpair->reg_2ghz_ctl,
821af6ae
MP
2065 regpair->reg_5ghz_ctl,
2066 wmi_dfs_reg);
5e3dd157 2067 if (ret)
7aa7a72a 2068 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
f7843d7f 2069}
548db54c 2070
f7843d7f
MK
2071static void ath10k_reg_notifier(struct wiphy *wiphy,
2072 struct regulatory_request *request)
2073{
2074 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2075 struct ath10k *ar = hw->priv;
9702c686 2076 bool result;
f7843d7f
MK
2077
2078 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2079
9702c686 2080 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
7aa7a72a 2081 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
9702c686
JD
2082 request->dfs_region);
2083 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2084 request->dfs_region);
2085 if (!result)
7aa7a72a 2086 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
9702c686
JD
2087 request->dfs_region);
2088 }
2089
f7843d7f
MK
2090 mutex_lock(&ar->conf_mutex);
2091 if (ar->state == ATH10K_STATE_ON)
2092 ath10k_regd_update(ar);
548db54c 2093 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
2094}
2095
2096/***************/
2097/* TX handlers */
2098/***************/
2099
42c3aa6f
MK
2100static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2101{
2102 if (ieee80211_is_mgmt(hdr->frame_control))
2103 return HTT_DATA_TX_EXT_TID_MGMT;
2104
2105 if (!ieee80211_is_data_qos(hdr->frame_control))
2106 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2107
2108 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2109 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2110
2111 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2112}
2113
2b37c295 2114static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
ddb6ad77 2115{
2b37c295
MK
2116 if (vif)
2117 return ath10k_vif_to_arvif(vif)->vdev_id;
ddb6ad77 2118
1bbc0975 2119 if (ar->monitor_started)
ddb6ad77
MK
2120 return ar->monitor_vdev_id;
2121
7aa7a72a 2122 ath10k_warn(ar, "failed to resolve vdev id\n");
ddb6ad77
MK
2123 return 0;
2124}
2125
4b604558
MK
2126/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2127 * Control in the header.
5e3dd157 2128 */
4b604558 2129static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
5e3dd157
KV
2130{
2131 struct ieee80211_hdr *hdr = (void *)skb->data;
c21c64d1 2132 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
5e3dd157
KV
2133 u8 *qos_ctl;
2134
2135 if (!ieee80211_is_data_qos(hdr->frame_control))
2136 return;
2137
2138 qos_ctl = ieee80211_get_qos_ctl(hdr);
ba0ccd7a
MK
2139 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2140 skb->data, (void *)qos_ctl - (void *)skb->data);
2141 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
c21c64d1
MK
2142
2143 /* Fw/Hw generates a corrupted QoS Control Field for QoS NullFunc
2144 * frames. Powersave is handled by the fw/hw so QoS NyllFunc frames are
2145 * used only for CQM purposes (e.g. hostapd station keepalive ping) so
2146 * it is safe to downgrade to NullFunc.
2147 */
2148 if (ieee80211_is_qos_nullfunc(hdr->frame_control)) {
2149 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2150 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2151 }
5e3dd157
KV
2152}
2153
cc4827b9
MK
2154static void ath10k_tx_wep_key_work(struct work_struct *work)
2155{
2156 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2157 wep_key_work);
7aa7a72a 2158 struct ath10k *ar = arvif->ar;
cc4827b9
MK
2159 int ret, keyidx = arvif->def_wep_key_newidx;
2160
911e6c0d
MK
2161 mutex_lock(&arvif->ar->conf_mutex);
2162
2163 if (arvif->ar->state != ATH10K_STATE_ON)
2164 goto unlock;
2165
cc4827b9 2166 if (arvif->def_wep_key_idx == keyidx)
911e6c0d 2167 goto unlock;
cc4827b9 2168
7aa7a72a 2169 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
cc4827b9
MK
2170 arvif->vdev_id, keyidx);
2171
2172 ret = ath10k_wmi_vdev_set_param(arvif->ar,
2173 arvif->vdev_id,
2174 arvif->ar->wmi.vdev_param->def_keyid,
2175 keyidx);
2176 if (ret) {
7aa7a72a 2177 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
be6546fc
KV
2178 arvif->vdev_id,
2179 ret);
911e6c0d 2180 goto unlock;
cc4827b9
MK
2181 }
2182
2183 arvif->def_wep_key_idx = keyidx;
911e6c0d
MK
2184
2185unlock:
2186 mutex_unlock(&arvif->ar->conf_mutex);
cc4827b9
MK
2187}
2188
4b604558
MK
2189static void ath10k_tx_h_update_wep_key(struct ieee80211_vif *vif,
2190 struct ieee80211_key_conf *key,
2191 struct sk_buff *skb)
5e3dd157 2192{
5e3dd157
KV
2193 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2194 struct ath10k *ar = arvif->ar;
2195 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e3dd157 2196
5e3dd157
KV
2197 if (!ieee80211_has_protected(hdr->frame_control))
2198 return;
2199
2200 if (!key)
2201 return;
2202
2203 if (key->cipher != WLAN_CIPHER_SUITE_WEP40 &&
2204 key->cipher != WLAN_CIPHER_SUITE_WEP104)
2205 return;
2206
cc4827b9 2207 if (key->keyidx == arvif->def_wep_key_idx)
5e3dd157 2208 return;
5e3dd157 2209
cc4827b9
MK
2210 /* FIXME: Most likely a few frames will be TXed with an old key. Simply
2211 * queueing frames until key index is updated is not an option because
2212 * sk_buff may need more processing to be done, e.g. offchannel */
2213 arvif->def_wep_key_newidx = key->keyidx;
2214 ieee80211_queue_work(ar->hw, &arvif->wep_key_work);
5e3dd157
KV
2215}
2216
4b604558
MK
2217static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2218 struct ieee80211_vif *vif,
2219 struct sk_buff *skb)
5e3dd157
KV
2220{
2221 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e3dd157
KV
2222 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2223
2224 /* This is case only for P2P_GO */
2225 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2226 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2227 return;
2228
2229 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2230 spin_lock_bh(&ar->data_lock);
2231 if (arvif->u.ap.noa_data)
2232 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2233 GFP_ATOMIC))
2234 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2235 arvif->u.ap.noa_data,
2236 arvif->u.ap.noa_len);
2237 spin_unlock_bh(&ar->data_lock);
2238 }
2239}
2240
8d6d3624
MK
2241static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2242{
2243 /* FIXME: Not really sure since when the behaviour changed. At some
2244 * point new firmware stopped requiring creation of peer entries for
2245 * offchannel tx (and actually creating them causes issues with wmi-htc
2246 * tx credit replenishment and reliability). Assuming it's at least 3.4
2247 * because that's when the `freq` was introduced to TX_FRM HTT command.
2248 */
2249 return !(ar->htt.target_version_major >= 3 &&
2250 ar->htt.target_version_minor >= 4);
2251}
2252
5e3dd157
KV
2253static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2254{
2255 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e00d31a 2256 int ret = 0;
5e3dd157 2257
961d4c38
MK
2258 if (ar->htt.target_version_major >= 3) {
2259 /* Since HTT 3.0 there is no separate mgmt tx command */
2260 ret = ath10k_htt_tx(&ar->htt, skb);
2261 goto exit;
2262 }
2263
5e00d31a
BM
2264 if (ieee80211_is_mgmt(hdr->frame_control)) {
2265 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2266 ar->fw_features)) {
2267 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2268 ATH10K_MAX_NUM_MGMT_PENDING) {
7aa7a72a 2269 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
5e00d31a
BM
2270 ret = -EBUSY;
2271 goto exit;
2272 }
2273
2274 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2275 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2276 } else {
2277 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2278 }
2279 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2280 ar->fw_features) &&
2281 ieee80211_is_nullfunc(hdr->frame_control)) {
5e3dd157
KV
2282 /* FW does not report tx status properly for NullFunc frames
2283 * unless they are sent through mgmt tx path. mac80211 sends
5e00d31a
BM
2284 * those frames when it detects link/beacon loss and depends
2285 * on the tx status to be correct. */
edb8236d 2286 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
5e00d31a 2287 } else {
edb8236d 2288 ret = ath10k_htt_tx(&ar->htt, skb);
5e00d31a 2289 }
5e3dd157 2290
961d4c38 2291exit:
5e3dd157 2292 if (ret) {
7aa7a72a
MK
2293 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2294 ret);
5e3dd157
KV
2295 ieee80211_free_txskb(ar->hw, skb);
2296 }
2297}
2298
2299void ath10k_offchan_tx_purge(struct ath10k *ar)
2300{
2301 struct sk_buff *skb;
2302
2303 for (;;) {
2304 skb = skb_dequeue(&ar->offchan_tx_queue);
2305 if (!skb)
2306 break;
2307
2308 ieee80211_free_txskb(ar->hw, skb);
2309 }
2310}
2311
2312void ath10k_offchan_tx_work(struct work_struct *work)
2313{
2314 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2315 struct ath10k_peer *peer;
2316 struct ieee80211_hdr *hdr;
2317 struct sk_buff *skb;
2318 const u8 *peer_addr;
2319 int vdev_id;
2320 int ret;
2321
2322 /* FW requirement: We must create a peer before FW will send out
2323 * an offchannel frame. Otherwise the frame will be stuck and
2324 * never transmitted. We delete the peer upon tx completion.
2325 * It is unlikely that a peer for offchannel tx will already be
2326 * present. However it may be in some rare cases so account for that.
2327 * Otherwise we might remove a legitimate peer and break stuff. */
2328
2329 for (;;) {
2330 skb = skb_dequeue(&ar->offchan_tx_queue);
2331 if (!skb)
2332 break;
2333
2334 mutex_lock(&ar->conf_mutex);
2335
7aa7a72a 2336 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
5e3dd157
KV
2337 skb);
2338
2339 hdr = (struct ieee80211_hdr *)skb->data;
2340 peer_addr = ieee80211_get_DA(hdr);
5e00d31a 2341 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
5e3dd157
KV
2342
2343 spin_lock_bh(&ar->data_lock);
2344 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2345 spin_unlock_bh(&ar->data_lock);
2346
2347 if (peer)
60c3daa8 2348 /* FIXME: should this use ath10k_warn()? */
7aa7a72a 2349 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
5e3dd157
KV
2350 peer_addr, vdev_id);
2351
2352 if (!peer) {
2353 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2354 if (ret)
7aa7a72a 2355 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
5e3dd157
KV
2356 peer_addr, vdev_id, ret);
2357 }
2358
2359 spin_lock_bh(&ar->data_lock);
16735d02 2360 reinit_completion(&ar->offchan_tx_completed);
5e3dd157
KV
2361 ar->offchan_tx_skb = skb;
2362 spin_unlock_bh(&ar->data_lock);
2363
2364 ath10k_tx_htt(ar, skb);
2365
2366 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2367 3 * HZ);
38e2a644 2368 if (ret == 0)
7aa7a72a 2369 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
5e3dd157
KV
2370 skb);
2371
2372 if (!peer) {
2373 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2374 if (ret)
7aa7a72a 2375 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
5e3dd157
KV
2376 peer_addr, vdev_id, ret);
2377 }
2378
2379 mutex_unlock(&ar->conf_mutex);
2380 }
2381}
2382
5e00d31a
BM
2383void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2384{
2385 struct sk_buff *skb;
2386
2387 for (;;) {
2388 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2389 if (!skb)
2390 break;
2391
2392 ieee80211_free_txskb(ar->hw, skb);
2393 }
2394}
2395
2396void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2397{
2398 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2399 struct sk_buff *skb;
2400 int ret;
2401
2402 for (;;) {
2403 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2404 if (!skb)
2405 break;
2406
2407 ret = ath10k_wmi_mgmt_tx(ar, skb);
5fb5e41f 2408 if (ret) {
7aa7a72a 2409 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
be6546fc 2410 ret);
5fb5e41f
MK
2411 ieee80211_free_txskb(ar->hw, skb);
2412 }
5e00d31a
BM
2413 }
2414}
2415
5e3dd157
KV
2416/************/
2417/* Scanning */
2418/************/
2419
5c81c7fd 2420void __ath10k_scan_finish(struct ath10k *ar)
5e3dd157 2421{
5c81c7fd 2422 lockdep_assert_held(&ar->data_lock);
5e3dd157 2423
5c81c7fd
MK
2424 switch (ar->scan.state) {
2425 case ATH10K_SCAN_IDLE:
2426 break;
2427 case ATH10K_SCAN_RUNNING:
5c81c7fd
MK
2428 if (ar->scan.is_roc)
2429 ieee80211_remain_on_channel_expired(ar->hw);
7305d3e0
MK
2430 case ATH10K_SCAN_ABORTING:
2431 if (!ar->scan.is_roc)
5c81c7fd
MK
2432 ieee80211_scan_completed(ar->hw,
2433 (ar->scan.state ==
2434 ATH10K_SCAN_ABORTING));
2435 /* fall through */
2436 case ATH10K_SCAN_STARTING:
2437 ar->scan.state = ATH10K_SCAN_IDLE;
2438 ar->scan_channel = NULL;
2439 ath10k_offchan_tx_purge(ar);
2440 cancel_delayed_work(&ar->scan.timeout);
2441 complete_all(&ar->scan.completed);
2442 break;
5e3dd157 2443 }
5c81c7fd 2444}
5e3dd157 2445
5c81c7fd
MK
2446void ath10k_scan_finish(struct ath10k *ar)
2447{
2448 spin_lock_bh(&ar->data_lock);
2449 __ath10k_scan_finish(ar);
5e3dd157
KV
2450 spin_unlock_bh(&ar->data_lock);
2451}
2452
5c81c7fd 2453static int ath10k_scan_stop(struct ath10k *ar)
5e3dd157
KV
2454{
2455 struct wmi_stop_scan_arg arg = {
2456 .req_id = 1, /* FIXME */
2457 .req_type = WMI_SCAN_STOP_ONE,
2458 .u.scan_id = ATH10K_SCAN_ID,
2459 };
2460 int ret;
2461
2462 lockdep_assert_held(&ar->conf_mutex);
2463
5e3dd157
KV
2464 ret = ath10k_wmi_stop_scan(ar, &arg);
2465 if (ret) {
7aa7a72a 2466 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
5c81c7fd 2467 goto out;
5e3dd157
KV
2468 }
2469
5e3dd157 2470 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
5c81c7fd 2471 if (ret == 0) {
7aa7a72a 2472 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
5c81c7fd
MK
2473 ret = -ETIMEDOUT;
2474 } else if (ret > 0) {
2475 ret = 0;
2476 }
5e3dd157 2477
5c81c7fd
MK
2478out:
2479 /* Scan state should be updated upon scan completion but in case
2480 * firmware fails to deliver the event (for whatever reason) it is
2481 * desired to clean up scan state anyway. Firmware may have just
2482 * dropped the scan completion event delivery due to transport pipe
2483 * being overflown with data and/or it can recover on its own before
2484 * next scan request is submitted.
2485 */
2486 spin_lock_bh(&ar->data_lock);
2487 if (ar->scan.state != ATH10K_SCAN_IDLE)
2488 __ath10k_scan_finish(ar);
2489 spin_unlock_bh(&ar->data_lock);
2490
2491 return ret;
2492}
2493
2494static void ath10k_scan_abort(struct ath10k *ar)
2495{
2496 int ret;
2497
2498 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
2499
2500 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
2501
2502 switch (ar->scan.state) {
2503 case ATH10K_SCAN_IDLE:
2504 /* This can happen if timeout worker kicked in and called
2505 * abortion while scan completion was being processed.
2506 */
2507 break;
2508 case ATH10K_SCAN_STARTING:
2509 case ATH10K_SCAN_ABORTING:
7aa7a72a 2510 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
5c81c7fd
MK
2511 ath10k_scan_state_str(ar->scan.state),
2512 ar->scan.state);
2513 break;
2514 case ATH10K_SCAN_RUNNING:
2515 ar->scan.state = ATH10K_SCAN_ABORTING;
2516 spin_unlock_bh(&ar->data_lock);
2517
2518 ret = ath10k_scan_stop(ar);
2519 if (ret)
7aa7a72a 2520 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
5c81c7fd
MK
2521
2522 spin_lock_bh(&ar->data_lock);
2523 break;
5e3dd157 2524 }
5c81c7fd 2525
5e3dd157 2526 spin_unlock_bh(&ar->data_lock);
5c81c7fd 2527}
5e3dd157 2528
5c81c7fd
MK
2529void ath10k_scan_timeout_work(struct work_struct *work)
2530{
2531 struct ath10k *ar = container_of(work, struct ath10k,
2532 scan.timeout.work);
2533
2534 mutex_lock(&ar->conf_mutex);
2535 ath10k_scan_abort(ar);
2536 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
2537}
2538
2539static int ath10k_start_scan(struct ath10k *ar,
2540 const struct wmi_start_scan_arg *arg)
2541{
2542 int ret;
2543
2544 lockdep_assert_held(&ar->conf_mutex);
2545
2546 ret = ath10k_wmi_start_scan(ar, arg);
2547 if (ret)
2548 return ret;
2549
5e3dd157
KV
2550 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2551 if (ret == 0) {
5c81c7fd
MK
2552 ret = ath10k_scan_stop(ar);
2553 if (ret)
7aa7a72a 2554 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd
MK
2555
2556 return -ETIMEDOUT;
5e3dd157
KV
2557 }
2558
5c81c7fd
MK
2559 /* Add a 200ms margin to account for event/command processing */
2560 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2561 msecs_to_jiffies(arg->max_scan_time+200));
5e3dd157
KV
2562 return 0;
2563}
2564
2565/**********************/
2566/* mac80211 callbacks */
2567/**********************/
2568
2569static void ath10k_tx(struct ieee80211_hw *hw,
2570 struct ieee80211_tx_control *control,
2571 struct sk_buff *skb)
2572{
4b604558 2573 struct ath10k *ar = hw->priv;
5e3dd157 2574 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4b604558
MK
2575 struct ieee80211_vif *vif = info->control.vif;
2576 struct ieee80211_key_conf *key = info->control.hw_key;
5e3dd157 2577 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e3dd157
KV
2578
2579 /* We should disable CCK RATE due to P2P */
2580 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
7aa7a72a 2581 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
5e3dd157 2582
4b604558
MK
2583 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2584 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2b37c295 2585 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
5e3dd157 2586
cf84bd4d 2587 /* it makes no sense to process injected frames like that */
4b604558
MK
2588 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2589 ath10k_tx_h_nwifi(hw, skb);
2590 ath10k_tx_h_update_wep_key(vif, key, skb);
2591 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2592 ath10k_tx_h_seq_no(vif, skb);
cf84bd4d 2593 }
5e3dd157 2594
5e3dd157
KV
2595 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2596 spin_lock_bh(&ar->data_lock);
8d6d3624 2597 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
5e00d31a 2598 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
5e3dd157
KV
2599 spin_unlock_bh(&ar->data_lock);
2600
8d6d3624
MK
2601 if (ath10k_mac_need_offchan_tx_work(ar)) {
2602 ATH10K_SKB_CB(skb)->htt.freq = 0;
2603 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
5e3dd157 2604
8d6d3624
MK
2605 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2606 skb);
2607
2608 skb_queue_tail(&ar->offchan_tx_queue, skb);
2609 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2610 return;
2611 }
5e3dd157
KV
2612 }
2613
2614 ath10k_tx_htt(ar, skb);
2615}
2616
bca7bafb 2617/* Must not be called with conf_mutex held as workers can use that also. */
7962b0d8 2618void ath10k_drain_tx(struct ath10k *ar)
bca7bafb
MK
2619{
2620 /* make sure rcu-protected mac80211 tx path itself is drained */
2621 synchronize_net();
2622
2623 ath10k_offchan_tx_purge(ar);
2624 ath10k_mgmt_over_wmi_tx_purge(ar);
2625
2626 cancel_work_sync(&ar->offchan_tx_work);
2627 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2628}
2629
affd3217 2630void ath10k_halt(struct ath10k *ar)
818bdd16 2631{
d9bc4b9b
MK
2632 struct ath10k_vif *arvif;
2633
818bdd16
MK
2634 lockdep_assert_held(&ar->conf_mutex);
2635
1933747f
MK
2636 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2637 ar->filter_flags = 0;
2638 ar->monitor = false;
2639
2640 if (ar->monitor_started)
1bbc0975 2641 ath10k_monitor_stop(ar);
1933747f
MK
2642
2643 ar->monitor_started = false;
1bbc0975 2644
5c81c7fd 2645 ath10k_scan_finish(ar);
818bdd16
MK
2646 ath10k_peer_cleanup_all(ar);
2647 ath10k_core_stop(ar);
2648 ath10k_hif_power_down(ar);
2649
2650 spin_lock_bh(&ar->data_lock);
64badcb6
MK
2651 list_for_each_entry(arvif, &ar->arvifs, list)
2652 ath10k_mac_vif_beacon_cleanup(arvif);
818bdd16
MK
2653 spin_unlock_bh(&ar->data_lock);
2654}
2655
46acf7bb
BG
2656static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
2657{
2658 struct ath10k *ar = hw->priv;
2659
2660 mutex_lock(&ar->conf_mutex);
2661
2662 if (ar->cfg_tx_chainmask) {
2663 *tx_ant = ar->cfg_tx_chainmask;
2664 *rx_ant = ar->cfg_rx_chainmask;
2665 } else {
2666 *tx_ant = ar->supp_tx_chainmask;
2667 *rx_ant = ar->supp_rx_chainmask;
2668 }
2669
2670 mutex_unlock(&ar->conf_mutex);
2671
2672 return 0;
2673}
2674
5572a95b
BG
2675static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
2676{
2677 /* It is not clear that allowing gaps in chainmask
2678 * is helpful. Probably it will not do what user
2679 * is hoping for, so warn in that case.
2680 */
2681 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
2682 return;
2683
2684 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
2685 dbg, cm);
2686}
2687
46acf7bb
BG
2688static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
2689{
2690 int ret;
2691
2692 lockdep_assert_held(&ar->conf_mutex);
2693
5572a95b
BG
2694 ath10k_check_chain_mask(ar, tx_ant, "tx");
2695 ath10k_check_chain_mask(ar, rx_ant, "rx");
2696
46acf7bb
BG
2697 ar->cfg_tx_chainmask = tx_ant;
2698 ar->cfg_rx_chainmask = rx_ant;
2699
2700 if ((ar->state != ATH10K_STATE_ON) &&
2701 (ar->state != ATH10K_STATE_RESTARTED))
2702 return 0;
2703
2704 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
2705 tx_ant);
2706 if (ret) {
7aa7a72a 2707 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
2708 ret, tx_ant);
2709 return ret;
2710 }
2711
2712 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
2713 rx_ant);
2714 if (ret) {
7aa7a72a 2715 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
2716 ret, rx_ant);
2717 return ret;
2718 }
2719
2720 return 0;
2721}
2722
2723static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
2724{
2725 struct ath10k *ar = hw->priv;
2726 int ret;
2727
2728 mutex_lock(&ar->conf_mutex);
2729 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
2730 mutex_unlock(&ar->conf_mutex);
2731 return ret;
2732}
2733
5e3dd157
KV
2734static int ath10k_start(struct ieee80211_hw *hw)
2735{
2736 struct ath10k *ar = hw->priv;
818bdd16 2737 int ret = 0;
5e3dd157 2738
bca7bafb
MK
2739 /*
2740 * This makes sense only when restarting hw. It is harmless to call
2741 * uncoditionally. This is necessary to make sure no HTT/WMI tx
2742 * commands will be submitted while restarting.
2743 */
2744 ath10k_drain_tx(ar);
2745
548db54c
MK
2746 mutex_lock(&ar->conf_mutex);
2747
c5058f5b
MK
2748 switch (ar->state) {
2749 case ATH10K_STATE_OFF:
2750 ar->state = ATH10K_STATE_ON;
2751 break;
2752 case ATH10K_STATE_RESTARTING:
2753 ath10k_halt(ar);
2754 ar->state = ATH10K_STATE_RESTARTED;
2755 break;
2756 case ATH10K_STATE_ON:
2757 case ATH10K_STATE_RESTARTED:
2758 case ATH10K_STATE_WEDGED:
2759 WARN_ON(1);
818bdd16 2760 ret = -EINVAL;
ae254433 2761 goto err;
43d2a30f
KV
2762 case ATH10K_STATE_UTF:
2763 ret = -EBUSY;
2764 goto err;
818bdd16
MK
2765 }
2766
2767 ret = ath10k_hif_power_up(ar);
2768 if (ret) {
7aa7a72a 2769 ath10k_err(ar, "Could not init hif: %d\n", ret);
ae254433 2770 goto err_off;
818bdd16
MK
2771 }
2772
43d2a30f 2773 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
818bdd16 2774 if (ret) {
7aa7a72a 2775 ath10k_err(ar, "Could not init core: %d\n", ret);
ae254433 2776 goto err_power_down;
818bdd16
MK
2777 }
2778
226a339b 2779 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
ae254433 2780 if (ret) {
7aa7a72a 2781 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
ae254433
MK
2782 goto err_core_stop;
2783 }
5e3dd157 2784
c4dd0d01 2785 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
ae254433 2786 if (ret) {
7aa7a72a 2787 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
ae254433
MK
2788 goto err_core_stop;
2789 }
5e3dd157 2790
46acf7bb
BG
2791 if (ar->cfg_tx_chainmask)
2792 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
2793 ar->cfg_rx_chainmask);
2794
ab6258ed
MP
2795 /*
2796 * By default FW set ARP frames ac to voice (6). In that case ARP
2797 * exchange is not working properly for UAPSD enabled AP. ARP requests
2798 * which arrives with access category 0 are processed by network stack
2799 * and send back with access category 0, but FW changes access category
2800 * to 6. Set ARP frames access category to best effort (0) solves
2801 * this problem.
2802 */
2803
2804 ret = ath10k_wmi_pdev_set_param(ar,
2805 ar->wmi.pdev_param->arp_ac_override, 0);
2806 if (ret) {
7aa7a72a 2807 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
ab6258ed 2808 ret);
ae254433 2809 goto err_core_stop;
ab6258ed
MP
2810 }
2811
d650097b 2812 ar->num_started_vdevs = 0;
f7843d7f
MK
2813 ath10k_regd_update(ar);
2814
855aed12
SW
2815 ath10k_spectral_start(ar);
2816
ae254433
MK
2817 mutex_unlock(&ar->conf_mutex);
2818 return 0;
2819
2820err_core_stop:
2821 ath10k_core_stop(ar);
2822
2823err_power_down:
2824 ath10k_hif_power_down(ar);
2825
2826err_off:
2827 ar->state = ATH10K_STATE_OFF;
2828
2829err:
548db54c 2830 mutex_unlock(&ar->conf_mutex);
c60bdd83 2831 return ret;
5e3dd157
KV
2832}
2833
2834static void ath10k_stop(struct ieee80211_hw *hw)
2835{
2836 struct ath10k *ar = hw->priv;
2837
bca7bafb
MK
2838 ath10k_drain_tx(ar);
2839
548db54c 2840 mutex_lock(&ar->conf_mutex);
c5058f5b 2841 if (ar->state != ATH10K_STATE_OFF) {
818bdd16 2842 ath10k_halt(ar);
c5058f5b
MK
2843 ar->state = ATH10K_STATE_OFF;
2844 }
548db54c
MK
2845 mutex_unlock(&ar->conf_mutex);
2846
5c81c7fd 2847 cancel_delayed_work_sync(&ar->scan.timeout);
affd3217 2848 cancel_work_sync(&ar->restart_work);
5e3dd157
KV
2849}
2850
ad088bfa 2851static int ath10k_config_ps(struct ath10k *ar)
5e3dd157 2852{
ad088bfa
MK
2853 struct ath10k_vif *arvif;
2854 int ret = 0;
affd3217
MK
2855
2856 lockdep_assert_held(&ar->conf_mutex);
2857
ad088bfa
MK
2858 list_for_each_entry(arvif, &ar->arvifs, list) {
2859 ret = ath10k_mac_vif_setup_ps(arvif);
2860 if (ret) {
7aa7a72a 2861 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
ad088bfa
MK
2862 break;
2863 }
2864 }
affd3217 2865
ad088bfa 2866 return ret;
affd3217
MK
2867}
2868
c930f744
MK
2869static const char *chandef_get_width(enum nl80211_chan_width width)
2870{
2871 switch (width) {
2872 case NL80211_CHAN_WIDTH_20_NOHT:
2873 return "20 (noht)";
2874 case NL80211_CHAN_WIDTH_20:
2875 return "20";
2876 case NL80211_CHAN_WIDTH_40:
2877 return "40";
2878 case NL80211_CHAN_WIDTH_80:
2879 return "80";
2880 case NL80211_CHAN_WIDTH_80P80:
2881 return "80+80";
2882 case NL80211_CHAN_WIDTH_160:
2883 return "160";
2884 case NL80211_CHAN_WIDTH_5:
2885 return "5";
2886 case NL80211_CHAN_WIDTH_10:
2887 return "10";
2888 }
2889 return "?";
2890}
2891
2892static void ath10k_config_chan(struct ath10k *ar)
2893{
2894 struct ath10k_vif *arvif;
c930f744
MK
2895 int ret;
2896
2897 lockdep_assert_held(&ar->conf_mutex);
2898
7aa7a72a 2899 ath10k_dbg(ar, ATH10K_DBG_MAC,
c930f744
MK
2900 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
2901 ar->chandef.chan->center_freq,
2902 ar->chandef.center_freq1,
2903 ar->chandef.center_freq2,
2904 chandef_get_width(ar->chandef.width));
2905
2906 /* First stop monitor interface. Some FW versions crash if there's a
2907 * lone monitor interface. */
1bbc0975 2908 if (ar->monitor_started)
1933747f 2909 ath10k_monitor_stop(ar);
c930f744
MK
2910
2911 list_for_each_entry(arvif, &ar->arvifs, list) {
2912 if (!arvif->is_started)
2913 continue;
2914
dc55e307
MK
2915 if (!arvif->is_up)
2916 continue;
2917
c930f744
MK
2918 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2919 continue;
2920
dc55e307 2921 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
c930f744 2922 if (ret) {
7aa7a72a 2923 ath10k_warn(ar, "failed to down vdev %d: %d\n",
c930f744
MK
2924 arvif->vdev_id, ret);
2925 continue;
2926 }
2927 }
2928
dc55e307 2929 /* all vdevs are downed now - attempt to restart and re-up them */
c930f744
MK
2930
2931 list_for_each_entry(arvif, &ar->arvifs, list) {
2932 if (!arvif->is_started)
2933 continue;
2934
2935 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
2936 continue;
2937
dc55e307 2938 ret = ath10k_vdev_restart(arvif);
c930f744 2939 if (ret) {
7aa7a72a 2940 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
c930f744
MK
2941 arvif->vdev_id, ret);
2942 continue;
2943 }
2944
2945 if (!arvif->is_up)
2946 continue;
2947
2948 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
2949 arvif->bssid);
2950 if (ret) {
7aa7a72a 2951 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
c930f744
MK
2952 arvif->vdev_id, ret);
2953 continue;
2954 }
2955 }
2956
1933747f 2957 ath10k_monitor_recalc(ar);
c930f744
MK
2958}
2959
7d9d5587
MK
2960static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2961{
2962 int ret;
2963 u32 param;
2964
2965 lockdep_assert_held(&ar->conf_mutex);
2966
2967 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
2968
2969 param = ar->wmi.pdev_param->txpower_limit2g;
2970 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2971 if (ret) {
2972 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2973 txpower, ret);
2974 return ret;
2975 }
2976
2977 param = ar->wmi.pdev_param->txpower_limit5g;
2978 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
2979 if (ret) {
2980 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2981 txpower, ret);
2982 return ret;
2983 }
2984
2985 return 0;
2986}
2987
2988static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2989{
2990 struct ath10k_vif *arvif;
2991 int ret, txpower = -1;
2992
2993 lockdep_assert_held(&ar->conf_mutex);
2994
2995 list_for_each_entry(arvif, &ar->arvifs, list) {
2996 WARN_ON(arvif->txpower < 0);
2997
2998 if (txpower == -1)
2999 txpower = arvif->txpower;
3000 else
3001 txpower = min(txpower, arvif->txpower);
3002 }
3003
3004 if (WARN_ON(txpower == -1))
3005 return -EINVAL;
3006
3007 ret = ath10k_mac_txpower_setup(ar, txpower);
3008 if (ret) {
3009 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3010 txpower, ret);
3011 return ret;
3012 }
3013
3014 return 0;
3015}
3016
affd3217
MK
3017static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3018{
5e3dd157
KV
3019 struct ath10k *ar = hw->priv;
3020 struct ieee80211_conf *conf = &hw->conf;
3021 int ret = 0;
5e3dd157
KV
3022
3023 mutex_lock(&ar->conf_mutex);
3024
3025 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
7aa7a72a 3026 ath10k_dbg(ar, ATH10K_DBG_MAC,
d650097b 3027 "mac config channel %dMHz flags 0x%x radar %d\n",
e8a50f8b 3028 conf->chandef.chan->center_freq,
d650097b
MK
3029 conf->chandef.chan->flags,
3030 conf->radar_enabled);
e8a50f8b 3031
5e3dd157
KV
3032 spin_lock_bh(&ar->data_lock);
3033 ar->rx_channel = conf->chandef.chan;
3034 spin_unlock_bh(&ar->data_lock);
e8a50f8b 3035
d650097b
MK
3036 ar->radar_enabled = conf->radar_enabled;
3037 ath10k_recalc_radar_detection(ar);
c930f744
MK
3038
3039 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3040 ar->chandef = conf->chandef;
3041 ath10k_config_chan(ar);
3042 }
5e3dd157
KV
3043 }
3044
affd3217
MK
3045 if (changed & IEEE80211_CONF_CHANGE_PS)
3046 ath10k_config_ps(ar);
5e3dd157
KV
3047
3048 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1933747f
MK
3049 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3050 ret = ath10k_monitor_recalc(ar);
3051 if (ret)
3052 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5e3dd157
KV
3053 }
3054
3055 mutex_unlock(&ar->conf_mutex);
3056 return ret;
3057}
3058
5572a95b
BG
3059static u32 get_nss_from_chainmask(u16 chain_mask)
3060{
3061 if ((chain_mask & 0x15) == 0x15)
3062 return 4;
3063 else if ((chain_mask & 0x7) == 0x7)
3064 return 3;
3065 else if ((chain_mask & 0x3) == 0x3)
3066 return 2;
3067 return 1;
3068}
3069
5e3dd157
KV
3070/*
3071 * TODO:
3072 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3073 * because we will send mgmt frames without CCK. This requirement
3074 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3075 * in the TX packet.
3076 */
3077static int ath10k_add_interface(struct ieee80211_hw *hw,
3078 struct ieee80211_vif *vif)
3079{
3080 struct ath10k *ar = hw->priv;
3081 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3082 enum wmi_sta_powersave_param param;
3083 int ret = 0;
5a13e76e 3084 u32 value;
5e3dd157 3085 int bit;
6d1506e7 3086 u32 vdev_param;
5e3dd157
KV
3087
3088 mutex_lock(&ar->conf_mutex);
3089
0dbd09e6
MK
3090 memset(arvif, 0, sizeof(*arvif));
3091
5e3dd157
KV
3092 arvif->ar = ar;
3093 arvif->vif = vif;
3094
cc4827b9 3095 INIT_WORK(&arvif->wep_key_work, ath10k_tx_wep_key_work);
e63b33f3 3096 INIT_LIST_HEAD(&arvif->list);
cc4827b9 3097
a9aefb3b 3098 if (ar->free_vdev_map == 0) {
7aa7a72a 3099 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5e3dd157 3100 ret = -EBUSY;
9dad14ae 3101 goto err;
5e3dd157 3102 }
16c11176 3103 bit = __ffs64(ar->free_vdev_map);
5e3dd157 3104
16c11176
BG
3105 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3106 bit, ar->free_vdev_map);
5e3dd157 3107
16c11176 3108 arvif->vdev_id = bit;
5e3dd157 3109 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
5e3dd157 3110
5e3dd157 3111 switch (vif->type) {
75d2bd48
MK
3112 case NL80211_IFTYPE_P2P_DEVICE:
3113 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3114 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3115 break;
5e3dd157
KV
3116 case NL80211_IFTYPE_UNSPECIFIED:
3117 case NL80211_IFTYPE_STATION:
3118 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3119 if (vif->p2p)
3120 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3121 break;
3122 case NL80211_IFTYPE_ADHOC:
3123 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3124 break;
3125 case NL80211_IFTYPE_AP:
3126 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3127
3128 if (vif->p2p)
3129 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3130 break;
3131 case NL80211_IFTYPE_MONITOR:
3132 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3133 break;
3134 default:
3135 WARN_ON(1);
3136 break;
3137 }
3138
64badcb6
MK
3139 /* Some firmware revisions don't wait for beacon tx completion before
3140 * sending another SWBA event. This could lead to hardware using old
3141 * (freed) beacon data in some cases, e.g. tx credit starvation
3142 * combined with missed TBTT. This is very very rare.
3143 *
3144 * On non-IOMMU-enabled hosts this could be a possible security issue
3145 * because hw could beacon some random data on the air. On
3146 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3147 * device would crash.
3148 *
3149 * Since there are no beacon tx completions (implicit nor explicit)
3150 * propagated to host the only workaround for this is to allocate a
3151 * DMA-coherent buffer for a lifetime of a vif and use it for all
3152 * beacon tx commands. Worst case for this approach is some beacons may
3153 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3154 */
3155 if (vif->type == NL80211_IFTYPE_ADHOC ||
3156 vif->type == NL80211_IFTYPE_AP) {
3157 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3158 IEEE80211_MAX_FRAME_LEN,
3159 &arvif->beacon_paddr,
82d7aba7 3160 GFP_ATOMIC);
64badcb6
MK
3161 if (!arvif->beacon_buf) {
3162 ret = -ENOMEM;
3163 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3164 ret);
3165 goto err;
3166 }
3167 }
3168
3169 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3170 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3171 arvif->beacon_buf ? "single-buf" : "per-skb");
5e3dd157
KV
3172
3173 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3174 arvif->vdev_subtype, vif->addr);
3175 if (ret) {
7aa7a72a 3176 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
69244e56 3177 arvif->vdev_id, ret);
9dad14ae 3178 goto err;
5e3dd157
KV
3179 }
3180
16c11176 3181 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
0579119f 3182 list_add(&arvif->list, &ar->arvifs);
9dad14ae 3183
6d1506e7
BM
3184 vdev_param = ar->wmi.vdev_param->def_keyid;
3185 ret = ath10k_wmi_vdev_set_param(ar, 0, vdev_param,
cc4827b9 3186 arvif->def_wep_key_idx);
9dad14ae 3187 if (ret) {
7aa7a72a 3188 ath10k_warn(ar, "failed to set vdev %i default key id: %d\n",
69244e56 3189 arvif->vdev_id, ret);
9dad14ae
MK
3190 goto err_vdev_delete;
3191 }
5e3dd157 3192
6d1506e7
BM
3193 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3194 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 3195 ATH10K_HW_TXRX_NATIVE_WIFI);
ebc9abdd 3196 /* 10.X firmware does not support this VDEV parameter. Do not warn */
9dad14ae 3197 if (ret && ret != -EOPNOTSUPP) {
7aa7a72a 3198 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
69244e56 3199 arvif->vdev_id, ret);
9dad14ae
MK
3200 goto err_vdev_delete;
3201 }
5e3dd157 3202
5572a95b
BG
3203 if (ar->cfg_tx_chainmask) {
3204 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3205
3206 vdev_param = ar->wmi.vdev_param->nss;
3207 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3208 nss);
3209 if (ret) {
3210 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3211 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3212 ret);
3213 goto err_vdev_delete;
3214 }
3215 }
3216
5e3dd157
KV
3217 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3218 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3219 if (ret) {
7aa7a72a 3220 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
69244e56 3221 arvif->vdev_id, ret);
9dad14ae 3222 goto err_vdev_delete;
5e3dd157 3223 }
cdf07409 3224
5a13e76e
KV
3225 ret = ath10k_mac_set_kickout(arvif);
3226 if (ret) {
7aa7a72a 3227 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
69244e56 3228 arvif->vdev_id, ret);
5a13e76e
KV
3229 goto err_peer_delete;
3230 }
5e3dd157
KV
3231 }
3232
3233 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3234 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3235 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3236 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3237 param, value);
9dad14ae 3238 if (ret) {
7aa7a72a 3239 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
69244e56 3240 arvif->vdev_id, ret);
9dad14ae
MK
3241 goto err_peer_delete;
3242 }
5e3dd157 3243
9f9b5746 3244 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
9dad14ae 3245 if (ret) {
9f9b5746 3246 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
69244e56 3247 arvif->vdev_id, ret);
9dad14ae
MK
3248 goto err_peer_delete;
3249 }
5e3dd157 3250
9f9b5746 3251 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
9dad14ae 3252 if (ret) {
9f9b5746 3253 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
69244e56 3254 arvif->vdev_id, ret);
9dad14ae
MK
3255 goto err_peer_delete;
3256 }
5e3dd157
KV
3257 }
3258
424121c3 3259 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
9dad14ae 3260 if (ret) {
7aa7a72a 3261 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
679c54a6 3262 arvif->vdev_id, ret);
9dad14ae
MK
3263 goto err_peer_delete;
3264 }
679c54a6 3265
424121c3 3266 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
9dad14ae 3267 if (ret) {
7aa7a72a 3268 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
679c54a6 3269 arvif->vdev_id, ret);
9dad14ae
MK
3270 goto err_peer_delete;
3271 }
679c54a6 3272
7d9d5587
MK
3273 arvif->txpower = vif->bss_conf.txpower;
3274 ret = ath10k_mac_txpower_recalc(ar);
3275 if (ret) {
3276 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3277 goto err_peer_delete;
3278 }
3279
5e3dd157 3280 mutex_unlock(&ar->conf_mutex);
9dad14ae
MK
3281 return 0;
3282
3283err_peer_delete:
3284 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3285 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3286
3287err_vdev_delete:
3288 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
16c11176 3289 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 3290 list_del(&arvif->list);
9dad14ae
MK
3291
3292err:
64badcb6
MK
3293 if (arvif->beacon_buf) {
3294 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3295 arvif->beacon_buf, arvif->beacon_paddr);
3296 arvif->beacon_buf = NULL;
3297 }
3298
9dad14ae
MK
3299 mutex_unlock(&ar->conf_mutex);
3300
5e3dd157
KV
3301 return ret;
3302}
3303
3304static void ath10k_remove_interface(struct ieee80211_hw *hw,
3305 struct ieee80211_vif *vif)
3306{
3307 struct ath10k *ar = hw->priv;
3308 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3309 int ret;
3310
cc4827b9
MK
3311 cancel_work_sync(&arvif->wep_key_work);
3312
5d011f5c
SM
3313 mutex_lock(&ar->conf_mutex);
3314
ed54388a 3315 spin_lock_bh(&ar->data_lock);
64badcb6 3316 ath10k_mac_vif_beacon_cleanup(arvif);
ed54388a
MK
3317 spin_unlock_bh(&ar->data_lock);
3318
855aed12
SW
3319 ret = ath10k_spectral_vif_stop(arvif);
3320 if (ret)
7aa7a72a 3321 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
855aed12
SW
3322 arvif->vdev_id, ret);
3323
16c11176 3324 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 3325 list_del(&arvif->list);
5e3dd157
KV
3326
3327 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3328 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, vif->addr);
3329 if (ret)
7aa7a72a 3330 ath10k_warn(ar, "failed to remove peer for AP vdev %i: %d\n",
69244e56 3331 arvif->vdev_id, ret);
5e3dd157
KV
3332
3333 kfree(arvif->u.ap.noa_data);
3334 }
3335
7aa7a72a 3336 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
60c3daa8
KV
3337 arvif->vdev_id);
3338
5e3dd157
KV
3339 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3340 if (ret)
7aa7a72a 3341 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
69244e56 3342 arvif->vdev_id, ret);
5e3dd157 3343
5e3dd157
KV
3344 ath10k_peer_cleanup(ar, arvif->vdev_id);
3345
3346 mutex_unlock(&ar->conf_mutex);
3347}
3348
3349/*
3350 * FIXME: Has to be verified.
3351 */
3352#define SUPPORTED_FILTERS \
3353 (FIF_PROMISC_IN_BSS | \
3354 FIF_ALLMULTI | \
3355 FIF_CONTROL | \
3356 FIF_PSPOLL | \
3357 FIF_OTHER_BSS | \
3358 FIF_BCN_PRBRESP_PROMISC | \
3359 FIF_PROBE_REQ | \
3360 FIF_FCSFAIL)
3361
3362static void ath10k_configure_filter(struct ieee80211_hw *hw,
3363 unsigned int changed_flags,
3364 unsigned int *total_flags,
3365 u64 multicast)
3366{
3367 struct ath10k *ar = hw->priv;
3368 int ret;
3369
3370 mutex_lock(&ar->conf_mutex);
3371
3372 changed_flags &= SUPPORTED_FILTERS;
3373 *total_flags &= SUPPORTED_FILTERS;
3374 ar->filter_flags = *total_flags;
3375
1933747f
MK
3376 ret = ath10k_monitor_recalc(ar);
3377 if (ret)
3378 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
5e3dd157
KV
3379
3380 mutex_unlock(&ar->conf_mutex);
3381}
3382
3383static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3384 struct ieee80211_vif *vif,
3385 struct ieee80211_bss_conf *info,
3386 u32 changed)
3387{
3388 struct ath10k *ar = hw->priv;
3389 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3390 int ret = 0;
af762c0b 3391 u32 vdev_param, pdev_param, slottime, preamble;
5e3dd157
KV
3392
3393 mutex_lock(&ar->conf_mutex);
3394
3395 if (changed & BSS_CHANGED_IBSS)
3396 ath10k_control_ibss(arvif, info, vif->addr);
3397
3398 if (changed & BSS_CHANGED_BEACON_INT) {
3399 arvif->beacon_interval = info->beacon_int;
6d1506e7
BM
3400 vdev_param = ar->wmi.vdev_param->beacon_interval;
3401 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 3402 arvif->beacon_interval);
7aa7a72a 3403 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3404 "mac vdev %d beacon_interval %d\n",
3405 arvif->vdev_id, arvif->beacon_interval);
3406
5e3dd157 3407 if (ret)
7aa7a72a 3408 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
69244e56 3409 arvif->vdev_id, ret);
5e3dd157
KV
3410 }
3411
3412 if (changed & BSS_CHANGED_BEACON) {
7aa7a72a 3413 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3414 "vdev %d set beacon tx mode to staggered\n",
3415 arvif->vdev_id);
3416
226a339b
BM
3417 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3418 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5e3dd157
KV
3419 WMI_BEACON_STAGGERED_MODE);
3420 if (ret)
7aa7a72a 3421 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
69244e56 3422 arvif->vdev_id, ret);
fbb8f1b7
MK
3423
3424 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3425 if (ret)
3426 ath10k_warn(ar, "failed to update beacon template: %d\n",
3427 ret);
3428 }
3429
3430 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3431 ret = ath10k_mac_setup_prb_tmpl(arvif);
3432 if (ret)
3433 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3434 arvif->vdev_id, ret);
5e3dd157
KV
3435 }
3436
b70727e8 3437 if (changed & BSS_CHANGED_BEACON_INFO) {
5e3dd157
KV
3438 arvif->dtim_period = info->dtim_period;
3439
7aa7a72a 3440 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3441 "mac vdev %d dtim_period %d\n",
3442 arvif->vdev_id, arvif->dtim_period);
3443
6d1506e7
BM
3444 vdev_param = ar->wmi.vdev_param->dtim_period;
3445 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3446 arvif->dtim_period);
3447 if (ret)
7aa7a72a 3448 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
69244e56 3449 arvif->vdev_id, ret);
5e3dd157
KV
3450 }
3451
3452 if (changed & BSS_CHANGED_SSID &&
3453 vif->type == NL80211_IFTYPE_AP) {
3454 arvif->u.ap.ssid_len = info->ssid_len;
3455 if (info->ssid_len)
3456 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3457 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3458 }
3459
077efc8c
MK
3460 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3461 ether_addr_copy(arvif->bssid, info->bssid);
5e3dd157
KV
3462
3463 if (changed & BSS_CHANGED_BEACON_ENABLED)
3464 ath10k_control_beaconing(arvif, info);
3465
3466 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
e81bd104 3467 arvif->use_cts_prot = info->use_cts_prot;
7aa7a72a 3468 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
e81bd104 3469 arvif->vdev_id, info->use_cts_prot);
60c3daa8 3470
e81bd104 3471 ret = ath10k_recalc_rtscts_prot(arvif);
5e3dd157 3472 if (ret)
7aa7a72a 3473 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
69244e56 3474 arvif->vdev_id, ret);
5e3dd157
KV
3475 }
3476
3477 if (changed & BSS_CHANGED_ERP_SLOT) {
5e3dd157
KV
3478 if (info->use_short_slot)
3479 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3480
3481 else
3482 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3483
7aa7a72a 3484 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
60c3daa8
KV
3485 arvif->vdev_id, slottime);
3486
6d1506e7
BM
3487 vdev_param = ar->wmi.vdev_param->slot_time;
3488 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3489 slottime);
3490 if (ret)
7aa7a72a 3491 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
69244e56 3492 arvif->vdev_id, ret);
5e3dd157
KV
3493 }
3494
3495 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5e3dd157
KV
3496 if (info->use_short_preamble)
3497 preamble = WMI_VDEV_PREAMBLE_SHORT;
3498 else
3499 preamble = WMI_VDEV_PREAMBLE_LONG;
3500
7aa7a72a 3501 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3502 "mac vdev %d preamble %dn",
3503 arvif->vdev_id, preamble);
3504
6d1506e7
BM
3505 vdev_param = ar->wmi.vdev_param->preamble;
3506 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
3507 preamble);
3508 if (ret)
7aa7a72a 3509 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
69244e56 3510 arvif->vdev_id, ret);
5e3dd157
KV
3511 }
3512
3513 if (changed & BSS_CHANGED_ASSOC) {
e556f111
MK
3514 if (info->assoc) {
3515 /* Workaround: Make sure monitor vdev is not running
3516 * when associating to prevent some firmware revisions
3517 * (e.g. 10.1 and 10.2) from crashing.
3518 */
3519 if (ar->monitor_started)
3520 ath10k_monitor_stop(ar);
5e3dd157 3521 ath10k_bss_assoc(hw, vif, info);
e556f111 3522 ath10k_monitor_recalc(ar);
077efc8c
MK
3523 } else {
3524 ath10k_bss_disassoc(hw, vif);
e556f111 3525 }
5e3dd157
KV
3526 }
3527
7d9d5587
MK
3528 if (changed & BSS_CHANGED_TXPOWER) {
3529 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3530 arvif->vdev_id, info->txpower);
3531
3532 arvif->txpower = info->txpower;
3533 ret = ath10k_mac_txpower_recalc(ar);
3534 if (ret)
3535 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3536 }
3537
bf14e65c
MK
3538 if (changed & BSS_CHANGED_PS) {
3539 ret = ath10k_mac_vif_setup_ps(arvif);
3540 if (ret)
3541 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3542 arvif->vdev_id, ret);
3543 }
3544
5e3dd157
KV
3545 mutex_unlock(&ar->conf_mutex);
3546}
3547
3548static int ath10k_hw_scan(struct ieee80211_hw *hw,
3549 struct ieee80211_vif *vif,
c56ef672 3550 struct ieee80211_scan_request *hw_req)
5e3dd157
KV
3551{
3552 struct ath10k *ar = hw->priv;
3553 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
c56ef672 3554 struct cfg80211_scan_request *req = &hw_req->req;
5e3dd157
KV
3555 struct wmi_start_scan_arg arg;
3556 int ret = 0;
3557 int i;
3558
3559 mutex_lock(&ar->conf_mutex);
3560
3561 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
3562 switch (ar->scan.state) {
3563 case ATH10K_SCAN_IDLE:
3564 reinit_completion(&ar->scan.started);
3565 reinit_completion(&ar->scan.completed);
3566 ar->scan.state = ATH10K_SCAN_STARTING;
3567 ar->scan.is_roc = false;
3568 ar->scan.vdev_id = arvif->vdev_id;
3569 ret = 0;
3570 break;
3571 case ATH10K_SCAN_STARTING:
3572 case ATH10K_SCAN_RUNNING:
3573 case ATH10K_SCAN_ABORTING:
5e3dd157 3574 ret = -EBUSY;
5c81c7fd 3575 break;
5e3dd157 3576 }
5e3dd157
KV
3577 spin_unlock_bh(&ar->data_lock);
3578
5c81c7fd
MK
3579 if (ret)
3580 goto exit;
3581
5e3dd157
KV
3582 memset(&arg, 0, sizeof(arg));
3583 ath10k_wmi_start_scan_init(ar, &arg);
3584 arg.vdev_id = arvif->vdev_id;
3585 arg.scan_id = ATH10K_SCAN_ID;
3586
3587 if (!req->no_cck)
3588 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3589
3590 if (req->ie_len) {
3591 arg.ie_len = req->ie_len;
3592 memcpy(arg.ie, req->ie, arg.ie_len);
3593 }
3594
3595 if (req->n_ssids) {
3596 arg.n_ssids = req->n_ssids;
3597 for (i = 0; i < arg.n_ssids; i++) {
3598 arg.ssids[i].len = req->ssids[i].ssid_len;
3599 arg.ssids[i].ssid = req->ssids[i].ssid;
3600 }
dcd4a561
MK
3601 } else {
3602 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5e3dd157
KV
3603 }
3604
3605 if (req->n_channels) {
3606 arg.n_channels = req->n_channels;
3607 for (i = 0; i < arg.n_channels; i++)
3608 arg.channels[i] = req->channels[i]->center_freq;
3609 }
3610
3611 ret = ath10k_start_scan(ar, &arg);
3612 if (ret) {
7aa7a72a 3613 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5e3dd157 3614 spin_lock_bh(&ar->data_lock);
5c81c7fd 3615 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
3616 spin_unlock_bh(&ar->data_lock);
3617 }
3618
3619exit:
3620 mutex_unlock(&ar->conf_mutex);
3621 return ret;
3622}
3623
3624static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
3625 struct ieee80211_vif *vif)
3626{
3627 struct ath10k *ar = hw->priv;
5e3dd157
KV
3628
3629 mutex_lock(&ar->conf_mutex);
5c81c7fd 3630 ath10k_scan_abort(ar);
5e3dd157 3631 mutex_unlock(&ar->conf_mutex);
4eb2e164
MK
3632
3633 cancel_delayed_work_sync(&ar->scan.timeout);
5e3dd157
KV
3634}
3635
cfb27d29
MK
3636static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
3637 struct ath10k_vif *arvif,
3638 enum set_key_cmd cmd,
3639 struct ieee80211_key_conf *key)
3640{
3641 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
3642 int ret;
3643
3644 /* 10.1 firmware branch requires default key index to be set to group
3645 * key index after installing it. Otherwise FW/HW Txes corrupted
3646 * frames with multi-vif APs. This is not required for main firmware
3647 * branch (e.g. 636).
3648 *
3649 * FIXME: This has been tested only in AP. It remains unknown if this
3650 * is required for multi-vif STA interfaces on 10.1 */
3651
3652 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
3653 return;
3654
3655 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
3656 return;
3657
3658 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
3659 return;
3660
3661 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
3662 return;
3663
3664 if (cmd != SET_KEY)
3665 return;
3666
3667 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3668 key->keyidx);
3669 if (ret)
7aa7a72a 3670 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
69244e56 3671 arvif->vdev_id, ret);
cfb27d29
MK
3672}
3673
5e3dd157
KV
3674static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3675 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
3676 struct ieee80211_key_conf *key)
3677{
3678 struct ath10k *ar = hw->priv;
3679 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3680 struct ath10k_peer *peer;
3681 const u8 *peer_addr;
3682 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
3683 key->cipher == WLAN_CIPHER_SUITE_WEP104;
3684 int ret = 0;
3685
3686 if (key->keyidx > WMI_MAX_KEY_INDEX)
3687 return -ENOSPC;
3688
3689 mutex_lock(&ar->conf_mutex);
3690
3691 if (sta)
3692 peer_addr = sta->addr;
3693 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
3694 peer_addr = vif->bss_conf.bssid;
3695 else
3696 peer_addr = vif->addr;
3697
3698 key->hw_key_idx = key->keyidx;
3699
3700 /* the peer should not disappear in mid-way (unless FW goes awry) since
3701 * we already hold conf_mutex. we just make sure its there now. */
3702 spin_lock_bh(&ar->data_lock);
3703 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3704 spin_unlock_bh(&ar->data_lock);
3705
3706 if (!peer) {
3707 if (cmd == SET_KEY) {
7aa7a72a 3708 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5e3dd157
KV
3709 peer_addr);
3710 ret = -EOPNOTSUPP;
3711 goto exit;
3712 } else {
3713 /* if the peer doesn't exist there is no key to disable
3714 * anymore */
3715 goto exit;
3716 }
3717 }
3718
3719 if (is_wep) {
3720 if (cmd == SET_KEY)
3721 arvif->wep_keys[key->keyidx] = key;
3722 else
3723 arvif->wep_keys[key->keyidx] = NULL;
3724
3725 if (cmd == DISABLE_KEY)
3726 ath10k_clear_vdev_key(arvif, key);
3727 }
3728
3729 ret = ath10k_install_key(arvif, key, cmd, peer_addr);
3730 if (ret) {
7aa7a72a 3731 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
69244e56 3732 arvif->vdev_id, peer_addr, ret);
5e3dd157
KV
3733 goto exit;
3734 }
3735
cfb27d29
MK
3736 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
3737
5e3dd157
KV
3738 spin_lock_bh(&ar->data_lock);
3739 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
3740 if (peer && cmd == SET_KEY)
3741 peer->keys[key->keyidx] = key;
3742 else if (peer && cmd == DISABLE_KEY)
3743 peer->keys[key->keyidx] = NULL;
3744 else if (peer == NULL)
3745 /* impossible unless FW goes crazy */
7aa7a72a 3746 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5e3dd157
KV
3747 spin_unlock_bh(&ar->data_lock);
3748
3749exit:
3750 mutex_unlock(&ar->conf_mutex);
3751 return ret;
3752}
3753
9797febc
MK
3754static void ath10k_sta_rc_update_wk(struct work_struct *wk)
3755{
3756 struct ath10k *ar;
3757 struct ath10k_vif *arvif;
3758 struct ath10k_sta *arsta;
3759 struct ieee80211_sta *sta;
3760 u32 changed, bw, nss, smps;
3761 int err;
3762
3763 arsta = container_of(wk, struct ath10k_sta, update_wk);
3764 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
3765 arvif = arsta->arvif;
3766 ar = arvif->ar;
3767
3768 spin_lock_bh(&ar->data_lock);
3769
3770 changed = arsta->changed;
3771 arsta->changed = 0;
3772
3773 bw = arsta->bw;
3774 nss = arsta->nss;
3775 smps = arsta->smps;
3776
3777 spin_unlock_bh(&ar->data_lock);
3778
3779 mutex_lock(&ar->conf_mutex);
3780
3781 if (changed & IEEE80211_RC_BW_CHANGED) {
7aa7a72a 3782 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
9797febc
MK
3783 sta->addr, bw);
3784
3785 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3786 WMI_PEER_CHAN_WIDTH, bw);
3787 if (err)
7aa7a72a 3788 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
9797febc
MK
3789 sta->addr, bw, err);
3790 }
3791
3792 if (changed & IEEE80211_RC_NSS_CHANGED) {
7aa7a72a 3793 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
9797febc
MK
3794 sta->addr, nss);
3795
3796 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3797 WMI_PEER_NSS, nss);
3798 if (err)
7aa7a72a 3799 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
9797febc
MK
3800 sta->addr, nss, err);
3801 }
3802
3803 if (changed & IEEE80211_RC_SMPS_CHANGED) {
7aa7a72a 3804 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
9797febc
MK
3805 sta->addr, smps);
3806
3807 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
3808 WMI_PEER_SMPS_STATE, smps);
3809 if (err)
7aa7a72a 3810 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
9797febc
MK
3811 sta->addr, smps, err);
3812 }
3813
55884c04
JD
3814 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
3815 changed & IEEE80211_RC_NSS_CHANGED) {
3816 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
44d6fa90
CYY
3817 sta->addr);
3818
590922a8 3819 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
44d6fa90 3820 if (err)
7aa7a72a 3821 ath10k_warn(ar, "failed to reassociate station: %pM\n",
44d6fa90
CYY
3822 sta->addr);
3823 }
3824
9797febc
MK
3825 mutex_unlock(&ar->conf_mutex);
3826}
3827
cfd1061e
MK
3828static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
3829{
3830 struct ath10k *ar = arvif->ar;
3831
3832 lockdep_assert_held(&ar->conf_mutex);
3833
3834 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3835 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3836 return 0;
3837
3838 if (ar->num_stations >= ar->max_num_stations)
3839 return -ENOBUFS;
3840
3841 ar->num_stations++;
3842
3843 return 0;
3844}
3845
3846static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
3847{
3848 struct ath10k *ar = arvif->ar;
3849
3850 lockdep_assert_held(&ar->conf_mutex);
3851
3852 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
3853 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
3854 return;
3855
3856 ar->num_stations--;
3857}
3858
5e3dd157
KV
3859static int ath10k_sta_state(struct ieee80211_hw *hw,
3860 struct ieee80211_vif *vif,
3861 struct ieee80211_sta *sta,
3862 enum ieee80211_sta_state old_state,
3863 enum ieee80211_sta_state new_state)
3864{
3865 struct ath10k *ar = hw->priv;
3866 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
9797febc 3867 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5e3dd157
KV
3868 int ret = 0;
3869
76f90024
MK
3870 if (old_state == IEEE80211_STA_NOTEXIST &&
3871 new_state == IEEE80211_STA_NONE) {
3872 memset(arsta, 0, sizeof(*arsta));
3873 arsta->arvif = arvif;
3874 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
3875 }
3876
9797febc
MK
3877 /* cancel must be done outside the mutex to avoid deadlock */
3878 if ((old_state == IEEE80211_STA_NONE &&
3879 new_state == IEEE80211_STA_NOTEXIST))
3880 cancel_work_sync(&arsta->update_wk);
3881
5e3dd157
KV
3882 mutex_lock(&ar->conf_mutex);
3883
3884 if (old_state == IEEE80211_STA_NOTEXIST &&
077efc8c 3885 new_state == IEEE80211_STA_NONE) {
5e3dd157
KV
3886 /*
3887 * New station addition.
3888 */
cfd1061e
MK
3889 ath10k_dbg(ar, ATH10K_DBG_MAC,
3890 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
3891 arvif->vdev_id, sta->addr,
3892 ar->num_stations + 1, ar->max_num_stations,
3893 ar->num_peers + 1, ar->max_num_peers);
0e759f36 3894
cfd1061e
MK
3895 ret = ath10k_mac_inc_num_stations(arvif);
3896 if (ret) {
3897 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
3898 ar->max_num_stations);
0e759f36
BM
3899 goto exit;
3900 }
3901
5e3dd157 3902 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
a52c0282 3903 if (ret) {
7aa7a72a 3904 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
479398b0 3905 sta->addr, arvif->vdev_id, ret);
cfd1061e 3906 ath10k_mac_dec_num_stations(arvif);
a52c0282
MK
3907 goto exit;
3908 }
077efc8c
MK
3909
3910 if (vif->type == NL80211_IFTYPE_STATION) {
3911 WARN_ON(arvif->is_started);
3912
3913 ret = ath10k_vdev_start(arvif);
3914 if (ret) {
3915 ath10k_warn(ar, "failed to start vdev %i: %d\n",
3916 arvif->vdev_id, ret);
3917 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
3918 sta->addr));
cfd1061e 3919 ath10k_mac_dec_num_stations(arvif);
077efc8c
MK
3920 goto exit;
3921 }
3922
3923 arvif->is_started = true;
3924 }
5e3dd157
KV
3925 } else if ((old_state == IEEE80211_STA_NONE &&
3926 new_state == IEEE80211_STA_NOTEXIST)) {
3927 /*
3928 * Existing station deletion.
3929 */
7aa7a72a 3930 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3931 "mac vdev %d peer delete %pM (sta gone)\n",
3932 arvif->vdev_id, sta->addr);
077efc8c
MK
3933
3934 if (vif->type == NL80211_IFTYPE_STATION) {
3935 WARN_ON(!arvif->is_started);
3936
3937 ret = ath10k_vdev_stop(arvif);
3938 if (ret)
3939 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
3940 arvif->vdev_id, ret);
3941
3942 arvif->is_started = false;
3943 }
3944
5e3dd157
KV
3945 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
3946 if (ret)
7aa7a72a 3947 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
69244e56 3948 sta->addr, arvif->vdev_id, ret);
5e3dd157 3949
cfd1061e 3950 ath10k_mac_dec_num_stations(arvif);
5e3dd157
KV
3951 } else if (old_state == IEEE80211_STA_AUTH &&
3952 new_state == IEEE80211_STA_ASSOC &&
3953 (vif->type == NL80211_IFTYPE_AP ||
3954 vif->type == NL80211_IFTYPE_ADHOC)) {
3955 /*
3956 * New association.
3957 */
7aa7a72a 3958 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
60c3daa8
KV
3959 sta->addr);
3960
590922a8 3961 ret = ath10k_station_assoc(ar, vif, sta, false);
5e3dd157 3962 if (ret)
7aa7a72a 3963 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
69244e56 3964 sta->addr, arvif->vdev_id, ret);
5e3dd157
KV
3965 } else if (old_state == IEEE80211_STA_ASSOC &&
3966 new_state == IEEE80211_STA_AUTH &&
3967 (vif->type == NL80211_IFTYPE_AP ||
3968 vif->type == NL80211_IFTYPE_ADHOC)) {
3969 /*
3970 * Disassociation.
3971 */
7aa7a72a 3972 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
60c3daa8
KV
3973 sta->addr);
3974
590922a8 3975 ret = ath10k_station_disassoc(ar, vif, sta);
5e3dd157 3976 if (ret)
7aa7a72a 3977 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
69244e56 3978 sta->addr, arvif->vdev_id, ret);
5e3dd157 3979 }
0e759f36 3980exit:
5e3dd157
KV
3981 mutex_unlock(&ar->conf_mutex);
3982 return ret;
3983}
3984
3985static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
5b07e07f 3986 u16 ac, bool enable)
5e3dd157
KV
3987{
3988 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3989 u32 value = 0;
3990 int ret = 0;
3991
548db54c
MK
3992 lockdep_assert_held(&ar->conf_mutex);
3993
5e3dd157
KV
3994 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
3995 return 0;
3996
3997 switch (ac) {
3998 case IEEE80211_AC_VO:
3999 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4000 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4001 break;
4002 case IEEE80211_AC_VI:
4003 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4004 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4005 break;
4006 case IEEE80211_AC_BE:
4007 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4008 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4009 break;
4010 case IEEE80211_AC_BK:
4011 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4012 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4013 break;
4014 }
4015
4016 if (enable)
4017 arvif->u.sta.uapsd |= value;
4018 else
4019 arvif->u.sta.uapsd &= ~value;
4020
4021 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4022 WMI_STA_PS_PARAM_UAPSD,
4023 arvif->u.sta.uapsd);
4024 if (ret) {
7aa7a72a 4025 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
5e3dd157
KV
4026 goto exit;
4027 }
4028
4029 if (arvif->u.sta.uapsd)
4030 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4031 else
4032 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4033
4034 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4035 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4036 value);
4037 if (ret)
7aa7a72a 4038 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
5e3dd157 4039
9f9b5746
MK
4040 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4041 if (ret) {
4042 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4043 arvif->vdev_id, ret);
4044 return ret;
4045 }
4046
4047 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4048 if (ret) {
4049 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4050 arvif->vdev_id, ret);
4051 return ret;
4052 }
4053
5e3dd157
KV
4054exit:
4055 return ret;
4056}
4057
4058static int ath10k_conf_tx(struct ieee80211_hw *hw,
4059 struct ieee80211_vif *vif, u16 ac,
4060 const struct ieee80211_tx_queue_params *params)
4061{
4062 struct ath10k *ar = hw->priv;
4063 struct wmi_wmm_params_arg *p = NULL;
4064 int ret;
4065
4066 mutex_lock(&ar->conf_mutex);
4067
4068 switch (ac) {
4069 case IEEE80211_AC_VO:
4070 p = &ar->wmm_params.ac_vo;
4071 break;
4072 case IEEE80211_AC_VI:
4073 p = &ar->wmm_params.ac_vi;
4074 break;
4075 case IEEE80211_AC_BE:
4076 p = &ar->wmm_params.ac_be;
4077 break;
4078 case IEEE80211_AC_BK:
4079 p = &ar->wmm_params.ac_bk;
4080 break;
4081 }
4082
4083 if (WARN_ON(!p)) {
4084 ret = -EINVAL;
4085 goto exit;
4086 }
4087
4088 p->cwmin = params->cw_min;
4089 p->cwmax = params->cw_max;
4090 p->aifs = params->aifs;
4091
4092 /*
4093 * The channel time duration programmed in the HW is in absolute
4094 * microseconds, while mac80211 gives the txop in units of
4095 * 32 microseconds.
4096 */
4097 p->txop = params->txop * 32;
4098
4099 /* FIXME: FW accepts wmm params per hw, not per vif */
4100 ret = ath10k_wmi_pdev_set_wmm_params(ar, &ar->wmm_params);
4101 if (ret) {
7aa7a72a 4102 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
5e3dd157
KV
4103 goto exit;
4104 }
4105
4106 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4107 if (ret)
7aa7a72a 4108 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
5e3dd157
KV
4109
4110exit:
4111 mutex_unlock(&ar->conf_mutex);
4112 return ret;
4113}
4114
4115#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4116
4117static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4118 struct ieee80211_vif *vif,
4119 struct ieee80211_channel *chan,
4120 int duration,
4121 enum ieee80211_roc_type type)
4122{
4123 struct ath10k *ar = hw->priv;
4124 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4125 struct wmi_start_scan_arg arg;
5c81c7fd 4126 int ret = 0;
5e3dd157
KV
4127
4128 mutex_lock(&ar->conf_mutex);
4129
4130 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
4131 switch (ar->scan.state) {
4132 case ATH10K_SCAN_IDLE:
4133 reinit_completion(&ar->scan.started);
4134 reinit_completion(&ar->scan.completed);
4135 reinit_completion(&ar->scan.on_channel);
4136 ar->scan.state = ATH10K_SCAN_STARTING;
4137 ar->scan.is_roc = true;
4138 ar->scan.vdev_id = arvif->vdev_id;
4139 ar->scan.roc_freq = chan->center_freq;
4140 ret = 0;
4141 break;
4142 case ATH10K_SCAN_STARTING:
4143 case ATH10K_SCAN_RUNNING:
4144 case ATH10K_SCAN_ABORTING:
5e3dd157 4145 ret = -EBUSY;
5c81c7fd 4146 break;
5e3dd157 4147 }
5e3dd157
KV
4148 spin_unlock_bh(&ar->data_lock);
4149
5c81c7fd
MK
4150 if (ret)
4151 goto exit;
4152
dcca0bdb
MK
4153 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4154
5e3dd157
KV
4155 memset(&arg, 0, sizeof(arg));
4156 ath10k_wmi_start_scan_init(ar, &arg);
4157 arg.vdev_id = arvif->vdev_id;
4158 arg.scan_id = ATH10K_SCAN_ID;
4159 arg.n_channels = 1;
4160 arg.channels[0] = chan->center_freq;
4161 arg.dwell_time_active = duration;
4162 arg.dwell_time_passive = duration;
4163 arg.max_scan_time = 2 * duration;
4164 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4165 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4166
4167 ret = ath10k_start_scan(ar, &arg);
4168 if (ret) {
7aa7a72a 4169 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5e3dd157 4170 spin_lock_bh(&ar->data_lock);
5c81c7fd 4171 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
4172 spin_unlock_bh(&ar->data_lock);
4173 goto exit;
4174 }
4175
4176 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4177 if (ret == 0) {
7aa7a72a 4178 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5c81c7fd
MK
4179
4180 ret = ath10k_scan_stop(ar);
4181 if (ret)
7aa7a72a 4182 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd 4183
5e3dd157
KV
4184 ret = -ETIMEDOUT;
4185 goto exit;
4186 }
4187
4188 ret = 0;
4189exit:
4190 mutex_unlock(&ar->conf_mutex);
4191 return ret;
4192}
4193
4194static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4195{
4196 struct ath10k *ar = hw->priv;
4197
4198 mutex_lock(&ar->conf_mutex);
5c81c7fd 4199 ath10k_scan_abort(ar);
5e3dd157
KV
4200 mutex_unlock(&ar->conf_mutex);
4201
4eb2e164
MK
4202 cancel_delayed_work_sync(&ar->scan.timeout);
4203
5e3dd157
KV
4204 return 0;
4205}
4206
4207/*
4208 * Both RTS and Fragmentation threshold are interface-specific
4209 * in ath10k, but device-specific in mac80211.
4210 */
5e3dd157 4211
ad088bfa
MK
4212static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4213{
4214 struct ath10k *ar = hw->priv;
4215 struct ath10k_vif *arvif;
4216 int ret = 0;
548db54c 4217
5e3dd157 4218 mutex_lock(&ar->conf_mutex);
ad088bfa 4219 list_for_each_entry(arvif, &ar->arvifs, list) {
7aa7a72a 4220 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
ad088bfa
MK
4221 arvif->vdev_id, value);
4222
4223 ret = ath10k_mac_set_rts(arvif, value);
4224 if (ret) {
7aa7a72a 4225 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
ad088bfa
MK
4226 arvif->vdev_id, ret);
4227 break;
4228 }
4229 }
5e3dd157
KV
4230 mutex_unlock(&ar->conf_mutex);
4231
ad088bfa 4232 return ret;
5e3dd157
KV
4233}
4234
77be2c54
EG
4235static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4236 u32 queues, bool drop)
5e3dd157
KV
4237{
4238 struct ath10k *ar = hw->priv;
affd3217 4239 bool skip;
5e3dd157
KV
4240 int ret;
4241
4242 /* mac80211 doesn't care if we really xmit queued frames or not
4243 * we'll collect those frames either way if we stop/delete vdevs */
4244 if (drop)
4245 return;
4246
548db54c
MK
4247 mutex_lock(&ar->conf_mutex);
4248
affd3217
MK
4249 if (ar->state == ATH10K_STATE_WEDGED)
4250 goto skip;
4251
edb8236d 4252 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
5e3dd157 4253 bool empty;
affd3217 4254
edb8236d 4255 spin_lock_bh(&ar->htt.tx_lock);
0945baf7 4256 empty = (ar->htt.num_pending_tx == 0);
edb8236d 4257 spin_unlock_bh(&ar->htt.tx_lock);
affd3217 4258
7962b0d8
MK
4259 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4260 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4261 &ar->dev_flags);
affd3217
MK
4262
4263 (empty || skip);
5e3dd157 4264 }), ATH10K_FLUSH_TIMEOUT_HZ);
affd3217
MK
4265
4266 if (ret <= 0 || skip)
7aa7a72a 4267 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
9ba4c787 4268 skip, ar->state, ret);
548db54c 4269
affd3217 4270skip:
548db54c 4271 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
4272}
4273
4274/* TODO: Implement this function properly
4275 * For now it is needed to reply to Probe Requests in IBSS mode.
4276 * Propably we need this information from FW.
4277 */
4278static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4279{
4280 return 1;
4281}
4282
8cd13cad
MK
4283#ifdef CONFIG_PM
4284static int ath10k_suspend(struct ieee80211_hw *hw,
4285 struct cfg80211_wowlan *wowlan)
4286{
4287 struct ath10k *ar = hw->priv;
4288 int ret;
4289
9042e17d
MP
4290 mutex_lock(&ar->conf_mutex);
4291
00f5482b 4292 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
8cd13cad 4293 if (ret) {
00f5482b
MP
4294 if (ret == -ETIMEDOUT)
4295 goto resume;
9042e17d
MP
4296 ret = 1;
4297 goto exit;
8cd13cad
MK
4298 }
4299
8cd13cad
MK
4300 ret = ath10k_hif_suspend(ar);
4301 if (ret) {
7aa7a72a 4302 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
8cd13cad
MK
4303 goto resume;
4304 }
4305
9042e17d
MP
4306 ret = 0;
4307 goto exit;
8cd13cad
MK
4308resume:
4309 ret = ath10k_wmi_pdev_resume_target(ar);
4310 if (ret)
7aa7a72a 4311 ath10k_warn(ar, "failed to resume target: %d\n", ret);
9042e17d
MP
4312
4313 ret = 1;
4314exit:
4315 mutex_unlock(&ar->conf_mutex);
4316 return ret;
8cd13cad
MK
4317}
4318
4319static int ath10k_resume(struct ieee80211_hw *hw)
4320{
4321 struct ath10k *ar = hw->priv;
4322 int ret;
4323
9042e17d
MP
4324 mutex_lock(&ar->conf_mutex);
4325
8cd13cad
MK
4326 ret = ath10k_hif_resume(ar);
4327 if (ret) {
7aa7a72a 4328 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
9042e17d
MP
4329 ret = 1;
4330 goto exit;
8cd13cad
MK
4331 }
4332
4333 ret = ath10k_wmi_pdev_resume_target(ar);
4334 if (ret) {
7aa7a72a 4335 ath10k_warn(ar, "failed to resume target: %d\n", ret);
9042e17d
MP
4336 ret = 1;
4337 goto exit;
8cd13cad
MK
4338 }
4339
9042e17d
MP
4340 ret = 0;
4341exit:
4342 mutex_unlock(&ar->conf_mutex);
4343 return ret;
8cd13cad
MK
4344}
4345#endif
4346
cf2c92d8
EP
4347static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4348 enum ieee80211_reconfig_type reconfig_type)
affd3217
MK
4349{
4350 struct ath10k *ar = hw->priv;
4351
cf2c92d8
EP
4352 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4353 return;
4354
affd3217
MK
4355 mutex_lock(&ar->conf_mutex);
4356
4357 /* If device failed to restart it will be in a different state, e.g.
4358 * ATH10K_STATE_WEDGED */
4359 if (ar->state == ATH10K_STATE_RESTARTED) {
7aa7a72a 4360 ath10k_info(ar, "device successfully recovered\n");
affd3217 4361 ar->state = ATH10K_STATE_ON;
7962b0d8 4362 ieee80211_wake_queues(ar->hw);
affd3217
MK
4363 }
4364
4365 mutex_unlock(&ar->conf_mutex);
4366}
4367
2e1dea40
MK
4368static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4369 struct survey_info *survey)
4370{
4371 struct ath10k *ar = hw->priv;
4372 struct ieee80211_supported_band *sband;
4373 struct survey_info *ar_survey = &ar->survey[idx];
4374 int ret = 0;
4375
4376 mutex_lock(&ar->conf_mutex);
4377
4378 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4379 if (sband && idx >= sband->n_channels) {
4380 idx -= sband->n_channels;
4381 sband = NULL;
4382 }
4383
4384 if (!sband)
4385 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4386
4387 if (!sband || idx >= sband->n_channels) {
4388 ret = -ENOENT;
4389 goto exit;
4390 }
4391
4392 spin_lock_bh(&ar->data_lock);
4393 memcpy(survey, ar_survey, sizeof(*survey));
4394 spin_unlock_bh(&ar->data_lock);
4395
4396 survey->channel = &sband->channels[idx];
4397
fa1d4df8
FF
4398 if (ar->rx_channel == survey->channel)
4399 survey->filled |= SURVEY_INFO_IN_USE;
4400
2e1dea40
MK
4401exit:
4402 mutex_unlock(&ar->conf_mutex);
4403 return ret;
4404}
4405
51ab1a0a
JD
4406/* Helper table for legacy fixed_rate/bitrate_mask */
4407static const u8 cck_ofdm_rate[] = {
4408 /* CCK */
4409 3, /* 1Mbps */
4410 2, /* 2Mbps */
4411 1, /* 5.5Mbps */
4412 0, /* 11Mbps */
4413 /* OFDM */
4414 3, /* 6Mbps */
4415 7, /* 9Mbps */
4416 2, /* 12Mbps */
4417 6, /* 18Mbps */
4418 1, /* 24Mbps */
4419 5, /* 36Mbps */
4420 0, /* 48Mbps */
4421 4, /* 54Mbps */
4422};
4423
4424/* Check if only one bit set */
4425static int ath10k_check_single_mask(u32 mask)
4426{
4427 int bit;
4428
4429 bit = ffs(mask);
4430 if (!bit)
4431 return 0;
4432
4433 mask &= ~BIT(bit - 1);
4434 if (mask)
4435 return 2;
4436
4437 return 1;
4438}
4439
4440static bool
4441ath10k_default_bitrate_mask(struct ath10k *ar,
4442 enum ieee80211_band band,
4443 const struct cfg80211_bitrate_mask *mask)
4444{
4445 u32 legacy = 0x00ff;
4446 u8 ht = 0xff, i;
4447 u16 vht = 0x3ff;
b116ea19
BG
4448 u16 nrf = ar->num_rf_chains;
4449
4450 if (ar->cfg_tx_chainmask)
4451 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
51ab1a0a
JD
4452
4453 switch (band) {
4454 case IEEE80211_BAND_2GHZ:
4455 legacy = 0x00fff;
4456 vht = 0;
4457 break;
4458 case IEEE80211_BAND_5GHZ:
4459 break;
4460 default:
4461 return false;
4462 }
4463
4464 if (mask->control[band].legacy != legacy)
4465 return false;
4466
b116ea19 4467 for (i = 0; i < nrf; i++)
51ab1a0a
JD
4468 if (mask->control[band].ht_mcs[i] != ht)
4469 return false;
4470
b116ea19 4471 for (i = 0; i < nrf; i++)
51ab1a0a
JD
4472 if (mask->control[band].vht_mcs[i] != vht)
4473 return false;
4474
4475 return true;
4476}
4477
4478static bool
4479ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4480 enum ieee80211_band band,
4481 u8 *fixed_nss)
4482{
4483 int ht_nss = 0, vht_nss = 0, i;
4484
4485 /* check legacy */
4486 if (ath10k_check_single_mask(mask->control[band].legacy))
4487 return false;
4488
4489 /* check HT */
4490 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
4491 if (mask->control[band].ht_mcs[i] == 0xff)
4492 continue;
4493 else if (mask->control[band].ht_mcs[i] == 0x00)
4494 break;
d8bb26b9
KV
4495
4496 return false;
51ab1a0a
JD
4497 }
4498
4499 ht_nss = i;
4500
4501 /* check VHT */
4502 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
4503 if (mask->control[band].vht_mcs[i] == 0x03ff)
4504 continue;
4505 else if (mask->control[band].vht_mcs[i] == 0x0000)
4506 break;
d8bb26b9
KV
4507
4508 return false;
51ab1a0a
JD
4509 }
4510
4511 vht_nss = i;
4512
4513 if (ht_nss > 0 && vht_nss > 0)
4514 return false;
4515
4516 if (ht_nss)
4517 *fixed_nss = ht_nss;
4518 else if (vht_nss)
4519 *fixed_nss = vht_nss;
4520 else
4521 return false;
4522
4523 return true;
4524}
4525
4526static bool
4527ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
4528 enum ieee80211_band band,
4529 enum wmi_rate_preamble *preamble)
4530{
4531 int legacy = 0, ht = 0, vht = 0, i;
4532
4533 *preamble = WMI_RATE_PREAMBLE_OFDM;
4534
4535 /* check legacy */
4536 legacy = ath10k_check_single_mask(mask->control[band].legacy);
4537 if (legacy > 1)
4538 return false;
4539
4540 /* check HT */
4541 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4542 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
4543 if (ht > 1)
4544 return false;
4545
4546 /* check VHT */
4547 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4548 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
4549 if (vht > 1)
4550 return false;
4551
4552 /* Currently we support only one fixed_rate */
4553 if ((legacy + ht + vht) != 1)
4554 return false;
4555
4556 if (ht)
4557 *preamble = WMI_RATE_PREAMBLE_HT;
4558 else if (vht)
4559 *preamble = WMI_RATE_PREAMBLE_VHT;
4560
4561 return true;
4562}
4563
4564static bool
7aa7a72a
MK
4565ath10k_bitrate_mask_rate(struct ath10k *ar,
4566 const struct cfg80211_bitrate_mask *mask,
51ab1a0a
JD
4567 enum ieee80211_band band,
4568 u8 *fixed_rate,
4569 u8 *fixed_nss)
4570{
4571 u8 rate = 0, pream = 0, nss = 0, i;
4572 enum wmi_rate_preamble preamble;
4573
4574 /* Check if single rate correct */
4575 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
4576 return false;
4577
4578 pream = preamble;
4579
4580 switch (preamble) {
4581 case WMI_RATE_PREAMBLE_CCK:
4582 case WMI_RATE_PREAMBLE_OFDM:
4583 i = ffs(mask->control[band].legacy) - 1;
4584
4585 if (band == IEEE80211_BAND_2GHZ && i < 4)
4586 pream = WMI_RATE_PREAMBLE_CCK;
4587
4588 if (band == IEEE80211_BAND_5GHZ)
4589 i += 4;
4590
4591 if (i >= ARRAY_SIZE(cck_ofdm_rate))
4592 return false;
4593
4594 rate = cck_ofdm_rate[i];
4595 break;
4596 case WMI_RATE_PREAMBLE_HT:
4597 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
4598 if (mask->control[band].ht_mcs[i])
4599 break;
4600
4601 if (i == IEEE80211_HT_MCS_MASK_LEN)
4602 return false;
4603
4604 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
4605 nss = i;
4606 break;
4607 case WMI_RATE_PREAMBLE_VHT:
4608 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
4609 if (mask->control[band].vht_mcs[i])
4610 break;
4611
4612 if (i == NL80211_VHT_NSS_MAX)
4613 return false;
4614
4615 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
4616 nss = i;
4617 break;
4618 }
4619
4620 *fixed_nss = nss + 1;
4621 nss <<= 4;
4622 pream <<= 6;
4623
7aa7a72a 4624 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
51ab1a0a
JD
4625 pream, nss, rate);
4626
4627 *fixed_rate = pream | nss | rate;
4628
4629 return true;
4630}
4631
7aa7a72a
MK
4632static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
4633 const struct cfg80211_bitrate_mask *mask,
51ab1a0a
JD
4634 enum ieee80211_band band,
4635 u8 *fixed_rate,
4636 u8 *fixed_nss)
4637{
4638 /* First check full NSS mask, if we can simply limit NSS */
4639 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
4640 return true;
4641
4642 /* Next Check single rate is set */
7aa7a72a 4643 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
51ab1a0a
JD
4644}
4645
4646static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
4647 u8 fixed_rate,
9f81f725
JD
4648 u8 fixed_nss,
4649 u8 force_sgi)
51ab1a0a
JD
4650{
4651 struct ath10k *ar = arvif->ar;
4652 u32 vdev_param;
4653 int ret = 0;
4654
4655 mutex_lock(&ar->conf_mutex);
4656
4657 if (arvif->fixed_rate == fixed_rate &&
9f81f725
JD
4658 arvif->fixed_nss == fixed_nss &&
4659 arvif->force_sgi == force_sgi)
51ab1a0a
JD
4660 goto exit;
4661
4662 if (fixed_rate == WMI_FIXED_RATE_NONE)
7aa7a72a 4663 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
51ab1a0a 4664
9f81f725 4665 if (force_sgi)
7aa7a72a 4666 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
9f81f725 4667
51ab1a0a
JD
4668 vdev_param = ar->wmi.vdev_param->fixed_rate;
4669 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4670 vdev_param, fixed_rate);
4671 if (ret) {
7aa7a72a 4672 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
51ab1a0a
JD
4673 fixed_rate, ret);
4674 ret = -EINVAL;
4675 goto exit;
4676 }
4677
4678 arvif->fixed_rate = fixed_rate;
4679
4680 vdev_param = ar->wmi.vdev_param->nss;
4681 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4682 vdev_param, fixed_nss);
4683
4684 if (ret) {
7aa7a72a 4685 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
51ab1a0a
JD
4686 fixed_nss, ret);
4687 ret = -EINVAL;
4688 goto exit;
4689 }
4690
4691 arvif->fixed_nss = fixed_nss;
4692
9f81f725
JD
4693 vdev_param = ar->wmi.vdev_param->sgi;
4694 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4695 force_sgi);
4696
4697 if (ret) {
7aa7a72a 4698 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
9f81f725
JD
4699 force_sgi, ret);
4700 ret = -EINVAL;
4701 goto exit;
4702 }
4703
4704 arvif->force_sgi = force_sgi;
4705
51ab1a0a
JD
4706exit:
4707 mutex_unlock(&ar->conf_mutex);
4708 return ret;
4709}
4710
4711static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
4712 struct ieee80211_vif *vif,
4713 const struct cfg80211_bitrate_mask *mask)
4714{
4715 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4716 struct ath10k *ar = arvif->ar;
4717 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
4718 u8 fixed_rate = WMI_FIXED_RATE_NONE;
4719 u8 fixed_nss = ar->num_rf_chains;
9f81f725
JD
4720 u8 force_sgi;
4721
b116ea19
BG
4722 if (ar->cfg_tx_chainmask)
4723 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4724
9f81f725
JD
4725 force_sgi = mask->control[band].gi;
4726 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
4727 return -EINVAL;
51ab1a0a
JD
4728
4729 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
7aa7a72a 4730 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
51ab1a0a
JD
4731 &fixed_rate,
4732 &fixed_nss))
4733 return -EINVAL;
4734 }
4735
9f81f725 4736 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
7aa7a72a 4737 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
9f81f725
JD
4738 return -EINVAL;
4739 }
4740
4741 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
4742 fixed_nss, force_sgi);
51ab1a0a
JD
4743}
4744
9797febc
MK
4745static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
4746 struct ieee80211_vif *vif,
4747 struct ieee80211_sta *sta,
4748 u32 changed)
4749{
4750 struct ath10k *ar = hw->priv;
4751 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4752 u32 bw, smps;
4753
4754 spin_lock_bh(&ar->data_lock);
4755
7aa7a72a 4756 ath10k_dbg(ar, ATH10K_DBG_MAC,
9797febc
MK
4757 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
4758 sta->addr, changed, sta->bandwidth, sta->rx_nss,
4759 sta->smps_mode);
4760
4761 if (changed & IEEE80211_RC_BW_CHANGED) {
4762 bw = WMI_PEER_CHWIDTH_20MHZ;
4763
4764 switch (sta->bandwidth) {
4765 case IEEE80211_STA_RX_BW_20:
4766 bw = WMI_PEER_CHWIDTH_20MHZ;
4767 break;
4768 case IEEE80211_STA_RX_BW_40:
4769 bw = WMI_PEER_CHWIDTH_40MHZ;
4770 break;
4771 case IEEE80211_STA_RX_BW_80:
4772 bw = WMI_PEER_CHWIDTH_80MHZ;
4773 break;
4774 case IEEE80211_STA_RX_BW_160:
7aa7a72a 4775 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
be6546fc 4776 sta->bandwidth, sta->addr);
9797febc
MK
4777 bw = WMI_PEER_CHWIDTH_20MHZ;
4778 break;
4779 }
4780
4781 arsta->bw = bw;
4782 }
4783
4784 if (changed & IEEE80211_RC_NSS_CHANGED)
4785 arsta->nss = sta->rx_nss;
4786
4787 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4788 smps = WMI_PEER_SMPS_PS_NONE;
4789
4790 switch (sta->smps_mode) {
4791 case IEEE80211_SMPS_AUTOMATIC:
4792 case IEEE80211_SMPS_OFF:
4793 smps = WMI_PEER_SMPS_PS_NONE;
4794 break;
4795 case IEEE80211_SMPS_STATIC:
4796 smps = WMI_PEER_SMPS_STATIC;
4797 break;
4798 case IEEE80211_SMPS_DYNAMIC:
4799 smps = WMI_PEER_SMPS_DYNAMIC;
4800 break;
4801 case IEEE80211_SMPS_NUM_MODES:
7aa7a72a 4802 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
be6546fc 4803 sta->smps_mode, sta->addr);
9797febc
MK
4804 smps = WMI_PEER_SMPS_PS_NONE;
4805 break;
4806 }
4807
4808 arsta->smps = smps;
4809 }
4810
9797febc
MK
4811 arsta->changed |= changed;
4812
4813 spin_unlock_bh(&ar->data_lock);
4814
4815 ieee80211_queue_work(hw, &arsta->update_wk);
4816}
4817
26ebbccf
CYY
4818static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
4819{
4820 /*
4821 * FIXME: Return 0 for time being. Need to figure out whether FW
4822 * has the API to fetch 64-bit local TSF
4823 */
4824
4825 return 0;
4826}
4827
aa5b4fbc
MK
4828static int ath10k_ampdu_action(struct ieee80211_hw *hw,
4829 struct ieee80211_vif *vif,
4830 enum ieee80211_ampdu_mlme_action action,
4831 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
4832 u8 buf_size)
4833{
7aa7a72a 4834 struct ath10k *ar = hw->priv;
aa5b4fbc
MK
4835 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4836
7aa7a72a 4837 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
aa5b4fbc
MK
4838 arvif->vdev_id, sta->addr, tid, action);
4839
4840 switch (action) {
4841 case IEEE80211_AMPDU_RX_START:
4842 case IEEE80211_AMPDU_RX_STOP:
4843 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
4844 * creation/removal. Do we need to verify this?
4845 */
4846 return 0;
4847 case IEEE80211_AMPDU_TX_START:
4848 case IEEE80211_AMPDU_TX_STOP_CONT:
4849 case IEEE80211_AMPDU_TX_STOP_FLUSH:
4850 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
4851 case IEEE80211_AMPDU_TX_OPERATIONAL:
4852 /* Firmware offloads Tx aggregation entirely so deny mac80211
4853 * Tx aggregation requests.
4854 */
4855 return -EOPNOTSUPP;
4856 }
4857
4858 return -EINVAL;
4859}
4860
5e3dd157
KV
4861static const struct ieee80211_ops ath10k_ops = {
4862 .tx = ath10k_tx,
4863 .start = ath10k_start,
4864 .stop = ath10k_stop,
4865 .config = ath10k_config,
4866 .add_interface = ath10k_add_interface,
4867 .remove_interface = ath10k_remove_interface,
4868 .configure_filter = ath10k_configure_filter,
4869 .bss_info_changed = ath10k_bss_info_changed,
4870 .hw_scan = ath10k_hw_scan,
4871 .cancel_hw_scan = ath10k_cancel_hw_scan,
4872 .set_key = ath10k_set_key,
4873 .sta_state = ath10k_sta_state,
4874 .conf_tx = ath10k_conf_tx,
4875 .remain_on_channel = ath10k_remain_on_channel,
4876 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
4877 .set_rts_threshold = ath10k_set_rts_threshold,
5e3dd157
KV
4878 .flush = ath10k_flush,
4879 .tx_last_beacon = ath10k_tx_last_beacon,
46acf7bb
BG
4880 .set_antenna = ath10k_set_antenna,
4881 .get_antenna = ath10k_get_antenna,
cf2c92d8 4882 .reconfig_complete = ath10k_reconfig_complete,
2e1dea40 4883 .get_survey = ath10k_get_survey,
51ab1a0a 4884 .set_bitrate_mask = ath10k_set_bitrate_mask,
9797febc 4885 .sta_rc_update = ath10k_sta_rc_update,
26ebbccf 4886 .get_tsf = ath10k_get_tsf,
aa5b4fbc 4887 .ampdu_action = ath10k_ampdu_action,
6cddcc7a
BG
4888 .get_et_sset_count = ath10k_debug_get_et_sset_count,
4889 .get_et_stats = ath10k_debug_get_et_stats,
4890 .get_et_strings = ath10k_debug_get_et_strings,
43d2a30f
KV
4891
4892 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
4893
8cd13cad
MK
4894#ifdef CONFIG_PM
4895 .suspend = ath10k_suspend,
4896 .resume = ath10k_resume,
4897#endif
f5045988
RM
4898#ifdef CONFIG_MAC80211_DEBUGFS
4899 .sta_add_debugfs = ath10k_sta_add_debugfs,
4900#endif
5e3dd157
KV
4901};
4902
4903#define RATETAB_ENT(_rate, _rateid, _flags) { \
4904 .bitrate = (_rate), \
4905 .flags = (_flags), \
4906 .hw_value = (_rateid), \
4907}
4908
4909#define CHAN2G(_channel, _freq, _flags) { \
4910 .band = IEEE80211_BAND_2GHZ, \
4911 .hw_value = (_channel), \
4912 .center_freq = (_freq), \
4913 .flags = (_flags), \
4914 .max_antenna_gain = 0, \
4915 .max_power = 30, \
4916}
4917
4918#define CHAN5G(_channel, _freq, _flags) { \
4919 .band = IEEE80211_BAND_5GHZ, \
4920 .hw_value = (_channel), \
4921 .center_freq = (_freq), \
4922 .flags = (_flags), \
4923 .max_antenna_gain = 0, \
4924 .max_power = 30, \
4925}
4926
4927static const struct ieee80211_channel ath10k_2ghz_channels[] = {
4928 CHAN2G(1, 2412, 0),
4929 CHAN2G(2, 2417, 0),
4930 CHAN2G(3, 2422, 0),
4931 CHAN2G(4, 2427, 0),
4932 CHAN2G(5, 2432, 0),
4933 CHAN2G(6, 2437, 0),
4934 CHAN2G(7, 2442, 0),
4935 CHAN2G(8, 2447, 0),
4936 CHAN2G(9, 2452, 0),
4937 CHAN2G(10, 2457, 0),
4938 CHAN2G(11, 2462, 0),
4939 CHAN2G(12, 2467, 0),
4940 CHAN2G(13, 2472, 0),
4941 CHAN2G(14, 2484, 0),
4942};
4943
4944static const struct ieee80211_channel ath10k_5ghz_channels[] = {
429ff56a
MK
4945 CHAN5G(36, 5180, 0),
4946 CHAN5G(40, 5200, 0),
4947 CHAN5G(44, 5220, 0),
4948 CHAN5G(48, 5240, 0),
4949 CHAN5G(52, 5260, 0),
4950 CHAN5G(56, 5280, 0),
4951 CHAN5G(60, 5300, 0),
4952 CHAN5G(64, 5320, 0),
4953 CHAN5G(100, 5500, 0),
4954 CHAN5G(104, 5520, 0),
4955 CHAN5G(108, 5540, 0),
4956 CHAN5G(112, 5560, 0),
4957 CHAN5G(116, 5580, 0),
4958 CHAN5G(120, 5600, 0),
4959 CHAN5G(124, 5620, 0),
4960 CHAN5G(128, 5640, 0),
4961 CHAN5G(132, 5660, 0),
4962 CHAN5G(136, 5680, 0),
4963 CHAN5G(140, 5700, 0),
4964 CHAN5G(149, 5745, 0),
4965 CHAN5G(153, 5765, 0),
4966 CHAN5G(157, 5785, 0),
4967 CHAN5G(161, 5805, 0),
4968 CHAN5G(165, 5825, 0),
5e3dd157
KV
4969};
4970
91b12089
MK
4971/* Note: Be careful if you re-order these. There is code which depends on this
4972 * ordering.
4973 */
5e3dd157
KV
4974static struct ieee80211_rate ath10k_rates[] = {
4975 /* CCK */
4976 RATETAB_ENT(10, 0x82, 0),
4977 RATETAB_ENT(20, 0x84, 0),
4978 RATETAB_ENT(55, 0x8b, 0),
4979 RATETAB_ENT(110, 0x96, 0),
4980 /* OFDM */
4981 RATETAB_ENT(60, 0x0c, 0),
4982 RATETAB_ENT(90, 0x12, 0),
4983 RATETAB_ENT(120, 0x18, 0),
4984 RATETAB_ENT(180, 0x24, 0),
4985 RATETAB_ENT(240, 0x30, 0),
4986 RATETAB_ENT(360, 0x48, 0),
4987 RATETAB_ENT(480, 0x60, 0),
4988 RATETAB_ENT(540, 0x6c, 0),
4989};
4990
4991#define ath10k_a_rates (ath10k_rates + 4)
4992#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
4993#define ath10k_g_rates (ath10k_rates + 0)
4994#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
4995
e7b54194 4996struct ath10k *ath10k_mac_create(size_t priv_size)
5e3dd157
KV
4997{
4998 struct ieee80211_hw *hw;
4999 struct ath10k *ar;
5000
e7b54194 5001 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5e3dd157
KV
5002 if (!hw)
5003 return NULL;
5004
5005 ar = hw->priv;
5006 ar->hw = hw;
5007
5008 return ar;
5009}
5010
5011void ath10k_mac_destroy(struct ath10k *ar)
5012{
5013 ieee80211_free_hw(ar->hw);
5014}
5015
5016static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5017 {
5018 .max = 8,
5019 .types = BIT(NL80211_IFTYPE_STATION)
5020 | BIT(NL80211_IFTYPE_P2P_CLIENT)
d531cb85
MK
5021 },
5022 {
5023 .max = 3,
5024 .types = BIT(NL80211_IFTYPE_P2P_GO)
5025 },
5026 {
75d2bd48
MK
5027 .max = 1,
5028 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5029 },
5030 {
d531cb85
MK
5031 .max = 7,
5032 .types = BIT(NL80211_IFTYPE_AP)
5033 },
5e3dd157
KV
5034};
5035
f259509b 5036static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
e8a50f8b
MP
5037 {
5038 .max = 8,
5039 .types = BIT(NL80211_IFTYPE_AP)
5040 },
5041};
e8a50f8b
MP
5042
5043static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5044 {
5045 .limits = ath10k_if_limits,
5046 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5047 .max_interfaces = 8,
5048 .num_different_channels = 1,
5049 .beacon_int_infra_match = true,
5050 },
f259509b
BM
5051};
5052
5053static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
e8a50f8b 5054 {
f259509b
BM
5055 .limits = ath10k_10x_if_limits,
5056 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
e8a50f8b
MP
5057 .max_interfaces = 8,
5058 .num_different_channels = 1,
5059 .beacon_int_infra_match = true,
f259509b 5060#ifdef CONFIG_ATH10K_DFS_CERTIFIED
e8a50f8b
MP
5061 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5062 BIT(NL80211_CHAN_WIDTH_20) |
5063 BIT(NL80211_CHAN_WIDTH_40) |
5064 BIT(NL80211_CHAN_WIDTH_80),
e8a50f8b 5065#endif
f259509b 5066 },
5e3dd157
KV
5067};
5068
5069static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5070{
5071 struct ieee80211_sta_vht_cap vht_cap = {0};
5072 u16 mcs_map;
8865bee4 5073 int i;
5e3dd157
KV
5074
5075 vht_cap.vht_supported = 1;
5076 vht_cap.cap = ar->vht_cap_info;
5077
8865bee4
MK
5078 mcs_map = 0;
5079 for (i = 0; i < 8; i++) {
5080 if (i < ar->num_rf_chains)
5081 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5082 else
5083 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5084 }
5e3dd157
KV
5085
5086 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5087 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5088
5089 return vht_cap;
5090}
5091
5092static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5093{
5094 int i;
5095 struct ieee80211_sta_ht_cap ht_cap = {0};
5096
5097 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5098 return ht_cap;
5099
5100 ht_cap.ht_supported = 1;
5101 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5102 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5103 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5104 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5105 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5106
5107 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5108 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5109
5110 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5111 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5112
5113 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5114 u32 smps;
5115
5116 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5117 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5118
5119 ht_cap.cap |= smps;
5120 }
5121
5122 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5123 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5124
5125 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5126 u32 stbc;
5127
5128 stbc = ar->ht_cap_info;
5129 stbc &= WMI_HT_CAP_RX_STBC;
5130 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5131 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5132 stbc &= IEEE80211_HT_CAP_RX_STBC;
5133
5134 ht_cap.cap |= stbc;
5135 }
5136
5137 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5138 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5139
5140 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5141 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5142
5143 /* max AMSDU is implicitly taken from vht_cap_info */
5144 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5145 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5146
8865bee4 5147 for (i = 0; i < ar->num_rf_chains; i++)
5e3dd157
KV
5148 ht_cap.mcs.rx_mask[i] = 0xFF;
5149
5150 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5151
5152 return ht_cap;
5153}
5154
5e3dd157
KV
5155static void ath10k_get_arvif_iter(void *data, u8 *mac,
5156 struct ieee80211_vif *vif)
5157{
5158 struct ath10k_vif_iter *arvif_iter = data;
5159 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5160
5161 if (arvif->vdev_id == arvif_iter->vdev_id)
5162 arvif_iter->arvif = arvif;
5163}
5164
5165struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5166{
5167 struct ath10k_vif_iter arvif_iter;
5168 u32 flags;
5169
5170 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5171 arvif_iter.vdev_id = vdev_id;
5172
5173 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5174 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5175 flags,
5176 ath10k_get_arvif_iter,
5177 &arvif_iter);
5178 if (!arvif_iter.arvif) {
7aa7a72a 5179 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5e3dd157
KV
5180 return NULL;
5181 }
5182
5183 return arvif_iter.arvif;
5184}
5185
5186int ath10k_mac_register(struct ath10k *ar)
5187{
5188 struct ieee80211_supported_band *band;
5189 struct ieee80211_sta_vht_cap vht_cap;
5190 struct ieee80211_sta_ht_cap ht_cap;
5191 void *channels;
5192 int ret;
5193
5194 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5195
5196 SET_IEEE80211_DEV(ar->hw, ar->dev);
5197
5198 ht_cap = ath10k_get_ht_cap(ar);
5199 vht_cap = ath10k_create_vht_cap(ar);
5200
5201 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5202 channels = kmemdup(ath10k_2ghz_channels,
5203 sizeof(ath10k_2ghz_channels),
5204 GFP_KERNEL);
d6015b27
MK
5205 if (!channels) {
5206 ret = -ENOMEM;
5207 goto err_free;
5208 }
5e3dd157
KV
5209
5210 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5211 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5212 band->channels = channels;
5213 band->n_bitrates = ath10k_g_rates_size;
5214 band->bitrates = ath10k_g_rates;
5215 band->ht_cap = ht_cap;
5216
5217 /* vht is not supported in 2.4 GHz */
5218
5219 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5220 }
5221
5222 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5223 channels = kmemdup(ath10k_5ghz_channels,
5224 sizeof(ath10k_5ghz_channels),
5225 GFP_KERNEL);
5226 if (!channels) {
d6015b27
MK
5227 ret = -ENOMEM;
5228 goto err_free;
5e3dd157
KV
5229 }
5230
5231 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5232 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5233 band->channels = channels;
5234 band->n_bitrates = ath10k_a_rates_size;
5235 band->bitrates = ath10k_a_rates;
5236 band->ht_cap = ht_cap;
5237 band->vht_cap = vht_cap;
5238 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5239 }
5240
5241 ar->hw->wiphy->interface_modes =
5242 BIT(NL80211_IFTYPE_STATION) |
d354181f
BM
5243 BIT(NL80211_IFTYPE_AP);
5244
46acf7bb
BG
5245 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5246 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5247
d354181f
BM
5248 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5249 ar->hw->wiphy->interface_modes |=
75d2bd48 5250 BIT(NL80211_IFTYPE_P2P_DEVICE) |
d354181f
BM
5251 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5252 BIT(NL80211_IFTYPE_P2P_GO);
5e3dd157
KV
5253
5254 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5255 IEEE80211_HW_SUPPORTS_PS |
5256 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5257 IEEE80211_HW_SUPPORTS_UAPSD |
5258 IEEE80211_HW_MFP_CAPABLE |
5259 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5260 IEEE80211_HW_HAS_RATE_CONTROL |
2f0f1121
JD
5261 IEEE80211_HW_AP_LINK_PS |
5262 IEEE80211_HW_SPECTRUM_MGMT;
5e3dd157 5263
0d8614b4
EP
5264 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5265
5e3dd157 5266 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
0d8614b4 5267 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5e3dd157
KV
5268
5269 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5270 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5271 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5272 }
5273
5274 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5275 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5276
5277 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9797febc 5278 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5e3dd157 5279
5e3dd157
KV
5280 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5281
fbb8f1b7
MK
5282 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5283 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5284
5285 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5286 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5287 * correct Probe Responses. This is more of a hack advert..
5288 */
5289 ar->hw->wiphy->probe_resp_offload |=
5290 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5291 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5292 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5293 }
5294
5e3dd157 5295 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
c2df44b3 5296 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5e3dd157
KV
5297 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5298
5299 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
78157a1c
RM
5300 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5301
5e3dd157
KV
5302 /*
5303 * on LL hardware queues are managed entirely by the FW
5304 * so we only advertise to mac we can do the queues thing
5305 */
5306 ar->hw->queues = 4;
5307
5cc7caf4
KV
5308 switch (ar->wmi.op_version) {
5309 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5310 case ATH10K_FW_WMI_OP_VERSION_TLV:
f259509b
BM
5311 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5312 ar->hw->wiphy->n_iface_combinations =
5313 ARRAY_SIZE(ath10k_if_comb);
cf850d1d 5314 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5cc7caf4
KV
5315 break;
5316 case ATH10K_FW_WMI_OP_VERSION_10_1:
5317 case ATH10K_FW_WMI_OP_VERSION_10_2:
4a16fbec 5318 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5cc7caf4
KV
5319 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5320 ar->hw->wiphy->n_iface_combinations =
5321 ARRAY_SIZE(ath10k_10x_if_comb);
5322 break;
5323 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5324 case ATH10K_FW_WMI_OP_VERSION_MAX:
5325 WARN_ON(1);
5326 ret = -EINVAL;
5327 goto err_free;
f259509b 5328 }
5e3dd157 5329
7c199997
MK
5330 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5331
9702c686
JD
5332 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5333 /* Init ath dfs pattern detector */
5334 ar->ath_common.debug_mask = ATH_DBG_DFS;
5335 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5336 NL80211_DFS_UNSET);
5337
5338 if (!ar->dfs_detector)
7aa7a72a 5339 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
9702c686
JD
5340 }
5341
5e3dd157
KV
5342 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5343 ath10k_reg_notifier);
5344 if (ret) {
7aa7a72a 5345 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
d6015b27 5346 goto err_free;
5e3dd157
KV
5347 }
5348
5349 ret = ieee80211_register_hw(ar->hw);
5350 if (ret) {
7aa7a72a 5351 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
d6015b27 5352 goto err_free;
5e3dd157
KV
5353 }
5354
5355 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5356 ret = regulatory_hint(ar->hw->wiphy,
5357 ar->ath_common.regulatory.alpha2);
5358 if (ret)
d6015b27 5359 goto err_unregister;
5e3dd157
KV
5360 }
5361
5362 return 0;
d6015b27
MK
5363
5364err_unregister:
5e3dd157 5365 ieee80211_unregister_hw(ar->hw);
d6015b27
MK
5366err_free:
5367 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5368 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5369
5e3dd157
KV
5370 return ret;
5371}
5372
5373void ath10k_mac_unregister(struct ath10k *ar)
5374{
5375 ieee80211_unregister_hw(ar->hw);
5376
9702c686
JD
5377 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5378 ar->dfs_detector->exit(ar->dfs_detector);
5379
5e3dd157
KV
5380 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5381 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5382
5383 SET_IEEE80211_DEV(ar->hw, NULL);
5384}