ath10k: allow setting coverage class
[linux-stable.git] / drivers / net / wireless / ath / ath10k / mac.c
blob2e5d2ca6d81615389c38c2903c911df667a57a9d
1 /*
2 * Copyright (c) 2005-2011 Atheros Communications Inc.
3 * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include "mac.h"
20 #include <net/mac80211.h>
21 #include <linux/etherdevice.h>
22 #include <linux/acpi.h>
24 #include "hif.h"
25 #include "core.h"
26 #include "debug.h"
27 #include "wmi.h"
28 #include "htt.h"
29 #include "txrx.h"
30 #include "testmode.h"
31 #include "wmi.h"
32 #include "wmi-tlv.h"
33 #include "wmi-ops.h"
34 #include "wow.h"
36 /*********/
37 /* Rates */
38 /*********/
40 static struct ieee80211_rate ath10k_rates[] = {
41 { .bitrate = 10,
42 .hw_value = ATH10K_HW_RATE_CCK_LP_1M },
43 { .bitrate = 20,
44 .hw_value = ATH10K_HW_RATE_CCK_LP_2M,
45 .hw_value_short = ATH10K_HW_RATE_CCK_SP_2M,
46 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
47 { .bitrate = 55,
48 .hw_value = ATH10K_HW_RATE_CCK_LP_5_5M,
49 .hw_value_short = ATH10K_HW_RATE_CCK_SP_5_5M,
50 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
51 { .bitrate = 110,
52 .hw_value = ATH10K_HW_RATE_CCK_LP_11M,
53 .hw_value_short = ATH10K_HW_RATE_CCK_SP_11M,
54 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
56 { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
57 { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
58 { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
59 { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
60 { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
61 { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
62 { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
63 { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
66 static struct ieee80211_rate ath10k_rates_rev2[] = {
67 { .bitrate = 10,
68 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_1M },
69 { .bitrate = 20,
70 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_2M,
71 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_2M,
72 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
73 { .bitrate = 55,
74 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_5_5M,
75 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_5_5M,
76 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
77 { .bitrate = 110,
78 .hw_value = ATH10K_HW_RATE_REV2_CCK_LP_11M,
79 .hw_value_short = ATH10K_HW_RATE_REV2_CCK_SP_11M,
80 .flags = IEEE80211_RATE_SHORT_PREAMBLE },
82 { .bitrate = 60, .hw_value = ATH10K_HW_RATE_OFDM_6M },
83 { .bitrate = 90, .hw_value = ATH10K_HW_RATE_OFDM_9M },
84 { .bitrate = 120, .hw_value = ATH10K_HW_RATE_OFDM_12M },
85 { .bitrate = 180, .hw_value = ATH10K_HW_RATE_OFDM_18M },
86 { .bitrate = 240, .hw_value = ATH10K_HW_RATE_OFDM_24M },
87 { .bitrate = 360, .hw_value = ATH10K_HW_RATE_OFDM_36M },
88 { .bitrate = 480, .hw_value = ATH10K_HW_RATE_OFDM_48M },
89 { .bitrate = 540, .hw_value = ATH10K_HW_RATE_OFDM_54M },
92 #define ATH10K_MAC_FIRST_OFDM_RATE_IDX 4
94 #define ath10k_a_rates (ath10k_rates + ATH10K_MAC_FIRST_OFDM_RATE_IDX)
95 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - \
96 ATH10K_MAC_FIRST_OFDM_RATE_IDX)
97 #define ath10k_g_rates (ath10k_rates + 0)
98 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
100 #define ath10k_g_rates_rev2 (ath10k_rates_rev2 + 0)
101 #define ath10k_g_rates_rev2_size (ARRAY_SIZE(ath10k_rates_rev2))
103 static bool ath10k_mac_bitrate_is_cck(int bitrate)
105 switch (bitrate) {
106 case 10:
107 case 20:
108 case 55:
109 case 110:
110 return true;
113 return false;
116 static u8 ath10k_mac_bitrate_to_rate(int bitrate)
118 return DIV_ROUND_UP(bitrate, 5) |
119 (ath10k_mac_bitrate_is_cck(bitrate) ? BIT(7) : 0);
122 u8 ath10k_mac_hw_rate_to_idx(const struct ieee80211_supported_band *sband,
123 u8 hw_rate, bool cck)
125 const struct ieee80211_rate *rate;
126 int i;
128 for (i = 0; i < sband->n_bitrates; i++) {
129 rate = &sband->bitrates[i];
131 if (ath10k_mac_bitrate_is_cck(rate->bitrate) != cck)
132 continue;
134 if (rate->hw_value == hw_rate)
135 return i;
136 else if (rate->flags & IEEE80211_RATE_SHORT_PREAMBLE &&
137 rate->hw_value_short == hw_rate)
138 return i;
141 return 0;
144 u8 ath10k_mac_bitrate_to_idx(const struct ieee80211_supported_band *sband,
145 u32 bitrate)
147 int i;
149 for (i = 0; i < sband->n_bitrates; i++)
150 if (sband->bitrates[i].bitrate == bitrate)
151 return i;
153 return 0;
156 static int ath10k_mac_get_max_vht_mcs_map(u16 mcs_map, int nss)
158 switch ((mcs_map >> (2 * nss)) & 0x3) {
159 case IEEE80211_VHT_MCS_SUPPORT_0_7: return BIT(8) - 1;
160 case IEEE80211_VHT_MCS_SUPPORT_0_8: return BIT(9) - 1;
161 case IEEE80211_VHT_MCS_SUPPORT_0_9: return BIT(10) - 1;
163 return 0;
166 static u32
167 ath10k_mac_max_ht_nss(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
169 int nss;
171 for (nss = IEEE80211_HT_MCS_MASK_LEN - 1; nss >= 0; nss--)
172 if (ht_mcs_mask[nss])
173 return nss + 1;
175 return 1;
178 static u32
179 ath10k_mac_max_vht_nss(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
181 int nss;
183 for (nss = NL80211_VHT_NSS_MAX - 1; nss >= 0; nss--)
184 if (vht_mcs_mask[nss])
185 return nss + 1;
187 return 1;
190 int ath10k_mac_ext_resource_config(struct ath10k *ar, u32 val)
192 enum wmi_host_platform_type platform_type;
193 int ret;
195 if (test_bit(WMI_SERVICE_TX_MODE_DYNAMIC, ar->wmi.svc_map))
196 platform_type = WMI_HOST_PLATFORM_LOW_PERF;
197 else
198 platform_type = WMI_HOST_PLATFORM_HIGH_PERF;
200 ret = ath10k_wmi_ext_resource_config(ar, platform_type, val);
202 if (ret && ret != -EOPNOTSUPP) {
203 ath10k_warn(ar, "failed to configure ext resource: %d\n", ret);
204 return ret;
207 return 0;
210 /**********/
211 /* Crypto */
212 /**********/
214 static int ath10k_send_key(struct ath10k_vif *arvif,
215 struct ieee80211_key_conf *key,
216 enum set_key_cmd cmd,
217 const u8 *macaddr, u32 flags)
219 struct ath10k *ar = arvif->ar;
220 struct wmi_vdev_install_key_arg arg = {
221 .vdev_id = arvif->vdev_id,
222 .key_idx = key->keyidx,
223 .key_len = key->keylen,
224 .key_data = key->key,
225 .key_flags = flags,
226 .macaddr = macaddr,
229 lockdep_assert_held(&arvif->ar->conf_mutex);
231 switch (key->cipher) {
232 case WLAN_CIPHER_SUITE_CCMP:
233 arg.key_cipher = WMI_CIPHER_AES_CCM;
234 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
235 break;
236 case WLAN_CIPHER_SUITE_TKIP:
237 arg.key_cipher = WMI_CIPHER_TKIP;
238 arg.key_txmic_len = 8;
239 arg.key_rxmic_len = 8;
240 break;
241 case WLAN_CIPHER_SUITE_WEP40:
242 case WLAN_CIPHER_SUITE_WEP104:
243 arg.key_cipher = WMI_CIPHER_WEP;
244 break;
245 case WLAN_CIPHER_SUITE_AES_CMAC:
246 WARN_ON(1);
247 return -EINVAL;
248 default:
249 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
250 return -EOPNOTSUPP;
253 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
254 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
256 if (cmd == DISABLE_KEY) {
257 arg.key_cipher = WMI_CIPHER_NONE;
258 arg.key_data = NULL;
261 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
264 static int ath10k_install_key(struct ath10k_vif *arvif,
265 struct ieee80211_key_conf *key,
266 enum set_key_cmd cmd,
267 const u8 *macaddr, u32 flags)
269 struct ath10k *ar = arvif->ar;
270 int ret;
271 unsigned long time_left;
273 lockdep_assert_held(&ar->conf_mutex);
275 reinit_completion(&ar->install_key_done);
277 if (arvif->nohwcrypt)
278 return 1;
280 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
281 if (ret)
282 return ret;
284 time_left = wait_for_completion_timeout(&ar->install_key_done, 3 * HZ);
285 if (time_left == 0)
286 return -ETIMEDOUT;
288 return 0;
291 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
292 const u8 *addr)
294 struct ath10k *ar = arvif->ar;
295 struct ath10k_peer *peer;
296 int ret;
297 int i;
298 u32 flags;
300 lockdep_assert_held(&ar->conf_mutex);
302 if (WARN_ON(arvif->vif->type != NL80211_IFTYPE_AP &&
303 arvif->vif->type != NL80211_IFTYPE_ADHOC &&
304 arvif->vif->type != NL80211_IFTYPE_MESH_POINT))
305 return -EINVAL;
307 spin_lock_bh(&ar->data_lock);
308 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
309 spin_unlock_bh(&ar->data_lock);
311 if (!peer)
312 return -ENOENT;
314 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
315 if (arvif->wep_keys[i] == NULL)
316 continue;
318 switch (arvif->vif->type) {
319 case NL80211_IFTYPE_AP:
320 flags = WMI_KEY_PAIRWISE;
322 if (arvif->def_wep_key_idx == i)
323 flags |= WMI_KEY_TX_USAGE;
325 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
326 SET_KEY, addr, flags);
327 if (ret < 0)
328 return ret;
329 break;
330 case NL80211_IFTYPE_ADHOC:
331 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
332 SET_KEY, addr,
333 WMI_KEY_PAIRWISE);
334 if (ret < 0)
335 return ret;
337 ret = ath10k_install_key(arvif, arvif->wep_keys[i],
338 SET_KEY, addr, WMI_KEY_GROUP);
339 if (ret < 0)
340 return ret;
341 break;
342 default:
343 WARN_ON(1);
344 return -EINVAL;
347 spin_lock_bh(&ar->data_lock);
348 peer->keys[i] = arvif->wep_keys[i];
349 spin_unlock_bh(&ar->data_lock);
352 /* In some cases (notably with static WEP IBSS with multiple keys)
353 * multicast Tx becomes broken. Both pairwise and groupwise keys are
354 * installed already. Using WMI_KEY_TX_USAGE in different combinations
355 * didn't seem help. Using def_keyid vdev parameter seems to be
356 * effective so use that.
358 * FIXME: Revisit. Perhaps this can be done in a less hacky way.
360 if (arvif->vif->type != NL80211_IFTYPE_ADHOC)
361 return 0;
363 if (arvif->def_wep_key_idx == -1)
364 return 0;
366 ret = ath10k_wmi_vdev_set_param(arvif->ar,
367 arvif->vdev_id,
368 arvif->ar->wmi.vdev_param->def_keyid,
369 arvif->def_wep_key_idx);
370 if (ret) {
371 ath10k_warn(ar, "failed to re-set def wpa key idxon vdev %i: %d\n",
372 arvif->vdev_id, ret);
373 return ret;
376 return 0;
379 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
380 const u8 *addr)
382 struct ath10k *ar = arvif->ar;
383 struct ath10k_peer *peer;
384 int first_errno = 0;
385 int ret;
386 int i;
387 u32 flags = 0;
389 lockdep_assert_held(&ar->conf_mutex);
391 spin_lock_bh(&ar->data_lock);
392 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
393 spin_unlock_bh(&ar->data_lock);
395 if (!peer)
396 return -ENOENT;
398 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
399 if (peer->keys[i] == NULL)
400 continue;
402 /* key flags are not required to delete the key */
403 ret = ath10k_install_key(arvif, peer->keys[i],
404 DISABLE_KEY, addr, flags);
405 if (ret < 0 && first_errno == 0)
406 first_errno = ret;
408 if (ret < 0)
409 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
410 i, ret);
412 spin_lock_bh(&ar->data_lock);
413 peer->keys[i] = NULL;
414 spin_unlock_bh(&ar->data_lock);
417 return first_errno;
420 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
421 u8 keyidx)
423 struct ath10k_peer *peer;
424 int i;
426 lockdep_assert_held(&ar->data_lock);
428 /* We don't know which vdev this peer belongs to,
429 * since WMI doesn't give us that information.
431 * FIXME: multi-bss needs to be handled.
433 peer = ath10k_peer_find(ar, 0, addr);
434 if (!peer)
435 return false;
437 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
438 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
439 return true;
442 return false;
445 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
446 struct ieee80211_key_conf *key)
448 struct ath10k *ar = arvif->ar;
449 struct ath10k_peer *peer;
450 u8 addr[ETH_ALEN];
451 int first_errno = 0;
452 int ret;
453 int i;
454 u32 flags = 0;
456 lockdep_assert_held(&ar->conf_mutex);
458 for (;;) {
459 /* since ath10k_install_key we can't hold data_lock all the
460 * time, so we try to remove the keys incrementally */
461 spin_lock_bh(&ar->data_lock);
462 i = 0;
463 list_for_each_entry(peer, &ar->peers, list) {
464 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
465 if (peer->keys[i] == key) {
466 ether_addr_copy(addr, peer->addr);
467 peer->keys[i] = NULL;
468 break;
472 if (i < ARRAY_SIZE(peer->keys))
473 break;
475 spin_unlock_bh(&ar->data_lock);
477 if (i == ARRAY_SIZE(peer->keys))
478 break;
479 /* key flags are not required to delete the key */
480 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
481 if (ret < 0 && first_errno == 0)
482 first_errno = ret;
484 if (ret)
485 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
486 addr, ret);
489 return first_errno;
492 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
493 struct ieee80211_key_conf *key)
495 struct ath10k *ar = arvif->ar;
496 struct ath10k_peer *peer;
497 int ret;
499 lockdep_assert_held(&ar->conf_mutex);
501 list_for_each_entry(peer, &ar->peers, list) {
502 if (ether_addr_equal(peer->addr, arvif->vif->addr))
503 continue;
505 if (ether_addr_equal(peer->addr, arvif->bssid))
506 continue;
508 if (peer->keys[key->keyidx] == key)
509 continue;
511 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
512 arvif->vdev_id, key->keyidx);
514 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
515 if (ret) {
516 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
517 arvif->vdev_id, peer->addr, ret);
518 return ret;
522 return 0;
525 /*********************/
526 /* General utilities */
527 /*********************/
529 static inline enum wmi_phy_mode
530 chan_to_phymode(const struct cfg80211_chan_def *chandef)
532 enum wmi_phy_mode phymode = MODE_UNKNOWN;
534 switch (chandef->chan->band) {
535 case NL80211_BAND_2GHZ:
536 switch (chandef->width) {
537 case NL80211_CHAN_WIDTH_20_NOHT:
538 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
539 phymode = MODE_11B;
540 else
541 phymode = MODE_11G;
542 break;
543 case NL80211_CHAN_WIDTH_20:
544 phymode = MODE_11NG_HT20;
545 break;
546 case NL80211_CHAN_WIDTH_40:
547 phymode = MODE_11NG_HT40;
548 break;
549 case NL80211_CHAN_WIDTH_5:
550 case NL80211_CHAN_WIDTH_10:
551 case NL80211_CHAN_WIDTH_80:
552 case NL80211_CHAN_WIDTH_80P80:
553 case NL80211_CHAN_WIDTH_160:
554 phymode = MODE_UNKNOWN;
555 break;
557 break;
558 case NL80211_BAND_5GHZ:
559 switch (chandef->width) {
560 case NL80211_CHAN_WIDTH_20_NOHT:
561 phymode = MODE_11A;
562 break;
563 case NL80211_CHAN_WIDTH_20:
564 phymode = MODE_11NA_HT20;
565 break;
566 case NL80211_CHAN_WIDTH_40:
567 phymode = MODE_11NA_HT40;
568 break;
569 case NL80211_CHAN_WIDTH_80:
570 phymode = MODE_11AC_VHT80;
571 break;
572 case NL80211_CHAN_WIDTH_5:
573 case NL80211_CHAN_WIDTH_10:
574 case NL80211_CHAN_WIDTH_80P80:
575 case NL80211_CHAN_WIDTH_160:
576 phymode = MODE_UNKNOWN;
577 break;
579 break;
580 default:
581 break;
584 WARN_ON(phymode == MODE_UNKNOWN);
585 return phymode;
588 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
591 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
592 * 0 for no restriction
593 * 1 for 1/4 us
594 * 2 for 1/2 us
595 * 3 for 1 us
596 * 4 for 2 us
597 * 5 for 4 us
598 * 6 for 8 us
599 * 7 for 16 us
601 switch (mpdudensity) {
602 case 0:
603 return 0;
604 case 1:
605 case 2:
606 case 3:
607 /* Our lower layer calculations limit our precision to
608 1 microsecond */
609 return 1;
610 case 4:
611 return 2;
612 case 5:
613 return 4;
614 case 6:
615 return 8;
616 case 7:
617 return 16;
618 default:
619 return 0;
623 int ath10k_mac_vif_chan(struct ieee80211_vif *vif,
624 struct cfg80211_chan_def *def)
626 struct ieee80211_chanctx_conf *conf;
628 rcu_read_lock();
629 conf = rcu_dereference(vif->chanctx_conf);
630 if (!conf) {
631 rcu_read_unlock();
632 return -ENOENT;
635 *def = conf->def;
636 rcu_read_unlock();
638 return 0;
641 static void ath10k_mac_num_chanctxs_iter(struct ieee80211_hw *hw,
642 struct ieee80211_chanctx_conf *conf,
643 void *data)
645 int *num = data;
647 (*num)++;
650 static int ath10k_mac_num_chanctxs(struct ath10k *ar)
652 int num = 0;
654 ieee80211_iter_chan_contexts_atomic(ar->hw,
655 ath10k_mac_num_chanctxs_iter,
656 &num);
658 return num;
661 static void
662 ath10k_mac_get_any_chandef_iter(struct ieee80211_hw *hw,
663 struct ieee80211_chanctx_conf *conf,
664 void *data)
666 struct cfg80211_chan_def **def = data;
668 *def = &conf->def;
671 static int ath10k_peer_create(struct ath10k *ar,
672 struct ieee80211_vif *vif,
673 struct ieee80211_sta *sta,
674 u32 vdev_id,
675 const u8 *addr,
676 enum wmi_peer_type peer_type)
678 struct ath10k_vif *arvif;
679 struct ath10k_peer *peer;
680 int num_peers = 0;
681 int ret;
683 lockdep_assert_held(&ar->conf_mutex);
685 num_peers = ar->num_peers;
687 /* Each vdev consumes a peer entry as well */
688 list_for_each_entry(arvif, &ar->arvifs, list)
689 num_peers++;
691 if (num_peers >= ar->max_num_peers)
692 return -ENOBUFS;
694 ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
695 if (ret) {
696 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
697 addr, vdev_id, ret);
698 return ret;
701 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
702 if (ret) {
703 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
704 addr, vdev_id, ret);
705 return ret;
708 spin_lock_bh(&ar->data_lock);
710 peer = ath10k_peer_find(ar, vdev_id, addr);
711 if (!peer) {
712 spin_unlock_bh(&ar->data_lock);
713 ath10k_warn(ar, "failed to find peer %pM on vdev %i after creation\n",
714 addr, vdev_id);
715 ath10k_wmi_peer_delete(ar, vdev_id, addr);
716 return -ENOENT;
719 peer->vif = vif;
720 peer->sta = sta;
722 spin_unlock_bh(&ar->data_lock);
724 ar->num_peers++;
726 return 0;
729 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
731 struct ath10k *ar = arvif->ar;
732 u32 param;
733 int ret;
735 param = ar->wmi.pdev_param->sta_kickout_th;
736 ret = ath10k_wmi_pdev_set_param(ar, param,
737 ATH10K_KICKOUT_THRESHOLD);
738 if (ret) {
739 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
740 arvif->vdev_id, ret);
741 return ret;
744 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
745 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
746 ATH10K_KEEPALIVE_MIN_IDLE);
747 if (ret) {
748 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
749 arvif->vdev_id, ret);
750 return ret;
753 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
754 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
755 ATH10K_KEEPALIVE_MAX_IDLE);
756 if (ret) {
757 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
758 arvif->vdev_id, ret);
759 return ret;
762 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
763 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
764 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
765 if (ret) {
766 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
767 arvif->vdev_id, ret);
768 return ret;
771 return 0;
774 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
776 struct ath10k *ar = arvif->ar;
777 u32 vdev_param;
779 vdev_param = ar->wmi.vdev_param->rts_threshold;
780 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
783 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
785 int ret;
787 lockdep_assert_held(&ar->conf_mutex);
789 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
790 if (ret)
791 return ret;
793 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
794 if (ret)
795 return ret;
797 ar->num_peers--;
799 return 0;
802 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
804 struct ath10k_peer *peer, *tmp;
805 int peer_id;
806 int i;
808 lockdep_assert_held(&ar->conf_mutex);
810 spin_lock_bh(&ar->data_lock);
811 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
812 if (peer->vdev_id != vdev_id)
813 continue;
815 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
816 peer->addr, vdev_id);
818 for_each_set_bit(peer_id, peer->peer_ids,
819 ATH10K_MAX_NUM_PEER_IDS) {
820 ar->peer_map[peer_id] = NULL;
823 /* Double check that peer is properly un-referenced from
824 * the peer_map
826 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
827 if (ar->peer_map[i] == peer) {
828 ath10k_warn(ar, "removing stale peer_map entry for %pM (ptr %pK idx %d)\n",
829 peer->addr, peer, i);
830 ar->peer_map[i] = NULL;
834 list_del(&peer->list);
835 kfree(peer);
836 ar->num_peers--;
838 spin_unlock_bh(&ar->data_lock);
841 static void ath10k_peer_cleanup_all(struct ath10k *ar)
843 struct ath10k_peer *peer, *tmp;
844 int i;
846 lockdep_assert_held(&ar->conf_mutex);
848 spin_lock_bh(&ar->data_lock);
849 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
850 list_del(&peer->list);
851 kfree(peer);
854 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++)
855 ar->peer_map[i] = NULL;
857 spin_unlock_bh(&ar->data_lock);
859 ar->num_peers = 0;
860 ar->num_stations = 0;
863 static int ath10k_mac_tdls_peer_update(struct ath10k *ar, u32 vdev_id,
864 struct ieee80211_sta *sta,
865 enum wmi_tdls_peer_state state)
867 int ret;
868 struct wmi_tdls_peer_update_cmd_arg arg = {};
869 struct wmi_tdls_peer_capab_arg cap = {};
870 struct wmi_channel_arg chan_arg = {};
872 lockdep_assert_held(&ar->conf_mutex);
874 arg.vdev_id = vdev_id;
875 arg.peer_state = state;
876 ether_addr_copy(arg.addr, sta->addr);
878 cap.peer_max_sp = sta->max_sp;
879 cap.peer_uapsd_queues = sta->uapsd_queues;
881 if (state == WMI_TDLS_PEER_STATE_CONNECTED &&
882 !sta->tdls_initiator)
883 cap.is_peer_responder = 1;
885 ret = ath10k_wmi_tdls_peer_update(ar, &arg, &cap, &chan_arg);
886 if (ret) {
887 ath10k_warn(ar, "failed to update tdls peer %pM on vdev %i: %i\n",
888 arg.addr, vdev_id, ret);
889 return ret;
892 return 0;
895 /************************/
896 /* Interface management */
897 /************************/
899 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
901 struct ath10k *ar = arvif->ar;
903 lockdep_assert_held(&ar->data_lock);
905 if (!arvif->beacon)
906 return;
908 if (!arvif->beacon_buf)
909 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
910 arvif->beacon->len, DMA_TO_DEVICE);
912 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
913 arvif->beacon_state != ATH10K_BEACON_SENT))
914 return;
916 dev_kfree_skb_any(arvif->beacon);
918 arvif->beacon = NULL;
919 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
922 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
924 struct ath10k *ar = arvif->ar;
926 lockdep_assert_held(&ar->data_lock);
928 ath10k_mac_vif_beacon_free(arvif);
930 if (arvif->beacon_buf) {
931 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
932 arvif->beacon_buf, arvif->beacon_paddr);
933 arvif->beacon_buf = NULL;
937 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
939 unsigned long time_left;
941 lockdep_assert_held(&ar->conf_mutex);
943 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
944 return -ESHUTDOWN;
946 time_left = wait_for_completion_timeout(&ar->vdev_setup_done,
947 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
948 if (time_left == 0)
949 return -ETIMEDOUT;
951 return 0;
954 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
956 struct cfg80211_chan_def *chandef = NULL;
957 struct ieee80211_channel *channel = NULL;
958 struct wmi_vdev_start_request_arg arg = {};
959 int ret = 0;
961 lockdep_assert_held(&ar->conf_mutex);
963 ieee80211_iter_chan_contexts_atomic(ar->hw,
964 ath10k_mac_get_any_chandef_iter,
965 &chandef);
966 if (WARN_ON_ONCE(!chandef))
967 return -ENOENT;
969 channel = chandef->chan;
971 arg.vdev_id = vdev_id;
972 arg.channel.freq = channel->center_freq;
973 arg.channel.band_center_freq1 = chandef->center_freq1;
975 /* TODO setup this dynamically, what in case we
976 don't have any vifs? */
977 arg.channel.mode = chan_to_phymode(chandef);
978 arg.channel.chan_radar =
979 !!(channel->flags & IEEE80211_CHAN_RADAR);
981 arg.channel.min_power = 0;
982 arg.channel.max_power = channel->max_power * 2;
983 arg.channel.max_reg_power = channel->max_reg_power * 2;
984 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
986 reinit_completion(&ar->vdev_setup_done);
988 ret = ath10k_wmi_vdev_start(ar, &arg);
989 if (ret) {
990 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
991 vdev_id, ret);
992 return ret;
995 ret = ath10k_vdev_setup_sync(ar);
996 if (ret) {
997 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
998 vdev_id, ret);
999 return ret;
1002 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
1003 if (ret) {
1004 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
1005 vdev_id, ret);
1006 goto vdev_stop;
1009 ar->monitor_vdev_id = vdev_id;
1011 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
1012 ar->monitor_vdev_id);
1013 return 0;
1015 vdev_stop:
1016 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1017 if (ret)
1018 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
1019 ar->monitor_vdev_id, ret);
1021 return ret;
1024 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
1026 int ret = 0;
1028 lockdep_assert_held(&ar->conf_mutex);
1030 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
1031 if (ret)
1032 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
1033 ar->monitor_vdev_id, ret);
1035 reinit_completion(&ar->vdev_setup_done);
1037 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
1038 if (ret)
1039 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
1040 ar->monitor_vdev_id, ret);
1042 ret = ath10k_vdev_setup_sync(ar);
1043 if (ret)
1044 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
1045 ar->monitor_vdev_id, ret);
1047 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
1048 ar->monitor_vdev_id);
1049 return ret;
1052 static int ath10k_monitor_vdev_create(struct ath10k *ar)
1054 int bit, ret = 0;
1056 lockdep_assert_held(&ar->conf_mutex);
1058 if (ar->free_vdev_map == 0) {
1059 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
1060 return -ENOMEM;
1063 bit = __ffs64(ar->free_vdev_map);
1065 ar->monitor_vdev_id = bit;
1067 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
1068 WMI_VDEV_TYPE_MONITOR,
1069 0, ar->mac_addr);
1070 if (ret) {
1071 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
1072 ar->monitor_vdev_id, ret);
1073 return ret;
1076 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
1077 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
1078 ar->monitor_vdev_id);
1080 return 0;
1083 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
1085 int ret = 0;
1087 lockdep_assert_held(&ar->conf_mutex);
1089 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
1090 if (ret) {
1091 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
1092 ar->monitor_vdev_id, ret);
1093 return ret;
1096 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
1098 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
1099 ar->monitor_vdev_id);
1100 return ret;
1103 static int ath10k_monitor_start(struct ath10k *ar)
1105 int ret;
1107 lockdep_assert_held(&ar->conf_mutex);
1109 ret = ath10k_monitor_vdev_create(ar);
1110 if (ret) {
1111 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
1112 return ret;
1115 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
1116 if (ret) {
1117 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
1118 ath10k_monitor_vdev_delete(ar);
1119 return ret;
1122 ar->monitor_started = true;
1123 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
1125 return 0;
1128 static int ath10k_monitor_stop(struct ath10k *ar)
1130 int ret;
1132 lockdep_assert_held(&ar->conf_mutex);
1134 ret = ath10k_monitor_vdev_stop(ar);
1135 if (ret) {
1136 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
1137 return ret;
1140 ret = ath10k_monitor_vdev_delete(ar);
1141 if (ret) {
1142 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
1143 return ret;
1146 ar->monitor_started = false;
1147 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
1149 return 0;
1152 static bool ath10k_mac_monitor_vdev_is_needed(struct ath10k *ar)
1154 int num_ctx;
1156 /* At least one chanctx is required to derive a channel to start
1157 * monitor vdev on.
1159 num_ctx = ath10k_mac_num_chanctxs(ar);
1160 if (num_ctx == 0)
1161 return false;
1163 /* If there's already an existing special monitor interface then don't
1164 * bother creating another monitor vdev.
1166 if (ar->monitor_arvif)
1167 return false;
1169 return ar->monitor ||
1170 ar->filter_flags & FIF_OTHER_BSS ||
1171 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1174 static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1176 int num_ctx;
1178 num_ctx = ath10k_mac_num_chanctxs(ar);
1180 /* FIXME: Current interface combinations and cfg80211/mac80211 code
1181 * shouldn't allow this but make sure to prevent handling the following
1182 * case anyway since multi-channel DFS hasn't been tested at all.
1184 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1185 return false;
1187 return true;
1190 static int ath10k_monitor_recalc(struct ath10k *ar)
1192 bool needed;
1193 bool allowed;
1194 int ret;
1196 lockdep_assert_held(&ar->conf_mutex);
1198 needed = ath10k_mac_monitor_vdev_is_needed(ar);
1199 allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1201 ath10k_dbg(ar, ATH10K_DBG_MAC,
1202 "mac monitor recalc started? %d needed? %d allowed? %d\n",
1203 ar->monitor_started, needed, allowed);
1205 if (WARN_ON(needed && !allowed)) {
1206 if (ar->monitor_started) {
1207 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1209 ret = ath10k_monitor_stop(ar);
1210 if (ret)
1211 ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1212 ret);
1213 /* not serious */
1216 return -EPERM;
1219 if (needed == ar->monitor_started)
1220 return 0;
1222 if (needed)
1223 return ath10k_monitor_start(ar);
1224 else
1225 return ath10k_monitor_stop(ar);
1228 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1230 struct ath10k *ar = arvif->ar;
1231 u32 vdev_param, rts_cts = 0;
1233 lockdep_assert_held(&ar->conf_mutex);
1235 vdev_param = ar->wmi.vdev_param->enable_rtscts;
1237 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1239 if (arvif->num_legacy_stations > 0)
1240 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1241 WMI_RTSCTS_PROFILE);
1242 else
1243 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1244 WMI_RTSCTS_PROFILE);
1246 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1247 rts_cts);
1250 static int ath10k_start_cac(struct ath10k *ar)
1252 int ret;
1254 lockdep_assert_held(&ar->conf_mutex);
1256 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1258 ret = ath10k_monitor_recalc(ar);
1259 if (ret) {
1260 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1261 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1262 return ret;
1265 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1266 ar->monitor_vdev_id);
1268 return 0;
1271 static int ath10k_stop_cac(struct ath10k *ar)
1273 lockdep_assert_held(&ar->conf_mutex);
1275 /* CAC is not running - do nothing */
1276 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1277 return 0;
1279 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1280 ath10k_monitor_stop(ar);
1282 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1284 return 0;
1287 static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1288 struct ieee80211_chanctx_conf *conf,
1289 void *data)
1291 bool *ret = data;
1293 if (!*ret && conf->radar_enabled)
1294 *ret = true;
1297 static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1299 bool has_radar = false;
1301 ieee80211_iter_chan_contexts_atomic(ar->hw,
1302 ath10k_mac_has_radar_iter,
1303 &has_radar);
1305 return has_radar;
1308 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1310 int ret;
1312 lockdep_assert_held(&ar->conf_mutex);
1314 ath10k_stop_cac(ar);
1316 if (!ath10k_mac_has_radar_enabled(ar))
1317 return;
1319 if (ar->num_started_vdevs > 0)
1320 return;
1322 ret = ath10k_start_cac(ar);
1323 if (ret) {
1325 * Not possible to start CAC on current channel so starting
1326 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1327 * by indicating that radar was detected.
1329 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1330 ieee80211_radar_detected(ar->hw);
1334 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1336 struct ath10k *ar = arvif->ar;
1337 int ret;
1339 lockdep_assert_held(&ar->conf_mutex);
1341 reinit_completion(&ar->vdev_setup_done);
1343 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1344 if (ret) {
1345 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1346 arvif->vdev_id, ret);
1347 return ret;
1350 ret = ath10k_vdev_setup_sync(ar);
1351 if (ret) {
1352 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
1353 arvif->vdev_id, ret);
1354 return ret;
1357 WARN_ON(ar->num_started_vdevs == 0);
1359 if (ar->num_started_vdevs != 0) {
1360 ar->num_started_vdevs--;
1361 ath10k_recalc_radar_detection(ar);
1364 return ret;
1367 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1368 const struct cfg80211_chan_def *chandef,
1369 bool restart)
1371 struct ath10k *ar = arvif->ar;
1372 struct wmi_vdev_start_request_arg arg = {};
1373 int ret = 0;
1375 lockdep_assert_held(&ar->conf_mutex);
1377 reinit_completion(&ar->vdev_setup_done);
1379 arg.vdev_id = arvif->vdev_id;
1380 arg.dtim_period = arvif->dtim_period;
1381 arg.bcn_intval = arvif->beacon_interval;
1383 arg.channel.freq = chandef->chan->center_freq;
1384 arg.channel.band_center_freq1 = chandef->center_freq1;
1385 arg.channel.mode = chan_to_phymode(chandef);
1387 arg.channel.min_power = 0;
1388 arg.channel.max_power = chandef->chan->max_power * 2;
1389 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1390 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1392 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1393 arg.ssid = arvif->u.ap.ssid;
1394 arg.ssid_len = arvif->u.ap.ssid_len;
1395 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1397 /* For now allow DFS for AP mode */
1398 arg.channel.chan_radar =
1399 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1400 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1401 arg.ssid = arvif->vif->bss_conf.ssid;
1402 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1405 ath10k_dbg(ar, ATH10K_DBG_MAC,
1406 "mac vdev %d start center_freq %d phymode %s\n",
1407 arg.vdev_id, arg.channel.freq,
1408 ath10k_wmi_phymode_str(arg.channel.mode));
1410 if (restart)
1411 ret = ath10k_wmi_vdev_restart(ar, &arg);
1412 else
1413 ret = ath10k_wmi_vdev_start(ar, &arg);
1415 if (ret) {
1416 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1417 arg.vdev_id, ret);
1418 return ret;
1421 ret = ath10k_vdev_setup_sync(ar);
1422 if (ret) {
1423 ath10k_warn(ar,
1424 "failed to synchronize setup for vdev %i restart %d: %d\n",
1425 arg.vdev_id, restart, ret);
1426 return ret;
1429 ar->num_started_vdevs++;
1430 ath10k_recalc_radar_detection(ar);
1432 return ret;
1435 static int ath10k_vdev_start(struct ath10k_vif *arvif,
1436 const struct cfg80211_chan_def *def)
1438 return ath10k_vdev_start_restart(arvif, def, false);
1441 static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1442 const struct cfg80211_chan_def *def)
1444 return ath10k_vdev_start_restart(arvif, def, true);
1447 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1448 struct sk_buff *bcn)
1450 struct ath10k *ar = arvif->ar;
1451 struct ieee80211_mgmt *mgmt;
1452 const u8 *p2p_ie;
1453 int ret;
1455 if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
1456 return 0;
1458 mgmt = (void *)bcn->data;
1459 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1460 mgmt->u.beacon.variable,
1461 bcn->len - (mgmt->u.beacon.variable -
1462 bcn->data));
1463 if (!p2p_ie)
1464 return -ENOENT;
1466 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1467 if (ret) {
1468 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1469 arvif->vdev_id, ret);
1470 return ret;
1473 return 0;
1476 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1477 u8 oui_type, size_t ie_offset)
1479 size_t len;
1480 const u8 *next;
1481 const u8 *end;
1482 u8 *ie;
1484 if (WARN_ON(skb->len < ie_offset))
1485 return -EINVAL;
1487 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1488 skb->data + ie_offset,
1489 skb->len - ie_offset);
1490 if (!ie)
1491 return -ENOENT;
1493 len = ie[1] + 2;
1494 end = skb->data + skb->len;
1495 next = ie + len;
1497 if (WARN_ON(next > end))
1498 return -EINVAL;
1500 memmove(ie, next, end - next);
1501 skb_trim(skb, skb->len - len);
1503 return 0;
1506 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1508 struct ath10k *ar = arvif->ar;
1509 struct ieee80211_hw *hw = ar->hw;
1510 struct ieee80211_vif *vif = arvif->vif;
1511 struct ieee80211_mutable_offsets offs = {};
1512 struct sk_buff *bcn;
1513 int ret;
1515 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1516 return 0;
1518 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1519 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1520 return 0;
1522 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1523 if (!bcn) {
1524 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1525 return -EPERM;
1528 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1529 if (ret) {
1530 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1531 kfree_skb(bcn);
1532 return ret;
1535 /* P2P IE is inserted by firmware automatically (as configured above)
1536 * so remove it from the base beacon template to avoid duplicate P2P
1537 * IEs in beacon frames.
1539 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1540 offsetof(struct ieee80211_mgmt,
1541 u.beacon.variable));
1543 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1544 0, NULL, 0);
1545 kfree_skb(bcn);
1547 if (ret) {
1548 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1549 ret);
1550 return ret;
1553 return 0;
1556 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1558 struct ath10k *ar = arvif->ar;
1559 struct ieee80211_hw *hw = ar->hw;
1560 struct ieee80211_vif *vif = arvif->vif;
1561 struct sk_buff *prb;
1562 int ret;
1564 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1565 return 0;
1567 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1568 return 0;
1570 prb = ieee80211_proberesp_get(hw, vif);
1571 if (!prb) {
1572 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1573 return -EPERM;
1576 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1577 kfree_skb(prb);
1579 if (ret) {
1580 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1581 ret);
1582 return ret;
1585 return 0;
1588 static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1590 struct ath10k *ar = arvif->ar;
1591 struct cfg80211_chan_def def;
1592 int ret;
1594 /* When originally vdev is started during assign_vif_chanctx() some
1595 * information is missing, notably SSID. Firmware revisions with beacon
1596 * offloading require the SSID to be provided during vdev (re)start to
1597 * handle hidden SSID properly.
1599 * Vdev restart must be done after vdev has been both started and
1600 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1601 * deliver vdev restart response event causing timeouts during vdev
1602 * syncing in ath10k.
1604 * Note: The vdev down/up and template reinstallation could be skipped
1605 * since only wmi-tlv firmware are known to have beacon offload and
1606 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1607 * response delivery. It's probably more robust to keep it as is.
1609 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1610 return 0;
1612 if (WARN_ON(!arvif->is_started))
1613 return -EINVAL;
1615 if (WARN_ON(!arvif->is_up))
1616 return -EINVAL;
1618 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1619 return -EINVAL;
1621 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1622 if (ret) {
1623 ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1624 arvif->vdev_id, ret);
1625 return ret;
1628 /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1629 * firmware will crash upon vdev up.
1632 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1633 if (ret) {
1634 ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1635 return ret;
1638 ret = ath10k_mac_setup_prb_tmpl(arvif);
1639 if (ret) {
1640 ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1641 return ret;
1644 ret = ath10k_vdev_restart(arvif, &def);
1645 if (ret) {
1646 ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1647 arvif->vdev_id, ret);
1648 return ret;
1651 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1652 arvif->bssid);
1653 if (ret) {
1654 ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1655 arvif->vdev_id, ret);
1656 return ret;
1659 return 0;
1662 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1663 struct ieee80211_bss_conf *info)
1665 struct ath10k *ar = arvif->ar;
1666 int ret = 0;
1668 lockdep_assert_held(&arvif->ar->conf_mutex);
1670 if (!info->enable_beacon) {
1671 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1672 if (ret)
1673 ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1674 arvif->vdev_id, ret);
1676 arvif->is_up = false;
1678 spin_lock_bh(&arvif->ar->data_lock);
1679 ath10k_mac_vif_beacon_free(arvif);
1680 spin_unlock_bh(&arvif->ar->data_lock);
1682 return;
1685 arvif->tx_seq_no = 0x1000;
1687 arvif->aid = 0;
1688 ether_addr_copy(arvif->bssid, info->bssid);
1690 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1691 arvif->bssid);
1692 if (ret) {
1693 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1694 arvif->vdev_id, ret);
1695 return;
1698 arvif->is_up = true;
1700 ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1701 if (ret) {
1702 ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1703 arvif->vdev_id, ret);
1704 return;
1707 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1710 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1711 struct ieee80211_bss_conf *info,
1712 const u8 self_peer[ETH_ALEN])
1714 struct ath10k *ar = arvif->ar;
1715 u32 vdev_param;
1716 int ret = 0;
1718 lockdep_assert_held(&arvif->ar->conf_mutex);
1720 if (!info->ibss_joined) {
1721 if (is_zero_ether_addr(arvif->bssid))
1722 return;
1724 eth_zero_addr(arvif->bssid);
1726 return;
1729 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1730 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1731 ATH10K_DEFAULT_ATIM);
1732 if (ret)
1733 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1734 arvif->vdev_id, ret);
1737 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1739 struct ath10k *ar = arvif->ar;
1740 u32 param;
1741 u32 value;
1742 int ret;
1744 lockdep_assert_held(&arvif->ar->conf_mutex);
1746 if (arvif->u.sta.uapsd)
1747 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1748 else
1749 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1751 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1752 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1753 if (ret) {
1754 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1755 value, arvif->vdev_id, ret);
1756 return ret;
1759 return 0;
1762 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1764 struct ath10k *ar = arvif->ar;
1765 u32 param;
1766 u32 value;
1767 int ret;
1769 lockdep_assert_held(&arvif->ar->conf_mutex);
1771 if (arvif->u.sta.uapsd)
1772 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1773 else
1774 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1776 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1777 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1778 param, value);
1779 if (ret) {
1780 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1781 value, arvif->vdev_id, ret);
1782 return ret;
1785 return 0;
1788 static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1790 struct ath10k_vif *arvif;
1791 int num = 0;
1793 lockdep_assert_held(&ar->conf_mutex);
1795 list_for_each_entry(arvif, &ar->arvifs, list)
1796 if (arvif->is_started)
1797 num++;
1799 return num;
1802 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1804 struct ath10k *ar = arvif->ar;
1805 struct ieee80211_vif *vif = arvif->vif;
1806 struct ieee80211_conf *conf = &ar->hw->conf;
1807 enum wmi_sta_powersave_param param;
1808 enum wmi_sta_ps_mode psmode;
1809 int ret;
1810 int ps_timeout;
1811 bool enable_ps;
1813 lockdep_assert_held(&arvif->ar->conf_mutex);
1815 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1816 return 0;
1818 enable_ps = arvif->ps;
1820 if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1821 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1822 ar->running_fw->fw_file.fw_features)) {
1823 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1824 arvif->vdev_id);
1825 enable_ps = false;
1828 if (!arvif->is_started) {
1829 /* mac80211 can update vif powersave state while disconnected.
1830 * Firmware doesn't behave nicely and consumes more power than
1831 * necessary if PS is disabled on a non-started vdev. Hence
1832 * force-enable PS for non-running vdevs.
1834 psmode = WMI_STA_PS_MODE_ENABLED;
1835 } else if (enable_ps) {
1836 psmode = WMI_STA_PS_MODE_ENABLED;
1837 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1839 ps_timeout = conf->dynamic_ps_timeout;
1840 if (ps_timeout == 0) {
1841 /* Firmware doesn't like 0 */
1842 ps_timeout = ieee80211_tu_to_usec(
1843 vif->bss_conf.beacon_int) / 1000;
1846 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1847 ps_timeout);
1848 if (ret) {
1849 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1850 arvif->vdev_id, ret);
1851 return ret;
1853 } else {
1854 psmode = WMI_STA_PS_MODE_DISABLED;
1857 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1858 arvif->vdev_id, psmode ? "enable" : "disable");
1860 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1861 if (ret) {
1862 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1863 psmode, arvif->vdev_id, ret);
1864 return ret;
1867 return 0;
1870 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1872 struct ath10k *ar = arvif->ar;
1873 struct wmi_sta_keepalive_arg arg = {};
1874 int ret;
1876 lockdep_assert_held(&arvif->ar->conf_mutex);
1878 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1879 return 0;
1881 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1882 return 0;
1884 /* Some firmware revisions have a bug and ignore the `enabled` field.
1885 * Instead use the interval to disable the keepalive.
1887 arg.vdev_id = arvif->vdev_id;
1888 arg.enabled = 1;
1889 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1890 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1892 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1893 if (ret) {
1894 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1895 arvif->vdev_id, ret);
1896 return ret;
1899 return 0;
1902 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1904 struct ath10k *ar = arvif->ar;
1905 struct ieee80211_vif *vif = arvif->vif;
1906 int ret;
1908 lockdep_assert_held(&arvif->ar->conf_mutex);
1910 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1911 return;
1913 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1914 return;
1916 if (!vif->csa_active)
1917 return;
1919 if (!arvif->is_up)
1920 return;
1922 if (!ieee80211_csa_is_complete(vif)) {
1923 ieee80211_csa_update_counter(vif);
1925 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1926 if (ret)
1927 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1928 ret);
1930 ret = ath10k_mac_setup_prb_tmpl(arvif);
1931 if (ret)
1932 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1933 ret);
1934 } else {
1935 ieee80211_csa_finish(vif);
1939 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1941 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1942 ap_csa_work);
1943 struct ath10k *ar = arvif->ar;
1945 mutex_lock(&ar->conf_mutex);
1946 ath10k_mac_vif_ap_csa_count_down(arvif);
1947 mutex_unlock(&ar->conf_mutex);
1950 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1951 struct ieee80211_vif *vif)
1953 struct sk_buff *skb = data;
1954 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1955 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1957 if (vif->type != NL80211_IFTYPE_STATION)
1958 return;
1960 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1961 return;
1963 cancel_delayed_work(&arvif->connection_loss_work);
1966 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1968 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1969 IEEE80211_IFACE_ITER_NORMAL,
1970 ath10k_mac_handle_beacon_iter,
1971 skb);
1974 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1975 struct ieee80211_vif *vif)
1977 u32 *vdev_id = data;
1978 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1979 struct ath10k *ar = arvif->ar;
1980 struct ieee80211_hw *hw = ar->hw;
1982 if (arvif->vdev_id != *vdev_id)
1983 return;
1985 if (!arvif->is_up)
1986 return;
1988 ieee80211_beacon_loss(vif);
1990 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1991 * (done by mac80211) succeeds but beacons do not resume then it
1992 * doesn't make sense to continue operation. Queue connection loss work
1993 * which can be cancelled when beacon is received.
1995 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1996 ATH10K_CONNECTION_LOSS_HZ);
1999 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2001 ieee80211_iterate_active_interfaces_atomic(ar->hw,
2002 IEEE80211_IFACE_ITER_NORMAL,
2003 ath10k_mac_handle_beacon_miss_iter,
2004 &vdev_id);
2007 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2009 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2010 connection_loss_work.work);
2011 struct ieee80211_vif *vif = arvif->vif;
2013 if (!arvif->is_up)
2014 return;
2016 ieee80211_connection_loss(vif);
2019 /**********************/
2020 /* Station management */
2021 /**********************/
2023 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2024 struct ieee80211_vif *vif)
2026 /* Some firmware revisions have unstable STA powersave when listen
2027 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2028 * generate NullFunc frames properly even if buffered frames have been
2029 * indicated in Beacon TIM. Firmware would seldom wake up to pull
2030 * buffered frames. Often pinging the device from AP would simply fail.
2032 * As a workaround set it to 1.
2034 if (vif->type == NL80211_IFTYPE_STATION)
2035 return 1;
2037 return ar->hw->conf.listen_interval;
2040 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
2041 struct ieee80211_vif *vif,
2042 struct ieee80211_sta *sta,
2043 struct wmi_peer_assoc_complete_arg *arg)
2045 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2046 u32 aid;
2048 lockdep_assert_held(&ar->conf_mutex);
2050 if (vif->type == NL80211_IFTYPE_STATION)
2051 aid = vif->bss_conf.aid;
2052 else
2053 aid = sta->aid;
2055 ether_addr_copy(arg->addr, sta->addr);
2056 arg->vdev_id = arvif->vdev_id;
2057 arg->peer_aid = aid;
2058 arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
2059 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
2060 arg->peer_num_spatial_streams = 1;
2061 arg->peer_caps = vif->bss_conf.assoc_capability;
2064 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
2065 struct ieee80211_vif *vif,
2066 struct ieee80211_sta *sta,
2067 struct wmi_peer_assoc_complete_arg *arg)
2069 struct ieee80211_bss_conf *info = &vif->bss_conf;
2070 struct cfg80211_chan_def def;
2071 struct cfg80211_bss *bss;
2072 const u8 *rsnie = NULL;
2073 const u8 *wpaie = NULL;
2075 lockdep_assert_held(&ar->conf_mutex);
2077 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2078 return;
2080 bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
2081 IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2082 if (bss) {
2083 const struct cfg80211_bss_ies *ies;
2085 rcu_read_lock();
2086 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2088 ies = rcu_dereference(bss->ies);
2090 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2091 WLAN_OUI_TYPE_MICROSOFT_WPA,
2092 ies->data,
2093 ies->len);
2094 rcu_read_unlock();
2095 cfg80211_put_bss(ar->hw->wiphy, bss);
2098 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2099 if (rsnie || wpaie) {
2100 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2101 arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2104 if (wpaie) {
2105 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2106 arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2109 if (sta->mfp &&
2110 test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2111 ar->running_fw->fw_file.fw_features)) {
2112 arg->peer_flags |= ar->wmi.peer_flags->pmf;
2116 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2117 struct ieee80211_vif *vif,
2118 struct ieee80211_sta *sta,
2119 struct wmi_peer_assoc_complete_arg *arg)
2121 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2122 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2123 struct cfg80211_chan_def def;
2124 const struct ieee80211_supported_band *sband;
2125 const struct ieee80211_rate *rates;
2126 enum nl80211_band band;
2127 u32 ratemask;
2128 u8 rate;
2129 int i;
2131 lockdep_assert_held(&ar->conf_mutex);
2133 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2134 return;
2136 band = def.chan->band;
2137 sband = ar->hw->wiphy->bands[band];
2138 ratemask = sta->supp_rates[band];
2139 ratemask &= arvif->bitrate_mask.control[band].legacy;
2140 rates = sband->bitrates;
2142 rateset->num_rates = 0;
2144 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2145 if (!(ratemask & 1))
2146 continue;
2148 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2149 rateset->rates[rateset->num_rates] = rate;
2150 rateset->num_rates++;
2154 static bool
2155 ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2157 int nss;
2159 for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2160 if (ht_mcs_mask[nss])
2161 return false;
2163 return true;
2166 static bool
2167 ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2169 int nss;
2171 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2172 if (vht_mcs_mask[nss])
2173 return false;
2175 return true;
2178 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2179 struct ieee80211_vif *vif,
2180 struct ieee80211_sta *sta,
2181 struct wmi_peer_assoc_complete_arg *arg)
2183 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2184 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2185 struct cfg80211_chan_def def;
2186 enum nl80211_band band;
2187 const u8 *ht_mcs_mask;
2188 const u16 *vht_mcs_mask;
2189 int i, n;
2190 u8 max_nss;
2191 u32 stbc;
2193 lockdep_assert_held(&ar->conf_mutex);
2195 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2196 return;
2198 if (!ht_cap->ht_supported)
2199 return;
2201 band = def.chan->band;
2202 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2203 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2205 if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2206 ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2207 return;
2209 arg->peer_flags |= ar->wmi.peer_flags->ht;
2210 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2211 ht_cap->ampdu_factor)) - 1;
2213 arg->peer_mpdu_density =
2214 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2216 arg->peer_ht_caps = ht_cap->cap;
2217 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2219 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2220 arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2222 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2223 arg->peer_flags |= ar->wmi.peer_flags->bw40;
2224 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2227 if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2228 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2229 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2231 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2232 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2235 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2236 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2237 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2240 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2241 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2242 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2243 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2244 arg->peer_rate_caps |= stbc;
2245 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2248 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2249 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2250 else if (ht_cap->mcs.rx_mask[1])
2251 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2253 for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2254 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2255 (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2256 max_nss = (i / 8) + 1;
2257 arg->peer_ht_rates.rates[n++] = i;
2261 * This is a workaround for HT-enabled STAs which break the spec
2262 * and have no HT capabilities RX mask (no HT RX MCS map).
2264 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2265 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2267 * Firmware asserts if such situation occurs.
2269 if (n == 0) {
2270 arg->peer_ht_rates.num_rates = 8;
2271 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2272 arg->peer_ht_rates.rates[i] = i;
2273 } else {
2274 arg->peer_ht_rates.num_rates = n;
2275 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2278 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2279 arg->addr,
2280 arg->peer_ht_rates.num_rates,
2281 arg->peer_num_spatial_streams);
2284 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2285 struct ath10k_vif *arvif,
2286 struct ieee80211_sta *sta)
2288 u32 uapsd = 0;
2289 u32 max_sp = 0;
2290 int ret = 0;
2292 lockdep_assert_held(&ar->conf_mutex);
2294 if (sta->wme && sta->uapsd_queues) {
2295 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2296 sta->uapsd_queues, sta->max_sp);
2298 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2299 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2300 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2301 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2302 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2303 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2304 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2305 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2306 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2307 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2308 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2309 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2311 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2312 max_sp = sta->max_sp;
2314 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2315 sta->addr,
2316 WMI_AP_PS_PEER_PARAM_UAPSD,
2317 uapsd);
2318 if (ret) {
2319 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2320 arvif->vdev_id, ret);
2321 return ret;
2324 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2325 sta->addr,
2326 WMI_AP_PS_PEER_PARAM_MAX_SP,
2327 max_sp);
2328 if (ret) {
2329 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2330 arvif->vdev_id, ret);
2331 return ret;
2334 /* TODO setup this based on STA listen interval and
2335 beacon interval. Currently we don't know
2336 sta->listen_interval - mac80211 patch required.
2337 Currently use 10 seconds */
2338 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2339 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2340 10);
2341 if (ret) {
2342 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2343 arvif->vdev_id, ret);
2344 return ret;
2348 return 0;
2351 static u16
2352 ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2353 const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2355 int idx_limit;
2356 int nss;
2357 u16 mcs_map;
2358 u16 mcs;
2360 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2361 mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2362 vht_mcs_limit[nss];
2364 if (mcs_map)
2365 idx_limit = fls(mcs_map) - 1;
2366 else
2367 idx_limit = -1;
2369 switch (idx_limit) {
2370 case 0: /* fall through */
2371 case 1: /* fall through */
2372 case 2: /* fall through */
2373 case 3: /* fall through */
2374 case 4: /* fall through */
2375 case 5: /* fall through */
2376 case 6: /* fall through */
2377 default:
2378 /* see ath10k_mac_can_set_bitrate_mask() */
2379 WARN_ON(1);
2380 /* fall through */
2381 case -1:
2382 mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2383 break;
2384 case 7:
2385 mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2386 break;
2387 case 8:
2388 mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2389 break;
2390 case 9:
2391 mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2392 break;
2395 tx_mcs_set &= ~(0x3 << (nss * 2));
2396 tx_mcs_set |= mcs << (nss * 2);
2399 return tx_mcs_set;
2402 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2403 struct ieee80211_vif *vif,
2404 struct ieee80211_sta *sta,
2405 struct wmi_peer_assoc_complete_arg *arg)
2407 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2408 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2409 struct cfg80211_chan_def def;
2410 enum nl80211_band band;
2411 const u16 *vht_mcs_mask;
2412 u8 ampdu_factor;
2414 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2415 return;
2417 if (!vht_cap->vht_supported)
2418 return;
2420 band = def.chan->band;
2421 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2423 if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2424 return;
2426 arg->peer_flags |= ar->wmi.peer_flags->vht;
2428 if (def.chan->band == NL80211_BAND_2GHZ)
2429 arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2431 arg->peer_vht_caps = vht_cap->cap;
2433 ampdu_factor = (vht_cap->cap &
2434 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2435 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2437 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2438 * zero in VHT IE. Using it would result in degraded throughput.
2439 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2440 * it if VHT max_mpdu is smaller. */
2441 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2442 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2443 ampdu_factor)) - 1);
2445 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2446 arg->peer_flags |= ar->wmi.peer_flags->bw80;
2448 arg->peer_vht_rates.rx_max_rate =
2449 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2450 arg->peer_vht_rates.rx_mcs_set =
2451 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2452 arg->peer_vht_rates.tx_max_rate =
2453 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2454 arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2455 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2457 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
2458 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
2461 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2462 struct ieee80211_vif *vif,
2463 struct ieee80211_sta *sta,
2464 struct wmi_peer_assoc_complete_arg *arg)
2466 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2468 switch (arvif->vdev_type) {
2469 case WMI_VDEV_TYPE_AP:
2470 if (sta->wme)
2471 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2473 if (sta->wme && sta->uapsd_queues) {
2474 arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2475 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2477 break;
2478 case WMI_VDEV_TYPE_STA:
2479 if (vif->bss_conf.qos)
2480 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2481 break;
2482 case WMI_VDEV_TYPE_IBSS:
2483 if (sta->wme)
2484 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2485 break;
2486 default:
2487 break;
2490 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2491 sta->addr, !!(arg->peer_flags &
2492 arvif->ar->wmi.peer_flags->qos));
2495 static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2497 return sta->supp_rates[NL80211_BAND_2GHZ] >>
2498 ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2501 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2502 struct ieee80211_vif *vif,
2503 struct ieee80211_sta *sta,
2504 struct wmi_peer_assoc_complete_arg *arg)
2506 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2507 struct cfg80211_chan_def def;
2508 enum nl80211_band band;
2509 const u8 *ht_mcs_mask;
2510 const u16 *vht_mcs_mask;
2511 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2513 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2514 return;
2516 band = def.chan->band;
2517 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2518 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2520 switch (band) {
2521 case NL80211_BAND_2GHZ:
2522 if (sta->vht_cap.vht_supported &&
2523 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2524 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2525 phymode = MODE_11AC_VHT40;
2526 else
2527 phymode = MODE_11AC_VHT20;
2528 } else if (sta->ht_cap.ht_supported &&
2529 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2530 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2531 phymode = MODE_11NG_HT40;
2532 else
2533 phymode = MODE_11NG_HT20;
2534 } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2535 phymode = MODE_11G;
2536 } else {
2537 phymode = MODE_11B;
2540 break;
2541 case NL80211_BAND_5GHZ:
2543 * Check VHT first.
2545 if (sta->vht_cap.vht_supported &&
2546 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2547 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2548 phymode = MODE_11AC_VHT80;
2549 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2550 phymode = MODE_11AC_VHT40;
2551 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2552 phymode = MODE_11AC_VHT20;
2553 } else if (sta->ht_cap.ht_supported &&
2554 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2555 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2556 phymode = MODE_11NA_HT40;
2557 else
2558 phymode = MODE_11NA_HT20;
2559 } else {
2560 phymode = MODE_11A;
2563 break;
2564 default:
2565 break;
2568 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2569 sta->addr, ath10k_wmi_phymode_str(phymode));
2571 arg->peer_phymode = phymode;
2572 WARN_ON(phymode == MODE_UNKNOWN);
2575 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2576 struct ieee80211_vif *vif,
2577 struct ieee80211_sta *sta,
2578 struct wmi_peer_assoc_complete_arg *arg)
2580 lockdep_assert_held(&ar->conf_mutex);
2582 memset(arg, 0, sizeof(*arg));
2584 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2585 ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2586 ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2587 ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2588 ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2589 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2590 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2592 return 0;
2595 static const u32 ath10k_smps_map[] = {
2596 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2597 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2598 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2599 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2602 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2603 const u8 *addr,
2604 const struct ieee80211_sta_ht_cap *ht_cap)
2606 int smps;
2608 if (!ht_cap->ht_supported)
2609 return 0;
2611 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2612 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2614 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2615 return -EINVAL;
2617 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2618 WMI_PEER_SMPS_STATE,
2619 ath10k_smps_map[smps]);
2622 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2623 struct ieee80211_vif *vif,
2624 struct ieee80211_sta_vht_cap vht_cap)
2626 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2627 int ret;
2628 u32 param;
2629 u32 value;
2631 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2632 return 0;
2634 if (!(ar->vht_cap_info &
2635 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2636 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2637 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2638 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2639 return 0;
2641 param = ar->wmi.vdev_param->txbf;
2642 value = 0;
2644 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2645 return 0;
2647 /* The following logic is correct. If a remote STA advertises support
2648 * for being a beamformer then we should enable us being a beamformee.
2651 if (ar->vht_cap_info &
2652 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2653 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2654 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2655 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2657 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2658 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2661 if (ar->vht_cap_info &
2662 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2663 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2664 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2665 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2667 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2668 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2671 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2672 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2674 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2675 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2677 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2678 if (ret) {
2679 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2680 value, ret);
2681 return ret;
2684 return 0;
2687 /* can be called only in mac80211 callbacks due to `key_count` usage */
2688 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2689 struct ieee80211_vif *vif,
2690 struct ieee80211_bss_conf *bss_conf)
2692 struct ath10k *ar = hw->priv;
2693 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2694 struct ieee80211_sta_ht_cap ht_cap;
2695 struct ieee80211_sta_vht_cap vht_cap;
2696 struct wmi_peer_assoc_complete_arg peer_arg;
2697 struct ieee80211_sta *ap_sta;
2698 int ret;
2700 lockdep_assert_held(&ar->conf_mutex);
2702 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2703 arvif->vdev_id, arvif->bssid, arvif->aid);
2705 rcu_read_lock();
2707 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2708 if (!ap_sta) {
2709 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2710 bss_conf->bssid, arvif->vdev_id);
2711 rcu_read_unlock();
2712 return;
2715 /* ap_sta must be accessed only within rcu section which must be left
2716 * before calling ath10k_setup_peer_smps() which might sleep. */
2717 ht_cap = ap_sta->ht_cap;
2718 vht_cap = ap_sta->vht_cap;
2720 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2721 if (ret) {
2722 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2723 bss_conf->bssid, arvif->vdev_id, ret);
2724 rcu_read_unlock();
2725 return;
2728 rcu_read_unlock();
2730 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2731 if (ret) {
2732 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2733 bss_conf->bssid, arvif->vdev_id, ret);
2734 return;
2737 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2738 if (ret) {
2739 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2740 arvif->vdev_id, ret);
2741 return;
2744 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2745 if (ret) {
2746 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2747 arvif->vdev_id, bss_conf->bssid, ret);
2748 return;
2751 ath10k_dbg(ar, ATH10K_DBG_MAC,
2752 "mac vdev %d up (associated) bssid %pM aid %d\n",
2753 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2755 WARN_ON(arvif->is_up);
2757 arvif->aid = bss_conf->aid;
2758 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2760 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2761 if (ret) {
2762 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2763 arvif->vdev_id, ret);
2764 return;
2767 arvif->is_up = true;
2769 /* Workaround: Some firmware revisions (tested with qca6174
2770 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2771 * poked with peer param command.
2773 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2774 WMI_PEER_DUMMY_VAR, 1);
2775 if (ret) {
2776 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2777 arvif->bssid, arvif->vdev_id, ret);
2778 return;
2782 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2783 struct ieee80211_vif *vif)
2785 struct ath10k *ar = hw->priv;
2786 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2787 struct ieee80211_sta_vht_cap vht_cap = {};
2788 int ret;
2790 lockdep_assert_held(&ar->conf_mutex);
2792 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2793 arvif->vdev_id, arvif->bssid);
2795 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2796 if (ret)
2797 ath10k_warn(ar, "failed to down vdev %i: %d\n",
2798 arvif->vdev_id, ret);
2800 arvif->def_wep_key_idx = -1;
2802 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2803 if (ret) {
2804 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2805 arvif->vdev_id, ret);
2806 return;
2809 arvif->is_up = false;
2811 cancel_delayed_work_sync(&arvif->connection_loss_work);
2814 static int ath10k_station_assoc(struct ath10k *ar,
2815 struct ieee80211_vif *vif,
2816 struct ieee80211_sta *sta,
2817 bool reassoc)
2819 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2820 struct wmi_peer_assoc_complete_arg peer_arg;
2821 int ret = 0;
2823 lockdep_assert_held(&ar->conf_mutex);
2825 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2826 if (ret) {
2827 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2828 sta->addr, arvif->vdev_id, ret);
2829 return ret;
2832 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2833 if (ret) {
2834 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2835 sta->addr, arvif->vdev_id, ret);
2836 return ret;
2839 /* Re-assoc is run only to update supported rates for given station. It
2840 * doesn't make much sense to reconfigure the peer completely.
2842 if (!reassoc) {
2843 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2844 &sta->ht_cap);
2845 if (ret) {
2846 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2847 arvif->vdev_id, ret);
2848 return ret;
2851 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2852 if (ret) {
2853 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2854 sta->addr, arvif->vdev_id, ret);
2855 return ret;
2858 if (!sta->wme) {
2859 arvif->num_legacy_stations++;
2860 ret = ath10k_recalc_rtscts_prot(arvif);
2861 if (ret) {
2862 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2863 arvif->vdev_id, ret);
2864 return ret;
2868 /* Plumb cached keys only for static WEP */
2869 if (arvif->def_wep_key_idx != -1) {
2870 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2871 if (ret) {
2872 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2873 arvif->vdev_id, ret);
2874 return ret;
2879 return ret;
2882 static int ath10k_station_disassoc(struct ath10k *ar,
2883 struct ieee80211_vif *vif,
2884 struct ieee80211_sta *sta)
2886 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2887 int ret = 0;
2889 lockdep_assert_held(&ar->conf_mutex);
2891 if (!sta->wme) {
2892 arvif->num_legacy_stations--;
2893 ret = ath10k_recalc_rtscts_prot(arvif);
2894 if (ret) {
2895 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2896 arvif->vdev_id, ret);
2897 return ret;
2901 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2902 if (ret) {
2903 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2904 arvif->vdev_id, ret);
2905 return ret;
2908 return ret;
2911 /**************/
2912 /* Regulatory */
2913 /**************/
2915 static int ath10k_update_channel_list(struct ath10k *ar)
2917 struct ieee80211_hw *hw = ar->hw;
2918 struct ieee80211_supported_band **bands;
2919 enum nl80211_band band;
2920 struct ieee80211_channel *channel;
2921 struct wmi_scan_chan_list_arg arg = {0};
2922 struct wmi_channel_arg *ch;
2923 bool passive;
2924 int len;
2925 int ret;
2926 int i;
2928 lockdep_assert_held(&ar->conf_mutex);
2930 bands = hw->wiphy->bands;
2931 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2932 if (!bands[band])
2933 continue;
2935 for (i = 0; i < bands[band]->n_channels; i++) {
2936 if (bands[band]->channels[i].flags &
2937 IEEE80211_CHAN_DISABLED)
2938 continue;
2940 arg.n_channels++;
2944 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2945 arg.channels = kzalloc(len, GFP_KERNEL);
2946 if (!arg.channels)
2947 return -ENOMEM;
2949 ch = arg.channels;
2950 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2951 if (!bands[band])
2952 continue;
2954 for (i = 0; i < bands[band]->n_channels; i++) {
2955 channel = &bands[band]->channels[i];
2957 if (channel->flags & IEEE80211_CHAN_DISABLED)
2958 continue;
2960 ch->allow_ht = true;
2962 /* FIXME: when should we really allow VHT? */
2963 ch->allow_vht = true;
2965 ch->allow_ibss =
2966 !(channel->flags & IEEE80211_CHAN_NO_IR);
2968 ch->ht40plus =
2969 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2971 ch->chan_radar =
2972 !!(channel->flags & IEEE80211_CHAN_RADAR);
2974 passive = channel->flags & IEEE80211_CHAN_NO_IR;
2975 ch->passive = passive;
2977 ch->freq = channel->center_freq;
2978 ch->band_center_freq1 = channel->center_freq;
2979 ch->min_power = 0;
2980 ch->max_power = channel->max_power * 2;
2981 ch->max_reg_power = channel->max_reg_power * 2;
2982 ch->max_antenna_gain = channel->max_antenna_gain * 2;
2983 ch->reg_class_id = 0; /* FIXME */
2985 /* FIXME: why use only legacy modes, why not any
2986 * HT/VHT modes? Would that even make any
2987 * difference? */
2988 if (channel->band == NL80211_BAND_2GHZ)
2989 ch->mode = MODE_11G;
2990 else
2991 ch->mode = MODE_11A;
2993 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2994 continue;
2996 ath10k_dbg(ar, ATH10K_DBG_WMI,
2997 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2998 ch - arg.channels, arg.n_channels,
2999 ch->freq, ch->max_power, ch->max_reg_power,
3000 ch->max_antenna_gain, ch->mode);
3002 ch++;
3006 ret = ath10k_wmi_scan_chan_list(ar, &arg);
3007 kfree(arg.channels);
3009 return ret;
3012 static enum wmi_dfs_region
3013 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3015 switch (dfs_region) {
3016 case NL80211_DFS_UNSET:
3017 return WMI_UNINIT_DFS_DOMAIN;
3018 case NL80211_DFS_FCC:
3019 return WMI_FCC_DFS_DOMAIN;
3020 case NL80211_DFS_ETSI:
3021 return WMI_ETSI_DFS_DOMAIN;
3022 case NL80211_DFS_JP:
3023 return WMI_MKK4_DFS_DOMAIN;
3025 return WMI_UNINIT_DFS_DOMAIN;
3028 static void ath10k_regd_update(struct ath10k *ar)
3030 struct reg_dmn_pair_mapping *regpair;
3031 int ret;
3032 enum wmi_dfs_region wmi_dfs_reg;
3033 enum nl80211_dfs_regions nl_dfs_reg;
3035 lockdep_assert_held(&ar->conf_mutex);
3037 ret = ath10k_update_channel_list(ar);
3038 if (ret)
3039 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
3041 regpair = ar->ath_common.regulatory.regpair;
3043 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3044 nl_dfs_reg = ar->dfs_detector->region;
3045 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3046 } else {
3047 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3050 /* Target allows setting up per-band regdomain but ath_common provides
3051 * a combined one only */
3052 ret = ath10k_wmi_pdev_set_regdomain(ar,
3053 regpair->reg_domain,
3054 regpair->reg_domain, /* 2ghz */
3055 regpair->reg_domain, /* 5ghz */
3056 regpair->reg_2ghz_ctl,
3057 regpair->reg_5ghz_ctl,
3058 wmi_dfs_reg);
3059 if (ret)
3060 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
3063 static void ath10k_reg_notifier(struct wiphy *wiphy,
3064 struct regulatory_request *request)
3066 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3067 struct ath10k *ar = hw->priv;
3068 bool result;
3070 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3072 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3073 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
3074 request->dfs_region);
3075 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3076 request->dfs_region);
3077 if (!result)
3078 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
3079 request->dfs_region);
3082 mutex_lock(&ar->conf_mutex);
3083 if (ar->state == ATH10K_STATE_ON)
3084 ath10k_regd_update(ar);
3085 mutex_unlock(&ar->conf_mutex);
3088 /***************/
3089 /* TX handlers */
3090 /***************/
3092 enum ath10k_mac_tx_path {
3093 ATH10K_MAC_TX_HTT,
3094 ATH10K_MAC_TX_HTT_MGMT,
3095 ATH10K_MAC_TX_WMI_MGMT,
3096 ATH10K_MAC_TX_UNKNOWN,
3099 void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3101 lockdep_assert_held(&ar->htt.tx_lock);
3103 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3104 ar->tx_paused |= BIT(reason);
3105 ieee80211_stop_queues(ar->hw);
3108 static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3109 struct ieee80211_vif *vif)
3111 struct ath10k *ar = data;
3112 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3114 if (arvif->tx_paused)
3115 return;
3117 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3120 void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3122 lockdep_assert_held(&ar->htt.tx_lock);
3124 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3125 ar->tx_paused &= ~BIT(reason);
3127 if (ar->tx_paused)
3128 return;
3130 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3131 IEEE80211_IFACE_ITER_RESUME_ALL,
3132 ath10k_mac_tx_unlock_iter,
3133 ar);
3135 ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3138 void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3140 struct ath10k *ar = arvif->ar;
3142 lockdep_assert_held(&ar->htt.tx_lock);
3144 WARN_ON(reason >= BITS_PER_LONG);
3145 arvif->tx_paused |= BIT(reason);
3146 ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3149 void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3151 struct ath10k *ar = arvif->ar;
3153 lockdep_assert_held(&ar->htt.tx_lock);
3155 WARN_ON(reason >= BITS_PER_LONG);
3156 arvif->tx_paused &= ~BIT(reason);
3158 if (ar->tx_paused)
3159 return;
3161 if (arvif->tx_paused)
3162 return;
3164 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3167 static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3168 enum wmi_tlv_tx_pause_id pause_id,
3169 enum wmi_tlv_tx_pause_action action)
3171 struct ath10k *ar = arvif->ar;
3173 lockdep_assert_held(&ar->htt.tx_lock);
3175 switch (action) {
3176 case WMI_TLV_TX_PAUSE_ACTION_STOP:
3177 ath10k_mac_vif_tx_lock(arvif, pause_id);
3178 break;
3179 case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3180 ath10k_mac_vif_tx_unlock(arvif, pause_id);
3181 break;
3182 default:
3183 ath10k_dbg(ar, ATH10K_DBG_BOOT,
3184 "received unknown tx pause action %d on vdev %i, ignoring\n",
3185 action, arvif->vdev_id);
3186 break;
3190 struct ath10k_mac_tx_pause {
3191 u32 vdev_id;
3192 enum wmi_tlv_tx_pause_id pause_id;
3193 enum wmi_tlv_tx_pause_action action;
3196 static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3197 struct ieee80211_vif *vif)
3199 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3200 struct ath10k_mac_tx_pause *arg = data;
3202 if (arvif->vdev_id != arg->vdev_id)
3203 return;
3205 ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3208 void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3209 enum wmi_tlv_tx_pause_id pause_id,
3210 enum wmi_tlv_tx_pause_action action)
3212 struct ath10k_mac_tx_pause arg = {
3213 .vdev_id = vdev_id,
3214 .pause_id = pause_id,
3215 .action = action,
3218 spin_lock_bh(&ar->htt.tx_lock);
3219 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3220 IEEE80211_IFACE_ITER_RESUME_ALL,
3221 ath10k_mac_handle_tx_pause_iter,
3222 &arg);
3223 spin_unlock_bh(&ar->htt.tx_lock);
3226 static enum ath10k_hw_txrx_mode
3227 ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3228 struct ieee80211_vif *vif,
3229 struct ieee80211_sta *sta,
3230 struct sk_buff *skb)
3232 const struct ieee80211_hdr *hdr = (void *)skb->data;
3233 __le16 fc = hdr->frame_control;
3235 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3236 return ATH10K_HW_TXRX_RAW;
3238 if (ieee80211_is_mgmt(fc))
3239 return ATH10K_HW_TXRX_MGMT;
3241 /* Workaround:
3243 * NullFunc frames are mostly used to ping if a client or AP are still
3244 * reachable and responsive. This implies tx status reports must be
3245 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3246 * come to a conclusion that the other end disappeared and tear down
3247 * BSS connection or it can never disconnect from BSS/client (which is
3248 * the case).
3250 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3251 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3252 * which seems to deliver correct tx reports for NullFunc frames. The
3253 * downside of using it is it ignores client powersave state so it can
3254 * end up disconnecting sleeping clients in AP mode. It should fix STA
3255 * mode though because AP don't sleep.
3257 if (ar->htt.target_version_major < 3 &&
3258 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3259 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3260 ar->running_fw->fw_file.fw_features) &&
3261 !test_bit(ATH10K_FW_FEATURE_SKIP_NULL_FUNC_WAR,
3262 ar->running_fw->fw_file.fw_features))
3263 return ATH10K_HW_TXRX_MGMT;
3265 /* Workaround:
3267 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3268 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3269 * to work with Ethernet txmode so use it.
3271 * FIXME: Check if raw mode works with TDLS.
3273 if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3274 return ATH10K_HW_TXRX_ETHERNET;
3276 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
3277 return ATH10K_HW_TXRX_RAW;
3279 return ATH10K_HW_TXRX_NATIVE_WIFI;
3282 static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3283 struct sk_buff *skb)
3285 const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3286 const struct ieee80211_hdr *hdr = (void *)skb->data;
3287 const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3288 IEEE80211_TX_CTL_INJECTED;
3290 if (!ieee80211_has_protected(hdr->frame_control))
3291 return false;
3293 if ((info->flags & mask) == mask)
3294 return false;
3296 if (vif)
3297 return !ath10k_vif_to_arvif(vif)->nohwcrypt;
3299 return true;
3302 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3303 * Control in the header.
3305 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3307 struct ieee80211_hdr *hdr = (void *)skb->data;
3308 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3309 u8 *qos_ctl;
3311 if (!ieee80211_is_data_qos(hdr->frame_control))
3312 return;
3314 qos_ctl = ieee80211_get_qos_ctl(hdr);
3315 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3316 skb->data, (void *)qos_ctl - (void *)skb->data);
3317 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3319 /* Some firmware revisions don't handle sending QoS NullFunc well.
3320 * These frames are mainly used for CQM purposes so it doesn't really
3321 * matter whether QoS NullFunc or NullFunc are sent.
3323 hdr = (void *)skb->data;
3324 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3325 cb->flags &= ~ATH10K_SKB_F_QOS;
3327 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3330 static void ath10k_tx_h_8023(struct sk_buff *skb)
3332 struct ieee80211_hdr *hdr;
3333 struct rfc1042_hdr *rfc1042;
3334 struct ethhdr *eth;
3335 size_t hdrlen;
3336 u8 da[ETH_ALEN];
3337 u8 sa[ETH_ALEN];
3338 __be16 type;
3340 hdr = (void *)skb->data;
3341 hdrlen = ieee80211_hdrlen(hdr->frame_control);
3342 rfc1042 = (void *)skb->data + hdrlen;
3344 ether_addr_copy(da, ieee80211_get_DA(hdr));
3345 ether_addr_copy(sa, ieee80211_get_SA(hdr));
3346 type = rfc1042->snap_type;
3348 skb_pull(skb, hdrlen + sizeof(*rfc1042));
3349 skb_push(skb, sizeof(*eth));
3351 eth = (void *)skb->data;
3352 ether_addr_copy(eth->h_dest, da);
3353 ether_addr_copy(eth->h_source, sa);
3354 eth->h_proto = type;
3357 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3358 struct ieee80211_vif *vif,
3359 struct sk_buff *skb)
3361 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3362 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3364 /* This is case only for P2P_GO */
3365 if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
3366 return;
3368 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3369 spin_lock_bh(&ar->data_lock);
3370 if (arvif->u.ap.noa_data)
3371 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3372 GFP_ATOMIC))
3373 memcpy(skb_put(skb, arvif->u.ap.noa_len),
3374 arvif->u.ap.noa_data,
3375 arvif->u.ap.noa_len);
3376 spin_unlock_bh(&ar->data_lock);
3380 static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3381 struct ieee80211_vif *vif,
3382 struct ieee80211_txq *txq,
3383 struct sk_buff *skb)
3385 struct ieee80211_hdr *hdr = (void *)skb->data;
3386 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3388 cb->flags = 0;
3389 if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3390 cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3392 if (ieee80211_is_mgmt(hdr->frame_control))
3393 cb->flags |= ATH10K_SKB_F_MGMT;
3395 if (ieee80211_is_data_qos(hdr->frame_control))
3396 cb->flags |= ATH10K_SKB_F_QOS;
3398 cb->vif = vif;
3399 cb->txq = txq;
3402 bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3404 /* FIXME: Not really sure since when the behaviour changed. At some
3405 * point new firmware stopped requiring creation of peer entries for
3406 * offchannel tx (and actually creating them causes issues with wmi-htc
3407 * tx credit replenishment and reliability). Assuming it's at least 3.4
3408 * because that's when the `freq` was introduced to TX_FRM HTT command.
3410 return (ar->htt.target_version_major >= 3 &&
3411 ar->htt.target_version_minor >= 4 &&
3412 ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3415 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3417 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3418 int ret = 0;
3420 spin_lock_bh(&ar->data_lock);
3422 if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
3423 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3424 ret = -ENOSPC;
3425 goto unlock;
3428 __skb_queue_tail(q, skb);
3429 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3431 unlock:
3432 spin_unlock_bh(&ar->data_lock);
3434 return ret;
3437 static enum ath10k_mac_tx_path
3438 ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3439 struct sk_buff *skb,
3440 enum ath10k_hw_txrx_mode txmode)
3442 switch (txmode) {
3443 case ATH10K_HW_TXRX_RAW:
3444 case ATH10K_HW_TXRX_NATIVE_WIFI:
3445 case ATH10K_HW_TXRX_ETHERNET:
3446 return ATH10K_MAC_TX_HTT;
3447 case ATH10K_HW_TXRX_MGMT:
3448 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3449 ar->running_fw->fw_file.fw_features))
3450 return ATH10K_MAC_TX_WMI_MGMT;
3451 else if (ar->htt.target_version_major >= 3)
3452 return ATH10K_MAC_TX_HTT;
3453 else
3454 return ATH10K_MAC_TX_HTT_MGMT;
3457 return ATH10K_MAC_TX_UNKNOWN;
3460 static int ath10k_mac_tx_submit(struct ath10k *ar,
3461 enum ath10k_hw_txrx_mode txmode,
3462 enum ath10k_mac_tx_path txpath,
3463 struct sk_buff *skb)
3465 struct ath10k_htt *htt = &ar->htt;
3466 int ret = -EINVAL;
3468 switch (txpath) {
3469 case ATH10K_MAC_TX_HTT:
3470 ret = ath10k_htt_tx(htt, txmode, skb);
3471 break;
3472 case ATH10K_MAC_TX_HTT_MGMT:
3473 ret = ath10k_htt_mgmt_tx(htt, skb);
3474 break;
3475 case ATH10K_MAC_TX_WMI_MGMT:
3476 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3477 break;
3478 case ATH10K_MAC_TX_UNKNOWN:
3479 WARN_ON_ONCE(1);
3480 ret = -EINVAL;
3481 break;
3484 if (ret) {
3485 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3486 ret);
3487 ieee80211_free_txskb(ar->hw, skb);
3490 return ret;
3493 /* This function consumes the sk_buff regardless of return value as far as
3494 * caller is concerned so no freeing is necessary afterwards.
3496 static int ath10k_mac_tx(struct ath10k *ar,
3497 struct ieee80211_vif *vif,
3498 struct ieee80211_sta *sta,
3499 enum ath10k_hw_txrx_mode txmode,
3500 enum ath10k_mac_tx_path txpath,
3501 struct sk_buff *skb)
3503 struct ieee80211_hw *hw = ar->hw;
3504 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3505 int ret;
3507 /* We should disable CCK RATE due to P2P */
3508 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3509 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3511 switch (txmode) {
3512 case ATH10K_HW_TXRX_MGMT:
3513 case ATH10K_HW_TXRX_NATIVE_WIFI:
3514 ath10k_tx_h_nwifi(hw, skb);
3515 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3516 ath10k_tx_h_seq_no(vif, skb);
3517 break;
3518 case ATH10K_HW_TXRX_ETHERNET:
3519 ath10k_tx_h_8023(skb);
3520 break;
3521 case ATH10K_HW_TXRX_RAW:
3522 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
3523 WARN_ON_ONCE(1);
3524 ieee80211_free_txskb(hw, skb);
3525 return -ENOTSUPP;
3529 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3530 if (!ath10k_mac_tx_frm_has_freq(ar)) {
3531 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %pK\n",
3532 skb);
3534 skb_queue_tail(&ar->offchan_tx_queue, skb);
3535 ieee80211_queue_work(hw, &ar->offchan_tx_work);
3536 return 0;
3540 ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
3541 if (ret) {
3542 ath10k_warn(ar, "failed to submit frame: %d\n", ret);
3543 return ret;
3546 return 0;
3549 void ath10k_offchan_tx_purge(struct ath10k *ar)
3551 struct sk_buff *skb;
3553 for (;;) {
3554 skb = skb_dequeue(&ar->offchan_tx_queue);
3555 if (!skb)
3556 break;
3558 ieee80211_free_txskb(ar->hw, skb);
3562 void ath10k_offchan_tx_work(struct work_struct *work)
3564 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
3565 struct ath10k_peer *peer;
3566 struct ath10k_vif *arvif;
3567 enum ath10k_hw_txrx_mode txmode;
3568 enum ath10k_mac_tx_path txpath;
3569 struct ieee80211_hdr *hdr;
3570 struct ieee80211_vif *vif;
3571 struct ieee80211_sta *sta;
3572 struct sk_buff *skb;
3573 const u8 *peer_addr;
3574 int vdev_id;
3575 int ret;
3576 unsigned long time_left;
3577 bool tmp_peer_created = false;
3579 /* FW requirement: We must create a peer before FW will send out
3580 * an offchannel frame. Otherwise the frame will be stuck and
3581 * never transmitted. We delete the peer upon tx completion.
3582 * It is unlikely that a peer for offchannel tx will already be
3583 * present. However it may be in some rare cases so account for that.
3584 * Otherwise we might remove a legitimate peer and break stuff. */
3586 for (;;) {
3587 skb = skb_dequeue(&ar->offchan_tx_queue);
3588 if (!skb)
3589 break;
3591 mutex_lock(&ar->conf_mutex);
3593 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK\n",
3594 skb);
3596 hdr = (struct ieee80211_hdr *)skb->data;
3597 peer_addr = ieee80211_get_DA(hdr);
3599 spin_lock_bh(&ar->data_lock);
3600 vdev_id = ar->scan.vdev_id;
3601 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
3602 spin_unlock_bh(&ar->data_lock);
3604 if (peer)
3605 /* FIXME: should this use ath10k_warn()? */
3606 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
3607 peer_addr, vdev_id);
3609 if (!peer) {
3610 ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
3611 peer_addr,
3612 WMI_PEER_TYPE_DEFAULT);
3613 if (ret)
3614 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
3615 peer_addr, vdev_id, ret);
3616 tmp_peer_created = (ret == 0);
3619 spin_lock_bh(&ar->data_lock);
3620 reinit_completion(&ar->offchan_tx_completed);
3621 ar->offchan_tx_skb = skb;
3622 spin_unlock_bh(&ar->data_lock);
3624 /* It's safe to access vif and sta - conf_mutex guarantees that
3625 * sta_state() and remove_interface() are locked exclusively
3626 * out wrt to this offchannel worker.
3628 arvif = ath10k_get_arvif(ar, vdev_id);
3629 if (arvif) {
3630 vif = arvif->vif;
3631 sta = ieee80211_find_sta(vif, peer_addr);
3632 } else {
3633 vif = NULL;
3634 sta = NULL;
3637 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3638 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3640 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
3641 if (ret) {
3642 ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
3643 ret);
3644 /* not serious */
3647 time_left =
3648 wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
3649 if (time_left == 0)
3650 ath10k_warn(ar, "timed out waiting for offchannel skb %pK\n",
3651 skb);
3653 if (!peer && tmp_peer_created) {
3654 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
3655 if (ret)
3656 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
3657 peer_addr, vdev_id, ret);
3660 mutex_unlock(&ar->conf_mutex);
3664 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
3666 struct sk_buff *skb;
3668 for (;;) {
3669 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3670 if (!skb)
3671 break;
3673 ieee80211_free_txskb(ar->hw, skb);
3677 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
3679 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
3680 struct sk_buff *skb;
3681 int ret;
3683 for (;;) {
3684 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3685 if (!skb)
3686 break;
3688 ret = ath10k_wmi_mgmt_tx(ar, skb);
3689 if (ret) {
3690 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
3691 ret);
3692 ieee80211_free_txskb(ar->hw, skb);
3697 static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
3699 struct ath10k_txq *artxq;
3701 if (!txq)
3702 return;
3704 artxq = (void *)txq->drv_priv;
3705 INIT_LIST_HEAD(&artxq->list);
3708 static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
3710 struct ath10k_txq *artxq;
3711 struct ath10k_skb_cb *cb;
3712 struct sk_buff *msdu;
3713 int msdu_id;
3715 if (!txq)
3716 return;
3718 artxq = (void *)txq->drv_priv;
3719 spin_lock_bh(&ar->txqs_lock);
3720 if (!list_empty(&artxq->list))
3721 list_del_init(&artxq->list);
3722 spin_unlock_bh(&ar->txqs_lock);
3724 spin_lock_bh(&ar->htt.tx_lock);
3725 idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
3726 cb = ATH10K_SKB_CB(msdu);
3727 if (cb->txq == txq)
3728 cb->txq = NULL;
3730 spin_unlock_bh(&ar->htt.tx_lock);
3733 struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
3734 u16 peer_id,
3735 u8 tid)
3737 struct ath10k_peer *peer;
3739 lockdep_assert_held(&ar->data_lock);
3741 peer = ar->peer_map[peer_id];
3742 if (!peer)
3743 return NULL;
3745 if (peer->sta)
3746 return peer->sta->txq[tid];
3747 else if (peer->vif)
3748 return peer->vif->txq;
3749 else
3750 return NULL;
3753 static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
3754 struct ieee80211_txq *txq)
3756 struct ath10k *ar = hw->priv;
3757 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3759 /* No need to get locks */
3761 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
3762 return true;
3764 if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
3765 return true;
3767 if (artxq->num_fw_queued < artxq->num_push_allowed)
3768 return true;
3770 return false;
3773 int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
3774 struct ieee80211_txq *txq)
3776 struct ath10k *ar = hw->priv;
3777 struct ath10k_htt *htt = &ar->htt;
3778 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3779 struct ieee80211_vif *vif = txq->vif;
3780 struct ieee80211_sta *sta = txq->sta;
3781 enum ath10k_hw_txrx_mode txmode;
3782 enum ath10k_mac_tx_path txpath;
3783 struct sk_buff *skb;
3784 struct ieee80211_hdr *hdr;
3785 size_t skb_len;
3786 bool is_mgmt, is_presp;
3787 int ret;
3789 spin_lock_bh(&ar->htt.tx_lock);
3790 ret = ath10k_htt_tx_inc_pending(htt);
3791 spin_unlock_bh(&ar->htt.tx_lock);
3793 if (ret)
3794 return ret;
3796 skb = ieee80211_tx_dequeue(hw, txq);
3797 if (!skb) {
3798 spin_lock_bh(&ar->htt.tx_lock);
3799 ath10k_htt_tx_dec_pending(htt);
3800 spin_unlock_bh(&ar->htt.tx_lock);
3802 return -ENOENT;
3805 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
3807 skb_len = skb->len;
3808 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3809 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3810 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
3812 if (is_mgmt) {
3813 hdr = (struct ieee80211_hdr *)skb->data;
3814 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
3816 spin_lock_bh(&ar->htt.tx_lock);
3817 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
3819 if (ret) {
3820 ath10k_htt_tx_dec_pending(htt);
3821 spin_unlock_bh(&ar->htt.tx_lock);
3822 return ret;
3824 spin_unlock_bh(&ar->htt.tx_lock);
3827 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
3828 if (unlikely(ret)) {
3829 ath10k_warn(ar, "failed to push frame: %d\n", ret);
3831 spin_lock_bh(&ar->htt.tx_lock);
3832 ath10k_htt_tx_dec_pending(htt);
3833 if (is_mgmt)
3834 ath10k_htt_tx_mgmt_dec_pending(htt);
3835 spin_unlock_bh(&ar->htt.tx_lock);
3837 return ret;
3840 spin_lock_bh(&ar->htt.tx_lock);
3841 artxq->num_fw_queued++;
3842 spin_unlock_bh(&ar->htt.tx_lock);
3844 return skb_len;
3847 void ath10k_mac_tx_push_pending(struct ath10k *ar)
3849 struct ieee80211_hw *hw = ar->hw;
3850 struct ieee80211_txq *txq;
3851 struct ath10k_txq *artxq;
3852 struct ath10k_txq *last;
3853 int ret;
3854 int max;
3856 if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
3857 return;
3859 spin_lock_bh(&ar->txqs_lock);
3860 rcu_read_lock();
3862 last = list_last_entry(&ar->txqs, struct ath10k_txq, list);
3863 while (!list_empty(&ar->txqs)) {
3864 artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
3865 txq = container_of((void *)artxq, struct ieee80211_txq,
3866 drv_priv);
3868 /* Prevent aggressive sta/tid taking over tx queue */
3869 max = 16;
3870 ret = 0;
3871 while (ath10k_mac_tx_can_push(hw, txq) && max--) {
3872 ret = ath10k_mac_tx_push_txq(hw, txq);
3873 if (ret < 0)
3874 break;
3877 list_del_init(&artxq->list);
3878 if (ret != -ENOENT)
3879 list_add_tail(&artxq->list, &ar->txqs);
3881 ath10k_htt_tx_txq_update(hw, txq);
3883 if (artxq == last || (ret < 0 && ret != -ENOENT))
3884 break;
3887 rcu_read_unlock();
3888 spin_unlock_bh(&ar->txqs_lock);
3891 /************/
3892 /* Scanning */
3893 /************/
3895 void __ath10k_scan_finish(struct ath10k *ar)
3897 lockdep_assert_held(&ar->data_lock);
3899 switch (ar->scan.state) {
3900 case ATH10K_SCAN_IDLE:
3901 break;
3902 case ATH10K_SCAN_RUNNING:
3903 case ATH10K_SCAN_ABORTING:
3904 if (!ar->scan.is_roc) {
3905 struct cfg80211_scan_info info = {
3906 .aborted = (ar->scan.state ==
3907 ATH10K_SCAN_ABORTING),
3910 ieee80211_scan_completed(ar->hw, &info);
3911 } else if (ar->scan.roc_notify) {
3912 ieee80211_remain_on_channel_expired(ar->hw);
3914 /* fall through */
3915 case ATH10K_SCAN_STARTING:
3916 ar->scan.state = ATH10K_SCAN_IDLE;
3917 ar->scan_channel = NULL;
3918 ar->scan.roc_freq = 0;
3919 ath10k_offchan_tx_purge(ar);
3920 cancel_delayed_work(&ar->scan.timeout);
3921 complete(&ar->scan.completed);
3922 break;
3926 void ath10k_scan_finish(struct ath10k *ar)
3928 spin_lock_bh(&ar->data_lock);
3929 __ath10k_scan_finish(ar);
3930 spin_unlock_bh(&ar->data_lock);
3933 static int ath10k_scan_stop(struct ath10k *ar)
3935 struct wmi_stop_scan_arg arg = {
3936 .req_id = 1, /* FIXME */
3937 .req_type = WMI_SCAN_STOP_ONE,
3938 .u.scan_id = ATH10K_SCAN_ID,
3940 int ret;
3942 lockdep_assert_held(&ar->conf_mutex);
3944 ret = ath10k_wmi_stop_scan(ar, &arg);
3945 if (ret) {
3946 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
3947 goto out;
3950 ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3951 if (ret == 0) {
3952 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
3953 ret = -ETIMEDOUT;
3954 } else if (ret > 0) {
3955 ret = 0;
3958 out:
3959 /* Scan state should be updated upon scan completion but in case
3960 * firmware fails to deliver the event (for whatever reason) it is
3961 * desired to clean up scan state anyway. Firmware may have just
3962 * dropped the scan completion event delivery due to transport pipe
3963 * being overflown with data and/or it can recover on its own before
3964 * next scan request is submitted.
3966 spin_lock_bh(&ar->data_lock);
3967 if (ar->scan.state != ATH10K_SCAN_IDLE)
3968 __ath10k_scan_finish(ar);
3969 spin_unlock_bh(&ar->data_lock);
3971 return ret;
3974 static void ath10k_scan_abort(struct ath10k *ar)
3976 int ret;
3978 lockdep_assert_held(&ar->conf_mutex);
3980 spin_lock_bh(&ar->data_lock);
3982 switch (ar->scan.state) {
3983 case ATH10K_SCAN_IDLE:
3984 /* This can happen if timeout worker kicked in and called
3985 * abortion while scan completion was being processed.
3987 break;
3988 case ATH10K_SCAN_STARTING:
3989 case ATH10K_SCAN_ABORTING:
3990 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
3991 ath10k_scan_state_str(ar->scan.state),
3992 ar->scan.state);
3993 break;
3994 case ATH10K_SCAN_RUNNING:
3995 ar->scan.state = ATH10K_SCAN_ABORTING;
3996 spin_unlock_bh(&ar->data_lock);
3998 ret = ath10k_scan_stop(ar);
3999 if (ret)
4000 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
4002 spin_lock_bh(&ar->data_lock);
4003 break;
4006 spin_unlock_bh(&ar->data_lock);
4009 void ath10k_scan_timeout_work(struct work_struct *work)
4011 struct ath10k *ar = container_of(work, struct ath10k,
4012 scan.timeout.work);
4014 mutex_lock(&ar->conf_mutex);
4015 ath10k_scan_abort(ar);
4016 mutex_unlock(&ar->conf_mutex);
4019 static int ath10k_start_scan(struct ath10k *ar,
4020 const struct wmi_start_scan_arg *arg)
4022 int ret;
4024 lockdep_assert_held(&ar->conf_mutex);
4026 ret = ath10k_wmi_start_scan(ar, arg);
4027 if (ret)
4028 return ret;
4030 ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4031 if (ret == 0) {
4032 ret = ath10k_scan_stop(ar);
4033 if (ret)
4034 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4036 return -ETIMEDOUT;
4039 /* If we failed to start the scan, return error code at
4040 * this point. This is probably due to some issue in the
4041 * firmware, but no need to wedge the driver due to that...
4043 spin_lock_bh(&ar->data_lock);
4044 if (ar->scan.state == ATH10K_SCAN_IDLE) {
4045 spin_unlock_bh(&ar->data_lock);
4046 return -EINVAL;
4048 spin_unlock_bh(&ar->data_lock);
4050 return 0;
4053 /**********************/
4054 /* mac80211 callbacks */
4055 /**********************/
4057 static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4058 struct ieee80211_tx_control *control,
4059 struct sk_buff *skb)
4061 struct ath10k *ar = hw->priv;
4062 struct ath10k_htt *htt = &ar->htt;
4063 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4064 struct ieee80211_vif *vif = info->control.vif;
4065 struct ieee80211_sta *sta = control->sta;
4066 struct ieee80211_txq *txq = NULL;
4067 struct ieee80211_hdr *hdr = (void *)skb->data;
4068 enum ath10k_hw_txrx_mode txmode;
4069 enum ath10k_mac_tx_path txpath;
4070 bool is_htt;
4071 bool is_mgmt;
4072 bool is_presp;
4073 int ret;
4075 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
4077 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4078 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4079 is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4080 txpath == ATH10K_MAC_TX_HTT_MGMT);
4081 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4083 if (is_htt) {
4084 spin_lock_bh(&ar->htt.tx_lock);
4085 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4087 ret = ath10k_htt_tx_inc_pending(htt);
4088 if (ret) {
4089 ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4090 ret);
4091 spin_unlock_bh(&ar->htt.tx_lock);
4092 ieee80211_free_txskb(ar->hw, skb);
4093 return;
4096 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4097 if (ret) {
4098 ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4099 ret);
4100 ath10k_htt_tx_dec_pending(htt);
4101 spin_unlock_bh(&ar->htt.tx_lock);
4102 ieee80211_free_txskb(ar->hw, skb);
4103 return;
4105 spin_unlock_bh(&ar->htt.tx_lock);
4108 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
4109 if (ret) {
4110 ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
4111 if (is_htt) {
4112 spin_lock_bh(&ar->htt.tx_lock);
4113 ath10k_htt_tx_dec_pending(htt);
4114 if (is_mgmt)
4115 ath10k_htt_tx_mgmt_dec_pending(htt);
4116 spin_unlock_bh(&ar->htt.tx_lock);
4118 return;
4122 static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4123 struct ieee80211_txq *txq)
4125 struct ath10k *ar = hw->priv;
4126 struct ath10k_txq *artxq = (void *)txq->drv_priv;
4127 struct ieee80211_txq *f_txq;
4128 struct ath10k_txq *f_artxq;
4129 int ret = 0;
4130 int max = 16;
4132 spin_lock_bh(&ar->txqs_lock);
4133 if (list_empty(&artxq->list))
4134 list_add_tail(&artxq->list, &ar->txqs);
4136 f_artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
4137 f_txq = container_of((void *)f_artxq, struct ieee80211_txq, drv_priv);
4138 list_del_init(&f_artxq->list);
4140 while (ath10k_mac_tx_can_push(hw, f_txq) && max--) {
4141 ret = ath10k_mac_tx_push_txq(hw, f_txq);
4142 if (ret)
4143 break;
4145 if (ret != -ENOENT)
4146 list_add_tail(&f_artxq->list, &ar->txqs);
4147 spin_unlock_bh(&ar->txqs_lock);
4149 ath10k_htt_tx_txq_update(hw, f_txq);
4150 ath10k_htt_tx_txq_update(hw, txq);
4153 /* Must not be called with conf_mutex held as workers can use that also. */
4154 void ath10k_drain_tx(struct ath10k *ar)
4156 /* make sure rcu-protected mac80211 tx path itself is drained */
4157 synchronize_net();
4159 ath10k_offchan_tx_purge(ar);
4160 ath10k_mgmt_over_wmi_tx_purge(ar);
4162 cancel_work_sync(&ar->offchan_tx_work);
4163 cancel_work_sync(&ar->wmi_mgmt_tx_work);
4166 void ath10k_halt(struct ath10k *ar)
4168 struct ath10k_vif *arvif;
4170 lockdep_assert_held(&ar->conf_mutex);
4172 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4173 ar->filter_flags = 0;
4174 ar->monitor = false;
4175 ar->monitor_arvif = NULL;
4177 if (ar->monitor_started)
4178 ath10k_monitor_stop(ar);
4180 ar->monitor_started = false;
4181 ar->tx_paused = 0;
4183 ath10k_scan_finish(ar);
4184 ath10k_peer_cleanup_all(ar);
4185 ath10k_core_stop(ar);
4186 ath10k_hif_power_down(ar);
4188 spin_lock_bh(&ar->data_lock);
4189 list_for_each_entry(arvif, &ar->arvifs, list)
4190 ath10k_mac_vif_beacon_cleanup(arvif);
4191 spin_unlock_bh(&ar->data_lock);
4194 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4196 struct ath10k *ar = hw->priv;
4198 mutex_lock(&ar->conf_mutex);
4200 *tx_ant = ar->cfg_tx_chainmask;
4201 *rx_ant = ar->cfg_rx_chainmask;
4203 mutex_unlock(&ar->conf_mutex);
4205 return 0;
4208 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4210 /* It is not clear that allowing gaps in chainmask
4211 * is helpful. Probably it will not do what user
4212 * is hoping for, so warn in that case.
4214 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4215 return;
4217 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
4218 dbg, cm);
4221 static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4223 int nsts = ar->vht_cap_info;
4225 nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4226 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4228 /* If firmware does not deliver to host number of space-time
4229 * streams supported, assume it support up to 4 BF STS and return
4230 * the value for VHT CAP: nsts-1)
4232 if (nsts == 0)
4233 return 3;
4235 return nsts;
4238 static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4240 int sound_dim = ar->vht_cap_info;
4242 sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4243 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4245 /* If the sounding dimension is not advertised by the firmware,
4246 * let's use a default value of 1
4248 if (sound_dim == 0)
4249 return 1;
4251 return sound_dim;
4254 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4256 struct ieee80211_sta_vht_cap vht_cap = {0};
4257 u16 mcs_map;
4258 u32 val;
4259 int i;
4261 vht_cap.vht_supported = 1;
4262 vht_cap.cap = ar->vht_cap_info;
4264 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4265 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4266 val = ath10k_mac_get_vht_cap_bf_sts(ar);
4267 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4268 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4270 vht_cap.cap |= val;
4273 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4274 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4275 val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4276 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4277 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4279 vht_cap.cap |= val;
4282 mcs_map = 0;
4283 for (i = 0; i < 8; i++) {
4284 if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4285 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4286 else
4287 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4290 if (ar->cfg_tx_chainmask <= 1)
4291 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4293 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4294 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4296 return vht_cap;
4299 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4301 int i;
4302 struct ieee80211_sta_ht_cap ht_cap = {0};
4304 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4305 return ht_cap;
4307 ht_cap.ht_supported = 1;
4308 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4309 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4310 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4311 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4312 ht_cap.cap |=
4313 WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
4315 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4316 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4318 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4319 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4321 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4322 u32 smps;
4324 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4325 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4327 ht_cap.cap |= smps;
4330 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
4331 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4333 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4334 u32 stbc;
4336 stbc = ar->ht_cap_info;
4337 stbc &= WMI_HT_CAP_RX_STBC;
4338 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4339 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4340 stbc &= IEEE80211_HT_CAP_RX_STBC;
4342 ht_cap.cap |= stbc;
4345 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4346 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4348 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4349 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4351 /* max AMSDU is implicitly taken from vht_cap_info */
4352 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4353 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4355 for (i = 0; i < ar->num_rf_chains; i++) {
4356 if (ar->cfg_rx_chainmask & BIT(i))
4357 ht_cap.mcs.rx_mask[i] = 0xFF;
4360 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4362 return ht_cap;
4365 static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4367 struct ieee80211_supported_band *band;
4368 struct ieee80211_sta_vht_cap vht_cap;
4369 struct ieee80211_sta_ht_cap ht_cap;
4371 ht_cap = ath10k_get_ht_cap(ar);
4372 vht_cap = ath10k_create_vht_cap(ar);
4374 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4375 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4376 band->ht_cap = ht_cap;
4378 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4379 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4380 band->ht_cap = ht_cap;
4381 band->vht_cap = vht_cap;
4385 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4387 int ret;
4389 lockdep_assert_held(&ar->conf_mutex);
4391 ath10k_check_chain_mask(ar, tx_ant, "tx");
4392 ath10k_check_chain_mask(ar, rx_ant, "rx");
4394 ar->cfg_tx_chainmask = tx_ant;
4395 ar->cfg_rx_chainmask = rx_ant;
4397 if ((ar->state != ATH10K_STATE_ON) &&
4398 (ar->state != ATH10K_STATE_RESTARTED))
4399 return 0;
4401 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
4402 tx_ant);
4403 if (ret) {
4404 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
4405 ret, tx_ant);
4406 return ret;
4409 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
4410 rx_ant);
4411 if (ret) {
4412 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
4413 ret, rx_ant);
4414 return ret;
4417 /* Reload HT/VHT capability */
4418 ath10k_mac_setup_ht_vht_cap(ar);
4420 return 0;
4423 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4425 struct ath10k *ar = hw->priv;
4426 int ret;
4428 mutex_lock(&ar->conf_mutex);
4429 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
4430 mutex_unlock(&ar->conf_mutex);
4431 return ret;
4434 static int ath10k_start(struct ieee80211_hw *hw)
4436 struct ath10k *ar = hw->priv;
4437 u32 param;
4438 int ret = 0;
4441 * This makes sense only when restarting hw. It is harmless to call
4442 * unconditionally. This is necessary to make sure no HTT/WMI tx
4443 * commands will be submitted while restarting.
4445 ath10k_drain_tx(ar);
4447 mutex_lock(&ar->conf_mutex);
4449 switch (ar->state) {
4450 case ATH10K_STATE_OFF:
4451 ar->state = ATH10K_STATE_ON;
4452 break;
4453 case ATH10K_STATE_RESTARTING:
4454 ath10k_halt(ar);
4455 ar->state = ATH10K_STATE_RESTARTED;
4456 break;
4457 case ATH10K_STATE_ON:
4458 case ATH10K_STATE_RESTARTED:
4459 case ATH10K_STATE_WEDGED:
4460 WARN_ON(1);
4461 ret = -EINVAL;
4462 goto err;
4463 case ATH10K_STATE_UTF:
4464 ret = -EBUSY;
4465 goto err;
4468 ret = ath10k_hif_power_up(ar);
4469 if (ret) {
4470 ath10k_err(ar, "Could not init hif: %d\n", ret);
4471 goto err_off;
4474 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
4475 &ar->normal_mode_fw);
4476 if (ret) {
4477 ath10k_err(ar, "Could not init core: %d\n", ret);
4478 goto err_power_down;
4481 param = ar->wmi.pdev_param->pmf_qos;
4482 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4483 if (ret) {
4484 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
4485 goto err_core_stop;
4488 param = ar->wmi.pdev_param->dynamic_bw;
4489 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4490 if (ret) {
4491 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
4492 goto err_core_stop;
4495 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
4496 ret = ath10k_wmi_adaptive_qcs(ar, true);
4497 if (ret) {
4498 ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
4499 ret);
4500 goto err_core_stop;
4504 if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
4505 param = ar->wmi.pdev_param->burst_enable;
4506 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4507 if (ret) {
4508 ath10k_warn(ar, "failed to disable burst: %d\n", ret);
4509 goto err_core_stop;
4513 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4516 * By default FW set ARP frames ac to voice (6). In that case ARP
4517 * exchange is not working properly for UAPSD enabled AP. ARP requests
4518 * which arrives with access category 0 are processed by network stack
4519 * and send back with access category 0, but FW changes access category
4520 * to 6. Set ARP frames access category to best effort (0) solves
4521 * this problem.
4524 param = ar->wmi.pdev_param->arp_ac_override;
4525 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4526 if (ret) {
4527 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
4528 ret);
4529 goto err_core_stop;
4532 if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
4533 ar->running_fw->fw_file.fw_features)) {
4534 ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
4535 WMI_CCA_DETECT_LEVEL_AUTO,
4536 WMI_CCA_DETECT_MARGIN_AUTO);
4537 if (ret) {
4538 ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
4539 ret);
4540 goto err_core_stop;
4544 param = ar->wmi.pdev_param->ani_enable;
4545 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4546 if (ret) {
4547 ath10k_warn(ar, "failed to enable ani by default: %d\n",
4548 ret);
4549 goto err_core_stop;
4552 ar->ani_enabled = true;
4554 if (ath10k_peer_stats_enabled(ar)) {
4555 param = ar->wmi.pdev_param->peer_stats_update_period;
4556 ret = ath10k_wmi_pdev_set_param(ar, param,
4557 PEER_DEFAULT_STATS_UPDATE_PERIOD);
4558 if (ret) {
4559 ath10k_warn(ar,
4560 "failed to set peer stats period : %d\n",
4561 ret);
4562 goto err_core_stop;
4566 param = ar->wmi.pdev_param->enable_btcoex;
4567 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
4568 test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
4569 ar->running_fw->fw_file.fw_features)) {
4570 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4571 if (ret) {
4572 ath10k_warn(ar,
4573 "failed to set btcoex param: %d\n", ret);
4574 goto err_core_stop;
4576 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
4579 ar->num_started_vdevs = 0;
4580 ath10k_regd_update(ar);
4582 ath10k_spectral_start(ar);
4583 ath10k_thermal_set_throttling(ar);
4585 mutex_unlock(&ar->conf_mutex);
4586 return 0;
4588 err_core_stop:
4589 ath10k_core_stop(ar);
4591 err_power_down:
4592 ath10k_hif_power_down(ar);
4594 err_off:
4595 ar->state = ATH10K_STATE_OFF;
4597 err:
4598 mutex_unlock(&ar->conf_mutex);
4599 return ret;
4602 static void ath10k_stop(struct ieee80211_hw *hw)
4604 struct ath10k *ar = hw->priv;
4606 ath10k_drain_tx(ar);
4608 mutex_lock(&ar->conf_mutex);
4609 if (ar->state != ATH10K_STATE_OFF) {
4610 ath10k_halt(ar);
4611 ar->state = ATH10K_STATE_OFF;
4613 mutex_unlock(&ar->conf_mutex);
4615 cancel_delayed_work_sync(&ar->scan.timeout);
4616 cancel_work_sync(&ar->restart_work);
4619 static int ath10k_config_ps(struct ath10k *ar)
4621 struct ath10k_vif *arvif;
4622 int ret = 0;
4624 lockdep_assert_held(&ar->conf_mutex);
4626 list_for_each_entry(arvif, &ar->arvifs, list) {
4627 ret = ath10k_mac_vif_setup_ps(arvif);
4628 if (ret) {
4629 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
4630 break;
4634 return ret;
4637 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
4639 int ret;
4640 u32 param;
4642 lockdep_assert_held(&ar->conf_mutex);
4644 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
4646 param = ar->wmi.pdev_param->txpower_limit2g;
4647 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4648 if (ret) {
4649 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
4650 txpower, ret);
4651 return ret;
4654 param = ar->wmi.pdev_param->txpower_limit5g;
4655 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4656 if (ret) {
4657 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
4658 txpower, ret);
4659 return ret;
4662 return 0;
4665 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
4667 struct ath10k_vif *arvif;
4668 int ret, txpower = -1;
4670 lockdep_assert_held(&ar->conf_mutex);
4672 list_for_each_entry(arvif, &ar->arvifs, list) {
4673 WARN_ON(arvif->txpower < 0);
4675 if (txpower == -1)
4676 txpower = arvif->txpower;
4677 else
4678 txpower = min(txpower, arvif->txpower);
4681 if (WARN_ON(txpower == -1))
4682 return -EINVAL;
4684 ret = ath10k_mac_txpower_setup(ar, txpower);
4685 if (ret) {
4686 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
4687 txpower, ret);
4688 return ret;
4691 return 0;
4694 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
4696 struct ath10k *ar = hw->priv;
4697 struct ieee80211_conf *conf = &hw->conf;
4698 int ret = 0;
4700 mutex_lock(&ar->conf_mutex);
4702 if (changed & IEEE80211_CONF_CHANGE_PS)
4703 ath10k_config_ps(ar);
4705 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
4706 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
4707 ret = ath10k_monitor_recalc(ar);
4708 if (ret)
4709 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4712 mutex_unlock(&ar->conf_mutex);
4713 return ret;
4716 static u32 get_nss_from_chainmask(u16 chain_mask)
4718 if ((chain_mask & 0xf) == 0xf)
4719 return 4;
4720 else if ((chain_mask & 0x7) == 0x7)
4721 return 3;
4722 else if ((chain_mask & 0x3) == 0x3)
4723 return 2;
4724 return 1;
4727 static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
4729 u32 value = 0;
4730 struct ath10k *ar = arvif->ar;
4731 int nsts;
4732 int sound_dim;
4734 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
4735 return 0;
4737 nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
4738 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4739 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
4740 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4742 sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4743 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4744 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
4745 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4747 if (!value)
4748 return 0;
4750 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
4751 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4753 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
4754 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
4755 WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
4757 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
4758 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4760 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
4761 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
4762 WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
4764 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4765 ar->wmi.vdev_param->txbf, value);
4769 * TODO:
4770 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
4771 * because we will send mgmt frames without CCK. This requirement
4772 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
4773 * in the TX packet.
4775 static int ath10k_add_interface(struct ieee80211_hw *hw,
4776 struct ieee80211_vif *vif)
4778 struct ath10k *ar = hw->priv;
4779 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4780 struct ath10k_peer *peer;
4781 enum wmi_sta_powersave_param param;
4782 int ret = 0;
4783 u32 value;
4784 int bit;
4785 int i;
4786 u32 vdev_param;
4788 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4790 mutex_lock(&ar->conf_mutex);
4792 memset(arvif, 0, sizeof(*arvif));
4793 ath10k_mac_txq_init(vif->txq);
4795 arvif->ar = ar;
4796 arvif->vif = vif;
4798 INIT_LIST_HEAD(&arvif->list);
4799 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
4800 INIT_DELAYED_WORK(&arvif->connection_loss_work,
4801 ath10k_mac_vif_sta_connection_loss_work);
4803 for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4804 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4805 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4806 sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4807 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4808 sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4811 if (ar->num_peers >= ar->max_num_peers) {
4812 ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
4813 ret = -ENOBUFS;
4814 goto err;
4817 if (ar->free_vdev_map == 0) {
4818 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
4819 ret = -EBUSY;
4820 goto err;
4822 bit = __ffs64(ar->free_vdev_map);
4824 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
4825 bit, ar->free_vdev_map);
4827 arvif->vdev_id = bit;
4828 arvif->vdev_subtype =
4829 ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
4831 switch (vif->type) {
4832 case NL80211_IFTYPE_P2P_DEVICE:
4833 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4834 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4835 (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
4836 break;
4837 case NL80211_IFTYPE_UNSPECIFIED:
4838 case NL80211_IFTYPE_STATION:
4839 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4840 if (vif->p2p)
4841 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4842 (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
4843 break;
4844 case NL80211_IFTYPE_ADHOC:
4845 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
4846 break;
4847 case NL80211_IFTYPE_MESH_POINT:
4848 if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
4849 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4850 (ar, WMI_VDEV_SUBTYPE_MESH_11S);
4851 } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4852 ret = -EINVAL;
4853 ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
4854 goto err;
4856 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4857 break;
4858 case NL80211_IFTYPE_AP:
4859 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4861 if (vif->p2p)
4862 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4863 (ar, WMI_VDEV_SUBTYPE_P2P_GO);
4864 break;
4865 case NL80211_IFTYPE_MONITOR:
4866 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4867 break;
4868 default:
4869 WARN_ON(1);
4870 break;
4873 /* Using vdev_id as queue number will make it very easy to do per-vif
4874 * tx queue locking. This shouldn't wrap due to interface combinations
4875 * but do a modulo for correctness sake and prevent using offchannel tx
4876 * queues for regular vif tx.
4878 vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4879 for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4880 vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4882 /* Some firmware revisions don't wait for beacon tx completion before
4883 * sending another SWBA event. This could lead to hardware using old
4884 * (freed) beacon data in some cases, e.g. tx credit starvation
4885 * combined with missed TBTT. This is very very rare.
4887 * On non-IOMMU-enabled hosts this could be a possible security issue
4888 * because hw could beacon some random data on the air. On
4889 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
4890 * device would crash.
4892 * Since there are no beacon tx completions (implicit nor explicit)
4893 * propagated to host the only workaround for this is to allocate a
4894 * DMA-coherent buffer for a lifetime of a vif and use it for all
4895 * beacon tx commands. Worst case for this approach is some beacons may
4896 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
4898 if (vif->type == NL80211_IFTYPE_ADHOC ||
4899 vif->type == NL80211_IFTYPE_MESH_POINT ||
4900 vif->type == NL80211_IFTYPE_AP) {
4901 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
4902 IEEE80211_MAX_FRAME_LEN,
4903 &arvif->beacon_paddr,
4904 GFP_ATOMIC);
4905 if (!arvif->beacon_buf) {
4906 ret = -ENOMEM;
4907 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
4908 ret);
4909 goto err;
4912 if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
4913 arvif->nohwcrypt = true;
4915 if (arvif->nohwcrypt &&
4916 !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4917 ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
4918 goto err;
4921 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
4922 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4923 arvif->beacon_buf ? "single-buf" : "per-skb");
4925 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
4926 arvif->vdev_subtype, vif->addr);
4927 if (ret) {
4928 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
4929 arvif->vdev_id, ret);
4930 goto err;
4933 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
4934 list_add(&arvif->list, &ar->arvifs);
4936 /* It makes no sense to have firmware do keepalives. mac80211 already
4937 * takes care of this with idle connection polling.
4939 ret = ath10k_mac_vif_disable_keepalive(arvif);
4940 if (ret) {
4941 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
4942 arvif->vdev_id, ret);
4943 goto err_vdev_delete;
4946 arvif->def_wep_key_idx = -1;
4948 vdev_param = ar->wmi.vdev_param->tx_encap_type;
4949 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4950 ATH10K_HW_TXRX_NATIVE_WIFI);
4951 /* 10.X firmware does not support this VDEV parameter. Do not warn */
4952 if (ret && ret != -EOPNOTSUPP) {
4953 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
4954 arvif->vdev_id, ret);
4955 goto err_vdev_delete;
4958 /* Configuring number of spatial stream for monitor interface is causing
4959 * target assert in qca9888 and qca6174.
4961 if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
4962 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4964 vdev_param = ar->wmi.vdev_param->nss;
4965 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4966 nss);
4967 if (ret) {
4968 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
4969 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
4970 ret);
4971 goto err_vdev_delete;
4975 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
4976 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
4977 ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
4978 vif->addr, WMI_PEER_TYPE_DEFAULT);
4979 if (ret) {
4980 ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
4981 arvif->vdev_id, ret);
4982 goto err_vdev_delete;
4985 spin_lock_bh(&ar->data_lock);
4987 peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
4988 if (!peer) {
4989 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
4990 vif->addr, arvif->vdev_id);
4991 spin_unlock_bh(&ar->data_lock);
4992 ret = -ENOENT;
4993 goto err_peer_delete;
4996 arvif->peer_id = find_first_bit(peer->peer_ids,
4997 ATH10K_MAX_NUM_PEER_IDS);
4999 spin_unlock_bh(&ar->data_lock);
5000 } else {
5001 arvif->peer_id = HTT_INVALID_PEERID;
5004 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5005 ret = ath10k_mac_set_kickout(arvif);
5006 if (ret) {
5007 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5008 arvif->vdev_id, ret);
5009 goto err_peer_delete;
5013 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5014 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5015 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5016 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5017 param, value);
5018 if (ret) {
5019 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5020 arvif->vdev_id, ret);
5021 goto err_peer_delete;
5024 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5025 if (ret) {
5026 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5027 arvif->vdev_id, ret);
5028 goto err_peer_delete;
5031 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5032 if (ret) {
5033 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5034 arvif->vdev_id, ret);
5035 goto err_peer_delete;
5039 ret = ath10k_mac_set_txbf_conf(arvif);
5040 if (ret) {
5041 ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5042 arvif->vdev_id, ret);
5043 goto err_peer_delete;
5046 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5047 if (ret) {
5048 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5049 arvif->vdev_id, ret);
5050 goto err_peer_delete;
5053 arvif->txpower = vif->bss_conf.txpower;
5054 ret = ath10k_mac_txpower_recalc(ar);
5055 if (ret) {
5056 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5057 goto err_peer_delete;
5060 if (vif->type == NL80211_IFTYPE_MONITOR) {
5061 ar->monitor_arvif = arvif;
5062 ret = ath10k_monitor_recalc(ar);
5063 if (ret) {
5064 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5065 goto err_peer_delete;
5069 spin_lock_bh(&ar->htt.tx_lock);
5070 if (!ar->tx_paused)
5071 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5072 spin_unlock_bh(&ar->htt.tx_lock);
5074 mutex_unlock(&ar->conf_mutex);
5075 return 0;
5077 err_peer_delete:
5078 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5079 arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
5080 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5082 err_vdev_delete:
5083 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5084 ar->free_vdev_map |= 1LL << arvif->vdev_id;
5085 list_del(&arvif->list);
5087 err:
5088 if (arvif->beacon_buf) {
5089 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5090 arvif->beacon_buf, arvif->beacon_paddr);
5091 arvif->beacon_buf = NULL;
5094 mutex_unlock(&ar->conf_mutex);
5096 return ret;
5099 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5101 int i;
5103 for (i = 0; i < BITS_PER_LONG; i++)
5104 ath10k_mac_vif_tx_unlock(arvif, i);
5107 static void ath10k_remove_interface(struct ieee80211_hw *hw,
5108 struct ieee80211_vif *vif)
5110 struct ath10k *ar = hw->priv;
5111 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5112 struct ath10k_peer *peer;
5113 int ret;
5114 int i;
5116 cancel_work_sync(&arvif->ap_csa_work);
5117 cancel_delayed_work_sync(&arvif->connection_loss_work);
5119 mutex_lock(&ar->conf_mutex);
5121 spin_lock_bh(&ar->data_lock);
5122 ath10k_mac_vif_beacon_cleanup(arvif);
5123 spin_unlock_bh(&ar->data_lock);
5125 ret = ath10k_spectral_vif_stop(arvif);
5126 if (ret)
5127 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5128 arvif->vdev_id, ret);
5130 ar->free_vdev_map |= 1LL << arvif->vdev_id;
5131 list_del(&arvif->list);
5133 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5134 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5135 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5136 vif->addr);
5137 if (ret)
5138 ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5139 arvif->vdev_id, ret);
5141 kfree(arvif->u.ap.noa_data);
5144 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5145 arvif->vdev_id);
5147 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5148 if (ret)
5149 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5150 arvif->vdev_id, ret);
5152 /* Some firmware revisions don't notify host about self-peer removal
5153 * until after associated vdev is deleted.
5155 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5156 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5157 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5158 vif->addr);
5159 if (ret)
5160 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5161 arvif->vdev_id, ret);
5163 spin_lock_bh(&ar->data_lock);
5164 ar->num_peers--;
5165 spin_unlock_bh(&ar->data_lock);
5168 spin_lock_bh(&ar->data_lock);
5169 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5170 peer = ar->peer_map[i];
5171 if (!peer)
5172 continue;
5174 if (peer->vif == vif) {
5175 ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5176 vif->addr, arvif->vdev_id);
5177 peer->vif = NULL;
5180 spin_unlock_bh(&ar->data_lock);
5182 ath10k_peer_cleanup(ar, arvif->vdev_id);
5183 ath10k_mac_txq_unref(ar, vif->txq);
5185 if (vif->type == NL80211_IFTYPE_MONITOR) {
5186 ar->monitor_arvif = NULL;
5187 ret = ath10k_monitor_recalc(ar);
5188 if (ret)
5189 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5192 spin_lock_bh(&ar->htt.tx_lock);
5193 ath10k_mac_vif_tx_unlock_all(arvif);
5194 spin_unlock_bh(&ar->htt.tx_lock);
5196 ath10k_mac_txq_unref(ar, vif->txq);
5198 mutex_unlock(&ar->conf_mutex);
5202 * FIXME: Has to be verified.
5204 #define SUPPORTED_FILTERS \
5205 (FIF_ALLMULTI | \
5206 FIF_CONTROL | \
5207 FIF_PSPOLL | \
5208 FIF_OTHER_BSS | \
5209 FIF_BCN_PRBRESP_PROMISC | \
5210 FIF_PROBE_REQ | \
5211 FIF_FCSFAIL)
5213 static void ath10k_configure_filter(struct ieee80211_hw *hw,
5214 unsigned int changed_flags,
5215 unsigned int *total_flags,
5216 u64 multicast)
5218 struct ath10k *ar = hw->priv;
5219 int ret;
5221 mutex_lock(&ar->conf_mutex);
5223 changed_flags &= SUPPORTED_FILTERS;
5224 *total_flags &= SUPPORTED_FILTERS;
5225 ar->filter_flags = *total_flags;
5227 ret = ath10k_monitor_recalc(ar);
5228 if (ret)
5229 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5231 mutex_unlock(&ar->conf_mutex);
5234 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
5235 struct ieee80211_vif *vif,
5236 struct ieee80211_bss_conf *info,
5237 u32 changed)
5239 struct ath10k *ar = hw->priv;
5240 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5241 int ret = 0;
5242 u32 vdev_param, pdev_param, slottime, preamble;
5244 mutex_lock(&ar->conf_mutex);
5246 if (changed & BSS_CHANGED_IBSS)
5247 ath10k_control_ibss(arvif, info, vif->addr);
5249 if (changed & BSS_CHANGED_BEACON_INT) {
5250 arvif->beacon_interval = info->beacon_int;
5251 vdev_param = ar->wmi.vdev_param->beacon_interval;
5252 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5253 arvif->beacon_interval);
5254 ath10k_dbg(ar, ATH10K_DBG_MAC,
5255 "mac vdev %d beacon_interval %d\n",
5256 arvif->vdev_id, arvif->beacon_interval);
5258 if (ret)
5259 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
5260 arvif->vdev_id, ret);
5263 if (changed & BSS_CHANGED_BEACON) {
5264 ath10k_dbg(ar, ATH10K_DBG_MAC,
5265 "vdev %d set beacon tx mode to staggered\n",
5266 arvif->vdev_id);
5268 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
5269 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5270 WMI_BEACON_STAGGERED_MODE);
5271 if (ret)
5272 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
5273 arvif->vdev_id, ret);
5275 ret = ath10k_mac_setup_bcn_tmpl(arvif);
5276 if (ret)
5277 ath10k_warn(ar, "failed to update beacon template: %d\n",
5278 ret);
5280 if (ieee80211_vif_is_mesh(vif)) {
5281 /* mesh doesn't use SSID but firmware needs it */
5282 strncpy(arvif->u.ap.ssid, "mesh",
5283 sizeof(arvif->u.ap.ssid));
5284 arvif->u.ap.ssid_len = 4;
5288 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
5289 ret = ath10k_mac_setup_prb_tmpl(arvif);
5290 if (ret)
5291 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
5292 arvif->vdev_id, ret);
5295 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5296 arvif->dtim_period = info->dtim_period;
5298 ath10k_dbg(ar, ATH10K_DBG_MAC,
5299 "mac vdev %d dtim_period %d\n",
5300 arvif->vdev_id, arvif->dtim_period);
5302 vdev_param = ar->wmi.vdev_param->dtim_period;
5303 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5304 arvif->dtim_period);
5305 if (ret)
5306 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
5307 arvif->vdev_id, ret);
5310 if (changed & BSS_CHANGED_SSID &&
5311 vif->type == NL80211_IFTYPE_AP) {
5312 arvif->u.ap.ssid_len = info->ssid_len;
5313 if (info->ssid_len)
5314 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
5315 arvif->u.ap.hidden_ssid = info->hidden_ssid;
5318 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
5319 ether_addr_copy(arvif->bssid, info->bssid);
5321 if (changed & BSS_CHANGED_BEACON_ENABLED)
5322 ath10k_control_beaconing(arvif, info);
5324 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
5325 arvif->use_cts_prot = info->use_cts_prot;
5326 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
5327 arvif->vdev_id, info->use_cts_prot);
5329 ret = ath10k_recalc_rtscts_prot(arvif);
5330 if (ret)
5331 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
5332 arvif->vdev_id, ret);
5334 vdev_param = ar->wmi.vdev_param->protection_mode;
5335 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5336 info->use_cts_prot ? 1 : 0);
5337 if (ret)
5338 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
5339 info->use_cts_prot, arvif->vdev_id, ret);
5342 if (changed & BSS_CHANGED_ERP_SLOT) {
5343 if (info->use_short_slot)
5344 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
5346 else
5347 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
5349 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
5350 arvif->vdev_id, slottime);
5352 vdev_param = ar->wmi.vdev_param->slot_time;
5353 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5354 slottime);
5355 if (ret)
5356 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
5357 arvif->vdev_id, ret);
5360 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5361 if (info->use_short_preamble)
5362 preamble = WMI_VDEV_PREAMBLE_SHORT;
5363 else
5364 preamble = WMI_VDEV_PREAMBLE_LONG;
5366 ath10k_dbg(ar, ATH10K_DBG_MAC,
5367 "mac vdev %d preamble %dn",
5368 arvif->vdev_id, preamble);
5370 vdev_param = ar->wmi.vdev_param->preamble;
5371 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5372 preamble);
5373 if (ret)
5374 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
5375 arvif->vdev_id, ret);
5378 if (changed & BSS_CHANGED_ASSOC) {
5379 if (info->assoc) {
5380 /* Workaround: Make sure monitor vdev is not running
5381 * when associating to prevent some firmware revisions
5382 * (e.g. 10.1 and 10.2) from crashing.
5384 if (ar->monitor_started)
5385 ath10k_monitor_stop(ar);
5386 ath10k_bss_assoc(hw, vif, info);
5387 ath10k_monitor_recalc(ar);
5388 } else {
5389 ath10k_bss_disassoc(hw, vif);
5393 if (changed & BSS_CHANGED_TXPOWER) {
5394 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
5395 arvif->vdev_id, info->txpower);
5397 arvif->txpower = info->txpower;
5398 ret = ath10k_mac_txpower_recalc(ar);
5399 if (ret)
5400 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5403 if (changed & BSS_CHANGED_PS) {
5404 arvif->ps = vif->bss_conf.ps;
5406 ret = ath10k_config_ps(ar);
5407 if (ret)
5408 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
5409 arvif->vdev_id, ret);
5412 mutex_unlock(&ar->conf_mutex);
5415 static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
5417 struct ath10k *ar = hw->priv;
5419 /* This function should never be called if setting the coverage class
5420 * is not supported on this hardware.
5422 if (!ar->hw_params.hw_ops->set_coverage_class) {
5423 WARN_ON_ONCE(1);
5424 return;
5426 ar->hw_params.hw_ops->set_coverage_class(ar, value);
5429 static int ath10k_hw_scan(struct ieee80211_hw *hw,
5430 struct ieee80211_vif *vif,
5431 struct ieee80211_scan_request *hw_req)
5433 struct ath10k *ar = hw->priv;
5434 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5435 struct cfg80211_scan_request *req = &hw_req->req;
5436 struct wmi_start_scan_arg arg;
5437 int ret = 0;
5438 int i;
5440 mutex_lock(&ar->conf_mutex);
5442 spin_lock_bh(&ar->data_lock);
5443 switch (ar->scan.state) {
5444 case ATH10K_SCAN_IDLE:
5445 reinit_completion(&ar->scan.started);
5446 reinit_completion(&ar->scan.completed);
5447 ar->scan.state = ATH10K_SCAN_STARTING;
5448 ar->scan.is_roc = false;
5449 ar->scan.vdev_id = arvif->vdev_id;
5450 ret = 0;
5451 break;
5452 case ATH10K_SCAN_STARTING:
5453 case ATH10K_SCAN_RUNNING:
5454 case ATH10K_SCAN_ABORTING:
5455 ret = -EBUSY;
5456 break;
5458 spin_unlock_bh(&ar->data_lock);
5460 if (ret)
5461 goto exit;
5463 memset(&arg, 0, sizeof(arg));
5464 ath10k_wmi_start_scan_init(ar, &arg);
5465 arg.vdev_id = arvif->vdev_id;
5466 arg.scan_id = ATH10K_SCAN_ID;
5468 if (req->ie_len) {
5469 arg.ie_len = req->ie_len;
5470 memcpy(arg.ie, req->ie, arg.ie_len);
5473 if (req->n_ssids) {
5474 arg.n_ssids = req->n_ssids;
5475 for (i = 0; i < arg.n_ssids; i++) {
5476 arg.ssids[i].len = req->ssids[i].ssid_len;
5477 arg.ssids[i].ssid = req->ssids[i].ssid;
5479 } else {
5480 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5483 if (req->n_channels) {
5484 arg.n_channels = req->n_channels;
5485 for (i = 0; i < arg.n_channels; i++)
5486 arg.channels[i] = req->channels[i]->center_freq;
5489 ret = ath10k_start_scan(ar, &arg);
5490 if (ret) {
5491 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5492 spin_lock_bh(&ar->data_lock);
5493 ar->scan.state = ATH10K_SCAN_IDLE;
5494 spin_unlock_bh(&ar->data_lock);
5497 /* Add a 200ms margin to account for event/command processing */
5498 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
5499 msecs_to_jiffies(arg.max_scan_time +
5500 200));
5502 exit:
5503 mutex_unlock(&ar->conf_mutex);
5504 return ret;
5507 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
5508 struct ieee80211_vif *vif)
5510 struct ath10k *ar = hw->priv;
5512 mutex_lock(&ar->conf_mutex);
5513 ath10k_scan_abort(ar);
5514 mutex_unlock(&ar->conf_mutex);
5516 cancel_delayed_work_sync(&ar->scan.timeout);
5519 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
5520 struct ath10k_vif *arvif,
5521 enum set_key_cmd cmd,
5522 struct ieee80211_key_conf *key)
5524 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
5525 int ret;
5527 /* 10.1 firmware branch requires default key index to be set to group
5528 * key index after installing it. Otherwise FW/HW Txes corrupted
5529 * frames with multi-vif APs. This is not required for main firmware
5530 * branch (e.g. 636).
5532 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
5534 * FIXME: It remains unknown if this is required for multi-vif STA
5535 * interfaces on 10.1.
5538 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5539 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
5540 return;
5542 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
5543 return;
5545 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
5546 return;
5548 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5549 return;
5551 if (cmd != SET_KEY)
5552 return;
5554 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5555 key->keyidx);
5556 if (ret)
5557 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
5558 arvif->vdev_id, ret);
5561 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5562 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5563 struct ieee80211_key_conf *key)
5565 struct ath10k *ar = hw->priv;
5566 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5567 struct ath10k_peer *peer;
5568 const u8 *peer_addr;
5569 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
5570 key->cipher == WLAN_CIPHER_SUITE_WEP104;
5571 int ret = 0;
5572 int ret2;
5573 u32 flags = 0;
5574 u32 flags2;
5576 /* this one needs to be done in software */
5577 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
5578 return 1;
5580 if (arvif->nohwcrypt)
5581 return 1;
5583 if (key->keyidx > WMI_MAX_KEY_INDEX)
5584 return -ENOSPC;
5586 mutex_lock(&ar->conf_mutex);
5588 if (sta)
5589 peer_addr = sta->addr;
5590 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
5591 peer_addr = vif->bss_conf.bssid;
5592 else
5593 peer_addr = vif->addr;
5595 key->hw_key_idx = key->keyidx;
5597 if (is_wep) {
5598 if (cmd == SET_KEY)
5599 arvif->wep_keys[key->keyidx] = key;
5600 else
5601 arvif->wep_keys[key->keyidx] = NULL;
5604 /* the peer should not disappear in mid-way (unless FW goes awry) since
5605 * we already hold conf_mutex. we just make sure its there now. */
5606 spin_lock_bh(&ar->data_lock);
5607 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5608 spin_unlock_bh(&ar->data_lock);
5610 if (!peer) {
5611 if (cmd == SET_KEY) {
5612 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5613 peer_addr);
5614 ret = -EOPNOTSUPP;
5615 goto exit;
5616 } else {
5617 /* if the peer doesn't exist there is no key to disable
5618 * anymore */
5619 goto exit;
5623 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5624 flags |= WMI_KEY_PAIRWISE;
5625 else
5626 flags |= WMI_KEY_GROUP;
5628 if (is_wep) {
5629 if (cmd == DISABLE_KEY)
5630 ath10k_clear_vdev_key(arvif, key);
5632 /* When WEP keys are uploaded it's possible that there are
5633 * stations associated already (e.g. when merging) without any
5634 * keys. Static WEP needs an explicit per-peer key upload.
5636 if (vif->type == NL80211_IFTYPE_ADHOC &&
5637 cmd == SET_KEY)
5638 ath10k_mac_vif_update_wep_key(arvif, key);
5640 /* 802.1x never sets the def_wep_key_idx so each set_key()
5641 * call changes default tx key.
5643 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
5644 * after first set_key().
5646 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
5647 flags |= WMI_KEY_TX_USAGE;
5650 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
5651 if (ret) {
5652 WARN_ON(ret > 0);
5653 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
5654 arvif->vdev_id, peer_addr, ret);
5655 goto exit;
5658 /* mac80211 sets static WEP keys as groupwise while firmware requires
5659 * them to be installed twice as both pairwise and groupwise.
5661 if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
5662 flags2 = flags;
5663 flags2 &= ~WMI_KEY_GROUP;
5664 flags2 |= WMI_KEY_PAIRWISE;
5666 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
5667 if (ret) {
5668 WARN_ON(ret > 0);
5669 ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
5670 arvif->vdev_id, peer_addr, ret);
5671 ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
5672 peer_addr, flags);
5673 if (ret2) {
5674 WARN_ON(ret2 > 0);
5675 ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
5676 arvif->vdev_id, peer_addr, ret2);
5678 goto exit;
5682 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
5684 spin_lock_bh(&ar->data_lock);
5685 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5686 if (peer && cmd == SET_KEY)
5687 peer->keys[key->keyidx] = key;
5688 else if (peer && cmd == DISABLE_KEY)
5689 peer->keys[key->keyidx] = NULL;
5690 else if (peer == NULL)
5691 /* impossible unless FW goes crazy */
5692 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5693 spin_unlock_bh(&ar->data_lock);
5695 exit:
5696 mutex_unlock(&ar->conf_mutex);
5697 return ret;
5700 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
5701 struct ieee80211_vif *vif,
5702 int keyidx)
5704 struct ath10k *ar = hw->priv;
5705 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5706 int ret;
5708 mutex_lock(&arvif->ar->conf_mutex);
5710 if (arvif->ar->state != ATH10K_STATE_ON)
5711 goto unlock;
5713 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
5714 arvif->vdev_id, keyidx);
5716 ret = ath10k_wmi_vdev_set_param(arvif->ar,
5717 arvif->vdev_id,
5718 arvif->ar->wmi.vdev_param->def_keyid,
5719 keyidx);
5721 if (ret) {
5722 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
5723 arvif->vdev_id,
5724 ret);
5725 goto unlock;
5728 arvif->def_wep_key_idx = keyidx;
5730 unlock:
5731 mutex_unlock(&arvif->ar->conf_mutex);
5734 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
5736 struct ath10k *ar;
5737 struct ath10k_vif *arvif;
5738 struct ath10k_sta *arsta;
5739 struct ieee80211_sta *sta;
5740 struct cfg80211_chan_def def;
5741 enum nl80211_band band;
5742 const u8 *ht_mcs_mask;
5743 const u16 *vht_mcs_mask;
5744 u32 changed, bw, nss, smps;
5745 int err;
5747 arsta = container_of(wk, struct ath10k_sta, update_wk);
5748 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
5749 arvif = arsta->arvif;
5750 ar = arvif->ar;
5752 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
5753 return;
5755 band = def.chan->band;
5756 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
5757 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
5759 spin_lock_bh(&ar->data_lock);
5761 changed = arsta->changed;
5762 arsta->changed = 0;
5764 bw = arsta->bw;
5765 nss = arsta->nss;
5766 smps = arsta->smps;
5768 spin_unlock_bh(&ar->data_lock);
5770 mutex_lock(&ar->conf_mutex);
5772 nss = max_t(u32, 1, nss);
5773 nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
5774 ath10k_mac_max_vht_nss(vht_mcs_mask)));
5776 if (changed & IEEE80211_RC_BW_CHANGED) {
5777 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
5778 sta->addr, bw);
5780 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5781 WMI_PEER_CHAN_WIDTH, bw);
5782 if (err)
5783 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
5784 sta->addr, bw, err);
5787 if (changed & IEEE80211_RC_NSS_CHANGED) {
5788 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
5789 sta->addr, nss);
5791 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5792 WMI_PEER_NSS, nss);
5793 if (err)
5794 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
5795 sta->addr, nss, err);
5798 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5799 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
5800 sta->addr, smps);
5802 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5803 WMI_PEER_SMPS_STATE, smps);
5804 if (err)
5805 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
5806 sta->addr, smps, err);
5809 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
5810 changed & IEEE80211_RC_NSS_CHANGED) {
5811 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
5812 sta->addr);
5814 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
5815 if (err)
5816 ath10k_warn(ar, "failed to reassociate station: %pM\n",
5817 sta->addr);
5820 mutex_unlock(&ar->conf_mutex);
5823 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
5824 struct ieee80211_sta *sta)
5826 struct ath10k *ar = arvif->ar;
5828 lockdep_assert_held(&ar->conf_mutex);
5830 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5831 return 0;
5833 if (ar->num_stations >= ar->max_num_stations)
5834 return -ENOBUFS;
5836 ar->num_stations++;
5838 return 0;
5841 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
5842 struct ieee80211_sta *sta)
5844 struct ath10k *ar = arvif->ar;
5846 lockdep_assert_held(&ar->conf_mutex);
5848 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5849 return;
5851 ar->num_stations--;
5854 struct ath10k_mac_tdls_iter_data {
5855 u32 num_tdls_stations;
5856 struct ieee80211_vif *curr_vif;
5859 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
5860 struct ieee80211_sta *sta)
5862 struct ath10k_mac_tdls_iter_data *iter_data = data;
5863 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5864 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
5866 if (sta->tdls && sta_vif == iter_data->curr_vif)
5867 iter_data->num_tdls_stations++;
5870 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
5871 struct ieee80211_vif *vif)
5873 struct ath10k_mac_tdls_iter_data data = {};
5875 data.curr_vif = vif;
5877 ieee80211_iterate_stations_atomic(hw,
5878 ath10k_mac_tdls_vif_stations_count_iter,
5879 &data);
5880 return data.num_tdls_stations;
5883 static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
5884 struct ieee80211_vif *vif)
5886 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5887 int *num_tdls_vifs = data;
5889 if (vif->type != NL80211_IFTYPE_STATION)
5890 return;
5892 if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
5893 (*num_tdls_vifs)++;
5896 static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
5898 int num_tdls_vifs = 0;
5900 ieee80211_iterate_active_interfaces_atomic(hw,
5901 IEEE80211_IFACE_ITER_NORMAL,
5902 ath10k_mac_tdls_vifs_count_iter,
5903 &num_tdls_vifs);
5904 return num_tdls_vifs;
5907 static int ath10k_sta_state(struct ieee80211_hw *hw,
5908 struct ieee80211_vif *vif,
5909 struct ieee80211_sta *sta,
5910 enum ieee80211_sta_state old_state,
5911 enum ieee80211_sta_state new_state)
5913 struct ath10k *ar = hw->priv;
5914 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5915 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5916 struct ath10k_peer *peer;
5917 int ret = 0;
5918 int i;
5920 if (old_state == IEEE80211_STA_NOTEXIST &&
5921 new_state == IEEE80211_STA_NONE) {
5922 memset(arsta, 0, sizeof(*arsta));
5923 arsta->arvif = arvif;
5924 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
5926 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
5927 ath10k_mac_txq_init(sta->txq[i]);
5930 /* cancel must be done outside the mutex to avoid deadlock */
5931 if ((old_state == IEEE80211_STA_NONE &&
5932 new_state == IEEE80211_STA_NOTEXIST))
5933 cancel_work_sync(&arsta->update_wk);
5935 mutex_lock(&ar->conf_mutex);
5937 if (old_state == IEEE80211_STA_NOTEXIST &&
5938 new_state == IEEE80211_STA_NONE) {
5940 * New station addition.
5942 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
5943 u32 num_tdls_stations;
5944 u32 num_tdls_vifs;
5946 ath10k_dbg(ar, ATH10K_DBG_MAC,
5947 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
5948 arvif->vdev_id, sta->addr,
5949 ar->num_stations + 1, ar->max_num_stations,
5950 ar->num_peers + 1, ar->max_num_peers);
5952 ret = ath10k_mac_inc_num_stations(arvif, sta);
5953 if (ret) {
5954 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
5955 ar->max_num_stations);
5956 goto exit;
5959 if (sta->tdls)
5960 peer_type = WMI_PEER_TYPE_TDLS;
5962 ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
5963 sta->addr, peer_type);
5964 if (ret) {
5965 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
5966 sta->addr, arvif->vdev_id, ret);
5967 ath10k_mac_dec_num_stations(arvif, sta);
5968 goto exit;
5971 spin_lock_bh(&ar->data_lock);
5973 peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
5974 if (!peer) {
5975 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5976 vif->addr, arvif->vdev_id);
5977 spin_unlock_bh(&ar->data_lock);
5978 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
5979 ath10k_mac_dec_num_stations(arvif, sta);
5980 ret = -ENOENT;
5981 goto exit;
5984 arsta->peer_id = find_first_bit(peer->peer_ids,
5985 ATH10K_MAX_NUM_PEER_IDS);
5987 spin_unlock_bh(&ar->data_lock);
5989 if (!sta->tdls)
5990 goto exit;
5992 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
5993 num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
5995 if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
5996 num_tdls_stations == 0) {
5997 ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
5998 arvif->vdev_id, ar->max_num_tdls_vdevs);
5999 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6000 ath10k_mac_dec_num_stations(arvif, sta);
6001 ret = -ENOBUFS;
6002 goto exit;
6005 if (num_tdls_stations == 0) {
6006 /* This is the first tdls peer in current vif */
6007 enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
6009 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6010 state);
6011 if (ret) {
6012 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6013 arvif->vdev_id, ret);
6014 ath10k_peer_delete(ar, arvif->vdev_id,
6015 sta->addr);
6016 ath10k_mac_dec_num_stations(arvif, sta);
6017 goto exit;
6021 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6022 WMI_TDLS_PEER_STATE_PEERING);
6023 if (ret) {
6024 ath10k_warn(ar,
6025 "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
6026 sta->addr, arvif->vdev_id, ret);
6027 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6028 ath10k_mac_dec_num_stations(arvif, sta);
6030 if (num_tdls_stations != 0)
6031 goto exit;
6032 ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6033 WMI_TDLS_DISABLE);
6035 } else if ((old_state == IEEE80211_STA_NONE &&
6036 new_state == IEEE80211_STA_NOTEXIST)) {
6038 * Existing station deletion.
6040 ath10k_dbg(ar, ATH10K_DBG_MAC,
6041 "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
6042 arvif->vdev_id, sta->addr, sta);
6044 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6045 if (ret)
6046 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
6047 sta->addr, arvif->vdev_id, ret);
6049 ath10k_mac_dec_num_stations(arvif, sta);
6051 spin_lock_bh(&ar->data_lock);
6052 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
6053 peer = ar->peer_map[i];
6054 if (!peer)
6055 continue;
6057 if (peer->sta == sta) {
6058 ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
6059 sta->addr, peer, i, arvif->vdev_id);
6060 peer->sta = NULL;
6062 /* Clean up the peer object as well since we
6063 * must have failed to do this above.
6065 list_del(&peer->list);
6066 ar->peer_map[i] = NULL;
6067 kfree(peer);
6068 ar->num_peers--;
6071 spin_unlock_bh(&ar->data_lock);
6073 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
6074 ath10k_mac_txq_unref(ar, sta->txq[i]);
6076 if (!sta->tdls)
6077 goto exit;
6079 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
6080 goto exit;
6082 /* This was the last tdls peer in current vif */
6083 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6084 WMI_TDLS_DISABLE);
6085 if (ret) {
6086 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6087 arvif->vdev_id, ret);
6089 } else if (old_state == IEEE80211_STA_AUTH &&
6090 new_state == IEEE80211_STA_ASSOC &&
6091 (vif->type == NL80211_IFTYPE_AP ||
6092 vif->type == NL80211_IFTYPE_MESH_POINT ||
6093 vif->type == NL80211_IFTYPE_ADHOC)) {
6095 * New association.
6097 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
6098 sta->addr);
6100 ret = ath10k_station_assoc(ar, vif, sta, false);
6101 if (ret)
6102 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
6103 sta->addr, arvif->vdev_id, ret);
6104 } else if (old_state == IEEE80211_STA_ASSOC &&
6105 new_state == IEEE80211_STA_AUTHORIZED &&
6106 sta->tdls) {
6108 * Tdls station authorized.
6110 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
6111 sta->addr);
6113 ret = ath10k_station_assoc(ar, vif, sta, false);
6114 if (ret) {
6115 ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
6116 sta->addr, arvif->vdev_id, ret);
6117 goto exit;
6120 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6121 WMI_TDLS_PEER_STATE_CONNECTED);
6122 if (ret)
6123 ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
6124 sta->addr, arvif->vdev_id, ret);
6125 } else if (old_state == IEEE80211_STA_ASSOC &&
6126 new_state == IEEE80211_STA_AUTH &&
6127 (vif->type == NL80211_IFTYPE_AP ||
6128 vif->type == NL80211_IFTYPE_MESH_POINT ||
6129 vif->type == NL80211_IFTYPE_ADHOC)) {
6131 * Disassociation.
6133 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
6134 sta->addr);
6136 ret = ath10k_station_disassoc(ar, vif, sta);
6137 if (ret)
6138 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
6139 sta->addr, arvif->vdev_id, ret);
6141 exit:
6142 mutex_unlock(&ar->conf_mutex);
6143 return ret;
6146 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
6147 u16 ac, bool enable)
6149 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6150 struct wmi_sta_uapsd_auto_trig_arg arg = {};
6151 u32 prio = 0, acc = 0;
6152 u32 value = 0;
6153 int ret = 0;
6155 lockdep_assert_held(&ar->conf_mutex);
6157 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
6158 return 0;
6160 switch (ac) {
6161 case IEEE80211_AC_VO:
6162 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
6163 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
6164 prio = 7;
6165 acc = 3;
6166 break;
6167 case IEEE80211_AC_VI:
6168 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
6169 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
6170 prio = 5;
6171 acc = 2;
6172 break;
6173 case IEEE80211_AC_BE:
6174 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
6175 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
6176 prio = 2;
6177 acc = 1;
6178 break;
6179 case IEEE80211_AC_BK:
6180 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
6181 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
6182 prio = 0;
6183 acc = 0;
6184 break;
6187 if (enable)
6188 arvif->u.sta.uapsd |= value;
6189 else
6190 arvif->u.sta.uapsd &= ~value;
6192 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6193 WMI_STA_PS_PARAM_UAPSD,
6194 arvif->u.sta.uapsd);
6195 if (ret) {
6196 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
6197 goto exit;
6200 if (arvif->u.sta.uapsd)
6201 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
6202 else
6203 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6205 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6206 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
6207 value);
6208 if (ret)
6209 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
6211 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
6212 if (ret) {
6213 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
6214 arvif->vdev_id, ret);
6215 return ret;
6218 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
6219 if (ret) {
6220 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
6221 arvif->vdev_id, ret);
6222 return ret;
6225 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
6226 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
6227 /* Only userspace can make an educated decision when to send
6228 * trigger frame. The following effectively disables u-UAPSD
6229 * autotrigger in firmware (which is enabled by default
6230 * provided the autotrigger service is available).
6233 arg.wmm_ac = acc;
6234 arg.user_priority = prio;
6235 arg.service_interval = 0;
6236 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6237 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6239 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
6240 arvif->bssid, &arg, 1);
6241 if (ret) {
6242 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
6243 ret);
6244 return ret;
6248 exit:
6249 return ret;
6252 static int ath10k_conf_tx(struct ieee80211_hw *hw,
6253 struct ieee80211_vif *vif, u16 ac,
6254 const struct ieee80211_tx_queue_params *params)
6256 struct ath10k *ar = hw->priv;
6257 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6258 struct wmi_wmm_params_arg *p = NULL;
6259 int ret;
6261 mutex_lock(&ar->conf_mutex);
6263 switch (ac) {
6264 case IEEE80211_AC_VO:
6265 p = &arvif->wmm_params.ac_vo;
6266 break;
6267 case IEEE80211_AC_VI:
6268 p = &arvif->wmm_params.ac_vi;
6269 break;
6270 case IEEE80211_AC_BE:
6271 p = &arvif->wmm_params.ac_be;
6272 break;
6273 case IEEE80211_AC_BK:
6274 p = &arvif->wmm_params.ac_bk;
6275 break;
6278 if (WARN_ON(!p)) {
6279 ret = -EINVAL;
6280 goto exit;
6283 p->cwmin = params->cw_min;
6284 p->cwmax = params->cw_max;
6285 p->aifs = params->aifs;
6288 * The channel time duration programmed in the HW is in absolute
6289 * microseconds, while mac80211 gives the txop in units of
6290 * 32 microseconds.
6292 p->txop = params->txop * 32;
6294 if (ar->wmi.ops->gen_vdev_wmm_conf) {
6295 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
6296 &arvif->wmm_params);
6297 if (ret) {
6298 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
6299 arvif->vdev_id, ret);
6300 goto exit;
6302 } else {
6303 /* This won't work well with multi-interface cases but it's
6304 * better than nothing.
6306 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
6307 if (ret) {
6308 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
6309 goto exit;
6313 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
6314 if (ret)
6315 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
6317 exit:
6318 mutex_unlock(&ar->conf_mutex);
6319 return ret;
6322 #define ATH10K_ROC_TIMEOUT_HZ (2 * HZ)
6324 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
6325 struct ieee80211_vif *vif,
6326 struct ieee80211_channel *chan,
6327 int duration,
6328 enum ieee80211_roc_type type)
6330 struct ath10k *ar = hw->priv;
6331 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6332 struct wmi_start_scan_arg arg;
6333 int ret = 0;
6334 u32 scan_time_msec;
6336 mutex_lock(&ar->conf_mutex);
6338 spin_lock_bh(&ar->data_lock);
6339 switch (ar->scan.state) {
6340 case ATH10K_SCAN_IDLE:
6341 reinit_completion(&ar->scan.started);
6342 reinit_completion(&ar->scan.completed);
6343 reinit_completion(&ar->scan.on_channel);
6344 ar->scan.state = ATH10K_SCAN_STARTING;
6345 ar->scan.is_roc = true;
6346 ar->scan.vdev_id = arvif->vdev_id;
6347 ar->scan.roc_freq = chan->center_freq;
6348 ar->scan.roc_notify = true;
6349 ret = 0;
6350 break;
6351 case ATH10K_SCAN_STARTING:
6352 case ATH10K_SCAN_RUNNING:
6353 case ATH10K_SCAN_ABORTING:
6354 ret = -EBUSY;
6355 break;
6357 spin_unlock_bh(&ar->data_lock);
6359 if (ret)
6360 goto exit;
6362 scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
6364 memset(&arg, 0, sizeof(arg));
6365 ath10k_wmi_start_scan_init(ar, &arg);
6366 arg.vdev_id = arvif->vdev_id;
6367 arg.scan_id = ATH10K_SCAN_ID;
6368 arg.n_channels = 1;
6369 arg.channels[0] = chan->center_freq;
6370 arg.dwell_time_active = scan_time_msec;
6371 arg.dwell_time_passive = scan_time_msec;
6372 arg.max_scan_time = scan_time_msec;
6373 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6374 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
6375 arg.burst_duration_ms = duration;
6377 ret = ath10k_start_scan(ar, &arg);
6378 if (ret) {
6379 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
6380 spin_lock_bh(&ar->data_lock);
6381 ar->scan.state = ATH10K_SCAN_IDLE;
6382 spin_unlock_bh(&ar->data_lock);
6383 goto exit;
6386 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
6387 if (ret == 0) {
6388 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
6390 ret = ath10k_scan_stop(ar);
6391 if (ret)
6392 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
6394 ret = -ETIMEDOUT;
6395 goto exit;
6398 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6399 msecs_to_jiffies(duration));
6401 ret = 0;
6402 exit:
6403 mutex_unlock(&ar->conf_mutex);
6404 return ret;
6407 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
6409 struct ath10k *ar = hw->priv;
6411 mutex_lock(&ar->conf_mutex);
6413 spin_lock_bh(&ar->data_lock);
6414 ar->scan.roc_notify = false;
6415 spin_unlock_bh(&ar->data_lock);
6417 ath10k_scan_abort(ar);
6419 mutex_unlock(&ar->conf_mutex);
6421 cancel_delayed_work_sync(&ar->scan.timeout);
6423 return 0;
6427 * Both RTS and Fragmentation threshold are interface-specific
6428 * in ath10k, but device-specific in mac80211.
6431 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6433 struct ath10k *ar = hw->priv;
6434 struct ath10k_vif *arvif;
6435 int ret = 0;
6437 mutex_lock(&ar->conf_mutex);
6438 list_for_each_entry(arvif, &ar->arvifs, list) {
6439 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
6440 arvif->vdev_id, value);
6442 ret = ath10k_mac_set_rts(arvif, value);
6443 if (ret) {
6444 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
6445 arvif->vdev_id, ret);
6446 break;
6449 mutex_unlock(&ar->conf_mutex);
6451 return ret;
6454 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6456 /* Even though there's a WMI enum for fragmentation threshold no known
6457 * firmware actually implements it. Moreover it is not possible to rely
6458 * frame fragmentation to mac80211 because firmware clears the "more
6459 * fragments" bit in frame control making it impossible for remote
6460 * devices to reassemble frames.
6462 * Hence implement a dummy callback just to say fragmentation isn't
6463 * supported. This effectively prevents mac80211 from doing frame
6464 * fragmentation in software.
6466 return -EOPNOTSUPP;
6469 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6470 u32 queues, bool drop)
6472 struct ath10k *ar = hw->priv;
6473 bool skip;
6474 long time_left;
6476 /* mac80211 doesn't care if we really xmit queued frames or not
6477 * we'll collect those frames either way if we stop/delete vdevs */
6478 if (drop)
6479 return;
6481 mutex_lock(&ar->conf_mutex);
6483 if (ar->state == ATH10K_STATE_WEDGED)
6484 goto skip;
6486 time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
6487 bool empty;
6489 spin_lock_bh(&ar->htt.tx_lock);
6490 empty = (ar->htt.num_pending_tx == 0);
6491 spin_unlock_bh(&ar->htt.tx_lock);
6493 skip = (ar->state == ATH10K_STATE_WEDGED) ||
6494 test_bit(ATH10K_FLAG_CRASH_FLUSH,
6495 &ar->dev_flags);
6497 (empty || skip);
6498 }), ATH10K_FLUSH_TIMEOUT_HZ);
6500 if (time_left == 0 || skip)
6501 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
6502 skip, ar->state, time_left);
6504 skip:
6505 mutex_unlock(&ar->conf_mutex);
6508 /* TODO: Implement this function properly
6509 * For now it is needed to reply to Probe Requests in IBSS mode.
6510 * Propably we need this information from FW.
6512 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
6514 return 1;
6517 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
6518 enum ieee80211_reconfig_type reconfig_type)
6520 struct ath10k *ar = hw->priv;
6522 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6523 return;
6525 mutex_lock(&ar->conf_mutex);
6527 /* If device failed to restart it will be in a different state, e.g.
6528 * ATH10K_STATE_WEDGED */
6529 if (ar->state == ATH10K_STATE_RESTARTED) {
6530 ath10k_info(ar, "device successfully recovered\n");
6531 ar->state = ATH10K_STATE_ON;
6532 ieee80211_wake_queues(ar->hw);
6535 mutex_unlock(&ar->conf_mutex);
6538 static void
6539 ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
6540 struct ieee80211_channel *channel)
6542 int ret;
6543 enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ_CLEAR;
6545 lockdep_assert_held(&ar->conf_mutex);
6547 if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
6548 (ar->rx_channel != channel))
6549 return;
6551 if (ar->scan.state != ATH10K_SCAN_IDLE) {
6552 ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
6553 return;
6556 reinit_completion(&ar->bss_survey_done);
6558 ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
6559 if (ret) {
6560 ath10k_warn(ar, "failed to send pdev bss chan info request\n");
6561 return;
6564 ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
6565 if (!ret) {
6566 ath10k_warn(ar, "bss channel survey timed out\n");
6567 return;
6571 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
6572 struct survey_info *survey)
6574 struct ath10k *ar = hw->priv;
6575 struct ieee80211_supported_band *sband;
6576 struct survey_info *ar_survey = &ar->survey[idx];
6577 int ret = 0;
6579 mutex_lock(&ar->conf_mutex);
6581 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
6582 if (sband && idx >= sband->n_channels) {
6583 idx -= sband->n_channels;
6584 sband = NULL;
6587 if (!sband)
6588 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
6590 if (!sband || idx >= sband->n_channels) {
6591 ret = -ENOENT;
6592 goto exit;
6595 ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
6597 spin_lock_bh(&ar->data_lock);
6598 memcpy(survey, ar_survey, sizeof(*survey));
6599 spin_unlock_bh(&ar->data_lock);
6601 survey->channel = &sband->channels[idx];
6603 if (ar->rx_channel == survey->channel)
6604 survey->filled |= SURVEY_INFO_IN_USE;
6606 exit:
6607 mutex_unlock(&ar->conf_mutex);
6608 return ret;
6611 static bool
6612 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6613 enum nl80211_band band,
6614 const struct cfg80211_bitrate_mask *mask)
6616 int num_rates = 0;
6617 int i;
6619 num_rates += hweight32(mask->control[band].legacy);
6621 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6622 num_rates += hweight8(mask->control[band].ht_mcs[i]);
6624 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6625 num_rates += hweight16(mask->control[band].vht_mcs[i]);
6627 return num_rates == 1;
6630 static bool
6631 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
6632 enum nl80211_band band,
6633 const struct cfg80211_bitrate_mask *mask,
6634 int *nss)
6636 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6637 u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6638 u8 ht_nss_mask = 0;
6639 u8 vht_nss_mask = 0;
6640 int i;
6642 if (mask->control[band].legacy)
6643 return false;
6645 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6646 if (mask->control[band].ht_mcs[i] == 0)
6647 continue;
6648 else if (mask->control[band].ht_mcs[i] ==
6649 sband->ht_cap.mcs.rx_mask[i])
6650 ht_nss_mask |= BIT(i);
6651 else
6652 return false;
6655 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6656 if (mask->control[band].vht_mcs[i] == 0)
6657 continue;
6658 else if (mask->control[band].vht_mcs[i] ==
6659 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6660 vht_nss_mask |= BIT(i);
6661 else
6662 return false;
6665 if (ht_nss_mask != vht_nss_mask)
6666 return false;
6668 if (ht_nss_mask == 0)
6669 return false;
6671 if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6672 return false;
6674 *nss = fls(ht_nss_mask);
6676 return true;
6679 static int
6680 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6681 enum nl80211_band band,
6682 const struct cfg80211_bitrate_mask *mask,
6683 u8 *rate, u8 *nss)
6685 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6686 int rate_idx;
6687 int i;
6688 u16 bitrate;
6689 u8 preamble;
6690 u8 hw_rate;
6692 if (hweight32(mask->control[band].legacy) == 1) {
6693 rate_idx = ffs(mask->control[band].legacy) - 1;
6695 hw_rate = sband->bitrates[rate_idx].hw_value;
6696 bitrate = sband->bitrates[rate_idx].bitrate;
6698 if (ath10k_mac_bitrate_is_cck(bitrate))
6699 preamble = WMI_RATE_PREAMBLE_CCK;
6700 else
6701 preamble = WMI_RATE_PREAMBLE_OFDM;
6703 *nss = 1;
6704 *rate = preamble << 6 |
6705 (*nss - 1) << 4 |
6706 hw_rate << 0;
6708 return 0;
6711 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6712 if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6713 *nss = i + 1;
6714 *rate = WMI_RATE_PREAMBLE_HT << 6 |
6715 (*nss - 1) << 4 |
6716 (ffs(mask->control[band].ht_mcs[i]) - 1);
6718 return 0;
6722 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6723 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6724 *nss = i + 1;
6725 *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6726 (*nss - 1) << 4 |
6727 (ffs(mask->control[band].vht_mcs[i]) - 1);
6729 return 0;
6733 return -EINVAL;
6736 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
6737 u8 rate, u8 nss, u8 sgi, u8 ldpc)
6739 struct ath10k *ar = arvif->ar;
6740 u32 vdev_param;
6741 int ret;
6743 lockdep_assert_held(&ar->conf_mutex);
6745 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
6746 arvif->vdev_id, rate, nss, sgi);
6748 vdev_param = ar->wmi.vdev_param->fixed_rate;
6749 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
6750 if (ret) {
6751 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
6752 rate, ret);
6753 return ret;
6756 vdev_param = ar->wmi.vdev_param->nss;
6757 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
6758 if (ret) {
6759 ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
6760 return ret;
6763 vdev_param = ar->wmi.vdev_param->sgi;
6764 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
6765 if (ret) {
6766 ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
6767 return ret;
6770 vdev_param = ar->wmi.vdev_param->ldpc;
6771 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
6772 if (ret) {
6773 ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
6774 return ret;
6777 return 0;
6780 static bool
6781 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
6782 enum nl80211_band band,
6783 const struct cfg80211_bitrate_mask *mask)
6785 int i;
6786 u16 vht_mcs;
6788 /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
6789 * to express all VHT MCS rate masks. Effectively only the following
6790 * ranges can be used: none, 0-7, 0-8 and 0-9.
6792 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6793 vht_mcs = mask->control[band].vht_mcs[i];
6795 switch (vht_mcs) {
6796 case 0:
6797 case BIT(8) - 1:
6798 case BIT(9) - 1:
6799 case BIT(10) - 1:
6800 break;
6801 default:
6802 ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
6803 return false;
6807 return true;
6810 static void ath10k_mac_set_bitrate_mask_iter(void *data,
6811 struct ieee80211_sta *sta)
6813 struct ath10k_vif *arvif = data;
6814 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6815 struct ath10k *ar = arvif->ar;
6817 if (arsta->arvif != arvif)
6818 return;
6820 spin_lock_bh(&ar->data_lock);
6821 arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6822 spin_unlock_bh(&ar->data_lock);
6824 ieee80211_queue_work(ar->hw, &arsta->update_wk);
6827 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6828 struct ieee80211_vif *vif,
6829 const struct cfg80211_bitrate_mask *mask)
6831 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6832 struct cfg80211_chan_def def;
6833 struct ath10k *ar = arvif->ar;
6834 enum nl80211_band band;
6835 const u8 *ht_mcs_mask;
6836 const u16 *vht_mcs_mask;
6837 u8 rate;
6838 u8 nss;
6839 u8 sgi;
6840 u8 ldpc;
6841 int single_nss;
6842 int ret;
6844 if (ath10k_mac_vif_chan(vif, &def))
6845 return -EPERM;
6847 band = def.chan->band;
6848 ht_mcs_mask = mask->control[band].ht_mcs;
6849 vht_mcs_mask = mask->control[band].vht_mcs;
6850 ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6852 sgi = mask->control[band].gi;
6853 if (sgi == NL80211_TXRATE_FORCE_LGI)
6854 return -EINVAL;
6856 if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
6857 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6858 &rate, &nss);
6859 if (ret) {
6860 ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
6861 arvif->vdev_id, ret);
6862 return ret;
6864 } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6865 &single_nss)) {
6866 rate = WMI_FIXED_RATE_NONE;
6867 nss = single_nss;
6868 } else {
6869 rate = WMI_FIXED_RATE_NONE;
6870 nss = min(ar->num_rf_chains,
6871 max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6872 ath10k_mac_max_vht_nss(vht_mcs_mask)));
6874 if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
6875 return -EINVAL;
6877 mutex_lock(&ar->conf_mutex);
6879 arvif->bitrate_mask = *mask;
6880 ieee80211_iterate_stations_atomic(ar->hw,
6881 ath10k_mac_set_bitrate_mask_iter,
6882 arvif);
6884 mutex_unlock(&ar->conf_mutex);
6887 mutex_lock(&ar->conf_mutex);
6889 ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6890 if (ret) {
6891 ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
6892 arvif->vdev_id, ret);
6893 goto exit;
6896 exit:
6897 mutex_unlock(&ar->conf_mutex);
6899 return ret;
6902 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
6903 struct ieee80211_vif *vif,
6904 struct ieee80211_sta *sta,
6905 u32 changed)
6907 struct ath10k *ar = hw->priv;
6908 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6909 u32 bw, smps;
6911 spin_lock_bh(&ar->data_lock);
6913 ath10k_dbg(ar, ATH10K_DBG_MAC,
6914 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
6915 sta->addr, changed, sta->bandwidth, sta->rx_nss,
6916 sta->smps_mode);
6918 if (changed & IEEE80211_RC_BW_CHANGED) {
6919 bw = WMI_PEER_CHWIDTH_20MHZ;
6921 switch (sta->bandwidth) {
6922 case IEEE80211_STA_RX_BW_20:
6923 bw = WMI_PEER_CHWIDTH_20MHZ;
6924 break;
6925 case IEEE80211_STA_RX_BW_40:
6926 bw = WMI_PEER_CHWIDTH_40MHZ;
6927 break;
6928 case IEEE80211_STA_RX_BW_80:
6929 bw = WMI_PEER_CHWIDTH_80MHZ;
6930 break;
6931 case IEEE80211_STA_RX_BW_160:
6932 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
6933 sta->bandwidth, sta->addr);
6934 bw = WMI_PEER_CHWIDTH_20MHZ;
6935 break;
6938 arsta->bw = bw;
6941 if (changed & IEEE80211_RC_NSS_CHANGED)
6942 arsta->nss = sta->rx_nss;
6944 if (changed & IEEE80211_RC_SMPS_CHANGED) {
6945 smps = WMI_PEER_SMPS_PS_NONE;
6947 switch (sta->smps_mode) {
6948 case IEEE80211_SMPS_AUTOMATIC:
6949 case IEEE80211_SMPS_OFF:
6950 smps = WMI_PEER_SMPS_PS_NONE;
6951 break;
6952 case IEEE80211_SMPS_STATIC:
6953 smps = WMI_PEER_SMPS_STATIC;
6954 break;
6955 case IEEE80211_SMPS_DYNAMIC:
6956 smps = WMI_PEER_SMPS_DYNAMIC;
6957 break;
6958 case IEEE80211_SMPS_NUM_MODES:
6959 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
6960 sta->smps_mode, sta->addr);
6961 smps = WMI_PEER_SMPS_PS_NONE;
6962 break;
6965 arsta->smps = smps;
6968 arsta->changed |= changed;
6970 spin_unlock_bh(&ar->data_lock);
6972 ieee80211_queue_work(hw, &arsta->update_wk);
6975 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
6978 * FIXME: Return 0 for time being. Need to figure out whether FW
6979 * has the API to fetch 64-bit local TSF
6982 return 0;
6985 static void ath10k_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6986 u64 tsf)
6988 struct ath10k *ar = hw->priv;
6989 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6990 u32 tsf_offset, vdev_param = ar->wmi.vdev_param->set_tsf;
6991 int ret;
6993 /* Workaround:
6995 * Given tsf argument is entire TSF value, but firmware accepts
6996 * only TSF offset to current TSF.
6998 * get_tsf function is used to get offset value, however since
6999 * ath10k_get_tsf is not implemented properly, it will return 0 always.
7000 * Luckily all the caller functions to set_tsf, as of now, also rely on
7001 * get_tsf function to get entire tsf value such get_tsf() + tsf_delta,
7002 * final tsf offset value to firmware will be arithmetically correct.
7004 tsf_offset = tsf - ath10k_get_tsf(hw, vif);
7005 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
7006 vdev_param, tsf_offset);
7007 if (ret && ret != -EOPNOTSUPP)
7008 ath10k_warn(ar, "failed to set tsf offset: %d\n", ret);
7011 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
7012 struct ieee80211_vif *vif,
7013 struct ieee80211_ampdu_params *params)
7015 struct ath10k *ar = hw->priv;
7016 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7017 struct ieee80211_sta *sta = params->sta;
7018 enum ieee80211_ampdu_mlme_action action = params->action;
7019 u16 tid = params->tid;
7021 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
7022 arvif->vdev_id, sta->addr, tid, action);
7024 switch (action) {
7025 case IEEE80211_AMPDU_RX_START:
7026 case IEEE80211_AMPDU_RX_STOP:
7027 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
7028 * creation/removal. Do we need to verify this?
7030 return 0;
7031 case IEEE80211_AMPDU_TX_START:
7032 case IEEE80211_AMPDU_TX_STOP_CONT:
7033 case IEEE80211_AMPDU_TX_STOP_FLUSH:
7034 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7035 case IEEE80211_AMPDU_TX_OPERATIONAL:
7036 /* Firmware offloads Tx aggregation entirely so deny mac80211
7037 * Tx aggregation requests.
7039 return -EOPNOTSUPP;
7042 return -EINVAL;
7045 static void
7046 ath10k_mac_update_rx_channel(struct ath10k *ar,
7047 struct ieee80211_chanctx_conf *ctx,
7048 struct ieee80211_vif_chanctx_switch *vifs,
7049 int n_vifs)
7051 struct cfg80211_chan_def *def = NULL;
7053 /* Both locks are required because ar->rx_channel is modified. This
7054 * allows readers to hold either lock.
7056 lockdep_assert_held(&ar->conf_mutex);
7057 lockdep_assert_held(&ar->data_lock);
7059 WARN_ON(ctx && vifs);
7060 WARN_ON(vifs && n_vifs != 1);
7062 /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
7063 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
7064 * ppdu on Rx may reduce performance on low-end systems. It should be
7065 * possible to make tables/hashmaps to speed the lookup up (be vary of
7066 * cpu data cache lines though regarding sizes) but to keep the initial
7067 * implementation simple and less intrusive fallback to the slow lookup
7068 * only for multi-channel cases. Single-channel cases will remain to
7069 * use the old channel derival and thus performance should not be
7070 * affected much.
7072 rcu_read_lock();
7073 if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
7074 ieee80211_iter_chan_contexts_atomic(ar->hw,
7075 ath10k_mac_get_any_chandef_iter,
7076 &def);
7078 if (vifs)
7079 def = &vifs[0].new_ctx->def;
7081 ar->rx_channel = def->chan;
7082 } else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
7083 (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
7084 /* During driver restart due to firmware assert, since mac80211
7085 * already has valid channel context for given radio, channel
7086 * context iteration return num_chanctx > 0. So fix rx_channel
7087 * when restart is in progress.
7089 ar->rx_channel = ctx->def.chan;
7090 } else {
7091 ar->rx_channel = NULL;
7093 rcu_read_unlock();
7096 static void
7097 ath10k_mac_update_vif_chan(struct ath10k *ar,
7098 struct ieee80211_vif_chanctx_switch *vifs,
7099 int n_vifs)
7101 struct ath10k_vif *arvif;
7102 int ret;
7103 int i;
7105 lockdep_assert_held(&ar->conf_mutex);
7107 /* First stop monitor interface. Some FW versions crash if there's a
7108 * lone monitor interface.
7110 if (ar->monitor_started)
7111 ath10k_monitor_stop(ar);
7113 for (i = 0; i < n_vifs; i++) {
7114 arvif = ath10k_vif_to_arvif(vifs[i].vif);
7116 ath10k_dbg(ar, ATH10K_DBG_MAC,
7117 "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
7118 arvif->vdev_id,
7119 vifs[i].old_ctx->def.chan->center_freq,
7120 vifs[i].new_ctx->def.chan->center_freq,
7121 vifs[i].old_ctx->def.width,
7122 vifs[i].new_ctx->def.width);
7124 if (WARN_ON(!arvif->is_started))
7125 continue;
7127 if (WARN_ON(!arvif->is_up))
7128 continue;
7130 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7131 if (ret) {
7132 ath10k_warn(ar, "failed to down vdev %d: %d\n",
7133 arvif->vdev_id, ret);
7134 continue;
7138 /* All relevant vdevs are downed and associated channel resources
7139 * should be available for the channel switch now.
7142 spin_lock_bh(&ar->data_lock);
7143 ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
7144 spin_unlock_bh(&ar->data_lock);
7146 for (i = 0; i < n_vifs; i++) {
7147 arvif = ath10k_vif_to_arvif(vifs[i].vif);
7149 if (WARN_ON(!arvif->is_started))
7150 continue;
7152 if (WARN_ON(!arvif->is_up))
7153 continue;
7155 ret = ath10k_mac_setup_bcn_tmpl(arvif);
7156 if (ret)
7157 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
7158 ret);
7160 ret = ath10k_mac_setup_prb_tmpl(arvif);
7161 if (ret)
7162 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
7163 ret);
7165 ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
7166 if (ret) {
7167 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
7168 arvif->vdev_id, ret);
7169 continue;
7172 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7173 arvif->bssid);
7174 if (ret) {
7175 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
7176 arvif->vdev_id, ret);
7177 continue;
7181 ath10k_monitor_recalc(ar);
7184 static int
7185 ath10k_mac_op_add_chanctx(struct ieee80211_hw *hw,
7186 struct ieee80211_chanctx_conf *ctx)
7188 struct ath10k *ar = hw->priv;
7190 ath10k_dbg(ar, ATH10K_DBG_MAC,
7191 "mac chanctx add freq %hu width %d ptr %pK\n",
7192 ctx->def.chan->center_freq, ctx->def.width, ctx);
7194 mutex_lock(&ar->conf_mutex);
7196 spin_lock_bh(&ar->data_lock);
7197 ath10k_mac_update_rx_channel(ar, ctx, NULL, 0);
7198 spin_unlock_bh(&ar->data_lock);
7200 ath10k_recalc_radar_detection(ar);
7201 ath10k_monitor_recalc(ar);
7203 mutex_unlock(&ar->conf_mutex);
7205 return 0;
7208 static void
7209 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
7210 struct ieee80211_chanctx_conf *ctx)
7212 struct ath10k *ar = hw->priv;
7214 ath10k_dbg(ar, ATH10K_DBG_MAC,
7215 "mac chanctx remove freq %hu width %d ptr %pK\n",
7216 ctx->def.chan->center_freq, ctx->def.width, ctx);
7218 mutex_lock(&ar->conf_mutex);
7220 spin_lock_bh(&ar->data_lock);
7221 ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
7222 spin_unlock_bh(&ar->data_lock);
7224 ath10k_recalc_radar_detection(ar);
7225 ath10k_monitor_recalc(ar);
7227 mutex_unlock(&ar->conf_mutex);
7230 struct ath10k_mac_change_chanctx_arg {
7231 struct ieee80211_chanctx_conf *ctx;
7232 struct ieee80211_vif_chanctx_switch *vifs;
7233 int n_vifs;
7234 int next_vif;
7237 static void
7238 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7239 struct ieee80211_vif *vif)
7241 struct ath10k_mac_change_chanctx_arg *arg = data;
7243 if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
7244 return;
7246 arg->n_vifs++;
7249 static void
7250 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7251 struct ieee80211_vif *vif)
7253 struct ath10k_mac_change_chanctx_arg *arg = data;
7254 struct ieee80211_chanctx_conf *ctx;
7256 ctx = rcu_access_pointer(vif->chanctx_conf);
7257 if (ctx != arg->ctx)
7258 return;
7260 if (WARN_ON(arg->next_vif == arg->n_vifs))
7261 return;
7263 arg->vifs[arg->next_vif].vif = vif;
7264 arg->vifs[arg->next_vif].old_ctx = ctx;
7265 arg->vifs[arg->next_vif].new_ctx = ctx;
7266 arg->next_vif++;
7269 static void
7270 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7271 struct ieee80211_chanctx_conf *ctx,
7272 u32 changed)
7274 struct ath10k *ar = hw->priv;
7275 struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
7277 mutex_lock(&ar->conf_mutex);
7279 ath10k_dbg(ar, ATH10K_DBG_MAC,
7280 "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
7281 ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7283 /* This shouldn't really happen because channel switching should use
7284 * switch_vif_chanctx().
7286 if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7287 goto unlock;
7289 if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
7290 ieee80211_iterate_active_interfaces_atomic(
7292 IEEE80211_IFACE_ITER_NORMAL,
7293 ath10k_mac_change_chanctx_cnt_iter,
7294 &arg);
7295 if (arg.n_vifs == 0)
7296 goto radar;
7298 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
7299 GFP_KERNEL);
7300 if (!arg.vifs)
7301 goto radar;
7303 ieee80211_iterate_active_interfaces_atomic(
7305 IEEE80211_IFACE_ITER_NORMAL,
7306 ath10k_mac_change_chanctx_fill_iter,
7307 &arg);
7308 ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7309 kfree(arg.vifs);
7312 radar:
7313 ath10k_recalc_radar_detection(ar);
7315 /* FIXME: How to configure Rx chains properly? */
7317 /* No other actions are actually necessary. Firmware maintains channel
7318 * definitions per vdev internally and there's no host-side channel
7319 * context abstraction to configure, e.g. channel width.
7322 unlock:
7323 mutex_unlock(&ar->conf_mutex);
7326 static int
7327 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7328 struct ieee80211_vif *vif,
7329 struct ieee80211_chanctx_conf *ctx)
7331 struct ath10k *ar = hw->priv;
7332 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7333 int ret;
7335 mutex_lock(&ar->conf_mutex);
7337 ath10k_dbg(ar, ATH10K_DBG_MAC,
7338 "mac chanctx assign ptr %pK vdev_id %i\n",
7339 ctx, arvif->vdev_id);
7341 if (WARN_ON(arvif->is_started)) {
7342 mutex_unlock(&ar->conf_mutex);
7343 return -EBUSY;
7346 ret = ath10k_vdev_start(arvif, &ctx->def);
7347 if (ret) {
7348 ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
7349 arvif->vdev_id, vif->addr,
7350 ctx->def.chan->center_freq, ret);
7351 goto err;
7354 arvif->is_started = true;
7356 ret = ath10k_mac_vif_setup_ps(arvif);
7357 if (ret) {
7358 ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
7359 arvif->vdev_id, ret);
7360 goto err_stop;
7363 if (vif->type == NL80211_IFTYPE_MONITOR) {
7364 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
7365 if (ret) {
7366 ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
7367 arvif->vdev_id, ret);
7368 goto err_stop;
7371 arvif->is_up = true;
7374 mutex_unlock(&ar->conf_mutex);
7375 return 0;
7377 err_stop:
7378 ath10k_vdev_stop(arvif);
7379 arvif->is_started = false;
7380 ath10k_mac_vif_setup_ps(arvif);
7382 err:
7383 mutex_unlock(&ar->conf_mutex);
7384 return ret;
7387 static void
7388 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7389 struct ieee80211_vif *vif,
7390 struct ieee80211_chanctx_conf *ctx)
7392 struct ath10k *ar = hw->priv;
7393 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7394 int ret;
7396 mutex_lock(&ar->conf_mutex);
7398 ath10k_dbg(ar, ATH10K_DBG_MAC,
7399 "mac chanctx unassign ptr %pK vdev_id %i\n",
7400 ctx, arvif->vdev_id);
7402 WARN_ON(!arvif->is_started);
7404 if (vif->type == NL80211_IFTYPE_MONITOR) {
7405 WARN_ON(!arvif->is_up);
7407 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7408 if (ret)
7409 ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
7410 arvif->vdev_id, ret);
7412 arvif->is_up = false;
7415 ret = ath10k_vdev_stop(arvif);
7416 if (ret)
7417 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
7418 arvif->vdev_id, ret);
7420 arvif->is_started = false;
7422 mutex_unlock(&ar->conf_mutex);
7425 static int
7426 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7427 struct ieee80211_vif_chanctx_switch *vifs,
7428 int n_vifs,
7429 enum ieee80211_chanctx_switch_mode mode)
7431 struct ath10k *ar = hw->priv;
7433 mutex_lock(&ar->conf_mutex);
7435 ath10k_dbg(ar, ATH10K_DBG_MAC,
7436 "mac chanctx switch n_vifs %d mode %d\n",
7437 n_vifs, mode);
7438 ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
7440 mutex_unlock(&ar->conf_mutex);
7441 return 0;
7444 static const struct ieee80211_ops ath10k_ops = {
7445 .tx = ath10k_mac_op_tx,
7446 .wake_tx_queue = ath10k_mac_op_wake_tx_queue,
7447 .start = ath10k_start,
7448 .stop = ath10k_stop,
7449 .config = ath10k_config,
7450 .add_interface = ath10k_add_interface,
7451 .remove_interface = ath10k_remove_interface,
7452 .configure_filter = ath10k_configure_filter,
7453 .bss_info_changed = ath10k_bss_info_changed,
7454 .set_coverage_class = ath10k_mac_op_set_coverage_class,
7455 .hw_scan = ath10k_hw_scan,
7456 .cancel_hw_scan = ath10k_cancel_hw_scan,
7457 .set_key = ath10k_set_key,
7458 .set_default_unicast_key = ath10k_set_default_unicast_key,
7459 .sta_state = ath10k_sta_state,
7460 .conf_tx = ath10k_conf_tx,
7461 .remain_on_channel = ath10k_remain_on_channel,
7462 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
7463 .set_rts_threshold = ath10k_set_rts_threshold,
7464 .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
7465 .flush = ath10k_flush,
7466 .tx_last_beacon = ath10k_tx_last_beacon,
7467 .set_antenna = ath10k_set_antenna,
7468 .get_antenna = ath10k_get_antenna,
7469 .reconfig_complete = ath10k_reconfig_complete,
7470 .get_survey = ath10k_get_survey,
7471 .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
7472 .sta_rc_update = ath10k_sta_rc_update,
7473 .get_tsf = ath10k_get_tsf,
7474 .set_tsf = ath10k_set_tsf,
7475 .ampdu_action = ath10k_ampdu_action,
7476 .get_et_sset_count = ath10k_debug_get_et_sset_count,
7477 .get_et_stats = ath10k_debug_get_et_stats,
7478 .get_et_strings = ath10k_debug_get_et_strings,
7479 .add_chanctx = ath10k_mac_op_add_chanctx,
7480 .remove_chanctx = ath10k_mac_op_remove_chanctx,
7481 .change_chanctx = ath10k_mac_op_change_chanctx,
7482 .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
7483 .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
7484 .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
7486 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
7488 #ifdef CONFIG_PM
7489 .suspend = ath10k_wow_op_suspend,
7490 .resume = ath10k_wow_op_resume,
7491 #endif
7492 #ifdef CONFIG_MAC80211_DEBUGFS
7493 .sta_add_debugfs = ath10k_sta_add_debugfs,
7494 .sta_statistics = ath10k_sta_statistics,
7495 #endif
7498 #define CHAN2G(_channel, _freq, _flags) { \
7499 .band = NL80211_BAND_2GHZ, \
7500 .hw_value = (_channel), \
7501 .center_freq = (_freq), \
7502 .flags = (_flags), \
7503 .max_antenna_gain = 0, \
7504 .max_power = 30, \
7507 #define CHAN5G(_channel, _freq, _flags) { \
7508 .band = NL80211_BAND_5GHZ, \
7509 .hw_value = (_channel), \
7510 .center_freq = (_freq), \
7511 .flags = (_flags), \
7512 .max_antenna_gain = 0, \
7513 .max_power = 30, \
7516 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
7517 CHAN2G(1, 2412, 0),
7518 CHAN2G(2, 2417, 0),
7519 CHAN2G(3, 2422, 0),
7520 CHAN2G(4, 2427, 0),
7521 CHAN2G(5, 2432, 0),
7522 CHAN2G(6, 2437, 0),
7523 CHAN2G(7, 2442, 0),
7524 CHAN2G(8, 2447, 0),
7525 CHAN2G(9, 2452, 0),
7526 CHAN2G(10, 2457, 0),
7527 CHAN2G(11, 2462, 0),
7528 CHAN2G(12, 2467, 0),
7529 CHAN2G(13, 2472, 0),
7530 CHAN2G(14, 2484, 0),
7533 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
7534 CHAN5G(36, 5180, 0),
7535 CHAN5G(40, 5200, 0),
7536 CHAN5G(44, 5220, 0),
7537 CHAN5G(48, 5240, 0),
7538 CHAN5G(52, 5260, 0),
7539 CHAN5G(56, 5280, 0),
7540 CHAN5G(60, 5300, 0),
7541 CHAN5G(64, 5320, 0),
7542 CHAN5G(100, 5500, 0),
7543 CHAN5G(104, 5520, 0),
7544 CHAN5G(108, 5540, 0),
7545 CHAN5G(112, 5560, 0),
7546 CHAN5G(116, 5580, 0),
7547 CHAN5G(120, 5600, 0),
7548 CHAN5G(124, 5620, 0),
7549 CHAN5G(128, 5640, 0),
7550 CHAN5G(132, 5660, 0),
7551 CHAN5G(136, 5680, 0),
7552 CHAN5G(140, 5700, 0),
7553 CHAN5G(144, 5720, 0),
7554 CHAN5G(149, 5745, 0),
7555 CHAN5G(153, 5765, 0),
7556 CHAN5G(157, 5785, 0),
7557 CHAN5G(161, 5805, 0),
7558 CHAN5G(165, 5825, 0),
7561 struct ath10k *ath10k_mac_create(size_t priv_size)
7563 struct ieee80211_hw *hw;
7564 struct ieee80211_ops *ops;
7565 struct ath10k *ar;
7567 ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
7568 if (!ops)
7569 return NULL;
7571 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
7572 if (!hw) {
7573 kfree(ops);
7574 return NULL;
7577 ar = hw->priv;
7578 ar->hw = hw;
7579 ar->ops = ops;
7581 return ar;
7584 void ath10k_mac_destroy(struct ath10k *ar)
7586 struct ieee80211_ops *ops = ar->ops;
7588 ieee80211_free_hw(ar->hw);
7589 kfree(ops);
7592 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
7594 .max = 8,
7595 .types = BIT(NL80211_IFTYPE_STATION)
7596 | BIT(NL80211_IFTYPE_P2P_CLIENT)
7599 .max = 3,
7600 .types = BIT(NL80211_IFTYPE_P2P_GO)
7603 .max = 1,
7604 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
7607 .max = 7,
7608 .types = BIT(NL80211_IFTYPE_AP)
7609 #ifdef CONFIG_MAC80211_MESH
7610 | BIT(NL80211_IFTYPE_MESH_POINT)
7611 #endif
7615 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
7617 .max = 8,
7618 .types = BIT(NL80211_IFTYPE_AP)
7619 #ifdef CONFIG_MAC80211_MESH
7620 | BIT(NL80211_IFTYPE_MESH_POINT)
7621 #endif
7624 .max = 1,
7625 .types = BIT(NL80211_IFTYPE_STATION)
7629 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
7631 .limits = ath10k_if_limits,
7632 .n_limits = ARRAY_SIZE(ath10k_if_limits),
7633 .max_interfaces = 8,
7634 .num_different_channels = 1,
7635 .beacon_int_infra_match = true,
7639 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
7641 .limits = ath10k_10x_if_limits,
7642 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
7643 .max_interfaces = 8,
7644 .num_different_channels = 1,
7645 .beacon_int_infra_match = true,
7646 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
7647 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7648 BIT(NL80211_CHAN_WIDTH_20) |
7649 BIT(NL80211_CHAN_WIDTH_40) |
7650 BIT(NL80211_CHAN_WIDTH_80),
7651 #endif
7655 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
7657 .max = 2,
7658 .types = BIT(NL80211_IFTYPE_STATION),
7661 .max = 2,
7662 .types = BIT(NL80211_IFTYPE_AP) |
7663 #ifdef CONFIG_MAC80211_MESH
7664 BIT(NL80211_IFTYPE_MESH_POINT) |
7665 #endif
7666 BIT(NL80211_IFTYPE_P2P_CLIENT) |
7667 BIT(NL80211_IFTYPE_P2P_GO),
7670 .max = 1,
7671 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7675 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
7677 .max = 2,
7678 .types = BIT(NL80211_IFTYPE_STATION),
7681 .max = 2,
7682 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
7685 .max = 1,
7686 .types = BIT(NL80211_IFTYPE_AP) |
7687 #ifdef CONFIG_MAC80211_MESH
7688 BIT(NL80211_IFTYPE_MESH_POINT) |
7689 #endif
7690 BIT(NL80211_IFTYPE_P2P_GO),
7693 .max = 1,
7694 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7698 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
7700 .max = 1,
7701 .types = BIT(NL80211_IFTYPE_STATION),
7704 .max = 1,
7705 .types = BIT(NL80211_IFTYPE_ADHOC),
7709 /* FIXME: This is not thouroughly tested. These combinations may over- or
7710 * underestimate hw/fw capabilities.
7712 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
7714 .limits = ath10k_tlv_if_limit,
7715 .num_different_channels = 1,
7716 .max_interfaces = 4,
7717 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7720 .limits = ath10k_tlv_if_limit_ibss,
7721 .num_different_channels = 1,
7722 .max_interfaces = 2,
7723 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7727 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
7729 .limits = ath10k_tlv_if_limit,
7730 .num_different_channels = 1,
7731 .max_interfaces = 4,
7732 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7735 .limits = ath10k_tlv_qcs_if_limit,
7736 .num_different_channels = 2,
7737 .max_interfaces = 4,
7738 .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
7741 .limits = ath10k_tlv_if_limit_ibss,
7742 .num_different_channels = 1,
7743 .max_interfaces = 2,
7744 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7748 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
7750 .max = 1,
7751 .types = BIT(NL80211_IFTYPE_STATION),
7754 .max = 16,
7755 .types = BIT(NL80211_IFTYPE_AP)
7756 #ifdef CONFIG_MAC80211_MESH
7757 | BIT(NL80211_IFTYPE_MESH_POINT)
7758 #endif
7762 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
7764 .limits = ath10k_10_4_if_limits,
7765 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
7766 .max_interfaces = 16,
7767 .num_different_channels = 1,
7768 .beacon_int_infra_match = true,
7769 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
7770 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7771 BIT(NL80211_CHAN_WIDTH_20) |
7772 BIT(NL80211_CHAN_WIDTH_40) |
7773 BIT(NL80211_CHAN_WIDTH_80),
7774 #endif
7778 static void ath10k_get_arvif_iter(void *data, u8 *mac,
7779 struct ieee80211_vif *vif)
7781 struct ath10k_vif_iter *arvif_iter = data;
7782 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7784 if (arvif->vdev_id == arvif_iter->vdev_id)
7785 arvif_iter->arvif = arvif;
7788 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
7790 struct ath10k_vif_iter arvif_iter;
7791 u32 flags;
7793 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
7794 arvif_iter.vdev_id = vdev_id;
7796 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
7797 ieee80211_iterate_active_interfaces_atomic(ar->hw,
7798 flags,
7799 ath10k_get_arvif_iter,
7800 &arvif_iter);
7801 if (!arvif_iter.arvif) {
7802 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
7803 return NULL;
7806 return arvif_iter.arvif;
7809 #define WRD_METHOD "WRDD"
7810 #define WRDD_WIFI (0x07)
7812 static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
7814 union acpi_object *mcc_pkg;
7815 union acpi_object *domain_type;
7816 union acpi_object *mcc_value;
7817 u32 i;
7819 if (wrdd->type != ACPI_TYPE_PACKAGE ||
7820 wrdd->package.count < 2 ||
7821 wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
7822 wrdd->package.elements[0].integer.value != 0) {
7823 ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
7824 return 0;
7827 for (i = 1; i < wrdd->package.count; ++i) {
7828 mcc_pkg = &wrdd->package.elements[i];
7830 if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
7831 continue;
7832 if (mcc_pkg->package.count < 2)
7833 continue;
7834 if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
7835 mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
7836 continue;
7838 domain_type = &mcc_pkg->package.elements[0];
7839 if (domain_type->integer.value != WRDD_WIFI)
7840 continue;
7842 mcc_value = &mcc_pkg->package.elements[1];
7843 return mcc_value->integer.value;
7845 return 0;
7848 static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
7850 struct pci_dev __maybe_unused *pdev = to_pci_dev(ar->dev);
7851 acpi_handle root_handle;
7852 acpi_handle handle;
7853 struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
7854 acpi_status status;
7855 u32 alpha2_code;
7856 char alpha2[3];
7858 root_handle = ACPI_HANDLE(&pdev->dev);
7859 if (!root_handle)
7860 return -EOPNOTSUPP;
7862 status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
7863 if (ACPI_FAILURE(status)) {
7864 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7865 "failed to get wrd method %d\n", status);
7866 return -EIO;
7869 status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
7870 if (ACPI_FAILURE(status)) {
7871 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7872 "failed to call wrdc %d\n", status);
7873 return -EIO;
7876 alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
7877 kfree(wrdd.pointer);
7878 if (!alpha2_code)
7879 return -EIO;
7881 alpha2[0] = (alpha2_code >> 8) & 0xff;
7882 alpha2[1] = (alpha2_code >> 0) & 0xff;
7883 alpha2[2] = '\0';
7885 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7886 "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
7888 *rd = ath_regd_find_country_by_name(alpha2);
7889 if (*rd == 0xffff)
7890 return -EIO;
7892 *rd |= COUNTRY_ERD_FLAG;
7893 return 0;
7896 static int ath10k_mac_init_rd(struct ath10k *ar)
7898 int ret;
7899 u16 rd;
7901 ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
7902 if (ret) {
7903 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7904 "fallback to eeprom programmed regulatory settings\n");
7905 rd = ar->hw_eeprom_rd;
7908 ar->ath_common.regulatory.current_rd = rd;
7909 return 0;
7912 int ath10k_mac_register(struct ath10k *ar)
7914 static const u32 cipher_suites[] = {
7915 WLAN_CIPHER_SUITE_WEP40,
7916 WLAN_CIPHER_SUITE_WEP104,
7917 WLAN_CIPHER_SUITE_TKIP,
7918 WLAN_CIPHER_SUITE_CCMP,
7919 WLAN_CIPHER_SUITE_AES_CMAC,
7921 struct ieee80211_supported_band *band;
7922 void *channels;
7923 int ret;
7925 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7927 SET_IEEE80211_DEV(ar->hw, ar->dev);
7929 BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
7930 ARRAY_SIZE(ath10k_5ghz_channels)) !=
7931 ATH10K_NUM_CHANS);
7933 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
7934 channels = kmemdup(ath10k_2ghz_channels,
7935 sizeof(ath10k_2ghz_channels),
7936 GFP_KERNEL);
7937 if (!channels) {
7938 ret = -ENOMEM;
7939 goto err_free;
7942 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
7943 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
7944 band->channels = channels;
7946 if (ar->hw_params.cck_rate_map_rev2) {
7947 band->n_bitrates = ath10k_g_rates_rev2_size;
7948 band->bitrates = ath10k_g_rates_rev2;
7949 } else {
7950 band->n_bitrates = ath10k_g_rates_size;
7951 band->bitrates = ath10k_g_rates;
7954 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
7957 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
7958 channels = kmemdup(ath10k_5ghz_channels,
7959 sizeof(ath10k_5ghz_channels),
7960 GFP_KERNEL);
7961 if (!channels) {
7962 ret = -ENOMEM;
7963 goto err_free;
7966 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7967 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
7968 band->channels = channels;
7969 band->n_bitrates = ath10k_a_rates_size;
7970 band->bitrates = ath10k_a_rates;
7971 ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
7974 ath10k_mac_setup_ht_vht_cap(ar);
7976 ar->hw->wiphy->interface_modes =
7977 BIT(NL80211_IFTYPE_STATION) |
7978 BIT(NL80211_IFTYPE_AP) |
7979 BIT(NL80211_IFTYPE_MESH_POINT);
7981 ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
7982 ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
7984 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
7985 ar->hw->wiphy->interface_modes |=
7986 BIT(NL80211_IFTYPE_P2P_DEVICE) |
7987 BIT(NL80211_IFTYPE_P2P_CLIENT) |
7988 BIT(NL80211_IFTYPE_P2P_GO);
7990 ieee80211_hw_set(ar->hw, SIGNAL_DBM);
7991 ieee80211_hw_set(ar->hw, SUPPORTS_PS);
7992 ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
7993 ieee80211_hw_set(ar->hw, MFP_CAPABLE);
7994 ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
7995 ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
7996 ieee80211_hw_set(ar->hw, AP_LINK_PS);
7997 ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
7998 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
7999 ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
8000 ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
8001 ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
8002 ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
8003 ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
8005 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
8006 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
8008 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
8009 ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
8011 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8012 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
8014 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
8015 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
8016 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
8019 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
8020 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
8022 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
8023 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
8024 ar->hw->txq_data_size = sizeof(struct ath10k_txq);
8026 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
8028 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
8029 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
8031 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
8032 * that userspace (e.g. wpa_supplicant/hostapd) can generate
8033 * correct Probe Responses. This is more of a hack advert..
8035 ar->hw->wiphy->probe_resp_offload |=
8036 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
8037 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
8038 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
8041 if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
8042 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
8044 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
8045 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
8046 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
8048 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
8049 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
8050 NL80211_FEATURE_AP_SCAN;
8052 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
8054 ret = ath10k_wow_init(ar);
8055 if (ret) {
8056 ath10k_warn(ar, "failed to init wow: %d\n", ret);
8057 goto err_free;
8060 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
8063 * on LL hardware queues are managed entirely by the FW
8064 * so we only advertise to mac we can do the queues thing
8066 ar->hw->queues = IEEE80211_MAX_QUEUES;
8068 /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
8069 * something that vdev_ids can't reach so that we don't stop the queue
8070 * accidentally.
8072 ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
8074 switch (ar->running_fw->fw_file.wmi_op_version) {
8075 case ATH10K_FW_WMI_OP_VERSION_MAIN:
8076 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
8077 ar->hw->wiphy->n_iface_combinations =
8078 ARRAY_SIZE(ath10k_if_comb);
8079 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
8080 break;
8081 case ATH10K_FW_WMI_OP_VERSION_TLV:
8082 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
8083 ar->hw->wiphy->iface_combinations =
8084 ath10k_tlv_qcs_if_comb;
8085 ar->hw->wiphy->n_iface_combinations =
8086 ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
8087 } else {
8088 ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
8089 ar->hw->wiphy->n_iface_combinations =
8090 ARRAY_SIZE(ath10k_tlv_if_comb);
8092 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
8093 break;
8094 case ATH10K_FW_WMI_OP_VERSION_10_1:
8095 case ATH10K_FW_WMI_OP_VERSION_10_2:
8096 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
8097 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
8098 ar->hw->wiphy->n_iface_combinations =
8099 ARRAY_SIZE(ath10k_10x_if_comb);
8100 break;
8101 case ATH10K_FW_WMI_OP_VERSION_10_4:
8102 ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
8103 ar->hw->wiphy->n_iface_combinations =
8104 ARRAY_SIZE(ath10k_10_4_if_comb);
8105 break;
8106 case ATH10K_FW_WMI_OP_VERSION_UNSET:
8107 case ATH10K_FW_WMI_OP_VERSION_MAX:
8108 WARN_ON(1);
8109 ret = -EINVAL;
8110 goto err_free;
8113 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
8114 ar->hw->netdev_features = NETIF_F_HW_CSUM;
8116 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
8117 /* Init ath dfs pattern detector */
8118 ar->ath_common.debug_mask = ATH_DBG_DFS;
8119 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
8120 NL80211_DFS_UNSET);
8122 if (!ar->dfs_detector)
8123 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
8126 /* Current wake_tx_queue implementation imposes a significant
8127 * performance penalty in some setups. The tx scheduling code needs
8128 * more work anyway so disable the wake_tx_queue unless firmware
8129 * supports the pull-push mechanism.
8131 if (!test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL,
8132 ar->running_fw->fw_file.fw_features))
8133 ar->ops->wake_tx_queue = NULL;
8135 ret = ath10k_mac_init_rd(ar);
8136 if (ret) {
8137 ath10k_err(ar, "failed to derive regdom: %d\n", ret);
8138 goto err_dfs_detector_exit;
8141 /* Disable set_coverage_class for chipsets that do not support it. */
8142 if (!ar->hw_params.hw_ops->set_coverage_class)
8143 ar->ops->set_coverage_class = NULL;
8145 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
8146 ath10k_reg_notifier);
8147 if (ret) {
8148 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
8149 goto err_dfs_detector_exit;
8152 ar->hw->wiphy->cipher_suites = cipher_suites;
8153 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
8155 ret = ieee80211_register_hw(ar->hw);
8156 if (ret) {
8157 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
8158 goto err_dfs_detector_exit;
8161 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
8162 ret = regulatory_hint(ar->hw->wiphy,
8163 ar->ath_common.regulatory.alpha2);
8164 if (ret)
8165 goto err_unregister;
8168 return 0;
8170 err_unregister:
8171 ieee80211_unregister_hw(ar->hw);
8173 err_dfs_detector_exit:
8174 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
8175 ar->dfs_detector->exit(ar->dfs_detector);
8177 err_free:
8178 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8179 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8181 SET_IEEE80211_DEV(ar->hw, NULL);
8182 return ret;
8185 void ath10k_mac_unregister(struct ath10k *ar)
8187 ieee80211_unregister_hw(ar->hw);
8189 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
8190 ar->dfs_detector->exit(ar->dfs_detector);
8192 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8193 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8195 SET_IEEE80211_DEV(ar->hw, NULL);