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