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