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.
20 #include <net/mac80211.h>
21 #include <linux/etherdevice.h>
37 static int ath10k_send_key(struct ath10k_vif *arvif,
38 struct ieee80211_key_conf *key,
40 const u8 *macaddr, u32 flags)
42 struct ath10k *ar = arvif->ar;
43 struct wmi_vdev_install_key_arg arg = {
44 .vdev_id = arvif->vdev_id,
45 .key_idx = key->keyidx,
46 .key_len = key->keylen,
52 lockdep_assert_held(&arvif->ar->conf_mutex);
54 switch (key->cipher) {
55 case WLAN_CIPHER_SUITE_CCMP:
56 arg.key_cipher = WMI_CIPHER_AES_CCM;
57 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
59 case WLAN_CIPHER_SUITE_TKIP:
60 arg.key_cipher = WMI_CIPHER_TKIP;
61 arg.key_txmic_len = 8;
62 arg.key_rxmic_len = 8;
64 case WLAN_CIPHER_SUITE_WEP40:
65 case WLAN_CIPHER_SUITE_WEP104:
66 arg.key_cipher = WMI_CIPHER_WEP;
68 case WLAN_CIPHER_SUITE_AES_CMAC:
72 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
76 if (cmd == DISABLE_KEY) {
77 arg.key_cipher = WMI_CIPHER_NONE;
81 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
84 static int ath10k_install_key(struct ath10k_vif *arvif,
85 struct ieee80211_key_conf *key,
87 const u8 *macaddr, u32 flags)
89 struct ath10k *ar = arvif->ar;
92 lockdep_assert_held(&ar->conf_mutex);
94 reinit_completion(&ar->install_key_done);
96 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
100 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
107 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
110 struct ath10k *ar = arvif->ar;
111 struct ath10k_peer *peer;
116 lockdep_assert_held(&ar->conf_mutex);
118 spin_lock_bh(&ar->data_lock);
119 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
120 spin_unlock_bh(&ar->data_lock);
125 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
126 if (arvif->wep_keys[i] == NULL)
130 flags |= WMI_KEY_PAIRWISE;
132 /* set TX_USAGE flag for default key id */
133 if (arvif->def_wep_key_idx == i)
134 flags |= WMI_KEY_TX_USAGE;
136 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
141 spin_lock_bh(&ar->data_lock);
142 peer->keys[i] = arvif->wep_keys[i];
143 spin_unlock_bh(&ar->data_lock);
149 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
152 struct ath10k *ar = arvif->ar;
153 struct ath10k_peer *peer;
159 lockdep_assert_held(&ar->conf_mutex);
161 spin_lock_bh(&ar->data_lock);
162 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
163 spin_unlock_bh(&ar->data_lock);
168 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
169 if (peer->keys[i] == NULL)
172 /* key flags are not required to delete the key */
173 ret = ath10k_install_key(arvif, peer->keys[i],
174 DISABLE_KEY, addr, flags);
175 if (ret && first_errno == 0)
179 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
182 spin_lock_bh(&ar->data_lock);
183 peer->keys[i] = NULL;
184 spin_unlock_bh(&ar->data_lock);
190 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
193 struct ath10k_peer *peer;
196 lockdep_assert_held(&ar->data_lock);
198 /* We don't know which vdev this peer belongs to,
199 * since WMI doesn't give us that information.
201 * FIXME: multi-bss needs to be handled.
203 peer = ath10k_peer_find(ar, 0, addr);
207 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
208 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
215 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
216 struct ieee80211_key_conf *key)
218 struct ath10k *ar = arvif->ar;
219 struct ath10k_peer *peer;
226 lockdep_assert_held(&ar->conf_mutex);
229 /* since ath10k_install_key we can't hold data_lock all the
230 * time, so we try to remove the keys incrementally */
231 spin_lock_bh(&ar->data_lock);
233 list_for_each_entry(peer, &ar->peers, list) {
234 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
235 if (peer->keys[i] == key) {
236 ether_addr_copy(addr, peer->addr);
237 peer->keys[i] = NULL;
242 if (i < ARRAY_SIZE(peer->keys))
245 spin_unlock_bh(&ar->data_lock);
247 if (i == ARRAY_SIZE(peer->keys))
249 /* key flags are not required to delete the key */
250 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
251 if (ret && first_errno == 0)
255 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
262 static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
264 struct ath10k *ar = arvif->ar;
265 enum nl80211_iftype iftype = arvif->vif->type;
266 struct ieee80211_key_conf *key;
272 lockdep_assert_held(&ar->conf_mutex);
274 if (iftype != NL80211_IFTYPE_STATION)
277 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
278 if (arvif->wep_keys[i]) {
279 key = arvif->wep_keys[i];
287 flags |= WMI_KEY_PAIRWISE;
288 flags |= WMI_KEY_TX_USAGE;
290 ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
292 ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
293 key->keyidx, arvif->vdev_id, ret);
300 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
301 struct ieee80211_key_conf *key)
303 struct ath10k *ar = arvif->ar;
304 struct ath10k_peer *peer;
307 lockdep_assert_held(&ar->conf_mutex);
309 list_for_each_entry(peer, &ar->peers, list) {
310 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
313 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
316 if (peer->keys[key->keyidx] == key)
319 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
320 arvif->vdev_id, key->keyidx);
322 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
324 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
325 arvif->vdev_id, peer->addr, ret);
333 /*********************/
334 /* General utilities */
335 /*********************/
337 static inline enum wmi_phy_mode
338 chan_to_phymode(const struct cfg80211_chan_def *chandef)
340 enum wmi_phy_mode phymode = MODE_UNKNOWN;
342 switch (chandef->chan->band) {
343 case IEEE80211_BAND_2GHZ:
344 switch (chandef->width) {
345 case NL80211_CHAN_WIDTH_20_NOHT:
346 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
351 case NL80211_CHAN_WIDTH_20:
352 phymode = MODE_11NG_HT20;
354 case NL80211_CHAN_WIDTH_40:
355 phymode = MODE_11NG_HT40;
357 case NL80211_CHAN_WIDTH_5:
358 case NL80211_CHAN_WIDTH_10:
359 case NL80211_CHAN_WIDTH_80:
360 case NL80211_CHAN_WIDTH_80P80:
361 case NL80211_CHAN_WIDTH_160:
362 phymode = MODE_UNKNOWN;
366 case IEEE80211_BAND_5GHZ:
367 switch (chandef->width) {
368 case NL80211_CHAN_WIDTH_20_NOHT:
371 case NL80211_CHAN_WIDTH_20:
372 phymode = MODE_11NA_HT20;
374 case NL80211_CHAN_WIDTH_40:
375 phymode = MODE_11NA_HT40;
377 case NL80211_CHAN_WIDTH_80:
378 phymode = MODE_11AC_VHT80;
380 case NL80211_CHAN_WIDTH_5:
381 case NL80211_CHAN_WIDTH_10:
382 case NL80211_CHAN_WIDTH_80P80:
383 case NL80211_CHAN_WIDTH_160:
384 phymode = MODE_UNKNOWN;
392 WARN_ON(phymode == MODE_UNKNOWN);
396 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
399 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
400 * 0 for no restriction
409 switch (mpdudensity) {
415 /* Our lower layer calculations limit our precision to
431 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr)
435 lockdep_assert_held(&ar->conf_mutex);
437 if (ar->num_peers >= ar->max_num_peers)
440 ret = ath10k_wmi_peer_create(ar, vdev_id, addr);
442 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
447 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
449 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
459 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
461 struct ath10k *ar = arvif->ar;
465 param = ar->wmi.pdev_param->sta_kickout_th;
466 ret = ath10k_wmi_pdev_set_param(ar, param,
467 ATH10K_KICKOUT_THRESHOLD);
469 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
470 arvif->vdev_id, ret);
474 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
475 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
476 ATH10K_KEEPALIVE_MIN_IDLE);
478 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
479 arvif->vdev_id, ret);
483 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
484 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
485 ATH10K_KEEPALIVE_MAX_IDLE);
487 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
488 arvif->vdev_id, ret);
492 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
493 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
494 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
496 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
497 arvif->vdev_id, ret);
504 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
506 struct ath10k *ar = arvif->ar;
509 vdev_param = ar->wmi.vdev_param->rts_threshold;
510 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
513 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
515 struct ath10k *ar = arvif->ar;
518 if (value != 0xFFFFFFFF)
519 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
520 ATH10K_FRAGMT_THRESHOLD_MIN,
521 ATH10K_FRAGMT_THRESHOLD_MAX);
523 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
524 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
527 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
531 lockdep_assert_held(&ar->conf_mutex);
533 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
537 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
546 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
548 struct ath10k_peer *peer, *tmp;
550 lockdep_assert_held(&ar->conf_mutex);
552 spin_lock_bh(&ar->data_lock);
553 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
554 if (peer->vdev_id != vdev_id)
557 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
558 peer->addr, vdev_id);
560 list_del(&peer->list);
564 spin_unlock_bh(&ar->data_lock);
567 static void ath10k_peer_cleanup_all(struct ath10k *ar)
569 struct ath10k_peer *peer, *tmp;
571 lockdep_assert_held(&ar->conf_mutex);
573 spin_lock_bh(&ar->data_lock);
574 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
575 list_del(&peer->list);
578 spin_unlock_bh(&ar->data_lock);
581 ar->num_stations = 0;
584 /************************/
585 /* Interface management */
586 /************************/
588 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
590 struct ath10k *ar = arvif->ar;
592 lockdep_assert_held(&ar->data_lock);
597 if (!arvif->beacon_buf)
598 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
599 arvif->beacon->len, DMA_TO_DEVICE);
601 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
602 arvif->beacon_state != ATH10K_BEACON_SENT))
605 dev_kfree_skb_any(arvif->beacon);
607 arvif->beacon = NULL;
608 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
611 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
613 struct ath10k *ar = arvif->ar;
615 lockdep_assert_held(&ar->data_lock);
617 ath10k_mac_vif_beacon_free(arvif);
619 if (arvif->beacon_buf) {
620 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
621 arvif->beacon_buf, arvif->beacon_paddr);
622 arvif->beacon_buf = NULL;
626 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
630 lockdep_assert_held(&ar->conf_mutex);
632 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
635 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
636 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
643 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
645 struct cfg80211_chan_def *chandef = &ar->chandef;
646 struct ieee80211_channel *channel = chandef->chan;
647 struct wmi_vdev_start_request_arg arg = {};
650 lockdep_assert_held(&ar->conf_mutex);
652 arg.vdev_id = vdev_id;
653 arg.channel.freq = channel->center_freq;
654 arg.channel.band_center_freq1 = chandef->center_freq1;
656 /* TODO setup this dynamically, what in case we
657 don't have any vifs? */
658 arg.channel.mode = chan_to_phymode(chandef);
659 arg.channel.chan_radar =
660 !!(channel->flags & IEEE80211_CHAN_RADAR);
662 arg.channel.min_power = 0;
663 arg.channel.max_power = channel->max_power * 2;
664 arg.channel.max_reg_power = channel->max_reg_power * 2;
665 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
667 reinit_completion(&ar->vdev_setup_done);
669 ret = ath10k_wmi_vdev_start(ar, &arg);
671 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
676 ret = ath10k_vdev_setup_sync(ar);
678 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
683 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
685 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
690 ar->monitor_vdev_id = vdev_id;
692 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
693 ar->monitor_vdev_id);
697 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
699 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
700 ar->monitor_vdev_id, ret);
705 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
709 lockdep_assert_held(&ar->conf_mutex);
711 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
713 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
714 ar->monitor_vdev_id, ret);
716 reinit_completion(&ar->vdev_setup_done);
718 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
720 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
721 ar->monitor_vdev_id, ret);
723 ret = ath10k_vdev_setup_sync(ar);
725 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
726 ar->monitor_vdev_id, ret);
728 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
729 ar->monitor_vdev_id);
733 static int ath10k_monitor_vdev_create(struct ath10k *ar)
737 lockdep_assert_held(&ar->conf_mutex);
739 if (ar->free_vdev_map == 0) {
740 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
744 bit = __ffs64(ar->free_vdev_map);
746 ar->monitor_vdev_id = bit;
748 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
749 WMI_VDEV_TYPE_MONITOR,
752 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
753 ar->monitor_vdev_id, ret);
757 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
758 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
759 ar->monitor_vdev_id);
764 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
768 lockdep_assert_held(&ar->conf_mutex);
770 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
772 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
773 ar->monitor_vdev_id, ret);
777 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
779 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
780 ar->monitor_vdev_id);
784 static int ath10k_monitor_start(struct ath10k *ar)
788 lockdep_assert_held(&ar->conf_mutex);
790 ret = ath10k_monitor_vdev_create(ar);
792 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
796 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
798 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
799 ath10k_monitor_vdev_delete(ar);
803 ar->monitor_started = true;
804 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
809 static int ath10k_monitor_stop(struct ath10k *ar)
813 lockdep_assert_held(&ar->conf_mutex);
815 ret = ath10k_monitor_vdev_stop(ar);
817 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
821 ret = ath10k_monitor_vdev_delete(ar);
823 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
827 ar->monitor_started = false;
828 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
833 static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
835 struct ath10k_vif *arvif;
837 if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
840 if (!ar->num_started_vdevs)
843 list_for_each_entry(arvif, &ar->arvifs, list)
844 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
847 ath10k_dbg(ar, ATH10K_DBG_MAC,
848 "mac disabling promiscuous mode because vdev is started\n");
852 static int ath10k_monitor_recalc(struct ath10k *ar)
856 lockdep_assert_held(&ar->conf_mutex);
858 should_start = ar->monitor ||
859 !ath10k_mac_should_disable_promisc(ar) ||
860 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
862 ath10k_dbg(ar, ATH10K_DBG_MAC,
863 "mac monitor recalc started? %d should? %d\n",
864 ar->monitor_started, should_start);
866 if (should_start == ar->monitor_started)
870 return ath10k_monitor_start(ar);
872 return ath10k_monitor_stop(ar);
875 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
877 struct ath10k *ar = arvif->ar;
878 u32 vdev_param, rts_cts = 0;
880 lockdep_assert_held(&ar->conf_mutex);
882 vdev_param = ar->wmi.vdev_param->enable_rtscts;
884 if (arvif->use_cts_prot || arvif->num_legacy_stations > 0)
885 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
887 if (arvif->num_legacy_stations > 0)
888 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
891 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
895 static int ath10k_start_cac(struct ath10k *ar)
899 lockdep_assert_held(&ar->conf_mutex);
901 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
903 ret = ath10k_monitor_recalc(ar);
905 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
906 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
910 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
911 ar->monitor_vdev_id);
916 static int ath10k_stop_cac(struct ath10k *ar)
918 lockdep_assert_held(&ar->conf_mutex);
920 /* CAC is not running - do nothing */
921 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
924 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
925 ath10k_monitor_stop(ar);
927 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
932 static void ath10k_recalc_radar_detection(struct ath10k *ar)
936 lockdep_assert_held(&ar->conf_mutex);
940 if (!ar->radar_enabled)
943 if (ar->num_started_vdevs > 0)
946 ret = ath10k_start_cac(ar);
949 * Not possible to start CAC on current channel so starting
950 * radiation is not allowed, make this channel DFS_UNAVAILABLE
951 * by indicating that radar was detected.
953 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
954 ieee80211_radar_detected(ar->hw);
958 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
960 struct ath10k *ar = arvif->ar;
963 lockdep_assert_held(&ar->conf_mutex);
965 reinit_completion(&ar->vdev_setup_done);
967 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
969 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
970 arvif->vdev_id, ret);
974 ret = ath10k_vdev_setup_sync(ar);
976 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
977 arvif->vdev_id, ret);
981 WARN_ON(ar->num_started_vdevs == 0);
983 if (ar->num_started_vdevs != 0) {
984 ar->num_started_vdevs--;
985 ath10k_recalc_radar_detection(ar);
991 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
993 struct ath10k *ar = arvif->ar;
994 struct cfg80211_chan_def *chandef = &ar->chandef;
995 struct wmi_vdev_start_request_arg arg = {};
998 lockdep_assert_held(&ar->conf_mutex);
1000 reinit_completion(&ar->vdev_setup_done);
1002 arg.vdev_id = arvif->vdev_id;
1003 arg.dtim_period = arvif->dtim_period;
1004 arg.bcn_intval = arvif->beacon_interval;
1006 arg.channel.freq = chandef->chan->center_freq;
1007 arg.channel.band_center_freq1 = chandef->center_freq1;
1008 arg.channel.mode = chan_to_phymode(chandef);
1010 arg.channel.min_power = 0;
1011 arg.channel.max_power = chandef->chan->max_power * 2;
1012 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1013 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1015 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1016 arg.ssid = arvif->u.ap.ssid;
1017 arg.ssid_len = arvif->u.ap.ssid_len;
1018 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1020 /* For now allow DFS for AP mode */
1021 arg.channel.chan_radar =
1022 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1023 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1024 arg.ssid = arvif->vif->bss_conf.ssid;
1025 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1028 ath10k_dbg(ar, ATH10K_DBG_MAC,
1029 "mac vdev %d start center_freq %d phymode %s\n",
1030 arg.vdev_id, arg.channel.freq,
1031 ath10k_wmi_phymode_str(arg.channel.mode));
1034 ret = ath10k_wmi_vdev_restart(ar, &arg);
1036 ret = ath10k_wmi_vdev_start(ar, &arg);
1039 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1044 ret = ath10k_vdev_setup_sync(ar);
1047 "failed to synchronize setup for vdev %i restart %d: %d\n",
1048 arg.vdev_id, restart, ret);
1052 ar->num_started_vdevs++;
1053 ath10k_recalc_radar_detection(ar);
1055 ret = ath10k_monitor_recalc(ar);
1057 ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
1058 arg.vdev_id, restart, ret);
1059 ret2 = ath10k_vdev_stop(arvif);
1061 ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
1062 arg.vdev_id, restart, ret2);
1068 static int ath10k_vdev_start(struct ath10k_vif *arvif)
1070 return ath10k_vdev_start_restart(arvif, false);
1073 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
1075 return ath10k_vdev_start_restart(arvif, true);
1078 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1079 struct sk_buff *bcn)
1081 struct ath10k *ar = arvif->ar;
1082 struct ieee80211_mgmt *mgmt;
1086 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1089 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1092 mgmt = (void *)bcn->data;
1093 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1094 mgmt->u.beacon.variable,
1095 bcn->len - (mgmt->u.beacon.variable -
1100 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1102 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1103 arvif->vdev_id, ret);
1110 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1111 u8 oui_type, size_t ie_offset)
1118 if (WARN_ON(skb->len < ie_offset))
1121 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1122 skb->data + ie_offset,
1123 skb->len - ie_offset);
1128 end = skb->data + skb->len;
1131 if (WARN_ON(next > end))
1134 memmove(ie, next, end - next);
1135 skb_trim(skb, skb->len - len);
1140 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1142 struct ath10k *ar = arvif->ar;
1143 struct ieee80211_hw *hw = ar->hw;
1144 struct ieee80211_vif *vif = arvif->vif;
1145 struct ieee80211_mutable_offsets offs = {};
1146 struct sk_buff *bcn;
1149 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1152 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1153 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1156 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1158 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1162 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1164 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1169 /* P2P IE is inserted by firmware automatically (as configured above)
1170 * so remove it from the base beacon template to avoid duplicate P2P
1171 * IEs in beacon frames.
1173 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1174 offsetof(struct ieee80211_mgmt,
1175 u.beacon.variable));
1177 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1182 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1190 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1192 struct ath10k *ar = arvif->ar;
1193 struct ieee80211_hw *hw = ar->hw;
1194 struct ieee80211_vif *vif = arvif->vif;
1195 struct sk_buff *prb;
1198 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1201 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1204 prb = ieee80211_proberesp_get(hw, vif);
1206 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1210 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1214 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1222 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1223 struct ieee80211_bss_conf *info)
1225 struct ath10k *ar = arvif->ar;
1228 lockdep_assert_held(&arvif->ar->conf_mutex);
1230 if (!info->enable_beacon) {
1231 ath10k_vdev_stop(arvif);
1233 arvif->is_started = false;
1234 arvif->is_up = false;
1236 spin_lock_bh(&arvif->ar->data_lock);
1237 ath10k_mac_vif_beacon_free(arvif);
1238 spin_unlock_bh(&arvif->ar->data_lock);
1243 arvif->tx_seq_no = 0x1000;
1245 ret = ath10k_vdev_start(arvif);
1250 ether_addr_copy(arvif->bssid, info->bssid);
1252 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1255 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1256 arvif->vdev_id, ret);
1257 ath10k_vdev_stop(arvif);
1261 arvif->is_started = true;
1262 arvif->is_up = true;
1264 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1267 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1268 struct ieee80211_bss_conf *info,
1269 const u8 self_peer[ETH_ALEN])
1271 struct ath10k *ar = arvif->ar;
1275 lockdep_assert_held(&arvif->ar->conf_mutex);
1277 if (!info->ibss_joined) {
1278 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1280 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1281 self_peer, arvif->vdev_id, ret);
1283 if (is_zero_ether_addr(arvif->bssid))
1286 memset(arvif->bssid, 0, ETH_ALEN);
1291 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer);
1293 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1294 self_peer, arvif->vdev_id, ret);
1298 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1299 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1300 ATH10K_DEFAULT_ATIM);
1302 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1303 arvif->vdev_id, ret);
1306 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1308 struct ath10k *ar = arvif->ar;
1313 lockdep_assert_held(&arvif->ar->conf_mutex);
1315 if (arvif->u.sta.uapsd)
1316 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1318 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1320 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1321 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1323 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1324 value, arvif->vdev_id, ret);
1331 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1333 struct ath10k *ar = arvif->ar;
1338 lockdep_assert_held(&arvif->ar->conf_mutex);
1340 if (arvif->u.sta.uapsd)
1341 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1343 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1345 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1346 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1349 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1350 value, arvif->vdev_id, ret);
1357 static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1359 struct ath10k_vif *arvif;
1362 lockdep_assert_held(&ar->conf_mutex);
1364 list_for_each_entry(arvif, &ar->arvifs, list)
1371 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1373 struct ath10k *ar = arvif->ar;
1374 struct ieee80211_vif *vif = arvif->vif;
1375 struct ieee80211_conf *conf = &ar->hw->conf;
1376 enum wmi_sta_powersave_param param;
1377 enum wmi_sta_ps_mode psmode;
1382 lockdep_assert_held(&arvif->ar->conf_mutex);
1384 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1387 enable_ps = arvif->ps;
1389 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1390 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1392 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1398 psmode = WMI_STA_PS_MODE_ENABLED;
1399 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1401 ps_timeout = conf->dynamic_ps_timeout;
1402 if (ps_timeout == 0) {
1403 /* Firmware doesn't like 0 */
1404 ps_timeout = ieee80211_tu_to_usec(
1405 vif->bss_conf.beacon_int) / 1000;
1408 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1411 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1412 arvif->vdev_id, ret);
1416 psmode = WMI_STA_PS_MODE_DISABLED;
1419 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1420 arvif->vdev_id, psmode ? "enable" : "disable");
1422 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1424 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1425 psmode, arvif->vdev_id, ret);
1432 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1434 struct ath10k *ar = arvif->ar;
1435 struct wmi_sta_keepalive_arg arg = {};
1438 lockdep_assert_held(&arvif->ar->conf_mutex);
1440 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1443 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1446 /* Some firmware revisions have a bug and ignore the `enabled` field.
1447 * Instead use the interval to disable the keepalive.
1449 arg.vdev_id = arvif->vdev_id;
1451 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1452 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1454 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1456 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1457 arvif->vdev_id, ret);
1464 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1466 struct ath10k *ar = arvif->ar;
1467 struct ieee80211_vif *vif = arvif->vif;
1470 lockdep_assert_held(&arvif->ar->conf_mutex);
1472 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1475 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1478 if (!vif->csa_active)
1484 if (!ieee80211_csa_is_complete(vif)) {
1485 ieee80211_csa_update_counter(vif);
1487 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1489 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1492 ret = ath10k_mac_setup_prb_tmpl(arvif);
1494 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1497 ieee80211_csa_finish(vif);
1501 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1503 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1505 struct ath10k *ar = arvif->ar;
1507 mutex_lock(&ar->conf_mutex);
1508 ath10k_mac_vif_ap_csa_count_down(arvif);
1509 mutex_unlock(&ar->conf_mutex);
1512 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1513 struct ieee80211_vif *vif)
1515 struct sk_buff *skb = data;
1516 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1517 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1519 if (vif->type != NL80211_IFTYPE_STATION)
1522 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1525 cancel_delayed_work(&arvif->connection_loss_work);
1528 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1530 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1531 IEEE80211_IFACE_ITER_NORMAL,
1532 ath10k_mac_handle_beacon_iter,
1536 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1537 struct ieee80211_vif *vif)
1539 u32 *vdev_id = data;
1540 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1541 struct ath10k *ar = arvif->ar;
1542 struct ieee80211_hw *hw = ar->hw;
1544 if (arvif->vdev_id != *vdev_id)
1550 ieee80211_beacon_loss(vif);
1552 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1553 * (done by mac80211) succeeds but beacons do not resume then it
1554 * doesn't make sense to continue operation. Queue connection loss work
1555 * which can be cancelled when beacon is received.
1557 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1558 ATH10K_CONNECTION_LOSS_HZ);
1561 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1563 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1564 IEEE80211_IFACE_ITER_NORMAL,
1565 ath10k_mac_handle_beacon_miss_iter,
1569 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1571 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1572 connection_loss_work.work);
1573 struct ieee80211_vif *vif = arvif->vif;
1578 ieee80211_connection_loss(vif);
1581 /**********************/
1582 /* Station management */
1583 /**********************/
1585 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1586 struct ieee80211_vif *vif)
1588 /* Some firmware revisions have unstable STA powersave when listen
1589 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1590 * generate NullFunc frames properly even if buffered frames have been
1591 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1592 * buffered frames. Often pinging the device from AP would simply fail.
1594 * As a workaround set it to 1.
1596 if (vif->type == NL80211_IFTYPE_STATION)
1599 return ar->hw->conf.listen_interval;
1602 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1603 struct ieee80211_vif *vif,
1604 struct ieee80211_sta *sta,
1605 struct wmi_peer_assoc_complete_arg *arg)
1607 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1609 lockdep_assert_held(&ar->conf_mutex);
1611 ether_addr_copy(arg->addr, sta->addr);
1612 arg->vdev_id = arvif->vdev_id;
1613 arg->peer_aid = sta->aid;
1614 arg->peer_flags |= WMI_PEER_AUTH;
1615 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1616 arg->peer_num_spatial_streams = 1;
1617 arg->peer_caps = vif->bss_conf.assoc_capability;
1620 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1621 struct ieee80211_vif *vif,
1622 struct wmi_peer_assoc_complete_arg *arg)
1624 struct ieee80211_bss_conf *info = &vif->bss_conf;
1625 struct cfg80211_bss *bss;
1626 const u8 *rsnie = NULL;
1627 const u8 *wpaie = NULL;
1629 lockdep_assert_held(&ar->conf_mutex);
1631 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1632 info->bssid, NULL, 0, 0, 0);
1634 const struct cfg80211_bss_ies *ies;
1637 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1639 ies = rcu_dereference(bss->ies);
1641 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1642 WLAN_OUI_TYPE_MICROSOFT_WPA,
1646 cfg80211_put_bss(ar->hw->wiphy, bss);
1649 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1650 if (rsnie || wpaie) {
1651 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1652 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1656 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1657 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1661 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1662 struct ieee80211_sta *sta,
1663 struct wmi_peer_assoc_complete_arg *arg)
1665 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1666 const struct ieee80211_supported_band *sband;
1667 const struct ieee80211_rate *rates;
1671 lockdep_assert_held(&ar->conf_mutex);
1673 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1674 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1675 rates = sband->bitrates;
1677 rateset->num_rates = 0;
1679 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1680 if (!(ratemask & 1))
1683 rateset->rates[rateset->num_rates] = rates->hw_value;
1684 rateset->num_rates++;
1688 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1689 struct ieee80211_sta *sta,
1690 struct wmi_peer_assoc_complete_arg *arg)
1692 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1696 lockdep_assert_held(&ar->conf_mutex);
1698 if (!ht_cap->ht_supported)
1701 arg->peer_flags |= WMI_PEER_HT;
1702 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1703 ht_cap->ampdu_factor)) - 1;
1705 arg->peer_mpdu_density =
1706 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1708 arg->peer_ht_caps = ht_cap->cap;
1709 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1711 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1712 arg->peer_flags |= WMI_PEER_LDPC;
1714 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1715 arg->peer_flags |= WMI_PEER_40MHZ;
1716 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1719 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1720 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1722 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1723 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1725 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1726 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1727 arg->peer_flags |= WMI_PEER_STBC;
1730 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1731 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1732 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1733 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1734 arg->peer_rate_caps |= stbc;
1735 arg->peer_flags |= WMI_PEER_STBC;
1738 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1739 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1740 else if (ht_cap->mcs.rx_mask[1])
1741 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1743 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1744 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1745 arg->peer_ht_rates.rates[n++] = i;
1748 * This is a workaround for HT-enabled STAs which break the spec
1749 * and have no HT capabilities RX mask (no HT RX MCS map).
1751 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1752 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1754 * Firmware asserts if such situation occurs.
1757 arg->peer_ht_rates.num_rates = 8;
1758 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1759 arg->peer_ht_rates.rates[i] = i;
1761 arg->peer_ht_rates.num_rates = n;
1762 arg->peer_num_spatial_streams = sta->rx_nss;
1765 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1767 arg->peer_ht_rates.num_rates,
1768 arg->peer_num_spatial_streams);
1771 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1772 struct ath10k_vif *arvif,
1773 struct ieee80211_sta *sta)
1779 lockdep_assert_held(&ar->conf_mutex);
1781 if (sta->wme && sta->uapsd_queues) {
1782 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1783 sta->uapsd_queues, sta->max_sp);
1785 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1786 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1787 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1788 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1789 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1790 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1791 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1792 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1793 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1794 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1795 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1796 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1798 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1799 max_sp = sta->max_sp;
1801 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1803 WMI_AP_PS_PEER_PARAM_UAPSD,
1806 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1807 arvif->vdev_id, ret);
1811 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1813 WMI_AP_PS_PEER_PARAM_MAX_SP,
1816 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1817 arvif->vdev_id, ret);
1821 /* TODO setup this based on STA listen interval and
1822 beacon interval. Currently we don't know
1823 sta->listen_interval - mac80211 patch required.
1824 Currently use 10 seconds */
1825 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1826 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1829 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1830 arvif->vdev_id, ret);
1838 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1839 struct ieee80211_sta *sta,
1840 struct wmi_peer_assoc_complete_arg *arg)
1842 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1845 if (!vht_cap->vht_supported)
1848 arg->peer_flags |= WMI_PEER_VHT;
1850 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1851 arg->peer_flags |= WMI_PEER_VHT_2G;
1853 arg->peer_vht_caps = vht_cap->cap;
1855 ampdu_factor = (vht_cap->cap &
1856 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1857 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1859 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1860 * zero in VHT IE. Using it would result in degraded throughput.
1861 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1862 * it if VHT max_mpdu is smaller. */
1863 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1864 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1865 ampdu_factor)) - 1);
1867 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1868 arg->peer_flags |= WMI_PEER_80MHZ;
1870 arg->peer_vht_rates.rx_max_rate =
1871 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1872 arg->peer_vht_rates.rx_mcs_set =
1873 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1874 arg->peer_vht_rates.tx_max_rate =
1875 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1876 arg->peer_vht_rates.tx_mcs_set =
1877 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1879 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1880 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1883 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1884 struct ieee80211_vif *vif,
1885 struct ieee80211_sta *sta,
1886 struct wmi_peer_assoc_complete_arg *arg)
1888 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1890 switch (arvif->vdev_type) {
1891 case WMI_VDEV_TYPE_AP:
1893 arg->peer_flags |= WMI_PEER_QOS;
1895 if (sta->wme && sta->uapsd_queues) {
1896 arg->peer_flags |= WMI_PEER_APSD;
1897 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1900 case WMI_VDEV_TYPE_STA:
1901 if (vif->bss_conf.qos)
1902 arg->peer_flags |= WMI_PEER_QOS;
1904 case WMI_VDEV_TYPE_IBSS:
1906 arg->peer_flags |= WMI_PEER_QOS;
1912 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1913 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
1916 static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1918 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1919 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1922 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1923 struct ieee80211_vif *vif,
1924 struct ieee80211_sta *sta,
1925 struct wmi_peer_assoc_complete_arg *arg)
1927 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1929 switch (ar->hw->conf.chandef.chan->band) {
1930 case IEEE80211_BAND_2GHZ:
1931 if (sta->vht_cap.vht_supported) {
1932 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1933 phymode = MODE_11AC_VHT40;
1935 phymode = MODE_11AC_VHT20;
1936 } else if (sta->ht_cap.ht_supported) {
1937 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1938 phymode = MODE_11NG_HT40;
1940 phymode = MODE_11NG_HT20;
1941 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
1948 case IEEE80211_BAND_5GHZ:
1952 if (sta->vht_cap.vht_supported) {
1953 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1954 phymode = MODE_11AC_VHT80;
1955 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1956 phymode = MODE_11AC_VHT40;
1957 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1958 phymode = MODE_11AC_VHT20;
1959 } else if (sta->ht_cap.ht_supported) {
1960 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1961 phymode = MODE_11NA_HT40;
1963 phymode = MODE_11NA_HT20;
1973 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1974 sta->addr, ath10k_wmi_phymode_str(phymode));
1976 arg->peer_phymode = phymode;
1977 WARN_ON(phymode == MODE_UNKNOWN);
1980 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1981 struct ieee80211_vif *vif,
1982 struct ieee80211_sta *sta,
1983 struct wmi_peer_assoc_complete_arg *arg)
1985 lockdep_assert_held(&ar->conf_mutex);
1987 memset(arg, 0, sizeof(*arg));
1989 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1990 ath10k_peer_assoc_h_crypto(ar, vif, arg);
1991 ath10k_peer_assoc_h_rates(ar, sta, arg);
1992 ath10k_peer_assoc_h_ht(ar, sta, arg);
1993 ath10k_peer_assoc_h_vht(ar, sta, arg);
1994 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
1995 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2000 static const u32 ath10k_smps_map[] = {
2001 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2002 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2003 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2004 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2007 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2009 const struct ieee80211_sta_ht_cap *ht_cap)
2013 if (!ht_cap->ht_supported)
2016 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2017 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2019 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2022 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2023 WMI_PEER_SMPS_STATE,
2024 ath10k_smps_map[smps]);
2027 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2028 struct ieee80211_vif *vif,
2029 struct ieee80211_sta_vht_cap vht_cap)
2031 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2036 if (!(ar->vht_cap_info &
2037 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2038 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2039 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2040 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2043 param = ar->wmi.vdev_param->txbf;
2046 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2049 /* The following logic is correct. If a remote STA advertises support
2050 * for being a beamformer then we should enable us being a beamformee.
2053 if (ar->vht_cap_info &
2054 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2055 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2056 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2057 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2059 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2060 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2063 if (ar->vht_cap_info &
2064 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2065 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2066 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2067 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2069 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2070 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2073 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2074 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2076 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2077 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2079 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2081 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2089 /* can be called only in mac80211 callbacks due to `key_count` usage */
2090 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2091 struct ieee80211_vif *vif,
2092 struct ieee80211_bss_conf *bss_conf)
2094 struct ath10k *ar = hw->priv;
2095 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2096 struct ieee80211_sta_ht_cap ht_cap;
2097 struct ieee80211_sta_vht_cap vht_cap;
2098 struct wmi_peer_assoc_complete_arg peer_arg;
2099 struct ieee80211_sta *ap_sta;
2102 lockdep_assert_held(&ar->conf_mutex);
2104 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2105 arvif->vdev_id, arvif->bssid, arvif->aid);
2109 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2111 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2112 bss_conf->bssid, arvif->vdev_id);
2117 /* ap_sta must be accessed only within rcu section which must be left
2118 * before calling ath10k_setup_peer_smps() which might sleep. */
2119 ht_cap = ap_sta->ht_cap;
2120 vht_cap = ap_sta->vht_cap;
2122 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2124 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2125 bss_conf->bssid, arvif->vdev_id, ret);
2132 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2134 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2135 bss_conf->bssid, arvif->vdev_id, ret);
2139 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2141 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2142 arvif->vdev_id, ret);
2146 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2148 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2149 arvif->vdev_id, bss_conf->bssid, ret);
2153 ath10k_dbg(ar, ATH10K_DBG_MAC,
2154 "mac vdev %d up (associated) bssid %pM aid %d\n",
2155 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2157 WARN_ON(arvif->is_up);
2159 arvif->aid = bss_conf->aid;
2160 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2162 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2164 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2165 arvif->vdev_id, ret);
2169 arvif->is_up = true;
2171 /* Workaround: Some firmware revisions (tested with qca6174
2172 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2173 * poked with peer param command.
2175 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2176 WMI_PEER_DUMMY_VAR, 1);
2178 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2179 arvif->bssid, arvif->vdev_id, ret);
2184 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2185 struct ieee80211_vif *vif)
2187 struct ath10k *ar = hw->priv;
2188 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2189 struct ieee80211_sta_vht_cap vht_cap = {};
2192 lockdep_assert_held(&ar->conf_mutex);
2194 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2195 arvif->vdev_id, arvif->bssid);
2197 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2199 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2200 arvif->vdev_id, ret);
2202 arvif->def_wep_key_idx = -1;
2204 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2206 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2207 arvif->vdev_id, ret);
2211 arvif->is_up = false;
2213 cancel_delayed_work_sync(&arvif->connection_loss_work);
2216 static int ath10k_station_assoc(struct ath10k *ar,
2217 struct ieee80211_vif *vif,
2218 struct ieee80211_sta *sta,
2221 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2222 struct wmi_peer_assoc_complete_arg peer_arg;
2225 lockdep_assert_held(&ar->conf_mutex);
2227 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2229 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2230 sta->addr, arvif->vdev_id, ret);
2234 peer_arg.peer_reassoc = reassoc;
2235 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2237 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2238 sta->addr, arvif->vdev_id, ret);
2242 /* Re-assoc is run only to update supported rates for given station. It
2243 * doesn't make much sense to reconfigure the peer completely.
2246 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2249 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2250 arvif->vdev_id, ret);
2254 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2256 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2257 sta->addr, arvif->vdev_id, ret);
2262 arvif->num_legacy_stations++;
2263 ret = ath10k_recalc_rtscts_prot(arvif);
2265 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2266 arvif->vdev_id, ret);
2271 /* Plumb cached keys only for static WEP */
2272 if (arvif->def_wep_key_idx != -1) {
2273 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2275 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2276 arvif->vdev_id, ret);
2285 static int ath10k_station_disassoc(struct ath10k *ar,
2286 struct ieee80211_vif *vif,
2287 struct ieee80211_sta *sta)
2289 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2292 lockdep_assert_held(&ar->conf_mutex);
2295 arvif->num_legacy_stations--;
2296 ret = ath10k_recalc_rtscts_prot(arvif);
2298 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2299 arvif->vdev_id, ret);
2304 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2306 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2307 arvif->vdev_id, ret);
2318 static int ath10k_update_channel_list(struct ath10k *ar)
2320 struct ieee80211_hw *hw = ar->hw;
2321 struct ieee80211_supported_band **bands;
2322 enum ieee80211_band band;
2323 struct ieee80211_channel *channel;
2324 struct wmi_scan_chan_list_arg arg = {0};
2325 struct wmi_channel_arg *ch;
2331 lockdep_assert_held(&ar->conf_mutex);
2333 bands = hw->wiphy->bands;
2334 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2338 for (i = 0; i < bands[band]->n_channels; i++) {
2339 if (bands[band]->channels[i].flags &
2340 IEEE80211_CHAN_DISABLED)
2347 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2348 arg.channels = kzalloc(len, GFP_KERNEL);
2353 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2357 for (i = 0; i < bands[band]->n_channels; i++) {
2358 channel = &bands[band]->channels[i];
2360 if (channel->flags & IEEE80211_CHAN_DISABLED)
2363 ch->allow_ht = true;
2365 /* FIXME: when should we really allow VHT? */
2366 ch->allow_vht = true;
2369 !(channel->flags & IEEE80211_CHAN_NO_IR);
2372 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2375 !!(channel->flags & IEEE80211_CHAN_RADAR);
2377 passive = channel->flags & IEEE80211_CHAN_NO_IR;
2378 ch->passive = passive;
2380 ch->freq = channel->center_freq;
2381 ch->band_center_freq1 = channel->center_freq;
2383 ch->max_power = channel->max_power * 2;
2384 ch->max_reg_power = channel->max_reg_power * 2;
2385 ch->max_antenna_gain = channel->max_antenna_gain * 2;
2386 ch->reg_class_id = 0; /* FIXME */
2388 /* FIXME: why use only legacy modes, why not any
2389 * HT/VHT modes? Would that even make any
2391 if (channel->band == IEEE80211_BAND_2GHZ)
2392 ch->mode = MODE_11G;
2394 ch->mode = MODE_11A;
2396 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2399 ath10k_dbg(ar, ATH10K_DBG_WMI,
2400 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2401 ch - arg.channels, arg.n_channels,
2402 ch->freq, ch->max_power, ch->max_reg_power,
2403 ch->max_antenna_gain, ch->mode);
2409 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2410 kfree(arg.channels);
2415 static enum wmi_dfs_region
2416 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2418 switch (dfs_region) {
2419 case NL80211_DFS_UNSET:
2420 return WMI_UNINIT_DFS_DOMAIN;
2421 case NL80211_DFS_FCC:
2422 return WMI_FCC_DFS_DOMAIN;
2423 case NL80211_DFS_ETSI:
2424 return WMI_ETSI_DFS_DOMAIN;
2425 case NL80211_DFS_JP:
2426 return WMI_MKK4_DFS_DOMAIN;
2428 return WMI_UNINIT_DFS_DOMAIN;
2431 static void ath10k_regd_update(struct ath10k *ar)
2433 struct reg_dmn_pair_mapping *regpair;
2435 enum wmi_dfs_region wmi_dfs_reg;
2436 enum nl80211_dfs_regions nl_dfs_reg;
2438 lockdep_assert_held(&ar->conf_mutex);
2440 ret = ath10k_update_channel_list(ar);
2442 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2444 regpair = ar->ath_common.regulatory.regpair;
2446 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2447 nl_dfs_reg = ar->dfs_detector->region;
2448 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2450 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2453 /* Target allows setting up per-band regdomain but ath_common provides
2454 * a combined one only */
2455 ret = ath10k_wmi_pdev_set_regdomain(ar,
2456 regpair->reg_domain,
2457 regpair->reg_domain, /* 2ghz */
2458 regpair->reg_domain, /* 5ghz */
2459 regpair->reg_2ghz_ctl,
2460 regpair->reg_5ghz_ctl,
2463 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2466 static void ath10k_reg_notifier(struct wiphy *wiphy,
2467 struct regulatory_request *request)
2469 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2470 struct ath10k *ar = hw->priv;
2473 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2475 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2476 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2477 request->dfs_region);
2478 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2479 request->dfs_region);
2481 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2482 request->dfs_region);
2485 mutex_lock(&ar->conf_mutex);
2486 if (ar->state == ATH10K_STATE_ON)
2487 ath10k_regd_update(ar);
2488 mutex_unlock(&ar->conf_mutex);
2495 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2497 if (ieee80211_is_mgmt(hdr->frame_control))
2498 return HTT_DATA_TX_EXT_TID_MGMT;
2500 if (!ieee80211_is_data_qos(hdr->frame_control))
2501 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2503 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2504 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2506 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2509 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
2512 return ath10k_vif_to_arvif(vif)->vdev_id;
2514 if (ar->monitor_started)
2515 return ar->monitor_vdev_id;
2517 ath10k_warn(ar, "failed to resolve vdev id\n");
2521 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2522 * Control in the header.
2524 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
2526 struct ieee80211_hdr *hdr = (void *)skb->data;
2527 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2530 if (!ieee80211_is_data_qos(hdr->frame_control))
2533 qos_ctl = ieee80211_get_qos_ctl(hdr);
2534 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2535 skb->data, (void *)qos_ctl - (void *)skb->data);
2536 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
2538 /* Some firmware revisions don't handle sending QoS NullFunc well.
2539 * These frames are mainly used for CQM purposes so it doesn't really
2540 * matter whether QoS NullFunc or NullFunc are sent.
2542 hdr = (void *)skb->data;
2543 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
2544 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2546 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2549 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2550 struct ieee80211_vif *vif,
2551 struct sk_buff *skb)
2553 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2554 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2556 /* This is case only for P2P_GO */
2557 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2558 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2561 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2562 spin_lock_bh(&ar->data_lock);
2563 if (arvif->u.ap.noa_data)
2564 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2566 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2567 arvif->u.ap.noa_data,
2568 arvif->u.ap.noa_len);
2569 spin_unlock_bh(&ar->data_lock);
2573 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2575 /* FIXME: Not really sure since when the behaviour changed. At some
2576 * point new firmware stopped requiring creation of peer entries for
2577 * offchannel tx (and actually creating them causes issues with wmi-htc
2578 * tx credit replenishment and reliability). Assuming it's at least 3.4
2579 * because that's when the `freq` was introduced to TX_FRM HTT command.
2581 return !(ar->htt.target_version_major >= 3 &&
2582 ar->htt.target_version_minor >= 4);
2585 static void ath10k_tx_htt(struct ath10k *ar, struct sk_buff *skb)
2587 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2590 if (ar->htt.target_version_major >= 3) {
2591 /* Since HTT 3.0 there is no separate mgmt tx command */
2592 ret = ath10k_htt_tx(&ar->htt, skb);
2596 if (ieee80211_is_mgmt(hdr->frame_control)) {
2597 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2599 if (skb_queue_len(&ar->wmi_mgmt_tx_queue) >=
2600 ATH10K_MAX_NUM_MGMT_PENDING) {
2601 ath10k_warn(ar, "reached WMI management transmit queue limit\n");
2606 skb_queue_tail(&ar->wmi_mgmt_tx_queue, skb);
2607 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2609 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2611 } else if (!test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2613 ieee80211_is_nullfunc(hdr->frame_control)) {
2614 /* FW does not report tx status properly for NullFunc frames
2615 * unless they are sent through mgmt tx path. mac80211 sends
2616 * those frames when it detects link/beacon loss and depends
2617 * on the tx status to be correct. */
2618 ret = ath10k_htt_mgmt_tx(&ar->htt, skb);
2620 ret = ath10k_htt_tx(&ar->htt, skb);
2625 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2627 ieee80211_free_txskb(ar->hw, skb);
2631 void ath10k_offchan_tx_purge(struct ath10k *ar)
2633 struct sk_buff *skb;
2636 skb = skb_dequeue(&ar->offchan_tx_queue);
2640 ieee80211_free_txskb(ar->hw, skb);
2644 void ath10k_offchan_tx_work(struct work_struct *work)
2646 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2647 struct ath10k_peer *peer;
2648 struct ieee80211_hdr *hdr;
2649 struct sk_buff *skb;
2650 const u8 *peer_addr;
2654 /* FW requirement: We must create a peer before FW will send out
2655 * an offchannel frame. Otherwise the frame will be stuck and
2656 * never transmitted. We delete the peer upon tx completion.
2657 * It is unlikely that a peer for offchannel tx will already be
2658 * present. However it may be in some rare cases so account for that.
2659 * Otherwise we might remove a legitimate peer and break stuff. */
2662 skb = skb_dequeue(&ar->offchan_tx_queue);
2666 mutex_lock(&ar->conf_mutex);
2668 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2671 hdr = (struct ieee80211_hdr *)skb->data;
2672 peer_addr = ieee80211_get_DA(hdr);
2673 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2675 spin_lock_bh(&ar->data_lock);
2676 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2677 spin_unlock_bh(&ar->data_lock);
2680 /* FIXME: should this use ath10k_warn()? */
2681 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2682 peer_addr, vdev_id);
2685 ret = ath10k_peer_create(ar, vdev_id, peer_addr);
2687 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2688 peer_addr, vdev_id, ret);
2691 spin_lock_bh(&ar->data_lock);
2692 reinit_completion(&ar->offchan_tx_completed);
2693 ar->offchan_tx_skb = skb;
2694 spin_unlock_bh(&ar->data_lock);
2696 ath10k_tx_htt(ar, skb);
2698 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2701 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2705 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2707 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2708 peer_addr, vdev_id, ret);
2711 mutex_unlock(&ar->conf_mutex);
2715 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2717 struct sk_buff *skb;
2720 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2724 ieee80211_free_txskb(ar->hw, skb);
2728 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2730 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2731 struct sk_buff *skb;
2735 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2739 ret = ath10k_wmi_mgmt_tx(ar, skb);
2741 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2743 ieee80211_free_txskb(ar->hw, skb);
2752 void __ath10k_scan_finish(struct ath10k *ar)
2754 lockdep_assert_held(&ar->data_lock);
2756 switch (ar->scan.state) {
2757 case ATH10K_SCAN_IDLE:
2759 case ATH10K_SCAN_RUNNING:
2760 if (ar->scan.is_roc)
2761 ieee80211_remain_on_channel_expired(ar->hw);
2763 case ATH10K_SCAN_ABORTING:
2764 if (!ar->scan.is_roc)
2765 ieee80211_scan_completed(ar->hw,
2767 ATH10K_SCAN_ABORTING));
2769 case ATH10K_SCAN_STARTING:
2770 ar->scan.state = ATH10K_SCAN_IDLE;
2771 ar->scan_channel = NULL;
2772 ath10k_offchan_tx_purge(ar);
2773 cancel_delayed_work(&ar->scan.timeout);
2774 complete_all(&ar->scan.completed);
2779 void ath10k_scan_finish(struct ath10k *ar)
2781 spin_lock_bh(&ar->data_lock);
2782 __ath10k_scan_finish(ar);
2783 spin_unlock_bh(&ar->data_lock);
2786 static int ath10k_scan_stop(struct ath10k *ar)
2788 struct wmi_stop_scan_arg arg = {
2789 .req_id = 1, /* FIXME */
2790 .req_type = WMI_SCAN_STOP_ONE,
2791 .u.scan_id = ATH10K_SCAN_ID,
2795 lockdep_assert_held(&ar->conf_mutex);
2797 ret = ath10k_wmi_stop_scan(ar, &arg);
2799 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2803 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2805 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2807 } else if (ret > 0) {
2812 /* Scan state should be updated upon scan completion but in case
2813 * firmware fails to deliver the event (for whatever reason) it is
2814 * desired to clean up scan state anyway. Firmware may have just
2815 * dropped the scan completion event delivery due to transport pipe
2816 * being overflown with data and/or it can recover on its own before
2817 * next scan request is submitted.
2819 spin_lock_bh(&ar->data_lock);
2820 if (ar->scan.state != ATH10K_SCAN_IDLE)
2821 __ath10k_scan_finish(ar);
2822 spin_unlock_bh(&ar->data_lock);
2827 static void ath10k_scan_abort(struct ath10k *ar)
2831 lockdep_assert_held(&ar->conf_mutex);
2833 spin_lock_bh(&ar->data_lock);
2835 switch (ar->scan.state) {
2836 case ATH10K_SCAN_IDLE:
2837 /* This can happen if timeout worker kicked in and called
2838 * abortion while scan completion was being processed.
2841 case ATH10K_SCAN_STARTING:
2842 case ATH10K_SCAN_ABORTING:
2843 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2844 ath10k_scan_state_str(ar->scan.state),
2847 case ATH10K_SCAN_RUNNING:
2848 ar->scan.state = ATH10K_SCAN_ABORTING;
2849 spin_unlock_bh(&ar->data_lock);
2851 ret = ath10k_scan_stop(ar);
2853 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2855 spin_lock_bh(&ar->data_lock);
2859 spin_unlock_bh(&ar->data_lock);
2862 void ath10k_scan_timeout_work(struct work_struct *work)
2864 struct ath10k *ar = container_of(work, struct ath10k,
2867 mutex_lock(&ar->conf_mutex);
2868 ath10k_scan_abort(ar);
2869 mutex_unlock(&ar->conf_mutex);
2872 static int ath10k_start_scan(struct ath10k *ar,
2873 const struct wmi_start_scan_arg *arg)
2877 lockdep_assert_held(&ar->conf_mutex);
2879 ret = ath10k_wmi_start_scan(ar, arg);
2883 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2885 ret = ath10k_scan_stop(ar);
2887 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2892 /* If we failed to start the scan, return error code at
2893 * this point. This is probably due to some issue in the
2894 * firmware, but no need to wedge the driver due to that...
2896 spin_lock_bh(&ar->data_lock);
2897 if (ar->scan.state == ATH10K_SCAN_IDLE) {
2898 spin_unlock_bh(&ar->data_lock);
2901 spin_unlock_bh(&ar->data_lock);
2903 /* Add a 200ms margin to account for event/command processing */
2904 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2905 msecs_to_jiffies(arg->max_scan_time+200));
2909 /**********************/
2910 /* mac80211 callbacks */
2911 /**********************/
2913 static void ath10k_tx(struct ieee80211_hw *hw,
2914 struct ieee80211_tx_control *control,
2915 struct sk_buff *skb)
2917 struct ath10k *ar = hw->priv;
2918 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2919 struct ieee80211_vif *vif = info->control.vif;
2920 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2922 /* We should disable CCK RATE due to P2P */
2923 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
2924 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
2926 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
2927 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
2928 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
2930 /* it makes no sense to process injected frames like that */
2931 if (vif && vif->type != NL80211_IFTYPE_MONITOR) {
2932 ath10k_tx_h_nwifi(hw, skb);
2933 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
2934 ath10k_tx_h_seq_no(vif, skb);
2937 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
2938 spin_lock_bh(&ar->data_lock);
2939 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
2940 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
2941 spin_unlock_bh(&ar->data_lock);
2943 if (ath10k_mac_need_offchan_tx_work(ar)) {
2944 ATH10K_SKB_CB(skb)->htt.freq = 0;
2945 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
2947 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
2950 skb_queue_tail(&ar->offchan_tx_queue, skb);
2951 ieee80211_queue_work(hw, &ar->offchan_tx_work);
2956 ath10k_tx_htt(ar, skb);
2959 /* Must not be called with conf_mutex held as workers can use that also. */
2960 void ath10k_drain_tx(struct ath10k *ar)
2962 /* make sure rcu-protected mac80211 tx path itself is drained */
2965 ath10k_offchan_tx_purge(ar);
2966 ath10k_mgmt_over_wmi_tx_purge(ar);
2968 cancel_work_sync(&ar->offchan_tx_work);
2969 cancel_work_sync(&ar->wmi_mgmt_tx_work);
2972 void ath10k_halt(struct ath10k *ar)
2974 struct ath10k_vif *arvif;
2976 lockdep_assert_held(&ar->conf_mutex);
2978 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
2979 ar->filter_flags = 0;
2980 ar->monitor = false;
2982 if (ar->monitor_started)
2983 ath10k_monitor_stop(ar);
2985 ar->monitor_started = false;
2987 ath10k_scan_finish(ar);
2988 ath10k_peer_cleanup_all(ar);
2989 ath10k_core_stop(ar);
2990 ath10k_hif_power_down(ar);
2992 spin_lock_bh(&ar->data_lock);
2993 list_for_each_entry(arvif, &ar->arvifs, list)
2994 ath10k_mac_vif_beacon_cleanup(arvif);
2995 spin_unlock_bh(&ar->data_lock);
2998 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3000 struct ath10k *ar = hw->priv;
3002 mutex_lock(&ar->conf_mutex);
3004 if (ar->cfg_tx_chainmask) {
3005 *tx_ant = ar->cfg_tx_chainmask;
3006 *rx_ant = ar->cfg_rx_chainmask;
3008 *tx_ant = ar->supp_tx_chainmask;
3009 *rx_ant = ar->supp_rx_chainmask;
3012 mutex_unlock(&ar->conf_mutex);
3017 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3019 /* It is not clear that allowing gaps in chainmask
3020 * is helpful. Probably it will not do what user
3021 * is hoping for, so warn in that case.
3023 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3026 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
3030 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3034 lockdep_assert_held(&ar->conf_mutex);
3036 ath10k_check_chain_mask(ar, tx_ant, "tx");
3037 ath10k_check_chain_mask(ar, rx_ant, "rx");
3039 ar->cfg_tx_chainmask = tx_ant;
3040 ar->cfg_rx_chainmask = rx_ant;
3042 if ((ar->state != ATH10K_STATE_ON) &&
3043 (ar->state != ATH10K_STATE_RESTARTED))
3046 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3049 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
3054 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3057 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
3065 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3067 struct ath10k *ar = hw->priv;
3070 mutex_lock(&ar->conf_mutex);
3071 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3072 mutex_unlock(&ar->conf_mutex);
3076 static int ath10k_start(struct ieee80211_hw *hw)
3078 struct ath10k *ar = hw->priv;
3082 * This makes sense only when restarting hw. It is harmless to call
3083 * uncoditionally. This is necessary to make sure no HTT/WMI tx
3084 * commands will be submitted while restarting.
3086 ath10k_drain_tx(ar);
3088 mutex_lock(&ar->conf_mutex);
3090 switch (ar->state) {
3091 case ATH10K_STATE_OFF:
3092 ar->state = ATH10K_STATE_ON;
3094 case ATH10K_STATE_RESTARTING:
3096 ar->state = ATH10K_STATE_RESTARTED;
3098 case ATH10K_STATE_ON:
3099 case ATH10K_STATE_RESTARTED:
3100 case ATH10K_STATE_WEDGED:
3104 case ATH10K_STATE_UTF:
3109 ret = ath10k_hif_power_up(ar);
3111 ath10k_err(ar, "Could not init hif: %d\n", ret);
3115 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
3117 ath10k_err(ar, "Could not init core: %d\n", ret);
3118 goto err_power_down;
3121 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
3123 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
3127 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
3129 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
3133 if (ar->cfg_tx_chainmask)
3134 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
3135 ar->cfg_rx_chainmask);
3138 * By default FW set ARP frames ac to voice (6). In that case ARP
3139 * exchange is not working properly for UAPSD enabled AP. ARP requests
3140 * which arrives with access category 0 are processed by network stack
3141 * and send back with access category 0, but FW changes access category
3142 * to 6. Set ARP frames access category to best effort (0) solves
3146 ret = ath10k_wmi_pdev_set_param(ar,
3147 ar->wmi.pdev_param->arp_ac_override, 0);
3149 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
3154 ar->num_started_vdevs = 0;
3155 ath10k_regd_update(ar);
3157 ath10k_spectral_start(ar);
3159 mutex_unlock(&ar->conf_mutex);
3163 ath10k_core_stop(ar);
3166 ath10k_hif_power_down(ar);
3169 ar->state = ATH10K_STATE_OFF;
3172 mutex_unlock(&ar->conf_mutex);
3176 static void ath10k_stop(struct ieee80211_hw *hw)
3178 struct ath10k *ar = hw->priv;
3180 ath10k_drain_tx(ar);
3182 mutex_lock(&ar->conf_mutex);
3183 if (ar->state != ATH10K_STATE_OFF) {
3185 ar->state = ATH10K_STATE_OFF;
3187 mutex_unlock(&ar->conf_mutex);
3189 cancel_delayed_work_sync(&ar->scan.timeout);
3190 cancel_work_sync(&ar->restart_work);
3193 static int ath10k_config_ps(struct ath10k *ar)
3195 struct ath10k_vif *arvif;
3198 lockdep_assert_held(&ar->conf_mutex);
3200 list_for_each_entry(arvif, &ar->arvifs, list) {
3201 ret = ath10k_mac_vif_setup_ps(arvif);
3203 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
3211 static const char *chandef_get_width(enum nl80211_chan_width width)
3214 case NL80211_CHAN_WIDTH_20_NOHT:
3216 case NL80211_CHAN_WIDTH_20:
3218 case NL80211_CHAN_WIDTH_40:
3220 case NL80211_CHAN_WIDTH_80:
3222 case NL80211_CHAN_WIDTH_80P80:
3224 case NL80211_CHAN_WIDTH_160:
3226 case NL80211_CHAN_WIDTH_5:
3228 case NL80211_CHAN_WIDTH_10:
3234 static void ath10k_config_chan(struct ath10k *ar)
3236 struct ath10k_vif *arvif;
3239 lockdep_assert_held(&ar->conf_mutex);
3241 ath10k_dbg(ar, ATH10K_DBG_MAC,
3242 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3243 ar->chandef.chan->center_freq,
3244 ar->chandef.center_freq1,
3245 ar->chandef.center_freq2,
3246 chandef_get_width(ar->chandef.width));
3248 /* First stop monitor interface. Some FW versions crash if there's a
3249 * lone monitor interface. */
3250 if (ar->monitor_started)
3251 ath10k_monitor_stop(ar);
3253 list_for_each_entry(arvif, &ar->arvifs, list) {
3254 if (!arvif->is_started)
3260 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3263 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3265 ath10k_warn(ar, "failed to down vdev %d: %d\n",
3266 arvif->vdev_id, ret);
3271 /* all vdevs are downed now - attempt to restart and re-up them */
3273 list_for_each_entry(arvif, &ar->arvifs, list) {
3274 if (!arvif->is_started)
3277 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3280 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3282 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3285 ret = ath10k_mac_setup_prb_tmpl(arvif);
3287 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3290 ret = ath10k_vdev_restart(arvif);
3292 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
3293 arvif->vdev_id, ret);
3300 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3303 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
3304 arvif->vdev_id, ret);
3309 ath10k_monitor_recalc(ar);
3312 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3317 lockdep_assert_held(&ar->conf_mutex);
3319 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3321 param = ar->wmi.pdev_param->txpower_limit2g;
3322 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3324 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3329 param = ar->wmi.pdev_param->txpower_limit5g;
3330 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3332 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3340 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3342 struct ath10k_vif *arvif;
3343 int ret, txpower = -1;
3345 lockdep_assert_held(&ar->conf_mutex);
3347 list_for_each_entry(arvif, &ar->arvifs, list) {
3348 WARN_ON(arvif->txpower < 0);
3351 txpower = arvif->txpower;
3353 txpower = min(txpower, arvif->txpower);
3356 if (WARN_ON(txpower == -1))
3359 ret = ath10k_mac_txpower_setup(ar, txpower);
3361 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3369 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3371 struct ath10k *ar = hw->priv;
3372 struct ieee80211_conf *conf = &hw->conf;
3375 mutex_lock(&ar->conf_mutex);
3377 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3378 ath10k_dbg(ar, ATH10K_DBG_MAC,
3379 "mac config channel %dMHz flags 0x%x radar %d\n",
3380 conf->chandef.chan->center_freq,
3381 conf->chandef.chan->flags,
3382 conf->radar_enabled);
3384 spin_lock_bh(&ar->data_lock);
3385 ar->rx_channel = conf->chandef.chan;
3386 spin_unlock_bh(&ar->data_lock);
3388 ar->radar_enabled = conf->radar_enabled;
3389 ath10k_recalc_radar_detection(ar);
3391 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3392 ar->chandef = conf->chandef;
3393 ath10k_config_chan(ar);
3397 if (changed & IEEE80211_CONF_CHANGE_PS)
3398 ath10k_config_ps(ar);
3400 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
3401 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3402 ret = ath10k_monitor_recalc(ar);
3404 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
3407 mutex_unlock(&ar->conf_mutex);
3411 static u32 get_nss_from_chainmask(u16 chain_mask)
3413 if ((chain_mask & 0x15) == 0x15)
3415 else if ((chain_mask & 0x7) == 0x7)
3417 else if ((chain_mask & 0x3) == 0x3)
3424 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3425 * because we will send mgmt frames without CCK. This requirement
3426 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3429 static int ath10k_add_interface(struct ieee80211_hw *hw,
3430 struct ieee80211_vif *vif)
3432 struct ath10k *ar = hw->priv;
3433 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3434 enum wmi_sta_powersave_param param;
3440 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3442 mutex_lock(&ar->conf_mutex);
3444 memset(arvif, 0, sizeof(*arvif));
3449 INIT_LIST_HEAD(&arvif->list);
3450 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
3451 INIT_DELAYED_WORK(&arvif->connection_loss_work,
3452 ath10k_mac_vif_sta_connection_loss_work);
3454 if (ar->free_vdev_map == 0) {
3455 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
3459 bit = __ffs64(ar->free_vdev_map);
3461 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3462 bit, ar->free_vdev_map);
3464 arvif->vdev_id = bit;
3465 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
3467 switch (vif->type) {
3468 case NL80211_IFTYPE_P2P_DEVICE:
3469 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3470 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3472 case NL80211_IFTYPE_UNSPECIFIED:
3473 case NL80211_IFTYPE_STATION:
3474 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3476 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3478 case NL80211_IFTYPE_ADHOC:
3479 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3481 case NL80211_IFTYPE_AP:
3482 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3485 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3487 case NL80211_IFTYPE_MONITOR:
3488 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3495 /* Some firmware revisions don't wait for beacon tx completion before
3496 * sending another SWBA event. This could lead to hardware using old
3497 * (freed) beacon data in some cases, e.g. tx credit starvation
3498 * combined with missed TBTT. This is very very rare.
3500 * On non-IOMMU-enabled hosts this could be a possible security issue
3501 * because hw could beacon some random data on the air. On
3502 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3503 * device would crash.
3505 * Since there are no beacon tx completions (implicit nor explicit)
3506 * propagated to host the only workaround for this is to allocate a
3507 * DMA-coherent buffer for a lifetime of a vif and use it for all
3508 * beacon tx commands. Worst case for this approach is some beacons may
3509 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3511 if (vif->type == NL80211_IFTYPE_ADHOC ||
3512 vif->type == NL80211_IFTYPE_AP) {
3513 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3514 IEEE80211_MAX_FRAME_LEN,
3515 &arvif->beacon_paddr,
3517 if (!arvif->beacon_buf) {
3519 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3525 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3526 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3527 arvif->beacon_buf ? "single-buf" : "per-skb");
3529 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3530 arvif->vdev_subtype, vif->addr);
3532 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
3533 arvif->vdev_id, ret);
3537 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
3538 list_add(&arvif->list, &ar->arvifs);
3540 /* It makes no sense to have firmware do keepalives. mac80211 already
3541 * takes care of this with idle connection polling.
3543 ret = ath10k_mac_vif_disable_keepalive(arvif);
3545 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
3546 arvif->vdev_id, ret);
3547 goto err_vdev_delete;
3550 arvif->def_wep_key_idx = -1;
3552 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3553 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3554 ATH10K_HW_TXRX_NATIVE_WIFI);
3555 /* 10.X firmware does not support this VDEV parameter. Do not warn */
3556 if (ret && ret != -EOPNOTSUPP) {
3557 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
3558 arvif->vdev_id, ret);
3559 goto err_vdev_delete;
3562 if (ar->cfg_tx_chainmask) {
3563 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3565 vdev_param = ar->wmi.vdev_param->nss;
3566 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3569 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3570 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3572 goto err_vdev_delete;
3576 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3577 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr);
3579 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3580 arvif->vdev_id, ret);
3581 goto err_vdev_delete;
3584 ret = ath10k_mac_set_kickout(arvif);
3586 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3587 arvif->vdev_id, ret);
3588 goto err_peer_delete;
3592 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3593 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3594 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3595 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3598 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3599 arvif->vdev_id, ret);
3600 goto err_peer_delete;
3603 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3605 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3606 arvif->vdev_id, ret);
3607 goto err_peer_delete;
3610 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3612 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3613 arvif->vdev_id, ret);
3614 goto err_peer_delete;
3618 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3620 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3621 arvif->vdev_id, ret);
3622 goto err_peer_delete;
3625 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3627 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3628 arvif->vdev_id, ret);
3629 goto err_peer_delete;
3632 arvif->txpower = vif->bss_conf.txpower;
3633 ret = ath10k_mac_txpower_recalc(ar);
3635 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3636 goto err_peer_delete;
3639 mutex_unlock(&ar->conf_mutex);
3643 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3644 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3647 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3648 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3649 list_del(&arvif->list);
3652 if (arvif->beacon_buf) {
3653 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3654 arvif->beacon_buf, arvif->beacon_paddr);
3655 arvif->beacon_buf = NULL;
3658 mutex_unlock(&ar->conf_mutex);
3663 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3664 struct ieee80211_vif *vif)
3666 struct ath10k *ar = hw->priv;
3667 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3670 cancel_work_sync(&arvif->ap_csa_work);
3671 cancel_delayed_work_sync(&arvif->connection_loss_work);
3673 mutex_lock(&ar->conf_mutex);
3675 spin_lock_bh(&ar->data_lock);
3676 ath10k_mac_vif_beacon_cleanup(arvif);
3677 spin_unlock_bh(&ar->data_lock);
3679 ret = ath10k_spectral_vif_stop(arvif);
3681 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3682 arvif->vdev_id, ret);
3684 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3685 list_del(&arvif->list);
3687 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3688 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3691 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
3692 arvif->vdev_id, ret);
3694 kfree(arvif->u.ap.noa_data);
3697 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3700 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3702 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3703 arvif->vdev_id, ret);
3705 /* Some firmware revisions don't notify host about self-peer removal
3706 * until after associated vdev is deleted.
3708 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3709 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3712 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3713 arvif->vdev_id, ret);
3715 spin_lock_bh(&ar->data_lock);
3717 spin_unlock_bh(&ar->data_lock);
3720 ath10k_peer_cleanup(ar, arvif->vdev_id);
3722 mutex_unlock(&ar->conf_mutex);
3726 * FIXME: Has to be verified.
3728 #define SUPPORTED_FILTERS \
3729 (FIF_PROMISC_IN_BSS | \
3734 FIF_BCN_PRBRESP_PROMISC | \
3738 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3739 unsigned int changed_flags,
3740 unsigned int *total_flags,
3743 struct ath10k *ar = hw->priv;
3746 mutex_lock(&ar->conf_mutex);
3748 changed_flags &= SUPPORTED_FILTERS;
3749 *total_flags &= SUPPORTED_FILTERS;
3750 ar->filter_flags = *total_flags;
3752 ret = ath10k_monitor_recalc(ar);
3754 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3756 mutex_unlock(&ar->conf_mutex);
3759 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3760 struct ieee80211_vif *vif,
3761 struct ieee80211_bss_conf *info,
3764 struct ath10k *ar = hw->priv;
3765 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3767 u32 vdev_param, pdev_param, slottime, preamble;
3769 mutex_lock(&ar->conf_mutex);
3771 if (changed & BSS_CHANGED_IBSS)
3772 ath10k_control_ibss(arvif, info, vif->addr);
3774 if (changed & BSS_CHANGED_BEACON_INT) {
3775 arvif->beacon_interval = info->beacon_int;
3776 vdev_param = ar->wmi.vdev_param->beacon_interval;
3777 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3778 arvif->beacon_interval);
3779 ath10k_dbg(ar, ATH10K_DBG_MAC,
3780 "mac vdev %d beacon_interval %d\n",
3781 arvif->vdev_id, arvif->beacon_interval);
3784 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3785 arvif->vdev_id, ret);
3788 if (changed & BSS_CHANGED_BEACON) {
3789 ath10k_dbg(ar, ATH10K_DBG_MAC,
3790 "vdev %d set beacon tx mode to staggered\n",
3793 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3794 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3795 WMI_BEACON_STAGGERED_MODE);
3797 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3798 arvif->vdev_id, ret);
3800 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3802 ath10k_warn(ar, "failed to update beacon template: %d\n",
3806 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3807 ret = ath10k_mac_setup_prb_tmpl(arvif);
3809 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3810 arvif->vdev_id, ret);
3813 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3814 arvif->dtim_period = info->dtim_period;
3816 ath10k_dbg(ar, ATH10K_DBG_MAC,
3817 "mac vdev %d dtim_period %d\n",
3818 arvif->vdev_id, arvif->dtim_period);
3820 vdev_param = ar->wmi.vdev_param->dtim_period;
3821 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3822 arvif->dtim_period);
3824 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
3825 arvif->vdev_id, ret);
3828 if (changed & BSS_CHANGED_SSID &&
3829 vif->type == NL80211_IFTYPE_AP) {
3830 arvif->u.ap.ssid_len = info->ssid_len;
3832 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3833 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3836 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3837 ether_addr_copy(arvif->bssid, info->bssid);
3839 if (changed & BSS_CHANGED_BEACON_ENABLED)
3840 ath10k_control_beaconing(arvif, info);
3842 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3843 arvif->use_cts_prot = info->use_cts_prot;
3844 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
3845 arvif->vdev_id, info->use_cts_prot);
3847 ret = ath10k_recalc_rtscts_prot(arvif);
3849 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3850 arvif->vdev_id, ret);
3852 vdev_param = ar->wmi.vdev_param->protection_mode;
3853 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3854 info->use_cts_prot ? 1 : 0);
3856 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
3857 info->use_cts_prot, arvif->vdev_id, ret);
3860 if (changed & BSS_CHANGED_ERP_SLOT) {
3861 if (info->use_short_slot)
3862 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3865 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3867 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
3868 arvif->vdev_id, slottime);
3870 vdev_param = ar->wmi.vdev_param->slot_time;
3871 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3874 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
3875 arvif->vdev_id, ret);
3878 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3879 if (info->use_short_preamble)
3880 preamble = WMI_VDEV_PREAMBLE_SHORT;
3882 preamble = WMI_VDEV_PREAMBLE_LONG;
3884 ath10k_dbg(ar, ATH10K_DBG_MAC,
3885 "mac vdev %d preamble %dn",
3886 arvif->vdev_id, preamble);
3888 vdev_param = ar->wmi.vdev_param->preamble;
3889 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3892 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
3893 arvif->vdev_id, ret);
3896 if (changed & BSS_CHANGED_ASSOC) {
3898 /* Workaround: Make sure monitor vdev is not running
3899 * when associating to prevent some firmware revisions
3900 * (e.g. 10.1 and 10.2) from crashing.
3902 if (ar->monitor_started)
3903 ath10k_monitor_stop(ar);
3904 ath10k_bss_assoc(hw, vif, info);
3905 ath10k_monitor_recalc(ar);
3907 ath10k_bss_disassoc(hw, vif);
3911 if (changed & BSS_CHANGED_TXPOWER) {
3912 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
3913 arvif->vdev_id, info->txpower);
3915 arvif->txpower = info->txpower;
3916 ret = ath10k_mac_txpower_recalc(ar);
3918 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3921 if (changed & BSS_CHANGED_PS) {
3922 arvif->ps = vif->bss_conf.ps;
3924 ret = ath10k_config_ps(ar);
3926 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
3927 arvif->vdev_id, ret);
3930 mutex_unlock(&ar->conf_mutex);
3933 static int ath10k_hw_scan(struct ieee80211_hw *hw,
3934 struct ieee80211_vif *vif,
3935 struct ieee80211_scan_request *hw_req)
3937 struct ath10k *ar = hw->priv;
3938 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3939 struct cfg80211_scan_request *req = &hw_req->req;
3940 struct wmi_start_scan_arg arg;
3944 mutex_lock(&ar->conf_mutex);
3946 spin_lock_bh(&ar->data_lock);
3947 switch (ar->scan.state) {
3948 case ATH10K_SCAN_IDLE:
3949 reinit_completion(&ar->scan.started);
3950 reinit_completion(&ar->scan.completed);
3951 ar->scan.state = ATH10K_SCAN_STARTING;
3952 ar->scan.is_roc = false;
3953 ar->scan.vdev_id = arvif->vdev_id;
3956 case ATH10K_SCAN_STARTING:
3957 case ATH10K_SCAN_RUNNING:
3958 case ATH10K_SCAN_ABORTING:
3962 spin_unlock_bh(&ar->data_lock);
3967 memset(&arg, 0, sizeof(arg));
3968 ath10k_wmi_start_scan_init(ar, &arg);
3969 arg.vdev_id = arvif->vdev_id;
3970 arg.scan_id = ATH10K_SCAN_ID;
3973 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
3976 arg.ie_len = req->ie_len;
3977 memcpy(arg.ie, req->ie, arg.ie_len);
3981 arg.n_ssids = req->n_ssids;
3982 for (i = 0; i < arg.n_ssids; i++) {
3983 arg.ssids[i].len = req->ssids[i].ssid_len;
3984 arg.ssids[i].ssid = req->ssids[i].ssid;
3987 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
3990 if (req->n_channels) {
3991 arg.n_channels = req->n_channels;
3992 for (i = 0; i < arg.n_channels; i++)
3993 arg.channels[i] = req->channels[i]->center_freq;
3996 ret = ath10k_start_scan(ar, &arg);
3998 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
3999 spin_lock_bh(&ar->data_lock);
4000 ar->scan.state = ATH10K_SCAN_IDLE;
4001 spin_unlock_bh(&ar->data_lock);
4005 mutex_unlock(&ar->conf_mutex);
4009 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4010 struct ieee80211_vif *vif)
4012 struct ath10k *ar = hw->priv;
4014 mutex_lock(&ar->conf_mutex);
4015 ath10k_scan_abort(ar);
4016 mutex_unlock(&ar->conf_mutex);
4018 cancel_delayed_work_sync(&ar->scan.timeout);
4021 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4022 struct ath10k_vif *arvif,
4023 enum set_key_cmd cmd,
4024 struct ieee80211_key_conf *key)
4026 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4029 /* 10.1 firmware branch requires default key index to be set to group
4030 * key index after installing it. Otherwise FW/HW Txes corrupted
4031 * frames with multi-vif APs. This is not required for main firmware
4032 * branch (e.g. 636).
4034 * FIXME: This has been tested only in AP. It remains unknown if this
4035 * is required for multi-vif STA interfaces on 10.1 */
4037 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4040 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4043 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4046 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4052 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4055 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
4056 arvif->vdev_id, ret);
4059 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4060 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4061 struct ieee80211_key_conf *key)
4063 struct ath10k *ar = hw->priv;
4064 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4065 struct ath10k_peer *peer;
4066 const u8 *peer_addr;
4067 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4068 key->cipher == WLAN_CIPHER_SUITE_WEP104;
4072 /* this one needs to be done in software */
4073 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4076 if (key->keyidx > WMI_MAX_KEY_INDEX)
4079 mutex_lock(&ar->conf_mutex);
4082 peer_addr = sta->addr;
4083 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4084 peer_addr = vif->bss_conf.bssid;
4086 peer_addr = vif->addr;
4088 key->hw_key_idx = key->keyidx;
4090 /* the peer should not disappear in mid-way (unless FW goes awry) since
4091 * we already hold conf_mutex. we just make sure its there now. */
4092 spin_lock_bh(&ar->data_lock);
4093 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4094 spin_unlock_bh(&ar->data_lock);
4097 if (cmd == SET_KEY) {
4098 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
4103 /* if the peer doesn't exist there is no key to disable
4109 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4110 flags |= WMI_KEY_PAIRWISE;
4112 flags |= WMI_KEY_GROUP;
4116 arvif->wep_keys[key->keyidx] = key;
4118 arvif->wep_keys[key->keyidx] = NULL;
4120 if (cmd == DISABLE_KEY)
4121 ath10k_clear_vdev_key(arvif, key);
4123 /* When WEP keys are uploaded it's possible that there are
4124 * stations associated already (e.g. when merging) without any
4125 * keys. Static WEP needs an explicit per-peer key upload.
4127 if (vif->type == NL80211_IFTYPE_ADHOC &&
4129 ath10k_mac_vif_update_wep_key(arvif, key);
4131 /* 802.1x never sets the def_wep_key_idx so each set_key()
4132 * call changes default tx key.
4134 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4135 * after first set_key().
4137 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4138 flags |= WMI_KEY_TX_USAGE;
4140 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4141 * requires pairwise keys for non-self peers, i.e. BSSID in STA
4142 * mode and associated stations in AP/IBSS.
4144 * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4145 * work fine when mapped directly from mac80211.
4147 * Note: When installing first static WEP groupwise key (which
4148 * should be pairwise) def_wep_key_idx isn't known yet (it's
4149 * equal to -1). Since .set_default_unicast_key is called only
4150 * for static WEP it's used to re-upload the key as pairwise.
4152 if (arvif->def_wep_key_idx >= 0 &&
4153 memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4154 flags &= ~WMI_KEY_GROUP;
4155 flags |= WMI_KEY_PAIRWISE;
4159 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
4161 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
4162 arvif->vdev_id, peer_addr, ret);
4166 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4168 spin_lock_bh(&ar->data_lock);
4169 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4170 if (peer && cmd == SET_KEY)
4171 peer->keys[key->keyidx] = key;
4172 else if (peer && cmd == DISABLE_KEY)
4173 peer->keys[key->keyidx] = NULL;
4174 else if (peer == NULL)
4175 /* impossible unless FW goes crazy */
4176 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
4177 spin_unlock_bh(&ar->data_lock);
4180 mutex_unlock(&ar->conf_mutex);
4184 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4185 struct ieee80211_vif *vif,
4188 struct ath10k *ar = hw->priv;
4189 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4192 mutex_lock(&arvif->ar->conf_mutex);
4194 if (arvif->ar->state != ATH10K_STATE_ON)
4197 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4198 arvif->vdev_id, keyidx);
4200 ret = ath10k_wmi_vdev_set_param(arvif->ar,
4202 arvif->ar->wmi.vdev_param->def_keyid,
4206 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4212 arvif->def_wep_key_idx = keyidx;
4214 ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4216 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4217 arvif->vdev_id, ret);
4222 mutex_unlock(&arvif->ar->conf_mutex);
4225 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4228 struct ath10k_vif *arvif;
4229 struct ath10k_sta *arsta;
4230 struct ieee80211_sta *sta;
4231 u32 changed, bw, nss, smps;
4234 arsta = container_of(wk, struct ath10k_sta, update_wk);
4235 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4236 arvif = arsta->arvif;
4239 spin_lock_bh(&ar->data_lock);
4241 changed = arsta->changed;
4248 spin_unlock_bh(&ar->data_lock);
4250 mutex_lock(&ar->conf_mutex);
4252 if (changed & IEEE80211_RC_BW_CHANGED) {
4253 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
4256 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4257 WMI_PEER_CHAN_WIDTH, bw);
4259 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
4260 sta->addr, bw, err);
4263 if (changed & IEEE80211_RC_NSS_CHANGED) {
4264 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
4267 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4270 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
4271 sta->addr, nss, err);
4274 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4275 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
4278 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4279 WMI_PEER_SMPS_STATE, smps);
4281 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
4282 sta->addr, smps, err);
4285 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4286 changed & IEEE80211_RC_NSS_CHANGED) {
4287 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
4290 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
4292 ath10k_warn(ar, "failed to reassociate station: %pM\n",
4296 mutex_unlock(&ar->conf_mutex);
4299 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif)
4301 struct ath10k *ar = arvif->ar;
4303 lockdep_assert_held(&ar->conf_mutex);
4305 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4306 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4309 if (ar->num_stations >= ar->max_num_stations)
4317 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif)
4319 struct ath10k *ar = arvif->ar;
4321 lockdep_assert_held(&ar->conf_mutex);
4323 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
4324 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
4330 static int ath10k_sta_state(struct ieee80211_hw *hw,
4331 struct ieee80211_vif *vif,
4332 struct ieee80211_sta *sta,
4333 enum ieee80211_sta_state old_state,
4334 enum ieee80211_sta_state new_state)
4336 struct ath10k *ar = hw->priv;
4337 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4338 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4341 if (old_state == IEEE80211_STA_NOTEXIST &&
4342 new_state == IEEE80211_STA_NONE) {
4343 memset(arsta, 0, sizeof(*arsta));
4344 arsta->arvif = arvif;
4345 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4348 /* cancel must be done outside the mutex to avoid deadlock */
4349 if ((old_state == IEEE80211_STA_NONE &&
4350 new_state == IEEE80211_STA_NOTEXIST))
4351 cancel_work_sync(&arsta->update_wk);
4353 mutex_lock(&ar->conf_mutex);
4355 if (old_state == IEEE80211_STA_NOTEXIST &&
4356 new_state == IEEE80211_STA_NONE) {
4358 * New station addition.
4360 ath10k_dbg(ar, ATH10K_DBG_MAC,
4361 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4362 arvif->vdev_id, sta->addr,
4363 ar->num_stations + 1, ar->max_num_stations,
4364 ar->num_peers + 1, ar->max_num_peers);
4366 ret = ath10k_mac_inc_num_stations(arvif);
4368 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4369 ar->max_num_stations);
4373 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr);
4375 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
4376 sta->addr, arvif->vdev_id, ret);
4377 ath10k_mac_dec_num_stations(arvif);
4381 if (vif->type == NL80211_IFTYPE_STATION) {
4382 WARN_ON(arvif->is_started);
4384 ret = ath10k_vdev_start(arvif);
4386 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4387 arvif->vdev_id, ret);
4388 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4390 ath10k_mac_dec_num_stations(arvif);
4394 arvif->is_started = true;
4396 } else if ((old_state == IEEE80211_STA_NONE &&
4397 new_state == IEEE80211_STA_NOTEXIST)) {
4399 * Existing station deletion.
4401 ath10k_dbg(ar, ATH10K_DBG_MAC,
4402 "mac vdev %d peer delete %pM (sta gone)\n",
4403 arvif->vdev_id, sta->addr);
4405 if (vif->type == NL80211_IFTYPE_STATION) {
4406 WARN_ON(!arvif->is_started);
4408 ret = ath10k_vdev_stop(arvif);
4410 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4411 arvif->vdev_id, ret);
4413 arvif->is_started = false;
4416 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4418 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
4419 sta->addr, arvif->vdev_id, ret);
4421 ath10k_mac_dec_num_stations(arvif);
4422 } else if (old_state == IEEE80211_STA_AUTH &&
4423 new_state == IEEE80211_STA_ASSOC &&
4424 (vif->type == NL80211_IFTYPE_AP ||
4425 vif->type == NL80211_IFTYPE_ADHOC)) {
4429 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
4432 ret = ath10k_station_assoc(ar, vif, sta, false);
4434 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
4435 sta->addr, arvif->vdev_id, ret);
4436 } else if (old_state == IEEE80211_STA_ASSOC &&
4437 new_state == IEEE80211_STA_AUTH &&
4438 (vif->type == NL80211_IFTYPE_AP ||
4439 vif->type == NL80211_IFTYPE_ADHOC)) {
4443 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
4446 ret = ath10k_station_disassoc(ar, vif, sta);
4448 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
4449 sta->addr, arvif->vdev_id, ret);
4452 mutex_unlock(&ar->conf_mutex);
4456 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
4457 u16 ac, bool enable)
4459 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4460 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4461 u32 prio = 0, acc = 0;
4465 lockdep_assert_held(&ar->conf_mutex);
4467 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4471 case IEEE80211_AC_VO:
4472 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4473 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4477 case IEEE80211_AC_VI:
4478 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4479 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4483 case IEEE80211_AC_BE:
4484 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4485 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4489 case IEEE80211_AC_BK:
4490 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4491 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4498 arvif->u.sta.uapsd |= value;
4500 arvif->u.sta.uapsd &= ~value;
4502 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4503 WMI_STA_PS_PARAM_UAPSD,
4504 arvif->u.sta.uapsd);
4506 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
4510 if (arvif->u.sta.uapsd)
4511 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4513 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4515 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4516 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4519 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
4521 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4523 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4524 arvif->vdev_id, ret);
4528 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4530 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4531 arvif->vdev_id, ret);
4535 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4536 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4537 /* Only userspace can make an educated decision when to send
4538 * trigger frame. The following effectively disables u-UAPSD
4539 * autotrigger in firmware (which is enabled by default
4540 * provided the autotrigger service is available).
4544 arg.user_priority = prio;
4545 arg.service_interval = 0;
4546 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4547 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4549 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4550 arvif->bssid, &arg, 1);
4552 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4562 static int ath10k_conf_tx(struct ieee80211_hw *hw,
4563 struct ieee80211_vif *vif, u16 ac,
4564 const struct ieee80211_tx_queue_params *params)
4566 struct ath10k *ar = hw->priv;
4567 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4568 struct wmi_wmm_params_arg *p = NULL;
4571 mutex_lock(&ar->conf_mutex);
4574 case IEEE80211_AC_VO:
4575 p = &arvif->wmm_params.ac_vo;
4577 case IEEE80211_AC_VI:
4578 p = &arvif->wmm_params.ac_vi;
4580 case IEEE80211_AC_BE:
4581 p = &arvif->wmm_params.ac_be;
4583 case IEEE80211_AC_BK:
4584 p = &arvif->wmm_params.ac_bk;
4593 p->cwmin = params->cw_min;
4594 p->cwmax = params->cw_max;
4595 p->aifs = params->aifs;
4598 * The channel time duration programmed in the HW is in absolute
4599 * microseconds, while mac80211 gives the txop in units of
4602 p->txop = params->txop * 32;
4604 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4605 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4606 &arvif->wmm_params);
4608 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4609 arvif->vdev_id, ret);
4613 /* This won't work well with multi-interface cases but it's
4614 * better than nothing.
4616 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4618 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4623 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4625 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
4628 mutex_unlock(&ar->conf_mutex);
4632 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4634 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4635 struct ieee80211_vif *vif,
4636 struct ieee80211_channel *chan,
4638 enum ieee80211_roc_type type)
4640 struct ath10k *ar = hw->priv;
4641 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4642 struct wmi_start_scan_arg arg;
4645 mutex_lock(&ar->conf_mutex);
4647 spin_lock_bh(&ar->data_lock);
4648 switch (ar->scan.state) {
4649 case ATH10K_SCAN_IDLE:
4650 reinit_completion(&ar->scan.started);
4651 reinit_completion(&ar->scan.completed);
4652 reinit_completion(&ar->scan.on_channel);
4653 ar->scan.state = ATH10K_SCAN_STARTING;
4654 ar->scan.is_roc = true;
4655 ar->scan.vdev_id = arvif->vdev_id;
4656 ar->scan.roc_freq = chan->center_freq;
4659 case ATH10K_SCAN_STARTING:
4660 case ATH10K_SCAN_RUNNING:
4661 case ATH10K_SCAN_ABORTING:
4665 spin_unlock_bh(&ar->data_lock);
4670 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4672 memset(&arg, 0, sizeof(arg));
4673 ath10k_wmi_start_scan_init(ar, &arg);
4674 arg.vdev_id = arvif->vdev_id;
4675 arg.scan_id = ATH10K_SCAN_ID;
4677 arg.channels[0] = chan->center_freq;
4678 arg.dwell_time_active = duration;
4679 arg.dwell_time_passive = duration;
4680 arg.max_scan_time = 2 * duration;
4681 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4682 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4684 ret = ath10k_start_scan(ar, &arg);
4686 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
4687 spin_lock_bh(&ar->data_lock);
4688 ar->scan.state = ATH10K_SCAN_IDLE;
4689 spin_unlock_bh(&ar->data_lock);
4693 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4695 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
4697 ret = ath10k_scan_stop(ar);
4699 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4707 mutex_unlock(&ar->conf_mutex);
4711 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4713 struct ath10k *ar = hw->priv;
4715 mutex_lock(&ar->conf_mutex);
4716 ath10k_scan_abort(ar);
4717 mutex_unlock(&ar->conf_mutex);
4719 cancel_delayed_work_sync(&ar->scan.timeout);
4725 * Both RTS and Fragmentation threshold are interface-specific
4726 * in ath10k, but device-specific in mac80211.
4729 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4731 struct ath10k *ar = hw->priv;
4732 struct ath10k_vif *arvif;
4735 mutex_lock(&ar->conf_mutex);
4736 list_for_each_entry(arvif, &ar->arvifs, list) {
4737 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
4738 arvif->vdev_id, value);
4740 ret = ath10k_mac_set_rts(arvif, value);
4742 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
4743 arvif->vdev_id, ret);
4747 mutex_unlock(&ar->conf_mutex);
4752 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4753 u32 queues, bool drop)
4755 struct ath10k *ar = hw->priv;
4759 /* mac80211 doesn't care if we really xmit queued frames or not
4760 * we'll collect those frames either way if we stop/delete vdevs */
4764 mutex_lock(&ar->conf_mutex);
4766 if (ar->state == ATH10K_STATE_WEDGED)
4769 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
4772 spin_lock_bh(&ar->htt.tx_lock);
4773 empty = (ar->htt.num_pending_tx == 0);
4774 spin_unlock_bh(&ar->htt.tx_lock);
4776 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4777 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4781 }), ATH10K_FLUSH_TIMEOUT_HZ);
4783 if (ret <= 0 || skip)
4784 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
4785 skip, ar->state, ret);
4788 mutex_unlock(&ar->conf_mutex);
4791 /* TODO: Implement this function properly
4792 * For now it is needed to reply to Probe Requests in IBSS mode.
4793 * Propably we need this information from FW.
4795 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4801 static int ath10k_suspend(struct ieee80211_hw *hw,
4802 struct cfg80211_wowlan *wowlan)
4804 struct ath10k *ar = hw->priv;
4807 mutex_lock(&ar->conf_mutex);
4809 ret = ath10k_wait_for_suspend(ar, WMI_PDEV_SUSPEND);
4811 if (ret == -ETIMEDOUT)
4817 ret = ath10k_hif_suspend(ar);
4819 ath10k_warn(ar, "failed to suspend hif: %d\n", ret);
4826 ret = ath10k_wmi_pdev_resume_target(ar);
4828 ath10k_warn(ar, "failed to resume target: %d\n", ret);
4832 mutex_unlock(&ar->conf_mutex);
4836 static int ath10k_resume(struct ieee80211_hw *hw)
4838 struct ath10k *ar = hw->priv;
4841 mutex_lock(&ar->conf_mutex);
4843 ret = ath10k_hif_resume(ar);
4845 ath10k_warn(ar, "failed to resume hif: %d\n", ret);
4850 ret = ath10k_wmi_pdev_resume_target(ar);
4852 ath10k_warn(ar, "failed to resume target: %d\n", ret);
4859 mutex_unlock(&ar->conf_mutex);
4864 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4865 enum ieee80211_reconfig_type reconfig_type)
4867 struct ath10k *ar = hw->priv;
4869 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4872 mutex_lock(&ar->conf_mutex);
4874 /* If device failed to restart it will be in a different state, e.g.
4875 * ATH10K_STATE_WEDGED */
4876 if (ar->state == ATH10K_STATE_RESTARTED) {
4877 ath10k_info(ar, "device successfully recovered\n");
4878 ar->state = ATH10K_STATE_ON;
4879 ieee80211_wake_queues(ar->hw);
4882 mutex_unlock(&ar->conf_mutex);
4885 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4886 struct survey_info *survey)
4888 struct ath10k *ar = hw->priv;
4889 struct ieee80211_supported_band *sband;
4890 struct survey_info *ar_survey = &ar->survey[idx];
4893 mutex_lock(&ar->conf_mutex);
4895 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4896 if (sband && idx >= sband->n_channels) {
4897 idx -= sband->n_channels;
4902 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4904 if (!sband || idx >= sband->n_channels) {
4909 spin_lock_bh(&ar->data_lock);
4910 memcpy(survey, ar_survey, sizeof(*survey));
4911 spin_unlock_bh(&ar->data_lock);
4913 survey->channel = &sband->channels[idx];
4915 if (ar->rx_channel == survey->channel)
4916 survey->filled |= SURVEY_INFO_IN_USE;
4919 mutex_unlock(&ar->conf_mutex);
4923 /* Helper table for legacy fixed_rate/bitrate_mask */
4924 static const u8 cck_ofdm_rate[] = {
4941 /* Check if only one bit set */
4942 static int ath10k_check_single_mask(u32 mask)
4950 mask &= ~BIT(bit - 1);
4958 ath10k_default_bitrate_mask(struct ath10k *ar,
4959 enum ieee80211_band band,
4960 const struct cfg80211_bitrate_mask *mask)
4962 u32 legacy = 0x00ff;
4965 u16 nrf = ar->num_rf_chains;
4967 if (ar->cfg_tx_chainmask)
4968 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
4971 case IEEE80211_BAND_2GHZ:
4975 case IEEE80211_BAND_5GHZ:
4981 if (mask->control[band].legacy != legacy)
4984 for (i = 0; i < nrf; i++)
4985 if (mask->control[band].ht_mcs[i] != ht)
4988 for (i = 0; i < nrf; i++)
4989 if (mask->control[band].vht_mcs[i] != vht)
4996 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
4997 enum ieee80211_band band,
5000 int ht_nss = 0, vht_nss = 0, i;
5003 if (ath10k_check_single_mask(mask->control[band].legacy))
5007 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5008 if (mask->control[band].ht_mcs[i] == 0xff)
5010 else if (mask->control[band].ht_mcs[i] == 0x00)
5019 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5020 if (mask->control[band].vht_mcs[i] == 0x03ff)
5022 else if (mask->control[band].vht_mcs[i] == 0x0000)
5030 if (ht_nss > 0 && vht_nss > 0)
5034 *fixed_nss = ht_nss;
5036 *fixed_nss = vht_nss;
5044 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5045 enum ieee80211_band band,
5046 enum wmi_rate_preamble *preamble)
5048 int legacy = 0, ht = 0, vht = 0, i;
5050 *preamble = WMI_RATE_PREAMBLE_OFDM;
5053 legacy = ath10k_check_single_mask(mask->control[band].legacy);
5058 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5059 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5064 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5065 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5069 /* Currently we support only one fixed_rate */
5070 if ((legacy + ht + vht) != 1)
5074 *preamble = WMI_RATE_PREAMBLE_HT;
5076 *preamble = WMI_RATE_PREAMBLE_VHT;
5082 ath10k_bitrate_mask_rate(struct ath10k *ar,
5083 const struct cfg80211_bitrate_mask *mask,
5084 enum ieee80211_band band,
5088 u8 rate = 0, pream = 0, nss = 0, i;
5089 enum wmi_rate_preamble preamble;
5091 /* Check if single rate correct */
5092 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5098 case WMI_RATE_PREAMBLE_CCK:
5099 case WMI_RATE_PREAMBLE_OFDM:
5100 i = ffs(mask->control[band].legacy) - 1;
5102 if (band == IEEE80211_BAND_2GHZ && i < 4)
5103 pream = WMI_RATE_PREAMBLE_CCK;
5105 if (band == IEEE80211_BAND_5GHZ)
5108 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5111 rate = cck_ofdm_rate[i];
5113 case WMI_RATE_PREAMBLE_HT:
5114 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5115 if (mask->control[band].ht_mcs[i])
5118 if (i == IEEE80211_HT_MCS_MASK_LEN)
5121 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5124 case WMI_RATE_PREAMBLE_VHT:
5125 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5126 if (mask->control[band].vht_mcs[i])
5129 if (i == NL80211_VHT_NSS_MAX)
5132 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5137 *fixed_nss = nss + 1;
5141 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
5144 *fixed_rate = pream | nss | rate;
5149 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5150 const struct cfg80211_bitrate_mask *mask,
5151 enum ieee80211_band band,
5155 /* First check full NSS mask, if we can simply limit NSS */
5156 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5159 /* Next Check single rate is set */
5160 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
5163 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5168 struct ath10k *ar = arvif->ar;
5172 mutex_lock(&ar->conf_mutex);
5174 if (arvif->fixed_rate == fixed_rate &&
5175 arvif->fixed_nss == fixed_nss &&
5176 arvif->force_sgi == force_sgi)
5179 if (fixed_rate == WMI_FIXED_RATE_NONE)
5180 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
5183 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
5185 vdev_param = ar->wmi.vdev_param->fixed_rate;
5186 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5187 vdev_param, fixed_rate);
5189 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
5195 arvif->fixed_rate = fixed_rate;
5197 vdev_param = ar->wmi.vdev_param->nss;
5198 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5199 vdev_param, fixed_nss);
5202 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
5208 arvif->fixed_nss = fixed_nss;
5210 vdev_param = ar->wmi.vdev_param->sgi;
5211 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5215 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
5221 arvif->force_sgi = force_sgi;
5224 mutex_unlock(&ar->conf_mutex);
5228 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5229 struct ieee80211_vif *vif,
5230 const struct cfg80211_bitrate_mask *mask)
5232 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5233 struct ath10k *ar = arvif->ar;
5234 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5235 u8 fixed_rate = WMI_FIXED_RATE_NONE;
5236 u8 fixed_nss = ar->num_rf_chains;
5239 if (ar->cfg_tx_chainmask)
5240 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5242 force_sgi = mask->control[band].gi;
5243 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5246 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
5247 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
5253 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
5254 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
5258 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5259 fixed_nss, force_sgi);
5262 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5263 struct ieee80211_vif *vif,
5264 struct ieee80211_sta *sta,
5267 struct ath10k *ar = hw->priv;
5268 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5271 spin_lock_bh(&ar->data_lock);
5273 ath10k_dbg(ar, ATH10K_DBG_MAC,
5274 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5275 sta->addr, changed, sta->bandwidth, sta->rx_nss,
5278 if (changed & IEEE80211_RC_BW_CHANGED) {
5279 bw = WMI_PEER_CHWIDTH_20MHZ;
5281 switch (sta->bandwidth) {
5282 case IEEE80211_STA_RX_BW_20:
5283 bw = WMI_PEER_CHWIDTH_20MHZ;
5285 case IEEE80211_STA_RX_BW_40:
5286 bw = WMI_PEER_CHWIDTH_40MHZ;
5288 case IEEE80211_STA_RX_BW_80:
5289 bw = WMI_PEER_CHWIDTH_80MHZ;
5291 case IEEE80211_STA_RX_BW_160:
5292 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
5293 sta->bandwidth, sta->addr);
5294 bw = WMI_PEER_CHWIDTH_20MHZ;
5301 if (changed & IEEE80211_RC_NSS_CHANGED)
5302 arsta->nss = sta->rx_nss;
5304 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5305 smps = WMI_PEER_SMPS_PS_NONE;
5307 switch (sta->smps_mode) {
5308 case IEEE80211_SMPS_AUTOMATIC:
5309 case IEEE80211_SMPS_OFF:
5310 smps = WMI_PEER_SMPS_PS_NONE;
5312 case IEEE80211_SMPS_STATIC:
5313 smps = WMI_PEER_SMPS_STATIC;
5315 case IEEE80211_SMPS_DYNAMIC:
5316 smps = WMI_PEER_SMPS_DYNAMIC;
5318 case IEEE80211_SMPS_NUM_MODES:
5319 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
5320 sta->smps_mode, sta->addr);
5321 smps = WMI_PEER_SMPS_PS_NONE;
5328 arsta->changed |= changed;
5330 spin_unlock_bh(&ar->data_lock);
5332 ieee80211_queue_work(hw, &arsta->update_wk);
5335 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5338 * FIXME: Return 0 for time being. Need to figure out whether FW
5339 * has the API to fetch 64-bit local TSF
5345 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5346 struct ieee80211_vif *vif,
5347 enum ieee80211_ampdu_mlme_action action,
5348 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5351 struct ath10k *ar = hw->priv;
5352 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5354 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
5355 arvif->vdev_id, sta->addr, tid, action);
5358 case IEEE80211_AMPDU_RX_START:
5359 case IEEE80211_AMPDU_RX_STOP:
5360 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5361 * creation/removal. Do we need to verify this?
5364 case IEEE80211_AMPDU_TX_START:
5365 case IEEE80211_AMPDU_TX_STOP_CONT:
5366 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5367 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5368 case IEEE80211_AMPDU_TX_OPERATIONAL:
5369 /* Firmware offloads Tx aggregation entirely so deny mac80211
5370 * Tx aggregation requests.
5378 static const struct ieee80211_ops ath10k_ops = {
5380 .start = ath10k_start,
5381 .stop = ath10k_stop,
5382 .config = ath10k_config,
5383 .add_interface = ath10k_add_interface,
5384 .remove_interface = ath10k_remove_interface,
5385 .configure_filter = ath10k_configure_filter,
5386 .bss_info_changed = ath10k_bss_info_changed,
5387 .hw_scan = ath10k_hw_scan,
5388 .cancel_hw_scan = ath10k_cancel_hw_scan,
5389 .set_key = ath10k_set_key,
5390 .set_default_unicast_key = ath10k_set_default_unicast_key,
5391 .sta_state = ath10k_sta_state,
5392 .conf_tx = ath10k_conf_tx,
5393 .remain_on_channel = ath10k_remain_on_channel,
5394 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5395 .set_rts_threshold = ath10k_set_rts_threshold,
5396 .flush = ath10k_flush,
5397 .tx_last_beacon = ath10k_tx_last_beacon,
5398 .set_antenna = ath10k_set_antenna,
5399 .get_antenna = ath10k_get_antenna,
5400 .reconfig_complete = ath10k_reconfig_complete,
5401 .get_survey = ath10k_get_survey,
5402 .set_bitrate_mask = ath10k_set_bitrate_mask,
5403 .sta_rc_update = ath10k_sta_rc_update,
5404 .get_tsf = ath10k_get_tsf,
5405 .ampdu_action = ath10k_ampdu_action,
5406 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5407 .get_et_stats = ath10k_debug_get_et_stats,
5408 .get_et_strings = ath10k_debug_get_et_strings,
5410 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5413 .suspend = ath10k_suspend,
5414 .resume = ath10k_resume,
5416 #ifdef CONFIG_MAC80211_DEBUGFS
5417 .sta_add_debugfs = ath10k_sta_add_debugfs,
5421 #define RATETAB_ENT(_rate, _rateid, _flags) { \
5422 .bitrate = (_rate), \
5423 .flags = (_flags), \
5424 .hw_value = (_rateid), \
5427 #define CHAN2G(_channel, _freq, _flags) { \
5428 .band = IEEE80211_BAND_2GHZ, \
5429 .hw_value = (_channel), \
5430 .center_freq = (_freq), \
5431 .flags = (_flags), \
5432 .max_antenna_gain = 0, \
5436 #define CHAN5G(_channel, _freq, _flags) { \
5437 .band = IEEE80211_BAND_5GHZ, \
5438 .hw_value = (_channel), \
5439 .center_freq = (_freq), \
5440 .flags = (_flags), \
5441 .max_antenna_gain = 0, \
5445 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5455 CHAN2G(10, 2457, 0),
5456 CHAN2G(11, 2462, 0),
5457 CHAN2G(12, 2467, 0),
5458 CHAN2G(13, 2472, 0),
5459 CHAN2G(14, 2484, 0),
5462 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
5463 CHAN5G(36, 5180, 0),
5464 CHAN5G(40, 5200, 0),
5465 CHAN5G(44, 5220, 0),
5466 CHAN5G(48, 5240, 0),
5467 CHAN5G(52, 5260, 0),
5468 CHAN5G(56, 5280, 0),
5469 CHAN5G(60, 5300, 0),
5470 CHAN5G(64, 5320, 0),
5471 CHAN5G(100, 5500, 0),
5472 CHAN5G(104, 5520, 0),
5473 CHAN5G(108, 5540, 0),
5474 CHAN5G(112, 5560, 0),
5475 CHAN5G(116, 5580, 0),
5476 CHAN5G(120, 5600, 0),
5477 CHAN5G(124, 5620, 0),
5478 CHAN5G(128, 5640, 0),
5479 CHAN5G(132, 5660, 0),
5480 CHAN5G(136, 5680, 0),
5481 CHAN5G(140, 5700, 0),
5482 CHAN5G(149, 5745, 0),
5483 CHAN5G(153, 5765, 0),
5484 CHAN5G(157, 5785, 0),
5485 CHAN5G(161, 5805, 0),
5486 CHAN5G(165, 5825, 0),
5489 /* Note: Be careful if you re-order these. There is code which depends on this
5492 static struct ieee80211_rate ath10k_rates[] = {
5494 RATETAB_ENT(10, 0x82, 0),
5495 RATETAB_ENT(20, 0x84, 0),
5496 RATETAB_ENT(55, 0x8b, 0),
5497 RATETAB_ENT(110, 0x96, 0),
5499 RATETAB_ENT(60, 0x0c, 0),
5500 RATETAB_ENT(90, 0x12, 0),
5501 RATETAB_ENT(120, 0x18, 0),
5502 RATETAB_ENT(180, 0x24, 0),
5503 RATETAB_ENT(240, 0x30, 0),
5504 RATETAB_ENT(360, 0x48, 0),
5505 RATETAB_ENT(480, 0x60, 0),
5506 RATETAB_ENT(540, 0x6c, 0),
5509 #define ath10k_a_rates (ath10k_rates + 4)
5510 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5511 #define ath10k_g_rates (ath10k_rates + 0)
5512 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5514 struct ath10k *ath10k_mac_create(size_t priv_size)
5516 struct ieee80211_hw *hw;
5519 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5529 void ath10k_mac_destroy(struct ath10k *ar)
5531 ieee80211_free_hw(ar->hw);
5534 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5537 .types = BIT(NL80211_IFTYPE_STATION)
5538 | BIT(NL80211_IFTYPE_P2P_CLIENT)
5542 .types = BIT(NL80211_IFTYPE_P2P_GO)
5546 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5550 .types = BIT(NL80211_IFTYPE_AP)
5554 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
5557 .types = BIT(NL80211_IFTYPE_AP)
5561 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5563 .limits = ath10k_if_limits,
5564 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5565 .max_interfaces = 8,
5566 .num_different_channels = 1,
5567 .beacon_int_infra_match = true,
5571 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
5573 .limits = ath10k_10x_if_limits,
5574 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
5575 .max_interfaces = 8,
5576 .num_different_channels = 1,
5577 .beacon_int_infra_match = true,
5578 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
5579 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5580 BIT(NL80211_CHAN_WIDTH_20) |
5581 BIT(NL80211_CHAN_WIDTH_40) |
5582 BIT(NL80211_CHAN_WIDTH_80),
5587 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5589 struct ieee80211_sta_vht_cap vht_cap = {0};
5594 vht_cap.vht_supported = 1;
5595 vht_cap.cap = ar->vht_cap_info;
5597 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5598 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5599 val = ar->num_rf_chains - 1;
5600 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5601 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5606 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5607 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5608 val = ar->num_rf_chains - 1;
5609 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5610 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5616 for (i = 0; i < 8; i++) {
5617 if (i < ar->num_rf_chains)
5618 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5620 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5623 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5624 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5629 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5632 struct ieee80211_sta_ht_cap ht_cap = {0};
5634 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5637 ht_cap.ht_supported = 1;
5638 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5639 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5640 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5641 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5642 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5644 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5645 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5647 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5648 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5650 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5653 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5654 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5659 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5660 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5662 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5665 stbc = ar->ht_cap_info;
5666 stbc &= WMI_HT_CAP_RX_STBC;
5667 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5668 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5669 stbc &= IEEE80211_HT_CAP_RX_STBC;
5674 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5675 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5677 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5678 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5680 /* max AMSDU is implicitly taken from vht_cap_info */
5681 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5682 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5684 for (i = 0; i < ar->num_rf_chains; i++)
5685 ht_cap.mcs.rx_mask[i] = 0xFF;
5687 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5692 static void ath10k_get_arvif_iter(void *data, u8 *mac,
5693 struct ieee80211_vif *vif)
5695 struct ath10k_vif_iter *arvif_iter = data;
5696 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5698 if (arvif->vdev_id == arvif_iter->vdev_id)
5699 arvif_iter->arvif = arvif;
5702 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5704 struct ath10k_vif_iter arvif_iter;
5707 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5708 arvif_iter.vdev_id = vdev_id;
5710 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5711 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5713 ath10k_get_arvif_iter,
5715 if (!arvif_iter.arvif) {
5716 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5720 return arvif_iter.arvif;
5723 int ath10k_mac_register(struct ath10k *ar)
5725 static const u32 cipher_suites[] = {
5726 WLAN_CIPHER_SUITE_WEP40,
5727 WLAN_CIPHER_SUITE_WEP104,
5728 WLAN_CIPHER_SUITE_TKIP,
5729 WLAN_CIPHER_SUITE_CCMP,
5730 WLAN_CIPHER_SUITE_AES_CMAC,
5732 struct ieee80211_supported_band *band;
5733 struct ieee80211_sta_vht_cap vht_cap;
5734 struct ieee80211_sta_ht_cap ht_cap;
5738 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5740 SET_IEEE80211_DEV(ar->hw, ar->dev);
5742 ht_cap = ath10k_get_ht_cap(ar);
5743 vht_cap = ath10k_create_vht_cap(ar);
5745 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5746 channels = kmemdup(ath10k_2ghz_channels,
5747 sizeof(ath10k_2ghz_channels),
5754 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5755 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5756 band->channels = channels;
5757 band->n_bitrates = ath10k_g_rates_size;
5758 band->bitrates = ath10k_g_rates;
5759 band->ht_cap = ht_cap;
5761 /* Enable the VHT support at 2.4 GHz */
5762 band->vht_cap = vht_cap;
5764 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5767 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5768 channels = kmemdup(ath10k_5ghz_channels,
5769 sizeof(ath10k_5ghz_channels),
5776 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5777 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5778 band->channels = channels;
5779 band->n_bitrates = ath10k_a_rates_size;
5780 band->bitrates = ath10k_a_rates;
5781 band->ht_cap = ht_cap;
5782 band->vht_cap = vht_cap;
5783 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5786 ar->hw->wiphy->interface_modes =
5787 BIT(NL80211_IFTYPE_STATION) |
5788 BIT(NL80211_IFTYPE_AP);
5790 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5791 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5793 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5794 ar->hw->wiphy->interface_modes |=
5795 BIT(NL80211_IFTYPE_P2P_DEVICE) |
5796 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5797 BIT(NL80211_IFTYPE_P2P_GO);
5799 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5800 IEEE80211_HW_SUPPORTS_PS |
5801 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5802 IEEE80211_HW_MFP_CAPABLE |
5803 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5804 IEEE80211_HW_HAS_RATE_CONTROL |
5805 IEEE80211_HW_AP_LINK_PS |
5806 IEEE80211_HW_SPECTRUM_MGMT |
5807 IEEE80211_HW_SW_CRYPTO_CONTROL |
5808 IEEE80211_HW_CONNECTION_MONITOR;
5810 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5812 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5813 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5815 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5816 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5817 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5820 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5821 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5823 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
5824 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5826 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5828 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5829 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5831 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5832 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5833 * correct Probe Responses. This is more of a hack advert..
5835 ar->hw->wiphy->probe_resp_offload |=
5836 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5837 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5838 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5841 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5842 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5843 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5845 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5846 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5849 * on LL hardware queues are managed entirely by the FW
5850 * so we only advertise to mac we can do the queues thing
5854 switch (ar->wmi.op_version) {
5855 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5856 case ATH10K_FW_WMI_OP_VERSION_TLV:
5857 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5858 ar->hw->wiphy->n_iface_combinations =
5859 ARRAY_SIZE(ath10k_if_comb);
5860 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5862 case ATH10K_FW_WMI_OP_VERSION_10_1:
5863 case ATH10K_FW_WMI_OP_VERSION_10_2:
5864 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5865 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5866 ar->hw->wiphy->n_iface_combinations =
5867 ARRAY_SIZE(ath10k_10x_if_comb);
5869 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5870 case ATH10K_FW_WMI_OP_VERSION_MAX:
5876 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5878 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5879 /* Init ath dfs pattern detector */
5880 ar->ath_common.debug_mask = ATH_DBG_DFS;
5881 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5884 if (!ar->dfs_detector)
5885 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
5888 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5889 ath10k_reg_notifier);
5891 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
5895 ar->hw->wiphy->cipher_suites = cipher_suites;
5896 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5898 ret = ieee80211_register_hw(ar->hw);
5900 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
5904 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5905 ret = regulatory_hint(ar->hw->wiphy,
5906 ar->ath_common.regulatory.alpha2);
5908 goto err_unregister;
5914 ieee80211_unregister_hw(ar->hw);
5916 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5917 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5922 void ath10k_mac_unregister(struct ath10k *ar)
5924 ieee80211_unregister_hw(ar->hw);
5926 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5927 ar->dfs_detector->exit(ar->dfs_detector);
5929 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5930 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5932 SET_IEEE80211_DEV(ar->hw, NULL);