root/drivers/net/wireless/ath/ath10k/mac.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ath10k_mac_bitrate_is_cck
  2. ath10k_mac_bitrate_to_rate
  3. ath10k_mac_hw_rate_to_idx
  4. ath10k_mac_bitrate_to_idx
  5. ath10k_mac_get_rate_hw_value
  6. ath10k_mac_get_max_vht_mcs_map
  7. ath10k_mac_max_ht_nss
  8. ath10k_mac_max_vht_nss
  9. ath10k_mac_ext_resource_config
  10. ath10k_send_key
  11. ath10k_install_key
  12. ath10k_install_peer_wep_keys
  13. ath10k_clear_peer_keys
  14. ath10k_mac_is_peer_wep_key_set
  15. ath10k_clear_vdev_key
  16. ath10k_mac_vif_update_wep_key
  17. chan_to_phymode
  18. ath10k_parse_mpdudensity
  19. ath10k_mac_vif_chan
  20. ath10k_mac_num_chanctxs_iter
  21. ath10k_mac_num_chanctxs
  22. ath10k_mac_get_any_chandef_iter
  23. ath10k_wait_for_peer_delete_done
  24. ath10k_peer_create
  25. ath10k_mac_set_kickout
  26. ath10k_mac_set_rts
  27. ath10k_peer_delete
  28. ath10k_peer_cleanup
  29. ath10k_peer_cleanup_all
  30. ath10k_mac_tdls_peer_update
  31. ath10k_mac_vif_beacon_free
  32. ath10k_mac_vif_beacon_cleanup
  33. ath10k_vdev_setup_sync
  34. ath10k_monitor_vdev_start
  35. ath10k_monitor_vdev_stop
  36. ath10k_monitor_vdev_create
  37. ath10k_monitor_vdev_delete
  38. ath10k_monitor_start
  39. ath10k_monitor_stop
  40. ath10k_mac_monitor_vdev_is_needed
  41. ath10k_mac_monitor_vdev_is_allowed
  42. ath10k_monitor_recalc
  43. ath10k_mac_can_set_cts_prot
  44. ath10k_mac_set_cts_prot
  45. ath10k_recalc_rtscts_prot
  46. ath10k_start_cac
  47. ath10k_stop_cac
  48. ath10k_mac_has_radar_iter
  49. ath10k_mac_has_radar_enabled
  50. ath10k_recalc_radar_detection
  51. ath10k_vdev_stop
  52. ath10k_vdev_start_restart
  53. ath10k_vdev_start
  54. ath10k_vdev_restart
  55. ath10k_mac_setup_bcn_p2p_ie
  56. ath10k_mac_remove_vendor_ie
  57. ath10k_mac_setup_bcn_tmpl
  58. ath10k_mac_setup_prb_tmpl
  59. ath10k_mac_vif_fix_hidden_ssid
  60. ath10k_control_beaconing
  61. ath10k_control_ibss
  62. ath10k_mac_vif_recalc_ps_wake_threshold
  63. ath10k_mac_vif_recalc_ps_poll_count
  64. ath10k_mac_num_vifs_started
  65. ath10k_mac_vif_setup_ps
  66. ath10k_mac_vif_disable_keepalive
  67. ath10k_mac_vif_ap_csa_count_down
  68. ath10k_mac_vif_ap_csa_work
  69. ath10k_mac_handle_beacon_iter
  70. ath10k_mac_handle_beacon
  71. ath10k_mac_handle_beacon_miss_iter
  72. ath10k_mac_handle_beacon_miss
  73. ath10k_mac_vif_sta_connection_loss_work
  74. ath10k_peer_assoc_h_listen_intval
  75. ath10k_peer_assoc_h_basic
  76. ath10k_peer_assoc_h_crypto
  77. ath10k_peer_assoc_h_rates
  78. ath10k_peer_assoc_h_ht_masked
  79. ath10k_peer_assoc_h_vht_masked
  80. ath10k_peer_assoc_h_ht
  81. ath10k_peer_assoc_qos_ap
  82. ath10k_peer_assoc_h_vht_limit
  83. ath10k_peer_assoc_h_vht
  84. ath10k_peer_assoc_h_qos
  85. ath10k_mac_sta_has_ofdm_only
  86. ath10k_mac_get_phymode_vht
  87. ath10k_peer_assoc_h_phymode
  88. ath10k_peer_assoc_prepare
  89. ath10k_setup_peer_smps
  90. ath10k_mac_vif_recalc_txbf
  91. ath10k_bss_assoc
  92. ath10k_bss_disassoc
  93. ath10k_station_assoc
  94. ath10k_station_disassoc
  95. ath10k_update_channel_list
  96. ath10k_mac_get_dfs_region
  97. ath10k_regd_update
  98. ath10k_mac_update_channel_list
  99. ath10k_reg_notifier
  100. ath10k_stop_radar_confirmation
  101. ath10k_mac_tx_lock
  102. ath10k_mac_tx_unlock_iter
  103. ath10k_mac_tx_unlock
  104. ath10k_mac_vif_tx_lock
  105. ath10k_mac_vif_tx_unlock
  106. ath10k_mac_vif_handle_tx_pause
  107. ath10k_mac_handle_tx_pause_iter
  108. ath10k_mac_handle_tx_pause_vdev
  109. ath10k_mac_tx_h_get_txmode
  110. ath10k_tx_h_use_hwcrypto
  111. ath10k_tx_h_nwifi
  112. ath10k_tx_h_8023
  113. ath10k_tx_h_add_p2p_noa_ie
  114. ath10k_mac_tx_h_fill_cb
  115. ath10k_mac_tx_frm_has_freq
  116. ath10k_mac_tx_wmi_mgmt
  117. ath10k_mac_tx_h_get_txpath
  118. ath10k_mac_tx_submit
  119. ath10k_mac_tx
  120. ath10k_offchan_tx_purge
  121. ath10k_offchan_tx_work
  122. ath10k_mgmt_over_wmi_tx_purge
  123. ath10k_mgmt_over_wmi_tx_work
  124. ath10k_mac_txq_init
  125. ath10k_mac_txq_unref
  126. ath10k_mac_txq_lookup
  127. ath10k_mac_tx_can_push
  128. ath10k_mac_update_airtime
  129. ath10k_mac_tx_push_txq
  130. ath10k_mac_schedule_txq
  131. ath10k_mac_tx_push_pending
  132. __ath10k_scan_finish
  133. ath10k_scan_finish
  134. ath10k_scan_stop
  135. ath10k_scan_abort
  136. ath10k_scan_timeout_work
  137. ath10k_start_scan
  138. ath10k_mac_op_tx
  139. ath10k_mac_op_wake_tx_queue
  140. ath10k_drain_tx
  141. ath10k_halt
  142. ath10k_get_antenna
  143. ath10k_check_chain_mask
  144. ath10k_mac_get_vht_cap_bf_sts
  145. ath10k_mac_get_vht_cap_bf_sound_dim
  146. ath10k_create_vht_cap
  147. ath10k_get_ht_cap
  148. ath10k_mac_setup_ht_vht_cap
  149. __ath10k_set_antenna
  150. ath10k_set_antenna
  151. __ath10k_fetch_bb_timing_dt
  152. ath10k_start
  153. ath10k_stop
  154. ath10k_config_ps
  155. ath10k_mac_txpower_setup
  156. ath10k_mac_txpower_recalc
  157. ath10k_config
  158. get_nss_from_chainmask
  159. ath10k_mac_set_txbf_conf
  160. ath10k_add_interface
  161. ath10k_mac_vif_tx_unlock_all
  162. ath10k_remove_interface
  163. ath10k_configure_filter
  164. ath10k_bss_info_changed
  165. ath10k_mac_op_set_coverage_class
  166. ath10k_mac_tdls_vif_stations_count_iter
  167. ath10k_mac_tdls_vif_stations_count
  168. ath10k_hw_scan
  169. ath10k_cancel_hw_scan
  170. ath10k_set_key_h_def_keyidx
  171. ath10k_set_key
  172. ath10k_set_default_unicast_key
  173. ath10k_sta_rc_update_wk
  174. ath10k_mac_inc_num_stations
  175. ath10k_mac_dec_num_stations
  176. ath10k_sta_set_txpwr
  177. ath10k_sta_state
  178. ath10k_conf_tx_uapsd
  179. ath10k_conf_tx
  180. ath10k_remain_on_channel
  181. ath10k_cancel_remain_on_channel
  182. ath10k_set_rts_threshold
  183. ath10k_mac_op_set_frag_threshold
  184. ath10k_mac_wait_tx_complete
  185. ath10k_flush
  186. ath10k_tx_last_beacon
  187. ath10k_reconfig_complete
  188. ath10k_mac_update_bss_chan_survey
  189. ath10k_get_survey
  190. ath10k_mac_bitrate_mask_has_single_rate
  191. ath10k_mac_bitrate_mask_get_single_nss
  192. ath10k_mac_bitrate_mask_get_single_rate
  193. ath10k_mac_set_fixed_rate_params
  194. ath10k_mac_can_set_bitrate_mask
  195. ath10k_mac_set_vht_bitrate_mask_fixup
  196. ath10k_mac_set_bitrate_mask_iter
  197. ath10k_mac_clr_bitrate_mask_iter
  198. ath10k_mac_op_set_bitrate_mask
  199. ath10k_sta_rc_update
  200. ath10k_offset_tsf
  201. ath10k_ampdu_action
  202. ath10k_mac_update_rx_channel
  203. ath10k_mac_update_vif_chan
  204. ath10k_mac_op_add_chanctx
  205. ath10k_mac_op_remove_chanctx
  206. ath10k_mac_change_chanctx_cnt_iter
  207. ath10k_mac_change_chanctx_fill_iter
  208. ath10k_mac_op_change_chanctx
  209. ath10k_mac_op_assign_vif_chanctx
  210. ath10k_mac_op_unassign_vif_chanctx
  211. ath10k_mac_op_switch_vif_chanctx
  212. ath10k_mac_op_sta_pre_rcu_remove
  213. ath10k_sta_statistics
  214. ath10k_mac_create
  215. ath10k_mac_destroy
  216. ath10k_get_arvif_iter
  217. ath10k_get_arvif
  218. ath10k_mac_wrdd_get_mcc
  219. ath10k_mac_get_wrdd_regulatory
  220. ath10k_mac_init_rd
  221. ath10k_mac_register
  222. ath10k_mac_unregister

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

/* [<][>][^][v][top][bottom][index][help] */