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