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