ath10k: add debug trace to rts/cts set function
[linux-stable.git] / drivers / net / wireless / ath / ath10k / mac.c
blob12a5fccf04cfef0ed29b8d694036423cd50f4835
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 (!test_bit(ATH10K_FW_FEATURE_ALLOWS_MESH_BCAST,
1171 ar->running_fw->fw_file.fw_features) &&
1172 (ar->filter_flags & FIF_OTHER_BSS)) ||
1173 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1176 static bool ath10k_mac_monitor_vdev_is_allowed(struct ath10k *ar)
1178 int num_ctx;
1180 num_ctx = ath10k_mac_num_chanctxs(ar);
1182 /* FIXME: Current interface combinations and cfg80211/mac80211 code
1183 * shouldn't allow this but make sure to prevent handling the following
1184 * case anyway since multi-channel DFS hasn't been tested at all.
1186 if (test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags) && num_ctx > 1)
1187 return false;
1189 return true;
1192 static int ath10k_monitor_recalc(struct ath10k *ar)
1194 bool needed;
1195 bool allowed;
1196 int ret;
1198 lockdep_assert_held(&ar->conf_mutex);
1200 needed = ath10k_mac_monitor_vdev_is_needed(ar);
1201 allowed = ath10k_mac_monitor_vdev_is_allowed(ar);
1203 ath10k_dbg(ar, ATH10K_DBG_MAC,
1204 "mac monitor recalc started? %d needed? %d allowed? %d\n",
1205 ar->monitor_started, needed, allowed);
1207 if (WARN_ON(needed && !allowed)) {
1208 if (ar->monitor_started) {
1209 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopping disallowed monitor\n");
1211 ret = ath10k_monitor_stop(ar);
1212 if (ret)
1213 ath10k_warn(ar, "failed to stop disallowed monitor: %d\n",
1214 ret);
1215 /* not serious */
1218 return -EPERM;
1221 if (needed == ar->monitor_started)
1222 return 0;
1224 if (needed)
1225 return ath10k_monitor_start(ar);
1226 else
1227 return ath10k_monitor_stop(ar);
1230 static bool ath10k_mac_can_set_cts_prot(struct ath10k_vif *arvif)
1232 struct ath10k *ar = arvif->ar;
1234 lockdep_assert_held(&ar->conf_mutex);
1236 if (!arvif->is_started) {
1237 ath10k_dbg(ar, ATH10K_DBG_MAC, "defer cts setup, vdev is not ready yet\n");
1238 return false;
1241 return true;
1244 static int ath10k_mac_set_cts_prot(struct ath10k_vif *arvif)
1246 struct ath10k *ar = arvif->ar;
1247 u32 vdev_param;
1249 lockdep_assert_held(&ar->conf_mutex);
1251 vdev_param = ar->wmi.vdev_param->protection_mode;
1253 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_protection %d\n",
1254 arvif->vdev_id, arvif->use_cts_prot);
1256 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1257 arvif->use_cts_prot ? 1 : 0);
1260 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
1262 struct ath10k *ar = arvif->ar;
1263 u32 vdev_param, rts_cts = 0;
1265 lockdep_assert_held(&ar->conf_mutex);
1267 vdev_param = ar->wmi.vdev_param->enable_rtscts;
1269 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
1271 if (arvif->num_legacy_stations > 0)
1272 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
1273 WMI_RTSCTS_PROFILE);
1274 else
1275 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
1276 WMI_RTSCTS_PROFILE);
1278 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d recalc rts/cts prot %d\n",
1279 arvif->vdev_id, rts_cts);
1281 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
1282 rts_cts);
1285 static int ath10k_start_cac(struct ath10k *ar)
1287 int ret;
1289 lockdep_assert_held(&ar->conf_mutex);
1291 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1293 ret = ath10k_monitor_recalc(ar);
1294 if (ret) {
1295 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
1296 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1297 return ret;
1300 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
1301 ar->monitor_vdev_id);
1303 return 0;
1306 static int ath10k_stop_cac(struct ath10k *ar)
1308 lockdep_assert_held(&ar->conf_mutex);
1310 /* CAC is not running - do nothing */
1311 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
1312 return 0;
1314 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
1315 ath10k_monitor_stop(ar);
1317 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
1319 return 0;
1322 static void ath10k_mac_has_radar_iter(struct ieee80211_hw *hw,
1323 struct ieee80211_chanctx_conf *conf,
1324 void *data)
1326 bool *ret = data;
1328 if (!*ret && conf->radar_enabled)
1329 *ret = true;
1332 static bool ath10k_mac_has_radar_enabled(struct ath10k *ar)
1334 bool has_radar = false;
1336 ieee80211_iter_chan_contexts_atomic(ar->hw,
1337 ath10k_mac_has_radar_iter,
1338 &has_radar);
1340 return has_radar;
1343 static void ath10k_recalc_radar_detection(struct ath10k *ar)
1345 int ret;
1347 lockdep_assert_held(&ar->conf_mutex);
1349 ath10k_stop_cac(ar);
1351 if (!ath10k_mac_has_radar_enabled(ar))
1352 return;
1354 if (ar->num_started_vdevs > 0)
1355 return;
1357 ret = ath10k_start_cac(ar);
1358 if (ret) {
1360 * Not possible to start CAC on current channel so starting
1361 * radiation is not allowed, make this channel DFS_UNAVAILABLE
1362 * by indicating that radar was detected.
1364 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
1365 ieee80211_radar_detected(ar->hw);
1369 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
1371 struct ath10k *ar = arvif->ar;
1372 int ret;
1374 lockdep_assert_held(&ar->conf_mutex);
1376 reinit_completion(&ar->vdev_setup_done);
1378 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
1379 if (ret) {
1380 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
1381 arvif->vdev_id, ret);
1382 return ret;
1385 ret = ath10k_vdev_setup_sync(ar);
1386 if (ret) {
1387 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
1388 arvif->vdev_id, ret);
1389 return ret;
1392 WARN_ON(ar->num_started_vdevs == 0);
1394 if (ar->num_started_vdevs != 0) {
1395 ar->num_started_vdevs--;
1396 ath10k_recalc_radar_detection(ar);
1399 return ret;
1402 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif,
1403 const struct cfg80211_chan_def *chandef,
1404 bool restart)
1406 struct ath10k *ar = arvif->ar;
1407 struct wmi_vdev_start_request_arg arg = {};
1408 int ret = 0;
1410 lockdep_assert_held(&ar->conf_mutex);
1412 reinit_completion(&ar->vdev_setup_done);
1414 arg.vdev_id = arvif->vdev_id;
1415 arg.dtim_period = arvif->dtim_period;
1416 arg.bcn_intval = arvif->beacon_interval;
1418 arg.channel.freq = chandef->chan->center_freq;
1419 arg.channel.band_center_freq1 = chandef->center_freq1;
1420 arg.channel.mode = chan_to_phymode(chandef);
1422 arg.channel.min_power = 0;
1423 arg.channel.max_power = chandef->chan->max_power * 2;
1424 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1425 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1427 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1428 arg.ssid = arvif->u.ap.ssid;
1429 arg.ssid_len = arvif->u.ap.ssid_len;
1430 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1432 /* For now allow DFS for AP mode */
1433 arg.channel.chan_radar =
1434 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1435 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1436 arg.ssid = arvif->vif->bss_conf.ssid;
1437 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1440 ath10k_dbg(ar, ATH10K_DBG_MAC,
1441 "mac vdev %d start center_freq %d phymode %s\n",
1442 arg.vdev_id, arg.channel.freq,
1443 ath10k_wmi_phymode_str(arg.channel.mode));
1445 if (restart)
1446 ret = ath10k_wmi_vdev_restart(ar, &arg);
1447 else
1448 ret = ath10k_wmi_vdev_start(ar, &arg);
1450 if (ret) {
1451 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1452 arg.vdev_id, ret);
1453 return ret;
1456 ret = ath10k_vdev_setup_sync(ar);
1457 if (ret) {
1458 ath10k_warn(ar,
1459 "failed to synchronize setup for vdev %i restart %d: %d\n",
1460 arg.vdev_id, restart, ret);
1461 return ret;
1464 ar->num_started_vdevs++;
1465 ath10k_recalc_radar_detection(ar);
1467 return ret;
1470 static int ath10k_vdev_start(struct ath10k_vif *arvif,
1471 const struct cfg80211_chan_def *def)
1473 return ath10k_vdev_start_restart(arvif, def, false);
1476 static int ath10k_vdev_restart(struct ath10k_vif *arvif,
1477 const struct cfg80211_chan_def *def)
1479 return ath10k_vdev_start_restart(arvif, def, true);
1482 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1483 struct sk_buff *bcn)
1485 struct ath10k *ar = arvif->ar;
1486 struct ieee80211_mgmt *mgmt;
1487 const u8 *p2p_ie;
1488 int ret;
1490 if (arvif->vif->type != NL80211_IFTYPE_AP || !arvif->vif->p2p)
1491 return 0;
1493 mgmt = (void *)bcn->data;
1494 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1495 mgmt->u.beacon.variable,
1496 bcn->len - (mgmt->u.beacon.variable -
1497 bcn->data));
1498 if (!p2p_ie)
1499 return -ENOENT;
1501 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1502 if (ret) {
1503 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1504 arvif->vdev_id, ret);
1505 return ret;
1508 return 0;
1511 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1512 u8 oui_type, size_t ie_offset)
1514 size_t len;
1515 const u8 *next;
1516 const u8 *end;
1517 u8 *ie;
1519 if (WARN_ON(skb->len < ie_offset))
1520 return -EINVAL;
1522 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1523 skb->data + ie_offset,
1524 skb->len - ie_offset);
1525 if (!ie)
1526 return -ENOENT;
1528 len = ie[1] + 2;
1529 end = skb->data + skb->len;
1530 next = ie + len;
1532 if (WARN_ON(next > end))
1533 return -EINVAL;
1535 memmove(ie, next, end - next);
1536 skb_trim(skb, skb->len - len);
1538 return 0;
1541 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1543 struct ath10k *ar = arvif->ar;
1544 struct ieee80211_hw *hw = ar->hw;
1545 struct ieee80211_vif *vif = arvif->vif;
1546 struct ieee80211_mutable_offsets offs = {};
1547 struct sk_buff *bcn;
1548 int ret;
1550 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1551 return 0;
1553 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1554 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1555 return 0;
1557 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1558 if (!bcn) {
1559 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1560 return -EPERM;
1563 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1564 if (ret) {
1565 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1566 kfree_skb(bcn);
1567 return ret;
1570 /* P2P IE is inserted by firmware automatically (as configured above)
1571 * so remove it from the base beacon template to avoid duplicate P2P
1572 * IEs in beacon frames.
1574 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1575 offsetof(struct ieee80211_mgmt,
1576 u.beacon.variable));
1578 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1579 0, NULL, 0);
1580 kfree_skb(bcn);
1582 if (ret) {
1583 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1584 ret);
1585 return ret;
1588 return 0;
1591 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1593 struct ath10k *ar = arvif->ar;
1594 struct ieee80211_hw *hw = ar->hw;
1595 struct ieee80211_vif *vif = arvif->vif;
1596 struct sk_buff *prb;
1597 int ret;
1599 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1600 return 0;
1602 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1603 return 0;
1605 prb = ieee80211_proberesp_get(hw, vif);
1606 if (!prb) {
1607 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1608 return -EPERM;
1611 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1612 kfree_skb(prb);
1614 if (ret) {
1615 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1616 ret);
1617 return ret;
1620 return 0;
1623 static int ath10k_mac_vif_fix_hidden_ssid(struct ath10k_vif *arvif)
1625 struct ath10k *ar = arvif->ar;
1626 struct cfg80211_chan_def def;
1627 int ret;
1629 /* When originally vdev is started during assign_vif_chanctx() some
1630 * information is missing, notably SSID. Firmware revisions with beacon
1631 * offloading require the SSID to be provided during vdev (re)start to
1632 * handle hidden SSID properly.
1634 * Vdev restart must be done after vdev has been both started and
1635 * upped. Otherwise some firmware revisions (at least 10.2) fail to
1636 * deliver vdev restart response event causing timeouts during vdev
1637 * syncing in ath10k.
1639 * Note: The vdev down/up and template reinstallation could be skipped
1640 * since only wmi-tlv firmware are known to have beacon offload and
1641 * wmi-tlv doesn't seem to misbehave like 10.2 wrt vdev restart
1642 * response delivery. It's probably more robust to keep it as is.
1644 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1645 return 0;
1647 if (WARN_ON(!arvif->is_started))
1648 return -EINVAL;
1650 if (WARN_ON(!arvif->is_up))
1651 return -EINVAL;
1653 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
1654 return -EINVAL;
1656 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1657 if (ret) {
1658 ath10k_warn(ar, "failed to bring down ap vdev %i: %d\n",
1659 arvif->vdev_id, ret);
1660 return ret;
1663 /* Vdev down reset beacon & presp templates. Reinstall them. Otherwise
1664 * firmware will crash upon vdev up.
1667 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1668 if (ret) {
1669 ath10k_warn(ar, "failed to update beacon template: %d\n", ret);
1670 return ret;
1673 ret = ath10k_mac_setup_prb_tmpl(arvif);
1674 if (ret) {
1675 ath10k_warn(ar, "failed to update presp template: %d\n", ret);
1676 return ret;
1679 ret = ath10k_vdev_restart(arvif, &def);
1680 if (ret) {
1681 ath10k_warn(ar, "failed to restart ap vdev %i: %d\n",
1682 arvif->vdev_id, ret);
1683 return ret;
1686 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1687 arvif->bssid);
1688 if (ret) {
1689 ath10k_warn(ar, "failed to bring up ap vdev %i: %d\n",
1690 arvif->vdev_id, ret);
1691 return ret;
1694 return 0;
1697 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1698 struct ieee80211_bss_conf *info)
1700 struct ath10k *ar = arvif->ar;
1701 int ret = 0;
1703 lockdep_assert_held(&arvif->ar->conf_mutex);
1705 if (!info->enable_beacon) {
1706 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
1707 if (ret)
1708 ath10k_warn(ar, "failed to down vdev_id %i: %d\n",
1709 arvif->vdev_id, ret);
1711 arvif->is_up = false;
1713 spin_lock_bh(&arvif->ar->data_lock);
1714 ath10k_mac_vif_beacon_free(arvif);
1715 spin_unlock_bh(&arvif->ar->data_lock);
1717 return;
1720 arvif->tx_seq_no = 0x1000;
1722 arvif->aid = 0;
1723 ether_addr_copy(arvif->bssid, info->bssid);
1725 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1726 arvif->bssid);
1727 if (ret) {
1728 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1729 arvif->vdev_id, ret);
1730 return;
1733 arvif->is_up = true;
1735 ret = ath10k_mac_vif_fix_hidden_ssid(arvif);
1736 if (ret) {
1737 ath10k_warn(ar, "failed to fix hidden ssid for vdev %i, expect trouble: %d\n",
1738 arvif->vdev_id, ret);
1739 return;
1742 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1745 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1746 struct ieee80211_bss_conf *info,
1747 const u8 self_peer[ETH_ALEN])
1749 struct ath10k *ar = arvif->ar;
1750 u32 vdev_param;
1751 int ret = 0;
1753 lockdep_assert_held(&arvif->ar->conf_mutex);
1755 if (!info->ibss_joined) {
1756 if (is_zero_ether_addr(arvif->bssid))
1757 return;
1759 eth_zero_addr(arvif->bssid);
1761 return;
1764 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1765 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1766 ATH10K_DEFAULT_ATIM);
1767 if (ret)
1768 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1769 arvif->vdev_id, ret);
1772 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1774 struct ath10k *ar = arvif->ar;
1775 u32 param;
1776 u32 value;
1777 int ret;
1779 lockdep_assert_held(&arvif->ar->conf_mutex);
1781 if (arvif->u.sta.uapsd)
1782 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1783 else
1784 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1786 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1787 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1788 if (ret) {
1789 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1790 value, arvif->vdev_id, ret);
1791 return ret;
1794 return 0;
1797 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1799 struct ath10k *ar = arvif->ar;
1800 u32 param;
1801 u32 value;
1802 int ret;
1804 lockdep_assert_held(&arvif->ar->conf_mutex);
1806 if (arvif->u.sta.uapsd)
1807 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1808 else
1809 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1811 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1812 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1813 param, value);
1814 if (ret) {
1815 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1816 value, arvif->vdev_id, ret);
1817 return ret;
1820 return 0;
1823 static int ath10k_mac_num_vifs_started(struct ath10k *ar)
1825 struct ath10k_vif *arvif;
1826 int num = 0;
1828 lockdep_assert_held(&ar->conf_mutex);
1830 list_for_each_entry(arvif, &ar->arvifs, list)
1831 if (arvif->is_started)
1832 num++;
1834 return num;
1837 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1839 struct ath10k *ar = arvif->ar;
1840 struct ieee80211_vif *vif = arvif->vif;
1841 struct ieee80211_conf *conf = &ar->hw->conf;
1842 enum wmi_sta_powersave_param param;
1843 enum wmi_sta_ps_mode psmode;
1844 int ret;
1845 int ps_timeout;
1846 bool enable_ps;
1848 lockdep_assert_held(&arvif->ar->conf_mutex);
1850 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1851 return 0;
1853 enable_ps = arvif->ps;
1855 if (enable_ps && ath10k_mac_num_vifs_started(ar) > 1 &&
1856 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1857 ar->running_fw->fw_file.fw_features)) {
1858 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1859 arvif->vdev_id);
1860 enable_ps = false;
1863 if (!arvif->is_started) {
1864 /* mac80211 can update vif powersave state while disconnected.
1865 * Firmware doesn't behave nicely and consumes more power than
1866 * necessary if PS is disabled on a non-started vdev. Hence
1867 * force-enable PS for non-running vdevs.
1869 psmode = WMI_STA_PS_MODE_ENABLED;
1870 } else if (enable_ps) {
1871 psmode = WMI_STA_PS_MODE_ENABLED;
1872 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1874 ps_timeout = conf->dynamic_ps_timeout;
1875 if (ps_timeout == 0) {
1876 /* Firmware doesn't like 0 */
1877 ps_timeout = ieee80211_tu_to_usec(
1878 vif->bss_conf.beacon_int) / 1000;
1881 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1882 ps_timeout);
1883 if (ret) {
1884 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1885 arvif->vdev_id, ret);
1886 return ret;
1888 } else {
1889 psmode = WMI_STA_PS_MODE_DISABLED;
1892 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1893 arvif->vdev_id, psmode ? "enable" : "disable");
1895 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1896 if (ret) {
1897 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1898 psmode, arvif->vdev_id, ret);
1899 return ret;
1902 return 0;
1905 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1907 struct ath10k *ar = arvif->ar;
1908 struct wmi_sta_keepalive_arg arg = {};
1909 int ret;
1911 lockdep_assert_held(&arvif->ar->conf_mutex);
1913 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1914 return 0;
1916 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1917 return 0;
1919 /* Some firmware revisions have a bug and ignore the `enabled` field.
1920 * Instead use the interval to disable the keepalive.
1922 arg.vdev_id = arvif->vdev_id;
1923 arg.enabled = 1;
1924 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1925 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1927 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1928 if (ret) {
1929 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1930 arvif->vdev_id, ret);
1931 return ret;
1934 return 0;
1937 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1939 struct ath10k *ar = arvif->ar;
1940 struct ieee80211_vif *vif = arvif->vif;
1941 int ret;
1943 lockdep_assert_held(&arvif->ar->conf_mutex);
1945 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1946 return;
1948 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1949 return;
1951 if (!vif->csa_active)
1952 return;
1954 if (!arvif->is_up)
1955 return;
1957 if (!ieee80211_csa_is_complete(vif)) {
1958 ieee80211_csa_update_counter(vif);
1960 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1961 if (ret)
1962 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1963 ret);
1965 ret = ath10k_mac_setup_prb_tmpl(arvif);
1966 if (ret)
1967 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1968 ret);
1969 } else {
1970 ieee80211_csa_finish(vif);
1974 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1976 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1977 ap_csa_work);
1978 struct ath10k *ar = arvif->ar;
1980 mutex_lock(&ar->conf_mutex);
1981 ath10k_mac_vif_ap_csa_count_down(arvif);
1982 mutex_unlock(&ar->conf_mutex);
1985 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1986 struct ieee80211_vif *vif)
1988 struct sk_buff *skb = data;
1989 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1990 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1992 if (vif->type != NL80211_IFTYPE_STATION)
1993 return;
1995 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1996 return;
1998 cancel_delayed_work(&arvif->connection_loss_work);
2001 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
2003 ieee80211_iterate_active_interfaces_atomic(ar->hw,
2004 IEEE80211_IFACE_ITER_NORMAL,
2005 ath10k_mac_handle_beacon_iter,
2006 skb);
2009 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
2010 struct ieee80211_vif *vif)
2012 u32 *vdev_id = data;
2013 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2014 struct ath10k *ar = arvif->ar;
2015 struct ieee80211_hw *hw = ar->hw;
2017 if (arvif->vdev_id != *vdev_id)
2018 return;
2020 if (!arvif->is_up)
2021 return;
2023 ieee80211_beacon_loss(vif);
2025 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
2026 * (done by mac80211) succeeds but beacons do not resume then it
2027 * doesn't make sense to continue operation. Queue connection loss work
2028 * which can be cancelled when beacon is received.
2030 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
2031 ATH10K_CONNECTION_LOSS_HZ);
2034 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
2036 ieee80211_iterate_active_interfaces_atomic(ar->hw,
2037 IEEE80211_IFACE_ITER_NORMAL,
2038 ath10k_mac_handle_beacon_miss_iter,
2039 &vdev_id);
2042 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
2044 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
2045 connection_loss_work.work);
2046 struct ieee80211_vif *vif = arvif->vif;
2048 if (!arvif->is_up)
2049 return;
2051 ieee80211_connection_loss(vif);
2054 /**********************/
2055 /* Station management */
2056 /**********************/
2058 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
2059 struct ieee80211_vif *vif)
2061 /* Some firmware revisions have unstable STA powersave when listen
2062 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
2063 * generate NullFunc frames properly even if buffered frames have been
2064 * indicated in Beacon TIM. Firmware would seldom wake up to pull
2065 * buffered frames. Often pinging the device from AP would simply fail.
2067 * As a workaround set it to 1.
2069 if (vif->type == NL80211_IFTYPE_STATION)
2070 return 1;
2072 return ar->hw->conf.listen_interval;
2075 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
2076 struct ieee80211_vif *vif,
2077 struct ieee80211_sta *sta,
2078 struct wmi_peer_assoc_complete_arg *arg)
2080 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2081 u32 aid;
2083 lockdep_assert_held(&ar->conf_mutex);
2085 if (vif->type == NL80211_IFTYPE_STATION)
2086 aid = vif->bss_conf.aid;
2087 else
2088 aid = sta->aid;
2090 ether_addr_copy(arg->addr, sta->addr);
2091 arg->vdev_id = arvif->vdev_id;
2092 arg->peer_aid = aid;
2093 arg->peer_flags |= arvif->ar->wmi.peer_flags->auth;
2094 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
2095 arg->peer_num_spatial_streams = 1;
2096 arg->peer_caps = vif->bss_conf.assoc_capability;
2099 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
2100 struct ieee80211_vif *vif,
2101 struct ieee80211_sta *sta,
2102 struct wmi_peer_assoc_complete_arg *arg)
2104 struct ieee80211_bss_conf *info = &vif->bss_conf;
2105 struct cfg80211_chan_def def;
2106 struct cfg80211_bss *bss;
2107 const u8 *rsnie = NULL;
2108 const u8 *wpaie = NULL;
2110 lockdep_assert_held(&ar->conf_mutex);
2112 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2113 return;
2115 bss = cfg80211_get_bss(ar->hw->wiphy, def.chan, info->bssid, NULL, 0,
2116 IEEE80211_BSS_TYPE_ANY, IEEE80211_PRIVACY_ANY);
2117 if (bss) {
2118 const struct cfg80211_bss_ies *ies;
2120 rcu_read_lock();
2121 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
2123 ies = rcu_dereference(bss->ies);
2125 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
2126 WLAN_OUI_TYPE_MICROSOFT_WPA,
2127 ies->data,
2128 ies->len);
2129 rcu_read_unlock();
2130 cfg80211_put_bss(ar->hw->wiphy, bss);
2133 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
2134 if (rsnie || wpaie) {
2135 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
2136 arg->peer_flags |= ar->wmi.peer_flags->need_ptk_4_way;
2139 if (wpaie) {
2140 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
2141 arg->peer_flags |= ar->wmi.peer_flags->need_gtk_2_way;
2144 if (sta->mfp &&
2145 test_bit(ATH10K_FW_FEATURE_MFP_SUPPORT,
2146 ar->running_fw->fw_file.fw_features)) {
2147 arg->peer_flags |= ar->wmi.peer_flags->pmf;
2151 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
2152 struct ieee80211_vif *vif,
2153 struct ieee80211_sta *sta,
2154 struct wmi_peer_assoc_complete_arg *arg)
2156 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2157 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
2158 struct cfg80211_chan_def def;
2159 const struct ieee80211_supported_band *sband;
2160 const struct ieee80211_rate *rates;
2161 enum nl80211_band band;
2162 u32 ratemask;
2163 u8 rate;
2164 int i;
2166 lockdep_assert_held(&ar->conf_mutex);
2168 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2169 return;
2171 band = def.chan->band;
2172 sband = ar->hw->wiphy->bands[band];
2173 ratemask = sta->supp_rates[band];
2174 ratemask &= arvif->bitrate_mask.control[band].legacy;
2175 rates = sband->bitrates;
2177 rateset->num_rates = 0;
2179 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
2180 if (!(ratemask & 1))
2181 continue;
2183 rate = ath10k_mac_bitrate_to_rate(rates->bitrate);
2184 rateset->rates[rateset->num_rates] = rate;
2185 rateset->num_rates++;
2189 static bool
2190 ath10k_peer_assoc_h_ht_masked(const u8 ht_mcs_mask[IEEE80211_HT_MCS_MASK_LEN])
2192 int nss;
2194 for (nss = 0; nss < IEEE80211_HT_MCS_MASK_LEN; nss++)
2195 if (ht_mcs_mask[nss])
2196 return false;
2198 return true;
2201 static bool
2202 ath10k_peer_assoc_h_vht_masked(const u16 vht_mcs_mask[NL80211_VHT_NSS_MAX])
2204 int nss;
2206 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++)
2207 if (vht_mcs_mask[nss])
2208 return false;
2210 return true;
2213 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
2214 struct ieee80211_vif *vif,
2215 struct ieee80211_sta *sta,
2216 struct wmi_peer_assoc_complete_arg *arg)
2218 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2219 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2220 struct cfg80211_chan_def def;
2221 enum nl80211_band band;
2222 const u8 *ht_mcs_mask;
2223 const u16 *vht_mcs_mask;
2224 int i, n;
2225 u8 max_nss;
2226 u32 stbc;
2228 lockdep_assert_held(&ar->conf_mutex);
2230 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2231 return;
2233 if (!ht_cap->ht_supported)
2234 return;
2236 band = def.chan->band;
2237 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2238 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2240 if (ath10k_peer_assoc_h_ht_masked(ht_mcs_mask) &&
2241 ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2242 return;
2244 arg->peer_flags |= ar->wmi.peer_flags->ht;
2245 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2246 ht_cap->ampdu_factor)) - 1;
2248 arg->peer_mpdu_density =
2249 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
2251 arg->peer_ht_caps = ht_cap->cap;
2252 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
2254 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
2255 arg->peer_flags |= ar->wmi.peer_flags->ldbc;
2257 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
2258 arg->peer_flags |= ar->wmi.peer_flags->bw40;
2259 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
2262 if (arvif->bitrate_mask.control[band].gi != NL80211_TXRATE_FORCE_LGI) {
2263 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
2264 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2266 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
2267 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
2270 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
2271 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
2272 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2275 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
2276 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
2277 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
2278 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
2279 arg->peer_rate_caps |= stbc;
2280 arg->peer_flags |= ar->wmi.peer_flags->stbc;
2283 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
2284 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
2285 else if (ht_cap->mcs.rx_mask[1])
2286 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
2288 for (i = 0, n = 0, max_nss = 0; i < IEEE80211_HT_MCS_MASK_LEN * 8; i++)
2289 if ((ht_cap->mcs.rx_mask[i / 8] & BIT(i % 8)) &&
2290 (ht_mcs_mask[i / 8] & BIT(i % 8))) {
2291 max_nss = (i / 8) + 1;
2292 arg->peer_ht_rates.rates[n++] = i;
2296 * This is a workaround for HT-enabled STAs which break the spec
2297 * and have no HT capabilities RX mask (no HT RX MCS map).
2299 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
2300 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
2302 * Firmware asserts if such situation occurs.
2304 if (n == 0) {
2305 arg->peer_ht_rates.num_rates = 8;
2306 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
2307 arg->peer_ht_rates.rates[i] = i;
2308 } else {
2309 arg->peer_ht_rates.num_rates = n;
2310 arg->peer_num_spatial_streams = min(sta->rx_nss, max_nss);
2313 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
2314 arg->addr,
2315 arg->peer_ht_rates.num_rates,
2316 arg->peer_num_spatial_streams);
2319 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
2320 struct ath10k_vif *arvif,
2321 struct ieee80211_sta *sta)
2323 u32 uapsd = 0;
2324 u32 max_sp = 0;
2325 int ret = 0;
2327 lockdep_assert_held(&ar->conf_mutex);
2329 if (sta->wme && sta->uapsd_queues) {
2330 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
2331 sta->uapsd_queues, sta->max_sp);
2333 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
2334 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
2335 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
2336 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
2337 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
2338 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
2339 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
2340 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
2341 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
2342 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
2343 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
2344 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
2346 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
2347 max_sp = sta->max_sp;
2349 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2350 sta->addr,
2351 WMI_AP_PS_PEER_PARAM_UAPSD,
2352 uapsd);
2353 if (ret) {
2354 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
2355 arvif->vdev_id, ret);
2356 return ret;
2359 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
2360 sta->addr,
2361 WMI_AP_PS_PEER_PARAM_MAX_SP,
2362 max_sp);
2363 if (ret) {
2364 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
2365 arvif->vdev_id, ret);
2366 return ret;
2369 /* TODO setup this based on STA listen interval and
2370 beacon interval. Currently we don't know
2371 sta->listen_interval - mac80211 patch required.
2372 Currently use 10 seconds */
2373 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
2374 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
2375 10);
2376 if (ret) {
2377 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
2378 arvif->vdev_id, ret);
2379 return ret;
2383 return 0;
2386 static u16
2387 ath10k_peer_assoc_h_vht_limit(u16 tx_mcs_set,
2388 const u16 vht_mcs_limit[NL80211_VHT_NSS_MAX])
2390 int idx_limit;
2391 int nss;
2392 u16 mcs_map;
2393 u16 mcs;
2395 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
2396 mcs_map = ath10k_mac_get_max_vht_mcs_map(tx_mcs_set, nss) &
2397 vht_mcs_limit[nss];
2399 if (mcs_map)
2400 idx_limit = fls(mcs_map) - 1;
2401 else
2402 idx_limit = -1;
2404 switch (idx_limit) {
2405 case 0: /* fall through */
2406 case 1: /* fall through */
2407 case 2: /* fall through */
2408 case 3: /* fall through */
2409 case 4: /* fall through */
2410 case 5: /* fall through */
2411 case 6: /* fall through */
2412 default:
2413 /* see ath10k_mac_can_set_bitrate_mask() */
2414 WARN_ON(1);
2415 /* fall through */
2416 case -1:
2417 mcs = IEEE80211_VHT_MCS_NOT_SUPPORTED;
2418 break;
2419 case 7:
2420 mcs = IEEE80211_VHT_MCS_SUPPORT_0_7;
2421 break;
2422 case 8:
2423 mcs = IEEE80211_VHT_MCS_SUPPORT_0_8;
2424 break;
2425 case 9:
2426 mcs = IEEE80211_VHT_MCS_SUPPORT_0_9;
2427 break;
2430 tx_mcs_set &= ~(0x3 << (nss * 2));
2431 tx_mcs_set |= mcs << (nss * 2);
2434 return tx_mcs_set;
2437 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
2438 struct ieee80211_vif *vif,
2439 struct ieee80211_sta *sta,
2440 struct wmi_peer_assoc_complete_arg *arg)
2442 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
2443 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2444 struct cfg80211_chan_def def;
2445 enum nl80211_band band;
2446 const u16 *vht_mcs_mask;
2447 u8 ampdu_factor;
2449 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2450 return;
2452 if (!vht_cap->vht_supported)
2453 return;
2455 band = def.chan->band;
2456 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2458 if (ath10k_peer_assoc_h_vht_masked(vht_mcs_mask))
2459 return;
2461 arg->peer_flags |= ar->wmi.peer_flags->vht;
2463 if (def.chan->band == NL80211_BAND_2GHZ)
2464 arg->peer_flags |= ar->wmi.peer_flags->vht_2g;
2466 arg->peer_vht_caps = vht_cap->cap;
2468 ampdu_factor = (vht_cap->cap &
2469 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
2470 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
2472 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
2473 * zero in VHT IE. Using it would result in degraded throughput.
2474 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
2475 * it if VHT max_mpdu is smaller. */
2476 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
2477 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
2478 ampdu_factor)) - 1);
2480 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2481 arg->peer_flags |= ar->wmi.peer_flags->bw80;
2483 arg->peer_vht_rates.rx_max_rate =
2484 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
2485 arg->peer_vht_rates.rx_mcs_set =
2486 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
2487 arg->peer_vht_rates.tx_max_rate =
2488 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
2489 arg->peer_vht_rates.tx_mcs_set = ath10k_peer_assoc_h_vht_limit(
2490 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map), vht_mcs_mask);
2492 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
2493 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
2496 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
2497 struct ieee80211_vif *vif,
2498 struct ieee80211_sta *sta,
2499 struct wmi_peer_assoc_complete_arg *arg)
2501 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2503 switch (arvif->vdev_type) {
2504 case WMI_VDEV_TYPE_AP:
2505 if (sta->wme)
2506 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2508 if (sta->wme && sta->uapsd_queues) {
2509 arg->peer_flags |= arvif->ar->wmi.peer_flags->apsd;
2510 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
2512 break;
2513 case WMI_VDEV_TYPE_STA:
2514 if (vif->bss_conf.qos)
2515 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2516 break;
2517 case WMI_VDEV_TYPE_IBSS:
2518 if (sta->wme)
2519 arg->peer_flags |= arvif->ar->wmi.peer_flags->qos;
2520 break;
2521 default:
2522 break;
2525 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
2526 sta->addr, !!(arg->peer_flags &
2527 arvif->ar->wmi.peer_flags->qos));
2530 static bool ath10k_mac_sta_has_ofdm_only(struct ieee80211_sta *sta)
2532 return sta->supp_rates[NL80211_BAND_2GHZ] >>
2533 ATH10K_MAC_FIRST_OFDM_RATE_IDX;
2536 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
2537 struct ieee80211_vif *vif,
2538 struct ieee80211_sta *sta,
2539 struct wmi_peer_assoc_complete_arg *arg)
2541 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2542 struct cfg80211_chan_def def;
2543 enum nl80211_band band;
2544 const u8 *ht_mcs_mask;
2545 const u16 *vht_mcs_mask;
2546 enum wmi_phy_mode phymode = MODE_UNKNOWN;
2548 if (WARN_ON(ath10k_mac_vif_chan(vif, &def)))
2549 return;
2551 band = def.chan->band;
2552 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
2553 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
2555 switch (band) {
2556 case NL80211_BAND_2GHZ:
2557 if (sta->vht_cap.vht_supported &&
2558 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2559 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2560 phymode = MODE_11AC_VHT40;
2561 else
2562 phymode = MODE_11AC_VHT20;
2563 } else if (sta->ht_cap.ht_supported &&
2564 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2565 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2566 phymode = MODE_11NG_HT40;
2567 else
2568 phymode = MODE_11NG_HT20;
2569 } else if (ath10k_mac_sta_has_ofdm_only(sta)) {
2570 phymode = MODE_11G;
2571 } else {
2572 phymode = MODE_11B;
2575 break;
2576 case NL80211_BAND_5GHZ:
2578 * Check VHT first.
2580 if (sta->vht_cap.vht_supported &&
2581 !ath10k_peer_assoc_h_vht_masked(vht_mcs_mask)) {
2582 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
2583 phymode = MODE_11AC_VHT80;
2584 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
2585 phymode = MODE_11AC_VHT40;
2586 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
2587 phymode = MODE_11AC_VHT20;
2588 } else if (sta->ht_cap.ht_supported &&
2589 !ath10k_peer_assoc_h_ht_masked(ht_mcs_mask)) {
2590 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
2591 phymode = MODE_11NA_HT40;
2592 else
2593 phymode = MODE_11NA_HT20;
2594 } else {
2595 phymode = MODE_11A;
2598 break;
2599 default:
2600 break;
2603 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
2604 sta->addr, ath10k_wmi_phymode_str(phymode));
2606 arg->peer_phymode = phymode;
2607 WARN_ON(phymode == MODE_UNKNOWN);
2610 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
2611 struct ieee80211_vif *vif,
2612 struct ieee80211_sta *sta,
2613 struct wmi_peer_assoc_complete_arg *arg)
2615 lockdep_assert_held(&ar->conf_mutex);
2617 memset(arg, 0, sizeof(*arg));
2619 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
2620 ath10k_peer_assoc_h_crypto(ar, vif, sta, arg);
2621 ath10k_peer_assoc_h_rates(ar, vif, sta, arg);
2622 ath10k_peer_assoc_h_ht(ar, vif, sta, arg);
2623 ath10k_peer_assoc_h_vht(ar, vif, sta, arg);
2624 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2625 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2627 return 0;
2630 static const u32 ath10k_smps_map[] = {
2631 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2632 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2633 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2634 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2637 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2638 const u8 *addr,
2639 const struct ieee80211_sta_ht_cap *ht_cap)
2641 int smps;
2643 if (!ht_cap->ht_supported)
2644 return 0;
2646 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2647 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2649 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2650 return -EINVAL;
2652 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2653 WMI_PEER_SMPS_STATE,
2654 ath10k_smps_map[smps]);
2657 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2658 struct ieee80211_vif *vif,
2659 struct ieee80211_sta_vht_cap vht_cap)
2661 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2662 int ret;
2663 u32 param;
2664 u32 value;
2666 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_AFTER_ASSOC)
2667 return 0;
2669 if (!(ar->vht_cap_info &
2670 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2671 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2672 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2673 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2674 return 0;
2676 param = ar->wmi.vdev_param->txbf;
2677 value = 0;
2679 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2680 return 0;
2682 /* The following logic is correct. If a remote STA advertises support
2683 * for being a beamformer then we should enable us being a beamformee.
2686 if (ar->vht_cap_info &
2687 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2688 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2689 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2690 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2692 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2693 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2696 if (ar->vht_cap_info &
2697 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2698 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2699 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2700 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2702 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2703 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2706 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2707 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2709 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2710 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2712 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2713 if (ret) {
2714 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2715 value, ret);
2716 return ret;
2719 return 0;
2722 /* can be called only in mac80211 callbacks due to `key_count` usage */
2723 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2724 struct ieee80211_vif *vif,
2725 struct ieee80211_bss_conf *bss_conf)
2727 struct ath10k *ar = hw->priv;
2728 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2729 struct ieee80211_sta_ht_cap ht_cap;
2730 struct ieee80211_sta_vht_cap vht_cap;
2731 struct wmi_peer_assoc_complete_arg peer_arg;
2732 struct ieee80211_sta *ap_sta;
2733 int ret;
2735 lockdep_assert_held(&ar->conf_mutex);
2737 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2738 arvif->vdev_id, arvif->bssid, arvif->aid);
2740 rcu_read_lock();
2742 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2743 if (!ap_sta) {
2744 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2745 bss_conf->bssid, arvif->vdev_id);
2746 rcu_read_unlock();
2747 return;
2750 /* ap_sta must be accessed only within rcu section which must be left
2751 * before calling ath10k_setup_peer_smps() which might sleep. */
2752 ht_cap = ap_sta->ht_cap;
2753 vht_cap = ap_sta->vht_cap;
2755 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2756 if (ret) {
2757 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2758 bss_conf->bssid, arvif->vdev_id, ret);
2759 rcu_read_unlock();
2760 return;
2763 rcu_read_unlock();
2765 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2766 if (ret) {
2767 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2768 bss_conf->bssid, arvif->vdev_id, ret);
2769 return;
2772 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2773 if (ret) {
2774 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2775 arvif->vdev_id, ret);
2776 return;
2779 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2780 if (ret) {
2781 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2782 arvif->vdev_id, bss_conf->bssid, ret);
2783 return;
2786 ath10k_dbg(ar, ATH10K_DBG_MAC,
2787 "mac vdev %d up (associated) bssid %pM aid %d\n",
2788 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2790 WARN_ON(arvif->is_up);
2792 arvif->aid = bss_conf->aid;
2793 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2795 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2796 if (ret) {
2797 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2798 arvif->vdev_id, ret);
2799 return;
2802 arvif->is_up = true;
2804 /* Workaround: Some firmware revisions (tested with qca6174
2805 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2806 * poked with peer param command.
2808 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2809 WMI_PEER_DUMMY_VAR, 1);
2810 if (ret) {
2811 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2812 arvif->bssid, arvif->vdev_id, ret);
2813 return;
2817 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2818 struct ieee80211_vif *vif)
2820 struct ath10k *ar = hw->priv;
2821 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2822 struct ieee80211_sta_vht_cap vht_cap = {};
2823 int ret;
2825 lockdep_assert_held(&ar->conf_mutex);
2827 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2828 arvif->vdev_id, arvif->bssid);
2830 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2831 if (ret)
2832 ath10k_warn(ar, "failed to down vdev %i: %d\n",
2833 arvif->vdev_id, ret);
2835 arvif->def_wep_key_idx = -1;
2837 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2838 if (ret) {
2839 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2840 arvif->vdev_id, ret);
2841 return;
2844 arvif->is_up = false;
2846 cancel_delayed_work_sync(&arvif->connection_loss_work);
2849 static int ath10k_station_assoc(struct ath10k *ar,
2850 struct ieee80211_vif *vif,
2851 struct ieee80211_sta *sta,
2852 bool reassoc)
2854 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2855 struct wmi_peer_assoc_complete_arg peer_arg;
2856 int ret = 0;
2858 lockdep_assert_held(&ar->conf_mutex);
2860 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2861 if (ret) {
2862 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2863 sta->addr, arvif->vdev_id, ret);
2864 return ret;
2867 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2868 if (ret) {
2869 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2870 sta->addr, arvif->vdev_id, ret);
2871 return ret;
2874 /* Re-assoc is run only to update supported rates for given station. It
2875 * doesn't make much sense to reconfigure the peer completely.
2877 if (!reassoc) {
2878 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2879 &sta->ht_cap);
2880 if (ret) {
2881 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2882 arvif->vdev_id, ret);
2883 return ret;
2886 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2887 if (ret) {
2888 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2889 sta->addr, arvif->vdev_id, ret);
2890 return ret;
2893 if (!sta->wme) {
2894 arvif->num_legacy_stations++;
2895 ret = ath10k_recalc_rtscts_prot(arvif);
2896 if (ret) {
2897 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2898 arvif->vdev_id, ret);
2899 return ret;
2903 /* Plumb cached keys only for static WEP */
2904 if (arvif->def_wep_key_idx != -1) {
2905 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2906 if (ret) {
2907 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2908 arvif->vdev_id, ret);
2909 return ret;
2914 return ret;
2917 static int ath10k_station_disassoc(struct ath10k *ar,
2918 struct ieee80211_vif *vif,
2919 struct ieee80211_sta *sta)
2921 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2922 int ret = 0;
2924 lockdep_assert_held(&ar->conf_mutex);
2926 if (!sta->wme) {
2927 arvif->num_legacy_stations--;
2928 ret = ath10k_recalc_rtscts_prot(arvif);
2929 if (ret) {
2930 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2931 arvif->vdev_id, ret);
2932 return ret;
2936 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2937 if (ret) {
2938 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2939 arvif->vdev_id, ret);
2940 return ret;
2943 return ret;
2946 /**************/
2947 /* Regulatory */
2948 /**************/
2950 static int ath10k_update_channel_list(struct ath10k *ar)
2952 struct ieee80211_hw *hw = ar->hw;
2953 struct ieee80211_supported_band **bands;
2954 enum nl80211_band band;
2955 struct ieee80211_channel *channel;
2956 struct wmi_scan_chan_list_arg arg = {0};
2957 struct wmi_channel_arg *ch;
2958 bool passive;
2959 int len;
2960 int ret;
2961 int i;
2963 lockdep_assert_held(&ar->conf_mutex);
2965 bands = hw->wiphy->bands;
2966 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2967 if (!bands[band])
2968 continue;
2970 for (i = 0; i < bands[band]->n_channels; i++) {
2971 if (bands[band]->channels[i].flags &
2972 IEEE80211_CHAN_DISABLED)
2973 continue;
2975 arg.n_channels++;
2979 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2980 arg.channels = kzalloc(len, GFP_KERNEL);
2981 if (!arg.channels)
2982 return -ENOMEM;
2984 ch = arg.channels;
2985 for (band = 0; band < NUM_NL80211_BANDS; band++) {
2986 if (!bands[band])
2987 continue;
2989 for (i = 0; i < bands[band]->n_channels; i++) {
2990 channel = &bands[band]->channels[i];
2992 if (channel->flags & IEEE80211_CHAN_DISABLED)
2993 continue;
2995 ch->allow_ht = true;
2997 /* FIXME: when should we really allow VHT? */
2998 ch->allow_vht = true;
3000 ch->allow_ibss =
3001 !(channel->flags & IEEE80211_CHAN_NO_IR);
3003 ch->ht40plus =
3004 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
3006 ch->chan_radar =
3007 !!(channel->flags & IEEE80211_CHAN_RADAR);
3009 passive = channel->flags & IEEE80211_CHAN_NO_IR;
3010 ch->passive = passive;
3012 ch->freq = channel->center_freq;
3013 ch->band_center_freq1 = channel->center_freq;
3014 ch->min_power = 0;
3015 ch->max_power = channel->max_power * 2;
3016 ch->max_reg_power = channel->max_reg_power * 2;
3017 ch->max_antenna_gain = channel->max_antenna_gain * 2;
3018 ch->reg_class_id = 0; /* FIXME */
3020 /* FIXME: why use only legacy modes, why not any
3021 * HT/VHT modes? Would that even make any
3022 * difference? */
3023 if (channel->band == NL80211_BAND_2GHZ)
3024 ch->mode = MODE_11G;
3025 else
3026 ch->mode = MODE_11A;
3028 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
3029 continue;
3031 ath10k_dbg(ar, ATH10K_DBG_WMI,
3032 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
3033 ch - arg.channels, arg.n_channels,
3034 ch->freq, ch->max_power, ch->max_reg_power,
3035 ch->max_antenna_gain, ch->mode);
3037 ch++;
3041 ret = ath10k_wmi_scan_chan_list(ar, &arg);
3042 kfree(arg.channels);
3044 return ret;
3047 static enum wmi_dfs_region
3048 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
3050 switch (dfs_region) {
3051 case NL80211_DFS_UNSET:
3052 return WMI_UNINIT_DFS_DOMAIN;
3053 case NL80211_DFS_FCC:
3054 return WMI_FCC_DFS_DOMAIN;
3055 case NL80211_DFS_ETSI:
3056 return WMI_ETSI_DFS_DOMAIN;
3057 case NL80211_DFS_JP:
3058 return WMI_MKK4_DFS_DOMAIN;
3060 return WMI_UNINIT_DFS_DOMAIN;
3063 static void ath10k_regd_update(struct ath10k *ar)
3065 struct reg_dmn_pair_mapping *regpair;
3066 int ret;
3067 enum wmi_dfs_region wmi_dfs_reg;
3068 enum nl80211_dfs_regions nl_dfs_reg;
3070 lockdep_assert_held(&ar->conf_mutex);
3072 ret = ath10k_update_channel_list(ar);
3073 if (ret)
3074 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
3076 regpair = ar->ath_common.regulatory.regpair;
3078 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3079 nl_dfs_reg = ar->dfs_detector->region;
3080 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
3081 } else {
3082 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
3085 /* Target allows setting up per-band regdomain but ath_common provides
3086 * a combined one only */
3087 ret = ath10k_wmi_pdev_set_regdomain(ar,
3088 regpair->reg_domain,
3089 regpair->reg_domain, /* 2ghz */
3090 regpair->reg_domain, /* 5ghz */
3091 regpair->reg_2ghz_ctl,
3092 regpair->reg_5ghz_ctl,
3093 wmi_dfs_reg);
3094 if (ret)
3095 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
3098 static void ath10k_reg_notifier(struct wiphy *wiphy,
3099 struct regulatory_request *request)
3101 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
3102 struct ath10k *ar = hw->priv;
3103 bool result;
3105 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
3107 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
3108 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
3109 request->dfs_region);
3110 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
3111 request->dfs_region);
3112 if (!result)
3113 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
3114 request->dfs_region);
3117 mutex_lock(&ar->conf_mutex);
3118 if (ar->state == ATH10K_STATE_ON)
3119 ath10k_regd_update(ar);
3120 mutex_unlock(&ar->conf_mutex);
3123 /***************/
3124 /* TX handlers */
3125 /***************/
3127 enum ath10k_mac_tx_path {
3128 ATH10K_MAC_TX_HTT,
3129 ATH10K_MAC_TX_HTT_MGMT,
3130 ATH10K_MAC_TX_WMI_MGMT,
3131 ATH10K_MAC_TX_UNKNOWN,
3134 void ath10k_mac_tx_lock(struct ath10k *ar, int reason)
3136 lockdep_assert_held(&ar->htt.tx_lock);
3138 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3139 ar->tx_paused |= BIT(reason);
3140 ieee80211_stop_queues(ar->hw);
3143 static void ath10k_mac_tx_unlock_iter(void *data, u8 *mac,
3144 struct ieee80211_vif *vif)
3146 struct ath10k *ar = data;
3147 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3149 if (arvif->tx_paused)
3150 return;
3152 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3155 void ath10k_mac_tx_unlock(struct ath10k *ar, int reason)
3157 lockdep_assert_held(&ar->htt.tx_lock);
3159 WARN_ON(reason >= ATH10K_TX_PAUSE_MAX);
3160 ar->tx_paused &= ~BIT(reason);
3162 if (ar->tx_paused)
3163 return;
3165 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3166 IEEE80211_IFACE_ITER_RESUME_ALL,
3167 ath10k_mac_tx_unlock_iter,
3168 ar);
3170 ieee80211_wake_queue(ar->hw, ar->hw->offchannel_tx_hw_queue);
3173 void ath10k_mac_vif_tx_lock(struct ath10k_vif *arvif, int reason)
3175 struct ath10k *ar = arvif->ar;
3177 lockdep_assert_held(&ar->htt.tx_lock);
3179 WARN_ON(reason >= BITS_PER_LONG);
3180 arvif->tx_paused |= BIT(reason);
3181 ieee80211_stop_queue(ar->hw, arvif->vdev_id);
3184 void ath10k_mac_vif_tx_unlock(struct ath10k_vif *arvif, int reason)
3186 struct ath10k *ar = arvif->ar;
3188 lockdep_assert_held(&ar->htt.tx_lock);
3190 WARN_ON(reason >= BITS_PER_LONG);
3191 arvif->tx_paused &= ~BIT(reason);
3193 if (ar->tx_paused)
3194 return;
3196 if (arvif->tx_paused)
3197 return;
3199 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
3202 static void ath10k_mac_vif_handle_tx_pause(struct ath10k_vif *arvif,
3203 enum wmi_tlv_tx_pause_id pause_id,
3204 enum wmi_tlv_tx_pause_action action)
3206 struct ath10k *ar = arvif->ar;
3208 lockdep_assert_held(&ar->htt.tx_lock);
3210 switch (action) {
3211 case WMI_TLV_TX_PAUSE_ACTION_STOP:
3212 ath10k_mac_vif_tx_lock(arvif, pause_id);
3213 break;
3214 case WMI_TLV_TX_PAUSE_ACTION_WAKE:
3215 ath10k_mac_vif_tx_unlock(arvif, pause_id);
3216 break;
3217 default:
3218 ath10k_dbg(ar, ATH10K_DBG_BOOT,
3219 "received unknown tx pause action %d on vdev %i, ignoring\n",
3220 action, arvif->vdev_id);
3221 break;
3225 struct ath10k_mac_tx_pause {
3226 u32 vdev_id;
3227 enum wmi_tlv_tx_pause_id pause_id;
3228 enum wmi_tlv_tx_pause_action action;
3231 static void ath10k_mac_handle_tx_pause_iter(void *data, u8 *mac,
3232 struct ieee80211_vif *vif)
3234 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3235 struct ath10k_mac_tx_pause *arg = data;
3237 if (arvif->vdev_id != arg->vdev_id)
3238 return;
3240 ath10k_mac_vif_handle_tx_pause(arvif, arg->pause_id, arg->action);
3243 void ath10k_mac_handle_tx_pause_vdev(struct ath10k *ar, u32 vdev_id,
3244 enum wmi_tlv_tx_pause_id pause_id,
3245 enum wmi_tlv_tx_pause_action action)
3247 struct ath10k_mac_tx_pause arg = {
3248 .vdev_id = vdev_id,
3249 .pause_id = pause_id,
3250 .action = action,
3253 spin_lock_bh(&ar->htt.tx_lock);
3254 ieee80211_iterate_active_interfaces_atomic(ar->hw,
3255 IEEE80211_IFACE_ITER_RESUME_ALL,
3256 ath10k_mac_handle_tx_pause_iter,
3257 &arg);
3258 spin_unlock_bh(&ar->htt.tx_lock);
3261 static enum ath10k_hw_txrx_mode
3262 ath10k_mac_tx_h_get_txmode(struct ath10k *ar,
3263 struct ieee80211_vif *vif,
3264 struct ieee80211_sta *sta,
3265 struct sk_buff *skb)
3267 const struct ieee80211_hdr *hdr = (void *)skb->data;
3268 __le16 fc = hdr->frame_control;
3270 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
3271 return ATH10K_HW_TXRX_RAW;
3273 if (ieee80211_is_mgmt(fc))
3274 return ATH10K_HW_TXRX_MGMT;
3276 /* Workaround:
3278 * NullFunc frames are mostly used to ping if a client or AP are still
3279 * reachable and responsive. This implies tx status reports must be
3280 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
3281 * come to a conclusion that the other end disappeared and tear down
3282 * BSS connection or it can never disconnect from BSS/client (which is
3283 * the case).
3285 * Firmware with HTT older than 3.0 delivers incorrect tx status for
3286 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
3287 * which seems to deliver correct tx reports for NullFunc frames. The
3288 * downside of using it is it ignores client powersave state so it can
3289 * end up disconnecting sleeping clients in AP mode. It should fix STA
3290 * mode though because AP don't sleep.
3292 if (ar->htt.target_version_major < 3 &&
3293 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
3294 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3295 ar->running_fw->fw_file.fw_features))
3296 return ATH10K_HW_TXRX_MGMT;
3298 /* Workaround:
3300 * Some wmi-tlv firmwares for qca6174 have broken Tx key selection for
3301 * NativeWifi txmode - it selects AP key instead of peer key. It seems
3302 * to work with Ethernet txmode so use it.
3304 * FIXME: Check if raw mode works with TDLS.
3306 if (ieee80211_is_data_present(fc) && sta && sta->tdls)
3307 return ATH10K_HW_TXRX_ETHERNET;
3309 if (test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
3310 return ATH10K_HW_TXRX_RAW;
3312 return ATH10K_HW_TXRX_NATIVE_WIFI;
3315 static bool ath10k_tx_h_use_hwcrypto(struct ieee80211_vif *vif,
3316 struct sk_buff *skb)
3318 const struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3319 const struct ieee80211_hdr *hdr = (void *)skb->data;
3320 const u32 mask = IEEE80211_TX_INTFL_DONT_ENCRYPT |
3321 IEEE80211_TX_CTL_INJECTED;
3323 if (!ieee80211_has_protected(hdr->frame_control))
3324 return false;
3326 if ((info->flags & mask) == mask)
3327 return false;
3329 if (vif)
3330 return !ath10k_vif_to_arvif(vif)->nohwcrypt;
3332 return true;
3335 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
3336 * Control in the header.
3338 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
3340 struct ieee80211_hdr *hdr = (void *)skb->data;
3341 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3342 u8 *qos_ctl;
3344 if (!ieee80211_is_data_qos(hdr->frame_control))
3345 return;
3347 qos_ctl = ieee80211_get_qos_ctl(hdr);
3348 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
3349 skb->data, (void *)qos_ctl - (void *)skb->data);
3350 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
3352 /* Some firmware revisions don't handle sending QoS NullFunc well.
3353 * These frames are mainly used for CQM purposes so it doesn't really
3354 * matter whether QoS NullFunc or NullFunc are sent.
3356 hdr = (void *)skb->data;
3357 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
3358 cb->flags &= ~ATH10K_SKB_F_QOS;
3360 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
3363 static void ath10k_tx_h_8023(struct sk_buff *skb)
3365 struct ieee80211_hdr *hdr;
3366 struct rfc1042_hdr *rfc1042;
3367 struct ethhdr *eth;
3368 size_t hdrlen;
3369 u8 da[ETH_ALEN];
3370 u8 sa[ETH_ALEN];
3371 __be16 type;
3373 hdr = (void *)skb->data;
3374 hdrlen = ieee80211_hdrlen(hdr->frame_control);
3375 rfc1042 = (void *)skb->data + hdrlen;
3377 ether_addr_copy(da, ieee80211_get_DA(hdr));
3378 ether_addr_copy(sa, ieee80211_get_SA(hdr));
3379 type = rfc1042->snap_type;
3381 skb_pull(skb, hdrlen + sizeof(*rfc1042));
3382 skb_push(skb, sizeof(*eth));
3384 eth = (void *)skb->data;
3385 ether_addr_copy(eth->h_dest, da);
3386 ether_addr_copy(eth->h_source, sa);
3387 eth->h_proto = type;
3390 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
3391 struct ieee80211_vif *vif,
3392 struct sk_buff *skb)
3394 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
3395 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3397 /* This is case only for P2P_GO */
3398 if (vif->type != NL80211_IFTYPE_AP || !vif->p2p)
3399 return;
3401 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
3402 spin_lock_bh(&ar->data_lock);
3403 if (arvif->u.ap.noa_data)
3404 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
3405 GFP_ATOMIC))
3406 memcpy(skb_put(skb, arvif->u.ap.noa_len),
3407 arvif->u.ap.noa_data,
3408 arvif->u.ap.noa_len);
3409 spin_unlock_bh(&ar->data_lock);
3413 static void ath10k_mac_tx_h_fill_cb(struct ath10k *ar,
3414 struct ieee80211_vif *vif,
3415 struct ieee80211_txq *txq,
3416 struct sk_buff *skb)
3418 struct ieee80211_hdr *hdr = (void *)skb->data;
3419 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
3421 cb->flags = 0;
3422 if (!ath10k_tx_h_use_hwcrypto(vif, skb))
3423 cb->flags |= ATH10K_SKB_F_NO_HWCRYPT;
3425 if (ieee80211_is_mgmt(hdr->frame_control))
3426 cb->flags |= ATH10K_SKB_F_MGMT;
3428 if (ieee80211_is_data_qos(hdr->frame_control))
3429 cb->flags |= ATH10K_SKB_F_QOS;
3431 cb->vif = vif;
3432 cb->txq = txq;
3435 bool ath10k_mac_tx_frm_has_freq(struct ath10k *ar)
3437 /* FIXME: Not really sure since when the behaviour changed. At some
3438 * point new firmware stopped requiring creation of peer entries for
3439 * offchannel tx (and actually creating them causes issues with wmi-htc
3440 * tx credit replenishment and reliability). Assuming it's at least 3.4
3441 * because that's when the `freq` was introduced to TX_FRM HTT command.
3443 return (ar->htt.target_version_major >= 3 &&
3444 ar->htt.target_version_minor >= 4 &&
3445 ar->running_fw->fw_file.htt_op_version == ATH10K_FW_HTT_OP_VERSION_TLV);
3448 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
3450 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
3451 int ret = 0;
3453 spin_lock_bh(&ar->data_lock);
3455 if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
3456 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
3457 ret = -ENOSPC;
3458 goto unlock;
3461 __skb_queue_tail(q, skb);
3462 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
3464 unlock:
3465 spin_unlock_bh(&ar->data_lock);
3467 return ret;
3470 static enum ath10k_mac_tx_path
3471 ath10k_mac_tx_h_get_txpath(struct ath10k *ar,
3472 struct sk_buff *skb,
3473 enum ath10k_hw_txrx_mode txmode)
3475 switch (txmode) {
3476 case ATH10K_HW_TXRX_RAW:
3477 case ATH10K_HW_TXRX_NATIVE_WIFI:
3478 case ATH10K_HW_TXRX_ETHERNET:
3479 return ATH10K_MAC_TX_HTT;
3480 case ATH10K_HW_TXRX_MGMT:
3481 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
3482 ar->running_fw->fw_file.fw_features))
3483 return ATH10K_MAC_TX_WMI_MGMT;
3484 else if (ar->htt.target_version_major >= 3)
3485 return ATH10K_MAC_TX_HTT;
3486 else
3487 return ATH10K_MAC_TX_HTT_MGMT;
3490 return ATH10K_MAC_TX_UNKNOWN;
3493 static int ath10k_mac_tx_submit(struct ath10k *ar,
3494 enum ath10k_hw_txrx_mode txmode,
3495 enum ath10k_mac_tx_path txpath,
3496 struct sk_buff *skb)
3498 struct ath10k_htt *htt = &ar->htt;
3499 int ret = -EINVAL;
3501 switch (txpath) {
3502 case ATH10K_MAC_TX_HTT:
3503 ret = ath10k_htt_tx(htt, txmode, skb);
3504 break;
3505 case ATH10K_MAC_TX_HTT_MGMT:
3506 ret = ath10k_htt_mgmt_tx(htt, skb);
3507 break;
3508 case ATH10K_MAC_TX_WMI_MGMT:
3509 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
3510 break;
3511 case ATH10K_MAC_TX_UNKNOWN:
3512 WARN_ON_ONCE(1);
3513 ret = -EINVAL;
3514 break;
3517 if (ret) {
3518 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
3519 ret);
3520 ieee80211_free_txskb(ar->hw, skb);
3523 return ret;
3526 /* This function consumes the sk_buff regardless of return value as far as
3527 * caller is concerned so no freeing is necessary afterwards.
3529 static int ath10k_mac_tx(struct ath10k *ar,
3530 struct ieee80211_vif *vif,
3531 struct ieee80211_sta *sta,
3532 enum ath10k_hw_txrx_mode txmode,
3533 enum ath10k_mac_tx_path txpath,
3534 struct sk_buff *skb)
3536 struct ieee80211_hw *hw = ar->hw;
3537 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
3538 int ret;
3540 /* We should disable CCK RATE due to P2P */
3541 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3542 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3544 switch (txmode) {
3545 case ATH10K_HW_TXRX_MGMT:
3546 case ATH10K_HW_TXRX_NATIVE_WIFI:
3547 ath10k_tx_h_nwifi(hw, skb);
3548 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3549 ath10k_tx_h_seq_no(vif, skb);
3550 break;
3551 case ATH10K_HW_TXRX_ETHERNET:
3552 ath10k_tx_h_8023(skb);
3553 break;
3554 case ATH10K_HW_TXRX_RAW:
3555 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
3556 WARN_ON_ONCE(1);
3557 ieee80211_free_txskb(hw, skb);
3558 return -ENOTSUPP;
3562 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3563 if (!ath10k_mac_tx_frm_has_freq(ar)) {
3564 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %pK\n",
3565 skb);
3567 skb_queue_tail(&ar->offchan_tx_queue, skb);
3568 ieee80211_queue_work(hw, &ar->offchan_tx_work);
3569 return 0;
3573 ret = ath10k_mac_tx_submit(ar, txmode, txpath, skb);
3574 if (ret) {
3575 ath10k_warn(ar, "failed to submit frame: %d\n", ret);
3576 return ret;
3579 return 0;
3582 void ath10k_offchan_tx_purge(struct ath10k *ar)
3584 struct sk_buff *skb;
3586 for (;;) {
3587 skb = skb_dequeue(&ar->offchan_tx_queue);
3588 if (!skb)
3589 break;
3591 ieee80211_free_txskb(ar->hw, skb);
3595 void ath10k_offchan_tx_work(struct work_struct *work)
3597 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
3598 struct ath10k_peer *peer;
3599 struct ath10k_vif *arvif;
3600 enum ath10k_hw_txrx_mode txmode;
3601 enum ath10k_mac_tx_path txpath;
3602 struct ieee80211_hdr *hdr;
3603 struct ieee80211_vif *vif;
3604 struct ieee80211_sta *sta;
3605 struct sk_buff *skb;
3606 const u8 *peer_addr;
3607 int vdev_id;
3608 int ret;
3609 unsigned long time_left;
3610 bool tmp_peer_created = false;
3612 /* FW requirement: We must create a peer before FW will send out
3613 * an offchannel frame. Otherwise the frame will be stuck and
3614 * never transmitted. We delete the peer upon tx completion.
3615 * It is unlikely that a peer for offchannel tx will already be
3616 * present. However it may be in some rare cases so account for that.
3617 * Otherwise we might remove a legitimate peer and break stuff. */
3619 for (;;) {
3620 skb = skb_dequeue(&ar->offchan_tx_queue);
3621 if (!skb)
3622 break;
3624 mutex_lock(&ar->conf_mutex);
3626 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %pK\n",
3627 skb);
3629 hdr = (struct ieee80211_hdr *)skb->data;
3630 peer_addr = ieee80211_get_DA(hdr);
3632 spin_lock_bh(&ar->data_lock);
3633 vdev_id = ar->scan.vdev_id;
3634 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
3635 spin_unlock_bh(&ar->data_lock);
3637 if (peer)
3638 /* FIXME: should this use ath10k_warn()? */
3639 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
3640 peer_addr, vdev_id);
3642 if (!peer) {
3643 ret = ath10k_peer_create(ar, NULL, NULL, vdev_id,
3644 peer_addr,
3645 WMI_PEER_TYPE_DEFAULT);
3646 if (ret)
3647 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
3648 peer_addr, vdev_id, ret);
3649 tmp_peer_created = (ret == 0);
3652 spin_lock_bh(&ar->data_lock);
3653 reinit_completion(&ar->offchan_tx_completed);
3654 ar->offchan_tx_skb = skb;
3655 spin_unlock_bh(&ar->data_lock);
3657 /* It's safe to access vif and sta - conf_mutex guarantees that
3658 * sta_state() and remove_interface() are locked exclusively
3659 * out wrt to this offchannel worker.
3661 arvif = ath10k_get_arvif(ar, vdev_id);
3662 if (arvif) {
3663 vif = arvif->vif;
3664 sta = ieee80211_find_sta(vif, peer_addr);
3665 } else {
3666 vif = NULL;
3667 sta = NULL;
3670 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3671 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3673 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
3674 if (ret) {
3675 ath10k_warn(ar, "failed to transmit offchannel frame: %d\n",
3676 ret);
3677 /* not serious */
3680 time_left =
3681 wait_for_completion_timeout(&ar->offchan_tx_completed, 3 * HZ);
3682 if (time_left == 0)
3683 ath10k_warn(ar, "timed out waiting for offchannel skb %pK\n",
3684 skb);
3686 if (!peer && tmp_peer_created) {
3687 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
3688 if (ret)
3689 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
3690 peer_addr, vdev_id, ret);
3693 mutex_unlock(&ar->conf_mutex);
3697 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
3699 struct sk_buff *skb;
3701 for (;;) {
3702 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3703 if (!skb)
3704 break;
3706 ieee80211_free_txskb(ar->hw, skb);
3710 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
3712 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
3713 struct sk_buff *skb;
3714 int ret;
3716 for (;;) {
3717 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
3718 if (!skb)
3719 break;
3721 ret = ath10k_wmi_mgmt_tx(ar, skb);
3722 if (ret) {
3723 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
3724 ret);
3725 ieee80211_free_txskb(ar->hw, skb);
3730 static void ath10k_mac_txq_init(struct ieee80211_txq *txq)
3732 struct ath10k_txq *artxq;
3734 if (!txq)
3735 return;
3737 artxq = (void *)txq->drv_priv;
3738 INIT_LIST_HEAD(&artxq->list);
3741 static void ath10k_mac_txq_unref(struct ath10k *ar, struct ieee80211_txq *txq)
3743 struct ath10k_txq *artxq;
3744 struct ath10k_skb_cb *cb;
3745 struct sk_buff *msdu;
3746 int msdu_id;
3748 if (!txq)
3749 return;
3751 artxq = (void *)txq->drv_priv;
3752 spin_lock_bh(&ar->txqs_lock);
3753 if (!list_empty(&artxq->list))
3754 list_del_init(&artxq->list);
3755 spin_unlock_bh(&ar->txqs_lock);
3757 spin_lock_bh(&ar->htt.tx_lock);
3758 idr_for_each_entry(&ar->htt.pending_tx, msdu, msdu_id) {
3759 cb = ATH10K_SKB_CB(msdu);
3760 if (cb->txq == txq)
3761 cb->txq = NULL;
3763 spin_unlock_bh(&ar->htt.tx_lock);
3766 struct ieee80211_txq *ath10k_mac_txq_lookup(struct ath10k *ar,
3767 u16 peer_id,
3768 u8 tid)
3770 struct ath10k_peer *peer;
3772 lockdep_assert_held(&ar->data_lock);
3774 peer = ar->peer_map[peer_id];
3775 if (!peer)
3776 return NULL;
3778 if (peer->sta)
3779 return peer->sta->txq[tid];
3780 else if (peer->vif)
3781 return peer->vif->txq;
3782 else
3783 return NULL;
3786 static bool ath10k_mac_tx_can_push(struct ieee80211_hw *hw,
3787 struct ieee80211_txq *txq)
3789 struct ath10k *ar = hw->priv;
3790 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3792 /* No need to get locks */
3794 if (ar->htt.tx_q_state.mode == HTT_TX_MODE_SWITCH_PUSH)
3795 return true;
3797 if (ar->htt.num_pending_tx < ar->htt.tx_q_state.num_push_allowed)
3798 return true;
3800 if (artxq->num_fw_queued < artxq->num_push_allowed)
3801 return true;
3803 return false;
3806 int ath10k_mac_tx_push_txq(struct ieee80211_hw *hw,
3807 struct ieee80211_txq *txq)
3809 struct ath10k *ar = hw->priv;
3810 struct ath10k_htt *htt = &ar->htt;
3811 struct ath10k_txq *artxq = (void *)txq->drv_priv;
3812 struct ieee80211_vif *vif = txq->vif;
3813 struct ieee80211_sta *sta = txq->sta;
3814 enum ath10k_hw_txrx_mode txmode;
3815 enum ath10k_mac_tx_path txpath;
3816 struct sk_buff *skb;
3817 struct ieee80211_hdr *hdr;
3818 size_t skb_len;
3819 bool is_mgmt, is_presp;
3820 int ret;
3822 spin_lock_bh(&ar->htt.tx_lock);
3823 ret = ath10k_htt_tx_inc_pending(htt);
3824 spin_unlock_bh(&ar->htt.tx_lock);
3826 if (ret)
3827 return ret;
3829 skb = ieee80211_tx_dequeue(hw, txq);
3830 if (!skb) {
3831 spin_lock_bh(&ar->htt.tx_lock);
3832 ath10k_htt_tx_dec_pending(htt);
3833 spin_unlock_bh(&ar->htt.tx_lock);
3835 return -ENOENT;
3838 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
3840 skb_len = skb->len;
3841 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
3842 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
3843 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
3845 if (is_mgmt) {
3846 hdr = (struct ieee80211_hdr *)skb->data;
3847 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
3849 spin_lock_bh(&ar->htt.tx_lock);
3850 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
3852 if (ret) {
3853 ath10k_htt_tx_dec_pending(htt);
3854 spin_unlock_bh(&ar->htt.tx_lock);
3855 return ret;
3857 spin_unlock_bh(&ar->htt.tx_lock);
3860 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
3861 if (unlikely(ret)) {
3862 ath10k_warn(ar, "failed to push frame: %d\n", ret);
3864 spin_lock_bh(&ar->htt.tx_lock);
3865 ath10k_htt_tx_dec_pending(htt);
3866 if (is_mgmt)
3867 ath10k_htt_tx_mgmt_dec_pending(htt);
3868 spin_unlock_bh(&ar->htt.tx_lock);
3870 return ret;
3873 spin_lock_bh(&ar->htt.tx_lock);
3874 artxq->num_fw_queued++;
3875 spin_unlock_bh(&ar->htt.tx_lock);
3877 return skb_len;
3880 void ath10k_mac_tx_push_pending(struct ath10k *ar)
3882 struct ieee80211_hw *hw = ar->hw;
3883 struct ieee80211_txq *txq;
3884 struct ath10k_txq *artxq;
3885 struct ath10k_txq *last;
3886 int ret;
3887 int max;
3889 if (ar->htt.num_pending_tx >= (ar->htt.max_num_pending_tx / 2))
3890 return;
3892 spin_lock_bh(&ar->txqs_lock);
3893 rcu_read_lock();
3895 last = list_last_entry(&ar->txqs, struct ath10k_txq, list);
3896 while (!list_empty(&ar->txqs)) {
3897 artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
3898 txq = container_of((void *)artxq, struct ieee80211_txq,
3899 drv_priv);
3901 /* Prevent aggressive sta/tid taking over tx queue */
3902 max = 16;
3903 ret = 0;
3904 while (ath10k_mac_tx_can_push(hw, txq) && max--) {
3905 ret = ath10k_mac_tx_push_txq(hw, txq);
3906 if (ret < 0)
3907 break;
3910 list_del_init(&artxq->list);
3911 if (ret != -ENOENT)
3912 list_add_tail(&artxq->list, &ar->txqs);
3914 ath10k_htt_tx_txq_update(hw, txq);
3916 if (artxq == last || (ret < 0 && ret != -ENOENT))
3917 break;
3920 rcu_read_unlock();
3921 spin_unlock_bh(&ar->txqs_lock);
3924 /************/
3925 /* Scanning */
3926 /************/
3928 void __ath10k_scan_finish(struct ath10k *ar)
3930 lockdep_assert_held(&ar->data_lock);
3932 switch (ar->scan.state) {
3933 case ATH10K_SCAN_IDLE:
3934 break;
3935 case ATH10K_SCAN_RUNNING:
3936 case ATH10K_SCAN_ABORTING:
3937 if (!ar->scan.is_roc) {
3938 struct cfg80211_scan_info info = {
3939 .aborted = (ar->scan.state ==
3940 ATH10K_SCAN_ABORTING),
3943 ieee80211_scan_completed(ar->hw, &info);
3944 } else if (ar->scan.roc_notify) {
3945 ieee80211_remain_on_channel_expired(ar->hw);
3947 /* fall through */
3948 case ATH10K_SCAN_STARTING:
3949 ar->scan.state = ATH10K_SCAN_IDLE;
3950 ar->scan_channel = NULL;
3951 ar->scan.roc_freq = 0;
3952 ath10k_offchan_tx_purge(ar);
3953 cancel_delayed_work(&ar->scan.timeout);
3954 complete(&ar->scan.completed);
3955 break;
3959 void ath10k_scan_finish(struct ath10k *ar)
3961 spin_lock_bh(&ar->data_lock);
3962 __ath10k_scan_finish(ar);
3963 spin_unlock_bh(&ar->data_lock);
3966 static int ath10k_scan_stop(struct ath10k *ar)
3968 struct wmi_stop_scan_arg arg = {
3969 .req_id = 1, /* FIXME */
3970 .req_type = WMI_SCAN_STOP_ONE,
3971 .u.scan_id = ATH10K_SCAN_ID,
3973 int ret;
3975 lockdep_assert_held(&ar->conf_mutex);
3977 ret = ath10k_wmi_stop_scan(ar, &arg);
3978 if (ret) {
3979 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
3980 goto out;
3983 ret = wait_for_completion_timeout(&ar->scan.completed, 3 * HZ);
3984 if (ret == 0) {
3985 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
3986 ret = -ETIMEDOUT;
3987 } else if (ret > 0) {
3988 ret = 0;
3991 out:
3992 /* Scan state should be updated upon scan completion but in case
3993 * firmware fails to deliver the event (for whatever reason) it is
3994 * desired to clean up scan state anyway. Firmware may have just
3995 * dropped the scan completion event delivery due to transport pipe
3996 * being overflown with data and/or it can recover on its own before
3997 * next scan request is submitted.
3999 spin_lock_bh(&ar->data_lock);
4000 if (ar->scan.state != ATH10K_SCAN_IDLE)
4001 __ath10k_scan_finish(ar);
4002 spin_unlock_bh(&ar->data_lock);
4004 return ret;
4007 static void ath10k_scan_abort(struct ath10k *ar)
4009 int ret;
4011 lockdep_assert_held(&ar->conf_mutex);
4013 spin_lock_bh(&ar->data_lock);
4015 switch (ar->scan.state) {
4016 case ATH10K_SCAN_IDLE:
4017 /* This can happen if timeout worker kicked in and called
4018 * abortion while scan completion was being processed.
4020 break;
4021 case ATH10K_SCAN_STARTING:
4022 case ATH10K_SCAN_ABORTING:
4023 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
4024 ath10k_scan_state_str(ar->scan.state),
4025 ar->scan.state);
4026 break;
4027 case ATH10K_SCAN_RUNNING:
4028 ar->scan.state = ATH10K_SCAN_ABORTING;
4029 spin_unlock_bh(&ar->data_lock);
4031 ret = ath10k_scan_stop(ar);
4032 if (ret)
4033 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
4035 spin_lock_bh(&ar->data_lock);
4036 break;
4039 spin_unlock_bh(&ar->data_lock);
4042 void ath10k_scan_timeout_work(struct work_struct *work)
4044 struct ath10k *ar = container_of(work, struct ath10k,
4045 scan.timeout.work);
4047 mutex_lock(&ar->conf_mutex);
4048 ath10k_scan_abort(ar);
4049 mutex_unlock(&ar->conf_mutex);
4052 static int ath10k_start_scan(struct ath10k *ar,
4053 const struct wmi_start_scan_arg *arg)
4055 int ret;
4057 lockdep_assert_held(&ar->conf_mutex);
4059 ret = ath10k_wmi_start_scan(ar, arg);
4060 if (ret)
4061 return ret;
4063 ret = wait_for_completion_timeout(&ar->scan.started, 1 * HZ);
4064 if (ret == 0) {
4065 ret = ath10k_scan_stop(ar);
4066 if (ret)
4067 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4069 return -ETIMEDOUT;
4072 /* If we failed to start the scan, return error code at
4073 * this point. This is probably due to some issue in the
4074 * firmware, but no need to wedge the driver due to that...
4076 spin_lock_bh(&ar->data_lock);
4077 if (ar->scan.state == ATH10K_SCAN_IDLE) {
4078 spin_unlock_bh(&ar->data_lock);
4079 return -EINVAL;
4081 spin_unlock_bh(&ar->data_lock);
4083 return 0;
4086 /**********************/
4087 /* mac80211 callbacks */
4088 /**********************/
4090 static void ath10k_mac_op_tx(struct ieee80211_hw *hw,
4091 struct ieee80211_tx_control *control,
4092 struct sk_buff *skb)
4094 struct ath10k *ar = hw->priv;
4095 struct ath10k_htt *htt = &ar->htt;
4096 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
4097 struct ieee80211_vif *vif = info->control.vif;
4098 struct ieee80211_sta *sta = control->sta;
4099 struct ieee80211_txq *txq = NULL;
4100 struct ieee80211_hdr *hdr = (void *)skb->data;
4101 enum ath10k_hw_txrx_mode txmode;
4102 enum ath10k_mac_tx_path txpath;
4103 bool is_htt;
4104 bool is_mgmt;
4105 bool is_presp;
4106 int ret;
4108 ath10k_mac_tx_h_fill_cb(ar, vif, txq, skb);
4110 txmode = ath10k_mac_tx_h_get_txmode(ar, vif, sta, skb);
4111 txpath = ath10k_mac_tx_h_get_txpath(ar, skb, txmode);
4112 is_htt = (txpath == ATH10K_MAC_TX_HTT ||
4113 txpath == ATH10K_MAC_TX_HTT_MGMT);
4114 is_mgmt = (txpath == ATH10K_MAC_TX_HTT_MGMT);
4116 if (is_htt) {
4117 spin_lock_bh(&ar->htt.tx_lock);
4118 is_presp = ieee80211_is_probe_resp(hdr->frame_control);
4120 ret = ath10k_htt_tx_inc_pending(htt);
4121 if (ret) {
4122 ath10k_warn(ar, "failed to increase tx pending count: %d, dropping\n",
4123 ret);
4124 spin_unlock_bh(&ar->htt.tx_lock);
4125 ieee80211_free_txskb(ar->hw, skb);
4126 return;
4129 ret = ath10k_htt_tx_mgmt_inc_pending(htt, is_mgmt, is_presp);
4130 if (ret) {
4131 ath10k_dbg(ar, ATH10K_DBG_MAC, "failed to increase tx mgmt pending count: %d, dropping\n",
4132 ret);
4133 ath10k_htt_tx_dec_pending(htt);
4134 spin_unlock_bh(&ar->htt.tx_lock);
4135 ieee80211_free_txskb(ar->hw, skb);
4136 return;
4138 spin_unlock_bh(&ar->htt.tx_lock);
4141 ret = ath10k_mac_tx(ar, vif, sta, txmode, txpath, skb);
4142 if (ret) {
4143 ath10k_warn(ar, "failed to transmit frame: %d\n", ret);
4144 if (is_htt) {
4145 spin_lock_bh(&ar->htt.tx_lock);
4146 ath10k_htt_tx_dec_pending(htt);
4147 if (is_mgmt)
4148 ath10k_htt_tx_mgmt_dec_pending(htt);
4149 spin_unlock_bh(&ar->htt.tx_lock);
4151 return;
4155 static void ath10k_mac_op_wake_tx_queue(struct ieee80211_hw *hw,
4156 struct ieee80211_txq *txq)
4158 struct ath10k *ar = hw->priv;
4159 struct ath10k_txq *artxq = (void *)txq->drv_priv;
4160 struct ieee80211_txq *f_txq;
4161 struct ath10k_txq *f_artxq;
4162 int ret = 0;
4163 int max = 16;
4165 spin_lock_bh(&ar->txqs_lock);
4166 if (list_empty(&artxq->list))
4167 list_add_tail(&artxq->list, &ar->txqs);
4169 f_artxq = list_first_entry(&ar->txqs, struct ath10k_txq, list);
4170 f_txq = container_of((void *)f_artxq, struct ieee80211_txq, drv_priv);
4171 list_del_init(&f_artxq->list);
4173 while (ath10k_mac_tx_can_push(hw, f_txq) && max--) {
4174 ret = ath10k_mac_tx_push_txq(hw, f_txq);
4175 if (ret)
4176 break;
4178 if (ret != -ENOENT)
4179 list_add_tail(&f_artxq->list, &ar->txqs);
4180 spin_unlock_bh(&ar->txqs_lock);
4182 ath10k_htt_tx_txq_update(hw, f_txq);
4183 ath10k_htt_tx_txq_update(hw, txq);
4186 /* Must not be called with conf_mutex held as workers can use that also. */
4187 void ath10k_drain_tx(struct ath10k *ar)
4189 /* make sure rcu-protected mac80211 tx path itself is drained */
4190 synchronize_net();
4192 ath10k_offchan_tx_purge(ar);
4193 ath10k_mgmt_over_wmi_tx_purge(ar);
4195 cancel_work_sync(&ar->offchan_tx_work);
4196 cancel_work_sync(&ar->wmi_mgmt_tx_work);
4199 void ath10k_halt(struct ath10k *ar)
4201 struct ath10k_vif *arvif;
4203 lockdep_assert_held(&ar->conf_mutex);
4205 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
4206 ar->filter_flags = 0;
4207 ar->monitor = false;
4208 ar->monitor_arvif = NULL;
4210 if (ar->monitor_started)
4211 ath10k_monitor_stop(ar);
4213 ar->monitor_started = false;
4214 ar->tx_paused = 0;
4216 ath10k_scan_finish(ar);
4217 ath10k_peer_cleanup_all(ar);
4218 ath10k_core_stop(ar);
4219 ath10k_hif_power_down(ar);
4221 spin_lock_bh(&ar->data_lock);
4222 list_for_each_entry(arvif, &ar->arvifs, list)
4223 ath10k_mac_vif_beacon_cleanup(arvif);
4224 spin_unlock_bh(&ar->data_lock);
4227 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
4229 struct ath10k *ar = hw->priv;
4231 mutex_lock(&ar->conf_mutex);
4233 *tx_ant = ar->cfg_tx_chainmask;
4234 *rx_ant = ar->cfg_rx_chainmask;
4236 mutex_unlock(&ar->conf_mutex);
4238 return 0;
4241 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
4243 /* It is not clear that allowing gaps in chainmask
4244 * is helpful. Probably it will not do what user
4245 * is hoping for, so warn in that case.
4247 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
4248 return;
4250 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
4251 dbg, cm);
4254 static int ath10k_mac_get_vht_cap_bf_sts(struct ath10k *ar)
4256 int nsts = ar->vht_cap_info;
4258 nsts &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4259 nsts >>= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4261 /* If firmware does not deliver to host number of space-time
4262 * streams supported, assume it support up to 4 BF STS and return
4263 * the value for VHT CAP: nsts-1)
4265 if (nsts == 0)
4266 return 3;
4268 return nsts;
4271 static int ath10k_mac_get_vht_cap_bf_sound_dim(struct ath10k *ar)
4273 int sound_dim = ar->vht_cap_info;
4275 sound_dim &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4276 sound_dim >>= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4278 /* If the sounding dimension is not advertised by the firmware,
4279 * let's use a default value of 1
4281 if (sound_dim == 0)
4282 return 1;
4284 return sound_dim;
4287 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
4289 struct ieee80211_sta_vht_cap vht_cap = {0};
4290 u16 mcs_map;
4291 u32 val;
4292 int i;
4294 vht_cap.vht_supported = 1;
4295 vht_cap.cap = ar->vht_cap_info;
4297 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4298 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
4299 val = ath10k_mac_get_vht_cap_bf_sts(ar);
4300 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
4301 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
4303 vht_cap.cap |= val;
4306 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4307 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
4308 val = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4309 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
4310 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
4312 vht_cap.cap |= val;
4315 mcs_map = 0;
4316 for (i = 0; i < 8; i++) {
4317 if ((i < ar->num_rf_chains) && (ar->cfg_tx_chainmask & BIT(i)))
4318 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i * 2);
4319 else
4320 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i * 2);
4323 if (ar->cfg_tx_chainmask <= 1)
4324 vht_cap.cap &= ~IEEE80211_VHT_CAP_TXSTBC;
4326 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
4327 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
4329 return vht_cap;
4332 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
4334 int i;
4335 struct ieee80211_sta_ht_cap ht_cap = {0};
4337 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
4338 return ht_cap;
4340 ht_cap.ht_supported = 1;
4341 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
4342 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
4343 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
4344 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
4345 ht_cap.cap |=
4346 WLAN_HT_CAP_SM_PS_DISABLED << IEEE80211_HT_CAP_SM_PS_SHIFT;
4348 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
4349 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
4351 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
4352 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
4354 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
4355 u32 smps;
4357 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
4358 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
4360 ht_cap.cap |= smps;
4363 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC && (ar->cfg_tx_chainmask > 1))
4364 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
4366 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
4367 u32 stbc;
4369 stbc = ar->ht_cap_info;
4370 stbc &= WMI_HT_CAP_RX_STBC;
4371 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
4372 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
4373 stbc &= IEEE80211_HT_CAP_RX_STBC;
4375 ht_cap.cap |= stbc;
4378 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
4379 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
4381 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
4382 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
4384 /* max AMSDU is implicitly taken from vht_cap_info */
4385 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
4386 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
4388 for (i = 0; i < ar->num_rf_chains; i++) {
4389 if (ar->cfg_rx_chainmask & BIT(i))
4390 ht_cap.mcs.rx_mask[i] = 0xFF;
4393 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
4395 return ht_cap;
4398 static void ath10k_mac_setup_ht_vht_cap(struct ath10k *ar)
4400 struct ieee80211_supported_band *band;
4401 struct ieee80211_sta_vht_cap vht_cap;
4402 struct ieee80211_sta_ht_cap ht_cap;
4404 ht_cap = ath10k_get_ht_cap(ar);
4405 vht_cap = ath10k_create_vht_cap(ar);
4407 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
4408 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
4409 band->ht_cap = ht_cap;
4411 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
4412 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
4413 band->ht_cap = ht_cap;
4414 band->vht_cap = vht_cap;
4418 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
4420 int ret;
4422 lockdep_assert_held(&ar->conf_mutex);
4424 ath10k_check_chain_mask(ar, tx_ant, "tx");
4425 ath10k_check_chain_mask(ar, rx_ant, "rx");
4427 ar->cfg_tx_chainmask = tx_ant;
4428 ar->cfg_rx_chainmask = rx_ant;
4430 if ((ar->state != ATH10K_STATE_ON) &&
4431 (ar->state != ATH10K_STATE_RESTARTED))
4432 return 0;
4434 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
4435 tx_ant);
4436 if (ret) {
4437 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
4438 ret, tx_ant);
4439 return ret;
4442 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
4443 rx_ant);
4444 if (ret) {
4445 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
4446 ret, rx_ant);
4447 return ret;
4450 /* Reload HT/VHT capability */
4451 ath10k_mac_setup_ht_vht_cap(ar);
4453 return 0;
4456 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
4458 struct ath10k *ar = hw->priv;
4459 int ret;
4461 mutex_lock(&ar->conf_mutex);
4462 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
4463 mutex_unlock(&ar->conf_mutex);
4464 return ret;
4467 static int ath10k_start(struct ieee80211_hw *hw)
4469 struct ath10k *ar = hw->priv;
4470 u32 param;
4471 int ret = 0;
4474 * This makes sense only when restarting hw. It is harmless to call
4475 * unconditionally. This is necessary to make sure no HTT/WMI tx
4476 * commands will be submitted while restarting.
4478 ath10k_drain_tx(ar);
4480 mutex_lock(&ar->conf_mutex);
4482 switch (ar->state) {
4483 case ATH10K_STATE_OFF:
4484 ar->state = ATH10K_STATE_ON;
4485 break;
4486 case ATH10K_STATE_RESTARTING:
4487 ar->state = ATH10K_STATE_RESTARTED;
4488 break;
4489 case ATH10K_STATE_ON:
4490 case ATH10K_STATE_RESTARTED:
4491 case ATH10K_STATE_WEDGED:
4492 WARN_ON(1);
4493 ret = -EINVAL;
4494 goto err;
4495 case ATH10K_STATE_UTF:
4496 ret = -EBUSY;
4497 goto err;
4500 ret = ath10k_hif_power_up(ar);
4501 if (ret) {
4502 ath10k_err(ar, "Could not init hif: %d\n", ret);
4503 goto err_off;
4506 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL,
4507 &ar->normal_mode_fw);
4508 if (ret) {
4509 ath10k_err(ar, "Could not init core: %d\n", ret);
4510 goto err_power_down;
4513 param = ar->wmi.pdev_param->pmf_qos;
4514 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4515 if (ret) {
4516 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
4517 goto err_core_stop;
4520 param = ar->wmi.pdev_param->dynamic_bw;
4521 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4522 if (ret) {
4523 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
4524 goto err_core_stop;
4527 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
4528 ret = ath10k_wmi_adaptive_qcs(ar, true);
4529 if (ret) {
4530 ath10k_warn(ar, "failed to enable adaptive qcs: %d\n",
4531 ret);
4532 goto err_core_stop;
4536 if (test_bit(WMI_SERVICE_BURST, ar->wmi.svc_map)) {
4537 param = ar->wmi.pdev_param->burst_enable;
4538 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4539 if (ret) {
4540 ath10k_warn(ar, "failed to disable burst: %d\n", ret);
4541 goto err_core_stop;
4545 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask, ar->cfg_rx_chainmask);
4548 * By default FW set ARP frames ac to voice (6). In that case ARP
4549 * exchange is not working properly for UAPSD enabled AP. ARP requests
4550 * which arrives with access category 0 are processed by network stack
4551 * and send back with access category 0, but FW changes access category
4552 * to 6. Set ARP frames access category to best effort (0) solves
4553 * this problem.
4556 param = ar->wmi.pdev_param->arp_ac_override;
4557 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4558 if (ret) {
4559 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
4560 ret);
4561 goto err_core_stop;
4564 if (test_bit(ATH10K_FW_FEATURE_SUPPORTS_ADAPTIVE_CCA,
4565 ar->running_fw->fw_file.fw_features)) {
4566 ret = ath10k_wmi_pdev_enable_adaptive_cca(ar, 1,
4567 WMI_CCA_DETECT_LEVEL_AUTO,
4568 WMI_CCA_DETECT_MARGIN_AUTO);
4569 if (ret) {
4570 ath10k_warn(ar, "failed to enable adaptive cca: %d\n",
4571 ret);
4572 goto err_core_stop;
4576 param = ar->wmi.pdev_param->ani_enable;
4577 ret = ath10k_wmi_pdev_set_param(ar, param, 1);
4578 if (ret) {
4579 ath10k_warn(ar, "failed to enable ani by default: %d\n",
4580 ret);
4581 goto err_core_stop;
4584 ar->ani_enabled = true;
4586 if (ath10k_peer_stats_enabled(ar)) {
4587 param = ar->wmi.pdev_param->peer_stats_update_period;
4588 ret = ath10k_wmi_pdev_set_param(ar, param,
4589 PEER_DEFAULT_STATS_UPDATE_PERIOD);
4590 if (ret) {
4591 ath10k_warn(ar,
4592 "failed to set peer stats period : %d\n",
4593 ret);
4594 goto err_core_stop;
4598 param = ar->wmi.pdev_param->enable_btcoex;
4599 if (test_bit(WMI_SERVICE_COEX_GPIO, ar->wmi.svc_map) &&
4600 test_bit(ATH10K_FW_FEATURE_BTCOEX_PARAM,
4601 ar->running_fw->fw_file.fw_features)) {
4602 ret = ath10k_wmi_pdev_set_param(ar, param, 0);
4603 if (ret) {
4604 ath10k_warn(ar,
4605 "failed to set btcoex param: %d\n", ret);
4606 goto err_core_stop;
4608 clear_bit(ATH10K_FLAG_BTCOEX, &ar->dev_flags);
4611 ar->num_started_vdevs = 0;
4612 ath10k_regd_update(ar);
4614 ath10k_spectral_start(ar);
4615 ath10k_thermal_set_throttling(ar);
4617 mutex_unlock(&ar->conf_mutex);
4618 return 0;
4620 err_core_stop:
4621 ath10k_core_stop(ar);
4623 err_power_down:
4624 ath10k_hif_power_down(ar);
4626 err_off:
4627 ar->state = ATH10K_STATE_OFF;
4629 err:
4630 mutex_unlock(&ar->conf_mutex);
4631 return ret;
4634 static void ath10k_stop(struct ieee80211_hw *hw)
4636 struct ath10k *ar = hw->priv;
4638 ath10k_drain_tx(ar);
4640 mutex_lock(&ar->conf_mutex);
4641 if (ar->state != ATH10K_STATE_OFF) {
4642 ath10k_halt(ar);
4643 ar->state = ATH10K_STATE_OFF;
4645 mutex_unlock(&ar->conf_mutex);
4647 cancel_delayed_work_sync(&ar->scan.timeout);
4648 cancel_work_sync(&ar->restart_work);
4651 static int ath10k_config_ps(struct ath10k *ar)
4653 struct ath10k_vif *arvif;
4654 int ret = 0;
4656 lockdep_assert_held(&ar->conf_mutex);
4658 list_for_each_entry(arvif, &ar->arvifs, list) {
4659 ret = ath10k_mac_vif_setup_ps(arvif);
4660 if (ret) {
4661 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
4662 break;
4666 return ret;
4669 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
4671 int ret;
4672 u32 param;
4674 lockdep_assert_held(&ar->conf_mutex);
4676 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
4678 param = ar->wmi.pdev_param->txpower_limit2g;
4679 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4680 if (ret) {
4681 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
4682 txpower, ret);
4683 return ret;
4686 param = ar->wmi.pdev_param->txpower_limit5g;
4687 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
4688 if (ret) {
4689 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
4690 txpower, ret);
4691 return ret;
4694 return 0;
4697 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
4699 struct ath10k_vif *arvif;
4700 int ret, txpower = -1;
4702 lockdep_assert_held(&ar->conf_mutex);
4704 list_for_each_entry(arvif, &ar->arvifs, list) {
4705 WARN_ON(arvif->txpower < 0);
4707 if (txpower == -1)
4708 txpower = arvif->txpower;
4709 else
4710 txpower = min(txpower, arvif->txpower);
4713 if (WARN_ON(txpower == -1))
4714 return -EINVAL;
4716 ret = ath10k_mac_txpower_setup(ar, txpower);
4717 if (ret) {
4718 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
4719 txpower, ret);
4720 return ret;
4723 return 0;
4726 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
4728 struct ath10k *ar = hw->priv;
4729 struct ieee80211_conf *conf = &hw->conf;
4730 int ret = 0;
4732 mutex_lock(&ar->conf_mutex);
4734 if (changed & IEEE80211_CONF_CHANGE_PS)
4735 ath10k_config_ps(ar);
4737 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
4738 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
4739 ret = ath10k_monitor_recalc(ar);
4740 if (ret)
4741 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
4744 mutex_unlock(&ar->conf_mutex);
4745 return ret;
4748 static u32 get_nss_from_chainmask(u16 chain_mask)
4750 if ((chain_mask & 0xf) == 0xf)
4751 return 4;
4752 else if ((chain_mask & 0x7) == 0x7)
4753 return 3;
4754 else if ((chain_mask & 0x3) == 0x3)
4755 return 2;
4756 return 1;
4759 static int ath10k_mac_set_txbf_conf(struct ath10k_vif *arvif)
4761 u32 value = 0;
4762 struct ath10k *ar = arvif->ar;
4763 int nsts;
4764 int sound_dim;
4766 if (ath10k_wmi_get_txbf_conf_scheme(ar) != WMI_TXBF_CONF_BEFORE_ASSOC)
4767 return 0;
4769 nsts = ath10k_mac_get_vht_cap_bf_sts(ar);
4770 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
4771 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE))
4772 value |= SM(nsts, WMI_TXBF_STS_CAP_OFFSET);
4774 sound_dim = ath10k_mac_get_vht_cap_bf_sound_dim(ar);
4775 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
4776 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE))
4777 value |= SM(sound_dim, WMI_BF_SOUND_DIM_OFFSET);
4779 if (!value)
4780 return 0;
4782 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
4783 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
4785 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
4786 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFER |
4787 WMI_VDEV_PARAM_TXBF_SU_TX_BFER);
4789 if (ar->vht_cap_info & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
4790 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
4792 if (ar->vht_cap_info & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
4793 value |= (WMI_VDEV_PARAM_TXBF_MU_TX_BFEE |
4794 WMI_VDEV_PARAM_TXBF_SU_TX_BFEE);
4796 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
4797 ar->wmi.vdev_param->txbf, value);
4801 * TODO:
4802 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
4803 * because we will send mgmt frames without CCK. This requirement
4804 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
4805 * in the TX packet.
4807 static int ath10k_add_interface(struct ieee80211_hw *hw,
4808 struct ieee80211_vif *vif)
4810 struct ath10k *ar = hw->priv;
4811 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4812 struct ath10k_peer *peer;
4813 enum wmi_sta_powersave_param param;
4814 int ret = 0;
4815 u32 value;
4816 int bit;
4817 int i;
4818 u32 vdev_param;
4820 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
4822 mutex_lock(&ar->conf_mutex);
4824 memset(arvif, 0, sizeof(*arvif));
4825 ath10k_mac_txq_init(vif->txq);
4827 arvif->ar = ar;
4828 arvif->vif = vif;
4830 INIT_LIST_HEAD(&arvif->list);
4831 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
4832 INIT_DELAYED_WORK(&arvif->connection_loss_work,
4833 ath10k_mac_vif_sta_connection_loss_work);
4835 for (i = 0; i < ARRAY_SIZE(arvif->bitrate_mask.control); i++) {
4836 arvif->bitrate_mask.control[i].legacy = 0xffffffff;
4837 memset(arvif->bitrate_mask.control[i].ht_mcs, 0xff,
4838 sizeof(arvif->bitrate_mask.control[i].ht_mcs));
4839 memset(arvif->bitrate_mask.control[i].vht_mcs, 0xff,
4840 sizeof(arvif->bitrate_mask.control[i].vht_mcs));
4843 if (ar->num_peers >= ar->max_num_peers) {
4844 ath10k_warn(ar, "refusing vdev creation due to insufficient peer entry resources in firmware\n");
4845 ret = -ENOBUFS;
4846 goto err;
4849 if (ar->free_vdev_map == 0) {
4850 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
4851 ret = -EBUSY;
4852 goto err;
4854 bit = __ffs64(ar->free_vdev_map);
4856 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
4857 bit, ar->free_vdev_map);
4859 arvif->vdev_id = bit;
4860 arvif->vdev_subtype =
4861 ath10k_wmi_get_vdev_subtype(ar, WMI_VDEV_SUBTYPE_NONE);
4863 switch (vif->type) {
4864 case NL80211_IFTYPE_P2P_DEVICE:
4865 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4866 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4867 (ar, WMI_VDEV_SUBTYPE_P2P_DEVICE);
4868 break;
4869 case NL80211_IFTYPE_UNSPECIFIED:
4870 case NL80211_IFTYPE_STATION:
4871 arvif->vdev_type = WMI_VDEV_TYPE_STA;
4872 if (vif->p2p)
4873 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4874 (ar, WMI_VDEV_SUBTYPE_P2P_CLIENT);
4875 break;
4876 case NL80211_IFTYPE_ADHOC:
4877 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
4878 break;
4879 case NL80211_IFTYPE_MESH_POINT:
4880 if (test_bit(WMI_SERVICE_MESH_11S, ar->wmi.svc_map)) {
4881 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4882 (ar, WMI_VDEV_SUBTYPE_MESH_11S);
4883 } else if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4884 ret = -EINVAL;
4885 ath10k_warn(ar, "must load driver with rawmode=1 to add mesh interfaces\n");
4886 goto err;
4888 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4889 break;
4890 case NL80211_IFTYPE_AP:
4891 arvif->vdev_type = WMI_VDEV_TYPE_AP;
4893 if (vif->p2p)
4894 arvif->vdev_subtype = ath10k_wmi_get_vdev_subtype
4895 (ar, WMI_VDEV_SUBTYPE_P2P_GO);
4896 break;
4897 case NL80211_IFTYPE_MONITOR:
4898 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
4899 break;
4900 default:
4901 WARN_ON(1);
4902 break;
4905 /* Using vdev_id as queue number will make it very easy to do per-vif
4906 * tx queue locking. This shouldn't wrap due to interface combinations
4907 * but do a modulo for correctness sake and prevent using offchannel tx
4908 * queues for regular vif tx.
4910 vif->cab_queue = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4911 for (i = 0; i < ARRAY_SIZE(vif->hw_queue); i++)
4912 vif->hw_queue[i] = arvif->vdev_id % (IEEE80211_MAX_QUEUES - 1);
4914 /* Some firmware revisions don't wait for beacon tx completion before
4915 * sending another SWBA event. This could lead to hardware using old
4916 * (freed) beacon data in some cases, e.g. tx credit starvation
4917 * combined with missed TBTT. This is very very rare.
4919 * On non-IOMMU-enabled hosts this could be a possible security issue
4920 * because hw could beacon some random data on the air. On
4921 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
4922 * device would crash.
4924 * Since there are no beacon tx completions (implicit nor explicit)
4925 * propagated to host the only workaround for this is to allocate a
4926 * DMA-coherent buffer for a lifetime of a vif and use it for all
4927 * beacon tx commands. Worst case for this approach is some beacons may
4928 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
4930 if (vif->type == NL80211_IFTYPE_ADHOC ||
4931 vif->type == NL80211_IFTYPE_MESH_POINT ||
4932 vif->type == NL80211_IFTYPE_AP) {
4933 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
4934 IEEE80211_MAX_FRAME_LEN,
4935 &arvif->beacon_paddr,
4936 GFP_ATOMIC);
4937 if (!arvif->beacon_buf) {
4938 ret = -ENOMEM;
4939 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
4940 ret);
4941 goto err;
4944 if (test_bit(ATH10K_FLAG_HW_CRYPTO_DISABLED, &ar->dev_flags))
4945 arvif->nohwcrypt = true;
4947 if (arvif->nohwcrypt &&
4948 !test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags)) {
4949 ath10k_warn(ar, "cryptmode module param needed for sw crypto\n");
4950 goto err;
4953 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
4954 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
4955 arvif->beacon_buf ? "single-buf" : "per-skb");
4957 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
4958 arvif->vdev_subtype, vif->addr);
4959 if (ret) {
4960 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
4961 arvif->vdev_id, ret);
4962 goto err;
4965 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
4966 spin_lock_bh(&ar->data_lock);
4967 list_add(&arvif->list, &ar->arvifs);
4968 spin_unlock_bh(&ar->data_lock);
4970 /* It makes no sense to have firmware do keepalives. mac80211 already
4971 * takes care of this with idle connection polling.
4973 ret = ath10k_mac_vif_disable_keepalive(arvif);
4974 if (ret) {
4975 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
4976 arvif->vdev_id, ret);
4977 goto err_vdev_delete;
4980 arvif->def_wep_key_idx = -1;
4982 vdev_param = ar->wmi.vdev_param->tx_encap_type;
4983 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4984 ATH10K_HW_TXRX_NATIVE_WIFI);
4985 /* 10.X firmware does not support this VDEV parameter. Do not warn */
4986 if (ret && ret != -EOPNOTSUPP) {
4987 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
4988 arvif->vdev_id, ret);
4989 goto err_vdev_delete;
4992 /* Configuring number of spatial stream for monitor interface is causing
4993 * target assert in qca9888 and qca6174.
4995 if (ar->cfg_tx_chainmask && (vif->type != NL80211_IFTYPE_MONITOR)) {
4996 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4998 vdev_param = ar->wmi.vdev_param->nss;
4999 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5000 nss);
5001 if (ret) {
5002 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
5003 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
5004 ret);
5005 goto err_vdev_delete;
5009 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5010 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5011 ret = ath10k_peer_create(ar, vif, NULL, arvif->vdev_id,
5012 vif->addr, WMI_PEER_TYPE_DEFAULT);
5013 if (ret) {
5014 ath10k_warn(ar, "failed to create vdev %i peer for AP/IBSS: %d\n",
5015 arvif->vdev_id, ret);
5016 goto err_vdev_delete;
5019 spin_lock_bh(&ar->data_lock);
5021 peer = ath10k_peer_find(ar, arvif->vdev_id, vif->addr);
5022 if (!peer) {
5023 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
5024 vif->addr, arvif->vdev_id);
5025 spin_unlock_bh(&ar->data_lock);
5026 ret = -ENOENT;
5027 goto err_peer_delete;
5030 arvif->peer_id = find_first_bit(peer->peer_ids,
5031 ATH10K_MAX_NUM_PEER_IDS);
5033 spin_unlock_bh(&ar->data_lock);
5034 } else {
5035 arvif->peer_id = HTT_INVALID_PEERID;
5038 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
5039 ret = ath10k_mac_set_kickout(arvif);
5040 if (ret) {
5041 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
5042 arvif->vdev_id, ret);
5043 goto err_peer_delete;
5047 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
5048 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
5049 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
5050 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
5051 param, value);
5052 if (ret) {
5053 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
5054 arvif->vdev_id, ret);
5055 goto err_peer_delete;
5058 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
5059 if (ret) {
5060 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
5061 arvif->vdev_id, ret);
5062 goto err_peer_delete;
5065 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
5066 if (ret) {
5067 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
5068 arvif->vdev_id, ret);
5069 goto err_peer_delete;
5073 ret = ath10k_mac_set_txbf_conf(arvif);
5074 if (ret) {
5075 ath10k_warn(ar, "failed to set txbf for vdev %d: %d\n",
5076 arvif->vdev_id, ret);
5077 goto err_peer_delete;
5080 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
5081 if (ret) {
5082 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
5083 arvif->vdev_id, ret);
5084 goto err_peer_delete;
5087 arvif->txpower = vif->bss_conf.txpower;
5088 ret = ath10k_mac_txpower_recalc(ar);
5089 if (ret) {
5090 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5091 goto err_peer_delete;
5094 if (vif->type == NL80211_IFTYPE_MONITOR) {
5095 ar->monitor_arvif = arvif;
5096 ret = ath10k_monitor_recalc(ar);
5097 if (ret) {
5098 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5099 goto err_peer_delete;
5103 spin_lock_bh(&ar->htt.tx_lock);
5104 if (!ar->tx_paused)
5105 ieee80211_wake_queue(ar->hw, arvif->vdev_id);
5106 spin_unlock_bh(&ar->htt.tx_lock);
5108 mutex_unlock(&ar->conf_mutex);
5109 return 0;
5111 err_peer_delete:
5112 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5113 arvif->vdev_type == WMI_VDEV_TYPE_IBSS)
5114 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
5116 err_vdev_delete:
5117 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5118 ar->free_vdev_map |= 1LL << arvif->vdev_id;
5119 spin_lock_bh(&ar->data_lock);
5120 list_del(&arvif->list);
5121 spin_unlock_bh(&ar->data_lock);
5123 err:
5124 if (arvif->beacon_buf) {
5125 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
5126 arvif->beacon_buf, arvif->beacon_paddr);
5127 arvif->beacon_buf = NULL;
5130 mutex_unlock(&ar->conf_mutex);
5132 return ret;
5135 static void ath10k_mac_vif_tx_unlock_all(struct ath10k_vif *arvif)
5137 int i;
5139 for (i = 0; i < BITS_PER_LONG; i++)
5140 ath10k_mac_vif_tx_unlock(arvif, i);
5143 static void ath10k_remove_interface(struct ieee80211_hw *hw,
5144 struct ieee80211_vif *vif)
5146 struct ath10k *ar = hw->priv;
5147 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5148 struct ath10k_peer *peer;
5149 int ret;
5150 int i;
5152 cancel_work_sync(&arvif->ap_csa_work);
5153 cancel_delayed_work_sync(&arvif->connection_loss_work);
5155 mutex_lock(&ar->conf_mutex);
5157 spin_lock_bh(&ar->data_lock);
5158 ath10k_mac_vif_beacon_cleanup(arvif);
5159 spin_unlock_bh(&ar->data_lock);
5161 ret = ath10k_spectral_vif_stop(arvif);
5162 if (ret)
5163 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
5164 arvif->vdev_id, ret);
5166 ar->free_vdev_map |= 1LL << arvif->vdev_id;
5167 spin_lock_bh(&ar->data_lock);
5168 list_del(&arvif->list);
5169 spin_unlock_bh(&ar->data_lock);
5171 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5172 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5173 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
5174 vif->addr);
5175 if (ret)
5176 ath10k_warn(ar, "failed to submit AP/IBSS self-peer removal on vdev %i: %d\n",
5177 arvif->vdev_id, ret);
5179 kfree(arvif->u.ap.noa_data);
5182 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
5183 arvif->vdev_id);
5185 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
5186 if (ret)
5187 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
5188 arvif->vdev_id, ret);
5190 /* Some firmware revisions don't notify host about self-peer removal
5191 * until after associated vdev is deleted.
5193 if (arvif->vdev_type == WMI_VDEV_TYPE_AP ||
5194 arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
5195 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
5196 vif->addr);
5197 if (ret)
5198 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
5199 arvif->vdev_id, ret);
5201 spin_lock_bh(&ar->data_lock);
5202 ar->num_peers--;
5203 spin_unlock_bh(&ar->data_lock);
5206 spin_lock_bh(&ar->data_lock);
5207 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
5208 peer = ar->peer_map[i];
5209 if (!peer)
5210 continue;
5212 if (peer->vif == vif) {
5213 ath10k_warn(ar, "found vif peer %pM entry on vdev %i after it was supposedly removed\n",
5214 vif->addr, arvif->vdev_id);
5215 peer->vif = NULL;
5218 spin_unlock_bh(&ar->data_lock);
5220 ath10k_peer_cleanup(ar, arvif->vdev_id);
5221 ath10k_mac_txq_unref(ar, vif->txq);
5223 if (vif->type == NL80211_IFTYPE_MONITOR) {
5224 ar->monitor_arvif = NULL;
5225 ret = ath10k_monitor_recalc(ar);
5226 if (ret)
5227 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5230 spin_lock_bh(&ar->htt.tx_lock);
5231 ath10k_mac_vif_tx_unlock_all(arvif);
5232 spin_unlock_bh(&ar->htt.tx_lock);
5234 ath10k_mac_txq_unref(ar, vif->txq);
5236 mutex_unlock(&ar->conf_mutex);
5240 * FIXME: Has to be verified.
5242 #define SUPPORTED_FILTERS \
5243 (FIF_ALLMULTI | \
5244 FIF_CONTROL | \
5245 FIF_PSPOLL | \
5246 FIF_OTHER_BSS | \
5247 FIF_BCN_PRBRESP_PROMISC | \
5248 FIF_PROBE_REQ | \
5249 FIF_FCSFAIL)
5251 static void ath10k_configure_filter(struct ieee80211_hw *hw,
5252 unsigned int changed_flags,
5253 unsigned int *total_flags,
5254 u64 multicast)
5256 struct ath10k *ar = hw->priv;
5257 int ret;
5259 mutex_lock(&ar->conf_mutex);
5261 changed_flags &= SUPPORTED_FILTERS;
5262 *total_flags &= SUPPORTED_FILTERS;
5263 ar->filter_flags = *total_flags;
5265 ret = ath10k_monitor_recalc(ar);
5266 if (ret)
5267 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
5269 mutex_unlock(&ar->conf_mutex);
5272 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
5273 struct ieee80211_vif *vif,
5274 struct ieee80211_bss_conf *info,
5275 u32 changed)
5277 struct ath10k *ar = hw->priv;
5278 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5279 int ret = 0;
5280 u32 vdev_param, pdev_param, slottime, preamble;
5282 mutex_lock(&ar->conf_mutex);
5284 if (changed & BSS_CHANGED_IBSS)
5285 ath10k_control_ibss(arvif, info, vif->addr);
5287 if (changed & BSS_CHANGED_BEACON_INT) {
5288 arvif->beacon_interval = info->beacon_int;
5289 vdev_param = ar->wmi.vdev_param->beacon_interval;
5290 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5291 arvif->beacon_interval);
5292 ath10k_dbg(ar, ATH10K_DBG_MAC,
5293 "mac vdev %d beacon_interval %d\n",
5294 arvif->vdev_id, arvif->beacon_interval);
5296 if (ret)
5297 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
5298 arvif->vdev_id, ret);
5301 if (changed & BSS_CHANGED_BEACON) {
5302 ath10k_dbg(ar, ATH10K_DBG_MAC,
5303 "vdev %d set beacon tx mode to staggered\n",
5304 arvif->vdev_id);
5306 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
5307 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
5308 WMI_BEACON_STAGGERED_MODE);
5309 if (ret)
5310 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
5311 arvif->vdev_id, ret);
5313 ret = ath10k_mac_setup_bcn_tmpl(arvif);
5314 if (ret)
5315 ath10k_warn(ar, "failed to update beacon template: %d\n",
5316 ret);
5318 if (ieee80211_vif_is_mesh(vif)) {
5319 /* mesh doesn't use SSID but firmware needs it */
5320 strncpy(arvif->u.ap.ssid, "mesh",
5321 sizeof(arvif->u.ap.ssid));
5322 arvif->u.ap.ssid_len = 4;
5326 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
5327 ret = ath10k_mac_setup_prb_tmpl(arvif);
5328 if (ret)
5329 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
5330 arvif->vdev_id, ret);
5333 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
5334 arvif->dtim_period = info->dtim_period;
5336 ath10k_dbg(ar, ATH10K_DBG_MAC,
5337 "mac vdev %d dtim_period %d\n",
5338 arvif->vdev_id, arvif->dtim_period);
5340 vdev_param = ar->wmi.vdev_param->dtim_period;
5341 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5342 arvif->dtim_period);
5343 if (ret)
5344 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
5345 arvif->vdev_id, ret);
5348 if (changed & BSS_CHANGED_SSID &&
5349 vif->type == NL80211_IFTYPE_AP) {
5350 arvif->u.ap.ssid_len = info->ssid_len;
5351 if (info->ssid_len)
5352 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
5353 arvif->u.ap.hidden_ssid = info->hidden_ssid;
5356 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
5357 ether_addr_copy(arvif->bssid, info->bssid);
5359 if (changed & BSS_CHANGED_BEACON_ENABLED)
5360 ath10k_control_beaconing(arvif, info);
5362 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
5363 arvif->use_cts_prot = info->use_cts_prot;
5365 ret = ath10k_recalc_rtscts_prot(arvif);
5366 if (ret)
5367 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
5368 arvif->vdev_id, ret);
5370 if (ath10k_mac_can_set_cts_prot(arvif)) {
5371 ret = ath10k_mac_set_cts_prot(arvif);
5372 if (ret)
5373 ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
5374 arvif->vdev_id, ret);
5378 if (changed & BSS_CHANGED_ERP_SLOT) {
5379 if (info->use_short_slot)
5380 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
5382 else
5383 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
5385 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
5386 arvif->vdev_id, slottime);
5388 vdev_param = ar->wmi.vdev_param->slot_time;
5389 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5390 slottime);
5391 if (ret)
5392 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
5393 arvif->vdev_id, ret);
5396 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
5397 if (info->use_short_preamble)
5398 preamble = WMI_VDEV_PREAMBLE_SHORT;
5399 else
5400 preamble = WMI_VDEV_PREAMBLE_LONG;
5402 ath10k_dbg(ar, ATH10K_DBG_MAC,
5403 "mac vdev %d preamble %dn",
5404 arvif->vdev_id, preamble);
5406 vdev_param = ar->wmi.vdev_param->preamble;
5407 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5408 preamble);
5409 if (ret)
5410 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
5411 arvif->vdev_id, ret);
5414 if (changed & BSS_CHANGED_ASSOC) {
5415 if (info->assoc) {
5416 /* Workaround: Make sure monitor vdev is not running
5417 * when associating to prevent some firmware revisions
5418 * (e.g. 10.1 and 10.2) from crashing.
5420 if (ar->monitor_started)
5421 ath10k_monitor_stop(ar);
5422 ath10k_bss_assoc(hw, vif, info);
5423 ath10k_monitor_recalc(ar);
5424 } else {
5425 ath10k_bss_disassoc(hw, vif);
5429 if (changed & BSS_CHANGED_TXPOWER) {
5430 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
5431 arvif->vdev_id, info->txpower);
5433 arvif->txpower = info->txpower;
5434 ret = ath10k_mac_txpower_recalc(ar);
5435 if (ret)
5436 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
5439 if (changed & BSS_CHANGED_PS) {
5440 arvif->ps = vif->bss_conf.ps;
5442 ret = ath10k_config_ps(ar);
5443 if (ret)
5444 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
5445 arvif->vdev_id, ret);
5448 mutex_unlock(&ar->conf_mutex);
5451 static void ath10k_mac_op_set_coverage_class(struct ieee80211_hw *hw, s16 value)
5453 struct ath10k *ar = hw->priv;
5455 /* This function should never be called if setting the coverage class
5456 * is not supported on this hardware.
5458 if (!ar->hw_params.hw_ops->set_coverage_class) {
5459 WARN_ON_ONCE(1);
5460 return;
5462 ar->hw_params.hw_ops->set_coverage_class(ar, value);
5465 static int ath10k_hw_scan(struct ieee80211_hw *hw,
5466 struct ieee80211_vif *vif,
5467 struct ieee80211_scan_request *hw_req)
5469 struct ath10k *ar = hw->priv;
5470 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5471 struct cfg80211_scan_request *req = &hw_req->req;
5472 struct wmi_start_scan_arg arg;
5473 int ret = 0;
5474 int i;
5476 mutex_lock(&ar->conf_mutex);
5478 spin_lock_bh(&ar->data_lock);
5479 switch (ar->scan.state) {
5480 case ATH10K_SCAN_IDLE:
5481 reinit_completion(&ar->scan.started);
5482 reinit_completion(&ar->scan.completed);
5483 ar->scan.state = ATH10K_SCAN_STARTING;
5484 ar->scan.is_roc = false;
5485 ar->scan.vdev_id = arvif->vdev_id;
5486 ret = 0;
5487 break;
5488 case ATH10K_SCAN_STARTING:
5489 case ATH10K_SCAN_RUNNING:
5490 case ATH10K_SCAN_ABORTING:
5491 ret = -EBUSY;
5492 break;
5494 spin_unlock_bh(&ar->data_lock);
5496 if (ret)
5497 goto exit;
5499 memset(&arg, 0, sizeof(arg));
5500 ath10k_wmi_start_scan_init(ar, &arg);
5501 arg.vdev_id = arvif->vdev_id;
5502 arg.scan_id = ATH10K_SCAN_ID;
5504 if (req->ie_len) {
5505 arg.ie_len = req->ie_len;
5506 memcpy(arg.ie, req->ie, arg.ie_len);
5509 if (req->n_ssids) {
5510 arg.n_ssids = req->n_ssids;
5511 for (i = 0; i < arg.n_ssids; i++) {
5512 arg.ssids[i].len = req->ssids[i].ssid_len;
5513 arg.ssids[i].ssid = req->ssids[i].ssid;
5515 } else {
5516 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
5519 if (req->n_channels) {
5520 arg.n_channels = req->n_channels;
5521 for (i = 0; i < arg.n_channels; i++)
5522 arg.channels[i] = req->channels[i]->center_freq;
5525 ret = ath10k_start_scan(ar, &arg);
5526 if (ret) {
5527 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
5528 spin_lock_bh(&ar->data_lock);
5529 ar->scan.state = ATH10K_SCAN_IDLE;
5530 spin_unlock_bh(&ar->data_lock);
5533 /* Add a 200ms margin to account for event/command processing */
5534 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
5535 msecs_to_jiffies(arg.max_scan_time +
5536 200));
5538 exit:
5539 mutex_unlock(&ar->conf_mutex);
5540 return ret;
5543 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
5544 struct ieee80211_vif *vif)
5546 struct ath10k *ar = hw->priv;
5548 mutex_lock(&ar->conf_mutex);
5549 ath10k_scan_abort(ar);
5550 mutex_unlock(&ar->conf_mutex);
5552 cancel_delayed_work_sync(&ar->scan.timeout);
5555 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
5556 struct ath10k_vif *arvif,
5557 enum set_key_cmd cmd,
5558 struct ieee80211_key_conf *key)
5560 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
5561 int ret;
5563 /* 10.1 firmware branch requires default key index to be set to group
5564 * key index after installing it. Otherwise FW/HW Txes corrupted
5565 * frames with multi-vif APs. This is not required for main firmware
5566 * branch (e.g. 636).
5568 * This is also needed for 636 fw for IBSS-RSN to work more reliably.
5570 * FIXME: It remains unknown if this is required for multi-vif STA
5571 * interfaces on 10.1.
5574 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
5575 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
5576 return;
5578 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
5579 return;
5581 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
5582 return;
5584 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5585 return;
5587 if (cmd != SET_KEY)
5588 return;
5590 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5591 key->keyidx);
5592 if (ret)
5593 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
5594 arvif->vdev_id, ret);
5597 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
5598 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
5599 struct ieee80211_key_conf *key)
5601 struct ath10k *ar = hw->priv;
5602 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5603 struct ath10k_peer *peer;
5604 const u8 *peer_addr;
5605 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
5606 key->cipher == WLAN_CIPHER_SUITE_WEP104;
5607 int ret = 0;
5608 int ret2;
5609 u32 flags = 0;
5610 u32 flags2;
5612 /* this one needs to be done in software */
5613 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
5614 return 1;
5616 if (arvif->nohwcrypt)
5617 return 1;
5619 if (key->keyidx > WMI_MAX_KEY_INDEX)
5620 return -ENOSPC;
5622 mutex_lock(&ar->conf_mutex);
5624 if (sta)
5625 peer_addr = sta->addr;
5626 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
5627 peer_addr = vif->bss_conf.bssid;
5628 else
5629 peer_addr = vif->addr;
5631 key->hw_key_idx = key->keyidx;
5633 if (is_wep) {
5634 if (cmd == SET_KEY)
5635 arvif->wep_keys[key->keyidx] = key;
5636 else
5637 arvif->wep_keys[key->keyidx] = NULL;
5640 /* the peer should not disappear in mid-way (unless FW goes awry) since
5641 * we already hold conf_mutex. we just make sure its there now. */
5642 spin_lock_bh(&ar->data_lock);
5643 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5644 spin_unlock_bh(&ar->data_lock);
5646 if (!peer) {
5647 if (cmd == SET_KEY) {
5648 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
5649 peer_addr);
5650 ret = -EOPNOTSUPP;
5651 goto exit;
5652 } else {
5653 /* if the peer doesn't exist there is no key to disable
5654 * anymore */
5655 goto exit;
5659 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
5660 flags |= WMI_KEY_PAIRWISE;
5661 else
5662 flags |= WMI_KEY_GROUP;
5664 if (is_wep) {
5665 if (cmd == DISABLE_KEY)
5666 ath10k_clear_vdev_key(arvif, key);
5668 /* When WEP keys are uploaded it's possible that there are
5669 * stations associated already (e.g. when merging) without any
5670 * keys. Static WEP needs an explicit per-peer key upload.
5672 if (vif->type == NL80211_IFTYPE_ADHOC &&
5673 cmd == SET_KEY)
5674 ath10k_mac_vif_update_wep_key(arvif, key);
5676 /* 802.1x never sets the def_wep_key_idx so each set_key()
5677 * call changes default tx key.
5679 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
5680 * after first set_key().
5682 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
5683 flags |= WMI_KEY_TX_USAGE;
5686 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
5687 if (ret) {
5688 WARN_ON(ret > 0);
5689 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
5690 arvif->vdev_id, peer_addr, ret);
5691 goto exit;
5694 /* mac80211 sets static WEP keys as groupwise while firmware requires
5695 * them to be installed twice as both pairwise and groupwise.
5697 if (is_wep && !sta && vif->type == NL80211_IFTYPE_STATION) {
5698 flags2 = flags;
5699 flags2 &= ~WMI_KEY_GROUP;
5700 flags2 |= WMI_KEY_PAIRWISE;
5702 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags2);
5703 if (ret) {
5704 WARN_ON(ret > 0);
5705 ath10k_warn(ar, "failed to install (ucast) key for vdev %i peer %pM: %d\n",
5706 arvif->vdev_id, peer_addr, ret);
5707 ret2 = ath10k_install_key(arvif, key, DISABLE_KEY,
5708 peer_addr, flags);
5709 if (ret2) {
5710 WARN_ON(ret2 > 0);
5711 ath10k_warn(ar, "failed to disable (mcast) key for vdev %i peer %pM: %d\n",
5712 arvif->vdev_id, peer_addr, ret2);
5714 goto exit;
5718 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
5720 spin_lock_bh(&ar->data_lock);
5721 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
5722 if (peer && cmd == SET_KEY)
5723 peer->keys[key->keyidx] = key;
5724 else if (peer && cmd == DISABLE_KEY)
5725 peer->keys[key->keyidx] = NULL;
5726 else if (peer == NULL)
5727 /* impossible unless FW goes crazy */
5728 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
5729 spin_unlock_bh(&ar->data_lock);
5731 exit:
5732 mutex_unlock(&ar->conf_mutex);
5733 return ret;
5736 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
5737 struct ieee80211_vif *vif,
5738 int keyidx)
5740 struct ath10k *ar = hw->priv;
5741 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5742 int ret;
5744 mutex_lock(&arvif->ar->conf_mutex);
5746 if (arvif->ar->state != ATH10K_STATE_ON)
5747 goto unlock;
5749 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
5750 arvif->vdev_id, keyidx);
5752 ret = ath10k_wmi_vdev_set_param(arvif->ar,
5753 arvif->vdev_id,
5754 arvif->ar->wmi.vdev_param->def_keyid,
5755 keyidx);
5757 if (ret) {
5758 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
5759 arvif->vdev_id,
5760 ret);
5761 goto unlock;
5764 arvif->def_wep_key_idx = keyidx;
5766 unlock:
5767 mutex_unlock(&arvif->ar->conf_mutex);
5770 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
5772 struct ath10k *ar;
5773 struct ath10k_vif *arvif;
5774 struct ath10k_sta *arsta;
5775 struct ieee80211_sta *sta;
5776 struct cfg80211_chan_def def;
5777 enum nl80211_band band;
5778 const u8 *ht_mcs_mask;
5779 const u16 *vht_mcs_mask;
5780 u32 changed, bw, nss, smps;
5781 int err;
5783 arsta = container_of(wk, struct ath10k_sta, update_wk);
5784 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
5785 arvif = arsta->arvif;
5786 ar = arvif->ar;
5788 if (WARN_ON(ath10k_mac_vif_chan(arvif->vif, &def)))
5789 return;
5791 band = def.chan->band;
5792 ht_mcs_mask = arvif->bitrate_mask.control[band].ht_mcs;
5793 vht_mcs_mask = arvif->bitrate_mask.control[band].vht_mcs;
5795 spin_lock_bh(&ar->data_lock);
5797 changed = arsta->changed;
5798 arsta->changed = 0;
5800 bw = arsta->bw;
5801 nss = arsta->nss;
5802 smps = arsta->smps;
5804 spin_unlock_bh(&ar->data_lock);
5806 mutex_lock(&ar->conf_mutex);
5808 nss = max_t(u32, 1, nss);
5809 nss = min(nss, max(ath10k_mac_max_ht_nss(ht_mcs_mask),
5810 ath10k_mac_max_vht_nss(vht_mcs_mask)));
5812 if (changed & IEEE80211_RC_BW_CHANGED) {
5813 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
5814 sta->addr, bw);
5816 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5817 WMI_PEER_CHAN_WIDTH, bw);
5818 if (err)
5819 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
5820 sta->addr, bw, err);
5823 if (changed & IEEE80211_RC_NSS_CHANGED) {
5824 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
5825 sta->addr, nss);
5827 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5828 WMI_PEER_NSS, nss);
5829 if (err)
5830 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
5831 sta->addr, nss, err);
5834 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5835 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
5836 sta->addr, smps);
5838 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
5839 WMI_PEER_SMPS_STATE, smps);
5840 if (err)
5841 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
5842 sta->addr, smps, err);
5845 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
5846 changed & IEEE80211_RC_NSS_CHANGED) {
5847 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
5848 sta->addr);
5850 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
5851 if (err)
5852 ath10k_warn(ar, "failed to reassociate station: %pM\n",
5853 sta->addr);
5856 mutex_unlock(&ar->conf_mutex);
5859 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
5860 struct ieee80211_sta *sta)
5862 struct ath10k *ar = arvif->ar;
5864 lockdep_assert_held(&ar->conf_mutex);
5866 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5867 return 0;
5869 if (ar->num_stations >= ar->max_num_stations)
5870 return -ENOBUFS;
5872 ar->num_stations++;
5874 return 0;
5877 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
5878 struct ieee80211_sta *sta)
5880 struct ath10k *ar = arvif->ar;
5882 lockdep_assert_held(&ar->conf_mutex);
5884 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
5885 return;
5887 ar->num_stations--;
5890 struct ath10k_mac_tdls_iter_data {
5891 u32 num_tdls_stations;
5892 struct ieee80211_vif *curr_vif;
5895 static void ath10k_mac_tdls_vif_stations_count_iter(void *data,
5896 struct ieee80211_sta *sta)
5898 struct ath10k_mac_tdls_iter_data *iter_data = data;
5899 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5900 struct ieee80211_vif *sta_vif = arsta->arvif->vif;
5902 if (sta->tdls && sta_vif == iter_data->curr_vif)
5903 iter_data->num_tdls_stations++;
5906 static int ath10k_mac_tdls_vif_stations_count(struct ieee80211_hw *hw,
5907 struct ieee80211_vif *vif)
5909 struct ath10k_mac_tdls_iter_data data = {};
5911 data.curr_vif = vif;
5913 ieee80211_iterate_stations_atomic(hw,
5914 ath10k_mac_tdls_vif_stations_count_iter,
5915 &data);
5916 return data.num_tdls_stations;
5919 static void ath10k_mac_tdls_vifs_count_iter(void *data, u8 *mac,
5920 struct ieee80211_vif *vif)
5922 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5923 int *num_tdls_vifs = data;
5925 if (vif->type != NL80211_IFTYPE_STATION)
5926 return;
5928 if (ath10k_mac_tdls_vif_stations_count(arvif->ar->hw, vif) > 0)
5929 (*num_tdls_vifs)++;
5932 static int ath10k_mac_tdls_vifs_count(struct ieee80211_hw *hw)
5934 int num_tdls_vifs = 0;
5936 ieee80211_iterate_active_interfaces_atomic(hw,
5937 IEEE80211_IFACE_ITER_NORMAL,
5938 ath10k_mac_tdls_vifs_count_iter,
5939 &num_tdls_vifs);
5940 return num_tdls_vifs;
5943 static int ath10k_sta_state(struct ieee80211_hw *hw,
5944 struct ieee80211_vif *vif,
5945 struct ieee80211_sta *sta,
5946 enum ieee80211_sta_state old_state,
5947 enum ieee80211_sta_state new_state)
5949 struct ath10k *ar = hw->priv;
5950 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5951 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5952 struct ath10k_peer *peer;
5953 int ret = 0;
5954 int i;
5956 if (old_state == IEEE80211_STA_NOTEXIST &&
5957 new_state == IEEE80211_STA_NONE) {
5958 memset(arsta, 0, sizeof(*arsta));
5959 arsta->arvif = arvif;
5960 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
5962 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
5963 ath10k_mac_txq_init(sta->txq[i]);
5966 /* cancel must be done outside the mutex to avoid deadlock */
5967 if ((old_state == IEEE80211_STA_NONE &&
5968 new_state == IEEE80211_STA_NOTEXIST))
5969 cancel_work_sync(&arsta->update_wk);
5971 mutex_lock(&ar->conf_mutex);
5973 if (old_state == IEEE80211_STA_NOTEXIST &&
5974 new_state == IEEE80211_STA_NONE) {
5976 * New station addition.
5978 enum wmi_peer_type peer_type = WMI_PEER_TYPE_DEFAULT;
5979 u32 num_tdls_stations;
5980 u32 num_tdls_vifs;
5982 ath10k_dbg(ar, ATH10K_DBG_MAC,
5983 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
5984 arvif->vdev_id, sta->addr,
5985 ar->num_stations + 1, ar->max_num_stations,
5986 ar->num_peers + 1, ar->max_num_peers);
5988 ret = ath10k_mac_inc_num_stations(arvif, sta);
5989 if (ret) {
5990 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
5991 ar->max_num_stations);
5992 goto exit;
5995 if (sta->tdls)
5996 peer_type = WMI_PEER_TYPE_TDLS;
5998 ret = ath10k_peer_create(ar, vif, sta, arvif->vdev_id,
5999 sta->addr, peer_type);
6000 if (ret) {
6001 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
6002 sta->addr, arvif->vdev_id, ret);
6003 ath10k_mac_dec_num_stations(arvif, sta);
6004 goto exit;
6007 spin_lock_bh(&ar->data_lock);
6009 peer = ath10k_peer_find(ar, arvif->vdev_id, sta->addr);
6010 if (!peer) {
6011 ath10k_warn(ar, "failed to lookup peer %pM on vdev %i\n",
6012 vif->addr, arvif->vdev_id);
6013 spin_unlock_bh(&ar->data_lock);
6014 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6015 ath10k_mac_dec_num_stations(arvif, sta);
6016 ret = -ENOENT;
6017 goto exit;
6020 arsta->peer_id = find_first_bit(peer->peer_ids,
6021 ATH10K_MAX_NUM_PEER_IDS);
6023 spin_unlock_bh(&ar->data_lock);
6025 if (!sta->tdls)
6026 goto exit;
6028 num_tdls_stations = ath10k_mac_tdls_vif_stations_count(hw, vif);
6029 num_tdls_vifs = ath10k_mac_tdls_vifs_count(hw);
6031 if (num_tdls_vifs >= ar->max_num_tdls_vdevs &&
6032 num_tdls_stations == 0) {
6033 ath10k_warn(ar, "vdev %i exceeded maximum number of tdls vdevs %i\n",
6034 arvif->vdev_id, ar->max_num_tdls_vdevs);
6035 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6036 ath10k_mac_dec_num_stations(arvif, sta);
6037 ret = -ENOBUFS;
6038 goto exit;
6041 if (num_tdls_stations == 0) {
6042 /* This is the first tdls peer in current vif */
6043 enum wmi_tdls_state state = WMI_TDLS_ENABLE_ACTIVE;
6045 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6046 state);
6047 if (ret) {
6048 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6049 arvif->vdev_id, ret);
6050 ath10k_peer_delete(ar, arvif->vdev_id,
6051 sta->addr);
6052 ath10k_mac_dec_num_stations(arvif, sta);
6053 goto exit;
6057 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6058 WMI_TDLS_PEER_STATE_PEERING);
6059 if (ret) {
6060 ath10k_warn(ar,
6061 "failed to update tdls peer %pM for vdev %d when adding a new sta: %i\n",
6062 sta->addr, arvif->vdev_id, ret);
6063 ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6064 ath10k_mac_dec_num_stations(arvif, sta);
6066 if (num_tdls_stations != 0)
6067 goto exit;
6068 ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6069 WMI_TDLS_DISABLE);
6071 } else if ((old_state == IEEE80211_STA_NONE &&
6072 new_state == IEEE80211_STA_NOTEXIST)) {
6074 * Existing station deletion.
6076 ath10k_dbg(ar, ATH10K_DBG_MAC,
6077 "mac vdev %d peer delete %pM sta %pK (sta gone)\n",
6078 arvif->vdev_id, sta->addr, sta);
6080 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
6081 if (ret)
6082 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
6083 sta->addr, arvif->vdev_id, ret);
6085 ath10k_mac_dec_num_stations(arvif, sta);
6087 spin_lock_bh(&ar->data_lock);
6088 for (i = 0; i < ARRAY_SIZE(ar->peer_map); i++) {
6089 peer = ar->peer_map[i];
6090 if (!peer)
6091 continue;
6093 if (peer->sta == sta) {
6094 ath10k_warn(ar, "found sta peer %pM (ptr %pK id %d) entry on vdev %i after it was supposedly removed\n",
6095 sta->addr, peer, i, arvif->vdev_id);
6096 peer->sta = NULL;
6098 /* Clean up the peer object as well since we
6099 * must have failed to do this above.
6101 list_del(&peer->list);
6102 ar->peer_map[i] = NULL;
6103 kfree(peer);
6104 ar->num_peers--;
6107 spin_unlock_bh(&ar->data_lock);
6109 for (i = 0; i < ARRAY_SIZE(sta->txq); i++)
6110 ath10k_mac_txq_unref(ar, sta->txq[i]);
6112 if (!sta->tdls)
6113 goto exit;
6115 if (ath10k_mac_tdls_vif_stations_count(hw, vif))
6116 goto exit;
6118 /* This was the last tdls peer in current vif */
6119 ret = ath10k_wmi_update_fw_tdls_state(ar, arvif->vdev_id,
6120 WMI_TDLS_DISABLE);
6121 if (ret) {
6122 ath10k_warn(ar, "failed to update fw tdls state on vdev %i: %i\n",
6123 arvif->vdev_id, ret);
6125 } else if (old_state == IEEE80211_STA_AUTH &&
6126 new_state == IEEE80211_STA_ASSOC &&
6127 (vif->type == NL80211_IFTYPE_AP ||
6128 vif->type == NL80211_IFTYPE_MESH_POINT ||
6129 vif->type == NL80211_IFTYPE_ADHOC)) {
6131 * New association.
6133 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
6134 sta->addr);
6136 ret = ath10k_station_assoc(ar, vif, sta, false);
6137 if (ret)
6138 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
6139 sta->addr, arvif->vdev_id, ret);
6140 } else if (old_state == IEEE80211_STA_ASSOC &&
6141 new_state == IEEE80211_STA_AUTHORIZED &&
6142 sta->tdls) {
6144 * Tdls station authorized.
6146 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac tdls sta %pM authorized\n",
6147 sta->addr);
6149 ret = ath10k_station_assoc(ar, vif, sta, false);
6150 if (ret) {
6151 ath10k_warn(ar, "failed to associate tdls station %pM for vdev %i: %i\n",
6152 sta->addr, arvif->vdev_id, ret);
6153 goto exit;
6156 ret = ath10k_mac_tdls_peer_update(ar, arvif->vdev_id, sta,
6157 WMI_TDLS_PEER_STATE_CONNECTED);
6158 if (ret)
6159 ath10k_warn(ar, "failed to update tdls peer %pM for vdev %i: %i\n",
6160 sta->addr, arvif->vdev_id, ret);
6161 } else if (old_state == IEEE80211_STA_ASSOC &&
6162 new_state == IEEE80211_STA_AUTH &&
6163 (vif->type == NL80211_IFTYPE_AP ||
6164 vif->type == NL80211_IFTYPE_MESH_POINT ||
6165 vif->type == NL80211_IFTYPE_ADHOC)) {
6167 * Disassociation.
6169 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
6170 sta->addr);
6172 ret = ath10k_station_disassoc(ar, vif, sta);
6173 if (ret)
6174 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
6175 sta->addr, arvif->vdev_id, ret);
6177 exit:
6178 mutex_unlock(&ar->conf_mutex);
6179 return ret;
6182 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
6183 u16 ac, bool enable)
6185 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6186 struct wmi_sta_uapsd_auto_trig_arg arg = {};
6187 u32 prio = 0, acc = 0;
6188 u32 value = 0;
6189 int ret = 0;
6191 lockdep_assert_held(&ar->conf_mutex);
6193 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
6194 return 0;
6196 switch (ac) {
6197 case IEEE80211_AC_VO:
6198 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
6199 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
6200 prio = 7;
6201 acc = 3;
6202 break;
6203 case IEEE80211_AC_VI:
6204 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
6205 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
6206 prio = 5;
6207 acc = 2;
6208 break;
6209 case IEEE80211_AC_BE:
6210 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
6211 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
6212 prio = 2;
6213 acc = 1;
6214 break;
6215 case IEEE80211_AC_BK:
6216 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
6217 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
6218 prio = 0;
6219 acc = 0;
6220 break;
6223 if (enable)
6224 arvif->u.sta.uapsd |= value;
6225 else
6226 arvif->u.sta.uapsd &= ~value;
6228 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6229 WMI_STA_PS_PARAM_UAPSD,
6230 arvif->u.sta.uapsd);
6231 if (ret) {
6232 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
6233 goto exit;
6236 if (arvif->u.sta.uapsd)
6237 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
6238 else
6239 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
6241 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
6242 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
6243 value);
6244 if (ret)
6245 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
6247 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
6248 if (ret) {
6249 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
6250 arvif->vdev_id, ret);
6251 return ret;
6254 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
6255 if (ret) {
6256 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
6257 arvif->vdev_id, ret);
6258 return ret;
6261 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
6262 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
6263 /* Only userspace can make an educated decision when to send
6264 * trigger frame. The following effectively disables u-UAPSD
6265 * autotrigger in firmware (which is enabled by default
6266 * provided the autotrigger service is available).
6269 arg.wmm_ac = acc;
6270 arg.user_priority = prio;
6271 arg.service_interval = 0;
6272 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6273 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
6275 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
6276 arvif->bssid, &arg, 1);
6277 if (ret) {
6278 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
6279 ret);
6280 return ret;
6284 exit:
6285 return ret;
6288 static int ath10k_conf_tx(struct ieee80211_hw *hw,
6289 struct ieee80211_vif *vif, u16 ac,
6290 const struct ieee80211_tx_queue_params *params)
6292 struct ath10k *ar = hw->priv;
6293 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6294 struct wmi_wmm_params_arg *p = NULL;
6295 int ret;
6297 mutex_lock(&ar->conf_mutex);
6299 switch (ac) {
6300 case IEEE80211_AC_VO:
6301 p = &arvif->wmm_params.ac_vo;
6302 break;
6303 case IEEE80211_AC_VI:
6304 p = &arvif->wmm_params.ac_vi;
6305 break;
6306 case IEEE80211_AC_BE:
6307 p = &arvif->wmm_params.ac_be;
6308 break;
6309 case IEEE80211_AC_BK:
6310 p = &arvif->wmm_params.ac_bk;
6311 break;
6314 if (WARN_ON(!p)) {
6315 ret = -EINVAL;
6316 goto exit;
6319 p->cwmin = params->cw_min;
6320 p->cwmax = params->cw_max;
6321 p->aifs = params->aifs;
6324 * The channel time duration programmed in the HW is in absolute
6325 * microseconds, while mac80211 gives the txop in units of
6326 * 32 microseconds.
6328 p->txop = params->txop * 32;
6330 if (ar->wmi.ops->gen_vdev_wmm_conf) {
6331 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
6332 &arvif->wmm_params);
6333 if (ret) {
6334 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
6335 arvif->vdev_id, ret);
6336 goto exit;
6338 } else {
6339 /* This won't work well with multi-interface cases but it's
6340 * better than nothing.
6342 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
6343 if (ret) {
6344 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
6345 goto exit;
6349 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
6350 if (ret)
6351 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
6353 exit:
6354 mutex_unlock(&ar->conf_mutex);
6355 return ret;
6358 #define ATH10K_ROC_TIMEOUT_HZ (2 * HZ)
6360 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
6361 struct ieee80211_vif *vif,
6362 struct ieee80211_channel *chan,
6363 int duration,
6364 enum ieee80211_roc_type type)
6366 struct ath10k *ar = hw->priv;
6367 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6368 struct wmi_start_scan_arg arg;
6369 int ret = 0;
6370 u32 scan_time_msec;
6372 mutex_lock(&ar->conf_mutex);
6374 spin_lock_bh(&ar->data_lock);
6375 switch (ar->scan.state) {
6376 case ATH10K_SCAN_IDLE:
6377 reinit_completion(&ar->scan.started);
6378 reinit_completion(&ar->scan.completed);
6379 reinit_completion(&ar->scan.on_channel);
6380 ar->scan.state = ATH10K_SCAN_STARTING;
6381 ar->scan.is_roc = true;
6382 ar->scan.vdev_id = arvif->vdev_id;
6383 ar->scan.roc_freq = chan->center_freq;
6384 ar->scan.roc_notify = true;
6385 ret = 0;
6386 break;
6387 case ATH10K_SCAN_STARTING:
6388 case ATH10K_SCAN_RUNNING:
6389 case ATH10K_SCAN_ABORTING:
6390 ret = -EBUSY;
6391 break;
6393 spin_unlock_bh(&ar->data_lock);
6395 if (ret)
6396 goto exit;
6398 scan_time_msec = ar->hw->wiphy->max_remain_on_channel_duration * 2;
6400 memset(&arg, 0, sizeof(arg));
6401 ath10k_wmi_start_scan_init(ar, &arg);
6402 arg.vdev_id = arvif->vdev_id;
6403 arg.scan_id = ATH10K_SCAN_ID;
6404 arg.n_channels = 1;
6405 arg.channels[0] = chan->center_freq;
6406 arg.dwell_time_active = scan_time_msec;
6407 arg.dwell_time_passive = scan_time_msec;
6408 arg.max_scan_time = scan_time_msec;
6409 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
6410 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
6411 arg.burst_duration_ms = duration;
6413 ret = ath10k_start_scan(ar, &arg);
6414 if (ret) {
6415 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
6416 spin_lock_bh(&ar->data_lock);
6417 ar->scan.state = ATH10K_SCAN_IDLE;
6418 spin_unlock_bh(&ar->data_lock);
6419 goto exit;
6422 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3 * HZ);
6423 if (ret == 0) {
6424 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
6426 ret = ath10k_scan_stop(ar);
6427 if (ret)
6428 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
6430 ret = -ETIMEDOUT;
6431 goto exit;
6434 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
6435 msecs_to_jiffies(duration));
6437 ret = 0;
6438 exit:
6439 mutex_unlock(&ar->conf_mutex);
6440 return ret;
6443 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
6445 struct ath10k *ar = hw->priv;
6447 mutex_lock(&ar->conf_mutex);
6449 spin_lock_bh(&ar->data_lock);
6450 ar->scan.roc_notify = false;
6451 spin_unlock_bh(&ar->data_lock);
6453 ath10k_scan_abort(ar);
6455 mutex_unlock(&ar->conf_mutex);
6457 cancel_delayed_work_sync(&ar->scan.timeout);
6459 return 0;
6463 * Both RTS and Fragmentation threshold are interface-specific
6464 * in ath10k, but device-specific in mac80211.
6467 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
6469 struct ath10k *ar = hw->priv;
6470 struct ath10k_vif *arvif;
6471 int ret = 0;
6473 mutex_lock(&ar->conf_mutex);
6474 list_for_each_entry(arvif, &ar->arvifs, list) {
6475 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
6476 arvif->vdev_id, value);
6478 ret = ath10k_mac_set_rts(arvif, value);
6479 if (ret) {
6480 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
6481 arvif->vdev_id, ret);
6482 break;
6485 mutex_unlock(&ar->conf_mutex);
6487 return ret;
6490 static int ath10k_mac_op_set_frag_threshold(struct ieee80211_hw *hw, u32 value)
6492 /* Even though there's a WMI enum for fragmentation threshold no known
6493 * firmware actually implements it. Moreover it is not possible to rely
6494 * frame fragmentation to mac80211 because firmware clears the "more
6495 * fragments" bit in frame control making it impossible for remote
6496 * devices to reassemble frames.
6498 * Hence implement a dummy callback just to say fragmentation isn't
6499 * supported. This effectively prevents mac80211 from doing frame
6500 * fragmentation in software.
6502 return -EOPNOTSUPP;
6505 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6506 u32 queues, bool drop)
6508 struct ath10k *ar = hw->priv;
6509 bool skip;
6510 long time_left;
6512 /* mac80211 doesn't care if we really xmit queued frames or not
6513 * we'll collect those frames either way if we stop/delete vdevs */
6514 if (drop)
6515 return;
6517 mutex_lock(&ar->conf_mutex);
6519 if (ar->state == ATH10K_STATE_WEDGED)
6520 goto skip;
6522 time_left = wait_event_timeout(ar->htt.empty_tx_wq, ({
6523 bool empty;
6525 spin_lock_bh(&ar->htt.tx_lock);
6526 empty = (ar->htt.num_pending_tx == 0);
6527 spin_unlock_bh(&ar->htt.tx_lock);
6529 skip = (ar->state == ATH10K_STATE_WEDGED) ||
6530 test_bit(ATH10K_FLAG_CRASH_FLUSH,
6531 &ar->dev_flags);
6533 (empty || skip);
6534 }), ATH10K_FLUSH_TIMEOUT_HZ);
6536 if (time_left == 0 || skip)
6537 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %ld\n",
6538 skip, ar->state, time_left);
6540 skip:
6541 mutex_unlock(&ar->conf_mutex);
6544 /* TODO: Implement this function properly
6545 * For now it is needed to reply to Probe Requests in IBSS mode.
6546 * Propably we need this information from FW.
6548 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
6550 return 1;
6553 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
6554 enum ieee80211_reconfig_type reconfig_type)
6556 struct ath10k *ar = hw->priv;
6558 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
6559 return;
6561 mutex_lock(&ar->conf_mutex);
6563 /* If device failed to restart it will be in a different state, e.g.
6564 * ATH10K_STATE_WEDGED */
6565 if (ar->state == ATH10K_STATE_RESTARTED) {
6566 ath10k_info(ar, "device successfully recovered\n");
6567 ar->state = ATH10K_STATE_ON;
6568 ieee80211_wake_queues(ar->hw);
6571 mutex_unlock(&ar->conf_mutex);
6574 static void
6575 ath10k_mac_update_bss_chan_survey(struct ath10k *ar,
6576 struct ieee80211_channel *channel)
6578 int ret;
6579 enum wmi_bss_survey_req_type type = WMI_BSS_SURVEY_REQ_TYPE_READ_CLEAR;
6581 lockdep_assert_held(&ar->conf_mutex);
6583 if (!test_bit(WMI_SERVICE_BSS_CHANNEL_INFO_64, ar->wmi.svc_map) ||
6584 (ar->rx_channel != channel))
6585 return;
6587 if (ar->scan.state != ATH10K_SCAN_IDLE) {
6588 ath10k_dbg(ar, ATH10K_DBG_MAC, "ignoring bss chan info request while scanning..\n");
6589 return;
6592 reinit_completion(&ar->bss_survey_done);
6594 ret = ath10k_wmi_pdev_bss_chan_info_request(ar, type);
6595 if (ret) {
6596 ath10k_warn(ar, "failed to send pdev bss chan info request\n");
6597 return;
6600 ret = wait_for_completion_timeout(&ar->bss_survey_done, 3 * HZ);
6601 if (!ret) {
6602 ath10k_warn(ar, "bss channel survey timed out\n");
6603 return;
6607 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
6608 struct survey_info *survey)
6610 struct ath10k *ar = hw->priv;
6611 struct ieee80211_supported_band *sband;
6612 struct survey_info *ar_survey = &ar->survey[idx];
6613 int ret = 0;
6615 mutex_lock(&ar->conf_mutex);
6617 sband = hw->wiphy->bands[NL80211_BAND_2GHZ];
6618 if (sband && idx >= sband->n_channels) {
6619 idx -= sband->n_channels;
6620 sband = NULL;
6623 if (!sband)
6624 sband = hw->wiphy->bands[NL80211_BAND_5GHZ];
6626 if (!sband || idx >= sband->n_channels) {
6627 ret = -ENOENT;
6628 goto exit;
6631 ath10k_mac_update_bss_chan_survey(ar, &sband->channels[idx]);
6633 spin_lock_bh(&ar->data_lock);
6634 memcpy(survey, ar_survey, sizeof(*survey));
6635 spin_unlock_bh(&ar->data_lock);
6637 survey->channel = &sband->channels[idx];
6639 if (ar->rx_channel == survey->channel)
6640 survey->filled |= SURVEY_INFO_IN_USE;
6642 exit:
6643 mutex_unlock(&ar->conf_mutex);
6644 return ret;
6647 static bool
6648 ath10k_mac_bitrate_mask_has_single_rate(struct ath10k *ar,
6649 enum nl80211_band band,
6650 const struct cfg80211_bitrate_mask *mask)
6652 int num_rates = 0;
6653 int i;
6655 num_rates += hweight32(mask->control[band].legacy);
6657 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++)
6658 num_rates += hweight8(mask->control[band].ht_mcs[i]);
6660 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++)
6661 num_rates += hweight16(mask->control[band].vht_mcs[i]);
6663 return num_rates == 1;
6666 static bool
6667 ath10k_mac_bitrate_mask_get_single_nss(struct ath10k *ar,
6668 enum nl80211_band band,
6669 const struct cfg80211_bitrate_mask *mask,
6670 int *nss)
6672 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6673 u16 vht_mcs_map = le16_to_cpu(sband->vht_cap.vht_mcs.tx_mcs_map);
6674 u8 ht_nss_mask = 0;
6675 u8 vht_nss_mask = 0;
6676 int i;
6678 if (mask->control[band].legacy)
6679 return false;
6681 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6682 if (mask->control[band].ht_mcs[i] == 0)
6683 continue;
6684 else if (mask->control[band].ht_mcs[i] ==
6685 sband->ht_cap.mcs.rx_mask[i])
6686 ht_nss_mask |= BIT(i);
6687 else
6688 return false;
6691 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6692 if (mask->control[band].vht_mcs[i] == 0)
6693 continue;
6694 else if (mask->control[band].vht_mcs[i] ==
6695 ath10k_mac_get_max_vht_mcs_map(vht_mcs_map, i))
6696 vht_nss_mask |= BIT(i);
6697 else
6698 return false;
6701 if (ht_nss_mask != vht_nss_mask)
6702 return false;
6704 if (ht_nss_mask == 0)
6705 return false;
6707 if (BIT(fls(ht_nss_mask)) - 1 != ht_nss_mask)
6708 return false;
6710 *nss = fls(ht_nss_mask);
6712 return true;
6715 static int
6716 ath10k_mac_bitrate_mask_get_single_rate(struct ath10k *ar,
6717 enum nl80211_band band,
6718 const struct cfg80211_bitrate_mask *mask,
6719 u8 *rate, u8 *nss)
6721 struct ieee80211_supported_band *sband = &ar->mac.sbands[band];
6722 int rate_idx;
6723 int i;
6724 u16 bitrate;
6725 u8 preamble;
6726 u8 hw_rate;
6728 if (hweight32(mask->control[band].legacy) == 1) {
6729 rate_idx = ffs(mask->control[band].legacy) - 1;
6731 hw_rate = sband->bitrates[rate_idx].hw_value;
6732 bitrate = sband->bitrates[rate_idx].bitrate;
6734 if (ath10k_mac_bitrate_is_cck(bitrate))
6735 preamble = WMI_RATE_PREAMBLE_CCK;
6736 else
6737 preamble = WMI_RATE_PREAMBLE_OFDM;
6739 *nss = 1;
6740 *rate = preamble << 6 |
6741 (*nss - 1) << 4 |
6742 hw_rate << 0;
6744 return 0;
6747 for (i = 0; i < ARRAY_SIZE(mask->control[band].ht_mcs); i++) {
6748 if (hweight8(mask->control[band].ht_mcs[i]) == 1) {
6749 *nss = i + 1;
6750 *rate = WMI_RATE_PREAMBLE_HT << 6 |
6751 (*nss - 1) << 4 |
6752 (ffs(mask->control[band].ht_mcs[i]) - 1);
6754 return 0;
6758 for (i = 0; i < ARRAY_SIZE(mask->control[band].vht_mcs); i++) {
6759 if (hweight16(mask->control[band].vht_mcs[i]) == 1) {
6760 *nss = i + 1;
6761 *rate = WMI_RATE_PREAMBLE_VHT << 6 |
6762 (*nss - 1) << 4 |
6763 (ffs(mask->control[band].vht_mcs[i]) - 1);
6765 return 0;
6769 return -EINVAL;
6772 static int ath10k_mac_set_fixed_rate_params(struct ath10k_vif *arvif,
6773 u8 rate, u8 nss, u8 sgi, u8 ldpc)
6775 struct ath10k *ar = arvif->ar;
6776 u32 vdev_param;
6777 int ret;
6779 lockdep_assert_held(&ar->conf_mutex);
6781 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac set fixed rate params vdev %i rate 0x%02hhx nss %hhu sgi %hhu\n",
6782 arvif->vdev_id, rate, nss, sgi);
6784 vdev_param = ar->wmi.vdev_param->fixed_rate;
6785 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, rate);
6786 if (ret) {
6787 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
6788 rate, ret);
6789 return ret;
6792 vdev_param = ar->wmi.vdev_param->nss;
6793 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, nss);
6794 if (ret) {
6795 ath10k_warn(ar, "failed to set nss param %d: %d\n", nss, ret);
6796 return ret;
6799 vdev_param = ar->wmi.vdev_param->sgi;
6800 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, sgi);
6801 if (ret) {
6802 ath10k_warn(ar, "failed to set sgi param %d: %d\n", sgi, ret);
6803 return ret;
6806 vdev_param = ar->wmi.vdev_param->ldpc;
6807 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, ldpc);
6808 if (ret) {
6809 ath10k_warn(ar, "failed to set ldpc param %d: %d\n", ldpc, ret);
6810 return ret;
6813 return 0;
6816 static bool
6817 ath10k_mac_can_set_bitrate_mask(struct ath10k *ar,
6818 enum nl80211_band band,
6819 const struct cfg80211_bitrate_mask *mask)
6821 int i;
6822 u16 vht_mcs;
6824 /* Due to firmware limitation in WMI_PEER_ASSOC_CMDID it is impossible
6825 * to express all VHT MCS rate masks. Effectively only the following
6826 * ranges can be used: none, 0-7, 0-8 and 0-9.
6828 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
6829 vht_mcs = mask->control[band].vht_mcs[i];
6831 switch (vht_mcs) {
6832 case 0:
6833 case BIT(8) - 1:
6834 case BIT(9) - 1:
6835 case BIT(10) - 1:
6836 break;
6837 default:
6838 ath10k_warn(ar, "refusing bitrate mask with missing 0-7 VHT MCS rates\n");
6839 return false;
6843 return true;
6846 static void ath10k_mac_set_bitrate_mask_iter(void *data,
6847 struct ieee80211_sta *sta)
6849 struct ath10k_vif *arvif = data;
6850 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6851 struct ath10k *ar = arvif->ar;
6853 if (arsta->arvif != arvif)
6854 return;
6856 spin_lock_bh(&ar->data_lock);
6857 arsta->changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
6858 spin_unlock_bh(&ar->data_lock);
6860 ieee80211_queue_work(ar->hw, &arsta->update_wk);
6863 static int ath10k_mac_op_set_bitrate_mask(struct ieee80211_hw *hw,
6864 struct ieee80211_vif *vif,
6865 const struct cfg80211_bitrate_mask *mask)
6867 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
6868 struct cfg80211_chan_def def;
6869 struct ath10k *ar = arvif->ar;
6870 enum nl80211_band band;
6871 const u8 *ht_mcs_mask;
6872 const u16 *vht_mcs_mask;
6873 u8 rate;
6874 u8 nss;
6875 u8 sgi;
6876 u8 ldpc;
6877 int single_nss;
6878 int ret;
6880 if (ath10k_mac_vif_chan(vif, &def))
6881 return -EPERM;
6883 band = def.chan->band;
6884 ht_mcs_mask = mask->control[band].ht_mcs;
6885 vht_mcs_mask = mask->control[band].vht_mcs;
6886 ldpc = !!(ar->ht_cap_info & WMI_HT_CAP_LDPC);
6888 sgi = mask->control[band].gi;
6889 if (sgi == NL80211_TXRATE_FORCE_LGI)
6890 return -EINVAL;
6892 if (ath10k_mac_bitrate_mask_has_single_rate(ar, band, mask)) {
6893 ret = ath10k_mac_bitrate_mask_get_single_rate(ar, band, mask,
6894 &rate, &nss);
6895 if (ret) {
6896 ath10k_warn(ar, "failed to get single rate for vdev %i: %d\n",
6897 arvif->vdev_id, ret);
6898 return ret;
6900 } else if (ath10k_mac_bitrate_mask_get_single_nss(ar, band, mask,
6901 &single_nss)) {
6902 rate = WMI_FIXED_RATE_NONE;
6903 nss = single_nss;
6904 } else {
6905 rate = WMI_FIXED_RATE_NONE;
6906 nss = min(ar->num_rf_chains,
6907 max(ath10k_mac_max_ht_nss(ht_mcs_mask),
6908 ath10k_mac_max_vht_nss(vht_mcs_mask)));
6910 if (!ath10k_mac_can_set_bitrate_mask(ar, band, mask))
6911 return -EINVAL;
6913 mutex_lock(&ar->conf_mutex);
6915 arvif->bitrate_mask = *mask;
6916 ieee80211_iterate_stations_atomic(ar->hw,
6917 ath10k_mac_set_bitrate_mask_iter,
6918 arvif);
6920 mutex_unlock(&ar->conf_mutex);
6923 mutex_lock(&ar->conf_mutex);
6925 ret = ath10k_mac_set_fixed_rate_params(arvif, rate, nss, sgi, ldpc);
6926 if (ret) {
6927 ath10k_warn(ar, "failed to set fixed rate params on vdev %i: %d\n",
6928 arvif->vdev_id, ret);
6929 goto exit;
6932 exit:
6933 mutex_unlock(&ar->conf_mutex);
6935 return ret;
6938 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
6939 struct ieee80211_vif *vif,
6940 struct ieee80211_sta *sta,
6941 u32 changed)
6943 struct ath10k *ar = hw->priv;
6944 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
6945 u32 bw, smps;
6947 spin_lock_bh(&ar->data_lock);
6949 ath10k_dbg(ar, ATH10K_DBG_MAC,
6950 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
6951 sta->addr, changed, sta->bandwidth, sta->rx_nss,
6952 sta->smps_mode);
6954 if (changed & IEEE80211_RC_BW_CHANGED) {
6955 bw = WMI_PEER_CHWIDTH_20MHZ;
6957 switch (sta->bandwidth) {
6958 case IEEE80211_STA_RX_BW_20:
6959 bw = WMI_PEER_CHWIDTH_20MHZ;
6960 break;
6961 case IEEE80211_STA_RX_BW_40:
6962 bw = WMI_PEER_CHWIDTH_40MHZ;
6963 break;
6964 case IEEE80211_STA_RX_BW_80:
6965 bw = WMI_PEER_CHWIDTH_80MHZ;
6966 break;
6967 case IEEE80211_STA_RX_BW_160:
6968 ath10k_warn(ar, "Invalid bandwidth %d in rc update for %pM\n",
6969 sta->bandwidth, sta->addr);
6970 bw = WMI_PEER_CHWIDTH_20MHZ;
6971 break;
6974 arsta->bw = bw;
6977 if (changed & IEEE80211_RC_NSS_CHANGED)
6978 arsta->nss = sta->rx_nss;
6980 if (changed & IEEE80211_RC_SMPS_CHANGED) {
6981 smps = WMI_PEER_SMPS_PS_NONE;
6983 switch (sta->smps_mode) {
6984 case IEEE80211_SMPS_AUTOMATIC:
6985 case IEEE80211_SMPS_OFF:
6986 smps = WMI_PEER_SMPS_PS_NONE;
6987 break;
6988 case IEEE80211_SMPS_STATIC:
6989 smps = WMI_PEER_SMPS_STATIC;
6990 break;
6991 case IEEE80211_SMPS_DYNAMIC:
6992 smps = WMI_PEER_SMPS_DYNAMIC;
6993 break;
6994 case IEEE80211_SMPS_NUM_MODES:
6995 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
6996 sta->smps_mode, sta->addr);
6997 smps = WMI_PEER_SMPS_PS_NONE;
6998 break;
7001 arsta->smps = smps;
7004 arsta->changed |= changed;
7006 spin_unlock_bh(&ar->data_lock);
7008 ieee80211_queue_work(hw, &arsta->update_wk);
7011 static void ath10k_offset_tsf(struct ieee80211_hw *hw,
7012 struct ieee80211_vif *vif, s64 tsf_offset)
7014 struct ath10k *ar = hw->priv;
7015 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7016 u32 offset, vdev_param;
7017 int ret;
7019 if (tsf_offset < 0) {
7020 vdev_param = ar->wmi.vdev_param->dec_tsf;
7021 offset = -tsf_offset;
7022 } else {
7023 vdev_param = ar->wmi.vdev_param->inc_tsf;
7024 offset = tsf_offset;
7027 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
7028 vdev_param, offset);
7030 if (ret && ret != -EOPNOTSUPP)
7031 ath10k_warn(ar, "failed to set tsf offset %d cmd %d: %d\n",
7032 offset, vdev_param, ret);
7035 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
7036 struct ieee80211_vif *vif,
7037 struct ieee80211_ampdu_params *params)
7039 struct ath10k *ar = hw->priv;
7040 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7041 struct ieee80211_sta *sta = params->sta;
7042 enum ieee80211_ampdu_mlme_action action = params->action;
7043 u16 tid = params->tid;
7045 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
7046 arvif->vdev_id, sta->addr, tid, action);
7048 switch (action) {
7049 case IEEE80211_AMPDU_RX_START:
7050 case IEEE80211_AMPDU_RX_STOP:
7051 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
7052 * creation/removal. Do we need to verify this?
7054 return 0;
7055 case IEEE80211_AMPDU_TX_START:
7056 case IEEE80211_AMPDU_TX_STOP_CONT:
7057 case IEEE80211_AMPDU_TX_STOP_FLUSH:
7058 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
7059 case IEEE80211_AMPDU_TX_OPERATIONAL:
7060 /* Firmware offloads Tx aggregation entirely so deny mac80211
7061 * Tx aggregation requests.
7063 return -EOPNOTSUPP;
7066 return -EINVAL;
7069 static void
7070 ath10k_mac_update_rx_channel(struct ath10k *ar,
7071 struct ieee80211_chanctx_conf *ctx,
7072 struct ieee80211_vif_chanctx_switch *vifs,
7073 int n_vifs)
7075 struct cfg80211_chan_def *def = NULL;
7077 /* Both locks are required because ar->rx_channel is modified. This
7078 * allows readers to hold either lock.
7080 lockdep_assert_held(&ar->conf_mutex);
7081 lockdep_assert_held(&ar->data_lock);
7083 WARN_ON(ctx && vifs);
7084 WARN_ON(vifs && n_vifs != 1);
7086 /* FIXME: Sort of an optimization and a workaround. Peers and vifs are
7087 * on a linked list now. Doing a lookup peer -> vif -> chanctx for each
7088 * ppdu on Rx may reduce performance on low-end systems. It should be
7089 * possible to make tables/hashmaps to speed the lookup up (be vary of
7090 * cpu data cache lines though regarding sizes) but to keep the initial
7091 * implementation simple and less intrusive fallback to the slow lookup
7092 * only for multi-channel cases. Single-channel cases will remain to
7093 * use the old channel derival and thus performance should not be
7094 * affected much.
7096 rcu_read_lock();
7097 if (!ctx && ath10k_mac_num_chanctxs(ar) == 1) {
7098 ieee80211_iter_chan_contexts_atomic(ar->hw,
7099 ath10k_mac_get_any_chandef_iter,
7100 &def);
7102 if (vifs)
7103 def = &vifs[0].new_ctx->def;
7105 ar->rx_channel = def->chan;
7106 } else if ((ctx && ath10k_mac_num_chanctxs(ar) == 0) ||
7107 (ctx && (ar->state == ATH10K_STATE_RESTARTED))) {
7108 /* During driver restart due to firmware assert, since mac80211
7109 * already has valid channel context for given radio, channel
7110 * context iteration return num_chanctx > 0. So fix rx_channel
7111 * when restart is in progress.
7113 ar->rx_channel = ctx->def.chan;
7114 } else {
7115 ar->rx_channel = NULL;
7117 rcu_read_unlock();
7120 static void
7121 ath10k_mac_update_vif_chan(struct ath10k *ar,
7122 struct ieee80211_vif_chanctx_switch *vifs,
7123 int n_vifs)
7125 struct ath10k_vif *arvif;
7126 int ret;
7127 int i;
7129 lockdep_assert_held(&ar->conf_mutex);
7131 /* First stop monitor interface. Some FW versions crash if there's a
7132 * lone monitor interface.
7134 if (ar->monitor_started)
7135 ath10k_monitor_stop(ar);
7137 for (i = 0; i < n_vifs; i++) {
7138 arvif = ath10k_vif_to_arvif(vifs[i].vif);
7140 ath10k_dbg(ar, ATH10K_DBG_MAC,
7141 "mac chanctx switch vdev_id %i freq %hu->%hu width %d->%d\n",
7142 arvif->vdev_id,
7143 vifs[i].old_ctx->def.chan->center_freq,
7144 vifs[i].new_ctx->def.chan->center_freq,
7145 vifs[i].old_ctx->def.width,
7146 vifs[i].new_ctx->def.width);
7148 if (WARN_ON(!arvif->is_started))
7149 continue;
7151 if (WARN_ON(!arvif->is_up))
7152 continue;
7154 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7155 if (ret) {
7156 ath10k_warn(ar, "failed to down vdev %d: %d\n",
7157 arvif->vdev_id, ret);
7158 continue;
7162 /* All relevant vdevs are downed and associated channel resources
7163 * should be available for the channel switch now.
7166 spin_lock_bh(&ar->data_lock);
7167 ath10k_mac_update_rx_channel(ar, NULL, vifs, n_vifs);
7168 spin_unlock_bh(&ar->data_lock);
7170 for (i = 0; i < n_vifs; i++) {
7171 arvif = ath10k_vif_to_arvif(vifs[i].vif);
7173 if (WARN_ON(!arvif->is_started))
7174 continue;
7176 if (WARN_ON(!arvif->is_up))
7177 continue;
7179 ret = ath10k_mac_setup_bcn_tmpl(arvif);
7180 if (ret)
7181 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
7182 ret);
7184 ret = ath10k_mac_setup_prb_tmpl(arvif);
7185 if (ret)
7186 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
7187 ret);
7189 ret = ath10k_vdev_restart(arvif, &vifs[i].new_ctx->def);
7190 if (ret) {
7191 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
7192 arvif->vdev_id, ret);
7193 continue;
7196 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
7197 arvif->bssid);
7198 if (ret) {
7199 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
7200 arvif->vdev_id, ret);
7201 continue;
7205 ath10k_monitor_recalc(ar);
7208 static int
7209 ath10k_mac_op_add_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 add 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, ctx, 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);
7229 return 0;
7232 static void
7233 ath10k_mac_op_remove_chanctx(struct ieee80211_hw *hw,
7234 struct ieee80211_chanctx_conf *ctx)
7236 struct ath10k *ar = hw->priv;
7238 ath10k_dbg(ar, ATH10K_DBG_MAC,
7239 "mac chanctx remove freq %hu width %d ptr %pK\n",
7240 ctx->def.chan->center_freq, ctx->def.width, ctx);
7242 mutex_lock(&ar->conf_mutex);
7244 spin_lock_bh(&ar->data_lock);
7245 ath10k_mac_update_rx_channel(ar, NULL, NULL, 0);
7246 spin_unlock_bh(&ar->data_lock);
7248 ath10k_recalc_radar_detection(ar);
7249 ath10k_monitor_recalc(ar);
7251 mutex_unlock(&ar->conf_mutex);
7254 struct ath10k_mac_change_chanctx_arg {
7255 struct ieee80211_chanctx_conf *ctx;
7256 struct ieee80211_vif_chanctx_switch *vifs;
7257 int n_vifs;
7258 int next_vif;
7261 static void
7262 ath10k_mac_change_chanctx_cnt_iter(void *data, u8 *mac,
7263 struct ieee80211_vif *vif)
7265 struct ath10k_mac_change_chanctx_arg *arg = data;
7267 if (rcu_access_pointer(vif->chanctx_conf) != arg->ctx)
7268 return;
7270 arg->n_vifs++;
7273 static void
7274 ath10k_mac_change_chanctx_fill_iter(void *data, u8 *mac,
7275 struct ieee80211_vif *vif)
7277 struct ath10k_mac_change_chanctx_arg *arg = data;
7278 struct ieee80211_chanctx_conf *ctx;
7280 ctx = rcu_access_pointer(vif->chanctx_conf);
7281 if (ctx != arg->ctx)
7282 return;
7284 if (WARN_ON(arg->next_vif == arg->n_vifs))
7285 return;
7287 arg->vifs[arg->next_vif].vif = vif;
7288 arg->vifs[arg->next_vif].old_ctx = ctx;
7289 arg->vifs[arg->next_vif].new_ctx = ctx;
7290 arg->next_vif++;
7293 static void
7294 ath10k_mac_op_change_chanctx(struct ieee80211_hw *hw,
7295 struct ieee80211_chanctx_conf *ctx,
7296 u32 changed)
7298 struct ath10k *ar = hw->priv;
7299 struct ath10k_mac_change_chanctx_arg arg = { .ctx = ctx };
7301 mutex_lock(&ar->conf_mutex);
7303 ath10k_dbg(ar, ATH10K_DBG_MAC,
7304 "mac chanctx change freq %hu width %d ptr %pK changed %x\n",
7305 ctx->def.chan->center_freq, ctx->def.width, ctx, changed);
7307 /* This shouldn't really happen because channel switching should use
7308 * switch_vif_chanctx().
7310 if (WARN_ON(changed & IEEE80211_CHANCTX_CHANGE_CHANNEL))
7311 goto unlock;
7313 if (changed & IEEE80211_CHANCTX_CHANGE_WIDTH) {
7314 ieee80211_iterate_active_interfaces_atomic(
7316 IEEE80211_IFACE_ITER_NORMAL,
7317 ath10k_mac_change_chanctx_cnt_iter,
7318 &arg);
7319 if (arg.n_vifs == 0)
7320 goto radar;
7322 arg.vifs = kcalloc(arg.n_vifs, sizeof(arg.vifs[0]),
7323 GFP_KERNEL);
7324 if (!arg.vifs)
7325 goto radar;
7327 ieee80211_iterate_active_interfaces_atomic(
7329 IEEE80211_IFACE_ITER_NORMAL,
7330 ath10k_mac_change_chanctx_fill_iter,
7331 &arg);
7332 ath10k_mac_update_vif_chan(ar, arg.vifs, arg.n_vifs);
7333 kfree(arg.vifs);
7336 radar:
7337 ath10k_recalc_radar_detection(ar);
7339 /* FIXME: How to configure Rx chains properly? */
7341 /* No other actions are actually necessary. Firmware maintains channel
7342 * definitions per vdev internally and there's no host-side channel
7343 * context abstraction to configure, e.g. channel width.
7346 unlock:
7347 mutex_unlock(&ar->conf_mutex);
7350 static int
7351 ath10k_mac_op_assign_vif_chanctx(struct ieee80211_hw *hw,
7352 struct ieee80211_vif *vif,
7353 struct ieee80211_chanctx_conf *ctx)
7355 struct ath10k *ar = hw->priv;
7356 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7357 int ret;
7359 mutex_lock(&ar->conf_mutex);
7361 ath10k_dbg(ar, ATH10K_DBG_MAC,
7362 "mac chanctx assign ptr %pK vdev_id %i\n",
7363 ctx, arvif->vdev_id);
7365 if (WARN_ON(arvif->is_started)) {
7366 mutex_unlock(&ar->conf_mutex);
7367 return -EBUSY;
7370 ret = ath10k_vdev_start(arvif, &ctx->def);
7371 if (ret) {
7372 ath10k_warn(ar, "failed to start vdev %i addr %pM on freq %d: %d\n",
7373 arvif->vdev_id, vif->addr,
7374 ctx->def.chan->center_freq, ret);
7375 goto err;
7378 arvif->is_started = true;
7380 ret = ath10k_mac_vif_setup_ps(arvif);
7381 if (ret) {
7382 ath10k_warn(ar, "failed to update vdev %i ps: %d\n",
7383 arvif->vdev_id, ret);
7384 goto err_stop;
7387 if (vif->type == NL80211_IFTYPE_MONITOR) {
7388 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, 0, vif->addr);
7389 if (ret) {
7390 ath10k_warn(ar, "failed to up monitor vdev %i: %d\n",
7391 arvif->vdev_id, ret);
7392 goto err_stop;
7395 arvif->is_up = true;
7398 if (ath10k_mac_can_set_cts_prot(arvif)) {
7399 ret = ath10k_mac_set_cts_prot(arvif);
7400 if (ret)
7401 ath10k_warn(ar, "failed to set cts protection for vdev %d: %d\n",
7402 arvif->vdev_id, ret);
7405 mutex_unlock(&ar->conf_mutex);
7406 return 0;
7408 err_stop:
7409 ath10k_vdev_stop(arvif);
7410 arvif->is_started = false;
7411 ath10k_mac_vif_setup_ps(arvif);
7413 err:
7414 mutex_unlock(&ar->conf_mutex);
7415 return ret;
7418 static void
7419 ath10k_mac_op_unassign_vif_chanctx(struct ieee80211_hw *hw,
7420 struct ieee80211_vif *vif,
7421 struct ieee80211_chanctx_conf *ctx)
7423 struct ath10k *ar = hw->priv;
7424 struct ath10k_vif *arvif = (void *)vif->drv_priv;
7425 int ret;
7427 mutex_lock(&ar->conf_mutex);
7429 ath10k_dbg(ar, ATH10K_DBG_MAC,
7430 "mac chanctx unassign ptr %pK vdev_id %i\n",
7431 ctx, arvif->vdev_id);
7433 WARN_ON(!arvif->is_started);
7435 if (vif->type == NL80211_IFTYPE_MONITOR) {
7436 WARN_ON(!arvif->is_up);
7438 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
7439 if (ret)
7440 ath10k_warn(ar, "failed to down monitor vdev %i: %d\n",
7441 arvif->vdev_id, ret);
7443 arvif->is_up = false;
7446 ret = ath10k_vdev_stop(arvif);
7447 if (ret)
7448 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
7449 arvif->vdev_id, ret);
7451 arvif->is_started = false;
7453 mutex_unlock(&ar->conf_mutex);
7456 static int
7457 ath10k_mac_op_switch_vif_chanctx(struct ieee80211_hw *hw,
7458 struct ieee80211_vif_chanctx_switch *vifs,
7459 int n_vifs,
7460 enum ieee80211_chanctx_switch_mode mode)
7462 struct ath10k *ar = hw->priv;
7464 mutex_lock(&ar->conf_mutex);
7466 ath10k_dbg(ar, ATH10K_DBG_MAC,
7467 "mac chanctx switch n_vifs %d mode %d\n",
7468 n_vifs, mode);
7469 ath10k_mac_update_vif_chan(ar, vifs, n_vifs);
7471 mutex_unlock(&ar->conf_mutex);
7472 return 0;
7475 static const struct ieee80211_ops ath10k_ops = {
7476 .tx = ath10k_mac_op_tx,
7477 .wake_tx_queue = ath10k_mac_op_wake_tx_queue,
7478 .start = ath10k_start,
7479 .stop = ath10k_stop,
7480 .config = ath10k_config,
7481 .add_interface = ath10k_add_interface,
7482 .remove_interface = ath10k_remove_interface,
7483 .configure_filter = ath10k_configure_filter,
7484 .bss_info_changed = ath10k_bss_info_changed,
7485 .set_coverage_class = ath10k_mac_op_set_coverage_class,
7486 .hw_scan = ath10k_hw_scan,
7487 .cancel_hw_scan = ath10k_cancel_hw_scan,
7488 .set_key = ath10k_set_key,
7489 .set_default_unicast_key = ath10k_set_default_unicast_key,
7490 .sta_state = ath10k_sta_state,
7491 .conf_tx = ath10k_conf_tx,
7492 .remain_on_channel = ath10k_remain_on_channel,
7493 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
7494 .set_rts_threshold = ath10k_set_rts_threshold,
7495 .set_frag_threshold = ath10k_mac_op_set_frag_threshold,
7496 .flush = ath10k_flush,
7497 .tx_last_beacon = ath10k_tx_last_beacon,
7498 .set_antenna = ath10k_set_antenna,
7499 .get_antenna = ath10k_get_antenna,
7500 .reconfig_complete = ath10k_reconfig_complete,
7501 .get_survey = ath10k_get_survey,
7502 .set_bitrate_mask = ath10k_mac_op_set_bitrate_mask,
7503 .sta_rc_update = ath10k_sta_rc_update,
7504 .offset_tsf = ath10k_offset_tsf,
7505 .ampdu_action = ath10k_ampdu_action,
7506 .get_et_sset_count = ath10k_debug_get_et_sset_count,
7507 .get_et_stats = ath10k_debug_get_et_stats,
7508 .get_et_strings = ath10k_debug_get_et_strings,
7509 .add_chanctx = ath10k_mac_op_add_chanctx,
7510 .remove_chanctx = ath10k_mac_op_remove_chanctx,
7511 .change_chanctx = ath10k_mac_op_change_chanctx,
7512 .assign_vif_chanctx = ath10k_mac_op_assign_vif_chanctx,
7513 .unassign_vif_chanctx = ath10k_mac_op_unassign_vif_chanctx,
7514 .switch_vif_chanctx = ath10k_mac_op_switch_vif_chanctx,
7516 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
7518 #ifdef CONFIG_PM
7519 .suspend = ath10k_wow_op_suspend,
7520 .resume = ath10k_wow_op_resume,
7521 #endif
7522 #ifdef CONFIG_MAC80211_DEBUGFS
7523 .sta_add_debugfs = ath10k_sta_add_debugfs,
7524 .sta_statistics = ath10k_sta_statistics,
7525 #endif
7528 #define CHAN2G(_channel, _freq, _flags) { \
7529 .band = NL80211_BAND_2GHZ, \
7530 .hw_value = (_channel), \
7531 .center_freq = (_freq), \
7532 .flags = (_flags), \
7533 .max_antenna_gain = 0, \
7534 .max_power = 30, \
7537 #define CHAN5G(_channel, _freq, _flags) { \
7538 .band = NL80211_BAND_5GHZ, \
7539 .hw_value = (_channel), \
7540 .center_freq = (_freq), \
7541 .flags = (_flags), \
7542 .max_antenna_gain = 0, \
7543 .max_power = 30, \
7546 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
7547 CHAN2G(1, 2412, 0),
7548 CHAN2G(2, 2417, 0),
7549 CHAN2G(3, 2422, 0),
7550 CHAN2G(4, 2427, 0),
7551 CHAN2G(5, 2432, 0),
7552 CHAN2G(6, 2437, 0),
7553 CHAN2G(7, 2442, 0),
7554 CHAN2G(8, 2447, 0),
7555 CHAN2G(9, 2452, 0),
7556 CHAN2G(10, 2457, 0),
7557 CHAN2G(11, 2462, 0),
7558 CHAN2G(12, 2467, 0),
7559 CHAN2G(13, 2472, 0),
7560 CHAN2G(14, 2484, 0),
7563 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
7564 CHAN5G(36, 5180, 0),
7565 CHAN5G(40, 5200, 0),
7566 CHAN5G(44, 5220, 0),
7567 CHAN5G(48, 5240, 0),
7568 CHAN5G(52, 5260, 0),
7569 CHAN5G(56, 5280, 0),
7570 CHAN5G(60, 5300, 0),
7571 CHAN5G(64, 5320, 0),
7572 CHAN5G(100, 5500, 0),
7573 CHAN5G(104, 5520, 0),
7574 CHAN5G(108, 5540, 0),
7575 CHAN5G(112, 5560, 0),
7576 CHAN5G(116, 5580, 0),
7577 CHAN5G(120, 5600, 0),
7578 CHAN5G(124, 5620, 0),
7579 CHAN5G(128, 5640, 0),
7580 CHAN5G(132, 5660, 0),
7581 CHAN5G(136, 5680, 0),
7582 CHAN5G(140, 5700, 0),
7583 CHAN5G(144, 5720, 0),
7584 CHAN5G(149, 5745, 0),
7585 CHAN5G(153, 5765, 0),
7586 CHAN5G(157, 5785, 0),
7587 CHAN5G(161, 5805, 0),
7588 CHAN5G(165, 5825, 0),
7591 struct ath10k *ath10k_mac_create(size_t priv_size)
7593 struct ieee80211_hw *hw;
7594 struct ieee80211_ops *ops;
7595 struct ath10k *ar;
7597 ops = kmemdup(&ath10k_ops, sizeof(ath10k_ops), GFP_KERNEL);
7598 if (!ops)
7599 return NULL;
7601 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, ops);
7602 if (!hw) {
7603 kfree(ops);
7604 return NULL;
7607 ar = hw->priv;
7608 ar->hw = hw;
7609 ar->ops = ops;
7611 return ar;
7614 void ath10k_mac_destroy(struct ath10k *ar)
7616 struct ieee80211_ops *ops = ar->ops;
7618 ieee80211_free_hw(ar->hw);
7619 kfree(ops);
7622 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
7624 .max = 8,
7625 .types = BIT(NL80211_IFTYPE_STATION)
7626 | BIT(NL80211_IFTYPE_P2P_CLIENT)
7629 .max = 3,
7630 .types = BIT(NL80211_IFTYPE_P2P_GO)
7633 .max = 1,
7634 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
7637 .max = 7,
7638 .types = BIT(NL80211_IFTYPE_AP)
7639 #ifdef CONFIG_MAC80211_MESH
7640 | BIT(NL80211_IFTYPE_MESH_POINT)
7641 #endif
7645 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
7647 .max = 8,
7648 .types = BIT(NL80211_IFTYPE_AP)
7649 #ifdef CONFIG_MAC80211_MESH
7650 | BIT(NL80211_IFTYPE_MESH_POINT)
7651 #endif
7654 .max = 1,
7655 .types = BIT(NL80211_IFTYPE_STATION)
7659 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
7661 .limits = ath10k_if_limits,
7662 .n_limits = ARRAY_SIZE(ath10k_if_limits),
7663 .max_interfaces = 8,
7664 .num_different_channels = 1,
7665 .beacon_int_infra_match = true,
7669 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
7671 .limits = ath10k_10x_if_limits,
7672 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
7673 .max_interfaces = 8,
7674 .num_different_channels = 1,
7675 .beacon_int_infra_match = true,
7676 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
7677 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7678 BIT(NL80211_CHAN_WIDTH_20) |
7679 BIT(NL80211_CHAN_WIDTH_40) |
7680 BIT(NL80211_CHAN_WIDTH_80),
7681 #endif
7685 static const struct ieee80211_iface_limit ath10k_tlv_if_limit[] = {
7687 .max = 2,
7688 .types = BIT(NL80211_IFTYPE_STATION),
7691 .max = 2,
7692 .types = BIT(NL80211_IFTYPE_AP) |
7693 #ifdef CONFIG_MAC80211_MESH
7694 BIT(NL80211_IFTYPE_MESH_POINT) |
7695 #endif
7696 BIT(NL80211_IFTYPE_P2P_CLIENT) |
7697 BIT(NL80211_IFTYPE_P2P_GO),
7700 .max = 1,
7701 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7705 static const struct ieee80211_iface_limit ath10k_tlv_qcs_if_limit[] = {
7707 .max = 2,
7708 .types = BIT(NL80211_IFTYPE_STATION),
7711 .max = 2,
7712 .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
7715 .max = 1,
7716 .types = BIT(NL80211_IFTYPE_AP) |
7717 #ifdef CONFIG_MAC80211_MESH
7718 BIT(NL80211_IFTYPE_MESH_POINT) |
7719 #endif
7720 BIT(NL80211_IFTYPE_P2P_GO),
7723 .max = 1,
7724 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
7728 static const struct ieee80211_iface_limit ath10k_tlv_if_limit_ibss[] = {
7730 .max = 1,
7731 .types = BIT(NL80211_IFTYPE_STATION),
7734 .max = 1,
7735 .types = BIT(NL80211_IFTYPE_ADHOC),
7739 /* FIXME: This is not thouroughly tested. These combinations may over- or
7740 * underestimate hw/fw capabilities.
7742 static struct ieee80211_iface_combination ath10k_tlv_if_comb[] = {
7744 .limits = ath10k_tlv_if_limit,
7745 .num_different_channels = 1,
7746 .max_interfaces = 4,
7747 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7750 .limits = ath10k_tlv_if_limit_ibss,
7751 .num_different_channels = 1,
7752 .max_interfaces = 2,
7753 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7757 static struct ieee80211_iface_combination ath10k_tlv_qcs_if_comb[] = {
7759 .limits = ath10k_tlv_if_limit,
7760 .num_different_channels = 1,
7761 .max_interfaces = 4,
7762 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit),
7765 .limits = ath10k_tlv_qcs_if_limit,
7766 .num_different_channels = 2,
7767 .max_interfaces = 4,
7768 .n_limits = ARRAY_SIZE(ath10k_tlv_qcs_if_limit),
7771 .limits = ath10k_tlv_if_limit_ibss,
7772 .num_different_channels = 1,
7773 .max_interfaces = 2,
7774 .n_limits = ARRAY_SIZE(ath10k_tlv_if_limit_ibss),
7778 static const struct ieee80211_iface_limit ath10k_10_4_if_limits[] = {
7780 .max = 1,
7781 .types = BIT(NL80211_IFTYPE_STATION),
7784 .max = 16,
7785 .types = BIT(NL80211_IFTYPE_AP)
7786 #ifdef CONFIG_MAC80211_MESH
7787 | BIT(NL80211_IFTYPE_MESH_POINT)
7788 #endif
7792 static const struct ieee80211_iface_combination ath10k_10_4_if_comb[] = {
7794 .limits = ath10k_10_4_if_limits,
7795 .n_limits = ARRAY_SIZE(ath10k_10_4_if_limits),
7796 .max_interfaces = 16,
7797 .num_different_channels = 1,
7798 .beacon_int_infra_match = true,
7799 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
7800 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
7801 BIT(NL80211_CHAN_WIDTH_20) |
7802 BIT(NL80211_CHAN_WIDTH_40) |
7803 BIT(NL80211_CHAN_WIDTH_80),
7804 #endif
7808 static void ath10k_get_arvif_iter(void *data, u8 *mac,
7809 struct ieee80211_vif *vif)
7811 struct ath10k_vif_iter *arvif_iter = data;
7812 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
7814 if (arvif->vdev_id == arvif_iter->vdev_id)
7815 arvif_iter->arvif = arvif;
7818 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
7820 struct ath10k_vif_iter arvif_iter;
7821 u32 flags;
7823 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
7824 arvif_iter.vdev_id = vdev_id;
7826 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
7827 ieee80211_iterate_active_interfaces_atomic(ar->hw,
7828 flags,
7829 ath10k_get_arvif_iter,
7830 &arvif_iter);
7831 if (!arvif_iter.arvif) {
7832 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
7833 return NULL;
7836 return arvif_iter.arvif;
7839 #define WRD_METHOD "WRDD"
7840 #define WRDD_WIFI (0x07)
7842 static u32 ath10k_mac_wrdd_get_mcc(struct ath10k *ar, union acpi_object *wrdd)
7844 union acpi_object *mcc_pkg;
7845 union acpi_object *domain_type;
7846 union acpi_object *mcc_value;
7847 u32 i;
7849 if (wrdd->type != ACPI_TYPE_PACKAGE ||
7850 wrdd->package.count < 2 ||
7851 wrdd->package.elements[0].type != ACPI_TYPE_INTEGER ||
7852 wrdd->package.elements[0].integer.value != 0) {
7853 ath10k_warn(ar, "ignoring malformed/unsupported wrdd structure\n");
7854 return 0;
7857 for (i = 1; i < wrdd->package.count; ++i) {
7858 mcc_pkg = &wrdd->package.elements[i];
7860 if (mcc_pkg->type != ACPI_TYPE_PACKAGE)
7861 continue;
7862 if (mcc_pkg->package.count < 2)
7863 continue;
7864 if (mcc_pkg->package.elements[0].type != ACPI_TYPE_INTEGER ||
7865 mcc_pkg->package.elements[1].type != ACPI_TYPE_INTEGER)
7866 continue;
7868 domain_type = &mcc_pkg->package.elements[0];
7869 if (domain_type->integer.value != WRDD_WIFI)
7870 continue;
7872 mcc_value = &mcc_pkg->package.elements[1];
7873 return mcc_value->integer.value;
7875 return 0;
7878 static int ath10k_mac_get_wrdd_regulatory(struct ath10k *ar, u16 *rd)
7880 struct pci_dev __maybe_unused *pdev = to_pci_dev(ar->dev);
7881 acpi_handle root_handle;
7882 acpi_handle handle;
7883 struct acpi_buffer wrdd = {ACPI_ALLOCATE_BUFFER, NULL};
7884 acpi_status status;
7885 u32 alpha2_code;
7886 char alpha2[3];
7888 root_handle = ACPI_HANDLE(&pdev->dev);
7889 if (!root_handle)
7890 return -EOPNOTSUPP;
7892 status = acpi_get_handle(root_handle, (acpi_string)WRD_METHOD, &handle);
7893 if (ACPI_FAILURE(status)) {
7894 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7895 "failed to get wrd method %d\n", status);
7896 return -EIO;
7899 status = acpi_evaluate_object(handle, NULL, NULL, &wrdd);
7900 if (ACPI_FAILURE(status)) {
7901 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7902 "failed to call wrdc %d\n", status);
7903 return -EIO;
7906 alpha2_code = ath10k_mac_wrdd_get_mcc(ar, wrdd.pointer);
7907 kfree(wrdd.pointer);
7908 if (!alpha2_code)
7909 return -EIO;
7911 alpha2[0] = (alpha2_code >> 8) & 0xff;
7912 alpha2[1] = (alpha2_code >> 0) & 0xff;
7913 alpha2[2] = '\0';
7915 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7916 "regulatory hint from WRDD (alpha2-code): %s\n", alpha2);
7918 *rd = ath_regd_find_country_by_name(alpha2);
7919 if (*rd == 0xffff)
7920 return -EIO;
7922 *rd |= COUNTRY_ERD_FLAG;
7923 return 0;
7926 static int ath10k_mac_init_rd(struct ath10k *ar)
7928 int ret;
7929 u16 rd;
7931 ret = ath10k_mac_get_wrdd_regulatory(ar, &rd);
7932 if (ret) {
7933 ath10k_dbg(ar, ATH10K_DBG_BOOT,
7934 "fallback to eeprom programmed regulatory settings\n");
7935 rd = ar->hw_eeprom_rd;
7938 ar->ath_common.regulatory.current_rd = rd;
7939 return 0;
7942 int ath10k_mac_register(struct ath10k *ar)
7944 static const u32 cipher_suites[] = {
7945 WLAN_CIPHER_SUITE_WEP40,
7946 WLAN_CIPHER_SUITE_WEP104,
7947 WLAN_CIPHER_SUITE_TKIP,
7948 WLAN_CIPHER_SUITE_CCMP,
7949 WLAN_CIPHER_SUITE_AES_CMAC,
7951 struct ieee80211_supported_band *band;
7952 void *channels;
7953 int ret;
7955 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
7957 SET_IEEE80211_DEV(ar->hw, ar->dev);
7959 BUILD_BUG_ON((ARRAY_SIZE(ath10k_2ghz_channels) +
7960 ARRAY_SIZE(ath10k_5ghz_channels)) !=
7961 ATH10K_NUM_CHANS);
7963 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
7964 channels = kmemdup(ath10k_2ghz_channels,
7965 sizeof(ath10k_2ghz_channels),
7966 GFP_KERNEL);
7967 if (!channels) {
7968 ret = -ENOMEM;
7969 goto err_free;
7972 band = &ar->mac.sbands[NL80211_BAND_2GHZ];
7973 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
7974 band->channels = channels;
7976 if (ar->hw_params.cck_rate_map_rev2) {
7977 band->n_bitrates = ath10k_g_rates_rev2_size;
7978 band->bitrates = ath10k_g_rates_rev2;
7979 } else {
7980 band->n_bitrates = ath10k_g_rates_size;
7981 band->bitrates = ath10k_g_rates;
7984 ar->hw->wiphy->bands[NL80211_BAND_2GHZ] = band;
7987 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
7988 channels = kmemdup(ath10k_5ghz_channels,
7989 sizeof(ath10k_5ghz_channels),
7990 GFP_KERNEL);
7991 if (!channels) {
7992 ret = -ENOMEM;
7993 goto err_free;
7996 band = &ar->mac.sbands[NL80211_BAND_5GHZ];
7997 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
7998 band->channels = channels;
7999 band->n_bitrates = ath10k_a_rates_size;
8000 band->bitrates = ath10k_a_rates;
8001 ar->hw->wiphy->bands[NL80211_BAND_5GHZ] = band;
8004 ath10k_mac_setup_ht_vht_cap(ar);
8006 ar->hw->wiphy->interface_modes =
8007 BIT(NL80211_IFTYPE_STATION) |
8008 BIT(NL80211_IFTYPE_AP) |
8009 BIT(NL80211_IFTYPE_MESH_POINT);
8011 ar->hw->wiphy->available_antennas_rx = ar->cfg_rx_chainmask;
8012 ar->hw->wiphy->available_antennas_tx = ar->cfg_tx_chainmask;
8014 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->normal_mode_fw.fw_file.fw_features))
8015 ar->hw->wiphy->interface_modes |=
8016 BIT(NL80211_IFTYPE_P2P_DEVICE) |
8017 BIT(NL80211_IFTYPE_P2P_CLIENT) |
8018 BIT(NL80211_IFTYPE_P2P_GO);
8020 ieee80211_hw_set(ar->hw, SIGNAL_DBM);
8021 ieee80211_hw_set(ar->hw, SUPPORTS_PS);
8022 ieee80211_hw_set(ar->hw, SUPPORTS_DYNAMIC_PS);
8023 ieee80211_hw_set(ar->hw, MFP_CAPABLE);
8024 ieee80211_hw_set(ar->hw, REPORTS_TX_ACK_STATUS);
8025 ieee80211_hw_set(ar->hw, HAS_RATE_CONTROL);
8026 ieee80211_hw_set(ar->hw, AP_LINK_PS);
8027 ieee80211_hw_set(ar->hw, SPECTRUM_MGMT);
8028 ieee80211_hw_set(ar->hw, SUPPORT_FAST_XMIT);
8029 ieee80211_hw_set(ar->hw, CONNECTION_MONITOR);
8030 ieee80211_hw_set(ar->hw, SUPPORTS_PER_STA_GTK);
8031 ieee80211_hw_set(ar->hw, WANT_MONITOR_VIF);
8032 ieee80211_hw_set(ar->hw, CHANCTX_STA_CSA);
8033 ieee80211_hw_set(ar->hw, QUEUE_CONTROL);
8034 ieee80211_hw_set(ar->hw, SUPPORTS_TX_FRAG);
8035 ieee80211_hw_set(ar->hw, REPORTS_LOW_ACK);
8037 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
8038 ieee80211_hw_set(ar->hw, SW_CRYPTO_CONTROL);
8040 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
8041 ar->hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
8043 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
8044 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
8046 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
8047 ieee80211_hw_set(ar->hw, AMPDU_AGGREGATION);
8048 ieee80211_hw_set(ar->hw, TX_AMPDU_SETUP_IN_HW);
8051 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
8052 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
8054 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
8055 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
8056 ar->hw->txq_data_size = sizeof(struct ath10k_txq);
8058 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
8060 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
8061 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
8063 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
8064 * that userspace (e.g. wpa_supplicant/hostapd) can generate
8065 * correct Probe Responses. This is more of a hack advert..
8067 ar->hw->wiphy->probe_resp_offload |=
8068 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
8069 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
8070 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
8073 if (test_bit(WMI_SERVICE_TDLS, ar->wmi.svc_map))
8074 ar->hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
8076 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
8077 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
8078 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
8080 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
8081 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
8082 NL80211_FEATURE_AP_SCAN;
8084 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
8086 ret = ath10k_wow_init(ar);
8087 if (ret) {
8088 ath10k_warn(ar, "failed to init wow: %d\n", ret);
8089 goto err_free;
8092 wiphy_ext_feature_set(ar->hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
8095 * on LL hardware queues are managed entirely by the FW
8096 * so we only advertise to mac we can do the queues thing
8098 ar->hw->queues = IEEE80211_MAX_QUEUES;
8100 /* vdev_ids are used as hw queue numbers. Make sure offchan tx queue is
8101 * something that vdev_ids can't reach so that we don't stop the queue
8102 * accidentally.
8104 ar->hw->offchannel_tx_hw_queue = IEEE80211_MAX_QUEUES - 1;
8106 switch (ar->running_fw->fw_file.wmi_op_version) {
8107 case ATH10K_FW_WMI_OP_VERSION_MAIN:
8108 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
8109 ar->hw->wiphy->n_iface_combinations =
8110 ARRAY_SIZE(ath10k_if_comb);
8111 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
8112 break;
8113 case ATH10K_FW_WMI_OP_VERSION_TLV:
8114 if (test_bit(WMI_SERVICE_ADAPTIVE_OCS, ar->wmi.svc_map)) {
8115 ar->hw->wiphy->iface_combinations =
8116 ath10k_tlv_qcs_if_comb;
8117 ar->hw->wiphy->n_iface_combinations =
8118 ARRAY_SIZE(ath10k_tlv_qcs_if_comb);
8119 } else {
8120 ar->hw->wiphy->iface_combinations = ath10k_tlv_if_comb;
8121 ar->hw->wiphy->n_iface_combinations =
8122 ARRAY_SIZE(ath10k_tlv_if_comb);
8124 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
8125 break;
8126 case ATH10K_FW_WMI_OP_VERSION_10_1:
8127 case ATH10K_FW_WMI_OP_VERSION_10_2:
8128 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
8129 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
8130 ar->hw->wiphy->n_iface_combinations =
8131 ARRAY_SIZE(ath10k_10x_if_comb);
8132 break;
8133 case ATH10K_FW_WMI_OP_VERSION_10_4:
8134 ar->hw->wiphy->iface_combinations = ath10k_10_4_if_comb;
8135 ar->hw->wiphy->n_iface_combinations =
8136 ARRAY_SIZE(ath10k_10_4_if_comb);
8137 break;
8138 case ATH10K_FW_WMI_OP_VERSION_UNSET:
8139 case ATH10K_FW_WMI_OP_VERSION_MAX:
8140 WARN_ON(1);
8141 ret = -EINVAL;
8142 goto err_free;
8145 if (!test_bit(ATH10K_FLAG_RAW_MODE, &ar->dev_flags))
8146 ar->hw->netdev_features = NETIF_F_HW_CSUM;
8148 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED)) {
8149 /* Init ath dfs pattern detector */
8150 ar->ath_common.debug_mask = ATH_DBG_DFS;
8151 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
8152 NL80211_DFS_UNSET);
8154 if (!ar->dfs_detector)
8155 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
8158 /* Current wake_tx_queue implementation imposes a significant
8159 * performance penalty in some setups. The tx scheduling code needs
8160 * more work anyway so disable the wake_tx_queue unless firmware
8161 * supports the pull-push mechanism.
8163 if (!test_bit(ATH10K_FW_FEATURE_PEER_FLOW_CONTROL,
8164 ar->running_fw->fw_file.fw_features))
8165 ar->ops->wake_tx_queue = NULL;
8167 ret = ath10k_mac_init_rd(ar);
8168 if (ret) {
8169 ath10k_err(ar, "failed to derive regdom: %d\n", ret);
8170 goto err_dfs_detector_exit;
8173 /* Disable set_coverage_class for chipsets that do not support it. */
8174 if (!ar->hw_params.hw_ops->set_coverage_class)
8175 ar->ops->set_coverage_class = NULL;
8177 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
8178 ath10k_reg_notifier);
8179 if (ret) {
8180 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
8181 goto err_dfs_detector_exit;
8184 ar->hw->wiphy->cipher_suites = cipher_suites;
8185 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
8187 ret = ieee80211_register_hw(ar->hw);
8188 if (ret) {
8189 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
8190 goto err_dfs_detector_exit;
8193 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
8194 ret = regulatory_hint(ar->hw->wiphy,
8195 ar->ath_common.regulatory.alpha2);
8196 if (ret)
8197 goto err_unregister;
8200 return 0;
8202 err_unregister:
8203 ieee80211_unregister_hw(ar->hw);
8205 err_dfs_detector_exit:
8206 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
8207 ar->dfs_detector->exit(ar->dfs_detector);
8209 err_free:
8210 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8211 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8213 SET_IEEE80211_DEV(ar->hw, NULL);
8214 return ret;
8217 void ath10k_mac_unregister(struct ath10k *ar)
8219 ieee80211_unregister_hw(ar->hw);
8221 if (IS_ENABLED(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
8222 ar->dfs_detector->exit(ar->dfs_detector);
8224 kfree(ar->mac.sbands[NL80211_BAND_2GHZ].channels);
8225 kfree(ar->mac.sbands[NL80211_BAND_5GHZ].channels);
8227 SET_IEEE80211_DEV(ar->hw, NULL);