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