]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/net/wireless/ath/ath10k/mac.c
ath10k: high latency fixes for beacon buffer
[mirror_ubuntu-jammy-kernel.git] / drivers / net / wireless / ath / ath10k / mac.c
CommitLineData
f0553ca9 1// SPDX-License-Identifier: ISC
5e3dd157
KV
2/*
3 * Copyright (c) 2005-2011 Atheros Communications Inc.
8b1083d6 4 * Copyright (c) 2011-2017 Qualcomm Atheros, Inc.
6e8a8991 5 * Copyright (c) 2018-2019, The Linux Foundation. All rights reserved.
5e3dd157
KV
6 */
7
8#include "mac.h"
9
34d5629d 10#include <net/cfg80211.h>
5e3dd157
KV
11#include <net/mac80211.h>
12#include <linux/etherdevice.h>
209b2a68 13#include <linux/acpi.h>
84758d4d 14#include <linux/of.h>
1382993f 15#include <linux/bitfield.h>
5e3dd157 16
8cd13cad 17#include "hif.h"
5e3dd157
KV
18#include "core.h"
19#include "debug.h"
20#include "wmi.h"
21#include "htt.h"
22#include "txrx.h"
43d2a30f 23#include "testmode.h"
b4aa539d 24#include "wmi-tlv.h"
d7579d12 25#include "wmi-ops.h"
5fd3ac3c 26#include "wow.h"
5e3dd157 27
dcc33098
MK
28/*********/
29/* Rates */
30/*********/
31
dcc33098 32static struct ieee80211_rate ath10k_rates[] = {
5528e032
MK
33 { .bitrate = 10,
34 .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
35 { .bitrate = 20,
36 .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
37 .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
38 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
39 { .bitrate = 55,
40 .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
41 .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
42 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
43 { .bitrate = 110,
44 .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
45 .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
46 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
af00148f
MK
47
48 { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
49 { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
50 { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
51 { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
52 { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
53 { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
54 { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
55 { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
dcc33098
MK
56};
57
5269c659
MSS
58static struct ieee80211_rate ath10k_rates_rev2[] = {
59 { .bitrate = 10,
60 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
61 { .bitrate = 20,
62 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
63 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
64 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
65 { .bitrate = 55,
66 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
67 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
68 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
69 { .bitrate = 110,
70 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
71 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
72 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
73
74 { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
75 { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
76 { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
77 { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
78 { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
79 { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
80 { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
81 { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
82};
83
442545ba
CH
84static const struct cfg80211_sar_freq_ranges ath10k_sar_freq_ranges[] = {
85 {.start_freq = 2402, .end_freq = 2494 },
86 {.start_freq = 5170, .end_freq = 5875 },
87};
88
89static const struct cfg80211_sar_capa ath10k_sar_capa = {
90 .type = NL80211_SAR_TYPE_POWER,
91 .num_freq_ranges = (ARRAY_SIZE(ath10k_sar_freq_ranges)),
92 .freq_ranges = &ath10k_sar_freq_ranges[0],
93};
94
8d7aa6bc
MK
95#define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
96
97#define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
98#define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
99 ATH10K_MAC_FIRST_OFDM_RATE_IDX)
dcc33098
MK
100#define ath10k_g_rates (ath10k_rates + 0)
101#define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
102
5269c659
MSS
103#define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
104#define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
105
cd93b83a
PKC
106#define ath10k_wmi_legacy_rates ath10k_rates
107
486017cc
MK
108static bool ath10k_mac_bitrate_is_cck(int bitrate)
109{
110 switch (bitrate) {
111 case 10:
112 case 20:
113 case 55:
114 case 110:
115 return true;
116 }
117
118 return false;
119}
120
121static u8 ath10k_mac_bitrate_to_rate(int bitrate)
122{
123 return DIV_ROUND_UP(bitrate, 5) |
124 (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
125}
126
5528e032 127u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
4b7f353b 128 u8 hw_rate, bool cck)
5528e032
MK
129{
130 const struct ieee80211_rate *rate;
131 int i;
132
133 for (i = 0; i < sband->n_bitrates; i++) {
134 rate = &sband->bitrates[i];
135
4b7f353b
YL
136 if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
137 continue;
138
5528e032
MK
139 if (rate->hw_value == hw_rate)
140 return i;
141 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
142 rate->hw_value_short == hw_rate)
143 return i;
144 }
145
146 return 0;
147}
148
01cebe1c
MK
149u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
150 u32 bitrate)
151{
152 int i;
153
154 for (i = 0; i < sband->n_bitrates; i++)
155 if (sband->bitrates[i].bitrate == bitrate)
156 return i;
157
158 return 0;
159}
160
f279294e
S
161static int ath10k_mac_get_rate_hw_value(int bitrate)
162{
163 int i;
164 u8 hw_value_prefix = 0;
165
166 if (ath10k_mac_bitrate_is_cck(bitrate))
167 hw_value_prefix = WMI_RATE_PREAMBLE_CCK << 6;
168
34e141ee 169 for (i = 0; i < ARRAY_SIZE(ath10k_rates); i++) {
f279294e
S
170 if (ath10k_rates[i].bitrate == bitrate)
171 return hw_value_prefix | ath10k_rates[i].hw_value;
172 }
173
174 return -EINVAL;
175}
176
3ae54225
MK
177static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
178{
179 switch ((mcs_map >> (2 * nss)) & 0x3) {
180 case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
181 case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
182 case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
183 }
184 return 0;
185}
186
45c9abc0
MK
187static u32
188ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
189{
190 int nss;
191
192 for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
193 if (ht_mcs_mask[nss])
194 return nss + 1;
195
196 return 1;
197}
198
199static u32
200ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
201{
202 int nss;
203
204 for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
205 if (vht_mcs_mask[nss])
206 return nss + 1;
207
208 return 1;
209}
5e3dd157 210
7e247a9e
RM
211int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
212{
213 enum wmi_host_platform_type platform_type;
214 int ret;
215
216 if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
217 platform_type = WMI_HOST_PLATFORM_LOW_PERF;
218 else
219 platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
220
221 ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
222
223 if (ret && ret != -EOPNOTSUPP) {
224 ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
225 return ret;
226 }
227
228 return 0;
229}
230
5e3dd157
KV
231/**********/
232/* Crypto */
233/**********/
234
235static int ath10k_send_key(struct ath10k_vif *arvif,
236 struct ieee80211_key_conf *key,
237 enum set_key_cmd cmd,
370e5673 238 const u8 *macaddr, u32 flags)
5e3dd157 239{
7aa7a72a 240 struct ath10k *ar = arvif->ar;
5e3dd157
KV
241 struct wmi_vdev_install_key_arg arg = {
242 .vdev_id = arvif->vdev_id,
243 .key_idx = key->keyidx,
244 .key_len = key->keylen,
245 .key_data = key->key,
370e5673 246 .key_flags = flags,
5e3dd157
KV
247 .macaddr = macaddr,
248 };
249
548db54c
MK
250 lockdep_assert_held(&arvif->ar->conf_mutex);
251
5e3dd157
KV
252 switch (key->cipher) {
253 case WLAN_CIPHER_SUITE_CCMP:
7d94f862 254 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
e4e82e9a 255 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
5e3dd157
KV
256 break;
257 case WLAN_CIPHER_SUITE_TKIP:
7d94f862 258 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_TKIP];
5e3dd157
KV
259 arg.key_txmic_len = 8;
260 arg.key_rxmic_len = 8;
261 break;
262 case WLAN_CIPHER_SUITE_WEP40:
263 case WLAN_CIPHER_SUITE_WEP104:
7d94f862 264 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_WEP];
5e3dd157 265 break;
2ea9f12c 266 case WLAN_CIPHER_SUITE_CCMP_256:
7d94f862 267 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_CCM];
2ea9f12c
RM
268 break;
269 case WLAN_CIPHER_SUITE_GCMP:
270 case WLAN_CIPHER_SUITE_GCMP_256:
7d94f862 271 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_AES_GCM];
95a568c4 272 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
2ea9f12c
RM
273 break;
274 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
275 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
276 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
3cb10943 277 case WLAN_CIPHER_SUITE_AES_CMAC:
d7131c04
BM
278 WARN_ON(1);
279 return -EINVAL;
5e3dd157 280 default:
7aa7a72a 281 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
5e3dd157
KV
282 return -EOPNOTSUPP;
283 }
284
b9e284e5 285 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
ccec9038 286 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
ccec9038 287
5e3dd157 288 if (cmd == DISABLE_KEY) {
7d94f862 289 arg.key_cipher = ar->wmi_key_cipher[WMI_CIPHER_NONE];
5e3dd157
KV
290 arg.key_data = NULL;
291 }
292
293 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
294}
295
296static int ath10k_install_key(struct ath10k_vif *arvif,
297 struct ieee80211_key_conf *key,
298 enum set_key_cmd cmd,
370e5673 299 const u8 *macaddr, u32 flags)
5e3dd157
KV
300{
301 struct ath10k *ar = arvif->ar;
302 int ret;
8e9904f5 303 unsigned long time_left;
5e3dd157 304
548db54c
MK
305 lockdep_assert_held(&ar->conf_mutex);
306
16735d02 307 reinit_completion(&ar->install_key_done);
5e3dd157 308
ccec9038
DL
309 if (arvif->nohwcrypt)
310 return 1;
311
370e5673 312 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
5e3dd157
KV
313 if (ret)
314 return ret;
315
8e9904f5
NMG
316 time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
317 if (time_left == 0)
5e3dd157
KV
318 return -ETIMEDOUT;
319
320 return 0;
321}
322
323static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
324 const u8 *addr)
325{
326 struct ath10k *ar = arvif->ar;
327 struct ath10k_peer *peer;
328 int ret;
329 int i;
370e5673 330 u32 flags;
5e3dd157
KV
331
332 lockdep_assert_held(&ar->conf_mutex);
333
8674d909 334 if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
7c97b72a
PO
335 arvif->vif->type != NL80211_IFTYPE_ADHOC &&
336 arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
8674d909
MK
337 return -EINVAL;
338
5e3dd157
KV
339 spin_lock_bh(&ar->data_lock);
340 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
341 spin_unlock_bh(&ar->data_lock);
342
343 if (!peer)
344 return -ENOENT;
345
346 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
347 if (arvif->wep_keys[i] == NULL)
348 continue;
370e5673 349
8674d909
MK
350 switch (arvif->vif->type) {
351 case NL80211_IFTYPE_AP:
352 flags = WMI_KEY_PAIRWISE;
353
354 if (arvif->def_wep_key_idx == i)
355 flags |= WMI_KEY_TX_USAGE;
356
357 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
358 SET_KEY, addr, flags);
359 if (ret < 0)
360 return ret;
361 break;
362 case NL80211_IFTYPE_ADHOC:
363 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
364 SET_KEY, addr,
365 WMI_KEY_PAIRWISE);
366 if (ret < 0)
367 return ret;
368
369 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
370 SET_KEY, addr, WMI_KEY_GROUP);
371 if (ret < 0)
372 return ret;
373 break;
374 default:
375 WARN_ON(1);
376 return -EINVAL;
377 }
5e3dd157 378
ae167131 379 spin_lock_bh(&ar->data_lock);
5e3dd157 380 peer->keys[i] = arvif->wep_keys[i];
ae167131 381 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
382 }
383
ce90b271
MK
384 /* In some cases (notably with static WEP IBSS with multiple keys)
385 * multicast Tx becomes broken. Both pairwise and groupwise keys are
386 * installed already. Using WMI_KEY_TX_USAGE in different combinations
387 * didn't seem help. Using def_keyid vdev parameter seems to be
388 * effective so use that.
389 *
390 * FIXME: Revisit. Perhaps this can be done in a less hacky way.
391 */
8674d909
MK
392 if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
393 return 0;
394
ce90b271
MK
395 if (arvif->def_wep_key_idx == -1)
396 return 0;
397
398 ret = ath10k_wmi_vdev_set_param(arvif->ar,
399 arvif->vdev_id,
400 arvif->ar->wmi.vdev_param->def_keyid,
401 arvif->def_wep_key_idx);
402 if (ret) {
403 ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
404 arvif->vdev_id, ret);
405 return ret;
406 }
407
5e3dd157
KV
408 return 0;
409}
410
411static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
412 const u8 *addr)
413{
414 struct ath10k *ar = arvif->ar;
415 struct ath10k_peer *peer;
416 int first_errno = 0;
417 int ret;
418 int i;
370e5673 419 u32 flags = 0;
5e3dd157
KV
420
421 lockdep_assert_held(&ar->conf_mutex);
422
423 spin_lock_bh(&ar->data_lock);
424 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
425 spin_unlock_bh(&ar->data_lock);
426
427 if (!peer)
428 return -ENOENT;
429
430 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
431 if (peer->keys[i] == NULL)
432 continue;
433
627613f8 434 /* key flags are not required to delete the key */
5e3dd157 435 ret = ath10k_install_key(arvif, peer->keys[i],
370e5673 436 DISABLE_KEY, addr, flags);
ccec9038 437 if (ret < 0 && first_errno == 0)
5e3dd157
KV
438 first_errno = ret;
439
ccec9038 440 if (ret < 0)
7aa7a72a 441 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
5e3dd157
KV
442 i, ret);
443
ae167131 444 spin_lock_bh(&ar->data_lock);
5e3dd157 445 peer->keys[i] = NULL;
ae167131 446 spin_unlock_bh(&ar->data_lock);
5e3dd157
KV
447 }
448
449 return first_errno;
450}
451
504f6cdf
SM
452bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
453 u8 keyidx)
454{
455 struct ath10k_peer *peer;
456 int i;
457
458 lockdep_assert_held(&ar->data_lock);
459
460 /* We don't know which vdev this peer belongs to,
461 * since WMI doesn't give us that information.
462 *
463 * FIXME: multi-bss needs to be handled.
464 */
465 peer = ath10k_peer_find(ar, 0, addr);
466 if (!peer)
467 return false;
468
469 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
470 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
471 return true;
472 }
473
474 return false;
475}
476
5e3dd157
KV
477static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
478 struct ieee80211_key_conf *key)
479{
480 struct ath10k *ar = arvif->ar;
481 struct ath10k_peer *peer;
482 u8 addr[ETH_ALEN];
483 int first_errno = 0;
484 int ret;
485 int i;
370e5673 486 u32 flags = 0;
5e3dd157
KV
487
488 lockdep_assert_held(&ar->conf_mutex);
489
490 for (;;) {
491 /* since ath10k_install_key we can't hold data_lock all the
d6dfe25c
MR
492 * time, so we try to remove the keys incrementally
493 */
5e3dd157
KV
494 spin_lock_bh(&ar->data_lock);
495 i = 0;
496 list_for_each_entry(peer, &ar->peers, list) {
497 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
498 if (peer->keys[i] == key) {
b25f32cb 499 ether_addr_copy(addr, peer->addr);
5e3dd157
KV
500 peer->keys[i] = NULL;
501 break;
502 }
503 }
504
505 if (i < ARRAY_SIZE(peer->keys))
506 break;
507 }
508 spin_unlock_bh(&ar->data_lock);
509
510 if (i == ARRAY_SIZE(peer->keys))
511 break;
627613f8 512 /* key flags are not required to delete the key */
370e5673 513 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
ccec9038 514 if (ret < 0 && first_errno == 0)
5e3dd157
KV
515 first_errno = ret;
516
517 if (ret)
7aa7a72a 518 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
be6546fc 519 addr, ret);
5e3dd157
KV
520 }
521
522 return first_errno;
523}
524
ad325cb5
MK
525static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
526 struct ieee80211_key_conf *key)
527{
528 struct ath10k *ar = arvif->ar;
529 struct ath10k_peer *peer;
530 int ret;
531
532 lockdep_assert_held(&ar->conf_mutex);
533
534 list_for_each_entry(peer, &ar->peers, list) {
c178da58 535 if (ether_addr_equal(peer->addr, arvif->vif->addr))
ad325cb5
MK
536 continue;
537
c178da58 538 if (ether_addr_equal(peer->addr, arvif->bssid))
ad325cb5
MK
539 continue;
540
541 if (peer->keys[key->keyidx] == key)
542 continue;
543
544 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
545 arvif->vdev_id, key->keyidx);
546
547 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
548 if (ret) {
549 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
550 arvif->vdev_id, peer->addr, ret);
551 return ret;
552 }
553 }
554
555 return 0;
556}
557
5e3dd157
KV
558/*********************/
559/* General utilities */
560/*********************/
561
562static inline enum wmi_phy_mode
563chan_to_phymode(const struct cfg80211_chan_def *chandef)
564{
565 enum wmi_phy_mode phymode = MODE_UNKNOWN;
566
567 switch (chandef->chan->band) {
57fbcce3 568 case NL80211_BAND_2GHZ:
5e3dd157
KV
569 switch (chandef->width) {
570 case NL80211_CHAN_WIDTH_20_NOHT:
6faab127
PO
571 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
572 phymode = MODE_11B;
573 else
574 phymode = MODE_11G;
5e3dd157
KV
575 break;
576 case NL80211_CHAN_WIDTH_20:
577 phymode = MODE_11NG_HT20;
578 break;
579 case NL80211_CHAN_WIDTH_40:
580 phymode = MODE_11NG_HT40;
581 break;
df78a0c0 582 default:
5e3dd157
KV
583 phymode = MODE_UNKNOWN;
584 break;
585 }
586 break;
57fbcce3 587 case NL80211_BAND_5GHZ:
5e3dd157
KV
588 switch (chandef->width) {
589 case NL80211_CHAN_WIDTH_20_NOHT:
590 phymode = MODE_11A;
591 break;
592 case NL80211_CHAN_WIDTH_20:
593 phymode = MODE_11NA_HT20;
594 break;
595 case NL80211_CHAN_WIDTH_40:
596 phymode = MODE_11NA_HT40;
597 break;
598 case NL80211_CHAN_WIDTH_80:
599 phymode = MODE_11AC_VHT80;
600 break;
bc1efd73
SG
601 case NL80211_CHAN_WIDTH_160:
602 phymode = MODE_11AC_VHT160;
603 break;
604 case NL80211_CHAN_WIDTH_80P80:
605 phymode = MODE_11AC_VHT80_80;
606 break;
df78a0c0 607 default:
5e3dd157
KV
608 phymode = MODE_UNKNOWN;
609 break;
610 }
611 break;
612 default:
613 break;
614 }
615
616 WARN_ON(phymode == MODE_UNKNOWN);
617 return phymode;
618}
619
620static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
621{
622/*
623 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
624 * 0 for no restriction
625 * 1 for 1/4 us
626 * 2 for 1/2 us
627 * 3 for 1 us
628 * 4 for 2 us
629 * 5 for 4 us
630 * 6 for 8 us
631 * 7 for 16 us
632 */
633 switch (mpdudensity) {
634 case 0:
635 return 0;
636 case 1:
637 case 2:
638 case 3:
639 /* Our lower layer calculations limit our precision to
d6dfe25c
MR
640 * 1 microsecond
641 */
5e3dd157
KV
642 return 1;
643 case 4:
644 return 2;
645 case 5:
646 return 4;
647 case 6:
648 return 8;
649 case 7:
650 return 16;
651 default:
652 return 0;
653 }
654}
655
500ff9f9
MK
656int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
657 struct cfg80211_chan_def *def)
658{
659 struct ieee80211_chanctx_conf *conf;
660
661 rcu_read_lock();
662 conf = rcu_dereference(vif->chanctx_conf);
663 if (!conf) {
664 rcu_read_unlock();
665 return -ENOENT;
666 }
667
668 *def = conf->def;
669 rcu_read_unlock();
670
671 return 0;
672}
673
674static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
675 struct ieee80211_chanctx_conf *conf,
676 void *data)
677{
678 int *num = data;
679
680 (*num)++;
681}
682
683static int ath10k_mac_num_chanctxs(struct ath10k *ar)
684{
685 int num = 0;
686
687 ieee80211_iter_chan_contexts_atomic(ar->hw,
688 ath10k_mac_num_chanctxs_iter,
689 &num);
690
691 return num;
692}
693
694static void
695ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
696 struct ieee80211_chanctx_conf *conf,
697 void *data)
698{
699 struct cfg80211_chan_def **def = data;
700
701 *def = &conf->def;
702}
703
c6f537a1
DR
704static void ath10k_wait_for_peer_delete_done(struct ath10k *ar, u32 vdev_id,
705 const u8 *addr)
706{
707 unsigned long time_left;
708 int ret;
709
710 if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
711 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
712 if (ret) {
713 ath10k_warn(ar, "failed wait for peer deleted");
714 return;
715 }
716
717 time_left = wait_for_completion_timeout(&ar->peer_delete_done,
718 5 * HZ);
719 if (!time_left)
720 ath10k_warn(ar, "Timeout in receiving peer delete response\n");
721 }
722}
723
6942726f
MK
724static int ath10k_peer_create(struct ath10k *ar,
725 struct ieee80211_vif *vif,
726 struct ieee80211_sta *sta,
727 u32 vdev_id,
728 const u8 *addr,
7390ed34 729 enum wmi_peer_type peer_type)
5e3dd157 730{
e04cafbc 731 struct ath10k_vif *arvif;
6942726f 732 struct ath10k_peer *peer;
e04cafbc 733 int num_peers = 0;
5e3dd157
KV
734 int ret;
735
736 lockdep_assert_held(&ar->conf_mutex);
737
e04cafbc
MK
738 num_peers = ar->num_peers;
739
740 /* Each vdev consumes a peer entry as well */
741 list_for_each_entry(arvif, &ar->arvifs, list)
742 num_peers++;
743
744 if (num_peers >= ar->max_num_peers)
cfd1061e
MK
745 return -ENOBUFS;
746
7390ed34 747 ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
479398b0 748 if (ret) {
7aa7a72a 749 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
69244e56 750 addr, vdev_id, ret);
5e3dd157 751 return ret;
479398b0 752 }
5e3dd157
KV
753
754 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
479398b0 755 if (ret) {
7aa7a72a 756 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
69244e56 757 addr, vdev_id, ret);
5e3dd157 758 return ret;
479398b0 759 }
292a753d 760
6942726f
MK
761 spin_lock_bh(&ar->data_lock);
762
763 peer = ath10k_peer_find(ar, vdev_id, addr);
764 if (!peer) {
fee48cf8 765 spin_unlock_bh(&ar->data_lock);
6942726f
MK
766 ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
767 addr, vdev_id);
c6f537a1 768 ath10k_wait_for_peer_delete_done(ar, vdev_id, addr);
6942726f
MK
769 return -ENOENT;
770 }
771
772 peer->vif = vif;
773 peer->sta = sta;
774
775 spin_unlock_bh(&ar->data_lock);
776
0e759f36 777 ar->num_peers++;
5e3dd157
KV
778
779 return 0;
780}
781
5a13e76e
KV
782static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
783{
784 struct ath10k *ar = arvif->ar;
785 u32 param;
786 int ret;
787
788 param = ar->wmi.pdev_param->sta_kickout_th;
789 ret = ath10k_wmi_pdev_set_param(ar, param,
790 ATH10K_KICKOUT_THRESHOLD);
791 if (ret) {
7aa7a72a 792 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
69244e56 793 arvif->vdev_id, ret);
5a13e76e
KV
794 return ret;
795 }
796
797 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
798 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
799 ATH10K_KEEPALIVE_MIN_IDLE);
800 if (ret) {
7aa7a72a 801 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
69244e56 802 arvif->vdev_id, ret);
5a13e76e
KV
803 return ret;
804 }
805
806 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
807 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
808 ATH10K_KEEPALIVE_MAX_IDLE);
809 if (ret) {
7aa7a72a 810 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
69244e56 811 arvif->vdev_id, ret);
5a13e76e
KV
812 return ret;
813 }
814
815 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
816 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
817 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
818 if (ret) {
7aa7a72a 819 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
69244e56 820 arvif->vdev_id, ret);
5a13e76e
KV
821 return ret;
822 }
823
824 return 0;
825}
826
acab6400 827static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
424121c3 828{
6d1506e7
BM
829 struct ath10k *ar = arvif->ar;
830 u32 vdev_param;
831
6d1506e7
BM
832 vdev_param = ar->wmi.vdev_param->rts_threshold;
833 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
424121c3
MK
834}
835
5e3dd157
KV
836static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
837{
838 int ret;
839
840 lockdep_assert_held(&ar->conf_mutex);
841
842 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
843 if (ret)
844 return ret;
845
846 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
847 if (ret)
848 return ret;
849
c6f537a1
DR
850 if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
851 unsigned long time_left;
852
853 time_left = wait_for_completion_timeout
854 (&ar->peer_delete_done, 5 * HZ);
855
856 if (!time_left) {
857 ath10k_warn(ar, "Timeout in receiving peer delete response\n");
858 return -ETIMEDOUT;
859 }
860 }
861
0e759f36 862 ar->num_peers--;
0e759f36 863
5e3dd157
KV
864 return 0;
865}
866
867static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
868{
869 struct ath10k_peer *peer, *tmp;
6942726f 870 int peer_id;
6d68f790 871 int i;
5e3dd157
KV
872
873 lockdep_assert_held(&ar->conf_mutex);
874
875 spin_lock_bh(&ar->data_lock);
876 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
877 if (peer->vdev_id != vdev_id)
878 continue;
879
7aa7a72a 880 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
5e3dd157
KV
881 peer->addr, vdev_id);
882
6942726f
MK
883 for_each_set_bit(peer_id, peer->peer_ids,
884 ATH10K_MAX_NUM_PEER_IDS) {
885 ar->peer_map[peer_id] = NULL;
886 }
887
6d68f790
BG
888 /* Double check that peer is properly un-referenced from
889 * the peer_map
890 */
891 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
892 if (ar->peer_map[i] == peer) {
75b34800 893 ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %pK idx %d)\n",
6d68f790
BG
894 peer->addr, peer, i);
895 ar->peer_map[i] = NULL;
896 }
897 }
898
5e3dd157
KV
899 list_del(&peer->list);
900 kfree(peer);
0e759f36 901 ar->num_peers--;
5e3dd157
KV
902 }
903 spin_unlock_bh(&ar->data_lock);
904}
905
a96d7745
MK
906static void ath10k_peer_cleanup_all(struct ath10k *ar)
907{
908 struct ath10k_peer *peer, *tmp;
6d68f790 909 int i;
a96d7745
MK
910
911 lockdep_assert_held(&ar->conf_mutex);
912
913 spin_lock_bh(&ar->data_lock);
914 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
915 list_del(&peer->list);
916 kfree(peer);
917 }
6d68f790
BG
918
919 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
920 ar->peer_map[i] = NULL;
921
a96d7745 922 spin_unlock_bh(&ar->data_lock);
292a753d
MK
923
924 ar->num_peers = 0;
cfd1061e 925 ar->num_stations = 0;
a96d7745
MK
926}
927
75d85fd9
MP
928static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
929 struct ieee80211_sta *sta,
930 enum wmi_tdls_peer_state state)
931{
932 int ret;
933 struct wmi_tdls_peer_update_cmd_arg arg = {};
934 struct wmi_tdls_peer_capab_arg cap = {};
935 struct wmi_channel_arg chan_arg = {};
936
937 lockdep_assert_held(&ar->conf_mutex);
938
939 arg.vdev_id = vdev_id;
940 arg.peer_state = state;
941 ether_addr_copy(arg.addr, sta->addr);
942
943 cap.peer_max_sp = sta->max_sp;
944 cap.peer_uapsd_queues = sta->uapsd_queues;
945
946 if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
947 !sta->tdls_initiator)
948 cap.is_peer_responder = 1;
949
950 ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
951 if (ret) {
952 ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
953 arg.addr, vdev_id, ret);
954 return ret;
955 }
956
957 return 0;
958}
959
5e3dd157
KV
960/************************/
961/* Interface management */
962/************************/
963
64badcb6
MK
964void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
965{
966 struct ath10k *ar = arvif->ar;
967
968 lockdep_assert_held(&ar->data_lock);
969
970 if (!arvif->beacon)
971 return;
972
973 if (!arvif->beacon_buf)
974 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
975 arvif->beacon->len, DMA_TO_DEVICE);
976
af21319f
MK
977 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
978 arvif->beacon_state != ATH10K_BEACON_SENT))
979 return;
980
64badcb6
MK
981 dev_kfree_skb_any(arvif->beacon);
982
983 arvif->beacon = NULL;
af21319f 984 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
64badcb6
MK
985}
986
987static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
988{
989 struct ath10k *ar = arvif->ar;
990
991 lockdep_assert_held(&ar->data_lock);
992
993 ath10k_mac_vif_beacon_free(arvif);
994
995 if (arvif->beacon_buf) {
4da8bc2c
AS
996 if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
997 kfree(arvif->beacon_buf);
998 else
999 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
1000 arvif->beacon_buf,
1001 arvif->beacon_paddr);
64badcb6
MK
1002 arvif->beacon_buf = NULL;
1003 }
1004}
1005
5e3dd157
KV
1006static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
1007{
8e9904f5 1008 unsigned long time_left;
5e3dd157 1009
548db54c
MK
1010 lockdep_assert_held(&ar->conf_mutex);
1011
7962b0d8
MK
1012 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
1013 return -ESHUTDOWN;
1014
8e9904f5
NMG
1015 time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
1016 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
1017 if (time_left == 0)
5e3dd157
KV
1018 return -ETIMEDOUT;
1019
833fd34d 1020 return ar->last_wmi_vdev_start_status;
5e3dd157
KV
1021}
1022
1bbc0975 1023static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
5e3dd157 1024{
500ff9f9 1025 struct cfg80211_chan_def *chandef = NULL;
19be9e9a 1026 struct ieee80211_channel *channel = NULL;
5e3dd157 1027 struct wmi_vdev_start_request_arg arg = {};
5e3dd157
KV
1028 int ret = 0;
1029
1030 lockdep_assert_held(&ar->conf_mutex);
1031
500ff9f9
MK
1032 ieee80211_iter_chan_contexts_atomic(ar->hw,
1033 ath10k_mac_get_any_chandef_iter,
1034 &chandef);
1035 if (WARN_ON_ONCE(!chandef))
1036 return -ENOENT;
1037
1038 channel = chandef->chan;
1039
5e3dd157
KV
1040 arg.vdev_id = vdev_id;
1041 arg.channel.freq = channel->center_freq;
c930f744 1042 arg.channel.band_center_freq1 = chandef->center_freq1;
bc1efd73 1043 arg.channel.band_center_freq2 = chandef->center_freq2;
5e3dd157
KV
1044
1045 /* TODO setup this dynamically, what in case we
d6dfe25c
MR
1046 * don't have any vifs?
1047 */
c930f744 1048 arg.channel.mode = chan_to_phymode(chandef);
e8a50f8b
MP
1049 arg.channel.chan_radar =
1050 !!(channel->flags & IEEE80211_CHAN_RADAR);
5e3dd157 1051
89c5c843 1052 arg.channel.min_power = 0;
02256930
MK
1053 arg.channel.max_power = channel->max_power * 2;
1054 arg.channel.max_reg_power = channel->max_reg_power * 2;
1055 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157 1056
7962b0d8 1057 reinit_completion(&ar->vdev_setup_done);
fe36e70f 1058 reinit_completion(&ar->vdev_delete_done);
7962b0d8 1059
5e3dd157
KV
1060 ret = ath10k_wmi_vdev_start(ar, &arg);
1061 if (ret) {
7aa7a72a 1062 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
69244e56 1063 vdev_id, ret);
5e3dd157
KV
1064 return ret;
1065 }
1066
1067 ret = ath10k_vdev_setup_sync(ar);
1068 if (ret) {
60028a81 1069 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
69244e56 1070 vdev_id, ret);
5e3dd157
KV
1071 return ret;
1072 }
1073
1074 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
1075 if (ret) {
7aa7a72a 1076 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
69244e56 1077 vdev_id, ret);
5e3dd157
KV
1078 goto vdev_stop;
1079 }
1080
1081 ar->monitor_vdev_id = vdev_id;
5e3dd157 1082
7aa7a72a 1083 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1bbc0975 1084 ar->monitor_vdev_id);
5e3dd157
KV
1085 return 0;
1086
1087vdev_stop:
1088 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1089 if (ret)
7aa7a72a 1090 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
69244e56 1091 ar->monitor_vdev_id, ret);
5e3dd157
KV
1092
1093 return ret;
1094}
1095
1bbc0975 1096static int ath10k_monitor_vdev_stop(struct ath10k *ar)
5e3dd157
KV
1097{
1098 int ret = 0;
1099
1100 lockdep_assert_held(&ar->conf_mutex);
1101
52fa0191
MP
1102 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1103 if (ret)
7aa7a72a 1104 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
69244e56 1105 ar->monitor_vdev_id, ret);
5e3dd157 1106
7962b0d8 1107 reinit_completion(&ar->vdev_setup_done);
fe36e70f 1108 reinit_completion(&ar->vdev_delete_done);
7962b0d8 1109
5e3dd157
KV
1110 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1111 if (ret)
a67bcec3 1112 ath10k_warn(ar, "failed to request monitor vdev %i stop: %d\n",
69244e56 1113 ar->monitor_vdev_id, ret);
5e3dd157
KV
1114
1115 ret = ath10k_vdev_setup_sync(ar);
1116 if (ret)
60028a81 1117 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
69244e56 1118 ar->monitor_vdev_id, ret);
5e3dd157 1119
7aa7a72a 1120 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1bbc0975 1121 ar->monitor_vdev_id);
5e3dd157
KV
1122 return ret;
1123}
1124
1bbc0975 1125static int ath10k_monitor_vdev_create(struct ath10k *ar)
5e3dd157
KV
1126{
1127 int bit, ret = 0;
1128
1129 lockdep_assert_held(&ar->conf_mutex);
1130
a9aefb3b 1131 if (ar->free_vdev_map == 0) {
7aa7a72a 1132 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
5e3dd157
KV
1133 return -ENOMEM;
1134 }
1135
16c11176 1136 bit = __ffs64(ar->free_vdev_map);
a9aefb3b 1137
16c11176 1138 ar->monitor_vdev_id = bit;
5e3dd157
KV
1139
1140 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1141 WMI_VDEV_TYPE_MONITOR,
1142 0, ar->mac_addr);
1143 if (ret) {
7aa7a72a 1144 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
69244e56 1145 ar->monitor_vdev_id, ret);
a9aefb3b 1146 return ret;
5e3dd157
KV
1147 }
1148
16c11176 1149 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
7aa7a72a 1150 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
5e3dd157
KV
1151 ar->monitor_vdev_id);
1152
5e3dd157 1153 return 0;
5e3dd157
KV
1154}
1155
1bbc0975 1156static int ath10k_monitor_vdev_delete(struct ath10k *ar)
5e3dd157
KV
1157{
1158 int ret = 0;
1159
1160 lockdep_assert_held(&ar->conf_mutex);
1161
5e3dd157
KV
1162 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1163 if (ret) {
7aa7a72a 1164 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
69244e56 1165 ar->monitor_vdev_id, ret);
5e3dd157
KV
1166 return ret;
1167 }
1168
16c11176 1169 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
5e3dd157 1170
7aa7a72a 1171 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
5e3dd157
KV
1172 ar->monitor_vdev_id);
1173 return ret;
1174}
1175
1bbc0975
MK
1176static int ath10k_monitor_start(struct ath10k *ar)
1177{
1178 int ret;
1179
1180 lockdep_assert_held(&ar->conf_mutex);
1181
1bbc0975
MK
1182 ret = ath10k_monitor_vdev_create(ar);
1183 if (ret) {
7aa7a72a 1184 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1bbc0975
MK
1185 return ret;
1186 }
1187
1188 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1189 if (ret) {
7aa7a72a 1190 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1bbc0975
MK
1191 ath10k_monitor_vdev_delete(ar);
1192 return ret;
1193 }
1194
1195 ar->monitor_started = true;
7aa7a72a 1196 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1bbc0975
MK
1197
1198 return 0;
1199}
1200
1933747f 1201static int ath10k_monitor_stop(struct ath10k *ar)
1bbc0975
MK
1202{
1203 int ret;
1204
1205 lockdep_assert_held(&ar->conf_mutex);
1206
1bbc0975 1207 ret = ath10k_monitor_vdev_stop(ar);
1933747f 1208 if (ret) {
7aa7a72a 1209 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1933747f
MK
1210 return ret;
1211 }
1bbc0975
MK
1212
1213 ret = ath10k_monitor_vdev_delete(ar);
1933747f 1214 if (ret) {
7aa7a72a 1215 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1933747f
MK
1216 return ret;
1217 }
1bbc0975
MK
1218
1219 ar->monitor_started = false;
7aa7a72a 1220 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1933747f
MK
1221
1222 return 0;
1223}
1224
500ff9f9
MK
1225static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1226{
1227 int num_ctx;
1228
1229 /* At least one chanctx is required to derive a channel to start
1230 * monitor vdev on.
1231 */
1232 num_ctx = ath10k_mac_num_chanctxs(ar);
1233 if (num_ctx == 0)
1234 return false;
1235
1236 /* If there's already an existing special monitor interface then don't
1237 * bother creating another monitor vdev.
1238 */
1239 if (ar->monitor_arvif)
1240 return false;
1241
1242 return ar->monitor ||
705d7aa0
MR
1243 (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
1244 ar->running_fw->fw_file.fw_features) &&
1245 (ar->filter_flags & FIF_OTHER_BSS)) ||
500ff9f9
MK
1246 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1247}
1248
1249static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1250{
1251 int num_ctx;
1252
1253 num_ctx = ath10k_mac_num_chanctxs(ar);
1254
1255 /* FIXME: Current interface combinations and cfg80211/mac80211 code
1256 * shouldn't allow this but make sure to prevent handling the following
1257 * case anyway since multi-channel DFS hasn't been tested at all.
1258 */
1259 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1260 return false;
1261
1262 return true;
1263}
1264
1933747f
MK
1265static int ath10k_monitor_recalc(struct ath10k *ar)
1266{
500ff9f9
MK
1267 bool needed;
1268 bool allowed;
1269 int ret;
1933747f
MK
1270
1271 lockdep_assert_held(&ar->conf_mutex);
1272
500ff9f9
MK
1273 needed = ath10k_mac_monitor_vdev_is_needed(ar);
1274 allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1933747f
MK
1275
1276 ath10k_dbg(ar, ATH10K_DBG_MAC,
500ff9f9
MK
1277 "mac monitor recalc started? %d needed? %d allowed? %d\n",
1278 ar->monitor_started, needed, allowed);
1933747f 1279
500ff9f9
MK
1280 if (WARN_ON(needed && !allowed)) {
1281 if (ar->monitor_started) {
1282 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1283
1284 ret = ath10k_monitor_stop(ar);
1285 if (ret)
2a995088
KV
1286 ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1287 ret);
500ff9f9
MK
1288 /* not serious */
1289 }
1933747f 1290
500ff9f9
MK
1291 return -EPERM;
1292 }
1933747f 1293
500ff9f9 1294 if (needed == ar->monitor_started)
1933747f
MK
1295 return 0;
1296
500ff9f9 1297 if (needed)
1933747f 1298 return ath10k_monitor_start(ar);
500ff9f9
MK
1299 else
1300 return ath10k_monitor_stop(ar);
1bbc0975
MK
1301}
1302
7cfe0455
BM
1303static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
1304{
1305 struct ath10k *ar = arvif->ar;
1306
1307 lockdep_assert_held(&ar->conf_mutex);
1308
1309 if (!arvif->is_started) {
1310 ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
1311 return false;
1312 }
1313
1314 return true;
1315}
1316
1317static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
1318{
1319 struct ath10k *ar = arvif->ar;
1320 u32 vdev_param;
1321
1322 lockdep_assert_held(&ar->conf_mutex);
1323
1324 vdev_param = ar->wmi.vdev_param->protection_mode;
1325
1326 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
1327 arvif->vdev_id, arvif->use_cts_prot);
1328
1329 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1330 arvif->use_cts_prot ? 1 : 0);
1331}
1332
e81bd104
MK
1333static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1334{
1335 struct ath10k *ar = arvif->ar;
1336 u32 vdev_param, rts_cts = 0;
1337
1338 lockdep_assert_held(&ar->conf_mutex);
1339
1340 vdev_param = ar->wmi.vdev_param->enable_rtscts;
1341
9a5ab0f4 1342 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
e81bd104
MK
1343
1344 if (arvif->num_legacy_stations > 0)
1345 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1346 WMI_RTSCTS_PROFILE);
9a5ab0f4
RM
1347 else
1348 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1349 WMI_RTSCTS_PROFILE);
e81bd104 1350
86176901
BM
1351 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1352 arvif->vdev_id, rts_cts);
1353
e81bd104
MK
1354 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1355 rts_cts);
1356}
1357
e8a50f8b
MP
1358static int ath10k_start_cac(struct ath10k *ar)
1359{
1360 int ret;
1361
1362 lockdep_assert_held(&ar->conf_mutex);
1363
1364 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1365
1933747f 1366 ret = ath10k_monitor_recalc(ar);
e8a50f8b 1367 if (ret) {
7aa7a72a 1368 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
e8a50f8b
MP
1369 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1370 return ret;
1371 }
1372
7aa7a72a 1373 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
e8a50f8b
MP
1374 ar->monitor_vdev_id);
1375
1376 return 0;
1377}
1378
1379static int ath10k_stop_cac(struct ath10k *ar)
1380{
1381 lockdep_assert_held(&ar->conf_mutex);
1382
1383 /* CAC is not running - do nothing */
1384 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1385 return 0;
1386
e8a50f8b 1387 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1bbc0975 1388 ath10k_monitor_stop(ar);
e8a50f8b 1389
7aa7a72a 1390 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
e8a50f8b
MP
1391
1392 return 0;
1393}
1394
500ff9f9
MK
1395static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1396 struct ieee80211_chanctx_conf *conf,
1397 void *data)
1398{
1399 bool *ret = data;
1400
1401 if (!*ret && conf->radar_enabled)
1402 *ret = true;
1403}
1404
1405static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1406{
1407 bool has_radar = false;
1408
1409 ieee80211_iter_chan_contexts_atomic(ar->hw,
1410 ath10k_mac_has_radar_iter,
1411 &has_radar);
1412
1413 return has_radar;
1414}
1415
d650097b 1416static void ath10k_recalc_radar_detection(struct ath10k *ar)
e8a50f8b 1417{
e8a50f8b
MP
1418 int ret;
1419
1420 lockdep_assert_held(&ar->conf_mutex);
1421
e8a50f8b
MP
1422 ath10k_stop_cac(ar);
1423
500ff9f9 1424 if (!ath10k_mac_has_radar_enabled(ar))
e8a50f8b
MP
1425 return;
1426
d650097b 1427 if (ar->num_started_vdevs > 0)
e8a50f8b
MP
1428 return;
1429
1430 ret = ath10k_start_cac(ar);
1431 if (ret) {
1432 /*
1433 * Not possible to start CAC on current channel so starting
1434 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1435 * by indicating that radar was detected.
1436 */
7aa7a72a 1437 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
e8a50f8b
MP
1438 ieee80211_radar_detected(ar->hw);
1439 }
1440}
1441
822b7e0b
VT
1442static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1443{
1444 struct ath10k *ar = arvif->ar;
1445 int ret;
1446
1447 lockdep_assert_held(&ar->conf_mutex);
1448
1449 reinit_completion(&ar->vdev_setup_done);
fe36e70f 1450 reinit_completion(&ar->vdev_delete_done);
822b7e0b
VT
1451
1452 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1453 if (ret) {
1454 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1455 arvif->vdev_id, ret);
1456 return ret;
1457 }
1458
1459 ret = ath10k_vdev_setup_sync(ar);
1460 if (ret) {
23de5797 1461 ath10k_warn(ar, "failed to synchronize setup for vdev %i: %d\n",
822b7e0b
VT
1462 arvif->vdev_id, ret);
1463 return ret;
1464 }
1465
1466 WARN_ON(ar->num_started_vdevs == 0);
1467
1468 if (ar->num_started_vdevs != 0) {
1469 ar->num_started_vdevs--;
1470 ath10k_recalc_radar_detection(ar);
1471 }
1472
1473 return ret;
1474}
1475
500ff9f9
MK
1476static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1477 const struct cfg80211_chan_def *chandef,
1478 bool restart)
72654fa7
MK
1479{
1480 struct ath10k *ar = arvif->ar;
72654fa7
MK
1481 struct wmi_vdev_start_request_arg arg = {};
1482 int ret = 0;
1483
1484 lockdep_assert_held(&ar->conf_mutex);
1485
1486 reinit_completion(&ar->vdev_setup_done);
fe36e70f 1487 reinit_completion(&ar->vdev_delete_done);
72654fa7
MK
1488
1489 arg.vdev_id = arvif->vdev_id;
1490 arg.dtim_period = arvif->dtim_period;
1491 arg.bcn_intval = arvif->beacon_interval;
1492
1493 arg.channel.freq = chandef->chan->center_freq;
1494 arg.channel.band_center_freq1 = chandef->center_freq1;
bc1efd73 1495 arg.channel.band_center_freq2 = chandef->center_freq2;
72654fa7
MK
1496 arg.channel.mode = chan_to_phymode(chandef);
1497
1498 arg.channel.min_power = 0;
1499 arg.channel.max_power = chandef->chan->max_power * 2;
1500 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1501 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1502
1503 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1504 arg.ssid = arvif->u.ap.ssid;
1505 arg.ssid_len = arvif->u.ap.ssid_len;
1506 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1507
1508 /* For now allow DFS for AP mode */
1509 arg.channel.chan_radar =
1510 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1511 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1512 arg.ssid = arvif->vif->bss_conf.ssid;
1513 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1514 }
1515
7aa7a72a 1516 ath10k_dbg(ar, ATH10K_DBG_MAC,
72654fa7
MK
1517 "mac vdev %d start center_freq %d phymode %s\n",
1518 arg.vdev_id, arg.channel.freq,
1519 ath10k_wmi_phymode_str(arg.channel.mode));
1520
dc55e307
MK
1521 if (restart)
1522 ret = ath10k_wmi_vdev_restart(ar, &arg);
1523 else
1524 ret = ath10k_wmi_vdev_start(ar, &arg);
1525
72654fa7 1526 if (ret) {
7aa7a72a 1527 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
72654fa7
MK
1528 arg.vdev_id, ret);
1529 return ret;
1530 }
1531
1532 ret = ath10k_vdev_setup_sync(ar);
1533 if (ret) {
60028a81
BG
1534 ath10k_warn(ar,
1535 "failed to synchronize setup for vdev %i restart %d: %d\n",
1536 arg.vdev_id, restart, ret);
72654fa7
MK
1537 return ret;
1538 }
1539
d650097b
MK
1540 ar->num_started_vdevs++;
1541 ath10k_recalc_radar_detection(ar);
1542
72654fa7
MK
1543 return ret;
1544}
1545
500ff9f9
MK
1546static int ath10k_vdev_start(struct ath10k_vif *arvif,
1547 const struct cfg80211_chan_def *def)
dc55e307 1548{
500ff9f9 1549 return ath10k_vdev_start_restart(arvif, def, false);
dc55e307
MK
1550}
1551
500ff9f9
MK
1552static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1553 const struct cfg80211_chan_def *def)
72654fa7 1554{
500ff9f9 1555 return ath10k_vdev_start_restart(arvif, def, true);
72654fa7
MK
1556}
1557
fbb8f1b7
MK
1558static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1559 struct sk_buff *bcn)
1560{
1561 struct ath10k *ar = arvif->ar;
1562 struct ieee80211_mgmt *mgmt;
1563 const u8 *p2p_ie;
1564 int ret;
1565
08c27be1 1566 if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
fbb8f1b7
MK
1567 return 0;
1568
1569 mgmt = (void *)bcn->data;
1570 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1571 mgmt->u.beacon.variable,
1572 bcn->len - (mgmt->u.beacon.variable -
1573 bcn->data));
1574 if (!p2p_ie)
1575 return -ENOENT;
1576
1577 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1578 if (ret) {
1579 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1580 arvif->vdev_id, ret);
1581 return ret;
1582 }
1583
1584 return 0;
1585}
1586
1587static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1588 u8 oui_type, size_t ie_offset)
1589{
1590 size_t len;
1591 const u8 *next;
1592 const u8 *end;
1593 u8 *ie;
1594
1595 if (WARN_ON(skb->len < ie_offset))
1596 return -EINVAL;
1597
1598 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1599 skb->data + ie_offset,
1600 skb->len - ie_offset);
1601 if (!ie)
1602 return -ENOENT;
1603
1604 len = ie[1] + 2;
1605 end = skb->data + skb->len;
1606 next = ie + len;
1607
1608 if (WARN_ON(next > end))
1609 return -EINVAL;
1610
1611 memmove(ie, next, end - next);
1612 skb_trim(skb, skb->len - len);
1613
1614 return 0;
1615}
1616
1617static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1618{
1619 struct ath10k *ar = arvif->ar;
1620 struct ieee80211_hw *hw = ar->hw;
1621 struct ieee80211_vif *vif = arvif->vif;
1622 struct ieee80211_mutable_offsets offs = {};
1623 struct sk_buff *bcn;
1624 int ret;
1625
1626 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1627 return 0;
1628
81a9a17d
MK
1629 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1630 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1631 return 0;
1632
fbb8f1b7
MK
1633 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1634 if (!bcn) {
1635 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1636 return -EPERM;
1637 }
1638
1639 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1640 if (ret) {
1641 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1642 kfree_skb(bcn);
1643 return ret;
1644 }
1645
1646 /* P2P IE is inserted by firmware automatically (as configured above)
1647 * so remove it from the base beacon template to avoid duplicate P2P
1648 * IEs in beacon frames.
1649 */
1650 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1651 offsetof(struct ieee80211_mgmt,
1652 u.beacon.variable));
1653
1654 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1655 0, NULL, 0);
1656 kfree_skb(bcn);
1657
1658 if (ret) {
1659 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1660 ret);
1661 return ret;
1662 }
1663
1664 return 0;
1665}
1666
1667static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1668{
1669 struct ath10k *ar = arvif->ar;
1670 struct ieee80211_hw *hw = ar->hw;
1671 struct ieee80211_vif *vif = arvif->vif;
1672 struct sk_buff *prb;
1673 int ret;
1674
1675 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1676 return 0;
1677
81a9a17d
MK
1678 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1679 return 0;
1680
97354f2c
SV
1681 /* For mesh, probe response and beacon share the same template */
1682 if (ieee80211_vif_is_mesh(vif))
1683 return 0;
1684
fbb8f1b7
MK
1685 prb = ieee80211_proberesp_get(hw, vif);
1686 if (!prb) {
1687 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1688 return -EPERM;
1689 }
1690
1691 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1692 kfree_skb(prb);
1693
1694 if (ret) {
1695 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1696 ret);
1697 return ret;
1698 }
1699
1700 return 0;
1701}
1702
500ff9f9
MK
1703static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1704{
1705 struct ath10k *ar = arvif->ar;
1706 struct cfg80211_chan_def def;
1707 int ret;
1708
1709 /* When originally vdev is started during assign_vif_chanctx() some
1710 * information is missing, notably SSID. Firmware revisions with beacon
1711 * offloading require the SSID to be provided during vdev (re)start to
1712 * handle hidden SSID properly.
1713 *
1714 * Vdev restart must be done after vdev has been both started and
1715 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1716 * deliver vdev restart response event causing timeouts during vdev
1717 * syncing in ath10k.
1718 *
1719 * Note: The vdev down/up and template reinstallation could be skipped
1720 * since only wmi-tlv firmware are known to have beacon offload and
1721 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1722 * response delivery. It's probably more robust to keep it as is.
1723 */
1724 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1725 return 0;
1726
1727 if (WARN_ON(!arvif->is_started))
1728 return -EINVAL;
1729
1730 if (WARN_ON(!arvif->is_up))
1731 return -EINVAL;
1732
1733 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1734 return -EINVAL;
1735
1736 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1737 if (ret) {
1738 ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1739 arvif->vdev_id, ret);
1740 return ret;
1741 }
1742
1743 /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1744 * firmware will crash upon vdev up.
1745 */
1746
1747 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1748 if (ret) {
1749 ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1750 return ret;
1751 }
1752
1753 ret = ath10k_mac_setup_prb_tmpl(arvif);
1754 if (ret) {
1755 ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1756 return ret;
1757 }
1758
1759 ret = ath10k_vdev_restart(arvif, &def);
1760 if (ret) {
1761 ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1762 arvif->vdev_id, ret);
1763 return ret;
1764 }
1765
1766 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1767 arvif->bssid);
1768 if (ret) {
1769 ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1770 arvif->vdev_id, ret);
1771 return ret;
1772 }
1773
1774 return 0;
1775}
1776
5e3dd157 1777static void ath10k_control_beaconing(struct ath10k_vif *arvif,
5b07e07f 1778 struct ieee80211_bss_conf *info)
5e3dd157 1779{
7aa7a72a 1780 struct ath10k *ar = arvif->ar;
5e3dd157
KV
1781 int ret = 0;
1782
548db54c
MK
1783 lockdep_assert_held(&arvif->ar->conf_mutex);
1784
5e3dd157 1785 if (!info->enable_beacon) {
500ff9f9
MK
1786 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1787 if (ret)
1788 ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1789 arvif->vdev_id, ret);
c930f744 1790
c930f744
MK
1791 arvif->is_up = false;
1792
748afc47 1793 spin_lock_bh(&arvif->ar->data_lock);
64badcb6 1794 ath10k_mac_vif_beacon_free(arvif);
748afc47
MK
1795 spin_unlock_bh(&arvif->ar->data_lock);
1796
5e3dd157
KV
1797 return;
1798 }
1799
1800 arvif->tx_seq_no = 0x1000;
1801
c930f744 1802 arvif->aid = 0;
b25f32cb 1803 ether_addr_copy(arvif->bssid, info->bssid);
c930f744
MK
1804
1805 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1806 arvif->bssid);
5e3dd157 1807 if (ret) {
7aa7a72a 1808 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
69244e56 1809 arvif->vdev_id, ret);
5e3dd157
KV
1810 return;
1811 }
c930f744 1812
c930f744
MK
1813 arvif->is_up = true;
1814
500ff9f9
MK
1815 ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1816 if (ret) {
1817 ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1818 arvif->vdev_id, ret);
1819 return;
1820 }
1821
7aa7a72a 1822 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
5e3dd157
KV
1823}
1824
1825static void ath10k_control_ibss(struct ath10k_vif *arvif,
1826 struct ieee80211_bss_conf *info,
1827 const u8 self_peer[ETH_ALEN])
1828{
7aa7a72a 1829 struct ath10k *ar = arvif->ar;
6d1506e7 1830 u32 vdev_param;
5e3dd157
KV
1831 int ret = 0;
1832
548db54c
MK
1833 lockdep_assert_held(&arvif->ar->conf_mutex);
1834
5e3dd157 1835 if (!info->ibss_joined) {
c930f744 1836 if (is_zero_ether_addr(arvif->bssid))
5e3dd157
KV
1837 return;
1838
93803b33 1839 eth_zero_addr(arvif->bssid);
5e3dd157
KV
1840
1841 return;
1842 }
1843
6d1506e7
BM
1844 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1845 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
1846 ATH10K_DEFAULT_ATIM);
1847 if (ret)
7aa7a72a 1848 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
5e3dd157
KV
1849 arvif->vdev_id, ret);
1850}
1851
9f9b5746
MK
1852static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1853{
1854 struct ath10k *ar = arvif->ar;
1855 u32 param;
1856 u32 value;
1857 int ret;
1858
1859 lockdep_assert_held(&arvif->ar->conf_mutex);
1860
1861 if (arvif->u.sta.uapsd)
1862 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1863 else
1864 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1865
1866 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1867 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1868 if (ret) {
1869 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1870 value, arvif->vdev_id, ret);
1871 return ret;
1872 }
1873
1874 return 0;
1875}
1876
1877static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1878{
1879 struct ath10k *ar = arvif->ar;
1880 u32 param;
1881 u32 value;
1882 int ret;
1883
1884 lockdep_assert_held(&arvif->ar->conf_mutex);
1885
1886 if (arvif->u.sta.uapsd)
1887 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1888 else
1889 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1890
1891 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1892 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1893 param, value);
1894 if (ret) {
1895 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1896 value, arvif->vdev_id, ret);
1897 return ret;
1898 }
1899
1900 return 0;
1901}
1902
424f2630 1903static int ath10k_mac_num_vifs_started(struct ath10k *ar)
cffb41f3
MK
1904{
1905 struct ath10k_vif *arvif;
1906 int num = 0;
1907
1908 lockdep_assert_held(&ar->conf_mutex);
1909
1910 list_for_each_entry(arvif, &ar->arvifs, list)
424f2630 1911 if (arvif->is_started)
cffb41f3
MK
1912 num++;
1913
1914 return num;
1915}
1916
ad088bfa 1917static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
5e3dd157 1918{
ad088bfa 1919 struct ath10k *ar = arvif->ar;
526549a8 1920 struct ieee80211_vif *vif = arvif->vif;
ad088bfa 1921 struct ieee80211_conf *conf = &ar->hw->conf;
5e3dd157
KV
1922 enum wmi_sta_powersave_param param;
1923 enum wmi_sta_ps_mode psmode;
1924 int ret;
526549a8 1925 int ps_timeout;
cffb41f3 1926 bool enable_ps;
5e3dd157 1927
548db54c
MK
1928 lockdep_assert_held(&arvif->ar->conf_mutex);
1929
ad088bfa
MK
1930 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1931 return 0;
5e3dd157 1932
cffb41f3
MK
1933 enable_ps = arvif->ps;
1934
424f2630 1935 if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
cffb41f3 1936 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
c4cdf753 1937 ar->running_fw->fw_file.fw_features)) {
cffb41f3
MK
1938 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1939 arvif->vdev_id);
1940 enable_ps = false;
1941 }
1942
917826be
JD
1943 if (!arvif->is_started) {
1944 /* mac80211 can update vif powersave state while disconnected.
1945 * Firmware doesn't behave nicely and consumes more power than
1946 * necessary if PS is disabled on a non-started vdev. Hence
1947 * force-enable PS for non-running vdevs.
1948 */
1949 psmode = WMI_STA_PS_MODE_ENABLED;
1950 } else if (enable_ps) {
5e3dd157
KV
1951 psmode = WMI_STA_PS_MODE_ENABLED;
1952 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1953
526549a8
MK
1954 ps_timeout = conf->dynamic_ps_timeout;
1955 if (ps_timeout == 0) {
1956 /* Firmware doesn't like 0 */
1957 ps_timeout = ieee80211_tu_to_usec(
1958 vif->bss_conf.beacon_int) / 1000;
1959 }
1960
ad088bfa 1961 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
526549a8 1962 ps_timeout);
5e3dd157 1963 if (ret) {
7aa7a72a 1964 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
69244e56 1965 arvif->vdev_id, ret);
ad088bfa 1966 return ret;
5e3dd157 1967 }
5e3dd157
KV
1968 } else {
1969 psmode = WMI_STA_PS_MODE_DISABLED;
1970 }
1971
7aa7a72a 1972 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
60c3daa8
KV
1973 arvif->vdev_id, psmode ? "enable" : "disable");
1974
ad088bfa
MK
1975 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1976 if (ret) {
7aa7a72a 1977 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
be6546fc 1978 psmode, arvif->vdev_id, ret);
ad088bfa
MK
1979 return ret;
1980 }
1981
1982 return 0;
5e3dd157
KV
1983}
1984
46725b15
MK
1985static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1986{
1987 struct ath10k *ar = arvif->ar;
1988 struct wmi_sta_keepalive_arg arg = {};
1989 int ret;
1990
1991 lockdep_assert_held(&arvif->ar->conf_mutex);
1992
1993 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1994 return 0;
1995
1996 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1997 return 0;
1998
1999 /* Some firmware revisions have a bug and ignore the `enabled` field.
2000 * Instead use the interval to disable the keepalive.
2001 */
2002 arg.vdev_id = arvif->vdev_id;
2003 arg.enabled = 1;
2004 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
2005 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
2006
2007 ret = ath10k_wmi_sta_keepalive(ar, &arg);
2008 if (ret) {
2009 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
2010 arvif->vdev_id, ret);
2011 return ret;
2012 }
2013
2014 return 0;
2015}
2016
81a9a17d
MK
2017static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
2018{
2019 struct ath10k *ar = arvif->ar;
2020 struct ieee80211_vif *vif = arvif->vif;
2021 int ret;
2022
8513d95b
MK
2023 lockdep_assert_held(&arvif->ar->conf_mutex);
2024
2025 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
2026 return;
2027
81a9a17d
MK
2028 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
2029 return;
2030
2031 if (!vif->csa_active)
2032 return;
2033
2034 if (!arvif->is_up)
2035 return;
2036
8552a434
JC
2037 if (!ieee80211_beacon_cntdwn_is_complete(vif)) {
2038 ieee80211_beacon_update_cntdwn(vif);
81a9a17d
MK
2039
2040 ret = ath10k_mac_setup_bcn_tmpl(arvif);
2041 if (ret)
2042 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
2043 ret);
2044
2045 ret = ath10k_mac_setup_prb_tmpl(arvif);
2046 if (ret)
2047 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
2048 ret);
2049 } else {
2050 ieee80211_csa_finish(vif);
2051 }
2052}
2053
2054static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
2055{
2056 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2057 ap_csa_work);
2058 struct ath10k *ar = arvif->ar;
2059
2060 mutex_lock(&ar->conf_mutex);
2061 ath10k_mac_vif_ap_csa_count_down(arvif);
2062 mutex_unlock(&ar->conf_mutex);
2063}
2064
cc9904e6
MK
2065static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
2066 struct ieee80211_vif *vif)
2067{
2068 struct sk_buff *skb = data;
2069 struct ieee80211_mgmt *mgmt = (void *)skb->data;
56ac13bf 2070 struct ath10k_vif *arvif = (void *)vif->drv_priv;
cc9904e6
MK
2071
2072 if (vif->type != NL80211_IFTYPE_STATION)
2073 return;
2074
2075 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
2076 return;
2077
2078 cancel_delayed_work(&arvif->connection_loss_work);
2079}
2080
2081void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
2082{
2083 ieee80211_iterate_active_interfaces_atomic(ar->hw,
ee06fcb9 2084 ATH10K_ITER_NORMAL_FLAGS,
cc9904e6
MK
2085 ath10k_mac_handle_beacon_iter,
2086 skb);
2087}
2088
2089static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
2090 struct ieee80211_vif *vif)
2091{
2092 u32 *vdev_id = data;
56ac13bf 2093 struct ath10k_vif *arvif = (void *)vif->drv_priv;
cc9904e6
MK
2094 struct ath10k *ar = arvif->ar;
2095 struct ieee80211_hw *hw = ar->hw;
2096
2097 if (arvif->vdev_id != *vdev_id)
2098 return;
2099
2100 if (!arvif->is_up)
2101 return;
2102
2103 ieee80211_beacon_loss(vif);
2104
2105 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
2106 * (done by mac80211) succeeds but beacons do not resume then it
2107 * doesn't make sense to continue operation. Queue connection loss work
2108 * which can be cancelled when beacon is received.
2109 */
2110 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
2111 ATH10K_CONNECTION_LOSS_HZ);
2112}
2113
2114void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2115{
2116 ieee80211_iterate_active_interfaces_atomic(ar->hw,
ee06fcb9 2117 ATH10K_ITER_NORMAL_FLAGS,
cc9904e6
MK
2118 ath10k_mac_handle_beacon_miss_iter,
2119 &vdev_id);
2120}
2121
2122static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2123{
2124 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2125 connection_loss_work.work);
2126 struct ieee80211_vif *vif = arvif->vif;
2127
2128 if (!arvif->is_up)
2129 return;
2130
2131 ieee80211_connection_loss(vif);
2132}
2133
5e3dd157
KV
2134/**********************/
2135/* Station management */
2136/**********************/
2137
590922a8
MK
2138static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2139 struct ieee80211_vif *vif)
2140{
2141 /* Some firmware revisions have unstable STA powersave when listen
2142 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2143 * generate NullFunc frames properly even if buffered frames have been
2144 * indicated in Beacon TIM. Firmware would seldom wake up to pull
2145 * buffered frames. Often pinging the device from AP would simply fail.
2146 *
2147 * As a workaround set it to 1.
2148 */
2149 if (vif->type == NL80211_IFTYPE_STATION)
2150 return 1;
2151
2152 return ar->hw->conf.listen_interval;
2153}
2154
5e3dd157 2155static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
590922a8 2156 struct ieee80211_vif *vif,
5e3dd157 2157 struct ieee80211_sta *sta,
5e3dd157
KV
2158 struct wmi_peer_assoc_complete_arg *arg)
2159{
56ac13bf 2160 struct ath10k_vif *arvif = (void *)vif->drv_priv;
c51880ea 2161 u32 aid;
590922a8 2162
548db54c
MK
2163 lockdep_assert_held(&ar->conf_mutex);
2164
c51880ea
MK
2165 if (vif->type == NL80211_IFTYPE_STATION)
2166 aid = vif->bss_conf.aid;
2167 else
2168 aid = sta->aid;
2169
b25f32cb 2170 ether_addr_copy(arg->addr, sta->addr);
5e3dd157 2171 arg->vdev_id = arvif->vdev_id;
c51880ea 2172 arg->peer_aid = aid;
3fab30f7 2173 arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
590922a8 2174 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
5e3dd157 2175 arg->peer_num_spatial_streams = 1;
590922a8 2176 arg->peer_caps = vif->bss_conf.assoc_capability;
5e3dd157
KV
2177}
2178
2179static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
590922a8 2180 struct ieee80211_vif *vif,
90eceb3b 2181 struct ieee80211_sta *sta,
5e3dd157
KV
2182 struct wmi_peer_assoc_complete_arg *arg)
2183{
5e3dd157 2184 struct ieee80211_bss_conf *info = &vif->bss_conf;
500ff9f9 2185 struct cfg80211_chan_def def;
5e3dd157
KV
2186 struct cfg80211_bss *bss;
2187 const u8 *rsnie = NULL;
2188 const u8 *wpaie = NULL;
2189
548db54c
MK
2190 lockdep_assert_held(&ar->conf_mutex);
2191
500ff9f9
MK
2192 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2193 return;
2194
22df5e1b
WG
2195 bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid,
2196 info->ssid_len ? info->ssid : NULL, info->ssid_len,
500ff9f9 2197 IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
5e3dd157
KV
2198 if (bss) {
2199 const struct cfg80211_bss_ies *ies;
2200
2201 rcu_read_lock();
2202 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2203
2204 ies = rcu_dereference(bss->ies);
2205
2206 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
5b07e07f
KV
2207 WLAN_OUI_TYPE_MICROSOFT_WPA,
2208 ies->data,
2209 ies->len);
5e3dd157
KV
2210 rcu_read_unlock();
2211 cfg80211_put_bss(ar->hw->wiphy, bss);
2212 }
2213
2214 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2215 if (rsnie || wpaie) {
7aa7a72a 2216 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
3fab30f7 2217 arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
5e3dd157
KV
2218 }
2219
2220 if (wpaie) {
7aa7a72a 2221 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
3fab30f7 2222 arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
5e3dd157 2223 }
90eceb3b
T
2224
2225 if (sta->mfp &&
c4cdf753
KV
2226 test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2227 ar->running_fw->fw_file.fw_features)) {
90eceb3b 2228 arg->peer_flags |= ar->wmi.peer_flags->pmf;
5e3dd157
KV
2229 }
2230}
2231
2232static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
500ff9f9 2233 struct ieee80211_vif *vif,
5e3dd157
KV
2234 struct ieee80211_sta *sta,
2235 struct wmi_peer_assoc_complete_arg *arg)
2236{
56ac13bf 2237 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157 2238 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
500ff9f9 2239 struct cfg80211_chan_def def;
5e3dd157
KV
2240 const struct ieee80211_supported_band *sband;
2241 const struct ieee80211_rate *rates;
57fbcce3 2242 enum nl80211_band band;
5e3dd157 2243 u32 ratemask;
486017cc 2244 u8 rate;
5e3dd157
KV
2245 int i;
2246
548db54c
MK
2247 lockdep_assert_held(&ar->conf_mutex);
2248
500ff9f9
MK
2249 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2250 return;
2251
45c9abc0
MK
2252 band = def.chan->band;
2253 sband = ar->hw->wiphy->bands[band];
2254 ratemask = sta->supp_rates[band];
2255 ratemask &= arvif->bitrate_mask.control[band].legacy;
5e3dd157
KV
2256 rates = sband->bitrates;
2257
2258 rateset->num_rates = 0;
2259
2260 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2261 if (!(ratemask & 1))
2262 continue;
2263
486017cc
MK
2264 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2265 rateset->rates[rateset->num_rates] = rate;
5e3dd157
KV
2266 rateset->num_rates++;
2267 }
2268}
2269
45c9abc0
MK
2270static bool
2271ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2272{
2273 int nss;
2274
2275 for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2276 if (ht_mcs_mask[nss])
2277 return false;
2278
2279 return true;
2280}
2281
2282static bool
2283ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2284{
2285 int nss;
2286
2287 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2288 if (vht_mcs_mask[nss])
2289 return false;
2290
2291 return true;
2292}
2293
5e3dd157 2294static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
45c9abc0 2295 struct ieee80211_vif *vif,
5e3dd157
KV
2296 struct ieee80211_sta *sta,
2297 struct wmi_peer_assoc_complete_arg *arg)
2298{
2299 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
56ac13bf 2300 struct ath10k_vif *arvif = (void *)vif->drv_priv;
45c9abc0 2301 struct cfg80211_chan_def def;
57fbcce3 2302 enum nl80211_band band;
45c9abc0
MK
2303 const u8 *ht_mcs_mask;
2304 const u16 *vht_mcs_mask;
72f8cef5
VN
2305 int i, n;
2306 u8 max_nss;
af762c0b 2307 u32 stbc;
5e3dd157 2308
548db54c
MK
2309 lockdep_assert_held(&ar->conf_mutex);
2310
45c9abc0
MK
2311 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2312 return;
2313
5e3dd157
KV
2314 if (!ht_cap->ht_supported)
2315 return;
2316
45c9abc0
MK
2317 band = def.chan->band;
2318 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2319 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2320
2321 if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2322 ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2323 return;
2324
3fab30f7 2325 arg->peer_flags |= ar->wmi.peer_flags->ht;
5e3dd157
KV
2326 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2327 ht_cap->ampdu_factor)) - 1;
2328
2329 arg->peer_mpdu_density =
2330 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2331
2332 arg->peer_ht_caps = ht_cap->cap;
2333 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2334
2335 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
3fab30f7 2336 arg->peer_flags |= ar->wmi.peer_flags->ldbc;
5e3dd157
KV
2337
2338 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
3fab30f7 2339 arg->peer_flags |= ar->wmi.peer_flags->bw40;
5e3dd157
KV
2340 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2341 }
2342
45c9abc0
MK
2343 if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2344 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2345 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
5e3dd157 2346
45c9abc0
MK
2347 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2348 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2349 }
5e3dd157
KV
2350
2351 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2352 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
3fab30f7 2353 arg->peer_flags |= ar->wmi.peer_flags->stbc;
5e3dd157
KV
2354 }
2355
2356 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
5e3dd157
KV
2357 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2358 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2359 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2360 arg->peer_rate_caps |= stbc;
3fab30f7 2361 arg->peer_flags |= ar->wmi.peer_flags->stbc;
5e3dd157
KV
2362 }
2363
5e3dd157
KV
2364 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2365 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2366 else if (ht_cap->mcs.rx_mask[1])
2367 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2368
45c9abc0
MK
2369 for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2370 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2371 (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2372 max_nss = (i / 8) + 1;
5e3dd157 2373 arg->peer_ht_rates.rates[n++] = i;
45c9abc0 2374 }
5e3dd157 2375
fd71f807
BM
2376 /*
2377 * This is a workaround for HT-enabled STAs which break the spec
2378 * and have no HT capabilities RX mask (no HT RX MCS map).
2379 *
2380 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2381 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2382 *
2383 * Firmware asserts if such situation occurs.
2384 */
2385 if (n == 0) {
2386 arg->peer_ht_rates.num_rates = 8;
2387 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2388 arg->peer_ht_rates.rates[i] = i;
2389 } else {
2390 arg->peer_ht_rates.num_rates = n;
72f8cef5 2391 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
fd71f807 2392 }
5e3dd157 2393
7aa7a72a 2394 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
60c3daa8 2395 arg->addr,
5e3dd157
KV
2396 arg->peer_ht_rates.num_rates,
2397 arg->peer_num_spatial_streams);
2398}
2399
d3d3ff42
JD
2400static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2401 struct ath10k_vif *arvif,
2402 struct ieee80211_sta *sta)
5e3dd157
KV
2403{
2404 u32 uapsd = 0;
2405 u32 max_sp = 0;
d3d3ff42 2406 int ret = 0;
5e3dd157 2407
548db54c
MK
2408 lockdep_assert_held(&ar->conf_mutex);
2409
5e3dd157 2410 if (sta->wme && sta->uapsd_queues) {
7aa7a72a 2411 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
5e3dd157
KV
2412 sta->uapsd_queues, sta->max_sp);
2413
5e3dd157
KV
2414 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2415 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2416 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2417 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2418 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2419 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2420 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2421 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2422 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2423 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2424 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2425 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2426
5e3dd157
KV
2427 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2428 max_sp = sta->max_sp;
2429
d3d3ff42
JD
2430 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2431 sta->addr,
2432 WMI_AP_PS_PEER_PARAM_UAPSD,
2433 uapsd);
2434 if (ret) {
7aa7a72a 2435 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
69244e56 2436 arvif->vdev_id, ret);
d3d3ff42
JD
2437 return ret;
2438 }
5e3dd157 2439
d3d3ff42
JD
2440 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2441 sta->addr,
2442 WMI_AP_PS_PEER_PARAM_MAX_SP,
2443 max_sp);
2444 if (ret) {
7aa7a72a 2445 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
69244e56 2446 arvif->vdev_id, ret);
d3d3ff42
JD
2447 return ret;
2448 }
5e3dd157
KV
2449
2450 /* TODO setup this based on STA listen interval and
d6dfe25c
MR
2451 * beacon interval. Currently we don't know
2452 * sta->listen_interval - mac80211 patch required.
2453 * Currently use 10 seconds
2454 */
d3d3ff42 2455 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
5b07e07f
KV
2456 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2457 10);
d3d3ff42 2458 if (ret) {
7aa7a72a 2459 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
69244e56 2460 arvif->vdev_id, ret);
d3d3ff42
JD
2461 return ret;
2462 }
5e3dd157 2463 }
5e3dd157 2464
d3d3ff42 2465 return 0;
5e3dd157
KV
2466}
2467
45c9abc0
MK
2468static u16
2469ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2470 const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2471{
2472 int idx_limit;
2473 int nss;
2474 u16 mcs_map;
2475 u16 mcs;
2476
2477 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2478 mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2479 vht_mcs_limit[nss];
2480
2481 if (mcs_map)
2482 idx_limit = fls(mcs_map) - 1;
2483 else
2484 idx_limit = -1;
2485
2486 switch (idx_limit) {
1885c0f7
GS
2487 case 0:
2488 case 1:
2489 case 2:
2490 case 3:
2491 case 4:
2492 case 5:
2493 case 6:
45c9abc0
MK
2494 default:
2495 /* see ath10k_mac_can_set_bitrate_mask() */
2496 WARN_ON(1);
1885c0f7 2497 fallthrough;
45c9abc0
MK
2498 case -1:
2499 mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2500 break;
2501 case 7:
2502 mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2503 break;
2504 case 8:
2505 mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2506 break;
2507 case 9:
2508 mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2509 break;
2510 }
2511
2512 tx_mcs_set &= ~(0x3 << (nss * 2));
2513 tx_mcs_set |= mcs << (nss * 2);
2514 }
2515
2516 return tx_mcs_set;
2517}
2518
3db24065
LW
2519static u32 get_160mhz_nss_from_maxrate(int rate)
2520{
2521 u32 nss;
2522
2523 switch (rate) {
2524 case 780:
2525 nss = 1;
2526 break;
2527 case 1560:
2528 nss = 2;
2529 break;
2530 case 2106:
2531 nss = 3; /* not support MCS9 from spec*/
2532 break;
2533 case 3120:
2534 nss = 4;
2535 break;
2536 default:
2537 nss = 1;
2538 }
2539
2540 return nss;
2541}
2542
5e3dd157 2543static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
500ff9f9 2544 struct ieee80211_vif *vif,
5e3dd157
KV
2545 struct ieee80211_sta *sta,
2546 struct wmi_peer_assoc_complete_arg *arg)
2547{
2548 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
56ac13bf 2549 struct ath10k_vif *arvif = (void *)vif->drv_priv;
3db24065 2550 struct ath10k_hw_params *hw = &ar->hw_params;
500ff9f9 2551 struct cfg80211_chan_def def;
57fbcce3 2552 enum nl80211_band band;
45c9abc0 2553 const u16 *vht_mcs_mask;
a24b88b5 2554 u8 ampdu_factor;
fefcd115
VRN
2555 u8 max_nss, vht_mcs;
2556 int i;
5e3dd157 2557
500ff9f9
MK
2558 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2559 return;
2560
5e3dd157
KV
2561 if (!vht_cap->vht_supported)
2562 return;
2563
45c9abc0
MK
2564 band = def.chan->band;
2565 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2566
2567 if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2568 return;
2569
3fab30f7 2570 arg->peer_flags |= ar->wmi.peer_flags->vht;
d68bb12a 2571
57fbcce3 2572 if (def.chan->band == NL80211_BAND_2GHZ)
3fab30f7 2573 arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
d68bb12a 2574
5e3dd157
KV
2575 arg->peer_vht_caps = vht_cap->cap;
2576
a24b88b5
SM
2577 ampdu_factor = (vht_cap->cap &
2578 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2579 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2580
2581 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2582 * zero in VHT IE. Using it would result in degraded throughput.
2583 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
d6dfe25c
MR
2584 * it if VHT max_mpdu is smaller.
2585 */
a24b88b5
SM
2586 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2587 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2588 ampdu_factor)) - 1);
2589
5e3dd157 2590 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
3fab30f7 2591 arg->peer_flags |= ar->wmi.peer_flags->bw80;
5e3dd157 2592
bc1efd73
SG
2593 if (sta->bandwidth == IEEE80211_STA_RX_BW_160)
2594 arg->peer_flags |= ar->wmi.peer_flags->bw160;
2595
fefcd115
VRN
2596 /* Calculate peer NSS capability from VHT capabilities if STA
2597 * supports VHT.
2598 */
2599 for (i = 0, max_nss = 0, vht_mcs = 0; i < NL80211_VHT_NSS_MAX; i++) {
2600 vht_mcs = __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) >>
2601 (2 * i) & 3;
2602
2603 if ((vht_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED) &&
2604 vht_mcs_mask[i])
2605 max_nss = i + 1;
2606 }
2607 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
5e3dd157
KV
2608 arg->peer_vht_rates.rx_max_rate =
2609 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2610 arg->peer_vht_rates.rx_mcs_set =
2611 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2612 arg->peer_vht_rates.tx_max_rate =
2613 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
45c9abc0
MK
2614 arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2615 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
5e3dd157 2616
3db24065
LW
2617 /* Configure bandwidth-NSS mapping to FW
2618 * for the chip's tx chains setting on 160Mhz bw
2619 */
2620 if (arg->peer_phymode == MODE_11AC_VHT160 ||
2621 arg->peer_phymode == MODE_11AC_VHT80_80) {
2622 u32 rx_nss;
2623 u32 max_rate;
cc914a55 2624
3db24065
LW
2625 max_rate = arg->peer_vht_rates.rx_max_rate;
2626 rx_nss = get_160mhz_nss_from_maxrate(max_rate);
2627
2628 if (rx_nss == 0)
2629 rx_nss = arg->peer_num_spatial_streams;
2630 else
2631 rx_nss = min(arg->peer_num_spatial_streams, rx_nss);
2632
2633 max_rate = hw->vht160_mcs_tx_highest;
2634 rx_nss = min(rx_nss, get_160mhz_nss_from_maxrate(max_rate));
2635
2636 arg->peer_bw_rxnss_override =
2637 FIELD_PREP(WMI_PEER_NSS_MAP_ENABLE, 1) |
2638 FIELD_PREP(WMI_PEER_NSS_160MHZ_MASK, (rx_nss - 1));
2639
2640 if (arg->peer_phymode == MODE_11AC_VHT80_80) {
2641 arg->peer_bw_rxnss_override |=
2642 FIELD_PREP(WMI_PEER_NSS_80_80MHZ_MASK, (rx_nss - 1));
cc914a55
BG
2643 }
2644 }
3db24065
LW
2645 ath10k_dbg(ar, ATH10K_DBG_MAC,
2646 "mac vht peer %pM max_mpdu %d flags 0x%x peer_rx_nss_override 0x%x\n",
2647 sta->addr, arg->peer_max_mpdu,
2648 arg->peer_flags, arg->peer_bw_rxnss_override);
5e3dd157
KV
2649}
2650
2651static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
590922a8 2652 struct ieee80211_vif *vif,
5e3dd157 2653 struct ieee80211_sta *sta,
5e3dd157
KV
2654 struct wmi_peer_assoc_complete_arg *arg)
2655{
56ac13bf 2656 struct ath10k_vif *arvif = (void *)vif->drv_priv;
590922a8 2657
5e3dd157
KV
2658 switch (arvif->vdev_type) {
2659 case WMI_VDEV_TYPE_AP:
d3d3ff42 2660 if (sta->wme)
3fab30f7 2661 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
d3d3ff42
JD
2662
2663 if (sta->wme && sta->uapsd_queues) {
3fab30f7 2664 arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
d3d3ff42
JD
2665 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2666 }
5e3dd157
KV
2667 break;
2668 case WMI_VDEV_TYPE_STA:
07ffb449 2669 if (sta->wme)
3fab30f7 2670 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
5e3dd157 2671 break;
627d9841
JD
2672 case WMI_VDEV_TYPE_IBSS:
2673 if (sta->wme)
3fab30f7 2674 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
627d9841 2675 break;
5e3dd157
KV
2676 default:
2677 break;
2678 }
627d9841
JD
2679
2680 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
3fab30f7
T
2681 sta->addr, !!(arg->peer_flags &
2682 arvif->ar->wmi.peer_flags->qos));
5e3dd157
KV
2683}
2684
8d7aa6bc 2685static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
91b12089 2686{
57fbcce3 2687 return sta->supp_rates[NL80211_BAND_2GHZ] >>
8d7aa6bc 2688 ATH10K_MAC_FIRST_OFDM_RATE_IDX;
91b12089
MK
2689}
2690
06efdbe7
KV
2691static enum wmi_phy_mode ath10k_mac_get_phymode_vht(struct ath10k *ar,
2692 struct ieee80211_sta *sta)
2693{
bc1efd73
SG
2694 if (sta->bandwidth == IEEE80211_STA_RX_BW_160) {
2695 switch (sta->vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) {
2696 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
2697 return MODE_11AC_VHT160;
2698 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
2699 return MODE_11AC_VHT80_80;
2700 default:
2701 /* not sure if this is a valid case? */
2702 return MODE_11AC_VHT160;
2703 }
2704 }
2705
06efdbe7
KV
2706 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2707 return MODE_11AC_VHT80;
2708
2709 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2710 return MODE_11AC_VHT40;
2711
2712 if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2713 return MODE_11AC_VHT20;
2714
2715 return MODE_UNKNOWN;
2716}
2717
5e3dd157 2718static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
590922a8 2719 struct ieee80211_vif *vif,
5e3dd157
KV
2720 struct ieee80211_sta *sta,
2721 struct wmi_peer_assoc_complete_arg *arg)
2722{
56ac13bf 2723 struct ath10k_vif *arvif = (void *)vif->drv_priv;
500ff9f9 2724 struct cfg80211_chan_def def;
57fbcce3 2725 enum nl80211_band band;
45c9abc0
MK
2726 const u8 *ht_mcs_mask;
2727 const u16 *vht_mcs_mask;
5e3dd157
KV
2728 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2729
500ff9f9
MK
2730 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2731 return;
2732
45c9abc0
MK
2733 band = def.chan->band;
2734 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2735 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2736
2737 switch (band) {
57fbcce3 2738 case NL80211_BAND_2GHZ:
45c9abc0
MK
2739 if (sta->vht_cap.vht_supported &&
2740 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
d68bb12a
YL
2741 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2742 phymode = MODE_11AC_VHT40;
2743 else
2744 phymode = MODE_11AC_VHT20;
45c9abc0
MK
2745 } else if (sta->ht_cap.ht_supported &&
2746 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
5e3dd157
KV
2747 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2748 phymode = MODE_11NG_HT40;
2749 else
2750 phymode = MODE_11NG_HT20;
8d7aa6bc 2751 } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
5e3dd157 2752 phymode = MODE_11G;
91b12089
MK
2753 } else {
2754 phymode = MODE_11B;
5e3dd157
KV
2755 }
2756
2757 break;
57fbcce3 2758 case NL80211_BAND_5GHZ:
7cc45e98
SM
2759 /*
2760 * Check VHT first.
2761 */
45c9abc0
MK
2762 if (sta->vht_cap.vht_supported &&
2763 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
06efdbe7 2764 phymode = ath10k_mac_get_phymode_vht(ar, sta);
45c9abc0
MK
2765 } else if (sta->ht_cap.ht_supported &&
2766 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2767 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
5e3dd157
KV
2768 phymode = MODE_11NA_HT40;
2769 else
2770 phymode = MODE_11NA_HT20;
2771 } else {
2772 phymode = MODE_11A;
2773 }
2774
2775 break;
2776 default:
2777 break;
2778 }
2779
7aa7a72a 2780 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
38a1d47e 2781 sta->addr, ath10k_wmi_phymode_str(phymode));
60c3daa8 2782
5e3dd157
KV
2783 arg->peer_phymode = phymode;
2784 WARN_ON(phymode == MODE_UNKNOWN);
2785}
2786
b9ada65d 2787static int ath10k_peer_assoc_prepare(struct ath10k *ar,
590922a8 2788 struct ieee80211_vif *vif,
b9ada65d 2789 struct ieee80211_sta *sta,
b9ada65d 2790 struct wmi_peer_assoc_complete_arg *arg)
5e3dd157 2791{
548db54c
MK
2792 lockdep_assert_held(&ar->conf_mutex);
2793
b9ada65d 2794 memset(arg, 0, sizeof(*arg));
5e3dd157 2795
590922a8 2796 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
90eceb3b 2797 ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
500ff9f9 2798 ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
45c9abc0 2799 ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
3db24065 2800 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
500ff9f9 2801 ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
590922a8 2802 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
5e3dd157 2803
b9ada65d 2804 return 0;
5e3dd157
KV
2805}
2806
90046f50
MK
2807static const u32 ath10k_smps_map[] = {
2808 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2809 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2810 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2811 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2812};
2813
2814static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2815 const u8 *addr,
2816 const struct ieee80211_sta_ht_cap *ht_cap)
2817{
2818 int smps;
2819
2820 if (!ht_cap->ht_supported)
2821 return 0;
2822
2823 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2824 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2825
2826 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2827 return -EINVAL;
2828
2829 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
c0e33fe6 2830 ar->wmi.peer_param->smps_state,
90046f50
MK
2831 ath10k_smps_map[smps]);
2832}
2833
139e170d
MK
2834static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2835 struct ieee80211_vif *vif,
2836 struct ieee80211_sta_vht_cap vht_cap)
2837{
56ac13bf 2838 struct ath10k_vif *arvif = (void *)vif->drv_priv;
139e170d
MK
2839 int ret;
2840 u32 param;
2841 u32 value;
2842
08e75ea8
VN
2843 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2844 return 0;
2845
139e170d
MK
2846 if (!(ar->vht_cap_info &
2847 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2848 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2849 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2850 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2851 return 0;
2852
2853 param = ar->wmi.vdev_param->txbf;
2854 value = 0;
2855
2856 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2857 return 0;
2858
2859 /* The following logic is correct. If a remote STA advertises support
2860 * for being a beamformer then we should enable us being a beamformee.
2861 */
2862
2863 if (ar->vht_cap_info &
2864 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2865 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2866 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2867 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2868
2869 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2870 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2871 }
2872
2873 if (ar->vht_cap_info &
2874 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2875 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2876 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2877 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2878
2879 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2880 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2881 }
2882
2883 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2884 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2885
2886 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2887 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2888
2889 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2890 if (ret) {
2891 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2892 value, ret);
2893 return ret;
2894 }
2895
2896 return 0;
2897}
2898
442545ba
CH
2899static bool ath10k_mac_is_connected(struct ath10k *ar)
2900{
2901 struct ath10k_vif *arvif;
2902
2903 list_for_each_entry(arvif, &ar->arvifs, list) {
2904 if (arvif->is_up && arvif->vdev_type == WMI_VDEV_TYPE_STA)
2905 return true;
2906 }
2907
2908 return false;
2909}
2910
2911static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
2912{
2913 int ret;
2914 u32 param;
2915 int tx_power_2g, tx_power_5g;
2916 bool connected;
2917
2918 lockdep_assert_held(&ar->conf_mutex);
2919
2920 /* ath10k internally uses unit of 0.5 dBm so multiply by 2 */
2921 tx_power_2g = txpower * 2;
2922 tx_power_5g = txpower * 2;
2923
2924 connected = ath10k_mac_is_connected(ar);
2925
2926 if (connected && ar->tx_power_2g_limit)
2927 if (tx_power_2g > ar->tx_power_2g_limit)
2928 tx_power_2g = ar->tx_power_2g_limit;
2929
2930 if (connected && ar->tx_power_5g_limit)
2931 if (tx_power_5g > ar->tx_power_5g_limit)
2932 tx_power_5g = ar->tx_power_5g_limit;
2933
2934 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower 2g: %d, 5g: %d\n",
2935 tx_power_2g, tx_power_5g);
2936
2937 param = ar->wmi.pdev_param->txpower_limit2g;
2938 ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_2g);
2939 if (ret) {
2940 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
2941 tx_power_2g, ret);
2942 return ret;
2943 }
2944
2945 param = ar->wmi.pdev_param->txpower_limit5g;
2946 ret = ath10k_wmi_pdev_set_param(ar, param, tx_power_5g);
2947 if (ret) {
2948 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
2949 tx_power_5g, ret);
2950 return ret;
2951 }
2952
2953 return 0;
2954}
2955
2956static int ath10k_mac_txpower_recalc(struct ath10k *ar)
2957{
2958 struct ath10k_vif *arvif;
2959 int ret, txpower = -1;
2960
2961 lockdep_assert_held(&ar->conf_mutex);
2962
2963 list_for_each_entry(arvif, &ar->arvifs, list) {
2964 /* txpower not initialized yet? */
2965 if (arvif->txpower == INT_MIN)
2966 continue;
2967
2968 if (txpower == -1)
2969 txpower = arvif->txpower;
2970 else
2971 txpower = min(txpower, arvif->txpower);
2972 }
2973
2974 if (txpower == -1)
2975 return 0;
2976
2977 ret = ath10k_mac_txpower_setup(ar, txpower);
2978 if (ret) {
2979 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
2980 txpower, ret);
2981 return ret;
2982 }
2983
2984 return 0;
2985}
2986
2987static int ath10k_mac_set_sar_power(struct ath10k *ar)
2988{
2989 if (!ar->hw_params.dynamic_sar_support)
2990 return -EOPNOTSUPP;
2991
2992 if (!ath10k_mac_is_connected(ar))
2993 return 0;
2994
2995 /* if connected, then arvif->txpower must be valid */
2996 return ath10k_mac_txpower_recalc(ar);
2997}
2998
2999static int ath10k_mac_set_sar_specs(struct ieee80211_hw *hw,
3000 const struct cfg80211_sar_specs *sar)
3001{
3002 const struct cfg80211_sar_sub_specs *sub_specs;
3003 struct ath10k *ar = hw->priv;
3004 u32 i;
3005 int ret;
3006
3007 mutex_lock(&ar->conf_mutex);
3008
3009 if (!ar->hw_params.dynamic_sar_support) {
3010 ret = -EOPNOTSUPP;
3011 goto err;
3012 }
3013
3014 if (!sar || sar->type != NL80211_SAR_TYPE_POWER ||
3015 sar->num_sub_specs == 0) {
3016 ret = -EINVAL;
3017 goto err;
3018 }
3019
3020 sub_specs = sar->sub_specs;
3021
3022 /* 0dbm is not a practical value for ath10k, so use 0
3023 * as no SAR limitation on it.
3024 */
3025 ar->tx_power_2g_limit = 0;
3026 ar->tx_power_5g_limit = 0;
3027
3028 /* note the power is in 0.25dbm unit, while ath10k uses
3029 * 0.5dbm unit.
3030 */
3031 for (i = 0; i < sar->num_sub_specs; i++) {
3032 if (sub_specs->freq_range_index == 0)
3033 ar->tx_power_2g_limit = sub_specs->power / 2;
3034 else if (sub_specs->freq_range_index == 1)
3035 ar->tx_power_5g_limit = sub_specs->power / 2;
3036
3037 sub_specs++;
3038 }
3039
3040 ret = ath10k_mac_set_sar_power(ar);
3041 if (ret) {
3042 ath10k_warn(ar, "failed to set sar power: %d", ret);
3043 goto err;
3044 }
3045
3046err:
3047 mutex_unlock(&ar->conf_mutex);
3048 return ret;
3049}
3050
5e3dd157
KV
3051/* can be called only in mac80211 callbacks due to `key_count` usage */
3052static void ath10k_bss_assoc(struct ieee80211_hw *hw,
3053 struct ieee80211_vif *vif,
3054 struct ieee80211_bss_conf *bss_conf)
3055{
3056 struct ath10k *ar = hw->priv;
56ac13bf 3057 struct ath10k_vif *arvif = (void *)vif->drv_priv;
90046f50 3058 struct ieee80211_sta_ht_cap ht_cap;
139e170d 3059 struct ieee80211_sta_vht_cap vht_cap;
b9ada65d 3060 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
3061 struct ieee80211_sta *ap_sta;
3062 int ret;
3063
548db54c
MK
3064 lockdep_assert_held(&ar->conf_mutex);
3065
077efc8c
MK
3066 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
3067 arvif->vdev_id, arvif->bssid, arvif->aid);
3068
5e3dd157
KV
3069 rcu_read_lock();
3070
3071 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
3072 if (!ap_sta) {
7aa7a72a 3073 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
69244e56 3074 bss_conf->bssid, arvif->vdev_id);
5e3dd157
KV
3075 rcu_read_unlock();
3076 return;
3077 }
3078
90046f50 3079 /* ap_sta must be accessed only within rcu section which must be left
d6dfe25c
MR
3080 * before calling ath10k_setup_peer_smps() which might sleep.
3081 */
90046f50 3082 ht_cap = ap_sta->ht_cap;
139e170d 3083 vht_cap = ap_sta->vht_cap;
90046f50 3084
590922a8 3085 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
5e3dd157 3086 if (ret) {
7aa7a72a 3087 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
69244e56 3088 bss_conf->bssid, arvif->vdev_id, ret);
5e3dd157
KV
3089 rcu_read_unlock();
3090 return;
3091 }
3092
3093 rcu_read_unlock();
3094
b9ada65d
KV
3095 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
3096 if (ret) {
7aa7a72a 3097 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
69244e56 3098 bss_conf->bssid, arvif->vdev_id, ret);
b9ada65d
KV
3099 return;
3100 }
3101
90046f50
MK
3102 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
3103 if (ret) {
7aa7a72a 3104 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
69244e56 3105 arvif->vdev_id, ret);
90046f50
MK
3106 return;
3107 }
3108
139e170d
MK
3109 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3110 if (ret) {
3111 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
3112 arvif->vdev_id, bss_conf->bssid, ret);
3113 return;
3114 }
3115
7aa7a72a 3116 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
3117 "mac vdev %d up (associated) bssid %pM aid %d\n",
3118 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
3119
077efc8c
MK
3120 WARN_ON(arvif->is_up);
3121
c930f744 3122 arvif->aid = bss_conf->aid;
b25f32cb 3123 ether_addr_copy(arvif->bssid, bss_conf->bssid);
c930f744 3124
2289bef2
WG
3125 ret = ath10k_wmi_pdev_set_param(ar,
3126 ar->wmi.pdev_param->peer_stats_info_enable, 1);
3127 if (ret)
3128 ath10k_warn(ar, "failed to enable peer stats info: %d\n", ret);
3129
c930f744
MK
3130 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
3131 if (ret) {
7aa7a72a 3132 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
5e3dd157 3133 arvif->vdev_id, ret);
c930f744
MK
3134 return;
3135 }
3136
3137 arvif->is_up = true;
0a987fb0 3138
442545ba
CH
3139 ath10k_mac_set_sar_power(ar);
3140
0a987fb0
MK
3141 /* Workaround: Some firmware revisions (tested with qca6174
3142 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
3143 * poked with peer param command.
3144 */
3145 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
c0e33fe6 3146 ar->wmi.peer_param->dummy_var, 1);
0a987fb0
MK
3147 if (ret) {
3148 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
3149 arvif->bssid, arvif->vdev_id, ret);
3150 return;
3151 }
5e3dd157
KV
3152}
3153
5e3dd157
KV
3154static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
3155 struct ieee80211_vif *vif)
3156{
3157 struct ath10k *ar = hw->priv;
56ac13bf 3158 struct ath10k_vif *arvif = (void *)vif->drv_priv;
139e170d 3159 struct ieee80211_sta_vht_cap vht_cap = {};
5e3dd157
KV
3160 int ret;
3161
548db54c
MK
3162 lockdep_assert_held(&ar->conf_mutex);
3163
077efc8c
MK
3164 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
3165 arvif->vdev_id, arvif->bssid);
60c3daa8 3166
5e3dd157 3167 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
077efc8c 3168 if (ret)
aa66ba0c 3169 ath10k_warn(ar, "failed to down vdev %i: %d\n",
077efc8c 3170 arvif->vdev_id, ret);
5e3dd157 3171
627613f8
SJ
3172 arvif->def_wep_key_idx = -1;
3173
139e170d
MK
3174 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
3175 if (ret) {
3176 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
3177 arvif->vdev_id, ret);
3178 return;
3179 }
3180
c930f744 3181 arvif->is_up = false;
cc9904e6 3182
442545ba
CH
3183 ath10k_mac_txpower_recalc(ar);
3184
cc9904e6 3185 cancel_delayed_work_sync(&arvif->connection_loss_work);
5e3dd157
KV
3186}
3187
7b2531d9
TC
3188static int ath10k_new_peer_tid_config(struct ath10k *ar,
3189 struct ieee80211_sta *sta,
3190 struct ath10k_vif *arvif)
3191{
3192 struct wmi_per_peer_per_tid_cfg_arg arg = {};
3193 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
3194 bool config_apply;
3195 int ret, i;
3196
3197 for (i = 0; i < ATH10K_TID_MAX; i++) {
3198 config_apply = false;
3199 if (arvif->retry_long[i] || arvif->ampdu[i] ||
3200 arvif->rate_ctrl[i] || arvif->rtscts[i]) {
3201 config_apply = true;
3202 arg.tid = i;
3203 arg.vdev_id = arvif->vdev_id;
3204 arg.retry_count = arvif->retry_long[i];
3205 arg.aggr_control = arvif->ampdu[i];
3206 arg.rate_ctrl = arvif->rate_ctrl[i];
3207 arg.rcode_flags = arvif->rate_code[i];
3208
3209 if (arvif->rtscts[i])
3210 arg.ext_tid_cfg_bitmap =
3211 WMI_EXT_TID_RTS_CTS_CONFIG;
3212 else
3213 arg.ext_tid_cfg_bitmap = 0;
3214
3215 arg.rtscts_ctrl = arvif->rtscts[i];
3216 }
3217
3218 if (arvif->noack[i]) {
3219 arg.ack_policy = arvif->noack[i];
3220 arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
3221 arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
3222 config_apply = true;
3223 }
3224
3225 /* Assign default value(-1) to newly connected station.
3226 * This is to identify station specific tid configuration not
3227 * configured for the station.
3228 */
3229 arsta->retry_long[i] = -1;
3230 arsta->noack[i] = -1;
3231 arsta->ampdu[i] = -1;
3232
3233 if (!config_apply)
3234 continue;
3235
3236 ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
3237
3238 ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
3239 if (ret) {
3240 ath10k_warn(ar, "failed to set per tid retry/aggr config for sta %pM: %d\n",
3241 sta->addr, ret);
3242 return ret;
3243 }
3244
3245 memset(&arg, 0, sizeof(arg));
3246 }
3247
3248 return 0;
3249}
3250
590922a8
MK
3251static int ath10k_station_assoc(struct ath10k *ar,
3252 struct ieee80211_vif *vif,
3253 struct ieee80211_sta *sta,
3254 bool reassoc)
5e3dd157 3255{
56ac13bf 3256 struct ath10k_vif *arvif = (void *)vif->drv_priv;
b9ada65d 3257 struct wmi_peer_assoc_complete_arg peer_arg;
5e3dd157
KV
3258 int ret = 0;
3259
548db54c
MK
3260 lockdep_assert_held(&ar->conf_mutex);
3261
590922a8 3262 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
b9ada65d 3263 if (ret) {
7aa7a72a 3264 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
69244e56 3265 sta->addr, arvif->vdev_id, ret);
b9ada65d
KV
3266 return ret;
3267 }
3268
3269 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
5e3dd157 3270 if (ret) {
7aa7a72a 3271 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
69244e56 3272 sta->addr, arvif->vdev_id, ret);
5e3dd157
KV
3273 return ret;
3274 }
3275
b1ecde36
MK
3276 /* Re-assoc is run only to update supported rates for given station. It
3277 * doesn't make much sense to reconfigure the peer completely.
3278 */
3279 if (!reassoc) {
3280 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
3281 &sta->ht_cap);
e81bd104 3282 if (ret) {
b1ecde36 3283 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
e81bd104
MK
3284 arvif->vdev_id, ret);
3285 return ret;
3286 }
e81bd104 3287
b1ecde36
MK
3288 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
3289 if (ret) {
3290 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
3291 sta->addr, arvif->vdev_id, ret);
3292 return ret;
3293 }
5e3dd157 3294
b1ecde36
MK
3295 if (!sta->wme) {
3296 arvif->num_legacy_stations++;
3297 ret = ath10k_recalc_rtscts_prot(arvif);
3298 if (ret) {
3299 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3300 arvif->vdev_id, ret);
3301 return ret;
3302 }
3303 }
3304
627613f8 3305 /* Plumb cached keys only for static WEP */
c3816c9e 3306 if ((arvif->def_wep_key_idx != -1) && (!sta->tdls)) {
627613f8
SJ
3307 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
3308 if (ret) {
3309 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
3310 arvif->vdev_id, ret);
3311 return ret;
3312 }
b1ecde36 3313 }
d3d3ff42
JD
3314 }
3315
7b2531d9
TC
3316 if (!test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map))
3317 return ret;
3318
3319 return ath10k_new_peer_tid_config(ar, sta, arvif);
5e3dd157
KV
3320}
3321
590922a8
MK
3322static int ath10k_station_disassoc(struct ath10k *ar,
3323 struct ieee80211_vif *vif,
5e3dd157
KV
3324 struct ieee80211_sta *sta)
3325{
56ac13bf 3326 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157
KV
3327 int ret = 0;
3328
548db54c
MK
3329 lockdep_assert_held(&ar->conf_mutex);
3330
e81bd104
MK
3331 if (!sta->wme) {
3332 arvif->num_legacy_stations--;
3333 ret = ath10k_recalc_rtscts_prot(arvif);
3334 if (ret) {
7aa7a72a 3335 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
e81bd104
MK
3336 arvif->vdev_id, ret);
3337 return ret;
3338 }
3339 }
3340
5e3dd157
KV
3341 ret = ath10k_clear_peer_keys(arvif, sta->addr);
3342 if (ret) {
7aa7a72a 3343 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
69244e56 3344 arvif->vdev_id, ret);
5e3dd157
KV
3345 return ret;
3346 }
3347
3348 return ret;
3349}
3350
3351/**************/
3352/* Regulatory */
3353/**************/
3354
3355static int ath10k_update_channel_list(struct ath10k *ar)
3356{
3357 struct ieee80211_hw *hw = ar->hw;
3358 struct ieee80211_supported_band **bands;
57fbcce3 3359 enum nl80211_band band;
5e3dd157
KV
3360 struct ieee80211_channel *channel;
3361 struct wmi_scan_chan_list_arg arg = {0};
3362 struct wmi_channel_arg *ch;
3363 bool passive;
3364 int len;
3365 int ret;
3366 int i;
3367
548db54c
MK
3368 lockdep_assert_held(&ar->conf_mutex);
3369
5e3dd157 3370 bands = hw->wiphy->bands;
57fbcce3 3371 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5e3dd157
KV
3372 if (!bands[band])
3373 continue;
3374
3375 for (i = 0; i < bands[band]->n_channels; i++) {
3376 if (bands[band]->channels[i].flags &
3377 IEEE80211_CHAN_DISABLED)
3378 continue;
3379
3380 arg.n_channels++;
3381 }
3382 }
3383
3384 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
3385 arg.channels = kzalloc(len, GFP_KERNEL);
3386 if (!arg.channels)
3387 return -ENOMEM;
3388
3389 ch = arg.channels;
57fbcce3 3390 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5e3dd157
KV
3391 if (!bands[band])
3392 continue;
3393
3394 for (i = 0; i < bands[band]->n_channels; i++) {
3395 channel = &bands[band]->channels[i];
3396
3397 if (channel->flags & IEEE80211_CHAN_DISABLED)
3398 continue;
3399
9802977d 3400 ch->allow_ht = true;
5e3dd157
KV
3401
3402 /* FIXME: when should we really allow VHT? */
3403 ch->allow_vht = true;
3404
3405 ch->allow_ibss =
8fe02e16 3406 !(channel->flags & IEEE80211_CHAN_NO_IR);
5e3dd157
KV
3407
3408 ch->ht40plus =
3409 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
3410
e8a50f8b
MP
3411 ch->chan_radar =
3412 !!(channel->flags & IEEE80211_CHAN_RADAR);
3413
8fe02e16 3414 passive = channel->flags & IEEE80211_CHAN_NO_IR;
5e3dd157
KV
3415 ch->passive = passive;
3416
3f259111
SE
3417 /* the firmware is ignoring the "radar" flag of the
3418 * channel and is scanning actively using Probe Requests
3419 * on "Radar detection"/DFS channels which are not
3420 * marked as "available"
3421 */
3422 ch->passive |= ch->chan_radar;
3423
5e3dd157 3424 ch->freq = channel->center_freq;
2d66721c 3425 ch->band_center_freq1 = channel->center_freq;
89c5c843 3426 ch->min_power = 0;
02256930
MK
3427 ch->max_power = channel->max_power * 2;
3428 ch->max_reg_power = channel->max_reg_power * 2;
3429 ch->max_antenna_gain = channel->max_antenna_gain * 2;
5e3dd157
KV
3430 ch->reg_class_id = 0; /* FIXME */
3431
3432 /* FIXME: why use only legacy modes, why not any
3433 * HT/VHT modes? Would that even make any
d6dfe25c
MR
3434 * difference?
3435 */
57fbcce3 3436 if (channel->band == NL80211_BAND_2GHZ)
5e3dd157
KV
3437 ch->mode = MODE_11G;
3438 else
3439 ch->mode = MODE_11A;
3440
3441 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
3442 continue;
3443
7aa7a72a 3444 ath10k_dbg(ar, ATH10K_DBG_WMI,
60c3daa8
KV
3445 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
3446 ch - arg.channels, arg.n_channels,
5e3dd157
KV
3447 ch->freq, ch->max_power, ch->max_reg_power,
3448 ch->max_antenna_gain, ch->mode);
3449
3450 ch++;
3451 }
3452 }
3453
3454 ret = ath10k_wmi_scan_chan_list(ar, &arg);
3455 kfree(arg.channels);
3456
3457 return ret;
3458}
3459
821af6ae
MP
3460static enum wmi_dfs_region
3461ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3462{
3463 switch (dfs_region) {
3464 case NL80211_DFS_UNSET:
3465 return WMI_UNINIT_DFS_DOMAIN;
3466 case NL80211_DFS_FCC:
3467 return WMI_FCC_DFS_DOMAIN;
3468 case NL80211_DFS_ETSI:
3469 return WMI_ETSI_DFS_DOMAIN;
3470 case NL80211_DFS_JP:
3471 return WMI_MKK4_DFS_DOMAIN;
3472 }
3473 return WMI_UNINIT_DFS_DOMAIN;
3474}
3475
f7843d7f 3476static void ath10k_regd_update(struct ath10k *ar)
5e3dd157 3477{
5e3dd157 3478 struct reg_dmn_pair_mapping *regpair;
5e3dd157 3479 int ret;
821af6ae
MP
3480 enum wmi_dfs_region wmi_dfs_reg;
3481 enum nl80211_dfs_regions nl_dfs_reg;
5e3dd157 3482
f7843d7f 3483 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
3484
3485 ret = ath10k_update_channel_list(ar);
3486 if (ret)
7aa7a72a 3487 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
5e3dd157
KV
3488
3489 regpair = ar->ath_common.regulatory.regpair;
f7843d7f 3490
97f2645f 3491 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
821af6ae
MP
3492 nl_dfs_reg = ar->dfs_detector->region;
3493 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3494 } else {
3495 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3496 }
3497
5e3dd157 3498 /* Target allows setting up per-band regdomain but ath_common provides
d6dfe25c
MR
3499 * a combined one only
3500 */
5e3dd157 3501 ret = ath10k_wmi_pdev_set_regdomain(ar,
ef8c0017
KV
3502 regpair->reg_domain,
3503 regpair->reg_domain, /* 2ghz */
3504 regpair->reg_domain, /* 5ghz */
5e3dd157 3505 regpair->reg_2ghz_ctl,
821af6ae
MP
3506 regpair->reg_5ghz_ctl,
3507 wmi_dfs_reg);
5e3dd157 3508 if (ret)
7aa7a72a 3509 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
f7843d7f 3510}
548db54c 3511
46bc92be
KV
3512static void ath10k_mac_update_channel_list(struct ath10k *ar,
3513 struct ieee80211_supported_band *band)
523f6701
T
3514{
3515 int i;
3516
3517 if (ar->low_5ghz_chan && ar->high_5ghz_chan) {
3518 for (i = 0; i < band->n_channels; i++) {
3519 if (band->channels[i].center_freq < ar->low_5ghz_chan ||
3520 band->channels[i].center_freq > ar->high_5ghz_chan)
3521 band->channels[i].flags |=
3522 IEEE80211_CHAN_DISABLED;
3523 }
3524 }
3525}
3526
f7843d7f
MK
3527static void ath10k_reg_notifier(struct wiphy *wiphy,
3528 struct regulatory_request *request)
3529{
3530 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3531 struct ath10k *ar = hw->priv;
9702c686 3532 bool result;
f7843d7f
MK
3533
3534 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3535
97f2645f 3536 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
7aa7a72a 3537 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
9702c686
JD
3538 request->dfs_region);
3539 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3540 request->dfs_region);
3541 if (!result)
7aa7a72a 3542 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
9702c686
JD
3543 request->dfs_region);
3544 }
3545
f7843d7f
MK
3546 mutex_lock(&ar->conf_mutex);
3547 if (ar->state == ATH10K_STATE_ON)
3548 ath10k_regd_update(ar);
548db54c 3549 mutex_unlock(&ar->conf_mutex);
523f6701
T
3550
3551 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
3552 ath10k_mac_update_channel_list(ar,
46bc92be 3553 ar->hw->wiphy->bands[NL80211_BAND_5GHZ]);
5e3dd157
KV
3554}
3555
6f6eb1bc
S
3556static void ath10k_stop_radar_confirmation(struct ath10k *ar)
3557{
3558 spin_lock_bh(&ar->data_lock);
3559 ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_STOPPED;
3560 spin_unlock_bh(&ar->data_lock);
3561
3562 cancel_work_sync(&ar->radar_confirmation_work);
3563}
3564
5e3dd157
KV
3565/***************/
3566/* TX handlers */
3567/***************/
3568
a30c7d00
MK
3569enum ath10k_mac_tx_path {
3570 ATH10K_MAC_TX_HTT,
3571 ATH10K_MAC_TX_HTT_MGMT,
3572 ATH10K_MAC_TX_WMI_MGMT,
3573 ATH10K_MAC_TX_UNKNOWN,
3574};
3575
96d828d4 3576void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
42c3aa6f 3577{
96d828d4 3578 lockdep_assert_held(&ar->htt.tx_lock);
42c3aa6f 3579
96d828d4
MK
3580 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3581 ar->tx_paused |= BIT(reason);
3582 ieee80211_stop_queues(ar->hw);
42c3aa6f
MK
3583}
3584
96d828d4
MK
3585static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3586 struct ieee80211_vif *vif)
ddb6ad77 3587{
96d828d4 3588 struct ath10k *ar = data;
56ac13bf 3589 struct ath10k_vif *arvif = (void *)vif->drv_priv;
ddb6ad77 3590
96d828d4
MK
3591 if (arvif->tx_paused)
3592 return;
ddb6ad77 3593
96d828d4 3594 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
ddb6ad77
MK
3595}
3596
96d828d4 3597void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
5e3dd157 3598{
96d828d4 3599 lockdep_assert_held(&ar->htt.tx_lock);
5e3dd157 3600
96d828d4
MK
3601 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3602 ar->tx_paused &= ~BIT(reason);
5e3dd157 3603
96d828d4
MK
3604 if (ar->tx_paused)
3605 return;
c21c64d1 3606
96d828d4 3607 ieee80211_iterate_active_interfaces_atomic(ar->hw,
ee06fcb9 3608 ATH10K_ITER_RESUME_FLAGS,
96d828d4
MK
3609 ath10k_mac_tx_unlock_iter,
3610 ar);
3a73d1a6
MK
3611
3612 ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
5e3dd157
KV
3613}
3614
96d828d4 3615void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
5e3dd157 3616{
96d828d4 3617 struct ath10k *ar = arvif->ar;
5e3dd157 3618
96d828d4 3619 lockdep_assert_held(&ar->htt.tx_lock);
5e3dd157 3620
96d828d4
MK
3621 WARN_ON(reason >= BITS_PER_LONG);
3622 arvif->tx_paused |= BIT(reason);
3623 ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3624}
3625
3626void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3627{
3628 struct ath10k *ar = arvif->ar;
3629
3630 lockdep_assert_held(&ar->htt.tx_lock);
3631
3632 WARN_ON(reason >= BITS_PER_LONG);
3633 arvif->tx_paused &= ~BIT(reason);
3634
3635 if (ar->tx_paused)
3636 return;
3637
3638 if (arvif->tx_paused)
3639 return;
3640
3641 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3642}
3643
b4aa539d
MK
3644static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3645 enum wmi_tlv_tx_pause_id pause_id,
3646 enum wmi_tlv_tx_pause_action action)
3647{
3648 struct ath10k *ar = arvif->ar;
3649
3650 lockdep_assert_held(&ar->htt.tx_lock);
3651
acd0b27b
MK
3652 switch (action) {
3653 case WMI_TLV_TX_PAUSE_ACTION_STOP:
3654 ath10k_mac_vif_tx_lock(arvif, pause_id);
3655 break;
3656 case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3657 ath10k_mac_vif_tx_unlock(arvif, pause_id);
b4aa539d 3658 break;
b4aa539d 3659 default:
209b2a68
BM
3660 ath10k_dbg(ar, ATH10K_DBG_BOOT,
3661 "received unknown tx pause action %d on vdev %i, ignoring\n",
acd0b27b 3662 action, arvif->vdev_id);
b4aa539d
MK
3663 break;
3664 }
3665}
3666
3667struct ath10k_mac_tx_pause {
3668 u32 vdev_id;
3669 enum wmi_tlv_tx_pause_id pause_id;
3670 enum wmi_tlv_tx_pause_action action;
3671};
3672
3673static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3674 struct ieee80211_vif *vif)
3675{
56ac13bf 3676 struct ath10k_vif *arvif = (void *)vif->drv_priv;
b4aa539d
MK
3677 struct ath10k_mac_tx_pause *arg = data;
3678
acd0b27b
MK
3679 if (arvif->vdev_id != arg->vdev_id)
3680 return;
3681
b4aa539d
MK
3682 ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3683}
3684
acd0b27b
MK
3685void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3686 enum wmi_tlv_tx_pause_id pause_id,
3687 enum wmi_tlv_tx_pause_action action)
b4aa539d
MK
3688{
3689 struct ath10k_mac_tx_pause arg = {
3690 .vdev_id = vdev_id,
3691 .pause_id = pause_id,
3692 .action = action,
3693 };
3694
3695 spin_lock_bh(&ar->htt.tx_lock);
3696 ieee80211_iterate_active_interfaces_atomic(ar->hw,
ee06fcb9 3697 ATH10K_ITER_RESUME_FLAGS,
b4aa539d
MK
3698 ath10k_mac_handle_tx_pause_iter,
3699 &arg);
3700 spin_unlock_bh(&ar->htt.tx_lock);
3701}
3702
d740d8fd 3703static enum ath10k_hw_txrx_mode
6a2636d8
MK
3704ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3705 struct ieee80211_vif *vif,
3706 struct ieee80211_sta *sta,
3707 struct sk_buff *skb)
d740d8fd
MK
3708{
3709 const struct ieee80211_hdr *hdr = (void *)skb->data;
4920ce3b 3710 const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
d740d8fd
MK
3711 __le16 fc = hdr->frame_control;
3712
3713 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3714 return ATH10K_HW_TXRX_RAW;
3715
3716 if (ieee80211_is_mgmt(fc))
3717 return ATH10K_HW_TXRX_MGMT;
3718
3719 /* Workaround:
3720 *
3721 * NullFunc frames are mostly used to ping if a client or AP are still
3722 * reachable and responsive. This implies tx status reports must be
3723 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3724 * come to a conclusion that the other end disappeared and tear down
3725 * BSS connection or it can never disconnect from BSS/client (which is
3726 * the case).
3727 *
3728 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3729 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3730 * which seems to deliver correct tx reports for NullFunc frames. The
3731 * downside of using it is it ignores client powersave state so it can
3732 * end up disconnecting sleeping clients in AP mode. It should fix STA
3733 * mode though because AP don't sleep.
3734 */
3735 if (ar->htt.target_version_major < 3 &&
3736 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
c4cdf753
KV
3737 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3738 ar->running_fw->fw_file.fw_features))
d740d8fd
MK
3739 return ATH10K_HW_TXRX_MGMT;
3740
75d85fd9
MP
3741 /* Workaround:
3742 *
3743 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3744 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3745 * to work with Ethernet txmode so use it.
ccec9038
DL
3746 *
3747 * FIXME: Check if raw mode works with TDLS.
75d85fd9
MP
3748 */
3749 if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3750 return ATH10K_HW_TXRX_ETHERNET;
3751
4920ce3b
MP
3752 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) ||
3753 skb_cb->flags & ATH10K_SKB_F_RAW_TX)
ccec9038
DL
3754 return ATH10K_HW_TXRX_RAW;
3755
d740d8fd
MK
3756 return ATH10K_HW_TXRX_NATIVE_WIFI;
3757}
3758
ccec9038 3759static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
fd12cb32
MK
3760 struct sk_buff *skb)
3761{
3762 const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3763 const struct ieee80211_hdr *hdr = (void *)skb->data;
ccec9038
DL
3764 const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3765 IEEE80211_TX_CTL_INJECTED;
fd12cb32
MK
3766
3767 if (!ieee80211_has_protected(hdr->frame_control))
3768 return false;
3769
ccec9038
DL
3770 if ((info->flags & mask) == mask)
3771 return false;
fd12cb32 3772
ccec9038 3773 if (vif)
56ac13bf 3774 return !((struct ath10k_vif *)vif->drv_priv)->nohwcrypt;
fd12cb32 3775
ccec9038
DL
3776 return true;
3777}
3778
4b604558
MK
3779/* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3780 * Control in the header.
5e3dd157 3781 */
4b604558 3782static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
5e3dd157
KV
3783{
3784 struct ieee80211_hdr *hdr = (void *)skb->data;
c21c64d1 3785 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
5e3dd157
KV
3786 u8 *qos_ctl;
3787
3788 if (!ieee80211_is_data_qos(hdr->frame_control))
3789 return;
3790
3791 qos_ctl = ieee80211_get_qos_ctl(hdr);
ba0ccd7a
MK
3792 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3793 skb->data, (void *)qos_ctl - (void *)skb->data);
3794 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
c21c64d1 3795
8bad8dcd
MK
3796 /* Some firmware revisions don't handle sending QoS NullFunc well.
3797 * These frames are mainly used for CQM purposes so it doesn't really
3798 * matter whether QoS NullFunc or NullFunc are sent.
c21c64d1 3799 */
bf0a26d3 3800 hdr = (void *)skb->data;
8bad8dcd 3801 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
609db229 3802 cb->flags &= ~ATH10K_SKB_F_QOS;
8bad8dcd
MK
3803
3804 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
5e3dd157
KV
3805}
3806
d740d8fd
MK
3807static void ath10k_tx_h_8023(struct sk_buff *skb)
3808{
3809 struct ieee80211_hdr *hdr;
3810 struct rfc1042_hdr *rfc1042;
3811 struct ethhdr *eth;
3812 size_t hdrlen;
3813 u8 da[ETH_ALEN];
3814 u8 sa[ETH_ALEN];
3815 __be16 type;
3816
3817 hdr = (void *)skb->data;
3818 hdrlen = ieee80211_hdrlen(hdr->frame_control);
3819 rfc1042 = (void *)skb->data + hdrlen;
3820
3821 ether_addr_copy(da, ieee80211_get_DA(hdr));
3822 ether_addr_copy(sa, ieee80211_get_SA(hdr));
3823 type = rfc1042->snap_type;
3824
3825 skb_pull(skb, hdrlen + sizeof(*rfc1042));
3826 skb_push(skb, sizeof(*eth));
3827
3828 eth = (void *)skb->data;
3829 ether_addr_copy(eth->h_dest, da);
3830 ether_addr_copy(eth->h_source, sa);
3831 eth->h_proto = type;
5e3dd157
KV
3832}
3833
4b604558
MK
3834static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3835 struct ieee80211_vif *vif,
3836 struct sk_buff *skb)
5e3dd157
KV
3837{
3838 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
56ac13bf 3839 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157
KV
3840
3841 /* This is case only for P2P_GO */
08c27be1 3842 if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
5e3dd157
KV
3843 return;
3844
3845 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3846 spin_lock_bh(&ar->data_lock);
3847 if (arvif->u.ap.noa_data)
3848 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3849 GFP_ATOMIC))
59ae1d12
JB
3850 skb_put_data(skb, arvif->u.ap.noa_data,
3851 arvif->u.ap.noa_len);
5e3dd157
KV
3852 spin_unlock_bh(&ar->data_lock);
3853 }
3854}
3855
f2f6ecab
MK
3856static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3857 struct ieee80211_vif *vif,
dd4717b6 3858 struct ieee80211_txq *txq,
95a568c4 3859 struct ieee80211_sta *sta,
d1ce37b7 3860 struct sk_buff *skb, u16 airtime)
f2f6ecab
MK
3861{
3862 struct ieee80211_hdr *hdr = (void *)skb->data;
3863 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
4920ce3b
MP
3864 const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3865 bool is_data = ieee80211_is_data(hdr->frame_control) ||
3866 ieee80211_is_data_qos(hdr->frame_control);
7b2531d9 3867 struct ath10k_vif *arvif = (void *)vif->drv_priv;
95a568c4 3868 struct ath10k_sta *arsta;
7b2531d9
TC
3869 u8 tid, *qos_ctl;
3870 bool noack = false;
f2f6ecab
MK
3871
3872 cb->flags = 0;
3873 if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3874 cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3875
3876 if (ieee80211_is_mgmt(hdr->frame_control))
3877 cb->flags |= ATH10K_SKB_F_MGMT;
3878
7b2531d9 3879 if (ieee80211_is_data_qos(hdr->frame_control)) {
f2f6ecab 3880 cb->flags |= ATH10K_SKB_F_QOS;
7b2531d9
TC
3881 qos_ctl = ieee80211_get_qos_ctl(hdr);
3882 tid = (*qos_ctl) & IEEE80211_QOS_CTL_TID_MASK;
3883
3884 if (arvif->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3885 noack = true;
3886
3887 if (sta) {
3888 arsta = (struct ath10k_sta *)sta->drv_priv;
3889
3890 if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_NOACK)
3891 noack = true;
3892
3893 if (arsta->noack[tid] == WMI_PEER_TID_CONFIG_ACK)
3894 noack = false;
3895 }
3896
3897 if (noack)
3898 cb->flags |= ATH10K_SKB_F_NOACK_TID;
3899 }
f2f6ecab 3900
4920ce3b
MP
3901 /* Data frames encrypted in software will be posted to firmware
3902 * with tx encap mode set to RAW. Ex: Multicast traffic generated
3903 * for a specific VLAN group will always be encrypted in software.
3904 */
3905 if (is_data && ieee80211_has_protected(hdr->frame_control) &&
3906 !info->control.hw_key) {
3907 cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3908 cb->flags |= ATH10K_SKB_F_RAW_TX;
3909 }
3910
f2f6ecab 3911 cb->vif = vif;
dd4717b6 3912 cb->txq = txq;
d1ce37b7 3913 cb->airtime_est = airtime;
95a568c4
YT
3914 if (sta) {
3915 arsta = (struct ath10k_sta *)sta->drv_priv;
3916 spin_lock_bh(&ar->data_lock);
3917 cb->ucast_cipher = arsta->ucast_cipher;
3918 spin_unlock_bh(&ar->data_lock);
3919 }
f2f6ecab
MK
3920}
3921
d39de991 3922bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
8d6d3624
MK
3923{
3924 /* FIXME: Not really sure since when the behaviour changed. At some
3925 * point new firmware stopped requiring creation of peer entries for
3926 * offchannel tx (and actually creating them causes issues with wmi-htc
3927 * tx credit replenishment and reliability). Assuming it's at least 3.4
3928 * because that's when the `freq` was introduced to TX_FRM HTT command.
3929 */
8921f5f7 3930 return (ar->htt.target_version_major >= 3 &&
d39de991 3931 ar->htt.target_version_minor >= 4 &&
77561f93 3932 ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
8d6d3624
MK
3933}
3934
d740d8fd 3935static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
5e3dd157 3936{
d740d8fd 3937 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
d740d8fd 3938
b55379e3 3939 if (skb_queue_len_lockless(q) >= ATH10K_MAX_NUM_MGMT_PENDING) {
d740d8fd 3940 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
b55379e3 3941 return -ENOSPC;
961d4c38
MK
3942 }
3943
b55379e3 3944 skb_queue_tail(q, skb);
d740d8fd 3945 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
5e00d31a 3946
b55379e3 3947 return 0;
d740d8fd
MK
3948}
3949
a30c7d00
MK
3950static enum ath10k_mac_tx_path
3951ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3952 struct sk_buff *skb,
3953 enum ath10k_hw_txrx_mode txmode)
d740d8fd 3954{
8a933964 3955 switch (txmode) {
d740d8fd
MK
3956 case ATH10K_HW_TXRX_RAW:
3957 case ATH10K_HW_TXRX_NATIVE_WIFI:
3958 case ATH10K_HW_TXRX_ETHERNET:
a30c7d00 3959 return ATH10K_MAC_TX_HTT;
d740d8fd
MK
3960 case ATH10K_HW_TXRX_MGMT:
3961 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
229329ff
RP
3962 ar->running_fw->fw_file.fw_features) ||
3963 test_bit(WMI_SERVICE_MGMT_TX_WMI,
3964 ar->wmi.svc_map))
a30c7d00 3965 return ATH10K_MAC_TX_WMI_MGMT;
d740d8fd 3966 else if (ar->htt.target_version_major >= 3)
a30c7d00 3967 return ATH10K_MAC_TX_HTT;
d740d8fd 3968 else
a30c7d00
MK
3969 return ATH10K_MAC_TX_HTT_MGMT;
3970 }
3971
3972 return ATH10K_MAC_TX_UNKNOWN;
3973}
3974
3975static int ath10k_mac_tx_submit(struct ath10k *ar,
3976 enum ath10k_hw_txrx_mode txmode,
6421969f 3977 enum ath10k_mac_tx_path txpath,
a30c7d00
MK
3978 struct sk_buff *skb)
3979{
3980 struct ath10k_htt *htt = &ar->htt;
6421969f 3981 int ret = -EINVAL;
a30c7d00
MK
3982
3983 switch (txpath) {
3984 case ATH10K_MAC_TX_HTT:
5df6e131 3985 ret = ath10k_htt_tx(htt, txmode, skb);
a30c7d00
MK
3986 break;
3987 case ATH10K_MAC_TX_HTT_MGMT:
3988 ret = ath10k_htt_mgmt_tx(htt, skb);
3989 break;
3990 case ATH10K_MAC_TX_WMI_MGMT:
3991 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3992 break;
3993 case ATH10K_MAC_TX_UNKNOWN:
3994 WARN_ON_ONCE(1);
3995 ret = -EINVAL;
d740d8fd 3996 break;
5e00d31a 3997 }
5e3dd157
KV
3998
3999 if (ret) {
7aa7a72a
MK
4000 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
4001 ret);
5e3dd157
KV
4002 ieee80211_free_txskb(ar->hw, skb);
4003 }
f2f6ecab
MK
4004
4005 return ret;
4006}
4007
4008/* This function consumes the sk_buff regardless of return value as far as
4009 * caller is concerned so no freeing is necessary afterwards.
4010 */
4011static int ath10k_mac_tx(struct ath10k *ar,
4012 struct ieee80211_vif *vif,
f2f6ecab 4013 enum ath10k_hw_txrx_mode txmode,
6421969f 4014 enum ath10k_mac_tx_path txpath,
cc6df017 4015 struct sk_buff *skb, bool noque_offchan)
f2f6ecab
MK
4016{
4017 struct ieee80211_hw *hw = ar->hw;
4018 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4920ce3b 4019 const struct ath10k_skb_cb *skb_cb = ATH10K_SKB_CB(skb);
f2f6ecab
MK
4020 int ret;
4021
4022 /* We should disable CCK RATE due to P2P */
4023 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
4024 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
4025
4026 switch (txmode) {
4027 case ATH10K_HW_TXRX_MGMT:
4028 case ATH10K_HW_TXRX_NATIVE_WIFI:
4029 ath10k_tx_h_nwifi(hw, skb);
4030 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
4031 ath10k_tx_h_seq_no(vif, skb);
4032 break;
4033 case ATH10K_HW_TXRX_ETHERNET:
4034 ath10k_tx_h_8023(skb);
4035 break;
4036 case ATH10K_HW_TXRX_RAW:
4920ce3b
MP
4037 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags) &&
4038 !(skb_cb->flags & ATH10K_SKB_F_RAW_TX)) {
f2f6ecab
MK
4039 WARN_ON_ONCE(1);
4040 ieee80211_free_txskb(hw, skb);
4041 return -ENOTSUPP;
4042 }
4043 }
4044
cc6df017 4045 if (!noque_offchan && info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
f2f6ecab 4046 if (!ath10k_mac_tx_frm_has_freq(ar)) {
cc6df017
BG
4047 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac queued offchannel skb %pK len %d\n",
4048 skb, skb->len);
f2f6ecab
MK
4049
4050 skb_queue_tail(&ar->offchan_tx_queue, skb);
4051 ieee80211_queue_work(hw, &ar->offchan_tx_work);
4052 return 0;
4053 }
4054 }
4055
6421969f 4056 ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
f2f6ecab
MK
4057 if (ret) {
4058 ath10k_warn(ar, "failed to submit frame: %d\n", ret);
4059 return ret;
4060 }
4061
4062 return 0;
5e3dd157
KV
4063}
4064
4065void ath10k_offchan_tx_purge(struct ath10k *ar)
4066{
4067 struct sk_buff *skb;
4068
4069 for (;;) {
4070 skb = skb_dequeue(&ar->offchan_tx_queue);
4071 if (!skb)
4072 break;
4073
4074 ieee80211_free_txskb(ar->hw, skb);
4075 }
4076}
4077
4078void ath10k_offchan_tx_work(struct work_struct *work)
4079{
4080 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
4081 struct ath10k_peer *peer;
8a933964 4082 struct ath10k_vif *arvif;
f2f6ecab 4083 enum ath10k_hw_txrx_mode txmode;
6421969f 4084 enum ath10k_mac_tx_path txpath;
5e3dd157 4085 struct ieee80211_hdr *hdr;
8a933964
MK
4086 struct ieee80211_vif *vif;
4087 struct ieee80211_sta *sta;
5e3dd157
KV
4088 struct sk_buff *skb;
4089 const u8 *peer_addr;
4090 int vdev_id;
4091 int ret;
8e9904f5 4092 unsigned long time_left;
adaeed74 4093 bool tmp_peer_created = false;
5e3dd157
KV
4094
4095 /* FW requirement: We must create a peer before FW will send out
4096 * an offchannel frame. Otherwise the frame will be stuck and
4097 * never transmitted. We delete the peer upon tx completion.
4098 * It is unlikely that a peer for offchannel tx will already be
4099 * present. However it may be in some rare cases so account for that.
d6dfe25c
MR
4100 * Otherwise we might remove a legitimate peer and break stuff.
4101 */
5e3dd157
KV
4102
4103 for (;;) {
4104 skb = skb_dequeue(&ar->offchan_tx_queue);
4105 if (!skb)
4106 break;
4107
4108 mutex_lock(&ar->conf_mutex);
4109
cc6df017
BG
4110 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK len %d\n",
4111 skb, skb->len);
5e3dd157
KV
4112
4113 hdr = (struct ieee80211_hdr *)skb->data;
4114 peer_addr = ieee80211_get_DA(hdr);
5e3dd157
KV
4115
4116 spin_lock_bh(&ar->data_lock);
609db229 4117 vdev_id = ar->scan.vdev_id;
5e3dd157
KV
4118 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
4119 spin_unlock_bh(&ar->data_lock);
4120
4121 if (peer)
83bae265
SK
4122 ath10k_warn(ar, "peer %pM on vdev %d already present\n",
4123 peer_addr, vdev_id);
5e3dd157
KV
4124
4125 if (!peer) {
6942726f
MK
4126 ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
4127 peer_addr,
7390ed34 4128 WMI_PEER_TYPE_DEFAULT);
5e3dd157 4129 if (ret)
7aa7a72a 4130 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
5e3dd157 4131 peer_addr, vdev_id, ret);
adaeed74 4132 tmp_peer_created = (ret == 0);
5e3dd157
KV
4133 }
4134
4135 spin_lock_bh(&ar->data_lock);
16735d02 4136 reinit_completion(&ar->offchan_tx_completed);
5e3dd157
KV
4137 ar->offchan_tx_skb = skb;
4138 spin_unlock_bh(&ar->data_lock);
4139
8a933964
MK
4140 /* It's safe to access vif and sta - conf_mutex guarantees that
4141 * sta_state() and remove_interface() are locked exclusively
4142 * out wrt to this offchannel worker.
4143 */
4144 arvif = ath10k_get_arvif(ar, vdev_id);
4145 if (arvif) {
4146 vif = arvif->vif;
4147 sta = ieee80211_find_sta(vif, peer_addr);
4148 } else {
4149 vif = NULL;
4150 sta = NULL;
4151 }
4152
4153 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
6421969f 4154 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
8a933964 4155
cc6df017 4156 ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, true);
f2f6ecab
MK
4157 if (ret) {
4158 ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
4159 ret);
4160 /* not serious */
4161 }
5e3dd157 4162
8e9904f5
NMG
4163 time_left =
4164 wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
4165 if (time_left == 0)
cc6df017
BG
4166 ath10k_warn(ar, "timed out waiting for offchannel skb %pK, len: %d\n",
4167 skb, skb->len);
5e3dd157 4168
adaeed74 4169 if (!peer && tmp_peer_created) {
5e3dd157
KV
4170 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
4171 if (ret)
7aa7a72a 4172 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
5e3dd157
KV
4173 peer_addr, vdev_id, ret);
4174 }
4175
4176 mutex_unlock(&ar->conf_mutex);
4177 }
4178}
4179
5e00d31a
BM
4180void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
4181{
4182 struct sk_buff *skb;
4183
4184 for (;;) {
4185 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4186 if (!skb)
4187 break;
4188
4189 ieee80211_free_txskb(ar->hw, skb);
4190 }
4191}
4192
4193void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
4194{
4195 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
4196 struct sk_buff *skb;
38a1390e 4197 dma_addr_t paddr;
5e00d31a
BM
4198 int ret;
4199
4200 for (;;) {
4201 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
4202 if (!skb)
4203 break;
4204
38a1390e
RP
4205 if (test_bit(ATH10K_FW_FEATURE_MGMT_TX_BY_REF,
4206 ar->running_fw->fw_file.fw_features)) {
4207 paddr = dma_map_single(ar->dev, skb->data,
4208 skb->len, DMA_TO_DEVICE);
d43810b2
BA
4209 if (dma_mapping_error(ar->dev, paddr)) {
4210 ieee80211_free_txskb(ar->hw, skb);
38a1390e 4211 continue;
d43810b2 4212 }
38a1390e
RP
4213 ret = ath10k_wmi_mgmt_tx_send(ar, skb, paddr);
4214 if (ret) {
4215 ath10k_warn(ar, "failed to transmit management frame by ref via WMI: %d\n",
4216 ret);
c730c477
RP
4217 /* remove this msdu from idr tracking */
4218 ath10k_wmi_cleanup_mgmt_tx_send(ar, skb);
4219
38a1390e 4220 dma_unmap_single(ar->dev, paddr, skb->len,
6e8a8991 4221 DMA_TO_DEVICE);
38a1390e
RP
4222 ieee80211_free_txskb(ar->hw, skb);
4223 }
4224 } else {
4225 ret = ath10k_wmi_mgmt_tx(ar, skb);
4226 if (ret) {
4227 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
4228 ret);
4229 ieee80211_free_txskb(ar->hw, skb);
4230 }
5fb5e41f 4231 }
5e00d31a
BM
4232 }
4233}
4234
29946878
MK
4235static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
4236{
a66cd733 4237 struct ath10k_txq *artxq;
29946878
MK
4238
4239 if (!txq)
4240 return;
4241
a66cd733 4242 artxq = (void *)txq->drv_priv;
29946878
MK
4243 INIT_LIST_HEAD(&artxq->list);
4244}
4245
4246static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
4247{
dd4717b6
MK
4248 struct ath10k_skb_cb *cb;
4249 struct sk_buff *msdu;
4250 int msdu_id;
29946878
MK
4251
4252 if (!txq)
4253 return;
4254
dd4717b6
MK
4255 spin_lock_bh(&ar->htt.tx_lock);
4256 idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
4257 cb = ATH10K_SKB_CB(msdu);
4258 if (cb->txq == txq)
4259 cb->txq = NULL;
4260 }
4261 spin_unlock_bh(&ar->htt.tx_lock);
29946878
MK
4262}
4263
426e10ea
MK
4264struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
4265 u16 peer_id,
4266 u8 tid)
4267{
4268 struct ath10k_peer *peer;
4269
4270 lockdep_assert_held(&ar->data_lock);
4271
4272 peer = ar->peer_map[peer_id];
4273 if (!peer)
4274 return NULL;
4275
0a744d92
MK
4276 if (peer->removed)
4277 return NULL;
4278
426e10ea
MK
4279 if (peer->sta)
4280 return peer->sta->txq[tid];
4281 else if (peer->vif)
4282 return peer->vif->txq;
4283 else
4284 return NULL;
4285}
4286
29946878
MK
4287static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
4288 struct ieee80211_txq *txq)
4289{
426e10ea
MK
4290 struct ath10k *ar = hw->priv;
4291 struct ath10k_txq *artxq = (void *)txq->drv_priv;
4292
4293 /* No need to get locks */
426e10ea
MK
4294 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
4295 return true;
4296
4297 if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
4298 return true;
4299
4300 if (artxq->num_fw_queued < artxq->num_push_allowed)
4301 return true;
4302
4303 return false;
29946878
MK
4304}
4305
d1ce37b7
KY
4306/* Return estimated airtime in microsecond, which is calculated using last
4307 * reported TX rate. This is just a rough estimation because host driver has no
4308 * knowledge of the actual transmit rate, retries or aggregation. If actual
4309 * airtime can be reported by firmware, then delta between estimated and actual
4310 * airtime can be adjusted from deficit.
4311 */
4312#define IEEE80211_ATF_OVERHEAD 100 /* IFS + some slot time */
4313#define IEEE80211_ATF_OVERHEAD_IFS 16 /* IFS only */
4314static u16 ath10k_mac_update_airtime(struct ath10k *ar,
4315 struct ieee80211_txq *txq,
4316 struct sk_buff *skb)
4317{
4318 struct ath10k_sta *arsta;
4319 u32 pktlen;
4320 u16 airtime = 0;
4321
4322 if (!txq || !txq->sta)
4323 return airtime;
4324
bb31b7cb
MP
4325 if (test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
4326 return airtime;
4327
d1ce37b7
KY
4328 spin_lock_bh(&ar->data_lock);
4329 arsta = (struct ath10k_sta *)txq->sta->drv_priv;
4330
4331 pktlen = skb->len + 38; /* Assume MAC header 30, SNAP 8 for most case */
4332 if (arsta->last_tx_bitrate) {
4333 /* airtime in us, last_tx_bitrate in 100kbps */
4334 airtime = (pktlen * 8 * (1000 / 100))
4335 / arsta->last_tx_bitrate;
4336 /* overhead for media access time and IFS */
4337 airtime += IEEE80211_ATF_OVERHEAD_IFS;
4338 } else {
4339 /* This is mostly for throttle excessive BC/MC frames, and the
4340 * airtime/rate doesn't need be exact. Airtime of BC/MC frames
4341 * in 2G get some discount, which helps prevent very low rate
4342 * frames from being blocked for too long.
4343 */
4344 airtime = (pktlen * 8 * (1000 / 100)) / 60; /* 6M */
4345 airtime += IEEE80211_ATF_OVERHEAD;
4346 }
4347 spin_unlock_bh(&ar->data_lock);
4348
4349 return airtime;
4350}
4351
426e10ea
MK
4352int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
4353 struct ieee80211_txq *txq)
29946878 4354{
29946878
MK
4355 struct ath10k *ar = hw->priv;
4356 struct ath10k_htt *htt = &ar->htt;
3cc0fef6 4357 struct ath10k_txq *artxq = (void *)txq->drv_priv;
29946878
MK
4358 struct ieee80211_vif *vif = txq->vif;
4359 struct ieee80211_sta *sta = txq->sta;
4360 enum ath10k_hw_txrx_mode txmode;
4361 enum ath10k_mac_tx_path txpath;
4362 struct sk_buff *skb;
e4fd726f 4363 struct ieee80211_hdr *hdr;
426e10ea 4364 size_t skb_len;
e4fd726f 4365 bool is_mgmt, is_presp;
29946878 4366 int ret;
d1ce37b7 4367 u16 airtime;
29946878
MK
4368
4369 spin_lock_bh(&ar->htt.tx_lock);
cac08552 4370 ret = ath10k_htt_tx_inc_pending(htt);
29946878
MK
4371 spin_unlock_bh(&ar->htt.tx_lock);
4372
4373 if (ret)
4374 return ret;
4375
30654760 4376 skb = ieee80211_tx_dequeue_ni(hw, txq);
29946878
MK
4377 if (!skb) {
4378 spin_lock_bh(&ar->htt.tx_lock);
cac08552 4379 ath10k_htt_tx_dec_pending(htt);
29946878
MK
4380 spin_unlock_bh(&ar->htt.tx_lock);
4381
4382 return -ENOENT;
4383 }
4384
d1ce37b7 4385 airtime = ath10k_mac_update_airtime(ar, txq, skb);
95a568c4 4386 ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
29946878 4387
426e10ea 4388 skb_len = skb->len;
29946878
MK
4389 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4390 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
e4fd726f
ARN
4391 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4392
4393 if (is_mgmt) {
4394 hdr = (struct ieee80211_hdr *)skb->data;
4395 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4396
4397 spin_lock_bh(&ar->htt.tx_lock);
4398 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4399
4400 if (ret) {
4401 ath10k_htt_tx_dec_pending(htt);
4402 spin_unlock_bh(&ar->htt.tx_lock);
4403 return ret;
4404 }
4405 spin_unlock_bh(&ar->htt.tx_lock);
4406 }
29946878 4407
cc6df017 4408 ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
29946878
MK
4409 if (unlikely(ret)) {
4410 ath10k_warn(ar, "failed to push frame: %d\n", ret);
4411
4412 spin_lock_bh(&ar->htt.tx_lock);
cac08552 4413 ath10k_htt_tx_dec_pending(htt);
e4fd726f
ARN
4414 if (is_mgmt)
4415 ath10k_htt_tx_mgmt_dec_pending(htt);
29946878
MK
4416 spin_unlock_bh(&ar->htt.tx_lock);
4417
4418 return ret;
4419 }
4420
3cc0fef6
MK
4421 spin_lock_bh(&ar->htt.tx_lock);
4422 artxq->num_fw_queued++;
4423 spin_unlock_bh(&ar->htt.tx_lock);
4424
426e10ea 4425 return skb_len;
29946878
MK
4426}
4427
bb2edb73 4428static int ath10k_mac_schedule_txq(struct ieee80211_hw *hw, u32 ac)
29946878 4429{
29946878 4430 struct ieee80211_txq *txq;
bb2edb73 4431 int ret = 0;
29946878 4432
bb2edb73
THJ
4433 ieee80211_txq_schedule_start(hw, ac);
4434 while ((txq = ieee80211_next_txq(hw, ac))) {
4435 while (ath10k_mac_tx_can_push(hw, txq)) {
29946878
MK
4436 ret = ath10k_mac_tx_push_txq(hw, txq);
4437 if (ret < 0)
4438 break;
4439 }
2b4a6698 4440 ieee80211_return_txq(hw, txq, false);
bb2edb73
THJ
4441 ath10k_htt_tx_txq_update(hw, txq);
4442 if (ret == -EBUSY)
4443 break;
4444 }
4445 ieee80211_txq_schedule_end(hw, ac);
4446
4447 return ret;
4448}
29946878 4449
bb2edb73
THJ
4450void ath10k_mac_tx_push_pending(struct ath10k *ar)
4451{
4452 struct ieee80211_hw *hw = ar->hw;
4453 u32 ac;
9d71d47e 4454
bb2edb73
THJ
4455 if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4456 return;
29946878 4457
bb2edb73
THJ
4458 if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
4459 return;
4460
4461 rcu_read_lock();
4462 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4463 if (ath10k_mac_schedule_txq(hw, ac) == -EBUSY)
29946878 4464 break;
29946878 4465 }
29946878 4466 rcu_read_unlock();
29946878 4467}
3f04950f 4468EXPORT_SYMBOL(ath10k_mac_tx_push_pending);
29946878 4469
5e3dd157
KV
4470/************/
4471/* Scanning */
4472/************/
4473
5c81c7fd 4474void __ath10k_scan_finish(struct ath10k *ar)
5e3dd157 4475{
5c81c7fd 4476 lockdep_assert_held(&ar->data_lock);
5e3dd157 4477
5c81c7fd
MK
4478 switch (ar->scan.state) {
4479 case ATH10K_SCAN_IDLE:
4480 break;
4481 case ATH10K_SCAN_RUNNING:
7305d3e0 4482 case ATH10K_SCAN_ABORTING:
7947d3e0
AS
4483 if (!ar->scan.is_roc) {
4484 struct cfg80211_scan_info info = {
4485 .aborted = (ar->scan.state ==
4486 ATH10K_SCAN_ABORTING),
4487 };
4488
4489 ieee80211_scan_completed(ar->hw, &info);
4490 } else if (ar->scan.roc_notify) {
d710e75d 4491 ieee80211_remain_on_channel_expired(ar->hw);
7947d3e0 4492 }
1885c0f7 4493 fallthrough;
5c81c7fd
MK
4494 case ATH10K_SCAN_STARTING:
4495 ar->scan.state = ATH10K_SCAN_IDLE;
4496 ar->scan_channel = NULL;
bd877440 4497 ar->scan.roc_freq = 0;
5c81c7fd
MK
4498 ath10k_offchan_tx_purge(ar);
4499 cancel_delayed_work(&ar->scan.timeout);
881ed54e 4500 complete(&ar->scan.completed);
5c81c7fd 4501 break;
5e3dd157 4502 }
5c81c7fd 4503}
5e3dd157 4504
5c81c7fd
MK
4505void ath10k_scan_finish(struct ath10k *ar)
4506{
4507 spin_lock_bh(&ar->data_lock);
4508 __ath10k_scan_finish(ar);
5e3dd157
KV
4509 spin_unlock_bh(&ar->data_lock);
4510}
4511
5c81c7fd 4512static int ath10k_scan_stop(struct ath10k *ar)
5e3dd157
KV
4513{
4514 struct wmi_stop_scan_arg arg = {
4515 .req_id = 1, /* FIXME */
4516 .req_type = WMI_SCAN_STOP_ONE,
4517 .u.scan_id = ATH10K_SCAN_ID,
4518 };
4519 int ret;
4520
4521 lockdep_assert_held(&ar->conf_mutex);
4522
5e3dd157
KV
4523 ret = ath10k_wmi_stop_scan(ar, &arg);
4524 if (ret) {
7aa7a72a 4525 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
5c81c7fd 4526 goto out;
5e3dd157
KV
4527 }
4528
14e105cd 4529 ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
5c81c7fd 4530 if (ret == 0) {
7aa7a72a 4531 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
5c81c7fd
MK
4532 ret = -ETIMEDOUT;
4533 } else if (ret > 0) {
4534 ret = 0;
4535 }
5e3dd157 4536
5c81c7fd
MK
4537out:
4538 /* Scan state should be updated upon scan completion but in case
4539 * firmware fails to deliver the event (for whatever reason) it is
4540 * desired to clean up scan state anyway. Firmware may have just
4541 * dropped the scan completion event delivery due to transport pipe
4542 * being overflown with data and/or it can recover on its own before
4543 * next scan request is submitted.
4544 */
4545 spin_lock_bh(&ar->data_lock);
4546 if (ar->scan.state != ATH10K_SCAN_IDLE)
4547 __ath10k_scan_finish(ar);
4548 spin_unlock_bh(&ar->data_lock);
4549
4550 return ret;
4551}
4552
4553static void ath10k_scan_abort(struct ath10k *ar)
4554{
4555 int ret;
4556
4557 lockdep_assert_held(&ar->conf_mutex);
5e3dd157
KV
4558
4559 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
4560
4561 switch (ar->scan.state) {
4562 case ATH10K_SCAN_IDLE:
4563 /* This can happen if timeout worker kicked in and called
4564 * abortion while scan completion was being processed.
4565 */
4566 break;
4567 case ATH10K_SCAN_STARTING:
4568 case ATH10K_SCAN_ABORTING:
7aa7a72a 4569 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
5c81c7fd
MK
4570 ath10k_scan_state_str(ar->scan.state),
4571 ar->scan.state);
4572 break;
4573 case ATH10K_SCAN_RUNNING:
4574 ar->scan.state = ATH10K_SCAN_ABORTING;
4575 spin_unlock_bh(&ar->data_lock);
4576
4577 ret = ath10k_scan_stop(ar);
4578 if (ret)
7aa7a72a 4579 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
5c81c7fd
MK
4580
4581 spin_lock_bh(&ar->data_lock);
4582 break;
5e3dd157 4583 }
5c81c7fd 4584
5e3dd157 4585 spin_unlock_bh(&ar->data_lock);
5c81c7fd 4586}
5e3dd157 4587
5c81c7fd
MK
4588void ath10k_scan_timeout_work(struct work_struct *work)
4589{
4590 struct ath10k *ar = container_of(work, struct ath10k,
4591 scan.timeout.work);
4592
4593 mutex_lock(&ar->conf_mutex);
4594 ath10k_scan_abort(ar);
4595 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
4596}
4597
4598static int ath10k_start_scan(struct ath10k *ar,
4599 const struct wmi_start_scan_arg *arg)
4600{
4601 int ret;
4602
4603 lockdep_assert_held(&ar->conf_mutex);
4604
4605 ret = ath10k_wmi_start_scan(ar, arg);
4606 if (ret)
4607 return ret;
4608
14e105cd 4609 ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
5e3dd157 4610 if (ret == 0) {
5c81c7fd
MK
4611 ret = ath10k_scan_stop(ar);
4612 if (ret)
7aa7a72a 4613 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd
MK
4614
4615 return -ETIMEDOUT;
5e3dd157
KV
4616 }
4617
2f9eec0b
BG
4618 /* If we failed to start the scan, return error code at
4619 * this point. This is probably due to some issue in the
4620 * firmware, but no need to wedge the driver due to that...
4621 */
4622 spin_lock_bh(&ar->data_lock);
4623 if (ar->scan.state == ATH10K_SCAN_IDLE) {
4624 spin_unlock_bh(&ar->data_lock);
4625 return -EINVAL;
4626 }
4627 spin_unlock_bh(&ar->data_lock);
4628
5e3dd157
KV
4629 return 0;
4630}
4631
4632/**********************/
4633/* mac80211 callbacks */
4634/**********************/
4635
f2f6ecab
MK
4636static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4637 struct ieee80211_tx_control *control,
4638 struct sk_buff *skb)
5e3dd157 4639{
4b604558 4640 struct ath10k *ar = hw->priv;
6421969f 4641 struct ath10k_htt *htt = &ar->htt;
5e3dd157 4642 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4b604558 4643 struct ieee80211_vif *vif = info->control.vif;
75d85fd9 4644 struct ieee80211_sta *sta = control->sta;
dd4717b6 4645 struct ieee80211_txq *txq = NULL;
6421969f 4646 struct ieee80211_hdr *hdr = (void *)skb->data;
8a933964 4647 enum ath10k_hw_txrx_mode txmode;
6421969f
MK
4648 enum ath10k_mac_tx_path txpath;
4649 bool is_htt;
4650 bool is_mgmt;
4651 bool is_presp;
f2f6ecab 4652 int ret;
d1ce37b7 4653 u16 airtime;
5e3dd157 4654
d1ce37b7 4655 airtime = ath10k_mac_update_airtime(ar, txq, skb);
95a568c4 4656 ath10k_mac_tx_h_fill_cb(ar, vif, txq, sta, skb, airtime);
5e3dd157 4657
8a933964 4658 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
6421969f
MK
4659 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4660 is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4661 txpath == ATH10K_MAC_TX_HTT_MGMT);
cac08552 4662 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
5e3dd157 4663
6421969f
MK
4664 if (is_htt) {
4665 spin_lock_bh(&ar->htt.tx_lock);
6421969f
MK
4666 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4667
cac08552 4668 ret = ath10k_htt_tx_inc_pending(htt);
6421969f
MK
4669 if (ret) {
4670 ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4671 ret);
4672 spin_unlock_bh(&ar->htt.tx_lock);
4673 ieee80211_free_txskb(ar->hw, skb);
4674 return;
4675 }
4676
cac08552
RM
4677 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4678 if (ret) {
dd7c280f
RM
4679 ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4680 ret);
cac08552
RM
4681 ath10k_htt_tx_dec_pending(htt);
4682 spin_unlock_bh(&ar->htt.tx_lock);
4683 ieee80211_free_txskb(ar->hw, skb);
4684 return;
4685 }
6421969f
MK
4686 spin_unlock_bh(&ar->htt.tx_lock);
4687 }
4688
cc6df017 4689 ret = ath10k_mac_tx(ar, vif, txmode, txpath, skb, false);
6421969f 4690 if (ret) {
f2f6ecab 4691 ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
6421969f
MK
4692 if (is_htt) {
4693 spin_lock_bh(&ar->htt.tx_lock);
cac08552
RM
4694 ath10k_htt_tx_dec_pending(htt);
4695 if (is_mgmt)
4696 ath10k_htt_tx_mgmt_dec_pending(htt);
6421969f
MK
4697 spin_unlock_bh(&ar->htt.tx_lock);
4698 }
4699 return;
4700 }
5e3dd157
KV
4701}
4702
29946878
MK
4703static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4704 struct ieee80211_txq *txq)
4705{
4706 struct ath10k *ar = hw->priv;
bb2edb73
THJ
4707 int ret;
4708 u8 ac;
29946878 4709
bb2edb73
THJ
4710 ath10k_htt_tx_txq_update(hw, txq);
4711 if (ar->htt.tx_q_state.mode != HTT_TX_MODE_SWITCH_PUSH)
4712 return;
83e164b7 4713
bb2edb73
THJ
4714 ac = txq->ac;
4715 ieee80211_txq_schedule_start(hw, ac);
4716 txq = ieee80211_next_txq(hw, ac);
4717 if (!txq)
4718 goto out;
83e164b7 4719
bb2edb73
THJ
4720 while (ath10k_mac_tx_can_push(hw, txq)) {
4721 ret = ath10k_mac_tx_push_txq(hw, txq);
e3148cc5 4722 if (ret < 0)
83e164b7
RM
4723 break;
4724 }
2b4a6698 4725 ieee80211_return_txq(hw, txq, false);
c1a43d97 4726 ath10k_htt_tx_txq_update(hw, txq);
bb2edb73
THJ
4727out:
4728 ieee80211_txq_schedule_end(hw, ac);
29946878
MK
4729}
4730
bca7bafb 4731/* Must not be called with conf_mutex held as workers can use that also. */
7962b0d8 4732void ath10k_drain_tx(struct ath10k *ar)
bca7bafb 4733{
bdb1050e
SK
4734 lockdep_assert_not_held(&ar->conf_mutex);
4735
bca7bafb
MK
4736 /* make sure rcu-protected mac80211 tx path itself is drained */
4737 synchronize_net();
4738
4739 ath10k_offchan_tx_purge(ar);
4740 ath10k_mgmt_over_wmi_tx_purge(ar);
4741
4742 cancel_work_sync(&ar->offchan_tx_work);
4743 cancel_work_sync(&ar->wmi_mgmt_tx_work);
4744}
4745
affd3217 4746void ath10k_halt(struct ath10k *ar)
818bdd16 4747{
d9bc4b9b
MK
4748 struct ath10k_vif *arvif;
4749
818bdd16
MK
4750 lockdep_assert_held(&ar->conf_mutex);
4751
1933747f
MK
4752 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4753 ar->filter_flags = 0;
4754 ar->monitor = false;
500ff9f9 4755 ar->monitor_arvif = NULL;
1933747f
MK
4756
4757 if (ar->monitor_started)
1bbc0975 4758 ath10k_monitor_stop(ar);
1933747f
MK
4759
4760 ar->monitor_started = false;
96d828d4 4761 ar->tx_paused = 0;
1bbc0975 4762
5c81c7fd 4763 ath10k_scan_finish(ar);
818bdd16 4764 ath10k_peer_cleanup_all(ar);
6f6eb1bc 4765 ath10k_stop_radar_confirmation(ar);
818bdd16
MK
4766 ath10k_core_stop(ar);
4767 ath10k_hif_power_down(ar);
4768
4769 spin_lock_bh(&ar->data_lock);
64badcb6
MK
4770 list_for_each_entry(arvif, &ar->arvifs, list)
4771 ath10k_mac_vif_beacon_cleanup(arvif);
818bdd16
MK
4772 spin_unlock_bh(&ar->data_lock);
4773}
4774
46acf7bb
BG
4775static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4776{
4777 struct ath10k *ar = hw->priv;
4778
4779 mutex_lock(&ar->conf_mutex);
4780
166de3f1
RM
4781 *tx_ant = ar->cfg_tx_chainmask;
4782 *rx_ant = ar->cfg_rx_chainmask;
46acf7bb
BG
4783
4784 mutex_unlock(&ar->conf_mutex);
4785
4786 return 0;
4787}
4788
aac392d8 4789static bool ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
5572a95b
BG
4790{
4791 /* It is not clear that allowing gaps in chainmask
4792 * is helpful. Probably it will not do what user
4793 * is hoping for, so warn in that case.
4794 */
4795 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
aac392d8 4796 return true;
5572a95b 4797
aac392d8 4798 ath10k_warn(ar, "mac %s antenna chainmask is invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
5572a95b 4799 dbg, cm);
aac392d8 4800 return false;
5572a95b
BG
4801}
4802
f58512f3
RM
4803static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4804{
4805 int nsts = ar->vht_cap_info;
4806
4807 nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4808 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4809
4810 /* If firmware does not deliver to host number of space-time
4811 * streams supported, assume it support up to 4 BF STS and return
4812 * the value for VHT CAP: nsts-1)
4813 */
4814 if (nsts == 0)
4815 return 3;
4816
4817 return nsts;
4818}
4819
4820static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4821{
4822 int sound_dim = ar->vht_cap_info;
4823
4824 sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4825 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4826
4827 /* If the sounding dimension is not advertised by the firmware,
4828 * let's use a default value of 1
4829 */
4830 if (sound_dim == 0)
4831 return 1;
4832
4833 return sound_dim;
4834}
4835
4836static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4837{
4838 struct ieee80211_sta_vht_cap vht_cap = {0};
cc914a55 4839 struct ath10k_hw_params *hw = &ar->hw_params;
f58512f3
RM
4840 u16 mcs_map;
4841 u32 val;
4842 int i;
4843
4844 vht_cap.vht_supported = 1;
4845 vht_cap.cap = ar->vht_cap_info;
4846
4847 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4848 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4849 val = ath10k_mac_get_vht_cap_bf_sts(ar);
4850 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4851 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4852
4853 vht_cap.cap |= val;
4854 }
4855
4856 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4857 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4858 val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4859 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4860 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4861
4862 vht_cap.cap |= val;
4863 }
4864
4865 mcs_map = 0;
4866 for (i = 0; i < 8; i++) {
4867 if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4868 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4869 else
4870 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4871 }
4872
34663241
MSS
4873 if (ar->cfg_tx_chainmask <= 1)
4874 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4875
f58512f3
RM
4876 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4877 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4878
cc914a55
BG
4879 /* If we are supporting 160Mhz or 80+80, then the NIC may be able to do
4880 * a restricted NSS for 160 or 80+80 vs what it can do for 80Mhz. Give
4881 * user-space a clue if that is the case.
4882 */
4883 if ((vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK) &&
4884 (hw->vht160_mcs_rx_highest != 0 ||
4885 hw->vht160_mcs_tx_highest != 0)) {
4886 vht_cap.vht_mcs.rx_highest = cpu_to_le16(hw->vht160_mcs_rx_highest);
4887 vht_cap.vht_mcs.tx_highest = cpu_to_le16(hw->vht160_mcs_tx_highest);
4888 }
4889
f58512f3
RM
4890 return vht_cap;
4891}
4892
4893static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4894{
4895 int i;
4896 struct ieee80211_sta_ht_cap ht_cap = {0};
4897
4898 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4899 return ht_cap;
4900
4901 ht_cap.ht_supported = 1;
4902 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4903 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4904 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4905 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
e33a99e2
PO
4906 ht_cap.cap |=
4907 WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
f58512f3
RM
4908
4909 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4910 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4911
4912 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4913 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4914
4915 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4916 u32 smps;
4917
4918 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4919 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4920
4921 ht_cap.cap |= smps;
4922 }
4923
34663241 4924 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
f58512f3
RM
4925 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4926
4927 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4928 u32 stbc;
4929
4930 stbc = ar->ht_cap_info;
4931 stbc &= WMI_HT_CAP_RX_STBC;
4932 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4933 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4934 stbc &= IEEE80211_HT_CAP_RX_STBC;
4935
4936 ht_cap.cap |= stbc;
4937 }
4938
ff488d0e
SV
4939 if (ar->ht_cap_info & WMI_HT_CAP_LDPC || (ar->ht_cap_info &
4940 WMI_HT_CAP_RX_LDPC && (ar->ht_cap_info & WMI_HT_CAP_TX_LDPC)))
f58512f3
RM
4941 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4942
4943 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4944 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4945
4946 /* max AMSDU is implicitly taken from vht_cap_info */
4947 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4948 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4949
4950 for (i = 0; i < ar->num_rf_chains; i++) {
4951 if (ar->cfg_rx_chainmask & BIT(i))
4952 ht_cap.mcs.rx_mask[i] = 0xFF;
4953 }
4954
4955 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4956
4957 return ht_cap;
4958}
4959
5036fe0f
RM
4960static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4961{
4962 struct ieee80211_supported_band *band;
4963 struct ieee80211_sta_vht_cap vht_cap;
4964 struct ieee80211_sta_ht_cap ht_cap;
4965
4966 ht_cap = ath10k_get_ht_cap(ar);
4967 vht_cap = ath10k_create_vht_cap(ar);
4968
4969 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
57fbcce3 4970 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5036fe0f 4971 band->ht_cap = ht_cap;
5036fe0f
RM
4972 }
4973 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
57fbcce3 4974 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5036fe0f
RM
4975 band->ht_cap = ht_cap;
4976 band->vht_cap = vht_cap;
4977 }
4978}
4979
46acf7bb
BG
4980static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4981{
4982 int ret;
aac392d8 4983 bool is_valid_tx_chain_mask, is_valid_rx_chain_mask;
46acf7bb
BG
4984
4985 lockdep_assert_held(&ar->conf_mutex);
4986
aac392d8
MK
4987 is_valid_tx_chain_mask = ath10k_check_chain_mask(ar, tx_ant, "tx");
4988 is_valid_rx_chain_mask = ath10k_check_chain_mask(ar, rx_ant, "rx");
4989
4990 if (!is_valid_tx_chain_mask || !is_valid_rx_chain_mask)
4991 return -EINVAL;
5572a95b 4992
46acf7bb
BG
4993 ar->cfg_tx_chainmask = tx_ant;
4994 ar->cfg_rx_chainmask = rx_ant;
4995
4996 if ((ar->state != ATH10K_STATE_ON) &&
4997 (ar->state != ATH10K_STATE_RESTARTED))
4998 return 0;
4999
5000 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
5001 tx_ant);
5002 if (ret) {
7aa7a72a 5003 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
5004 ret, tx_ant);
5005 return ret;
5006 }
5007
5008 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
5009 rx_ant);
5010 if (ret) {
7aa7a72a 5011 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
46acf7bb
BG
5012 ret, rx_ant);
5013 return ret;
5014 }
5015
5036fe0f
RM
5016 /* Reload HT/VHT capability */
5017 ath10k_mac_setup_ht_vht_cap(ar);
5018
46acf7bb
BG
5019 return 0;
5020}
5021
5022static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
5023{
5024 struct ath10k *ar = hw->priv;
5025 int ret;
5026
5027 mutex_lock(&ar->conf_mutex);
5028 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
5029 mutex_unlock(&ar->conf_mutex);
5030 return ret;
5031}
5032
84758d4d
BP
5033static int __ath10k_fetch_bb_timing_dt(struct ath10k *ar,
5034 struct wmi_bb_timing_cfg_arg *bb_timing)
5035{
5036 struct device_node *node;
5037 const char *fem_name;
5038 int ret;
5039
5040 node = ar->dev->of_node;
5041 if (!node)
5042 return -ENOENT;
5043
5044 ret = of_property_read_string_index(node, "ext-fem-name", 0, &fem_name);
5045 if (ret)
5046 return -ENOENT;
5047
5048 /*
5049 * If external Front End module used in hardware, then default base band timing
5050 * parameter cannot be used since they were fine tuned for reference hardware,
5051 * so choosing different value suitable for that external FEM.
5052 */
5053 if (!strcmp("microsemi-lx5586", fem_name)) {
5054 bb_timing->bb_tx_timing = 0x00;
5055 bb_timing->bb_xpa_timing = 0x0101;
5056 } else {
5057 return -ENOENT;
5058 }
5059
5060 ath10k_dbg(ar, ATH10K_DBG_BOOT, "boot bb_tx_timing 0x%x bb_xpa_timing 0x%x\n",
5061 bb_timing->bb_tx_timing, bb_timing->bb_xpa_timing);
5062 return 0;
5063}
5064
1382993f
WG
5065static int ath10k_mac_rfkill_config(struct ath10k *ar)
5066{
5067 u32 param;
5068 int ret;
5069
5070 if (ar->hw_values->rfkill_pin == 0) {
5071 ath10k_warn(ar, "ath10k does not support hardware rfkill with this device\n");
5072 return -EOPNOTSUPP;
5073 }
5074
5075 ath10k_dbg(ar, ATH10K_DBG_MAC,
5076 "mac rfkill_pin %d rfkill_cfg %d rfkill_on_level %d",
5077 ar->hw_values->rfkill_pin, ar->hw_values->rfkill_cfg,
5078 ar->hw_values->rfkill_on_level);
5079
5080 param = FIELD_PREP(WMI_TLV_RFKILL_CFG_RADIO_LEVEL,
5081 ar->hw_values->rfkill_on_level) |
5082 FIELD_PREP(WMI_TLV_RFKILL_CFG_GPIO_PIN_NUM,
5083 ar->hw_values->rfkill_pin) |
5084 FIELD_PREP(WMI_TLV_RFKILL_CFG_PIN_AS_GPIO,
5085 ar->hw_values->rfkill_cfg);
5086
5087 ret = ath10k_wmi_pdev_set_param(ar,
5088 ar->wmi.pdev_param->rfkill_config,
5089 param);
5090 if (ret) {
5091 ath10k_warn(ar,
5092 "failed to set rfkill config 0x%x: %d\n",
5093 param, ret);
5094 return ret;
5095 }
5096 return 0;
5097}
5098
5099int ath10k_mac_rfkill_enable_radio(struct ath10k *ar, bool enable)
5100{
5101 enum wmi_tlv_rfkill_enable_radio param;
5102 int ret;
5103
5104 if (enable)
5105 param = WMI_TLV_RFKILL_ENABLE_RADIO_ON;
5106 else
5107 param = WMI_TLV_RFKILL_ENABLE_RADIO_OFF;
5108
5109 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac rfkill enable %d", param);
5110
5111 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rfkill_enable,
5112 param);
5113 if (ret) {
5114 ath10k_warn(ar, "failed to set rfkill enable param %d: %d\n",
5115 param, ret);
5116 return ret;
5117 }
5118
5119 return 0;
5120}
5121
5e3dd157
KV
5122static int ath10k_start(struct ieee80211_hw *hw)
5123{
5124 struct ath10k *ar = hw->priv;
1fe374f5 5125 u32 param;
818bdd16 5126 int ret = 0;
84758d4d 5127 struct wmi_bb_timing_cfg_arg bb_timing = {0};
5e3dd157 5128
bca7bafb
MK
5129 /*
5130 * This makes sense only when restarting hw. It is harmless to call
6dfdbfc7 5131 * unconditionally. This is necessary to make sure no HTT/WMI tx
bca7bafb
MK
5132 * commands will be submitted while restarting.
5133 */
5134 ath10k_drain_tx(ar);
5135
548db54c
MK
5136 mutex_lock(&ar->conf_mutex);
5137
c5058f5b
MK
5138 switch (ar->state) {
5139 case ATH10K_STATE_OFF:
5140 ar->state = ATH10K_STATE_ON;
5141 break;
5142 case ATH10K_STATE_RESTARTING:
c5058f5b
MK
5143 ar->state = ATH10K_STATE_RESTARTED;
5144 break;
5145 case ATH10K_STATE_ON:
5146 case ATH10K_STATE_RESTARTED:
5147 case ATH10K_STATE_WEDGED:
5148 WARN_ON(1);
818bdd16 5149 ret = -EINVAL;
ae254433 5150 goto err;
43d2a30f
KV
5151 case ATH10K_STATE_UTF:
5152 ret = -EBUSY;
5153 goto err;
818bdd16
MK
5154 }
5155
1382993f
WG
5156 spin_lock_bh(&ar->data_lock);
5157
5158 if (ar->hw_rfkill_on) {
5159 ar->hw_rfkill_on = false;
5160 spin_unlock_bh(&ar->data_lock);
5161 goto err;
5162 }
5163
5164 spin_unlock_bh(&ar->data_lock);
5165
3c545a25 5166 ret = ath10k_hif_power_up(ar, ATH10K_FIRMWARE_MODE_NORMAL);
818bdd16 5167 if (ret) {
7aa7a72a 5168 ath10k_err(ar, "Could not init hif: %d\n", ret);
ae254433 5169 goto err_off;
818bdd16
MK
5170 }
5171
7ebf721d
KV
5172 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
5173 &ar->normal_mode_fw);
818bdd16 5174 if (ret) {
7aa7a72a 5175 ath10k_err(ar, "Could not init core: %d\n", ret);
ae254433 5176 goto err_power_down;
818bdd16
MK
5177 }
5178
1382993f
WG
5179 if (ar->sys_cap_info & WMI_TLV_SYS_CAP_INFO_RFKILL) {
5180 ret = ath10k_mac_rfkill_config(ar);
5181 if (ret && ret != -EOPNOTSUPP) {
5182 ath10k_warn(ar, "failed to configure rfkill: %d", ret);
5183 goto err_core_stop;
5184 }
5185 }
5186
1fe374f5
MSS
5187 param = ar->wmi.pdev_param->pmf_qos;
5188 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
ae254433 5189 if (ret) {
7aa7a72a 5190 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
ae254433
MK
5191 goto err_core_stop;
5192 }
5e3dd157 5193
1fe374f5
MSS
5194 param = ar->wmi.pdev_param->dynamic_bw;
5195 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
ae254433 5196 if (ret) {
7aa7a72a 5197 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
ae254433
MK
5198 goto err_core_stop;
5199 }
5e3dd157 5200
f1157695
RP
5201 if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
5202 ret = ath10k_wmi_scan_prob_req_oui(ar, ar->mac_addr);
5203 if (ret) {
5204 ath10k_err(ar, "failed to set prob req oui: %i\n", ret);
5205 goto err_core_stop;
5206 }
5207 }
5208
cf32784c
MK
5209 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
5210 ret = ath10k_wmi_adaptive_qcs(ar, true);
5211 if (ret) {
5212 ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
5213 ret);
5214 goto err_core_stop;
5215 }
5216 }
5217
24ab13ef 5218 if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
1fe374f5
MSS
5219 param = ar->wmi.pdev_param->burst_enable;
5220 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
24ab13ef
JD
5221 if (ret) {
5222 ath10k_warn(ar, "failed to disable burst: %d\n", ret);
5223 goto err_core_stop;
5224 }
5225 }
5226
d5cded16
GS
5227 param = ar->wmi.pdev_param->idle_ps_config;
5228 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
5229 if (ret && ret != -EOPNOTSUPP) {
5230 ath10k_warn(ar, "failed to enable idle_ps_config: %d\n", ret);
5231 goto err_core_stop;
5232 }
5233
166de3f1 5234 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
46acf7bb 5235
ab6258ed
MP
5236 /*
5237 * By default FW set ARP frames ac to voice (6). In that case ARP
5238 * exchange is not working properly for UAPSD enabled AP. ARP requests
5239 * which arrives with access category 0 are processed by network stack
5240 * and send back with access category 0, but FW changes access category
5241 * to 6. Set ARP frames access category to best effort (0) solves
5242 * this problem.
5243 */
5244
1fe374f5
MSS
5245 param = ar->wmi.pdev_param->arp_ac_override;
5246 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
ab6258ed 5247 if (ret) {
7aa7a72a 5248 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
ab6258ed 5249 ret);
ae254433 5250 goto err_core_stop;
ab6258ed
MP
5251 }
5252
62f77f09 5253 if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
c4cdf753 5254 ar->running_fw->fw_file.fw_features)) {
62f77f09
M
5255 ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
5256 WMI_CCA_DETECT_LEVEL_AUTO,
5257 WMI_CCA_DETECT_MARGIN_AUTO);
5258 if (ret) {
5259 ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
5260 ret);
5261 goto err_core_stop;
5262 }
5263 }
5264
1fe374f5
MSS
5265 param = ar->wmi.pdev_param->ani_enable;
5266 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
575f1c3d
ARN
5267 if (ret) {
5268 ath10k_warn(ar, "failed to enable ani by default: %d\n",
5269 ret);
5270 goto err_core_stop;
5271 }
5272
b3e71d7a
ARN
5273 ar->ani_enabled = true;
5274
cc61a1bb 5275 if (ath10k_peer_stats_enabled(ar)) {
8351c052
MSS
5276 param = ar->wmi.pdev_param->peer_stats_update_period;
5277 ret = ath10k_wmi_pdev_set_param(ar, param,
5278 PEER_DEFAULT_STATS_UPDATE_PERIOD);
5279 if (ret) {
5280 ath10k_warn(ar,
5281 "failed to set peer stats period : %d\n",
5282 ret);
5283 goto err_core_stop;
5284 }
5285 }
5286
39136248
RM
5287 param = ar->wmi.pdev_param->enable_btcoex;
5288 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
5289 test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
9f83993e
TC
5290 ar->running_fw->fw_file.fw_features) &&
5291 ar->coex_support) {
39136248
RM
5292 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
5293 if (ret) {
5294 ath10k_warn(ar,
5295 "failed to set btcoex param: %d\n", ret);
5296 goto err_core_stop;
5297 }
5298 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
5299 }
5300
84758d4d
BP
5301 if (test_bit(WMI_SERVICE_BB_TIMING_CONFIG_SUPPORT, ar->wmi.svc_map)) {
5302 ret = __ath10k_fetch_bb_timing_dt(ar, &bb_timing);
5303 if (!ret) {
5304 ret = ath10k_wmi_pdev_bb_timing(ar, &bb_timing);
5305 if (ret) {
5306 ath10k_warn(ar,
5307 "failed to set bb timings: %d\n",
5308 ret);
5309 goto err_core_stop;
5310 }
5311 }
5312 }
5313
d650097b 5314 ar->num_started_vdevs = 0;
f7843d7f
MK
5315 ath10k_regd_update(ar);
5316
855aed12 5317 ath10k_spectral_start(ar);
8515b5c7 5318 ath10k_thermal_set_throttling(ar);
855aed12 5319
6f6eb1bc
S
5320 ar->radar_conf_state = ATH10K_RADAR_CONFIRMATION_IDLE;
5321
ae254433
MK
5322 mutex_unlock(&ar->conf_mutex);
5323 return 0;
5324
5325err_core_stop:
5326 ath10k_core_stop(ar);
5327
5328err_power_down:
5329 ath10k_hif_power_down(ar);
5330
5331err_off:
5332 ar->state = ATH10K_STATE_OFF;
5333
5334err:
548db54c 5335 mutex_unlock(&ar->conf_mutex);
c60bdd83 5336 return ret;
5e3dd157
KV
5337}
5338
5339static void ath10k_stop(struct ieee80211_hw *hw)
5340{
5341 struct ath10k *ar = hw->priv;
5342
bca7bafb
MK
5343 ath10k_drain_tx(ar);
5344
548db54c 5345 mutex_lock(&ar->conf_mutex);
c5058f5b 5346 if (ar->state != ATH10K_STATE_OFF) {
1382993f
WG
5347 if (!ar->hw_rfkill_on)
5348 ath10k_halt(ar);
c5058f5b
MK
5349 ar->state = ATH10K_STATE_OFF;
5350 }
548db54c
MK
5351 mutex_unlock(&ar->conf_mutex);
5352
d94475c2 5353 cancel_work_sync(&ar->set_coverage_class_work);
5c81c7fd 5354 cancel_delayed_work_sync(&ar->scan.timeout);
affd3217 5355 cancel_work_sync(&ar->restart_work);
5e3dd157
KV
5356}
5357
ad088bfa 5358static int ath10k_config_ps(struct ath10k *ar)
5e3dd157 5359{
ad088bfa
MK
5360 struct ath10k_vif *arvif;
5361 int ret = 0;
affd3217
MK
5362
5363 lockdep_assert_held(&ar->conf_mutex);
5364
ad088bfa
MK
5365 list_for_each_entry(arvif, &ar->arvifs, list) {
5366 ret = ath10k_mac_vif_setup_ps(arvif);
5367 if (ret) {
7aa7a72a 5368 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
ad088bfa
MK
5369 break;
5370 }
5371 }
affd3217 5372
ad088bfa 5373 return ret;
affd3217
MK
5374}
5375
5376static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
5377{
5e3dd157
KV
5378 struct ath10k *ar = hw->priv;
5379 struct ieee80211_conf *conf = &hw->conf;
5380 int ret = 0;
5e3dd157
KV
5381
5382 mutex_lock(&ar->conf_mutex);
5383
affd3217
MK
5384 if (changed & IEEE80211_CONF_CHANGE_PS)
5385 ath10k_config_ps(ar);
5e3dd157
KV
5386
5387 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
1933747f
MK
5388 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
5389 ret = ath10k_monitor_recalc(ar);
5390 if (ret)
5391 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5e3dd157
KV
5392 }
5393
5394 mutex_unlock(&ar->conf_mutex);
5395 return ret;
5396}
5397
5572a95b
BG
5398static u32 get_nss_from_chainmask(u16 chain_mask)
5399{
f680f70a 5400 if ((chain_mask & 0xf) == 0xf)
5572a95b
BG
5401 return 4;
5402 else if ((chain_mask & 0x7) == 0x7)
5403 return 3;
5404 else if ((chain_mask & 0x3) == 0x3)
5405 return 2;
5406 return 1;
5407}
5408
a48e2cc8
VN
5409static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
5410{
5411 u32 value = 0;
5412 struct ath10k *ar = arvif->ar;
707a0c81 5413 int nsts;
0c6d6f26 5414 int sound_dim;
a48e2cc8
VN
5415
5416 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
5417 return 0;
5418
707a0c81 5419 nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
a48e2cc8
VN
5420 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5421 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
707a0c81 5422 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
a48e2cc8 5423
0c6d6f26 5424 sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
a48e2cc8
VN
5425 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5426 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
0c6d6f26 5427 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
a48e2cc8
VN
5428
5429 if (!value)
5430 return 0;
5431
5432 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
5433 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
5434
5435 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
5436 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
5437 WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
5438
5439 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
5440 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
5441
5442 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
5443 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
5444 WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
5445
5446 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5447 ar->wmi.vdev_param->txbf, value);
5448}
5449
5e3dd157
KV
5450/*
5451 * TODO:
5452 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
5453 * because we will send mgmt frames without CCK. This requirement
5454 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
5455 * in the TX packet.
5456 */
5457static int ath10k_add_interface(struct ieee80211_hw *hw,
5458 struct ieee80211_vif *vif)
5459{
5460 struct ath10k *ar = hw->priv;
56ac13bf 5461 struct ath10k_vif *arvif = (void *)vif->drv_priv;
bb8f0c6a 5462 struct ath10k_peer *peer;
5e3dd157
KV
5463 enum wmi_sta_powersave_param param;
5464 int ret = 0;
5a13e76e 5465 u32 value;
5e3dd157 5466 int bit;
96d828d4 5467 int i;
6d1506e7 5468 u32 vdev_param;
5e3dd157 5469
848955cc
JB
5470 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
5471
5e3dd157
KV
5472 mutex_lock(&ar->conf_mutex);
5473
0dbd09e6 5474 memset(arvif, 0, sizeof(*arvif));
29946878 5475 ath10k_mac_txq_init(vif->txq);
0dbd09e6 5476
5e3dd157
KV
5477 arvif->ar = ar;
5478 arvif->vif = vif;
5479
e63b33f3 5480 INIT_LIST_HEAD(&arvif->list);
81a9a17d 5481 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
cc9904e6
MK
5482 INIT_DELAYED_WORK(&arvif->connection_loss_work,
5483 ath10k_mac_vif_sta_connection_loss_work);
cc4827b9 5484
45c9abc0
MK
5485 for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
5486 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
5487 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
5488 sizeof(arvif->bitrate_mask.control[i].ht_mcs));
5489 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
5490 sizeof(arvif->bitrate_mask.control[i].vht_mcs));
5491 }
cc4827b9 5492
e04cafbc
MK
5493 if (ar->num_peers >= ar->max_num_peers) {
5494 ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
503422d9
MK
5495 ret = -ENOBUFS;
5496 goto err;
e04cafbc
MK
5497 }
5498
a9aefb3b 5499 if (ar->free_vdev_map == 0) {
7aa7a72a 5500 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
5e3dd157 5501 ret = -EBUSY;
9dad14ae 5502 goto err;
5e3dd157 5503 }
16c11176 5504 bit = __ffs64(ar->free_vdev_map);
5e3dd157 5505
16c11176
BG
5506 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
5507 bit, ar->free_vdev_map);
5e3dd157 5508
16c11176 5509 arvif->vdev_id = bit;
6e4de1a4
PO
5510 arvif->vdev_subtype =
5511 ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
5e3dd157 5512
5e3dd157 5513 switch (vif->type) {
75d2bd48
MK
5514 case NL80211_IFTYPE_P2P_DEVICE:
5515 arvif->vdev_type = WMI_VDEV_TYPE_STA;
6e4de1a4
PO
5516 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5517 (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
75d2bd48 5518 break;
5e3dd157
KV
5519 case NL80211_IFTYPE_UNSPECIFIED:
5520 case NL80211_IFTYPE_STATION:
5521 arvif->vdev_type = WMI_VDEV_TYPE_STA;
5522 if (vif->p2p)
6e4de1a4
PO
5523 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5524 (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
5e3dd157
KV
5525 break;
5526 case NL80211_IFTYPE_ADHOC:
5527 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
5528 break;
b6c7bafa 5529 case NL80211_IFTYPE_MESH_POINT:
0b3d76e9 5530 if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
6e4de1a4
PO
5531 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5532 (ar, WMI_VDEV_SUBTYPE_MESH_11S);
bb58b89c 5533 } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
b6c7bafa
BC
5534 ret = -EINVAL;
5535 ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
5536 goto err;
5537 }
5538 arvif->vdev_type = WMI_VDEV_TYPE_AP;
5539 break;
5e3dd157
KV
5540 case NL80211_IFTYPE_AP:
5541 arvif->vdev_type = WMI_VDEV_TYPE_AP;
5542
5543 if (vif->p2p)
6e4de1a4
PO
5544 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
5545 (ar, WMI_VDEV_SUBTYPE_P2P_GO);
5e3dd157
KV
5546 break;
5547 case NL80211_IFTYPE_MONITOR:
5548 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
5549 break;
5550 default:
5551 WARN_ON(1);
5552 break;
5553 }
5554
96d828d4
MK
5555 /* Using vdev_id as queue number will make it very easy to do per-vif
5556 * tx queue locking. This shouldn't wrap due to interface combinations
5557 * but do a modulo for correctness sake and prevent using offchannel tx
5558 * queues for regular vif tx.
5559 */
5560 vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5561 for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
5562 vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
5563
64badcb6
MK
5564 /* Some firmware revisions don't wait for beacon tx completion before
5565 * sending another SWBA event. This could lead to hardware using old
5566 * (freed) beacon data in some cases, e.g. tx credit starvation
762fd1ae 5567 * combined with missed TBTT. This is very rare.
64badcb6
MK
5568 *
5569 * On non-IOMMU-enabled hosts this could be a possible security issue
5570 * because hw could beacon some random data on the air. On
5571 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
5572 * device would crash.
5573 *
5574 * Since there are no beacon tx completions (implicit nor explicit)
5575 * propagated to host the only workaround for this is to allocate a
5576 * DMA-coherent buffer for a lifetime of a vif and use it for all
5577 * beacon tx commands. Worst case for this approach is some beacons may
5578 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
5579 */
5580 if (vif->type == NL80211_IFTYPE_ADHOC ||
b6c7bafa 5581 vif->type == NL80211_IFTYPE_MESH_POINT ||
64badcb6 5582 vif->type == NL80211_IFTYPE_AP) {
4da8bc2c
AS
5583 if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL) {
5584 arvif->beacon_buf = kmalloc(IEEE80211_MAX_FRAME_LEN,
5585 GFP_KERNEL);
5586 arvif->beacon_paddr = (dma_addr_t)arvif->beacon_buf;
5587 } else {
5588 arvif->beacon_buf =
5589 dma_alloc_coherent(ar->dev,
5590 IEEE80211_MAX_FRAME_LEN,
5591 &arvif->beacon_paddr,
5592 GFP_ATOMIC);
5593 }
64badcb6
MK
5594 if (!arvif->beacon_buf) {
5595 ret = -ENOMEM;
5596 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
5597 ret);
5598 goto err;
5599 }
5600 }
ccec9038
DL
5601 if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
5602 arvif->nohwcrypt = true;
5603
5604 if (arvif->nohwcrypt &&
5605 !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
e9ca70c7 5606 ret = -EINVAL;
ccec9038
DL
5607 ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
5608 goto err;
5609 }
64badcb6
MK
5610
5611 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
5612 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
5613 arvif->beacon_buf ? "single-buf" : "per-skb");
5e3dd157
KV
5614
5615 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
5616 arvif->vdev_subtype, vif->addr);
5617 if (ret) {
7aa7a72a 5618 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
69244e56 5619 arvif->vdev_id, ret);
9dad14ae 5620 goto err;
5e3dd157
KV
5621 }
5622
68c295f2
SM
5623 if (test_bit(WMI_SERVICE_VDEV_DISABLE_4_ADDR_SRC_LRN_SUPPORT,
5624 ar->wmi.svc_map)) {
5625 vdev_param = ar->wmi.vdev_param->disable_4addr_src_lrn;
5626 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5627 WMI_VDEV_DISABLE_4_ADDR_SRC_LRN);
5628 if (ret && ret != -EOPNOTSUPP) {
5629 ath10k_warn(ar, "failed to disable 4addr src lrn vdev %i: %d\n",
5630 arvif->vdev_id, ret);
5631 }
5632 }
5633
16c11176 5634 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
ebaa4b16 5635 spin_lock_bh(&ar->data_lock);
0579119f 5636 list_add(&arvif->list, &ar->arvifs);
ebaa4b16 5637 spin_unlock_bh(&ar->data_lock);
9dad14ae 5638
46725b15
MK
5639 /* It makes no sense to have firmware do keepalives. mac80211 already
5640 * takes care of this with idle connection polling.
5641 */
5642 ret = ath10k_mac_vif_disable_keepalive(arvif);
9dad14ae 5643 if (ret) {
46725b15 5644 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
69244e56 5645 arvif->vdev_id, ret);
9dad14ae
MK
5646 goto err_vdev_delete;
5647 }
5e3dd157 5648
627613f8 5649 arvif->def_wep_key_idx = -1;
5e3dd157 5650
6d1506e7
BM
5651 vdev_param = ar->wmi.vdev_param->tx_encap_type;
5652 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 5653 ATH10K_HW_TXRX_NATIVE_WIFI);
ebc9abdd 5654 /* 10.X firmware does not support this VDEV parameter. Do not warn */
9dad14ae 5655 if (ret && ret != -EOPNOTSUPP) {
7aa7a72a 5656 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
69244e56 5657 arvif->vdev_id, ret);
9dad14ae
MK
5658 goto err_vdev_delete;
5659 }
5e3dd157 5660
8a75fc54
RM
5661 /* Configuring number of spatial stream for monitor interface is causing
5662 * target assert in qca9888 and qca6174.
5663 */
5664 if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
5572a95b
BG
5665 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5666
5667 vdev_param = ar->wmi.vdev_param->nss;
5668 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5669 nss);
5670 if (ret) {
5671 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5672 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5673 ret);
5674 goto err_vdev_delete;
5675 }
5676 }
5677
e57e0571
MK
5678 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5679 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
6942726f
MK
5680 ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5681 vif->addr, WMI_PEER_TYPE_DEFAULT);
5e3dd157 5682 if (ret) {
e57e0571 5683 ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
69244e56 5684 arvif->vdev_id, ret);
9dad14ae 5685 goto err_vdev_delete;
5e3dd157 5686 }
bb8f0c6a
MK
5687
5688 spin_lock_bh(&ar->data_lock);
5689
5690 peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5691 if (!peer) {
5692 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5693 vif->addr, arvif->vdev_id);
5694 spin_unlock_bh(&ar->data_lock);
5695 ret = -ENOENT;
5696 goto err_peer_delete;
5697 }
5698
5699 arvif->peer_id = find_first_bit(peer->peer_ids,
5700 ATH10K_MAX_NUM_PEER_IDS);
5701
5702 spin_unlock_bh(&ar->data_lock);
5703 } else {
5704 arvif->peer_id = HTT_INVALID_PEERID;
e57e0571 5705 }
cdf07409 5706
e57e0571 5707 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5a13e76e
KV
5708 ret = ath10k_mac_set_kickout(arvif);
5709 if (ret) {
7aa7a72a 5710 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
69244e56 5711 arvif->vdev_id, ret);
5a13e76e
KV
5712 goto err_peer_delete;
5713 }
5e3dd157
KV
5714 }
5715
5716 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5717 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5718 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5719 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5720 param, value);
9dad14ae 5721 if (ret) {
7aa7a72a 5722 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
69244e56 5723 arvif->vdev_id, ret);
9dad14ae
MK
5724 goto err_peer_delete;
5725 }
5e3dd157 5726
9f9b5746 5727 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
9dad14ae 5728 if (ret) {
9f9b5746 5729 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
69244e56 5730 arvif->vdev_id, ret);
9dad14ae
MK
5731 goto err_peer_delete;
5732 }
5e3dd157 5733
9f9b5746 5734 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
9dad14ae 5735 if (ret) {
9f9b5746 5736 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
69244e56 5737 arvif->vdev_id, ret);
9dad14ae
MK
5738 goto err_peer_delete;
5739 }
5e3dd157
KV
5740 }
5741
a48e2cc8
VN
5742 ret = ath10k_mac_set_txbf_conf(arvif);
5743 if (ret) {
5744 ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5745 arvif->vdev_id, ret);
5746 goto err_peer_delete;
5747 }
5748
424121c3 5749 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
9dad14ae 5750 if (ret) {
7aa7a72a 5751 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
679c54a6 5752 arvif->vdev_id, ret);
9dad14ae
MK
5753 goto err_peer_delete;
5754 }
679c54a6 5755
7d9d5587
MK
5756 arvif->txpower = vif->bss_conf.txpower;
5757 ret = ath10k_mac_txpower_recalc(ar);
5758 if (ret) {
5759 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5760 goto err_peer_delete;
5761 }
5762
059104bf
PKC
5763 if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
5764 vdev_param = ar->wmi.vdev_param->rtt_responder_role;
5765 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5766 arvif->ftm_responder);
5767
5768 /* It is harmless to not set FTM role. Do not warn */
5769 if (ret && ret != -EOPNOTSUPP)
5770 ath10k_warn(ar, "failed to set vdev %i FTM Responder: %d\n",
5771 arvif->vdev_id, ret);
5772 }
5773
500ff9f9
MK
5774 if (vif->type == NL80211_IFTYPE_MONITOR) {
5775 ar->monitor_arvif = arvif;
5776 ret = ath10k_monitor_recalc(ar);
5777 if (ret) {
5778 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5779 goto err_peer_delete;
5780 }
5781 }
5782
6d2d51ec
MK
5783 spin_lock_bh(&ar->htt.tx_lock);
5784 if (!ar->tx_paused)
5785 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5786 spin_unlock_bh(&ar->htt.tx_lock);
5787
5e3dd157 5788 mutex_unlock(&ar->conf_mutex);
9dad14ae
MK
5789 return 0;
5790
5791err_peer_delete:
e57e0571 5792 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
c6f537a1 5793 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
9dad14ae 5794 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
c6f537a1
DR
5795 ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5796 vif->addr);
5797 }
9dad14ae
MK
5798
5799err_vdev_delete:
5800 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
16c11176 5801 ar->free_vdev_map |= 1LL << arvif->vdev_id;
ebaa4b16 5802 spin_lock_bh(&ar->data_lock);
0579119f 5803 list_del(&arvif->list);
ebaa4b16 5804 spin_unlock_bh(&ar->data_lock);
9dad14ae
MK
5805
5806err:
64badcb6 5807 if (arvif->beacon_buf) {
4da8bc2c
AS
5808 if (ar->bus_param.dev_type == ATH10K_DEV_TYPE_HL)
5809 kfree(arvif->beacon_buf);
5810 else
5811 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5812 arvif->beacon_buf,
5813 arvif->beacon_paddr);
64badcb6
MK
5814 arvif->beacon_buf = NULL;
5815 }
5816
9dad14ae
MK
5817 mutex_unlock(&ar->conf_mutex);
5818
5e3dd157
KV
5819 return ret;
5820}
5821
b4aa539d
MK
5822static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5823{
5824 int i;
5825
5826 for (i = 0; i < BITS_PER_LONG; i++)
5827 ath10k_mac_vif_tx_unlock(arvif, i);
5828}
5829
5e3dd157
KV
5830static void ath10k_remove_interface(struct ieee80211_hw *hw,
5831 struct ieee80211_vif *vif)
5832{
5833 struct ath10k *ar = hw->priv;
56ac13bf 5834 struct ath10k_vif *arvif = (void *)vif->drv_priv;
6942726f 5835 struct ath10k_peer *peer;
fe36e70f 5836 unsigned long time_left;
5e3dd157 5837 int ret;
6942726f 5838 int i;
5e3dd157 5839
81a9a17d 5840 cancel_work_sync(&arvif->ap_csa_work);
cc9904e6 5841 cancel_delayed_work_sync(&arvif->connection_loss_work);
81a9a17d 5842
5d011f5c
SM
5843 mutex_lock(&ar->conf_mutex);
5844
855aed12
SW
5845 ret = ath10k_spectral_vif_stop(arvif);
5846 if (ret)
7aa7a72a 5847 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
855aed12
SW
5848 arvif->vdev_id, ret);
5849
16c11176 5850 ar->free_vdev_map |= 1LL << arvif->vdev_id;
ebaa4b16 5851 spin_lock_bh(&ar->data_lock);
0579119f 5852 list_del(&arvif->list);
ebaa4b16 5853 spin_unlock_bh(&ar->data_lock);
5e3dd157 5854
e57e0571
MK
5855 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5856 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
2c512059
MK
5857 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5858 vif->addr);
5e3dd157 5859 if (ret)
e57e0571 5860 ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
69244e56 5861 arvif->vdev_id, ret);
5e3dd157 5862
c6f537a1
DR
5863 ath10k_wait_for_peer_delete_done(ar, arvif->vdev_id,
5864 vif->addr);
5e3dd157
KV
5865 kfree(arvif->u.ap.noa_data);
5866 }
5867
7aa7a72a 5868 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
60c3daa8
KV
5869 arvif->vdev_id);
5870
5e3dd157
KV
5871 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5872 if (ret)
7aa7a72a 5873 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
69244e56 5874 arvif->vdev_id, ret);
5e3dd157 5875
fe36e70f
RP
5876 if (test_bit(WMI_SERVICE_SYNC_DELETE_CMDS, ar->wmi.svc_map)) {
5877 time_left = wait_for_completion_timeout(&ar->vdev_delete_done,
5878 ATH10K_VDEV_DELETE_TIMEOUT_HZ);
5879 if (time_left == 0) {
5880 ath10k_warn(ar, "Timeout in receiving vdev delete response\n");
5881 goto out;
5882 }
5883 }
5884
2c512059
MK
5885 /* Some firmware revisions don't notify host about self-peer removal
5886 * until after associated vdev is deleted.
5887 */
e57e0571
MK
5888 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5889 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
2c512059
MK
5890 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5891 vif->addr);
5892 if (ret)
5893 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5894 arvif->vdev_id, ret);
5895
5896 spin_lock_bh(&ar->data_lock);
5897 ar->num_peers--;
5898 spin_unlock_bh(&ar->data_lock);
5899 }
5900
6942726f
MK
5901 spin_lock_bh(&ar->data_lock);
5902 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5903 peer = ar->peer_map[i];
5904 if (!peer)
5905 continue;
5906
5907 if (peer->vif == vif) {
5908 ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5909 vif->addr, arvif->vdev_id);
5910 peer->vif = NULL;
5911 }
5912 }
b3281c6c
BG
5913
5914 /* Clean this up late, less opportunity for firmware to access
5915 * DMA memory we have deleted.
5916 */
5917 ath10k_mac_vif_beacon_cleanup(arvif);
6942726f
MK
5918 spin_unlock_bh(&ar->data_lock);
5919
5e3dd157 5920 ath10k_peer_cleanup(ar, arvif->vdev_id);
dd4717b6 5921 ath10k_mac_txq_unref(ar, vif->txq);
5e3dd157 5922
500ff9f9
MK
5923 if (vif->type == NL80211_IFTYPE_MONITOR) {
5924 ar->monitor_arvif = NULL;
5925 ret = ath10k_monitor_recalc(ar);
5926 if (ret)
5927 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5928 }
5929
d679fa1b
RH
5930 ret = ath10k_mac_txpower_recalc(ar);
5931 if (ret)
5932 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5933
b4aa539d
MK
5934 spin_lock_bh(&ar->htt.tx_lock);
5935 ath10k_mac_vif_tx_unlock_all(arvif);
5936 spin_unlock_bh(&ar->htt.tx_lock);
5937
29946878
MK
5938 ath10k_mac_txq_unref(ar, vif->txq);
5939
fe36e70f 5940out:
5e3dd157
KV
5941 mutex_unlock(&ar->conf_mutex);
5942}
5943
5944/*
5945 * FIXME: Has to be verified.
5946 */
5947#define SUPPORTED_FILTERS \
df140465 5948 (FIF_ALLMULTI | \
5e3dd157
KV
5949 FIF_CONTROL | \
5950 FIF_PSPOLL | \
5951 FIF_OTHER_BSS | \
5952 FIF_BCN_PRBRESP_PROMISC | \
5953 FIF_PROBE_REQ | \
5954 FIF_FCSFAIL)
5955
5956static void ath10k_configure_filter(struct ieee80211_hw *hw,
5957 unsigned int changed_flags,
5958 unsigned int *total_flags,
5959 u64 multicast)
5960{
5961 struct ath10k *ar = hw->priv;
5962 int ret;
5963
5964 mutex_lock(&ar->conf_mutex);
5965
5966 changed_flags &= SUPPORTED_FILTERS;
5967 *total_flags &= SUPPORTED_FILTERS;
5968 ar->filter_flags = *total_flags;
5969
1933747f
MK
5970 ret = ath10k_monitor_recalc(ar);
5971 if (ret)
7f03d306 5972 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5e3dd157
KV
5973
5974 mutex_unlock(&ar->conf_mutex);
5975}
5976
0227ff36
SE
5977static void ath10k_recalculate_mgmt_rate(struct ath10k *ar,
5978 struct ieee80211_vif *vif,
5979 struct cfg80211_chan_def *def)
5980{
5981 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5982 const struct ieee80211_supported_band *sband;
5983 u8 basic_rate_idx;
5984 int hw_rate_code;
5985 u32 vdev_param;
5986 u16 bitrate;
5987 int ret;
5988
5989 lockdep_assert_held(&ar->conf_mutex);
5990
5991 sband = ar->hw->wiphy->bands[def->chan->band];
5992 basic_rate_idx = ffs(vif->bss_conf.basic_rates) - 1;
5993 bitrate = sband->bitrates[basic_rate_idx].bitrate;
5994
5995 hw_rate_code = ath10k_mac_get_rate_hw_value(bitrate);
5996 if (hw_rate_code < 0) {
5997 ath10k_warn(ar, "bitrate not supported %d\n", bitrate);
5998 return;
5999 }
6000
6001 vdev_param = ar->wmi.vdev_param->mgmt_rate;
6002 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6003 hw_rate_code);
6004 if (ret)
6005 ath10k_warn(ar, "failed to set mgmt tx rate %d\n", ret);
6006}
6007
5e3dd157
KV
6008static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
6009 struct ieee80211_vif *vif,
6010 struct ieee80211_bss_conf *info,
6011 u32 changed)
6012{
6013 struct ath10k *ar = hw->priv;
56ac13bf 6014 struct ath10k_vif *arvif = (void *)vif->drv_priv;
cd93b83a 6015 struct cfg80211_chan_def def;
af762c0b 6016 u32 vdev_param, pdev_param, slottime, preamble;
cd93b83a 6017 u16 bitrate, hw_value;
0227ff36
SE
6018 u8 rate, rateidx;
6019 int ret = 0, mcast_rate;
cd93b83a 6020 enum nl80211_band band;
5e3dd157
KV
6021
6022 mutex_lock(&ar->conf_mutex);
6023
6024 if (changed & BSS_CHANGED_IBSS)
6025 ath10k_control_ibss(arvif, info, vif->addr);
6026
6027 if (changed & BSS_CHANGED_BEACON_INT) {
6028 arvif->beacon_interval = info->beacon_int;
6d1506e7
BM
6029 vdev_param = ar->wmi.vdev_param->beacon_interval;
6030 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157 6031 arvif->beacon_interval);
7aa7a72a 6032 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
6033 "mac vdev %d beacon_interval %d\n",
6034 arvif->vdev_id, arvif->beacon_interval);
6035
5e3dd157 6036 if (ret)
7aa7a72a 6037 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
69244e56 6038 arvif->vdev_id, ret);
5e3dd157
KV
6039 }
6040
6041 if (changed & BSS_CHANGED_BEACON) {
7aa7a72a 6042 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
6043 "vdev %d set beacon tx mode to staggered\n",
6044 arvif->vdev_id);
6045
226a339b
BM
6046 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
6047 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5e3dd157
KV
6048 WMI_BEACON_STAGGERED_MODE);
6049 if (ret)
7aa7a72a 6050 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
69244e56 6051 arvif->vdev_id, ret);
fbb8f1b7
MK
6052
6053 ret = ath10k_mac_setup_bcn_tmpl(arvif);
6054 if (ret)
6055 ath10k_warn(ar, "failed to update beacon template: %d\n",
6056 ret);
b6c7bafa
BC
6057
6058 if (ieee80211_vif_is_mesh(vif)) {
6059 /* mesh doesn't use SSID but firmware needs it */
6060 strncpy(arvif->u.ap.ssid, "mesh",
6061 sizeof(arvif->u.ap.ssid));
6062 arvif->u.ap.ssid_len = 4;
6063 }
fbb8f1b7
MK
6064 }
6065
6066 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
6067 ret = ath10k_mac_setup_prb_tmpl(arvif);
6068 if (ret)
6069 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
6070 arvif->vdev_id, ret);
5e3dd157
KV
6071 }
6072
ba2479fe 6073 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5e3dd157
KV
6074 arvif->dtim_period = info->dtim_period;
6075
7aa7a72a 6076 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
6077 "mac vdev %d dtim_period %d\n",
6078 arvif->vdev_id, arvif->dtim_period);
6079
6d1506e7
BM
6080 vdev_param = ar->wmi.vdev_param->dtim_period;
6081 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
6082 arvif->dtim_period);
6083 if (ret)
7aa7a72a 6084 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
69244e56 6085 arvif->vdev_id, ret);
5e3dd157
KV
6086 }
6087
6088 if (changed & BSS_CHANGED_SSID &&
6089 vif->type == NL80211_IFTYPE_AP) {
6090 arvif->u.ap.ssid_len = info->ssid_len;
6091 if (info->ssid_len)
6092 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
6093 arvif->u.ap.hidden_ssid = info->hidden_ssid;
6094 }
6095
077efc8c
MK
6096 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
6097 ether_addr_copy(arvif->bssid, info->bssid);
5e3dd157 6098
059104bf
PKC
6099 if (changed & BSS_CHANGED_FTM_RESPONDER &&
6100 arvif->ftm_responder != info->ftm_responder &&
6101 test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map)) {
6102 arvif->ftm_responder = info->ftm_responder;
6103
6104 vdev_param = ar->wmi.vdev_param->rtt_responder_role;
6105 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6106 arvif->ftm_responder);
6107
6108 ath10k_dbg(ar, ATH10K_DBG_MAC,
6109 "mac vdev %d ftm_responder %d:ret %d\n",
6110 arvif->vdev_id, arvif->ftm_responder, ret);
6111 }
6112
5e3dd157
KV
6113 if (changed & BSS_CHANGED_BEACON_ENABLED)
6114 ath10k_control_beaconing(arvif, info);
6115
6116 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
e81bd104 6117 arvif->use_cts_prot = info->use_cts_prot;
60c3daa8 6118
e81bd104 6119 ret = ath10k_recalc_rtscts_prot(arvif);
5e3dd157 6120 if (ret)
7aa7a72a 6121 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
69244e56 6122 arvif->vdev_id, ret);
a87fd4b9 6123
7cfe0455
BM
6124 if (ath10k_mac_can_set_cts_prot(arvif)) {
6125 ret = ath10k_mac_set_cts_prot(arvif);
6126 if (ret)
6127 ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
6128 arvif->vdev_id, ret);
6129 }
5e3dd157
KV
6130 }
6131
6132 if (changed & BSS_CHANGED_ERP_SLOT) {
5e3dd157
KV
6133 if (info->use_short_slot)
6134 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
6135
6136 else
6137 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
6138
7aa7a72a 6139 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
60c3daa8
KV
6140 arvif->vdev_id, slottime);
6141
6d1506e7
BM
6142 vdev_param = ar->wmi.vdev_param->slot_time;
6143 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
6144 slottime);
6145 if (ret)
7aa7a72a 6146 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
69244e56 6147 arvif->vdev_id, ret);
5e3dd157
KV
6148 }
6149
6150 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5e3dd157
KV
6151 if (info->use_short_preamble)
6152 preamble = WMI_VDEV_PREAMBLE_SHORT;
6153 else
6154 preamble = WMI_VDEV_PREAMBLE_LONG;
6155
7aa7a72a 6156 ath10k_dbg(ar, ATH10K_DBG_MAC,
60c3daa8
KV
6157 "mac vdev %d preamble %dn",
6158 arvif->vdev_id, preamble);
6159
6d1506e7
BM
6160 vdev_param = ar->wmi.vdev_param->preamble;
6161 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5e3dd157
KV
6162 preamble);
6163 if (ret)
7aa7a72a 6164 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
69244e56 6165 arvif->vdev_id, ret);
5e3dd157
KV
6166 }
6167
6168 if (changed & BSS_CHANGED_ASSOC) {
e556f111
MK
6169 if (info->assoc) {
6170 /* Workaround: Make sure monitor vdev is not running
6171 * when associating to prevent some firmware revisions
6172 * (e.g. 10.1 and 10.2) from crashing.
6173 */
6174 if (ar->monitor_started)
6175 ath10k_monitor_stop(ar);
5e3dd157 6176 ath10k_bss_assoc(hw, vif, info);
e556f111 6177 ath10k_monitor_recalc(ar);
077efc8c
MK
6178 } else {
6179 ath10k_bss_disassoc(hw, vif);
e556f111 6180 }
5e3dd157
KV
6181 }
6182
7d9d5587
MK
6183 if (changed & BSS_CHANGED_TXPOWER) {
6184 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
6185 arvif->vdev_id, info->txpower);
6186
6187 arvif->txpower = info->txpower;
6188 ret = ath10k_mac_txpower_recalc(ar);
6189 if (ret)
6190 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
6191 }
6192
bf14e65c 6193 if (changed & BSS_CHANGED_PS) {
cffb41f3
MK
6194 arvif->ps = vif->bss_conf.ps;
6195
6196 ret = ath10k_config_ps(ar);
bf14e65c
MK
6197 if (ret)
6198 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
6199 arvif->vdev_id, ret);
6200 }
6201
cd93b83a 6202 if (changed & BSS_CHANGED_MCAST_RATE &&
9e80ad37 6203 !ath10k_mac_vif_chan(arvif->vif, &def)) {
cd93b83a 6204 band = def.chan->band;
93ee3d10
PC
6205 mcast_rate = vif->bss_conf.mcast_rate[band];
6206 if (mcast_rate > 0)
6207 rateidx = mcast_rate - 1;
6208 else
6209 rateidx = ffs(vif->bss_conf.basic_rates) - 1;
cd93b83a
PKC
6210
6211 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6212 rateidx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6213
6214 bitrate = ath10k_wmi_legacy_rates[rateidx].bitrate;
6215 hw_value = ath10k_wmi_legacy_rates[rateidx].hw_value;
6216 if (ath10k_mac_bitrate_is_cck(bitrate))
6217 preamble = WMI_RATE_PREAMBLE_CCK;
6218 else
6219 preamble = WMI_RATE_PREAMBLE_OFDM;
6220
6221 rate = ATH10K_HW_RATECODE(hw_value, 0, preamble);
6222
6223 ath10k_dbg(ar, ATH10K_DBG_MAC,
6224 "mac vdev %d mcast_rate %x\n",
6225 arvif->vdev_id, rate);
6226
6227 vdev_param = ar->wmi.vdev_param->mcast_data_rate;
6228 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6229 vdev_param, rate);
6230 if (ret)
6231 ath10k_warn(ar,
6232 "failed to set mcast rate on vdev %i: %d\n",
6233 arvif->vdev_id, ret);
6234
6235 vdev_param = ar->wmi.vdev_param->bcast_data_rate;
6236 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
6237 vdev_param, rate);
6238 if (ret)
6239 ath10k_warn(ar,
6240 "failed to set bcast rate on vdev %i: %d\n",
6241 arvif->vdev_id, ret);
6242 }
6243
0227ff36
SE
6244 if (changed & BSS_CHANGED_BASIC_RATES &&
6245 !ath10k_mac_vif_chan(arvif->vif, &def))
6246 ath10k_recalculate_mgmt_rate(ar, vif, &def);
f279294e 6247
5e3dd157
KV
6248 mutex_unlock(&ar->conf_mutex);
6249}
6250
ebee76f7
BB
6251static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
6252{
6253 struct ath10k *ar = hw->priv;
6254
6255 /* This function should never be called if setting the coverage class
6256 * is not supported on this hardware.
6257 */
6258 if (!ar->hw_params.hw_ops->set_coverage_class) {
6259 WARN_ON_ONCE(1);
6260 return;
6261 }
6262 ar->hw_params.hw_ops->set_coverage_class(ar, value);
6263}
6264
dd0f9cd6
AK
6265struct ath10k_mac_tdls_iter_data {
6266 u32 num_tdls_stations;
6267 struct ieee80211_vif *curr_vif;
6268};
6269
6270static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
6271 struct ieee80211_sta *sta)
6272{
6273 struct ath10k_mac_tdls_iter_data *iter_data = data;
6274 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6275 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
6276
6277 if (sta->tdls && sta_vif == iter_data->curr_vif)
6278 iter_data->num_tdls_stations++;
6279}
6280
6281static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
6282 struct ieee80211_vif *vif)
6283{
6284 struct ath10k_mac_tdls_iter_data data = {};
6285
6286 data.curr_vif = vif;
6287
6288 ieee80211_iterate_stations_atomic(hw,
6289 ath10k_mac_tdls_vif_stations_count_iter,
6290 &data);
6291 return data.num_tdls_stations;
6292}
6293
5e3dd157
KV
6294static int ath10k_hw_scan(struct ieee80211_hw *hw,
6295 struct ieee80211_vif *vif,
c56ef672 6296 struct ieee80211_scan_request *hw_req)
5e3dd157
KV
6297{
6298 struct ath10k *ar = hw->priv;
56ac13bf 6299 struct ath10k_vif *arvif = (void *)vif->drv_priv;
c56ef672 6300 struct cfg80211_scan_request *req = &hw_req->req;
5e3dd157
KV
6301 struct wmi_start_scan_arg arg;
6302 int ret = 0;
6303 int i;
be8cce96 6304 u32 scan_timeout;
5e3dd157
KV
6305
6306 mutex_lock(&ar->conf_mutex);
6307
a6080931
AK
6308 if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
6309 ret = -EBUSY;
6310 goto exit;
6311 }
6312
5e3dd157 6313 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
6314 switch (ar->scan.state) {
6315 case ATH10K_SCAN_IDLE:
6316 reinit_completion(&ar->scan.started);
6317 reinit_completion(&ar->scan.completed);
6318 ar->scan.state = ATH10K_SCAN_STARTING;
6319 ar->scan.is_roc = false;
6320 ar->scan.vdev_id = arvif->vdev_id;
6321 ret = 0;
6322 break;
6323 case ATH10K_SCAN_STARTING:
6324 case ATH10K_SCAN_RUNNING:
6325 case ATH10K_SCAN_ABORTING:
5e3dd157 6326 ret = -EBUSY;
5c81c7fd 6327 break;
5e3dd157 6328 }
5e3dd157
KV
6329 spin_unlock_bh(&ar->data_lock);
6330
5c81c7fd
MK
6331 if (ret)
6332 goto exit;
6333
5e3dd157
KV
6334 memset(&arg, 0, sizeof(arg));
6335 ath10k_wmi_start_scan_init(ar, &arg);
6336 arg.vdev_id = arvif->vdev_id;
6337 arg.scan_id = ATH10K_SCAN_ID;
6338
5e3dd157
KV
6339 if (req->ie_len) {
6340 arg.ie_len = req->ie_len;
6341 memcpy(arg.ie, req->ie, arg.ie_len);
6342 }
6343
6344 if (req->n_ssids) {
6345 arg.n_ssids = req->n_ssids;
6346 for (i = 0; i < arg.n_ssids; i++) {
6347 arg.ssids[i].len = req->ssids[i].ssid_len;
6348 arg.ssids[i].ssid = req->ssids[i].ssid;
6349 }
dcd4a561
MK
6350 } else {
6351 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5e3dd157
KV
6352 }
6353
60e1d0fb
CH
6354 if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR) {
6355 arg.scan_ctrl_flags |= WMI_SCAN_ADD_SPOOFED_MAC_IN_PROBE_REQ;
6356 ether_addr_copy(arg.mac_addr.addr, req->mac_addr);
6357 ether_addr_copy(arg.mac_mask.addr, req->mac_addr_mask);
6358 }
6359
5e3dd157
KV
6360 if (req->n_channels) {
6361 arg.n_channels = req->n_channels;
6362 for (i = 0; i < arg.n_channels; i++)
6363 arg.channels[i] = req->channels[i]->center_freq;
6364 }
6365
be8cce96
PKC
6366 /* if duration is set, default dwell times will be overwritten */
6367 if (req->duration) {
6368 arg.dwell_time_active = req->duration;
6369 arg.dwell_time_passive = req->duration;
6370 arg.burst_duration_ms = req->duration;
6371
6372 scan_timeout = min_t(u32, arg.max_rest_time *
6373 (arg.n_channels - 1) + (req->duration +
6374 ATH10K_SCAN_CHANNEL_SWITCH_WMI_EVT_OVERHEAD) *
6375 arg.n_channels, arg.max_scan_time + 200);
6376
6377 } else {
6378 /* Add a 200ms margin to account for event/command processing */
6379 scan_timeout = arg.max_scan_time + 200;
6380 }
6381
5e3dd157
KV
6382 ret = ath10k_start_scan(ar, &arg);
6383 if (ret) {
7aa7a72a 6384 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5e3dd157 6385 spin_lock_bh(&ar->data_lock);
5c81c7fd 6386 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
6387 spin_unlock_bh(&ar->data_lock);
6388 }
6389
634349ba 6390 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
be8cce96 6391 msecs_to_jiffies(scan_timeout));
634349ba 6392
5e3dd157
KV
6393exit:
6394 mutex_unlock(&ar->conf_mutex);
6395 return ret;
6396}
6397
6398static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
6399 struct ieee80211_vif *vif)
6400{
6401 struct ath10k *ar = hw->priv;
5e3dd157
KV
6402
6403 mutex_lock(&ar->conf_mutex);
5c81c7fd 6404 ath10k_scan_abort(ar);
5e3dd157 6405 mutex_unlock(&ar->conf_mutex);
4eb2e164
MK
6406
6407 cancel_delayed_work_sync(&ar->scan.timeout);
5e3dd157
KV
6408}
6409
cfb27d29
MK
6410static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
6411 struct ath10k_vif *arvif,
6412 enum set_key_cmd cmd,
6413 struct ieee80211_key_conf *key)
6414{
6415 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
6416 int ret;
6417
6418 /* 10.1 firmware branch requires default key index to be set to group
6419 * key index after installing it. Otherwise FW/HW Txes corrupted
6420 * frames with multi-vif APs. This is not required for main firmware
6421 * branch (e.g. 636).
6422 *
8461baf7
MK
6423 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
6424 *
6425 * FIXME: It remains unknown if this is required for multi-vif STA
6426 * interfaces on 10.1.
6427 */
cfb27d29 6428
8461baf7
MK
6429 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
6430 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
cfb27d29
MK
6431 return;
6432
6433 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
6434 return;
6435
6436 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
6437 return;
6438
6439 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6440 return;
6441
6442 if (cmd != SET_KEY)
6443 return;
6444
6445 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
6446 key->keyidx);
6447 if (ret)
7aa7a72a 6448 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
69244e56 6449 arvif->vdev_id, ret);
cfb27d29
MK
6450}
6451
5e3dd157
KV
6452static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
6453 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
6454 struct ieee80211_key_conf *key)
6455{
6456 struct ath10k *ar = hw->priv;
56ac13bf 6457 struct ath10k_vif *arvif = (void *)vif->drv_priv;
95a568c4 6458 struct ath10k_sta *arsta;
5e3dd157
KV
6459 struct ath10k_peer *peer;
6460 const u8 *peer_addr;
6461 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
6462 key->cipher == WLAN_CIPHER_SUITE_WEP104;
6463 int ret = 0;
29a10006 6464 int ret2;
370e5673 6465 u32 flags = 0;
29a10006 6466 u32 flags2;
5e3dd157 6467
d7131c04 6468 /* this one needs to be done in software */
2ea9f12c
RM
6469 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
6470 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
6471 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256 ||
6472 key->cipher == WLAN_CIPHER_SUITE_BIP_CMAC_256)
d7131c04 6473 return 1;
5e3dd157 6474
ccec9038
DL
6475 if (arvif->nohwcrypt)
6476 return 1;
6477
5e3dd157
KV
6478 if (key->keyidx > WMI_MAX_KEY_INDEX)
6479 return -ENOSPC;
6480
6481 mutex_lock(&ar->conf_mutex);
6482
95a568c4
YT
6483 if (sta) {
6484 arsta = (struct ath10k_sta *)sta->drv_priv;
5e3dd157 6485 peer_addr = sta->addr;
95a568c4
YT
6486 spin_lock_bh(&ar->data_lock);
6487 arsta->ucast_cipher = key->cipher;
6488 spin_unlock_bh(&ar->data_lock);
6489 } else if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5e3dd157 6490 peer_addr = vif->bss_conf.bssid;
95a568c4 6491 } else {
5e3dd157 6492 peer_addr = vif->addr;
95a568c4 6493 }
5e3dd157
KV
6494
6495 key->hw_key_idx = key->keyidx;
6496
7c8cc7eb
MK
6497 if (is_wep) {
6498 if (cmd == SET_KEY)
6499 arvif->wep_keys[key->keyidx] = key;
6500 else
6501 arvif->wep_keys[key->keyidx] = NULL;
6502 }
6503
5e3dd157 6504 /* the peer should not disappear in mid-way (unless FW goes awry) since
d6dfe25c
MR
6505 * we already hold conf_mutex. we just make sure its there now.
6506 */
5e3dd157
KV
6507 spin_lock_bh(&ar->data_lock);
6508 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6509 spin_unlock_bh(&ar->data_lock);
6510
6511 if (!peer) {
6512 if (cmd == SET_KEY) {
7aa7a72a 6513 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5e3dd157
KV
6514 peer_addr);
6515 ret = -EOPNOTSUPP;
6516 goto exit;
6517 } else {
d6dfe25c 6518 /* if the peer doesn't exist there is no key to disable anymore */
5e3dd157
KV
6519 goto exit;
6520 }
6521 }
6522
7cc4573e
MK
6523 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6524 flags |= WMI_KEY_PAIRWISE;
6525 else
6526 flags |= WMI_KEY_GROUP;
5e3dd157 6527
5e3dd157 6528 if (is_wep) {
5e3dd157
KV
6529 if (cmd == DISABLE_KEY)
6530 ath10k_clear_vdev_key(arvif, key);
5e3dd157 6531
ad325cb5
MK
6532 /* When WEP keys are uploaded it's possible that there are
6533 * stations associated already (e.g. when merging) without any
6534 * keys. Static WEP needs an explicit per-peer key upload.
6535 */
6536 if (vif->type == NL80211_IFTYPE_ADHOC &&
6537 cmd == SET_KEY)
6538 ath10k_mac_vif_update_wep_key(arvif, key);
6539
370e5673
MK
6540 /* 802.1x never sets the def_wep_key_idx so each set_key()
6541 * call changes default tx key.
6542 *
6543 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
6544 * after first set_key().
6545 */
6546 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
6547 flags |= WMI_KEY_TX_USAGE;
370e5673 6548 }
627613f8 6549
370e5673 6550 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
5e3dd157 6551 if (ret) {
ccec9038 6552 WARN_ON(ret > 0);
7aa7a72a 6553 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
69244e56 6554 arvif->vdev_id, peer_addr, ret);
5e3dd157
KV
6555 goto exit;
6556 }
6557
29a10006
MK
6558 /* mac80211 sets static WEP keys as groupwise while firmware requires
6559 * them to be installed twice as both pairwise and groupwise.
6560 */
6561 if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
6562 flags2 = flags;
6563 flags2 &= ~WMI_KEY_GROUP;
6564 flags2 |= WMI_KEY_PAIRWISE;
6565
6566 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
6567 if (ret) {
ccec9038 6568 WARN_ON(ret > 0);
29a10006
MK
6569 ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
6570 arvif->vdev_id, peer_addr, ret);
6571 ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
6572 peer_addr, flags);
ccec9038
DL
6573 if (ret2) {
6574 WARN_ON(ret2 > 0);
29a10006
MK
6575 ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
6576 arvif->vdev_id, peer_addr, ret2);
ccec9038 6577 }
29a10006
MK
6578 goto exit;
6579 }
6580 }
6581
cfb27d29
MK
6582 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
6583
5e3dd157
KV
6584 spin_lock_bh(&ar->data_lock);
6585 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
6586 if (peer && cmd == SET_KEY)
6587 peer->keys[key->keyidx] = key;
6588 else if (peer && cmd == DISABLE_KEY)
6589 peer->keys[key->keyidx] = NULL;
6590 else if (peer == NULL)
6591 /* impossible unless FW goes crazy */
7aa7a72a 6592 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5e3dd157
KV
6593 spin_unlock_bh(&ar->data_lock);
6594
9cdd0057
YT
6595 if (sta && sta->tdls)
6596 ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6597 ar->wmi.peer_param->authorize, 1);
382e51c1
WG
6598 else if (sta && cmd == SET_KEY && (key->flags & IEEE80211_KEY_FLAG_PAIRWISE))
6599 ath10k_wmi_peer_set_param(ar, arvif->vdev_id, peer_addr,
6600 ar->wmi.peer_param->authorize, 1);
9cdd0057 6601
5e3dd157
KV
6602exit:
6603 mutex_unlock(&ar->conf_mutex);
6604 return ret;
6605}
6606
627613f8
SJ
6607static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
6608 struct ieee80211_vif *vif,
6609 int keyidx)
6610{
6611 struct ath10k *ar = hw->priv;
56ac13bf 6612 struct ath10k_vif *arvif = (void *)vif->drv_priv;
627613f8
SJ
6613 int ret;
6614
6615 mutex_lock(&arvif->ar->conf_mutex);
6616
6617 if (arvif->ar->state != ATH10K_STATE_ON)
6618 goto unlock;
6619
6620 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
6621 arvif->vdev_id, keyidx);
6622
6623 ret = ath10k_wmi_vdev_set_param(arvif->ar,
6624 arvif->vdev_id,
6625 arvif->ar->wmi.vdev_param->def_keyid,
6626 keyidx);
6627
6628 if (ret) {
6629 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
6630 arvif->vdev_id,
6631 ret);
6632 goto unlock;
6633 }
6634
6635 arvif->def_wep_key_idx = keyidx;
370e5673 6636
627613f8
SJ
6637unlock:
6638 mutex_unlock(&arvif->ar->conf_mutex);
6639}
6640
9797febc
MK
6641static void ath10k_sta_rc_update_wk(struct work_struct *wk)
6642{
6643 struct ath10k *ar;
6644 struct ath10k_vif *arvif;
6645 struct ath10k_sta *arsta;
6646 struct ieee80211_sta *sta;
45c9abc0 6647 struct cfg80211_chan_def def;
57fbcce3 6648 enum nl80211_band band;
45c9abc0
MK
6649 const u8 *ht_mcs_mask;
6650 const u16 *vht_mcs_mask;
9797febc
MK
6651 u32 changed, bw, nss, smps;
6652 int err;
6653
6654 arsta = container_of(wk, struct ath10k_sta, update_wk);
6655 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
6656 arvif = arsta->arvif;
6657 ar = arvif->ar;
6658
45c9abc0
MK
6659 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
6660 return;
6661
6662 band = def.chan->band;
6663 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
6664 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
6665
9797febc
MK
6666 spin_lock_bh(&ar->data_lock);
6667
6668 changed = arsta->changed;
6669 arsta->changed = 0;
6670
6671 bw = arsta->bw;
6672 nss = arsta->nss;
6673 smps = arsta->smps;
6674
6675 spin_unlock_bh(&ar->data_lock);
6676
6677 mutex_lock(&ar->conf_mutex);
6678
45c9abc0
MK
6679 nss = max_t(u32, 1, nss);
6680 nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6681 ath10k_mac_max_vht_nss(vht_mcs_mask)));
6682
9797febc 6683 if (changed & IEEE80211_RC_BW_CHANGED) {
9191fc2a
RH
6684 enum wmi_phy_mode mode;
6685
6686 mode = chan_to_phymode(&def);
97614c59 6687 ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM peer bw %d phymode %d\n",
ebfac1d0 6688 sta->addr, bw, mode);
9191fc2a
RH
6689
6690 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6691 ar->wmi.peer_param->phymode, mode);
9191fc2a
RH
6692 if (err) {
6693 ath10k_warn(ar, "failed to update STA %pM peer phymode %d: %d\n",
ebfac1d0 6694 sta->addr, mode, err);
9191fc2a
RH
6695 goto exit;
6696 }
9797febc
MK
6697
6698 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6699 ar->wmi.peer_param->chan_width, bw);
9797febc 6700 if (err)
7aa7a72a 6701 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
9797febc
MK
6702 sta->addr, bw, err);
6703 }
6704
6705 if (changed & IEEE80211_RC_NSS_CHANGED) {
97614c59 6706 ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM nss %d\n",
9797febc
MK
6707 sta->addr, nss);
6708
6709 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6710 ar->wmi.peer_param->nss, nss);
9797febc 6711 if (err)
7aa7a72a 6712 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
9797febc
MK
6713 sta->addr, nss, err);
6714 }
6715
6716 if (changed & IEEE80211_RC_SMPS_CHANGED) {
97614c59 6717 ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM smps %d\n",
9797febc
MK
6718 sta->addr, smps);
6719
6720 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6721 ar->wmi.peer_param->smps_state, smps);
9797febc 6722 if (err)
7aa7a72a 6723 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
9797febc
MK
6724 sta->addr, smps, err);
6725 }
6726
55cc11da 6727 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED) {
97614c59 6728 ath10k_dbg(ar, ATH10K_DBG_STA, "mac update sta %pM supp rates\n",
44d6fa90
CYY
6729 sta->addr);
6730
590922a8 6731 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
44d6fa90 6732 if (err)
7aa7a72a 6733 ath10k_warn(ar, "failed to reassociate station: %pM\n",
44d6fa90
CYY
6734 sta->addr);
6735 }
6736
9191fc2a 6737exit:
9797febc
MK
6738 mutex_unlock(&ar->conf_mutex);
6739}
6740
7c354242
MP
6741static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
6742 struct ieee80211_sta *sta)
cfd1061e
MK
6743{
6744 struct ath10k *ar = arvif->ar;
6745
6746 lockdep_assert_held(&ar->conf_mutex);
6747
7c354242 6748 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
cfd1061e
MK
6749 return 0;
6750
6751 if (ar->num_stations >= ar->max_num_stations)
6752 return -ENOBUFS;
6753
6754 ar->num_stations++;
6755
6756 return 0;
6757}
6758
7c354242
MP
6759static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
6760 struct ieee80211_sta *sta)
cfd1061e
MK
6761{
6762 struct ath10k *ar = arvif->ar;
6763
6764 lockdep_assert_held(&ar->conf_mutex);
6765
7c354242 6766 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
cfd1061e
MK
6767 return;
6768
6769 ar->num_stations--;
6770}
6771
33410a51
ARN
6772static int ath10k_sta_set_txpwr(struct ieee80211_hw *hw,
6773 struct ieee80211_vif *vif,
6774 struct ieee80211_sta *sta)
6775{
6776 struct ath10k *ar = hw->priv;
6777 struct ath10k_vif *arvif = (void *)vif->drv_priv;
6778 int ret = 0;
6779 s16 txpwr;
6780
6781 if (sta->txpwr.type == NL80211_TX_POWER_AUTOMATIC) {
6782 txpwr = 0;
6783 } else {
6784 txpwr = sta->txpwr.power;
6785 if (!txpwr)
6786 return -EINVAL;
6787 }
6788
6789 if (txpwr > ATH10K_TX_POWER_MAX_VAL || txpwr < ATH10K_TX_POWER_MIN_VAL)
6790 return -EINVAL;
6791
6792 mutex_lock(&ar->conf_mutex);
6793
6794 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
c0e33fe6 6795 ar->wmi.peer_param->use_fixed_power, txpwr);
33410a51
ARN
6796 if (ret) {
6797 ath10k_warn(ar, "failed to set tx power for station ret: %d\n",
6798 ret);
6799 goto out;
6800 }
6801
6802out:
6803 mutex_unlock(&ar->conf_mutex);
6804 return ret;
6805}
6806
7b2531d9
TC
6807struct ath10k_mac_iter_tid_conf_data {
6808 struct ieee80211_vif *curr_vif;
6809 struct ath10k *ar;
2ca6a1dd 6810 bool reset_config;
7b2531d9
TC
6811};
6812
1c0b3fbd
TC
6813static bool
6814ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6815 enum nl80211_band band,
6816 const struct cfg80211_bitrate_mask *mask,
6817 int *vht_num_rates)
6818{
6819 int num_rates = 0;
6820 int i, tmp;
6821
6822 num_rates += hweight32(mask->control[band].legacy);
6823
6824 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6825 num_rates += hweight8(mask->control[band].ht_mcs[i]);
6826
6827 *vht_num_rates = 0;
6828 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6829 tmp = hweight16(mask->control[band].vht_mcs[i]);
6830 num_rates += tmp;
6831 *vht_num_rates += tmp;
6832 }
6833
6834 return num_rates == 1;
6835}
6836
6837static int
6838ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6839 enum nl80211_band band,
6840 const struct cfg80211_bitrate_mask *mask,
6841 u8 *rate, u8 *nss, bool vht_only)
6842{
6843 int rate_idx;
6844 int i;
6845 u16 bitrate;
6846 u8 preamble;
6847 u8 hw_rate;
6848
6849 if (vht_only)
6850 goto next;
6851
6852 if (hweight32(mask->control[band].legacy) == 1) {
6853 rate_idx = ffs(mask->control[band].legacy) - 1;
6854
6855 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY)
6856 rate_idx += ATH10K_MAC_FIRST_OFDM_RATE_IDX;
6857
6858 hw_rate = ath10k_wmi_legacy_rates[rate_idx].hw_value;
6859 bitrate = ath10k_wmi_legacy_rates[rate_idx].bitrate;
6860
6861 if (ath10k_mac_bitrate_is_cck(bitrate))
6862 preamble = WMI_RATE_PREAMBLE_CCK;
6863 else
6864 preamble = WMI_RATE_PREAMBLE_OFDM;
6865
6866 *nss = 1;
6867 *rate = preamble << 6 |
6868 (*nss - 1) << 4 |
6869 hw_rate << 0;
6870
6871 return 0;
6872 }
6873
6874 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6875 if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6876 *nss = i + 1;
6877 *rate = WMI_RATE_PREAMBLE_HT << 6 |
6878 (*nss - 1) << 4 |
6879 (ffs(mask->control[band].ht_mcs[i]) - 1);
6880
6881 return 0;
6882 }
6883 }
6884
6885next:
6886 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6887 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6888 *nss = i + 1;
6889 *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6890 (*nss - 1) << 4 |
6891 (ffs(mask->control[band].vht_mcs[i]) - 1);
6892
6893 return 0;
6894 }
6895 }
6896
6897 return -EINVAL;
6898}
6899
7b2531d9
TC
6900static int ath10k_mac_validate_rate_mask(struct ath10k *ar,
6901 struct ieee80211_sta *sta,
6902 u32 rate_ctrl_flag, u8 nss)
6903{
6904 if (nss > sta->rx_nss) {
6905 ath10k_warn(ar, "Invalid nss field, configured %u limit %u\n",
6906 nss, sta->rx_nss);
6907 return -EINVAL;
6908 }
6909
6910 if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_VHT) {
6911 if (!sta->vht_cap.vht_supported) {
6912 ath10k_warn(ar, "Invalid VHT rate for sta %pM\n",
6913 sta->addr);
6914 return -EINVAL;
6915 }
6916 } else if (ATH10K_HW_PREAMBLE(rate_ctrl_flag) == WMI_RATE_PREAMBLE_HT) {
6917 if (!sta->ht_cap.ht_supported || sta->vht_cap.vht_supported) {
6918 ath10k_warn(ar, "Invalid HT rate for sta %pM\n",
6919 sta->addr);
6920 return -EINVAL;
6921 }
6922 } else {
6923 if (sta->ht_cap.ht_supported || sta->vht_cap.vht_supported)
6924 return -EINVAL;
6925 }
6926
6927 return 0;
6928}
6929
6930static int
6931ath10k_mac_tid_bitrate_config(struct ath10k *ar,
6932 struct ieee80211_vif *vif,
6933 struct ieee80211_sta *sta,
6934 u32 *rate_ctrl_flag, u8 *rate_ctrl,
6935 enum nl80211_tx_rate_setting txrate_type,
6936 const struct cfg80211_bitrate_mask *mask)
6937{
6938 struct cfg80211_chan_def def;
6939 enum nl80211_band band;
6940 u8 nss, rate;
6941 int vht_num_rates, ret;
6942
6943 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
6944 return -EINVAL;
6945
6946 if (txrate_type == NL80211_TX_RATE_AUTOMATIC) {
6947 *rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
6948 *rate_ctrl_flag = 0;
6949 return 0;
6950 }
6951
6952 band = def.chan->band;
6953
6954 if (!ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
6955 &vht_num_rates)) {
6956 return -EINVAL;
6957 }
6958
6959 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6960 &rate, &nss, false);
6961 if (ret) {
6962 ath10k_warn(ar, "failed to get single rate: %d\n",
6963 ret);
6964 return ret;
6965 }
6966
6967 *rate_ctrl_flag = rate;
6968
6969 if (sta && ath10k_mac_validate_rate_mask(ar, sta, *rate_ctrl_flag, nss))
6970 return -EINVAL;
6971
6972 if (txrate_type == NL80211_TX_RATE_FIXED)
6973 *rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_FIXED_RATE;
6974 else if (txrate_type == NL80211_TX_RATE_LIMITED &&
6975 (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
6976 ar->wmi.svc_map)))
6977 *rate_ctrl = WMI_PEER_TID_CONFIG_RATE_UPPER_CAP;
6978 else
6979 return -EOPNOTSUPP;
6980
6981 return 0;
6982}
6983
6984static int ath10k_mac_set_tid_config(struct ath10k *ar, struct ieee80211_sta *sta,
6985 struct ieee80211_vif *vif, u32 changed,
6986 struct wmi_per_peer_per_tid_cfg_arg *arg)
6987{
6988 struct ath10k_vif *arvif = (void *)vif->drv_priv;
6989 struct ath10k_sta *arsta;
6990 int ret;
6991
6992 if (sta) {
6993 if (!sta->wme)
6994 return -ENOTSUPP;
6995
6996 arsta = (struct ath10k_sta *)sta->drv_priv;
6997
6998 if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
6999 if ((arsta->retry_long[arg->tid] > 0 ||
7000 arsta->rate_code[arg->tid] > 0 ||
7001 arsta->ampdu[arg->tid] ==
7002 WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7003 arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7004 changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7005 arg->ack_policy = 0;
7006 arg->aggr_control = 0;
7007 arg->rate_ctrl = 0;
7008 arg->rcode_flags = 0;
7009 }
7010 }
7011
7012 if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7013 if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7014 arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7015 arg->aggr_control = 0;
7016 changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7017 }
7018 }
7019
7020 if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7021 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7022 if (arsta->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK ||
7023 arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7024 arg->rate_ctrl = 0;
7025 arg->rcode_flags = 0;
7026 }
7027 }
7028
7029 ether_addr_copy(arg->peer_macaddr.addr, sta->addr);
7030
7031 ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, arg);
7032 if (ret)
7033 return ret;
7034
7035 /* Store the configured parameters in success case */
7036 if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7037 arsta->noack[arg->tid] = arg->ack_policy;
7038 arg->ack_policy = 0;
7039 arg->aggr_control = 0;
7040 arg->rate_ctrl = 0;
7041 arg->rcode_flags = 0;
7042 }
7043
7044 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7045 arsta->retry_long[arg->tid] = arg->retry_count;
7046 arg->retry_count = 0;
7047 }
7048
7049 if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7050 arsta->ampdu[arg->tid] = arg->aggr_control;
7051 arg->aggr_control = 0;
7052 }
7053
7054 if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7055 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7056 arsta->rate_ctrl[arg->tid] = arg->rate_ctrl;
7057 arg->rate_ctrl = 0;
7058 arg->rcode_flags = 0;
7059 }
7060
7061 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7062 arsta->rtscts[arg->tid] = arg->rtscts_ctrl;
7063 arg->ext_tid_cfg_bitmap = 0;
7064 }
7065 } else {
7066 if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7067 if ((arvif->retry_long[arg->tid] ||
7068 arvif->rate_code[arg->tid] ||
7069 arvif->ampdu[arg->tid] ==
7070 WMI_TID_CONFIG_AGGR_CONTROL_ENABLE) &&
7071 arg->ack_policy == WMI_PEER_TID_CONFIG_NOACK) {
7072 changed &= ~BIT(NL80211_TID_CONFIG_ATTR_NOACK);
7073 } else {
7074 arvif->noack[arg->tid] = arg->ack_policy;
7075 arvif->ampdu[arg->tid] = arg->aggr_control;
7076 arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7077 }
7078 }
7079
7080 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7081 if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7082 changed &= ~BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG);
7083 else
7084 arvif->retry_long[arg->tid] = arg->retry_count;
7085 }
7086
7087 if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7088 if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK)
7089 changed &= ~BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL);
7090 else
7091 arvif->ampdu[arg->tid] = arg->aggr_control;
7092 }
7093
7094 if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7095 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7096 if (arvif->noack[arg->tid] == WMI_PEER_TID_CONFIG_NOACK) {
7097 changed &= ~(BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7098 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE));
7099 } else {
7100 arvif->rate_ctrl[arg->tid] = arg->rate_ctrl;
7101 arvif->rate_code[arg->tid] = arg->rcode_flags;
7102 }
7103 }
7104
7105 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7106 arvif->rtscts[arg->tid] = arg->rtscts_ctrl;
7107 arg->ext_tid_cfg_bitmap = 0;
7108 }
7109
7110 if (changed)
7111 arvif->tid_conf_changed[arg->tid] |= changed;
7112 }
7113
7114 return 0;
7115}
7116
7117static int
7118ath10k_mac_parse_tid_config(struct ath10k *ar,
7119 struct ieee80211_sta *sta,
7120 struct ieee80211_vif *vif,
7121 struct cfg80211_tid_cfg *tid_conf,
7122 struct wmi_per_peer_per_tid_cfg_arg *arg)
7123{
7124 u32 changed = tid_conf->mask;
7125 int ret = 0, i = 0;
7126
7127 if (!changed)
7128 return -EINVAL;
7129
7130 while (i < ATH10K_TID_MAX) {
7131 if (!(tid_conf->tids & BIT(i))) {
7132 i++;
7133 continue;
7134 }
7135
7136 arg->tid = i;
7137
7138 if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7139 if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE) {
7140 arg->ack_policy = WMI_PEER_TID_CONFIG_NOACK;
7141 arg->rate_ctrl =
7142 WMI_TID_CONFIG_RATE_CONTROL_DEFAULT_LOWEST_RATE;
7143 arg->aggr_control =
7144 WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7145 } else {
7146 arg->ack_policy =
7147 WMI_PEER_TID_CONFIG_ACK;
7148 arg->rate_ctrl =
7149 WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7150 arg->aggr_control =
7151 WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7152 }
7153 }
7154
7155 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG))
7156 arg->retry_count = tid_conf->retry_long;
7157
7158 if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7159 if (tid_conf->noack == NL80211_TID_CONFIG_ENABLE)
7160 arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7161 else
7162 arg->aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_DISABLE;
7163 }
7164
7165 if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7166 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7167 ret = ath10k_mac_tid_bitrate_config(ar, vif, sta,
7168 &arg->rcode_flags,
7169 &arg->rate_ctrl,
7170 tid_conf->txrate_type,
7171 &tid_conf->txrate_mask);
7172 if (ret) {
7173 ath10k_warn(ar, "failed to configure bitrate mask %d\n",
7174 ret);
7175 arg->rcode_flags = 0;
7176 arg->rate_ctrl = 0;
7177 }
7178 }
7179
7180 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7181 if (tid_conf->rtscts)
7182 arg->rtscts_ctrl = tid_conf->rtscts;
7183
7184 arg->ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7185 }
7186
7187 ret = ath10k_mac_set_tid_config(ar, sta, vif, changed, arg);
7188 if (ret)
7189 return ret;
7190 i++;
7191 }
7192
7193 return ret;
7194}
7195
2ca6a1dd
TC
7196static int ath10k_mac_reset_tid_config(struct ath10k *ar,
7197 struct ieee80211_sta *sta,
7198 struct ath10k_vif *arvif,
7199 u8 tids)
7200{
7201 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7202 struct wmi_per_peer_per_tid_cfg_arg arg;
7203 int ret = 0, i = 0;
7204
7205 arg.vdev_id = arvif->vdev_id;
7206 while (i < ATH10K_TID_MAX) {
7207 if (!(tids & BIT(i))) {
7208 i++;
7209 continue;
7210 }
7211
7212 arg.tid = i;
7213 arg.ack_policy = WMI_PEER_TID_CONFIG_ACK;
7214 arg.retry_count = ATH10K_MAX_RETRY_COUNT;
7215 arg.rate_ctrl = WMI_TID_CONFIG_RATE_CONTROL_AUTO;
7216 arg.aggr_control = WMI_TID_CONFIG_AGGR_CONTROL_ENABLE;
7217 arg.rtscts_ctrl = WMI_TID_CONFIG_RTSCTS_CONTROL_ENABLE;
7218 arg.ext_tid_cfg_bitmap = WMI_EXT_TID_RTS_CTS_CONFIG;
7219
7220 ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7221
7222 ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7223 if (ret)
7224 return ret;
7225
7226 if (!arvif->tids_rst) {
7227 arsta->retry_long[i] = -1;
7228 arsta->noack[i] = -1;
7229 arsta->ampdu[i] = -1;
7230 arsta->rate_code[i] = -1;
7231 arsta->rate_ctrl[i] = 0;
7232 arsta->rtscts[i] = -1;
7233 } else {
7234 arvif->retry_long[i] = 0;
7235 arvif->noack[i] = 0;
7236 arvif->ampdu[i] = 0;
7237 arvif->rate_code[i] = 0;
7238 arvif->rate_ctrl[i] = 0;
7239 arvif->rtscts[i] = 0;
7240 }
7241
7242 i++;
7243 }
7244
7245 return ret;
7246}
7247
7b2531d9
TC
7248static void ath10k_sta_tid_cfg_wk(struct work_struct *wk)
7249{
7250 struct wmi_per_peer_per_tid_cfg_arg arg = {};
7251 struct ieee80211_sta *sta;
7252 struct ath10k_sta *arsta;
7253 struct ath10k_vif *arvif;
7254 struct ath10k *ar;
7255 bool config_apply;
7256 int ret, i;
7257 u32 changed;
7258 u8 nss;
7259
7260 arsta = container_of(wk, struct ath10k_sta, tid_config_wk);
7261 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
7262 arvif = arsta->arvif;
7263 ar = arvif->ar;
7264
7265 mutex_lock(&ar->conf_mutex);
7266
2ca6a1dd
TC
7267 if (arvif->tids_rst) {
7268 ret = ath10k_mac_reset_tid_config(ar, sta, arvif,
7269 arvif->tids_rst);
7270 goto exit;
7271 }
7272
7b2531d9
TC
7273 ether_addr_copy(arg.peer_macaddr.addr, sta->addr);
7274
7275 for (i = 0; i < ATH10K_TID_MAX; i++) {
7276 config_apply = false;
7277 changed = arvif->tid_conf_changed[i];
7278
7279 if (changed & BIT(NL80211_TID_CONFIG_ATTR_NOACK)) {
7280 if (arsta->noack[i] != -1) {
7281 arg.ack_policy = 0;
7282 } else {
7283 config_apply = true;
7284 arg.ack_policy = arvif->noack[i];
7285 arg.aggr_control = arvif->ampdu[i];
7286 arg.rate_ctrl = arvif->rate_ctrl[i];
7287 }
7288 }
7289
7290 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG)) {
7291 if (arsta->retry_long[i] != -1 ||
7292 arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7293 arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7294 arg.retry_count = 0;
7295 } else {
7296 arg.retry_count = arvif->retry_long[i];
7297 config_apply = true;
7298 }
7299 }
7300
7301 if (changed & BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL)) {
7302 if (arsta->ampdu[i] != -1 ||
7303 arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7304 arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7305 arg.aggr_control = 0;
7306 } else {
7307 arg.aggr_control = arvif->ampdu[i];
7308 config_apply = true;
7309 }
7310 }
7311
7312 if (changed & (BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
7313 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE))) {
7314 nss = ATH10K_HW_NSS(arvif->rate_code[i]);
7315 ret = ath10k_mac_validate_rate_mask(ar, sta,
7316 arvif->rate_code[i],
7317 nss);
7318 if (ret &&
7319 arvif->rate_ctrl[i] > WMI_TID_CONFIG_RATE_CONTROL_AUTO) {
7320 arg.rate_ctrl = 0;
7321 arg.rcode_flags = 0;
7322 }
7323
7324 if (arsta->rate_ctrl[i] >
7325 WMI_TID_CONFIG_RATE_CONTROL_AUTO ||
7326 arsta->noack[i] == WMI_PEER_TID_CONFIG_NOACK ||
7327 arvif->noack[i] == WMI_PEER_TID_CONFIG_NOACK) {
7328 arg.rate_ctrl = 0;
7329 arg.rcode_flags = 0;
7330 } else {
7331 arg.rate_ctrl = arvif->rate_ctrl[i];
7332 arg.rcode_flags = arvif->rate_code[i];
7333 config_apply = true;
7334 }
7335 }
7336
7337 if (changed & BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL)) {
7338 if (arsta->rtscts[i]) {
7339 arg.rtscts_ctrl = 0;
7340 arg.ext_tid_cfg_bitmap = 0;
7341 } else {
7342 arg.rtscts_ctrl = arvif->rtscts[i] - 1;
7343 arg.ext_tid_cfg_bitmap =
7344 WMI_EXT_TID_RTS_CTS_CONFIG;
7345 config_apply = true;
7346 }
7347 }
7348
7349 arg.tid = i;
7350
7351 if (config_apply) {
7352 ret = ath10k_wmi_set_per_peer_per_tid_cfg(ar, &arg);
7353 if (ret)
7354 ath10k_warn(ar, "failed to set per tid config for sta %pM: %d\n",
7355 sta->addr, ret);
7356 }
7357
7358 arg.ack_policy = 0;
7359 arg.retry_count = 0;
7360 arg.aggr_control = 0;
7361 arg.rate_ctrl = 0;
7362 arg.rcode_flags = 0;
7363 }
7364
2ca6a1dd 7365exit:
7b2531d9
TC
7366 mutex_unlock(&ar->conf_mutex);
7367}
7368
7369static void ath10k_mac_vif_stations_tid_conf(void *data,
7370 struct ieee80211_sta *sta)
7371{
7372 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
7373 struct ath10k_mac_iter_tid_conf_data *iter_data = data;
7374 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
7375
7376 if (sta_vif != iter_data->curr_vif || !sta->wme)
7377 return;
7378
7379 ieee80211_queue_work(iter_data->ar->hw, &arsta->tid_config_wk);
7380}
7381
5e3dd157
KV
7382static int ath10k_sta_state(struct ieee80211_hw *hw,
7383 struct ieee80211_vif *vif,
7384 struct ieee80211_sta *sta,
7385 enum ieee80211_sta_state old_state,
7386 enum ieee80211_sta_state new_state)
7387{
7388 struct ath10k *ar = hw->priv;
56ac13bf 7389 struct ath10k_vif *arvif = (void *)vif->drv_priv;
9797febc 7390 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
bb8f0c6a 7391 struct ath10k_peer *peer;
5e3dd157 7392 int ret = 0;
6942726f 7393 int i;
5e3dd157 7394
76f90024
MK
7395 if (old_state == IEEE80211_STA_NOTEXIST &&
7396 new_state == IEEE80211_STA_NONE) {
7397 memset(arsta, 0, sizeof(*arsta));
7398 arsta->arvif = arvif;
d70c0d46 7399 arsta->peer_ps_state = WMI_PEER_PS_STATE_DISABLED;
76f90024 7400 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
7b2531d9 7401 INIT_WORK(&arsta->tid_config_wk, ath10k_sta_tid_cfg_wk);
29946878
MK
7402
7403 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7404 ath10k_mac_txq_init(sta->txq[i]);
76f90024
MK
7405 }
7406
9797febc
MK
7407 /* cancel must be done outside the mutex to avoid deadlock */
7408 if ((old_state == IEEE80211_STA_NONE &&
7b2531d9 7409 new_state == IEEE80211_STA_NOTEXIST)) {
9797febc 7410 cancel_work_sync(&arsta->update_wk);
7b2531d9
TC
7411 cancel_work_sync(&arsta->tid_config_wk);
7412 }
9797febc 7413
5e3dd157
KV
7414 mutex_lock(&ar->conf_mutex);
7415
7416 if (old_state == IEEE80211_STA_NOTEXIST &&
077efc8c 7417 new_state == IEEE80211_STA_NONE) {
5e3dd157
KV
7418 /*
7419 * New station addition.
7420 */
75d85fd9
MP
7421 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
7422 u32 num_tdls_stations;
75d85fd9 7423
97614c59 7424 ath10k_dbg(ar, ATH10K_DBG_STA,
cfd1061e
MK
7425 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
7426 arvif->vdev_id, sta->addr,
7427 ar->num_stations + 1, ar->max_num_stations,
7428 ar->num_peers + 1, ar->max_num_peers);
0e759f36 7429
9a993cc1 7430 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
9a993cc1
RH
7431
7432 if (sta->tdls) {
7433 if (num_tdls_stations >= ar->max_num_tdls_vdevs) {
7434 ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
7435 arvif->vdev_id,
7436 ar->max_num_tdls_vdevs);
7437 ret = -ELNRNG;
7438 goto exit;
7439 }
7440 peer_type = WMI_PEER_TYPE_TDLS;
7441 }
7442
7c354242 7443 ret = ath10k_mac_inc_num_stations(arvif, sta);
cfd1061e
MK
7444 if (ret) {
7445 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
7446 ar->max_num_stations);
0e759f36
BM
7447 goto exit;
7448 }
7449
386f97e3
ZC
7450 if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7451 arsta->tx_stats = kzalloc(sizeof(*arsta->tx_stats),
7452 GFP_KERNEL);
7453 if (!arsta->tx_stats) {
402838a0 7454 ath10k_mac_dec_num_stations(arvif, sta);
386f97e3
ZC
7455 ret = -ENOMEM;
7456 goto exit;
7457 }
7458 }
7459
6942726f
MK
7460 ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
7461 sta->addr, peer_type);
a52c0282 7462 if (ret) {
7aa7a72a 7463 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
479398b0 7464 sta->addr, arvif->vdev_id, ret);
7c354242 7465 ath10k_mac_dec_num_stations(arvif, sta);
386f97e3 7466 kfree(arsta->tx_stats);
a52c0282
MK
7467 goto exit;
7468 }
077efc8c 7469
bb8f0c6a
MK
7470 spin_lock_bh(&ar->data_lock);
7471
7472 peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
7473 if (!peer) {
7474 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
7475 vif->addr, arvif->vdev_id);
7476 spin_unlock_bh(&ar->data_lock);
7477 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7478 ath10k_mac_dec_num_stations(arvif, sta);
386f97e3 7479 kfree(arsta->tx_stats);
bb8f0c6a
MK
7480 ret = -ENOENT;
7481 goto exit;
7482 }
7483
7484 arsta->peer_id = find_first_bit(peer->peer_ids,
7485 ATH10K_MAX_NUM_PEER_IDS);
7486
7487 spin_unlock_bh(&ar->data_lock);
7488
f4fe2e53 7489 if (!sta->tdls)
75d85fd9
MP
7490 goto exit;
7491
9a993cc1
RH
7492 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7493 WMI_TDLS_ENABLE_ACTIVE);
7494 if (ret) {
7495 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7496 arvif->vdev_id, ret);
7497 ath10k_peer_delete(ar, arvif->vdev_id,
7498 sta->addr);
75d85fd9 7499 ath10k_mac_dec_num_stations(arvif, sta);
386f97e3 7500 kfree(arsta->tx_stats);
a52c0282
MK
7501 goto exit;
7502 }
077efc8c 7503
75d85fd9
MP
7504 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7505 WMI_TDLS_PEER_STATE_PEERING);
7506 if (ret) {
7507 ath10k_warn(ar,
7508 "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
7509 sta->addr, arvif->vdev_id, ret);
7510 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7511 ath10k_mac_dec_num_stations(arvif, sta);
386f97e3 7512 kfree(arsta->tx_stats);
077efc8c 7513
75d85fd9
MP
7514 if (num_tdls_stations != 0)
7515 goto exit;
7516 ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7517 WMI_TDLS_DISABLE);
077efc8c 7518 }
5e3dd157
KV
7519 } else if ((old_state == IEEE80211_STA_NONE &&
7520 new_state == IEEE80211_STA_NOTEXIST)) {
7521 /*
7522 * Existing station deletion.
7523 */
97614c59 7524 ath10k_dbg(ar, ATH10K_DBG_STA,
30404201
BG
7525 "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
7526 arvif->vdev_id, sta->addr, sta);
077efc8c 7527
424ea0d1
MP
7528 if (sta->tdls) {
7529 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id,
7530 sta,
7531 WMI_TDLS_PEER_STATE_TEARDOWN);
7532 if (ret)
7533 ath10k_warn(ar, "failed to update tdls peer state for %pM state %d: %i\n",
7534 sta->addr,
7535 WMI_TDLS_PEER_STATE_TEARDOWN, ret);
7536 }
7537
5e3dd157
KV
7538 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
7539 if (ret)
7aa7a72a 7540 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
69244e56 7541 sta->addr, arvif->vdev_id, ret);
5e3dd157 7542
7c354242 7543 ath10k_mac_dec_num_stations(arvif, sta);
75d85fd9 7544
6942726f
MK
7545 spin_lock_bh(&ar->data_lock);
7546 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
7547 peer = ar->peer_map[i];
7548 if (!peer)
7549 continue;
7550
7551 if (peer->sta == sta) {
75b34800 7552 ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
d0eeafad 7553 sta->addr, peer, i, arvif->vdev_id);
6942726f 7554 peer->sta = NULL;
d0eeafad
BG
7555
7556 /* Clean up the peer object as well since we
7557 * must have failed to do this above.
7558 */
7559 list_del(&peer->list);
7560 ar->peer_map[i] = NULL;
7561 kfree(peer);
7562 ar->num_peers--;
6942726f
MK
7563 }
7564 }
7565 spin_unlock_bh(&ar->data_lock);
7566
553a7cca
KP
7567 if (ath10k_debug_is_extd_tx_stats_enabled(ar)) {
7568 kfree(arsta->tx_stats);
7569 arsta->tx_stats = NULL;
7570 }
7571
29946878
MK
7572 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
7573 ath10k_mac_txq_unref(ar, sta->txq[i]);
7574
75d85fd9
MP
7575 if (!sta->tdls)
7576 goto exit;
7577
7578 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
7579 goto exit;
7580
7581 /* This was the last tdls peer in current vif */
7582 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
7583 WMI_TDLS_DISABLE);
7584 if (ret) {
7585 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
7586 arvif->vdev_id, ret);
7587 }
5e3dd157
KV
7588 } else if (old_state == IEEE80211_STA_AUTH &&
7589 new_state == IEEE80211_STA_ASSOC &&
7590 (vif->type == NL80211_IFTYPE_AP ||
b6c7bafa 7591 vif->type == NL80211_IFTYPE_MESH_POINT ||
5e3dd157
KV
7592 vif->type == NL80211_IFTYPE_ADHOC)) {
7593 /*
7594 * New association.
7595 */
97614c59 7596 ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM associated\n",
60c3daa8
KV
7597 sta->addr);
7598
590922a8 7599 ret = ath10k_station_assoc(ar, vif, sta, false);
5e3dd157 7600 if (ret)
7aa7a72a 7601 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
69244e56 7602 sta->addr, arvif->vdev_id, ret);
5e3dd157 7603 } else if (old_state == IEEE80211_STA_ASSOC &&
75d85fd9
MP
7604 new_state == IEEE80211_STA_AUTHORIZED &&
7605 sta->tdls) {
7606 /*
7607 * Tdls station authorized.
7608 */
97614c59 7609 ath10k_dbg(ar, ATH10K_DBG_STA, "mac tdls sta %pM authorized\n",
75d85fd9
MP
7610 sta->addr);
7611
7612 ret = ath10k_station_assoc(ar, vif, sta, false);
7613 if (ret) {
7614 ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
7615 sta->addr, arvif->vdev_id, ret);
7616 goto exit;
7617 }
7618
7619 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
7620 WMI_TDLS_PEER_STATE_CONNECTED);
7621 if (ret)
7622 ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
7623 sta->addr, arvif->vdev_id, ret);
7624 } else if (old_state == IEEE80211_STA_ASSOC &&
7625 new_state == IEEE80211_STA_AUTH &&
7626 (vif->type == NL80211_IFTYPE_AP ||
b6c7bafa 7627 vif->type == NL80211_IFTYPE_MESH_POINT ||
75d85fd9 7628 vif->type == NL80211_IFTYPE_ADHOC)) {
5e3dd157
KV
7629 /*
7630 * Disassociation.
7631 */
97614c59 7632 ath10k_dbg(ar, ATH10K_DBG_STA, "mac sta %pM disassociated\n",
60c3daa8
KV
7633 sta->addr);
7634
590922a8 7635 ret = ath10k_station_disassoc(ar, vif, sta);
5e3dd157 7636 if (ret)
7aa7a72a 7637 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
69244e56 7638 sta->addr, arvif->vdev_id, ret);
5e3dd157 7639 }
0e759f36 7640exit:
5e3dd157
KV
7641 mutex_unlock(&ar->conf_mutex);
7642 return ret;
7643}
7644
7645static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
5b07e07f 7646 u16 ac, bool enable)
5e3dd157 7647{
56ac13bf 7648 struct ath10k_vif *arvif = (void *)vif->drv_priv;
b0e56154
MK
7649 struct wmi_sta_uapsd_auto_trig_arg arg = {};
7650 u32 prio = 0, acc = 0;
5e3dd157
KV
7651 u32 value = 0;
7652 int ret = 0;
7653
548db54c
MK
7654 lockdep_assert_held(&ar->conf_mutex);
7655
5e3dd157
KV
7656 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
7657 return 0;
7658
7659 switch (ac) {
7660 case IEEE80211_AC_VO:
7661 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
7662 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
b0e56154
MK
7663 prio = 7;
7664 acc = 3;
5e3dd157
KV
7665 break;
7666 case IEEE80211_AC_VI:
7667 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
7668 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
b0e56154
MK
7669 prio = 5;
7670 acc = 2;
5e3dd157
KV
7671 break;
7672 case IEEE80211_AC_BE:
7673 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
7674 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
b0e56154
MK
7675 prio = 2;
7676 acc = 1;
5e3dd157
KV
7677 break;
7678 case IEEE80211_AC_BK:
7679 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
7680 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
b0e56154
MK
7681 prio = 0;
7682 acc = 0;
5e3dd157
KV
7683 break;
7684 }
7685
7686 if (enable)
7687 arvif->u.sta.uapsd |= value;
7688 else
7689 arvif->u.sta.uapsd &= ~value;
7690
7691 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7692 WMI_STA_PS_PARAM_UAPSD,
7693 arvif->u.sta.uapsd);
7694 if (ret) {
7aa7a72a 7695 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
5e3dd157
KV
7696 goto exit;
7697 }
7698
7699 if (arvif->u.sta.uapsd)
7700 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
7701 else
7702 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
7703
7704 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
7705 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
7706 value);
7707 if (ret)
7aa7a72a 7708 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
5e3dd157 7709
9f9b5746
MK
7710 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
7711 if (ret) {
7712 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
7713 arvif->vdev_id, ret);
7714 return ret;
7715 }
7716
7717 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
7718 if (ret) {
7719 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
7720 arvif->vdev_id, ret);
7721 return ret;
7722 }
7723
b0e56154
MK
7724 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
7725 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
7726 /* Only userspace can make an educated decision when to send
7727 * trigger frame. The following effectively disables u-UAPSD
7728 * autotrigger in firmware (which is enabled by default
7729 * provided the autotrigger service is available).
7730 */
7731
7732 arg.wmm_ac = acc;
7733 arg.user_priority = prio;
7734 arg.service_interval = 0;
7735 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7736 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
7737
7738 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
7739 arvif->bssid, &arg, 1);
7740 if (ret) {
7741 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
7742 ret);
7743 return ret;
7744 }
7745 }
7746
5e3dd157
KV
7747exit:
7748 return ret;
7749}
7750
7751static int ath10k_conf_tx(struct ieee80211_hw *hw,
7752 struct ieee80211_vif *vif, u16 ac,
7753 const struct ieee80211_tx_queue_params *params)
7754{
7755 struct ath10k *ar = hw->priv;
56ac13bf 7756 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157
KV
7757 struct wmi_wmm_params_arg *p = NULL;
7758 int ret;
7759
7760 mutex_lock(&ar->conf_mutex);
7761
7762 switch (ac) {
7763 case IEEE80211_AC_VO:
5e752e42 7764 p = &arvif->wmm_params.ac_vo;
5e3dd157
KV
7765 break;
7766 case IEEE80211_AC_VI:
5e752e42 7767 p = &arvif->wmm_params.ac_vi;
5e3dd157
KV
7768 break;
7769 case IEEE80211_AC_BE:
5e752e42 7770 p = &arvif->wmm_params.ac_be;
5e3dd157
KV
7771 break;
7772 case IEEE80211_AC_BK:
5e752e42 7773 p = &arvif->wmm_params.ac_bk;
5e3dd157
KV
7774 break;
7775 }
7776
7777 if (WARN_ON(!p)) {
7778 ret = -EINVAL;
7779 goto exit;
7780 }
7781
7782 p->cwmin = params->cw_min;
7783 p->cwmax = params->cw_max;
7784 p->aifs = params->aifs;
7785
7786 /*
7787 * The channel time duration programmed in the HW is in absolute
7788 * microseconds, while mac80211 gives the txop in units of
7789 * 32 microseconds.
7790 */
7791 p->txop = params->txop * 32;
7792
7fc979a7
MK
7793 if (ar->wmi.ops->gen_vdev_wmm_conf) {
7794 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
7795 &arvif->wmm_params);
7796 if (ret) {
7797 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
7798 arvif->vdev_id, ret);
7799 goto exit;
7800 }
7801 } else {
7802 /* This won't work well with multi-interface cases but it's
7803 * better than nothing.
7804 */
7805 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
7806 if (ret) {
7807 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
7808 goto exit;
7809 }
5e3dd157
KV
7810 }
7811
7812 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
7813 if (ret)
7aa7a72a 7814 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
5e3dd157
KV
7815
7816exit:
7817 mutex_unlock(&ar->conf_mutex);
7818 return ret;
7819}
7820
5e3dd157
KV
7821static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
7822 struct ieee80211_vif *vif,
7823 struct ieee80211_channel *chan,
7824 int duration,
7825 enum ieee80211_roc_type type)
7826{
7827 struct ath10k *ar = hw->priv;
56ac13bf 7828 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157 7829 struct wmi_start_scan_arg arg;
5c81c7fd 7830 int ret = 0;
fcf98446 7831 u32 scan_time_msec;
5e3dd157
KV
7832
7833 mutex_lock(&ar->conf_mutex);
7834
a6080931
AK
7835 if (ath10k_mac_tdls_vif_stations_count(hw, vif) > 0) {
7836 ret = -EBUSY;
7837 goto exit;
7838 }
7839
5e3dd157 7840 spin_lock_bh(&ar->data_lock);
5c81c7fd
MK
7841 switch (ar->scan.state) {
7842 case ATH10K_SCAN_IDLE:
7843 reinit_completion(&ar->scan.started);
7844 reinit_completion(&ar->scan.completed);
7845 reinit_completion(&ar->scan.on_channel);
7846 ar->scan.state = ATH10K_SCAN_STARTING;
7847 ar->scan.is_roc = true;
7848 ar->scan.vdev_id = arvif->vdev_id;
7849 ar->scan.roc_freq = chan->center_freq;
d710e75d 7850 ar->scan.roc_notify = true;
5c81c7fd
MK
7851 ret = 0;
7852 break;
7853 case ATH10K_SCAN_STARTING:
7854 case ATH10K_SCAN_RUNNING:
7855 case ATH10K_SCAN_ABORTING:
5e3dd157 7856 ret = -EBUSY;
5c81c7fd 7857 break;
5e3dd157 7858 }
5e3dd157
KV
7859 spin_unlock_bh(&ar->data_lock);
7860
5c81c7fd
MK
7861 if (ret)
7862 goto exit;
7863
fcf98446 7864 scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
dcca0bdb 7865
5e3dd157
KV
7866 memset(&arg, 0, sizeof(arg));
7867 ath10k_wmi_start_scan_init(ar, &arg);
7868 arg.vdev_id = arvif->vdev_id;
7869 arg.scan_id = ATH10K_SCAN_ID;
7870 arg.n_channels = 1;
7871 arg.channels[0] = chan->center_freq;
fcf98446
MK
7872 arg.dwell_time_active = scan_time_msec;
7873 arg.dwell_time_passive = scan_time_msec;
7874 arg.max_scan_time = scan_time_msec;
5e3dd157
KV
7875 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
7876 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
dbd3f9f3 7877 arg.burst_duration_ms = duration;
5e3dd157
KV
7878
7879 ret = ath10k_start_scan(ar, &arg);
7880 if (ret) {
7aa7a72a 7881 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
5e3dd157 7882 spin_lock_bh(&ar->data_lock);
5c81c7fd 7883 ar->scan.state = ATH10K_SCAN_IDLE;
5e3dd157
KV
7884 spin_unlock_bh(&ar->data_lock);
7885 goto exit;
7886 }
7887
14e105cd 7888 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
5e3dd157 7889 if (ret == 0) {
7aa7a72a 7890 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
5c81c7fd
MK
7891
7892 ret = ath10k_scan_stop(ar);
7893 if (ret)
7aa7a72a 7894 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
5c81c7fd 7895
5e3dd157
KV
7896 ret = -ETIMEDOUT;
7897 goto exit;
7898 }
7899
fcf98446
MK
7900 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
7901 msecs_to_jiffies(duration));
7902
5e3dd157
KV
7903 ret = 0;
7904exit:
7905 mutex_unlock(&ar->conf_mutex);
7906 return ret;
7907}
7908
5db4c4b9
EG
7909static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw,
7910 struct ieee80211_vif *vif)
5e3dd157
KV
7911{
7912 struct ath10k *ar = hw->priv;
7913
7914 mutex_lock(&ar->conf_mutex);
d710e75d
MK
7915
7916 spin_lock_bh(&ar->data_lock);
7917 ar->scan.roc_notify = false;
7918 spin_unlock_bh(&ar->data_lock);
7919
5c81c7fd 7920 ath10k_scan_abort(ar);
d710e75d 7921
5e3dd157
KV
7922 mutex_unlock(&ar->conf_mutex);
7923
4eb2e164
MK
7924 cancel_delayed_work_sync(&ar->scan.timeout);
7925
5e3dd157
KV
7926 return 0;
7927}
7928
7929/*
7930 * Both RTS and Fragmentation threshold are interface-specific
7931 * in ath10k, but device-specific in mac80211.
7932 */
5e3dd157 7933
ad088bfa
MK
7934static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
7935{
7936 struct ath10k *ar = hw->priv;
7937 struct ath10k_vif *arvif;
7938 int ret = 0;
548db54c 7939
5e3dd157 7940 mutex_lock(&ar->conf_mutex);
ad088bfa 7941 list_for_each_entry(arvif, &ar->arvifs, list) {
7aa7a72a 7942 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
ad088bfa
MK
7943 arvif->vdev_id, value);
7944
7945 ret = ath10k_mac_set_rts(arvif, value);
7946 if (ret) {
7aa7a72a 7947 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
ad088bfa
MK
7948 arvif->vdev_id, ret);
7949 break;
7950 }
7951 }
5e3dd157
KV
7952 mutex_unlock(&ar->conf_mutex);
7953
ad088bfa 7954 return ret;
5e3dd157
KV
7955}
7956
92092fe5
MK
7957static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
7958{
7959 /* Even though there's a WMI enum for fragmentation threshold no known
7960 * firmware actually implements it. Moreover it is not possible to rely
7961 * frame fragmentation to mac80211 because firmware clears the "more
7962 * fragments" bit in frame control making it impossible for remote
7963 * devices to reassemble frames.
7964 *
7965 * Hence implement a dummy callback just to say fragmentation isn't
7966 * supported. This effectively prevents mac80211 from doing frame
7967 * fragmentation in software.
7968 */
7969 return -EOPNOTSUPP;
7970}
7971
828853ac 7972void ath10k_mac_wait_tx_complete(struct ath10k *ar)
5e3dd157 7973{
affd3217 7974 bool skip;
d4298a3a 7975 long time_left;
5e3dd157
KV
7976
7977 /* mac80211 doesn't care if we really xmit queued frames or not
d6dfe25c
MR
7978 * we'll collect those frames either way if we stop/delete vdevs
7979 */
548db54c 7980
affd3217 7981 if (ar->state == ATH10K_STATE_WEDGED)
828853ac 7982 return;
affd3217 7983
d4298a3a 7984 time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
5e3dd157 7985 bool empty;
affd3217 7986
edb8236d 7987 spin_lock_bh(&ar->htt.tx_lock);
0945baf7 7988 empty = (ar->htt.num_pending_tx == 0);
edb8236d 7989 spin_unlock_bh(&ar->htt.tx_lock);
affd3217 7990
7962b0d8
MK
7991 skip = (ar->state == ATH10K_STATE_WEDGED) ||
7992 test_bit(ATH10K_FLAG_CRASH_FLUSH,
7993 &ar->dev_flags);
affd3217
MK
7994
7995 (empty || skip);
5e3dd157 7996 }), ATH10K_FLUSH_TIMEOUT_HZ);
affd3217 7997
d4298a3a
NMG
7998 if (time_left == 0 || skip)
7999 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
8000 skip, ar->state, time_left);
828853ac 8001}
548db54c 8002
828853ac
WG
8003static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
8004 u32 queues, bool drop)
8005{
8006 struct ath10k *ar = hw->priv;
9de4162f
WG
8007 struct ath10k_vif *arvif;
8008 u32 bitmap;
8009
8010 if (drop) {
d987f783 8011 if (vif && vif->type == NL80211_IFTYPE_STATION) {
9de4162f
WG
8012 bitmap = ~(1 << WMI_MGMT_TID);
8013 list_for_each_entry(arvif, &ar->arvifs, list) {
8014 if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
8015 ath10k_wmi_peer_flush(ar, arvif->vdev_id,
8016 arvif->bssid, bitmap);
8017 }
dd7fc554 8018 ath10k_htt_flush_tx(&ar->htt);
9de4162f 8019 }
828853ac 8020 return;
9de4162f 8021 }
828853ac
WG
8022
8023 mutex_lock(&ar->conf_mutex);
8024 ath10k_mac_wait_tx_complete(ar);
548db54c 8025 mutex_unlock(&ar->conf_mutex);
5e3dd157
KV
8026}
8027
8028/* TODO: Implement this function properly
8029 * For now it is needed to reply to Probe Requests in IBSS mode.
8030 * Propably we need this information from FW.
8031 */
8032static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
8033{
8034 return 1;
8035}
8036
cf2c92d8
EP
8037static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
8038 enum ieee80211_reconfig_type reconfig_type)
affd3217
MK
8039{
8040 struct ath10k *ar = hw->priv;
8041
cf2c92d8
EP
8042 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
8043 return;
8044
affd3217
MK
8045 mutex_lock(&ar->conf_mutex);
8046
8047 /* If device failed to restart it will be in a different state, e.g.
d6dfe25c
MR
8048 * ATH10K_STATE_WEDGED
8049 */
affd3217 8050 if (ar->state == ATH10K_STATE_RESTARTED) {
7aa7a72a 8051 ath10k_info(ar, "device successfully recovered\n");
affd3217 8052 ar->state = ATH10K_STATE_ON;
7962b0d8 8053 ieee80211_wake_queues(ar->hw);
5dadbe4e 8054 clear_bit(ATH10K_FLAG_RESTARTING, &ar->dev_flags);
affd3217
MK
8055 }
8056
8057 mutex_unlock(&ar->conf_mutex);
8058}
8059
fa7937e3
RM
8060static void
8061ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
8062 struct ieee80211_channel *channel)
8063{
8064 int ret;
720e5c03 8065 enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ;
fa7937e3
RM
8066
8067 lockdep_assert_held(&ar->conf_mutex);
8068
8069 if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
8070 (ar->rx_channel != channel))
8071 return;
8072
8073 if (ar->scan.state != ATH10K_SCAN_IDLE) {
8074 ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
8075 return;
8076 }
8077
8078 reinit_completion(&ar->bss_survey_done);
8079
8080 ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
8081 if (ret) {
8082 ath10k_warn(ar, "failed to send pdev bss chan info request\n");
8083 return;
8084 }
8085
8086 ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
8087 if (!ret) {
8088 ath10k_warn(ar, "bss channel survey timed out\n");
8089 return;
8090 }
8091}
8092
2e1dea40
MK
8093static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
8094 struct survey_info *survey)
8095{
8096 struct ath10k *ar = hw->priv;
8097 struct ieee80211_supported_band *sband;
8098 struct survey_info *ar_survey = &ar->survey[idx];
8099 int ret = 0;
8100
8101 mutex_lock(&ar->conf_mutex);
8102
57fbcce3 8103 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
2e1dea40
MK
8104 if (sband && idx >= sband->n_channels) {
8105 idx -= sband->n_channels;
8106 sband = NULL;
8107 }
8108
8109 if (!sband)
57fbcce3 8110 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
2e1dea40
MK
8111
8112 if (!sband || idx >= sband->n_channels) {
8113 ret = -ENOENT;
8114 goto exit;
8115 }
8116
77eb3d69 8117 ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
fa7937e3 8118
2e1dea40
MK
8119 spin_lock_bh(&ar->data_lock);
8120 memcpy(survey, ar_survey, sizeof(*survey));
8121 spin_unlock_bh(&ar->data_lock);
8122
8123 survey->channel = &sband->channels[idx];
8124
fa1d4df8
FF
8125 if (ar->rx_channel == survey->channel)
8126 survey->filled |= SURVEY_INFO_IN_USE;
8127
2e1dea40
MK
8128exit:
8129 mutex_unlock(&ar->conf_mutex);
8130 return ret;
8131}
8132
51ab1a0a 8133static bool
3ae54225 8134ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
57fbcce3 8135 enum nl80211_band band,
3ae54225
MK
8136 const struct cfg80211_bitrate_mask *mask,
8137 int *nss)
8138{
8139 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
8140 u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
8141 u8 ht_nss_mask = 0;
8142 u8 vht_nss_mask = 0;
8143 int i;
51ab1a0a 8144
3ae54225 8145 if (mask->control[band].legacy)
51ab1a0a
JD
8146 return false;
8147
3ae54225
MK
8148 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
8149 if (mask->control[band].ht_mcs[i] == 0)
51ab1a0a 8150 continue;
3ae54225
MK
8151 else if (mask->control[band].ht_mcs[i] ==
8152 sband->ht_cap.mcs.rx_mask[i])
8153 ht_nss_mask |= BIT(i);
8154 else
8155 return false;
51ab1a0a
JD
8156 }
8157
3ae54225
MK
8158 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
8159 if (mask->control[band].vht_mcs[i] == 0)
51ab1a0a 8160 continue;
3ae54225
MK
8161 else if (mask->control[band].vht_mcs[i] ==
8162 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
8163 vht_nss_mask |= BIT(i);
8164 else
8165 return false;
51ab1a0a
JD
8166 }
8167
3ae54225 8168 if (ht_nss_mask != vht_nss_mask)
51ab1a0a
JD
8169 return false;
8170
3ae54225 8171 if (ht_nss_mask == 0)
51ab1a0a
JD
8172 return false;
8173
3ae54225 8174 if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
51ab1a0a
JD
8175 return false;
8176
3ae54225 8177 *nss = fls(ht_nss_mask);
51ab1a0a
JD
8178
8179 return true;
8180}
8181
3ae54225 8182static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
bd4a41e6 8183 u8 rate, u8 nss, u8 sgi, u8 ldpc)
51ab1a0a
JD
8184{
8185 struct ath10k *ar = arvif->ar;
8186 u32 vdev_param;
3ae54225 8187 int ret;
51ab1a0a 8188
3ae54225 8189 lockdep_assert_held(&ar->conf_mutex);
51ab1a0a 8190
779750bb 8191 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02x nss %u sgi %u\n",
3ae54225 8192 arvif->vdev_id, rate, nss, sgi);
9f81f725 8193
51ab1a0a 8194 vdev_param = ar->wmi.vdev_param->fixed_rate;
3ae54225 8195 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
51ab1a0a 8196 if (ret) {
7aa7a72a 8197 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
3ae54225
MK
8198 rate, ret);
8199 return ret;
51ab1a0a
JD
8200 }
8201
51ab1a0a 8202 vdev_param = ar->wmi.vdev_param->nss;
3ae54225 8203 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
51ab1a0a 8204 if (ret) {
3ae54225
MK
8205 ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
8206 return ret;
51ab1a0a 8207 }
51ab1a0a 8208
9f81f725 8209 vdev_param = ar->wmi.vdev_param->sgi;
3ae54225 8210 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
51ab1a0a 8211 if (ret) {
3ae54225
MK
8212 ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
8213 return ret;
51ab1a0a
JD
8214 }
8215
bd4a41e6
RM
8216 vdev_param = ar->wmi.vdev_param->ldpc;
8217 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
8218 if (ret) {
8219 ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
8220 return ret;
8221 }
8222
3ae54225 8223 return 0;
51ab1a0a 8224}
51ab1a0a 8225
45c9abc0
MK
8226static bool
8227ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
57fbcce3 8228 enum nl80211_band band,
8b97b055
MP
8229 const struct cfg80211_bitrate_mask *mask,
8230 bool allow_pfr)
45c9abc0
MK
8231{
8232 int i;
8233 u16 vht_mcs;
9f81f725 8234
45c9abc0
MK
8235 /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
8236 * to express all VHT MCS rate masks. Effectively only the following
8237 * ranges can be used: none, 0-7, 0-8 and 0-9.
8238 */
8239 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
8240 vht_mcs = mask->control[band].vht_mcs[i];
8241
8242 switch (vht_mcs) {
8243 case 0:
8244 case BIT(8) - 1:
8245 case BIT(9) - 1:
8246 case BIT(10) - 1:
8247 break;
8248 default:
8b97b055
MP
8249 if (!allow_pfr)
8250 ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
45c9abc0
MK
8251 return false;
8252 }
9f81f725
JD
8253 }
8254
45c9abc0
MK
8255 return true;
8256}
9f81f725 8257
8b97b055
MP
8258static bool ath10k_mac_set_vht_bitrate_mask_fixup(struct ath10k *ar,
8259 struct ath10k_vif *arvif,
8260 struct ieee80211_sta *sta)
8261{
8262 int err;
8263 u8 rate = arvif->vht_pfr;
8264
8265 /* skip non vht and multiple rate peers */
8266 if (!sta->vht_cap.vht_supported || arvif->vht_num_rates != 1)
8267 return false;
8268
8269 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8270 WMI_PEER_PARAM_FIXED_RATE, rate);
8271 if (err)
09764659 8272 ath10k_warn(ar, "failed to enable STA %pM peer fixed rate: %d\n",
8b97b055
MP
8273 sta->addr, err);
8274
8275 return true;
8276}
8277
45c9abc0
MK
8278static void ath10k_mac_set_bitrate_mask_iter(void *data,
8279 struct ieee80211_sta *sta)
8280{
8281 struct ath10k_vif *arvif = data;
8282 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8283 struct ath10k *ar = arvif->ar;
8284
8285 if (arsta->arvif != arvif)
8286 return;
8287
8b97b055
MP
8288 if (ath10k_mac_set_vht_bitrate_mask_fixup(ar, arvif, sta))
8289 return;
8290
45c9abc0
MK
8291 spin_lock_bh(&ar->data_lock);
8292 arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
8293 spin_unlock_bh(&ar->data_lock);
8294
8295 ieee80211_queue_work(ar->hw, &arsta->update_wk);
51ab1a0a
JD
8296}
8297
8b97b055
MP
8298static void ath10k_mac_clr_bitrate_mask_iter(void *data,
8299 struct ieee80211_sta *sta)
8300{
8301 struct ath10k_vif *arvif = data;
8302 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8303 struct ath10k *ar = arvif->ar;
8304 int err;
8305
8306 /* clear vht peers only */
8307 if (arsta->arvif != arvif || !sta->vht_cap.vht_supported)
8308 return;
8309
8310 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
8311 WMI_PEER_PARAM_FIXED_RATE,
8312 WMI_FIXED_RATE_NONE);
8313 if (err)
8314 ath10k_warn(ar, "failed to clear STA %pM peer fixed rate: %d\n",
8315 sta->addr, err);
8316}
8317
3ae54225
MK
8318static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
8319 struct ieee80211_vif *vif,
8320 const struct cfg80211_bitrate_mask *mask)
51ab1a0a 8321{
56ac13bf 8322 struct ath10k_vif *arvif = (void *)vif->drv_priv;
500ff9f9 8323 struct cfg80211_chan_def def;
51ab1a0a 8324 struct ath10k *ar = arvif->ar;
57fbcce3 8325 enum nl80211_band band;
45c9abc0
MK
8326 const u8 *ht_mcs_mask;
8327 const u16 *vht_mcs_mask;
3ae54225
MK
8328 u8 rate;
8329 u8 nss;
8330 u8 sgi;
bd4a41e6 8331 u8 ldpc;
3ae54225
MK
8332 int single_nss;
8333 int ret;
8b97b055
MP
8334 int vht_num_rates, allow_pfr;
8335 u8 vht_pfr;
8336 bool update_bitrate_mask = true;
9f81f725 8337
500ff9f9
MK
8338 if (ath10k_mac_vif_chan(vif, &def))
8339 return -EPERM;
b116ea19 8340
500ff9f9 8341 band = def.chan->band;
45c9abc0
MK
8342 ht_mcs_mask = mask->control[band].ht_mcs;
8343 vht_mcs_mask = mask->control[band].vht_mcs;
bd4a41e6 8344 ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
b116ea19 8345
3ae54225
MK
8346 sgi = mask->control[band].gi;
8347 if (sgi == NL80211_TXRATE_FORCE_LGI)
9f81f725 8348 return -EINVAL;
51ab1a0a 8349
8b97b055
MP
8350 allow_pfr = test_bit(ATH10K_FW_FEATURE_PEER_FIXED_RATE,
8351 ar->normal_mode_fw.fw_file.fw_features);
8352 if (allow_pfr) {
8353 mutex_lock(&ar->conf_mutex);
8354 ieee80211_iterate_stations_atomic(ar->hw,
8355 ath10k_mac_clr_bitrate_mask_iter,
8356 arvif);
8357 mutex_unlock(&ar->conf_mutex);
8358 }
8359
8360 if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask,
8361 &vht_num_rates)) {
3ae54225 8362 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8b97b055
MP
8363 &rate, &nss,
8364 false);
3ae54225
MK
8365 if (ret) {
8366 ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
8367 arvif->vdev_id, ret);
8368 return ret;
8369 }
8370 } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
8371 &single_nss)) {
8372 rate = WMI_FIXED_RATE_NONE;
8373 nss = single_nss;
8374 } else {
8375 rate = WMI_FIXED_RATE_NONE;
45c9abc0
MK
8376 nss = min(ar->num_rf_chains,
8377 max(ath10k_mac_max_ht_nss(ht_mcs_mask),
8378 ath10k_mac_max_vht_nss(vht_mcs_mask)));
8379
8b97b055
MP
8380 if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask,
8381 allow_pfr)) {
8382 u8 vht_nss;
8383
8384 if (!allow_pfr || vht_num_rates != 1)
8385 return -EINVAL;
8386
8387 /* Reach here, firmware supports peer fixed rate and has
8388 * single vht rate, and don't update vif birate_mask, as
8389 * the rate only for specific peer.
8390 */
8391 ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
8392 &vht_pfr,
8393 &vht_nss,
8394 true);
8395 update_bitrate_mask = false;
ff414f31
AB
8396 } else {
8397 vht_pfr = 0;
8b97b055 8398 }
45c9abc0
MK
8399
8400 mutex_lock(&ar->conf_mutex);
8401
8b97b055
MP
8402 if (update_bitrate_mask)
8403 arvif->bitrate_mask = *mask;
8404 arvif->vht_num_rates = vht_num_rates;
8405 arvif->vht_pfr = vht_pfr;
45c9abc0
MK
8406 ieee80211_iterate_stations_atomic(ar->hw,
8407 ath10k_mac_set_bitrate_mask_iter,
8408 arvif);
8409
8410 mutex_unlock(&ar->conf_mutex);
51ab1a0a
JD
8411 }
8412
3ae54225
MK
8413 mutex_lock(&ar->conf_mutex);
8414
bd4a41e6 8415 ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
3ae54225
MK
8416 if (ret) {
8417 ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
8418 arvif->vdev_id, ret);
8419 goto exit;
9f81f725
JD
8420 }
8421
3ae54225
MK
8422exit:
8423 mutex_unlock(&ar->conf_mutex);
8424
8425 return ret;
51ab1a0a
JD
8426}
8427
9797febc
MK
8428static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
8429 struct ieee80211_vif *vif,
8430 struct ieee80211_sta *sta,
8431 u32 changed)
8432{
8433 struct ath10k *ar = hw->priv;
8434 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
8b2d93dd
KP
8435 struct ath10k_vif *arvif = (void *)vif->drv_priv;
8436 struct ath10k_peer *peer;
9797febc
MK
8437 u32 bw, smps;
8438
8439 spin_lock_bh(&ar->data_lock);
8440
8b2d93dd
KP
8441 peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
8442 if (!peer) {
8443 spin_unlock_bh(&ar->data_lock);
8444 ath10k_warn(ar, "mac sta rc update failed to find peer %pM on vdev %i\n",
8445 sta->addr, arvif->vdev_id);
8446 return;
8447 }
8448
97614c59 8449 ath10k_dbg(ar, ATH10K_DBG_STA,
9797febc
MK
8450 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
8451 sta->addr, changed, sta->bandwidth, sta->rx_nss,
8452 sta->smps_mode);
8453
8454 if (changed & IEEE80211_RC_BW_CHANGED) {
8455 bw = WMI_PEER_CHWIDTH_20MHZ;
8456
8457 switch (sta->bandwidth) {
8458 case IEEE80211_STA_RX_BW_20:
8459 bw = WMI_PEER_CHWIDTH_20MHZ;
8460 break;
8461 case IEEE80211_STA_RX_BW_40:
8462 bw = WMI_PEER_CHWIDTH_40MHZ;
8463 break;
8464 case IEEE80211_STA_RX_BW_80:
8465 bw = WMI_PEER_CHWIDTH_80MHZ;
8466 break;
8467 case IEEE80211_STA_RX_BW_160:
bc1efd73
SG
8468 bw = WMI_PEER_CHWIDTH_160MHZ;
8469 break;
8470 default:
d939be3a 8471 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
be6546fc 8472 sta->bandwidth, sta->addr);
9797febc
MK
8473 bw = WMI_PEER_CHWIDTH_20MHZ;
8474 break;
8475 }
8476
8477 arsta->bw = bw;
8478 }
8479
8480 if (changed & IEEE80211_RC_NSS_CHANGED)
8481 arsta->nss = sta->rx_nss;
8482
8483 if (changed & IEEE80211_RC_SMPS_CHANGED) {
8484 smps = WMI_PEER_SMPS_PS_NONE;
8485
8486 switch (sta->smps_mode) {
8487 case IEEE80211_SMPS_AUTOMATIC:
8488 case IEEE80211_SMPS_OFF:
8489 smps = WMI_PEER_SMPS_PS_NONE;
8490 break;
8491 case IEEE80211_SMPS_STATIC:
8492 smps = WMI_PEER_SMPS_STATIC;
8493 break;
8494 case IEEE80211_SMPS_DYNAMIC:
8495 smps = WMI_PEER_SMPS_DYNAMIC;
8496 break;
8497 case IEEE80211_SMPS_NUM_MODES:
7aa7a72a 8498 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
be6546fc 8499 sta->smps_mode, sta->addr);
9797febc
MK
8500 smps = WMI_PEER_SMPS_PS_NONE;
8501 break;
8502 }
8503
8504 arsta->smps = smps;
8505 }
8506
9797febc
MK
8507 arsta->changed |= changed;
8508
8509 spin_unlock_bh(&ar->data_lock);
8510
8511 ieee80211_queue_work(hw, &arsta->update_wk);
8512}
8513
973324ff
PT
8514static void ath10k_offset_tsf(struct ieee80211_hw *hw,
8515 struct ieee80211_vif *vif, s64 tsf_offset)
9f0b7e7d
PO
8516{
8517 struct ath10k *ar = hw->priv;
56ac13bf 8518 struct ath10k_vif *arvif = (void *)vif->drv_priv;
973324ff 8519 u32 offset, vdev_param;
9f0b7e7d
PO
8520 int ret;
8521
973324ff
PT
8522 if (tsf_offset < 0) {
8523 vdev_param = ar->wmi.vdev_param->dec_tsf;
8524 offset = -tsf_offset;
8525 } else {
8526 vdev_param = ar->wmi.vdev_param->inc_tsf;
8527 offset = tsf_offset;
8528 }
8529
9f0b7e7d 8530 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
973324ff
PT
8531 vdev_param, offset);
8532
9f0b7e7d 8533 if (ret && ret != -EOPNOTSUPP)
973324ff
PT
8534 ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
8535 offset, vdev_param, ret);
9f0b7e7d
PO
8536}
8537
aa5b4fbc
MK
8538static int ath10k_ampdu_action(struct ieee80211_hw *hw,
8539 struct ieee80211_vif *vif,
50ea05ef 8540 struct ieee80211_ampdu_params *params)
aa5b4fbc 8541{
7aa7a72a 8542 struct ath10k *ar = hw->priv;
56ac13bf 8543 struct ath10k_vif *arvif = (void *)vif->drv_priv;
50ea05ef
SS
8544 struct ieee80211_sta *sta = params->sta;
8545 enum ieee80211_ampdu_mlme_action action = params->action;
8546 u16 tid = params->tid;
aa5b4fbc 8547
779750bb 8548 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %u action %d\n",
aa5b4fbc
MK
8549 arvif->vdev_id, sta->addr, tid, action);
8550
8551 switch (action) {
8552 case IEEE80211_AMPDU_RX_START:
8553 case IEEE80211_AMPDU_RX_STOP:
8554 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
8555 * creation/removal. Do we need to verify this?
8556 */
8557 return 0;
8558 case IEEE80211_AMPDU_TX_START:
8559 case IEEE80211_AMPDU_TX_STOP_CONT:
8560 case IEEE80211_AMPDU_TX_STOP_FLUSH:
8561 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
8562 case IEEE80211_AMPDU_TX_OPERATIONAL:
8563 /* Firmware offloads Tx aggregation entirely so deny mac80211
8564 * Tx aggregation requests.
8565 */
8566 return -EOPNOTSUPP;
8567 }
8568
8569 return -EINVAL;
8570}
8571
500ff9f9 8572static void
d7bf4b4a
MK
8573ath10k_mac_update_rx_channel(struct ath10k *ar,
8574 struct ieee80211_chanctx_conf *ctx,
8575 struct ieee80211_vif_chanctx_switch *vifs,
8576 int n_vifs)
500ff9f9
MK
8577{
8578 struct cfg80211_chan_def *def = NULL;
8579
8580 /* Both locks are required because ar->rx_channel is modified. This
8581 * allows readers to hold either lock.
8582 */
8583 lockdep_assert_held(&ar->conf_mutex);
8584 lockdep_assert_held(&ar->data_lock);
8585
d7bf4b4a 8586 WARN_ON(ctx && vifs);
c73f8c00 8587 WARN_ON(vifs && !n_vifs);
d7bf4b4a 8588
500ff9f9
MK
8589 /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
8590 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
8591 * ppdu on Rx may reduce performance on low-end systems. It should be
8592 * possible to make tables/hashmaps to speed the lookup up (be vary of
8593 * cpu data cache lines though regarding sizes) but to keep the initial
8594 * implementation simple and less intrusive fallback to the slow lookup
8595 * only for multi-channel cases. Single-channel cases will remain to
8596 * use the old channel derival and thus performance should not be
8597 * affected much.
8598 */
8599 rcu_read_lock();
d7bf4b4a 8600 if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
500ff9f9 8601 ieee80211_iter_chan_contexts_atomic(ar->hw,
617b0f4d
KV
8602 ath10k_mac_get_any_chandef_iter,
8603 &def);
d7bf4b4a
MK
8604
8605 if (vifs)
8606 def = &vifs[0].new_ctx->def;
8607
500ff9f9 8608 ar->rx_channel = def->chan;
1ce8c148
RM
8609 } else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
8610 (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
8611 /* During driver restart due to firmware assert, since mac80211
8612 * already has valid channel context for given radio, channel
8613 * context iteration return num_chanctx > 0. So fix rx_channel
8614 * when restart is in progress.
8615 */
d7bf4b4a 8616 ar->rx_channel = ctx->def.chan;
500ff9f9
MK
8617 } else {
8618 ar->rx_channel = NULL;
8619 }
8620 rcu_read_unlock();
8621}
8622
7be6d1b7
MK
8623static void
8624ath10k_mac_update_vif_chan(struct ath10k *ar,
8625 struct ieee80211_vif_chanctx_switch *vifs,
8626 int n_vifs)
8627{
8628 struct ath10k_vif *arvif;
8629 int ret;
8630 int i;
8631
8632 lockdep_assert_held(&ar->conf_mutex);
8633
8634 /* First stop monitor interface. Some FW versions crash if there's a
8635 * lone monitor interface.
8636 */
8637 if (ar->monitor_started)
8638 ath10k_monitor_stop(ar);
8639
8640 for (i = 0; i < n_vifs; i++) {
56ac13bf 8641 arvif = (void *)vifs[i].vif->drv_priv;
7be6d1b7
MK
8642
8643 ath10k_dbg(ar, ATH10K_DBG_MAC,
779750bb 8644 "mac chanctx switch vdev_id %i freq %u->%u width %d->%d\n",
7be6d1b7
MK
8645 arvif->vdev_id,
8646 vifs[i].old_ctx->def.chan->center_freq,
8647 vifs[i].new_ctx->def.chan->center_freq,
8648 vifs[i].old_ctx->def.width,
8649 vifs[i].new_ctx->def.width);
8650
8651 if (WARN_ON(!arvif->is_started))
8652 continue;
8653
8654 if (WARN_ON(!arvif->is_up))
8655 continue;
8656
8657 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8658 if (ret) {
8659 ath10k_warn(ar, "failed to down vdev %d: %d\n",
8660 arvif->vdev_id, ret);
8661 continue;
8662 }
8663 }
8664
8665 /* All relevant vdevs are downed and associated channel resources
8666 * should be available for the channel switch now.
8667 */
8668
8669 spin_lock_bh(&ar->data_lock);
8670 ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
8671 spin_unlock_bh(&ar->data_lock);
8672
8673 for (i = 0; i < n_vifs; i++) {
56ac13bf 8674 arvif = (void *)vifs[i].vif->drv_priv;
7be6d1b7
MK
8675
8676 if (WARN_ON(!arvif->is_started))
8677 continue;
8678
8679 if (WARN_ON(!arvif->is_up))
8680 continue;
8681
8682 ret = ath10k_mac_setup_bcn_tmpl(arvif);
8683 if (ret)
8684 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
8685 ret);
8686
8687 ret = ath10k_mac_setup_prb_tmpl(arvif);
8688 if (ret)
8689 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
8690 ret);
8691
8692 ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
8693 if (ret) {
8694 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
8695 arvif->vdev_id, ret);
8696 continue;
8697 }
8698
8699 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
8700 arvif->bssid);
8701 if (ret) {
8702 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
8703 arvif->vdev_id, ret);
8704 continue;
8705 }
8706 }
8707
8708 ath10k_monitor_recalc(ar);
8709}
8710
500ff9f9
MK
8711static int
8712ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
8713 struct ieee80211_chanctx_conf *ctx)
8714{
8715 struct ath10k *ar = hw->priv;
500ff9f9
MK
8716
8717 ath10k_dbg(ar, ATH10K_DBG_MAC,
779750bb 8718 "mac chanctx add freq %u width %d ptr %pK\n",
500ff9f9
MK
8719 ctx->def.chan->center_freq, ctx->def.width, ctx);
8720
8721 mutex_lock(&ar->conf_mutex);
8722
8723 spin_lock_bh(&ar->data_lock);
d7bf4b4a 8724 ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
500ff9f9
MK
8725 spin_unlock_bh(&ar->data_lock);
8726
8727 ath10k_recalc_radar_detection(ar);
8728 ath10k_monitor_recalc(ar);
8729
8730 mutex_unlock(&ar->conf_mutex);
8731
8732 return 0;
8733}
8734
8735static void
8736ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
8737 struct ieee80211_chanctx_conf *ctx)
8738{
8739 struct ath10k *ar = hw->priv;
8740
8741 ath10k_dbg(ar, ATH10K_DBG_MAC,
779750bb 8742 "mac chanctx remove freq %u width %d ptr %pK\n",
500ff9f9
MK
8743 ctx->def.chan->center_freq, ctx->def.width, ctx);
8744
8745 mutex_lock(&ar->conf_mutex);
8746
8747 spin_lock_bh(&ar->data_lock);
d7bf4b4a 8748 ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
500ff9f9
MK
8749 spin_unlock_bh(&ar->data_lock);
8750
8751 ath10k_recalc_radar_detection(ar);
8752 ath10k_monitor_recalc(ar);
8753
8754 mutex_unlock(&ar->conf_mutex);
8755}
8756
9713e3de
MK
8757struct ath10k_mac_change_chanctx_arg {
8758 struct ieee80211_chanctx_conf *ctx;
8759 struct ieee80211_vif_chanctx_switch *vifs;
8760 int n_vifs;
8761 int next_vif;
8762};
8763
8764static void
8765ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
8766 struct ieee80211_vif *vif)
8767{
8768 struct ath10k_mac_change_chanctx_arg *arg = data;
8769
8770 if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
8771 return;
8772
8773 arg->n_vifs++;
8774}
8775
8776static void
8777ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
8778 struct ieee80211_vif *vif)
8779{
8780 struct ath10k_mac_change_chanctx_arg *arg = data;
8781 struct ieee80211_chanctx_conf *ctx;
8782
8783 ctx = rcu_access_pointer(vif->chanctx_conf);
8784 if (ctx != arg->ctx)
8785 return;
8786
8787 if (WARN_ON(arg->next_vif == arg->n_vifs))
8788 return;
8789
8790 arg->vifs[arg->next_vif].vif = vif;
8791 arg->vifs[arg->next_vif].old_ctx = ctx;
8792 arg->vifs[arg->next_vif].new_ctx = ctx;
8793 arg->next_vif++;
8794}
8795
500ff9f9
MK
8796static void
8797ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
8798 struct ieee80211_chanctx_conf *ctx,
8799 u32 changed)
8800{
8801 struct ath10k *ar = hw->priv;
9713e3de 8802 struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
500ff9f9
MK
8803
8804 mutex_lock(&ar->conf_mutex);
8805
8806 ath10k_dbg(ar, ATH10K_DBG_MAC,
779750bb 8807 "mac chanctx change freq %u width %d ptr %pK changed %x\n",
089ab7a5 8808 ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
500ff9f9
MK
8809
8810 /* This shouldn't really happen because channel switching should use
8811 * switch_vif_chanctx().
8812 */
8813 if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
8814 goto unlock;
8815
9713e3de
MK
8816 if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
8817 ieee80211_iterate_active_interfaces_atomic(
8818 hw,
ee06fcb9 8819 ATH10K_ITER_NORMAL_FLAGS,
9713e3de
MK
8820 ath10k_mac_change_chanctx_cnt_iter,
8821 &arg);
8822 if (arg.n_vifs == 0)
8823 goto radar;
8824
8825 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
8826 GFP_KERNEL);
8827 if (!arg.vifs)
8828 goto radar;
8829
8830 ieee80211_iterate_active_interfaces_atomic(
8831 hw,
ee06fcb9 8832 ATH10K_ITER_NORMAL_FLAGS,
9713e3de
MK
8833 ath10k_mac_change_chanctx_fill_iter,
8834 &arg);
8835 ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
8836 kfree(arg.vifs);
8837 }
8838
8839radar:
500ff9f9
MK
8840 ath10k_recalc_radar_detection(ar);
8841
8842 /* FIXME: How to configure Rx chains properly? */
8843
8844 /* No other actions are actually necessary. Firmware maintains channel
8845 * definitions per vdev internally and there's no host-side channel
8846 * context abstraction to configure, e.g. channel width.
8847 */
8848
8849unlock:
8850 mutex_unlock(&ar->conf_mutex);
8851}
8852
8853static int
8854ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
8855 struct ieee80211_vif *vif,
8856 struct ieee80211_chanctx_conf *ctx)
8857{
8858 struct ath10k *ar = hw->priv;
500ff9f9
MK
8859 struct ath10k_vif *arvif = (void *)vif->drv_priv;
8860 int ret;
8861
8862 mutex_lock(&ar->conf_mutex);
8863
8864 ath10k_dbg(ar, ATH10K_DBG_MAC,
75b34800 8865 "mac chanctx assign ptr %pK vdev_id %i\n",
500ff9f9
MK
8866 ctx, arvif->vdev_id);
8867
8868 if (WARN_ON(arvif->is_started)) {
8869 mutex_unlock(&ar->conf_mutex);
8870 return -EBUSY;
8871 }
8872
089ab7a5 8873 ret = ath10k_vdev_start(arvif, &ctx->def);
500ff9f9
MK
8874 if (ret) {
8875 ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
8876 arvif->vdev_id, vif->addr,
089ab7a5 8877 ctx->def.chan->center_freq, ret);
500ff9f9
MK
8878 goto err;
8879 }
8880
8881 arvif->is_started = true;
8882
f23e587e
MK
8883 ret = ath10k_mac_vif_setup_ps(arvif);
8884 if (ret) {
8885 ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
8886 arvif->vdev_id, ret);
8887 goto err_stop;
8888 }
8889
500ff9f9
MK
8890 if (vif->type == NL80211_IFTYPE_MONITOR) {
8891 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
8892 if (ret) {
8893 ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
8894 arvif->vdev_id, ret);
8895 goto err_stop;
8896 }
8897
8898 arvif->is_up = true;
8899 }
8900
7cfe0455
BM
8901 if (ath10k_mac_can_set_cts_prot(arvif)) {
8902 ret = ath10k_mac_set_cts_prot(arvif);
8903 if (ret)
8904 ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
8905 arvif->vdev_id, ret);
8906 }
8907
4fa42ade
BP
8908 if (ath10k_peer_stats_enabled(ar) &&
8909 ar->hw_params.tx_stats_over_pktlog) {
e8123bb7
AK
8910 ar->pktlog_filter |= ATH10K_PKTLOG_PEER_STATS;
8911 ret = ath10k_wmi_pdev_pktlog_enable(ar,
8912 ar->pktlog_filter);
8913 if (ret) {
8914 ath10k_warn(ar, "failed to enable pktlog %d\n", ret);
8915 goto err_stop;
8916 }
8917 }
8918
500ff9f9
MK
8919 mutex_unlock(&ar->conf_mutex);
8920 return 0;
8921
8922err_stop:
8923 ath10k_vdev_stop(arvif);
8924 arvif->is_started = false;
f23e587e 8925 ath10k_mac_vif_setup_ps(arvif);
500ff9f9
MK
8926
8927err:
8928 mutex_unlock(&ar->conf_mutex);
8929 return ret;
8930}
8931
8932static void
8933ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
8934 struct ieee80211_vif *vif,
8935 struct ieee80211_chanctx_conf *ctx)
8936{
8937 struct ath10k *ar = hw->priv;
8938 struct ath10k_vif *arvif = (void *)vif->drv_priv;
8939 int ret;
8940
8941 mutex_lock(&ar->conf_mutex);
8942
8943 ath10k_dbg(ar, ATH10K_DBG_MAC,
75b34800 8944 "mac chanctx unassign ptr %pK vdev_id %i\n",
500ff9f9
MK
8945 ctx, arvif->vdev_id);
8946
8947 WARN_ON(!arvif->is_started);
8948
8949 if (vif->type == NL80211_IFTYPE_MONITOR) {
8950 WARN_ON(!arvif->is_up);
8951
8952 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
8953 if (ret)
8954 ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
8955 arvif->vdev_id, ret);
8956
8957 arvif->is_up = false;
8958 }
8959
8960 ret = ath10k_vdev_stop(arvif);
8961 if (ret)
8962 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
8963 arvif->vdev_id, ret);
8964
8965 arvif->is_started = false;
8966
8967 mutex_unlock(&ar->conf_mutex);
8968}
8969
8970static int
8971ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
8972 struct ieee80211_vif_chanctx_switch *vifs,
8973 int n_vifs,
8974 enum ieee80211_chanctx_switch_mode mode)
8975{
8976 struct ath10k *ar = hw->priv;
500ff9f9
MK
8977
8978 mutex_lock(&ar->conf_mutex);
8979
8980 ath10k_dbg(ar, ATH10K_DBG_MAC,
8981 "mac chanctx switch n_vifs %d mode %d\n",
8982 n_vifs, mode);
7be6d1b7 8983 ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
500ff9f9
MK
8984
8985 mutex_unlock(&ar->conf_mutex);
8986 return 0;
8987}
8988
0a744d92
MK
8989static void ath10k_mac_op_sta_pre_rcu_remove(struct ieee80211_hw *hw,
8990 struct ieee80211_vif *vif,
8991 struct ieee80211_sta *sta)
8992{
8993 struct ath10k *ar;
8994 struct ath10k_peer *peer;
8995
8996 ar = hw->priv;
8997
8998 list_for_each_entry(peer, &ar->peers, list)
8999 if (peer->sta == sta)
9000 peer->removed = true;
9001}
9002
3344b99d
WG
9003/* HT MCS parameters with Nss = 1 */
9004static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss1[] = {
9005 /* MCS L20 L40 S20 S40 */
9006 {0, { 65, 135, 72, 150} },
9007 {1, { 130, 270, 144, 300} },
9008 {2, { 195, 405, 217, 450} },
9009 {3, { 260, 540, 289, 600} },
9010 {4, { 390, 810, 433, 900} },
9011 {5, { 520, 1080, 578, 1200} },
9012 {6, { 585, 1215, 650, 1350} },
9013 {7, { 650, 1350, 722, 1500} }
9014};
9015
9016/* HT MCS parameters with Nss = 2 */
9017static const struct ath10k_index_ht_data_rate_type supported_ht_mcs_rate_nss2[] = {
9018 /* MCS L20 L40 S20 S40 */
9019 {0, {130, 270, 144, 300} },
9020 {1, {260, 540, 289, 600} },
9021 {2, {390, 810, 433, 900} },
9022 {3, {520, 1080, 578, 1200} },
9023 {4, {780, 1620, 867, 1800} },
9024 {5, {1040, 2160, 1156, 2400} },
9025 {6, {1170, 2430, 1300, 2700} },
9026 {7, {1300, 2700, 1444, 3000} }
9027};
9028
9029/* MCS parameters with Nss = 1 */
9030static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss1[] = {
9031 /* MCS L80 S80 L40 S40 L20 S20 */
9032 {0, {293, 325}, {135, 150}, {65, 72} },
9033 {1, {585, 650}, {270, 300}, {130, 144} },
9034 {2, {878, 975}, {405, 450}, {195, 217} },
9035 {3, {1170, 1300}, {540, 600}, {260, 289} },
9036 {4, {1755, 1950}, {810, 900}, {390, 433} },
9037 {5, {2340, 2600}, {1080, 1200}, {520, 578} },
9038 {6, {2633, 2925}, {1215, 1350}, {585, 650} },
9039 {7, {2925, 3250}, {1350, 1500}, {650, 722} },
9040 {8, {3510, 3900}, {1620, 1800}, {780, 867} },
9041 {9, {3900, 4333}, {1800, 2000}, {780, 867} }
9042};
9043
9044/*MCS parameters with Nss = 2 */
9045static const struct ath10k_index_vht_data_rate_type supported_vht_mcs_rate_nss2[] = {
9046 /* MCS L80 S80 L40 S40 L20 S20 */
9047 {0, {585, 650}, {270, 300}, {130, 144} },
9048 {1, {1170, 1300}, {540, 600}, {260, 289} },
9049 {2, {1755, 1950}, {810, 900}, {390, 433} },
9050 {3, {2340, 2600}, {1080, 1200}, {520, 578} },
9051 {4, {3510, 3900}, {1620, 1800}, {780, 867} },
9052 {5, {4680, 5200}, {2160, 2400}, {1040, 1156} },
9053 {6, {5265, 5850}, {2430, 2700}, {1170, 1300} },
9054 {7, {5850, 6500}, {2700, 3000}, {1300, 1444} },
9055 {8, {7020, 7800}, {3240, 3600}, {1560, 1733} },
9056 {9, {7800, 8667}, {3600, 4000}, {1560, 1733} }
9057};
9058
9059static void ath10k_mac_get_rate_flags_ht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9060 u8 *flags, u8 *bw)
9061{
9062 struct ath10k_index_ht_data_rate_type *mcs_rate;
db04b755
WG
9063 u8 index;
9064 size_t len_nss1 = ARRAY_SIZE(supported_ht_mcs_rate_nss1);
9065 size_t len_nss2 = ARRAY_SIZE(supported_ht_mcs_rate_nss2);
9066
9067 if (mcs >= (len_nss1 + len_nss2)) {
9068 ath10k_warn(ar, "not supported mcs %d in current rate table", mcs);
9069 return;
9070 }
3344b99d
WG
9071
9072 mcs_rate = (struct ath10k_index_ht_data_rate_type *)
9073 ((nss == 1) ? &supported_ht_mcs_rate_nss1 :
9074 &supported_ht_mcs_rate_nss2);
9075
db04b755
WG
9076 if (mcs >= len_nss1)
9077 index = mcs - len_nss1;
9078 else
9079 index = mcs;
9080
9081 if (rate == mcs_rate[index].supported_rate[0]) {
3344b99d 9082 *bw = RATE_INFO_BW_20;
db04b755 9083 } else if (rate == mcs_rate[index].supported_rate[1]) {
3344b99d 9084 *bw |= RATE_INFO_BW_40;
db04b755 9085 } else if (rate == mcs_rate[index].supported_rate[2]) {
3344b99d
WG
9086 *bw |= RATE_INFO_BW_20;
9087 *flags |= RATE_INFO_FLAGS_SHORT_GI;
db04b755 9088 } else if (rate == mcs_rate[index].supported_rate[3]) {
3344b99d
WG
9089 *bw |= RATE_INFO_BW_40;
9090 *flags |= RATE_INFO_FLAGS_SHORT_GI;
9091 } else {
9092 ath10k_warn(ar, "invalid ht params rate %d 100kbps nss %d mcs %d",
9093 rate, nss, mcs);
9094 }
9095}
9096
9097static void ath10k_mac_get_rate_flags_vht(struct ath10k *ar, u32 rate, u8 nss, u8 mcs,
9098 u8 *flags, u8 *bw)
9099{
9100 struct ath10k_index_vht_data_rate_type *mcs_rate;
9101
9102 mcs_rate = (struct ath10k_index_vht_data_rate_type *)
9103 ((nss == 1) ? &supported_vht_mcs_rate_nss1 :
9104 &supported_vht_mcs_rate_nss2);
9105
9106 if (rate == mcs_rate[mcs].supported_VHT80_rate[0]) {
9107 *bw = RATE_INFO_BW_80;
9108 } else if (rate == mcs_rate[mcs].supported_VHT80_rate[1]) {
9109 *bw = RATE_INFO_BW_80;
9110 *flags |= RATE_INFO_FLAGS_SHORT_GI;
9111 } else if (rate == mcs_rate[mcs].supported_VHT40_rate[0]) {
9112 *bw = RATE_INFO_BW_40;
9113 } else if (rate == mcs_rate[mcs].supported_VHT40_rate[1]) {
9114 *bw = RATE_INFO_BW_40;
9115 *flags |= RATE_INFO_FLAGS_SHORT_GI;
9116 } else if (rate == mcs_rate[mcs].supported_VHT20_rate[0]) {
9117 *bw = RATE_INFO_BW_20;
9118 } else if (rate == mcs_rate[mcs].supported_VHT20_rate[1]) {
9119 *bw = RATE_INFO_BW_20;
9120 *flags |= RATE_INFO_FLAGS_SHORT_GI;
9121 } else {
9122 ath10k_warn(ar, "invalid vht params rate %d 100kbps nss %d mcs %d",
9123 rate, nss, mcs);
9124 }
9125}
9126
9127static void ath10k_mac_get_rate_flags(struct ath10k *ar, u32 rate,
9128 enum ath10k_phy_mode mode, u8 nss, u8 mcs,
9129 u8 *flags, u8 *bw)
9130{
9131 if (mode == ATH10K_PHY_MODE_HT) {
9132 *flags = RATE_INFO_FLAGS_MCS;
9133 ath10k_mac_get_rate_flags_ht(ar, rate, nss, mcs, flags, bw);
9134 } else if (mode == ATH10K_PHY_MODE_VHT) {
9135 *flags = RATE_INFO_FLAGS_VHT_MCS;
9136 ath10k_mac_get_rate_flags_vht(ar, rate, nss, mcs, flags, bw);
9137 }
9138}
9139
9140static void ath10k_mac_parse_bitrate(struct ath10k *ar, u32 rate_code,
9141 u32 bitrate_kbps, struct rate_info *rate)
9142{
9143 enum ath10k_phy_mode mode = ATH10K_PHY_MODE_LEGACY;
9144 enum wmi_rate_preamble preamble = WMI_TLV_GET_HW_RC_PREAM_V1(rate_code);
9145 u8 nss = WMI_TLV_GET_HW_RC_NSS_V1(rate_code) + 1;
9146 u8 mcs = WMI_TLV_GET_HW_RC_RATE_V1(rate_code);
9147 u8 flags = 0, bw = 0;
9148
db04b755
WG
9149 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac parse rate code 0x%x bitrate %d kbps\n",
9150 rate_code, bitrate_kbps);
9151
3344b99d
WG
9152 if (preamble == WMI_RATE_PREAMBLE_HT)
9153 mode = ATH10K_PHY_MODE_HT;
9154 else if (preamble == WMI_RATE_PREAMBLE_VHT)
9155 mode = ATH10K_PHY_MODE_VHT;
9156
9157 ath10k_mac_get_rate_flags(ar, bitrate_kbps / 100, mode, nss, mcs, &flags, &bw);
9158
9159 ath10k_dbg(ar, ATH10K_DBG_MAC,
9160 "mac parse bitrate preamble %d mode %d nss %d mcs %d flags %x bw %d\n",
9161 preamble, mode, nss, mcs, flags, bw);
9162
9163 rate->flags = flags;
9164 rate->bw = bw;
9165 rate->legacy = bitrate_kbps / 100;
9166 rate->nss = nss;
9167 rate->mcs = mcs;
9168}
9169
0f7cb268
WG
9170static void ath10k_mac_sta_get_peer_stats_info(struct ath10k *ar,
9171 struct ieee80211_sta *sta,
9172 struct station_info *sinfo)
9173{
9174 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9175 struct ath10k_peer *peer;
9176 unsigned long time_left;
9177 int ret;
9178
9179 if (!(ar->hw_params.supports_peer_stats_info &&
9180 arsta->arvif->vdev_type == WMI_VDEV_TYPE_STA))
9181 return;
9182
9183 spin_lock_bh(&ar->data_lock);
9184 peer = ath10k_peer_find(ar, arsta->arvif->vdev_id, sta->addr);
9185 spin_unlock_bh(&ar->data_lock);
9186 if (!peer)
9187 return;
9188
9189 reinit_completion(&ar->peer_stats_info_complete);
9190
9191 ret = ath10k_wmi_request_peer_stats_info(ar,
9192 arsta->arvif->vdev_id,
9193 WMI_REQUEST_ONE_PEER_STATS_INFO,
9194 arsta->arvif->bssid,
9195 0);
9196 if (ret && ret != -EOPNOTSUPP) {
9197 ath10k_warn(ar, "could not request peer stats info: %d\n", ret);
9198 return;
9199 }
9200
9201 time_left = wait_for_completion_timeout(&ar->peer_stats_info_complete, 3 * HZ);
9202 if (time_left == 0) {
9203 ath10k_warn(ar, "timed out waiting peer stats info\n");
9204 return;
9205 }
3344b99d
WG
9206
9207 if (arsta->rx_rate_code != 0 && arsta->rx_bitrate_kbps != 0) {
9208 ath10k_mac_parse_bitrate(ar, arsta->rx_rate_code,
9209 arsta->rx_bitrate_kbps,
9210 &sinfo->rxrate);
9211
9212 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
9213 arsta->rx_rate_code = 0;
9214 arsta->rx_bitrate_kbps = 0;
9215 }
4cc02c7c
WG
9216
9217 if (arsta->tx_rate_code != 0 && arsta->tx_bitrate_kbps != 0) {
9218 ath10k_mac_parse_bitrate(ar, arsta->tx_rate_code,
9219 arsta->tx_bitrate_kbps,
9220 &sinfo->txrate);
9221
9222 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
9223 arsta->tx_rate_code = 0;
9224 arsta->tx_bitrate_kbps = 0;
9225 }
0f7cb268
WG
9226}
9227
6a7f8911
AK
9228static void ath10k_sta_statistics(struct ieee80211_hw *hw,
9229 struct ieee80211_vif *vif,
9230 struct ieee80211_sta *sta,
9231 struct station_info *sinfo)
9232{
9233 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
9234 struct ath10k *ar = arsta->arvif->ar;
9235
9236 if (!ath10k_peer_stats_enabled(ar))
9237 return;
9238
7df28718 9239 mutex_lock(&ar->conf_mutex);
4913e675 9240 ath10k_debug_fw_stats_request(ar);
7df28718 9241 mutex_unlock(&ar->conf_mutex);
4913e675 9242
6a7f8911 9243 sinfo->rx_duration = arsta->rx_duration;
22d0d2fa 9244 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_RX_DURATION);
6a7f8911 9245
1cd6ba8a
WG
9246 if (arsta->txrate.legacy || arsta->txrate.nss) {
9247 if (arsta->txrate.legacy) {
9248 sinfo->txrate.legacy = arsta->txrate.legacy;
9249 } else {
9250 sinfo->txrate.mcs = arsta->txrate.mcs;
9251 sinfo->txrate.nss = arsta->txrate.nss;
9252 sinfo->txrate.bw = arsta->txrate.bw;
9253 }
9254 sinfo->txrate.flags = arsta->txrate.flags;
9255 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6a7f8911 9256 }
59a022cc
WG
9257
9258 if (ar->htt.disable_tx_comp) {
59a022cc
WG
9259 sinfo->tx_failed = arsta->tx_failed;
9260 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_FAILED);
9261 }
0f7cb268 9262
67b927f9
VN
9263 sinfo->tx_retries = arsta->tx_retries;
9264 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_RETRIES);
9265
0f7cb268 9266 ath10k_mac_sta_get_peer_stats_info(ar, sta, sinfo);
6a7f8911
AK
9267}
9268
7b2531d9
TC
9269static int ath10k_mac_op_set_tid_config(struct ieee80211_hw *hw,
9270 struct ieee80211_vif *vif,
9271 struct ieee80211_sta *sta,
9272 struct cfg80211_tid_config *tid_config)
9273{
9274 struct ath10k *ar = hw->priv;
9275 struct ath10k_vif *arvif = (void *)vif->drv_priv;
9276 struct ath10k_mac_iter_tid_conf_data data = {};
9277 struct wmi_per_peer_per_tid_cfg_arg arg = {};
9278 int ret, i;
9279
9280 mutex_lock(&ar->conf_mutex);
9281 arg.vdev_id = arvif->vdev_id;
9282
2ca6a1dd 9283 arvif->tids_rst = 0;
7b2531d9
TC
9284 memset(arvif->tid_conf_changed, 0, sizeof(arvif->tid_conf_changed));
9285
9286 for (i = 0; i < tid_config->n_tid_conf; i++) {
9287 ret = ath10k_mac_parse_tid_config(ar, sta, vif,
9288 &tid_config->tid_conf[i],
9289 &arg);
9290 if (ret)
9291 goto exit;
9292 }
9293
b9162645
TC
9294 ret = 0;
9295
7b2531d9
TC
9296 if (sta)
9297 goto exit;
9298
2ca6a1dd 9299 arvif->tids_rst = 0;
7b2531d9
TC
9300 data.curr_vif = vif;
9301 data.ar = ar;
9302
9303 ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9304 &data);
9305
9306exit:
9307 mutex_unlock(&ar->conf_mutex);
9308 return ret;
9309}
9310
2ca6a1dd
TC
9311static int ath10k_mac_op_reset_tid_config(struct ieee80211_hw *hw,
9312 struct ieee80211_vif *vif,
9313 struct ieee80211_sta *sta,
9314 u8 tids)
9315{
9316 struct ath10k_vif *arvif = (void *)vif->drv_priv;
9317 struct ath10k_mac_iter_tid_conf_data data = {};
9318 struct ath10k *ar = hw->priv;
9319 int ret = 0;
9320
9321 mutex_lock(&ar->conf_mutex);
9322
9323 if (sta) {
9324 arvif->tids_rst = 0;
9325 ret = ath10k_mac_reset_tid_config(ar, sta, arvif, tids);
9326 goto exit;
9327 }
9328
9329 arvif->tids_rst = tids;
9330 data.curr_vif = vif;
9331 data.ar = ar;
9332 ieee80211_iterate_stations_atomic(hw, ath10k_mac_vif_stations_tid_conf,
9333 &data);
9334
9335exit:
9336 mutex_unlock(&ar->conf_mutex);
9337 return ret;
9338}
9339
5e3dd157 9340static const struct ieee80211_ops ath10k_ops = {
f2f6ecab 9341 .tx = ath10k_mac_op_tx,
29946878 9342 .wake_tx_queue = ath10k_mac_op_wake_tx_queue,
5e3dd157
KV
9343 .start = ath10k_start,
9344 .stop = ath10k_stop,
9345 .config = ath10k_config,
9346 .add_interface = ath10k_add_interface,
9347 .remove_interface = ath10k_remove_interface,
9348 .configure_filter = ath10k_configure_filter,
9349 .bss_info_changed = ath10k_bss_info_changed,
ebee76f7 9350 .set_coverage_class = ath10k_mac_op_set_coverage_class,
5e3dd157
KV
9351 .hw_scan = ath10k_hw_scan,
9352 .cancel_hw_scan = ath10k_cancel_hw_scan,
9353 .set_key = ath10k_set_key,
627613f8 9354 .set_default_unicast_key = ath10k_set_default_unicast_key,
5e3dd157 9355 .sta_state = ath10k_sta_state,
33410a51 9356 .sta_set_txpwr = ath10k_sta_set_txpwr,
5e3dd157
KV
9357 .conf_tx = ath10k_conf_tx,
9358 .remain_on_channel = ath10k_remain_on_channel,
9359 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
9360 .set_rts_threshold = ath10k_set_rts_threshold,
92092fe5 9361 .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
5e3dd157
KV
9362 .flush = ath10k_flush,
9363 .tx_last_beacon = ath10k_tx_last_beacon,
46acf7bb
BG
9364 .set_antenna = ath10k_set_antenna,
9365 .get_antenna = ath10k_get_antenna,
cf2c92d8 9366 .reconfig_complete = ath10k_reconfig_complete,
2e1dea40 9367 .get_survey = ath10k_get_survey,
3ae54225 9368 .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
9797febc 9369 .sta_rc_update = ath10k_sta_rc_update,
973324ff 9370 .offset_tsf = ath10k_offset_tsf,
aa5b4fbc 9371 .ampdu_action = ath10k_ampdu_action,
6cddcc7a
BG
9372 .get_et_sset_count = ath10k_debug_get_et_sset_count,
9373 .get_et_stats = ath10k_debug_get_et_stats,
9374 .get_et_strings = ath10k_debug_get_et_strings,
500ff9f9
MK
9375 .add_chanctx = ath10k_mac_op_add_chanctx,
9376 .remove_chanctx = ath10k_mac_op_remove_chanctx,
9377 .change_chanctx = ath10k_mac_op_change_chanctx,
9378 .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
9379 .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
9380 .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
0a744d92 9381 .sta_pre_rcu_remove = ath10k_mac_op_sta_pre_rcu_remove,
6a7f8911 9382 .sta_statistics = ath10k_sta_statistics,
7b2531d9 9383 .set_tid_config = ath10k_mac_op_set_tid_config,
2ca6a1dd 9384 .reset_tid_config = ath10k_mac_op_reset_tid_config,
43d2a30f
KV
9385
9386 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
9387
8cd13cad 9388#ifdef CONFIG_PM
5fd3ac3c
JD
9389 .suspend = ath10k_wow_op_suspend,
9390 .resume = ath10k_wow_op_resume,
393b706c 9391 .set_wakeup = ath10k_wow_op_set_wakeup,
8cd13cad 9392#endif
f5045988
RM
9393#ifdef CONFIG_MAC80211_DEBUGFS
9394 .sta_add_debugfs = ath10k_sta_add_debugfs,
9395#endif
442545ba 9396 .set_sar_specs = ath10k_mac_set_sar_specs,
5e3dd157
KV
9397};
9398
5e3dd157 9399#define CHAN2G(_channel, _freq, _flags) { \
57fbcce3 9400 .band = NL80211_BAND_2GHZ, \
5e3dd157
KV
9401 .hw_value = (_channel), \
9402 .center_freq = (_freq), \
9403 .flags = (_flags), \
9404 .max_antenna_gain = 0, \
9405 .max_power = 30, \
9406}
9407
9408#define CHAN5G(_channel, _freq, _flags) { \
57fbcce3 9409 .band = NL80211_BAND_5GHZ, \
5e3dd157
KV
9410 .hw_value = (_channel), \
9411 .center_freq = (_freq), \
9412 .flags = (_flags), \
9413 .max_antenna_gain = 0, \
9414 .max_power = 30, \
9415}
9416
9417static const struct ieee80211_channel ath10k_2ghz_channels[] = {
9418 CHAN2G(1, 2412, 0),
9419 CHAN2G(2, 2417, 0),
9420 CHAN2G(3, 2422, 0),
9421 CHAN2G(4, 2427, 0),
9422 CHAN2G(5, 2432, 0),
9423 CHAN2G(6, 2437, 0),
9424 CHAN2G(7, 2442, 0),
9425 CHAN2G(8, 2447, 0),
9426 CHAN2G(9, 2452, 0),
9427 CHAN2G(10, 2457, 0),
9428 CHAN2G(11, 2462, 0),
9429 CHAN2G(12, 2467, 0),
9430 CHAN2G(13, 2472, 0),
9431 CHAN2G(14, 2484, 0),
9432};
9433
9434static const struct ieee80211_channel ath10k_5ghz_channels[] = {
429ff56a
MK
9435 CHAN5G(36, 5180, 0),
9436 CHAN5G(40, 5200, 0),
9437 CHAN5G(44, 5220, 0),
9438 CHAN5G(48, 5240, 0),
9439 CHAN5G(52, 5260, 0),
9440 CHAN5G(56, 5280, 0),
9441 CHAN5G(60, 5300, 0),
9442 CHAN5G(64, 5320, 0),
9443 CHAN5G(100, 5500, 0),
9444 CHAN5G(104, 5520, 0),
9445 CHAN5G(108, 5540, 0),
9446 CHAN5G(112, 5560, 0),
9447 CHAN5G(116, 5580, 0),
9448 CHAN5G(120, 5600, 0),
9449 CHAN5G(124, 5620, 0),
9450 CHAN5G(128, 5640, 0),
9451 CHAN5G(132, 5660, 0),
9452 CHAN5G(136, 5680, 0),
9453 CHAN5G(140, 5700, 0),
4a7898fe 9454 CHAN5G(144, 5720, 0),
429ff56a
MK
9455 CHAN5G(149, 5745, 0),
9456 CHAN5G(153, 5765, 0),
9457 CHAN5G(157, 5785, 0),
9458 CHAN5G(161, 5805, 0),
9459 CHAN5G(165, 5825, 0),
34c30b0a 9460 CHAN5G(169, 5845, 0),
38441fb6
BG
9461 CHAN5G(173, 5865, 0),
9462 /* If you add more, you may need to change ATH10K_MAX_5G_CHAN */
9463 /* And you will definitely need to change ATH10K_NUM_CHANS in core.h */
5e3dd157
KV
9464};
9465
e7b54194 9466struct ath10k *ath10k_mac_create(size_t priv_size)
5e3dd157
KV
9467{
9468 struct ieee80211_hw *hw;
4ca18078 9469 struct ieee80211_ops *ops;
5e3dd157
KV
9470 struct ath10k *ar;
9471
4ca18078
MK
9472 ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
9473 if (!ops)
5e3dd157
KV
9474 return NULL;
9475
4ca18078
MK
9476 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
9477 if (!hw) {
9478 kfree(ops);
9479 return NULL;
9480 }
9481
5e3dd157
KV
9482 ar = hw->priv;
9483 ar->hw = hw;
4ca18078 9484 ar->ops = ops;
5e3dd157
KV
9485
9486 return ar;
9487}
9488
9489void ath10k_mac_destroy(struct ath10k *ar)
9490{
4ca18078
MK
9491 struct ieee80211_ops *ops = ar->ops;
9492
5e3dd157 9493 ieee80211_free_hw(ar->hw);
4ca18078 9494 kfree(ops);
5e3dd157
KV
9495}
9496
9497static const struct ieee80211_iface_limit ath10k_if_limits[] = {
9498 {
78f7aeb0
MSS
9499 .max = 8,
9500 .types = BIT(NL80211_IFTYPE_STATION)
9501 | BIT(NL80211_IFTYPE_P2P_CLIENT)
d531cb85
MK
9502 },
9503 {
78f7aeb0
MSS
9504 .max = 3,
9505 .types = BIT(NL80211_IFTYPE_P2P_GO)
d531cb85
MK
9506 },
9507 {
78f7aeb0
MSS
9508 .max = 1,
9509 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
75d2bd48
MK
9510 },
9511 {
78f7aeb0
MSS
9512 .max = 7,
9513 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa 9514#ifdef CONFIG_MAC80211_MESH
78f7aeb0 9515 | BIT(NL80211_IFTYPE_MESH_POINT)
b6c7bafa 9516#endif
d531cb85 9517 },
5e3dd157
KV
9518};
9519
f259509b 9520static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
e8a50f8b 9521 {
78f7aeb0
MSS
9522 .max = 8,
9523 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa 9524#ifdef CONFIG_MAC80211_MESH
78f7aeb0 9525 | BIT(NL80211_IFTYPE_MESH_POINT)
b6c7bafa 9526#endif
e8a50f8b 9527 },
78f7aeb0
MSS
9528 {
9529 .max = 1,
9530 .types = BIT(NL80211_IFTYPE_STATION)
9531 },
e8a50f8b 9532};
e8a50f8b
MP
9533
9534static const struct ieee80211_iface_combination ath10k_if_comb[] = {
9535 {
9536 .limits = ath10k_if_limits,
9537 .n_limits = ARRAY_SIZE(ath10k_if_limits),
9538 .max_interfaces = 8,
9539 .num_different_channels = 1,
9540 .beacon_int_infra_match = true,
9541 },
f259509b
BM
9542};
9543
9544static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
e8a50f8b 9545 {
f259509b
BM
9546 .limits = ath10k_10x_if_limits,
9547 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
e8a50f8b
MP
9548 .max_interfaces = 8,
9549 .num_different_channels = 1,
9550 .beacon_int_infra_match = true,
8ebee73b 9551 .beacon_int_min_gcd = 1,
f259509b 9552#ifdef CONFIG_ATH10K_DFS_CERTIFIED
e8a50f8b
MP
9553 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9554 BIT(NL80211_CHAN_WIDTH_20) |
9555 BIT(NL80211_CHAN_WIDTH_40) |
9556 BIT(NL80211_CHAN_WIDTH_80),
e8a50f8b 9557#endif
f259509b 9558 },
5e3dd157
KV
9559};
9560
cf32784c
MK
9561static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
9562 {
9563 .max = 2,
ed25b113
MK
9564 .types = BIT(NL80211_IFTYPE_STATION),
9565 },
9566 {
9567 .max = 2,
9568 .types = BIT(NL80211_IFTYPE_AP) |
b6c7bafa
BC
9569#ifdef CONFIG_MAC80211_MESH
9570 BIT(NL80211_IFTYPE_MESH_POINT) |
9571#endif
cf32784c
MK
9572 BIT(NL80211_IFTYPE_P2P_CLIENT) |
9573 BIT(NL80211_IFTYPE_P2P_GO),
9574 },
9575 {
9576 .max = 1,
9577 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9578 },
9579};
9580
ed25b113
MK
9581static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
9582 {
9583 .max = 2,
9584 .types = BIT(NL80211_IFTYPE_STATION),
9585 },
9586 {
9587 .max = 2,
9588 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
9589 },
9590 {
9591 .max = 1,
9592 .types = BIT(NL80211_IFTYPE_AP) |
b6c7bafa
BC
9593#ifdef CONFIG_MAC80211_MESH
9594 BIT(NL80211_IFTYPE_MESH_POINT) |
9595#endif
ed25b113
MK
9596 BIT(NL80211_IFTYPE_P2P_GO),
9597 },
9598 {
9599 .max = 1,
9600 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
9601 },
9602};
9603
cf32784c
MK
9604static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
9605 {
9606 .max = 1,
9607 .types = BIT(NL80211_IFTYPE_STATION),
9608 },
9609 {
9610 .max = 1,
9611 .types = BIT(NL80211_IFTYPE_ADHOC),
9612 },
9613};
9614
9615/* FIXME: This is not thouroughly tested. These combinations may over- or
9616 * underestimate hw/fw capabilities.
9617 */
9618static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
9619 {
9620 .limits = ath10k_tlv_if_limit,
9621 .num_different_channels = 1,
ed25b113 9622 .max_interfaces = 4,
cf32784c
MK
9623 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9624 },
9625 {
9626 .limits = ath10k_tlv_if_limit_ibss,
9627 .num_different_channels = 1,
9628 .max_interfaces = 2,
9629 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9630 },
9631};
9632
9633static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
9634 {
9635 .limits = ath10k_tlv_if_limit,
ed25b113
MK
9636 .num_different_channels = 1,
9637 .max_interfaces = 4,
cf32784c
MK
9638 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
9639 },
ed25b113
MK
9640 {
9641 .limits = ath10k_tlv_qcs_if_limit,
9642 .num_different_channels = 2,
9643 .max_interfaces = 4,
9644 .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
9645 },
cf32784c
MK
9646 {
9647 .limits = ath10k_tlv_if_limit_ibss,
9648 .num_different_channels = 1,
9649 .max_interfaces = 2,
9650 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
9651 },
9652};
9653
cf36fef0
RM
9654static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
9655 {
9656 .max = 1,
9657 .types = BIT(NL80211_IFTYPE_STATION),
9658 },
9659 {
9660 .max = 16,
9661 .types = BIT(NL80211_IFTYPE_AP)
b6c7bafa
BC
9662#ifdef CONFIG_MAC80211_MESH
9663 | BIT(NL80211_IFTYPE_MESH_POINT)
9664#endif
cf36fef0
RM
9665 },
9666};
9667
9668static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
9669 {
9670 .limits = ath10k_10_4_if_limits,
9671 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9672 .max_interfaces = 16,
9673 .num_different_channels = 1,
9674 .beacon_int_infra_match = true,
8ebee73b 9675 .beacon_int_min_gcd = 1,
cf36fef0
RM
9676#ifdef CONFIG_ATH10K_DFS_CERTIFIED
9677 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9678 BIT(NL80211_CHAN_WIDTH_20) |
9679 BIT(NL80211_CHAN_WIDTH_40) |
3db24065
LW
9680 BIT(NL80211_CHAN_WIDTH_80) |
9681 BIT(NL80211_CHAN_WIDTH_80P80) |
9682 BIT(NL80211_CHAN_WIDTH_160),
cf36fef0
RM
9683#endif
9684 },
9685};
9686
4600563f
MK
9687static const struct
9688ieee80211_iface_combination ath10k_10_4_bcn_int_if_comb[] = {
9689 {
9690 .limits = ath10k_10_4_if_limits,
9691 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
9692 .max_interfaces = 16,
9693 .num_different_channels = 1,
9694 .beacon_int_infra_match = true,
9695 .beacon_int_min_gcd = 100,
9696#ifdef CONFIG_ATH10K_DFS_CERTIFIED
9697 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
9698 BIT(NL80211_CHAN_WIDTH_20) |
9699 BIT(NL80211_CHAN_WIDTH_40) |
3db24065
LW
9700 BIT(NL80211_CHAN_WIDTH_80) |
9701 BIT(NL80211_CHAN_WIDTH_80P80) |
9702 BIT(NL80211_CHAN_WIDTH_160),
4600563f
MK
9703#endif
9704 },
9705};
9706
5e3dd157
KV
9707static void ath10k_get_arvif_iter(void *data, u8 *mac,
9708 struct ieee80211_vif *vif)
9709{
9710 struct ath10k_vif_iter *arvif_iter = data;
56ac13bf 9711 struct ath10k_vif *arvif = (void *)vif->drv_priv;
5e3dd157
KV
9712
9713 if (arvif->vdev_id == arvif_iter->vdev_id)
9714 arvif_iter->arvif = arvif;
9715}
9716
9717struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
9718{
9719 struct ath10k_vif_iter arvif_iter;
5e3dd157
KV
9720
9721 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
9722 arvif_iter.vdev_id = vdev_id;
9723
5e3dd157 9724 ieee80211_iterate_active_interfaces_atomic(ar->hw,
ee06fcb9 9725 ATH10K_ITER_RESUME_FLAGS,
5e3dd157
KV
9726 ath10k_get_arvif_iter,
9727 &arvif_iter);
9728 if (!arvif_iter.arvif) {
7aa7a72a 9729 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5e3dd157
KV
9730 return NULL;
9731 }
9732
9733 return arvif_iter.arvif;
9734}
9735
209b2a68
BM
9736#define WRD_METHOD "WRDD"
9737#define WRDD_WIFI (0x07)
9738
9739static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
9740{
9741 union acpi_object *mcc_pkg;
9742 union acpi_object *domain_type;
9743 union acpi_object *mcc_value;
9744 u32 i;
9745
9746 if (wrdd->type != ACPI_TYPE_PACKAGE ||
9747 wrdd->package.count < 2 ||
9748 wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
9749 wrdd->package.elements[0].integer.value != 0) {
9750 ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
9751 return 0;
9752 }
9753
9754 for (i = 1; i < wrdd->package.count; ++i) {
9755 mcc_pkg = &wrdd->package.elements[i];
9756
9757 if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
9758 continue;
9759 if (mcc_pkg->package.count < 2)
9760 continue;
9761 if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
9762 mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
9763 continue;
9764
9765 domain_type = &mcc_pkg->package.elements[0];
9766 if (domain_type->integer.value != WRDD_WIFI)
9767 continue;
9768
9769 mcc_value = &mcc_pkg->package.elements[1];
9770 return mcc_value->integer.value;
9771 }
9772 return 0;
9773}
9774
9775static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
9776{
209b2a68
BM
9777 acpi_handle root_handle;
9778 acpi_handle handle;
9779 struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
9780 acpi_status status;
9781 u32 alpha2_code;
9782 char alpha2[3];
9783
79169f14 9784 root_handle = ACPI_HANDLE(ar->dev);
209b2a68
BM
9785 if (!root_handle)
9786 return -EOPNOTSUPP;
9787
9788 status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
9789 if (ACPI_FAILURE(status)) {
9790 ath10k_dbg(ar, ATH10K_DBG_BOOT,
9791 "failed to get wrd method %d\n", status);
9792 return -EIO;
9793 }
9794
9795 status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
9796 if (ACPI_FAILURE(status)) {
9797 ath10k_dbg(ar, ATH10K_DBG_BOOT,
9798 "failed to call wrdc %d\n", status);
9799 return -EIO;
9800 }
9801
9802 alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
9803 kfree(wrdd.pointer);
9804 if (!alpha2_code)
9805 return -EIO;
9806
9807 alpha2[0] = (alpha2_code >> 8) & 0xff;
9808 alpha2[1] = (alpha2_code >> 0) & 0xff;
9809 alpha2[2] = '\0';
9810
9811 ath10k_dbg(ar, ATH10K_DBG_BOOT,
9812 "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
9813
9814 *rd = ath_regd_find_country_by_name(alpha2);
9815 if (*rd == 0xffff)
9816 return -EIO;
9817
9818 *rd |= COUNTRY_ERD_FLAG;
9819 return 0;
9820}
9821
9822static int ath10k_mac_init_rd(struct ath10k *ar)
9823{
9824 int ret;
9825 u16 rd;
9826
9827 ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
9828 if (ret) {
9829 ath10k_dbg(ar, ATH10K_DBG_BOOT,
9830 "fallback to eeprom programmed regulatory settings\n");
9831 rd = ar->hw_eeprom_rd;
9832 }
9833
9834 ar->ath_common.regulatory.current_rd = rd;
9835 return 0;
9836}
9837
5e3dd157
KV
9838int ath10k_mac_register(struct ath10k *ar)
9839{
3cb10943
JB
9840 static const u32 cipher_suites[] = {
9841 WLAN_CIPHER_SUITE_WEP40,
9842 WLAN_CIPHER_SUITE_WEP104,
9843 WLAN_CIPHER_SUITE_TKIP,
9844 WLAN_CIPHER_SUITE_CCMP,
2ea9f12c
RM
9845
9846 /* Do not add hardware supported ciphers before this line.
9847 * Allow software encryption for all chips. Don't forget to
9848 * update n_cipher_suites below.
9849 */
3cb10943 9850 WLAN_CIPHER_SUITE_AES_CMAC,
2ea9f12c
RM
9851 WLAN_CIPHER_SUITE_BIP_CMAC_256,
9852 WLAN_CIPHER_SUITE_BIP_GMAC_128,
9853 WLAN_CIPHER_SUITE_BIP_GMAC_256,
9854
9855 /* Only QCA99x0 and QCA4019 varients support GCMP-128, GCMP-256
9856 * and CCMP-256 in hardware.
9857 */
9858 WLAN_CIPHER_SUITE_GCMP,
9859 WLAN_CIPHER_SUITE_GCMP_256,
9860 WLAN_CIPHER_SUITE_CCMP_256,
3cb10943 9861 };
5e3dd157 9862 struct ieee80211_supported_band *band;
5e3dd157
KV
9863 void *channels;
9864 int ret;
9865
234e4307
BN
9866 if (!is_valid_ether_addr(ar->mac_addr)) {
9867 ath10k_warn(ar, "invalid MAC address; choosing random\n");
9868 eth_random_addr(ar->mac_addr);
9869 }
5e3dd157
KV
9870 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
9871
9872 SET_IEEE80211_DEV(ar->hw, ar->dev);
9873
c94aa7ef
MK
9874 BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
9875 ARRAY_SIZE(ath10k_5ghz_channels)) !=
9876 ATH10K_NUM_CHANS);
9877
5e3dd157
KV
9878 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
9879 channels = kmemdup(ath10k_2ghz_channels,
9880 sizeof(ath10k_2ghz_channels),
9881 GFP_KERNEL);
d6015b27
MK
9882 if (!channels) {
9883 ret = -ENOMEM;
9884 goto err_free;
9885 }
5e3dd157 9886
57fbcce3 9887 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
5e3dd157
KV
9888 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
9889 band->channels = channels;
5269c659
MSS
9890
9891 if (ar->hw_params.cck_rate_map_rev2) {
9892 band->n_bitrates = ath10k_g_rates_rev2_size;
9893 band->bitrates = ath10k_g_rates_rev2;
9894 } else {
9895 band->n_bitrates = ath10k_g_rates_size;
9896 band->bitrates = ath10k_g_rates;
9897 }
5e3dd157 9898
57fbcce3 9899 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
5e3dd157
KV
9900 }
9901
9902 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
9903 channels = kmemdup(ath10k_5ghz_channels,
9904 sizeof(ath10k_5ghz_channels),
9905 GFP_KERNEL);
9906 if (!channels) {
d6015b27
MK
9907 ret = -ENOMEM;
9908 goto err_free;
5e3dd157
KV
9909 }
9910
57fbcce3 9911 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
5e3dd157
KV
9912 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
9913 band->channels = channels;
9914 band->n_bitrates = ath10k_a_rates_size;
9915 band->bitrates = ath10k_a_rates;
57fbcce3 9916 ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
5e3dd157
KV
9917 }
9918
34d5629d 9919 wiphy_read_of_freq_limits(ar->hw->wiphy);
5036fe0f
RM
9920 ath10k_mac_setup_ht_vht_cap(ar);
9921
5e3dd157
KV
9922 ar->hw->wiphy->interface_modes =
9923 BIT(NL80211_IFTYPE_STATION) |
b6c7bafa
BC
9924 BIT(NL80211_IFTYPE_AP) |
9925 BIT(NL80211_IFTYPE_MESH_POINT);
d354181f 9926
166de3f1
RM
9927 ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
9928 ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
46acf7bb 9929
c4cdf753 9930 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
d354181f 9931 ar->hw->wiphy->interface_modes |=
75d2bd48 9932 BIT(NL80211_IFTYPE_P2P_DEVICE) |
d354181f
BM
9933 BIT(NL80211_IFTYPE_P2P_CLIENT) |
9934 BIT(NL80211_IFTYPE_P2P_GO);
5e3dd157 9935
30686bf7 9936 ieee80211_hw_set(ar->hw, SIGNAL_DBM);
36d9cdb6
VN
9937
9938 if (!test_bit(ATH10K_FW_FEATURE_NO_PS,
9939 ar->running_fw->fw_file.fw_features)) {
9940 ieee80211_hw_set(ar->hw, SUPPORTS_PS);
9941 ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
9942 }
9943
30686bf7
JB
9944 ieee80211_hw_set(ar->hw, MFP_CAPABLE);
9945 ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
9946 ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
9947 ieee80211_hw_set(ar->hw, AP_LINK_PS);
9948 ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
30686bf7
JB
9949 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
9950 ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
9951 ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
9952 ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
9953 ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
9954 ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
f3fe4e93 9955 ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
ff32eeb8 9956 ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
5e3dd157 9957
ccec9038
DL
9958 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
9959 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
9960
0d8614b4 9961 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
0cd9bc14 9962 ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
0d8614b4 9963
5e3dd157 9964 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
0d8614b4 9965 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5e3dd157
KV
9966
9967 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
30686bf7
JB
9968 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
9969 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
5e3dd157
KV
9970 }
9971
9972 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
9973 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
9974
ce834e28 9975 if (test_bit(WMI_SERVICE_NLO, ar->wmi.svc_map)) {
ce834e28
WG
9976 ar->hw->wiphy->max_sched_scan_ssids = WMI_PNO_MAX_SUPP_NETWORKS;
9977 ar->hw->wiphy->max_match_sets = WMI_PNO_MAX_SUPP_NETWORKS;
9978 ar->hw->wiphy->max_sched_scan_ie_len = WMI_PNO_MAX_IE_LENGTH;
9979 ar->hw->wiphy->max_sched_scan_plans = WMI_PNO_MAX_SCHED_SCAN_PLANS;
9980 ar->hw->wiphy->max_sched_scan_plan_interval =
9981 WMI_PNO_MAX_SCHED_SCAN_PLAN_INT;
9982 ar->hw->wiphy->max_sched_scan_plan_iterations =
9983 WMI_PNO_MAX_SCHED_SCAN_PLAN_ITRNS;
23b5156a 9984 ar->hw->wiphy->features |= NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
ce834e28
WG
9985 }
9986
5e3dd157 9987 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
9797febc 9988 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
29946878 9989 ar->hw->txq_data_size = sizeof(struct ath10k_txq);
5e3dd157 9990
5e3dd157
KV
9991 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
9992
fbb8f1b7
MK
9993 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
9994 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
9995
9996 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
9997 * that userspace (e.g. wpa_supplicant/hostapd) can generate
9998 * correct Probe Responses. This is more of a hack advert..
9999 */
10000 ar->hw->wiphy->probe_resp_offload |=
10001 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
10002 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
10003 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
10004 }
10005
add6cd8d
MP
10006 if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map) ||
10007 test_bit(WMI_SERVICE_TDLS_EXPLICIT_MODE_ONLY, ar->wmi.svc_map)) {
75d85fd9 10008 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
14d65775
BP
10009 if (test_bit(WMI_SERVICE_TDLS_WIDER_BANDWIDTH, ar->wmi.svc_map))
10010 ieee80211_hw_set(ar->hw, TDLS_WIDER_BW);
add6cd8d 10011 }
75d85fd9 10012
802ca335
YT
10013 if (test_bit(WMI_SERVICE_TDLS_UAPSD_BUFFER_STA, ar->wmi.svc_map))
10014 ieee80211_hw_set(ar->hw, SUPPORTS_TDLS_BUFFER_STA);
10015
5e3dd157 10016 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
c2df44b3 10017 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5e3dd157
KV
10018 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
10019
10020 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
bf031bc4
VT
10021 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
10022 NL80211_FEATURE_AP_SCAN;
78157a1c 10023
37a0b394
JD
10024 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
10025
5fd3ac3c
JD
10026 ret = ath10k_wow_init(ar);
10027 if (ret) {
10028 ath10k_warn(ar, "failed to init wow: %d\n", ret);
10029 goto err_free;
10030 }
10031
c702534a 10032 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
be8cce96
PKC
10033 wiphy_ext_feature_set(ar->hw->wiphy,
10034 NL80211_EXT_FEATURE_SET_SCAN_DWELL);
911bde0f 10035 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_AQL);
c702534a 10036
6bc17950
BP
10037 if (test_bit(WMI_SERVICE_TX_DATA_ACK_RSSI, ar->wmi.svc_map) ||
10038 test_bit(WMI_SERVICE_HTT_MGMT_TX_COMP_VALID_FLAGS, ar->wmi.svc_map))
c7fd8d23 10039 wiphy_ext_feature_set(ar->hw->wiphy,
6bc17950 10040 NL80211_EXT_FEATURE_ACK_SIGNAL_SUPPORT);
c7fd8d23 10041
bb31b7cb
MP
10042 if (ath10k_peer_stats_enabled(ar) ||
10043 test_bit(WMI_SERVICE_REPORT_AIRTIME, ar->wmi.svc_map))
d1ce37b7
KY
10044 wiphy_ext_feature_set(ar->hw->wiphy,
10045 NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
059104bf
PKC
10046
10047 if (test_bit(WMI_SERVICE_RTT_RESPONDER_ROLE, ar->wmi.svc_map))
10048 wiphy_ext_feature_set(ar->hw->wiphy,
10049 NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
10050
33410a51
ARN
10051 if (test_bit(WMI_SERVICE_TX_PWR_PER_PEER, ar->wmi.svc_map))
10052 wiphy_ext_feature_set(ar->hw->wiphy,
10053 NL80211_EXT_FEATURE_STA_TX_PWR);
7b2531d9
TC
10054
10055 if (test_bit(WMI_SERVICE_PEER_TID_CONFIGS_SUPPORT, ar->wmi.svc_map)) {
10056 ar->hw->wiphy->tid_config_support.vif |=
10057 BIT(NL80211_TID_CONFIG_ATTR_NOACK) |
10058 BIT(NL80211_TID_CONFIG_ATTR_RETRY_SHORT) |
10059 BIT(NL80211_TID_CONFIG_ATTR_RETRY_LONG) |
10060 BIT(NL80211_TID_CONFIG_ATTR_AMPDU_CTRL) |
10061 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE) |
10062 BIT(NL80211_TID_CONFIG_ATTR_TX_RATE_TYPE);
10063
10064 if (test_bit(WMI_SERVICE_EXT_PEER_TID_CONFIGS_SUPPORT,
10065 ar->wmi.svc_map)) {
10066 ar->hw->wiphy->tid_config_support.vif |=
10067 BIT(NL80211_TID_CONFIG_ATTR_RTSCTS_CTRL);
10068 }
10069
10070 ar->hw->wiphy->tid_config_support.peer =
10071 ar->hw->wiphy->tid_config_support.vif;
10072 ar->hw->wiphy->max_data_retry_count = ATH10K_MAX_RETRY_COUNT;
10073 } else {
10074 ar->ops->set_tid_config = NULL;
10075 }
5e3dd157
KV
10076 /*
10077 * on LL hardware queues are managed entirely by the FW
10078 * so we only advertise to mac we can do the queues thing
10079 */
96d828d4
MK
10080 ar->hw->queues = IEEE80211_MAX_QUEUES;
10081
10082 /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
10083 * something that vdev_ids can't reach so that we don't stop the queue
10084 * accidentally.
10085 */
10086 ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
5e3dd157 10087
bf3c13ab 10088 switch (ar->running_fw->fw_file.wmi_op_version) {
5cc7caf4 10089 case ATH10K_FW_WMI_OP_VERSION_MAIN:
f259509b
BM
10090 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
10091 ar->hw->wiphy->n_iface_combinations =
10092 ARRAY_SIZE(ath10k_if_comb);
cf850d1d 10093 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5cc7caf4 10094 break;
cf32784c
MK
10095 case ATH10K_FW_WMI_OP_VERSION_TLV:
10096 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
10097 ar->hw->wiphy->iface_combinations =
10098 ath10k_tlv_qcs_if_comb;
10099 ar->hw->wiphy->n_iface_combinations =
10100 ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
10101 } else {
10102 ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
10103 ar->hw->wiphy->n_iface_combinations =
10104 ARRAY_SIZE(ath10k_tlv_if_comb);
10105 }
10106 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
10107 break;
5cc7caf4
KV
10108 case ATH10K_FW_WMI_OP_VERSION_10_1:
10109 case ATH10K_FW_WMI_OP_VERSION_10_2:
4a16fbec 10110 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5cc7caf4
KV
10111 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
10112 ar->hw->wiphy->n_iface_combinations =
10113 ARRAY_SIZE(ath10k_10x_if_comb);
10114 break;
9bd21322 10115 case ATH10K_FW_WMI_OP_VERSION_10_4:
cf36fef0
RM
10116 ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
10117 ar->hw->wiphy->n_iface_combinations =
10118 ARRAY_SIZE(ath10k_10_4_if_comb);
4600563f
MK
10119 if (test_bit(WMI_SERVICE_VDEV_DIFFERENT_BEACON_INTERVAL_SUPPORT,
10120 ar->wmi.svc_map)) {
10121 ar->hw->wiphy->iface_combinations =
10122 ath10k_10_4_bcn_int_if_comb;
10123 ar->hw->wiphy->n_iface_combinations =
10124 ARRAY_SIZE(ath10k_10_4_bcn_int_if_comb);
10125 }
9bd21322 10126 break;
5cc7caf4
KV
10127 case ATH10K_FW_WMI_OP_VERSION_UNSET:
10128 case ATH10K_FW_WMI_OP_VERSION_MAX:
10129 WARN_ON(1);
10130 ret = -EINVAL;
10131 goto err_free;
f259509b 10132 }
5e3dd157 10133
442545ba
CH
10134 if (ar->hw_params.dynamic_sar_support)
10135 ar->hw->wiphy->sar_capa = &ath10k_sar_capa;
10136
ccec9038
DL
10137 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
10138 ar->hw->netdev_features = NETIF_F_HW_CSUM;
7c199997 10139
97f2645f 10140 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
9702c686
JD
10141 /* Init ath dfs pattern detector */
10142 ar->ath_common.debug_mask = ATH_DBG_DFS;
10143 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
10144 NL80211_DFS_UNSET);
10145
10146 if (!ar->dfs_detector)
7aa7a72a 10147 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
9702c686
JD
10148 }
10149
209b2a68
BM
10150 ret = ath10k_mac_init_rd(ar);
10151 if (ret) {
10152 ath10k_err(ar, "failed to derive regdom: %d\n", ret);
10153 goto err_dfs_detector_exit;
10154 }
10155
ebee76f7
BB
10156 /* Disable set_coverage_class for chipsets that do not support it. */
10157 if (!ar->hw_params.hw_ops->set_coverage_class)
10158 ar->ops->set_coverage_class = NULL;
10159
5e3dd157
KV
10160 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
10161 ath10k_reg_notifier);
10162 if (ret) {
7aa7a72a 10163 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
0e339447 10164 goto err_dfs_detector_exit;
5e3dd157
KV
10165 }
10166
60e1d0fb 10167 if (test_bit(WMI_SERVICE_SPOOF_MAC_SUPPORT, ar->wmi.svc_map)) {
60e1d0fb
CH
10168 ar->hw->wiphy->features |=
10169 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR;
10170 }
10171
3cb10943 10172 ar->hw->wiphy->cipher_suites = cipher_suites;
2ea9f12c
RM
10173
10174 /* QCA988x and QCA6174 family chips do not support CCMP-256, GCMP-128
10175 * and GCMP-256 ciphers in hardware. Fetch number of ciphers supported
10176 * from chip specific hw_param table.
10177 */
10178 if (!ar->hw_params.n_cipher_suites ||
10179 ar->hw_params.n_cipher_suites > ARRAY_SIZE(cipher_suites)) {
10180 ath10k_err(ar, "invalid hw_params.n_cipher_suites %d\n",
10181 ar->hw_params.n_cipher_suites);
10182 ar->hw_params.n_cipher_suites = 8;
10183 }
10184 ar->hw->wiphy->n_cipher_suites = ar->hw_params.n_cipher_suites;
ae44b502
AZ
10185
10186 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
3cb10943 10187
bb2edb73
THJ
10188 ar->hw->weight_multiplier = ATH10K_AIRTIME_WEIGHT_MULTIPLIER;
10189
5e3dd157
KV
10190 ret = ieee80211_register_hw(ar->hw);
10191 if (ret) {
7aa7a72a 10192 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
0e339447 10193 goto err_dfs_detector_exit;
5e3dd157
KV
10194 }
10195
4920ce3b
MP
10196 if (test_bit(WMI_SERVICE_PER_PACKET_SW_ENCRYPT, ar->wmi.svc_map)) {
10197 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_AP_VLAN);
10198 ar->hw->wiphy->software_iftypes |= BIT(NL80211_IFTYPE_AP_VLAN);
10199 }
10200
5e3dd157
KV
10201 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
10202 ret = regulatory_hint(ar->hw->wiphy,
10203 ar->ath_common.regulatory.alpha2);
10204 if (ret)
d6015b27 10205 goto err_unregister;
5e3dd157
KV
10206 }
10207
10208 return 0;
d6015b27
MK
10209
10210err_unregister:
5e3dd157 10211 ieee80211_unregister_hw(ar->hw);
0e339447
JJ
10212
10213err_dfs_detector_exit:
97f2645f 10214 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
0e339447
JJ
10215 ar->dfs_detector->exit(ar->dfs_detector);
10216
d6015b27 10217err_free:
57fbcce3
JB
10218 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10219 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
d6015b27 10220
0e339447 10221 SET_IEEE80211_DEV(ar->hw, NULL);
5e3dd157
KV
10222 return ret;
10223}
10224
10225void ath10k_mac_unregister(struct ath10k *ar)
10226{
10227 ieee80211_unregister_hw(ar->hw);
10228
97f2645f 10229 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
9702c686
JD
10230 ar->dfs_detector->exit(ar->dfs_detector);
10231
57fbcce3
JB
10232 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
10233 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
5e3dd157
KV
10234
10235 SET_IEEE80211_DEV(ar->hw, NULL);
10236}