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