root/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c

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

DEFINITIONS

This source file includes following definitions.
  1. iwl_mvm_reset_phy_ctxts
  2. iwl_mvm_get_regdomain
  3. iwl_mvm_update_changed_regdom
  4. iwl_mvm_get_current_regdomain
  5. iwl_mvm_init_fw_regd
  6. iwl_mvm_mac_setup_register
  7. iwl_mvm_tx_skb
  8. iwl_mvm_mac_tx
  9. iwl_mvm_mac_itxq_xmit
  10. iwl_mvm_mac_wake_tx_queue
  11. iwl_mvm_ampdu_check_trigger
  12. iwl_mvm_mac_ampdu_action
  13. iwl_mvm_cleanup_iterator
  14. iwl_mvm_restart_cleanup
  15. __iwl_mvm_mac_start
  16. iwl_mvm_mac_start
  17. iwl_mvm_restart_complete
  18. iwl_mvm_mac_reconfig_complete
  19. __iwl_mvm_mac_stop
  20. iwl_mvm_mac_stop
  21. iwl_mvm_get_free_phy_ctxt
  22. iwl_mvm_set_tx_power
  23. iwl_mvm_post_channel_switch
  24. iwl_mvm_abort_channel_switch
  25. iwl_mvm_channel_switch_disconnect_wk
  26. iwl_mvm_mac_add_interface
  27. iwl_mvm_prepare_mac_removal
  28. iwl_mvm_mac_remove_interface
  29. iwl_mvm_mac_config
  30. iwl_mvm_mc_iface_iterator
  31. iwl_mvm_recalc_multicast
  32. iwl_mvm_prepare_multicast
  33. iwl_mvm_configure_filter
  34. iwl_mvm_config_iface_filter
  35. iwl_mvm_set_bcast_filter
  36. iwl_mvm_bcast_filter_iterator
  37. iwl_mvm_bcast_filter_build_cmd
  38. iwl_mvm_configure_bcast_filter
  39. iwl_mvm_configure_bcast_filter
  40. iwl_mvm_update_mu_groups
  41. iwl_mvm_mu_mimo_iface_iterator
  42. iwl_mvm_mu_mimo_grp_notif
  43. iwl_mvm_he_get_ppe_val
  44. iwl_mvm_cfg_he_sta
  45. iwl_mvm_bss_info_changed_station
  46. iwl_mvm_start_ap_ibss
  47. iwl_mvm_stop_ap_ibss
  48. iwl_mvm_bss_info_changed_ap_ibss
  49. iwl_mvm_bss_info_changed
  50. iwl_mvm_mac_hw_scan
  51. iwl_mvm_mac_cancel_hw_scan
  52. iwl_mvm_mac_allow_buffered_frames
  53. iwl_mvm_mac_release_buffered_frames
  54. __iwl_mvm_mac_sta_notify
  55. iwl_mvm_mac_sta_notify
  56. iwl_mvm_sta_pm_notif
  57. iwl_mvm_sta_pre_rcu_remove
  58. iwl_mvm_check_uapsd
  59. iwl_mvm_tdls_check_trigger
  60. iwl_mvm_check_he_obss_narrow_bw_ru_iter
  61. iwl_mvm_check_he_obss_narrow_bw_ru
  62. iwl_mvm_mac_sta_state
  63. iwl_mvm_mac_set_rts_threshold
  64. iwl_mvm_sta_rc_update
  65. iwl_mvm_mac_conf_tx
  66. iwl_mvm_mac_mgd_prepare_tx
  67. iwl_mvm_mac_sched_scan_start
  68. iwl_mvm_mac_sched_scan_stop
  69. __iwl_mvm_mac_set_key
  70. iwl_mvm_mac_set_key
  71. iwl_mvm_mac_update_tkip_key
  72. iwl_mvm_rx_aux_roc
  73. iwl_mvm_send_aux_roc_cmd
  74. iwl_mvm_roc
  75. iwl_mvm_cancel_roc
  76. iwl_mvm_ftm_responder_chanctx_iter
  77. iwl_mvm_is_ftm_responder_chanctx
  78. __iwl_mvm_add_chanctx
  79. iwl_mvm_add_chanctx
  80. __iwl_mvm_remove_chanctx
  81. iwl_mvm_remove_chanctx
  82. iwl_mvm_change_chanctx
  83. __iwl_mvm_assign_vif_chanctx
  84. iwl_mvm_assign_vif_chanctx
  85. __iwl_mvm_unassign_vif_chanctx
  86. iwl_mvm_unassign_vif_chanctx
  87. iwl_mvm_switch_vif_chanctx_swap
  88. iwl_mvm_switch_vif_chanctx_reassign
  89. iwl_mvm_switch_vif_chanctx
  90. iwl_mvm_tx_last_beacon
  91. iwl_mvm_set_tim
  92. __iwl_mvm_mac_testmode_cmd
  93. iwl_mvm_mac_testmode_cmd
  94. iwl_mvm_channel_switch
  95. iwl_mvm_schedule_client_csa
  96. iwl_mvm_old_pre_chan_sw_sta
  97. iwl_mvm_pre_channel_switch
  98. iwl_mvm_channel_switch_rx_beacon
  99. iwl_mvm_flush_no_vif
  100. iwl_mvm_mac_flush
  101. iwl_mvm_mac_get_survey
  102. iwl_mvm_mac_sta_statistics
  103. iwl_mvm_event_mlme_callback
  104. iwl_mvm_event_bar_rx_callback
  105. iwl_mvm_mac_event_callback
  106. iwl_mvm_sync_rx_queues_internal
  107. iwl_mvm_sync_rx_queues
  108. iwl_mvm_mac_get_ftm_responder_stats
  109. iwl_mvm_start_pmsr
  110. iwl_mvm_abort_pmsr
  111. iwl_mvm_can_hw_csum
  112. iwl_mvm_mac_can_aggregate

   1 /******************************************************************************
   2  *
   3  * This file is provided under a dual BSD/GPLv2 license.  When using or
   4  * redistributing this file, you may do so under either license.
   5  *
   6  * GPL LICENSE SUMMARY
   7  *
   8  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
   9  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  10  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
  11  *
  12  * This program is free software; you can redistribute it and/or modify
  13  * it under the terms of version 2 of the GNU General Public License as
  14  * published by the Free Software Foundation.
  15  *
  16  * This program is distributed in the hope that it will be useful, but
  17  * WITHOUT ANY WARRANTY; without even the implied warranty of
  18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19  * General Public License for more details.
  20  *
  21  * The full GNU General Public License is included in this distribution
  22  * in the file called COPYING.
  23  *
  24  * Contact Information:
  25  *  Intel Linux Wireless <linuxwifi@intel.com>
  26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27  *
  28  * BSD LICENSE
  29  *
  30  * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  31  * Copyright(c) 2016 - 2017 Intel Deutschland GmbH
  32  * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
  33  * All rights reserved.
  34  *
  35  * Redistribution and use in source and binary forms, with or without
  36  * modification, are permitted provided that the following conditions
  37  * are met:
  38  *
  39  *  * Redistributions of source code must retain the above copyright
  40  *    notice, this list of conditions and the following disclaimer.
  41  *  * Redistributions in binary form must reproduce the above copyright
  42  *    notice, this list of conditions and the following disclaimer in
  43  *    the documentation and/or other materials provided with the
  44  *    distribution.
  45  *  * Neither the name Intel Corporation nor the names of its
  46  *    contributors may be used to endorse or promote products derived
  47  *    from this software without specific prior written permission.
  48  *
  49  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  50  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  51  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  52  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  53  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  56  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  57  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  58  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60  *
  61  *****************************************************************************/
  62 #include <linux/kernel.h>
  63 #include <linux/slab.h>
  64 #include <linux/skbuff.h>
  65 #include <linux/netdevice.h>
  66 #include <linux/etherdevice.h>
  67 #include <linux/ip.h>
  68 #include <linux/if_arp.h>
  69 #include <linux/time.h>
  70 #include <net/mac80211.h>
  71 #include <net/ieee80211_radiotap.h>
  72 #include <net/tcp.h>
  73 
  74 #include "iwl-op-mode.h"
  75 #include "iwl-io.h"
  76 #include "mvm.h"
  77 #include "sta.h"
  78 #include "time-event.h"
  79 #include "iwl-eeprom-parse.h"
  80 #include "iwl-phy-db.h"
  81 #include "testmode.h"
  82 #include "fw/error-dump.h"
  83 #include "iwl-prph.h"
  84 #include "iwl-nvm-parse.h"
  85 
  86 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
  87         {
  88                 .max = 1,
  89                 .types = BIT(NL80211_IFTYPE_STATION),
  90         },
  91         {
  92                 .max = 1,
  93                 .types = BIT(NL80211_IFTYPE_AP) |
  94                         BIT(NL80211_IFTYPE_P2P_CLIENT) |
  95                         BIT(NL80211_IFTYPE_P2P_GO),
  96         },
  97         {
  98                 .max = 1,
  99                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),
 100         },
 101 };
 102 
 103 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
 104         {
 105                 .num_different_channels = 2,
 106                 .max_interfaces = 3,
 107                 .limits = iwl_mvm_limits,
 108                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
 109         },
 110 };
 111 
 112 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
 113 /*
 114  * Use the reserved field to indicate magic values.
 115  * these values will only be used internally by the driver,
 116  * and won't make it to the fw (reserved will be 0).
 117  * BC_FILTER_MAGIC_IP - configure the val of this attribute to
 118  *      be the vif's ip address. in case there is not a single
 119  *      ip address (0, or more than 1), this attribute will
 120  *      be skipped.
 121  * BC_FILTER_MAGIC_MAC - set the val of this attribute to
 122  *      the LSB bytes of the vif's mac address
 123  */
 124 enum {
 125         BC_FILTER_MAGIC_NONE = 0,
 126         BC_FILTER_MAGIC_IP,
 127         BC_FILTER_MAGIC_MAC,
 128 };
 129 
 130 static const struct iwl_fw_bcast_filter iwl_mvm_default_bcast_filters[] = {
 131         {
 132                 /* arp */
 133                 .discard = 0,
 134                 .frame_type = BCAST_FILTER_FRAME_TYPE_ALL,
 135                 .attrs = {
 136                         {
 137                                 /* frame type - arp, hw type - ethernet */
 138                                 .offset_type =
 139                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
 140                                 .offset = sizeof(rfc1042_header),
 141                                 .val = cpu_to_be32(0x08060001),
 142                                 .mask = cpu_to_be32(0xffffffff),
 143                         },
 144                         {
 145                                 /* arp dest ip */
 146                                 .offset_type =
 147                                         BCAST_FILTER_OFFSET_PAYLOAD_START,
 148                                 .offset = sizeof(rfc1042_header) + 2 +
 149                                           sizeof(struct arphdr) +
 150                                           ETH_ALEN + sizeof(__be32) +
 151                                           ETH_ALEN,
 152                                 .mask = cpu_to_be32(0xffffffff),
 153                                 /* mark it as special field */
 154                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_IP),
 155                         },
 156                 },
 157         },
 158         {
 159                 /* dhcp offer bcast */
 160                 .discard = 0,
 161                 .frame_type = BCAST_FILTER_FRAME_TYPE_IPV4,
 162                 .attrs = {
 163                         {
 164                                 /* udp dest port - 68 (bootp client)*/
 165                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
 166                                 .offset = offsetof(struct udphdr, dest),
 167                                 .val = cpu_to_be32(0x00440000),
 168                                 .mask = cpu_to_be32(0xffff0000),
 169                         },
 170                         {
 171                                 /* dhcp - lsb bytes of client hw address */
 172                                 .offset_type = BCAST_FILTER_OFFSET_IP_END,
 173                                 .offset = 38,
 174                                 .mask = cpu_to_be32(0xffffffff),
 175                                 /* mark it as special field */
 176                                 .reserved1 = cpu_to_le16(BC_FILTER_MAGIC_MAC),
 177                         },
 178                 },
 179         },
 180         /* last filter must be empty */
 181         {},
 182 };
 183 #endif
 184 
 185 static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
 186         .max_peers = IWL_MVM_TOF_MAX_APS,
 187         .report_ap_tsf = 1,
 188         .randomize_mac_addr = 1,
 189 
 190         .ftm = {
 191                 .supported = 1,
 192                 .asap = 1,
 193                 .non_asap = 1,
 194                 .request_lci = 1,
 195                 .request_civicloc = 1,
 196                 .max_bursts_exponent = -1, /* all supported */
 197                 .max_ftms_per_burst = 0, /* no limits */
 198                 .bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
 199                               BIT(NL80211_CHAN_WIDTH_20) |
 200                               BIT(NL80211_CHAN_WIDTH_40) |
 201                               BIT(NL80211_CHAN_WIDTH_80),
 202                 .preambles = BIT(NL80211_PREAMBLE_LEGACY) |
 203                              BIT(NL80211_PREAMBLE_HT) |
 204                              BIT(NL80211_PREAMBLE_VHT),
 205         },
 206 };
 207 
 208 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
 209                                  enum set_key_cmd cmd,
 210                                  struct ieee80211_vif *vif,
 211                                  struct ieee80211_sta *sta,
 212                                  struct ieee80211_key_conf *key);
 213 
 214 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
 215 {
 216         int i;
 217 
 218         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
 219         for (i = 0; i < NUM_PHY_CTX; i++) {
 220                 mvm->phy_ctxts[i].id = i;
 221                 mvm->phy_ctxts[i].ref = 0;
 222         }
 223 }
 224 
 225 struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
 226                                                   const char *alpha2,
 227                                                   enum iwl_mcc_source src_id,
 228                                                   bool *changed)
 229 {
 230         struct ieee80211_regdomain *regd = NULL;
 231         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
 232         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 233         struct iwl_mcc_update_resp *resp;
 234 
 235         IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
 236 
 237         lockdep_assert_held(&mvm->mutex);
 238 
 239         resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
 240         if (IS_ERR_OR_NULL(resp)) {
 241                 IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
 242                               PTR_ERR_OR_ZERO(resp));
 243                 goto out;
 244         }
 245 
 246         if (changed) {
 247                 u32 status = le32_to_cpu(resp->status);
 248 
 249                 *changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
 250                             status == MCC_RESP_ILLEGAL);
 251         }
 252 
 253         regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
 254                                       __le32_to_cpu(resp->n_channels),
 255                                       resp->channels,
 256                                       __le16_to_cpu(resp->mcc),
 257                                       __le16_to_cpu(resp->geo_info),
 258                                       __le16_to_cpu(resp->cap));
 259         /* Store the return source id */
 260         src_id = resp->source_id;
 261         kfree(resp);
 262         if (IS_ERR_OR_NULL(regd)) {
 263                 IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
 264                               PTR_ERR_OR_ZERO(regd));
 265                 goto out;
 266         }
 267 
 268         IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
 269                       regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
 270         mvm->lar_regdom_set = true;
 271         mvm->mcc_src = src_id;
 272 
 273 out:
 274         return regd;
 275 }
 276 
 277 void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
 278 {
 279         bool changed;
 280         struct ieee80211_regdomain *regd;
 281 
 282         if (!iwl_mvm_is_lar_supported(mvm))
 283                 return;
 284 
 285         regd = iwl_mvm_get_current_regdomain(mvm, &changed);
 286         if (!IS_ERR_OR_NULL(regd)) {
 287                 /* only update the regulatory core if changed */
 288                 if (changed)
 289                         regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
 290 
 291                 kfree(regd);
 292         }
 293 }
 294 
 295 struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
 296                                                           bool *changed)
 297 {
 298         return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
 299                                      iwl_mvm_is_wifi_mcc_supported(mvm) ?
 300                                      MCC_SOURCE_GET_CURRENT :
 301                                      MCC_SOURCE_OLD_FW, changed);
 302 }
 303 
 304 int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm)
 305 {
 306         enum iwl_mcc_source used_src;
 307         struct ieee80211_regdomain *regd;
 308         int ret;
 309         bool changed;
 310         const struct ieee80211_regdomain *r =
 311                         rtnl_dereference(mvm->hw->wiphy->regd);
 312 
 313         if (!r)
 314                 return -ENOENT;
 315 
 316         /* save the last source in case we overwrite it below */
 317         used_src = mvm->mcc_src;
 318         if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
 319                 /* Notify the firmware we support wifi location updates */
 320                 regd = iwl_mvm_get_current_regdomain(mvm, NULL);
 321                 if (!IS_ERR_OR_NULL(regd))
 322                         kfree(regd);
 323         }
 324 
 325         /* Now set our last stored MCC and source */
 326         regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
 327                                      &changed);
 328         if (IS_ERR_OR_NULL(regd))
 329                 return -EIO;
 330 
 331         /* update cfg80211 if the regdomain was changed */
 332         if (changed)
 333                 ret = regulatory_set_wiphy_regd_sync_rtnl(mvm->hw->wiphy, regd);
 334         else
 335                 ret = 0;
 336 
 337         kfree(regd);
 338         return ret;
 339 }
 340 
 341 const static u8 he_if_types_ext_capa_sta[] = {
 342          [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
 343          [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
 344          [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF,
 345          [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
 346 };
 347 
 348 const static struct wiphy_iftype_ext_capab he_iftypes_ext_capa[] = {
 349         {
 350                 .iftype = NL80211_IFTYPE_STATION,
 351                 .extended_capabilities = he_if_types_ext_capa_sta,
 352                 .extended_capabilities_mask = he_if_types_ext_capa_sta,
 353                 .extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
 354         },
 355 };
 356 
 357 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
 358 {
 359         struct ieee80211_hw *hw = mvm->hw;
 360         int num_mac, ret, i;
 361         static const u32 mvm_ciphers[] = {
 362                 WLAN_CIPHER_SUITE_WEP40,
 363                 WLAN_CIPHER_SUITE_WEP104,
 364                 WLAN_CIPHER_SUITE_TKIP,
 365                 WLAN_CIPHER_SUITE_CCMP,
 366         };
 367 #ifdef CONFIG_PM_SLEEP
 368         bool unified = fw_has_capa(&mvm->fw->ucode_capa,
 369                                    IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
 370 #endif
 371 
 372         /* Tell mac80211 our characteristics */
 373         ieee80211_hw_set(hw, SIGNAL_DBM);
 374         ieee80211_hw_set(hw, SPECTRUM_MGMT);
 375         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
 376         ieee80211_hw_set(hw, WANT_MONITOR_VIF);
 377         ieee80211_hw_set(hw, SUPPORTS_PS);
 378         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
 379         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
 380         ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
 381         ieee80211_hw_set(hw, CONNECTION_MONITOR);
 382         ieee80211_hw_set(hw, CHANCTX_STA_CSA);
 383         ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
 384         ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
 385         ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
 386         ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
 387         ieee80211_hw_set(hw, DEAUTH_NEED_MGD_TX_PREP);
 388         ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
 389         ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
 390         ieee80211_hw_set(hw, STA_MMPDU_TXQ);
 391         /*
 392          * On older devices, enabling TX A-MSDU occasionally leads to
 393          * something getting messed up, the command read from the FIFO
 394          * gets out of sync and isn't a TX command, so that we have an
 395          * assert EDC.
 396          *
 397          * It's not clear where the bug is, but since we didn't used to
 398          * support A-MSDU until moving the mac80211 iTXQs, just leave it
 399          * for older devices. We also don't see this issue on any newer
 400          * devices.
 401          */
 402         if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
 403                 ieee80211_hw_set(hw, TX_AMSDU);
 404         ieee80211_hw_set(hw, TX_FRAG_LIST);
 405 
 406         if (iwl_mvm_has_tlc_offload(mvm)) {
 407                 ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
 408                 ieee80211_hw_set(hw, HAS_RATE_CONTROL);
 409         }
 410 
 411         if (iwl_mvm_has_new_rx_api(mvm))
 412                 ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
 413 
 414         if (fw_has_capa(&mvm->fw->ucode_capa,
 415                         IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
 416                 ieee80211_hw_set(hw, AP_LINK_PS);
 417         } else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
 418                 /*
 419                  * we absolutely need this for the new TX API since that comes
 420                  * with many more queues than the current code can deal with
 421                  * for station powersave
 422                  */
 423                 return -EINVAL;
 424         }
 425 
 426         if (mvm->trans->num_rx_queues > 1)
 427                 ieee80211_hw_set(hw, USES_RSS);
 428 
 429         if (mvm->trans->max_skb_frags)
 430                 hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
 431 
 432         hw->queues = IEEE80211_MAX_QUEUES;
 433         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
 434         hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
 435                                     IEEE80211_RADIOTAP_MCS_HAVE_STBC;
 436         hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
 437                 IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
 438 
 439         hw->radiotap_timestamp.units_pos =
 440                 IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
 441                 IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
 442         /* this is the case for CCK frames, it's better (only 8) for OFDM */
 443         hw->radiotap_timestamp.accuracy = 22;
 444 
 445         if (!iwl_mvm_has_tlc_offload(mvm))
 446                 hw->rate_control_algorithm = RS_NAME;
 447 
 448         hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
 449         hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
 450         hw->max_tx_fragments = mvm->trans->max_skb_frags;
 451 
 452         BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
 453         memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
 454         hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
 455         hw->wiphy->cipher_suites = mvm->ciphers;
 456 
 457         if (iwl_mvm_has_new_rx_api(mvm)) {
 458                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
 459                         WLAN_CIPHER_SUITE_GCMP;
 460                 hw->wiphy->n_cipher_suites++;
 461                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
 462                         WLAN_CIPHER_SUITE_GCMP_256;
 463                 hw->wiphy->n_cipher_suites++;
 464         }
 465 
 466         /* Enable 11w if software crypto is not enabled (as the
 467          * firmware will interpret some mgmt packets, so enabling it
 468          * with software crypto isn't safe).
 469          */
 470         if (!iwlwifi_mod_params.swcrypto) {
 471                 ieee80211_hw_set(hw, MFP_CAPABLE);
 472                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
 473                         WLAN_CIPHER_SUITE_AES_CMAC;
 474                 hw->wiphy->n_cipher_suites++;
 475                 if (iwl_mvm_has_new_rx_api(mvm)) {
 476                         mvm->ciphers[hw->wiphy->n_cipher_suites] =
 477                                 WLAN_CIPHER_SUITE_BIP_GMAC_128;
 478                         hw->wiphy->n_cipher_suites++;
 479                         mvm->ciphers[hw->wiphy->n_cipher_suites] =
 480                                 WLAN_CIPHER_SUITE_BIP_GMAC_256;
 481                         hw->wiphy->n_cipher_suites++;
 482                 }
 483         }
 484 
 485         /* currently FW API supports only one optional cipher scheme */
 486         if (mvm->fw->cs[0].cipher) {
 487                 const struct iwl_fw_cipher_scheme *fwcs = &mvm->fw->cs[0];
 488                 struct ieee80211_cipher_scheme *cs = &mvm->cs[0];
 489 
 490                 mvm->hw->n_cipher_schemes = 1;
 491 
 492                 cs->cipher = le32_to_cpu(fwcs->cipher);
 493                 cs->iftype = BIT(NL80211_IFTYPE_STATION);
 494                 cs->hdr_len = fwcs->hdr_len;
 495                 cs->pn_len = fwcs->pn_len;
 496                 cs->pn_off = fwcs->pn_off;
 497                 cs->key_idx_off = fwcs->key_idx_off;
 498                 cs->key_idx_mask = fwcs->key_idx_mask;
 499                 cs->key_idx_shift = fwcs->key_idx_shift;
 500                 cs->mic_len = fwcs->mic_len;
 501 
 502                 mvm->hw->cipher_schemes = mvm->cs;
 503                 mvm->ciphers[hw->wiphy->n_cipher_suites] = cs->cipher;
 504                 hw->wiphy->n_cipher_suites++;
 505         }
 506 
 507         if (fw_has_capa(&mvm->fw->ucode_capa,
 508                         IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
 509                 wiphy_ext_feature_set(hw->wiphy,
 510                                       NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
 511                 hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
 512         }
 513 
 514         ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
 515         hw->wiphy->features |=
 516                 NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
 517                 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
 518                 NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
 519 
 520         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
 521         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
 522         hw->chanctx_data_size = sizeof(u16);
 523         hw->txq_data_size = sizeof(struct iwl_mvm_txq);
 524 
 525         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
 526                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
 527                 BIT(NL80211_IFTYPE_AP) |
 528                 BIT(NL80211_IFTYPE_P2P_GO) |
 529                 BIT(NL80211_IFTYPE_P2P_DEVICE) |
 530                 BIT(NL80211_IFTYPE_ADHOC);
 531 
 532         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
 533         wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
 534         hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
 535 
 536         hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
 537         if (iwl_mvm_is_lar_supported(mvm))
 538                 hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
 539         else
 540                 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
 541                                                REGULATORY_DISABLE_BEACON_HINTS;
 542 
 543         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
 544         hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
 545 
 546         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
 547         hw->wiphy->n_iface_combinations =
 548                 ARRAY_SIZE(iwl_mvm_iface_combinations);
 549 
 550         hw->wiphy->max_remain_on_channel_duration = 10000;
 551         hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
 552 
 553         /* Extract MAC address */
 554         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
 555         hw->wiphy->addresses = mvm->addresses;
 556         hw->wiphy->n_addresses = 1;
 557 
 558         /* Extract additional MAC addresses if available */
 559         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
 560                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
 561 
 562         for (i = 1; i < num_mac; i++) {
 563                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
 564                        ETH_ALEN);
 565                 mvm->addresses[i].addr[5]++;
 566                 hw->wiphy->n_addresses++;
 567         }
 568 
 569         iwl_mvm_reset_phy_ctxts(mvm);
 570 
 571         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
 572 
 573         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
 574 
 575         BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
 576         BUILD_BUG_ON(IWL_MVM_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
 577                      IWL_MVM_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
 578 
 579         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
 580                 mvm->max_scans = IWL_MVM_MAX_UMAC_SCANS;
 581         else
 582                 mvm->max_scans = IWL_MVM_MAX_LMAC_SCANS;
 583 
 584         if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
 585                 hw->wiphy->bands[NL80211_BAND_2GHZ] =
 586                         &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
 587         if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
 588                 hw->wiphy->bands[NL80211_BAND_5GHZ] =
 589                         &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
 590 
 591                 if (fw_has_capa(&mvm->fw->ucode_capa,
 592                                 IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
 593                     fw_has_api(&mvm->fw->ucode_capa,
 594                                IWL_UCODE_TLV_API_LQ_SS_PARAMS))
 595                         hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
 596                                 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
 597         }
 598 
 599         hw->wiphy->hw_version = mvm->trans->hw_id;
 600 
 601         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
 602                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
 603         else
 604                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
 605 
 606         hw->wiphy->max_sched_scan_reqs = 1;
 607         hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
 608         hw->wiphy->max_match_sets = IWL_SCAN_MAX_PROFILES;
 609         /* we create the 802.11 header and zero length SSID IE. */
 610         hw->wiphy->max_sched_scan_ie_len =
 611                 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
 612         hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
 613         hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
 614 
 615         /*
 616          * the firmware uses u8 for num of iterations, but 0xff is saved for
 617          * infinite loop, so the maximum number of iterations is actually 254.
 618          */
 619         hw->wiphy->max_sched_scan_plan_iterations = 254;
 620 
 621         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
 622                                NL80211_FEATURE_LOW_PRIORITY_SCAN |
 623                                NL80211_FEATURE_P2P_GO_OPPPS |
 624                                NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
 625                                NL80211_FEATURE_DYNAMIC_SMPS |
 626                                NL80211_FEATURE_STATIC_SMPS |
 627                                NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
 628 
 629         if (fw_has_capa(&mvm->fw->ucode_capa,
 630                         IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
 631                 hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
 632         if (fw_has_capa(&mvm->fw->ucode_capa,
 633                         IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
 634                 hw->wiphy->features |= NL80211_FEATURE_QUIET;
 635 
 636         if (fw_has_capa(&mvm->fw->ucode_capa,
 637                         IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
 638                 hw->wiphy->features |=
 639                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
 640 
 641         if (fw_has_capa(&mvm->fw->ucode_capa,
 642                         IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
 643                 hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
 644 
 645         if (fw_has_api(&mvm->fw->ucode_capa,
 646                        IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
 647                 wiphy_ext_feature_set(hw->wiphy,
 648                                       NL80211_EXT_FEATURE_SCAN_START_TIME);
 649                 wiphy_ext_feature_set(hw->wiphy,
 650                                       NL80211_EXT_FEATURE_BSS_PARENT_TSF);
 651                 wiphy_ext_feature_set(hw->wiphy,
 652                                       NL80211_EXT_FEATURE_SET_SCAN_DWELL);
 653         }
 654 
 655         if (iwl_mvm_is_oce_supported(mvm)) {
 656                 wiphy_ext_feature_set(hw->wiphy,
 657                         NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
 658                 wiphy_ext_feature_set(hw->wiphy,
 659                         NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
 660                 wiphy_ext_feature_set(hw->wiphy,
 661                         NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
 662                 wiphy_ext_feature_set(hw->wiphy,
 663                         NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
 664         }
 665 
 666         if (mvm->nvm_data->sku_cap_11ax_enable &&
 667             !iwlwifi_mod_params.disable_11ax) {
 668                 hw->wiphy->iftype_ext_capab = he_iftypes_ext_capa;
 669                 hw->wiphy->num_iftype_ext_capab =
 670                         ARRAY_SIZE(he_iftypes_ext_capa);
 671 
 672                 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
 673                 ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
 674         }
 675 
 676         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
 677 
 678 #ifdef CONFIG_PM_SLEEP
 679         if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
 680             mvm->trans->ops->d3_suspend &&
 681             mvm->trans->ops->d3_resume &&
 682             device_can_wakeup(mvm->trans->dev)) {
 683                 mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
 684                                      WIPHY_WOWLAN_DISCONNECT |
 685                                      WIPHY_WOWLAN_EAP_IDENTITY_REQ |
 686                                      WIPHY_WOWLAN_RFKILL_RELEASE |
 687                                      WIPHY_WOWLAN_NET_DETECT;
 688                 if (!iwlwifi_mod_params.swcrypto)
 689                         mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
 690                                              WIPHY_WOWLAN_GTK_REKEY_FAILURE |
 691                                              WIPHY_WOWLAN_4WAY_HANDSHAKE;
 692 
 693                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
 694                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
 695                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
 696                 mvm->wowlan.max_nd_match_sets = IWL_SCAN_MAX_PROFILES;
 697                 hw->wiphy->wowlan = &mvm->wowlan;
 698         }
 699 #endif
 700 
 701 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
 702         /* assign default bcast filtering configuration */
 703         mvm->bcast_filters = iwl_mvm_default_bcast_filters;
 704 #endif
 705 
 706         ret = iwl_mvm_leds_init(mvm);
 707         if (ret)
 708                 return ret;
 709 
 710         if (fw_has_capa(&mvm->fw->ucode_capa,
 711                         IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
 712                 IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
 713                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
 714                 ieee80211_hw_set(hw, TDLS_WIDER_BW);
 715         }
 716 
 717         if (fw_has_capa(&mvm->fw->ucode_capa,
 718                         IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
 719                 IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
 720                 hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
 721         }
 722 
 723         hw->netdev_features |= mvm->cfg->features;
 724         if (!iwl_mvm_is_csum_supported(mvm)) {
 725                 hw->netdev_features &= ~(IWL_TX_CSUM_NETIF_FLAGS |
 726                                          NETIF_F_RXCSUM);
 727                 /* We may support SW TX CSUM */
 728                 if (IWL_MVM_SW_TX_CSUM_OFFLOAD)
 729                         hw->netdev_features |= IWL_TX_CSUM_NETIF_FLAGS;
 730         }
 731 
 732         if (mvm->cfg->vht_mu_mimo_supported)
 733                 wiphy_ext_feature_set(hw->wiphy,
 734                                       NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
 735 
 736         ret = ieee80211_register_hw(mvm->hw);
 737         if (ret) {
 738                 iwl_mvm_leds_exit(mvm);
 739         }
 740 
 741         return ret;
 742 }
 743 
 744 static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
 745                            struct ieee80211_sta *sta)
 746 {
 747         if (likely(sta)) {
 748                 if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
 749                         return;
 750         } else {
 751                 if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
 752                         return;
 753         }
 754 
 755         ieee80211_free_txskb(mvm->hw, skb);
 756 }
 757 
 758 static void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
 759                            struct ieee80211_tx_control *control,
 760                            struct sk_buff *skb)
 761 {
 762         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 763         struct ieee80211_sta *sta = control->sta;
 764         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
 765         struct ieee80211_hdr *hdr = (void *)skb->data;
 766         bool offchannel = IEEE80211_SKB_CB(skb)->flags &
 767                 IEEE80211_TX_CTL_TX_OFFCHAN;
 768 
 769         if (iwl_mvm_is_radio_killed(mvm)) {
 770                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
 771                 goto drop;
 772         }
 773 
 774         if (offchannel &&
 775             !test_bit(IWL_MVM_STATUS_ROC_RUNNING, &mvm->status) &&
 776             !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
 777                 goto drop;
 778 
 779         /* treat non-bufferable MMPDUs on AP interfaces as broadcast */
 780         if ((info->control.vif->type == NL80211_IFTYPE_AP ||
 781              info->control.vif->type == NL80211_IFTYPE_ADHOC) &&
 782             ieee80211_is_mgmt(hdr->frame_control) &&
 783             !ieee80211_is_bufferable_mmpdu(hdr->frame_control))
 784                 sta = NULL;
 785 
 786         /* If there is no sta, and it's not offchannel - send through AP */
 787         if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
 788             !offchannel) {
 789                 struct iwl_mvm_vif *mvmvif =
 790                         iwl_mvm_vif_from_mac80211(info->control.vif);
 791                 u8 ap_sta_id = READ_ONCE(mvmvif->ap_sta_id);
 792 
 793                 if (ap_sta_id < IWL_MVM_STATION_COUNT) {
 794                         /* mac80211 holds rcu read lock */
 795                         sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
 796                         if (IS_ERR_OR_NULL(sta))
 797                                 goto drop;
 798                 }
 799         }
 800 
 801         iwl_mvm_tx_skb(mvm, skb, sta);
 802         return;
 803  drop:
 804         ieee80211_free_txskb(hw, skb);
 805 }
 806 
 807 void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
 808 {
 809         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 810         struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
 811         struct sk_buff *skb = NULL;
 812 
 813         /*
 814          * No need for threads to be pending here, they can leave the first
 815          * taker all the work.
 816          *
 817          * mvmtxq->tx_request logic:
 818          *
 819          * If 0, no one is currently TXing, set to 1 to indicate current thread
 820          * will now start TX and other threads should quit.
 821          *
 822          * If 1, another thread is currently TXing, set to 2 to indicate to
 823          * that thread that there was another request. Since that request may
 824          * have raced with the check whether the queue is empty, the TXing
 825          * thread should check the queue's status one more time before leaving.
 826          * This check is done in order to not leave any TX hanging in the queue
 827          * until the next TX invocation (which may not even happen).
 828          *
 829          * If 2, another thread is currently TXing, and it will already double
 830          * check the queue, so do nothing.
 831          */
 832         if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
 833                 return;
 834 
 835         rcu_read_lock();
 836         do {
 837                 while (likely(!mvmtxq->stopped &&
 838                               (mvm->trans->system_pm_mode ==
 839                                IWL_PLAT_PM_MODE_DISABLED))) {
 840                         skb = ieee80211_tx_dequeue(hw, txq);
 841 
 842                         if (!skb) {
 843                                 if (txq->sta)
 844                                         IWL_DEBUG_TX(mvm,
 845                                                      "TXQ of sta %pM tid %d is now empty\n",
 846                                                      txq->sta->addr,
 847                                                      txq->tid);
 848                                 break;
 849                         }
 850 
 851                         iwl_mvm_tx_skb(mvm, skb, txq->sta);
 852                 }
 853         } while (atomic_dec_return(&mvmtxq->tx_request));
 854         rcu_read_unlock();
 855 }
 856 
 857 static void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
 858                                       struct ieee80211_txq *txq)
 859 {
 860         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 861         struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
 862 
 863         /*
 864          * Please note that racing is handled very carefully here:
 865          * mvmtxq->txq_id is updated during allocation, and mvmtxq->list is
 866          * deleted afterwards.
 867          * This means that if:
 868          * mvmtxq->txq_id != INVALID_QUEUE && list_empty(&mvmtxq->list):
 869          *      queue is allocated and we can TX.
 870          * mvmtxq->txq_id != INVALID_QUEUE && !list_empty(&mvmtxq->list):
 871          *      a race, should defer the frame.
 872          * mvmtxq->txq_id == INVALID_QUEUE && list_empty(&mvmtxq->list):
 873          *      need to allocate the queue and defer the frame.
 874          * mvmtxq->txq_id == INVALID_QUEUE && !list_empty(&mvmtxq->list):
 875          *      queue is already scheduled for allocation, no need to allocate,
 876          *      should defer the frame.
 877          */
 878 
 879         /* If the queue is allocated TX and return. */
 880         if (!txq->sta || mvmtxq->txq_id != IWL_MVM_INVALID_QUEUE) {
 881                 /*
 882                  * Check that list is empty to avoid a race where txq_id is
 883                  * already updated, but the queue allocation work wasn't
 884                  * finished
 885                  */
 886                 if (unlikely(txq->sta && !list_empty(&mvmtxq->list)))
 887                         return;
 888 
 889                 iwl_mvm_mac_itxq_xmit(hw, txq);
 890                 return;
 891         }
 892 
 893         /* The list is being deleted only after the queue is fully allocated. */
 894         if (!list_empty(&mvmtxq->list))
 895                 return;
 896 
 897         list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
 898         schedule_work(&mvm->add_stream_wk);
 899 }
 900 
 901 #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)           \
 902         do {                                                            \
 903                 if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))                \
 904                         break;                                          \
 905                 iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);    \
 906         } while (0)
 907 
 908 static void
 909 iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
 910                             struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
 911                             enum ieee80211_ampdu_mlme_action action)
 912 {
 913         struct iwl_fw_dbg_trigger_tlv *trig;
 914         struct iwl_fw_dbg_trigger_ba *ba_trig;
 915 
 916         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
 917                                      FW_DBG_TRIGGER_BA);
 918         if (!trig)
 919                 return;
 920 
 921         ba_trig = (void *)trig->data;
 922 
 923         switch (action) {
 924         case IEEE80211_AMPDU_TX_OPERATIONAL: {
 925                 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
 926                 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
 927 
 928                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
 929                                  "TX AGG START: MAC %pM tid %d ssn %d\n",
 930                                  sta->addr, tid, tid_data->ssn);
 931                 break;
 932                 }
 933         case IEEE80211_AMPDU_TX_STOP_CONT:
 934                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
 935                                  "TX AGG STOP: MAC %pM tid %d\n",
 936                                  sta->addr, tid);
 937                 break;
 938         case IEEE80211_AMPDU_RX_START:
 939                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
 940                                  "RX AGG START: MAC %pM tid %d ssn %d\n",
 941                                  sta->addr, tid, rx_ba_ssn);
 942                 break;
 943         case IEEE80211_AMPDU_RX_STOP:
 944                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
 945                                  "RX AGG STOP: MAC %pM tid %d\n",
 946                                  sta->addr, tid);
 947                 break;
 948         default:
 949                 break;
 950         }
 951 }
 952 
 953 static int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
 954                                     struct ieee80211_vif *vif,
 955                                     struct ieee80211_ampdu_params *params)
 956 {
 957         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
 958         int ret;
 959         struct ieee80211_sta *sta = params->sta;
 960         enum ieee80211_ampdu_mlme_action action = params->action;
 961         u16 tid = params->tid;
 962         u16 *ssn = &params->ssn;
 963         u16 buf_size = params->buf_size;
 964         bool amsdu = params->amsdu;
 965         u16 timeout = params->timeout;
 966 
 967         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
 968                      sta->addr, tid, action);
 969 
 970         if (!(mvm->nvm_data->sku_cap_11n_enable))
 971                 return -EACCES;
 972 
 973         mutex_lock(&mvm->mutex);
 974 
 975         switch (action) {
 976         case IEEE80211_AMPDU_RX_START:
 977                 if (iwl_mvm_vif_from_mac80211(vif)->ap_sta_id ==
 978                                 iwl_mvm_sta_from_mac80211(sta)->sta_id) {
 979                         struct iwl_mvm_vif *mvmvif;
 980                         u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
 981                         struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
 982 
 983                         mdata->opened_rx_ba_sessions = true;
 984                         mvmvif = iwl_mvm_vif_from_mac80211(vif);
 985                         cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
 986                 }
 987                 if (!iwl_enable_rx_ampdu()) {
 988                         ret = -EINVAL;
 989                         break;
 990                 }
 991                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
 992                                          timeout);
 993                 break;
 994         case IEEE80211_AMPDU_RX_STOP:
 995                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
 996                                          timeout);
 997                 break;
 998         case IEEE80211_AMPDU_TX_START:
 999                 if (!iwl_enable_tx_ampdu()) {
1000                         ret = -EINVAL;
1001                         break;
1002                 }
1003                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1004                 break;
1005         case IEEE80211_AMPDU_TX_STOP_CONT:
1006                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1007                 break;
1008         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1009         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1010                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1011                 break;
1012         case IEEE80211_AMPDU_TX_OPERATIONAL:
1013                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1014                                               buf_size, amsdu);
1015                 break;
1016         default:
1017                 WARN_ON_ONCE(1);
1018                 ret = -EINVAL;
1019                 break;
1020         }
1021 
1022         if (!ret) {
1023                 u16 rx_ba_ssn = 0;
1024 
1025                 if (action == IEEE80211_AMPDU_RX_START)
1026                         rx_ba_ssn = *ssn;
1027 
1028                 iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1029                                             rx_ba_ssn, action);
1030         }
1031         mutex_unlock(&mvm->mutex);
1032 
1033         return ret;
1034 }
1035 
1036 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1037                                      struct ieee80211_vif *vif)
1038 {
1039         struct iwl_mvm *mvm = data;
1040         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1041 
1042         mvmvif->uploaded = false;
1043         mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
1044 
1045         spin_lock_bh(&mvm->time_event_lock);
1046         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1047         spin_unlock_bh(&mvm->time_event_lock);
1048 
1049         mvmvif->phy_ctxt = NULL;
1050         memset(&mvmvif->bf_data, 0, sizeof(mvmvif->bf_data));
1051         memset(&mvmvif->probe_resp_data, 0, sizeof(mvmvif->probe_resp_data));
1052 }
1053 
1054 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1055 {
1056         iwl_mvm_stop_device(mvm);
1057 
1058         mvm->cur_aid = 0;
1059 
1060         mvm->scan_status = 0;
1061         mvm->ps_disabled = false;
1062         mvm->rfkill_safe_init_done = false;
1063 
1064         /* just in case one was running */
1065         iwl_mvm_cleanup_roc_te(mvm);
1066         ieee80211_remain_on_channel_expired(mvm->hw);
1067 
1068         iwl_mvm_ftm_restart(mvm);
1069 
1070         /*
1071          * cleanup all interfaces, even inactive ones, as some might have
1072          * gone down during the HW restart
1073          */
1074         ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1075 
1076         mvm->p2p_device_vif = NULL;
1077 
1078         iwl_mvm_reset_phy_ctxts(mvm);
1079         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1080         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1081         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1082 
1083         ieee80211_wake_queues(mvm->hw);
1084 
1085         mvm->vif_count = 0;
1086         mvm->rx_ba_sessions = 0;
1087         mvm->fwrt.dump.conf = FW_DBG_INVALID;
1088         mvm->monitor_on = false;
1089 
1090         /* keep statistics ticking */
1091         iwl_mvm_accu_radio_stats(mvm);
1092 }
1093 
1094 int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1095 {
1096         int ret;
1097 
1098         lockdep_assert_held(&mvm->mutex);
1099 
1100         if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1101                 /*
1102                  * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1103                  * so later code will - from now on - see that we're doing it.
1104                  */
1105                 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1106                 clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1107                 /* Clean up some internal and mac80211 state on restart */
1108                 iwl_mvm_restart_cleanup(mvm);
1109         }
1110         ret = iwl_mvm_up(mvm);
1111 
1112         iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1113                                NULL);
1114         iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1115                                NULL);
1116 
1117         if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1118                 /* Something went wrong - we need to finish some cleanup
1119                  * that normally iwl_mvm_mac_restart_complete() below
1120                  * would do.
1121                  */
1122                 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1123         }
1124 
1125         return ret;
1126 }
1127 
1128 static int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1129 {
1130         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1131         int ret;
1132 
1133         mutex_lock(&mvm->mutex);
1134         ret = __iwl_mvm_mac_start(mvm);
1135         mutex_unlock(&mvm->mutex);
1136 
1137         return ret;
1138 }
1139 
1140 static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1141 {
1142         int ret;
1143 
1144         mutex_lock(&mvm->mutex);
1145 
1146         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1147 
1148         ret = iwl_mvm_update_quotas(mvm, true, NULL);
1149         if (ret)
1150                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1151                         ret);
1152 
1153         iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1154 
1155         /*
1156          * If we have TDLS peers, remove them. We don't know the last seqno/PN
1157          * of packets the FW sent out, so we must reconnect.
1158          */
1159         iwl_mvm_teardown_tdls_peers(mvm);
1160 
1161         mutex_unlock(&mvm->mutex);
1162 }
1163 
1164 static void
1165 iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1166                               enum ieee80211_reconfig_type reconfig_type)
1167 {
1168         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1169 
1170         switch (reconfig_type) {
1171         case IEEE80211_RECONFIG_TYPE_RESTART:
1172                 iwl_mvm_restart_complete(mvm);
1173                 break;
1174         case IEEE80211_RECONFIG_TYPE_SUSPEND:
1175                 break;
1176         }
1177 }
1178 
1179 void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1180 {
1181         lockdep_assert_held(&mvm->mutex);
1182 
1183         /* firmware counters are obviously reset now, but we shouldn't
1184          * partially track so also clear the fw_reset_accu counters.
1185          */
1186         memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1187 
1188         /* async_handlers_wk is now blocked */
1189 
1190         /*
1191          * The work item could be running or queued if the
1192          * ROC time event stops just as we get here.
1193          */
1194         flush_work(&mvm->roc_done_wk);
1195 
1196         iwl_mvm_stop_device(mvm);
1197 
1198         iwl_mvm_async_handlers_purge(mvm);
1199         /* async_handlers_list is empty and will stay empty: HW is stopped */
1200 
1201         /* the fw is stopped, the aux sta is dead: clean up driver state */
1202         iwl_mvm_del_aux_sta(mvm);
1203 
1204         /*
1205          * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1206          * hw (as restart_complete() won't be called in this case) and mac80211
1207          * won't execute the restart.
1208          * But make sure to cleanup interfaces that have gone down before/during
1209          * HW restart was requested.
1210          */
1211         if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1212             test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1213                                &mvm->status))
1214                 ieee80211_iterate_interfaces(mvm->hw, 0,
1215                                              iwl_mvm_cleanup_iterator, mvm);
1216 
1217         /* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1218          * make sure there's nothing left there and warn if any is found.
1219          */
1220         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1221                 int i;
1222 
1223                 for (i = 0; i < mvm->max_scans; i++) {
1224                         if (WARN_ONCE(mvm->scan_uid_status[i],
1225                                       "UMAC scan UID %d status was not cleaned\n",
1226                                       i))
1227                                 mvm->scan_uid_status[i] = 0;
1228                 }
1229         }
1230 }
1231 
1232 static void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1233 {
1234         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1235 
1236         flush_work(&mvm->async_handlers_wk);
1237         flush_work(&mvm->add_stream_wk);
1238 
1239         /*
1240          * Lock and clear the firmware running bit here already, so that
1241          * new commands coming in elsewhere, e.g. from debugfs, will not
1242          * be able to proceed. This is important here because one of those
1243          * debugfs files causes the firmware dump to be triggered, and if we
1244          * don't stop debugfs accesses before canceling that it could be
1245          * retriggered after we flush it but before we've cleared the bit.
1246          */
1247         clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1248 
1249         cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1250         cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1251         iwl_fw_free_dump_desc(&mvm->fwrt);
1252 
1253         mutex_lock(&mvm->mutex);
1254         __iwl_mvm_mac_stop(mvm);
1255         mutex_unlock(&mvm->mutex);
1256 
1257         /*
1258          * The worker might have been waiting for the mutex, let it run and
1259          * discover that its list is now empty.
1260          */
1261         cancel_work_sync(&mvm->async_handlers_wk);
1262 }
1263 
1264 static struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1265 {
1266         u16 i;
1267 
1268         lockdep_assert_held(&mvm->mutex);
1269 
1270         for (i = 0; i < NUM_PHY_CTX; i++)
1271                 if (!mvm->phy_ctxts[i].ref)
1272                         return &mvm->phy_ctxts[i];
1273 
1274         IWL_ERR(mvm, "No available PHY context\n");
1275         return NULL;
1276 }
1277 
1278 static int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1279                                 s16 tx_power)
1280 {
1281         int len;
1282         union {
1283                 struct iwl_dev_tx_power_cmd v5;
1284                 struct iwl_dev_tx_power_cmd_v4 v4;
1285         } cmd = {
1286                 .v5.v3.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1287                 .v5.v3.mac_context_id =
1288                         cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1289                 .v5.v3.pwr_restriction = cpu_to_le16(8 * tx_power),
1290         };
1291 
1292         if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1293                 cmd.v5.v3.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1294 
1295         if (fw_has_api(&mvm->fw->ucode_capa,
1296                        IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1297                 len = sizeof(cmd.v5);
1298         else if (fw_has_capa(&mvm->fw->ucode_capa,
1299                              IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1300                 len = sizeof(cmd.v4);
1301         else
1302                 len = sizeof(cmd.v4.v3);
1303 
1304         return iwl_mvm_send_cmd_pdu(mvm, REDUCE_TX_POWER_CMD, 0, len, &cmd);
1305 }
1306 
1307 static int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1308                                        struct ieee80211_vif *vif)
1309 {
1310         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1311         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1312         int ret;
1313 
1314         mutex_lock(&mvm->mutex);
1315 
1316         if (mvmvif->csa_failed) {
1317                 mvmvif->csa_failed = false;
1318                 ret = -EIO;
1319                 goto out_unlock;
1320         }
1321 
1322         if (vif->type == NL80211_IFTYPE_STATION) {
1323                 struct iwl_mvm_sta *mvmsta;
1324 
1325                 mvmvif->csa_bcn_pending = false;
1326                 mvmsta = iwl_mvm_sta_from_staid_protected(mvm,
1327                                                           mvmvif->ap_sta_id);
1328 
1329                 if (WARN_ON(!mvmsta)) {
1330                         ret = -EIO;
1331                         goto out_unlock;
1332                 }
1333 
1334                 if (!fw_has_capa(&mvm->fw->ucode_capa,
1335                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1336                         iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1337 
1338                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1339 
1340                 if (!fw_has_capa(&mvm->fw->ucode_capa,
1341                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1342                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
1343                         if (ret)
1344                                 goto out_unlock;
1345 
1346                         iwl_mvm_stop_session_protection(mvm, vif);
1347                 }
1348         }
1349 
1350         mvmvif->ps_disabled = false;
1351 
1352         ret = iwl_mvm_power_update_ps(mvm);
1353 
1354 out_unlock:
1355         mutex_unlock(&mvm->mutex);
1356 
1357         return ret;
1358 }
1359 
1360 static void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1361                                          struct ieee80211_vif *vif)
1362 {
1363         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1364         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1365         struct iwl_chan_switch_te_cmd cmd = {
1366                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1367                                                           mvmvif->color)),
1368                 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1369         };
1370 
1371         IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1372 
1373         mutex_lock(&mvm->mutex);
1374         WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1375                                      WIDE_ID(MAC_CONF_GROUP,
1376                                              CHANNEL_SWITCH_TIME_EVENT_CMD),
1377                                      0, sizeof(cmd), &cmd));
1378         mutex_unlock(&mvm->mutex);
1379 
1380         WARN_ON(iwl_mvm_post_channel_switch(hw, vif));
1381 }
1382 
1383 static void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1384 {
1385         struct iwl_mvm *mvm;
1386         struct iwl_mvm_vif *mvmvif;
1387         struct ieee80211_vif *vif;
1388 
1389         mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1390         vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1391         mvm = mvmvif->mvm;
1392 
1393         iwl_mvm_abort_channel_switch(mvm->hw, vif);
1394         ieee80211_chswitch_done(vif, false);
1395 }
1396 
1397 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1398                                      struct ieee80211_vif *vif)
1399 {
1400         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1401         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1402         int ret;
1403 
1404         mvmvif->mvm = mvm;
1405         RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1406 
1407         /*
1408          * Not much to do here. The stack will not allow interface
1409          * types or combinations that we didn't advertise, so we
1410          * don't really have to check the types.
1411          */
1412 
1413         mutex_lock(&mvm->mutex);
1414 
1415         /* make sure that beacon statistics don't go backwards with FW reset */
1416         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1417                 mvmvif->beacon_stats.accu_num_beacons +=
1418                         mvmvif->beacon_stats.num_beacons;
1419 
1420         /* Allocate resources for the MAC context, and add it to the fw  */
1421         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1422         if (ret)
1423                 goto out_unlock;
1424 
1425         rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1426 
1427         /* Counting number of interfaces is needed for legacy PM */
1428         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1429                 mvm->vif_count++;
1430 
1431         /*
1432          * The AP binding flow can be done only after the beacon
1433          * template is configured (which happens only in the mac80211
1434          * start_ap() flow), and adding the broadcast station can happen
1435          * only after the binding.
1436          * In addition, since modifying the MAC before adding a bcast
1437          * station is not allowed by the FW, delay the adding of MAC context to
1438          * the point where we can also add the bcast station.
1439          * In short: there's not much we can do at this point, other than
1440          * allocating resources :)
1441          */
1442         if (vif->type == NL80211_IFTYPE_AP ||
1443             vif->type == NL80211_IFTYPE_ADHOC) {
1444                 ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1445                 if (ret) {
1446                         IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1447                         goto out_release;
1448                 }
1449 
1450                 /*
1451                  * Only queue for this station is the mcast queue,
1452                  * which shouldn't be in TFD mask anyway
1453                  */
1454                 ret = iwl_mvm_allocate_int_sta(mvm, &mvmvif->mcast_sta,
1455                                                0, vif->type,
1456                                                IWL_STA_MULTICAST);
1457                 if (ret)
1458                         goto out_release;
1459 
1460                 iwl_mvm_vif_dbgfs_register(mvm, vif);
1461                 goto out_unlock;
1462         }
1463 
1464         mvmvif->features |= hw->netdev_features;
1465 
1466         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1467         if (ret)
1468                 goto out_release;
1469 
1470         ret = iwl_mvm_power_update_mac(mvm);
1471         if (ret)
1472                 goto out_remove_mac;
1473 
1474         /* beacon filtering */
1475         ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
1476         if (ret)
1477                 goto out_remove_mac;
1478 
1479         if (!mvm->bf_allowed_vif &&
1480             vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1481                 mvm->bf_allowed_vif = mvmvif;
1482                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1483                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1484         }
1485 
1486         /*
1487          * P2P_DEVICE interface does not have a channel context assigned to it,
1488          * so a dedicated PHY context is allocated to it and the corresponding
1489          * MAC context is bound to it at this stage.
1490          */
1491         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1492 
1493                 mvmvif->phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
1494                 if (!mvmvif->phy_ctxt) {
1495                         ret = -ENOSPC;
1496                         goto out_free_bf;
1497                 }
1498 
1499                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
1500                 ret = iwl_mvm_binding_add_vif(mvm, vif);
1501                 if (ret)
1502                         goto out_unref_phy;
1503 
1504                 ret = iwl_mvm_add_p2p_bcast_sta(mvm, vif);
1505                 if (ret)
1506                         goto out_unbind;
1507 
1508                 /* Save a pointer to p2p device vif, so it can later be used to
1509                  * update the p2p device MAC when a GO is started/stopped */
1510                 mvm->p2p_device_vif = vif;
1511         }
1512 
1513         iwl_mvm_tcm_add_vif(mvm, vif);
1514         INIT_DELAYED_WORK(&mvmvif->csa_work,
1515                           iwl_mvm_channel_switch_disconnect_wk);
1516 
1517         if (vif->type == NL80211_IFTYPE_MONITOR)
1518                 mvm->monitor_on = true;
1519 
1520         iwl_mvm_vif_dbgfs_register(mvm, vif);
1521         goto out_unlock;
1522 
1523  out_unbind:
1524         iwl_mvm_binding_remove_vif(mvm, vif);
1525  out_unref_phy:
1526         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1527  out_free_bf:
1528         if (mvm->bf_allowed_vif == mvmvif) {
1529                 mvm->bf_allowed_vif = NULL;
1530                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1531                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1532         }
1533  out_remove_mac:
1534         mvmvif->phy_ctxt = NULL;
1535         iwl_mvm_mac_ctxt_remove(mvm, vif);
1536  out_release:
1537         if (vif->type != NL80211_IFTYPE_P2P_DEVICE)
1538                 mvm->vif_count--;
1539  out_unlock:
1540         mutex_unlock(&mvm->mutex);
1541 
1542         return ret;
1543 }
1544 
1545 static void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1546                                         struct ieee80211_vif *vif)
1547 {
1548         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1549                 /*
1550                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
1551                  * We assume here that all the packets sent to the OFFCHANNEL
1552                  * queue are sent in ROC session.
1553                  */
1554                 flush_work(&mvm->roc_done_wk);
1555         }
1556 }
1557 
1558 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1559                                          struct ieee80211_vif *vif)
1560 {
1561         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1562         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1563         struct iwl_probe_resp_data *probe_data;
1564 
1565         iwl_mvm_prepare_mac_removal(mvm, vif);
1566 
1567         if (!(vif->type == NL80211_IFTYPE_AP ||
1568               vif->type == NL80211_IFTYPE_ADHOC))
1569                 iwl_mvm_tcm_rm_vif(mvm, vif);
1570 
1571         mutex_lock(&mvm->mutex);
1572 
1573         probe_data = rcu_dereference_protected(mvmvif->probe_resp_data,
1574                                                lockdep_is_held(&mvm->mutex));
1575         RCU_INIT_POINTER(mvmvif->probe_resp_data, NULL);
1576         if (probe_data)
1577                 kfree_rcu(probe_data, rcu_head);
1578 
1579         if (mvm->bf_allowed_vif == mvmvif) {
1580                 mvm->bf_allowed_vif = NULL;
1581                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1582                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1583         }
1584 
1585         if (vif->bss_conf.ftm_responder)
1586                 memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1587 
1588         iwl_mvm_vif_dbgfs_clean(mvm, vif);
1589 
1590         /*
1591          * For AP/GO interface, the tear down of the resources allocated to the
1592          * interface is be handled as part of the stop_ap flow.
1593          */
1594         if (vif->type == NL80211_IFTYPE_AP ||
1595             vif->type == NL80211_IFTYPE_ADHOC) {
1596 #ifdef CONFIG_NL80211_TESTMODE
1597                 if (vif == mvm->noa_vif) {
1598                         mvm->noa_vif = NULL;
1599                         mvm->noa_duration = 0;
1600                 }
1601 #endif
1602                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->mcast_sta);
1603                 iwl_mvm_dealloc_bcast_sta(mvm, vif);
1604                 goto out_release;
1605         }
1606 
1607         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1608                 mvm->p2p_device_vif = NULL;
1609                 iwl_mvm_rm_p2p_bcast_sta(mvm, vif);
1610                 iwl_mvm_binding_remove_vif(mvm, vif);
1611                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
1612                 mvmvif->phy_ctxt = NULL;
1613         }
1614 
1615         if (mvm->vif_count && vif->type != NL80211_IFTYPE_P2P_DEVICE)
1616                 mvm->vif_count--;
1617 
1618         iwl_mvm_power_update_mac(mvm);
1619         iwl_mvm_mac_ctxt_remove(mvm, vif);
1620 
1621         RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1622 
1623         if (vif->type == NL80211_IFTYPE_MONITOR)
1624                 mvm->monitor_on = false;
1625 
1626 out_release:
1627         mutex_unlock(&mvm->mutex);
1628 }
1629 
1630 static int iwl_mvm_mac_config(struct ieee80211_hw *hw, u32 changed)
1631 {
1632         return 0;
1633 }
1634 
1635 struct iwl_mvm_mc_iter_data {
1636         struct iwl_mvm *mvm;
1637         int port_id;
1638 };
1639 
1640 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1641                                       struct ieee80211_vif *vif)
1642 {
1643         struct iwl_mvm_mc_iter_data *data = _data;
1644         struct iwl_mvm *mvm = data->mvm;
1645         struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1646         struct iwl_host_cmd hcmd = {
1647                 .id = MCAST_FILTER_CMD,
1648                 .flags = CMD_ASYNC,
1649                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1650         };
1651         int ret, len;
1652 
1653         /* if we don't have free ports, mcast frames will be dropped */
1654         if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1655                 return;
1656 
1657         if (vif->type != NL80211_IFTYPE_STATION ||
1658             !vif->bss_conf.assoc)
1659                 return;
1660 
1661         cmd->port_id = data->port_id++;
1662         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1663         len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1664 
1665         hcmd.len[0] = len;
1666         hcmd.data[0] = cmd;
1667 
1668         ret = iwl_mvm_send_cmd(mvm, &hcmd);
1669         if (ret)
1670                 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
1671 }
1672 
1673 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
1674 {
1675         struct iwl_mvm_mc_iter_data iter_data = {
1676                 .mvm = mvm,
1677         };
1678 
1679         lockdep_assert_held(&mvm->mutex);
1680 
1681         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1682                 return;
1683 
1684         ieee80211_iterate_active_interfaces_atomic(
1685                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1686                 iwl_mvm_mc_iface_iterator, &iter_data);
1687 }
1688 
1689 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1690                                      struct netdev_hw_addr_list *mc_list)
1691 {
1692         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1693         struct iwl_mcast_filter_cmd *cmd;
1694         struct netdev_hw_addr *addr;
1695         int addr_count;
1696         bool pass_all;
1697         int len;
1698 
1699         addr_count = netdev_hw_addr_list_count(mc_list);
1700         pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1701                    IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1702         if (pass_all)
1703                 addr_count = 0;
1704 
1705         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1706         cmd = kzalloc(len, GFP_ATOMIC);
1707         if (!cmd)
1708                 return 0;
1709 
1710         if (pass_all) {
1711                 cmd->pass_all = 1;
1712                 return (u64)(unsigned long)cmd;
1713         }
1714 
1715         netdev_hw_addr_list_for_each(addr, mc_list) {
1716                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1717                                    cmd->count, addr->addr);
1718                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1719                        addr->addr, ETH_ALEN);
1720                 cmd->count++;
1721         }
1722 
1723         return (u64)(unsigned long)cmd;
1724 }
1725 
1726 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1727                                      unsigned int changed_flags,
1728                                      unsigned int *total_flags,
1729                                      u64 multicast)
1730 {
1731         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1732         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
1733 
1734         mutex_lock(&mvm->mutex);
1735 
1736         /* replace previous configuration */
1737         kfree(mvm->mcast_filter_cmd);
1738         mvm->mcast_filter_cmd = cmd;
1739 
1740         if (!cmd)
1741                 goto out;
1742 
1743         if (changed_flags & FIF_ALLMULTI)
1744                 cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
1745 
1746         if (cmd->pass_all)
1747                 cmd->count = 0;
1748 
1749         iwl_mvm_recalc_multicast(mvm);
1750 out:
1751         mutex_unlock(&mvm->mutex);
1752         *total_flags = 0;
1753 }
1754 
1755 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1756                                         struct ieee80211_vif *vif,
1757                                         unsigned int filter_flags,
1758                                         unsigned int changed_flags)
1759 {
1760         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1761 
1762         /* We support only filter for probe requests */
1763         if (!(changed_flags & FIF_PROBE_REQ))
1764                 return;
1765 
1766         /* Supported only for p2p client interfaces */
1767         if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1768             !vif->p2p)
1769                 return;
1770 
1771         mutex_lock(&mvm->mutex);
1772         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1773         mutex_unlock(&mvm->mutex);
1774 }
1775 
1776 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1777 struct iwl_bcast_iter_data {
1778         struct iwl_mvm *mvm;
1779         struct iwl_bcast_filter_cmd *cmd;
1780         u8 current_filter;
1781 };
1782 
1783 static void
1784 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1785                          const struct iwl_fw_bcast_filter *in_filter,
1786                          struct iwl_fw_bcast_filter *out_filter)
1787 {
1788         struct iwl_fw_bcast_filter_attr *attr;
1789         int i;
1790 
1791         memcpy(out_filter, in_filter, sizeof(*out_filter));
1792 
1793         for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1794                 attr = &out_filter->attrs[i];
1795 
1796                 if (!attr->mask)
1797                         break;
1798 
1799                 switch (attr->reserved1) {
1800                 case cpu_to_le16(BC_FILTER_MAGIC_IP):
1801                         if (vif->bss_conf.arp_addr_cnt != 1) {
1802                                 attr->mask = 0;
1803                                 continue;
1804                         }
1805 
1806                         attr->val = vif->bss_conf.arp_addr_list[0];
1807                         break;
1808                 case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1809                         attr->val = *(__be32 *)&vif->addr[2];
1810                         break;
1811                 default:
1812                         break;
1813                 }
1814                 attr->reserved1 = 0;
1815                 out_filter->num_attrs++;
1816         }
1817 }
1818 
1819 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1820                                           struct ieee80211_vif *vif)
1821 {
1822         struct iwl_bcast_iter_data *data = _data;
1823         struct iwl_mvm *mvm = data->mvm;
1824         struct iwl_bcast_filter_cmd *cmd = data->cmd;
1825         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1826         struct iwl_fw_bcast_mac *bcast_mac;
1827         int i;
1828 
1829         if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1830                 return;
1831 
1832         bcast_mac = &cmd->macs[mvmvif->id];
1833 
1834         /*
1835          * enable filtering only for associated stations, but not for P2P
1836          * Clients
1837          */
1838         if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1839             !vif->bss_conf.assoc)
1840                 return;
1841 
1842         bcast_mac->default_discard = 1;
1843 
1844         /* copy all configured filters */
1845         for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1846                 /*
1847                  * Make sure we don't exceed our filters limit.
1848                  * if there is still a valid filter to be configured,
1849                  * be on the safe side and just allow bcast for this mac.
1850                  */
1851                 if (WARN_ON_ONCE(data->current_filter >=
1852                                  ARRAY_SIZE(cmd->filters))) {
1853                         bcast_mac->default_discard = 0;
1854                         bcast_mac->attached_filters = 0;
1855                         break;
1856                 }
1857 
1858                 iwl_mvm_set_bcast_filter(vif,
1859                                          &mvm->bcast_filters[i],
1860                                          &cmd->filters[data->current_filter]);
1861 
1862                 /* skip current filter if it contains no attributes */
1863                 if (!cmd->filters[data->current_filter].num_attrs)
1864                         continue;
1865 
1866                 /* attach the filter to current mac */
1867                 bcast_mac->attached_filters |=
1868                                 cpu_to_le16(BIT(data->current_filter));
1869 
1870                 data->current_filter++;
1871         }
1872 }
1873 
1874 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1875                                     struct iwl_bcast_filter_cmd *cmd)
1876 {
1877         struct iwl_bcast_iter_data iter_data = {
1878                 .mvm = mvm,
1879                 .cmd = cmd,
1880         };
1881 
1882         if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1883                 return false;
1884 
1885         memset(cmd, 0, sizeof(*cmd));
1886         cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1887         cmd->max_macs = ARRAY_SIZE(cmd->macs);
1888 
1889 #ifdef CONFIG_IWLWIFI_DEBUGFS
1890         /* use debugfs filters/macs if override is configured */
1891         if (mvm->dbgfs_bcast_filtering.override) {
1892                 memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1893                        sizeof(cmd->filters));
1894                 memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1895                        sizeof(cmd->macs));
1896                 return true;
1897         }
1898 #endif
1899 
1900         /* if no filters are configured, do nothing */
1901         if (!mvm->bcast_filters)
1902                 return false;
1903 
1904         /* configure and attach these filters for each associated sta vif */
1905         ieee80211_iterate_active_interfaces(
1906                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1907                 iwl_mvm_bcast_filter_iterator, &iter_data);
1908 
1909         return true;
1910 }
1911 
1912 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1913 {
1914         struct iwl_bcast_filter_cmd cmd;
1915 
1916         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1917                 return 0;
1918 
1919         if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1920                 return 0;
1921 
1922         return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1923                                     sizeof(cmd), &cmd);
1924 }
1925 #else
1926 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1927 {
1928         return 0;
1929 }
1930 #endif
1931 
1932 static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1933                                     struct ieee80211_vif *vif)
1934 {
1935         struct iwl_mu_group_mgmt_cmd cmd = {};
1936 
1937         memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1938                WLAN_MEMBERSHIP_LEN);
1939         memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1940                WLAN_USER_POSITION_LEN);
1941 
1942         return iwl_mvm_send_cmd_pdu(mvm,
1943                                     WIDE_ID(DATA_PATH_GROUP,
1944                                             UPDATE_MU_GROUPS_CMD),
1945                                     0, sizeof(cmd), &cmd);
1946 }
1947 
1948 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1949                                            struct ieee80211_vif *vif)
1950 {
1951         if (vif->mu_mimo_owner) {
1952                 struct iwl_mu_group_mgmt_notif *notif = _data;
1953 
1954                 /*
1955                  * MU-MIMO Group Id action frame is little endian. We treat
1956                  * the data received from firmware as if it came from the
1957                  * action frame, so no conversion is needed.
1958                  */
1959                 ieee80211_update_mu_groups(vif,
1960                                            (u8 *)&notif->membership_status,
1961                                            (u8 *)&notif->user_position);
1962         }
1963 }
1964 
1965 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1966                                struct iwl_rx_cmd_buffer *rxb)
1967 {
1968         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1969         struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1970 
1971         ieee80211_iterate_active_interfaces_atomic(
1972                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1973                         iwl_mvm_mu_mimo_iface_iterator, notif);
1974 }
1975 
1976 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1977 {
1978         u8 byte_num = ppe_pos_bit / 8;
1979         u8 bit_num = ppe_pos_bit % 8;
1980         u8 residue_bits;
1981         u8 res;
1982 
1983         if (bit_num <= 5)
1984                 return (ppe[byte_num] >> bit_num) &
1985                        (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1986 
1987         /*
1988          * If bit_num > 5, we have to combine bits with next byte.
1989          * Calculate how many bits we need to take from current byte (called
1990          * here "residue_bits"), and add them to bits from next byte.
1991          */
1992 
1993         residue_bits = 8 - bit_num;
1994 
1995         res = (ppe[byte_num + 1] &
1996                (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
1997               residue_bits;
1998         res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
1999 
2000         return res;
2001 }
2002 
2003 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2004                                struct ieee80211_vif *vif, u8 sta_id)
2005 {
2006         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2007         struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2008                 .sta_id = sta_id,
2009                 .tid_limit = IWL_MAX_TID_COUNT,
2010                 .bss_color = vif->bss_conf.bss_color,
2011                 .htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2012                 .frame_time_rts_th =
2013                         cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2014         };
2015         int size = fw_has_api(&mvm->fw->ucode_capa,
2016                               IWL_UCODE_TLV_API_MBSSID_HE) ?
2017                    sizeof(sta_ctxt_cmd) :
2018                    sizeof(struct iwl_he_sta_context_cmd_v1);
2019         struct ieee80211_sta *sta;
2020         u32 flags;
2021         int i;
2022 
2023         rcu_read_lock();
2024 
2025         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2026         if (IS_ERR_OR_NULL(sta)) {
2027                 rcu_read_unlock();
2028                 WARN(1, "Can't find STA to configure HE\n");
2029                 return;
2030         }
2031 
2032         if (!sta->he_cap.has_he) {
2033                 rcu_read_unlock();
2034                 return;
2035         }
2036 
2037         flags = 0;
2038 
2039         /* Block 26-tone RU OFDMA transmissions */
2040         if (mvmvif->he_ru_2mhz_block)
2041                 flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2042 
2043         /* HTC flags */
2044         if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2045             IEEE80211_HE_MAC_CAP0_HTC_HE)
2046                 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2047         if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2048               IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2049             (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2050               IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2051                 u8 link_adap =
2052                         ((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2053                           IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2054                          (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2055                           IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2056 
2057                 if (link_adap == 2)
2058                         sta_ctxt_cmd.htc_flags |=
2059                                 cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2060                 else if (link_adap == 3)
2061                         sta_ctxt_cmd.htc_flags |=
2062                                 cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2063         }
2064         if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2065                 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2066         if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2067             IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2068                 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2069         if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2070                 sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2071 
2072         /*
2073          * Initialize the PPE thresholds to "None" (7), as described in Table
2074          * 9-262ac of 80211.ax/D3.0.
2075          */
2076         memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2077 
2078         /* If PPE Thresholds exist, parse them into a FW-familiar format. */
2079         if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2080             IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2081                 u8 nss = (sta->he_cap.ppe_thres[0] &
2082                           IEEE80211_PPE_THRES_NSS_MASK) + 1;
2083                 u8 ru_index_bitmap =
2084                         (sta->he_cap.ppe_thres[0] &
2085                          IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2086                         IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2087                 u8 *ppe = &sta->he_cap.ppe_thres[0];
2088                 u8 ppe_pos_bit = 7; /* Starting after PPE header */
2089 
2090                 /*
2091                  * FW currently supports only nss == MAX_HE_SUPP_NSS
2092                  *
2093                  * If nss > MAX: we can ignore values we don't support
2094                  * If nss < MAX: we can set zeros in other streams
2095                  */
2096                 if (nss > MAX_HE_SUPP_NSS) {
2097                         IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2098                                  MAX_HE_SUPP_NSS);
2099                         nss = MAX_HE_SUPP_NSS;
2100                 }
2101 
2102                 for (i = 0; i < nss; i++) {
2103                         u8 ru_index_tmp = ru_index_bitmap << 1;
2104                         u8 bw;
2105 
2106                         for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2107                                 ru_index_tmp >>= 1;
2108                                 if (!(ru_index_tmp & 1))
2109                                         continue;
2110 
2111                                 sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2112                                         iwl_mvm_he_get_ppe_val(ppe,
2113                                                                ppe_pos_bit);
2114                                 ppe_pos_bit +=
2115                                         IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2116                                 sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2117                                         iwl_mvm_he_get_ppe_val(ppe,
2118                                                                ppe_pos_bit);
2119                                 ppe_pos_bit +=
2120                                         IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2121                         }
2122                 }
2123 
2124                 flags |= STA_CTXT_HE_PACKET_EXT;
2125         } else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
2126                     IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
2127                   IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
2128                 int low_th = -1;
2129                 int high_th = -1;
2130 
2131                 /* Take the PPE thresholds from the nominal padding info */
2132                 switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
2133                         IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
2134                 case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
2135                         low_th = IWL_HE_PKT_EXT_NONE;
2136                         high_th = IWL_HE_PKT_EXT_NONE;
2137                         break;
2138                 case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
2139                         low_th = IWL_HE_PKT_EXT_BPSK;
2140                         high_th = IWL_HE_PKT_EXT_NONE;
2141                         break;
2142                 case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
2143                         low_th = IWL_HE_PKT_EXT_NONE;
2144                         high_th = IWL_HE_PKT_EXT_BPSK;
2145                         break;
2146                 }
2147 
2148                 /* Set the PPE thresholds accordingly */
2149                 if (low_th >= 0 && high_th >= 0) {
2150                         struct iwl_he_pkt_ext *pkt_ext =
2151                                 (struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
2152 
2153                         for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2154                                 u8 bw;
2155 
2156                                 for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
2157                                      bw++) {
2158                                         pkt_ext->pkt_ext_qam_th[i][bw][0] =
2159                                                 low_th;
2160                                         pkt_ext->pkt_ext_qam_th[i][bw][1] =
2161                                                 high_th;
2162                                 }
2163                         }
2164 
2165                         flags |= STA_CTXT_HE_PACKET_EXT;
2166                 }
2167         }
2168         rcu_read_unlock();
2169 
2170         /* Mark MU EDCA as enabled, unless none detected on some AC */
2171         flags |= STA_CTXT_HE_MU_EDCA_CW;
2172         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2173                 struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2174                         &mvmvif->queue_params[i].mu_edca_param_rec;
2175                 u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2176 
2177                 if (!mvmvif->queue_params[i].mu_edca) {
2178                         flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2179                         break;
2180                 }
2181 
2182                 sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2183                         cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2184                 sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2185                         cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2186                 sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2187                         cpu_to_le16(mu_edca->aifsn);
2188                 sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2189                         cpu_to_le16(mu_edca->mu_edca_timer);
2190         }
2191 
2192         if (vif->bss_conf.multi_sta_back_32bit)
2193                 flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2194 
2195         if (vif->bss_conf.ack_enabled)
2196                 flags |= STA_CTXT_HE_ACK_ENABLED;
2197 
2198         if (vif->bss_conf.uora_exists) {
2199                 flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2200 
2201                 sta_ctxt_cmd.rand_alloc_ecwmin =
2202                         vif->bss_conf.uora_ocw_range & 0x7;
2203                 sta_ctxt_cmd.rand_alloc_ecwmax =
2204                         (vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2205         }
2206 
2207         if (vif->bss_conf.nontransmitted) {
2208                 flags |= STA_CTXT_HE_REF_BSSID_VALID;
2209                 ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2210                                 vif->bss_conf.transmitter_bssid);
2211                 sta_ctxt_cmd.max_bssid_indicator =
2212                         vif->bss_conf.bssid_indicator;
2213                 sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2214                 sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2215                 sta_ctxt_cmd.profile_periodicity =
2216                         vif->bss_conf.profile_periodicity;
2217         }
2218 
2219         sta_ctxt_cmd.flags = cpu_to_le32(flags);
2220 
2221         if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2222                                                  DATA_PATH_GROUP, 0),
2223                                  0, size, &sta_ctxt_cmd))
2224                 IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2225 }
2226 
2227 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2228                                              struct ieee80211_vif *vif,
2229                                              struct ieee80211_bss_conf *bss_conf,
2230                                              u32 changes)
2231 {
2232         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2233         int ret;
2234 
2235         /*
2236          * Re-calculate the tsf id, as the master-slave relations depend on the
2237          * beacon interval, which was not known when the station interface was
2238          * added.
2239          */
2240         if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2241                 if (vif->bss_conf.he_support &&
2242                     !iwlwifi_mod_params.disable_11ax)
2243                         iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2244 
2245                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2246         }
2247 
2248         /* Update MU EDCA params */
2249         if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2250             bss_conf->assoc && vif->bss_conf.he_support &&
2251             !iwlwifi_mod_params.disable_11ax)
2252                 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2253 
2254         /*
2255          * If we're not associated yet, take the (new) BSSID before associating
2256          * so the firmware knows. If we're already associated, then use the old
2257          * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2258          * branch for disassociation below.
2259          */
2260         if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2261                 memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2262 
2263         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2264         if (ret)
2265                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2266 
2267         /* after sending it once, adopt mac80211 data */
2268         memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2269         mvmvif->associated = bss_conf->assoc;
2270 
2271         if (changes & BSS_CHANGED_ASSOC) {
2272                 if (bss_conf->assoc) {
2273                         /* clear statistics to get clean beacon counter */
2274                         iwl_mvm_request_statistics(mvm, true);
2275                         memset(&mvmvif->beacon_stats, 0,
2276                                sizeof(mvmvif->beacon_stats));
2277 
2278                         /* add quota for this interface */
2279                         ret = iwl_mvm_update_quotas(mvm, true, NULL);
2280                         if (ret) {
2281                                 IWL_ERR(mvm, "failed to update quotas\n");
2282                                 return;
2283                         }
2284 
2285                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2286                                      &mvm->status)) {
2287                                 /*
2288                                  * If we're restarting then the firmware will
2289                                  * obviously have lost synchronisation with
2290                                  * the AP. It will attempt to synchronise by
2291                                  * itself, but we can make it more reliable by
2292                                  * scheduling a session protection time event.
2293                                  *
2294                                  * The firmware needs to receive a beacon to
2295                                  * catch up with synchronisation, use 110% of
2296                                  * the beacon interval.
2297                                  *
2298                                  * Set a large maximum delay to allow for more
2299                                  * than a single interface.
2300                                  */
2301                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2302                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
2303                                                         5 * dur, false);
2304                         }
2305 
2306                         iwl_mvm_sf_update(mvm, vif, false);
2307                         iwl_mvm_power_vif_assoc(mvm, vif);
2308                         if (vif->p2p) {
2309                                 iwl_mvm_update_smps(mvm, vif,
2310                                                     IWL_MVM_SMPS_REQ_PROT,
2311                                                     IEEE80211_SMPS_DYNAMIC);
2312                         }
2313                 } else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2314                         /*
2315                          * If update fails - SF might be running in associated
2316                          * mode while disassociated - which is forbidden.
2317                          */
2318                         ret = iwl_mvm_sf_update(mvm, vif, false);
2319                         WARN_ONCE(ret &&
2320                                   !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2321                                             &mvm->status),
2322                                   "Failed to update SF upon disassociation\n");
2323 
2324                         /*
2325                          * If we get an assert during the connection (after the
2326                          * station has been added, but before the vif is set
2327                          * to associated), mac80211 will re-add the station and
2328                          * then configure the vif. Since the vif is not
2329                          * associated, we would remove the station here and
2330                          * this would fail the recovery.
2331                          */
2332                         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2333                                       &mvm->status)) {
2334                                 /*
2335                                  * Remove AP station now that
2336                                  * the MAC is unassoc
2337                                  */
2338                                 ret = iwl_mvm_rm_sta_id(mvm, vif,
2339                                                         mvmvif->ap_sta_id);
2340                                 if (ret)
2341                                         IWL_ERR(mvm,
2342                                                 "failed to remove AP station\n");
2343 
2344                                 mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
2345                         }
2346 
2347                         /* remove quota for this interface */
2348                         ret = iwl_mvm_update_quotas(mvm, false, NULL);
2349                         if (ret)
2350                                 IWL_ERR(mvm, "failed to update quotas\n");
2351 
2352                         /* this will take the cleared BSSID from bss_conf */
2353                         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2354                         if (ret)
2355                                 IWL_ERR(mvm,
2356                                         "failed to update MAC %pM (clear after unassoc)\n",
2357                                         vif->addr);
2358                 }
2359 
2360                 /*
2361                  * The firmware tracks the MU-MIMO group on its own.
2362                  * However, on HW restart we should restore this data.
2363                  */
2364                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2365                     (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2366                         ret = iwl_mvm_update_mu_groups(mvm, vif);
2367                         if (ret)
2368                                 IWL_ERR(mvm,
2369                                         "failed to update VHT MU_MIMO groups\n");
2370                 }
2371 
2372                 iwl_mvm_recalc_multicast(mvm);
2373                 iwl_mvm_configure_bcast_filter(mvm);
2374 
2375                 /* reset rssi values */
2376                 mvmvif->bf_data.ave_beacon_signal = 0;
2377 
2378                 iwl_mvm_bt_coex_vif_change(mvm);
2379                 iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2380                                     IEEE80211_SMPS_AUTOMATIC);
2381                 if (fw_has_capa(&mvm->fw->ucode_capa,
2382                                 IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2383                         iwl_mvm_config_scan(mvm);
2384         }
2385 
2386         if (changes & BSS_CHANGED_BEACON_INFO) {
2387                 /*
2388                  * We received a beacon from the associated AP so
2389                  * remove the session protection.
2390                  */
2391                 iwl_mvm_stop_session_protection(mvm, vif);
2392 
2393                 iwl_mvm_sf_update(mvm, vif, false);
2394                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2395         }
2396 
2397         if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2398                        /*
2399                         * Send power command on every beacon change,
2400                         * because we may have not enabled beacon abort yet.
2401                         */
2402                        BSS_CHANGED_BEACON_INFO)) {
2403                 ret = iwl_mvm_power_update_mac(mvm);
2404                 if (ret)
2405                         IWL_ERR(mvm, "failed to update power mode\n");
2406         }
2407 
2408         if (changes & BSS_CHANGED_TXPOWER) {
2409                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2410                                 bss_conf->txpower);
2411                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2412         }
2413 
2414         if (changes & BSS_CHANGED_CQM) {
2415                 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2416                 /* reset cqm events tracking */
2417                 mvmvif->bf_data.last_cqm_event = 0;
2418                 if (mvmvif->bf_data.bf_enabled) {
2419                         ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2420                         if (ret)
2421                                 IWL_ERR(mvm,
2422                                         "failed to update CQM thresholds\n");
2423                 }
2424         }
2425 
2426         if (changes & BSS_CHANGED_ARP_FILTER) {
2427                 IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
2428                 iwl_mvm_configure_bcast_filter(mvm);
2429         }
2430 }
2431 
2432 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2433                                  struct ieee80211_vif *vif)
2434 {
2435         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2436         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2437         int ret, i;
2438 
2439         mutex_lock(&mvm->mutex);
2440 
2441         /* Send the beacon template */
2442         ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2443         if (ret)
2444                 goto out_unlock;
2445 
2446         /*
2447          * Re-calculate the tsf id, as the master-slave relations depend on the
2448          * beacon interval, which was not known when the AP interface was added.
2449          */
2450         if (vif->type == NL80211_IFTYPE_AP)
2451                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2452 
2453         mvmvif->ap_assoc_sta_count = 0;
2454 
2455         /* Add the mac context */
2456         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2457         if (ret)
2458                 goto out_unlock;
2459 
2460         /* Perform the binding */
2461         ret = iwl_mvm_binding_add_vif(mvm, vif);
2462         if (ret)
2463                 goto out_remove;
2464 
2465         /*
2466          * This is not very nice, but the simplest:
2467          * For older FWs adding the mcast sta before the bcast station may
2468          * cause assert 0x2b00.
2469          * This is fixed in later FW so make the order of removal depend on
2470          * the TLV
2471          */
2472         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2473                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2474                 if (ret)
2475                         goto out_unbind;
2476                 /*
2477                  * Send the bcast station. At this stage the TBTT and DTIM time
2478                  * events are added and applied to the scheduler
2479                  */
2480                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2481                 if (ret) {
2482                         iwl_mvm_rm_mcast_sta(mvm, vif);
2483                         goto out_unbind;
2484                 }
2485         } else {
2486                 /*
2487                  * Send the bcast station. At this stage the TBTT and DTIM time
2488                  * events are added and applied to the scheduler
2489                  */
2490                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2491                 if (ret)
2492                         goto out_unbind;
2493                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2494                 if (ret) {
2495                         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2496                         goto out_unbind;
2497                 }
2498         }
2499 
2500         /* must be set before quota calculations */
2501         mvmvif->ap_ibss_active = true;
2502 
2503         /* send all the early keys to the device now */
2504         for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2505                 struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2506 
2507                 if (!key)
2508                         continue;
2509 
2510                 mvmvif->ap_early_keys[i] = NULL;
2511 
2512                 ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2513                 if (ret)
2514                         goto out_quota_failed;
2515         }
2516 
2517         if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2518                 iwl_mvm_vif_set_low_latency(mvmvif, true,
2519                                             LOW_LATENCY_VIF_TYPE);
2520                 iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2521         }
2522 
2523         /* power updated needs to be done before quotas */
2524         iwl_mvm_power_update_mac(mvm);
2525 
2526         ret = iwl_mvm_update_quotas(mvm, false, NULL);
2527         if (ret)
2528                 goto out_quota_failed;
2529 
2530         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2531         if (vif->p2p && mvm->p2p_device_vif)
2532                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2533 
2534         iwl_mvm_bt_coex_vif_change(mvm);
2535 
2536         /* we don't support TDLS during DCM */
2537         if (iwl_mvm_phy_ctx_count(mvm) > 1)
2538                 iwl_mvm_teardown_tdls_peers(mvm);
2539 
2540         iwl_mvm_ftm_restart_responder(mvm, vif);
2541 
2542         goto out_unlock;
2543 
2544 out_quota_failed:
2545         iwl_mvm_power_update_mac(mvm);
2546         mvmvif->ap_ibss_active = false;
2547         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2548         iwl_mvm_rm_mcast_sta(mvm, vif);
2549 out_unbind:
2550         iwl_mvm_binding_remove_vif(mvm, vif);
2551 out_remove:
2552         iwl_mvm_mac_ctxt_remove(mvm, vif);
2553 out_unlock:
2554         mutex_unlock(&mvm->mutex);
2555         return ret;
2556 }
2557 
2558 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2559                                  struct ieee80211_vif *vif)
2560 {
2561         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2562         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2563 
2564         iwl_mvm_prepare_mac_removal(mvm, vif);
2565 
2566         mutex_lock(&mvm->mutex);
2567 
2568         /* Handle AP stop while in CSA */
2569         if (rcu_access_pointer(mvm->csa_vif) == vif) {
2570                 iwl_mvm_remove_time_event(mvm, mvmvif,
2571                                           &mvmvif->time_event_data);
2572                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
2573                 mvmvif->csa_countdown = false;
2574         }
2575 
2576         if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2577                 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2578                 mvm->csa_tx_block_bcn_timeout = 0;
2579         }
2580 
2581         mvmvif->ap_ibss_active = false;
2582         mvm->ap_last_beacon_gp2 = 0;
2583 
2584         if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2585                 iwl_mvm_vif_set_low_latency(mvmvif, false,
2586                                             LOW_LATENCY_VIF_TYPE);
2587                 iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
2588         }
2589 
2590         iwl_mvm_bt_coex_vif_change(mvm);
2591 
2592         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2593         if (vif->p2p && mvm->p2p_device_vif)
2594                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2595 
2596         iwl_mvm_update_quotas(mvm, false, NULL);
2597 
2598         /*
2599          * This is not very nice, but the simplest:
2600          * For older FWs removing the mcast sta before the bcast station may
2601          * cause assert 0x2b00.
2602          * This is fixed in later FW (which will stop beaconing when removing
2603          * bcast station).
2604          * So make the order of removal depend on the TLV
2605          */
2606         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2607                 iwl_mvm_rm_mcast_sta(mvm, vif);
2608         iwl_mvm_send_rm_bcast_sta(mvm, vif);
2609         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2610                 iwl_mvm_rm_mcast_sta(mvm, vif);
2611         iwl_mvm_binding_remove_vif(mvm, vif);
2612 
2613         iwl_mvm_power_update_mac(mvm);
2614 
2615         iwl_mvm_mac_ctxt_remove(mvm, vif);
2616 
2617         mutex_unlock(&mvm->mutex);
2618 }
2619 
2620 static void
2621 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2622                                  struct ieee80211_vif *vif,
2623                                  struct ieee80211_bss_conf *bss_conf,
2624                                  u32 changes)
2625 {
2626         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2627 
2628         /* Changes will be applied when the AP/IBSS is started */
2629         if (!mvmvif->ap_ibss_active)
2630                 return;
2631 
2632         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2633                        BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2634             iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2635                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2636 
2637         /* Need to send a new beacon template to the FW */
2638         if (changes & BSS_CHANGED_BEACON &&
2639             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2640                 IWL_WARN(mvm, "Failed updating beacon data\n");
2641 
2642         if (changes & BSS_CHANGED_TXPOWER) {
2643                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d\n",
2644                                 bss_conf->txpower);
2645                 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2646         }
2647 
2648         if (changes & BSS_CHANGED_FTM_RESPONDER) {
2649                 int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2650 
2651                 if (ret)
2652                         IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2653                                  ret);
2654         }
2655 
2656 }
2657 
2658 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2659                                      struct ieee80211_vif *vif,
2660                                      struct ieee80211_bss_conf *bss_conf,
2661                                      u32 changes)
2662 {
2663         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2664 
2665         mutex_lock(&mvm->mutex);
2666 
2667         if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2668                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2669 
2670         switch (vif->type) {
2671         case NL80211_IFTYPE_STATION:
2672                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2673                 break;
2674         case NL80211_IFTYPE_AP:
2675         case NL80211_IFTYPE_ADHOC:
2676                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2677                 break;
2678         case NL80211_IFTYPE_MONITOR:
2679                 if (changes & BSS_CHANGED_MU_GROUPS)
2680                         iwl_mvm_update_mu_groups(mvm, vif);
2681                 break;
2682         default:
2683                 /* shouldn't happen */
2684                 WARN_ON_ONCE(1);
2685         }
2686 
2687         mutex_unlock(&mvm->mutex);
2688 }
2689 
2690 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2691                                struct ieee80211_vif *vif,
2692                                struct ieee80211_scan_request *hw_req)
2693 {
2694         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2695         int ret;
2696 
2697         if (hw_req->req.n_channels == 0 ||
2698             hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2699                 return -EINVAL;
2700 
2701         mutex_lock(&mvm->mutex);
2702         ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2703         mutex_unlock(&mvm->mutex);
2704 
2705         return ret;
2706 }
2707 
2708 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2709                                        struct ieee80211_vif *vif)
2710 {
2711         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2712 
2713         mutex_lock(&mvm->mutex);
2714 
2715         /* Due to a race condition, it's possible that mac80211 asks
2716          * us to stop a hw_scan when it's already stopped.  This can
2717          * happen, for instance, if we stopped the scan ourselves,
2718          * called ieee80211_scan_completed() and the userspace called
2719          * cancel scan scan before ieee80211_scan_work() could run.
2720          * To handle that, simply return if the scan is not running.
2721         */
2722         if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2723                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2724 
2725         mutex_unlock(&mvm->mutex);
2726 }
2727 
2728 static void
2729 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2730                                   struct ieee80211_sta *sta, u16 tids,
2731                                   int num_frames,
2732                                   enum ieee80211_frame_release_type reason,
2733                                   bool more_data)
2734 {
2735         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2736 
2737         /* Called when we need to transmit (a) frame(s) from mac80211 */
2738 
2739         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2740                                           tids, more_data, false);
2741 }
2742 
2743 static void
2744 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2745                                     struct ieee80211_sta *sta, u16 tids,
2746                                     int num_frames,
2747                                     enum ieee80211_frame_release_type reason,
2748                                     bool more_data)
2749 {
2750         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2751 
2752         /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2753 
2754         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2755                                           tids, more_data, true);
2756 }
2757 
2758 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2759                                      enum sta_notify_cmd cmd,
2760                                      struct ieee80211_sta *sta)
2761 {
2762         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2763         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2764         unsigned long txqs = 0, tids = 0;
2765         int tid;
2766 
2767         /*
2768          * If we have TVQM then we get too high queue numbers - luckily
2769          * we really shouldn't get here with that because such hardware
2770          * should have firmware supporting buffer station offload.
2771          */
2772         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2773                 return;
2774 
2775         spin_lock_bh(&mvmsta->lock);
2776         for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2777                 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2778 
2779                 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
2780                         continue;
2781 
2782                 __set_bit(tid_data->txq_id, &txqs);
2783 
2784                 if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2785                         continue;
2786 
2787                 __set_bit(tid, &tids);
2788         }
2789 
2790         switch (cmd) {
2791         case STA_NOTIFY_SLEEP:
2792                 for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2793                         ieee80211_sta_set_buffered(sta, tid, true);
2794 
2795                 if (txqs)
2796                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2797                 /*
2798                  * The fw updates the STA to be asleep. Tx packets on the Tx
2799                  * queues to this station will not be transmitted. The fw will
2800                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2801                  */
2802                 break;
2803         case STA_NOTIFY_AWAKE:
2804                 if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2805                         break;
2806 
2807                 if (txqs)
2808                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2809                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
2810                 break;
2811         default:
2812                 break;
2813         }
2814         spin_unlock_bh(&mvmsta->lock);
2815 }
2816 
2817 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2818                                    struct ieee80211_vif *vif,
2819                                    enum sta_notify_cmd cmd,
2820                                    struct ieee80211_sta *sta)
2821 {
2822         __iwl_mvm_mac_sta_notify(hw, cmd, sta);
2823 }
2824 
2825 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2826 {
2827         struct iwl_rx_packet *pkt = rxb_addr(rxb);
2828         struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
2829         struct ieee80211_sta *sta;
2830         struct iwl_mvm_sta *mvmsta;
2831         bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
2832 
2833         if (WARN_ON(notif->sta_id >= ARRAY_SIZE(mvm->fw_id_to_mac_id)))
2834                 return;
2835 
2836         rcu_read_lock();
2837         sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
2838         if (WARN_ON(IS_ERR_OR_NULL(sta))) {
2839                 rcu_read_unlock();
2840                 return;
2841         }
2842 
2843         mvmsta = iwl_mvm_sta_from_mac80211(sta);
2844 
2845         if (!mvmsta->vif ||
2846             mvmsta->vif->type != NL80211_IFTYPE_AP) {
2847                 rcu_read_unlock();
2848                 return;
2849         }
2850 
2851         if (mvmsta->sleeping != sleeping) {
2852                 mvmsta->sleeping = sleeping;
2853                 __iwl_mvm_mac_sta_notify(mvm->hw,
2854                         sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
2855                         sta);
2856                 ieee80211_sta_ps_transition(sta, sleeping);
2857         }
2858 
2859         if (sleeping) {
2860                 switch (notif->type) {
2861                 case IWL_MVM_PM_EVENT_AWAKE:
2862                 case IWL_MVM_PM_EVENT_ASLEEP:
2863                         break;
2864                 case IWL_MVM_PM_EVENT_UAPSD:
2865                         ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
2866                         break;
2867                 case IWL_MVM_PM_EVENT_PS_POLL:
2868                         ieee80211_sta_pspoll(sta);
2869                         break;
2870                 default:
2871                         break;
2872                 }
2873         }
2874 
2875         rcu_read_unlock();
2876 }
2877 
2878 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2879                                        struct ieee80211_vif *vif,
2880                                        struct ieee80211_sta *sta)
2881 {
2882         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2883         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2884 
2885         /*
2886          * This is called before mac80211 does RCU synchronisation,
2887          * so here we already invalidate our internal RCU-protected
2888          * station pointer. The rest of the code will thus no longer
2889          * be able to find the station this way, and we don't rely
2890          * on further RCU synchronisation after the sta_state()
2891          * callback deleted the station.
2892          */
2893         mutex_lock(&mvm->mutex);
2894         if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2895                 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2896                                    ERR_PTR(-ENOENT));
2897 
2898         mutex_unlock(&mvm->mutex);
2899 }
2900 
2901 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2902                                 const u8 *bssid)
2903 {
2904         int i;
2905 
2906         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2907                 struct iwl_mvm_tcm_mac *mdata;
2908 
2909                 mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2910                 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2911                 mdata->opened_rx_ba_sessions = false;
2912         }
2913 
2914         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2915                 return;
2916 
2917         if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2918                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2919                 return;
2920         }
2921 
2922         if (!vif->p2p &&
2923             (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
2924                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2925                 return;
2926         }
2927 
2928         for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
2929                 if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
2930                         vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2931                         return;
2932                 }
2933         }
2934 
2935         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
2936 }
2937 
2938 static void
2939 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
2940                            struct ieee80211_vif *vif, u8 *peer_addr,
2941                            enum nl80211_tdls_operation action)
2942 {
2943         struct iwl_fw_dbg_trigger_tlv *trig;
2944         struct iwl_fw_dbg_trigger_tdls *tdls_trig;
2945 
2946         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
2947                                      FW_DBG_TRIGGER_TDLS);
2948         if (!trig)
2949                 return;
2950 
2951         tdls_trig = (void *)trig->data;
2952 
2953         if (!(tdls_trig->action_bitmap & BIT(action)))
2954                 return;
2955 
2956         if (tdls_trig->peer_mode &&
2957             memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
2958                 return;
2959 
2960         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
2961                                 "TDLS event occurred, peer %pM, action %d",
2962                                 peer_addr, action);
2963 }
2964 
2965 struct iwl_mvm_he_obss_narrow_bw_ru_data {
2966         bool tolerated;
2967 };
2968 
2969 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
2970                                                     struct cfg80211_bss *bss,
2971                                                     void *_data)
2972 {
2973         struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
2974         const struct element *elem;
2975 
2976         elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, bss->ies->data,
2977                                   bss->ies->len);
2978 
2979         if (!elem || elem->datalen < 10 ||
2980             !(elem->data[10] &
2981               WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
2982                 data->tolerated = false;
2983         }
2984 }
2985 
2986 static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
2987                                                struct ieee80211_vif *vif)
2988 {
2989         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2990         struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
2991                 .tolerated = true,
2992         };
2993 
2994         if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
2995                 mvmvif->he_ru_2mhz_block = false;
2996                 return;
2997         }
2998 
2999         cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
3000                           iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3001                           &iter_data);
3002 
3003         /*
3004          * If there is at least one AP on radar channel that cannot
3005          * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3006          */
3007         mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
3008 }
3009 
3010 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3011                                  struct ieee80211_vif *vif,
3012                                  struct ieee80211_sta *sta,
3013                                  enum ieee80211_sta_state old_state,
3014                                  enum ieee80211_sta_state new_state)
3015 {
3016         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3017         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3018         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3019         int ret;
3020 
3021         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3022                            sta->addr, old_state, new_state);
3023 
3024         /* this would be a mac80211 bug ... but don't crash */
3025         if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3026                 return -EINVAL;
3027 
3028         /*
3029          * If we are in a STA removal flow and in DQA mode:
3030          *
3031          * This is after the sync_rcu part, so the queues have already been
3032          * flushed. No more TXs on their way in mac80211's path, and no more in
3033          * the queues.
3034          * Also, we won't be getting any new TX frames for this station.
3035          * What we might have are deferred TX frames that need to be taken care
3036          * of.
3037          *
3038          * Drop any still-queued deferred-frame before removing the STA, and
3039          * make sure the worker is no longer handling frames for this STA.
3040          */
3041         if (old_state == IEEE80211_STA_NONE &&
3042             new_state == IEEE80211_STA_NOTEXIST) {
3043                 flush_work(&mvm->add_stream_wk);
3044 
3045                 /*
3046                  * No need to make sure deferred TX indication is off since the
3047                  * worker will already remove it if it was on
3048                  */
3049         }
3050 
3051         mutex_lock(&mvm->mutex);
3052         /* track whether or not the station is associated */
3053         mvm_sta->sta_state = new_state;
3054 
3055         if (old_state == IEEE80211_STA_NOTEXIST &&
3056             new_state == IEEE80211_STA_NONE) {
3057                 /*
3058                  * Firmware bug - it'll crash if the beacon interval is less
3059                  * than 16. We can't avoid connecting at all, so refuse the
3060                  * station state change, this will cause mac80211 to abandon
3061                  * attempts to connect to this AP, and eventually wpa_s will
3062                  * blacklist the AP...
3063                  */
3064                 if (vif->type == NL80211_IFTYPE_STATION &&
3065                     vif->bss_conf.beacon_int < 16) {
3066                         IWL_ERR(mvm,
3067                                 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3068                                 sta->addr, vif->bss_conf.beacon_int);
3069                         ret = -EINVAL;
3070                         goto out_unlock;
3071                 }
3072 
3073                 if (sta->tdls &&
3074                     (vif->p2p ||
3075                      iwl_mvm_tdls_sta_count(mvm, NULL) ==
3076                                                 IWL_MVM_TDLS_STA_COUNT ||
3077                      iwl_mvm_phy_ctx_count(mvm) > 1)) {
3078                         IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3079                         ret = -EBUSY;
3080                         goto out_unlock;
3081                 }
3082 
3083                 ret = iwl_mvm_add_sta(mvm, vif, sta);
3084                 if (sta->tdls && ret == 0) {
3085                         iwl_mvm_recalc_tdls_state(mvm, vif, true);
3086                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3087                                                    NL80211_TDLS_SETUP);
3088                 }
3089 
3090                 sta->max_rc_amsdu_len = 1;
3091         } else if (old_state == IEEE80211_STA_NONE &&
3092                    new_state == IEEE80211_STA_AUTH) {
3093                 /*
3094                  * EBS may be disabled due to previous failures reported by FW.
3095                  * Reset EBS status here assuming environment has been changed.
3096                  */
3097                 mvm->last_ebs_successful = true;
3098                 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3099                 ret = 0;
3100         } else if (old_state == IEEE80211_STA_AUTH &&
3101                    new_state == IEEE80211_STA_ASSOC) {
3102                 if (vif->type == NL80211_IFTYPE_AP) {
3103                         vif->bss_conf.he_support = sta->he_cap.has_he;
3104                         mvmvif->ap_assoc_sta_count++;
3105                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3106                         if (vif->bss_conf.he_support &&
3107                             !iwlwifi_mod_params.disable_11ax)
3108                                 iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
3109                 } else if (vif->type == NL80211_IFTYPE_STATION) {
3110                         vif->bss_conf.he_support = sta->he_cap.has_he;
3111 
3112                         mvmvif->he_ru_2mhz_block = false;
3113                         if (sta->he_cap.has_he)
3114                                 iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
3115 
3116                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3117                 }
3118 
3119                 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3120                                      false);
3121                 ret = iwl_mvm_update_sta(mvm, vif, sta);
3122         } else if (old_state == IEEE80211_STA_ASSOC &&
3123                    new_state == IEEE80211_STA_AUTHORIZED) {
3124                 ret = 0;
3125 
3126                 /* we don't support TDLS during DCM */
3127                 if (iwl_mvm_phy_ctx_count(mvm) > 1)
3128                         iwl_mvm_teardown_tdls_peers(mvm);
3129 
3130                 if (sta->tdls)
3131                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3132                                                    NL80211_TDLS_ENABLE_LINK);
3133 
3134                 /* enable beacon filtering */
3135                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
3136 
3137                 /*
3138                  * Now that the station is authorized, i.e., keys were already
3139                  * installed, need to indicate to the FW that
3140                  * multicast data frames can be forwarded to the driver
3141                  */
3142                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3143 
3144                 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3145                                      true);
3146         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
3147                    new_state == IEEE80211_STA_ASSOC) {
3148                 /* Multicast data frames are no longer allowed */
3149                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3150 
3151                 /* disable beacon filtering */
3152                 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3153                 WARN_ON(ret &&
3154                         !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3155                                   &mvm->status));
3156                 ret = 0;
3157         } else if (old_state == IEEE80211_STA_ASSOC &&
3158                    new_state == IEEE80211_STA_AUTH) {
3159                 if (vif->type == NL80211_IFTYPE_AP) {
3160                         mvmvif->ap_assoc_sta_count--;
3161                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3162                 }
3163                 ret = 0;
3164         } else if (old_state == IEEE80211_STA_AUTH &&
3165                    new_state == IEEE80211_STA_NONE) {
3166                 ret = 0;
3167         } else if (old_state == IEEE80211_STA_NONE &&
3168                    new_state == IEEE80211_STA_NOTEXIST) {
3169                 ret = iwl_mvm_rm_sta(mvm, vif, sta);
3170                 if (sta->tdls) {
3171                         iwl_mvm_recalc_tdls_state(mvm, vif, false);
3172                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3173                                                    NL80211_TDLS_DISABLE_LINK);
3174                 }
3175 
3176                 if (unlikely(ret &&
3177                              test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3178                                       &mvm->status)))
3179                         ret = 0;
3180         } else {
3181                 ret = -EIO;
3182         }
3183  out_unlock:
3184         mutex_unlock(&mvm->mutex);
3185 
3186         if (sta->tdls && ret == 0) {
3187                 if (old_state == IEEE80211_STA_NOTEXIST &&
3188                     new_state == IEEE80211_STA_NONE)
3189                         ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3190                 else if (old_state == IEEE80211_STA_NONE &&
3191                          new_state == IEEE80211_STA_NOTEXIST)
3192                         ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3193         }
3194 
3195         return ret;
3196 }
3197 
3198 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3199 {
3200         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3201 
3202         mvm->rts_threshold = value;
3203 
3204         return 0;
3205 }
3206 
3207 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3208                                   struct ieee80211_vif *vif,
3209                                   struct ieee80211_sta *sta, u32 changed)
3210 {
3211         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3212         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3213 
3214         if (changed & (IEEE80211_RC_BW_CHANGED |
3215                        IEEE80211_RC_SUPP_RATES_CHANGED |
3216                        IEEE80211_RC_NSS_CHANGED))
3217                 iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3218                                      true);
3219 
3220         if (vif->type == NL80211_IFTYPE_STATION &&
3221             changed & IEEE80211_RC_NSS_CHANGED)
3222                 iwl_mvm_sf_update(mvm, vif, false);
3223 }
3224 
3225 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3226                                struct ieee80211_vif *vif, u16 ac,
3227                                const struct ieee80211_tx_queue_params *params)
3228 {
3229         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3230         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3231 
3232         mvmvif->queue_params[ac] = *params;
3233 
3234         /*
3235          * No need to update right away, we'll get BSS_CHANGED_QOS
3236          * The exception is P2P_DEVICE interface which needs immediate update.
3237          */
3238         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3239                 int ret;
3240 
3241                 mutex_lock(&mvm->mutex);
3242                 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3243                 mutex_unlock(&mvm->mutex);
3244                 return ret;
3245         }
3246         return 0;
3247 }
3248 
3249 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3250                                        struct ieee80211_vif *vif,
3251                                        u16 req_duration)
3252 {
3253         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3254         u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
3255         u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
3256 
3257         if (req_duration > duration)
3258                 duration = req_duration;
3259 
3260         mutex_lock(&mvm->mutex);
3261         /* Try really hard to protect the session and hear a beacon */
3262         iwl_mvm_protect_session(mvm, vif, duration, min_duration, 500, false);
3263         mutex_unlock(&mvm->mutex);
3264 }
3265 
3266 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3267                                         struct ieee80211_vif *vif,
3268                                         struct cfg80211_sched_scan_request *req,
3269                                         struct ieee80211_scan_ies *ies)
3270 {
3271         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3272 
3273         int ret;
3274 
3275         mutex_lock(&mvm->mutex);
3276 
3277         if (!vif->bss_conf.idle) {
3278                 ret = -EBUSY;
3279                 goto out;
3280         }
3281 
3282         ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3283 
3284 out:
3285         mutex_unlock(&mvm->mutex);
3286         return ret;
3287 }
3288 
3289 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3290                                        struct ieee80211_vif *vif)
3291 {
3292         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3293         int ret;
3294 
3295         mutex_lock(&mvm->mutex);
3296 
3297         /* Due to a race condition, it's possible that mac80211 asks
3298          * us to stop a sched_scan when it's already stopped.  This
3299          * can happen, for instance, if we stopped the scan ourselves,
3300          * called ieee80211_sched_scan_stopped() and the userspace called
3301          * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3302          * could run.  To handle this, simply return if the scan is
3303          * not running.
3304         */
3305         if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3306                 mutex_unlock(&mvm->mutex);
3307                 return 0;
3308         }
3309 
3310         ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3311         mutex_unlock(&mvm->mutex);
3312         iwl_mvm_wait_for_async_handlers(mvm);
3313 
3314         return ret;
3315 }
3316 
3317 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3318                                  enum set_key_cmd cmd,
3319                                  struct ieee80211_vif *vif,
3320                                  struct ieee80211_sta *sta,
3321                                  struct ieee80211_key_conf *key)
3322 {
3323         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3324         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3325         struct iwl_mvm_sta *mvmsta;
3326         struct iwl_mvm_key_pn *ptk_pn;
3327         int keyidx = key->keyidx;
3328         int ret, i;
3329         u8 key_offset;
3330 
3331         if (iwlwifi_mod_params.swcrypto) {
3332                 IWL_DEBUG_MAC80211(mvm, "leave - hwcrypto disabled\n");
3333                 return -EOPNOTSUPP;
3334         }
3335 
3336         switch (key->cipher) {
3337         case WLAN_CIPHER_SUITE_TKIP:
3338                 if (!mvm->trans->trans_cfg->gen2) {
3339                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3340                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3341                 } else if (vif->type == NL80211_IFTYPE_STATION) {
3342                         key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
3343                 } else {
3344                         IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
3345                         return -EOPNOTSUPP;
3346                 }
3347                 break;
3348         case WLAN_CIPHER_SUITE_CCMP:
3349         case WLAN_CIPHER_SUITE_GCMP:
3350         case WLAN_CIPHER_SUITE_GCMP_256:
3351                 if (!iwl_mvm_has_new_tx_api(mvm))
3352                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3353                 break;
3354         case WLAN_CIPHER_SUITE_AES_CMAC:
3355         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3356         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3357                 WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3358                 break;
3359         case WLAN_CIPHER_SUITE_WEP40:
3360         case WLAN_CIPHER_SUITE_WEP104:
3361                 if (vif->type == NL80211_IFTYPE_STATION)
3362                         break;
3363                 if (iwl_mvm_has_new_tx_api(mvm))
3364                         return -EOPNOTSUPP;
3365                 /* support HW crypto on TX */
3366                 return 0;
3367         default:
3368                 /* currently FW supports only one optional cipher scheme */
3369                 if (hw->n_cipher_schemes &&
3370                     hw->cipher_schemes->cipher == key->cipher)
3371                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3372                 else
3373                         return -EOPNOTSUPP;
3374         }
3375 
3376         switch (cmd) {
3377         case SET_KEY:
3378                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
3379                      vif->type == NL80211_IFTYPE_AP) && !sta) {
3380                         /*
3381                          * GTK on AP interface is a TX-only key, return 0;
3382                          * on IBSS they're per-station and because we're lazy
3383                          * we don't support them for RX, so do the same.
3384                          * CMAC/GMAC in AP/IBSS modes must be done in software.
3385                          */
3386                         if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3387                             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3388                             key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)
3389                                 ret = -EOPNOTSUPP;
3390                         else
3391                                 ret = 0;
3392 
3393                         if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
3394                             key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
3395                             !iwl_mvm_has_new_tx_api(mvm)) {
3396                                 key->hw_key_idx = STA_KEY_IDX_INVALID;
3397                                 break;
3398                         }
3399 
3400                         if (!mvmvif->ap_ibss_active) {
3401                                 for (i = 0;
3402                                      i < ARRAY_SIZE(mvmvif->ap_early_keys);
3403                                      i++) {
3404                                         if (!mvmvif->ap_early_keys[i]) {
3405                                                 mvmvif->ap_early_keys[i] = key;
3406                                                 break;
3407                                         }
3408                                 }
3409 
3410                                 if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3411                                         ret = -ENOSPC;
3412 
3413                                 break;
3414                         }
3415                 }
3416 
3417                 /* During FW restart, in order to restore the state as it was,
3418                  * don't try to reprogram keys we previously failed for.
3419                  */
3420                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3421                     key->hw_key_idx == STA_KEY_IDX_INVALID) {
3422                         IWL_DEBUG_MAC80211(mvm,
3423                                            "skip invalid idx key programming during restart\n");
3424                         ret = 0;
3425                         break;
3426                 }
3427 
3428                 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3429                     sta && iwl_mvm_has_new_rx_api(mvm) &&
3430                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3431                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3432                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3433                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3434                         struct ieee80211_key_seq seq;
3435                         int tid, q;
3436 
3437                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
3438                         WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3439                         ptk_pn = kzalloc(struct_size(ptk_pn, q,
3440                                                      mvm->trans->num_rx_queues),
3441                                          GFP_KERNEL);
3442                         if (!ptk_pn) {
3443                                 ret = -ENOMEM;
3444                                 break;
3445                         }
3446 
3447                         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3448                                 ieee80211_get_key_rx_seq(key, tid, &seq);
3449                                 for (q = 0; q < mvm->trans->num_rx_queues; q++)
3450                                         memcpy(ptk_pn->q[q].pn[tid],
3451                                                seq.ccmp.pn,
3452                                                IEEE80211_CCMP_PN_LEN);
3453                         }
3454 
3455                         rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3456                 }
3457 
3458                 /* in HW restart reuse the index, otherwise request a new one */
3459                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3460                         key_offset = key->hw_key_idx;
3461                 else
3462                         key_offset = STA_KEY_IDX_INVALID;
3463 
3464                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
3465                 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3466                 if (ret) {
3467                         IWL_WARN(mvm, "set key failed\n");
3468                         key->hw_key_idx = STA_KEY_IDX_INVALID;
3469                         /*
3470                          * can't add key for RX, but we don't need it
3471                          * in the device for TX so still return 0,
3472                          * unless we have new TX API where we cannot
3473                          * put key material into the TX_CMD
3474                          */
3475                         if (iwl_mvm_has_new_tx_api(mvm))
3476                                 ret = -EOPNOTSUPP;
3477                         else
3478                                 ret = 0;
3479                 }
3480 
3481                 break;
3482         case DISABLE_KEY:
3483                 ret = -ENOENT;
3484                 for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3485                         if (mvmvif->ap_early_keys[i] == key) {
3486                                 mvmvif->ap_early_keys[i] = NULL;
3487                                 ret = 0;
3488                         }
3489                 }
3490 
3491                 /* found in pending list - don't do anything else */
3492                 if (ret == 0)
3493                         break;
3494 
3495                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3496                         ret = 0;
3497                         break;
3498                 }
3499 
3500                 if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3501                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3502                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3503                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3504                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3505                         mvmsta = iwl_mvm_sta_from_mac80211(sta);
3506                         ptk_pn = rcu_dereference_protected(
3507                                                 mvmsta->ptk_pn[keyidx],
3508                                                 lockdep_is_held(&mvm->mutex));
3509                         RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3510                         if (ptk_pn)
3511                                 kfree_rcu(ptk_pn, rcu_head);
3512                 }
3513 
3514                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3515                 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3516                 break;
3517         default:
3518                 ret = -EINVAL;
3519         }
3520 
3521         return ret;
3522 }
3523 
3524 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3525                                enum set_key_cmd cmd,
3526                                struct ieee80211_vif *vif,
3527                                struct ieee80211_sta *sta,
3528                                struct ieee80211_key_conf *key)
3529 {
3530         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3531         int ret;
3532 
3533         mutex_lock(&mvm->mutex);
3534         ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3535         mutex_unlock(&mvm->mutex);
3536 
3537         return ret;
3538 }
3539 
3540 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3541                                         struct ieee80211_vif *vif,
3542                                         struct ieee80211_key_conf *keyconf,
3543                                         struct ieee80211_sta *sta,
3544                                         u32 iv32, u16 *phase1key)
3545 {
3546         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3547 
3548         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3549                 return;
3550 
3551         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3552 }
3553 
3554 
3555 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3556                                struct iwl_rx_packet *pkt, void *data)
3557 {
3558         struct iwl_mvm *mvm =
3559                 container_of(notif_wait, struct iwl_mvm, notif_wait);
3560         struct iwl_hs20_roc_res *resp;
3561         int resp_len = iwl_rx_packet_payload_len(pkt);
3562         struct iwl_mvm_time_event_data *te_data = data;
3563 
3564         if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3565                 return true;
3566 
3567         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3568                 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3569                 return true;
3570         }
3571 
3572         resp = (void *)pkt->data;
3573 
3574         IWL_DEBUG_TE(mvm,
3575                      "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3576                      resp->status, resp->event_unique_id);
3577 
3578         te_data->uid = le32_to_cpu(resp->event_unique_id);
3579         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3580                      te_data->uid);
3581 
3582         spin_lock_bh(&mvm->time_event_lock);
3583         list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3584         spin_unlock_bh(&mvm->time_event_lock);
3585 
3586         return true;
3587 }
3588 
3589 #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3590 #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3591 #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3592 #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3593 #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3594 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3595                                     struct ieee80211_channel *channel,
3596                                     struct ieee80211_vif *vif,
3597                                     int duration)
3598 {
3599         int res;
3600         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3601         struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3602         static const u16 time_event_response[] = { HOT_SPOT_CMD };
3603         struct iwl_notification_wait wait_time_event;
3604         u32 dtim_interval = vif->bss_conf.dtim_period *
3605                 vif->bss_conf.beacon_int;
3606         u32 req_dur, delay;
3607         struct iwl_hs20_roc_req aux_roc_req = {
3608                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3609                 .id_and_color =
3610                         cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3611                 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3612         };
3613         struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
3614                 &aux_roc_req.channel_info);
3615         u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
3616 
3617         /* Set the channel info data */
3618         iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
3619                               (channel->band == NL80211_BAND_2GHZ) ?
3620                                PHY_BAND_24 : PHY_BAND_5,
3621                               PHY_VHT_CHANNEL_MODE20,
3622                               0);
3623 
3624         /* Set the time and duration */
3625         tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3626 
3627         delay = AUX_ROC_MIN_DELAY;
3628         req_dur = MSEC_TO_TU(duration);
3629 
3630         /*
3631          * If we are associated we want the delay time to be at least one
3632          * dtim interval so that the FW can wait until after the DTIM and
3633          * then start the time event, this will potentially allow us to
3634          * remain off-channel for the max duration.
3635          * Since we want to use almost a whole dtim interval we would also
3636          * like the delay to be for 2-3 dtim intervals, in case there are
3637          * other time events with higher priority.
3638          */
3639         if (vif->bss_conf.assoc) {
3640                 delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3641                 /* We cannot remain off-channel longer than the DTIM interval */
3642                 if (dtim_interval <= req_dur) {
3643                         req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3644                         if (req_dur <= AUX_ROC_MIN_DURATION)
3645                                 req_dur = dtim_interval -
3646                                         AUX_ROC_MIN_SAFETY_BUFFER;
3647                 }
3648         }
3649 
3650         tail->duration = cpu_to_le32(req_dur);
3651         tail->apply_time_max_delay = cpu_to_le32(delay);
3652 
3653         IWL_DEBUG_TE(mvm,
3654                      "ROC: Requesting to remain on channel %u for %ums (requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3655                      channel->hw_value, req_dur, duration, delay,
3656                      dtim_interval);
3657         /* Set the node address */
3658         memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3659 
3660         lockdep_assert_held(&mvm->mutex);
3661 
3662         spin_lock_bh(&mvm->time_event_lock);
3663 
3664         if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3665                 spin_unlock_bh(&mvm->time_event_lock);
3666                 return -EIO;
3667         }
3668 
3669         te_data->vif = vif;
3670         te_data->duration = duration;
3671         te_data->id = HOT_SPOT_CMD;
3672 
3673         spin_unlock_bh(&mvm->time_event_lock);
3674 
3675         /*
3676          * Use a notification wait, which really just processes the
3677          * command response and doesn't wait for anything, in order
3678          * to be able to process the response and get the UID inside
3679          * the RX path. Using CMD_WANT_SKB doesn't work because it
3680          * stores the buffer and then wakes up this thread, by which
3681          * time another notification (that the time event started)
3682          * might already be processed unsuccessfully.
3683          */
3684         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3685                                    time_event_response,
3686                                    ARRAY_SIZE(time_event_response),
3687                                    iwl_mvm_rx_aux_roc, te_data);
3688 
3689         res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3690                                    &aux_roc_req);
3691 
3692         if (res) {
3693                 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3694                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3695                 goto out_clear_te;
3696         }
3697 
3698         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
3699         res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3700         /* should never fail */
3701         WARN_ON_ONCE(res);
3702 
3703         if (res) {
3704  out_clear_te:
3705                 spin_lock_bh(&mvm->time_event_lock);
3706                 iwl_mvm_te_clear_data(mvm, te_data);
3707                 spin_unlock_bh(&mvm->time_event_lock);
3708         }
3709 
3710         return res;
3711 }
3712 
3713 static int iwl_mvm_roc(struct ieee80211_hw *hw,
3714                        struct ieee80211_vif *vif,
3715                        struct ieee80211_channel *channel,
3716                        int duration,
3717                        enum ieee80211_roc_type type)
3718 {
3719         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3720         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3721         struct cfg80211_chan_def chandef;
3722         struct iwl_mvm_phy_ctxt *phy_ctxt;
3723         int ret, i;
3724 
3725         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3726                            duration, type);
3727 
3728         /*
3729          * Flush the done work, just in case it's still pending, so that
3730          * the work it does can complete and we can accept new frames.
3731          */
3732         flush_work(&mvm->roc_done_wk);
3733 
3734         mutex_lock(&mvm->mutex);
3735 
3736         switch (vif->type) {
3737         case NL80211_IFTYPE_STATION:
3738                 if (fw_has_capa(&mvm->fw->ucode_capa,
3739                                 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3740                         /* Use aux roc framework (HS20) */
3741                         ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3742                                                        vif, duration);
3743                         goto out_unlock;
3744                 }
3745                 IWL_ERR(mvm, "hotspot not supported\n");
3746                 ret = -EINVAL;
3747                 goto out_unlock;
3748         case NL80211_IFTYPE_P2P_DEVICE:
3749                 /* handle below */
3750                 break;
3751         default:
3752                 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3753                 ret = -EINVAL;
3754                 goto out_unlock;
3755         }
3756 
3757         for (i = 0; i < NUM_PHY_CTX; i++) {
3758                 phy_ctxt = &mvm->phy_ctxts[i];
3759                 if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3760                         continue;
3761 
3762                 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3763                         /*
3764                          * Unbind the P2P_DEVICE from the current PHY context,
3765                          * and if the PHY context is not used remove it.
3766                          */
3767                         ret = iwl_mvm_binding_remove_vif(mvm, vif);
3768                         if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3769                                 goto out_unlock;
3770 
3771                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3772 
3773                         /* Bind the P2P_DEVICE to the current PHY Context */
3774                         mvmvif->phy_ctxt = phy_ctxt;
3775 
3776                         ret = iwl_mvm_binding_add_vif(mvm, vif);
3777                         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3778                                 goto out_unlock;
3779 
3780                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3781                         goto schedule_time_event;
3782                 }
3783         }
3784 
3785         /* Need to update the PHY context only if the ROC channel changed */
3786         if (channel == mvmvif->phy_ctxt->channel)
3787                 goto schedule_time_event;
3788 
3789         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3790 
3791         /*
3792          * Change the PHY context configuration as it is currently referenced
3793          * only by the P2P Device MAC
3794          */
3795         if (mvmvif->phy_ctxt->ref == 1) {
3796                 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3797                                                &chandef, 1, 1);
3798                 if (ret)
3799                         goto out_unlock;
3800         } else {
3801                 /*
3802                  * The PHY context is shared with other MACs. Need to remove the
3803                  * P2P Device from the binding, allocate an new PHY context and
3804                  * create a new binding
3805                  */
3806                 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3807                 if (!phy_ctxt) {
3808                         ret = -ENOSPC;
3809                         goto out_unlock;
3810                 }
3811 
3812                 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3813                                                1, 1);
3814                 if (ret) {
3815                         IWL_ERR(mvm, "Failed to change PHY context\n");
3816                         goto out_unlock;
3817                 }
3818 
3819                 /* Unbind the P2P_DEVICE from the current PHY context */
3820                 ret = iwl_mvm_binding_remove_vif(mvm, vif);
3821                 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3822                         goto out_unlock;
3823 
3824                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3825 
3826                 /* Bind the P2P_DEVICE to the new allocated PHY context */
3827                 mvmvif->phy_ctxt = phy_ctxt;
3828 
3829                 ret = iwl_mvm_binding_add_vif(mvm, vif);
3830                 if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3831                         goto out_unlock;
3832 
3833                 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3834         }
3835 
3836 schedule_time_event:
3837         /* Schedule the time events */
3838         ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
3839 
3840 out_unlock:
3841         mutex_unlock(&mvm->mutex);
3842         IWL_DEBUG_MAC80211(mvm, "leave\n");
3843         return ret;
3844 }
3845 
3846 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
3847                               struct ieee80211_vif *vif)
3848 {
3849         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3850 
3851         IWL_DEBUG_MAC80211(mvm, "enter\n");
3852 
3853         mutex_lock(&mvm->mutex);
3854         iwl_mvm_stop_roc(mvm);
3855         mutex_unlock(&mvm->mutex);
3856 
3857         IWL_DEBUG_MAC80211(mvm, "leave\n");
3858         return 0;
3859 }
3860 
3861 struct iwl_mvm_ftm_responder_iter_data {
3862         bool responder;
3863         struct ieee80211_chanctx_conf *ctx;
3864 };
3865 
3866 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
3867                                                struct ieee80211_vif *vif)
3868 {
3869         struct iwl_mvm_ftm_responder_iter_data *data = _data;
3870 
3871         if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
3872             vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
3873                 data->responder = true;
3874 }
3875 
3876 static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
3877                                              struct ieee80211_chanctx_conf *ctx)
3878 {
3879         struct iwl_mvm_ftm_responder_iter_data data = {
3880                 .responder = false,
3881                 .ctx = ctx,
3882         };
3883 
3884         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
3885                                         IEEE80211_IFACE_ITER_NORMAL,
3886                                         iwl_mvm_ftm_responder_chanctx_iter,
3887                                         &data);
3888         return data.responder;
3889 }
3890 
3891 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
3892                                  struct ieee80211_chanctx_conf *ctx)
3893 {
3894         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3895         struct iwl_mvm_phy_ctxt *phy_ctxt;
3896         bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3897         struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3898         int ret;
3899 
3900         lockdep_assert_held(&mvm->mutex);
3901 
3902         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
3903 
3904         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3905         if (!phy_ctxt) {
3906                 ret = -ENOSPC;
3907                 goto out;
3908         }
3909 
3910         ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
3911                                        ctx->rx_chains_static,
3912                                        ctx->rx_chains_dynamic);
3913         if (ret) {
3914                 IWL_ERR(mvm, "Failed to add PHY context\n");
3915                 goto out;
3916         }
3917 
3918         iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
3919         *phy_ctxt_id = phy_ctxt->id;
3920 out:
3921         return ret;
3922 }
3923 
3924 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
3925                                struct ieee80211_chanctx_conf *ctx)
3926 {
3927         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3928         int ret;
3929 
3930         mutex_lock(&mvm->mutex);
3931         ret = __iwl_mvm_add_chanctx(mvm, ctx);
3932         mutex_unlock(&mvm->mutex);
3933 
3934         return ret;
3935 }
3936 
3937 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
3938                                      struct ieee80211_chanctx_conf *ctx)
3939 {
3940         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3941         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3942 
3943         lockdep_assert_held(&mvm->mutex);
3944 
3945         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
3946 }
3947 
3948 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
3949                                    struct ieee80211_chanctx_conf *ctx)
3950 {
3951         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3952 
3953         mutex_lock(&mvm->mutex);
3954         __iwl_mvm_remove_chanctx(mvm, ctx);
3955         mutex_unlock(&mvm->mutex);
3956 }
3957 
3958 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
3959                                    struct ieee80211_chanctx_conf *ctx,
3960                                    u32 changed)
3961 {
3962         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3963         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
3964         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
3965         bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
3966         struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
3967 
3968         if (WARN_ONCE((phy_ctxt->ref > 1) &&
3969                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
3970                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
3971                                    IEEE80211_CHANCTX_CHANGE_RADAR |
3972                                    IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
3973                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
3974                       phy_ctxt->ref, changed))
3975                 return;
3976 
3977         mutex_lock(&mvm->mutex);
3978 
3979         /* we are only changing the min_width, may be a noop */
3980         if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
3981                 if (phy_ctxt->width == def->width)
3982                         goto out_unlock;
3983 
3984                 /* we are just toggling between 20_NOHT and 20 */
3985                 if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
3986                     def->width <= NL80211_CHAN_WIDTH_20)
3987                         goto out_unlock;
3988         }
3989 
3990         iwl_mvm_bt_coex_vif_change(mvm);
3991         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
3992                                  ctx->rx_chains_static,
3993                                  ctx->rx_chains_dynamic);
3994 
3995 out_unlock:
3996         mutex_unlock(&mvm->mutex);
3997 }
3998 
3999 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4000                                         struct ieee80211_vif *vif,
4001                                         struct ieee80211_chanctx_conf *ctx,
4002                                         bool switching_chanctx)
4003 {
4004         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4005         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4006         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4007         int ret;
4008 
4009         lockdep_assert_held(&mvm->mutex);
4010 
4011         mvmvif->phy_ctxt = phy_ctxt;
4012 
4013         switch (vif->type) {
4014         case NL80211_IFTYPE_AP:
4015                 /* only needed if we're switching chanctx (i.e. during CSA) */
4016                 if (switching_chanctx) {
4017                         mvmvif->ap_ibss_active = true;
4018                         break;
4019                 }
4020                 /* fall through */
4021         case NL80211_IFTYPE_ADHOC:
4022                 /*
4023                  * The AP binding flow is handled as part of the start_ap flow
4024                  * (in bss_info_changed), similarly for IBSS.
4025                  */
4026                 ret = 0;
4027                 goto out;
4028         case NL80211_IFTYPE_STATION:
4029                 mvmvif->csa_bcn_pending = false;
4030                 break;
4031         case NL80211_IFTYPE_MONITOR:
4032                 /* always disable PS when a monitor interface is active */
4033                 mvmvif->ps_disabled = true;
4034                 break;
4035         default:
4036                 ret = -EINVAL;
4037                 goto out;
4038         }
4039 
4040         ret = iwl_mvm_binding_add_vif(mvm, vif);
4041         if (ret)
4042                 goto out;
4043 
4044         /*
4045          * Power state must be updated before quotas,
4046          * otherwise fw will complain.
4047          */
4048         iwl_mvm_power_update_mac(mvm);
4049 
4050         /* Setting the quota at this stage is only required for monitor
4051          * interfaces. For the other types, the bss_info changed flow
4052          * will handle quota settings.
4053          */
4054         if (vif->type == NL80211_IFTYPE_MONITOR) {
4055                 mvmvif->monitor_active = true;
4056                 ret = iwl_mvm_update_quotas(mvm, false, NULL);
4057                 if (ret)
4058                         goto out_remove_binding;
4059 
4060                 ret = iwl_mvm_add_snif_sta(mvm, vif);
4061                 if (ret)
4062                         goto out_remove_binding;
4063 
4064         }
4065 
4066         /* Handle binding during CSA */
4067         if (vif->type == NL80211_IFTYPE_AP) {
4068                 iwl_mvm_update_quotas(mvm, false, NULL);
4069                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4070         }
4071 
4072         if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
4073                 mvmvif->csa_bcn_pending = true;
4074 
4075                 if (!fw_has_capa(&mvm->fw->ucode_capa,
4076                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4077                         u32 duration = 3 * vif->bss_conf.beacon_int;
4078 
4079                         /* Protect the session to make sure we hear the first
4080                          * beacon on the new channel.
4081                          */
4082                         iwl_mvm_protect_session(mvm, vif, duration, duration,
4083                                                 vif->bss_conf.beacon_int / 2,
4084                                                 true);
4085                 }
4086 
4087                 iwl_mvm_update_quotas(mvm, false, NULL);
4088         }
4089 
4090         goto out;
4091 
4092 out_remove_binding:
4093         iwl_mvm_binding_remove_vif(mvm, vif);
4094         iwl_mvm_power_update_mac(mvm);
4095 out:
4096         if (ret)
4097                 mvmvif->phy_ctxt = NULL;
4098         return ret;
4099 }
4100 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4101                                       struct ieee80211_vif *vif,
4102                                       struct ieee80211_chanctx_conf *ctx)
4103 {
4104         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4105         int ret;
4106 
4107         mutex_lock(&mvm->mutex);
4108         ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4109         mutex_unlock(&mvm->mutex);
4110 
4111         return ret;
4112 }
4113 
4114 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4115                                            struct ieee80211_vif *vif,
4116                                            struct ieee80211_chanctx_conf *ctx,
4117                                            bool switching_chanctx)
4118 {
4119         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4120         struct ieee80211_vif *disabled_vif = NULL;
4121 
4122         lockdep_assert_held(&mvm->mutex);
4123 
4124         iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4125 
4126         switch (vif->type) {
4127         case NL80211_IFTYPE_ADHOC:
4128                 goto out;
4129         case NL80211_IFTYPE_MONITOR:
4130                 mvmvif->monitor_active = false;
4131                 mvmvif->ps_disabled = false;
4132                 iwl_mvm_rm_snif_sta(mvm, vif);
4133                 break;
4134         case NL80211_IFTYPE_AP:
4135                 /* This part is triggered only during CSA */
4136                 if (!switching_chanctx || !mvmvif->ap_ibss_active)
4137                         goto out;
4138 
4139                 mvmvif->csa_countdown = false;
4140 
4141                 /* Set CS bit on all the stations */
4142                 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4143 
4144                 /* Save blocked iface, the timeout is set on the next beacon */
4145                 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4146 
4147                 mvmvif->ap_ibss_active = false;
4148                 break;
4149         case NL80211_IFTYPE_STATION:
4150                 if (!switching_chanctx)
4151                         break;
4152 
4153                 disabled_vif = vif;
4154 
4155                 if (!fw_has_capa(&mvm->fw->ucode_capa,
4156                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4157                         iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4158                 break;
4159         default:
4160                 break;
4161         }
4162 
4163         iwl_mvm_update_quotas(mvm, false, disabled_vif);
4164         iwl_mvm_binding_remove_vif(mvm, vif);
4165 
4166 out:
4167         mvmvif->phy_ctxt = NULL;
4168         iwl_mvm_power_update_mac(mvm);
4169 }
4170 
4171 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4172                                          struct ieee80211_vif *vif,
4173                                          struct ieee80211_chanctx_conf *ctx)
4174 {
4175         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4176 
4177         mutex_lock(&mvm->mutex);
4178         __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4179         mutex_unlock(&mvm->mutex);
4180 }
4181 
4182 static int
4183 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4184                                 struct ieee80211_vif_chanctx_switch *vifs)
4185 {
4186         int ret;
4187 
4188         mutex_lock(&mvm->mutex);
4189         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4190         __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4191 
4192         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4193         if (ret) {
4194                 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4195                 goto out_reassign;
4196         }
4197 
4198         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4199                                            true);
4200         if (ret) {
4201                 IWL_ERR(mvm,
4202                         "failed to assign new_ctx during channel switch\n");
4203                 goto out_remove;
4204         }
4205 
4206         /* we don't support TDLS during DCM - can be caused by channel switch */
4207         if (iwl_mvm_phy_ctx_count(mvm) > 1)
4208                 iwl_mvm_teardown_tdls_peers(mvm);
4209 
4210         goto out;
4211 
4212 out_remove:
4213         __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4214 
4215 out_reassign:
4216         if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4217                 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4218                 goto out_restart;
4219         }
4220 
4221         if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4222                                          true)) {
4223                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4224                 goto out_restart;
4225         }
4226 
4227         goto out;
4228 
4229 out_restart:
4230         /* things keep failing, better restart the hw */
4231         iwl_mvm_nic_restart(mvm, false);
4232 
4233 out:
4234         mutex_unlock(&mvm->mutex);
4235 
4236         return ret;
4237 }
4238 
4239 static int
4240 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4241                                     struct ieee80211_vif_chanctx_switch *vifs)
4242 {
4243         int ret;
4244 
4245         mutex_lock(&mvm->mutex);
4246         __iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4247 
4248         ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4249                                            true);
4250         if (ret) {
4251                 IWL_ERR(mvm,
4252                         "failed to assign new_ctx during channel switch\n");
4253                 goto out_reassign;
4254         }
4255 
4256         goto out;
4257 
4258 out_reassign:
4259         if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4260                                          true)) {
4261                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4262                 goto out_restart;
4263         }
4264 
4265         goto out;
4266 
4267 out_restart:
4268         /* things keep failing, better restart the hw */
4269         iwl_mvm_nic_restart(mvm, false);
4270 
4271 out:
4272         mutex_unlock(&mvm->mutex);
4273 
4274         return ret;
4275 }
4276 
4277 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4278                                       struct ieee80211_vif_chanctx_switch *vifs,
4279                                       int n_vifs,
4280                                       enum ieee80211_chanctx_switch_mode mode)
4281 {
4282         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4283         int ret;
4284 
4285         /* we only support a single-vif right now */
4286         if (n_vifs > 1)
4287                 return -EOPNOTSUPP;
4288 
4289         switch (mode) {
4290         case CHANCTX_SWMODE_SWAP_CONTEXTS:
4291                 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4292                 break;
4293         case CHANCTX_SWMODE_REASSIGN_VIF:
4294                 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4295                 break;
4296         default:
4297                 ret = -EOPNOTSUPP;
4298                 break;
4299         }
4300 
4301         return ret;
4302 }
4303 
4304 static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
4305 {
4306         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4307 
4308         return mvm->ibss_manager;
4309 }
4310 
4311 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4312                            struct ieee80211_sta *sta,
4313                            bool set)
4314 {
4315         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4316         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4317 
4318         if (!mvm_sta || !mvm_sta->vif) {
4319                 IWL_ERR(mvm, "Station is not associated to a vif\n");
4320                 return -EINVAL;
4321         }
4322 
4323         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4324 }
4325 
4326 #ifdef CONFIG_NL80211_TESTMODE
4327 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4328         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4329         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4330         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4331 };
4332 
4333 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4334                                       struct ieee80211_vif *vif,
4335                                       void *data, int len)
4336 {
4337         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4338         int err;
4339         u32 noa_duration;
4340 
4341         err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
4342                                    iwl_mvm_tm_policy, NULL);
4343         if (err)
4344                 return err;
4345 
4346         if (!tb[IWL_MVM_TM_ATTR_CMD])
4347                 return -EINVAL;
4348 
4349         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4350         case IWL_MVM_TM_CMD_SET_NOA:
4351                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4352                     !vif->bss_conf.enable_beacon ||
4353                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4354                         return -EINVAL;
4355 
4356                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4357                 if (noa_duration >= vif->bss_conf.beacon_int)
4358                         return -EINVAL;
4359 
4360                 mvm->noa_duration = noa_duration;
4361                 mvm->noa_vif = vif;
4362 
4363                 return iwl_mvm_update_quotas(mvm, true, NULL);
4364         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4365                 /* must be associated client vif - ignore authorized */
4366                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4367                     !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4368                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4369                         return -EINVAL;
4370 
4371                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4372                         return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4373                 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4374         }
4375 
4376         return -EOPNOTSUPP;
4377 }
4378 
4379 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4380                                     struct ieee80211_vif *vif,
4381                                     void *data, int len)
4382 {
4383         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4384         int err;
4385 
4386         mutex_lock(&mvm->mutex);
4387         err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4388         mutex_unlock(&mvm->mutex);
4389 
4390         return err;
4391 }
4392 #endif
4393 
4394 static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4395                                    struct ieee80211_vif *vif,
4396                                    struct ieee80211_channel_switch *chsw)
4397 {
4398         /* By implementing this operation, we prevent mac80211 from
4399          * starting its own channel switch timer, so that we can call
4400          * ieee80211_chswitch_done() ourselves at the right time
4401          * (which is when the absence time event starts).
4402          */
4403 
4404         IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4405                            "dummy channel switch op\n");
4406 }
4407 
4408 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
4409                                        struct ieee80211_vif *vif,
4410                                        struct ieee80211_channel_switch *chsw)
4411 {
4412         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4413         struct iwl_chan_switch_te_cmd cmd = {
4414                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4415                                                           mvmvif->color)),
4416                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4417                 .tsf = cpu_to_le32(chsw->timestamp),
4418                 .cs_count = chsw->count,
4419                 .cs_mode = chsw->block_tx,
4420         };
4421 
4422         lockdep_assert_held(&mvm->mutex);
4423 
4424         if (chsw->delay)
4425                 cmd.cs_delayed_bcn_count =
4426                         DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
4427 
4428         return iwl_mvm_send_cmd_pdu(mvm,
4429                                     WIDE_ID(MAC_CONF_GROUP,
4430                                             CHANNEL_SWITCH_TIME_EVENT_CMD),
4431                                     0, sizeof(cmd), &cmd);
4432 }
4433 
4434 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
4435                                        struct ieee80211_vif *vif,
4436                                        struct ieee80211_channel_switch *chsw)
4437 {
4438         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4439         u32 apply_time;
4440 
4441         /* Schedule the time event to a bit before beacon 1,
4442          * to make sure we're in the new channel when the
4443          * GO/AP arrives. In case count <= 1 immediately schedule the
4444          * TE (this might result with some packet loss or connection
4445          * loss).
4446          */
4447         if (chsw->count <= 1)
4448                 apply_time = 0;
4449         else
4450                 apply_time = chsw->device_timestamp +
4451                         ((vif->bss_conf.beacon_int * (chsw->count - 1) -
4452                           IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
4453 
4454         if (chsw->block_tx)
4455                 iwl_mvm_csa_client_absent(mvm, vif);
4456 
4457         if (mvmvif->bf_data.bf_enabled) {
4458                 int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4459 
4460                 if (ret)
4461                         return ret;
4462         }
4463 
4464         iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
4465                                     apply_time);
4466 
4467         return 0;
4468 }
4469 
4470 #define IWL_MAX_CSA_BLOCK_TX 1500
4471 static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4472                                       struct ieee80211_vif *vif,
4473                                       struct ieee80211_channel_switch *chsw)
4474 {
4475         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4476         struct ieee80211_vif *csa_vif;
4477         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4478         int ret;
4479 
4480         mutex_lock(&mvm->mutex);
4481 
4482         mvmvif->csa_failed = false;
4483 
4484         IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4485                            chsw->chandef.center_freq1);
4486 
4487         iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
4488                                        ieee80211_vif_to_wdev(vif),
4489                                        FW_DBG_TRIGGER_CHANNEL_SWITCH);
4490 
4491         switch (vif->type) {
4492         case NL80211_IFTYPE_AP:
4493                 csa_vif =
4494                         rcu_dereference_protected(mvm->csa_vif,
4495                                                   lockdep_is_held(&mvm->mutex));
4496                 if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4497                               "Another CSA is already in progress")) {
4498                         ret = -EBUSY;
4499                         goto out_unlock;
4500                 }
4501 
4502                 /* we still didn't unblock tx. prevent new CS meanwhile */
4503                 if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4504                                               lockdep_is_held(&mvm->mutex))) {
4505                         ret = -EBUSY;
4506                         goto out_unlock;
4507                 }
4508 
4509                 rcu_assign_pointer(mvm->csa_vif, vif);
4510 
4511                 if (WARN_ONCE(mvmvif->csa_countdown,
4512                               "Previous CSA countdown didn't complete")) {
4513                         ret = -EBUSY;
4514                         goto out_unlock;
4515                 }
4516 
4517                 mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4518 
4519                 break;
4520         case NL80211_IFTYPE_STATION:
4521                 if (chsw->block_tx) {
4522                         /*
4523                          * In case of undetermined / long time with immediate
4524                          * quiet monitor status to gracefully disconnect
4525                          */
4526                         if (!chsw->count ||
4527                             chsw->count * vif->bss_conf.beacon_int >
4528                             IWL_MAX_CSA_BLOCK_TX)
4529                                 schedule_delayed_work(&mvmvif->csa_work,
4530                                                       msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4531                 }
4532 
4533                 if (!fw_has_capa(&mvm->fw->ucode_capa,
4534                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4535                         ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4536                         if (ret)
4537                                 goto out_unlock;
4538                 } else {
4539                         iwl_mvm_schedule_client_csa(mvm, vif, chsw);
4540                 }
4541 
4542                 mvmvif->csa_count = chsw->count;
4543                 mvmvif->csa_misbehave = false;
4544                 break;
4545         default:
4546                 break;
4547         }
4548 
4549         mvmvif->ps_disabled = true;
4550 
4551         ret = iwl_mvm_power_update_ps(mvm);
4552         if (ret)
4553                 goto out_unlock;
4554 
4555         /* we won't be on this channel any longer */
4556         iwl_mvm_teardown_tdls_peers(mvm);
4557 
4558 out_unlock:
4559         mutex_unlock(&mvm->mutex);
4560 
4561         return ret;
4562 }
4563 
4564 static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4565                                              struct ieee80211_vif *vif,
4566                                              struct ieee80211_channel_switch *chsw)
4567 {
4568         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4569         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4570         struct iwl_chan_switch_te_cmd cmd = {
4571                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4572                                                           mvmvif->color)),
4573                 .action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4574                 .tsf = cpu_to_le32(chsw->timestamp),
4575                 .cs_count = chsw->count,
4576                 .cs_mode = chsw->block_tx,
4577         };
4578 
4579         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4580                 return;
4581 
4582         if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
4583                 if (mvmvif->csa_misbehave) {
4584                         /* Second time, give up on this AP*/
4585                         iwl_mvm_abort_channel_switch(hw, vif);
4586                         ieee80211_chswitch_done(vif, false);
4587                         mvmvif->csa_misbehave = false;
4588                         return;
4589                 }
4590                 mvmvif->csa_misbehave = true;
4591         }
4592         mvmvif->csa_count = chsw->count;
4593 
4594         IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d\n", mvmvif->id);
4595 
4596         WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4597                                      WIDE_ID(MAC_CONF_GROUP,
4598                                              CHANNEL_SWITCH_TIME_EVENT_CMD),
4599                                      CMD_ASYNC, sizeof(cmd), &cmd));
4600 }
4601 
4602 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
4603 {
4604         int i;
4605 
4606         if (!iwl_mvm_has_new_tx_api(mvm)) {
4607                 if (drop) {
4608                         mutex_lock(&mvm->mutex);
4609                         iwl_mvm_flush_tx_path(mvm,
4610                                 iwl_mvm_flushable_queues(mvm) & queues, 0);
4611                         mutex_unlock(&mvm->mutex);
4612                 } else {
4613                         iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4614                 }
4615                 return;
4616         }
4617 
4618         mutex_lock(&mvm->mutex);
4619         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4620                 struct ieee80211_sta *sta;
4621 
4622                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4623                                                 lockdep_is_held(&mvm->mutex));
4624                 if (IS_ERR_OR_NULL(sta))
4625                         continue;
4626 
4627                 if (drop)
4628                         iwl_mvm_flush_sta_tids(mvm, i, 0xFF, 0);
4629                 else
4630                         iwl_mvm_wait_sta_queues_empty(mvm,
4631                                         iwl_mvm_sta_from_mac80211(sta));
4632         }
4633         mutex_unlock(&mvm->mutex);
4634 }
4635 
4636 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4637                               struct ieee80211_vif *vif, u32 queues, bool drop)
4638 {
4639         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4640         struct iwl_mvm_vif *mvmvif;
4641         struct iwl_mvm_sta *mvmsta;
4642         struct ieee80211_sta *sta;
4643         int i;
4644         u32 msk = 0;
4645 
4646         if (!vif) {
4647                 iwl_mvm_flush_no_vif(mvm, queues, drop);
4648                 return;
4649         }
4650 
4651         if (vif->type != NL80211_IFTYPE_STATION)
4652                 return;
4653 
4654         /* Make sure we're done with the deferred traffic before flushing */
4655         flush_work(&mvm->add_stream_wk);
4656 
4657         mutex_lock(&mvm->mutex);
4658         mvmvif = iwl_mvm_vif_from_mac80211(vif);
4659 
4660         /* flush the AP-station and all TDLS peers */
4661         for (i = 0; i < ARRAY_SIZE(mvm->fw_id_to_mac_id); i++) {
4662                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4663                                                 lockdep_is_held(&mvm->mutex));
4664                 if (IS_ERR_OR_NULL(sta))
4665                         continue;
4666 
4667                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
4668                 if (mvmsta->vif != vif)
4669                         continue;
4670 
4671                 /* make sure only TDLS peers or the AP are flushed */
4672                 WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4673 
4674                 if (drop) {
4675                         if (iwl_mvm_flush_sta(mvm, mvmsta, false, 0))
4676                                 IWL_ERR(mvm, "flush request fail\n");
4677                 } else {
4678                         msk |= mvmsta->tfd_queue_msk;
4679                         if (iwl_mvm_has_new_tx_api(mvm))
4680                                 iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4681                 }
4682         }
4683 
4684         mutex_unlock(&mvm->mutex);
4685 
4686         /* this can take a while, and we may need/want other operations
4687          * to succeed while doing this, so do it without the mutex held
4688          */
4689         if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4690                 iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4691 }
4692 
4693 static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4694                                   struct survey_info *survey)
4695 {
4696         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4697         int ret;
4698 
4699         memset(survey, 0, sizeof(*survey));
4700 
4701         /* only support global statistics right now */
4702         if (idx != 0)
4703                 return -ENOENT;
4704 
4705         if (!fw_has_capa(&mvm->fw->ucode_capa,
4706                          IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4707                 return -ENOENT;
4708 
4709         mutex_lock(&mvm->mutex);
4710 
4711         if (iwl_mvm_firmware_running(mvm)) {
4712                 ret = iwl_mvm_request_statistics(mvm, false);
4713                 if (ret)
4714                         goto out;
4715         }
4716 
4717         survey->filled = SURVEY_INFO_TIME |
4718                          SURVEY_INFO_TIME_RX |
4719                          SURVEY_INFO_TIME_TX |
4720                          SURVEY_INFO_TIME_SCAN;
4721         survey->time = mvm->accu_radio_stats.on_time_rf +
4722                        mvm->radio_stats.on_time_rf;
4723         do_div(survey->time, USEC_PER_MSEC);
4724 
4725         survey->time_rx = mvm->accu_radio_stats.rx_time +
4726                           mvm->radio_stats.rx_time;
4727         do_div(survey->time_rx, USEC_PER_MSEC);
4728 
4729         survey->time_tx = mvm->accu_radio_stats.tx_time +
4730                           mvm->radio_stats.tx_time;
4731         do_div(survey->time_tx, USEC_PER_MSEC);
4732 
4733         survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4734                             mvm->radio_stats.on_time_scan;
4735         do_div(survey->time_scan, USEC_PER_MSEC);
4736 
4737         ret = 0;
4738  out:
4739         mutex_unlock(&mvm->mutex);
4740         return ret;
4741 }
4742 
4743 static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
4744                                        struct ieee80211_vif *vif,
4745                                        struct ieee80211_sta *sta,
4746                                        struct station_info *sinfo)
4747 {
4748         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4749         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4750         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4751 
4752         if (mvmsta->avg_energy) {
4753                 sinfo->signal_avg = mvmsta->avg_energy;
4754                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
4755         }
4756 
4757         /* if beacon filtering isn't on mac80211 does it anyway */
4758         if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
4759                 return;
4760 
4761         if (!vif->bss_conf.assoc)
4762                 return;
4763 
4764         mutex_lock(&mvm->mutex);
4765 
4766         if (mvmvif->ap_sta_id != mvmsta->sta_id)
4767                 goto unlock;
4768 
4769         if (iwl_mvm_request_statistics(mvm, false))
4770                 goto unlock;
4771 
4772         sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
4773                            mvmvif->beacon_stats.accu_num_beacons;
4774         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
4775         if (mvmvif->beacon_stats.avg_signal) {
4776                 /* firmware only reports a value after RXing a few beacons */
4777                 sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
4778                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
4779         }
4780  unlock:
4781         mutex_unlock(&mvm->mutex);
4782 }
4783 
4784 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
4785                                         struct ieee80211_vif *vif,
4786                                         const struct ieee80211_event *event)
4787 {
4788 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)                               \
4789         do {                                                            \
4790                 if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))           \
4791                         break;                                          \
4792                 iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);      \
4793         } while (0)
4794 
4795         struct iwl_fw_dbg_trigger_tlv *trig;
4796         struct iwl_fw_dbg_trigger_mlme *trig_mlme;
4797 
4798         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
4799                                      FW_DBG_TRIGGER_MLME);
4800         if (!trig)
4801                 return;
4802 
4803         trig_mlme = (void *)trig->data;
4804 
4805         if (event->u.mlme.data == ASSOC_EVENT) {
4806                 if (event->u.mlme.status == MLME_DENIED)
4807                         CHECK_MLME_TRIGGER(stop_assoc_denied,
4808                                            "DENIED ASSOC: reason %d",
4809                                             event->u.mlme.reason);
4810                 else if (event->u.mlme.status == MLME_TIMEOUT)
4811                         CHECK_MLME_TRIGGER(stop_assoc_timeout,
4812                                            "ASSOC TIMEOUT");
4813         } else if (event->u.mlme.data == AUTH_EVENT) {
4814                 if (event->u.mlme.status == MLME_DENIED)
4815                         CHECK_MLME_TRIGGER(stop_auth_denied,
4816                                            "DENIED AUTH: reason %d",
4817                                            event->u.mlme.reason);
4818                 else if (event->u.mlme.status == MLME_TIMEOUT)
4819                         CHECK_MLME_TRIGGER(stop_auth_timeout,
4820                                            "AUTH TIMEOUT");
4821         } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
4822                 CHECK_MLME_TRIGGER(stop_rx_deauth,
4823                                    "DEAUTH RX %d", event->u.mlme.reason);
4824         } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
4825                 CHECK_MLME_TRIGGER(stop_tx_deauth,
4826                                    "DEAUTH TX %d", event->u.mlme.reason);
4827         }
4828 #undef CHECK_MLME_TRIGGER
4829 }
4830 
4831 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
4832                                           struct ieee80211_vif *vif,
4833                                           const struct ieee80211_event *event)
4834 {
4835         struct iwl_fw_dbg_trigger_tlv *trig;
4836         struct iwl_fw_dbg_trigger_ba *ba_trig;
4837 
4838         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
4839                                      FW_DBG_TRIGGER_BA);
4840         if (!trig)
4841                 return;
4842 
4843         ba_trig = (void *)trig->data;
4844 
4845         if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
4846                 return;
4847 
4848         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
4849                                 "BAR received from %pM, tid %d, ssn %d",
4850                                 event->u.ba.sta->addr, event->u.ba.tid,
4851                                 event->u.ba.ssn);
4852 }
4853 
4854 static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
4855                                        struct ieee80211_vif *vif,
4856                                        const struct ieee80211_event *event)
4857 {
4858         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4859 
4860         switch (event->type) {
4861         case MLME_EVENT:
4862                 iwl_mvm_event_mlme_callback(mvm, vif, event);
4863                 break;
4864         case BAR_RX_EVENT:
4865                 iwl_mvm_event_bar_rx_callback(mvm, vif, event);
4866                 break;
4867         case BA_FRAME_TIMEOUT:
4868                 iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
4869                                                      event->u.ba.tid);
4870                 break;
4871         default:
4872                 break;
4873         }
4874 }
4875 
4876 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
4877                                      struct iwl_mvm_internal_rxq_notif *notif,
4878                                      u32 size)
4879 {
4880         u32 qmask = BIT(mvm->trans->num_rx_queues) - 1;
4881         int ret;
4882 
4883 
4884         if (!iwl_mvm_has_new_rx_api(mvm))
4885                 return;
4886 
4887         if (notif->sync) {
4888                 notif->cookie = mvm->queue_sync_cookie;
4889                 atomic_set(&mvm->queue_sync_counter,
4890                            mvm->trans->num_rx_queues);
4891         }
4892 
4893         ret = iwl_mvm_notify_rx_queue(mvm, qmask, (u8 *)notif,
4894                                       size, !notif->sync);
4895         if (ret) {
4896                 IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
4897                 goto out;
4898         }
4899 
4900         if (notif->sync) {
4901                 lockdep_assert_held(&mvm->mutex);
4902                 ret = wait_event_timeout(mvm->rx_sync_waitq,
4903                                          atomic_read(&mvm->queue_sync_counter) == 0 ||
4904                                          iwl_mvm_is_radio_killed(mvm),
4905                                          HZ);
4906                 WARN_ON_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm));
4907         }
4908 
4909 out:
4910         atomic_set(&mvm->queue_sync_counter, 0);
4911         if (notif->sync)
4912                 mvm->queue_sync_cookie++;
4913 }
4914 
4915 static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
4916 {
4917         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4918         struct iwl_mvm_internal_rxq_notif data = {
4919                 .type = IWL_MVM_RXQ_EMPTY,
4920                 .sync = 1,
4921         };
4922 
4923         mutex_lock(&mvm->mutex);
4924         iwl_mvm_sync_rx_queues_internal(mvm, &data, sizeof(data));
4925         mutex_unlock(&mvm->mutex);
4926 }
4927 
4928 static int
4929 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
4930                                     struct ieee80211_vif *vif,
4931                                     struct cfg80211_ftm_responder_stats *stats)
4932 {
4933         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4934         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4935 
4936         if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
4937             !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
4938                 return -EINVAL;
4939 
4940         mutex_lock(&mvm->mutex);
4941         *stats = mvm->ftm_resp_stats;
4942         mutex_unlock(&mvm->mutex);
4943 
4944         stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
4945                         BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
4946                         BIT(NL80211_FTM_STATS_FAILED_NUM) |
4947                         BIT(NL80211_FTM_STATS_ASAP_NUM) |
4948                         BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
4949                         BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
4950                         BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
4951                         BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
4952                         BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
4953 
4954         return 0;
4955 }
4956 
4957 static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
4958                               struct ieee80211_vif *vif,
4959                               struct cfg80211_pmsr_request *request)
4960 {
4961         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4962         int ret;
4963 
4964         mutex_lock(&mvm->mutex);
4965         ret = iwl_mvm_ftm_start(mvm, vif, request);
4966         mutex_unlock(&mvm->mutex);
4967 
4968         return ret;
4969 }
4970 
4971 static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
4972                                struct ieee80211_vif *vif,
4973                                struct cfg80211_pmsr_request *request)
4974 {
4975         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4976 
4977         mutex_lock(&mvm->mutex);
4978         iwl_mvm_ftm_abort(mvm, request);
4979         mutex_unlock(&mvm->mutex);
4980 }
4981 
4982 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
4983 {
4984         u8 protocol = ip_hdr(skb)->protocol;
4985 
4986         if (!IS_ENABLED(CONFIG_INET))
4987                 return false;
4988 
4989         return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
4990 }
4991 
4992 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
4993                                       struct sk_buff *head,
4994                                       struct sk_buff *skb)
4995 {
4996         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4997 
4998         /* For now don't aggregate IPv6 in AMSDU */
4999         if (skb->protocol != htons(ETH_P_IP))
5000                 return false;
5001 
5002         if (!iwl_mvm_is_csum_supported(mvm))
5003                 return true;
5004 
5005         return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5006 }
5007 
5008 const struct ieee80211_ops iwl_mvm_hw_ops = {
5009         .tx = iwl_mvm_mac_tx,
5010         .wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5011         .ampdu_action = iwl_mvm_mac_ampdu_action,
5012         .start = iwl_mvm_mac_start,
5013         .reconfig_complete = iwl_mvm_mac_reconfig_complete,
5014         .stop = iwl_mvm_mac_stop,
5015         .add_interface = iwl_mvm_mac_add_interface,
5016         .remove_interface = iwl_mvm_mac_remove_interface,
5017         .config = iwl_mvm_mac_config,
5018         .prepare_multicast = iwl_mvm_prepare_multicast,
5019         .configure_filter = iwl_mvm_configure_filter,
5020         .config_iface_filter = iwl_mvm_config_iface_filter,
5021         .bss_info_changed = iwl_mvm_bss_info_changed,
5022         .hw_scan = iwl_mvm_mac_hw_scan,
5023         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5024         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5025         .sta_state = iwl_mvm_mac_sta_state,
5026         .sta_notify = iwl_mvm_mac_sta_notify,
5027         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5028         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5029         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5030         .sta_rc_update = iwl_mvm_sta_rc_update,
5031         .conf_tx = iwl_mvm_mac_conf_tx,
5032         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5033         .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5034         .flush = iwl_mvm_mac_flush,
5035         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
5036         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5037         .set_key = iwl_mvm_mac_set_key,
5038         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
5039         .remain_on_channel = iwl_mvm_roc,
5040         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
5041         .add_chanctx = iwl_mvm_add_chanctx,
5042         .remove_chanctx = iwl_mvm_remove_chanctx,
5043         .change_chanctx = iwl_mvm_change_chanctx,
5044         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5045         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5046         .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5047 
5048         .start_ap = iwl_mvm_start_ap_ibss,
5049         .stop_ap = iwl_mvm_stop_ap_ibss,
5050         .join_ibss = iwl_mvm_start_ap_ibss,
5051         .leave_ibss = iwl_mvm_stop_ap_ibss,
5052 
5053         .tx_last_beacon = iwl_mvm_tx_last_beacon,
5054 
5055         .set_tim = iwl_mvm_set_tim,
5056 
5057         .channel_switch = iwl_mvm_channel_switch,
5058         .pre_channel_switch = iwl_mvm_pre_channel_switch,
5059         .post_channel_switch = iwl_mvm_post_channel_switch,
5060         .abort_channel_switch = iwl_mvm_abort_channel_switch,
5061         .channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5062 
5063         .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5064         .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5065         .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5066 
5067         .event_callback = iwl_mvm_mac_event_callback,
5068 
5069         .sync_rx_queues = iwl_mvm_sync_rx_queues,
5070 
5071         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5072 
5073 #ifdef CONFIG_PM_SLEEP
5074         /* look at d3.c */
5075         .suspend = iwl_mvm_suspend,
5076         .resume = iwl_mvm_resume,
5077         .set_wakeup = iwl_mvm_set_wakeup,
5078         .set_rekey_data = iwl_mvm_set_rekey_data,
5079 #if IS_ENABLED(CONFIG_IPV6)
5080         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5081 #endif
5082         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5083 #endif
5084         .get_survey = iwl_mvm_mac_get_survey,
5085         .sta_statistics = iwl_mvm_mac_sta_statistics,
5086         .get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5087         .start_pmsr = iwl_mvm_start_pmsr,
5088         .abort_pmsr = iwl_mvm_abort_pmsr,
5089 
5090         .can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5091 #ifdef CONFIG_IWLWIFI_DEBUGFS
5092         .sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5093 #endif
5094 };

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