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