]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/net/wireless/ath/ath10k/mac.c
cfg80211: remove enum ieee80211_band
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / ath / ath10k / mac.c
CommitLineData
5e3dd157
KV
1/*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "mac.h"
19
20#include <net/mac80211.h>
21#include <linux/etherdevice.h>
22
8cd13cad 23#include "hif.h"
5e3dd157
KV
24#include "core.h"
25#include "debug.h"
26#include "wmi.h"
27#include "htt.h"
28#include "txrx.h"
43d2a30f 29#include "testmode.h"
d7579d12 30#include "wmi.h"
b4aa539d 31#include "wmi-tlv.h"
d7579d12 32#include "wmi-ops.h"
5fd3ac3c 33#include "wow.h"
5e3dd157 34
dcc33098
MK
35/*********/
36/* Rates */
37/*********/
38
dcc33098 39static struct ieee80211_rate ath10k_rates[] = {
5528e032
MK
40 { .bitrate = 10,
41 .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
42 { .bitrate = 20,
43 .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
44 .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
45 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
46 { .bitrate = 55,
47 .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
48 .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
49 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
50 { .bitrate = 110,
51 .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
52 .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
53 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
af00148f
MK
54
55 { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
56 { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
57 { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
58 { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
59 { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
60 { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
61 { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
62 { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
dcc33098
MK
63};
64
8d7aa6bc
MK
65#define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
66
67#define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
68#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
69 ATH10K_MAC_FIRST_OFDM_RATE_IDX)
dcc33098
MK
70#define ath10k_g_rates (ath10k_rates + 0)
71#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
72
486017cc
MK
73static bool ath10k_mac_bitrate_is_cck(int bitrate)
74{
75 switch (bitrate) {
76 case 10:
77 case 20:
78 case 55:
79 case 110:
80 return true;
81 }
82
83 return false;
84}
85
86static u8 ath10k_mac_bitrate_to_rate(int bitrate)
87{
88 return DIV_ROUND_UP(bitrate, 5) |
89 (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
90}
91
5528e032 92u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
4b7f353b 93 u8 hw_rate, bool cck)
5528e032
MK
94{
95 const struct ieee80211_rate *rate;
96 int i;
97
98 for (i = 0; i < sband->n_bitrates; i++) {
99 rate = &sband->bitrates[i];
100
4b7f353b
YL
101 if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
102 continue;
103
5528e032
MK
104 if (rate->hw_value == hw_rate)
105 return i;
106 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
107 rate->hw_value_short == hw_rate)
108 return i;
109 }
110
111 return 0;
112}
113
01cebe1c
MK
114u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
115 u32 bitrate)
116{
117 int i;
118
119 for (i = 0; i < sband->n_bitrates; i++)
120 if (sband->bitrates[i].bitrate == bitrate)
121 return i;
122
123 return 0;
124}
125
3ae54225
MK
126static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
127{
128 switch ((mcs_map >> (2 * nss)) & 0x3) {
129 case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
130 case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
131 case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
132 }
133 return 0;
134}
135
45c9abc0
MK
136static u32
137ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
138{
139 int nss;
140
141 for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
142 if (ht_mcs_mask[nss])
143 return nss + 1;
144
145 return 1;
146}
147
148static u32
149ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
150{
151 int nss;
152
153 for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
154 if (vht_mcs_mask[nss])
155 return nss + 1;
156
157 return 1;
158}
5e3dd157
KV
159
160/**********/
161/* Crypto */
162/**********/
163
164static int ath10k_send_key(struct ath10k_vif *arvif,
165 struct ieee80211_key_conf *key,
166 enum set_key_cmd cmd,
370e5673 167 const u8 *macaddr, u32 flags)
5e3dd157 168{
7aa7a72a 169 struct ath10k *ar = arvif->ar;
5e3dd157
KV
170 struct wmi_vdev_install_key_arg arg = {
171 .vdev_id = arvif->vdev_id,
172 .key_idx = key->keyidx,
173 .key_len = key->keylen,
174 .key_data = key->key,
370e5673 175 .key_flags = flags,
5e3dd157
KV
176 .macaddr = macaddr,
177 };
178
548db54c
MK
179 lockdep_assert_held(&arvif->ar->conf_mutex);
180
5e3dd157
KV
181 switch (key->cipher) {
182 case WLAN_CIPHER_SUITE_CCMP:
183 arg.key_cipher = WMI_CIPHER_AES_CCM;
e4e82e9a 184 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5e3dd157
KV
185 break;
186 case WLAN_CIPHER_SUITE_TKIP:
5e3dd157
KV
187 arg.key_cipher = WMI_CIPHER_TKIP;
188 arg.key_txmic_len = 8;
189 arg.key_rxmic_len = 8;
190 break;
191 case WLAN_CIPHER_SUITE_WEP40:
192 case WLAN_CIPHER_SUITE_WEP104:
193 arg.key_cipher = WMI_CIPHER_WEP;
5e3dd157 194 break;
3cb10943 195 case WLAN_CIPHER_SUITE_AES_CMAC:
d7131c04
BM
196 WARN_ON(1);
197 return -EINVAL;
5e3dd157 198 default:
7aa7a72a 199 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
5e3dd157
KV
200 return -EOPNOTSUPP;
201 }
202
b9e284e5 203 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
ccec9038 204 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
ccec9038 205
5e3dd157
KV
206 if (cmd == DISABLE_KEY) {
207 arg.key_cipher = WMI_CIPHER_NONE;
208 arg.key_data = NULL;
209 }
210
211 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
212}
213
214static int ath10k_install_key(struct ath10k_vif *arvif,
215 struct ieee80211_key_conf *key,
216 enum set_key_cmd cmd,
370e5673 217 const u8 *macaddr, u32 flags)
5e3dd157
KV
218{
219 struct ath10k *ar = arvif->ar;
220 int ret;
8e9904f5 221 unsigned long time_left;
5e3dd157 222
548db54c
MK
223 lockdep_assert_held(&ar->conf_mutex);
224
16735d02 225 reinit_completion(&ar->install_key_done);
5e3dd157 226
ccec9038
DL
227 if (arvif->nohwcrypt)
228 return 1;
229
370e5673 230 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
5e3dd157
KV
231 if (ret)
232 return ret;
233
8e9904f5
NMG
234 time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
235 if (time_left == 0)
5e3dd157
KV
236 return -ETIMEDOUT;
237
238 return 0;
239}
240
241static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
242 const u8 *addr)
243{
244 struct ath10k *ar = arvif->ar;
245 struct ath10k_peer *peer;
246 int ret;
247 int i;
370e5673 248 u32 flags;
5e3dd157
KV
249
250 lockdep_assert_held(&ar->conf_mutex);
251
8674d909 252 if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
7c97b72a
PO
253 arvif->vif->type != NL80211_IFTYPE_ADHOC &&
254 arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
8674d909
MK
255 return -EINVAL;
256
5e3dd157
KV
257 spin_lock_bh(&ar->data_lock);
258 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
259 spin_unlock_bh(&ar->data_lock);
260
261 if (!peer)
262 return -ENOENT;
263
264 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
265 if (arvif->wep_keys[i] == NULL)
266 continue;
370e5673 267
8674d909
MK
268 switch (arvif->vif->type) {
269 case NL80211_IFTYPE_AP:
270 flags = WMI_KEY_PAIRWISE;
271
272 if (arvif->def_wep_key_idx == i)
273 flags |= WMI_KEY_TX_USAGE;
274
275 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
276 SET_KEY, addr, flags);
277 if (ret < 0)
278 return ret;
279 break;
280 case NL80211_IFTYPE_ADHOC:
281 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
282 SET_KEY, addr,
283 WMI_KEY_PAIRWISE);
284 if (ret < 0)
285 return ret;
286
287 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
288 SET_KEY, addr, WMI_KEY_GROUP);
289 if (ret < 0)
290 return ret;
291 break;
292 default:
293 WARN_ON(1);
294 return -EINVAL;
295 }
5e3dd157 296
ae167131 297 spin_lock_bh(&ar->data_lock);
5e3dd157 298 peer->keys[i] = arvif->wep_keys[i];
ae167131 299 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
300 }
301
ce90b271
MK
302 /* In some cases (notably with static WEP IBSS with multiple keys)
303 * multicast Tx becomes broken. Both pairwise and groupwise keys are
304 * installed already. Using WMI_KEY_TX_USAGE in different combinations
305 * didn't seem help. Using def_keyid vdev parameter seems to be
306 * effective so use that.
307 *
308 * FIXME: Revisit. Perhaps this can be done in a less hacky way.
309 */
8674d909
MK
310 if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
311 return 0;
312
ce90b271
MK
313 if (arvif->def_wep_key_idx == -1)
314 return 0;
315
316 ret = ath10k_wmi_vdev_set_param(arvif->ar,
317 arvif->vdev_id,
318 arvif->ar->wmi.vdev_param->def_keyid,
319 arvif->def_wep_key_idx);
320 if (ret) {
321 ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
322 arvif->vdev_id, ret);
323 return ret;
324 }
325
5e3dd157
KV
326 return 0;
327}
328
329static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
330 const u8 *addr)
331{
332 struct ath10k *ar = arvif->ar;
333 struct ath10k_peer *peer;
334 int first_errno = 0;
335 int ret;
336 int i;
370e5673 337 u32 flags = 0;
5e3dd157
KV
338
339 lockdep_assert_held(&ar->conf_mutex);
340
341 spin_lock_bh(&ar->data_lock);
342 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
343 spin_unlock_bh(&ar->data_lock);
344
345 if (!peer)
346 return -ENOENT;
347
348 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
349 if (peer->keys[i] == NULL)
350 continue;
351
627613f8 352 /* key flags are not required to delete the key */
5e3dd157 353 ret = ath10k_install_key(arvif, peer->keys[i],
370e5673 354 DISABLE_KEY, addr, flags);
ccec9038 355 if (ret < 0 && first_errno == 0)
5e3dd157
KV
356 first_errno = ret;
357
ccec9038 358 if (ret < 0)
7aa7a72a 359 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
5e3dd157
KV
360 i, ret);
361
ae167131 362 spin_lock_bh(&ar->data_lock);
5e3dd157 363 peer->keys[i] = NULL;
ae167131 364 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
365 }
366
367 return first_errno;
368}
369
504f6cdf
SM
370bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
371 u8 keyidx)
372{
373 struct ath10k_peer *peer;
374 int i;
375
376 lockdep_assert_held(&ar->data_lock);
377
378 /* We don't know which vdev this peer belongs to,
379 * since WMI doesn't give us that information.
380 *
381 * FIXME: multi-bss needs to be handled.
382 */
383 peer = ath10k_peer_find(ar, 0, addr);
384 if (!peer)
385 return false;
386
387 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
388 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
389 return true;
390 }
391
392 return false;
393}
394
5e3dd157
KV
395static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
396 struct ieee80211_key_conf *key)
397{
398 struct ath10k *ar = arvif->ar;
399 struct ath10k_peer *peer;
400 u8 addr[ETH_ALEN];
401 int first_errno = 0;
402 int ret;
403 int i;
370e5673 404 u32 flags = 0;
5e3dd157
KV
405
406 lockdep_assert_held(&ar->conf_mutex);
407
408 for (;;) {
409 /* since ath10k_install_key we can't hold data_lock all the
410 * time, so we try to remove the keys incrementally */
411 spin_lock_bh(&ar->data_lock);
412 i = 0;
413 list_for_each_entry(peer, &ar->peers, list) {
414 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
415 if (peer->keys[i] == key) {
b25f32cb 416 ether_addr_copy(addr, peer->addr);
5e3dd157
KV
417 peer->keys[i] = NULL;
418 break;
419 }
420 }
421
422 if (i < ARRAY_SIZE(peer->keys))
423 break;
424 }
425 spin_unlock_bh(&ar->data_lock);
426
427 if (i == ARRAY_SIZE(peer->keys))
428 break;
627613f8 429 /* key flags are not required to delete the key */
370e5673 430 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
ccec9038 431 if (ret < 0 && first_errno == 0)
5e3dd157
KV
432 first_errno = ret;
433
434 if (ret)
7aa7a72a 435 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
be6546fc 436 addr, ret);
5e3dd157
KV
437 }
438
439 return first_errno;
440}
441
ad325cb5
MK
442static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
443 struct ieee80211_key_conf *key)
444{
445 struct ath10k *ar = arvif->ar;
446 struct ath10k_peer *peer;
447 int ret;
448
449 lockdep_assert_held(&ar->conf_mutex);
450
451 list_for_each_entry(peer, &ar->peers, list) {
452 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
453 continue;
454
455 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
456 continue;
457
458 if (peer->keys[key->keyidx] == key)
459 continue;
460
461 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
462 arvif->vdev_id, key->keyidx);
463
464 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
465 if (ret) {
466 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
467 arvif->vdev_id, peer->addr, ret);
468 return ret;
469 }
470 }
471
472 return 0;
473}
474
5e3dd157
KV
475/*********************/
476/* General utilities */
477/*********************/
478
479static inline enum wmi_phy_mode
480chan_to_phymode(const struct cfg80211_chan_def *chandef)
481{
482 enum wmi_phy_mode phymode = MODE_UNKNOWN;
483
484 switch (chandef->chan->band) {
57fbcce3 485 case NL80211_BAND_2GHZ:
5e3dd157
KV
486 switch (chandef->width) {
487 case NL80211_CHAN_WIDTH_20_NOHT:
6faab127
PO
488 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
489 phymode = MODE_11B;
490 else
491 phymode = MODE_11G;
5e3dd157
KV
492 break;
493 case NL80211_CHAN_WIDTH_20:
494 phymode = MODE_11NG_HT20;
495 break;
496 case NL80211_CHAN_WIDTH_40:
497 phymode = MODE_11NG_HT40;
498 break;
0f817ed5
JL
499 case NL80211_CHAN_WIDTH_5:
500 case NL80211_CHAN_WIDTH_10:
5e3dd157
KV
501 case NL80211_CHAN_WIDTH_80:
502 case NL80211_CHAN_WIDTH_80P80:
503 case NL80211_CHAN_WIDTH_160:
504 phymode = MODE_UNKNOWN;
505 break;
506 }
507 break;
57fbcce3 508 case NL80211_BAND_5GHZ:
5e3dd157
KV
509 switch (chandef->width) {
510 case NL80211_CHAN_WIDTH_20_NOHT:
511 phymode = MODE_11A;
512 break;
513 case NL80211_CHAN_WIDTH_20:
514 phymode = MODE_11NA_HT20;
515 break;
516 case NL80211_CHAN_WIDTH_40:
517 phymode = MODE_11NA_HT40;
518 break;
519 case NL80211_CHAN_WIDTH_80:
520 phymode = MODE_11AC_VHT80;
521 break;
0f817ed5
JL
522 case NL80211_CHAN_WIDTH_5:
523 case NL80211_CHAN_WIDTH_10:
5e3dd157
KV
524 case NL80211_CHAN_WIDTH_80P80:
525 case NL80211_CHAN_WIDTH_160:
526 phymode = MODE_UNKNOWN;
527 break;
528 }
529 break;
530 default:
531 break;
532 }
533
534 WARN_ON(phymode == MODE_UNKNOWN);
535 return phymode;
536}
537
538static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
539{
540/*
541 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
542 * 0 for no restriction
543 * 1 for 1/4 us
544 * 2 for 1/2 us
545 * 3 for 1 us
546 * 4 for 2 us
547 * 5 for 4 us
548 * 6 for 8 us
549 * 7 for 16 us
550 */
551 switch (mpdudensity) {
552 case 0:
553 return 0;
554 case 1:
555 case 2:
556 case 3:
557 /* Our lower layer calculations limit our precision to
558 1 microsecond */
559 return 1;
560 case 4:
561 return 2;
562 case 5:
563 return 4;
564 case 6:
565 return 8;
566 case 7:
567 return 16;
568 default:
569 return 0;
570 }
571}
572
500ff9f9
MK
573int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
574 struct cfg80211_chan_def *def)
575{
576 struct ieee80211_chanctx_conf *conf;
577
578 rcu_read_lock();
579 conf = rcu_dereference(vif->chanctx_conf);
580 if (!conf) {
581 rcu_read_unlock();
582 return -ENOENT;
583 }
584
585 *def = conf->def;
586 rcu_read_unlock();
587
588 return 0;
589}
590
591static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
592 struct ieee80211_chanctx_conf *conf,
593 void *data)
594{
595 int *num = data;
596
597 (*num)++;
598}
599
600static int ath10k_mac_num_chanctxs(struct ath10k *ar)
601{
602 int num = 0;
603
604 ieee80211_iter_chan_contexts_atomic(ar->hw,
605 ath10k_mac_num_chanctxs_iter,
606 &num);
607
608 return num;
609}
610
611static void
612ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
613 struct ieee80211_chanctx_conf *conf,
614 void *data)
615{
616 struct cfg80211_chan_def **def = data;
617
618 *def = &conf->def;
619}
620
6942726f
MK
621static int ath10k_peer_create(struct ath10k *ar,
622 struct ieee80211_vif *vif,
623 struct ieee80211_sta *sta,
624 u32 vdev_id,
625 const u8 *addr,
7390ed34 626 enum wmi_peer_type peer_type)
5e3dd157 627{
e04cafbc 628 struct ath10k_vif *arvif;
6942726f 629 struct ath10k_peer *peer;
e04cafbc 630 int num_peers = 0;
5e3dd157
KV
631 int ret;
632
633 lockdep_assert_held(&ar->conf_mutex);
634
e04cafbc
MK
635 num_peers = ar->num_peers;
636
637 /* Each vdev consumes a peer entry as well */
638 list_for_each_entry(arvif, &ar->arvifs, list)
639 num_peers++;
640
641 if (num_peers >= ar->max_num_peers)
cfd1061e
MK
642 return -ENOBUFS;
643
7390ed34 644 ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
479398b0 645 if (ret) {
7aa7a72a 646 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
69244e56 647 addr, vdev_id, ret);
5e3dd157 648 return ret;
479398b0 649 }
5e3dd157
KV
650
651 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
479398b0 652 if (ret) {
7aa7a72a 653 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
69244e56 654 addr, vdev_id, ret);
5e3dd157 655 return ret;
479398b0 656 }
292a753d 657
6942726f
MK
658 spin_lock_bh(&ar->data_lock);
659
660 peer = ath10k_peer_find(ar, vdev_id, addr);
661 if (!peer) {
662 ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
663 addr, vdev_id);
664 ath10k_wmi_peer_delete(ar, vdev_id, addr);
665 spin_unlock_bh(&ar->data_lock);
666 return -ENOENT;
667 }
668
669 peer->vif = vif;
670 peer->sta = sta;
671
672 spin_unlock_bh(&ar->data_lock);
673
0e759f36 674 ar->num_peers++;
5e3dd157
KV
675
676 return 0;
677}
678
5a13e76e
KV
679static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
680{
681 struct ath10k *ar = arvif->ar;
682 u32 param;
683 int ret;
684
685 param = ar->wmi.pdev_param->sta_kickout_th;
686 ret = ath10k_wmi_pdev_set_param(ar, param,
687 ATH10K_KICKOUT_THRESHOLD);
688 if (ret) {
7aa7a72a 689 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
69244e56 690 arvif->vdev_id, ret);
5a13e76e
KV
691 return ret;
692 }
693
694 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
695 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
696 ATH10K_KEEPALIVE_MIN_IDLE);
697 if (ret) {
7aa7a72a 698 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
69244e56 699 arvif->vdev_id, ret);
5a13e76e
KV
700 return ret;
701 }
702
703 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
704 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
705 ATH10K_KEEPALIVE_MAX_IDLE);
706 if (ret) {
7aa7a72a 707 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
69244e56 708 arvif->vdev_id, ret);
5a13e76e
KV
709 return ret;
710 }
711
712 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
713 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
714 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
715 if (ret) {
7aa7a72a 716 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
69244e56 717 arvif->vdev_id, ret);
5a13e76e
KV
718 return ret;
719 }
720
721 return 0;
722}
723
acab6400 724static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
424121c3 725{
6d1506e7
BM
726 struct ath10k *ar = arvif->ar;
727 u32 vdev_param;
728
6d1506e7
BM
729 vdev_param = ar->wmi.vdev_param->rts_threshold;
730 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
424121c3
MK
731}
732
5e3dd157
KV
733static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
734{
735 int ret;
736
737 lockdep_assert_held(&ar->conf_mutex);
738
739 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
740 if (ret)
741 return ret;
742
743 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
744 if (ret)
745 return ret;
746
0e759f36 747 ar->num_peers--;
0e759f36 748
5e3dd157
KV
749 return 0;
750}
751
752static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
753{
754 struct ath10k_peer *peer, *tmp;
6942726f 755 int peer_id;
5e3dd157
KV
756
757 lockdep_assert_held(&ar->conf_mutex);
758
759 spin_lock_bh(&ar->data_lock);
760 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
761 if (peer->vdev_id != vdev_id)
762 continue;
763
7aa7a72a 764 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
5e3dd157
KV
765 peer->addr, vdev_id);
766
6942726f
MK
767 for_each_set_bit(peer_id, peer->peer_ids,
768 ATH10K_MAX_NUM_PEER_IDS) {
769 ar->peer_map[peer_id] = NULL;
770 }
771
5e3dd157
KV
772 list_del(&peer->list);
773 kfree(peer);
0e759f36 774 ar->num_peers--;
5e3dd157
KV
775 }
776 spin_unlock_bh(&ar->data_lock);
777}
778
a96d7745
MK
779static void ath10k_peer_cleanup_all(struct ath10k *ar)
780{
781 struct ath10k_peer *peer, *tmp;
782
783 lockdep_assert_held(&ar->conf_mutex);
784
785 spin_lock_bh(&ar->data_lock);
786 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
787 list_del(&peer->list);
788 kfree(peer);
789 }
790 spin_unlock_bh(&ar->data_lock);
292a753d
MK
791
792 ar->num_peers = 0;
cfd1061e 793 ar->num_stations = 0;
a96d7745
MK
794}
795
75d85fd9
MP
796static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
797 struct ieee80211_sta *sta,
798 enum wmi_tdls_peer_state state)
799{
800 int ret;
801 struct wmi_tdls_peer_update_cmd_arg arg = {};
802 struct wmi_tdls_peer_capab_arg cap = {};
803 struct wmi_channel_arg chan_arg = {};
804
805 lockdep_assert_held(&ar->conf_mutex);
806
807 arg.vdev_id = vdev_id;
808 arg.peer_state = state;
809 ether_addr_copy(arg.addr, sta->addr);
810
811 cap.peer_max_sp = sta->max_sp;
812 cap.peer_uapsd_queues = sta->uapsd_queues;
813
814 if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
815 !sta->tdls_initiator)
816 cap.is_peer_responder = 1;
817
818 ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
819 if (ret) {
820 ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
821 arg.addr, vdev_id, ret);
822 return ret;
823 }
824
825 return 0;
826}
827
5e3dd157
KV
828/************************/
829/* Interface management */
830/************************/
831
64badcb6
MK
832void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
833{
834 struct ath10k *ar = arvif->ar;
835
836 lockdep_assert_held(&ar->data_lock);
837
838 if (!arvif->beacon)
839 return;
840
841 if (!arvif->beacon_buf)
842 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
843 arvif->beacon->len, DMA_TO_DEVICE);
844
af21319f
MK
845 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
846 arvif->beacon_state != ATH10K_BEACON_SENT))
847 return;
848
64badcb6
MK
849 dev_kfree_skb_any(arvif->beacon);
850
851 arvif->beacon = NULL;
af21319f 852 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
64badcb6
MK
853}
854
855static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
856{
857 struct ath10k *ar = arvif->ar;
858
859 lockdep_assert_held(&ar->data_lock);
860
861 ath10k_mac_vif_beacon_free(arvif);
862
863 if (arvif->beacon_buf) {
864 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
865 arvif->beacon_buf, arvif->beacon_paddr);
866 arvif->beacon_buf = NULL;
867 }
868}
869
5e3dd157
KV
870static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
871{
8e9904f5 872 unsigned long time_left;
5e3dd157 873
548db54c
MK
874 lockdep_assert_held(&ar->conf_mutex);
875
7962b0d8
MK
876 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
877 return -ESHUTDOWN;
878
8e9904f5
NMG
879 time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
880 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
881 if (time_left == 0)
5e3dd157
KV
882 return -ETIMEDOUT;
883
884 return 0;
885}
886
1bbc0975 887static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
5e3dd157 888{
500ff9f9 889 struct cfg80211_chan_def *chandef = NULL;
19be9e9a 890 struct ieee80211_channel *channel = NULL;
5e3dd157 891 struct wmi_vdev_start_request_arg arg = {};
5e3dd157
KV
892 int ret = 0;
893
894 lockdep_assert_held(&ar->conf_mutex);
895
500ff9f9
MK
896 ieee80211_iter_chan_contexts_atomic(ar->hw,
897 ath10k_mac_get_any_chandef_iter,
898 &chandef);
899 if (WARN_ON_ONCE(!chandef))
900 return -ENOENT;
901
902 channel = chandef->chan;
903
5e3dd157
KV
904 arg.vdev_id = vdev_id;
905 arg.channel.freq = channel->center_freq;
c930f744 906 arg.channel.band_center_freq1 = chandef->center_freq1;
5e3dd157
KV
907
908 /* TODO setup this dynamically, what in case we
909 don't have any vifs? */
c930f744 910 arg.channel.mode = chan_to_phymode(chandef);
e8a50f8b
MP
911 arg.channel.chan_radar =
912 !!(channel->flags & IEEE80211_CHAN_RADAR);
5e3dd157 913
89c5c843 914 arg.channel.min_power = 0;
02256930
MK
915 arg.channel.max_power = channel->max_power * 2;
916 arg.channel.max_reg_power = channel->max_reg_power * 2;
917 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157 918
7962b0d8
MK
919 reinit_completion(&ar->vdev_setup_done);
920
5e3dd157
KV
921 ret = ath10k_wmi_vdev_start(ar, &arg);
922 if (ret) {
7aa7a72a 923 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
69244e56 924 vdev_id, ret);
5e3dd157
KV
925 return ret;
926 }
927
928 ret = ath10k_vdev_setup_sync(ar);
929 if (ret) {
60028a81 930 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
69244e56 931 vdev_id, ret);
5e3dd157
KV
932 return ret;
933 }
934
935 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
936 if (ret) {
7aa7a72a 937 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
69244e56 938 vdev_id, ret);
5e3dd157
KV
939 goto vdev_stop;
940 }
941
942 ar->monitor_vdev_id = vdev_id;
5e3dd157 943
7aa7a72a 944 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1bbc0975 945 ar->monitor_vdev_id);
5e3dd157
KV
946 return 0;
947
948vdev_stop:
949 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
950 if (ret)
7aa7a72a 951 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
69244e56 952 ar->monitor_vdev_id, ret);
5e3dd157
KV
953
954 return ret;
955}
956
1bbc0975 957static int ath10k_monitor_vdev_stop(struct ath10k *ar)
5e3dd157
KV
958{
959 int ret = 0;
960
961 lockdep_assert_held(&ar->conf_mutex);
962
52fa0191
MP
963 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
964 if (ret)
7aa7a72a 965 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
69244e56 966 ar->monitor_vdev_id, ret);
5e3dd157 967
7962b0d8
MK
968 reinit_completion(&ar->vdev_setup_done);
969
5e3dd157
KV
970 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
971 if (ret)
7aa7a72a 972 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
69244e56 973 ar->monitor_vdev_id, ret);
5e3dd157
KV
974
975 ret = ath10k_vdev_setup_sync(ar);
976 if (ret)
60028a81 977 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
69244e56 978 ar->monitor_vdev_id, ret);
5e3dd157 979
7aa7a72a 980 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1bbc0975 981 ar->monitor_vdev_id);
5e3dd157
KV
982 return ret;
983}
984
1bbc0975 985static int ath10k_monitor_vdev_create(struct ath10k *ar)
5e3dd157
KV
986{
987 int bit, ret = 0;
988
989 lockdep_assert_held(&ar->conf_mutex);
990
a9aefb3b 991 if (ar->free_vdev_map == 0) {
7aa7a72a 992 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
5e3dd157
KV
993 return -ENOMEM;
994 }
995
16c11176 996 bit = __ffs64(ar->free_vdev_map);
a9aefb3b 997
16c11176 998 ar->monitor_vdev_id = bit;
5e3dd157
KV
999
1000 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1001 WMI_VDEV_TYPE_MONITOR,
1002 0, ar->mac_addr);
1003 if (ret) {
7aa7a72a 1004 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
69244e56 1005 ar->monitor_vdev_id, ret);
a9aefb3b 1006 return ret;
5e3dd157
KV
1007 }
1008
16c11176 1009 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
7aa7a72a 1010 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
5e3dd157
KV
1011 ar->monitor_vdev_id);
1012
5e3dd157 1013 return 0;
5e3dd157
KV
1014}
1015
1bbc0975 1016static int ath10k_monitor_vdev_delete(struct ath10k *ar)
5e3dd157
KV
1017{
1018 int ret = 0;
1019
1020 lockdep_assert_held(&ar->conf_mutex);
1021
5e3dd157
KV
1022 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1023 if (ret) {
7aa7a72a 1024 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
69244e56 1025 ar->monitor_vdev_id, ret);
5e3dd157
KV
1026 return ret;
1027 }
1028
16c11176 1029 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
5e3dd157 1030
7aa7a72a 1031 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
5e3dd157
KV
1032 ar->monitor_vdev_id);
1033 return ret;
1034}
1035
1bbc0975
MK
1036static int ath10k_monitor_start(struct ath10k *ar)
1037{
1038 int ret;
1039
1040 lockdep_assert_held(&ar->conf_mutex);
1041
1bbc0975
MK
1042 ret = ath10k_monitor_vdev_create(ar);
1043 if (ret) {
7aa7a72a 1044 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1bbc0975
MK
1045 return ret;
1046 }
1047
1048 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1049 if (ret) {
7aa7a72a 1050 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1bbc0975
MK
1051 ath10k_monitor_vdev_delete(ar);
1052 return ret;
1053 }
1054
1055 ar->monitor_started = true;
7aa7a72a 1056 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1bbc0975
MK
1057
1058 return 0;
1059}
1060
1933747f 1061static int ath10k_monitor_stop(struct ath10k *ar)
1bbc0975
MK
1062{
1063 int ret;
1064
1065 lockdep_assert_held(&ar->conf_mutex);
1066
1bbc0975 1067 ret = ath10k_monitor_vdev_stop(ar);
1933747f 1068 if (ret) {
7aa7a72a 1069 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1933747f
MK
1070 return ret;
1071 }
1bbc0975
MK
1072
1073 ret = ath10k_monitor_vdev_delete(ar);
1933747f 1074 if (ret) {
7aa7a72a 1075 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1933747f
MK
1076 return ret;
1077 }
1bbc0975
MK
1078
1079 ar->monitor_started = false;
7aa7a72a 1080 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1933747f
MK
1081
1082 return 0;
1083}
1084
500ff9f9
MK
1085static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1086{
1087 int num_ctx;
1088
1089 /* At least one chanctx is required to derive a channel to start
1090 * monitor vdev on.
1091 */
1092 num_ctx = ath10k_mac_num_chanctxs(ar);
1093 if (num_ctx == 0)
1094 return false;
1095
1096 /* If there's already an existing special monitor interface then don't
1097 * bother creating another monitor vdev.
1098 */
1099 if (ar->monitor_arvif)
1100 return false;
1101
1102 return ar->monitor ||
0d031c89 1103 ar->filter_flags & FIF_OTHER_BSS ||
500ff9f9
MK
1104 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1105}
1106
1107static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1108{
1109 int num_ctx;
1110
1111 num_ctx = ath10k_mac_num_chanctxs(ar);
1112
1113 /* FIXME: Current interface combinations and cfg80211/mac80211 code
1114 * shouldn't allow this but make sure to prevent handling the following
1115 * case anyway since multi-channel DFS hasn't been tested at all.
1116 */
1117 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1118 return false;
1119
1120 return true;
1121}
1122
1933747f
MK
1123static int ath10k_monitor_recalc(struct ath10k *ar)
1124{
500ff9f9
MK
1125 bool needed;
1126 bool allowed;
1127 int ret;
1933747f
MK
1128
1129 lockdep_assert_held(&ar->conf_mutex);
1130
500ff9f9
MK
1131 needed = ath10k_mac_monitor_vdev_is_needed(ar);
1132 allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1933747f
MK
1133
1134 ath10k_dbg(ar, ATH10K_DBG_MAC,
500ff9f9
MK
1135 "mac monitor recalc started? %d needed? %d allowed? %d\n",
1136 ar->monitor_started, needed, allowed);
1933747f 1137
500ff9f9
MK
1138 if (WARN_ON(needed && !allowed)) {
1139 if (ar->monitor_started) {
1140 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1141
1142 ret = ath10k_monitor_stop(ar);
1143 if (ret)
2a995088
KV
1144 ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1145 ret);
500ff9f9
MK
1146 /* not serious */
1147 }
1933747f 1148
500ff9f9
MK
1149 return -EPERM;
1150 }
1933747f 1151
500ff9f9 1152 if (needed == ar->monitor_started)
1933747f
MK
1153 return 0;
1154
500ff9f9 1155 if (needed)
1933747f 1156 return ath10k_monitor_start(ar);
500ff9f9
MK
1157 else
1158 return ath10k_monitor_stop(ar);
1bbc0975
MK
1159}
1160
e81bd104
MK
1161static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1162{
1163 struct ath10k *ar = arvif->ar;
1164 u32 vdev_param, rts_cts = 0;
1165
1166 lockdep_assert_held(&ar->conf_mutex);
1167
1168 vdev_param = ar->wmi.vdev_param->enable_rtscts;
1169
9a5ab0f4 1170 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
e81bd104
MK
1171
1172 if (arvif->num_legacy_stations > 0)
1173 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1174 WMI_RTSCTS_PROFILE);
9a5ab0f4
RM
1175 else
1176 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1177 WMI_RTSCTS_PROFILE);
e81bd104
MK
1178
1179 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1180 rts_cts);
1181}
1182
e8a50f8b
MP
1183static int ath10k_start_cac(struct ath10k *ar)
1184{
1185 int ret;
1186
1187 lockdep_assert_held(&ar->conf_mutex);
1188
1189 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1190
1933747f 1191 ret = ath10k_monitor_recalc(ar);
e8a50f8b 1192 if (ret) {
7aa7a72a 1193 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
e8a50f8b
MP
1194 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1195 return ret;
1196 }
1197
7aa7a72a 1198 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
e8a50f8b
MP
1199 ar->monitor_vdev_id);
1200
1201 return 0;
1202}
1203
1204static int ath10k_stop_cac(struct ath10k *ar)
1205{
1206 lockdep_assert_held(&ar->conf_mutex);
1207
1208 /* CAC is not running - do nothing */
1209 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1210 return 0;
1211
e8a50f8b 1212 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1bbc0975 1213 ath10k_monitor_stop(ar);
e8a50f8b 1214
7aa7a72a 1215 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
e8a50f8b
MP
1216
1217 return 0;
1218}
1219
500ff9f9
MK
1220static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1221 struct ieee80211_chanctx_conf *conf,
1222 void *data)
1223{
1224 bool *ret = data;
1225
1226 if (!*ret && conf->radar_enabled)
1227 *ret = true;
1228}
1229
1230static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1231{
1232 bool has_radar = false;
1233
1234 ieee80211_iter_chan_contexts_atomic(ar->hw,
1235 ath10k_mac_has_radar_iter,
1236 &has_radar);
1237
1238 return has_radar;
1239}
1240
d650097b 1241static void ath10k_recalc_radar_detection(struct ath10k *ar)
e8a50f8b 1242{
e8a50f8b
MP
1243 int ret;
1244
1245 lockdep_assert_held(&ar->conf_mutex);
1246
e8a50f8b
MP
1247 ath10k_stop_cac(ar);
1248
500ff9f9 1249 if (!ath10k_mac_has_radar_enabled(ar))
e8a50f8b
MP
1250 return;
1251
d650097b 1252 if (ar->num_started_vdevs > 0)
e8a50f8b
MP
1253 return;
1254
1255 ret = ath10k_start_cac(ar);
1256 if (ret) {
1257 /*
1258 * Not possible to start CAC on current channel so starting
1259 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1260 * by indicating that radar was detected.
1261 */
7aa7a72a 1262 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
e8a50f8b
MP
1263 ieee80211_radar_detected(ar->hw);
1264 }
1265}
1266
822b7e0b
VT
1267static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1268{
1269 struct ath10k *ar = arvif->ar;
1270 int ret;
1271
1272 lockdep_assert_held(&ar->conf_mutex);
1273
1274 reinit_completion(&ar->vdev_setup_done);
1275
1276 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1277 if (ret) {
1278 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1279 arvif->vdev_id, ret);
1280 return ret;
1281 }
1282
1283 ret = ath10k_vdev_setup_sync(ar);
1284 if (ret) {
1285 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
1286 arvif->vdev_id, ret);
1287 return ret;
1288 }
1289
1290 WARN_ON(ar->num_started_vdevs == 0);
1291
1292 if (ar->num_started_vdevs != 0) {
1293 ar->num_started_vdevs--;
1294 ath10k_recalc_radar_detection(ar);
1295 }
1296
1297 return ret;
1298}
1299
500ff9f9
MK
1300static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1301 const struct cfg80211_chan_def *chandef,
1302 bool restart)
72654fa7
MK
1303{
1304 struct ath10k *ar = arvif->ar;
72654fa7
MK
1305 struct wmi_vdev_start_request_arg arg = {};
1306 int ret = 0;
1307
1308 lockdep_assert_held(&ar->conf_mutex);
1309
1310 reinit_completion(&ar->vdev_setup_done);
1311
1312 arg.vdev_id = arvif->vdev_id;
1313 arg.dtim_period = arvif->dtim_period;
1314 arg.bcn_intval = arvif->beacon_interval;
1315
1316 arg.channel.freq = chandef->chan->center_freq;
1317 arg.channel.band_center_freq1 = chandef->center_freq1;
1318 arg.channel.mode = chan_to_phymode(chandef);
1319
1320 arg.channel.min_power = 0;
1321 arg.channel.max_power = chandef->chan->max_power * 2;
1322 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1323 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1324
1325 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1326 arg.ssid = arvif->u.ap.ssid;
1327 arg.ssid_len = arvif->u.ap.ssid_len;
1328 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1329
1330 /* For now allow DFS for AP mode */
1331 arg.channel.chan_radar =
1332 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1333 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1334 arg.ssid = arvif->vif->bss_conf.ssid;
1335 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1336 }
1337
7aa7a72a 1338 ath10k_dbg(ar, ATH10K_DBG_MAC,
72654fa7
MK
1339 "mac vdev %d start center_freq %d phymode %s\n",
1340 arg.vdev_id, arg.channel.freq,
1341 ath10k_wmi_phymode_str(arg.channel.mode));
1342
dc55e307
MK
1343 if (restart)
1344 ret = ath10k_wmi_vdev_restart(ar, &arg);
1345 else
1346 ret = ath10k_wmi_vdev_start(ar, &arg);
1347
72654fa7 1348 if (ret) {
7aa7a72a 1349 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
72654fa7
MK
1350 arg.vdev_id, ret);
1351 return ret;
1352 }
1353
1354 ret = ath10k_vdev_setup_sync(ar);
1355 if (ret) {
60028a81
BG
1356 ath10k_warn(ar,
1357 "failed to synchronize setup for vdev %i restart %d: %d\n",
1358 arg.vdev_id, restart, ret);
72654fa7
MK
1359 return ret;
1360 }
1361
d650097b
MK
1362 ar->num_started_vdevs++;
1363 ath10k_recalc_radar_detection(ar);
1364
72654fa7
MK
1365 return ret;
1366}
1367
500ff9f9
MK
1368static int ath10k_vdev_start(struct ath10k_vif *arvif,
1369 const struct cfg80211_chan_def *def)
dc55e307 1370{
500ff9f9 1371 return ath10k_vdev_start_restart(arvif, def, false);
dc55e307
MK
1372}
1373
500ff9f9
MK
1374static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1375 const struct cfg80211_chan_def *def)
72654fa7 1376{
500ff9f9 1377 return ath10k_vdev_start_restart(arvif, def, true);
72654fa7
MK
1378}
1379
fbb8f1b7
MK
1380static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1381 struct sk_buff *bcn)
1382{
1383 struct ath10k *ar = arvif->ar;
1384 struct ieee80211_mgmt *mgmt;
1385 const u8 *p2p_ie;
1386 int ret;
1387
08c27be1 1388 if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
fbb8f1b7
MK
1389 return 0;
1390
1391 mgmt = (void *)bcn->data;
1392 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1393 mgmt->u.beacon.variable,
1394 bcn->len - (mgmt->u.beacon.variable -
1395 bcn->data));
1396 if (!p2p_ie)
1397 return -ENOENT;
1398
1399 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1400 if (ret) {
1401 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1402 arvif->vdev_id, ret);
1403 return ret;
1404 }
1405
1406 return 0;
1407}
1408
1409static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1410 u8 oui_type, size_t ie_offset)
1411{
1412 size_t len;
1413 const u8 *next;
1414 const u8 *end;
1415 u8 *ie;
1416
1417 if (WARN_ON(skb->len < ie_offset))
1418 return -EINVAL;
1419
1420 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1421 skb->data + ie_offset,
1422 skb->len - ie_offset);
1423 if (!ie)
1424 return -ENOENT;
1425
1426 len = ie[1] + 2;
1427 end = skb->data + skb->len;
1428 next = ie + len;
1429
1430 if (WARN_ON(next > end))
1431 return -EINVAL;
1432
1433 memmove(ie, next, end - next);
1434 skb_trim(skb, skb->len - len);
1435
1436 return 0;
1437}
1438
1439static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1440{
1441 struct ath10k *ar = arvif->ar;
1442 struct ieee80211_hw *hw = ar->hw;
1443 struct ieee80211_vif *vif = arvif->vif;
1444 struct ieee80211_mutable_offsets offs = {};
1445 struct sk_buff *bcn;
1446 int ret;
1447
1448 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1449 return 0;
1450
81a9a17d
MK
1451 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1452 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1453 return 0;
1454
fbb8f1b7
MK
1455 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1456 if (!bcn) {
1457 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1458 return -EPERM;
1459 }
1460
1461 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1462 if (ret) {
1463 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1464 kfree_skb(bcn);
1465 return ret;
1466 }
1467
1468 /* P2P IE is inserted by firmware automatically (as configured above)
1469 * so remove it from the base beacon template to avoid duplicate P2P
1470 * IEs in beacon frames.
1471 */
1472 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1473 offsetof(struct ieee80211_mgmt,
1474 u.beacon.variable));
1475
1476 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1477 0, NULL, 0);
1478 kfree_skb(bcn);
1479
1480 if (ret) {
1481 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1482 ret);
1483 return ret;
1484 }
1485
1486 return 0;
1487}
1488
1489static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1490{
1491 struct ath10k *ar = arvif->ar;
1492 struct ieee80211_hw *hw = ar->hw;
1493 struct ieee80211_vif *vif = arvif->vif;
1494 struct sk_buff *prb;
1495 int ret;
1496
1497 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1498 return 0;
1499
81a9a17d
MK
1500 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1501 return 0;
1502
fbb8f1b7
MK
1503 prb = ieee80211_proberesp_get(hw, vif);
1504 if (!prb) {
1505 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1506 return -EPERM;
1507 }
1508
1509 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1510 kfree_skb(prb);
1511
1512 if (ret) {
1513 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1514 ret);
1515 return ret;
1516 }
1517
1518 return 0;
1519}
1520
500ff9f9
MK
1521static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1522{
1523 struct ath10k *ar = arvif->ar;
1524 struct cfg80211_chan_def def;
1525 int ret;
1526
1527 /* When originally vdev is started during assign_vif_chanctx() some
1528 * information is missing, notably SSID. Firmware revisions with beacon
1529 * offloading require the SSID to be provided during vdev (re)start to
1530 * handle hidden SSID properly.
1531 *
1532 * Vdev restart must be done after vdev has been both started and
1533 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1534 * deliver vdev restart response event causing timeouts during vdev
1535 * syncing in ath10k.
1536 *
1537 * Note: The vdev down/up and template reinstallation could be skipped
1538 * since only wmi-tlv firmware are known to have beacon offload and
1539 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1540 * response delivery. It's probably more robust to keep it as is.
1541 */
1542 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1543 return 0;
1544
1545 if (WARN_ON(!arvif->is_started))
1546 return -EINVAL;
1547
1548 if (WARN_ON(!arvif->is_up))
1549 return -EINVAL;
1550
1551 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1552 return -EINVAL;
1553
1554 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1555 if (ret) {
1556 ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1557 arvif->vdev_id, ret);
1558 return ret;
1559 }
1560
1561 /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1562 * firmware will crash upon vdev up.
1563 */
1564
1565 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1566 if (ret) {
1567 ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1568 return ret;
1569 }
1570
1571 ret = ath10k_mac_setup_prb_tmpl(arvif);
1572 if (ret) {
1573 ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1574 return ret;
1575 }
1576
1577 ret = ath10k_vdev_restart(arvif, &def);
1578 if (ret) {
1579 ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1580 arvif->vdev_id, ret);
1581 return ret;
1582 }
1583
1584 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1585 arvif->bssid);
1586 if (ret) {
1587 ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1588 arvif->vdev_id, ret);
1589 return ret;
1590 }
1591
1592 return 0;
1593}
1594
5e3dd157 1595static void ath10k_control_beaconing(struct ath10k_vif *arvif,
5b07e07f 1596 struct ieee80211_bss_conf *info)
5e3dd157 1597{
7aa7a72a 1598 struct ath10k *ar = arvif->ar;
5e3dd157
KV
1599 int ret = 0;
1600
548db54c
MK
1601 lockdep_assert_held(&arvif->ar->conf_mutex);
1602
5e3dd157 1603 if (!info->enable_beacon) {
500ff9f9
MK
1604 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1605 if (ret)
1606 ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1607 arvif->vdev_id, ret);
c930f744 1608
c930f744
MK
1609 arvif->is_up = false;
1610
748afc47 1611 spin_lock_bh(&arvif->ar->data_lock);
64badcb6 1612 ath10k_mac_vif_beacon_free(arvif);
748afc47
MK
1613 spin_unlock_bh(&arvif->ar->data_lock);
1614
5e3dd157
KV
1615 return;
1616 }
1617
1618 arvif->tx_seq_no = 0x1000;
1619
c930f744 1620 arvif->aid = 0;
b25f32cb 1621 ether_addr_copy(arvif->bssid, info->bssid);
c930f744
MK
1622
1623 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1624 arvif->bssid);
5e3dd157 1625 if (ret) {
7aa7a72a 1626 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
69244e56 1627 arvif->vdev_id, ret);
5e3dd157
KV
1628 return;
1629 }
c930f744 1630
c930f744
MK
1631 arvif->is_up = true;
1632
500ff9f9
MK
1633 ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1634 if (ret) {
1635 ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1636 arvif->vdev_id, ret);
1637 return;
1638 }
1639
7aa7a72a 1640 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
5e3dd157
KV
1641}
1642
1643static void ath10k_control_ibss(struct ath10k_vif *arvif,
1644 struct ieee80211_bss_conf *info,
1645 const u8 self_peer[ETH_ALEN])
1646{
7aa7a72a 1647 struct ath10k *ar = arvif->ar;
6d1506e7 1648 u32 vdev_param;
5e3dd157
KV
1649 int ret = 0;
1650
548db54c
MK
1651 lockdep_assert_held(&arvif->ar->conf_mutex);
1652
5e3dd157 1653 if (!info->ibss_joined) {
c930f744 1654 if (is_zero_ether_addr(arvif->bssid))
5e3dd157
KV
1655 return;
1656
93803b33 1657 eth_zero_addr(arvif->bssid);
5e3dd157
KV
1658
1659 return;
1660 }
1661
6d1506e7
BM
1662 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1663 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
1664 ATH10K_DEFAULT_ATIM);
1665 if (ret)
7aa7a72a 1666 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
5e3dd157
KV
1667 arvif->vdev_id, ret);
1668}
1669
9f9b5746
MK
1670static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1671{
1672 struct ath10k *ar = arvif->ar;
1673 u32 param;
1674 u32 value;
1675 int ret;
1676
1677 lockdep_assert_held(&arvif->ar->conf_mutex);
1678
1679 if (arvif->u.sta.uapsd)
1680 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1681 else
1682 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1683
1684 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1685 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1686 if (ret) {
1687 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1688 value, arvif->vdev_id, ret);
1689 return ret;
1690 }
1691
1692 return 0;
1693}
1694
1695static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1696{
1697 struct ath10k *ar = arvif->ar;
1698 u32 param;
1699 u32 value;
1700 int ret;
1701
1702 lockdep_assert_held(&arvif->ar->conf_mutex);
1703
1704 if (arvif->u.sta.uapsd)
1705 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1706 else
1707 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1708
1709 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1710 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1711 param, value);
1712 if (ret) {
1713 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1714 value, arvif->vdev_id, ret);
1715 return ret;
1716 }
1717
1718 return 0;
1719}
1720
424f2630 1721static int ath10k_mac_num_vifs_started(struct ath10k *ar)
cffb41f3
MK
1722{
1723 struct ath10k_vif *arvif;
1724 int num = 0;
1725
1726 lockdep_assert_held(&ar->conf_mutex);
1727
1728 list_for_each_entry(arvif, &ar->arvifs, list)
424f2630 1729 if (arvif->is_started)
cffb41f3
MK
1730 num++;
1731
1732 return num;
1733}
1734
ad088bfa 1735static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
5e3dd157 1736{
ad088bfa 1737 struct ath10k *ar = arvif->ar;
526549a8 1738 struct ieee80211_vif *vif = arvif->vif;
ad088bfa 1739 struct ieee80211_conf *conf = &ar->hw->conf;
5e3dd157
KV
1740 enum wmi_sta_powersave_param param;
1741 enum wmi_sta_ps_mode psmode;
1742 int ret;
526549a8 1743 int ps_timeout;
cffb41f3 1744 bool enable_ps;
5e3dd157 1745
548db54c
MK
1746 lockdep_assert_held(&arvif->ar->conf_mutex);
1747
ad088bfa
MK
1748 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1749 return 0;
5e3dd157 1750
cffb41f3
MK
1751 enable_ps = arvif->ps;
1752
424f2630 1753 if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
cffb41f3
MK
1754 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1755 ar->fw_features)) {
1756 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1757 arvif->vdev_id);
1758 enable_ps = false;
1759 }
1760
917826be
JD
1761 if (!arvif->is_started) {
1762 /* mac80211 can update vif powersave state while disconnected.
1763 * Firmware doesn't behave nicely and consumes more power than
1764 * necessary if PS is disabled on a non-started vdev. Hence
1765 * force-enable PS for non-running vdevs.
1766 */
1767 psmode = WMI_STA_PS_MODE_ENABLED;
1768 } else if (enable_ps) {
5e3dd157
KV
1769 psmode = WMI_STA_PS_MODE_ENABLED;
1770 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1771
526549a8
MK
1772 ps_timeout = conf->dynamic_ps_timeout;
1773 if (ps_timeout == 0) {
1774 /* Firmware doesn't like 0 */
1775 ps_timeout = ieee80211_tu_to_usec(
1776 vif->bss_conf.beacon_int) / 1000;
1777 }
1778
ad088bfa 1779 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
526549a8 1780 ps_timeout);
5e3dd157 1781 if (ret) {
7aa7a72a 1782 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
69244e56 1783 arvif->vdev_id, ret);
ad088bfa 1784 return ret;
5e3dd157 1785 }
5e3dd157
KV
1786 } else {
1787 psmode = WMI_STA_PS_MODE_DISABLED;
1788 }
1789
7aa7a72a 1790 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
60c3daa8
KV
1791 arvif->vdev_id, psmode ? "enable" : "disable");
1792
ad088bfa
MK
1793 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1794 if (ret) {
7aa7a72a 1795 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
be6546fc 1796 psmode, arvif->vdev_id, ret);
ad088bfa
MK
1797 return ret;
1798 }
1799
1800 return 0;
5e3dd157
KV
1801}
1802
46725b15
MK
1803static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1804{
1805 struct ath10k *ar = arvif->ar;
1806 struct wmi_sta_keepalive_arg arg = {};
1807 int ret;
1808
1809 lockdep_assert_held(&arvif->ar->conf_mutex);
1810
1811 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1812 return 0;
1813
1814 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1815 return 0;
1816
1817 /* Some firmware revisions have a bug and ignore the `enabled` field.
1818 * Instead use the interval to disable the keepalive.
1819 */
1820 arg.vdev_id = arvif->vdev_id;
1821 arg.enabled = 1;
1822 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1823 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1824
1825 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1826 if (ret) {
1827 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1828 arvif->vdev_id, ret);
1829 return ret;
1830 }
1831
1832 return 0;
1833}
1834
81a9a17d
MK
1835static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1836{
1837 struct ath10k *ar = arvif->ar;
1838 struct ieee80211_vif *vif = arvif->vif;
1839 int ret;
1840
8513d95b
MK
1841 lockdep_assert_held(&arvif->ar->conf_mutex);
1842
1843 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1844 return;
1845
81a9a17d
MK
1846 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1847 return;
1848
1849 if (!vif->csa_active)
1850 return;
1851
1852 if (!arvif->is_up)
1853 return;
1854
1855 if (!ieee80211_csa_is_complete(vif)) {
1856 ieee80211_csa_update_counter(vif);
1857
1858 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1859 if (ret)
1860 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1861 ret);
1862
1863 ret = ath10k_mac_setup_prb_tmpl(arvif);
1864 if (ret)
1865 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1866 ret);
1867 } else {
1868 ieee80211_csa_finish(vif);
1869 }
1870}
1871
1872static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1873{
1874 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1875 ap_csa_work);
1876 struct ath10k *ar = arvif->ar;
1877
1878 mutex_lock(&ar->conf_mutex);
1879 ath10k_mac_vif_ap_csa_count_down(arvif);
1880 mutex_unlock(&ar->conf_mutex);
1881}
1882
cc9904e6
MK
1883static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1884 struct ieee80211_vif *vif)
1885{
1886 struct sk_buff *skb = data;
1887 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1888 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1889
1890 if (vif->type != NL80211_IFTYPE_STATION)
1891 return;
1892
1893 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1894 return;
1895
1896 cancel_delayed_work(&arvif->connection_loss_work);
1897}
1898
1899void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1900{
1901 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1902 IEEE80211_IFACE_ITER_NORMAL,
1903 ath10k_mac_handle_beacon_iter,
1904 skb);
1905}
1906
1907static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1908 struct ieee80211_vif *vif)
1909{
1910 u32 *vdev_id = data;
1911 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1912 struct ath10k *ar = arvif->ar;
1913 struct ieee80211_hw *hw = ar->hw;
1914
1915 if (arvif->vdev_id != *vdev_id)
1916 return;
1917
1918 if (!arvif->is_up)
1919 return;
1920
1921 ieee80211_beacon_loss(vif);
1922
1923 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1924 * (done by mac80211) succeeds but beacons do not resume then it
1925 * doesn't make sense to continue operation. Queue connection loss work
1926 * which can be cancelled when beacon is received.
1927 */
1928 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1929 ATH10K_CONNECTION_LOSS_HZ);
1930}
1931
1932void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1933{
1934 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1935 IEEE80211_IFACE_ITER_NORMAL,
1936 ath10k_mac_handle_beacon_miss_iter,
1937 &vdev_id);
1938}
1939
1940static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1941{
1942 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1943 connection_loss_work.work);
1944 struct ieee80211_vif *vif = arvif->vif;
1945
1946 if (!arvif->is_up)
1947 return;
1948
1949 ieee80211_connection_loss(vif);
1950}
1951
5e3dd157
KV
1952/**********************/
1953/* Station management */
1954/**********************/
1955
590922a8
MK
1956static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1957 struct ieee80211_vif *vif)
1958{
1959 /* Some firmware revisions have unstable STA powersave when listen
1960 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1961 * generate NullFunc frames properly even if buffered frames have been
1962 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1963 * buffered frames. Often pinging the device from AP would simply fail.
1964 *
1965 * As a workaround set it to 1.
1966 */
1967 if (vif->type == NL80211_IFTYPE_STATION)
1968 return 1;
1969
1970 return ar->hw->conf.listen_interval;
1971}
1972
5e3dd157 1973static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
590922a8 1974 struct ieee80211_vif *vif,
5e3dd157 1975 struct ieee80211_sta *sta,
5e3dd157
KV
1976 struct wmi_peer_assoc_complete_arg *arg)
1977{
590922a8 1978 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
c51880ea 1979 u32 aid;
590922a8 1980
548db54c
MK
1981 lockdep_assert_held(&ar->conf_mutex);
1982
c51880ea
MK
1983 if (vif->type == NL80211_IFTYPE_STATION)
1984 aid = vif->bss_conf.aid;
1985 else
1986 aid = sta->aid;
1987
b25f32cb 1988 ether_addr_copy(arg->addr, sta->addr);
5e3dd157 1989 arg->vdev_id = arvif->vdev_id;
c51880ea 1990 arg->peer_aid = aid;
3fab30f7 1991 arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
590922a8 1992 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
5e3dd157 1993 arg->peer_num_spatial_streams = 1;
590922a8 1994 arg->peer_caps = vif->bss_conf.assoc_capability;
5e3dd157
KV
1995}
1996
1997static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
590922a8 1998 struct ieee80211_vif *vif,
90eceb3b 1999 struct ieee80211_sta *sta,
5e3dd157
KV
2000 struct wmi_peer_assoc_complete_arg *arg)
2001{
5e3dd157 2002 struct ieee80211_bss_conf *info = &vif->bss_conf;
500ff9f9 2003 struct cfg80211_chan_def def;
5e3dd157
KV
2004 struct cfg80211_bss *bss;
2005 const u8 *rsnie = NULL;
2006 const u8 *wpaie = NULL;
2007
548db54c
MK
2008 lockdep_assert_held(&ar->conf_mutex);
2009
500ff9f9
MK
2010 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2011 return;
2012
2013 bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
2014 IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
5e3dd157
KV
2015 if (bss) {
2016 const struct cfg80211_bss_ies *ies;
2017
2018 rcu_read_lock();
2019 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2020
2021 ies = rcu_dereference(bss->ies);
2022
2023 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
5b07e07f
KV
2024 WLAN_OUI_TYPE_MICROSOFT_WPA,
2025 ies->data,
2026 ies->len);
5e3dd157
KV
2027 rcu_read_unlock();
2028 cfg80211_put_bss(ar->hw->wiphy, bss);
2029 }
2030
2031 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2032 if (rsnie || wpaie) {
7aa7a72a 2033 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
3fab30f7 2034 arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
5e3dd157
KV
2035 }
2036
2037 if (wpaie) {
7aa7a72a 2038 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
3fab30f7 2039 arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
5e3dd157 2040 }
90eceb3b
T
2041
2042 if (sta->mfp &&
2043 test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT, ar->fw_features)) {
2044 arg->peer_flags |= ar->wmi.peer_flags->pmf;
5e3dd157
KV
2045 }
2046}
2047
2048static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
500ff9f9 2049 struct ieee80211_vif *vif,
5e3dd157
KV
2050 struct ieee80211_sta *sta,
2051 struct wmi_peer_assoc_complete_arg *arg)
2052{
45c9abc0 2053 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5e3dd157 2054 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
500ff9f9 2055 struct cfg80211_chan_def def;
5e3dd157
KV
2056 const struct ieee80211_supported_band *sband;
2057 const struct ieee80211_rate *rates;
57fbcce3 2058 enum nl80211_band band;
5e3dd157 2059 u32 ratemask;
486017cc 2060 u8 rate;
5e3dd157
KV
2061 int i;
2062
548db54c
MK
2063 lockdep_assert_held(&ar->conf_mutex);
2064
500ff9f9
MK
2065 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2066 return;
2067
45c9abc0
MK
2068 band = def.chan->band;
2069 sband = ar->hw->wiphy->bands[band];
2070 ratemask = sta->supp_rates[band];
2071 ratemask &= arvif->bitrate_mask.control[band].legacy;
5e3dd157
KV
2072 rates = sband->bitrates;
2073
2074 rateset->num_rates = 0;
2075
2076 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2077 if (!(ratemask & 1))
2078 continue;
2079
486017cc
MK
2080 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2081 rateset->rates[rateset->num_rates] = rate;
5e3dd157
KV
2082 rateset->num_rates++;
2083 }
2084}
2085
45c9abc0
MK
2086static bool
2087ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2088{
2089 int nss;
2090
2091 for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2092 if (ht_mcs_mask[nss])
2093 return false;
2094
2095 return true;
2096}
2097
2098static bool
2099ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2100{
2101 int nss;
2102
2103 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2104 if (vht_mcs_mask[nss])
2105 return false;
2106
2107 return true;
2108}
2109
5e3dd157 2110static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
45c9abc0 2111 struct ieee80211_vif *vif,
5e3dd157
KV
2112 struct ieee80211_sta *sta,
2113 struct wmi_peer_assoc_complete_arg *arg)
2114{
2115 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
45c9abc0
MK
2116 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2117 struct cfg80211_chan_def def;
57fbcce3 2118 enum nl80211_band band;
45c9abc0
MK
2119 const u8 *ht_mcs_mask;
2120 const u16 *vht_mcs_mask;
72f8cef5
VN
2121 int i, n;
2122 u8 max_nss;
af762c0b 2123 u32 stbc;
5e3dd157 2124
548db54c
MK
2125 lockdep_assert_held(&ar->conf_mutex);
2126
45c9abc0
MK
2127 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2128 return;
2129
5e3dd157
KV
2130 if (!ht_cap->ht_supported)
2131 return;
2132
45c9abc0
MK
2133 band = def.chan->band;
2134 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2135 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2136
2137 if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2138 ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2139 return;
2140
3fab30f7 2141 arg->peer_flags |= ar->wmi.peer_flags->ht;
5e3dd157
KV
2142 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2143 ht_cap->ampdu_factor)) - 1;
2144
2145 arg->peer_mpdu_density =
2146 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2147
2148 arg->peer_ht_caps = ht_cap->cap;
2149 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2150
2151 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
3fab30f7 2152 arg->peer_flags |= ar->wmi.peer_flags->ldbc;
5e3dd157
KV
2153
2154 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
3fab30f7 2155 arg->peer_flags |= ar->wmi.peer_flags->bw40;
5e3dd157
KV
2156 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2157 }
2158
45c9abc0
MK
2159 if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2160 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2161 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
5e3dd157 2162
45c9abc0
MK
2163 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2164 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2165 }
5e3dd157
KV
2166
2167 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2168 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
3fab30f7 2169 arg->peer_flags |= ar->wmi.peer_flags->stbc;
5e3dd157
KV
2170 }
2171
2172 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
5e3dd157
KV
2173 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2174 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2175 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2176 arg->peer_rate_caps |= stbc;
3fab30f7 2177 arg->peer_flags |= ar->wmi.peer_flags->stbc;
5e3dd157
KV
2178 }
2179
5e3dd157
KV
2180 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2181 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2182 else if (ht_cap->mcs.rx_mask[1])
2183 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2184
45c9abc0
MK
2185 for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2186 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2187 (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2188 max_nss = (i / 8) + 1;
5e3dd157 2189 arg->peer_ht_rates.rates[n++] = i;
45c9abc0 2190 }
5e3dd157 2191
fd71f807
BM
2192 /*
2193 * This is a workaround for HT-enabled STAs which break the spec
2194 * and have no HT capabilities RX mask (no HT RX MCS map).
2195 *
2196 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2197 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2198 *
2199 * Firmware asserts if such situation occurs.
2200 */
2201 if (n == 0) {
2202 arg->peer_ht_rates.num_rates = 8;
2203 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2204 arg->peer_ht_rates.rates[i] = i;
2205 } else {
2206 arg->peer_ht_rates.num_rates = n;
72f8cef5 2207 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
fd71f807 2208 }
5e3dd157 2209
7aa7a72a 2210 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
60c3daa8 2211 arg->addr,
5e3dd157
KV
2212 arg->peer_ht_rates.num_rates,
2213 arg->peer_num_spatial_streams);
2214}
2215
d3d3ff42
JD
2216static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2217 struct ath10k_vif *arvif,
2218 struct ieee80211_sta *sta)
5e3dd157
KV
2219{
2220 u32 uapsd = 0;
2221 u32 max_sp = 0;
d3d3ff42 2222 int ret = 0;
5e3dd157 2223
548db54c
MK
2224 lockdep_assert_held(&ar->conf_mutex);
2225
5e3dd157 2226 if (sta->wme && sta->uapsd_queues) {
7aa7a72a 2227 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
5e3dd157
KV
2228 sta->uapsd_queues, sta->max_sp);
2229
5e3dd157
KV
2230 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2231 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2232 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2233 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2234 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2235 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2236 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2237 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2238 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2239 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2240 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2241 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2242
5e3dd157
KV
2243 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2244 max_sp = sta->max_sp;
2245
d3d3ff42
JD
2246 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2247 sta->addr,
2248 WMI_AP_PS_PEER_PARAM_UAPSD,
2249 uapsd);
2250 if (ret) {
7aa7a72a 2251 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
69244e56 2252 arvif->vdev_id, ret);
d3d3ff42
JD
2253 return ret;
2254 }
5e3dd157 2255
d3d3ff42
JD
2256 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2257 sta->addr,
2258 WMI_AP_PS_PEER_PARAM_MAX_SP,
2259 max_sp);
2260 if (ret) {
7aa7a72a 2261 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
69244e56 2262 arvif->vdev_id, ret);
d3d3ff42
JD
2263 return ret;
2264 }
5e3dd157
KV
2265
2266 /* TODO setup this based on STA listen interval and
2267 beacon interval. Currently we don't know
2268 sta->listen_interval - mac80211 patch required.
2269 Currently use 10 seconds */
d3d3ff42 2270 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
5b07e07f
KV
2271 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2272 10);
d3d3ff42 2273 if (ret) {
7aa7a72a 2274 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
69244e56 2275 arvif->vdev_id, ret);
d3d3ff42
JD
2276 return ret;
2277 }
5e3dd157 2278 }
5e3dd157 2279
d3d3ff42 2280 return 0;
5e3dd157
KV
2281}
2282
45c9abc0
MK
2283static u16
2284ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2285 const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2286{
2287 int idx_limit;
2288 int nss;
2289 u16 mcs_map;
2290 u16 mcs;
2291
2292 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2293 mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2294 vht_mcs_limit[nss];
2295
2296 if (mcs_map)
2297 idx_limit = fls(mcs_map) - 1;
2298 else
2299 idx_limit = -1;
2300
2301 switch (idx_limit) {
2302 case 0: /* fall through */
2303 case 1: /* fall through */
2304 case 2: /* fall through */
2305 case 3: /* fall through */
2306 case 4: /* fall through */
2307 case 5: /* fall through */
2308 case 6: /* fall through */
2309 default:
2310 /* see ath10k_mac_can_set_bitrate_mask() */
2311 WARN_ON(1);
2312 /* fall through */
2313 case -1:
2314 mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2315 break;
2316 case 7:
2317 mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2318 break;
2319 case 8:
2320 mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2321 break;
2322 case 9:
2323 mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2324 break;
2325 }
2326
2327 tx_mcs_set &= ~(0x3 << (nss * 2));
2328 tx_mcs_set |= mcs << (nss * 2);
2329 }
2330
2331 return tx_mcs_set;
2332}
2333
5e3dd157 2334static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
500ff9f9 2335 struct ieee80211_vif *vif,
5e3dd157
KV
2336 struct ieee80211_sta *sta,
2337 struct wmi_peer_assoc_complete_arg *arg)
2338{
2339 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
45c9abc0 2340 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
500ff9f9 2341 struct cfg80211_chan_def def;
57fbcce3 2342 enum nl80211_band band;
45c9abc0 2343 const u16 *vht_mcs_mask;
a24b88b5 2344 u8 ampdu_factor;
5e3dd157 2345
500ff9f9
MK
2346 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2347 return;
2348
5e3dd157
KV
2349 if (!vht_cap->vht_supported)
2350 return;
2351
45c9abc0
MK
2352 band = def.chan->band;
2353 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2354
2355 if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2356 return;
2357
3fab30f7 2358 arg->peer_flags |= ar->wmi.peer_flags->vht;
d68bb12a 2359
57fbcce3 2360 if (def.chan->band == NL80211_BAND_2GHZ)
3fab30f7 2361 arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
d68bb12a 2362
5e3dd157
KV
2363 arg->peer_vht_caps = vht_cap->cap;
2364
a24b88b5
SM
2365 ampdu_factor = (vht_cap->cap &
2366 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2367 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2368
2369 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2370 * zero in VHT IE. Using it would result in degraded throughput.
2371 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2372 * it if VHT max_mpdu is smaller. */
2373 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2374 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2375 ampdu_factor)) - 1);
2376
5e3dd157 2377 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
3fab30f7 2378 arg->peer_flags |= ar->wmi.peer_flags->bw80;
5e3dd157
KV
2379
2380 arg->peer_vht_rates.rx_max_rate =
2381 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2382 arg->peer_vht_rates.rx_mcs_set =
2383 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2384 arg->peer_vht_rates.tx_max_rate =
2385 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
45c9abc0
MK
2386 arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2387 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
5e3dd157 2388
7aa7a72a 2389 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
60c3daa8 2390 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
5e3dd157
KV
2391}
2392
2393static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
590922a8 2394 struct ieee80211_vif *vif,
5e3dd157 2395 struct ieee80211_sta *sta,
5e3dd157
KV
2396 struct wmi_peer_assoc_complete_arg *arg)
2397{
590922a8
MK
2398 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2399
5e3dd157
KV
2400 switch (arvif->vdev_type) {
2401 case WMI_VDEV_TYPE_AP:
d3d3ff42 2402 if (sta->wme)
3fab30f7 2403 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
d3d3ff42
JD
2404
2405 if (sta->wme && sta->uapsd_queues) {
3fab30f7 2406 arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
d3d3ff42
JD
2407 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2408 }
5e3dd157
KV
2409 break;
2410 case WMI_VDEV_TYPE_STA:
590922a8 2411 if (vif->bss_conf.qos)
3fab30f7 2412 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
5e3dd157 2413 break;
627d9841
JD
2414 case WMI_VDEV_TYPE_IBSS:
2415 if (sta->wme)
3fab30f7 2416 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
627d9841 2417 break;
5e3dd157
KV
2418 default:
2419 break;
2420 }
627d9841
JD
2421
2422 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
3fab30f7
T
2423 sta->addr, !!(arg->peer_flags &
2424 arvif->ar->wmi.peer_flags->qos));
5e3dd157
KV
2425}
2426
8d7aa6bc 2427static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
91b12089 2428{
57fbcce3 2429 return sta->supp_rates[NL80211_BAND_2GHZ] >>
8d7aa6bc 2430 ATH10K_MAC_FIRST_OFDM_RATE_IDX;
91b12089
MK
2431}
2432
5e3dd157 2433static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
590922a8 2434 struct ieee80211_vif *vif,
5e3dd157
KV
2435 struct ieee80211_sta *sta,
2436 struct wmi_peer_assoc_complete_arg *arg)
2437{
45c9abc0 2438 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
500ff9f9 2439 struct cfg80211_chan_def def;
57fbcce3 2440 enum nl80211_band band;
45c9abc0
MK
2441 const u8 *ht_mcs_mask;
2442 const u16 *vht_mcs_mask;
5e3dd157
KV
2443 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2444
500ff9f9
MK
2445 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2446 return;
2447
45c9abc0
MK
2448 band = def.chan->band;
2449 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2450 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2451
2452 switch (band) {
57fbcce3 2453 case NL80211_BAND_2GHZ:
45c9abc0
MK
2454 if (sta->vht_cap.vht_supported &&
2455 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
d68bb12a
YL
2456 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2457 phymode = MODE_11AC_VHT40;
2458 else
2459 phymode = MODE_11AC_VHT20;
45c9abc0
MK
2460 } else if (sta->ht_cap.ht_supported &&
2461 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
5e3dd157
KV
2462 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2463 phymode = MODE_11NG_HT40;
2464 else
2465 phymode = MODE_11NG_HT20;
8d7aa6bc 2466 } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
5e3dd157 2467 phymode = MODE_11G;
91b12089
MK
2468 } else {
2469 phymode = MODE_11B;
5e3dd157
KV
2470 }
2471
2472 break;
57fbcce3 2473 case NL80211_BAND_5GHZ:
7cc45e98
SM
2474 /*
2475 * Check VHT first.
2476 */
45c9abc0
MK
2477 if (sta->vht_cap.vht_supported &&
2478 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
7cc45e98
SM
2479 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2480 phymode = MODE_11AC_VHT80;
2481 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2482 phymode = MODE_11AC_VHT40;
2483 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2484 phymode = MODE_11AC_VHT20;
45c9abc0
MK
2485 } else if (sta->ht_cap.ht_supported &&
2486 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2487 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
5e3dd157
KV
2488 phymode = MODE_11NA_HT40;
2489 else
2490 phymode = MODE_11NA_HT20;
2491 } else {
2492 phymode = MODE_11A;
2493 }
2494
2495 break;
2496 default:
2497 break;
2498 }
2499
7aa7a72a 2500 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
38a1d47e 2501 sta->addr, ath10k_wmi_phymode_str(phymode));
60c3daa8 2502
5e3dd157
KV
2503 arg->peer_phymode = phymode;
2504 WARN_ON(phymode == MODE_UNKNOWN);
2505}
2506
b9ada65d 2507static int ath10k_peer_assoc_prepare(struct ath10k *ar,
590922a8 2508 struct ieee80211_vif *vif,
b9ada65d 2509 struct ieee80211_sta *sta,
b9ada65d 2510 struct wmi_peer_assoc_complete_arg *arg)
5e3dd157 2511{
548db54c
MK
2512 lockdep_assert_held(&ar->conf_mutex);
2513
b9ada65d 2514 memset(arg, 0, sizeof(*arg));
5e3dd157 2515
590922a8 2516 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
90eceb3b 2517 ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
500ff9f9 2518 ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
45c9abc0 2519 ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
500ff9f9 2520 ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
590922a8
MK
2521 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2522 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
5e3dd157 2523
b9ada65d 2524 return 0;
5e3dd157
KV
2525}
2526
90046f50
MK
2527static const u32 ath10k_smps_map[] = {
2528 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2529 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2530 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2531 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2532};
2533
2534static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2535 const u8 *addr,
2536 const struct ieee80211_sta_ht_cap *ht_cap)
2537{
2538 int smps;
2539
2540 if (!ht_cap->ht_supported)
2541 return 0;
2542
2543 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2544 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2545
2546 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2547 return -EINVAL;
2548
2549 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2550 WMI_PEER_SMPS_STATE,
2551 ath10k_smps_map[smps]);
2552}
2553
139e170d
MK
2554static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2555 struct ieee80211_vif *vif,
2556 struct ieee80211_sta_vht_cap vht_cap)
2557{
2558 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2559 int ret;
2560 u32 param;
2561 u32 value;
2562
08e75ea8
VN
2563 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2564 return 0;
2565
139e170d
MK
2566 if (!(ar->vht_cap_info &
2567 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2568 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2569 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2570 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2571 return 0;
2572
2573 param = ar->wmi.vdev_param->txbf;
2574 value = 0;
2575
2576 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2577 return 0;
2578
2579 /* The following logic is correct. If a remote STA advertises support
2580 * for being a beamformer then we should enable us being a beamformee.
2581 */
2582
2583 if (ar->vht_cap_info &
2584 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2585 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2586 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2587 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2588
2589 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2590 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2591 }
2592
2593 if (ar->vht_cap_info &
2594 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2595 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2596 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2597 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2598
2599 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2600 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2601 }
2602
2603 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2604 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2605
2606 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2607 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2608
2609 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2610 if (ret) {
2611 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2612 value, ret);
2613 return ret;
2614 }
2615
2616 return 0;
2617}
2618
5e3dd157
KV
2619/* can be called only in mac80211 callbacks due to `key_count` usage */
2620static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2621 struct ieee80211_vif *vif,
2622 struct ieee80211_bss_conf *bss_conf)
2623{
2624 struct ath10k *ar = hw->priv;
2625 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
90046f50 2626 struct ieee80211_sta_ht_cap ht_cap;
139e170d 2627 struct ieee80211_sta_vht_cap vht_cap;
b9ada65d 2628 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
2629 struct ieee80211_sta *ap_sta;
2630 int ret;
2631
548db54c
MK
2632 lockdep_assert_held(&ar->conf_mutex);
2633
077efc8c
MK
2634 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2635 arvif->vdev_id, arvif->bssid, arvif->aid);
2636
5e3dd157
KV
2637 rcu_read_lock();
2638
2639 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2640 if (!ap_sta) {
7aa7a72a 2641 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
69244e56 2642 bss_conf->bssid, arvif->vdev_id);
5e3dd157
KV
2643 rcu_read_unlock();
2644 return;
2645 }
2646
90046f50
MK
2647 /* ap_sta must be accessed only within rcu section which must be left
2648 * before calling ath10k_setup_peer_smps() which might sleep. */
2649 ht_cap = ap_sta->ht_cap;
139e170d 2650 vht_cap = ap_sta->vht_cap;
90046f50 2651
590922a8 2652 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
5e3dd157 2653 if (ret) {
7aa7a72a 2654 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
69244e56 2655 bss_conf->bssid, arvif->vdev_id, ret);
5e3dd157
KV
2656 rcu_read_unlock();
2657 return;
2658 }
2659
2660 rcu_read_unlock();
2661
b9ada65d
KV
2662 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2663 if (ret) {
7aa7a72a 2664 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
69244e56 2665 bss_conf->bssid, arvif->vdev_id, ret);
b9ada65d
KV
2666 return;
2667 }
2668
90046f50
MK
2669 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2670 if (ret) {
7aa7a72a 2671 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
69244e56 2672 arvif->vdev_id, ret);
90046f50
MK
2673 return;
2674 }
2675
139e170d
MK
2676 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2677 if (ret) {
2678 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2679 arvif->vdev_id, bss_conf->bssid, ret);
2680 return;
2681 }
2682
7aa7a72a 2683 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
2684 "mac vdev %d up (associated) bssid %pM aid %d\n",
2685 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2686
077efc8c
MK
2687 WARN_ON(arvif->is_up);
2688
c930f744 2689 arvif->aid = bss_conf->aid;
b25f32cb 2690 ether_addr_copy(arvif->bssid, bss_conf->bssid);
c930f744
MK
2691
2692 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2693 if (ret) {
7aa7a72a 2694 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
5e3dd157 2695 arvif->vdev_id, ret);
c930f744
MK
2696 return;
2697 }
2698
2699 arvif->is_up = true;
0a987fb0
MK
2700
2701 /* Workaround: Some firmware revisions (tested with qca6174
2702 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2703 * poked with peer param command.
2704 */
2705 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2706 WMI_PEER_DUMMY_VAR, 1);
2707 if (ret) {
2708 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2709 arvif->bssid, arvif->vdev_id, ret);
2710 return;
2711 }
5e3dd157
KV
2712}
2713
5e3dd157
KV
2714static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2715 struct ieee80211_vif *vif)
2716{
2717 struct ath10k *ar = hw->priv;
2718 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
139e170d 2719 struct ieee80211_sta_vht_cap vht_cap = {};
5e3dd157
KV
2720 int ret;
2721
548db54c
MK
2722 lockdep_assert_held(&ar->conf_mutex);
2723
077efc8c
MK
2724 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2725 arvif->vdev_id, arvif->bssid);
60c3daa8 2726
5e3dd157 2727 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
077efc8c
MK
2728 if (ret)
2729 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2730 arvif->vdev_id, ret);
5e3dd157 2731
627613f8
SJ
2732 arvif->def_wep_key_idx = -1;
2733
139e170d
MK
2734 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2735 if (ret) {
2736 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2737 arvif->vdev_id, ret);
2738 return;
2739 }
2740
c930f744 2741 arvif->is_up = false;
cc9904e6
MK
2742
2743 cancel_delayed_work_sync(&arvif->connection_loss_work);
5e3dd157
KV
2744}
2745
590922a8
MK
2746static int ath10k_station_assoc(struct ath10k *ar,
2747 struct ieee80211_vif *vif,
2748 struct ieee80211_sta *sta,
2749 bool reassoc)
5e3dd157 2750{
590922a8 2751 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
b9ada65d 2752 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
2753 int ret = 0;
2754
548db54c
MK
2755 lockdep_assert_held(&ar->conf_mutex);
2756
590922a8 2757 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
b9ada65d 2758 if (ret) {
7aa7a72a 2759 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
69244e56 2760 sta->addr, arvif->vdev_id, ret);
b9ada65d
KV
2761 return ret;
2762 }
2763
2764 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
5e3dd157 2765 if (ret) {
7aa7a72a 2766 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
69244e56 2767 sta->addr, arvif->vdev_id, ret);
5e3dd157
KV
2768 return ret;
2769 }
2770
b1ecde36
MK
2771 /* Re-assoc is run only to update supported rates for given station. It
2772 * doesn't make much sense to reconfigure the peer completely.
2773 */
2774 if (!reassoc) {
2775 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2776 &sta->ht_cap);
e81bd104 2777 if (ret) {
b1ecde36 2778 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
e81bd104
MK
2779 arvif->vdev_id, ret);
2780 return ret;
2781 }
e81bd104 2782
b1ecde36
MK
2783 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2784 if (ret) {
2785 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2786 sta->addr, arvif->vdev_id, ret);
2787 return ret;
2788 }
5e3dd157 2789
b1ecde36
MK
2790 if (!sta->wme) {
2791 arvif->num_legacy_stations++;
2792 ret = ath10k_recalc_rtscts_prot(arvif);
2793 if (ret) {
2794 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2795 arvif->vdev_id, ret);
2796 return ret;
2797 }
2798 }
2799
627613f8
SJ
2800 /* Plumb cached keys only for static WEP */
2801 if (arvif->def_wep_key_idx != -1) {
2802 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2803 if (ret) {
2804 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2805 arvif->vdev_id, ret);
2806 return ret;
2807 }
b1ecde36 2808 }
d3d3ff42
JD
2809 }
2810
5e3dd157
KV
2811 return ret;
2812}
2813
590922a8
MK
2814static int ath10k_station_disassoc(struct ath10k *ar,
2815 struct ieee80211_vif *vif,
5e3dd157
KV
2816 struct ieee80211_sta *sta)
2817{
590922a8 2818 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5e3dd157
KV
2819 int ret = 0;
2820
548db54c
MK
2821 lockdep_assert_held(&ar->conf_mutex);
2822
e81bd104
MK
2823 if (!sta->wme) {
2824 arvif->num_legacy_stations--;
2825 ret = ath10k_recalc_rtscts_prot(arvif);
2826 if (ret) {
7aa7a72a 2827 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
e81bd104
MK
2828 arvif->vdev_id, ret);
2829 return ret;
2830 }
2831 }
2832
5e3dd157
KV
2833 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2834 if (ret) {
7aa7a72a 2835 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
69244e56 2836 arvif->vdev_id, ret);
5e3dd157
KV
2837 return ret;
2838 }
2839
2840 return ret;
2841}
2842
2843/**************/
2844/* Regulatory */
2845/**************/
2846
2847static int ath10k_update_channel_list(struct ath10k *ar)
2848{
2849 struct ieee80211_hw *hw = ar->hw;
2850 struct ieee80211_supported_band **bands;
57fbcce3 2851 enum nl80211_band band;
5e3dd157
KV
2852 struct ieee80211_channel *channel;
2853 struct wmi_scan_chan_list_arg arg = {0};
2854 struct wmi_channel_arg *ch;
2855 bool passive;
2856 int len;
2857 int ret;
2858 int i;
2859
548db54c
MK
2860 lockdep_assert_held(&ar->conf_mutex);
2861
5e3dd157 2862 bands = hw->wiphy->bands;
57fbcce3 2863 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5e3dd157
KV
2864 if (!bands[band])
2865 continue;
2866
2867 for (i = 0; i < bands[band]->n_channels; i++) {
2868 if (bands[band]->channels[i].flags &
2869 IEEE80211_CHAN_DISABLED)
2870 continue;
2871
2872 arg.n_channels++;
2873 }
2874 }
2875
2876 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2877 arg.channels = kzalloc(len, GFP_KERNEL);
2878 if (!arg.channels)
2879 return -ENOMEM;
2880
2881 ch = arg.channels;
57fbcce3 2882 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5e3dd157
KV
2883 if (!bands[band])
2884 continue;
2885
2886 for (i = 0; i < bands[band]->n_channels; i++) {
2887 channel = &bands[band]->channels[i];
2888
2889 if (channel->flags & IEEE80211_CHAN_DISABLED)
2890 continue;
2891
2892 ch->allow_ht = true;
2893
2894 /* FIXME: when should we really allow VHT? */
2895 ch->allow_vht = true;
2896
2897 ch->allow_ibss =
8fe02e16 2898 !(channel->flags & IEEE80211_CHAN_NO_IR);
5e3dd157
KV
2899
2900 ch->ht40plus =
2901 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2902
e8a50f8b
MP
2903 ch->chan_radar =
2904 !!(channel->flags & IEEE80211_CHAN_RADAR);
2905
8fe02e16 2906 passive = channel->flags & IEEE80211_CHAN_NO_IR;
5e3dd157
KV
2907 ch->passive = passive;
2908
2909 ch->freq = channel->center_freq;
2d66721c 2910 ch->band_center_freq1 = channel->center_freq;
89c5c843 2911 ch->min_power = 0;
02256930
MK
2912 ch->max_power = channel->max_power * 2;
2913 ch->max_reg_power = channel->max_reg_power * 2;
2914 ch->max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157
KV
2915 ch->reg_class_id = 0; /* FIXME */
2916
2917 /* FIXME: why use only legacy modes, why not any
2918 * HT/VHT modes? Would that even make any
2919 * difference? */
57fbcce3 2920 if (channel->band == NL80211_BAND_2GHZ)
5e3dd157
KV
2921 ch->mode = MODE_11G;
2922 else
2923 ch->mode = MODE_11A;
2924
2925 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2926 continue;
2927
7aa7a72a 2928 ath10k_dbg(ar, ATH10K_DBG_WMI,
60c3daa8
KV
2929 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2930 ch - arg.channels, arg.n_channels,
5e3dd157
KV
2931 ch->freq, ch->max_power, ch->max_reg_power,
2932 ch->max_antenna_gain, ch->mode);
2933
2934 ch++;
2935 }
2936 }
2937
2938 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2939 kfree(arg.channels);
2940
2941 return ret;
2942}
2943
821af6ae
MP
2944static enum wmi_dfs_region
2945ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2946{
2947 switch (dfs_region) {
2948 case NL80211_DFS_UNSET:
2949 return WMI_UNINIT_DFS_DOMAIN;
2950 case NL80211_DFS_FCC:
2951 return WMI_FCC_DFS_DOMAIN;
2952 case NL80211_DFS_ETSI:
2953 return WMI_ETSI_DFS_DOMAIN;
2954 case NL80211_DFS_JP:
2955 return WMI_MKK4_DFS_DOMAIN;
2956 }
2957 return WMI_UNINIT_DFS_DOMAIN;
2958}
2959
f7843d7f 2960static void ath10k_regd_update(struct ath10k *ar)
5e3dd157 2961{
5e3dd157 2962 struct reg_dmn_pair_mapping *regpair;
5e3dd157 2963 int ret;
821af6ae
MP
2964 enum wmi_dfs_region wmi_dfs_reg;
2965 enum nl80211_dfs_regions nl_dfs_reg;
5e3dd157 2966
f7843d7f 2967 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
2968
2969 ret = ath10k_update_channel_list(ar);
2970 if (ret)
7aa7a72a 2971 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
5e3dd157
KV
2972
2973 regpair = ar->ath_common.regulatory.regpair;
f7843d7f 2974
821af6ae
MP
2975 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2976 nl_dfs_reg = ar->dfs_detector->region;
2977 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2978 } else {
2979 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2980 }
2981
5e3dd157
KV
2982 /* Target allows setting up per-band regdomain but ath_common provides
2983 * a combined one only */
2984 ret = ath10k_wmi_pdev_set_regdomain(ar,
ef8c0017
KV
2985 regpair->reg_domain,
2986 regpair->reg_domain, /* 2ghz */
2987 regpair->reg_domain, /* 5ghz */
5e3dd157 2988 regpair->reg_2ghz_ctl,
821af6ae
MP
2989 regpair->reg_5ghz_ctl,
2990 wmi_dfs_reg);
5e3dd157 2991 if (ret)
7aa7a72a 2992 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
f7843d7f 2993}
548db54c 2994
f7843d7f
MK
2995static void ath10k_reg_notifier(struct wiphy *wiphy,
2996 struct regulatory_request *request)
2997{
2998 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2999 struct ath10k *ar = hw->priv;
9702c686 3000 bool result;
f7843d7f
MK
3001
3002 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3003
9702c686 3004 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
7aa7a72a 3005 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
9702c686
JD
3006 request->dfs_region);
3007 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3008 request->dfs_region);
3009 if (!result)
7aa7a72a 3010 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
9702c686
JD
3011 request->dfs_region);
3012 }
3013
f7843d7f
MK
3014 mutex_lock(&ar->conf_mutex);
3015 if (ar->state == ATH10K_STATE_ON)
3016 ath10k_regd_update(ar);
548db54c 3017 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
3018}
3019
3020/***************/
3021/* TX handlers */
3022/***************/
3023
a30c7d00
MK
3024enum ath10k_mac_tx_path {
3025 ATH10K_MAC_TX_HTT,
3026 ATH10K_MAC_TX_HTT_MGMT,
3027 ATH10K_MAC_TX_WMI_MGMT,
3028 ATH10K_MAC_TX_UNKNOWN,
3029};
3030
96d828d4 3031void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
42c3aa6f 3032{
96d828d4 3033 lockdep_assert_held(&ar->htt.tx_lock);
42c3aa6f 3034
96d828d4
MK
3035 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3036 ar->tx_paused |= BIT(reason);
3037 ieee80211_stop_queues(ar->hw);
42c3aa6f
MK
3038}
3039
96d828d4
MK
3040static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3041 struct ieee80211_vif *vif)
ddb6ad77 3042{
96d828d4
MK
3043 struct ath10k *ar = data;
3044 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
ddb6ad77 3045
96d828d4
MK
3046 if (arvif->tx_paused)
3047 return;
ddb6ad77 3048
96d828d4 3049 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
ddb6ad77
MK
3050}
3051
96d828d4 3052void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
5e3dd157 3053{
96d828d4 3054 lockdep_assert_held(&ar->htt.tx_lock);
5e3dd157 3055
96d828d4
MK
3056 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3057 ar->tx_paused &= ~BIT(reason);
5e3dd157 3058
96d828d4
MK
3059 if (ar->tx_paused)
3060 return;
c21c64d1 3061
96d828d4
MK
3062 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3063 IEEE80211_IFACE_ITER_RESUME_ALL,
3064 ath10k_mac_tx_unlock_iter,
3065 ar);
3a73d1a6
MK
3066
3067 ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
5e3dd157
KV
3068}
3069
96d828d4 3070void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
5e3dd157 3071{
96d828d4 3072 struct ath10k *ar = arvif->ar;
5e3dd157 3073
96d828d4 3074 lockdep_assert_held(&ar->htt.tx_lock);
5e3dd157 3075
96d828d4
MK
3076 WARN_ON(reason >= BITS_PER_LONG);
3077 arvif->tx_paused |= BIT(reason);
3078 ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3079}
3080
3081void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3082{
3083 struct ath10k *ar = arvif->ar;
3084
3085 lockdep_assert_held(&ar->htt.tx_lock);
3086
3087 WARN_ON(reason >= BITS_PER_LONG);
3088 arvif->tx_paused &= ~BIT(reason);
3089
3090 if (ar->tx_paused)
3091 return;
3092
3093 if (arvif->tx_paused)
3094 return;
3095
3096 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3097}
3098
b4aa539d
MK
3099static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3100 enum wmi_tlv_tx_pause_id pause_id,
3101 enum wmi_tlv_tx_pause_action action)
3102{
3103 struct ath10k *ar = arvif->ar;
3104
3105 lockdep_assert_held(&ar->htt.tx_lock);
3106
acd0b27b
MK
3107 switch (action) {
3108 case WMI_TLV_TX_PAUSE_ACTION_STOP:
3109 ath10k_mac_vif_tx_lock(arvif, pause_id);
3110 break;
3111 case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3112 ath10k_mac_vif_tx_unlock(arvif, pause_id);
b4aa539d 3113 break;
b4aa539d 3114 default:
acd0b27b
MK
3115 ath10k_warn(ar, "received unknown tx pause action %d on vdev %i, ignoring\n",
3116 action, arvif->vdev_id);
b4aa539d
MK
3117 break;
3118 }
3119}
3120
3121struct ath10k_mac_tx_pause {
3122 u32 vdev_id;
3123 enum wmi_tlv_tx_pause_id pause_id;
3124 enum wmi_tlv_tx_pause_action action;
3125};
3126
3127static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3128 struct ieee80211_vif *vif)
3129{
3130 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3131 struct ath10k_mac_tx_pause *arg = data;
3132
acd0b27b
MK
3133 if (arvif->vdev_id != arg->vdev_id)
3134 return;
3135
b4aa539d
MK
3136 ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3137}
3138
acd0b27b
MK
3139void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3140 enum wmi_tlv_tx_pause_id pause_id,
3141 enum wmi_tlv_tx_pause_action action)
b4aa539d
MK
3142{
3143 struct ath10k_mac_tx_pause arg = {
3144 .vdev_id = vdev_id,
3145 .pause_id = pause_id,
3146 .action = action,
3147 };
3148
3149 spin_lock_bh(&ar->htt.tx_lock);
3150 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3151 IEEE80211_IFACE_ITER_RESUME_ALL,
3152 ath10k_mac_handle_tx_pause_iter,
3153 &arg);
3154 spin_unlock_bh(&ar->htt.tx_lock);
3155}
3156
d740d8fd 3157static enum ath10k_hw_txrx_mode
6a2636d8
MK
3158ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3159 struct ieee80211_vif *vif,
3160 struct ieee80211_sta *sta,
3161 struct sk_buff *skb)
d740d8fd
MK
3162{
3163 const struct ieee80211_hdr *hdr = (void *)skb->data;
3164 __le16 fc = hdr->frame_control;
3165
3166 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3167 return ATH10K_HW_TXRX_RAW;
3168
3169 if (ieee80211_is_mgmt(fc))
3170 return ATH10K_HW_TXRX_MGMT;
3171
3172 /* Workaround:
3173 *
3174 * NullFunc frames are mostly used to ping if a client or AP are still
3175 * reachable and responsive. This implies tx status reports must be
3176 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3177 * come to a conclusion that the other end disappeared and tear down
3178 * BSS connection or it can never disconnect from BSS/client (which is
3179 * the case).
3180 *
3181 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3182 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3183 * which seems to deliver correct tx reports for NullFunc frames. The
3184 * downside of using it is it ignores client powersave state so it can
3185 * end up disconnecting sleeping clients in AP mode. It should fix STA
3186 * mode though because AP don't sleep.
3187 */
3188 if (ar->htt.target_version_major < 3 &&
3189 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3190 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
3191 return ATH10K_HW_TXRX_MGMT;
3192
75d85fd9
MP
3193 /* Workaround:
3194 *
3195 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3196 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3197 * to work with Ethernet txmode so use it.
ccec9038
DL
3198 *
3199 * FIXME: Check if raw mode works with TDLS.
75d85fd9
MP
3200 */
3201 if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3202 return ATH10K_HW_TXRX_ETHERNET;
3203
ccec9038
DL
3204 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
3205 return ATH10K_HW_TXRX_RAW;
3206
d740d8fd
MK
3207 return ATH10K_HW_TXRX_NATIVE_WIFI;
3208}
3209
ccec9038 3210static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
fd12cb32
MK
3211 struct sk_buff *skb)
3212{
3213 const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3214 const struct ieee80211_hdr *hdr = (void *)skb->data;
ccec9038
DL
3215 const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3216 IEEE80211_TX_CTL_INJECTED;
fd12cb32
MK
3217
3218 if (!ieee80211_has_protected(hdr->frame_control))
3219 return false;
3220
ccec9038
DL
3221 if ((info->flags & mask) == mask)
3222 return false;
fd12cb32 3223
ccec9038
DL
3224 if (vif)
3225 return !ath10k_vif_to_arvif(vif)->nohwcrypt;
fd12cb32 3226
ccec9038
DL
3227 return true;
3228}
3229
4b604558
MK
3230/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3231 * Control in the header.
5e3dd157 3232 */
4b604558 3233static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
5e3dd157
KV
3234{
3235 struct ieee80211_hdr *hdr = (void *)skb->data;
c21c64d1 3236 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
5e3dd157
KV
3237 u8 *qos_ctl;
3238
3239 if (!ieee80211_is_data_qos(hdr->frame_control))
3240 return;
3241
3242 qos_ctl = ieee80211_get_qos_ctl(hdr);
ba0ccd7a
MK
3243 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3244 skb->data, (void *)qos_ctl - (void *)skb->data);
3245 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
c21c64d1 3246
8bad8dcd
MK
3247 /* Some firmware revisions don't handle sending QoS NullFunc well.
3248 * These frames are mainly used for CQM purposes so it doesn't really
3249 * matter whether QoS NullFunc or NullFunc are sent.
c21c64d1 3250 */
bf0a26d3 3251 hdr = (void *)skb->data;
8bad8dcd 3252 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
609db229 3253 cb->flags &= ~ATH10K_SKB_F_QOS;
8bad8dcd
MK
3254
3255 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
5e3dd157
KV
3256}
3257
d740d8fd
MK
3258static void ath10k_tx_h_8023(struct sk_buff *skb)
3259{
3260 struct ieee80211_hdr *hdr;
3261 struct rfc1042_hdr *rfc1042;
3262 struct ethhdr *eth;
3263 size_t hdrlen;
3264 u8 da[ETH_ALEN];
3265 u8 sa[ETH_ALEN];
3266 __be16 type;
3267
3268 hdr = (void *)skb->data;
3269 hdrlen = ieee80211_hdrlen(hdr->frame_control);
3270 rfc1042 = (void *)skb->data + hdrlen;
3271
3272 ether_addr_copy(da, ieee80211_get_DA(hdr));
3273 ether_addr_copy(sa, ieee80211_get_SA(hdr));
3274 type = rfc1042->snap_type;
3275
3276 skb_pull(skb, hdrlen + sizeof(*rfc1042));
3277 skb_push(skb, sizeof(*eth));
3278
3279 eth = (void *)skb->data;
3280 ether_addr_copy(eth->h_dest, da);
3281 ether_addr_copy(eth->h_source, sa);
3282 eth->h_proto = type;
5e3dd157
KV
3283}
3284
4b604558
MK
3285static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3286 struct ieee80211_vif *vif,
3287 struct sk_buff *skb)
5e3dd157
KV
3288{
3289 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
5e3dd157
KV
3290 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3291
3292 /* This is case only for P2P_GO */
08c27be1 3293 if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
5e3dd157
KV
3294 return;
3295
3296 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3297 spin_lock_bh(&ar->data_lock);
3298 if (arvif->u.ap.noa_data)
3299 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3300 GFP_ATOMIC))
3301 memcpy(skb_put(skb, arvif->u.ap.noa_len),
3302 arvif->u.ap.noa_data,
3303 arvif->u.ap.noa_len);
3304 spin_unlock_bh(&ar->data_lock);
3305 }
3306}
3307
f2f6ecab
MK
3308static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3309 struct ieee80211_vif *vif,
dd4717b6 3310 struct ieee80211_txq *txq,
f2f6ecab
MK
3311 struct sk_buff *skb)
3312{
3313 struct ieee80211_hdr *hdr = (void *)skb->data;
3314 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3315
3316 cb->flags = 0;
3317 if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3318 cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3319
3320 if (ieee80211_is_mgmt(hdr->frame_control))
3321 cb->flags |= ATH10K_SKB_F_MGMT;
3322
3323 if (ieee80211_is_data_qos(hdr->frame_control))
3324 cb->flags |= ATH10K_SKB_F_QOS;
3325
3326 cb->vif = vif;
dd4717b6 3327 cb->txq = txq;
f2f6ecab
MK
3328}
3329
d39de991 3330bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
8d6d3624
MK
3331{
3332 /* FIXME: Not really sure since when the behaviour changed. At some
3333 * point new firmware stopped requiring creation of peer entries for
3334 * offchannel tx (and actually creating them causes issues with wmi-htc
3335 * tx credit replenishment and reliability). Assuming it's at least 3.4
3336 * because that's when the `freq` was introduced to TX_FRM HTT command.
3337 */
8921f5f7 3338 return (ar->htt.target_version_major >= 3 &&
d39de991
VT
3339 ar->htt.target_version_minor >= 4 &&
3340 ar->htt.op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
8d6d3624
MK
3341}
3342
d740d8fd 3343static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
5e3dd157 3344{
d740d8fd 3345 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
5e00d31a 3346 int ret = 0;
5e3dd157 3347
d740d8fd
MK
3348 spin_lock_bh(&ar->data_lock);
3349
3350 if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
3351 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3352 ret = -ENOSPC;
3353 goto unlock;
961d4c38
MK
3354 }
3355
d740d8fd
MK
3356 __skb_queue_tail(q, skb);
3357 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
5e00d31a 3358
d740d8fd
MK
3359unlock:
3360 spin_unlock_bh(&ar->data_lock);
3361
3362 return ret;
3363}
3364
a30c7d00
MK
3365static enum ath10k_mac_tx_path
3366ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3367 struct sk_buff *skb,
3368 enum ath10k_hw_txrx_mode txmode)
d740d8fd 3369{
8a933964 3370 switch (txmode) {
d740d8fd
MK
3371 case ATH10K_HW_TXRX_RAW:
3372 case ATH10K_HW_TXRX_NATIVE_WIFI:
3373 case ATH10K_HW_TXRX_ETHERNET:
a30c7d00 3374 return ATH10K_MAC_TX_HTT;
d740d8fd
MK
3375 case ATH10K_HW_TXRX_MGMT:
3376 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3377 ar->fw_features))
a30c7d00 3378 return ATH10K_MAC_TX_WMI_MGMT;
d740d8fd 3379 else if (ar->htt.target_version_major >= 3)
a30c7d00 3380 return ATH10K_MAC_TX_HTT;
d740d8fd 3381 else
a30c7d00
MK
3382 return ATH10K_MAC_TX_HTT_MGMT;
3383 }
3384
3385 return ATH10K_MAC_TX_UNKNOWN;
3386}
3387
3388static int ath10k_mac_tx_submit(struct ath10k *ar,
3389 enum ath10k_hw_txrx_mode txmode,
6421969f 3390 enum ath10k_mac_tx_path txpath,
a30c7d00
MK
3391 struct sk_buff *skb)
3392{
3393 struct ath10k_htt *htt = &ar->htt;
6421969f 3394 int ret = -EINVAL;
a30c7d00
MK
3395
3396 switch (txpath) {
3397 case ATH10K_MAC_TX_HTT:
3398 ret = ath10k_htt_tx(htt, txmode, skb);
3399 break;
3400 case ATH10K_MAC_TX_HTT_MGMT:
3401 ret = ath10k_htt_mgmt_tx(htt, skb);
3402 break;
3403 case ATH10K_MAC_TX_WMI_MGMT:
3404 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3405 break;
3406 case ATH10K_MAC_TX_UNKNOWN:
3407 WARN_ON_ONCE(1);
3408 ret = -EINVAL;
d740d8fd 3409 break;
5e00d31a 3410 }
5e3dd157
KV
3411
3412 if (ret) {
7aa7a72a
MK
3413 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3414 ret);
5e3dd157
KV
3415 ieee80211_free_txskb(ar->hw, skb);
3416 }
f2f6ecab
MK
3417
3418 return ret;
3419}
3420
3421/* This function consumes the sk_buff regardless of return value as far as
3422 * caller is concerned so no freeing is necessary afterwards.
3423 */
3424static int ath10k_mac_tx(struct ath10k *ar,
3425 struct ieee80211_vif *vif,
3426 struct ieee80211_sta *sta,
3427 enum ath10k_hw_txrx_mode txmode,
6421969f 3428 enum ath10k_mac_tx_path txpath,
f2f6ecab
MK
3429 struct sk_buff *skb)
3430{
3431 struct ieee80211_hw *hw = ar->hw;
3432 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3433 int ret;
3434
3435 /* We should disable CCK RATE due to P2P */
3436 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3437 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3438
3439 switch (txmode) {
3440 case ATH10K_HW_TXRX_MGMT:
3441 case ATH10K_HW_TXRX_NATIVE_WIFI:
3442 ath10k_tx_h_nwifi(hw, skb);
3443 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3444 ath10k_tx_h_seq_no(vif, skb);
3445 break;
3446 case ATH10K_HW_TXRX_ETHERNET:
3447 ath10k_tx_h_8023(skb);
3448 break;
3449 case ATH10K_HW_TXRX_RAW:
3450 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
3451 WARN_ON_ONCE(1);
3452 ieee80211_free_txskb(hw, skb);
3453 return -ENOTSUPP;
3454 }
3455 }
3456
3457 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3458 if (!ath10k_mac_tx_frm_has_freq(ar)) {
3459 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
3460 skb);
3461
3462 skb_queue_tail(&ar->offchan_tx_queue, skb);
3463 ieee80211_queue_work(hw, &ar->offchan_tx_work);
3464 return 0;
3465 }
3466 }
3467
6421969f 3468 ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
f2f6ecab
MK
3469 if (ret) {
3470 ath10k_warn(ar, "failed to submit frame: %d\n", ret);
3471 return ret;
3472 }
3473
3474 return 0;
5e3dd157
KV
3475}
3476
3477void ath10k_offchan_tx_purge(struct ath10k *ar)
3478{
3479 struct sk_buff *skb;
3480
3481 for (;;) {
3482 skb = skb_dequeue(&ar->offchan_tx_queue);
3483 if (!skb)
3484 break;
3485
3486 ieee80211_free_txskb(ar->hw, skb);
3487 }
3488}
3489
3490void ath10k_offchan_tx_work(struct work_struct *work)
3491{
3492 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
3493 struct ath10k_peer *peer;
8a933964 3494 struct ath10k_vif *arvif;
f2f6ecab 3495 enum ath10k_hw_txrx_mode txmode;
6421969f 3496 enum ath10k_mac_tx_path txpath;
5e3dd157 3497 struct ieee80211_hdr *hdr;
8a933964
MK
3498 struct ieee80211_vif *vif;
3499 struct ieee80211_sta *sta;
5e3dd157
KV
3500 struct sk_buff *skb;
3501 const u8 *peer_addr;
3502 int vdev_id;
3503 int ret;
8e9904f5 3504 unsigned long time_left;
adaeed74 3505 bool tmp_peer_created = false;
5e3dd157
KV
3506
3507 /* FW requirement: We must create a peer before FW will send out
3508 * an offchannel frame. Otherwise the frame will be stuck and
3509 * never transmitted. We delete the peer upon tx completion.
3510 * It is unlikely that a peer for offchannel tx will already be
3511 * present. However it may be in some rare cases so account for that.
3512 * Otherwise we might remove a legitimate peer and break stuff. */
3513
3514 for (;;) {
3515 skb = skb_dequeue(&ar->offchan_tx_queue);
3516 if (!skb)
3517 break;
3518
3519 mutex_lock(&ar->conf_mutex);
3520
7aa7a72a 3521 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
5e3dd157
KV
3522 skb);
3523
3524 hdr = (struct ieee80211_hdr *)skb->data;
3525 peer_addr = ieee80211_get_DA(hdr);
5e3dd157
KV
3526
3527 spin_lock_bh(&ar->data_lock);
609db229 3528 vdev_id = ar->scan.vdev_id;
5e3dd157
KV
3529 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
3530 spin_unlock_bh(&ar->data_lock);
3531
3532 if (peer)
60c3daa8 3533 /* FIXME: should this use ath10k_warn()? */
7aa7a72a 3534 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
5e3dd157
KV
3535 peer_addr, vdev_id);
3536
3537 if (!peer) {
6942726f
MK
3538 ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
3539 peer_addr,
7390ed34 3540 WMI_PEER_TYPE_DEFAULT);
5e3dd157 3541 if (ret)
7aa7a72a 3542 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
5e3dd157 3543 peer_addr, vdev_id, ret);
adaeed74 3544 tmp_peer_created = (ret == 0);
5e3dd157
KV
3545 }
3546
3547 spin_lock_bh(&ar->data_lock);
16735d02 3548 reinit_completion(&ar->offchan_tx_completed);
5e3dd157
KV
3549 ar->offchan_tx_skb = skb;
3550 spin_unlock_bh(&ar->data_lock);
3551
8a933964
MK
3552 /* It's safe to access vif and sta - conf_mutex guarantees that
3553 * sta_state() and remove_interface() are locked exclusively
3554 * out wrt to this offchannel worker.
3555 */
3556 arvif = ath10k_get_arvif(ar, vdev_id);
3557 if (arvif) {
3558 vif = arvif->vif;
3559 sta = ieee80211_find_sta(vif, peer_addr);
3560 } else {
3561 vif = NULL;
3562 sta = NULL;
3563 }
3564
3565 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
6421969f 3566 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
8a933964 3567
6421969f 3568 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
f2f6ecab
MK
3569 if (ret) {
3570 ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
3571 ret);
3572 /* not serious */
3573 }
5e3dd157 3574
8e9904f5
NMG
3575 time_left =
3576 wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
3577 if (time_left == 0)
7aa7a72a 3578 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
5e3dd157
KV
3579 skb);
3580
adaeed74 3581 if (!peer && tmp_peer_created) {
5e3dd157
KV
3582 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
3583 if (ret)
7aa7a72a 3584 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
5e3dd157
KV
3585 peer_addr, vdev_id, ret);
3586 }
3587
3588 mutex_unlock(&ar->conf_mutex);
3589 }
3590}
3591
5e00d31a
BM
3592void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
3593{
3594 struct sk_buff *skb;
3595
3596 for (;;) {
3597 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3598 if (!skb)
3599 break;
3600
3601 ieee80211_free_txskb(ar->hw, skb);
3602 }
3603}
3604
3605void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
3606{
3607 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
3608 struct sk_buff *skb;
3609 int ret;
3610
3611 for (;;) {
3612 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3613 if (!skb)
3614 break;
3615
3616 ret = ath10k_wmi_mgmt_tx(ar, skb);
5fb5e41f 3617 if (ret) {
7aa7a72a 3618 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
be6546fc 3619 ret);
5fb5e41f
MK
3620 ieee80211_free_txskb(ar->hw, skb);
3621 }
5e00d31a
BM
3622 }
3623}
3624
29946878
MK
3625static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
3626{
3627 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3628
3629 if (!txq)
3630 return;
3631
3632 INIT_LIST_HEAD(&artxq->list);
3633}
3634
3635static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
3636{
3637 struct ath10k_txq *artxq = (void *)txq->drv_priv;
dd4717b6
MK
3638 struct ath10k_skb_cb *cb;
3639 struct sk_buff *msdu;
3640 int msdu_id;
29946878
MK
3641
3642 if (!txq)
3643 return;
3644
3645 spin_lock_bh(&ar->txqs_lock);
3646 if (!list_empty(&artxq->list))
3647 list_del_init(&artxq->list);
3648 spin_unlock_bh(&ar->txqs_lock);
dd4717b6
MK
3649
3650 spin_lock_bh(&ar->htt.tx_lock);
3651 idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
3652 cb = ATH10K_SKB_CB(msdu);
3653 if (cb->txq == txq)
3654 cb->txq = NULL;
3655 }
3656 spin_unlock_bh(&ar->htt.tx_lock);
29946878
MK
3657}
3658
426e10ea
MK
3659struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
3660 u16 peer_id,
3661 u8 tid)
3662{
3663 struct ath10k_peer *peer;
3664
3665 lockdep_assert_held(&ar->data_lock);
3666
3667 peer = ar->peer_map[peer_id];
3668 if (!peer)
3669 return NULL;
3670
3671 if (peer->sta)
3672 return peer->sta->txq[tid];
3673 else if (peer->vif)
3674 return peer->vif->txq;
3675 else
3676 return NULL;
3677}
3678
29946878
MK
3679static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
3680 struct ieee80211_txq *txq)
3681{
426e10ea
MK
3682 struct ath10k *ar = hw->priv;
3683 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3684
3685 /* No need to get locks */
3686
3687 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
3688 return true;
3689
3690 if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
3691 return true;
3692
3693 if (artxq->num_fw_queued < artxq->num_push_allowed)
3694 return true;
3695
3696 return false;
29946878
MK
3697}
3698
426e10ea
MK
3699int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
3700 struct ieee80211_txq *txq)
29946878 3701{
29946878
MK
3702 struct ath10k *ar = hw->priv;
3703 struct ath10k_htt *htt = &ar->htt;
3cc0fef6 3704 struct ath10k_txq *artxq = (void *)txq->drv_priv;
29946878
MK
3705 struct ieee80211_vif *vif = txq->vif;
3706 struct ieee80211_sta *sta = txq->sta;
3707 enum ath10k_hw_txrx_mode txmode;
3708 enum ath10k_mac_tx_path txpath;
3709 struct sk_buff *skb;
426e10ea 3710 size_t skb_len;
29946878
MK
3711 int ret;
3712
3713 spin_lock_bh(&ar->htt.tx_lock);
cac08552 3714 ret = ath10k_htt_tx_inc_pending(htt);
29946878
MK
3715 spin_unlock_bh(&ar->htt.tx_lock);
3716
3717 if (ret)
3718 return ret;
3719
3720 skb = ieee80211_tx_dequeue(hw, txq);
3721 if (!skb) {
3722 spin_lock_bh(&ar->htt.tx_lock);
cac08552 3723 ath10k_htt_tx_dec_pending(htt);
29946878
MK
3724 spin_unlock_bh(&ar->htt.tx_lock);
3725
3726 return -ENOENT;
3727 }
3728
dd4717b6 3729 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
29946878 3730
426e10ea 3731 skb_len = skb->len;
29946878
MK
3732 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3733 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3734
3735 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
3736 if (unlikely(ret)) {
3737 ath10k_warn(ar, "failed to push frame: %d\n", ret);
3738
3739 spin_lock_bh(&ar->htt.tx_lock);
cac08552 3740 ath10k_htt_tx_dec_pending(htt);
29946878
MK
3741 spin_unlock_bh(&ar->htt.tx_lock);
3742
3743 return ret;
3744 }
3745
3cc0fef6
MK
3746 spin_lock_bh(&ar->htt.tx_lock);
3747 artxq->num_fw_queued++;
3748 spin_unlock_bh(&ar->htt.tx_lock);
3749
426e10ea 3750 return skb_len;
29946878
MK
3751}
3752
3753void ath10k_mac_tx_push_pending(struct ath10k *ar)
3754{
3755 struct ieee80211_hw *hw = ar->hw;
3756 struct ieee80211_txq *txq;
3757 struct ath10k_txq *artxq;
3758 struct ath10k_txq *last;
3759 int ret;
3760 int max;
3761
3762 spin_lock_bh(&ar->txqs_lock);
3763 rcu_read_lock();
3764
3765 last = list_last_entry(&ar->txqs, struct ath10k_txq, list);
3766 while (!list_empty(&ar->txqs)) {
3767 artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
3768 txq = container_of((void *)artxq, struct ieee80211_txq,
3769 drv_priv);
3770
3771 /* Prevent aggressive sta/tid taking over tx queue */
3772 max = 16;
750eeed8
MK
3773 ret = 0;
3774 while (ath10k_mac_tx_can_push(hw, txq) && max--) {
29946878
MK
3775 ret = ath10k_mac_tx_push_txq(hw, txq);
3776 if (ret < 0)
3777 break;
3778 }
3779
3780 list_del_init(&artxq->list);
9d71d47e
MK
3781 if (ret != -ENOENT)
3782 list_add_tail(&artxq->list, &ar->txqs);
3783
c1a43d97 3784 ath10k_htt_tx_txq_update(hw, txq);
29946878 3785
9d71d47e 3786 if (artxq == last || (ret < 0 && ret != -ENOENT))
29946878 3787 break;
29946878
MK
3788 }
3789
3790 rcu_read_unlock();
3791 spin_unlock_bh(&ar->txqs_lock);
3792}
3793
5e3dd157
KV
3794/************/
3795/* Scanning */
3796/************/
3797
5c81c7fd 3798void __ath10k_scan_finish(struct ath10k *ar)
5e3dd157 3799{
5c81c7fd 3800 lockdep_assert_held(&ar->data_lock);
5e3dd157 3801
5c81c7fd
MK
3802 switch (ar->scan.state) {
3803 case ATH10K_SCAN_IDLE:
3804 break;
3805 case ATH10K_SCAN_RUNNING:
7305d3e0
MK
3806 case ATH10K_SCAN_ABORTING:
3807 if (!ar->scan.is_roc)
5c81c7fd
MK
3808 ieee80211_scan_completed(ar->hw,
3809 (ar->scan.state ==
3810 ATH10K_SCAN_ABORTING));
d710e75d
MK
3811 else if (ar->scan.roc_notify)
3812 ieee80211_remain_on_channel_expired(ar->hw);
5c81c7fd
MK
3813 /* fall through */
3814 case ATH10K_SCAN_STARTING:
3815 ar->scan.state = ATH10K_SCAN_IDLE;
3816 ar->scan_channel = NULL;
bd877440 3817 ar->scan.roc_freq = 0;
5c81c7fd
MK
3818 ath10k_offchan_tx_purge(ar);
3819 cancel_delayed_work(&ar->scan.timeout);
3820 complete_all(&ar->scan.completed);
3821 break;
5e3dd157 3822 }
5c81c7fd 3823}
5e3dd157 3824
5c81c7fd
MK
3825void ath10k_scan_finish(struct ath10k *ar)
3826{
3827 spin_lock_bh(&ar->data_lock);
3828 __ath10k_scan_finish(ar);
5e3dd157
KV
3829 spin_unlock_bh(&ar->data_lock);
3830}
3831
5c81c7fd 3832static int ath10k_scan_stop(struct ath10k *ar)
5e3dd157
KV
3833{
3834 struct wmi_stop_scan_arg arg = {
3835 .req_id = 1, /* FIXME */
3836 .req_type = WMI_SCAN_STOP_ONE,
3837 .u.scan_id = ATH10K_SCAN_ID,
3838 };
3839 int ret;
3840
3841 lockdep_assert_held(&ar->conf_mutex);
3842
5e3dd157
KV
3843 ret = ath10k_wmi_stop_scan(ar, &arg);
3844 if (ret) {
7aa7a72a 3845 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
5c81c7fd 3846 goto out;
5e3dd157
KV
3847 }
3848
5e3dd157 3849 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
5c81c7fd 3850 if (ret == 0) {
7aa7a72a 3851 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
5c81c7fd
MK
3852 ret = -ETIMEDOUT;
3853 } else if (ret > 0) {
3854 ret = 0;
3855 }
5e3dd157 3856
5c81c7fd
MK
3857out:
3858 /* Scan state should be updated upon scan completion but in case
3859 * firmware fails to deliver the event (for whatever reason) it is
3860 * desired to clean up scan state anyway. Firmware may have just
3861 * dropped the scan completion event delivery due to transport pipe
3862 * being overflown with data and/or it can recover on its own before
3863 * next scan request is submitted.
3864 */
3865 spin_lock_bh(&ar->data_lock);
3866 if (ar->scan.state != ATH10K_SCAN_IDLE)
3867 __ath10k_scan_finish(ar);
3868 spin_unlock_bh(&ar->data_lock);
3869
3870 return ret;
3871}
3872
3873static void ath10k_scan_abort(struct ath10k *ar)
3874{
3875 int ret;
3876
3877 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
3878
3879 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
3880
3881 switch (ar->scan.state) {
3882 case ATH10K_SCAN_IDLE:
3883 /* This can happen if timeout worker kicked in and called
3884 * abortion while scan completion was being processed.
3885 */
3886 break;
3887 case ATH10K_SCAN_STARTING:
3888 case ATH10K_SCAN_ABORTING:
7aa7a72a 3889 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
5c81c7fd
MK
3890 ath10k_scan_state_str(ar->scan.state),
3891 ar->scan.state);
3892 break;
3893 case ATH10K_SCAN_RUNNING:
3894 ar->scan.state = ATH10K_SCAN_ABORTING;
3895 spin_unlock_bh(&ar->data_lock);
3896
3897 ret = ath10k_scan_stop(ar);
3898 if (ret)
7aa7a72a 3899 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
5c81c7fd
MK
3900
3901 spin_lock_bh(&ar->data_lock);
3902 break;
5e3dd157 3903 }
5c81c7fd 3904
5e3dd157 3905 spin_unlock_bh(&ar->data_lock);
5c81c7fd 3906}
5e3dd157 3907
5c81c7fd
MK
3908void ath10k_scan_timeout_work(struct work_struct *work)
3909{
3910 struct ath10k *ar = container_of(work, struct ath10k,
3911 scan.timeout.work);
3912
3913 mutex_lock(&ar->conf_mutex);
3914 ath10k_scan_abort(ar);
3915 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
3916}
3917
3918static int ath10k_start_scan(struct ath10k *ar,
3919 const struct wmi_start_scan_arg *arg)
3920{
3921 int ret;
3922
3923 lockdep_assert_held(&ar->conf_mutex);
3924
3925 ret = ath10k_wmi_start_scan(ar, arg);
3926 if (ret)
3927 return ret;
3928
5e3dd157
KV
3929 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
3930 if (ret == 0) {
5c81c7fd
MK
3931 ret = ath10k_scan_stop(ar);
3932 if (ret)
7aa7a72a 3933 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd
MK
3934
3935 return -ETIMEDOUT;
5e3dd157
KV
3936 }
3937
2f9eec0b
BG
3938 /* If we failed to start the scan, return error code at
3939 * this point. This is probably due to some issue in the
3940 * firmware, but no need to wedge the driver due to that...
3941 */
3942 spin_lock_bh(&ar->data_lock);
3943 if (ar->scan.state == ATH10K_SCAN_IDLE) {
3944 spin_unlock_bh(&ar->data_lock);
3945 return -EINVAL;
3946 }
3947 spin_unlock_bh(&ar->data_lock);
3948
5e3dd157
KV
3949 return 0;
3950}
3951
3952/**********************/
3953/* mac80211 callbacks */
3954/**********************/
3955
f2f6ecab
MK
3956static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
3957 struct ieee80211_tx_control *control,
3958 struct sk_buff *skb)
5e3dd157 3959{
4b604558 3960 struct ath10k *ar = hw->priv;
6421969f 3961 struct ath10k_htt *htt = &ar->htt;
5e3dd157 3962 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4b604558 3963 struct ieee80211_vif *vif = info->control.vif;
75d85fd9 3964 struct ieee80211_sta *sta = control->sta;
dd4717b6 3965 struct ieee80211_txq *txq = NULL;
6421969f 3966 struct ieee80211_hdr *hdr = (void *)skb->data;
8a933964 3967 enum ath10k_hw_txrx_mode txmode;
6421969f
MK
3968 enum ath10k_mac_tx_path txpath;
3969 bool is_htt;
3970 bool is_mgmt;
3971 bool is_presp;
f2f6ecab 3972 int ret;
5e3dd157 3973
dd4717b6 3974 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
5e3dd157 3975
8a933964 3976 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
6421969f
MK
3977 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3978 is_htt = (txpath == ATH10K_MAC_TX_HTT ||
3979 txpath == ATH10K_MAC_TX_HTT_MGMT);
cac08552 3980 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
5e3dd157 3981
6421969f
MK
3982 if (is_htt) {
3983 spin_lock_bh(&ar->htt.tx_lock);
6421969f
MK
3984 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
3985
cac08552 3986 ret = ath10k_htt_tx_inc_pending(htt);
6421969f
MK
3987 if (ret) {
3988 ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
3989 ret);
3990 spin_unlock_bh(&ar->htt.tx_lock);
3991 ieee80211_free_txskb(ar->hw, skb);
3992 return;
3993 }
3994
cac08552
RM
3995 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
3996 if (ret) {
dd7c280f
RM
3997 ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
3998 ret);
cac08552
RM
3999 ath10k_htt_tx_dec_pending(htt);
4000 spin_unlock_bh(&ar->htt.tx_lock);
4001 ieee80211_free_txskb(ar->hw, skb);
4002 return;
4003 }
6421969f
MK
4004 spin_unlock_bh(&ar->htt.tx_lock);
4005 }
4006
4007 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
4008 if (ret) {
f2f6ecab 4009 ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
6421969f
MK
4010 if (is_htt) {
4011 spin_lock_bh(&ar->htt.tx_lock);
cac08552
RM
4012 ath10k_htt_tx_dec_pending(htt);
4013 if (is_mgmt)
4014 ath10k_htt_tx_mgmt_dec_pending(htt);
6421969f
MK
4015 spin_unlock_bh(&ar->htt.tx_lock);
4016 }
4017 return;
4018 }
5e3dd157
KV
4019}
4020
29946878
MK
4021static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4022 struct ieee80211_txq *txq)
4023{
4024 struct ath10k *ar = hw->priv;
4025 struct ath10k_txq *artxq = (void *)txq->drv_priv;
4026
750eeed8
MK
4027 spin_lock_bh(&ar->txqs_lock);
4028 if (list_empty(&artxq->list))
4029 list_add_tail(&artxq->list, &ar->txqs);
4030 spin_unlock_bh(&ar->txqs_lock);
29946878 4031
750eeed8 4032 if (ath10k_mac_tx_can_push(hw, txq))
29946878 4033 tasklet_schedule(&ar->htt.txrx_compl_task);
c1a43d97
MK
4034
4035 ath10k_htt_tx_txq_update(hw, txq);
29946878
MK
4036}
4037
bca7bafb 4038/* Must not be called with conf_mutex held as workers can use that also. */
7962b0d8 4039void ath10k_drain_tx(struct ath10k *ar)
bca7bafb
MK
4040{
4041 /* make sure rcu-protected mac80211 tx path itself is drained */
4042 synchronize_net();
4043
4044 ath10k_offchan_tx_purge(ar);
4045 ath10k_mgmt_over_wmi_tx_purge(ar);
4046
4047 cancel_work_sync(&ar->offchan_tx_work);
4048 cancel_work_sync(&ar->wmi_mgmt_tx_work);
4049}
4050
affd3217 4051void ath10k_halt(struct ath10k *ar)
818bdd16 4052{
d9bc4b9b
MK
4053 struct ath10k_vif *arvif;
4054
818bdd16
MK
4055 lockdep_assert_held(&ar->conf_mutex);
4056
1933747f
MK
4057 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4058 ar->filter_flags = 0;
4059 ar->monitor = false;
500ff9f9 4060 ar->monitor_arvif = NULL;
1933747f
MK
4061
4062 if (ar->monitor_started)
1bbc0975 4063 ath10k_monitor_stop(ar);
1933747f
MK
4064
4065 ar->monitor_started = false;
96d828d4 4066 ar->tx_paused = 0;
1bbc0975 4067
5c81c7fd 4068 ath10k_scan_finish(ar);
818bdd16
MK
4069 ath10k_peer_cleanup_all(ar);
4070 ath10k_core_stop(ar);
4071 ath10k_hif_power_down(ar);
4072
4073 spin_lock_bh(&ar->data_lock);
64badcb6
MK
4074 list_for_each_entry(arvif, &ar->arvifs, list)
4075 ath10k_mac_vif_beacon_cleanup(arvif);
818bdd16
MK
4076 spin_unlock_bh(&ar->data_lock);
4077}
4078
46acf7bb
BG
4079static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4080{
4081 struct ath10k *ar = hw->priv;
4082
4083 mutex_lock(&ar->conf_mutex);
4084
166de3f1
RM
4085 *tx_ant = ar->cfg_tx_chainmask;
4086 *rx_ant = ar->cfg_rx_chainmask;
46acf7bb
BG
4087
4088 mutex_unlock(&ar->conf_mutex);
4089
4090 return 0;
4091}
4092
5572a95b
BG
4093static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4094{
4095 /* It is not clear that allowing gaps in chainmask
4096 * is helpful. Probably it will not do what user
4097 * is hoping for, so warn in that case.
4098 */
4099 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4100 return;
4101
4102 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
4103 dbg, cm);
4104}
4105
f58512f3
RM
4106static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4107{
4108 int nsts = ar->vht_cap_info;
4109
4110 nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4111 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4112
4113 /* If firmware does not deliver to host number of space-time
4114 * streams supported, assume it support up to 4 BF STS and return
4115 * the value for VHT CAP: nsts-1)
4116 */
4117 if (nsts == 0)
4118 return 3;
4119
4120 return nsts;
4121}
4122
4123static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4124{
4125 int sound_dim = ar->vht_cap_info;
4126
4127 sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4128 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4129
4130 /* If the sounding dimension is not advertised by the firmware,
4131 * let's use a default value of 1
4132 */
4133 if (sound_dim == 0)
4134 return 1;
4135
4136 return sound_dim;
4137}
4138
4139static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4140{
4141 struct ieee80211_sta_vht_cap vht_cap = {0};
4142 u16 mcs_map;
4143 u32 val;
4144 int i;
4145
4146 vht_cap.vht_supported = 1;
4147 vht_cap.cap = ar->vht_cap_info;
4148
4149 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4150 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4151 val = ath10k_mac_get_vht_cap_bf_sts(ar);
4152 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4153 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4154
4155 vht_cap.cap |= val;
4156 }
4157
4158 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4159 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4160 val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4161 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4162 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4163
4164 vht_cap.cap |= val;
4165 }
4166
4167 mcs_map = 0;
4168 for (i = 0; i < 8; i++) {
4169 if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4170 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4171 else
4172 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4173 }
4174
4175 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4176 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4177
4178 return vht_cap;
4179}
4180
4181static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4182{
4183 int i;
4184 struct ieee80211_sta_ht_cap ht_cap = {0};
4185
4186 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4187 return ht_cap;
4188
4189 ht_cap.ht_supported = 1;
4190 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4191 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4192 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4193 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
e33a99e2
PO
4194 ht_cap.cap |=
4195 WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
f58512f3
RM
4196
4197 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4198 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4199
4200 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4201 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4202
4203 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4204 u32 smps;
4205
4206 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4207 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4208
4209 ht_cap.cap |= smps;
4210 }
4211
4212 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
4213 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4214
4215 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4216 u32 stbc;
4217
4218 stbc = ar->ht_cap_info;
4219 stbc &= WMI_HT_CAP_RX_STBC;
4220 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4221 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4222 stbc &= IEEE80211_HT_CAP_RX_STBC;
4223
4224 ht_cap.cap |= stbc;
4225 }
4226
4227 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4228 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4229
4230 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4231 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4232
4233 /* max AMSDU is implicitly taken from vht_cap_info */
4234 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4235 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4236
4237 for (i = 0; i < ar->num_rf_chains; i++) {
4238 if (ar->cfg_rx_chainmask & BIT(i))
4239 ht_cap.mcs.rx_mask[i] = 0xFF;
4240 }
4241
4242 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4243
4244 return ht_cap;
4245}
4246
5036fe0f
RM
4247static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4248{
4249 struct ieee80211_supported_band *band;
4250 struct ieee80211_sta_vht_cap vht_cap;
4251 struct ieee80211_sta_ht_cap ht_cap;
4252
4253 ht_cap = ath10k_get_ht_cap(ar);
4254 vht_cap = ath10k_create_vht_cap(ar);
4255
4256 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
57fbcce3 4257 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5036fe0f
RM
4258 band->ht_cap = ht_cap;
4259
4260 /* Enable the VHT support at 2.4 GHz */
4261 band->vht_cap = vht_cap;
4262 }
4263 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
57fbcce3 4264 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5036fe0f
RM
4265 band->ht_cap = ht_cap;
4266 band->vht_cap = vht_cap;
4267 }
4268}
4269
46acf7bb
BG
4270static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4271{
4272 int ret;
4273
4274 lockdep_assert_held(&ar->conf_mutex);
4275
5572a95b
BG
4276 ath10k_check_chain_mask(ar, tx_ant, "tx");
4277 ath10k_check_chain_mask(ar, rx_ant, "rx");
4278
46acf7bb
BG
4279 ar->cfg_tx_chainmask = tx_ant;
4280 ar->cfg_rx_chainmask = rx_ant;
4281
4282 if ((ar->state != ATH10K_STATE_ON) &&
4283 (ar->state != ATH10K_STATE_RESTARTED))
4284 return 0;
4285
4286 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
4287 tx_ant);
4288 if (ret) {
7aa7a72a 4289 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
4290 ret, tx_ant);
4291 return ret;
4292 }
4293
4294 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
4295 rx_ant);
4296 if (ret) {
7aa7a72a 4297 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
4298 ret, rx_ant);
4299 return ret;
4300 }
4301
5036fe0f
RM
4302 /* Reload HT/VHT capability */
4303 ath10k_mac_setup_ht_vht_cap(ar);
4304
46acf7bb
BG
4305 return 0;
4306}
4307
4308static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4309{
4310 struct ath10k *ar = hw->priv;
4311 int ret;
4312
4313 mutex_lock(&ar->conf_mutex);
4314 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
4315 mutex_unlock(&ar->conf_mutex);
4316 return ret;
4317}
4318
5e3dd157
KV
4319static int ath10k_start(struct ieee80211_hw *hw)
4320{
4321 struct ath10k *ar = hw->priv;
1fe374f5 4322 u32 param;
818bdd16 4323 int ret = 0;
5e3dd157 4324
bca7bafb
MK
4325 /*
4326 * This makes sense only when restarting hw. It is harmless to call
4327 * uncoditionally. This is necessary to make sure no HTT/WMI tx
4328 * commands will be submitted while restarting.
4329 */
4330 ath10k_drain_tx(ar);
4331
548db54c
MK
4332 mutex_lock(&ar->conf_mutex);
4333
c5058f5b
MK
4334 switch (ar->state) {
4335 case ATH10K_STATE_OFF:
4336 ar->state = ATH10K_STATE_ON;
4337 break;
4338 case ATH10K_STATE_RESTARTING:
4339 ath10k_halt(ar);
4340 ar->state = ATH10K_STATE_RESTARTED;
4341 break;
4342 case ATH10K_STATE_ON:
4343 case ATH10K_STATE_RESTARTED:
4344 case ATH10K_STATE_WEDGED:
4345 WARN_ON(1);
818bdd16 4346 ret = -EINVAL;
ae254433 4347 goto err;
43d2a30f
KV
4348 case ATH10K_STATE_UTF:
4349 ret = -EBUSY;
4350 goto err;
818bdd16
MK
4351 }
4352
4353 ret = ath10k_hif_power_up(ar);
4354 if (ret) {
7aa7a72a 4355 ath10k_err(ar, "Could not init hif: %d\n", ret);
ae254433 4356 goto err_off;
818bdd16
MK
4357 }
4358
43d2a30f 4359 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
818bdd16 4360 if (ret) {
7aa7a72a 4361 ath10k_err(ar, "Could not init core: %d\n", ret);
ae254433 4362 goto err_power_down;
818bdd16
MK
4363 }
4364
1fe374f5
MSS
4365 param = ar->wmi.pdev_param->pmf_qos;
4366 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
ae254433 4367 if (ret) {
7aa7a72a 4368 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
ae254433
MK
4369 goto err_core_stop;
4370 }
5e3dd157 4371
1fe374f5
MSS
4372 param = ar->wmi.pdev_param->dynamic_bw;
4373 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
ae254433 4374 if (ret) {
7aa7a72a 4375 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
ae254433
MK
4376 goto err_core_stop;
4377 }
5e3dd157 4378
cf32784c
MK
4379 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
4380 ret = ath10k_wmi_adaptive_qcs(ar, true);
4381 if (ret) {
4382 ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
4383 ret);
4384 goto err_core_stop;
4385 }
4386 }
4387
24ab13ef 4388 if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
1fe374f5
MSS
4389 param = ar->wmi.pdev_param->burst_enable;
4390 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
24ab13ef
JD
4391 if (ret) {
4392 ath10k_warn(ar, "failed to disable burst: %d\n", ret);
4393 goto err_core_stop;
4394 }
4395 }
4396
166de3f1 4397 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
46acf7bb 4398
ab6258ed
MP
4399 /*
4400 * By default FW set ARP frames ac to voice (6). In that case ARP
4401 * exchange is not working properly for UAPSD enabled AP. ARP requests
4402 * which arrives with access category 0 are processed by network stack
4403 * and send back with access category 0, but FW changes access category
4404 * to 6. Set ARP frames access category to best effort (0) solves
4405 * this problem.
4406 */
4407
1fe374f5
MSS
4408 param = ar->wmi.pdev_param->arp_ac_override;
4409 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
ab6258ed 4410 if (ret) {
7aa7a72a 4411 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
ab6258ed 4412 ret);
ae254433 4413 goto err_core_stop;
ab6258ed
MP
4414 }
4415
62f77f09
M
4416 if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
4417 ar->fw_features)) {
4418 ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
4419 WMI_CCA_DETECT_LEVEL_AUTO,
4420 WMI_CCA_DETECT_MARGIN_AUTO);
4421 if (ret) {
4422 ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
4423 ret);
4424 goto err_core_stop;
4425 }
4426 }
4427
1fe374f5
MSS
4428 param = ar->wmi.pdev_param->ani_enable;
4429 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
575f1c3d
ARN
4430 if (ret) {
4431 ath10k_warn(ar, "failed to enable ani by default: %d\n",
4432 ret);
4433 goto err_core_stop;
4434 }
4435
b3e71d7a
ARN
4436 ar->ani_enabled = true;
4437
cc61a1bb 4438 if (ath10k_peer_stats_enabled(ar)) {
8351c052
MSS
4439 param = ar->wmi.pdev_param->peer_stats_update_period;
4440 ret = ath10k_wmi_pdev_set_param(ar, param,
4441 PEER_DEFAULT_STATS_UPDATE_PERIOD);
4442 if (ret) {
4443 ath10k_warn(ar,
4444 "failed to set peer stats period : %d\n",
4445 ret);
4446 goto err_core_stop;
4447 }
4448 }
4449
d650097b 4450 ar->num_started_vdevs = 0;
f7843d7f
MK
4451 ath10k_regd_update(ar);
4452
855aed12 4453 ath10k_spectral_start(ar);
8515b5c7 4454 ath10k_thermal_set_throttling(ar);
855aed12 4455
ae254433
MK
4456 mutex_unlock(&ar->conf_mutex);
4457 return 0;
4458
4459err_core_stop:
4460 ath10k_core_stop(ar);
4461
4462err_power_down:
4463 ath10k_hif_power_down(ar);
4464
4465err_off:
4466 ar->state = ATH10K_STATE_OFF;
4467
4468err:
548db54c 4469 mutex_unlock(&ar->conf_mutex);
c60bdd83 4470 return ret;
5e3dd157
KV
4471}
4472
4473static void ath10k_stop(struct ieee80211_hw *hw)
4474{
4475 struct ath10k *ar = hw->priv;
4476
bca7bafb
MK
4477 ath10k_drain_tx(ar);
4478
548db54c 4479 mutex_lock(&ar->conf_mutex);
c5058f5b 4480 if (ar->state != ATH10K_STATE_OFF) {
818bdd16 4481 ath10k_halt(ar);
c5058f5b
MK
4482 ar->state = ATH10K_STATE_OFF;
4483 }
548db54c
MK
4484 mutex_unlock(&ar->conf_mutex);
4485
5c81c7fd 4486 cancel_delayed_work_sync(&ar->scan.timeout);
affd3217 4487 cancel_work_sync(&ar->restart_work);
5e3dd157
KV
4488}
4489
ad088bfa 4490static int ath10k_config_ps(struct ath10k *ar)
5e3dd157 4491{
ad088bfa
MK
4492 struct ath10k_vif *arvif;
4493 int ret = 0;
affd3217
MK
4494
4495 lockdep_assert_held(&ar->conf_mutex);
4496
ad088bfa
MK
4497 list_for_each_entry(arvif, &ar->arvifs, list) {
4498 ret = ath10k_mac_vif_setup_ps(arvif);
4499 if (ret) {
7aa7a72a 4500 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
ad088bfa
MK
4501 break;
4502 }
4503 }
affd3217 4504
ad088bfa 4505 return ret;
affd3217
MK
4506}
4507
7d9d5587
MK
4508static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
4509{
4510 int ret;
4511 u32 param;
4512
4513 lockdep_assert_held(&ar->conf_mutex);
4514
4515 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
4516
4517 param = ar->wmi.pdev_param->txpower_limit2g;
4518 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4519 if (ret) {
4520 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
4521 txpower, ret);
4522 return ret;
4523 }
4524
4525 param = ar->wmi.pdev_param->txpower_limit5g;
4526 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4527 if (ret) {
4528 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
4529 txpower, ret);
4530 return ret;
4531 }
4532
4533 return 0;
4534}
4535
4536static int ath10k_mac_txpower_recalc(struct ath10k *ar)
4537{
4538 struct ath10k_vif *arvif;
4539 int ret, txpower = -1;
4540
4541 lockdep_assert_held(&ar->conf_mutex);
4542
4543 list_for_each_entry(arvif, &ar->arvifs, list) {
4544 WARN_ON(arvif->txpower < 0);
4545
4546 if (txpower == -1)
4547 txpower = arvif->txpower;
4548 else
4549 txpower = min(txpower, arvif->txpower);
4550 }
4551
4552 if (WARN_ON(txpower == -1))
4553 return -EINVAL;
4554
4555 ret = ath10k_mac_txpower_setup(ar, txpower);
4556 if (ret) {
4557 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
4558 txpower, ret);
4559 return ret;
4560 }
4561
4562 return 0;
4563}
4564
affd3217
MK
4565static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
4566{
5e3dd157
KV
4567 struct ath10k *ar = hw->priv;
4568 struct ieee80211_conf *conf = &hw->conf;
4569 int ret = 0;
5e3dd157
KV
4570
4571 mutex_lock(&ar->conf_mutex);
4572
affd3217
MK
4573 if (changed & IEEE80211_CONF_CHANGE_PS)
4574 ath10k_config_ps(ar);
5e3dd157
KV
4575
4576 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1933747f
MK
4577 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
4578 ret = ath10k_monitor_recalc(ar);
4579 if (ret)
4580 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5e3dd157
KV
4581 }
4582
4583 mutex_unlock(&ar->conf_mutex);
4584 return ret;
4585}
4586
5572a95b
BG
4587static u32 get_nss_from_chainmask(u16 chain_mask)
4588{
f680f70a 4589 if ((chain_mask & 0xf) == 0xf)
5572a95b
BG
4590 return 4;
4591 else if ((chain_mask & 0x7) == 0x7)
4592 return 3;
4593 else if ((chain_mask & 0x3) == 0x3)
4594 return 2;
4595 return 1;
4596}
4597
a48e2cc8
VN
4598static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
4599{
4600 u32 value = 0;
4601 struct ath10k *ar = arvif->ar;
707a0c81 4602 int nsts;
0c6d6f26 4603 int sound_dim;
a48e2cc8
VN
4604
4605 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
4606 return 0;
4607
707a0c81 4608 nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
a48e2cc8
VN
4609 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4610 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
707a0c81 4611 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
a48e2cc8 4612
0c6d6f26 4613 sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
a48e2cc8
VN
4614 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4615 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
0c6d6f26 4616 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
a48e2cc8
VN
4617
4618 if (!value)
4619 return 0;
4620
4621 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
4622 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4623
4624 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
4625 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
4626 WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
4627
4628 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
4629 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4630
4631 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
4632 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
4633 WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
4634
4635 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4636 ar->wmi.vdev_param->txbf, value);
4637}
4638
5e3dd157
KV
4639/*
4640 * TODO:
4641 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
4642 * because we will send mgmt frames without CCK. This requirement
4643 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
4644 * in the TX packet.
4645 */
4646static int ath10k_add_interface(struct ieee80211_hw *hw,
4647 struct ieee80211_vif *vif)
4648{
4649 struct ath10k *ar = hw->priv;
4650 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
bb8f0c6a 4651 struct ath10k_peer *peer;
5e3dd157
KV
4652 enum wmi_sta_powersave_param param;
4653 int ret = 0;
5a13e76e 4654 u32 value;
5e3dd157 4655 int bit;
96d828d4 4656 int i;
6d1506e7 4657 u32 vdev_param;
5e3dd157 4658
848955cc
JB
4659 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4660
5e3dd157
KV
4661 mutex_lock(&ar->conf_mutex);
4662
0dbd09e6 4663 memset(arvif, 0, sizeof(*arvif));
29946878 4664 ath10k_mac_txq_init(vif->txq);
0dbd09e6 4665
5e3dd157
KV
4666 arvif->ar = ar;
4667 arvif->vif = vif;
4668
e63b33f3 4669 INIT_LIST_HEAD(&arvif->list);
81a9a17d 4670 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
cc9904e6
MK
4671 INIT_DELAYED_WORK(&arvif->connection_loss_work,
4672 ath10k_mac_vif_sta_connection_loss_work);
cc4827b9 4673
45c9abc0
MK
4674 for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4675 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4676 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4677 sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4678 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4679 sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4680 }
cc4827b9 4681
e04cafbc
MK
4682 if (ar->num_peers >= ar->max_num_peers) {
4683 ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
503422d9
MK
4684 ret = -ENOBUFS;
4685 goto err;
e04cafbc
MK
4686 }
4687
a9aefb3b 4688 if (ar->free_vdev_map == 0) {
7aa7a72a 4689 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5e3dd157 4690 ret = -EBUSY;
9dad14ae 4691 goto err;
5e3dd157 4692 }
16c11176 4693 bit = __ffs64(ar->free_vdev_map);
5e3dd157 4694
16c11176
BG
4695 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
4696 bit, ar->free_vdev_map);
5e3dd157 4697
16c11176 4698 arvif->vdev_id = bit;
6e4de1a4
PO
4699 arvif->vdev_subtype =
4700 ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
5e3dd157 4701
5e3dd157 4702 switch (vif->type) {
75d2bd48
MK
4703 case NL80211_IFTYPE_P2P_DEVICE:
4704 arvif->vdev_type = WMI_VDEV_TYPE_STA;
6e4de1a4
PO
4705 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4706 (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
75d2bd48 4707 break;
5e3dd157
KV
4708 case NL80211_IFTYPE_UNSPECIFIED:
4709 case NL80211_IFTYPE_STATION:
4710 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4711 if (vif->p2p)
6e4de1a4
PO
4712 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4713 (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
5e3dd157
KV
4714 break;
4715 case NL80211_IFTYPE_ADHOC:
4716 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
4717 break;
b6c7bafa 4718 case NL80211_IFTYPE_MESH_POINT:
0b3d76e9 4719 if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
6e4de1a4
PO
4720 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4721 (ar, WMI_VDEV_SUBTYPE_MESH_11S);
bb58b89c 4722 } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
b6c7bafa
BC
4723 ret = -EINVAL;
4724 ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
4725 goto err;
4726 }
4727 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4728 break;
5e3dd157
KV
4729 case NL80211_IFTYPE_AP:
4730 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4731
4732 if (vif->p2p)
6e4de1a4
PO
4733 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4734 (ar, WMI_VDEV_SUBTYPE_P2P_GO);
5e3dd157
KV
4735 break;
4736 case NL80211_IFTYPE_MONITOR:
4737 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4738 break;
4739 default:
4740 WARN_ON(1);
4741 break;
4742 }
4743
96d828d4
MK
4744 /* Using vdev_id as queue number will make it very easy to do per-vif
4745 * tx queue locking. This shouldn't wrap due to interface combinations
4746 * but do a modulo for correctness sake and prevent using offchannel tx
4747 * queues for regular vif tx.
4748 */
4749 vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4750 for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4751 vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4752
64badcb6
MK
4753 /* Some firmware revisions don't wait for beacon tx completion before
4754 * sending another SWBA event. This could lead to hardware using old
4755 * (freed) beacon data in some cases, e.g. tx credit starvation
4756 * combined with missed TBTT. This is very very rare.
4757 *
4758 * On non-IOMMU-enabled hosts this could be a possible security issue
4759 * because hw could beacon some random data on the air. On
4760 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
4761 * device would crash.
4762 *
4763 * Since there are no beacon tx completions (implicit nor explicit)
4764 * propagated to host the only workaround for this is to allocate a
4765 * DMA-coherent buffer for a lifetime of a vif and use it for all
4766 * beacon tx commands. Worst case for this approach is some beacons may
4767 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
4768 */
4769 if (vif->type == NL80211_IFTYPE_ADHOC ||
b6c7bafa 4770 vif->type == NL80211_IFTYPE_MESH_POINT ||
64badcb6
MK
4771 vif->type == NL80211_IFTYPE_AP) {
4772 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
4773 IEEE80211_MAX_FRAME_LEN,
4774 &arvif->beacon_paddr,
82d7aba7 4775 GFP_ATOMIC);
64badcb6
MK
4776 if (!arvif->beacon_buf) {
4777 ret = -ENOMEM;
4778 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
4779 ret);
4780 goto err;
4781 }
4782 }
ccec9038
DL
4783 if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
4784 arvif->nohwcrypt = true;
4785
4786 if (arvif->nohwcrypt &&
4787 !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4788 ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
4789 goto err;
4790 }
64badcb6
MK
4791
4792 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
4793 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4794 arvif->beacon_buf ? "single-buf" : "per-skb");
5e3dd157
KV
4795
4796 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
4797 arvif->vdev_subtype, vif->addr);
4798 if (ret) {
7aa7a72a 4799 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
69244e56 4800 arvif->vdev_id, ret);
9dad14ae 4801 goto err;
5e3dd157
KV
4802 }
4803
16c11176 4804 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
0579119f 4805 list_add(&arvif->list, &ar->arvifs);
9dad14ae 4806
46725b15
MK
4807 /* It makes no sense to have firmware do keepalives. mac80211 already
4808 * takes care of this with idle connection polling.
4809 */
4810 ret = ath10k_mac_vif_disable_keepalive(arvif);
9dad14ae 4811 if (ret) {
46725b15 4812 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
69244e56 4813 arvif->vdev_id, ret);
9dad14ae
MK
4814 goto err_vdev_delete;
4815 }
5e3dd157 4816
627613f8 4817 arvif->def_wep_key_idx = -1;
5e3dd157 4818
6d1506e7
BM
4819 vdev_param = ar->wmi.vdev_param->tx_encap_type;
4820 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 4821 ATH10K_HW_TXRX_NATIVE_WIFI);
ebc9abdd 4822 /* 10.X firmware does not support this VDEV parameter. Do not warn */
9dad14ae 4823 if (ret && ret != -EOPNOTSUPP) {
7aa7a72a 4824 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
69244e56 4825 arvif->vdev_id, ret);
9dad14ae
MK
4826 goto err_vdev_delete;
4827 }
5e3dd157 4828
8a75fc54
RM
4829 /* Configuring number of spatial stream for monitor interface is causing
4830 * target assert in qca9888 and qca6174.
4831 */
4832 if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5572a95b
BG
4833 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4834
4835 vdev_param = ar->wmi.vdev_param->nss;
4836 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4837 nss);
4838 if (ret) {
4839 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
4840 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
4841 ret);
4842 goto err_vdev_delete;
4843 }
4844 }
4845
e57e0571
MK
4846 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4847 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
6942726f
MK
4848 ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
4849 vif->addr, WMI_PEER_TYPE_DEFAULT);
5e3dd157 4850 if (ret) {
e57e0571 4851 ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
69244e56 4852 arvif->vdev_id, ret);
9dad14ae 4853 goto err_vdev_delete;
5e3dd157 4854 }
bb8f0c6a
MK
4855
4856 spin_lock_bh(&ar->data_lock);
4857
4858 peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
4859 if (!peer) {
4860 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
4861 vif->addr, arvif->vdev_id);
4862 spin_unlock_bh(&ar->data_lock);
4863 ret = -ENOENT;
4864 goto err_peer_delete;
4865 }
4866
4867 arvif->peer_id = find_first_bit(peer->peer_ids,
4868 ATH10K_MAX_NUM_PEER_IDS);
4869
4870 spin_unlock_bh(&ar->data_lock);
4871 } else {
4872 arvif->peer_id = HTT_INVALID_PEERID;
e57e0571 4873 }
cdf07409 4874
e57e0571 4875 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5a13e76e
KV
4876 ret = ath10k_mac_set_kickout(arvif);
4877 if (ret) {
7aa7a72a 4878 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
69244e56 4879 arvif->vdev_id, ret);
5a13e76e
KV
4880 goto err_peer_delete;
4881 }
5e3dd157
KV
4882 }
4883
4884 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
4885 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
4886 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4887 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4888 param, value);
9dad14ae 4889 if (ret) {
7aa7a72a 4890 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
69244e56 4891 arvif->vdev_id, ret);
9dad14ae
MK
4892 goto err_peer_delete;
4893 }
5e3dd157 4894
9f9b5746 4895 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
9dad14ae 4896 if (ret) {
9f9b5746 4897 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
69244e56 4898 arvif->vdev_id, ret);
9dad14ae
MK
4899 goto err_peer_delete;
4900 }
5e3dd157 4901
9f9b5746 4902 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
9dad14ae 4903 if (ret) {
9f9b5746 4904 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
69244e56 4905 arvif->vdev_id, ret);
9dad14ae
MK
4906 goto err_peer_delete;
4907 }
5e3dd157
KV
4908 }
4909
a48e2cc8
VN
4910 ret = ath10k_mac_set_txbf_conf(arvif);
4911 if (ret) {
4912 ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
4913 arvif->vdev_id, ret);
4914 goto err_peer_delete;
4915 }
4916
424121c3 4917 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
9dad14ae 4918 if (ret) {
7aa7a72a 4919 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
679c54a6 4920 arvif->vdev_id, ret);
9dad14ae
MK
4921 goto err_peer_delete;
4922 }
679c54a6 4923
7d9d5587
MK
4924 arvif->txpower = vif->bss_conf.txpower;
4925 ret = ath10k_mac_txpower_recalc(ar);
4926 if (ret) {
4927 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4928 goto err_peer_delete;
4929 }
4930
500ff9f9
MK
4931 if (vif->type == NL80211_IFTYPE_MONITOR) {
4932 ar->monitor_arvif = arvif;
4933 ret = ath10k_monitor_recalc(ar);
4934 if (ret) {
4935 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4936 goto err_peer_delete;
4937 }
4938 }
4939
6d2d51ec
MK
4940 spin_lock_bh(&ar->htt.tx_lock);
4941 if (!ar->tx_paused)
4942 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
4943 spin_unlock_bh(&ar->htt.tx_lock);
4944
5e3dd157 4945 mutex_unlock(&ar->conf_mutex);
9dad14ae
MK
4946 return 0;
4947
4948err_peer_delete:
e57e0571
MK
4949 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4950 arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
9dad14ae
MK
4951 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
4952
4953err_vdev_delete:
4954 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
16c11176 4955 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 4956 list_del(&arvif->list);
9dad14ae
MK
4957
4958err:
64badcb6
MK
4959 if (arvif->beacon_buf) {
4960 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
4961 arvif->beacon_buf, arvif->beacon_paddr);
4962 arvif->beacon_buf = NULL;
4963 }
4964
9dad14ae
MK
4965 mutex_unlock(&ar->conf_mutex);
4966
5e3dd157
KV
4967 return ret;
4968}
4969
b4aa539d
MK
4970static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
4971{
4972 int i;
4973
4974 for (i = 0; i < BITS_PER_LONG; i++)
4975 ath10k_mac_vif_tx_unlock(arvif, i);
4976}
4977
5e3dd157
KV
4978static void ath10k_remove_interface(struct ieee80211_hw *hw,
4979 struct ieee80211_vif *vif)
4980{
4981 struct ath10k *ar = hw->priv;
4982 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6942726f 4983 struct ath10k_peer *peer;
5e3dd157 4984 int ret;
6942726f 4985 int i;
5e3dd157 4986
81a9a17d 4987 cancel_work_sync(&arvif->ap_csa_work);
cc9904e6 4988 cancel_delayed_work_sync(&arvif->connection_loss_work);
81a9a17d 4989
5d011f5c
SM
4990 mutex_lock(&ar->conf_mutex);
4991
ed54388a 4992 spin_lock_bh(&ar->data_lock);
64badcb6 4993 ath10k_mac_vif_beacon_cleanup(arvif);
ed54388a
MK
4994 spin_unlock_bh(&ar->data_lock);
4995
855aed12
SW
4996 ret = ath10k_spectral_vif_stop(arvif);
4997 if (ret)
7aa7a72a 4998 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
855aed12
SW
4999 arvif->vdev_id, ret);
5000
16c11176 5001 ar->free_vdev_map |= 1LL << arvif->vdev_id;
0579119f 5002 list_del(&arvif->list);
5e3dd157 5003
e57e0571
MK
5004 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5005 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
2c512059
MK
5006 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5007 vif->addr);
5e3dd157 5008 if (ret)
e57e0571 5009 ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
69244e56 5010 arvif->vdev_id, ret);
5e3dd157
KV
5011
5012 kfree(arvif->u.ap.noa_data);
5013 }
5014
7aa7a72a 5015 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
60c3daa8
KV
5016 arvif->vdev_id);
5017
5e3dd157
KV
5018 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5019 if (ret)
7aa7a72a 5020 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
69244e56 5021 arvif->vdev_id, ret);
5e3dd157 5022
2c512059
MK
5023 /* Some firmware revisions don't notify host about self-peer removal
5024 * until after associated vdev is deleted.
5025 */
e57e0571
MK
5026 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5027 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
2c512059
MK
5028 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5029 vif->addr);
5030 if (ret)
5031 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5032 arvif->vdev_id, ret);
5033
5034 spin_lock_bh(&ar->data_lock);
5035 ar->num_peers--;
5036 spin_unlock_bh(&ar->data_lock);
5037 }
5038
6942726f
MK
5039 spin_lock_bh(&ar->data_lock);
5040 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5041 peer = ar->peer_map[i];
5042 if (!peer)
5043 continue;
5044
5045 if (peer->vif == vif) {
5046 ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5047 vif->addr, arvif->vdev_id);
5048 peer->vif = NULL;
5049 }
5050 }
5051 spin_unlock_bh(&ar->data_lock);
5052
5e3dd157 5053 ath10k_peer_cleanup(ar, arvif->vdev_id);
dd4717b6 5054 ath10k_mac_txq_unref(ar, vif->txq);
5e3dd157 5055
500ff9f9
MK
5056 if (vif->type == NL80211_IFTYPE_MONITOR) {
5057 ar->monitor_arvif = NULL;
5058 ret = ath10k_monitor_recalc(ar);
5059 if (ret)
5060 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5061 }
5062
b4aa539d
MK
5063 spin_lock_bh(&ar->htt.tx_lock);
5064 ath10k_mac_vif_tx_unlock_all(arvif);
5065 spin_unlock_bh(&ar->htt.tx_lock);
5066
29946878
MK
5067 ath10k_mac_txq_unref(ar, vif->txq);
5068
5e3dd157
KV
5069 mutex_unlock(&ar->conf_mutex);
5070}
5071
5072/*
5073 * FIXME: Has to be verified.
5074 */
5075#define SUPPORTED_FILTERS \
df140465 5076 (FIF_ALLMULTI | \
5e3dd157
KV
5077 FIF_CONTROL | \
5078 FIF_PSPOLL | \
5079 FIF_OTHER_BSS | \
5080 FIF_BCN_PRBRESP_PROMISC | \
5081 FIF_PROBE_REQ | \
5082 FIF_FCSFAIL)
5083
5084static void ath10k_configure_filter(struct ieee80211_hw *hw,
5085 unsigned int changed_flags,
5086 unsigned int *total_flags,
5087 u64 multicast)
5088{
5089 struct ath10k *ar = hw->priv;
5090 int ret;
5091
5092 mutex_lock(&ar->conf_mutex);
5093
5094 changed_flags &= SUPPORTED_FILTERS;
5095 *total_flags &= SUPPORTED_FILTERS;
5096 ar->filter_flags = *total_flags;
5097
1933747f
MK
5098 ret = ath10k_monitor_recalc(ar);
5099 if (ret)
5100 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
5e3dd157
KV
5101
5102 mutex_unlock(&ar->conf_mutex);
5103}
5104
5105static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
5106 struct ieee80211_vif *vif,
5107 struct ieee80211_bss_conf *info,
5108 u32 changed)
5109{
5110 struct ath10k *ar = hw->priv;
5111 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5112 int ret = 0;
af762c0b 5113 u32 vdev_param, pdev_param, slottime, preamble;
5e3dd157
KV
5114
5115 mutex_lock(&ar->conf_mutex);
5116
5117 if (changed & BSS_CHANGED_IBSS)
5118 ath10k_control_ibss(arvif, info, vif->addr);
5119
5120 if (changed & BSS_CHANGED_BEACON_INT) {
5121 arvif->beacon_interval = info->beacon_int;
6d1506e7
BM
5122 vdev_param = ar->wmi.vdev_param->beacon_interval;
5123 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 5124 arvif->beacon_interval);
7aa7a72a 5125 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
5126 "mac vdev %d beacon_interval %d\n",
5127 arvif->vdev_id, arvif->beacon_interval);
5128
5e3dd157 5129 if (ret)
7aa7a72a 5130 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
69244e56 5131 arvif->vdev_id, ret);
5e3dd157
KV
5132 }
5133
5134 if (changed & BSS_CHANGED_BEACON) {
7aa7a72a 5135 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
5136 "vdev %d set beacon tx mode to staggered\n",
5137 arvif->vdev_id);
5138
226a339b
BM
5139 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
5140 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5e3dd157
KV
5141 WMI_BEACON_STAGGERED_MODE);
5142 if (ret)
7aa7a72a 5143 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
69244e56 5144 arvif->vdev_id, ret);
fbb8f1b7
MK
5145
5146 ret = ath10k_mac_setup_bcn_tmpl(arvif);
5147 if (ret)
5148 ath10k_warn(ar, "failed to update beacon template: %d\n",
5149 ret);
b6c7bafa
BC
5150
5151 if (ieee80211_vif_is_mesh(vif)) {
5152 /* mesh doesn't use SSID but firmware needs it */
5153 strncpy(arvif->u.ap.ssid, "mesh",
5154 sizeof(arvif->u.ap.ssid));
5155 arvif->u.ap.ssid_len = 4;
5156 }
fbb8f1b7
MK
5157 }
5158
5159 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
5160 ret = ath10k_mac_setup_prb_tmpl(arvif);
5161 if (ret)
5162 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
5163 arvif->vdev_id, ret);
5e3dd157
KV
5164 }
5165
ba2479fe 5166 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5e3dd157
KV
5167 arvif->dtim_period = info->dtim_period;
5168
7aa7a72a 5169 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
5170 "mac vdev %d dtim_period %d\n",
5171 arvif->vdev_id, arvif->dtim_period);
5172
6d1506e7
BM
5173 vdev_param = ar->wmi.vdev_param->dtim_period;
5174 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
5175 arvif->dtim_period);
5176 if (ret)
7aa7a72a 5177 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
69244e56 5178 arvif->vdev_id, ret);
5e3dd157
KV
5179 }
5180
5181 if (changed & BSS_CHANGED_SSID &&
5182 vif->type == NL80211_IFTYPE_AP) {
5183 arvif->u.ap.ssid_len = info->ssid_len;
5184 if (info->ssid_len)
5185 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
5186 arvif->u.ap.hidden_ssid = info->hidden_ssid;
5187 }
5188
077efc8c
MK
5189 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
5190 ether_addr_copy(arvif->bssid, info->bssid);
5e3dd157
KV
5191
5192 if (changed & BSS_CHANGED_BEACON_ENABLED)
5193 ath10k_control_beaconing(arvif, info);
5194
5195 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
e81bd104 5196 arvif->use_cts_prot = info->use_cts_prot;
7aa7a72a 5197 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
e81bd104 5198 arvif->vdev_id, info->use_cts_prot);
60c3daa8 5199
e81bd104 5200 ret = ath10k_recalc_rtscts_prot(arvif);
5e3dd157 5201 if (ret)
7aa7a72a 5202 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
69244e56 5203 arvif->vdev_id, ret);
a87fd4b9
MK
5204
5205 vdev_param = ar->wmi.vdev_param->protection_mode;
5206 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5207 info->use_cts_prot ? 1 : 0);
5208 if (ret)
5209 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
617b0f4d 5210 info->use_cts_prot, arvif->vdev_id, ret);
5e3dd157
KV
5211 }
5212
5213 if (changed & BSS_CHANGED_ERP_SLOT) {
5e3dd157
KV
5214 if (info->use_short_slot)
5215 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
5216
5217 else
5218 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
5219
7aa7a72a 5220 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
60c3daa8
KV
5221 arvif->vdev_id, slottime);
5222
6d1506e7
BM
5223 vdev_param = ar->wmi.vdev_param->slot_time;
5224 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
5225 slottime);
5226 if (ret)
7aa7a72a 5227 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
69244e56 5228 arvif->vdev_id, ret);
5e3dd157
KV
5229 }
5230
5231 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5e3dd157
KV
5232 if (info->use_short_preamble)
5233 preamble = WMI_VDEV_PREAMBLE_SHORT;
5234 else
5235 preamble = WMI_VDEV_PREAMBLE_LONG;
5236
7aa7a72a 5237 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
5238 "mac vdev %d preamble %dn",
5239 arvif->vdev_id, preamble);
5240
6d1506e7
BM
5241 vdev_param = ar->wmi.vdev_param->preamble;
5242 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
5243 preamble);
5244 if (ret)
7aa7a72a 5245 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
69244e56 5246 arvif->vdev_id, ret);
5e3dd157
KV
5247 }
5248
5249 if (changed & BSS_CHANGED_ASSOC) {
e556f111
MK
5250 if (info->assoc) {
5251 /* Workaround: Make sure monitor vdev is not running
5252 * when associating to prevent some firmware revisions
5253 * (e.g. 10.1 and 10.2) from crashing.
5254 */
5255 if (ar->monitor_started)
5256 ath10k_monitor_stop(ar);
5e3dd157 5257 ath10k_bss_assoc(hw, vif, info);
e556f111 5258 ath10k_monitor_recalc(ar);
077efc8c
MK
5259 } else {
5260 ath10k_bss_disassoc(hw, vif);
e556f111 5261 }
5e3dd157
KV
5262 }
5263
7d9d5587
MK
5264 if (changed & BSS_CHANGED_TXPOWER) {
5265 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
5266 arvif->vdev_id, info->txpower);
5267
5268 arvif->txpower = info->txpower;
5269 ret = ath10k_mac_txpower_recalc(ar);
5270 if (ret)
5271 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5272 }
5273
bf14e65c 5274 if (changed & BSS_CHANGED_PS) {
cffb41f3
MK
5275 arvif->ps = vif->bss_conf.ps;
5276
5277 ret = ath10k_config_ps(ar);
bf14e65c
MK
5278 if (ret)
5279 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
5280 arvif->vdev_id, ret);
5281 }
5282
5e3dd157
KV
5283 mutex_unlock(&ar->conf_mutex);
5284}
5285
5286static int ath10k_hw_scan(struct ieee80211_hw *hw,
5287 struct ieee80211_vif *vif,
c56ef672 5288 struct ieee80211_scan_request *hw_req)
5e3dd157
KV
5289{
5290 struct ath10k *ar = hw->priv;
5291 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
c56ef672 5292 struct cfg80211_scan_request *req = &hw_req->req;
5e3dd157
KV
5293 struct wmi_start_scan_arg arg;
5294 int ret = 0;
5295 int i;
5296
5297 mutex_lock(&ar->conf_mutex);
5298
5299 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
5300 switch (ar->scan.state) {
5301 case ATH10K_SCAN_IDLE:
5302 reinit_completion(&ar->scan.started);
5303 reinit_completion(&ar->scan.completed);
5304 ar->scan.state = ATH10K_SCAN_STARTING;
5305 ar->scan.is_roc = false;
5306 ar->scan.vdev_id = arvif->vdev_id;
5307 ret = 0;
5308 break;
5309 case ATH10K_SCAN_STARTING:
5310 case ATH10K_SCAN_RUNNING:
5311 case ATH10K_SCAN_ABORTING:
5e3dd157 5312 ret = -EBUSY;
5c81c7fd 5313 break;
5e3dd157 5314 }
5e3dd157
KV
5315 spin_unlock_bh(&ar->data_lock);
5316
5c81c7fd
MK
5317 if (ret)
5318 goto exit;
5319
5e3dd157
KV
5320 memset(&arg, 0, sizeof(arg));
5321 ath10k_wmi_start_scan_init(ar, &arg);
5322 arg.vdev_id = arvif->vdev_id;
5323 arg.scan_id = ATH10K_SCAN_ID;
5324
5e3dd157
KV
5325 if (req->ie_len) {
5326 arg.ie_len = req->ie_len;
5327 memcpy(arg.ie, req->ie, arg.ie_len);
5328 }
5329
5330 if (req->n_ssids) {
5331 arg.n_ssids = req->n_ssids;
5332 for (i = 0; i < arg.n_ssids; i++) {
5333 arg.ssids[i].len = req->ssids[i].ssid_len;
5334 arg.ssids[i].ssid = req->ssids[i].ssid;
5335 }
dcd4a561
MK
5336 } else {
5337 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5e3dd157
KV
5338 }
5339
5340 if (req->n_channels) {
5341 arg.n_channels = req->n_channels;
5342 for (i = 0; i < arg.n_channels; i++)
5343 arg.channels[i] = req->channels[i]->center_freq;
5344 }
5345
5346 ret = ath10k_start_scan(ar, &arg);
5347 if (ret) {
7aa7a72a 5348 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5e3dd157 5349 spin_lock_bh(&ar->data_lock);
5c81c7fd 5350 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
5351 spin_unlock_bh(&ar->data_lock);
5352 }
5353
634349ba
MK
5354 /* Add a 200ms margin to account for event/command processing */
5355 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
5356 msecs_to_jiffies(arg.max_scan_time +
5357 200));
5358
5e3dd157
KV
5359exit:
5360 mutex_unlock(&ar->conf_mutex);
5361 return ret;
5362}
5363
5364static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
5365 struct ieee80211_vif *vif)
5366{
5367 struct ath10k *ar = hw->priv;
5e3dd157
KV
5368
5369 mutex_lock(&ar->conf_mutex);
5c81c7fd 5370 ath10k_scan_abort(ar);
5e3dd157 5371 mutex_unlock(&ar->conf_mutex);
4eb2e164
MK
5372
5373 cancel_delayed_work_sync(&ar->scan.timeout);
5e3dd157
KV
5374}
5375
cfb27d29
MK
5376static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
5377 struct ath10k_vif *arvif,
5378 enum set_key_cmd cmd,
5379 struct ieee80211_key_conf *key)
5380{
5381 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
5382 int ret;
5383
5384 /* 10.1 firmware branch requires default key index to be set to group
5385 * key index after installing it. Otherwise FW/HW Txes corrupted
5386 * frames with multi-vif APs. This is not required for main firmware
5387 * branch (e.g. 636).
5388 *
8461baf7
MK
5389 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
5390 *
5391 * FIXME: It remains unknown if this is required for multi-vif STA
5392 * interfaces on 10.1.
5393 */
cfb27d29 5394
8461baf7
MK
5395 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5396 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
cfb27d29
MK
5397 return;
5398
5399 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
5400 return;
5401
5402 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
5403 return;
5404
5405 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5406 return;
5407
5408 if (cmd != SET_KEY)
5409 return;
5410
5411 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5412 key->keyidx);
5413 if (ret)
7aa7a72a 5414 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
69244e56 5415 arvif->vdev_id, ret);
cfb27d29
MK
5416}
5417
5e3dd157
KV
5418static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5419 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5420 struct ieee80211_key_conf *key)
5421{
5422 struct ath10k *ar = hw->priv;
5423 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5424 struct ath10k_peer *peer;
5425 const u8 *peer_addr;
5426 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
5427 key->cipher == WLAN_CIPHER_SUITE_WEP104;
5428 int ret = 0;
29a10006 5429 int ret2;
370e5673 5430 u32 flags = 0;
29a10006 5431 u32 flags2;
5e3dd157 5432
d7131c04
BM
5433 /* this one needs to be done in software */
5434 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
5435 return 1;
5e3dd157 5436
ccec9038
DL
5437 if (arvif->nohwcrypt)
5438 return 1;
5439
5e3dd157
KV
5440 if (key->keyidx > WMI_MAX_KEY_INDEX)
5441 return -ENOSPC;
5442
5443 mutex_lock(&ar->conf_mutex);
5444
5445 if (sta)
5446 peer_addr = sta->addr;
5447 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
5448 peer_addr = vif->bss_conf.bssid;
5449 else
5450 peer_addr = vif->addr;
5451
5452 key->hw_key_idx = key->keyidx;
5453
7c8cc7eb
MK
5454 if (is_wep) {
5455 if (cmd == SET_KEY)
5456 arvif->wep_keys[key->keyidx] = key;
5457 else
5458 arvif->wep_keys[key->keyidx] = NULL;
5459 }
5460
5e3dd157
KV
5461 /* the peer should not disappear in mid-way (unless FW goes awry) since
5462 * we already hold conf_mutex. we just make sure its there now. */
5463 spin_lock_bh(&ar->data_lock);
5464 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5465 spin_unlock_bh(&ar->data_lock);
5466
5467 if (!peer) {
5468 if (cmd == SET_KEY) {
7aa7a72a 5469 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5e3dd157
KV
5470 peer_addr);
5471 ret = -EOPNOTSUPP;
5472 goto exit;
5473 } else {
5474 /* if the peer doesn't exist there is no key to disable
5475 * anymore */
5476 goto exit;
5477 }
5478 }
5479
7cc4573e
MK
5480 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5481 flags |= WMI_KEY_PAIRWISE;
5482 else
5483 flags |= WMI_KEY_GROUP;
5e3dd157 5484
5e3dd157 5485 if (is_wep) {
5e3dd157
KV
5486 if (cmd == DISABLE_KEY)
5487 ath10k_clear_vdev_key(arvif, key);
5e3dd157 5488
ad325cb5
MK
5489 /* When WEP keys are uploaded it's possible that there are
5490 * stations associated already (e.g. when merging) without any
5491 * keys. Static WEP needs an explicit per-peer key upload.
5492 */
5493 if (vif->type == NL80211_IFTYPE_ADHOC &&
5494 cmd == SET_KEY)
5495 ath10k_mac_vif_update_wep_key(arvif, key);
5496
370e5673
MK
5497 /* 802.1x never sets the def_wep_key_idx so each set_key()
5498 * call changes default tx key.
5499 *
5500 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
5501 * after first set_key().
5502 */
5503 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
5504 flags |= WMI_KEY_TX_USAGE;
370e5673 5505 }
627613f8 5506
370e5673 5507 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
5e3dd157 5508 if (ret) {
ccec9038 5509 WARN_ON(ret > 0);
7aa7a72a 5510 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
69244e56 5511 arvif->vdev_id, peer_addr, ret);
5e3dd157
KV
5512 goto exit;
5513 }
5514
29a10006
MK
5515 /* mac80211 sets static WEP keys as groupwise while firmware requires
5516 * them to be installed twice as both pairwise and groupwise.
5517 */
5518 if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
5519 flags2 = flags;
5520 flags2 &= ~WMI_KEY_GROUP;
5521 flags2 |= WMI_KEY_PAIRWISE;
5522
5523 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
5524 if (ret) {
ccec9038 5525 WARN_ON(ret > 0);
29a10006
MK
5526 ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
5527 arvif->vdev_id, peer_addr, ret);
5528 ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
5529 peer_addr, flags);
ccec9038
DL
5530 if (ret2) {
5531 WARN_ON(ret2 > 0);
29a10006
MK
5532 ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
5533 arvif->vdev_id, peer_addr, ret2);
ccec9038 5534 }
29a10006
MK
5535 goto exit;
5536 }
5537 }
5538
cfb27d29
MK
5539 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
5540
5e3dd157
KV
5541 spin_lock_bh(&ar->data_lock);
5542 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5543 if (peer && cmd == SET_KEY)
5544 peer->keys[key->keyidx] = key;
5545 else if (peer && cmd == DISABLE_KEY)
5546 peer->keys[key->keyidx] = NULL;
5547 else if (peer == NULL)
5548 /* impossible unless FW goes crazy */
7aa7a72a 5549 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5e3dd157
KV
5550 spin_unlock_bh(&ar->data_lock);
5551
5552exit:
5553 mutex_unlock(&ar->conf_mutex);
5554 return ret;
5555}
5556
627613f8
SJ
5557static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
5558 struct ieee80211_vif *vif,
5559 int keyidx)
5560{
5561 struct ath10k *ar = hw->priv;
5562 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5563 int ret;
5564
5565 mutex_lock(&arvif->ar->conf_mutex);
5566
5567 if (arvif->ar->state != ATH10K_STATE_ON)
5568 goto unlock;
5569
5570 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
5571 arvif->vdev_id, keyidx);
5572
5573 ret = ath10k_wmi_vdev_set_param(arvif->ar,
5574 arvif->vdev_id,
5575 arvif->ar->wmi.vdev_param->def_keyid,
5576 keyidx);
5577
5578 if (ret) {
5579 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
5580 arvif->vdev_id,
5581 ret);
5582 goto unlock;
5583 }
5584
5585 arvif->def_wep_key_idx = keyidx;
370e5673 5586
627613f8
SJ
5587unlock:
5588 mutex_unlock(&arvif->ar->conf_mutex);
5589}
5590
9797febc
MK
5591static void ath10k_sta_rc_update_wk(struct work_struct *wk)
5592{
5593 struct ath10k *ar;
5594 struct ath10k_vif *arvif;
5595 struct ath10k_sta *arsta;
5596 struct ieee80211_sta *sta;
45c9abc0 5597 struct cfg80211_chan_def def;
57fbcce3 5598 enum nl80211_band band;
45c9abc0
MK
5599 const u8 *ht_mcs_mask;
5600 const u16 *vht_mcs_mask;
9797febc
MK
5601 u32 changed, bw, nss, smps;
5602 int err;
5603
5604 arsta = container_of(wk, struct ath10k_sta, update_wk);
5605 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
5606 arvif = arsta->arvif;
5607 ar = arvif->ar;
5608
45c9abc0
MK
5609 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
5610 return;
5611
5612 band = def.chan->band;
5613 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
5614 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
5615
9797febc
MK
5616 spin_lock_bh(&ar->data_lock);
5617
5618 changed = arsta->changed;
5619 arsta->changed = 0;
5620
5621 bw = arsta->bw;
5622 nss = arsta->nss;
5623 smps = arsta->smps;
5624
5625 spin_unlock_bh(&ar->data_lock);
5626
5627 mutex_lock(&ar->conf_mutex);
5628
45c9abc0
MK
5629 nss = max_t(u32, 1, nss);
5630 nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
5631 ath10k_mac_max_vht_nss(vht_mcs_mask)));
5632
9797febc 5633 if (changed & IEEE80211_RC_BW_CHANGED) {
7aa7a72a 5634 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
9797febc
MK
5635 sta->addr, bw);
5636
5637 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5638 WMI_PEER_CHAN_WIDTH, bw);
5639 if (err)
7aa7a72a 5640 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
9797febc
MK
5641 sta->addr, bw, err);
5642 }
5643
5644 if (changed & IEEE80211_RC_NSS_CHANGED) {
7aa7a72a 5645 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
9797febc
MK
5646 sta->addr, nss);
5647
5648 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5649 WMI_PEER_NSS, nss);
5650 if (err)
7aa7a72a 5651 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
9797febc
MK
5652 sta->addr, nss, err);
5653 }
5654
5655 if (changed & IEEE80211_RC_SMPS_CHANGED) {
7aa7a72a 5656 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
9797febc
MK
5657 sta->addr, smps);
5658
5659 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5660 WMI_PEER_SMPS_STATE, smps);
5661 if (err)
7aa7a72a 5662 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
9797febc
MK
5663 sta->addr, smps, err);
5664 }
5665
55884c04
JD
5666 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
5667 changed & IEEE80211_RC_NSS_CHANGED) {
5668 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
44d6fa90
CYY
5669 sta->addr);
5670
590922a8 5671 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
44d6fa90 5672 if (err)
7aa7a72a 5673 ath10k_warn(ar, "failed to reassociate station: %pM\n",
44d6fa90
CYY
5674 sta->addr);
5675 }
5676
9797febc
MK
5677 mutex_unlock(&ar->conf_mutex);
5678}
5679
7c354242
MP
5680static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
5681 struct ieee80211_sta *sta)
cfd1061e
MK
5682{
5683 struct ath10k *ar = arvif->ar;
5684
5685 lockdep_assert_held(&ar->conf_mutex);
5686
7c354242 5687 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
cfd1061e
MK
5688 return 0;
5689
5690 if (ar->num_stations >= ar->max_num_stations)
5691 return -ENOBUFS;
5692
5693 ar->num_stations++;
5694
5695 return 0;
5696}
5697
7c354242
MP
5698static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
5699 struct ieee80211_sta *sta)
cfd1061e
MK
5700{
5701 struct ath10k *ar = arvif->ar;
5702
5703 lockdep_assert_held(&ar->conf_mutex);
5704
7c354242 5705 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
cfd1061e
MK
5706 return;
5707
5708 ar->num_stations--;
5709}
5710
75d85fd9
MP
5711struct ath10k_mac_tdls_iter_data {
5712 u32 num_tdls_stations;
5713 struct ieee80211_vif *curr_vif;
5714};
5715
5716static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
5717 struct ieee80211_sta *sta)
5e3dd157 5718{
75d85fd9 5719 struct ath10k_mac_tdls_iter_data *iter_data = data;
9797febc 5720 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
75d85fd9 5721 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
5e3dd157 5722
75d85fd9
MP
5723 if (sta->tdls && sta_vif == iter_data->curr_vif)
5724 iter_data->num_tdls_stations++;
5725}
5726
5727static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
5728 struct ieee80211_vif *vif)
5729{
5730 struct ath10k_mac_tdls_iter_data data = {};
5731
5732 data.curr_vif = vif;
5733
5734 ieee80211_iterate_stations_atomic(hw,
5735 ath10k_mac_tdls_vif_stations_count_iter,
5736 &data);
5737 return data.num_tdls_stations;
5738}
5739
5740static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
5741 struct ieee80211_vif *vif)
5742{
5743 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5744 int *num_tdls_vifs = data;
5745
5746 if (vif->type != NL80211_IFTYPE_STATION)
5747 return;
5748
5749 if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
5750 (*num_tdls_vifs)++;
5751}
5752
5753static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
5754{
5755 int num_tdls_vifs = 0;
5756
5757 ieee80211_iterate_active_interfaces_atomic(hw,
5758 IEEE80211_IFACE_ITER_NORMAL,
5759 ath10k_mac_tdls_vifs_count_iter,
5760 &num_tdls_vifs);
5761 return num_tdls_vifs;
5762}
5763
5e3dd157
KV
5764static int ath10k_sta_state(struct ieee80211_hw *hw,
5765 struct ieee80211_vif *vif,
5766 struct ieee80211_sta *sta,
5767 enum ieee80211_sta_state old_state,
5768 enum ieee80211_sta_state new_state)
5769{
5770 struct ath10k *ar = hw->priv;
5771 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
9797febc 5772 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
bb8f0c6a 5773 struct ath10k_peer *peer;
5e3dd157 5774 int ret = 0;
6942726f 5775 int i;
5e3dd157 5776
76f90024
MK
5777 if (old_state == IEEE80211_STA_NOTEXIST &&
5778 new_state == IEEE80211_STA_NONE) {
5779 memset(arsta, 0, sizeof(*arsta));
5780 arsta->arvif = arvif;
5781 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
29946878
MK
5782
5783 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
5784 ath10k_mac_txq_init(sta->txq[i]);
76f90024
MK
5785 }
5786
9797febc
MK
5787 /* cancel must be done outside the mutex to avoid deadlock */
5788 if ((old_state == IEEE80211_STA_NONE &&
5789 new_state == IEEE80211_STA_NOTEXIST))
5790 cancel_work_sync(&arsta->update_wk);
5791
5e3dd157
KV
5792 mutex_lock(&ar->conf_mutex);
5793
5794 if (old_state == IEEE80211_STA_NOTEXIST &&
077efc8c 5795 new_state == IEEE80211_STA_NONE) {
5e3dd157
KV
5796 /*
5797 * New station addition.
5798 */
75d85fd9
MP
5799 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
5800 u32 num_tdls_stations;
5801 u32 num_tdls_vifs;
5802
cfd1061e
MK
5803 ath10k_dbg(ar, ATH10K_DBG_MAC,
5804 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
5805 arvif->vdev_id, sta->addr,
5806 ar->num_stations + 1, ar->max_num_stations,
5807 ar->num_peers + 1, ar->max_num_peers);
0e759f36 5808
7c354242 5809 ret = ath10k_mac_inc_num_stations(arvif, sta);
cfd1061e
MK
5810 if (ret) {
5811 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
5812 ar->max_num_stations);
0e759f36
BM
5813 goto exit;
5814 }
5815
75d85fd9
MP
5816 if (sta->tdls)
5817 peer_type = WMI_PEER_TYPE_TDLS;
5818
6942726f
MK
5819 ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
5820 sta->addr, peer_type);
a52c0282 5821 if (ret) {
7aa7a72a 5822 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
479398b0 5823 sta->addr, arvif->vdev_id, ret);
7c354242 5824 ath10k_mac_dec_num_stations(arvif, sta);
a52c0282
MK
5825 goto exit;
5826 }
077efc8c 5827
bb8f0c6a
MK
5828 spin_lock_bh(&ar->data_lock);
5829
5830 peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
5831 if (!peer) {
5832 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5833 vif->addr, arvif->vdev_id);
5834 spin_unlock_bh(&ar->data_lock);
5835 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5836 ath10k_mac_dec_num_stations(arvif, sta);
5837 ret = -ENOENT;
5838 goto exit;
5839 }
5840
5841 arsta->peer_id = find_first_bit(peer->peer_ids,
5842 ATH10K_MAX_NUM_PEER_IDS);
5843
5844 spin_unlock_bh(&ar->data_lock);
5845
75d85fd9
MP
5846 if (!sta->tdls)
5847 goto exit;
5848
5849 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
5850 num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
5851
5852 if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
5853 num_tdls_stations == 0) {
5854 ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
5855 arvif->vdev_id, ar->max_num_tdls_vdevs);
5856 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5857 ath10k_mac_dec_num_stations(arvif, sta);
5858 ret = -ENOBUFS;
a52c0282
MK
5859 goto exit;
5860 }
077efc8c 5861
75d85fd9
MP
5862 if (num_tdls_stations == 0) {
5863 /* This is the first tdls peer in current vif */
5864 enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
077efc8c 5865
75d85fd9
MP
5866 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5867 state);
077efc8c 5868 if (ret) {
75d85fd9 5869 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
077efc8c 5870 arvif->vdev_id, ret);
75d85fd9
MP
5871 ath10k_peer_delete(ar, arvif->vdev_id,
5872 sta->addr);
5873 ath10k_mac_dec_num_stations(arvif, sta);
077efc8c
MK
5874 goto exit;
5875 }
75d85fd9
MP
5876 }
5877
5878 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
5879 WMI_TDLS_PEER_STATE_PEERING);
5880 if (ret) {
5881 ath10k_warn(ar,
5882 "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
5883 sta->addr, arvif->vdev_id, ret);
5884 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5885 ath10k_mac_dec_num_stations(arvif, sta);
077efc8c 5886
75d85fd9
MP
5887 if (num_tdls_stations != 0)
5888 goto exit;
5889 ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5890 WMI_TDLS_DISABLE);
077efc8c 5891 }
5e3dd157
KV
5892 } else if ((old_state == IEEE80211_STA_NONE &&
5893 new_state == IEEE80211_STA_NOTEXIST)) {
5894 /*
5895 * Existing station deletion.
5896 */
7aa7a72a 5897 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
5898 "mac vdev %d peer delete %pM (sta gone)\n",
5899 arvif->vdev_id, sta->addr);
077efc8c 5900
5e3dd157
KV
5901 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5902 if (ret)
7aa7a72a 5903 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
69244e56 5904 sta->addr, arvif->vdev_id, ret);
5e3dd157 5905
7c354242 5906 ath10k_mac_dec_num_stations(arvif, sta);
75d85fd9 5907
6942726f
MK
5908 spin_lock_bh(&ar->data_lock);
5909 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5910 peer = ar->peer_map[i];
5911 if (!peer)
5912 continue;
5913
5914 if (peer->sta == sta) {
5915 ath10k_warn(ar, "found sta peer %pM entry on vdev %i after it was supposedly removed\n",
5916 sta->addr, arvif->vdev_id);
5917 peer->sta = NULL;
5918 }
5919 }
5920 spin_unlock_bh(&ar->data_lock);
5921
29946878
MK
5922 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
5923 ath10k_mac_txq_unref(ar, sta->txq[i]);
5924
75d85fd9
MP
5925 if (!sta->tdls)
5926 goto exit;
5927
5928 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
5929 goto exit;
5930
5931 /* This was the last tdls peer in current vif */
5932 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
5933 WMI_TDLS_DISABLE);
5934 if (ret) {
5935 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
5936 arvif->vdev_id, ret);
5937 }
5e3dd157
KV
5938 } else if (old_state == IEEE80211_STA_AUTH &&
5939 new_state == IEEE80211_STA_ASSOC &&
5940 (vif->type == NL80211_IFTYPE_AP ||
b6c7bafa 5941 vif->type == NL80211_IFTYPE_MESH_POINT ||
5e3dd157
KV
5942 vif->type == NL80211_IFTYPE_ADHOC)) {
5943 /*
5944 * New association.
5945 */
7aa7a72a 5946 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
60c3daa8
KV
5947 sta->addr);
5948
590922a8 5949 ret = ath10k_station_assoc(ar, vif, sta, false);
5e3dd157 5950 if (ret)
7aa7a72a 5951 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
69244e56 5952 sta->addr, arvif->vdev_id, ret);
5e3dd157 5953 } else if (old_state == IEEE80211_STA_ASSOC &&
75d85fd9
MP
5954 new_state == IEEE80211_STA_AUTHORIZED &&
5955 sta->tdls) {
5956 /*
5957 * Tdls station authorized.
5958 */
5959 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
5960 sta->addr);
5961
5962 ret = ath10k_station_assoc(ar, vif, sta, false);
5963 if (ret) {
5964 ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
5965 sta->addr, arvif->vdev_id, ret);
5966 goto exit;
5967 }
5968
5969 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
5970 WMI_TDLS_PEER_STATE_CONNECTED);
5971 if (ret)
5972 ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
5973 sta->addr, arvif->vdev_id, ret);
5974 } else if (old_state == IEEE80211_STA_ASSOC &&
5975 new_state == IEEE80211_STA_AUTH &&
5976 (vif->type == NL80211_IFTYPE_AP ||
b6c7bafa 5977 vif->type == NL80211_IFTYPE_MESH_POINT ||
75d85fd9 5978 vif->type == NL80211_IFTYPE_ADHOC)) {
5e3dd157
KV
5979 /*
5980 * Disassociation.
5981 */
7aa7a72a 5982 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
60c3daa8
KV
5983 sta->addr);
5984
590922a8 5985 ret = ath10k_station_disassoc(ar, vif, sta);
5e3dd157 5986 if (ret)
7aa7a72a 5987 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
69244e56 5988 sta->addr, arvif->vdev_id, ret);
5e3dd157 5989 }
0e759f36 5990exit:
5e3dd157
KV
5991 mutex_unlock(&ar->conf_mutex);
5992 return ret;
5993}
5994
5995static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
5b07e07f 5996 u16 ac, bool enable)
5e3dd157
KV
5997{
5998 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
b0e56154
MK
5999 struct wmi_sta_uapsd_auto_trig_arg arg = {};
6000 u32 prio = 0, acc = 0;
5e3dd157
KV
6001 u32 value = 0;
6002 int ret = 0;
6003
548db54c
MK
6004 lockdep_assert_held(&ar->conf_mutex);
6005
5e3dd157
KV
6006 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
6007 return 0;
6008
6009 switch (ac) {
6010 case IEEE80211_AC_VO:
6011 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
6012 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
b0e56154
MK
6013 prio = 7;
6014 acc = 3;
5e3dd157
KV
6015 break;
6016 case IEEE80211_AC_VI:
6017 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
6018 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
b0e56154
MK
6019 prio = 5;
6020 acc = 2;
5e3dd157
KV
6021 break;
6022 case IEEE80211_AC_BE:
6023 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
6024 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
b0e56154
MK
6025 prio = 2;
6026 acc = 1;
5e3dd157
KV
6027 break;
6028 case IEEE80211_AC_BK:
6029 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
6030 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
b0e56154
MK
6031 prio = 0;
6032 acc = 0;
5e3dd157
KV
6033 break;
6034 }
6035
6036 if (enable)
6037 arvif->u.sta.uapsd |= value;
6038 else
6039 arvif->u.sta.uapsd &= ~value;
6040
6041 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6042 WMI_STA_PS_PARAM_UAPSD,
6043 arvif->u.sta.uapsd);
6044 if (ret) {
7aa7a72a 6045 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
5e3dd157
KV
6046 goto exit;
6047 }
6048
6049 if (arvif->u.sta.uapsd)
6050 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
6051 else
6052 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6053
6054 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6055 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
6056 value);
6057 if (ret)
7aa7a72a 6058 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
5e3dd157 6059
9f9b5746
MK
6060 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
6061 if (ret) {
6062 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
6063 arvif->vdev_id, ret);
6064 return ret;
6065 }
6066
6067 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
6068 if (ret) {
6069 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
6070 arvif->vdev_id, ret);
6071 return ret;
6072 }
6073
b0e56154
MK
6074 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
6075 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
6076 /* Only userspace can make an educated decision when to send
6077 * trigger frame. The following effectively disables u-UAPSD
6078 * autotrigger in firmware (which is enabled by default
6079 * provided the autotrigger service is available).
6080 */
6081
6082 arg.wmm_ac = acc;
6083 arg.user_priority = prio;
6084 arg.service_interval = 0;
6085 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6086 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6087
6088 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
6089 arvif->bssid, &arg, 1);
6090 if (ret) {
6091 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
6092 ret);
6093 return ret;
6094 }
6095 }
6096
5e3dd157
KV
6097exit:
6098 return ret;
6099}
6100
6101static int ath10k_conf_tx(struct ieee80211_hw *hw,
6102 struct ieee80211_vif *vif, u16 ac,
6103 const struct ieee80211_tx_queue_params *params)
6104{
6105 struct ath10k *ar = hw->priv;
5e752e42 6106 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5e3dd157
KV
6107 struct wmi_wmm_params_arg *p = NULL;
6108 int ret;
6109
6110 mutex_lock(&ar->conf_mutex);
6111
6112 switch (ac) {
6113 case IEEE80211_AC_VO:
5e752e42 6114 p = &arvif->wmm_params.ac_vo;
5e3dd157
KV
6115 break;
6116 case IEEE80211_AC_VI:
5e752e42 6117 p = &arvif->wmm_params.ac_vi;
5e3dd157
KV
6118 break;
6119 case IEEE80211_AC_BE:
5e752e42 6120 p = &arvif->wmm_params.ac_be;
5e3dd157
KV
6121 break;
6122 case IEEE80211_AC_BK:
5e752e42 6123 p = &arvif->wmm_params.ac_bk;
5e3dd157
KV
6124 break;
6125 }
6126
6127 if (WARN_ON(!p)) {
6128 ret = -EINVAL;
6129 goto exit;
6130 }
6131
6132 p->cwmin = params->cw_min;
6133 p->cwmax = params->cw_max;
6134 p->aifs = params->aifs;
6135
6136 /*
6137 * The channel time duration programmed in the HW is in absolute
6138 * microseconds, while mac80211 gives the txop in units of
6139 * 32 microseconds.
6140 */
6141 p->txop = params->txop * 32;
6142
7fc979a7
MK
6143 if (ar->wmi.ops->gen_vdev_wmm_conf) {
6144 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
6145 &arvif->wmm_params);
6146 if (ret) {
6147 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
6148 arvif->vdev_id, ret);
6149 goto exit;
6150 }
6151 } else {
6152 /* This won't work well with multi-interface cases but it's
6153 * better than nothing.
6154 */
6155 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
6156 if (ret) {
6157 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
6158 goto exit;
6159 }
5e3dd157
KV
6160 }
6161
6162 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
6163 if (ret)
7aa7a72a 6164 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
5e3dd157
KV
6165
6166exit:
6167 mutex_unlock(&ar->conf_mutex);
6168 return ret;
6169}
6170
6171#define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
6172
6173static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
6174 struct ieee80211_vif *vif,
6175 struct ieee80211_channel *chan,
6176 int duration,
6177 enum ieee80211_roc_type type)
6178{
6179 struct ath10k *ar = hw->priv;
6180 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6181 struct wmi_start_scan_arg arg;
5c81c7fd 6182 int ret = 0;
fcf98446 6183 u32 scan_time_msec;
5e3dd157
KV
6184
6185 mutex_lock(&ar->conf_mutex);
6186
6187 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
6188 switch (ar->scan.state) {
6189 case ATH10K_SCAN_IDLE:
6190 reinit_completion(&ar->scan.started);
6191 reinit_completion(&ar->scan.completed);
6192 reinit_completion(&ar->scan.on_channel);
6193 ar->scan.state = ATH10K_SCAN_STARTING;
6194 ar->scan.is_roc = true;
6195 ar->scan.vdev_id = arvif->vdev_id;
6196 ar->scan.roc_freq = chan->center_freq;
d710e75d 6197 ar->scan.roc_notify = true;
5c81c7fd
MK
6198 ret = 0;
6199 break;
6200 case ATH10K_SCAN_STARTING:
6201 case ATH10K_SCAN_RUNNING:
6202 case ATH10K_SCAN_ABORTING:
5e3dd157 6203 ret = -EBUSY;
5c81c7fd 6204 break;
5e3dd157 6205 }
5e3dd157
KV
6206 spin_unlock_bh(&ar->data_lock);
6207
5c81c7fd
MK
6208 if (ret)
6209 goto exit;
6210
fcf98446 6211 scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
dcca0bdb 6212
5e3dd157
KV
6213 memset(&arg, 0, sizeof(arg));
6214 ath10k_wmi_start_scan_init(ar, &arg);
6215 arg.vdev_id = arvif->vdev_id;
6216 arg.scan_id = ATH10K_SCAN_ID;
6217 arg.n_channels = 1;
6218 arg.channels[0] = chan->center_freq;
fcf98446
MK
6219 arg.dwell_time_active = scan_time_msec;
6220 arg.dwell_time_passive = scan_time_msec;
6221 arg.max_scan_time = scan_time_msec;
5e3dd157
KV
6222 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6223 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
dbd3f9f3 6224 arg.burst_duration_ms = duration;
5e3dd157
KV
6225
6226 ret = ath10k_start_scan(ar, &arg);
6227 if (ret) {
7aa7a72a 6228 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5e3dd157 6229 spin_lock_bh(&ar->data_lock);
5c81c7fd 6230 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
6231 spin_unlock_bh(&ar->data_lock);
6232 goto exit;
6233 }
6234
6235 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
6236 if (ret == 0) {
7aa7a72a 6237 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5c81c7fd
MK
6238
6239 ret = ath10k_scan_stop(ar);
6240 if (ret)
7aa7a72a 6241 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd 6242
5e3dd157
KV
6243 ret = -ETIMEDOUT;
6244 goto exit;
6245 }
6246
fcf98446
MK
6247 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6248 msecs_to_jiffies(duration));
6249
5e3dd157
KV
6250 ret = 0;
6251exit:
6252 mutex_unlock(&ar->conf_mutex);
6253 return ret;
6254}
6255
6256static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
6257{
6258 struct ath10k *ar = hw->priv;
6259
6260 mutex_lock(&ar->conf_mutex);
d710e75d
MK
6261
6262 spin_lock_bh(&ar->data_lock);
6263 ar->scan.roc_notify = false;
6264 spin_unlock_bh(&ar->data_lock);
6265
5c81c7fd 6266 ath10k_scan_abort(ar);
d710e75d 6267
5e3dd157
KV
6268 mutex_unlock(&ar->conf_mutex);
6269
4eb2e164
MK
6270 cancel_delayed_work_sync(&ar->scan.timeout);
6271
5e3dd157
KV
6272 return 0;
6273}
6274
6275/*
6276 * Both RTS and Fragmentation threshold are interface-specific
6277 * in ath10k, but device-specific in mac80211.
6278 */
5e3dd157 6279
ad088bfa
MK
6280static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6281{
6282 struct ath10k *ar = hw->priv;
6283 struct ath10k_vif *arvif;
6284 int ret = 0;
548db54c 6285
5e3dd157 6286 mutex_lock(&ar->conf_mutex);
ad088bfa 6287 list_for_each_entry(arvif, &ar->arvifs, list) {
7aa7a72a 6288 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
ad088bfa
MK
6289 arvif->vdev_id, value);
6290
6291 ret = ath10k_mac_set_rts(arvif, value);
6292 if (ret) {
7aa7a72a 6293 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
ad088bfa
MK
6294 arvif->vdev_id, ret);
6295 break;
6296 }
6297 }
5e3dd157
KV
6298 mutex_unlock(&ar->conf_mutex);
6299
ad088bfa 6300 return ret;
5e3dd157
KV
6301}
6302
92092fe5
MK
6303static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6304{
6305 /* Even though there's a WMI enum for fragmentation threshold no known
6306 * firmware actually implements it. Moreover it is not possible to rely
6307 * frame fragmentation to mac80211 because firmware clears the "more
6308 * fragments" bit in frame control making it impossible for remote
6309 * devices to reassemble frames.
6310 *
6311 * Hence implement a dummy callback just to say fragmentation isn't
6312 * supported. This effectively prevents mac80211 from doing frame
6313 * fragmentation in software.
6314 */
6315 return -EOPNOTSUPP;
6316}
6317
77be2c54
EG
6318static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6319 u32 queues, bool drop)
5e3dd157
KV
6320{
6321 struct ath10k *ar = hw->priv;
affd3217 6322 bool skip;
d4298a3a 6323 long time_left;
5e3dd157
KV
6324
6325 /* mac80211 doesn't care if we really xmit queued frames or not
6326 * we'll collect those frames either way if we stop/delete vdevs */
6327 if (drop)
6328 return;
6329
548db54c
MK
6330 mutex_lock(&ar->conf_mutex);
6331
affd3217
MK
6332 if (ar->state == ATH10K_STATE_WEDGED)
6333 goto skip;
6334
d4298a3a 6335 time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
5e3dd157 6336 bool empty;
affd3217 6337
edb8236d 6338 spin_lock_bh(&ar->htt.tx_lock);
0945baf7 6339 empty = (ar->htt.num_pending_tx == 0);
edb8236d 6340 spin_unlock_bh(&ar->htt.tx_lock);
affd3217 6341
7962b0d8
MK
6342 skip = (ar->state == ATH10K_STATE_WEDGED) ||
6343 test_bit(ATH10K_FLAG_CRASH_FLUSH,
6344 &ar->dev_flags);
affd3217
MK
6345
6346 (empty || skip);
5e3dd157 6347 }), ATH10K_FLUSH_TIMEOUT_HZ);
affd3217 6348
d4298a3a
NMG
6349 if (time_left == 0 || skip)
6350 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
6351 skip, ar->state, time_left);
548db54c 6352
affd3217 6353skip:
548db54c 6354 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
6355}
6356
6357/* TODO: Implement this function properly
6358 * For now it is needed to reply to Probe Requests in IBSS mode.
6359 * Propably we need this information from FW.
6360 */
6361static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
6362{
6363 return 1;
6364}
6365
cf2c92d8
EP
6366static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
6367 enum ieee80211_reconfig_type reconfig_type)
affd3217
MK
6368{
6369 struct ath10k *ar = hw->priv;
6370
cf2c92d8
EP
6371 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6372 return;
6373
affd3217
MK
6374 mutex_lock(&ar->conf_mutex);
6375
6376 /* If device failed to restart it will be in a different state, e.g.
6377 * ATH10K_STATE_WEDGED */
6378 if (ar->state == ATH10K_STATE_RESTARTED) {
7aa7a72a 6379 ath10k_info(ar, "device successfully recovered\n");
affd3217 6380 ar->state = ATH10K_STATE_ON;
7962b0d8 6381 ieee80211_wake_queues(ar->hw);
affd3217
MK
6382 }
6383
6384 mutex_unlock(&ar->conf_mutex);
6385}
6386
2e1dea40
MK
6387static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
6388 struct survey_info *survey)
6389{
6390 struct ath10k *ar = hw->priv;
6391 struct ieee80211_supported_band *sband;
6392 struct survey_info *ar_survey = &ar->survey[idx];
6393 int ret = 0;
6394
6395 mutex_lock(&ar->conf_mutex);
6396
57fbcce3 6397 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
2e1dea40
MK
6398 if (sband && idx >= sband->n_channels) {
6399 idx -= sband->n_channels;
6400 sband = NULL;
6401 }
6402
6403 if (!sband)
57fbcce3 6404 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
2e1dea40
MK
6405
6406 if (!sband || idx >= sband->n_channels) {
6407 ret = -ENOENT;
6408 goto exit;
6409 }
6410
6411 spin_lock_bh(&ar->data_lock);
6412 memcpy(survey, ar_survey, sizeof(*survey));
6413 spin_unlock_bh(&ar->data_lock);
6414
6415 survey->channel = &sband->channels[idx];
6416
fa1d4df8
FF
6417 if (ar->rx_channel == survey->channel)
6418 survey->filled |= SURVEY_INFO_IN_USE;
6419
2e1dea40
MK
6420exit:
6421 mutex_unlock(&ar->conf_mutex);
6422 return ret;
6423}
6424
51ab1a0a 6425static bool
3ae54225 6426ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
57fbcce3 6427 enum nl80211_band band,
3ae54225 6428 const struct cfg80211_bitrate_mask *mask)
51ab1a0a 6429{
3ae54225
MK
6430 int num_rates = 0;
6431 int i;
51ab1a0a 6432
3ae54225 6433 num_rates += hweight32(mask->control[band].legacy);
51ab1a0a 6434
3ae54225
MK
6435 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6436 num_rates += hweight8(mask->control[band].ht_mcs[i]);
51ab1a0a 6437
3ae54225
MK
6438 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6439 num_rates += hweight16(mask->control[band].vht_mcs[i]);
51ab1a0a 6440
3ae54225 6441 return num_rates == 1;
51ab1a0a
JD
6442}
6443
6444static bool
3ae54225 6445ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
57fbcce3 6446 enum nl80211_band band,
3ae54225
MK
6447 const struct cfg80211_bitrate_mask *mask,
6448 int *nss)
6449{
6450 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6451 u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6452 u8 ht_nss_mask = 0;
6453 u8 vht_nss_mask = 0;
6454 int i;
51ab1a0a 6455
3ae54225 6456 if (mask->control[band].legacy)
51ab1a0a
JD
6457 return false;
6458
3ae54225
MK
6459 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6460 if (mask->control[band].ht_mcs[i] == 0)
51ab1a0a 6461 continue;
3ae54225
MK
6462 else if (mask->control[band].ht_mcs[i] ==
6463 sband->ht_cap.mcs.rx_mask[i])
6464 ht_nss_mask |= BIT(i);
6465 else
6466 return false;
51ab1a0a
JD
6467 }
6468
3ae54225
MK
6469 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6470 if (mask->control[band].vht_mcs[i] == 0)
51ab1a0a 6471 continue;
3ae54225
MK
6472 else if (mask->control[band].vht_mcs[i] ==
6473 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6474 vht_nss_mask |= BIT(i);
6475 else
6476 return false;
51ab1a0a
JD
6477 }
6478
3ae54225 6479 if (ht_nss_mask != vht_nss_mask)
51ab1a0a
JD
6480 return false;
6481
3ae54225 6482 if (ht_nss_mask == 0)
51ab1a0a
JD
6483 return false;
6484
3ae54225 6485 if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
51ab1a0a
JD
6486 return false;
6487
3ae54225 6488 *nss = fls(ht_nss_mask);
51ab1a0a
JD
6489
6490 return true;
6491}
6492
3ae54225
MK
6493static int
6494ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
57fbcce3 6495 enum nl80211_band band,
3ae54225
MK
6496 const struct cfg80211_bitrate_mask *mask,
6497 u8 *rate, u8 *nss)
51ab1a0a 6498{
3ae54225
MK
6499 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6500 int rate_idx;
6501 int i;
6502 u16 bitrate;
6503 u8 preamble;
6504 u8 hw_rate;
51ab1a0a 6505
3ae54225
MK
6506 if (hweight32(mask->control[band].legacy) == 1) {
6507 rate_idx = ffs(mask->control[band].legacy) - 1;
51ab1a0a 6508
3ae54225
MK
6509 hw_rate = sband->bitrates[rate_idx].hw_value;
6510 bitrate = sband->bitrates[rate_idx].bitrate;
51ab1a0a 6511
3ae54225
MK
6512 if (ath10k_mac_bitrate_is_cck(bitrate))
6513 preamble = WMI_RATE_PREAMBLE_CCK;
6514 else
6515 preamble = WMI_RATE_PREAMBLE_OFDM;
51ab1a0a 6516
3ae54225
MK
6517 *nss = 1;
6518 *rate = preamble << 6 |
6519 (*nss - 1) << 4 |
6520 hw_rate << 0;
51ab1a0a 6521
3ae54225 6522 return 0;
51ab1a0a
JD
6523 }
6524
3ae54225
MK
6525 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6526 if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6527 *nss = i + 1;
6528 *rate = WMI_RATE_PREAMBLE_HT << 6 |
6529 (*nss - 1) << 4 |
6530 (ffs(mask->control[band].ht_mcs[i]) - 1);
51ab1a0a 6531
3ae54225
MK
6532 return 0;
6533 }
6534 }
51ab1a0a 6535
3ae54225
MK
6536 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6537 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6538 *nss = i + 1;
6539 *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6540 (*nss - 1) << 4 |
6541 (ffs(mask->control[band].vht_mcs[i]) - 1);
51ab1a0a 6542
3ae54225
MK
6543 return 0;
6544 }
6545 }
51ab1a0a 6546
3ae54225 6547 return -EINVAL;
51ab1a0a
JD
6548}
6549
3ae54225 6550static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
bd4a41e6 6551 u8 rate, u8 nss, u8 sgi, u8 ldpc)
51ab1a0a
JD
6552{
6553 struct ath10k *ar = arvif->ar;
6554 u32 vdev_param;
3ae54225 6555 int ret;
51ab1a0a 6556
3ae54225 6557 lockdep_assert_held(&ar->conf_mutex);
51ab1a0a 6558
3ae54225
MK
6559 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
6560 arvif->vdev_id, rate, nss, sgi);
9f81f725 6561
51ab1a0a 6562 vdev_param = ar->wmi.vdev_param->fixed_rate;
3ae54225 6563 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
51ab1a0a 6564 if (ret) {
7aa7a72a 6565 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
3ae54225
MK
6566 rate, ret);
6567 return ret;
51ab1a0a
JD
6568 }
6569
51ab1a0a 6570 vdev_param = ar->wmi.vdev_param->nss;
3ae54225 6571 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
51ab1a0a 6572 if (ret) {
3ae54225
MK
6573 ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
6574 return ret;
51ab1a0a 6575 }
51ab1a0a 6576
9f81f725 6577 vdev_param = ar->wmi.vdev_param->sgi;
3ae54225 6578 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
51ab1a0a 6579 if (ret) {
3ae54225
MK
6580 ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
6581 return ret;
51ab1a0a
JD
6582 }
6583
bd4a41e6
RM
6584 vdev_param = ar->wmi.vdev_param->ldpc;
6585 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
6586 if (ret) {
6587 ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
6588 return ret;
6589 }
6590
3ae54225 6591 return 0;
51ab1a0a 6592}
51ab1a0a 6593
45c9abc0
MK
6594static bool
6595ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
57fbcce3 6596 enum nl80211_band band,
45c9abc0
MK
6597 const struct cfg80211_bitrate_mask *mask)
6598{
6599 int i;
6600 u16 vht_mcs;
9f81f725 6601
45c9abc0
MK
6602 /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
6603 * to express all VHT MCS rate masks. Effectively only the following
6604 * ranges can be used: none, 0-7, 0-8 and 0-9.
6605 */
6606 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6607 vht_mcs = mask->control[band].vht_mcs[i];
6608
6609 switch (vht_mcs) {
6610 case 0:
6611 case BIT(8) - 1:
6612 case BIT(9) - 1:
6613 case BIT(10) - 1:
6614 break;
6615 default:
6616 ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
6617 return false;
6618 }
9f81f725
JD
6619 }
6620
45c9abc0
MK
6621 return true;
6622}
9f81f725 6623
45c9abc0
MK
6624static void ath10k_mac_set_bitrate_mask_iter(void *data,
6625 struct ieee80211_sta *sta)
6626{
6627 struct ath10k_vif *arvif = data;
6628 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6629 struct ath10k *ar = arvif->ar;
6630
6631 if (arsta->arvif != arvif)
6632 return;
6633
6634 spin_lock_bh(&ar->data_lock);
6635 arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6636 spin_unlock_bh(&ar->data_lock);
6637
6638 ieee80211_queue_work(ar->hw, &arsta->update_wk);
51ab1a0a
JD
6639}
6640
3ae54225
MK
6641static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6642 struct ieee80211_vif *vif,
6643 const struct cfg80211_bitrate_mask *mask)
51ab1a0a
JD
6644{
6645 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
500ff9f9 6646 struct cfg80211_chan_def def;
51ab1a0a 6647 struct ath10k *ar = arvif->ar;
57fbcce3 6648 enum nl80211_band band;
45c9abc0
MK
6649 const u8 *ht_mcs_mask;
6650 const u16 *vht_mcs_mask;
3ae54225
MK
6651 u8 rate;
6652 u8 nss;
6653 u8 sgi;
bd4a41e6 6654 u8 ldpc;
3ae54225
MK
6655 int single_nss;
6656 int ret;
9f81f725 6657
500ff9f9
MK
6658 if (ath10k_mac_vif_chan(vif, &def))
6659 return -EPERM;
b116ea19 6660
500ff9f9 6661 band = def.chan->band;
45c9abc0
MK
6662 ht_mcs_mask = mask->control[band].ht_mcs;
6663 vht_mcs_mask = mask->control[band].vht_mcs;
bd4a41e6 6664 ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
b116ea19 6665
3ae54225
MK
6666 sgi = mask->control[band].gi;
6667 if (sgi == NL80211_TXRATE_FORCE_LGI)
9f81f725 6668 return -EINVAL;
51ab1a0a 6669
3ae54225
MK
6670 if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
6671 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6672 &rate, &nss);
6673 if (ret) {
6674 ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
6675 arvif->vdev_id, ret);
6676 return ret;
6677 }
6678 } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6679 &single_nss)) {
6680 rate = WMI_FIXED_RATE_NONE;
6681 nss = single_nss;
6682 } else {
6683 rate = WMI_FIXED_RATE_NONE;
45c9abc0
MK
6684 nss = min(ar->num_rf_chains,
6685 max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6686 ath10k_mac_max_vht_nss(vht_mcs_mask)));
6687
6688 if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
51ab1a0a 6689 return -EINVAL;
45c9abc0
MK
6690
6691 mutex_lock(&ar->conf_mutex);
6692
6693 arvif->bitrate_mask = *mask;
6694 ieee80211_iterate_stations_atomic(ar->hw,
6695 ath10k_mac_set_bitrate_mask_iter,
6696 arvif);
6697
6698 mutex_unlock(&ar->conf_mutex);
51ab1a0a
JD
6699 }
6700
3ae54225
MK
6701 mutex_lock(&ar->conf_mutex);
6702
bd4a41e6 6703 ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
3ae54225
MK
6704 if (ret) {
6705 ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
6706 arvif->vdev_id, ret);
6707 goto exit;
9f81f725
JD
6708 }
6709
3ae54225
MK
6710exit:
6711 mutex_unlock(&ar->conf_mutex);
6712
6713 return ret;
51ab1a0a
JD
6714}
6715
9797febc
MK
6716static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
6717 struct ieee80211_vif *vif,
6718 struct ieee80211_sta *sta,
6719 u32 changed)
6720{
6721 struct ath10k *ar = hw->priv;
6722 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6723 u32 bw, smps;
6724
6725 spin_lock_bh(&ar->data_lock);
6726
7aa7a72a 6727 ath10k_dbg(ar, ATH10K_DBG_MAC,
9797febc
MK
6728 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
6729 sta->addr, changed, sta->bandwidth, sta->rx_nss,
6730 sta->smps_mode);
6731
6732 if (changed & IEEE80211_RC_BW_CHANGED) {
6733 bw = WMI_PEER_CHWIDTH_20MHZ;
6734
6735 switch (sta->bandwidth) {
6736 case IEEE80211_STA_RX_BW_20:
6737 bw = WMI_PEER_CHWIDTH_20MHZ;
6738 break;
6739 case IEEE80211_STA_RX_BW_40:
6740 bw = WMI_PEER_CHWIDTH_40MHZ;
6741 break;
6742 case IEEE80211_STA_RX_BW_80:
6743 bw = WMI_PEER_CHWIDTH_80MHZ;
6744 break;
6745 case IEEE80211_STA_RX_BW_160:
d939be3a 6746 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
be6546fc 6747 sta->bandwidth, sta->addr);
9797febc
MK
6748 bw = WMI_PEER_CHWIDTH_20MHZ;
6749 break;
6750 }
6751
6752 arsta->bw = bw;
6753 }
6754
6755 if (changed & IEEE80211_RC_NSS_CHANGED)
6756 arsta->nss = sta->rx_nss;
6757
6758 if (changed & IEEE80211_RC_SMPS_CHANGED) {
6759 smps = WMI_PEER_SMPS_PS_NONE;
6760
6761 switch (sta->smps_mode) {
6762 case IEEE80211_SMPS_AUTOMATIC:
6763 case IEEE80211_SMPS_OFF:
6764 smps = WMI_PEER_SMPS_PS_NONE;
6765 break;
6766 case IEEE80211_SMPS_STATIC:
6767 smps = WMI_PEER_SMPS_STATIC;
6768 break;
6769 case IEEE80211_SMPS_DYNAMIC:
6770 smps = WMI_PEER_SMPS_DYNAMIC;
6771 break;
6772 case IEEE80211_SMPS_NUM_MODES:
7aa7a72a 6773 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
be6546fc 6774 sta->smps_mode, sta->addr);
9797febc
MK
6775 smps = WMI_PEER_SMPS_PS_NONE;
6776 break;
6777 }
6778
6779 arsta->smps = smps;
6780 }
6781
9797febc
MK
6782 arsta->changed |= changed;
6783
6784 spin_unlock_bh(&ar->data_lock);
6785
6786 ieee80211_queue_work(hw, &arsta->update_wk);
6787}
6788
26ebbccf
CYY
6789static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
6790{
6791 /*
6792 * FIXME: Return 0 for time being. Need to figure out whether FW
6793 * has the API to fetch 64-bit local TSF
6794 */
6795
6796 return 0;
6797}
6798
aa5b4fbc
MK
6799static int ath10k_ampdu_action(struct ieee80211_hw *hw,
6800 struct ieee80211_vif *vif,
50ea05ef 6801 struct ieee80211_ampdu_params *params)
aa5b4fbc 6802{
7aa7a72a 6803 struct ath10k *ar = hw->priv;
aa5b4fbc 6804 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
50ea05ef
SS
6805 struct ieee80211_sta *sta = params->sta;
6806 enum ieee80211_ampdu_mlme_action action = params->action;
6807 u16 tid = params->tid;
aa5b4fbc 6808
7aa7a72a 6809 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
aa5b4fbc
MK
6810 arvif->vdev_id, sta->addr, tid, action);
6811
6812 switch (action) {
6813 case IEEE80211_AMPDU_RX_START:
6814 case IEEE80211_AMPDU_RX_STOP:
6815 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
6816 * creation/removal. Do we need to verify this?
6817 */
6818 return 0;
6819 case IEEE80211_AMPDU_TX_START:
6820 case IEEE80211_AMPDU_TX_STOP_CONT:
6821 case IEEE80211_AMPDU_TX_STOP_FLUSH:
6822 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
6823 case IEEE80211_AMPDU_TX_OPERATIONAL:
6824 /* Firmware offloads Tx aggregation entirely so deny mac80211
6825 * Tx aggregation requests.
6826 */
6827 return -EOPNOTSUPP;
6828 }
6829
6830 return -EINVAL;
6831}
6832
500ff9f9 6833static void
d7bf4b4a
MK
6834ath10k_mac_update_rx_channel(struct ath10k *ar,
6835 struct ieee80211_chanctx_conf *ctx,
6836 struct ieee80211_vif_chanctx_switch *vifs,
6837 int n_vifs)
500ff9f9
MK
6838{
6839 struct cfg80211_chan_def *def = NULL;
6840
6841 /* Both locks are required because ar->rx_channel is modified. This
6842 * allows readers to hold either lock.
6843 */
6844 lockdep_assert_held(&ar->conf_mutex);
6845 lockdep_assert_held(&ar->data_lock);
6846
d7bf4b4a
MK
6847 WARN_ON(ctx && vifs);
6848 WARN_ON(vifs && n_vifs != 1);
6849
500ff9f9
MK
6850 /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
6851 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
6852 * ppdu on Rx may reduce performance on low-end systems. It should be
6853 * possible to make tables/hashmaps to speed the lookup up (be vary of
6854 * cpu data cache lines though regarding sizes) but to keep the initial
6855 * implementation simple and less intrusive fallback to the slow lookup
6856 * only for multi-channel cases. Single-channel cases will remain to
6857 * use the old channel derival and thus performance should not be
6858 * affected much.
6859 */
6860 rcu_read_lock();
d7bf4b4a 6861 if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
500ff9f9 6862 ieee80211_iter_chan_contexts_atomic(ar->hw,
617b0f4d
KV
6863 ath10k_mac_get_any_chandef_iter,
6864 &def);
d7bf4b4a
MK
6865
6866 if (vifs)
6867 def = &vifs[0].new_ctx->def;
6868
500ff9f9 6869 ar->rx_channel = def->chan;
d7bf4b4a
MK
6870 } else if (ctx && ath10k_mac_num_chanctxs(ar) == 0) {
6871 ar->rx_channel = ctx->def.chan;
500ff9f9
MK
6872 } else {
6873 ar->rx_channel = NULL;
6874 }
6875 rcu_read_unlock();
6876}
6877
7be6d1b7
MK
6878static void
6879ath10k_mac_update_vif_chan(struct ath10k *ar,
6880 struct ieee80211_vif_chanctx_switch *vifs,
6881 int n_vifs)
6882{
6883 struct ath10k_vif *arvif;
6884 int ret;
6885 int i;
6886
6887 lockdep_assert_held(&ar->conf_mutex);
6888
6889 /* First stop monitor interface. Some FW versions crash if there's a
6890 * lone monitor interface.
6891 */
6892 if (ar->monitor_started)
6893 ath10k_monitor_stop(ar);
6894
6895 for (i = 0; i < n_vifs; i++) {
6896 arvif = ath10k_vif_to_arvif(vifs[i].vif);
6897
6898 ath10k_dbg(ar, ATH10K_DBG_MAC,
6899 "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
6900 arvif->vdev_id,
6901 vifs[i].old_ctx->def.chan->center_freq,
6902 vifs[i].new_ctx->def.chan->center_freq,
6903 vifs[i].old_ctx->def.width,
6904 vifs[i].new_ctx->def.width);
6905
6906 if (WARN_ON(!arvif->is_started))
6907 continue;
6908
6909 if (WARN_ON(!arvif->is_up))
6910 continue;
6911
6912 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
6913 if (ret) {
6914 ath10k_warn(ar, "failed to down vdev %d: %d\n",
6915 arvif->vdev_id, ret);
6916 continue;
6917 }
6918 }
6919
6920 /* All relevant vdevs are downed and associated channel resources
6921 * should be available for the channel switch now.
6922 */
6923
6924 spin_lock_bh(&ar->data_lock);
6925 ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
6926 spin_unlock_bh(&ar->data_lock);
6927
6928 for (i = 0; i < n_vifs; i++) {
6929 arvif = ath10k_vif_to_arvif(vifs[i].vif);
6930
6931 if (WARN_ON(!arvif->is_started))
6932 continue;
6933
6934 if (WARN_ON(!arvif->is_up))
6935 continue;
6936
6937 ret = ath10k_mac_setup_bcn_tmpl(arvif);
6938 if (ret)
6939 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
6940 ret);
6941
6942 ret = ath10k_mac_setup_prb_tmpl(arvif);
6943 if (ret)
6944 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
6945 ret);
6946
6947 ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
6948 if (ret) {
6949 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
6950 arvif->vdev_id, ret);
6951 continue;
6952 }
6953
6954 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
6955 arvif->bssid);
6956 if (ret) {
6957 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
6958 arvif->vdev_id, ret);
6959 continue;
6960 }
6961 }
6962
6963 ath10k_monitor_recalc(ar);
6964}
6965
500ff9f9
MK
6966static int
6967ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
6968 struct ieee80211_chanctx_conf *ctx)
6969{
6970 struct ath10k *ar = hw->priv;
500ff9f9
MK
6971
6972 ath10k_dbg(ar, ATH10K_DBG_MAC,
6973 "mac chanctx add freq %hu width %d ptr %p\n",
6974 ctx->def.chan->center_freq, ctx->def.width, ctx);
6975
6976 mutex_lock(&ar->conf_mutex);
6977
6978 spin_lock_bh(&ar->data_lock);
d7bf4b4a 6979 ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
500ff9f9
MK
6980 spin_unlock_bh(&ar->data_lock);
6981
6982 ath10k_recalc_radar_detection(ar);
6983 ath10k_monitor_recalc(ar);
6984
6985 mutex_unlock(&ar->conf_mutex);
6986
6987 return 0;
6988}
6989
6990static void
6991ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
6992 struct ieee80211_chanctx_conf *ctx)
6993{
6994 struct ath10k *ar = hw->priv;
6995
6996 ath10k_dbg(ar, ATH10K_DBG_MAC,
6997 "mac chanctx remove freq %hu width %d ptr %p\n",
6998 ctx->def.chan->center_freq, ctx->def.width, ctx);
6999
7000 mutex_lock(&ar->conf_mutex);
7001
7002 spin_lock_bh(&ar->data_lock);
d7bf4b4a 7003 ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
500ff9f9
MK
7004 spin_unlock_bh(&ar->data_lock);
7005
7006 ath10k_recalc_radar_detection(ar);
7007 ath10k_monitor_recalc(ar);
7008
7009 mutex_unlock(&ar->conf_mutex);
7010}
7011
9713e3de
MK
7012struct ath10k_mac_change_chanctx_arg {
7013 struct ieee80211_chanctx_conf *ctx;
7014 struct ieee80211_vif_chanctx_switch *vifs;
7015 int n_vifs;
7016 int next_vif;
7017};
7018
7019static void
7020ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7021 struct ieee80211_vif *vif)
7022{
7023 struct ath10k_mac_change_chanctx_arg *arg = data;
7024
7025 if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
7026 return;
7027
7028 arg->n_vifs++;
7029}
7030
7031static void
7032ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7033 struct ieee80211_vif *vif)
7034{
7035 struct ath10k_mac_change_chanctx_arg *arg = data;
7036 struct ieee80211_chanctx_conf *ctx;
7037
7038 ctx = rcu_access_pointer(vif->chanctx_conf);
7039 if (ctx != arg->ctx)
7040 return;
7041
7042 if (WARN_ON(arg->next_vif == arg->n_vifs))
7043 return;
7044
7045 arg->vifs[arg->next_vif].vif = vif;
7046 arg->vifs[arg->next_vif].old_ctx = ctx;
7047 arg->vifs[arg->next_vif].new_ctx = ctx;
7048 arg->next_vif++;
7049}
7050
500ff9f9
MK
7051static void
7052ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7053 struct ieee80211_chanctx_conf *ctx,
7054 u32 changed)
7055{
7056 struct ath10k *ar = hw->priv;
9713e3de 7057 struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
500ff9f9
MK
7058
7059 mutex_lock(&ar->conf_mutex);
7060
7061 ath10k_dbg(ar, ATH10K_DBG_MAC,
089ab7a5
MK
7062 "mac chanctx change freq %hu width %d ptr %p changed %x\n",
7063 ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
500ff9f9
MK
7064
7065 /* This shouldn't really happen because channel switching should use
7066 * switch_vif_chanctx().
7067 */
7068 if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7069 goto unlock;
7070
9713e3de
MK
7071 if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
7072 ieee80211_iterate_active_interfaces_atomic(
7073 hw,
7074 IEEE80211_IFACE_ITER_NORMAL,
7075 ath10k_mac_change_chanctx_cnt_iter,
7076 &arg);
7077 if (arg.n_vifs == 0)
7078 goto radar;
7079
7080 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
7081 GFP_KERNEL);
7082 if (!arg.vifs)
7083 goto radar;
7084
7085 ieee80211_iterate_active_interfaces_atomic(
7086 hw,
7087 IEEE80211_IFACE_ITER_NORMAL,
7088 ath10k_mac_change_chanctx_fill_iter,
7089 &arg);
7090 ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7091 kfree(arg.vifs);
7092 }
7093
7094radar:
500ff9f9
MK
7095 ath10k_recalc_radar_detection(ar);
7096
7097 /* FIXME: How to configure Rx chains properly? */
7098
7099 /* No other actions are actually necessary. Firmware maintains channel
7100 * definitions per vdev internally and there's no host-side channel
7101 * context abstraction to configure, e.g. channel width.
7102 */
7103
7104unlock:
7105 mutex_unlock(&ar->conf_mutex);
7106}
7107
7108static int
7109ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7110 struct ieee80211_vif *vif,
7111 struct ieee80211_chanctx_conf *ctx)
7112{
7113 struct ath10k *ar = hw->priv;
500ff9f9
MK
7114 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7115 int ret;
7116
7117 mutex_lock(&ar->conf_mutex);
7118
7119 ath10k_dbg(ar, ATH10K_DBG_MAC,
7120 "mac chanctx assign ptr %p vdev_id %i\n",
7121 ctx, arvif->vdev_id);
7122
7123 if (WARN_ON(arvif->is_started)) {
7124 mutex_unlock(&ar->conf_mutex);
7125 return -EBUSY;
7126 }
7127
089ab7a5 7128 ret = ath10k_vdev_start(arvif, &ctx->def);
500ff9f9
MK
7129 if (ret) {
7130 ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
7131 arvif->vdev_id, vif->addr,
089ab7a5 7132 ctx->def.chan->center_freq, ret);
500ff9f9
MK
7133 goto err;
7134 }
7135
7136 arvif->is_started = true;
7137
f23e587e
MK
7138 ret = ath10k_mac_vif_setup_ps(arvif);
7139 if (ret) {
7140 ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
7141 arvif->vdev_id, ret);
7142 goto err_stop;
7143 }
7144
500ff9f9
MK
7145 if (vif->type == NL80211_IFTYPE_MONITOR) {
7146 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
7147 if (ret) {
7148 ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
7149 arvif->vdev_id, ret);
7150 goto err_stop;
7151 }
7152
7153 arvif->is_up = true;
7154 }
7155
7156 mutex_unlock(&ar->conf_mutex);
7157 return 0;
7158
7159err_stop:
7160 ath10k_vdev_stop(arvif);
7161 arvif->is_started = false;
f23e587e 7162 ath10k_mac_vif_setup_ps(arvif);
500ff9f9
MK
7163
7164err:
7165 mutex_unlock(&ar->conf_mutex);
7166 return ret;
7167}
7168
7169static void
7170ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7171 struct ieee80211_vif *vif,
7172 struct ieee80211_chanctx_conf *ctx)
7173{
7174 struct ath10k *ar = hw->priv;
7175 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7176 int ret;
7177
7178 mutex_lock(&ar->conf_mutex);
7179
7180 ath10k_dbg(ar, ATH10K_DBG_MAC,
7181 "mac chanctx unassign ptr %p vdev_id %i\n",
7182 ctx, arvif->vdev_id);
7183
7184 WARN_ON(!arvif->is_started);
7185
7186 if (vif->type == NL80211_IFTYPE_MONITOR) {
7187 WARN_ON(!arvif->is_up);
7188
7189 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7190 if (ret)
7191 ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
7192 arvif->vdev_id, ret);
7193
7194 arvif->is_up = false;
7195 }
7196
7197 ret = ath10k_vdev_stop(arvif);
7198 if (ret)
7199 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
7200 arvif->vdev_id, ret);
7201
7202 arvif->is_started = false;
7203
7204 mutex_unlock(&ar->conf_mutex);
7205}
7206
7207static int
7208ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7209 struct ieee80211_vif_chanctx_switch *vifs,
7210 int n_vifs,
7211 enum ieee80211_chanctx_switch_mode mode)
7212{
7213 struct ath10k *ar = hw->priv;
500ff9f9
MK
7214
7215 mutex_lock(&ar->conf_mutex);
7216
7217 ath10k_dbg(ar, ATH10K_DBG_MAC,
7218 "mac chanctx switch n_vifs %d mode %d\n",
7219 n_vifs, mode);
7be6d1b7 7220 ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
500ff9f9
MK
7221
7222 mutex_unlock(&ar->conf_mutex);
7223 return 0;
7224}
7225
5e3dd157 7226static const struct ieee80211_ops ath10k_ops = {
f2f6ecab 7227 .tx = ath10k_mac_op_tx,
29946878 7228 .wake_tx_queue = ath10k_mac_op_wake_tx_queue,
5e3dd157
KV
7229 .start = ath10k_start,
7230 .stop = ath10k_stop,
7231 .config = ath10k_config,
7232 .add_interface = ath10k_add_interface,
7233 .remove_interface = ath10k_remove_interface,
7234 .configure_filter = ath10k_configure_filter,
7235 .bss_info_changed = ath10k_bss_info_changed,
7236 .hw_scan = ath10k_hw_scan,
7237 .cancel_hw_scan = ath10k_cancel_hw_scan,
7238 .set_key = ath10k_set_key,
627613f8 7239 .set_default_unicast_key = ath10k_set_default_unicast_key,
5e3dd157
KV
7240 .sta_state = ath10k_sta_state,
7241 .conf_tx = ath10k_conf_tx,
7242 .remain_on_channel = ath10k_remain_on_channel,
7243 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
7244 .set_rts_threshold = ath10k_set_rts_threshold,
92092fe5 7245 .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
5e3dd157
KV
7246 .flush = ath10k_flush,
7247 .tx_last_beacon = ath10k_tx_last_beacon,
46acf7bb
BG
7248 .set_antenna = ath10k_set_antenna,
7249 .get_antenna = ath10k_get_antenna,
cf2c92d8 7250 .reconfig_complete = ath10k_reconfig_complete,
2e1dea40 7251 .get_survey = ath10k_get_survey,
3ae54225 7252 .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
9797febc 7253 .sta_rc_update = ath10k_sta_rc_update,
26ebbccf 7254 .get_tsf = ath10k_get_tsf,
aa5b4fbc 7255 .ampdu_action = ath10k_ampdu_action,
6cddcc7a
BG
7256 .get_et_sset_count = ath10k_debug_get_et_sset_count,
7257 .get_et_stats = ath10k_debug_get_et_stats,
7258 .get_et_strings = ath10k_debug_get_et_strings,
500ff9f9
MK
7259 .add_chanctx = ath10k_mac_op_add_chanctx,
7260 .remove_chanctx = ath10k_mac_op_remove_chanctx,
7261 .change_chanctx = ath10k_mac_op_change_chanctx,
7262 .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
7263 .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
7264 .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
43d2a30f
KV
7265
7266 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
7267
8cd13cad 7268#ifdef CONFIG_PM
5fd3ac3c
JD
7269 .suspend = ath10k_wow_op_suspend,
7270 .resume = ath10k_wow_op_resume,
8cd13cad 7271#endif
f5045988
RM
7272#ifdef CONFIG_MAC80211_DEBUGFS
7273 .sta_add_debugfs = ath10k_sta_add_debugfs,
7274#endif
5e3dd157
KV
7275};
7276
5e3dd157 7277#define CHAN2G(_channel, _freq, _flags) { \
57fbcce3 7278 .band = NL80211_BAND_2GHZ, \
5e3dd157
KV
7279 .hw_value = (_channel), \
7280 .center_freq = (_freq), \
7281 .flags = (_flags), \
7282 .max_antenna_gain = 0, \
7283 .max_power = 30, \
7284}
7285
7286#define CHAN5G(_channel, _freq, _flags) { \
57fbcce3 7287 .band = NL80211_BAND_5GHZ, \
5e3dd157
KV
7288 .hw_value = (_channel), \
7289 .center_freq = (_freq), \
7290 .flags = (_flags), \
7291 .max_antenna_gain = 0, \
7292 .max_power = 30, \
7293}
7294
7295static const struct ieee80211_channel ath10k_2ghz_channels[] = {
7296 CHAN2G(1, 2412, 0),
7297 CHAN2G(2, 2417, 0),
7298 CHAN2G(3, 2422, 0),
7299 CHAN2G(4, 2427, 0),
7300 CHAN2G(5, 2432, 0),
7301 CHAN2G(6, 2437, 0),
7302 CHAN2G(7, 2442, 0),
7303 CHAN2G(8, 2447, 0),
7304 CHAN2G(9, 2452, 0),
7305 CHAN2G(10, 2457, 0),
7306 CHAN2G(11, 2462, 0),
7307 CHAN2G(12, 2467, 0),
7308 CHAN2G(13, 2472, 0),
7309 CHAN2G(14, 2484, 0),
7310};
7311
7312static const struct ieee80211_channel ath10k_5ghz_channels[] = {
429ff56a
MK
7313 CHAN5G(36, 5180, 0),
7314 CHAN5G(40, 5200, 0),
7315 CHAN5G(44, 5220, 0),
7316 CHAN5G(48, 5240, 0),
7317 CHAN5G(52, 5260, 0),
7318 CHAN5G(56, 5280, 0),
7319 CHAN5G(60, 5300, 0),
7320 CHAN5G(64, 5320, 0),
7321 CHAN5G(100, 5500, 0),
7322 CHAN5G(104, 5520, 0),
7323 CHAN5G(108, 5540, 0),
7324 CHAN5G(112, 5560, 0),
7325 CHAN5G(116, 5580, 0),
7326 CHAN5G(120, 5600, 0),
7327 CHAN5G(124, 5620, 0),
7328 CHAN5G(128, 5640, 0),
7329 CHAN5G(132, 5660, 0),
7330 CHAN5G(136, 5680, 0),
7331 CHAN5G(140, 5700, 0),
4a7898fe 7332 CHAN5G(144, 5720, 0),
429ff56a
MK
7333 CHAN5G(149, 5745, 0),
7334 CHAN5G(153, 5765, 0),
7335 CHAN5G(157, 5785, 0),
7336 CHAN5G(161, 5805, 0),
7337 CHAN5G(165, 5825, 0),
5e3dd157
KV
7338};
7339
e7b54194 7340struct ath10k *ath10k_mac_create(size_t priv_size)
5e3dd157
KV
7341{
7342 struct ieee80211_hw *hw;
7343 struct ath10k *ar;
7344
e7b54194 7345 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5e3dd157
KV
7346 if (!hw)
7347 return NULL;
7348
7349 ar = hw->priv;
7350 ar->hw = hw;
7351
7352 return ar;
7353}
7354
7355void ath10k_mac_destroy(struct ath10k *ar)
7356{
7357 ieee80211_free_hw(ar->hw);
7358}
7359
7360static const struct ieee80211_iface_limit ath10k_if_limits[] = {
7361 {
78f7aeb0
MSS
7362 .max = 8,
7363 .types = BIT(NL80211_IFTYPE_STATION)
7364 | BIT(NL80211_IFTYPE_P2P_CLIENT)
d531cb85
MK
7365 },
7366 {
78f7aeb0
MSS
7367 .max = 3,
7368 .types = BIT(NL80211_IFTYPE_P2P_GO)
d531cb85
MK
7369 },
7370 {
78f7aeb0
MSS
7371 .max = 1,
7372 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
75d2bd48
MK
7373 },
7374 {
78f7aeb0
MSS
7375 .max = 7,
7376 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa 7377#ifdef CONFIG_MAC80211_MESH
78f7aeb0 7378 | BIT(NL80211_IFTYPE_MESH_POINT)
b6c7bafa 7379#endif
d531cb85 7380 },
5e3dd157
KV
7381};
7382
f259509b 7383static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
e8a50f8b 7384 {
78f7aeb0
MSS
7385 .max = 8,
7386 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa 7387#ifdef CONFIG_MAC80211_MESH
78f7aeb0 7388 | BIT(NL80211_IFTYPE_MESH_POINT)
b6c7bafa 7389#endif
e8a50f8b 7390 },
78f7aeb0
MSS
7391 {
7392 .max = 1,
7393 .types = BIT(NL80211_IFTYPE_STATION)
7394 },
e8a50f8b 7395};
e8a50f8b
MP
7396
7397static const struct ieee80211_iface_combination ath10k_if_comb[] = {
7398 {
7399 .limits = ath10k_if_limits,
7400 .n_limits = ARRAY_SIZE(ath10k_if_limits),
7401 .max_interfaces = 8,
7402 .num_different_channels = 1,
7403 .beacon_int_infra_match = true,
7404 },
f259509b
BM
7405};
7406
7407static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
e8a50f8b 7408 {
f259509b
BM
7409 .limits = ath10k_10x_if_limits,
7410 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
e8a50f8b
MP
7411 .max_interfaces = 8,
7412 .num_different_channels = 1,
7413 .beacon_int_infra_match = true,
f259509b 7414#ifdef CONFIG_ATH10K_DFS_CERTIFIED
e8a50f8b
MP
7415 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7416 BIT(NL80211_CHAN_WIDTH_20) |
7417 BIT(NL80211_CHAN_WIDTH_40) |
7418 BIT(NL80211_CHAN_WIDTH_80),
e8a50f8b 7419#endif
f259509b 7420 },
5e3dd157
KV
7421};
7422
cf32784c
MK
7423static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
7424 {
7425 .max = 2,
ed25b113
MK
7426 .types = BIT(NL80211_IFTYPE_STATION),
7427 },
7428 {
7429 .max = 2,
7430 .types = BIT(NL80211_IFTYPE_AP) |
b6c7bafa
BC
7431#ifdef CONFIG_MAC80211_MESH
7432 BIT(NL80211_IFTYPE_MESH_POINT) |
7433#endif
cf32784c
MK
7434 BIT(NL80211_IFTYPE_P2P_CLIENT) |
7435 BIT(NL80211_IFTYPE_P2P_GO),
7436 },
7437 {
7438 .max = 1,
7439 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7440 },
7441};
7442
ed25b113
MK
7443static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
7444 {
7445 .max = 2,
7446 .types = BIT(NL80211_IFTYPE_STATION),
7447 },
7448 {
7449 .max = 2,
7450 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
7451 },
7452 {
7453 .max = 1,
7454 .types = BIT(NL80211_IFTYPE_AP) |
b6c7bafa
BC
7455#ifdef CONFIG_MAC80211_MESH
7456 BIT(NL80211_IFTYPE_MESH_POINT) |
7457#endif
ed25b113
MK
7458 BIT(NL80211_IFTYPE_P2P_GO),
7459 },
7460 {
7461 .max = 1,
7462 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7463 },
7464};
7465
cf32784c
MK
7466static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
7467 {
7468 .max = 1,
7469 .types = BIT(NL80211_IFTYPE_STATION),
7470 },
7471 {
7472 .max = 1,
7473 .types = BIT(NL80211_IFTYPE_ADHOC),
7474 },
7475};
7476
7477/* FIXME: This is not thouroughly tested. These combinations may over- or
7478 * underestimate hw/fw capabilities.
7479 */
7480static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
7481 {
7482 .limits = ath10k_tlv_if_limit,
7483 .num_different_channels = 1,
ed25b113 7484 .max_interfaces = 4,
cf32784c
MK
7485 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7486 },
7487 {
7488 .limits = ath10k_tlv_if_limit_ibss,
7489 .num_different_channels = 1,
7490 .max_interfaces = 2,
7491 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7492 },
7493};
7494
7495static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
7496 {
7497 .limits = ath10k_tlv_if_limit,
ed25b113
MK
7498 .num_different_channels = 1,
7499 .max_interfaces = 4,
cf32784c
MK
7500 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7501 },
ed25b113
MK
7502 {
7503 .limits = ath10k_tlv_qcs_if_limit,
7504 .num_different_channels = 2,
7505 .max_interfaces = 4,
7506 .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
7507 },
cf32784c
MK
7508 {
7509 .limits = ath10k_tlv_if_limit_ibss,
7510 .num_different_channels = 1,
7511 .max_interfaces = 2,
7512 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7513 },
7514};
7515
cf36fef0
RM
7516static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
7517 {
7518 .max = 1,
7519 .types = BIT(NL80211_IFTYPE_STATION),
7520 },
7521 {
7522 .max = 16,
7523 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa
BC
7524#ifdef CONFIG_MAC80211_MESH
7525 | BIT(NL80211_IFTYPE_MESH_POINT)
7526#endif
cf36fef0
RM
7527 },
7528};
7529
7530static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
7531 {
7532 .limits = ath10k_10_4_if_limits,
7533 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
7534 .max_interfaces = 16,
7535 .num_different_channels = 1,
7536 .beacon_int_infra_match = true,
7537#ifdef CONFIG_ATH10K_DFS_CERTIFIED
7538 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7539 BIT(NL80211_CHAN_WIDTH_20) |
7540 BIT(NL80211_CHAN_WIDTH_40) |
7541 BIT(NL80211_CHAN_WIDTH_80),
7542#endif
7543 },
7544};
7545
5e3dd157
KV
7546static void ath10k_get_arvif_iter(void *data, u8 *mac,
7547 struct ieee80211_vif *vif)
7548{
7549 struct ath10k_vif_iter *arvif_iter = data;
7550 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7551
7552 if (arvif->vdev_id == arvif_iter->vdev_id)
7553 arvif_iter->arvif = arvif;
7554}
7555
7556struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
7557{
7558 struct ath10k_vif_iter arvif_iter;
7559 u32 flags;
7560
7561 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
7562 arvif_iter.vdev_id = vdev_id;
7563
7564 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
7565 ieee80211_iterate_active_interfaces_atomic(ar->hw,
7566 flags,
7567 ath10k_get_arvif_iter,
7568 &arvif_iter);
7569 if (!arvif_iter.arvif) {
7aa7a72a 7570 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5e3dd157
KV
7571 return NULL;
7572 }
7573
7574 return arvif_iter.arvif;
7575}
7576
7577int ath10k_mac_register(struct ath10k *ar)
7578{
3cb10943
JB
7579 static const u32 cipher_suites[] = {
7580 WLAN_CIPHER_SUITE_WEP40,
7581 WLAN_CIPHER_SUITE_WEP104,
7582 WLAN_CIPHER_SUITE_TKIP,
7583 WLAN_CIPHER_SUITE_CCMP,
7584 WLAN_CIPHER_SUITE_AES_CMAC,
7585 };
5e3dd157 7586 struct ieee80211_supported_band *band;
5e3dd157
KV
7587 void *channels;
7588 int ret;
7589
7590 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7591
7592 SET_IEEE80211_DEV(ar->hw, ar->dev);
7593
c94aa7ef
MK
7594 BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
7595 ARRAY_SIZE(ath10k_5ghz_channels)) !=
7596 ATH10K_NUM_CHANS);
7597
5e3dd157
KV
7598 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
7599 channels = kmemdup(ath10k_2ghz_channels,
7600 sizeof(ath10k_2ghz_channels),
7601 GFP_KERNEL);
d6015b27
MK
7602 if (!channels) {
7603 ret = -ENOMEM;
7604 goto err_free;
7605 }
5e3dd157 7606
57fbcce3 7607 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5e3dd157
KV
7608 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
7609 band->channels = channels;
7610 band->n_bitrates = ath10k_g_rates_size;
7611 band->bitrates = ath10k_g_rates;
5e3dd157 7612
57fbcce3 7613 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
5e3dd157
KV
7614 }
7615
7616 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
7617 channels = kmemdup(ath10k_5ghz_channels,
7618 sizeof(ath10k_5ghz_channels),
7619 GFP_KERNEL);
7620 if (!channels) {
d6015b27
MK
7621 ret = -ENOMEM;
7622 goto err_free;
5e3dd157
KV
7623 }
7624
57fbcce3 7625 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5e3dd157
KV
7626 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
7627 band->channels = channels;
7628 band->n_bitrates = ath10k_a_rates_size;
7629 band->bitrates = ath10k_a_rates;
57fbcce3 7630 ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
5e3dd157
KV
7631 }
7632
5036fe0f
RM
7633 ath10k_mac_setup_ht_vht_cap(ar);
7634
5e3dd157
KV
7635 ar->hw->wiphy->interface_modes =
7636 BIT(NL80211_IFTYPE_STATION) |
b6c7bafa
BC
7637 BIT(NL80211_IFTYPE_AP) |
7638 BIT(NL80211_IFTYPE_MESH_POINT);
d354181f 7639
166de3f1
RM
7640 ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
7641 ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
46acf7bb 7642
d354181f
BM
7643 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
7644 ar->hw->wiphy->interface_modes |=
75d2bd48 7645 BIT(NL80211_IFTYPE_P2P_DEVICE) |
d354181f
BM
7646 BIT(NL80211_IFTYPE_P2P_CLIENT) |
7647 BIT(NL80211_IFTYPE_P2P_GO);
5e3dd157 7648
30686bf7
JB
7649 ieee80211_hw_set(ar->hw, SIGNAL_DBM);
7650 ieee80211_hw_set(ar->hw, SUPPORTS_PS);
7651 ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
7652 ieee80211_hw_set(ar->hw, MFP_CAPABLE);
7653 ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
7654 ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
7655 ieee80211_hw_set(ar->hw, AP_LINK_PS);
7656 ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
30686bf7
JB
7657 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
7658 ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
7659 ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
7660 ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
7661 ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
7662 ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
5e3dd157 7663
ccec9038
DL
7664 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
7665 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
7666
0d8614b4 7667 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
0cd9bc14 7668 ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
0d8614b4 7669
5e3dd157 7670 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
0d8614b4 7671 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5e3dd157
KV
7672
7673 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
30686bf7
JB
7674 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
7675 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
5e3dd157
KV
7676 }
7677
7678 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
7679 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
7680
7681 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9797febc 7682 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
29946878 7683 ar->hw->txq_data_size = sizeof(struct ath10k_txq);
5e3dd157 7684
5e3dd157
KV
7685 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
7686
fbb8f1b7
MK
7687 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
7688 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
7689
7690 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
7691 * that userspace (e.g. wpa_supplicant/hostapd) can generate
7692 * correct Probe Responses. This is more of a hack advert..
7693 */
7694 ar->hw->wiphy->probe_resp_offload |=
7695 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
7696 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
7697 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
7698 }
7699
75d85fd9
MP
7700 if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
7701 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
7702
5e3dd157 7703 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
c2df44b3 7704 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5e3dd157
KV
7705 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
7706
7707 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
bf031bc4
VT
7708 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
7709 NL80211_FEATURE_AP_SCAN;
78157a1c 7710
37a0b394
JD
7711 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
7712
5fd3ac3c
JD
7713 ret = ath10k_wow_init(ar);
7714 if (ret) {
7715 ath10k_warn(ar, "failed to init wow: %d\n", ret);
7716 goto err_free;
7717 }
7718
c702534a
JD
7719 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
7720
5e3dd157
KV
7721 /*
7722 * on LL hardware queues are managed entirely by the FW
7723 * so we only advertise to mac we can do the queues thing
7724 */
96d828d4
MK
7725 ar->hw->queues = IEEE80211_MAX_QUEUES;
7726
7727 /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
7728 * something that vdev_ids can't reach so that we don't stop the queue
7729 * accidentally.
7730 */
7731 ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
5e3dd157 7732
5cc7caf4
KV
7733 switch (ar->wmi.op_version) {
7734 case ATH10K_FW_WMI_OP_VERSION_MAIN:
f259509b
BM
7735 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
7736 ar->hw->wiphy->n_iface_combinations =
7737 ARRAY_SIZE(ath10k_if_comb);
cf850d1d 7738 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5cc7caf4 7739 break;
cf32784c
MK
7740 case ATH10K_FW_WMI_OP_VERSION_TLV:
7741 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
7742 ar->hw->wiphy->iface_combinations =
7743 ath10k_tlv_qcs_if_comb;
7744 ar->hw->wiphy->n_iface_combinations =
7745 ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
7746 } else {
7747 ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
7748 ar->hw->wiphy->n_iface_combinations =
7749 ARRAY_SIZE(ath10k_tlv_if_comb);
7750 }
7751 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
7752 break;
5cc7caf4
KV
7753 case ATH10K_FW_WMI_OP_VERSION_10_1:
7754 case ATH10K_FW_WMI_OP_VERSION_10_2:
4a16fbec 7755 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5cc7caf4
KV
7756 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
7757 ar->hw->wiphy->n_iface_combinations =
7758 ARRAY_SIZE(ath10k_10x_if_comb);
7759 break;
9bd21322 7760 case ATH10K_FW_WMI_OP_VERSION_10_4:
cf36fef0
RM
7761 ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
7762 ar->hw->wiphy->n_iface_combinations =
7763 ARRAY_SIZE(ath10k_10_4_if_comb);
9bd21322 7764 break;
5cc7caf4
KV
7765 case ATH10K_FW_WMI_OP_VERSION_UNSET:
7766 case ATH10K_FW_WMI_OP_VERSION_MAX:
7767 WARN_ON(1);
7768 ret = -EINVAL;
7769 goto err_free;
f259509b 7770 }
5e3dd157 7771
ccec9038
DL
7772 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
7773 ar->hw->netdev_features = NETIF_F_HW_CSUM;
7c199997 7774
9702c686
JD
7775 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
7776 /* Init ath dfs pattern detector */
7777 ar->ath_common.debug_mask = ATH_DBG_DFS;
7778 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
7779 NL80211_DFS_UNSET);
7780
7781 if (!ar->dfs_detector)
7aa7a72a 7782 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
9702c686
JD
7783 }
7784
5e3dd157
KV
7785 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
7786 ath10k_reg_notifier);
7787 if (ret) {
7aa7a72a 7788 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
0e339447 7789 goto err_dfs_detector_exit;
5e3dd157
KV
7790 }
7791
3cb10943
JB
7792 ar->hw->wiphy->cipher_suites = cipher_suites;
7793 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
7794
5e3dd157
KV
7795 ret = ieee80211_register_hw(ar->hw);
7796 if (ret) {
7aa7a72a 7797 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
0e339447 7798 goto err_dfs_detector_exit;
5e3dd157
KV
7799 }
7800
7801 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
7802 ret = regulatory_hint(ar->hw->wiphy,
7803 ar->ath_common.regulatory.alpha2);
7804 if (ret)
d6015b27 7805 goto err_unregister;
5e3dd157
KV
7806 }
7807
7808 return 0;
d6015b27
MK
7809
7810err_unregister:
5e3dd157 7811 ieee80211_unregister_hw(ar->hw);
0e339447
JJ
7812
7813err_dfs_detector_exit:
7814 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
7815 ar->dfs_detector->exit(ar->dfs_detector);
7816
d6015b27 7817err_free:
57fbcce3
JB
7818 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7819 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
d6015b27 7820
0e339447 7821 SET_IEEE80211_DEV(ar->hw, NULL);
5e3dd157
KV
7822 return ret;
7823}
7824
7825void ath10k_mac_unregister(struct ath10k *ar)
7826{
7827 ieee80211_unregister_hw(ar->hw);
7828
9702c686
JD
7829 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
7830 ar->dfs_detector->exit(ar->dfs_detector);
7831
57fbcce3
JB
7832 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
7833 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
5e3dd157
KV
7834
7835 SET_IEEE80211_DEV(ar->hw, NULL);
7836}