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