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>
38 static int ath10k_send_key(struct ath10k_vif *arvif,
39 struct ieee80211_key_conf *key,
41 const u8 *macaddr, u32 flags)
43 struct ath10k *ar = arvif->ar;
44 struct wmi_vdev_install_key_arg arg = {
45 .vdev_id = arvif->vdev_id,
46 .key_idx = key->keyidx,
47 .key_len = key->keylen,
53 lockdep_assert_held(&arvif->ar->conf_mutex);
55 switch (key->cipher) {
56 case WLAN_CIPHER_SUITE_CCMP:
57 arg.key_cipher = WMI_CIPHER_AES_CCM;
58 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV_MGMT;
60 case WLAN_CIPHER_SUITE_TKIP:
61 arg.key_cipher = WMI_CIPHER_TKIP;
62 arg.key_txmic_len = 8;
63 arg.key_rxmic_len = 8;
65 case WLAN_CIPHER_SUITE_WEP40:
66 case WLAN_CIPHER_SUITE_WEP104:
67 arg.key_cipher = WMI_CIPHER_WEP;
69 case WLAN_CIPHER_SUITE_AES_CMAC:
73 ath10k_warn(ar, "cipher %d is not supported\n", key->cipher);
77 if (cmd == DISABLE_KEY) {
78 arg.key_cipher = WMI_CIPHER_NONE;
82 return ath10k_wmi_vdev_install_key(arvif->ar, &arg);
85 static int ath10k_install_key(struct ath10k_vif *arvif,
86 struct ieee80211_key_conf *key,
88 const u8 *macaddr, u32 flags)
90 struct ath10k *ar = arvif->ar;
93 lockdep_assert_held(&ar->conf_mutex);
95 reinit_completion(&ar->install_key_done);
97 ret = ath10k_send_key(arvif, key, cmd, macaddr, flags);
101 ret = wait_for_completion_timeout(&ar->install_key_done, 3*HZ);
108 static int ath10k_install_peer_wep_keys(struct ath10k_vif *arvif,
111 struct ath10k *ar = arvif->ar;
112 struct ath10k_peer *peer;
117 lockdep_assert_held(&ar->conf_mutex);
119 spin_lock_bh(&ar->data_lock);
120 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
121 spin_unlock_bh(&ar->data_lock);
126 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
127 if (arvif->wep_keys[i] == NULL)
131 flags |= WMI_KEY_PAIRWISE;
133 /* set TX_USAGE flag for default key id */
134 if (arvif->def_wep_key_idx == i)
135 flags |= WMI_KEY_TX_USAGE;
137 ret = ath10k_install_key(arvif, arvif->wep_keys[i], SET_KEY,
142 spin_lock_bh(&ar->data_lock);
143 peer->keys[i] = arvif->wep_keys[i];
144 spin_unlock_bh(&ar->data_lock);
150 static int ath10k_clear_peer_keys(struct ath10k_vif *arvif,
153 struct ath10k *ar = arvif->ar;
154 struct ath10k_peer *peer;
160 lockdep_assert_held(&ar->conf_mutex);
162 spin_lock_bh(&ar->data_lock);
163 peer = ath10k_peer_find(ar, arvif->vdev_id, addr);
164 spin_unlock_bh(&ar->data_lock);
169 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
170 if (peer->keys[i] == NULL)
173 /* key flags are not required to delete the key */
174 ret = ath10k_install_key(arvif, peer->keys[i],
175 DISABLE_KEY, addr, flags);
176 if (ret && first_errno == 0)
180 ath10k_warn(ar, "failed to remove peer wep key %d: %d\n",
183 spin_lock_bh(&ar->data_lock);
184 peer->keys[i] = NULL;
185 spin_unlock_bh(&ar->data_lock);
191 bool ath10k_mac_is_peer_wep_key_set(struct ath10k *ar, const u8 *addr,
194 struct ath10k_peer *peer;
197 lockdep_assert_held(&ar->data_lock);
199 /* We don't know which vdev this peer belongs to,
200 * since WMI doesn't give us that information.
202 * FIXME: multi-bss needs to be handled.
204 peer = ath10k_peer_find(ar, 0, addr);
208 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
209 if (peer->keys[i] && peer->keys[i]->keyidx == keyidx)
216 static int ath10k_clear_vdev_key(struct ath10k_vif *arvif,
217 struct ieee80211_key_conf *key)
219 struct ath10k *ar = arvif->ar;
220 struct ath10k_peer *peer;
227 lockdep_assert_held(&ar->conf_mutex);
230 /* since ath10k_install_key we can't hold data_lock all the
231 * time, so we try to remove the keys incrementally */
232 spin_lock_bh(&ar->data_lock);
234 list_for_each_entry(peer, &ar->peers, list) {
235 for (i = 0; i < ARRAY_SIZE(peer->keys); i++) {
236 if (peer->keys[i] == key) {
237 ether_addr_copy(addr, peer->addr);
238 peer->keys[i] = NULL;
243 if (i < ARRAY_SIZE(peer->keys))
246 spin_unlock_bh(&ar->data_lock);
248 if (i == ARRAY_SIZE(peer->keys))
250 /* key flags are not required to delete the key */
251 ret = ath10k_install_key(arvif, key, DISABLE_KEY, addr, flags);
252 if (ret && first_errno == 0)
256 ath10k_warn(ar, "failed to remove key for %pM: %d\n",
263 static int ath10k_mac_vif_sta_fix_wep_key(struct ath10k_vif *arvif)
265 struct ath10k *ar = arvif->ar;
266 enum nl80211_iftype iftype = arvif->vif->type;
267 struct ieee80211_key_conf *key;
273 lockdep_assert_held(&ar->conf_mutex);
275 if (iftype != NL80211_IFTYPE_STATION)
278 for (i = 0; i < ARRAY_SIZE(arvif->wep_keys); i++) {
279 if (arvif->wep_keys[i]) {
280 key = arvif->wep_keys[i];
288 flags |= WMI_KEY_PAIRWISE;
289 flags |= WMI_KEY_TX_USAGE;
291 ret = ath10k_install_key(arvif, key, SET_KEY, arvif->bssid, flags);
293 ath10k_warn(ar, "failed to install key %i on vdev %i: %d\n",
294 key->keyidx, arvif->vdev_id, ret);
301 static int ath10k_mac_vif_update_wep_key(struct ath10k_vif *arvif,
302 struct ieee80211_key_conf *key)
304 struct ath10k *ar = arvif->ar;
305 struct ath10k_peer *peer;
308 lockdep_assert_held(&ar->conf_mutex);
310 list_for_each_entry(peer, &ar->peers, list) {
311 if (!memcmp(peer->addr, arvif->vif->addr, ETH_ALEN))
314 if (!memcmp(peer->addr, arvif->bssid, ETH_ALEN))
317 if (peer->keys[key->keyidx] == key)
320 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vif vdev %i update key %i needs update\n",
321 arvif->vdev_id, key->keyidx);
323 ret = ath10k_install_peer_wep_keys(arvif, peer->addr);
325 ath10k_warn(ar, "failed to update wep keys on vdev %i for peer %pM: %d\n",
326 arvif->vdev_id, peer->addr, ret);
334 /*********************/
335 /* General utilities */
336 /*********************/
338 static inline enum wmi_phy_mode
339 chan_to_phymode(const struct cfg80211_chan_def *chandef)
341 enum wmi_phy_mode phymode = MODE_UNKNOWN;
343 switch (chandef->chan->band) {
344 case IEEE80211_BAND_2GHZ:
345 switch (chandef->width) {
346 case NL80211_CHAN_WIDTH_20_NOHT:
347 if (chandef->chan->flags & IEEE80211_CHAN_NO_OFDM)
352 case NL80211_CHAN_WIDTH_20:
353 phymode = MODE_11NG_HT20;
355 case NL80211_CHAN_WIDTH_40:
356 phymode = MODE_11NG_HT40;
358 case NL80211_CHAN_WIDTH_5:
359 case NL80211_CHAN_WIDTH_10:
360 case NL80211_CHAN_WIDTH_80:
361 case NL80211_CHAN_WIDTH_80P80:
362 case NL80211_CHAN_WIDTH_160:
363 phymode = MODE_UNKNOWN;
367 case IEEE80211_BAND_5GHZ:
368 switch (chandef->width) {
369 case NL80211_CHAN_WIDTH_20_NOHT:
372 case NL80211_CHAN_WIDTH_20:
373 phymode = MODE_11NA_HT20;
375 case NL80211_CHAN_WIDTH_40:
376 phymode = MODE_11NA_HT40;
378 case NL80211_CHAN_WIDTH_80:
379 phymode = MODE_11AC_VHT80;
381 case NL80211_CHAN_WIDTH_5:
382 case NL80211_CHAN_WIDTH_10:
383 case NL80211_CHAN_WIDTH_80P80:
384 case NL80211_CHAN_WIDTH_160:
385 phymode = MODE_UNKNOWN;
393 WARN_ON(phymode == MODE_UNKNOWN);
397 static u8 ath10k_parse_mpdudensity(u8 mpdudensity)
400 * 802.11n D2.0 defined values for "Minimum MPDU Start Spacing":
401 * 0 for no restriction
410 switch (mpdudensity) {
416 /* Our lower layer calculations limit our precision to
432 static int ath10k_peer_create(struct ath10k *ar, u32 vdev_id, const u8 *addr,
433 enum wmi_peer_type peer_type)
437 lockdep_assert_held(&ar->conf_mutex);
439 if (ar->num_peers >= ar->max_num_peers)
442 ret = ath10k_wmi_peer_create(ar, vdev_id, addr, peer_type);
444 ath10k_warn(ar, "failed to create wmi peer %pM on vdev %i: %i\n",
449 ret = ath10k_wait_for_peer_created(ar, vdev_id, addr);
451 ath10k_warn(ar, "failed to wait for created wmi peer %pM on vdev %i: %i\n",
461 static int ath10k_mac_set_kickout(struct ath10k_vif *arvif)
463 struct ath10k *ar = arvif->ar;
467 param = ar->wmi.pdev_param->sta_kickout_th;
468 ret = ath10k_wmi_pdev_set_param(ar, param,
469 ATH10K_KICKOUT_THRESHOLD);
471 ath10k_warn(ar, "failed to set kickout threshold on vdev %i: %d\n",
472 arvif->vdev_id, ret);
476 param = ar->wmi.vdev_param->ap_keepalive_min_idle_inactive_time_secs;
477 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
478 ATH10K_KEEPALIVE_MIN_IDLE);
480 ath10k_warn(ar, "failed to set keepalive minimum idle time on vdev %i: %d\n",
481 arvif->vdev_id, ret);
485 param = ar->wmi.vdev_param->ap_keepalive_max_idle_inactive_time_secs;
486 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
487 ATH10K_KEEPALIVE_MAX_IDLE);
489 ath10k_warn(ar, "failed to set keepalive maximum idle time on vdev %i: %d\n",
490 arvif->vdev_id, ret);
494 param = ar->wmi.vdev_param->ap_keepalive_max_unresponsive_time_secs;
495 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param,
496 ATH10K_KEEPALIVE_MAX_UNRESPONSIVE);
498 ath10k_warn(ar, "failed to set keepalive maximum unresponsive time on vdev %i: %d\n",
499 arvif->vdev_id, ret);
506 static int ath10k_mac_set_rts(struct ath10k_vif *arvif, u32 value)
508 struct ath10k *ar = arvif->ar;
511 vdev_param = ar->wmi.vdev_param->rts_threshold;
512 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
515 static int ath10k_mac_set_frag(struct ath10k_vif *arvif, u32 value)
517 struct ath10k *ar = arvif->ar;
520 if (value != 0xFFFFFFFF)
521 value = clamp_t(u32, arvif->ar->hw->wiphy->frag_threshold,
522 ATH10K_FRAGMT_THRESHOLD_MIN,
523 ATH10K_FRAGMT_THRESHOLD_MAX);
525 vdev_param = ar->wmi.vdev_param->fragmentation_threshold;
526 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param, value);
529 static int ath10k_peer_delete(struct ath10k *ar, u32 vdev_id, const u8 *addr)
533 lockdep_assert_held(&ar->conf_mutex);
535 ret = ath10k_wmi_peer_delete(ar, vdev_id, addr);
539 ret = ath10k_wait_for_peer_deleted(ar, vdev_id, addr);
548 static void ath10k_peer_cleanup(struct ath10k *ar, u32 vdev_id)
550 struct ath10k_peer *peer, *tmp;
552 lockdep_assert_held(&ar->conf_mutex);
554 spin_lock_bh(&ar->data_lock);
555 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
556 if (peer->vdev_id != vdev_id)
559 ath10k_warn(ar, "removing stale peer %pM from vdev_id %d\n",
560 peer->addr, vdev_id);
562 list_del(&peer->list);
566 spin_unlock_bh(&ar->data_lock);
569 static void ath10k_peer_cleanup_all(struct ath10k *ar)
571 struct ath10k_peer *peer, *tmp;
573 lockdep_assert_held(&ar->conf_mutex);
575 spin_lock_bh(&ar->data_lock);
576 list_for_each_entry_safe(peer, tmp, &ar->peers, list) {
577 list_del(&peer->list);
580 spin_unlock_bh(&ar->data_lock);
583 ar->num_stations = 0;
586 /************************/
587 /* Interface management */
588 /************************/
590 void ath10k_mac_vif_beacon_free(struct ath10k_vif *arvif)
592 struct ath10k *ar = arvif->ar;
594 lockdep_assert_held(&ar->data_lock);
599 if (!arvif->beacon_buf)
600 dma_unmap_single(ar->dev, ATH10K_SKB_CB(arvif->beacon)->paddr,
601 arvif->beacon->len, DMA_TO_DEVICE);
603 if (WARN_ON(arvif->beacon_state != ATH10K_BEACON_SCHEDULED &&
604 arvif->beacon_state != ATH10K_BEACON_SENT))
607 dev_kfree_skb_any(arvif->beacon);
609 arvif->beacon = NULL;
610 arvif->beacon_state = ATH10K_BEACON_SCHEDULED;
613 static void ath10k_mac_vif_beacon_cleanup(struct ath10k_vif *arvif)
615 struct ath10k *ar = arvif->ar;
617 lockdep_assert_held(&ar->data_lock);
619 ath10k_mac_vif_beacon_free(arvif);
621 if (arvif->beacon_buf) {
622 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
623 arvif->beacon_buf, arvif->beacon_paddr);
624 arvif->beacon_buf = NULL;
628 static inline int ath10k_vdev_setup_sync(struct ath10k *ar)
632 lockdep_assert_held(&ar->conf_mutex);
634 if (test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags))
637 ret = wait_for_completion_timeout(&ar->vdev_setup_done,
638 ATH10K_VDEV_SETUP_TIMEOUT_HZ);
645 static int ath10k_monitor_vdev_start(struct ath10k *ar, int vdev_id)
647 struct cfg80211_chan_def *chandef = &ar->chandef;
648 struct ieee80211_channel *channel = chandef->chan;
649 struct wmi_vdev_start_request_arg arg = {};
652 lockdep_assert_held(&ar->conf_mutex);
654 arg.vdev_id = vdev_id;
655 arg.channel.freq = channel->center_freq;
656 arg.channel.band_center_freq1 = chandef->center_freq1;
658 /* TODO setup this dynamically, what in case we
659 don't have any vifs? */
660 arg.channel.mode = chan_to_phymode(chandef);
661 arg.channel.chan_radar =
662 !!(channel->flags & IEEE80211_CHAN_RADAR);
664 arg.channel.min_power = 0;
665 arg.channel.max_power = channel->max_power * 2;
666 arg.channel.max_reg_power = channel->max_reg_power * 2;
667 arg.channel.max_antenna_gain = channel->max_antenna_gain * 2;
669 reinit_completion(&ar->vdev_setup_done);
671 ret = ath10k_wmi_vdev_start(ar, &arg);
673 ath10k_warn(ar, "failed to request monitor vdev %i start: %d\n",
678 ret = ath10k_vdev_setup_sync(ar);
680 ath10k_warn(ar, "failed to synchronize setup for monitor vdev %i start: %d\n",
685 ret = ath10k_wmi_vdev_up(ar, vdev_id, 0, ar->mac_addr);
687 ath10k_warn(ar, "failed to put up monitor vdev %i: %d\n",
692 ar->monitor_vdev_id = vdev_id;
694 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i started\n",
695 ar->monitor_vdev_id);
699 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
701 ath10k_warn(ar, "failed to stop monitor vdev %i after start failure: %d\n",
702 ar->monitor_vdev_id, ret);
707 static int ath10k_monitor_vdev_stop(struct ath10k *ar)
711 lockdep_assert_held(&ar->conf_mutex);
713 ret = ath10k_wmi_vdev_down(ar, ar->monitor_vdev_id);
715 ath10k_warn(ar, "failed to put down monitor vdev %i: %d\n",
716 ar->monitor_vdev_id, ret);
718 reinit_completion(&ar->vdev_setup_done);
720 ret = ath10k_wmi_vdev_stop(ar, ar->monitor_vdev_id);
722 ath10k_warn(ar, "failed to to request monitor vdev %i stop: %d\n",
723 ar->monitor_vdev_id, ret);
725 ret = ath10k_vdev_setup_sync(ar);
727 ath10k_warn(ar, "failed to synchronize monitor vdev %i stop: %d\n",
728 ar->monitor_vdev_id, ret);
730 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %i stopped\n",
731 ar->monitor_vdev_id);
735 static int ath10k_monitor_vdev_create(struct ath10k *ar)
739 lockdep_assert_held(&ar->conf_mutex);
741 if (ar->free_vdev_map == 0) {
742 ath10k_warn(ar, "failed to find free vdev id for monitor vdev\n");
746 bit = __ffs64(ar->free_vdev_map);
748 ar->monitor_vdev_id = bit;
750 ret = ath10k_wmi_vdev_create(ar, ar->monitor_vdev_id,
751 WMI_VDEV_TYPE_MONITOR,
754 ath10k_warn(ar, "failed to request monitor vdev %i creation: %d\n",
755 ar->monitor_vdev_id, ret);
759 ar->free_vdev_map &= ~(1LL << ar->monitor_vdev_id);
760 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d created\n",
761 ar->monitor_vdev_id);
766 static int ath10k_monitor_vdev_delete(struct ath10k *ar)
770 lockdep_assert_held(&ar->conf_mutex);
772 ret = ath10k_wmi_vdev_delete(ar, ar->monitor_vdev_id);
774 ath10k_warn(ar, "failed to request wmi monitor vdev %i removal: %d\n",
775 ar->monitor_vdev_id, ret);
779 ar->free_vdev_map |= 1LL << ar->monitor_vdev_id;
781 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor vdev %d deleted\n",
782 ar->monitor_vdev_id);
786 static int ath10k_monitor_start(struct ath10k *ar)
790 lockdep_assert_held(&ar->conf_mutex);
792 ret = ath10k_monitor_vdev_create(ar);
794 ath10k_warn(ar, "failed to create monitor vdev: %d\n", ret);
798 ret = ath10k_monitor_vdev_start(ar, ar->monitor_vdev_id);
800 ath10k_warn(ar, "failed to start monitor vdev: %d\n", ret);
801 ath10k_monitor_vdev_delete(ar);
805 ar->monitor_started = true;
806 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor started\n");
811 static int ath10k_monitor_stop(struct ath10k *ar)
815 lockdep_assert_held(&ar->conf_mutex);
817 ret = ath10k_monitor_vdev_stop(ar);
819 ath10k_warn(ar, "failed to stop monitor vdev: %d\n", ret);
823 ret = ath10k_monitor_vdev_delete(ar);
825 ath10k_warn(ar, "failed to delete monitor vdev: %d\n", ret);
829 ar->monitor_started = false;
830 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac monitor stopped\n");
835 static bool ath10k_mac_should_disable_promisc(struct ath10k *ar)
837 struct ath10k_vif *arvif;
839 if (!(ar->filter_flags & FIF_PROMISC_IN_BSS))
842 if (!ar->num_started_vdevs)
845 list_for_each_entry(arvif, &ar->arvifs, list)
846 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
849 ath10k_dbg(ar, ATH10K_DBG_MAC,
850 "mac disabling promiscuous mode because vdev is started\n");
854 static int ath10k_monitor_recalc(struct ath10k *ar)
858 lockdep_assert_held(&ar->conf_mutex);
860 should_start = ar->monitor ||
861 !ath10k_mac_should_disable_promisc(ar) ||
862 test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
864 ath10k_dbg(ar, ATH10K_DBG_MAC,
865 "mac monitor recalc started? %d should? %d\n",
866 ar->monitor_started, should_start);
868 if (should_start == ar->monitor_started)
872 return ath10k_monitor_start(ar);
874 return ath10k_monitor_stop(ar);
877 static int ath10k_recalc_rtscts_prot(struct ath10k_vif *arvif)
879 struct ath10k *ar = arvif->ar;
880 u32 vdev_param, rts_cts = 0;
882 lockdep_assert_held(&ar->conf_mutex);
884 vdev_param = ar->wmi.vdev_param->enable_rtscts;
886 rts_cts |= SM(WMI_RTSCTS_ENABLED, WMI_RTSCTS_SET);
888 if (arvif->num_legacy_stations > 0)
889 rts_cts |= SM(WMI_RTSCTS_ACROSS_SW_RETRIES,
892 rts_cts |= SM(WMI_RTSCTS_FOR_SECOND_RATESERIES,
895 return ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
899 static int ath10k_start_cac(struct ath10k *ar)
903 lockdep_assert_held(&ar->conf_mutex);
905 set_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
907 ret = ath10k_monitor_recalc(ar);
909 ath10k_warn(ar, "failed to start monitor (cac): %d\n", ret);
910 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
914 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac start monitor vdev %d\n",
915 ar->monitor_vdev_id);
920 static int ath10k_stop_cac(struct ath10k *ar)
922 lockdep_assert_held(&ar->conf_mutex);
924 /* CAC is not running - do nothing */
925 if (!test_bit(ATH10K_CAC_RUNNING, &ar->dev_flags))
928 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
929 ath10k_monitor_stop(ar);
931 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac cac finished\n");
936 static void ath10k_recalc_radar_detection(struct ath10k *ar)
940 lockdep_assert_held(&ar->conf_mutex);
944 if (!ar->radar_enabled)
947 if (ar->num_started_vdevs > 0)
950 ret = ath10k_start_cac(ar);
953 * Not possible to start CAC on current channel so starting
954 * radiation is not allowed, make this channel DFS_UNAVAILABLE
955 * by indicating that radar was detected.
957 ath10k_warn(ar, "failed to start CAC: %d\n", ret);
958 ieee80211_radar_detected(ar->hw);
962 static int ath10k_vdev_stop(struct ath10k_vif *arvif)
964 struct ath10k *ar = arvif->ar;
967 lockdep_assert_held(&ar->conf_mutex);
969 reinit_completion(&ar->vdev_setup_done);
971 ret = ath10k_wmi_vdev_stop(ar, arvif->vdev_id);
973 ath10k_warn(ar, "failed to stop WMI vdev %i: %d\n",
974 arvif->vdev_id, ret);
978 ret = ath10k_vdev_setup_sync(ar);
980 ath10k_warn(ar, "failed to syncronise setup for vdev %i: %d\n",
981 arvif->vdev_id, ret);
985 WARN_ON(ar->num_started_vdevs == 0);
987 if (ar->num_started_vdevs != 0) {
988 ar->num_started_vdevs--;
989 ath10k_recalc_radar_detection(ar);
995 static int ath10k_vdev_start_restart(struct ath10k_vif *arvif, bool restart)
997 struct ath10k *ar = arvif->ar;
998 struct cfg80211_chan_def *chandef = &ar->chandef;
999 struct wmi_vdev_start_request_arg arg = {};
1002 lockdep_assert_held(&ar->conf_mutex);
1004 reinit_completion(&ar->vdev_setup_done);
1006 arg.vdev_id = arvif->vdev_id;
1007 arg.dtim_period = arvif->dtim_period;
1008 arg.bcn_intval = arvif->beacon_interval;
1010 arg.channel.freq = chandef->chan->center_freq;
1011 arg.channel.band_center_freq1 = chandef->center_freq1;
1012 arg.channel.mode = chan_to_phymode(chandef);
1014 arg.channel.min_power = 0;
1015 arg.channel.max_power = chandef->chan->max_power * 2;
1016 arg.channel.max_reg_power = chandef->chan->max_reg_power * 2;
1017 arg.channel.max_antenna_gain = chandef->chan->max_antenna_gain * 2;
1019 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
1020 arg.ssid = arvif->u.ap.ssid;
1021 arg.ssid_len = arvif->u.ap.ssid_len;
1022 arg.hidden_ssid = arvif->u.ap.hidden_ssid;
1024 /* For now allow DFS for AP mode */
1025 arg.channel.chan_radar =
1026 !!(chandef->chan->flags & IEEE80211_CHAN_RADAR);
1027 } else if (arvif->vdev_type == WMI_VDEV_TYPE_IBSS) {
1028 arg.ssid = arvif->vif->bss_conf.ssid;
1029 arg.ssid_len = arvif->vif->bss_conf.ssid_len;
1032 ath10k_dbg(ar, ATH10K_DBG_MAC,
1033 "mac vdev %d start center_freq %d phymode %s\n",
1034 arg.vdev_id, arg.channel.freq,
1035 ath10k_wmi_phymode_str(arg.channel.mode));
1038 ret = ath10k_wmi_vdev_restart(ar, &arg);
1040 ret = ath10k_wmi_vdev_start(ar, &arg);
1043 ath10k_warn(ar, "failed to start WMI vdev %i: %d\n",
1048 ret = ath10k_vdev_setup_sync(ar);
1051 "failed to synchronize setup for vdev %i restart %d: %d\n",
1052 arg.vdev_id, restart, ret);
1056 ar->num_started_vdevs++;
1057 ath10k_recalc_radar_detection(ar);
1059 ret = ath10k_monitor_recalc(ar);
1061 ath10k_warn(ar, "mac failed to recalc monitor for vdev %i restart %d: %d\n",
1062 arg.vdev_id, restart, ret);
1063 ret2 = ath10k_vdev_stop(arvif);
1065 ath10k_warn(ar, "mac failed to stop vdev %i restart %d: %d\n",
1066 arg.vdev_id, restart, ret2);
1072 static int ath10k_vdev_start(struct ath10k_vif *arvif)
1074 return ath10k_vdev_start_restart(arvif, false);
1077 static int ath10k_vdev_restart(struct ath10k_vif *arvif)
1079 return ath10k_vdev_start_restart(arvif, true);
1082 static int ath10k_mac_setup_bcn_p2p_ie(struct ath10k_vif *arvif,
1083 struct sk_buff *bcn)
1085 struct ath10k *ar = arvif->ar;
1086 struct ieee80211_mgmt *mgmt;
1090 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1093 if (arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
1096 mgmt = (void *)bcn->data;
1097 p2p_ie = cfg80211_find_vendor_ie(WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1098 mgmt->u.beacon.variable,
1099 bcn->len - (mgmt->u.beacon.variable -
1104 ret = ath10k_wmi_p2p_go_bcn_ie(ar, arvif->vdev_id, p2p_ie);
1106 ath10k_warn(ar, "failed to submit p2p go bcn ie for vdev %i: %d\n",
1107 arvif->vdev_id, ret);
1114 static int ath10k_mac_remove_vendor_ie(struct sk_buff *skb, unsigned int oui,
1115 u8 oui_type, size_t ie_offset)
1122 if (WARN_ON(skb->len < ie_offset))
1125 ie = (u8 *)cfg80211_find_vendor_ie(oui, oui_type,
1126 skb->data + ie_offset,
1127 skb->len - ie_offset);
1132 end = skb->data + skb->len;
1135 if (WARN_ON(next > end))
1138 memmove(ie, next, end - next);
1139 skb_trim(skb, skb->len - len);
1144 static int ath10k_mac_setup_bcn_tmpl(struct ath10k_vif *arvif)
1146 struct ath10k *ar = arvif->ar;
1147 struct ieee80211_hw *hw = ar->hw;
1148 struct ieee80211_vif *vif = arvif->vif;
1149 struct ieee80211_mutable_offsets offs = {};
1150 struct sk_buff *bcn;
1153 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1156 if (arvif->vdev_type != WMI_VDEV_TYPE_AP &&
1157 arvif->vdev_type != WMI_VDEV_TYPE_IBSS)
1160 bcn = ieee80211_beacon_get_template(hw, vif, &offs);
1162 ath10k_warn(ar, "failed to get beacon template from mac80211\n");
1166 ret = ath10k_mac_setup_bcn_p2p_ie(arvif, bcn);
1168 ath10k_warn(ar, "failed to setup p2p go bcn ie: %d\n", ret);
1173 /* P2P IE is inserted by firmware automatically (as configured above)
1174 * so remove it from the base beacon template to avoid duplicate P2P
1175 * IEs in beacon frames.
1177 ath10k_mac_remove_vendor_ie(bcn, WLAN_OUI_WFA, WLAN_OUI_TYPE_WFA_P2P,
1178 offsetof(struct ieee80211_mgmt,
1179 u.beacon.variable));
1181 ret = ath10k_wmi_bcn_tmpl(ar, arvif->vdev_id, offs.tim_offset, bcn, 0,
1186 ath10k_warn(ar, "failed to submit beacon template command: %d\n",
1194 static int ath10k_mac_setup_prb_tmpl(struct ath10k_vif *arvif)
1196 struct ath10k *ar = arvif->ar;
1197 struct ieee80211_hw *hw = ar->hw;
1198 struct ieee80211_vif *vif = arvif->vif;
1199 struct sk_buff *prb;
1202 if (!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map))
1205 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1208 prb = ieee80211_proberesp_get(hw, vif);
1210 ath10k_warn(ar, "failed to get probe resp template from mac80211\n");
1214 ret = ath10k_wmi_prb_tmpl(ar, arvif->vdev_id, prb);
1218 ath10k_warn(ar, "failed to submit probe resp template command: %d\n",
1226 static void ath10k_control_beaconing(struct ath10k_vif *arvif,
1227 struct ieee80211_bss_conf *info)
1229 struct ath10k *ar = arvif->ar;
1232 lockdep_assert_held(&arvif->ar->conf_mutex);
1234 if (!info->enable_beacon) {
1235 ath10k_vdev_stop(arvif);
1237 arvif->is_started = false;
1238 arvif->is_up = false;
1240 spin_lock_bh(&arvif->ar->data_lock);
1241 ath10k_mac_vif_beacon_free(arvif);
1242 spin_unlock_bh(&arvif->ar->data_lock);
1247 arvif->tx_seq_no = 0x1000;
1249 ret = ath10k_vdev_start(arvif);
1254 ether_addr_copy(arvif->bssid, info->bssid);
1256 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
1259 ath10k_warn(ar, "failed to bring up vdev %d: %i\n",
1260 arvif->vdev_id, ret);
1261 ath10k_vdev_stop(arvif);
1265 arvif->is_started = true;
1266 arvif->is_up = true;
1268 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d up\n", arvif->vdev_id);
1271 static void ath10k_control_ibss(struct ath10k_vif *arvif,
1272 struct ieee80211_bss_conf *info,
1273 const u8 self_peer[ETH_ALEN])
1275 struct ath10k *ar = arvif->ar;
1279 lockdep_assert_held(&arvif->ar->conf_mutex);
1281 if (!info->ibss_joined) {
1282 ret = ath10k_peer_delete(arvif->ar, arvif->vdev_id, self_peer);
1284 ath10k_warn(ar, "failed to delete IBSS self peer %pM for vdev %d: %d\n",
1285 self_peer, arvif->vdev_id, ret);
1287 if (is_zero_ether_addr(arvif->bssid))
1290 memset(arvif->bssid, 0, ETH_ALEN);
1295 ret = ath10k_peer_create(arvif->ar, arvif->vdev_id, self_peer,
1296 WMI_PEER_TYPE_DEFAULT);
1298 ath10k_warn(ar, "failed to create IBSS self peer %pM for vdev %d: %d\n",
1299 self_peer, arvif->vdev_id, ret);
1303 vdev_param = arvif->ar->wmi.vdev_param->atim_window;
1304 ret = ath10k_wmi_vdev_set_param(arvif->ar, arvif->vdev_id, vdev_param,
1305 ATH10K_DEFAULT_ATIM);
1307 ath10k_warn(ar, "failed to set IBSS ATIM for vdev %d: %d\n",
1308 arvif->vdev_id, ret);
1311 static int ath10k_mac_vif_recalc_ps_wake_threshold(struct ath10k_vif *arvif)
1313 struct ath10k *ar = arvif->ar;
1318 lockdep_assert_held(&arvif->ar->conf_mutex);
1320 if (arvif->u.sta.uapsd)
1321 value = WMI_STA_PS_TX_WAKE_THRESHOLD_NEVER;
1323 value = WMI_STA_PS_TX_WAKE_THRESHOLD_ALWAYS;
1325 param = WMI_STA_PS_PARAM_TX_WAKE_THRESHOLD;
1326 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param, value);
1328 ath10k_warn(ar, "failed to submit ps wake threshold %u on vdev %i: %d\n",
1329 value, arvif->vdev_id, ret);
1336 static int ath10k_mac_vif_recalc_ps_poll_count(struct ath10k_vif *arvif)
1338 struct ath10k *ar = arvif->ar;
1343 lockdep_assert_held(&arvif->ar->conf_mutex);
1345 if (arvif->u.sta.uapsd)
1346 value = WMI_STA_PS_PSPOLL_COUNT_UAPSD;
1348 value = WMI_STA_PS_PSPOLL_COUNT_NO_MAX;
1350 param = WMI_STA_PS_PARAM_PSPOLL_COUNT;
1351 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
1354 ath10k_warn(ar, "failed to submit ps poll count %u on vdev %i: %d\n",
1355 value, arvif->vdev_id, ret);
1362 static int ath10k_mac_ps_vif_count(struct ath10k *ar)
1364 struct ath10k_vif *arvif;
1367 lockdep_assert_held(&ar->conf_mutex);
1369 list_for_each_entry(arvif, &ar->arvifs, list)
1376 static int ath10k_mac_vif_setup_ps(struct ath10k_vif *arvif)
1378 struct ath10k *ar = arvif->ar;
1379 struct ieee80211_vif *vif = arvif->vif;
1380 struct ieee80211_conf *conf = &ar->hw->conf;
1381 enum wmi_sta_powersave_param param;
1382 enum wmi_sta_ps_mode psmode;
1387 lockdep_assert_held(&arvif->ar->conf_mutex);
1389 if (arvif->vif->type != NL80211_IFTYPE_STATION)
1392 enable_ps = arvif->ps;
1394 if (enable_ps && ath10k_mac_ps_vif_count(ar) > 1 &&
1395 !test_bit(ATH10K_FW_FEATURE_MULTI_VIF_PS_SUPPORT,
1397 ath10k_warn(ar, "refusing to enable ps on vdev %i: not supported by fw\n",
1403 psmode = WMI_STA_PS_MODE_ENABLED;
1404 param = WMI_STA_PS_PARAM_INACTIVITY_TIME;
1406 ps_timeout = conf->dynamic_ps_timeout;
1407 if (ps_timeout == 0) {
1408 /* Firmware doesn't like 0 */
1409 ps_timeout = ieee80211_tu_to_usec(
1410 vif->bss_conf.beacon_int) / 1000;
1413 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id, param,
1416 ath10k_warn(ar, "failed to set inactivity time for vdev %d: %i\n",
1417 arvif->vdev_id, ret);
1421 psmode = WMI_STA_PS_MODE_DISABLED;
1424 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d psmode %s\n",
1425 arvif->vdev_id, psmode ? "enable" : "disable");
1427 ret = ath10k_wmi_set_psmode(ar, arvif->vdev_id, psmode);
1429 ath10k_warn(ar, "failed to set PS Mode %d for vdev %d: %d\n",
1430 psmode, arvif->vdev_id, ret);
1437 static int ath10k_mac_vif_disable_keepalive(struct ath10k_vif *arvif)
1439 struct ath10k *ar = arvif->ar;
1440 struct wmi_sta_keepalive_arg arg = {};
1443 lockdep_assert_held(&arvif->ar->conf_mutex);
1445 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
1448 if (!test_bit(WMI_SERVICE_STA_KEEP_ALIVE, ar->wmi.svc_map))
1451 /* Some firmware revisions have a bug and ignore the `enabled` field.
1452 * Instead use the interval to disable the keepalive.
1454 arg.vdev_id = arvif->vdev_id;
1456 arg.method = WMI_STA_KEEPALIVE_METHOD_NULL_FRAME;
1457 arg.interval = WMI_STA_KEEPALIVE_INTERVAL_DISABLE;
1459 ret = ath10k_wmi_sta_keepalive(ar, &arg);
1461 ath10k_warn(ar, "failed to submit keepalive on vdev %i: %d\n",
1462 arvif->vdev_id, ret);
1469 static void ath10k_mac_vif_ap_csa_count_down(struct ath10k_vif *arvif)
1471 struct ath10k *ar = arvif->ar;
1472 struct ieee80211_vif *vif = arvif->vif;
1475 lockdep_assert_held(&arvif->ar->conf_mutex);
1477 if (WARN_ON(!test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)))
1480 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
1483 if (!vif->csa_active)
1489 if (!ieee80211_csa_is_complete(vif)) {
1490 ieee80211_csa_update_counter(vif);
1492 ret = ath10k_mac_setup_bcn_tmpl(arvif);
1494 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
1497 ret = ath10k_mac_setup_prb_tmpl(arvif);
1499 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
1502 ieee80211_csa_finish(vif);
1506 static void ath10k_mac_vif_ap_csa_work(struct work_struct *work)
1508 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1510 struct ath10k *ar = arvif->ar;
1512 mutex_lock(&ar->conf_mutex);
1513 ath10k_mac_vif_ap_csa_count_down(arvif);
1514 mutex_unlock(&ar->conf_mutex);
1517 static void ath10k_mac_handle_beacon_iter(void *data, u8 *mac,
1518 struct ieee80211_vif *vif)
1520 struct sk_buff *skb = data;
1521 struct ieee80211_mgmt *mgmt = (void *)skb->data;
1522 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1524 if (vif->type != NL80211_IFTYPE_STATION)
1527 if (!ether_addr_equal(mgmt->bssid, vif->bss_conf.bssid))
1530 cancel_delayed_work(&arvif->connection_loss_work);
1533 void ath10k_mac_handle_beacon(struct ath10k *ar, struct sk_buff *skb)
1535 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1536 IEEE80211_IFACE_ITER_NORMAL,
1537 ath10k_mac_handle_beacon_iter,
1541 static void ath10k_mac_handle_beacon_miss_iter(void *data, u8 *mac,
1542 struct ieee80211_vif *vif)
1544 u32 *vdev_id = data;
1545 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1546 struct ath10k *ar = arvif->ar;
1547 struct ieee80211_hw *hw = ar->hw;
1549 if (arvif->vdev_id != *vdev_id)
1555 ieee80211_beacon_loss(vif);
1557 /* Firmware doesn't report beacon loss events repeatedly. If AP probe
1558 * (done by mac80211) succeeds but beacons do not resume then it
1559 * doesn't make sense to continue operation. Queue connection loss work
1560 * which can be cancelled when beacon is received.
1562 ieee80211_queue_delayed_work(hw, &arvif->connection_loss_work,
1563 ATH10K_CONNECTION_LOSS_HZ);
1566 void ath10k_mac_handle_beacon_miss(struct ath10k *ar, u32 vdev_id)
1568 ieee80211_iterate_active_interfaces_atomic(ar->hw,
1569 IEEE80211_IFACE_ITER_NORMAL,
1570 ath10k_mac_handle_beacon_miss_iter,
1574 static void ath10k_mac_vif_sta_connection_loss_work(struct work_struct *work)
1576 struct ath10k_vif *arvif = container_of(work, struct ath10k_vif,
1577 connection_loss_work.work);
1578 struct ieee80211_vif *vif = arvif->vif;
1583 ieee80211_connection_loss(vif);
1586 /**********************/
1587 /* Station management */
1588 /**********************/
1590 static u32 ath10k_peer_assoc_h_listen_intval(struct ath10k *ar,
1591 struct ieee80211_vif *vif)
1593 /* Some firmware revisions have unstable STA powersave when listen
1594 * interval is set too high (e.g. 5). The symptoms are firmware doesn't
1595 * generate NullFunc frames properly even if buffered frames have been
1596 * indicated in Beacon TIM. Firmware would seldom wake up to pull
1597 * buffered frames. Often pinging the device from AP would simply fail.
1599 * As a workaround set it to 1.
1601 if (vif->type == NL80211_IFTYPE_STATION)
1604 return ar->hw->conf.listen_interval;
1607 static void ath10k_peer_assoc_h_basic(struct ath10k *ar,
1608 struct ieee80211_vif *vif,
1609 struct ieee80211_sta *sta,
1610 struct wmi_peer_assoc_complete_arg *arg)
1612 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1614 lockdep_assert_held(&ar->conf_mutex);
1616 ether_addr_copy(arg->addr, sta->addr);
1617 arg->vdev_id = arvif->vdev_id;
1618 arg->peer_aid = sta->aid;
1619 arg->peer_flags |= WMI_PEER_AUTH;
1620 arg->peer_listen_intval = ath10k_peer_assoc_h_listen_intval(ar, vif);
1621 arg->peer_num_spatial_streams = 1;
1622 arg->peer_caps = vif->bss_conf.assoc_capability;
1625 static void ath10k_peer_assoc_h_crypto(struct ath10k *ar,
1626 struct ieee80211_vif *vif,
1627 struct wmi_peer_assoc_complete_arg *arg)
1629 struct ieee80211_bss_conf *info = &vif->bss_conf;
1630 struct cfg80211_bss *bss;
1631 const u8 *rsnie = NULL;
1632 const u8 *wpaie = NULL;
1634 lockdep_assert_held(&ar->conf_mutex);
1636 bss = cfg80211_get_bss(ar->hw->wiphy, ar->hw->conf.chandef.chan,
1637 info->bssid, NULL, 0, IEEE80211_BSS_TYPE_ANY,
1638 IEEE80211_PRIVACY_ANY);
1640 const struct cfg80211_bss_ies *ies;
1643 rsnie = ieee80211_bss_get_ie(bss, WLAN_EID_RSN);
1645 ies = rcu_dereference(bss->ies);
1647 wpaie = cfg80211_find_vendor_ie(WLAN_OUI_MICROSOFT,
1648 WLAN_OUI_TYPE_MICROSOFT_WPA,
1652 cfg80211_put_bss(ar->hw->wiphy, bss);
1655 /* FIXME: base on RSN IE/WPA IE is a correct idea? */
1656 if (rsnie || wpaie) {
1657 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: rsn ie found\n", __func__);
1658 arg->peer_flags |= WMI_PEER_NEED_PTK_4_WAY;
1662 ath10k_dbg(ar, ATH10K_DBG_WMI, "%s: wpa ie found\n", __func__);
1663 arg->peer_flags |= WMI_PEER_NEED_GTK_2_WAY;
1667 static void ath10k_peer_assoc_h_rates(struct ath10k *ar,
1668 struct ieee80211_sta *sta,
1669 struct wmi_peer_assoc_complete_arg *arg)
1671 struct wmi_rate_set_arg *rateset = &arg->peer_legacy_rates;
1672 const struct ieee80211_supported_band *sband;
1673 const struct ieee80211_rate *rates;
1677 lockdep_assert_held(&ar->conf_mutex);
1679 sband = ar->hw->wiphy->bands[ar->hw->conf.chandef.chan->band];
1680 ratemask = sta->supp_rates[ar->hw->conf.chandef.chan->band];
1681 rates = sband->bitrates;
1683 rateset->num_rates = 0;
1685 for (i = 0; i < 32; i++, ratemask >>= 1, rates++) {
1686 if (!(ratemask & 1))
1689 rateset->rates[rateset->num_rates] = rates->hw_value;
1690 rateset->num_rates++;
1694 static void ath10k_peer_assoc_h_ht(struct ath10k *ar,
1695 struct ieee80211_sta *sta,
1696 struct wmi_peer_assoc_complete_arg *arg)
1698 const struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1702 lockdep_assert_held(&ar->conf_mutex);
1704 if (!ht_cap->ht_supported)
1707 arg->peer_flags |= WMI_PEER_HT;
1708 arg->peer_max_mpdu = (1 << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1709 ht_cap->ampdu_factor)) - 1;
1711 arg->peer_mpdu_density =
1712 ath10k_parse_mpdudensity(ht_cap->ampdu_density);
1714 arg->peer_ht_caps = ht_cap->cap;
1715 arg->peer_rate_caps |= WMI_RC_HT_FLAG;
1717 if (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING)
1718 arg->peer_flags |= WMI_PEER_LDPC;
1720 if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
1721 arg->peer_flags |= WMI_PEER_40MHZ;
1722 arg->peer_rate_caps |= WMI_RC_CW40_FLAG;
1725 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_20)
1726 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1728 if (ht_cap->cap & IEEE80211_HT_CAP_SGI_40)
1729 arg->peer_rate_caps |= WMI_RC_SGI_FLAG;
1731 if (ht_cap->cap & IEEE80211_HT_CAP_TX_STBC) {
1732 arg->peer_rate_caps |= WMI_RC_TX_STBC_FLAG;
1733 arg->peer_flags |= WMI_PEER_STBC;
1736 if (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC) {
1737 stbc = ht_cap->cap & IEEE80211_HT_CAP_RX_STBC;
1738 stbc = stbc >> IEEE80211_HT_CAP_RX_STBC_SHIFT;
1739 stbc = stbc << WMI_RC_RX_STBC_FLAG_S;
1740 arg->peer_rate_caps |= stbc;
1741 arg->peer_flags |= WMI_PEER_STBC;
1744 if (ht_cap->mcs.rx_mask[1] && ht_cap->mcs.rx_mask[2])
1745 arg->peer_rate_caps |= WMI_RC_TS_FLAG;
1746 else if (ht_cap->mcs.rx_mask[1])
1747 arg->peer_rate_caps |= WMI_RC_DS_FLAG;
1749 for (i = 0, n = 0; i < IEEE80211_HT_MCS_MASK_LEN*8; i++)
1750 if (ht_cap->mcs.rx_mask[i/8] & (1 << i%8))
1751 arg->peer_ht_rates.rates[n++] = i;
1754 * This is a workaround for HT-enabled STAs which break the spec
1755 * and have no HT capabilities RX mask (no HT RX MCS map).
1757 * As per spec, in section 20.3.5 Modulation and coding scheme (MCS),
1758 * MCS 0 through 7 are mandatory in 20MHz with 800 ns GI at all STAs.
1760 * Firmware asserts if such situation occurs.
1763 arg->peer_ht_rates.num_rates = 8;
1764 for (i = 0; i < arg->peer_ht_rates.num_rates; i++)
1765 arg->peer_ht_rates.rates[i] = i;
1767 arg->peer_ht_rates.num_rates = n;
1768 arg->peer_num_spatial_streams = sta->rx_nss;
1771 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ht peer %pM mcs cnt %d nss %d\n",
1773 arg->peer_ht_rates.num_rates,
1774 arg->peer_num_spatial_streams);
1777 static int ath10k_peer_assoc_qos_ap(struct ath10k *ar,
1778 struct ath10k_vif *arvif,
1779 struct ieee80211_sta *sta)
1785 lockdep_assert_held(&ar->conf_mutex);
1787 if (sta->wme && sta->uapsd_queues) {
1788 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac uapsd_queues 0x%x max_sp %d\n",
1789 sta->uapsd_queues, sta->max_sp);
1791 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VO)
1792 uapsd |= WMI_AP_PS_UAPSD_AC3_DELIVERY_EN |
1793 WMI_AP_PS_UAPSD_AC3_TRIGGER_EN;
1794 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_VI)
1795 uapsd |= WMI_AP_PS_UAPSD_AC2_DELIVERY_EN |
1796 WMI_AP_PS_UAPSD_AC2_TRIGGER_EN;
1797 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BK)
1798 uapsd |= WMI_AP_PS_UAPSD_AC1_DELIVERY_EN |
1799 WMI_AP_PS_UAPSD_AC1_TRIGGER_EN;
1800 if (sta->uapsd_queues & IEEE80211_WMM_IE_STA_QOSINFO_AC_BE)
1801 uapsd |= WMI_AP_PS_UAPSD_AC0_DELIVERY_EN |
1802 WMI_AP_PS_UAPSD_AC0_TRIGGER_EN;
1804 if (sta->max_sp < MAX_WMI_AP_PS_PEER_PARAM_MAX_SP)
1805 max_sp = sta->max_sp;
1807 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1809 WMI_AP_PS_PEER_PARAM_UAPSD,
1812 ath10k_warn(ar, "failed to set ap ps peer param uapsd for vdev %i: %d\n",
1813 arvif->vdev_id, ret);
1817 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id,
1819 WMI_AP_PS_PEER_PARAM_MAX_SP,
1822 ath10k_warn(ar, "failed to set ap ps peer param max sp for vdev %i: %d\n",
1823 arvif->vdev_id, ret);
1827 /* TODO setup this based on STA listen interval and
1828 beacon interval. Currently we don't know
1829 sta->listen_interval - mac80211 patch required.
1830 Currently use 10 seconds */
1831 ret = ath10k_wmi_set_ap_ps_param(ar, arvif->vdev_id, sta->addr,
1832 WMI_AP_PS_PEER_PARAM_AGEOUT_TIME,
1835 ath10k_warn(ar, "failed to set ap ps peer param ageout time for vdev %i: %d\n",
1836 arvif->vdev_id, ret);
1844 static void ath10k_peer_assoc_h_vht(struct ath10k *ar,
1845 struct ieee80211_sta *sta,
1846 struct wmi_peer_assoc_complete_arg *arg)
1848 const struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
1851 if (!vht_cap->vht_supported)
1854 arg->peer_flags |= WMI_PEER_VHT;
1856 if (ar->hw->conf.chandef.chan->band == IEEE80211_BAND_2GHZ)
1857 arg->peer_flags |= WMI_PEER_VHT_2G;
1859 arg->peer_vht_caps = vht_cap->cap;
1861 ampdu_factor = (vht_cap->cap &
1862 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK) >>
1863 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
1865 /* Workaround: Some Netgear/Linksys 11ac APs set Rx A-MPDU factor to
1866 * zero in VHT IE. Using it would result in degraded throughput.
1867 * arg->peer_max_mpdu at this point contains HT max_mpdu so keep
1868 * it if VHT max_mpdu is smaller. */
1869 arg->peer_max_mpdu = max(arg->peer_max_mpdu,
1870 (1U << (IEEE80211_HT_MAX_AMPDU_FACTOR +
1871 ampdu_factor)) - 1);
1873 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1874 arg->peer_flags |= WMI_PEER_80MHZ;
1876 arg->peer_vht_rates.rx_max_rate =
1877 __le16_to_cpu(vht_cap->vht_mcs.rx_highest);
1878 arg->peer_vht_rates.rx_mcs_set =
1879 __le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map);
1880 arg->peer_vht_rates.tx_max_rate =
1881 __le16_to_cpu(vht_cap->vht_mcs.tx_highest);
1882 arg->peer_vht_rates.tx_mcs_set =
1883 __le16_to_cpu(vht_cap->vht_mcs.tx_mcs_map);
1885 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vht peer %pM max_mpdu %d flags 0x%x\n",
1886 sta->addr, arg->peer_max_mpdu, arg->peer_flags);
1889 static void ath10k_peer_assoc_h_qos(struct ath10k *ar,
1890 struct ieee80211_vif *vif,
1891 struct ieee80211_sta *sta,
1892 struct wmi_peer_assoc_complete_arg *arg)
1894 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
1896 switch (arvif->vdev_type) {
1897 case WMI_VDEV_TYPE_AP:
1899 arg->peer_flags |= WMI_PEER_QOS;
1901 if (sta->wme && sta->uapsd_queues) {
1902 arg->peer_flags |= WMI_PEER_APSD;
1903 arg->peer_rate_caps |= WMI_RC_UAPSD_FLAG;
1906 case WMI_VDEV_TYPE_STA:
1907 if (vif->bss_conf.qos)
1908 arg->peer_flags |= WMI_PEER_QOS;
1910 case WMI_VDEV_TYPE_IBSS:
1912 arg->peer_flags |= WMI_PEER_QOS;
1918 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM qos %d\n",
1919 sta->addr, !!(arg->peer_flags & WMI_PEER_QOS));
1922 static bool ath10k_mac_sta_has_11g_rates(struct ieee80211_sta *sta)
1924 /* First 4 rates in ath10k_rates are CCK (11b) rates. */
1925 return sta->supp_rates[IEEE80211_BAND_2GHZ] >> 4;
1928 static void ath10k_peer_assoc_h_phymode(struct ath10k *ar,
1929 struct ieee80211_vif *vif,
1930 struct ieee80211_sta *sta,
1931 struct wmi_peer_assoc_complete_arg *arg)
1933 enum wmi_phy_mode phymode = MODE_UNKNOWN;
1935 switch (ar->hw->conf.chandef.chan->band) {
1936 case IEEE80211_BAND_2GHZ:
1937 if (sta->vht_cap.vht_supported) {
1938 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1939 phymode = MODE_11AC_VHT40;
1941 phymode = MODE_11AC_VHT20;
1942 } else if (sta->ht_cap.ht_supported) {
1943 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1944 phymode = MODE_11NG_HT40;
1946 phymode = MODE_11NG_HT20;
1947 } else if (ath10k_mac_sta_has_11g_rates(sta)) {
1954 case IEEE80211_BAND_5GHZ:
1958 if (sta->vht_cap.vht_supported) {
1959 if (sta->bandwidth == IEEE80211_STA_RX_BW_80)
1960 phymode = MODE_11AC_VHT80;
1961 else if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1962 phymode = MODE_11AC_VHT40;
1963 else if (sta->bandwidth == IEEE80211_STA_RX_BW_20)
1964 phymode = MODE_11AC_VHT20;
1965 } else if (sta->ht_cap.ht_supported) {
1966 if (sta->bandwidth == IEEE80211_STA_RX_BW_40)
1967 phymode = MODE_11NA_HT40;
1969 phymode = MODE_11NA_HT20;
1979 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac peer %pM phymode %s\n",
1980 sta->addr, ath10k_wmi_phymode_str(phymode));
1982 arg->peer_phymode = phymode;
1983 WARN_ON(phymode == MODE_UNKNOWN);
1986 static int ath10k_peer_assoc_prepare(struct ath10k *ar,
1987 struct ieee80211_vif *vif,
1988 struct ieee80211_sta *sta,
1989 struct wmi_peer_assoc_complete_arg *arg)
1991 lockdep_assert_held(&ar->conf_mutex);
1993 memset(arg, 0, sizeof(*arg));
1995 ath10k_peer_assoc_h_basic(ar, vif, sta, arg);
1996 ath10k_peer_assoc_h_crypto(ar, vif, arg);
1997 ath10k_peer_assoc_h_rates(ar, sta, arg);
1998 ath10k_peer_assoc_h_ht(ar, sta, arg);
1999 ath10k_peer_assoc_h_vht(ar, sta, arg);
2000 ath10k_peer_assoc_h_qos(ar, vif, sta, arg);
2001 ath10k_peer_assoc_h_phymode(ar, vif, sta, arg);
2006 static const u32 ath10k_smps_map[] = {
2007 [WLAN_HT_CAP_SM_PS_STATIC] = WMI_PEER_SMPS_STATIC,
2008 [WLAN_HT_CAP_SM_PS_DYNAMIC] = WMI_PEER_SMPS_DYNAMIC,
2009 [WLAN_HT_CAP_SM_PS_INVALID] = WMI_PEER_SMPS_PS_NONE,
2010 [WLAN_HT_CAP_SM_PS_DISABLED] = WMI_PEER_SMPS_PS_NONE,
2013 static int ath10k_setup_peer_smps(struct ath10k *ar, struct ath10k_vif *arvif,
2015 const struct ieee80211_sta_ht_cap *ht_cap)
2019 if (!ht_cap->ht_supported)
2022 smps = ht_cap->cap & IEEE80211_HT_CAP_SM_PS;
2023 smps >>= IEEE80211_HT_CAP_SM_PS_SHIFT;
2025 if (smps >= ARRAY_SIZE(ath10k_smps_map))
2028 return ath10k_wmi_peer_set_param(ar, arvif->vdev_id, addr,
2029 WMI_PEER_SMPS_STATE,
2030 ath10k_smps_map[smps]);
2033 static int ath10k_mac_vif_recalc_txbf(struct ath10k *ar,
2034 struct ieee80211_vif *vif,
2035 struct ieee80211_sta_vht_cap vht_cap)
2037 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2042 if (!(ar->vht_cap_info &
2043 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2044 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE |
2045 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2046 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)))
2049 param = ar->wmi.vdev_param->txbf;
2052 if (WARN_ON(param == WMI_VDEV_PARAM_UNSUPPORTED))
2055 /* The following logic is correct. If a remote STA advertises support
2056 * for being a beamformer then we should enable us being a beamformee.
2059 if (ar->vht_cap_info &
2060 (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
2061 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
2062 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE)
2063 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2065 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)
2066 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFEE;
2069 if (ar->vht_cap_info &
2070 (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
2071 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
2072 if (vht_cap.cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE)
2073 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2075 if (vht_cap.cap & IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)
2076 value |= WMI_VDEV_PARAM_TXBF_MU_TX_BFER;
2079 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFEE)
2080 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFEE;
2082 if (value & WMI_VDEV_PARAM_TXBF_MU_TX_BFER)
2083 value |= WMI_VDEV_PARAM_TXBF_SU_TX_BFER;
2085 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, param, value);
2087 ath10k_warn(ar, "failed to submit vdev param txbf 0x%x: %d\n",
2095 /* can be called only in mac80211 callbacks due to `key_count` usage */
2096 static void ath10k_bss_assoc(struct ieee80211_hw *hw,
2097 struct ieee80211_vif *vif,
2098 struct ieee80211_bss_conf *bss_conf)
2100 struct ath10k *ar = hw->priv;
2101 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2102 struct ieee80211_sta_ht_cap ht_cap;
2103 struct ieee80211_sta_vht_cap vht_cap;
2104 struct wmi_peer_assoc_complete_arg peer_arg;
2105 struct ieee80211_sta *ap_sta;
2108 lockdep_assert_held(&ar->conf_mutex);
2110 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i assoc bssid %pM aid %d\n",
2111 arvif->vdev_id, arvif->bssid, arvif->aid);
2115 ap_sta = ieee80211_find_sta(vif, bss_conf->bssid);
2117 ath10k_warn(ar, "failed to find station entry for bss %pM vdev %i\n",
2118 bss_conf->bssid, arvif->vdev_id);
2123 /* ap_sta must be accessed only within rcu section which must be left
2124 * before calling ath10k_setup_peer_smps() which might sleep. */
2125 ht_cap = ap_sta->ht_cap;
2126 vht_cap = ap_sta->vht_cap;
2128 ret = ath10k_peer_assoc_prepare(ar, vif, ap_sta, &peer_arg);
2130 ath10k_warn(ar, "failed to prepare peer assoc for %pM vdev %i: %d\n",
2131 bss_conf->bssid, arvif->vdev_id, ret);
2138 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2140 ath10k_warn(ar, "failed to run peer assoc for %pM vdev %i: %d\n",
2141 bss_conf->bssid, arvif->vdev_id, ret);
2145 ret = ath10k_setup_peer_smps(ar, arvif, bss_conf->bssid, &ht_cap);
2147 ath10k_warn(ar, "failed to setup peer SMPS for vdev %i: %d\n",
2148 arvif->vdev_id, ret);
2152 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2154 ath10k_warn(ar, "failed to recalc txbf for vdev %i on bss %pM: %d\n",
2155 arvif->vdev_id, bss_conf->bssid, ret);
2159 ath10k_dbg(ar, ATH10K_DBG_MAC,
2160 "mac vdev %d up (associated) bssid %pM aid %d\n",
2161 arvif->vdev_id, bss_conf->bssid, bss_conf->aid);
2163 WARN_ON(arvif->is_up);
2165 arvif->aid = bss_conf->aid;
2166 ether_addr_copy(arvif->bssid, bss_conf->bssid);
2168 ret = ath10k_wmi_vdev_up(ar, arvif->vdev_id, arvif->aid, arvif->bssid);
2170 ath10k_warn(ar, "failed to set vdev %d up: %d\n",
2171 arvif->vdev_id, ret);
2175 arvif->is_up = true;
2177 /* Workaround: Some firmware revisions (tested with qca6174
2178 * WLAN.RM.2.0-00073) have buggy powersave state machine and must be
2179 * poked with peer param command.
2181 ret = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, arvif->bssid,
2182 WMI_PEER_DUMMY_VAR, 1);
2184 ath10k_warn(ar, "failed to poke peer %pM param for ps workaround on vdev %i: %d\n",
2185 arvif->bssid, arvif->vdev_id, ret);
2190 static void ath10k_bss_disassoc(struct ieee80211_hw *hw,
2191 struct ieee80211_vif *vif)
2193 struct ath10k *ar = hw->priv;
2194 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2195 struct ieee80211_sta_vht_cap vht_cap = {};
2198 lockdep_assert_held(&ar->conf_mutex);
2200 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i disassoc bssid %pM\n",
2201 arvif->vdev_id, arvif->bssid);
2203 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
2205 ath10k_warn(ar, "faield to down vdev %i: %d\n",
2206 arvif->vdev_id, ret);
2208 arvif->def_wep_key_idx = -1;
2210 ret = ath10k_mac_vif_recalc_txbf(ar, vif, vht_cap);
2212 ath10k_warn(ar, "failed to recalc txbf for vdev %i: %d\n",
2213 arvif->vdev_id, ret);
2217 arvif->is_up = false;
2219 cancel_delayed_work_sync(&arvif->connection_loss_work);
2222 static int ath10k_station_assoc(struct ath10k *ar,
2223 struct ieee80211_vif *vif,
2224 struct ieee80211_sta *sta,
2227 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2228 struct wmi_peer_assoc_complete_arg peer_arg;
2231 lockdep_assert_held(&ar->conf_mutex);
2233 ret = ath10k_peer_assoc_prepare(ar, vif, sta, &peer_arg);
2235 ath10k_warn(ar, "failed to prepare WMI peer assoc for %pM vdev %i: %i\n",
2236 sta->addr, arvif->vdev_id, ret);
2240 peer_arg.peer_reassoc = reassoc;
2241 ret = ath10k_wmi_peer_assoc(ar, &peer_arg);
2243 ath10k_warn(ar, "failed to run peer assoc for STA %pM vdev %i: %d\n",
2244 sta->addr, arvif->vdev_id, ret);
2248 /* Re-assoc is run only to update supported rates for given station. It
2249 * doesn't make much sense to reconfigure the peer completely.
2252 ret = ath10k_setup_peer_smps(ar, arvif, sta->addr,
2255 ath10k_warn(ar, "failed to setup peer SMPS for vdev %d: %d\n",
2256 arvif->vdev_id, ret);
2260 ret = ath10k_peer_assoc_qos_ap(ar, arvif, sta);
2262 ath10k_warn(ar, "failed to set qos params for STA %pM for vdev %i: %d\n",
2263 sta->addr, arvif->vdev_id, ret);
2268 arvif->num_legacy_stations++;
2269 ret = ath10k_recalc_rtscts_prot(arvif);
2271 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2272 arvif->vdev_id, ret);
2277 /* Plumb cached keys only for static WEP */
2278 if (arvif->def_wep_key_idx != -1) {
2279 ret = ath10k_install_peer_wep_keys(arvif, sta->addr);
2281 ath10k_warn(ar, "failed to install peer wep keys for vdev %i: %d\n",
2282 arvif->vdev_id, ret);
2291 static int ath10k_station_disassoc(struct ath10k *ar,
2292 struct ieee80211_vif *vif,
2293 struct ieee80211_sta *sta)
2295 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2298 lockdep_assert_held(&ar->conf_mutex);
2301 arvif->num_legacy_stations--;
2302 ret = ath10k_recalc_rtscts_prot(arvif);
2304 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
2305 arvif->vdev_id, ret);
2310 ret = ath10k_clear_peer_keys(arvif, sta->addr);
2312 ath10k_warn(ar, "failed to clear all peer wep keys for vdev %i: %d\n",
2313 arvif->vdev_id, ret);
2324 static int ath10k_update_channel_list(struct ath10k *ar)
2326 struct ieee80211_hw *hw = ar->hw;
2327 struct ieee80211_supported_band **bands;
2328 enum ieee80211_band band;
2329 struct ieee80211_channel *channel;
2330 struct wmi_scan_chan_list_arg arg = {0};
2331 struct wmi_channel_arg *ch;
2337 lockdep_assert_held(&ar->conf_mutex);
2339 bands = hw->wiphy->bands;
2340 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2344 for (i = 0; i < bands[band]->n_channels; i++) {
2345 if (bands[band]->channels[i].flags &
2346 IEEE80211_CHAN_DISABLED)
2353 len = sizeof(struct wmi_channel_arg) * arg.n_channels;
2354 arg.channels = kzalloc(len, GFP_KERNEL);
2359 for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
2363 for (i = 0; i < bands[band]->n_channels; i++) {
2364 channel = &bands[band]->channels[i];
2366 if (channel->flags & IEEE80211_CHAN_DISABLED)
2369 ch->allow_ht = true;
2371 /* FIXME: when should we really allow VHT? */
2372 ch->allow_vht = true;
2375 !(channel->flags & IEEE80211_CHAN_NO_IR);
2378 !(channel->flags & IEEE80211_CHAN_NO_HT40PLUS);
2381 !!(channel->flags & IEEE80211_CHAN_RADAR);
2383 passive = channel->flags & IEEE80211_CHAN_NO_IR;
2384 ch->passive = passive;
2386 ch->freq = channel->center_freq;
2387 ch->band_center_freq1 = channel->center_freq;
2389 ch->max_power = channel->max_power * 2;
2390 ch->max_reg_power = channel->max_reg_power * 2;
2391 ch->max_antenna_gain = channel->max_antenna_gain * 2;
2392 ch->reg_class_id = 0; /* FIXME */
2394 /* FIXME: why use only legacy modes, why not any
2395 * HT/VHT modes? Would that even make any
2397 if (channel->band == IEEE80211_BAND_2GHZ)
2398 ch->mode = MODE_11G;
2400 ch->mode = MODE_11A;
2402 if (WARN_ON_ONCE(ch->mode == MODE_UNKNOWN))
2405 ath10k_dbg(ar, ATH10K_DBG_WMI,
2406 "mac channel [%zd/%d] freq %d maxpower %d regpower %d antenna %d mode %d\n",
2407 ch - arg.channels, arg.n_channels,
2408 ch->freq, ch->max_power, ch->max_reg_power,
2409 ch->max_antenna_gain, ch->mode);
2415 ret = ath10k_wmi_scan_chan_list(ar, &arg);
2416 kfree(arg.channels);
2421 static enum wmi_dfs_region
2422 ath10k_mac_get_dfs_region(enum nl80211_dfs_regions dfs_region)
2424 switch (dfs_region) {
2425 case NL80211_DFS_UNSET:
2426 return WMI_UNINIT_DFS_DOMAIN;
2427 case NL80211_DFS_FCC:
2428 return WMI_FCC_DFS_DOMAIN;
2429 case NL80211_DFS_ETSI:
2430 return WMI_ETSI_DFS_DOMAIN;
2431 case NL80211_DFS_JP:
2432 return WMI_MKK4_DFS_DOMAIN;
2434 return WMI_UNINIT_DFS_DOMAIN;
2437 static void ath10k_regd_update(struct ath10k *ar)
2439 struct reg_dmn_pair_mapping *regpair;
2441 enum wmi_dfs_region wmi_dfs_reg;
2442 enum nl80211_dfs_regions nl_dfs_reg;
2444 lockdep_assert_held(&ar->conf_mutex);
2446 ret = ath10k_update_channel_list(ar);
2448 ath10k_warn(ar, "failed to update channel list: %d\n", ret);
2450 regpair = ar->ath_common.regulatory.regpair;
2452 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2453 nl_dfs_reg = ar->dfs_detector->region;
2454 wmi_dfs_reg = ath10k_mac_get_dfs_region(nl_dfs_reg);
2456 wmi_dfs_reg = WMI_UNINIT_DFS_DOMAIN;
2459 /* Target allows setting up per-band regdomain but ath_common provides
2460 * a combined one only */
2461 ret = ath10k_wmi_pdev_set_regdomain(ar,
2462 regpair->reg_domain,
2463 regpair->reg_domain, /* 2ghz */
2464 regpair->reg_domain, /* 5ghz */
2465 regpair->reg_2ghz_ctl,
2466 regpair->reg_5ghz_ctl,
2469 ath10k_warn(ar, "failed to set pdev regdomain: %d\n", ret);
2472 static void ath10k_reg_notifier(struct wiphy *wiphy,
2473 struct regulatory_request *request)
2475 struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
2476 struct ath10k *ar = hw->priv;
2479 ath_reg_notifier_apply(wiphy, request, &ar->ath_common.regulatory);
2481 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector) {
2482 ath10k_dbg(ar, ATH10K_DBG_REGULATORY, "dfs region 0x%x\n",
2483 request->dfs_region);
2484 result = ar->dfs_detector->set_dfs_domain(ar->dfs_detector,
2485 request->dfs_region);
2487 ath10k_warn(ar, "DFS region 0x%X not supported, will trigger radar for every pulse\n",
2488 request->dfs_region);
2491 mutex_lock(&ar->conf_mutex);
2492 if (ar->state == ATH10K_STATE_ON)
2493 ath10k_regd_update(ar);
2494 mutex_unlock(&ar->conf_mutex);
2501 static u8 ath10k_tx_h_get_tid(struct ieee80211_hdr *hdr)
2503 if (ieee80211_is_mgmt(hdr->frame_control))
2504 return HTT_DATA_TX_EXT_TID_MGMT;
2506 if (!ieee80211_is_data_qos(hdr->frame_control))
2507 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2509 if (!is_unicast_ether_addr(ieee80211_get_DA(hdr)))
2510 return HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2512 return ieee80211_get_qos_ctl(hdr)[0] & IEEE80211_QOS_CTL_TID_MASK;
2515 static u8 ath10k_tx_h_get_vdev_id(struct ath10k *ar, struct ieee80211_vif *vif)
2518 return ath10k_vif_to_arvif(vif)->vdev_id;
2520 if (ar->monitor_started)
2521 return ar->monitor_vdev_id;
2523 ath10k_warn(ar, "failed to resolve vdev id\n");
2527 static enum ath10k_hw_txrx_mode
2528 ath10k_tx_h_get_txmode(struct ath10k *ar, struct ieee80211_vif *vif,
2529 struct sk_buff *skb)
2531 const struct ieee80211_hdr *hdr = (void *)skb->data;
2532 __le16 fc = hdr->frame_control;
2534 if (!vif || vif->type == NL80211_IFTYPE_MONITOR)
2535 return ATH10K_HW_TXRX_RAW;
2537 if (ieee80211_is_mgmt(fc))
2538 return ATH10K_HW_TXRX_MGMT;
2542 * NullFunc frames are mostly used to ping if a client or AP are still
2543 * reachable and responsive. This implies tx status reports must be
2544 * accurate - otherwise either mac80211 or userspace (e.g. hostapd) can
2545 * come to a conclusion that the other end disappeared and tear down
2546 * BSS connection or it can never disconnect from BSS/client (which is
2549 * Firmware with HTT older than 3.0 delivers incorrect tx status for
2550 * NullFunc frames to driver. However there's a HTT Mgmt Tx command
2551 * which seems to deliver correct tx reports for NullFunc frames. The
2552 * downside of using it is it ignores client powersave state so it can
2553 * end up disconnecting sleeping clients in AP mode. It should fix STA
2554 * mode though because AP don't sleep.
2556 if (ar->htt.target_version_major < 3 &&
2557 (ieee80211_is_nullfunc(fc) || ieee80211_is_qos_nullfunc(fc)) &&
2558 !test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX, ar->fw_features))
2559 return ATH10K_HW_TXRX_MGMT;
2561 return ATH10K_HW_TXRX_NATIVE_WIFI;
2564 /* HTT Tx uses Native Wifi tx mode which expects 802.11 frames without QoS
2565 * Control in the header.
2567 static void ath10k_tx_h_nwifi(struct ieee80211_hw *hw, struct sk_buff *skb)
2569 struct ieee80211_hdr *hdr = (void *)skb->data;
2570 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2573 if (!ieee80211_is_data_qos(hdr->frame_control))
2576 qos_ctl = ieee80211_get_qos_ctl(hdr);
2577 memmove(skb->data + IEEE80211_QOS_CTL_LEN,
2578 skb->data, (void *)qos_ctl - (void *)skb->data);
2579 skb_pull(skb, IEEE80211_QOS_CTL_LEN);
2581 /* Some firmware revisions don't handle sending QoS NullFunc well.
2582 * These frames are mainly used for CQM purposes so it doesn't really
2583 * matter whether QoS NullFunc or NullFunc are sent.
2585 hdr = (void *)skb->data;
2586 if (ieee80211_is_qos_nullfunc(hdr->frame_control))
2587 cb->htt.tid = HTT_DATA_TX_EXT_TID_NON_QOS_MCAST_BCAST;
2589 hdr->frame_control &= ~__cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
2592 static void ath10k_tx_h_8023(struct sk_buff *skb)
2594 struct ieee80211_hdr *hdr;
2595 struct rfc1042_hdr *rfc1042;
2602 hdr = (void *)skb->data;
2603 hdrlen = ieee80211_hdrlen(hdr->frame_control);
2604 rfc1042 = (void *)skb->data + hdrlen;
2606 ether_addr_copy(da, ieee80211_get_DA(hdr));
2607 ether_addr_copy(sa, ieee80211_get_SA(hdr));
2608 type = rfc1042->snap_type;
2610 skb_pull(skb, hdrlen + sizeof(*rfc1042));
2611 skb_push(skb, sizeof(*eth));
2613 eth = (void *)skb->data;
2614 ether_addr_copy(eth->h_dest, da);
2615 ether_addr_copy(eth->h_source, sa);
2616 eth->h_proto = type;
2619 static void ath10k_tx_h_add_p2p_noa_ie(struct ath10k *ar,
2620 struct ieee80211_vif *vif,
2621 struct sk_buff *skb)
2623 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2624 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
2626 /* This is case only for P2P_GO */
2627 if (arvif->vdev_type != WMI_VDEV_TYPE_AP ||
2628 arvif->vdev_subtype != WMI_VDEV_SUBTYPE_P2P_GO)
2631 if (unlikely(ieee80211_is_probe_resp(hdr->frame_control))) {
2632 spin_lock_bh(&ar->data_lock);
2633 if (arvif->u.ap.noa_data)
2634 if (!pskb_expand_head(skb, 0, arvif->u.ap.noa_len,
2636 memcpy(skb_put(skb, arvif->u.ap.noa_len),
2637 arvif->u.ap.noa_data,
2638 arvif->u.ap.noa_len);
2639 spin_unlock_bh(&ar->data_lock);
2643 static bool ath10k_mac_need_offchan_tx_work(struct ath10k *ar)
2645 /* FIXME: Not really sure since when the behaviour changed. At some
2646 * point new firmware stopped requiring creation of peer entries for
2647 * offchannel tx (and actually creating them causes issues with wmi-htc
2648 * tx credit replenishment and reliability). Assuming it's at least 3.4
2649 * because that's when the `freq` was introduced to TX_FRM HTT command.
2651 return !(ar->htt.target_version_major >= 3 &&
2652 ar->htt.target_version_minor >= 4);
2655 static int ath10k_mac_tx_wmi_mgmt(struct ath10k *ar, struct sk_buff *skb)
2657 struct sk_buff_head *q = &ar->wmi_mgmt_tx_queue;
2660 spin_lock_bh(&ar->data_lock);
2662 if (skb_queue_len(q) == ATH10K_MAX_NUM_MGMT_PENDING) {
2663 ath10k_warn(ar, "wmi mgmt tx queue is full\n");
2668 __skb_queue_tail(q, skb);
2669 ieee80211_queue_work(ar->hw, &ar->wmi_mgmt_tx_work);
2672 spin_unlock_bh(&ar->data_lock);
2677 static void ath10k_mac_tx(struct ath10k *ar, struct sk_buff *skb)
2679 struct ath10k_skb_cb *cb = ATH10K_SKB_CB(skb);
2680 struct ath10k_htt *htt = &ar->htt;
2683 switch (cb->txmode) {
2684 case ATH10K_HW_TXRX_RAW:
2685 case ATH10K_HW_TXRX_NATIVE_WIFI:
2686 case ATH10K_HW_TXRX_ETHERNET:
2687 ret = ath10k_htt_tx(htt, skb);
2689 case ATH10K_HW_TXRX_MGMT:
2690 if (test_bit(ATH10K_FW_FEATURE_HAS_WMI_MGMT_TX,
2692 ret = ath10k_mac_tx_wmi_mgmt(ar, skb);
2693 else if (ar->htt.target_version_major >= 3)
2694 ret = ath10k_htt_tx(htt, skb);
2696 ret = ath10k_htt_mgmt_tx(htt, skb);
2701 ath10k_warn(ar, "failed to transmit packet, dropping: %d\n",
2703 ieee80211_free_txskb(ar->hw, skb);
2707 void ath10k_offchan_tx_purge(struct ath10k *ar)
2709 struct sk_buff *skb;
2712 skb = skb_dequeue(&ar->offchan_tx_queue);
2716 ieee80211_free_txskb(ar->hw, skb);
2720 void ath10k_offchan_tx_work(struct work_struct *work)
2722 struct ath10k *ar = container_of(work, struct ath10k, offchan_tx_work);
2723 struct ath10k_peer *peer;
2724 struct ieee80211_hdr *hdr;
2725 struct sk_buff *skb;
2726 const u8 *peer_addr;
2730 /* FW requirement: We must create a peer before FW will send out
2731 * an offchannel frame. Otherwise the frame will be stuck and
2732 * never transmitted. We delete the peer upon tx completion.
2733 * It is unlikely that a peer for offchannel tx will already be
2734 * present. However it may be in some rare cases so account for that.
2735 * Otherwise we might remove a legitimate peer and break stuff. */
2738 skb = skb_dequeue(&ar->offchan_tx_queue);
2742 mutex_lock(&ar->conf_mutex);
2744 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac offchannel skb %p\n",
2747 hdr = (struct ieee80211_hdr *)skb->data;
2748 peer_addr = ieee80211_get_DA(hdr);
2749 vdev_id = ATH10K_SKB_CB(skb)->vdev_id;
2751 spin_lock_bh(&ar->data_lock);
2752 peer = ath10k_peer_find(ar, vdev_id, peer_addr);
2753 spin_unlock_bh(&ar->data_lock);
2756 /* FIXME: should this use ath10k_warn()? */
2757 ath10k_dbg(ar, ATH10K_DBG_MAC, "peer %pM on vdev %d already present\n",
2758 peer_addr, vdev_id);
2761 ret = ath10k_peer_create(ar, vdev_id, peer_addr,
2762 WMI_PEER_TYPE_DEFAULT);
2764 ath10k_warn(ar, "failed to create peer %pM on vdev %d: %d\n",
2765 peer_addr, vdev_id, ret);
2768 spin_lock_bh(&ar->data_lock);
2769 reinit_completion(&ar->offchan_tx_completed);
2770 ar->offchan_tx_skb = skb;
2771 spin_unlock_bh(&ar->data_lock);
2773 ath10k_mac_tx(ar, skb);
2775 ret = wait_for_completion_timeout(&ar->offchan_tx_completed,
2778 ath10k_warn(ar, "timed out waiting for offchannel skb %p\n",
2782 ret = ath10k_peer_delete(ar, vdev_id, peer_addr);
2784 ath10k_warn(ar, "failed to delete peer %pM on vdev %d: %d\n",
2785 peer_addr, vdev_id, ret);
2788 mutex_unlock(&ar->conf_mutex);
2792 void ath10k_mgmt_over_wmi_tx_purge(struct ath10k *ar)
2794 struct sk_buff *skb;
2797 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2801 ieee80211_free_txskb(ar->hw, skb);
2805 void ath10k_mgmt_over_wmi_tx_work(struct work_struct *work)
2807 struct ath10k *ar = container_of(work, struct ath10k, wmi_mgmt_tx_work);
2808 struct sk_buff *skb;
2812 skb = skb_dequeue(&ar->wmi_mgmt_tx_queue);
2816 ret = ath10k_wmi_mgmt_tx(ar, skb);
2818 ath10k_warn(ar, "failed to transmit management frame via WMI: %d\n",
2820 ieee80211_free_txskb(ar->hw, skb);
2829 void __ath10k_scan_finish(struct ath10k *ar)
2831 lockdep_assert_held(&ar->data_lock);
2833 switch (ar->scan.state) {
2834 case ATH10K_SCAN_IDLE:
2836 case ATH10K_SCAN_RUNNING:
2837 if (ar->scan.is_roc)
2838 ieee80211_remain_on_channel_expired(ar->hw);
2840 case ATH10K_SCAN_ABORTING:
2841 if (!ar->scan.is_roc)
2842 ieee80211_scan_completed(ar->hw,
2844 ATH10K_SCAN_ABORTING));
2846 case ATH10K_SCAN_STARTING:
2847 ar->scan.state = ATH10K_SCAN_IDLE;
2848 ar->scan_channel = NULL;
2849 ath10k_offchan_tx_purge(ar);
2850 cancel_delayed_work(&ar->scan.timeout);
2851 complete_all(&ar->scan.completed);
2856 void ath10k_scan_finish(struct ath10k *ar)
2858 spin_lock_bh(&ar->data_lock);
2859 __ath10k_scan_finish(ar);
2860 spin_unlock_bh(&ar->data_lock);
2863 static int ath10k_scan_stop(struct ath10k *ar)
2865 struct wmi_stop_scan_arg arg = {
2866 .req_id = 1, /* FIXME */
2867 .req_type = WMI_SCAN_STOP_ONE,
2868 .u.scan_id = ATH10K_SCAN_ID,
2872 lockdep_assert_held(&ar->conf_mutex);
2874 ret = ath10k_wmi_stop_scan(ar, &arg);
2876 ath10k_warn(ar, "failed to stop wmi scan: %d\n", ret);
2880 ret = wait_for_completion_timeout(&ar->scan.completed, 3*HZ);
2882 ath10k_warn(ar, "failed to receive scan abortion completion: timed out\n");
2884 } else if (ret > 0) {
2889 /* Scan state should be updated upon scan completion but in case
2890 * firmware fails to deliver the event (for whatever reason) it is
2891 * desired to clean up scan state anyway. Firmware may have just
2892 * dropped the scan completion event delivery due to transport pipe
2893 * being overflown with data and/or it can recover on its own before
2894 * next scan request is submitted.
2896 spin_lock_bh(&ar->data_lock);
2897 if (ar->scan.state != ATH10K_SCAN_IDLE)
2898 __ath10k_scan_finish(ar);
2899 spin_unlock_bh(&ar->data_lock);
2904 static void ath10k_scan_abort(struct ath10k *ar)
2908 lockdep_assert_held(&ar->conf_mutex);
2910 spin_lock_bh(&ar->data_lock);
2912 switch (ar->scan.state) {
2913 case ATH10K_SCAN_IDLE:
2914 /* This can happen if timeout worker kicked in and called
2915 * abortion while scan completion was being processed.
2918 case ATH10K_SCAN_STARTING:
2919 case ATH10K_SCAN_ABORTING:
2920 ath10k_warn(ar, "refusing scan abortion due to invalid scan state: %s (%d)\n",
2921 ath10k_scan_state_str(ar->scan.state),
2924 case ATH10K_SCAN_RUNNING:
2925 ar->scan.state = ATH10K_SCAN_ABORTING;
2926 spin_unlock_bh(&ar->data_lock);
2928 ret = ath10k_scan_stop(ar);
2930 ath10k_warn(ar, "failed to abort scan: %d\n", ret);
2932 spin_lock_bh(&ar->data_lock);
2936 spin_unlock_bh(&ar->data_lock);
2939 void ath10k_scan_timeout_work(struct work_struct *work)
2941 struct ath10k *ar = container_of(work, struct ath10k,
2944 mutex_lock(&ar->conf_mutex);
2945 ath10k_scan_abort(ar);
2946 mutex_unlock(&ar->conf_mutex);
2949 static int ath10k_start_scan(struct ath10k *ar,
2950 const struct wmi_start_scan_arg *arg)
2954 lockdep_assert_held(&ar->conf_mutex);
2956 ret = ath10k_wmi_start_scan(ar, arg);
2960 ret = wait_for_completion_timeout(&ar->scan.started, 1*HZ);
2962 ret = ath10k_scan_stop(ar);
2964 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
2969 /* If we failed to start the scan, return error code at
2970 * this point. This is probably due to some issue in the
2971 * firmware, but no need to wedge the driver due to that...
2973 spin_lock_bh(&ar->data_lock);
2974 if (ar->scan.state == ATH10K_SCAN_IDLE) {
2975 spin_unlock_bh(&ar->data_lock);
2978 spin_unlock_bh(&ar->data_lock);
2980 /* Add a 200ms margin to account for event/command processing */
2981 ieee80211_queue_delayed_work(ar->hw, &ar->scan.timeout,
2982 msecs_to_jiffies(arg->max_scan_time+200));
2986 /**********************/
2987 /* mac80211 callbacks */
2988 /**********************/
2990 static void ath10k_tx(struct ieee80211_hw *hw,
2991 struct ieee80211_tx_control *control,
2992 struct sk_buff *skb)
2994 struct ath10k *ar = hw->priv;
2995 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2996 struct ieee80211_vif *vif = info->control.vif;
2997 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2998 __le16 fc = hdr->frame_control;
3000 /* We should disable CCK RATE due to P2P */
3001 if (info->flags & IEEE80211_TX_CTL_NO_CCK_RATE)
3002 ath10k_dbg(ar, ATH10K_DBG_MAC, "IEEE80211_TX_CTL_NO_CCK_RATE\n");
3004 ATH10K_SKB_CB(skb)->htt.is_offchan = false;
3005 ATH10K_SKB_CB(skb)->htt.freq = 0;
3006 ATH10K_SKB_CB(skb)->htt.tid = ath10k_tx_h_get_tid(hdr);
3007 ATH10K_SKB_CB(skb)->vdev_id = ath10k_tx_h_get_vdev_id(ar, vif);
3008 ATH10K_SKB_CB(skb)->txmode = ath10k_tx_h_get_txmode(ar, vif, skb);
3009 ATH10K_SKB_CB(skb)->is_protected = ieee80211_has_protected(fc);
3011 switch (ATH10K_SKB_CB(skb)->txmode) {
3012 case ATH10K_HW_TXRX_MGMT:
3013 case ATH10K_HW_TXRX_NATIVE_WIFI:
3014 ath10k_tx_h_nwifi(hw, skb);
3015 ath10k_tx_h_add_p2p_noa_ie(ar, vif, skb);
3016 ath10k_tx_h_seq_no(vif, skb);
3018 case ATH10K_HW_TXRX_ETHERNET:
3019 ath10k_tx_h_8023(skb);
3021 case ATH10K_HW_TXRX_RAW:
3022 /* FIXME: Packet injection isn't implemented. It should be
3023 * doable with firmware 10.2 on qca988x.
3026 ieee80211_free_txskb(hw, skb);
3030 if (info->flags & IEEE80211_TX_CTL_TX_OFFCHAN) {
3031 spin_lock_bh(&ar->data_lock);
3032 ATH10K_SKB_CB(skb)->htt.freq = ar->scan.roc_freq;
3033 ATH10K_SKB_CB(skb)->vdev_id = ar->scan.vdev_id;
3034 spin_unlock_bh(&ar->data_lock);
3036 if (ath10k_mac_need_offchan_tx_work(ar)) {
3037 ATH10K_SKB_CB(skb)->htt.freq = 0;
3038 ATH10K_SKB_CB(skb)->htt.is_offchan = true;
3040 ath10k_dbg(ar, ATH10K_DBG_MAC, "queued offchannel skb %p\n",
3043 skb_queue_tail(&ar->offchan_tx_queue, skb);
3044 ieee80211_queue_work(hw, &ar->offchan_tx_work);
3049 ath10k_mac_tx(ar, skb);
3052 /* Must not be called with conf_mutex held as workers can use that also. */
3053 void ath10k_drain_tx(struct ath10k *ar)
3055 /* make sure rcu-protected mac80211 tx path itself is drained */
3058 ath10k_offchan_tx_purge(ar);
3059 ath10k_mgmt_over_wmi_tx_purge(ar);
3061 cancel_work_sync(&ar->offchan_tx_work);
3062 cancel_work_sync(&ar->wmi_mgmt_tx_work);
3065 void ath10k_halt(struct ath10k *ar)
3067 struct ath10k_vif *arvif;
3069 lockdep_assert_held(&ar->conf_mutex);
3071 clear_bit(ATH10K_CAC_RUNNING, &ar->dev_flags);
3072 ar->filter_flags = 0;
3073 ar->monitor = false;
3075 if (ar->monitor_started)
3076 ath10k_monitor_stop(ar);
3078 ar->monitor_started = false;
3080 ath10k_scan_finish(ar);
3081 ath10k_peer_cleanup_all(ar);
3082 ath10k_core_stop(ar);
3083 ath10k_hif_power_down(ar);
3085 spin_lock_bh(&ar->data_lock);
3086 list_for_each_entry(arvif, &ar->arvifs, list)
3087 ath10k_mac_vif_beacon_cleanup(arvif);
3088 spin_unlock_bh(&ar->data_lock);
3091 static int ath10k_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
3093 struct ath10k *ar = hw->priv;
3095 mutex_lock(&ar->conf_mutex);
3097 if (ar->cfg_tx_chainmask) {
3098 *tx_ant = ar->cfg_tx_chainmask;
3099 *rx_ant = ar->cfg_rx_chainmask;
3101 *tx_ant = ar->supp_tx_chainmask;
3102 *rx_ant = ar->supp_rx_chainmask;
3105 mutex_unlock(&ar->conf_mutex);
3110 static void ath10k_check_chain_mask(struct ath10k *ar, u32 cm, const char *dbg)
3112 /* It is not clear that allowing gaps in chainmask
3113 * is helpful. Probably it will not do what user
3114 * is hoping for, so warn in that case.
3116 if (cm == 15 || cm == 7 || cm == 3 || cm == 1 || cm == 0)
3119 ath10k_warn(ar, "mac %s antenna chainmask may be invalid: 0x%x. Suggested values: 15, 7, 3, 1 or 0.\n",
3123 static int __ath10k_set_antenna(struct ath10k *ar, u32 tx_ant, u32 rx_ant)
3127 lockdep_assert_held(&ar->conf_mutex);
3129 ath10k_check_chain_mask(ar, tx_ant, "tx");
3130 ath10k_check_chain_mask(ar, rx_ant, "rx");
3132 ar->cfg_tx_chainmask = tx_ant;
3133 ar->cfg_rx_chainmask = rx_ant;
3135 if ((ar->state != ATH10K_STATE_ON) &&
3136 (ar->state != ATH10K_STATE_RESTARTED))
3139 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->tx_chain_mask,
3142 ath10k_warn(ar, "failed to set tx-chainmask: %d, req 0x%x\n",
3147 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->rx_chain_mask,
3150 ath10k_warn(ar, "failed to set rx-chainmask: %d, req 0x%x\n",
3158 static int ath10k_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
3160 struct ath10k *ar = hw->priv;
3163 mutex_lock(&ar->conf_mutex);
3164 ret = __ath10k_set_antenna(ar, tx_ant, rx_ant);
3165 mutex_unlock(&ar->conf_mutex);
3169 static int ath10k_start(struct ieee80211_hw *hw)
3171 struct ath10k *ar = hw->priv;
3175 * This makes sense only when restarting hw. It is harmless to call
3176 * uncoditionally. This is necessary to make sure no HTT/WMI tx
3177 * commands will be submitted while restarting.
3179 ath10k_drain_tx(ar);
3181 mutex_lock(&ar->conf_mutex);
3183 switch (ar->state) {
3184 case ATH10K_STATE_OFF:
3185 ar->state = ATH10K_STATE_ON;
3187 case ATH10K_STATE_RESTARTING:
3189 ar->state = ATH10K_STATE_RESTARTED;
3191 case ATH10K_STATE_ON:
3192 case ATH10K_STATE_RESTARTED:
3193 case ATH10K_STATE_WEDGED:
3197 case ATH10K_STATE_UTF:
3202 ret = ath10k_hif_power_up(ar);
3204 ath10k_err(ar, "Could not init hif: %d\n", ret);
3208 ret = ath10k_core_start(ar, ATH10K_FIRMWARE_MODE_NORMAL);
3210 ath10k_err(ar, "Could not init core: %d\n", ret);
3211 goto err_power_down;
3214 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->pmf_qos, 1);
3216 ath10k_warn(ar, "failed to enable PMF QOS: %d\n", ret);
3220 ret = ath10k_wmi_pdev_set_param(ar, ar->wmi.pdev_param->dynamic_bw, 1);
3222 ath10k_warn(ar, "failed to enable dynamic BW: %d\n", ret);
3226 if (ar->cfg_tx_chainmask)
3227 __ath10k_set_antenna(ar, ar->cfg_tx_chainmask,
3228 ar->cfg_rx_chainmask);
3231 * By default FW set ARP frames ac to voice (6). In that case ARP
3232 * exchange is not working properly for UAPSD enabled AP. ARP requests
3233 * which arrives with access category 0 are processed by network stack
3234 * and send back with access category 0, but FW changes access category
3235 * to 6. Set ARP frames access category to best effort (0) solves
3239 ret = ath10k_wmi_pdev_set_param(ar,
3240 ar->wmi.pdev_param->arp_ac_override, 0);
3242 ath10k_warn(ar, "failed to set arp ac override parameter: %d\n",
3247 ret = ath10k_wmi_pdev_set_param(ar,
3248 ar->wmi.pdev_param->ani_enable, 1);
3250 ath10k_warn(ar, "failed to enable ani by default: %d\n",
3255 ar->ani_enabled = true;
3257 ar->num_started_vdevs = 0;
3258 ath10k_regd_update(ar);
3260 ath10k_spectral_start(ar);
3261 ath10k_thermal_set_throttling(ar);
3263 mutex_unlock(&ar->conf_mutex);
3267 ath10k_core_stop(ar);
3270 ath10k_hif_power_down(ar);
3273 ar->state = ATH10K_STATE_OFF;
3276 mutex_unlock(&ar->conf_mutex);
3280 static void ath10k_stop(struct ieee80211_hw *hw)
3282 struct ath10k *ar = hw->priv;
3284 ath10k_drain_tx(ar);
3286 mutex_lock(&ar->conf_mutex);
3287 if (ar->state != ATH10K_STATE_OFF) {
3289 ar->state = ATH10K_STATE_OFF;
3291 mutex_unlock(&ar->conf_mutex);
3293 cancel_delayed_work_sync(&ar->scan.timeout);
3294 cancel_work_sync(&ar->restart_work);
3297 static int ath10k_config_ps(struct ath10k *ar)
3299 struct ath10k_vif *arvif;
3302 lockdep_assert_held(&ar->conf_mutex);
3304 list_for_each_entry(arvif, &ar->arvifs, list) {
3305 ret = ath10k_mac_vif_setup_ps(arvif);
3307 ath10k_warn(ar, "failed to setup powersave: %d\n", ret);
3315 static const char *chandef_get_width(enum nl80211_chan_width width)
3318 case NL80211_CHAN_WIDTH_20_NOHT:
3320 case NL80211_CHAN_WIDTH_20:
3322 case NL80211_CHAN_WIDTH_40:
3324 case NL80211_CHAN_WIDTH_80:
3326 case NL80211_CHAN_WIDTH_80P80:
3328 case NL80211_CHAN_WIDTH_160:
3330 case NL80211_CHAN_WIDTH_5:
3332 case NL80211_CHAN_WIDTH_10:
3338 static void ath10k_config_chan(struct ath10k *ar)
3340 struct ath10k_vif *arvif;
3343 lockdep_assert_held(&ar->conf_mutex);
3345 ath10k_dbg(ar, ATH10K_DBG_MAC,
3346 "mac config channel to %dMHz (cf1 %dMHz cf2 %dMHz width %s)\n",
3347 ar->chandef.chan->center_freq,
3348 ar->chandef.center_freq1,
3349 ar->chandef.center_freq2,
3350 chandef_get_width(ar->chandef.width));
3352 /* First stop monitor interface. Some FW versions crash if there's a
3353 * lone monitor interface. */
3354 if (ar->monitor_started)
3355 ath10k_monitor_stop(ar);
3357 list_for_each_entry(arvif, &ar->arvifs, list) {
3358 if (!arvif->is_started)
3364 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3367 ret = ath10k_wmi_vdev_down(ar, arvif->vdev_id);
3369 ath10k_warn(ar, "failed to down vdev %d: %d\n",
3370 arvif->vdev_id, ret);
3375 /* all vdevs are downed now - attempt to restart and re-up them */
3377 list_for_each_entry(arvif, &ar->arvifs, list) {
3378 if (!arvif->is_started)
3381 if (arvif->vdev_type == WMI_VDEV_TYPE_MONITOR)
3384 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3386 ath10k_warn(ar, "failed to update bcn tmpl during csa: %d\n",
3389 ret = ath10k_mac_setup_prb_tmpl(arvif);
3391 ath10k_warn(ar, "failed to update prb tmpl during csa: %d\n",
3394 ret = ath10k_vdev_restart(arvif);
3396 ath10k_warn(ar, "failed to restart vdev %d: %d\n",
3397 arvif->vdev_id, ret);
3404 ret = ath10k_wmi_vdev_up(arvif->ar, arvif->vdev_id, arvif->aid,
3407 ath10k_warn(ar, "failed to bring vdev up %d: %d\n",
3408 arvif->vdev_id, ret);
3413 ath10k_monitor_recalc(ar);
3416 static int ath10k_mac_txpower_setup(struct ath10k *ar, int txpower)
3421 lockdep_assert_held(&ar->conf_mutex);
3423 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac txpower %d\n", txpower);
3425 param = ar->wmi.pdev_param->txpower_limit2g;
3426 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3428 ath10k_warn(ar, "failed to set 2g txpower %d: %d\n",
3433 param = ar->wmi.pdev_param->txpower_limit5g;
3434 ret = ath10k_wmi_pdev_set_param(ar, param, txpower * 2);
3436 ath10k_warn(ar, "failed to set 5g txpower %d: %d\n",
3444 static int ath10k_mac_txpower_recalc(struct ath10k *ar)
3446 struct ath10k_vif *arvif;
3447 int ret, txpower = -1;
3449 lockdep_assert_held(&ar->conf_mutex);
3451 list_for_each_entry(arvif, &ar->arvifs, list) {
3452 WARN_ON(arvif->txpower < 0);
3455 txpower = arvif->txpower;
3457 txpower = min(txpower, arvif->txpower);
3460 if (WARN_ON(txpower == -1))
3463 ret = ath10k_mac_txpower_setup(ar, txpower);
3465 ath10k_warn(ar, "failed to setup tx power %d: %d\n",
3473 static int ath10k_config(struct ieee80211_hw *hw, u32 changed)
3475 struct ath10k *ar = hw->priv;
3476 struct ieee80211_conf *conf = &hw->conf;
3479 mutex_lock(&ar->conf_mutex);
3481 if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
3482 ath10k_dbg(ar, ATH10K_DBG_MAC,
3483 "mac config channel %dMHz flags 0x%x radar %d\n",
3484 conf->chandef.chan->center_freq,
3485 conf->chandef.chan->flags,
3486 conf->radar_enabled);
3488 spin_lock_bh(&ar->data_lock);
3489 ar->rx_channel = conf->chandef.chan;
3490 spin_unlock_bh(&ar->data_lock);
3492 ar->radar_enabled = conf->radar_enabled;
3493 ath10k_recalc_radar_detection(ar);
3495 if (!cfg80211_chandef_identical(&ar->chandef, &conf->chandef)) {
3496 ar->chandef = conf->chandef;
3497 ath10k_config_chan(ar);
3501 if (changed & IEEE80211_CONF_CHANGE_PS)
3502 ath10k_config_ps(ar);
3504 if (changed & IEEE80211_CONF_CHANGE_MONITOR) {
3505 ar->monitor = conf->flags & IEEE80211_CONF_MONITOR;
3506 ret = ath10k_monitor_recalc(ar);
3508 ath10k_warn(ar, "failed to recalc monitor: %d\n", ret);
3511 mutex_unlock(&ar->conf_mutex);
3515 static u32 get_nss_from_chainmask(u16 chain_mask)
3517 if ((chain_mask & 0x15) == 0x15)
3519 else if ((chain_mask & 0x7) == 0x7)
3521 else if ((chain_mask & 0x3) == 0x3)
3528 * Figure out how to handle WMI_VDEV_SUBTYPE_P2P_DEVICE,
3529 * because we will send mgmt frames without CCK. This requirement
3530 * for P2P_FIND/GO_NEG should be handled by checking CCK flag
3533 static int ath10k_add_interface(struct ieee80211_hw *hw,
3534 struct ieee80211_vif *vif)
3536 struct ath10k *ar = hw->priv;
3537 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3538 enum wmi_sta_powersave_param param;
3544 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3546 mutex_lock(&ar->conf_mutex);
3548 memset(arvif, 0, sizeof(*arvif));
3553 INIT_LIST_HEAD(&arvif->list);
3554 INIT_WORK(&arvif->ap_csa_work, ath10k_mac_vif_ap_csa_work);
3555 INIT_DELAYED_WORK(&arvif->connection_loss_work,
3556 ath10k_mac_vif_sta_connection_loss_work);
3558 if (ar->free_vdev_map == 0) {
3559 ath10k_warn(ar, "Free vdev map is empty, no more interfaces allowed.\n");
3563 bit = __ffs64(ar->free_vdev_map);
3565 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac create vdev %i map %llx\n",
3566 bit, ar->free_vdev_map);
3568 arvif->vdev_id = bit;
3569 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_NONE;
3571 switch (vif->type) {
3572 case NL80211_IFTYPE_P2P_DEVICE:
3573 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3574 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_DEVICE;
3576 case NL80211_IFTYPE_UNSPECIFIED:
3577 case NL80211_IFTYPE_STATION:
3578 arvif->vdev_type = WMI_VDEV_TYPE_STA;
3580 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_CLIENT;
3582 case NL80211_IFTYPE_ADHOC:
3583 arvif->vdev_type = WMI_VDEV_TYPE_IBSS;
3585 case NL80211_IFTYPE_AP:
3586 arvif->vdev_type = WMI_VDEV_TYPE_AP;
3589 arvif->vdev_subtype = WMI_VDEV_SUBTYPE_P2P_GO;
3591 case NL80211_IFTYPE_MONITOR:
3592 arvif->vdev_type = WMI_VDEV_TYPE_MONITOR;
3599 /* Some firmware revisions don't wait for beacon tx completion before
3600 * sending another SWBA event. This could lead to hardware using old
3601 * (freed) beacon data in some cases, e.g. tx credit starvation
3602 * combined with missed TBTT. This is very very rare.
3604 * On non-IOMMU-enabled hosts this could be a possible security issue
3605 * because hw could beacon some random data on the air. On
3606 * IOMMU-enabled hosts DMAR faults would occur in most cases and target
3607 * device would crash.
3609 * Since there are no beacon tx completions (implicit nor explicit)
3610 * propagated to host the only workaround for this is to allocate a
3611 * DMA-coherent buffer for a lifetime of a vif and use it for all
3612 * beacon tx commands. Worst case for this approach is some beacons may
3613 * become corrupted, e.g. have garbled IEs or out-of-date TIM bitmap.
3615 if (vif->type == NL80211_IFTYPE_ADHOC ||
3616 vif->type == NL80211_IFTYPE_AP) {
3617 arvif->beacon_buf = dma_zalloc_coherent(ar->dev,
3618 IEEE80211_MAX_FRAME_LEN,
3619 &arvif->beacon_paddr,
3621 if (!arvif->beacon_buf) {
3623 ath10k_warn(ar, "failed to allocate beacon buffer: %d\n",
3629 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev create %d (add interface) type %d subtype %d bcnmode %s\n",
3630 arvif->vdev_id, arvif->vdev_type, arvif->vdev_subtype,
3631 arvif->beacon_buf ? "single-buf" : "per-skb");
3633 ret = ath10k_wmi_vdev_create(ar, arvif->vdev_id, arvif->vdev_type,
3634 arvif->vdev_subtype, vif->addr);
3636 ath10k_warn(ar, "failed to create WMI vdev %i: %d\n",
3637 arvif->vdev_id, ret);
3641 ar->free_vdev_map &= ~(1LL << arvif->vdev_id);
3642 list_add(&arvif->list, &ar->arvifs);
3644 /* It makes no sense to have firmware do keepalives. mac80211 already
3645 * takes care of this with idle connection polling.
3647 ret = ath10k_mac_vif_disable_keepalive(arvif);
3649 ath10k_warn(ar, "failed to disable keepalive on vdev %i: %d\n",
3650 arvif->vdev_id, ret);
3651 goto err_vdev_delete;
3654 arvif->def_wep_key_idx = -1;
3656 vdev_param = ar->wmi.vdev_param->tx_encap_type;
3657 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3658 ATH10K_HW_TXRX_NATIVE_WIFI);
3659 /* 10.X firmware does not support this VDEV parameter. Do not warn */
3660 if (ret && ret != -EOPNOTSUPP) {
3661 ath10k_warn(ar, "failed to set vdev %i TX encapsulation: %d\n",
3662 arvif->vdev_id, ret);
3663 goto err_vdev_delete;
3666 if (ar->cfg_tx_chainmask) {
3667 u16 nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
3669 vdev_param = ar->wmi.vdev_param->nss;
3670 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3673 ath10k_warn(ar, "failed to set vdev %i chainmask 0x%x, nss %i: %d\n",
3674 arvif->vdev_id, ar->cfg_tx_chainmask, nss,
3676 goto err_vdev_delete;
3680 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3681 ret = ath10k_peer_create(ar, arvif->vdev_id, vif->addr,
3682 WMI_PEER_TYPE_DEFAULT);
3684 ath10k_warn(ar, "failed to create vdev %i peer for AP: %d\n",
3685 arvif->vdev_id, ret);
3686 goto err_vdev_delete;
3689 ret = ath10k_mac_set_kickout(arvif);
3691 ath10k_warn(ar, "failed to set vdev %i kickout parameters: %d\n",
3692 arvif->vdev_id, ret);
3693 goto err_peer_delete;
3697 if (arvif->vdev_type == WMI_VDEV_TYPE_STA) {
3698 param = WMI_STA_PS_PARAM_RX_WAKE_POLICY;
3699 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
3700 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
3703 ath10k_warn(ar, "failed to set vdev %i RX wake policy: %d\n",
3704 arvif->vdev_id, ret);
3705 goto err_peer_delete;
3708 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
3710 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
3711 arvif->vdev_id, ret);
3712 goto err_peer_delete;
3715 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
3717 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
3718 arvif->vdev_id, ret);
3719 goto err_peer_delete;
3723 ret = ath10k_mac_set_rts(arvif, ar->hw->wiphy->rts_threshold);
3725 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
3726 arvif->vdev_id, ret);
3727 goto err_peer_delete;
3730 ret = ath10k_mac_set_frag(arvif, ar->hw->wiphy->frag_threshold);
3732 ath10k_warn(ar, "failed to set frag threshold for vdev %d: %d\n",
3733 arvif->vdev_id, ret);
3734 goto err_peer_delete;
3737 arvif->txpower = vif->bss_conf.txpower;
3738 ret = ath10k_mac_txpower_recalc(ar);
3740 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
3741 goto err_peer_delete;
3744 mutex_unlock(&ar->conf_mutex);
3748 if (arvif->vdev_type == WMI_VDEV_TYPE_AP)
3749 ath10k_wmi_peer_delete(ar, arvif->vdev_id, vif->addr);
3752 ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3753 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3754 list_del(&arvif->list);
3757 if (arvif->beacon_buf) {
3758 dma_free_coherent(ar->dev, IEEE80211_MAX_FRAME_LEN,
3759 arvif->beacon_buf, arvif->beacon_paddr);
3760 arvif->beacon_buf = NULL;
3763 mutex_unlock(&ar->conf_mutex);
3768 static void ath10k_remove_interface(struct ieee80211_hw *hw,
3769 struct ieee80211_vif *vif)
3771 struct ath10k *ar = hw->priv;
3772 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3775 cancel_work_sync(&arvif->ap_csa_work);
3776 cancel_delayed_work_sync(&arvif->connection_loss_work);
3778 mutex_lock(&ar->conf_mutex);
3780 spin_lock_bh(&ar->data_lock);
3781 ath10k_mac_vif_beacon_cleanup(arvif);
3782 spin_unlock_bh(&ar->data_lock);
3784 ret = ath10k_spectral_vif_stop(arvif);
3786 ath10k_warn(ar, "failed to stop spectral for vdev %i: %d\n",
3787 arvif->vdev_id, ret);
3789 ar->free_vdev_map |= 1LL << arvif->vdev_id;
3790 list_del(&arvif->list);
3792 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3793 ret = ath10k_wmi_peer_delete(arvif->ar, arvif->vdev_id,
3796 ath10k_warn(ar, "failed to submit AP self-peer removal on vdev %i: %d\n",
3797 arvif->vdev_id, ret);
3799 kfree(arvif->u.ap.noa_data);
3802 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %i delete (remove interface)\n",
3805 ret = ath10k_wmi_vdev_delete(ar, arvif->vdev_id);
3807 ath10k_warn(ar, "failed to delete WMI vdev %i: %d\n",
3808 arvif->vdev_id, ret);
3810 /* Some firmware revisions don't notify host about self-peer removal
3811 * until after associated vdev is deleted.
3813 if (arvif->vdev_type == WMI_VDEV_TYPE_AP) {
3814 ret = ath10k_wait_for_peer_deleted(ar, arvif->vdev_id,
3817 ath10k_warn(ar, "failed to remove AP self-peer on vdev %i: %d\n",
3818 arvif->vdev_id, ret);
3820 spin_lock_bh(&ar->data_lock);
3822 spin_unlock_bh(&ar->data_lock);
3825 ath10k_peer_cleanup(ar, arvif->vdev_id);
3827 mutex_unlock(&ar->conf_mutex);
3831 * FIXME: Has to be verified.
3833 #define SUPPORTED_FILTERS \
3834 (FIF_PROMISC_IN_BSS | \
3839 FIF_BCN_PRBRESP_PROMISC | \
3843 static void ath10k_configure_filter(struct ieee80211_hw *hw,
3844 unsigned int changed_flags,
3845 unsigned int *total_flags,
3848 struct ath10k *ar = hw->priv;
3851 mutex_lock(&ar->conf_mutex);
3853 changed_flags &= SUPPORTED_FILTERS;
3854 *total_flags &= SUPPORTED_FILTERS;
3855 ar->filter_flags = *total_flags;
3857 ret = ath10k_monitor_recalc(ar);
3859 ath10k_warn(ar, "failed to recalc montior: %d\n", ret);
3861 mutex_unlock(&ar->conf_mutex);
3864 static void ath10k_bss_info_changed(struct ieee80211_hw *hw,
3865 struct ieee80211_vif *vif,
3866 struct ieee80211_bss_conf *info,
3869 struct ath10k *ar = hw->priv;
3870 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
3872 u32 vdev_param, pdev_param, slottime, preamble;
3874 mutex_lock(&ar->conf_mutex);
3876 if (changed & BSS_CHANGED_IBSS)
3877 ath10k_control_ibss(arvif, info, vif->addr);
3879 if (changed & BSS_CHANGED_BEACON_INT) {
3880 arvif->beacon_interval = info->beacon_int;
3881 vdev_param = ar->wmi.vdev_param->beacon_interval;
3882 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3883 arvif->beacon_interval);
3884 ath10k_dbg(ar, ATH10K_DBG_MAC,
3885 "mac vdev %d beacon_interval %d\n",
3886 arvif->vdev_id, arvif->beacon_interval);
3889 ath10k_warn(ar, "failed to set beacon interval for vdev %d: %i\n",
3890 arvif->vdev_id, ret);
3893 if (changed & BSS_CHANGED_BEACON) {
3894 ath10k_dbg(ar, ATH10K_DBG_MAC,
3895 "vdev %d set beacon tx mode to staggered\n",
3898 pdev_param = ar->wmi.pdev_param->beacon_tx_mode;
3899 ret = ath10k_wmi_pdev_set_param(ar, pdev_param,
3900 WMI_BEACON_STAGGERED_MODE);
3902 ath10k_warn(ar, "failed to set beacon mode for vdev %d: %i\n",
3903 arvif->vdev_id, ret);
3905 ret = ath10k_mac_setup_bcn_tmpl(arvif);
3907 ath10k_warn(ar, "failed to update beacon template: %d\n",
3911 if (changed & BSS_CHANGED_AP_PROBE_RESP) {
3912 ret = ath10k_mac_setup_prb_tmpl(arvif);
3914 ath10k_warn(ar, "failed to setup probe resp template on vdev %i: %d\n",
3915 arvif->vdev_id, ret);
3918 if (changed & (BSS_CHANGED_BEACON_INFO | BSS_CHANGED_BEACON)) {
3919 arvif->dtim_period = info->dtim_period;
3921 ath10k_dbg(ar, ATH10K_DBG_MAC,
3922 "mac vdev %d dtim_period %d\n",
3923 arvif->vdev_id, arvif->dtim_period);
3925 vdev_param = ar->wmi.vdev_param->dtim_period;
3926 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3927 arvif->dtim_period);
3929 ath10k_warn(ar, "failed to set dtim period for vdev %d: %i\n",
3930 arvif->vdev_id, ret);
3933 if (changed & BSS_CHANGED_SSID &&
3934 vif->type == NL80211_IFTYPE_AP) {
3935 arvif->u.ap.ssid_len = info->ssid_len;
3937 memcpy(arvif->u.ap.ssid, info->ssid, info->ssid_len);
3938 arvif->u.ap.hidden_ssid = info->hidden_ssid;
3941 if (changed & BSS_CHANGED_BSSID && !is_zero_ether_addr(info->bssid))
3942 ether_addr_copy(arvif->bssid, info->bssid);
3944 if (changed & BSS_CHANGED_BEACON_ENABLED)
3945 ath10k_control_beaconing(arvif, info);
3947 if (changed & BSS_CHANGED_ERP_CTS_PROT) {
3948 arvif->use_cts_prot = info->use_cts_prot;
3949 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d cts_prot %d\n",
3950 arvif->vdev_id, info->use_cts_prot);
3952 ret = ath10k_recalc_rtscts_prot(arvif);
3954 ath10k_warn(ar, "failed to recalculate rts/cts prot for vdev %d: %d\n",
3955 arvif->vdev_id, ret);
3957 vdev_param = ar->wmi.vdev_param->protection_mode;
3958 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3959 info->use_cts_prot ? 1 : 0);
3961 ath10k_warn(ar, "failed to set protection mode %d on vdev %i: %d\n",
3962 info->use_cts_prot, arvif->vdev_id, ret);
3965 if (changed & BSS_CHANGED_ERP_SLOT) {
3966 if (info->use_short_slot)
3967 slottime = WMI_VDEV_SLOT_TIME_SHORT; /* 9us */
3970 slottime = WMI_VDEV_SLOT_TIME_LONG; /* 20us */
3972 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d slot_time %d\n",
3973 arvif->vdev_id, slottime);
3975 vdev_param = ar->wmi.vdev_param->slot_time;
3976 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3979 ath10k_warn(ar, "failed to set erp slot for vdev %d: %i\n",
3980 arvif->vdev_id, ret);
3983 if (changed & BSS_CHANGED_ERP_PREAMBLE) {
3984 if (info->use_short_preamble)
3985 preamble = WMI_VDEV_PREAMBLE_SHORT;
3987 preamble = WMI_VDEV_PREAMBLE_LONG;
3989 ath10k_dbg(ar, ATH10K_DBG_MAC,
3990 "mac vdev %d preamble %dn",
3991 arvif->vdev_id, preamble);
3993 vdev_param = ar->wmi.vdev_param->preamble;
3994 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
3997 ath10k_warn(ar, "failed to set preamble for vdev %d: %i\n",
3998 arvif->vdev_id, ret);
4001 if (changed & BSS_CHANGED_ASSOC) {
4003 /* Workaround: Make sure monitor vdev is not running
4004 * when associating to prevent some firmware revisions
4005 * (e.g. 10.1 and 10.2) from crashing.
4007 if (ar->monitor_started)
4008 ath10k_monitor_stop(ar);
4009 ath10k_bss_assoc(hw, vif, info);
4010 ath10k_monitor_recalc(ar);
4012 ath10k_bss_disassoc(hw, vif);
4016 if (changed & BSS_CHANGED_TXPOWER) {
4017 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev_id %i txpower %d\n",
4018 arvif->vdev_id, info->txpower);
4020 arvif->txpower = info->txpower;
4021 ret = ath10k_mac_txpower_recalc(ar);
4023 ath10k_warn(ar, "failed to recalc tx power: %d\n", ret);
4026 if (changed & BSS_CHANGED_PS) {
4027 arvif->ps = vif->bss_conf.ps;
4029 ret = ath10k_config_ps(ar);
4031 ath10k_warn(ar, "failed to setup ps on vdev %i: %d\n",
4032 arvif->vdev_id, ret);
4035 mutex_unlock(&ar->conf_mutex);
4038 static int ath10k_hw_scan(struct ieee80211_hw *hw,
4039 struct ieee80211_vif *vif,
4040 struct ieee80211_scan_request *hw_req)
4042 struct ath10k *ar = hw->priv;
4043 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4044 struct cfg80211_scan_request *req = &hw_req->req;
4045 struct wmi_start_scan_arg arg;
4049 mutex_lock(&ar->conf_mutex);
4051 spin_lock_bh(&ar->data_lock);
4052 switch (ar->scan.state) {
4053 case ATH10K_SCAN_IDLE:
4054 reinit_completion(&ar->scan.started);
4055 reinit_completion(&ar->scan.completed);
4056 ar->scan.state = ATH10K_SCAN_STARTING;
4057 ar->scan.is_roc = false;
4058 ar->scan.vdev_id = arvif->vdev_id;
4061 case ATH10K_SCAN_STARTING:
4062 case ATH10K_SCAN_RUNNING:
4063 case ATH10K_SCAN_ABORTING:
4067 spin_unlock_bh(&ar->data_lock);
4072 memset(&arg, 0, sizeof(arg));
4073 ath10k_wmi_start_scan_init(ar, &arg);
4074 arg.vdev_id = arvif->vdev_id;
4075 arg.scan_id = ATH10K_SCAN_ID;
4078 arg.scan_ctrl_flags |= WMI_SCAN_ADD_CCK_RATES;
4081 arg.ie_len = req->ie_len;
4082 memcpy(arg.ie, req->ie, arg.ie_len);
4086 arg.n_ssids = req->n_ssids;
4087 for (i = 0; i < arg.n_ssids; i++) {
4088 arg.ssids[i].len = req->ssids[i].ssid_len;
4089 arg.ssids[i].ssid = req->ssids[i].ssid;
4092 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4095 if (req->n_channels) {
4096 arg.n_channels = req->n_channels;
4097 for (i = 0; i < arg.n_channels; i++)
4098 arg.channels[i] = req->channels[i]->center_freq;
4101 ret = ath10k_start_scan(ar, &arg);
4103 ath10k_warn(ar, "failed to start hw scan: %d\n", ret);
4104 spin_lock_bh(&ar->data_lock);
4105 ar->scan.state = ATH10K_SCAN_IDLE;
4106 spin_unlock_bh(&ar->data_lock);
4110 mutex_unlock(&ar->conf_mutex);
4114 static void ath10k_cancel_hw_scan(struct ieee80211_hw *hw,
4115 struct ieee80211_vif *vif)
4117 struct ath10k *ar = hw->priv;
4119 mutex_lock(&ar->conf_mutex);
4120 ath10k_scan_abort(ar);
4121 mutex_unlock(&ar->conf_mutex);
4123 cancel_delayed_work_sync(&ar->scan.timeout);
4126 static void ath10k_set_key_h_def_keyidx(struct ath10k *ar,
4127 struct ath10k_vif *arvif,
4128 enum set_key_cmd cmd,
4129 struct ieee80211_key_conf *key)
4131 u32 vdev_param = arvif->ar->wmi.vdev_param->def_keyid;
4134 /* 10.1 firmware branch requires default key index to be set to group
4135 * key index after installing it. Otherwise FW/HW Txes corrupted
4136 * frames with multi-vif APs. This is not required for main firmware
4137 * branch (e.g. 636).
4139 * FIXME: This has been tested only in AP. It remains unknown if this
4140 * is required for multi-vif STA interfaces on 10.1 */
4142 if (arvif->vdev_type != WMI_VDEV_TYPE_AP)
4145 if (key->cipher == WLAN_CIPHER_SUITE_WEP40)
4148 if (key->cipher == WLAN_CIPHER_SUITE_WEP104)
4151 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4157 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
4160 ath10k_warn(ar, "failed to set vdev %i group key as default key: %d\n",
4161 arvif->vdev_id, ret);
4164 static int ath10k_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4165 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4166 struct ieee80211_key_conf *key)
4168 struct ath10k *ar = hw->priv;
4169 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4170 struct ath10k_peer *peer;
4171 const u8 *peer_addr;
4172 bool is_wep = key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
4173 key->cipher == WLAN_CIPHER_SUITE_WEP104;
4177 /* this one needs to be done in software */
4178 if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
4181 if (key->keyidx > WMI_MAX_KEY_INDEX)
4184 mutex_lock(&ar->conf_mutex);
4187 peer_addr = sta->addr;
4188 else if (arvif->vdev_type == WMI_VDEV_TYPE_STA)
4189 peer_addr = vif->bss_conf.bssid;
4191 peer_addr = vif->addr;
4193 key->hw_key_idx = key->keyidx;
4195 /* the peer should not disappear in mid-way (unless FW goes awry) since
4196 * we already hold conf_mutex. we just make sure its there now. */
4197 spin_lock_bh(&ar->data_lock);
4198 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4199 spin_unlock_bh(&ar->data_lock);
4202 if (cmd == SET_KEY) {
4203 ath10k_warn(ar, "failed to install key for non-existent peer %pM\n",
4208 /* if the peer doesn't exist there is no key to disable
4214 if (key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4215 flags |= WMI_KEY_PAIRWISE;
4217 flags |= WMI_KEY_GROUP;
4221 arvif->wep_keys[key->keyidx] = key;
4223 arvif->wep_keys[key->keyidx] = NULL;
4225 if (cmd == DISABLE_KEY)
4226 ath10k_clear_vdev_key(arvif, key);
4228 /* When WEP keys are uploaded it's possible that there are
4229 * stations associated already (e.g. when merging) without any
4230 * keys. Static WEP needs an explicit per-peer key upload.
4232 if (vif->type == NL80211_IFTYPE_ADHOC &&
4234 ath10k_mac_vif_update_wep_key(arvif, key);
4236 /* 802.1x never sets the def_wep_key_idx so each set_key()
4237 * call changes default tx key.
4239 * Static WEP sets def_wep_key_idx via .set_default_unicast_key
4240 * after first set_key().
4242 if (cmd == SET_KEY && arvif->def_wep_key_idx == -1)
4243 flags |= WMI_KEY_TX_USAGE;
4245 /* mac80211 uploads static WEP keys as groupwise while fw/hw
4246 * requires pairwise keys for non-self peers, i.e. BSSID in STA
4247 * mode and associated stations in AP/IBSS.
4249 * Static WEP keys for peer_addr=vif->addr and 802.1X WEP keys
4250 * work fine when mapped directly from mac80211.
4252 * Note: When installing first static WEP groupwise key (which
4253 * should be pairwise) def_wep_key_idx isn't known yet (it's
4254 * equal to -1). Since .set_default_unicast_key is called only
4255 * for static WEP it's used to re-upload the key as pairwise.
4257 if (arvif->def_wep_key_idx >= 0 &&
4258 memcmp(peer_addr, arvif->vif->addr, ETH_ALEN)) {
4259 flags &= ~WMI_KEY_GROUP;
4260 flags |= WMI_KEY_PAIRWISE;
4264 ret = ath10k_install_key(arvif, key, cmd, peer_addr, flags);
4266 ath10k_warn(ar, "failed to install key for vdev %i peer %pM: %d\n",
4267 arvif->vdev_id, peer_addr, ret);
4271 ath10k_set_key_h_def_keyidx(ar, arvif, cmd, key);
4273 spin_lock_bh(&ar->data_lock);
4274 peer = ath10k_peer_find(ar, arvif->vdev_id, peer_addr);
4275 if (peer && cmd == SET_KEY)
4276 peer->keys[key->keyidx] = key;
4277 else if (peer && cmd == DISABLE_KEY)
4278 peer->keys[key->keyidx] = NULL;
4279 else if (peer == NULL)
4280 /* impossible unless FW goes crazy */
4281 ath10k_warn(ar, "Peer %pM disappeared!\n", peer_addr);
4282 spin_unlock_bh(&ar->data_lock);
4285 mutex_unlock(&ar->conf_mutex);
4289 static void ath10k_set_default_unicast_key(struct ieee80211_hw *hw,
4290 struct ieee80211_vif *vif,
4293 struct ath10k *ar = hw->priv;
4294 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4297 mutex_lock(&arvif->ar->conf_mutex);
4299 if (arvif->ar->state != ATH10K_STATE_ON)
4302 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d set keyidx %d\n",
4303 arvif->vdev_id, keyidx);
4305 ret = ath10k_wmi_vdev_set_param(arvif->ar,
4307 arvif->ar->wmi.vdev_param->def_keyid,
4311 ath10k_warn(ar, "failed to update wep key index for vdev %d: %d\n",
4317 arvif->def_wep_key_idx = keyidx;
4319 ret = ath10k_mac_vif_sta_fix_wep_key(arvif);
4321 ath10k_warn(ar, "failed to fix sta wep key on vdev %i: %d\n",
4322 arvif->vdev_id, ret);
4327 mutex_unlock(&arvif->ar->conf_mutex);
4330 static void ath10k_sta_rc_update_wk(struct work_struct *wk)
4333 struct ath10k_vif *arvif;
4334 struct ath10k_sta *arsta;
4335 struct ieee80211_sta *sta;
4336 u32 changed, bw, nss, smps;
4339 arsta = container_of(wk, struct ath10k_sta, update_wk);
4340 sta = container_of((void *)arsta, struct ieee80211_sta, drv_priv);
4341 arvif = arsta->arvif;
4344 spin_lock_bh(&ar->data_lock);
4346 changed = arsta->changed;
4353 spin_unlock_bh(&ar->data_lock);
4355 mutex_lock(&ar->conf_mutex);
4357 if (changed & IEEE80211_RC_BW_CHANGED) {
4358 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM peer bw %d\n",
4361 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4362 WMI_PEER_CHAN_WIDTH, bw);
4364 ath10k_warn(ar, "failed to update STA %pM peer bw %d: %d\n",
4365 sta->addr, bw, err);
4368 if (changed & IEEE80211_RC_NSS_CHANGED) {
4369 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM nss %d\n",
4372 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4375 ath10k_warn(ar, "failed to update STA %pM nss %d: %d\n",
4376 sta->addr, nss, err);
4379 if (changed & IEEE80211_RC_SMPS_CHANGED) {
4380 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM smps %d\n",
4383 err = ath10k_wmi_peer_set_param(ar, arvif->vdev_id, sta->addr,
4384 WMI_PEER_SMPS_STATE, smps);
4386 ath10k_warn(ar, "failed to update STA %pM smps %d: %d\n",
4387 sta->addr, smps, err);
4390 if (changed & IEEE80211_RC_SUPP_RATES_CHANGED ||
4391 changed & IEEE80211_RC_NSS_CHANGED) {
4392 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac update sta %pM supp rates/nss\n",
4395 err = ath10k_station_assoc(ar, arvif->vif, sta, true);
4397 ath10k_warn(ar, "failed to reassociate station: %pM\n",
4401 mutex_unlock(&ar->conf_mutex);
4404 static int ath10k_mac_inc_num_stations(struct ath10k_vif *arvif,
4405 struct ieee80211_sta *sta)
4407 struct ath10k *ar = arvif->ar;
4409 lockdep_assert_held(&ar->conf_mutex);
4411 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4414 if (ar->num_stations >= ar->max_num_stations)
4422 static void ath10k_mac_dec_num_stations(struct ath10k_vif *arvif,
4423 struct ieee80211_sta *sta)
4425 struct ath10k *ar = arvif->ar;
4427 lockdep_assert_held(&ar->conf_mutex);
4429 if (arvif->vdev_type == WMI_VDEV_TYPE_STA && !sta->tdls)
4435 static int ath10k_sta_state(struct ieee80211_hw *hw,
4436 struct ieee80211_vif *vif,
4437 struct ieee80211_sta *sta,
4438 enum ieee80211_sta_state old_state,
4439 enum ieee80211_sta_state new_state)
4441 struct ath10k *ar = hw->priv;
4442 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4443 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
4446 if (old_state == IEEE80211_STA_NOTEXIST &&
4447 new_state == IEEE80211_STA_NONE) {
4448 memset(arsta, 0, sizeof(*arsta));
4449 arsta->arvif = arvif;
4450 INIT_WORK(&arsta->update_wk, ath10k_sta_rc_update_wk);
4453 /* cancel must be done outside the mutex to avoid deadlock */
4454 if ((old_state == IEEE80211_STA_NONE &&
4455 new_state == IEEE80211_STA_NOTEXIST))
4456 cancel_work_sync(&arsta->update_wk);
4458 mutex_lock(&ar->conf_mutex);
4460 if (old_state == IEEE80211_STA_NOTEXIST &&
4461 new_state == IEEE80211_STA_NONE) {
4463 * New station addition.
4465 ath10k_dbg(ar, ATH10K_DBG_MAC,
4466 "mac vdev %d peer create %pM (new sta) sta %d / %d peer %d / %d\n",
4467 arvif->vdev_id, sta->addr,
4468 ar->num_stations + 1, ar->max_num_stations,
4469 ar->num_peers + 1, ar->max_num_peers);
4471 ret = ath10k_mac_inc_num_stations(arvif, sta);
4473 ath10k_warn(ar, "refusing to associate station: too many connected already (%d)\n",
4474 ar->max_num_stations);
4478 ret = ath10k_peer_create(ar, arvif->vdev_id, sta->addr,
4479 WMI_PEER_TYPE_DEFAULT);
4481 ath10k_warn(ar, "failed to add peer %pM for vdev %d when adding a new sta: %i\n",
4482 sta->addr, arvif->vdev_id, ret);
4483 ath10k_mac_dec_num_stations(arvif, sta);
4487 if (vif->type == NL80211_IFTYPE_STATION) {
4488 WARN_ON(arvif->is_started);
4490 ret = ath10k_vdev_start(arvif);
4492 ath10k_warn(ar, "failed to start vdev %i: %d\n",
4493 arvif->vdev_id, ret);
4494 WARN_ON(ath10k_peer_delete(ar, arvif->vdev_id,
4496 ath10k_mac_dec_num_stations(arvif, sta);
4500 arvif->is_started = true;
4502 } else if ((old_state == IEEE80211_STA_NONE &&
4503 new_state == IEEE80211_STA_NOTEXIST)) {
4505 * Existing station deletion.
4507 ath10k_dbg(ar, ATH10K_DBG_MAC,
4508 "mac vdev %d peer delete %pM (sta gone)\n",
4509 arvif->vdev_id, sta->addr);
4511 if (vif->type == NL80211_IFTYPE_STATION) {
4512 WARN_ON(!arvif->is_started);
4514 ret = ath10k_vdev_stop(arvif);
4516 ath10k_warn(ar, "failed to stop vdev %i: %d\n",
4517 arvif->vdev_id, ret);
4519 arvif->is_started = false;
4522 ret = ath10k_peer_delete(ar, arvif->vdev_id, sta->addr);
4524 ath10k_warn(ar, "failed to delete peer %pM for vdev %d: %i\n",
4525 sta->addr, arvif->vdev_id, ret);
4527 ath10k_mac_dec_num_stations(arvif, sta);
4528 } else if (old_state == IEEE80211_STA_AUTH &&
4529 new_state == IEEE80211_STA_ASSOC &&
4530 (vif->type == NL80211_IFTYPE_AP ||
4531 vif->type == NL80211_IFTYPE_ADHOC)) {
4535 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM associated\n",
4538 ret = ath10k_station_assoc(ar, vif, sta, false);
4540 ath10k_warn(ar, "failed to associate station %pM for vdev %i: %i\n",
4541 sta->addr, arvif->vdev_id, ret);
4542 } else if (old_state == IEEE80211_STA_ASSOC &&
4543 new_state == IEEE80211_STA_AUTH &&
4544 (vif->type == NL80211_IFTYPE_AP ||
4545 vif->type == NL80211_IFTYPE_ADHOC)) {
4549 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac sta %pM disassociated\n",
4552 ret = ath10k_station_disassoc(ar, vif, sta);
4554 ath10k_warn(ar, "failed to disassociate station: %pM vdev %i: %i\n",
4555 sta->addr, arvif->vdev_id, ret);
4558 mutex_unlock(&ar->conf_mutex);
4562 static int ath10k_conf_tx_uapsd(struct ath10k *ar, struct ieee80211_vif *vif,
4563 u16 ac, bool enable)
4565 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4566 struct wmi_sta_uapsd_auto_trig_arg arg = {};
4567 u32 prio = 0, acc = 0;
4571 lockdep_assert_held(&ar->conf_mutex);
4573 if (arvif->vdev_type != WMI_VDEV_TYPE_STA)
4577 case IEEE80211_AC_VO:
4578 value = WMI_STA_PS_UAPSD_AC3_DELIVERY_EN |
4579 WMI_STA_PS_UAPSD_AC3_TRIGGER_EN;
4583 case IEEE80211_AC_VI:
4584 value = WMI_STA_PS_UAPSD_AC2_DELIVERY_EN |
4585 WMI_STA_PS_UAPSD_AC2_TRIGGER_EN;
4589 case IEEE80211_AC_BE:
4590 value = WMI_STA_PS_UAPSD_AC1_DELIVERY_EN |
4591 WMI_STA_PS_UAPSD_AC1_TRIGGER_EN;
4595 case IEEE80211_AC_BK:
4596 value = WMI_STA_PS_UAPSD_AC0_DELIVERY_EN |
4597 WMI_STA_PS_UAPSD_AC0_TRIGGER_EN;
4604 arvif->u.sta.uapsd |= value;
4606 arvif->u.sta.uapsd &= ~value;
4608 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4609 WMI_STA_PS_PARAM_UAPSD,
4610 arvif->u.sta.uapsd);
4612 ath10k_warn(ar, "failed to set uapsd params: %d\n", ret);
4616 if (arvif->u.sta.uapsd)
4617 value = WMI_STA_PS_RX_WAKE_POLICY_POLL_UAPSD;
4619 value = WMI_STA_PS_RX_WAKE_POLICY_WAKE;
4621 ret = ath10k_wmi_set_sta_ps_param(ar, arvif->vdev_id,
4622 WMI_STA_PS_PARAM_RX_WAKE_POLICY,
4625 ath10k_warn(ar, "failed to set rx wake param: %d\n", ret);
4627 ret = ath10k_mac_vif_recalc_ps_wake_threshold(arvif);
4629 ath10k_warn(ar, "failed to recalc ps wake threshold on vdev %i: %d\n",
4630 arvif->vdev_id, ret);
4634 ret = ath10k_mac_vif_recalc_ps_poll_count(arvif);
4636 ath10k_warn(ar, "failed to recalc ps poll count on vdev %i: %d\n",
4637 arvif->vdev_id, ret);
4641 if (test_bit(WMI_SERVICE_STA_UAPSD_BASIC_AUTO_TRIG, ar->wmi.svc_map) ||
4642 test_bit(WMI_SERVICE_STA_UAPSD_VAR_AUTO_TRIG, ar->wmi.svc_map)) {
4643 /* Only userspace can make an educated decision when to send
4644 * trigger frame. The following effectively disables u-UAPSD
4645 * autotrigger in firmware (which is enabled by default
4646 * provided the autotrigger service is available).
4650 arg.user_priority = prio;
4651 arg.service_interval = 0;
4652 arg.suspend_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4653 arg.delay_interval = WMI_STA_UAPSD_MAX_INTERVAL_MSEC;
4655 ret = ath10k_wmi_vdev_sta_uapsd(ar, arvif->vdev_id,
4656 arvif->bssid, &arg, 1);
4658 ath10k_warn(ar, "failed to set uapsd auto trigger %d\n",
4668 static int ath10k_conf_tx(struct ieee80211_hw *hw,
4669 struct ieee80211_vif *vif, u16 ac,
4670 const struct ieee80211_tx_queue_params *params)
4672 struct ath10k *ar = hw->priv;
4673 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4674 struct wmi_wmm_params_arg *p = NULL;
4677 mutex_lock(&ar->conf_mutex);
4680 case IEEE80211_AC_VO:
4681 p = &arvif->wmm_params.ac_vo;
4683 case IEEE80211_AC_VI:
4684 p = &arvif->wmm_params.ac_vi;
4686 case IEEE80211_AC_BE:
4687 p = &arvif->wmm_params.ac_be;
4689 case IEEE80211_AC_BK:
4690 p = &arvif->wmm_params.ac_bk;
4699 p->cwmin = params->cw_min;
4700 p->cwmax = params->cw_max;
4701 p->aifs = params->aifs;
4704 * The channel time duration programmed in the HW is in absolute
4705 * microseconds, while mac80211 gives the txop in units of
4708 p->txop = params->txop * 32;
4710 if (ar->wmi.ops->gen_vdev_wmm_conf) {
4711 ret = ath10k_wmi_vdev_wmm_conf(ar, arvif->vdev_id,
4712 &arvif->wmm_params);
4714 ath10k_warn(ar, "failed to set vdev wmm params on vdev %i: %d\n",
4715 arvif->vdev_id, ret);
4719 /* This won't work well with multi-interface cases but it's
4720 * better than nothing.
4722 ret = ath10k_wmi_pdev_set_wmm_params(ar, &arvif->wmm_params);
4724 ath10k_warn(ar, "failed to set wmm params: %d\n", ret);
4729 ret = ath10k_conf_tx_uapsd(ar, vif, ac, params->uapsd);
4731 ath10k_warn(ar, "failed to set sta uapsd: %d\n", ret);
4734 mutex_unlock(&ar->conf_mutex);
4738 #define ATH10K_ROC_TIMEOUT_HZ (2*HZ)
4740 static int ath10k_remain_on_channel(struct ieee80211_hw *hw,
4741 struct ieee80211_vif *vif,
4742 struct ieee80211_channel *chan,
4744 enum ieee80211_roc_type type)
4746 struct ath10k *ar = hw->priv;
4747 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
4748 struct wmi_start_scan_arg arg;
4751 mutex_lock(&ar->conf_mutex);
4753 spin_lock_bh(&ar->data_lock);
4754 switch (ar->scan.state) {
4755 case ATH10K_SCAN_IDLE:
4756 reinit_completion(&ar->scan.started);
4757 reinit_completion(&ar->scan.completed);
4758 reinit_completion(&ar->scan.on_channel);
4759 ar->scan.state = ATH10K_SCAN_STARTING;
4760 ar->scan.is_roc = true;
4761 ar->scan.vdev_id = arvif->vdev_id;
4762 ar->scan.roc_freq = chan->center_freq;
4765 case ATH10K_SCAN_STARTING:
4766 case ATH10K_SCAN_RUNNING:
4767 case ATH10K_SCAN_ABORTING:
4771 spin_unlock_bh(&ar->data_lock);
4776 duration = max(duration, WMI_SCAN_CHAN_MIN_TIME_MSEC);
4778 memset(&arg, 0, sizeof(arg));
4779 ath10k_wmi_start_scan_init(ar, &arg);
4780 arg.vdev_id = arvif->vdev_id;
4781 arg.scan_id = ATH10K_SCAN_ID;
4783 arg.channels[0] = chan->center_freq;
4784 arg.dwell_time_active = duration;
4785 arg.dwell_time_passive = duration;
4786 arg.max_scan_time = 2 * duration;
4787 arg.scan_ctrl_flags |= WMI_SCAN_FLAG_PASSIVE;
4788 arg.scan_ctrl_flags |= WMI_SCAN_FILTER_PROBE_REQ;
4790 ret = ath10k_start_scan(ar, &arg);
4792 ath10k_warn(ar, "failed to start roc scan: %d\n", ret);
4793 spin_lock_bh(&ar->data_lock);
4794 ar->scan.state = ATH10K_SCAN_IDLE;
4795 spin_unlock_bh(&ar->data_lock);
4799 ret = wait_for_completion_timeout(&ar->scan.on_channel, 3*HZ);
4801 ath10k_warn(ar, "failed to switch to channel for roc scan\n");
4803 ret = ath10k_scan_stop(ar);
4805 ath10k_warn(ar, "failed to stop scan: %d\n", ret);
4813 mutex_unlock(&ar->conf_mutex);
4817 static int ath10k_cancel_remain_on_channel(struct ieee80211_hw *hw)
4819 struct ath10k *ar = hw->priv;
4821 mutex_lock(&ar->conf_mutex);
4822 ath10k_scan_abort(ar);
4823 mutex_unlock(&ar->conf_mutex);
4825 cancel_delayed_work_sync(&ar->scan.timeout);
4831 * Both RTS and Fragmentation threshold are interface-specific
4832 * in ath10k, but device-specific in mac80211.
4835 static int ath10k_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4837 struct ath10k *ar = hw->priv;
4838 struct ath10k_vif *arvif;
4841 mutex_lock(&ar->conf_mutex);
4842 list_for_each_entry(arvif, &ar->arvifs, list) {
4843 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac vdev %d rts threshold %d\n",
4844 arvif->vdev_id, value);
4846 ret = ath10k_mac_set_rts(arvif, value);
4848 ath10k_warn(ar, "failed to set rts threshold for vdev %d: %d\n",
4849 arvif->vdev_id, ret);
4853 mutex_unlock(&ar->conf_mutex);
4858 static void ath10k_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4859 u32 queues, bool drop)
4861 struct ath10k *ar = hw->priv;
4865 /* mac80211 doesn't care if we really xmit queued frames or not
4866 * we'll collect those frames either way if we stop/delete vdevs */
4870 mutex_lock(&ar->conf_mutex);
4872 if (ar->state == ATH10K_STATE_WEDGED)
4875 ret = wait_event_timeout(ar->htt.empty_tx_wq, ({
4878 spin_lock_bh(&ar->htt.tx_lock);
4879 empty = (ar->htt.num_pending_tx == 0);
4880 spin_unlock_bh(&ar->htt.tx_lock);
4882 skip = (ar->state == ATH10K_STATE_WEDGED) ||
4883 test_bit(ATH10K_FLAG_CRASH_FLUSH,
4887 }), ATH10K_FLUSH_TIMEOUT_HZ);
4889 if (ret <= 0 || skip)
4890 ath10k_warn(ar, "failed to flush transmit queue (skip %i ar-state %i): %i\n",
4891 skip, ar->state, ret);
4894 mutex_unlock(&ar->conf_mutex);
4897 /* TODO: Implement this function properly
4898 * For now it is needed to reply to Probe Requests in IBSS mode.
4899 * Propably we need this information from FW.
4901 static int ath10k_tx_last_beacon(struct ieee80211_hw *hw)
4906 static void ath10k_reconfig_complete(struct ieee80211_hw *hw,
4907 enum ieee80211_reconfig_type reconfig_type)
4909 struct ath10k *ar = hw->priv;
4911 if (reconfig_type != IEEE80211_RECONFIG_TYPE_RESTART)
4914 mutex_lock(&ar->conf_mutex);
4916 /* If device failed to restart it will be in a different state, e.g.
4917 * ATH10K_STATE_WEDGED */
4918 if (ar->state == ATH10K_STATE_RESTARTED) {
4919 ath10k_info(ar, "device successfully recovered\n");
4920 ar->state = ATH10K_STATE_ON;
4921 ieee80211_wake_queues(ar->hw);
4924 mutex_unlock(&ar->conf_mutex);
4927 static int ath10k_get_survey(struct ieee80211_hw *hw, int idx,
4928 struct survey_info *survey)
4930 struct ath10k *ar = hw->priv;
4931 struct ieee80211_supported_band *sband;
4932 struct survey_info *ar_survey = &ar->survey[idx];
4935 mutex_lock(&ar->conf_mutex);
4937 sband = hw->wiphy->bands[IEEE80211_BAND_2GHZ];
4938 if (sband && idx >= sband->n_channels) {
4939 idx -= sband->n_channels;
4944 sband = hw->wiphy->bands[IEEE80211_BAND_5GHZ];
4946 if (!sband || idx >= sband->n_channels) {
4951 spin_lock_bh(&ar->data_lock);
4952 memcpy(survey, ar_survey, sizeof(*survey));
4953 spin_unlock_bh(&ar->data_lock);
4955 survey->channel = &sband->channels[idx];
4957 if (ar->rx_channel == survey->channel)
4958 survey->filled |= SURVEY_INFO_IN_USE;
4961 mutex_unlock(&ar->conf_mutex);
4965 /* Helper table for legacy fixed_rate/bitrate_mask */
4966 static const u8 cck_ofdm_rate[] = {
4983 /* Check if only one bit set */
4984 static int ath10k_check_single_mask(u32 mask)
4992 mask &= ~BIT(bit - 1);
5000 ath10k_default_bitrate_mask(struct ath10k *ar,
5001 enum ieee80211_band band,
5002 const struct cfg80211_bitrate_mask *mask)
5004 u32 legacy = 0x00ff;
5007 u16 nrf = ar->num_rf_chains;
5009 if (ar->cfg_tx_chainmask)
5010 nrf = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5013 case IEEE80211_BAND_2GHZ:
5017 case IEEE80211_BAND_5GHZ:
5023 if (mask->control[band].legacy != legacy)
5026 for (i = 0; i < nrf; i++)
5027 if (mask->control[band].ht_mcs[i] != ht)
5030 for (i = 0; i < nrf; i++)
5031 if (mask->control[band].vht_mcs[i] != vht)
5038 ath10k_bitrate_mask_nss(const struct cfg80211_bitrate_mask *mask,
5039 enum ieee80211_band band,
5042 int ht_nss = 0, vht_nss = 0, i;
5045 if (ath10k_check_single_mask(mask->control[band].legacy))
5049 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
5050 if (mask->control[band].ht_mcs[i] == 0xff)
5052 else if (mask->control[band].ht_mcs[i] == 0x00)
5061 for (i = 0; i < NL80211_VHT_NSS_MAX; i++) {
5062 if (mask->control[band].vht_mcs[i] == 0x03ff)
5064 else if (mask->control[band].vht_mcs[i] == 0x0000)
5072 if (ht_nss > 0 && vht_nss > 0)
5076 *fixed_nss = ht_nss;
5078 *fixed_nss = vht_nss;
5086 ath10k_bitrate_mask_correct(const struct cfg80211_bitrate_mask *mask,
5087 enum ieee80211_band band,
5088 enum wmi_rate_preamble *preamble)
5090 int legacy = 0, ht = 0, vht = 0, i;
5092 *preamble = WMI_RATE_PREAMBLE_OFDM;
5095 legacy = ath10k_check_single_mask(mask->control[band].legacy);
5100 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5101 ht += ath10k_check_single_mask(mask->control[band].ht_mcs[i]);
5106 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5107 vht += ath10k_check_single_mask(mask->control[band].vht_mcs[i]);
5111 /* Currently we support only one fixed_rate */
5112 if ((legacy + ht + vht) != 1)
5116 *preamble = WMI_RATE_PREAMBLE_HT;
5118 *preamble = WMI_RATE_PREAMBLE_VHT;
5124 ath10k_bitrate_mask_rate(struct ath10k *ar,
5125 const struct cfg80211_bitrate_mask *mask,
5126 enum ieee80211_band band,
5130 u8 rate = 0, pream = 0, nss = 0, i;
5131 enum wmi_rate_preamble preamble;
5133 /* Check if single rate correct */
5134 if (!ath10k_bitrate_mask_correct(mask, band, &preamble))
5140 case WMI_RATE_PREAMBLE_CCK:
5141 case WMI_RATE_PREAMBLE_OFDM:
5142 i = ffs(mask->control[band].legacy) - 1;
5144 if (band == IEEE80211_BAND_2GHZ && i < 4)
5145 pream = WMI_RATE_PREAMBLE_CCK;
5147 if (band == IEEE80211_BAND_5GHZ)
5150 if (i >= ARRAY_SIZE(cck_ofdm_rate))
5153 rate = cck_ofdm_rate[i];
5155 case WMI_RATE_PREAMBLE_HT:
5156 for (i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++)
5157 if (mask->control[band].ht_mcs[i])
5160 if (i == IEEE80211_HT_MCS_MASK_LEN)
5163 rate = ffs(mask->control[band].ht_mcs[i]) - 1;
5166 case WMI_RATE_PREAMBLE_VHT:
5167 for (i = 0; i < NL80211_VHT_NSS_MAX; i++)
5168 if (mask->control[band].vht_mcs[i])
5171 if (i == NL80211_VHT_NSS_MAX)
5174 rate = ffs(mask->control[band].vht_mcs[i]) - 1;
5179 *fixed_nss = nss + 1;
5183 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac fixed rate pream 0x%02x nss 0x%02x rate 0x%02x\n",
5186 *fixed_rate = pream | nss | rate;
5191 static bool ath10k_get_fixed_rate_nss(struct ath10k *ar,
5192 const struct cfg80211_bitrate_mask *mask,
5193 enum ieee80211_band band,
5197 /* First check full NSS mask, if we can simply limit NSS */
5198 if (ath10k_bitrate_mask_nss(mask, band, fixed_nss))
5201 /* Next Check single rate is set */
5202 return ath10k_bitrate_mask_rate(ar, mask, band, fixed_rate, fixed_nss);
5205 static int ath10k_set_fixed_rate_param(struct ath10k_vif *arvif,
5210 struct ath10k *ar = arvif->ar;
5214 mutex_lock(&ar->conf_mutex);
5216 if (arvif->fixed_rate == fixed_rate &&
5217 arvif->fixed_nss == fixed_nss &&
5218 arvif->force_sgi == force_sgi)
5221 if (fixed_rate == WMI_FIXED_RATE_NONE)
5222 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac disable fixed bitrate mask\n");
5225 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac force sgi\n");
5227 vdev_param = ar->wmi.vdev_param->fixed_rate;
5228 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5229 vdev_param, fixed_rate);
5231 ath10k_warn(ar, "failed to set fixed rate param 0x%02x: %d\n",
5237 arvif->fixed_rate = fixed_rate;
5239 vdev_param = ar->wmi.vdev_param->nss;
5240 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id,
5241 vdev_param, fixed_nss);
5244 ath10k_warn(ar, "failed to set fixed nss param %d: %d\n",
5250 arvif->fixed_nss = fixed_nss;
5252 vdev_param = ar->wmi.vdev_param->sgi;
5253 ret = ath10k_wmi_vdev_set_param(ar, arvif->vdev_id, vdev_param,
5257 ath10k_warn(ar, "failed to set sgi param %d: %d\n",
5263 arvif->force_sgi = force_sgi;
5266 mutex_unlock(&ar->conf_mutex);
5270 static int ath10k_set_bitrate_mask(struct ieee80211_hw *hw,
5271 struct ieee80211_vif *vif,
5272 const struct cfg80211_bitrate_mask *mask)
5274 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5275 struct ath10k *ar = arvif->ar;
5276 enum ieee80211_band band = ar->hw->conf.chandef.chan->band;
5277 u8 fixed_rate = WMI_FIXED_RATE_NONE;
5278 u8 fixed_nss = ar->num_rf_chains;
5281 if (ar->cfg_tx_chainmask)
5282 fixed_nss = get_nss_from_chainmask(ar->cfg_tx_chainmask);
5284 force_sgi = mask->control[band].gi;
5285 if (force_sgi == NL80211_TXRATE_FORCE_LGI)
5288 if (!ath10k_default_bitrate_mask(ar, band, mask)) {
5289 if (!ath10k_get_fixed_rate_nss(ar, mask, band,
5295 if (fixed_rate == WMI_FIXED_RATE_NONE && force_sgi) {
5296 ath10k_warn(ar, "failed to force SGI usage for default rate settings\n");
5300 return ath10k_set_fixed_rate_param(arvif, fixed_rate,
5301 fixed_nss, force_sgi);
5304 static void ath10k_sta_rc_update(struct ieee80211_hw *hw,
5305 struct ieee80211_vif *vif,
5306 struct ieee80211_sta *sta,
5309 struct ath10k *ar = hw->priv;
5310 struct ath10k_sta *arsta = (struct ath10k_sta *)sta->drv_priv;
5313 spin_lock_bh(&ar->data_lock);
5315 ath10k_dbg(ar, ATH10K_DBG_MAC,
5316 "mac sta rc update for %pM changed %08x bw %d nss %d smps %d\n",
5317 sta->addr, changed, sta->bandwidth, sta->rx_nss,
5320 if (changed & IEEE80211_RC_BW_CHANGED) {
5321 bw = WMI_PEER_CHWIDTH_20MHZ;
5323 switch (sta->bandwidth) {
5324 case IEEE80211_STA_RX_BW_20:
5325 bw = WMI_PEER_CHWIDTH_20MHZ;
5327 case IEEE80211_STA_RX_BW_40:
5328 bw = WMI_PEER_CHWIDTH_40MHZ;
5330 case IEEE80211_STA_RX_BW_80:
5331 bw = WMI_PEER_CHWIDTH_80MHZ;
5333 case IEEE80211_STA_RX_BW_160:
5334 ath10k_warn(ar, "Invalid bandwith %d in rc update for %pM\n",
5335 sta->bandwidth, sta->addr);
5336 bw = WMI_PEER_CHWIDTH_20MHZ;
5343 if (changed & IEEE80211_RC_NSS_CHANGED)
5344 arsta->nss = sta->rx_nss;
5346 if (changed & IEEE80211_RC_SMPS_CHANGED) {
5347 smps = WMI_PEER_SMPS_PS_NONE;
5349 switch (sta->smps_mode) {
5350 case IEEE80211_SMPS_AUTOMATIC:
5351 case IEEE80211_SMPS_OFF:
5352 smps = WMI_PEER_SMPS_PS_NONE;
5354 case IEEE80211_SMPS_STATIC:
5355 smps = WMI_PEER_SMPS_STATIC;
5357 case IEEE80211_SMPS_DYNAMIC:
5358 smps = WMI_PEER_SMPS_DYNAMIC;
5360 case IEEE80211_SMPS_NUM_MODES:
5361 ath10k_warn(ar, "Invalid smps %d in sta rc update for %pM\n",
5362 sta->smps_mode, sta->addr);
5363 smps = WMI_PEER_SMPS_PS_NONE;
5370 arsta->changed |= changed;
5372 spin_unlock_bh(&ar->data_lock);
5374 ieee80211_queue_work(hw, &arsta->update_wk);
5377 static u64 ath10k_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
5380 * FIXME: Return 0 for time being. Need to figure out whether FW
5381 * has the API to fetch 64-bit local TSF
5387 static int ath10k_ampdu_action(struct ieee80211_hw *hw,
5388 struct ieee80211_vif *vif,
5389 enum ieee80211_ampdu_mlme_action action,
5390 struct ieee80211_sta *sta, u16 tid, u16 *ssn,
5393 struct ath10k *ar = hw->priv;
5394 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5396 ath10k_dbg(ar, ATH10K_DBG_MAC, "mac ampdu vdev_id %i sta %pM tid %hu action %d\n",
5397 arvif->vdev_id, sta->addr, tid, action);
5400 case IEEE80211_AMPDU_RX_START:
5401 case IEEE80211_AMPDU_RX_STOP:
5402 /* HTT AddBa/DelBa events trigger mac80211 Rx BA session
5403 * creation/removal. Do we need to verify this?
5406 case IEEE80211_AMPDU_TX_START:
5407 case IEEE80211_AMPDU_TX_STOP_CONT:
5408 case IEEE80211_AMPDU_TX_STOP_FLUSH:
5409 case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
5410 case IEEE80211_AMPDU_TX_OPERATIONAL:
5411 /* Firmware offloads Tx aggregation entirely so deny mac80211
5412 * Tx aggregation requests.
5420 static const struct ieee80211_ops ath10k_ops = {
5422 .start = ath10k_start,
5423 .stop = ath10k_stop,
5424 .config = ath10k_config,
5425 .add_interface = ath10k_add_interface,
5426 .remove_interface = ath10k_remove_interface,
5427 .configure_filter = ath10k_configure_filter,
5428 .bss_info_changed = ath10k_bss_info_changed,
5429 .hw_scan = ath10k_hw_scan,
5430 .cancel_hw_scan = ath10k_cancel_hw_scan,
5431 .set_key = ath10k_set_key,
5432 .set_default_unicast_key = ath10k_set_default_unicast_key,
5433 .sta_state = ath10k_sta_state,
5434 .conf_tx = ath10k_conf_tx,
5435 .remain_on_channel = ath10k_remain_on_channel,
5436 .cancel_remain_on_channel = ath10k_cancel_remain_on_channel,
5437 .set_rts_threshold = ath10k_set_rts_threshold,
5438 .flush = ath10k_flush,
5439 .tx_last_beacon = ath10k_tx_last_beacon,
5440 .set_antenna = ath10k_set_antenna,
5441 .get_antenna = ath10k_get_antenna,
5442 .reconfig_complete = ath10k_reconfig_complete,
5443 .get_survey = ath10k_get_survey,
5444 .set_bitrate_mask = ath10k_set_bitrate_mask,
5445 .sta_rc_update = ath10k_sta_rc_update,
5446 .get_tsf = ath10k_get_tsf,
5447 .ampdu_action = ath10k_ampdu_action,
5448 .get_et_sset_count = ath10k_debug_get_et_sset_count,
5449 .get_et_stats = ath10k_debug_get_et_stats,
5450 .get_et_strings = ath10k_debug_get_et_strings,
5452 CFG80211_TESTMODE_CMD(ath10k_tm_cmd)
5455 .suspend = ath10k_wow_op_suspend,
5456 .resume = ath10k_wow_op_resume,
5458 #ifdef CONFIG_MAC80211_DEBUGFS
5459 .sta_add_debugfs = ath10k_sta_add_debugfs,
5463 #define RATETAB_ENT(_rate, _rateid, _flags) { \
5464 .bitrate = (_rate), \
5465 .flags = (_flags), \
5466 .hw_value = (_rateid), \
5469 #define CHAN2G(_channel, _freq, _flags) { \
5470 .band = IEEE80211_BAND_2GHZ, \
5471 .hw_value = (_channel), \
5472 .center_freq = (_freq), \
5473 .flags = (_flags), \
5474 .max_antenna_gain = 0, \
5478 #define CHAN5G(_channel, _freq, _flags) { \
5479 .band = IEEE80211_BAND_5GHZ, \
5480 .hw_value = (_channel), \
5481 .center_freq = (_freq), \
5482 .flags = (_flags), \
5483 .max_antenna_gain = 0, \
5487 static const struct ieee80211_channel ath10k_2ghz_channels[] = {
5497 CHAN2G(10, 2457, 0),
5498 CHAN2G(11, 2462, 0),
5499 CHAN2G(12, 2467, 0),
5500 CHAN2G(13, 2472, 0),
5501 CHAN2G(14, 2484, 0),
5504 static const struct ieee80211_channel ath10k_5ghz_channels[] = {
5505 CHAN5G(36, 5180, 0),
5506 CHAN5G(40, 5200, 0),
5507 CHAN5G(44, 5220, 0),
5508 CHAN5G(48, 5240, 0),
5509 CHAN5G(52, 5260, 0),
5510 CHAN5G(56, 5280, 0),
5511 CHAN5G(60, 5300, 0),
5512 CHAN5G(64, 5320, 0),
5513 CHAN5G(100, 5500, 0),
5514 CHAN5G(104, 5520, 0),
5515 CHAN5G(108, 5540, 0),
5516 CHAN5G(112, 5560, 0),
5517 CHAN5G(116, 5580, 0),
5518 CHAN5G(120, 5600, 0),
5519 CHAN5G(124, 5620, 0),
5520 CHAN5G(128, 5640, 0),
5521 CHAN5G(132, 5660, 0),
5522 CHAN5G(136, 5680, 0),
5523 CHAN5G(140, 5700, 0),
5524 CHAN5G(144, 5720, 0),
5525 CHAN5G(149, 5745, 0),
5526 CHAN5G(153, 5765, 0),
5527 CHAN5G(157, 5785, 0),
5528 CHAN5G(161, 5805, 0),
5529 CHAN5G(165, 5825, 0),
5532 /* Note: Be careful if you re-order these. There is code which depends on this
5535 static struct ieee80211_rate ath10k_rates[] = {
5537 RATETAB_ENT(10, 0x82, 0),
5538 RATETAB_ENT(20, 0x84, 0),
5539 RATETAB_ENT(55, 0x8b, 0),
5540 RATETAB_ENT(110, 0x96, 0),
5542 RATETAB_ENT(60, 0x0c, 0),
5543 RATETAB_ENT(90, 0x12, 0),
5544 RATETAB_ENT(120, 0x18, 0),
5545 RATETAB_ENT(180, 0x24, 0),
5546 RATETAB_ENT(240, 0x30, 0),
5547 RATETAB_ENT(360, 0x48, 0),
5548 RATETAB_ENT(480, 0x60, 0),
5549 RATETAB_ENT(540, 0x6c, 0),
5552 #define ath10k_a_rates (ath10k_rates + 4)
5553 #define ath10k_a_rates_size (ARRAY_SIZE(ath10k_rates) - 4)
5554 #define ath10k_g_rates (ath10k_rates + 0)
5555 #define ath10k_g_rates_size (ARRAY_SIZE(ath10k_rates))
5557 struct ath10k *ath10k_mac_create(size_t priv_size)
5559 struct ieee80211_hw *hw;
5562 hw = ieee80211_alloc_hw(sizeof(struct ath10k) + priv_size, &ath10k_ops);
5572 void ath10k_mac_destroy(struct ath10k *ar)
5574 ieee80211_free_hw(ar->hw);
5577 static const struct ieee80211_iface_limit ath10k_if_limits[] = {
5580 .types = BIT(NL80211_IFTYPE_STATION)
5581 | BIT(NL80211_IFTYPE_P2P_CLIENT)
5585 .types = BIT(NL80211_IFTYPE_P2P_GO)
5589 .types = BIT(NL80211_IFTYPE_P2P_DEVICE)
5593 .types = BIT(NL80211_IFTYPE_AP)
5597 static const struct ieee80211_iface_limit ath10k_10x_if_limits[] = {
5600 .types = BIT(NL80211_IFTYPE_AP)
5604 static const struct ieee80211_iface_combination ath10k_if_comb[] = {
5606 .limits = ath10k_if_limits,
5607 .n_limits = ARRAY_SIZE(ath10k_if_limits),
5608 .max_interfaces = 8,
5609 .num_different_channels = 1,
5610 .beacon_int_infra_match = true,
5614 static const struct ieee80211_iface_combination ath10k_10x_if_comb[] = {
5616 .limits = ath10k_10x_if_limits,
5617 .n_limits = ARRAY_SIZE(ath10k_10x_if_limits),
5618 .max_interfaces = 8,
5619 .num_different_channels = 1,
5620 .beacon_int_infra_match = true,
5621 #ifdef CONFIG_ATH10K_DFS_CERTIFIED
5622 .radar_detect_widths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
5623 BIT(NL80211_CHAN_WIDTH_20) |
5624 BIT(NL80211_CHAN_WIDTH_40) |
5625 BIT(NL80211_CHAN_WIDTH_80),
5630 static struct ieee80211_sta_vht_cap ath10k_create_vht_cap(struct ath10k *ar)
5632 struct ieee80211_sta_vht_cap vht_cap = {0};
5637 vht_cap.vht_supported = 1;
5638 vht_cap.cap = ar->vht_cap_info;
5640 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE |
5641 IEEE80211_VHT_CAP_MU_BEAMFORMEE_CAPABLE)) {
5642 val = ar->num_rf_chains - 1;
5643 val <<= IEEE80211_VHT_CAP_BEAMFORMEE_STS_SHIFT;
5644 val &= IEEE80211_VHT_CAP_BEAMFORMEE_STS_MASK;
5649 if (ar->vht_cap_info & (IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE |
5650 IEEE80211_VHT_CAP_MU_BEAMFORMER_CAPABLE)) {
5651 val = ar->num_rf_chains - 1;
5652 val <<= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_SHIFT;
5653 val &= IEEE80211_VHT_CAP_SOUNDING_DIMENSIONS_MASK;
5659 for (i = 0; i < 8; i++) {
5660 if (i < ar->num_rf_chains)
5661 mcs_map |= IEEE80211_VHT_MCS_SUPPORT_0_9 << (i*2);
5663 mcs_map |= IEEE80211_VHT_MCS_NOT_SUPPORTED << (i*2);
5666 vht_cap.vht_mcs.rx_mcs_map = cpu_to_le16(mcs_map);
5667 vht_cap.vht_mcs.tx_mcs_map = cpu_to_le16(mcs_map);
5672 static struct ieee80211_sta_ht_cap ath10k_get_ht_cap(struct ath10k *ar)
5675 struct ieee80211_sta_ht_cap ht_cap = {0};
5677 if (!(ar->ht_cap_info & WMI_HT_CAP_ENABLED))
5680 ht_cap.ht_supported = 1;
5681 ht_cap.ampdu_factor = IEEE80211_HT_MAX_AMPDU_64K;
5682 ht_cap.ampdu_density = IEEE80211_HT_MPDU_DENSITY_8;
5683 ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
5684 ht_cap.cap |= IEEE80211_HT_CAP_DSSSCCK40;
5685 ht_cap.cap |= WLAN_HT_CAP_SM_PS_STATIC << IEEE80211_HT_CAP_SM_PS_SHIFT;
5687 if (ar->ht_cap_info & WMI_HT_CAP_HT20_SGI)
5688 ht_cap.cap |= IEEE80211_HT_CAP_SGI_20;
5690 if (ar->ht_cap_info & WMI_HT_CAP_HT40_SGI)
5691 ht_cap.cap |= IEEE80211_HT_CAP_SGI_40;
5693 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS) {
5696 smps = WLAN_HT_CAP_SM_PS_DYNAMIC;
5697 smps <<= IEEE80211_HT_CAP_SM_PS_SHIFT;
5702 if (ar->ht_cap_info & WMI_HT_CAP_TX_STBC)
5703 ht_cap.cap |= IEEE80211_HT_CAP_TX_STBC;
5705 if (ar->ht_cap_info & WMI_HT_CAP_RX_STBC) {
5708 stbc = ar->ht_cap_info;
5709 stbc &= WMI_HT_CAP_RX_STBC;
5710 stbc >>= WMI_HT_CAP_RX_STBC_MASK_SHIFT;
5711 stbc <<= IEEE80211_HT_CAP_RX_STBC_SHIFT;
5712 stbc &= IEEE80211_HT_CAP_RX_STBC;
5717 if (ar->ht_cap_info & WMI_HT_CAP_LDPC)
5718 ht_cap.cap |= IEEE80211_HT_CAP_LDPC_CODING;
5720 if (ar->ht_cap_info & WMI_HT_CAP_L_SIG_TXOP_PROT)
5721 ht_cap.cap |= IEEE80211_HT_CAP_LSIG_TXOP_PROT;
5723 /* max AMSDU is implicitly taken from vht_cap_info */
5724 if (ar->vht_cap_info & WMI_VHT_CAP_MAX_MPDU_LEN_MASK)
5725 ht_cap.cap |= IEEE80211_HT_CAP_MAX_AMSDU;
5727 for (i = 0; i < ar->num_rf_chains; i++)
5728 ht_cap.mcs.rx_mask[i] = 0xFF;
5730 ht_cap.mcs.tx_params |= IEEE80211_HT_MCS_TX_DEFINED;
5735 static void ath10k_get_arvif_iter(void *data, u8 *mac,
5736 struct ieee80211_vif *vif)
5738 struct ath10k_vif_iter *arvif_iter = data;
5739 struct ath10k_vif *arvif = ath10k_vif_to_arvif(vif);
5741 if (arvif->vdev_id == arvif_iter->vdev_id)
5742 arvif_iter->arvif = arvif;
5745 struct ath10k_vif *ath10k_get_arvif(struct ath10k *ar, u32 vdev_id)
5747 struct ath10k_vif_iter arvif_iter;
5750 memset(&arvif_iter, 0, sizeof(struct ath10k_vif_iter));
5751 arvif_iter.vdev_id = vdev_id;
5753 flags = IEEE80211_IFACE_ITER_RESUME_ALL;
5754 ieee80211_iterate_active_interfaces_atomic(ar->hw,
5756 ath10k_get_arvif_iter,
5758 if (!arvif_iter.arvif) {
5759 ath10k_warn(ar, "No VIF found for vdev %d\n", vdev_id);
5763 return arvif_iter.arvif;
5766 int ath10k_mac_register(struct ath10k *ar)
5768 static const u32 cipher_suites[] = {
5769 WLAN_CIPHER_SUITE_WEP40,
5770 WLAN_CIPHER_SUITE_WEP104,
5771 WLAN_CIPHER_SUITE_TKIP,
5772 WLAN_CIPHER_SUITE_CCMP,
5773 WLAN_CIPHER_SUITE_AES_CMAC,
5775 struct ieee80211_supported_band *band;
5776 struct ieee80211_sta_vht_cap vht_cap;
5777 struct ieee80211_sta_ht_cap ht_cap;
5781 SET_IEEE80211_PERM_ADDR(ar->hw, ar->mac_addr);
5783 SET_IEEE80211_DEV(ar->hw, ar->dev);
5785 ht_cap = ath10k_get_ht_cap(ar);
5786 vht_cap = ath10k_create_vht_cap(ar);
5788 if (ar->phy_capability & WHAL_WLAN_11G_CAPABILITY) {
5789 channels = kmemdup(ath10k_2ghz_channels,
5790 sizeof(ath10k_2ghz_channels),
5797 band = &ar->mac.sbands[IEEE80211_BAND_2GHZ];
5798 band->n_channels = ARRAY_SIZE(ath10k_2ghz_channels);
5799 band->channels = channels;
5800 band->n_bitrates = ath10k_g_rates_size;
5801 band->bitrates = ath10k_g_rates;
5802 band->ht_cap = ht_cap;
5804 /* Enable the VHT support at 2.4 GHz */
5805 band->vht_cap = vht_cap;
5807 ar->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = band;
5810 if (ar->phy_capability & WHAL_WLAN_11A_CAPABILITY) {
5811 channels = kmemdup(ath10k_5ghz_channels,
5812 sizeof(ath10k_5ghz_channels),
5819 band = &ar->mac.sbands[IEEE80211_BAND_5GHZ];
5820 band->n_channels = ARRAY_SIZE(ath10k_5ghz_channels);
5821 band->channels = channels;
5822 band->n_bitrates = ath10k_a_rates_size;
5823 band->bitrates = ath10k_a_rates;
5824 band->ht_cap = ht_cap;
5825 band->vht_cap = vht_cap;
5826 ar->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = band;
5829 ar->hw->wiphy->interface_modes =
5830 BIT(NL80211_IFTYPE_STATION) |
5831 BIT(NL80211_IFTYPE_AP);
5833 ar->hw->wiphy->available_antennas_rx = ar->supp_rx_chainmask;
5834 ar->hw->wiphy->available_antennas_tx = ar->supp_tx_chainmask;
5836 if (!test_bit(ATH10K_FW_FEATURE_NO_P2P, ar->fw_features))
5837 ar->hw->wiphy->interface_modes |=
5838 BIT(NL80211_IFTYPE_P2P_DEVICE) |
5839 BIT(NL80211_IFTYPE_P2P_CLIENT) |
5840 BIT(NL80211_IFTYPE_P2P_GO);
5842 ar->hw->flags = IEEE80211_HW_SIGNAL_DBM |
5843 IEEE80211_HW_SUPPORTS_PS |
5844 IEEE80211_HW_SUPPORTS_DYNAMIC_PS |
5845 IEEE80211_HW_MFP_CAPABLE |
5846 IEEE80211_HW_REPORTS_TX_ACK_STATUS |
5847 IEEE80211_HW_HAS_RATE_CONTROL |
5848 IEEE80211_HW_AP_LINK_PS |
5849 IEEE80211_HW_SPECTRUM_MGMT |
5850 IEEE80211_HW_SW_CRYPTO_CONTROL |
5851 IEEE80211_HW_CONNECTION_MONITOR;
5853 ar->hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS;
5855 if (ar->ht_cap_info & WMI_HT_CAP_DYNAMIC_SMPS)
5856 ar->hw->wiphy->features |= NL80211_FEATURE_DYNAMIC_SMPS;
5858 if (ar->ht_cap_info & WMI_HT_CAP_ENABLED) {
5859 ar->hw->flags |= IEEE80211_HW_AMPDU_AGGREGATION;
5860 ar->hw->flags |= IEEE80211_HW_TX_AMPDU_SETUP_IN_HW;
5863 ar->hw->wiphy->max_scan_ssids = WLAN_SCAN_PARAMS_MAX_SSID;
5864 ar->hw->wiphy->max_scan_ie_len = WLAN_SCAN_PARAMS_MAX_IE_LEN;
5866 ar->hw->vif_data_size = sizeof(struct ath10k_vif);
5867 ar->hw->sta_data_size = sizeof(struct ath10k_sta);
5869 ar->hw->max_listen_interval = ATH10K_MAX_HW_LISTEN_INTERVAL;
5871 if (test_bit(WMI_SERVICE_BEACON_OFFLOAD, ar->wmi.svc_map)) {
5872 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_PROBE_RESP_OFFLOAD;
5874 /* Firmware delivers WPS/P2P Probe Requests frames to driver so
5875 * that userspace (e.g. wpa_supplicant/hostapd) can generate
5876 * correct Probe Responses. This is more of a hack advert..
5878 ar->hw->wiphy->probe_resp_offload |=
5879 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS |
5880 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_WPS2 |
5881 NL80211_PROBE_RESP_OFFLOAD_SUPPORT_P2P;
5884 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_REMAIN_ON_CHANNEL;
5885 ar->hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
5886 ar->hw->wiphy->max_remain_on_channel_duration = 5000;
5888 ar->hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
5889 ar->hw->wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE;
5891 ar->hw->wiphy->max_ap_assoc_sta = ar->max_num_stations;
5893 ret = ath10k_wow_init(ar);
5895 ath10k_warn(ar, "failed to init wow: %d\n", ret);
5900 * on LL hardware queues are managed entirely by the FW
5901 * so we only advertise to mac we can do the queues thing
5905 switch (ar->wmi.op_version) {
5906 case ATH10K_FW_WMI_OP_VERSION_MAIN:
5907 case ATH10K_FW_WMI_OP_VERSION_TLV:
5908 ar->hw->wiphy->iface_combinations = ath10k_if_comb;
5909 ar->hw->wiphy->n_iface_combinations =
5910 ARRAY_SIZE(ath10k_if_comb);
5911 ar->hw->wiphy->interface_modes |= BIT(NL80211_IFTYPE_ADHOC);
5913 case ATH10K_FW_WMI_OP_VERSION_10_1:
5914 case ATH10K_FW_WMI_OP_VERSION_10_2:
5915 case ATH10K_FW_WMI_OP_VERSION_10_2_4:
5916 ar->hw->wiphy->iface_combinations = ath10k_10x_if_comb;
5917 ar->hw->wiphy->n_iface_combinations =
5918 ARRAY_SIZE(ath10k_10x_if_comb);
5920 case ATH10K_FW_WMI_OP_VERSION_UNSET:
5921 case ATH10K_FW_WMI_OP_VERSION_MAX:
5927 ar->hw->netdev_features = NETIF_F_HW_CSUM;
5929 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED)) {
5930 /* Init ath dfs pattern detector */
5931 ar->ath_common.debug_mask = ATH_DBG_DFS;
5932 ar->dfs_detector = dfs_pattern_detector_init(&ar->ath_common,
5935 if (!ar->dfs_detector)
5936 ath10k_warn(ar, "failed to initialise DFS pattern detector\n");
5939 ret = ath_regd_init(&ar->ath_common.regulatory, ar->hw->wiphy,
5940 ath10k_reg_notifier);
5942 ath10k_err(ar, "failed to initialise regulatory: %i\n", ret);
5946 ar->hw->wiphy->cipher_suites = cipher_suites;
5947 ar->hw->wiphy->n_cipher_suites = ARRAY_SIZE(cipher_suites);
5949 ret = ieee80211_register_hw(ar->hw);
5951 ath10k_err(ar, "failed to register ieee80211: %d\n", ret);
5955 if (!ath_is_world_regd(&ar->ath_common.regulatory)) {
5956 ret = regulatory_hint(ar->hw->wiphy,
5957 ar->ath_common.regulatory.alpha2);
5959 goto err_unregister;
5965 ieee80211_unregister_hw(ar->hw);
5967 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5968 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5973 void ath10k_mac_unregister(struct ath10k *ar)
5975 ieee80211_unregister_hw(ar->hw);
5977 if (config_enabled(CONFIG_ATH10K_DFS_CERTIFIED) && ar->dfs_detector)
5978 ar->dfs_detector->exit(ar->dfs_detector);
5980 kfree(ar->mac.sbands[IEEE80211_BAND_2GHZ].channels);
5981 kfree(ar->mac.sbands[IEEE80211_BAND_5GHZ].channels);
5983 SET_IEEE80211_DEV(ar->hw, NULL);