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