]> Git Repo - J-linux.git/blob - drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c
Merge tag 'vfs-6.13-rc7.fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs
[J-linux.git] / drivers / net / wireless / intel / iwlwifi / mvm / mac80211.c
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /*
3  * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4  * Copyright (C) 2013-2015 Intel Mobile Communications GmbH
5  * Copyright (C) 2016-2017 Intel Deutschland GmbH
6  */
7 #include <linux/kernel.h>
8 #include <linux/slab.h>
9 #include <linux/skbuff.h>
10 #include <linux/netdevice.h>
11 #include <linux/etherdevice.h>
12 #include <linux/ip.h>
13 #include <linux/if_arp.h>
14 #include <linux/time.h>
15 #include <net/mac80211.h>
16 #include <net/ieee80211_radiotap.h>
17 #include <net/tcp.h>
18
19 #include "iwl-drv.h"
20 #include "iwl-op-mode.h"
21 #include "iwl-io.h"
22 #include "mvm.h"
23 #include "sta.h"
24 #include "time-event.h"
25 #include "iwl-nvm-utils.h"
26 #include "iwl-phy-db.h"
27 #include "testmode.h"
28 #include "fw/error-dump.h"
29 #include "iwl-prph.h"
30 #include "iwl-nvm-parse.h"
31 #include "time-sync.h"
32
33 #define IWL_MVM_LIMITS(ap)                                      \
34         {                                                       \
35                 .max = 1,                                       \
36                 .types = BIT(NL80211_IFTYPE_STATION),           \
37         },                                                      \
38         {                                                       \
39                 .max = 1,                                       \
40                 .types = ap |                                   \
41                          BIT(NL80211_IFTYPE_P2P_CLIENT) |       \
42                          BIT(NL80211_IFTYPE_P2P_GO),            \
43         },                                                      \
44         {                                                       \
45                 .max = 1,                                       \
46                 .types = BIT(NL80211_IFTYPE_P2P_DEVICE),        \
47         }
48
49 static const struct ieee80211_iface_limit iwl_mvm_limits[] = {
50         IWL_MVM_LIMITS(0)
51 };
52
53 static const struct ieee80211_iface_limit iwl_mvm_limits_ap[] = {
54         IWL_MVM_LIMITS(BIT(NL80211_IFTYPE_AP))
55 };
56
57 static const struct ieee80211_iface_combination iwl_mvm_iface_combinations[] = {
58         {
59                 .num_different_channels = 2,
60                 .max_interfaces = 3,
61                 .limits = iwl_mvm_limits,
62                 .n_limits = ARRAY_SIZE(iwl_mvm_limits),
63         },
64         {
65                 .num_different_channels = 1,
66                 .max_interfaces = 3,
67                 .limits = iwl_mvm_limits_ap,
68                 .n_limits = ARRAY_SIZE(iwl_mvm_limits_ap),
69         },
70 };
71
72 static const struct cfg80211_pmsr_capabilities iwl_mvm_pmsr_capa = {
73         .max_peers = IWL_MVM_TOF_MAX_APS,
74         .report_ap_tsf = 1,
75         .randomize_mac_addr = 1,
76
77         .ftm = {
78                 .supported = 1,
79                 .asap = 1,
80                 .non_asap = 1,
81                 .request_lci = 1,
82                 .request_civicloc = 1,
83                 .trigger_based = 1,
84                 .non_trigger_based = 1,
85                 .max_bursts_exponent = -1, /* all supported */
86                 .max_ftms_per_burst = 0, /* no limits */
87                 .bandwidths = BIT(NL80211_CHAN_WIDTH_20_NOHT) |
88                               BIT(NL80211_CHAN_WIDTH_20) |
89                               BIT(NL80211_CHAN_WIDTH_40) |
90                               BIT(NL80211_CHAN_WIDTH_80) |
91                               BIT(NL80211_CHAN_WIDTH_160),
92                 .preambles = BIT(NL80211_PREAMBLE_LEGACY) |
93                              BIT(NL80211_PREAMBLE_HT) |
94                              BIT(NL80211_PREAMBLE_VHT) |
95                              BIT(NL80211_PREAMBLE_HE),
96         },
97 };
98
99 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
100                                  enum set_key_cmd cmd,
101                                  struct ieee80211_vif *vif,
102                                  struct ieee80211_sta *sta,
103                                  struct ieee80211_key_conf *key);
104
105 static void iwl_mvm_reset_phy_ctxts(struct iwl_mvm *mvm)
106 {
107         int i;
108
109         memset(mvm->phy_ctxts, 0, sizeof(mvm->phy_ctxts));
110         for (i = 0; i < NUM_PHY_CTX; i++) {
111                 mvm->phy_ctxts[i].id = i;
112                 mvm->phy_ctxts[i].ref = 0;
113         }
114 }
115
116 struct ieee80211_regdomain *iwl_mvm_get_regdomain(struct wiphy *wiphy,
117                                                   const char *alpha2,
118                                                   enum iwl_mcc_source src_id,
119                                                   bool *changed)
120 {
121         struct ieee80211_regdomain *regd = NULL;
122         struct ieee80211_hw *hw = wiphy_to_ieee80211_hw(wiphy);
123         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
124         struct iwl_mcc_update_resp_v8 *resp;
125         u8 resp_ver;
126
127         IWL_DEBUG_LAR(mvm, "Getting regdomain data for %s from FW\n", alpha2);
128
129         lockdep_assert_held(&mvm->mutex);
130
131         resp = iwl_mvm_update_mcc(mvm, alpha2, src_id);
132         if (IS_ERR_OR_NULL(resp)) {
133                 IWL_DEBUG_LAR(mvm, "Could not get update from FW %d\n",
134                               PTR_ERR_OR_ZERO(resp));
135                 resp = NULL;
136                 goto out;
137         }
138
139         if (changed) {
140                 u32 status = le32_to_cpu(resp->status);
141
142                 *changed = (status == MCC_RESP_NEW_CHAN_PROFILE ||
143                             status == MCC_RESP_ILLEGAL);
144         }
145         resp_ver = iwl_fw_lookup_notif_ver(mvm->fw, IWL_ALWAYS_LONG_GROUP,
146                                            MCC_UPDATE_CMD, 0);
147         IWL_DEBUG_LAR(mvm, "MCC update response version: %d\n", resp_ver);
148
149         regd = iwl_parse_nvm_mcc_info(mvm->trans->dev, mvm->cfg,
150                                       __le32_to_cpu(resp->n_channels),
151                                       resp->channels,
152                                       __le16_to_cpu(resp->mcc),
153                                       __le16_to_cpu(resp->geo_info),
154                                       le32_to_cpu(resp->cap), resp_ver);
155         /* Store the return source id */
156         src_id = resp->source_id;
157         if (IS_ERR_OR_NULL(regd)) {
158                 IWL_DEBUG_LAR(mvm, "Could not get parse update from FW %d\n",
159                               PTR_ERR_OR_ZERO(regd));
160                 goto out;
161         }
162
163         IWL_DEBUG_LAR(mvm, "setting alpha2 from FW to %s (0x%x, 0x%x) src=%d\n",
164                       regd->alpha2, regd->alpha2[0], regd->alpha2[1], src_id);
165         mvm->lar_regdom_set = true;
166         mvm->mcc_src = src_id;
167
168         if (!iwl_puncturing_is_allowed_in_bios(mvm->bios_enable_puncturing,
169                                                le16_to_cpu(resp->mcc)))
170                 ieee80211_hw_set(mvm->hw, DISALLOW_PUNCTURING);
171         else
172                 __clear_bit(IEEE80211_HW_DISALLOW_PUNCTURING, mvm->hw->flags);
173
174         iwl_mei_set_country_code(__le16_to_cpu(resp->mcc));
175
176 out:
177         kfree(resp);
178         return regd;
179 }
180
181 void iwl_mvm_update_changed_regdom(struct iwl_mvm *mvm)
182 {
183         bool changed;
184         struct ieee80211_regdomain *regd;
185
186         if (!iwl_mvm_is_lar_supported(mvm))
187                 return;
188
189         regd = iwl_mvm_get_current_regdomain(mvm, &changed);
190         if (!IS_ERR_OR_NULL(regd)) {
191                 /* only update the regulatory core if changed */
192                 if (changed)
193                         regulatory_set_wiphy_regd(mvm->hw->wiphy, regd);
194
195                 kfree(regd);
196         }
197 }
198
199 struct ieee80211_regdomain *iwl_mvm_get_current_regdomain(struct iwl_mvm *mvm,
200                                                           bool *changed)
201 {
202         return iwl_mvm_get_regdomain(mvm->hw->wiphy, "ZZ",
203                                      iwl_mvm_is_wifi_mcc_supported(mvm) ?
204                                      MCC_SOURCE_GET_CURRENT :
205                                      MCC_SOURCE_OLD_FW, changed);
206 }
207
208 int iwl_mvm_init_fw_regd(struct iwl_mvm *mvm, bool force_regd_sync)
209 {
210         enum iwl_mcc_source used_src;
211         struct ieee80211_regdomain *regd;
212         int ret;
213         bool changed;
214         const struct ieee80211_regdomain *r =
215                         wiphy_dereference(mvm->hw->wiphy, mvm->hw->wiphy->regd);
216
217         if (!r)
218                 return -ENOENT;
219
220         /* save the last source in case we overwrite it below */
221         used_src = mvm->mcc_src;
222         if (iwl_mvm_is_wifi_mcc_supported(mvm)) {
223                 /* Notify the firmware we support wifi location updates */
224                 regd = iwl_mvm_get_current_regdomain(mvm, NULL);
225                 if (!IS_ERR_OR_NULL(regd))
226                         kfree(regd);
227         }
228
229         /* Now set our last stored MCC and source */
230         regd = iwl_mvm_get_regdomain(mvm->hw->wiphy, r->alpha2, used_src,
231                                      &changed);
232         if (IS_ERR_OR_NULL(regd))
233                 return -EIO;
234
235         /* update cfg80211 if the regdomain was changed or the caller explicitly
236          * asked to update regdomain
237          */
238         if (changed || force_regd_sync)
239                 ret = regulatory_set_wiphy_regd_sync(mvm->hw->wiphy, regd);
240         else
241                 ret = 0;
242
243         kfree(regd);
244         return ret;
245 }
246
247 /* Each capability added here should also be add to tm_if_types_ext_capa_sta */
248 static const u8 he_if_types_ext_capa_sta[] = {
249          [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
250          [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT,
251          [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
252                WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
253          [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
254 };
255
256 static const u8 tm_if_types_ext_capa_sta[] = {
257          [0] = WLAN_EXT_CAPA1_EXT_CHANNEL_SWITCHING,
258          [2] = WLAN_EXT_CAPA3_MULTI_BSSID_SUPPORT |
259                WLAN_EXT_CAPA3_TIMING_MEASUREMENT_SUPPORT,
260          [7] = WLAN_EXT_CAPA8_OPMODE_NOTIF |
261                WLAN_EXT_CAPA8_MAX_MSDU_IN_AMSDU_LSB,
262          [8] = WLAN_EXT_CAPA9_MAX_MSDU_IN_AMSDU_MSB,
263          [9] = WLAN_EXT_CAPA10_TWT_REQUESTER_SUPPORT,
264 };
265
266 /* Additional interface types for which extended capabilities are
267  * specified separately
268  */
269
270 #define IWL_MVM_EMLSR_CAPA      (IEEE80211_EML_CAP_EMLSR_SUPP | \
271                                  IEEE80211_EML_CAP_EMLSR_PADDING_DELAY_32US << \
272                                         __bf_shf(IEEE80211_EML_CAP_EMLSR_PADDING_DELAY) | \
273                                  IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY_64US << \
274                                         __bf_shf(IEEE80211_EML_CAP_EMLSR_TRANSITION_DELAY))
275 #define IWL_MVM_MLD_CAPA_OPS FIELD_PREP_CONST( \
276                         IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP, \
277                         IEEE80211_MLD_CAP_OP_TID_TO_LINK_MAP_NEG_SUPP_SAME)
278
279 static const struct wiphy_iftype_ext_capab add_iftypes_ext_capa[] = {
280         {
281                 .iftype = NL80211_IFTYPE_STATION,
282                 .extended_capabilities = he_if_types_ext_capa_sta,
283                 .extended_capabilities_mask = he_if_types_ext_capa_sta,
284                 .extended_capabilities_len = sizeof(he_if_types_ext_capa_sta),
285                 /* relevant only if EHT is supported */
286                 .eml_capabilities = IWL_MVM_EMLSR_CAPA,
287                 .mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
288         },
289         {
290                 .iftype = NL80211_IFTYPE_STATION,
291                 .extended_capabilities = tm_if_types_ext_capa_sta,
292                 .extended_capabilities_mask = tm_if_types_ext_capa_sta,
293                 .extended_capabilities_len = sizeof(tm_if_types_ext_capa_sta),
294                 /* relevant only if EHT is supported */
295                 .eml_capabilities = IWL_MVM_EMLSR_CAPA,
296                 .mld_capa_and_ops = IWL_MVM_MLD_CAPA_OPS,
297         },
298 };
299
300 int iwl_mvm_op_get_antenna(struct ieee80211_hw *hw, u32 *tx_ant, u32 *rx_ant)
301 {
302         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
303         *tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
304         *rx_ant = iwl_mvm_get_valid_rx_ant(mvm);
305         return 0;
306 }
307
308 int iwl_mvm_op_set_antenna(struct ieee80211_hw *hw, u32 tx_ant, u32 rx_ant)
309 {
310         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
311
312         /* This has been tested on those devices only */
313         if (mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_9000 &&
314             mvm->trans->trans_cfg->device_family != IWL_DEVICE_FAMILY_22000)
315                 return -EOPNOTSUPP;
316
317         if (!mvm->nvm_data)
318                 return -EBUSY;
319
320         /* mac80211 ensures the device is not started,
321          * so the firmware cannot be running
322          */
323
324         mvm->set_tx_ant = tx_ant;
325         mvm->set_rx_ant = rx_ant;
326
327         iwl_reinit_cab(mvm->trans, mvm->nvm_data, tx_ant, rx_ant, mvm->fw);
328
329         return 0;
330 }
331
332 int iwl_mvm_mac_setup_register(struct iwl_mvm *mvm)
333 {
334         struct ieee80211_hw *hw = mvm->hw;
335         int num_mac, ret, i;
336         static const u32 mvm_ciphers[] = {
337                 WLAN_CIPHER_SUITE_WEP40,
338                 WLAN_CIPHER_SUITE_WEP104,
339                 WLAN_CIPHER_SUITE_TKIP,
340                 WLAN_CIPHER_SUITE_CCMP,
341         };
342 #ifdef CONFIG_PM_SLEEP
343         bool unified = fw_has_capa(&mvm->fw->ucode_capa,
344                                    IWL_UCODE_TLV_CAPA_CNSLDTD_D3_D0_IMG);
345 #endif
346         u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
347         u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
348
349         /* Tell mac80211 our characteristics */
350         ieee80211_hw_set(hw, SIGNAL_DBM);
351         ieee80211_hw_set(hw, SPECTRUM_MGMT);
352         ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
353         ieee80211_hw_set(hw, WANT_MONITOR_VIF);
354         ieee80211_hw_set(hw, SUPPORTS_PS);
355         ieee80211_hw_set(hw, SUPPORTS_DYNAMIC_PS);
356         ieee80211_hw_set(hw, AMPDU_AGGREGATION);
357         ieee80211_hw_set(hw, CONNECTION_MONITOR);
358         ieee80211_hw_set(hw, CHANCTX_STA_CSA);
359         ieee80211_hw_set(hw, SUPPORT_FAST_XMIT);
360         ieee80211_hw_set(hw, SUPPORTS_CLONED_SKBS);
361         ieee80211_hw_set(hw, SUPPORTS_AMSDU_IN_AMPDU);
362         ieee80211_hw_set(hw, NEEDS_UNIQUE_STA_ADDR);
363         ieee80211_hw_set(hw, SUPPORTS_VHT_EXT_NSS_BW);
364         ieee80211_hw_set(hw, BUFF_MMPDU_TXQ);
365         ieee80211_hw_set(hw, STA_MMPDU_TXQ);
366
367         /* Set this early since we need to have it for the check below */
368         if (mvm->mld_api_is_used && mvm->nvm_data->sku_cap_11be_enable &&
369             !iwlwifi_mod_params.disable_11ax &&
370             !iwlwifi_mod_params.disable_11be) {
371                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_MLO;
372                 /* we handle this already earlier, but need it for MLO */
373                 ieee80211_hw_set(hw, HANDLES_QUIET_CSA);
374         }
375
376         /* With MLD FW API, it tracks timing by itself,
377          * no need for any timing from the host
378          */
379         if (!mvm->mld_api_is_used)
380                 ieee80211_hw_set(hw, TIMING_BEACON_ONLY);
381
382         /*
383          * On older devices, enabling TX A-MSDU occasionally leads to
384          * something getting messed up, the command read from the FIFO
385          * gets out of sync and isn't a TX command, so that we have an
386          * assert EDC.
387          *
388          * It's not clear where the bug is, but since we didn't used to
389          * support A-MSDU until moving the mac80211 iTXQs, just leave it
390          * for older devices. We also don't see this issue on any newer
391          * devices.
392          */
393         if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_9000)
394                 ieee80211_hw_set(hw, TX_AMSDU);
395         ieee80211_hw_set(hw, TX_FRAG_LIST);
396
397         if (iwl_mvm_has_tlc_offload(mvm)) {
398                 ieee80211_hw_set(hw, TX_AMPDU_SETUP_IN_HW);
399                 ieee80211_hw_set(hw, HAS_RATE_CONTROL);
400         }
401
402         /* We want to use the mac80211's reorder buffer for 9000 */
403         if (iwl_mvm_has_new_rx_api(mvm) &&
404             mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_9000)
405                 ieee80211_hw_set(hw, SUPPORTS_REORDERING_BUFFER);
406
407         if (fw_has_capa(&mvm->fw->ucode_capa,
408                         IWL_UCODE_TLV_CAPA_STA_PM_NOTIF)) {
409                 ieee80211_hw_set(hw, AP_LINK_PS);
410         } else if (WARN_ON(iwl_mvm_has_new_tx_api(mvm))) {
411                 /*
412                  * we absolutely need this for the new TX API since that comes
413                  * with many more queues than the current code can deal with
414                  * for station powersave
415                  */
416                 return -EINVAL;
417         }
418
419         if (mvm->trans->num_rx_queues > 1)
420                 ieee80211_hw_set(hw, USES_RSS);
421
422         if (mvm->trans->max_skb_frags)
423                 hw->netdev_features = NETIF_F_HIGHDMA | NETIF_F_SG;
424
425         hw->queues = IEEE80211_NUM_ACS;
426         hw->offchannel_tx_hw_queue = IWL_MVM_OFFCHANNEL_QUEUE;
427         hw->radiotap_mcs_details |= IEEE80211_RADIOTAP_MCS_HAVE_FEC |
428                                     IEEE80211_RADIOTAP_MCS_HAVE_STBC;
429         hw->radiotap_vht_details |= IEEE80211_RADIOTAP_VHT_KNOWN_STBC |
430                 IEEE80211_RADIOTAP_VHT_KNOWN_BEAMFORMED;
431
432         hw->radiotap_timestamp.units_pos =
433                 IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
434                 IEEE80211_RADIOTAP_TIMESTAMP_SPOS_PLCP_SIG_ACQ;
435         /* this is the case for CCK frames, it's better (only 8) for OFDM */
436         hw->radiotap_timestamp.accuracy = 22;
437
438         if (!iwl_mvm_has_tlc_offload(mvm))
439                 hw->rate_control_algorithm = RS_NAME;
440
441         hw->uapsd_queues = IWL_MVM_UAPSD_QUEUES;
442         hw->uapsd_max_sp_len = IWL_UAPSD_MAX_SP;
443         hw->max_tx_fragments = mvm->trans->max_skb_frags;
444
445         BUILD_BUG_ON(ARRAY_SIZE(mvm->ciphers) < ARRAY_SIZE(mvm_ciphers) + 6);
446         memcpy(mvm->ciphers, mvm_ciphers, sizeof(mvm_ciphers));
447         hw->wiphy->n_cipher_suites = ARRAY_SIZE(mvm_ciphers);
448         hw->wiphy->cipher_suites = mvm->ciphers;
449
450         if (iwl_mvm_has_new_rx_api(mvm)) {
451                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
452                         WLAN_CIPHER_SUITE_GCMP;
453                 hw->wiphy->n_cipher_suites++;
454                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
455                         WLAN_CIPHER_SUITE_GCMP_256;
456                 hw->wiphy->n_cipher_suites++;
457         }
458
459         if (iwlwifi_mod_params.swcrypto)
460                 IWL_ERR(mvm,
461                         "iwlmvm doesn't allow to disable HW crypto, check swcrypto module parameter\n");
462         if (!iwlwifi_mod_params.bt_coex_active)
463                 IWL_ERR(mvm,
464                         "iwlmvm doesn't allow to disable BT Coex, check bt_coex_active module parameter\n");
465
466         ieee80211_hw_set(hw, MFP_CAPABLE);
467         mvm->ciphers[hw->wiphy->n_cipher_suites] = WLAN_CIPHER_SUITE_AES_CMAC;
468         hw->wiphy->n_cipher_suites++;
469         if (iwl_mvm_has_new_rx_api(mvm)) {
470                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
471                         WLAN_CIPHER_SUITE_BIP_GMAC_128;
472                 hw->wiphy->n_cipher_suites++;
473                 mvm->ciphers[hw->wiphy->n_cipher_suites] =
474                         WLAN_CIPHER_SUITE_BIP_GMAC_256;
475                 hw->wiphy->n_cipher_suites++;
476         }
477
478         wiphy_ext_feature_set(hw->wiphy,
479                               NL80211_EXT_FEATURE_BEACON_RATE_LEGACY);
480         wiphy_ext_feature_set(hw->wiphy,
481                               NL80211_EXT_FEATURE_SCAN_MIN_PREQ_CONTENT);
482
483         if (fw_has_capa(&mvm->fw->ucode_capa,
484                         IWL_UCODE_TLV_CAPA_FTM_CALIBRATED)) {
485                 wiphy_ext_feature_set(hw->wiphy,
486                                       NL80211_EXT_FEATURE_ENABLE_FTM_RESPONDER);
487                 hw->wiphy->pmsr_capa = &iwl_mvm_pmsr_capa;
488         }
489
490         if (sec_key_ver &&
491             fw_has_capa(&mvm->fw->ucode_capa,
492                         IWL_UCODE_TLV_CAPA_BIGTK_TX_SUPPORT))
493                 wiphy_ext_feature_set(hw->wiphy,
494                                       NL80211_EXT_FEATURE_BEACON_PROTECTION);
495         else if (fw_has_capa(&mvm->fw->ucode_capa,
496                              IWL_UCODE_TLV_CAPA_BIGTK_SUPPORT))
497                 wiphy_ext_feature_set(hw->wiphy,
498                                       NL80211_EXT_FEATURE_BEACON_PROTECTION_CLIENT);
499
500         if (fw_has_capa(&mvm->fw->ucode_capa,
501                         IWL_UCODE_TLV_CAPA_TIME_SYNC_BOTH_FTM_TM))
502                 hw->wiphy->hw_timestamp_max_peers = 1;
503
504         if (fw_has_capa(&mvm->fw->ucode_capa,
505                         IWL_UCODE_TLV_CAPA_SPP_AMSDU_SUPPORT))
506                 wiphy_ext_feature_set(hw->wiphy,
507                                       NL80211_EXT_FEATURE_SPP_AMSDU_SUPPORT);
508
509         ieee80211_hw_set(hw, SINGLE_SCAN_ON_ALL_BANDS);
510         hw->wiphy->features |=
511                 NL80211_FEATURE_SCHED_SCAN_RANDOM_MAC_ADDR |
512                 NL80211_FEATURE_SCAN_RANDOM_MAC_ADDR |
513                 NL80211_FEATURE_ND_RANDOM_MAC_ADDR;
514
515         hw->sta_data_size = sizeof(struct iwl_mvm_sta);
516         hw->vif_data_size = sizeof(struct iwl_mvm_vif);
517         hw->chanctx_data_size = sizeof(u16);
518         hw->txq_data_size = sizeof(struct iwl_mvm_txq);
519
520         hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
521                 BIT(NL80211_IFTYPE_P2P_CLIENT) |
522                 BIT(NL80211_IFTYPE_AP) |
523                 BIT(NL80211_IFTYPE_P2P_GO) |
524                 BIT(NL80211_IFTYPE_P2P_DEVICE) |
525                 BIT(NL80211_IFTYPE_ADHOC);
526
527         hw->wiphy->flags |= WIPHY_FLAG_IBSS_RSN;
528         wiphy_ext_feature_set(hw->wiphy, NL80211_EXT_FEATURE_VHT_IBSS);
529
530         /* The new Tx API does not allow to pass the key or keyid of a MPDU to
531          * the hw, preventing us to control which key(id) to use per MPDU.
532          * Till that's fixed we can't use Extended Key ID for the newer cards.
533          */
534         if (!iwl_mvm_has_new_tx_api(mvm))
535                 wiphy_ext_feature_set(hw->wiphy,
536                                       NL80211_EXT_FEATURE_EXT_KEY_ID);
537         hw->wiphy->features |= NL80211_FEATURE_HT_IBSS;
538
539         hw->wiphy->regulatory_flags |= REGULATORY_ENABLE_RELAX_NO_IR;
540         if (iwl_mvm_is_lar_supported(mvm))
541                 hw->wiphy->regulatory_flags |= REGULATORY_WIPHY_SELF_MANAGED;
542         else
543                 hw->wiphy->regulatory_flags |= REGULATORY_CUSTOM_REG |
544                                                REGULATORY_DISABLE_BEACON_HINTS;
545
546         if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
547                 wiphy_ext_feature_set(hw->wiphy,
548                                       NL80211_EXT_FEATURE_DFS_CONCURRENT);
549
550         hw->wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
551         hw->wiphy->flags |= WIPHY_FLAG_HAS_CHANNEL_SWITCH;
552         hw->wiphy->flags |= WIPHY_FLAG_SPLIT_SCAN_6GHZ;
553
554         hw->wiphy->iface_combinations = iwl_mvm_iface_combinations;
555         hw->wiphy->n_iface_combinations =
556                 ARRAY_SIZE(iwl_mvm_iface_combinations);
557
558         hw->wiphy->max_remain_on_channel_duration = 10000;
559         hw->max_listen_interval = IWL_MVM_CONN_LISTEN_INTERVAL;
560
561         /* Extract MAC address */
562         memcpy(mvm->addresses[0].addr, mvm->nvm_data->hw_addr, ETH_ALEN);
563         hw->wiphy->addresses = mvm->addresses;
564         hw->wiphy->n_addresses = 1;
565
566         /* Extract additional MAC addresses if available */
567         num_mac = (mvm->nvm_data->n_hw_addrs > 1) ?
568                 min(IWL_MVM_MAX_ADDRESSES, mvm->nvm_data->n_hw_addrs) : 1;
569
570         for (i = 1; i < num_mac; i++) {
571                 memcpy(mvm->addresses[i].addr, mvm->addresses[i-1].addr,
572                        ETH_ALEN);
573                 mvm->addresses[i].addr[5]++;
574                 hw->wiphy->n_addresses++;
575         }
576
577         iwl_mvm_reset_phy_ctxts(mvm);
578
579         hw->wiphy->max_scan_ie_len = iwl_mvm_max_scan_ie_len(mvm);
580
581         hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
582
583         BUILD_BUG_ON(IWL_MVM_SCAN_STOPPING_MASK & IWL_MVM_SCAN_MASK);
584         BUILD_BUG_ON(IWL_MAX_UMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK) ||
585                      IWL_MAX_LMAC_SCANS > HWEIGHT32(IWL_MVM_SCAN_MASK));
586
587         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
588                 mvm->max_scans = IWL_MAX_UMAC_SCANS;
589         else
590                 mvm->max_scans = IWL_MAX_LMAC_SCANS;
591
592         if (mvm->nvm_data->bands[NL80211_BAND_2GHZ].n_channels)
593                 hw->wiphy->bands[NL80211_BAND_2GHZ] =
594                         &mvm->nvm_data->bands[NL80211_BAND_2GHZ];
595         if (mvm->nvm_data->bands[NL80211_BAND_5GHZ].n_channels) {
596                 hw->wiphy->bands[NL80211_BAND_5GHZ] =
597                         &mvm->nvm_data->bands[NL80211_BAND_5GHZ];
598
599                 if (fw_has_capa(&mvm->fw->ucode_capa,
600                                 IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
601                     fw_has_api(&mvm->fw->ucode_capa,
602                                IWL_UCODE_TLV_API_LQ_SS_PARAMS))
603                         hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap |=
604                                 IEEE80211_VHT_CAP_SU_BEAMFORMER_CAPABLE;
605         }
606         if (fw_has_capa(&mvm->fw->ucode_capa,
607                         IWL_UCODE_TLV_CAPA_PSC_CHAN_SUPPORT) &&
608             mvm->nvm_data->bands[NL80211_BAND_6GHZ].n_channels)
609                 hw->wiphy->bands[NL80211_BAND_6GHZ] =
610                         &mvm->nvm_data->bands[NL80211_BAND_6GHZ];
611
612         hw->wiphy->hw_version = mvm->trans->hw_id;
613
614         if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
615                 hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
616         else
617                 hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
618
619         hw->wiphy->max_sched_scan_reqs = 1;
620         hw->wiphy->max_sched_scan_ssids = PROBE_OPTION_MAX;
621         hw->wiphy->max_match_sets = iwl_umac_scan_get_max_profiles(mvm->fw);
622         /* we create the 802.11 header and zero length SSID IE. */
623         hw->wiphy->max_sched_scan_ie_len =
624                 SCAN_OFFLOAD_PROBE_REQ_SIZE - 24 - 2;
625         hw->wiphy->max_sched_scan_plans = IWL_MAX_SCHED_SCAN_PLANS;
626         hw->wiphy->max_sched_scan_plan_interval = U16_MAX;
627
628         /*
629          * the firmware uses u8 for num of iterations, but 0xff is saved for
630          * infinite loop, so the maximum number of iterations is actually 254.
631          */
632         hw->wiphy->max_sched_scan_plan_iterations = 254;
633
634         hw->wiphy->features |= NL80211_FEATURE_P2P_GO_CTWIN |
635                                NL80211_FEATURE_LOW_PRIORITY_SCAN |
636                                NL80211_FEATURE_P2P_GO_OPPPS |
637                                NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
638                                NL80211_FEATURE_SUPPORTS_WMM_ADMISSION;
639
640         /* when firmware supports RLC/SMPS offload, do not set these
641          * driver features, since it's no longer supported by driver.
642          */
643         if (!iwl_mvm_has_rlc_offload(mvm))
644                 hw->wiphy->features |= NL80211_FEATURE_STATIC_SMPS |
645                                        NL80211_FEATURE_DYNAMIC_SMPS;
646
647         if (fw_has_capa(&mvm->fw->ucode_capa,
648                         IWL_UCODE_TLV_CAPA_TXPOWER_INSERTION_SUPPORT))
649                 hw->wiphy->features |= NL80211_FEATURE_TX_POWER_INSERTION;
650         if (fw_has_capa(&mvm->fw->ucode_capa,
651                         IWL_UCODE_TLV_CAPA_QUIET_PERIOD_SUPPORT))
652                 hw->wiphy->features |= NL80211_FEATURE_QUIET;
653
654         if (fw_has_capa(&mvm->fw->ucode_capa,
655                         IWL_UCODE_TLV_CAPA_DS_PARAM_SET_IE_SUPPORT))
656                 hw->wiphy->features |=
657                         NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES;
658
659         if (fw_has_capa(&mvm->fw->ucode_capa,
660                         IWL_UCODE_TLV_CAPA_WFA_TPC_REP_IE_SUPPORT))
661                 hw->wiphy->features |= NL80211_FEATURE_WFA_TPC_IE_IN_PROBES;
662
663         if (iwl_fw_lookup_cmd_ver(mvm->fw, WOWLAN_KEK_KCK_MATERIAL,
664                                   IWL_FW_CMD_VER_UNKNOWN) >= 3)
665                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_EXT_KEK_KCK;
666
667         if (fw_has_api(&mvm->fw->ucode_capa,
668                        IWL_UCODE_TLV_API_SCAN_TSF_REPORT)) {
669                 wiphy_ext_feature_set(hw->wiphy,
670                                       NL80211_EXT_FEATURE_SCAN_START_TIME);
671                 wiphy_ext_feature_set(hw->wiphy,
672                                       NL80211_EXT_FEATURE_BSS_PARENT_TSF);
673         }
674
675         if (iwl_mvm_is_oce_supported(mvm)) {
676                 u8 scan_ver = iwl_fw_lookup_cmd_ver(mvm->fw, SCAN_REQ_UMAC, 0);
677
678                 wiphy_ext_feature_set(hw->wiphy,
679                         NL80211_EXT_FEATURE_ACCEPT_BCAST_PROBE_RESP);
680                 wiphy_ext_feature_set(hw->wiphy,
681                         NL80211_EXT_FEATURE_FILS_MAX_CHANNEL_TIME);
682                 wiphy_ext_feature_set(hw->wiphy,
683                         NL80211_EXT_FEATURE_OCE_PROBE_REQ_HIGH_TX_RATE);
684
685                 /* Old firmware also supports probe deferral and suppression */
686                 if (scan_ver < 15)
687                         wiphy_ext_feature_set(hw->wiphy,
688                                               NL80211_EXT_FEATURE_OCE_PROBE_REQ_DEFERRAL_SUPPRESSION);
689         }
690
691         hw->wiphy->iftype_ext_capab = NULL;
692         hw->wiphy->num_iftype_ext_capab = 0;
693
694         if (mvm->nvm_data->sku_cap_11ax_enable &&
695             !iwlwifi_mod_params.disable_11ax) {
696                 hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa;
697                 hw->wiphy->num_iftype_ext_capab =
698                         ARRAY_SIZE(add_iftypes_ext_capa) - 1;
699
700                 ieee80211_hw_set(hw, SUPPORTS_MULTI_BSSID);
701                 ieee80211_hw_set(hw, SUPPORTS_ONLY_HE_MULTI_BSSID);
702         }
703
704         if (iwl_fw_lookup_cmd_ver(mvm->fw,
705                                   WIDE_ID(DATA_PATH_GROUP,
706                                           WNM_80211V_TIMING_MEASUREMENT_CONFIG_CMD),
707                                   IWL_FW_CMD_VER_UNKNOWN) >= 1) {
708                 IWL_DEBUG_INFO(mvm->trans, "Timing measurement supported\n");
709
710                 if (!hw->wiphy->iftype_ext_capab) {
711                         hw->wiphy->num_iftype_ext_capab = 1;
712                         hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa +
713                                 ARRAY_SIZE(add_iftypes_ext_capa) - 1;
714                 } else {
715                         hw->wiphy->iftype_ext_capab = add_iftypes_ext_capa + 1;
716                 }
717         }
718
719         if (iwl_fw_lookup_cmd_ver(mvm->fw, WIDE_ID(LOCATION_GROUP,
720                                                    TOF_RANGE_REQ_CMD),
721                                   IWL_FW_CMD_VER_UNKNOWN) >= 11) {
722                 wiphy_ext_feature_set(hw->wiphy,
723                                       NL80211_EXT_FEATURE_PROT_RANGE_NEGO_AND_MEASURE);
724
725                 if (fw_has_capa(&mvm->fw->ucode_capa,
726                                 IWL_UCODE_TLV_CAPA_SECURE_LTF_SUPPORT))
727                         wiphy_ext_feature_set(hw->wiphy,
728                                               NL80211_EXT_FEATURE_SECURE_LTF);
729         }
730
731         mvm->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
732
733         ieee80211_hw_set(hw, DISALLOW_PUNCTURING_5GHZ);
734
735 #ifdef CONFIG_PM_SLEEP
736         if ((unified || mvm->fw->img[IWL_UCODE_WOWLAN].num_sec) &&
737             device_can_wakeup(mvm->trans->dev)) {
738                 mvm->wowlan.flags |= WIPHY_WOWLAN_MAGIC_PKT |
739                                      WIPHY_WOWLAN_DISCONNECT |
740                                      WIPHY_WOWLAN_EAP_IDENTITY_REQ |
741                                      WIPHY_WOWLAN_RFKILL_RELEASE |
742                                      WIPHY_WOWLAN_NET_DETECT;
743                 mvm->wowlan.flags |= WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
744                                      WIPHY_WOWLAN_GTK_REKEY_FAILURE |
745                                      WIPHY_WOWLAN_4WAY_HANDSHAKE;
746
747                 mvm->wowlan.n_patterns = IWL_WOWLAN_MAX_PATTERNS;
748                 mvm->wowlan.pattern_min_len = IWL_WOWLAN_MIN_PATTERN_LEN;
749                 mvm->wowlan.pattern_max_len = IWL_WOWLAN_MAX_PATTERN_LEN;
750                 mvm->wowlan.max_nd_match_sets =
751                         iwl_umac_scan_get_max_profiles(mvm->fw);
752                 hw->wiphy->wowlan = &mvm->wowlan;
753         }
754 #endif
755
756         ret = iwl_mvm_leds_init(mvm);
757         if (ret)
758                 return ret;
759
760         if (fw_has_capa(&mvm->fw->ucode_capa,
761                         IWL_UCODE_TLV_CAPA_TDLS_SUPPORT)) {
762                 IWL_DEBUG_TDLS(mvm, "TDLS supported\n");
763                 hw->wiphy->flags |= WIPHY_FLAG_SUPPORTS_TDLS;
764                 ieee80211_hw_set(hw, TDLS_WIDER_BW);
765         }
766
767         if (fw_has_capa(&mvm->fw->ucode_capa,
768                         IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH)) {
769                 IWL_DEBUG_TDLS(mvm, "TDLS channel switch supported\n");
770                 hw->wiphy->features |= NL80211_FEATURE_TDLS_CHANNEL_SWITCH;
771         }
772
773         hw->netdev_features |= mvm->cfg->features;
774         if (!iwl_mvm_is_csum_supported(mvm))
775                 hw->netdev_features &= ~IWL_CSUM_NETIF_FLAGS_MASK;
776
777         if (mvm->cfg->vht_mu_mimo_supported)
778                 wiphy_ext_feature_set(hw->wiphy,
779                                       NL80211_EXT_FEATURE_MU_MIMO_AIR_SNIFFER);
780
781         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_PROTECTED_TWT))
782                 wiphy_ext_feature_set(hw->wiphy,
783                                       NL80211_EXT_FEATURE_PROTECTED_TWT);
784
785         iwl_mvm_vendor_cmds_register(mvm);
786
787         hw->wiphy->available_antennas_tx = iwl_mvm_get_valid_tx_ant(mvm);
788         hw->wiphy->available_antennas_rx = iwl_mvm_get_valid_rx_ant(mvm);
789
790         ret = ieee80211_register_hw(mvm->hw);
791         if (ret) {
792                 iwl_mvm_leds_exit(mvm);
793         }
794
795         return ret;
796 }
797
798 static void iwl_mvm_tx_skb(struct iwl_mvm *mvm, struct sk_buff *skb,
799                            struct ieee80211_sta *sta)
800 {
801         if (likely(sta)) {
802                 if (likely(iwl_mvm_tx_skb_sta(mvm, skb, sta) == 0))
803                         return;
804         } else {
805                 if (likely(iwl_mvm_tx_skb_non_sta(mvm, skb) == 0))
806                         return;
807         }
808
809         ieee80211_free_txskb(mvm->hw, skb);
810 }
811
812 void iwl_mvm_mac_tx(struct ieee80211_hw *hw,
813                     struct ieee80211_tx_control *control, struct sk_buff *skb)
814 {
815         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
816         struct ieee80211_sta *sta = control->sta;
817         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
818         struct ieee80211_hdr *hdr = (void *)skb->data;
819         bool offchannel = IEEE80211_SKB_CB(skb)->flags &
820                 IEEE80211_TX_CTL_TX_OFFCHAN;
821         u32 link_id = u32_get_bits(info->control.flags,
822                                    IEEE80211_TX_CTRL_MLO_LINK);
823         struct ieee80211_sta *tmp_sta = sta;
824
825         if (iwl_mvm_is_radio_killed(mvm)) {
826                 IWL_DEBUG_DROP(mvm, "Dropping - RF/CT KILL\n");
827                 goto drop;
828         }
829
830         if (offchannel &&
831             !test_bit(IWL_MVM_STATUS_ROC_P2P_RUNNING, &mvm->status) &&
832             !test_bit(IWL_MVM_STATUS_ROC_AUX_RUNNING, &mvm->status))
833                 goto drop;
834
835         /*
836          * bufferable MMPDUs or MMPDUs on STA interfaces come via TXQs
837          * so we treat the others as broadcast
838          */
839         if (ieee80211_is_mgmt(hdr->frame_control))
840                 sta = NULL;
841
842         /* this shouldn't even happen: just drop */
843         if (!sta && info->control.vif->type == NL80211_IFTYPE_STATION &&
844             !offchannel)
845                 goto drop;
846
847         if (tmp_sta && !sta && link_id != IEEE80211_LINK_UNSPECIFIED &&
848             !ieee80211_is_probe_resp(hdr->frame_control)) {
849                 /* translate MLD addresses to LINK addresses */
850                 struct ieee80211_link_sta *link_sta =
851                         rcu_dereference(tmp_sta->link[link_id]);
852                 struct ieee80211_bss_conf *link_conf =
853                         rcu_dereference(info->control.vif->link_conf[link_id]);
854                 struct ieee80211_mgmt *mgmt;
855
856                 if (WARN_ON(!link_sta || !link_conf))
857                         goto drop;
858
859                 /* if sta is NULL, the frame is a management frame */
860                 mgmt = (void *)hdr;
861                 memcpy(mgmt->da, link_sta->addr, ETH_ALEN);
862                 memcpy(mgmt->sa, link_conf->addr, ETH_ALEN);
863                 memcpy(mgmt->bssid, link_conf->bssid, ETH_ALEN);
864         }
865
866         iwl_mvm_tx_skb(mvm, skb, sta);
867         return;
868  drop:
869         ieee80211_free_txskb(hw, skb);
870 }
871
872 void iwl_mvm_mac_itxq_xmit(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
873 {
874         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
875         struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
876         struct sk_buff *skb = NULL;
877
878         /*
879          * No need for threads to be pending here, they can leave the first
880          * taker all the work.
881          *
882          * mvmtxq->tx_request logic:
883          *
884          * If 0, no one is currently TXing, set to 1 to indicate current thread
885          * will now start TX and other threads should quit.
886          *
887          * If 1, another thread is currently TXing, set to 2 to indicate to
888          * that thread that there was another request. Since that request may
889          * have raced with the check whether the queue is empty, the TXing
890          * thread should check the queue's status one more time before leaving.
891          * This check is done in order to not leave any TX hanging in the queue
892          * until the next TX invocation (which may not even happen).
893          *
894          * If 2, another thread is currently TXing, and it will already double
895          * check the queue, so do nothing.
896          */
897         if (atomic_fetch_add_unless(&mvmtxq->tx_request, 1, 2))
898                 return;
899
900         rcu_read_lock();
901         do {
902                 while (likely(!test_bit(IWL_MVM_TXQ_STATE_STOP_FULL,
903                                         &mvmtxq->state) &&
904                               !test_bit(IWL_MVM_TXQ_STATE_STOP_REDIRECT,
905                                         &mvmtxq->state) &&
906                               !test_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA,
907                                         &mvmtxq->state) &&
908                               !test_bit(IWL_MVM_STATUS_IN_D3, &mvm->status))) {
909                         skb = ieee80211_tx_dequeue(hw, txq);
910
911                         if (!skb) {
912                                 if (txq->sta)
913                                         IWL_DEBUG_TX(mvm,
914                                                      "TXQ of sta %pM tid %d is now empty\n",
915                                                      txq->sta->addr,
916                                                      txq->tid);
917                                 break;
918                         }
919
920                         iwl_mvm_tx_skb(mvm, skb, txq->sta);
921                 }
922         } while (atomic_dec_return(&mvmtxq->tx_request));
923         rcu_read_unlock();
924 }
925
926 void iwl_mvm_mac_wake_tx_queue(struct ieee80211_hw *hw,
927                                struct ieee80211_txq *txq)
928 {
929         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
930         struct iwl_mvm_txq *mvmtxq = iwl_mvm_txq_from_mac80211(txq);
931
932         if (likely(test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) ||
933             !txq->sta) {
934                 iwl_mvm_mac_itxq_xmit(hw, txq);
935                 return;
936         }
937
938         /* iwl_mvm_mac_itxq_xmit() will later be called by the worker
939          * to handle any packets we leave on the txq now
940          */
941
942         spin_lock_bh(&mvm->add_stream_lock);
943         /* The list is being deleted only after the queue is fully allocated. */
944         if (list_empty(&mvmtxq->list) &&
945             /* recheck under lock */
946             !test_bit(IWL_MVM_TXQ_STATE_READY, &mvmtxq->state)) {
947                 list_add_tail(&mvmtxq->list, &mvm->add_stream_txqs);
948                 schedule_work(&mvm->add_stream_wk);
949         }
950         spin_unlock_bh(&mvm->add_stream_lock);
951 }
952
953 #define CHECK_BA_TRIGGER(_mvm, _trig, _tid_bm, _tid, _fmt...)           \
954         do {                                                            \
955                 if (!(le16_to_cpu(_tid_bm) & BIT(_tid)))                \
956                         break;                                          \
957                 iwl_fw_dbg_collect_trig(&(_mvm)->fwrt, _trig, _fmt);    \
958         } while (0)
959
960 static void
961 iwl_mvm_ampdu_check_trigger(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
962                             struct ieee80211_sta *sta, u16 tid, u16 rx_ba_ssn,
963                             enum ieee80211_ampdu_mlme_action action)
964 {
965         struct iwl_fw_dbg_trigger_tlv *trig;
966         struct iwl_fw_dbg_trigger_ba *ba_trig;
967
968         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
969                                      FW_DBG_TRIGGER_BA);
970         if (!trig)
971                 return;
972
973         ba_trig = (void *)trig->data;
974
975         switch (action) {
976         case IEEE80211_AMPDU_TX_OPERATIONAL: {
977                 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
978                 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
979
980                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_start, tid,
981                                  "TX AGG START: MAC %pM tid %d ssn %d\n",
982                                  sta->addr, tid, tid_data->ssn);
983                 break;
984                 }
985         case IEEE80211_AMPDU_TX_STOP_CONT:
986                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->tx_ba_stop, tid,
987                                  "TX AGG STOP: MAC %pM tid %d\n",
988                                  sta->addr, tid);
989                 break;
990         case IEEE80211_AMPDU_RX_START:
991                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_start, tid,
992                                  "RX AGG START: MAC %pM tid %d ssn %d\n",
993                                  sta->addr, tid, rx_ba_ssn);
994                 break;
995         case IEEE80211_AMPDU_RX_STOP:
996                 CHECK_BA_TRIGGER(mvm, trig, ba_trig->rx_ba_stop, tid,
997                                  "RX AGG STOP: MAC %pM tid %d\n",
998                                  sta->addr, tid);
999                 break;
1000         default:
1001                 break;
1002         }
1003 }
1004
1005 int iwl_mvm_mac_ampdu_action(struct ieee80211_hw *hw,
1006                              struct ieee80211_vif *vif,
1007                              struct ieee80211_ampdu_params *params)
1008 {
1009         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1010         int ret;
1011         struct ieee80211_sta *sta = params->sta;
1012         enum ieee80211_ampdu_mlme_action action = params->action;
1013         u16 tid = params->tid;
1014         u16 *ssn = &params->ssn;
1015         u16 buf_size = params->buf_size;
1016         bool amsdu = params->amsdu;
1017         u16 timeout = params->timeout;
1018
1019         IWL_DEBUG_HT(mvm, "A-MPDU action on addr %pM tid %d: action %d\n",
1020                      sta->addr, tid, action);
1021
1022         if (!(mvm->nvm_data->sku_cap_11n_enable))
1023                 return -EACCES;
1024
1025         mutex_lock(&mvm->mutex);
1026
1027         switch (action) {
1028         case IEEE80211_AMPDU_RX_START:
1029                 if (iwl_mvm_vif_from_mac80211(vif)->deflink.ap_sta_id ==
1030                     iwl_mvm_sta_from_mac80211(sta)->deflink.sta_id) {
1031                         struct iwl_mvm_vif *mvmvif;
1032                         u16 macid = iwl_mvm_vif_from_mac80211(vif)->id;
1033                         struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[macid];
1034
1035                         mdata->opened_rx_ba_sessions = true;
1036                         mvmvif = iwl_mvm_vif_from_mac80211(vif);
1037                         cancel_delayed_work(&mvmvif->uapsd_nonagg_detected_wk);
1038                 }
1039                 if (!iwl_enable_rx_ampdu()) {
1040                         ret = -EINVAL;
1041                         break;
1042                 }
1043                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, *ssn, true, buf_size,
1044                                          timeout);
1045                 break;
1046         case IEEE80211_AMPDU_RX_STOP:
1047                 ret = iwl_mvm_sta_rx_agg(mvm, sta, tid, 0, false, buf_size,
1048                                          timeout);
1049                 break;
1050         case IEEE80211_AMPDU_TX_START:
1051                 if (!iwl_enable_tx_ampdu()) {
1052                         ret = -EINVAL;
1053                         break;
1054                 }
1055                 ret = iwl_mvm_sta_tx_agg_start(mvm, vif, sta, tid, ssn);
1056                 break;
1057         case IEEE80211_AMPDU_TX_STOP_CONT:
1058                 ret = iwl_mvm_sta_tx_agg_stop(mvm, vif, sta, tid);
1059                 break;
1060         case IEEE80211_AMPDU_TX_STOP_FLUSH:
1061         case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1062                 ret = iwl_mvm_sta_tx_agg_flush(mvm, vif, sta, tid);
1063                 break;
1064         case IEEE80211_AMPDU_TX_OPERATIONAL:
1065                 ret = iwl_mvm_sta_tx_agg_oper(mvm, vif, sta, tid,
1066                                               buf_size, amsdu);
1067                 break;
1068         default:
1069                 WARN_ON_ONCE(1);
1070                 ret = -EINVAL;
1071                 break;
1072         }
1073
1074         if (!ret) {
1075                 u16 rx_ba_ssn = 0;
1076
1077                 if (action == IEEE80211_AMPDU_RX_START)
1078                         rx_ba_ssn = *ssn;
1079
1080                 iwl_mvm_ampdu_check_trigger(mvm, vif, sta, tid,
1081                                             rx_ba_ssn, action);
1082         }
1083         mutex_unlock(&mvm->mutex);
1084
1085         return ret;
1086 }
1087
1088 static void iwl_mvm_cleanup_iterator(void *data, u8 *mac,
1089                                      struct ieee80211_vif *vif)
1090 {
1091         struct iwl_mvm *mvm = data;
1092         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1093         struct iwl_probe_resp_data *probe_data;
1094         unsigned int link_id;
1095
1096         mvmvif->uploaded = false;
1097
1098         spin_lock_bh(&mvm->time_event_lock);
1099         iwl_mvm_te_clear_data(mvm, &mvmvif->time_event_data);
1100         spin_unlock_bh(&mvm->time_event_lock);
1101
1102         mvmvif->roc_activity = ROC_NUM_ACTIVITIES;
1103
1104         mvmvif->bf_enabled = false;
1105         mvmvif->ba_enabled = false;
1106         mvmvif->ap_sta = NULL;
1107
1108         mvmvif->esr_active = false;
1109         vif->driver_flags &= ~IEEE80211_VIF_EML_ACTIVE;
1110
1111         for_each_mvm_vif_valid_link(mvmvif, link_id) {
1112                 mvmvif->link[link_id]->ap_sta_id = IWL_INVALID_STA;
1113                 mvmvif->link[link_id]->fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1114                 mvmvif->link[link_id]->phy_ctxt = NULL;
1115                 mvmvif->link[link_id]->active = 0;
1116                 mvmvif->link[link_id]->igtk = NULL;
1117                 memset(&mvmvif->link[link_id]->bf_data, 0,
1118                        sizeof(mvmvif->link[link_id]->bf_data));
1119         }
1120
1121         probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1122                                                lockdep_is_held(&mvm->mutex));
1123         if (probe_data)
1124                 kfree_rcu(probe_data, rcu_head);
1125         RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1126 }
1127
1128 static void iwl_mvm_cleanup_sta_iterator(void *data, struct ieee80211_sta *sta)
1129 {
1130         struct iwl_mvm *mvm = data;
1131         struct iwl_mvm_sta *mvm_sta;
1132         struct ieee80211_vif *vif;
1133         int link_id;
1134
1135         mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1136         vif = mvm_sta->vif;
1137
1138         if (!sta->valid_links)
1139                 return;
1140
1141         for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
1142                 struct iwl_mvm_link_sta *mvm_link_sta;
1143
1144                 mvm_link_sta =
1145                         rcu_dereference_check(mvm_sta->link[link_id],
1146                                               lockdep_is_held(&mvm->mutex));
1147                 if (mvm_link_sta && !(vif->active_links & BIT(link_id))) {
1148                         /*
1149                          * We have a link STA but the link is inactive in
1150                          * mac80211. This will happen if we failed to
1151                          * deactivate the link but mac80211 roll back the
1152                          * deactivation of the link.
1153                          * Delete the stale data to avoid issues later on.
1154                          */
1155                         iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
1156                                                   link_id, false);
1157                 }
1158         }
1159 }
1160
1161 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1162 {
1163         iwl_mvm_stop_device(mvm);
1164
1165         mvm->cur_aid = 0;
1166
1167         mvm->scan_status = 0;
1168         mvm->ps_disabled = false;
1169         mvm->rfkill_safe_init_done = false;
1170
1171         /* just in case one was running */
1172         iwl_mvm_cleanup_roc_te(mvm);
1173         ieee80211_remain_on_channel_expired(mvm->hw);
1174
1175         iwl_mvm_ftm_restart(mvm);
1176
1177         /*
1178          * cleanup all interfaces, even inactive ones, as some might have
1179          * gone down during the HW restart
1180          */
1181         ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1182
1183         /* cleanup stations as links may be gone after restart */
1184         ieee80211_iterate_stations_atomic(mvm->hw,
1185                                           iwl_mvm_cleanup_sta_iterator, mvm);
1186
1187         mvm->p2p_device_vif = NULL;
1188
1189         iwl_mvm_reset_phy_ctxts(mvm);
1190         memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1191         memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1192         memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1193
1194         ieee80211_wake_queues(mvm->hw);
1195
1196         mvm->rx_ba_sessions = 0;
1197         mvm->fwrt.dump.conf = FW_DBG_INVALID;
1198         mvm->monitor_on = false;
1199 #ifdef CONFIG_IWLWIFI_DEBUGFS
1200         mvm->beacon_inject_active = false;
1201 #endif
1202
1203         /* keep statistics ticking */
1204         iwl_mvm_accu_radio_stats(mvm);
1205 }
1206
1207 int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1208 {
1209         bool fast_resume = false;
1210         int ret;
1211
1212         lockdep_assert_held(&mvm->mutex);
1213
1214         ret = iwl_mvm_mei_get_ownership(mvm);
1215         if (ret)
1216                 return ret;
1217
1218         if (mvm->mei_nvm_data) {
1219                 /* We got the NIC, we can now free the MEI NVM data */
1220                 kfree(mvm->mei_nvm_data);
1221                 mvm->mei_nvm_data = NULL;
1222
1223                 /*
1224                  * We can't free the nvm_data we allocated based on the SAP
1225                  * data because we registered to cfg80211 with the channels
1226                  * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
1227                  * just in order to be able free it later.
1228                  * NULLify nvm_data so that we will read the NVM from the
1229                  * firmware this time.
1230                  */
1231                 mvm->temp_nvm_data = mvm->nvm_data;
1232                 mvm->nvm_data = NULL;
1233         }
1234
1235 #ifdef CONFIG_PM_SLEEP
1236         /* fast_resume will be cleared by iwl_mvm_fast_resume */
1237         fast_resume = mvm->fast_resume;
1238
1239         if (fast_resume) {
1240                 iwl_mvm_mei_device_state(mvm, true);
1241                 ret = iwl_mvm_fast_resume(mvm);
1242                 if (ret) {
1243                         iwl_mvm_stop_device(mvm);
1244                         /* iwl_mvm_up() will be called further down */
1245                 } else {
1246                         /*
1247                          * We clear IWL_MVM_STATUS_FIRMWARE_RUNNING upon
1248                          * mac_down() so that debugfs will stop honoring
1249                          * requests after we flush all the workers.
1250                          * Set the IWL_MVM_STATUS_FIRMWARE_RUNNING bit again
1251                          * now that we are back. This is a bit abusing the
1252                          * flag since the firmware wasn't really ever stopped,
1253                          * but this still serves the purpose.
1254                          */
1255                         set_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1256                 }
1257         }
1258 #endif /* CONFIG_PM_SLEEP */
1259
1260         if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1261                 /*
1262                  * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1263                  * so later code will - from now on - see that we're doing it.
1264                  */
1265                 set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1266                 clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1267                 /* Clean up some internal and mac80211 state on restart */
1268                 iwl_mvm_restart_cleanup(mvm);
1269         }
1270
1271         /* we also want to load the firmware if fast_resume failed */
1272         if (!fast_resume || ret)
1273                 ret = iwl_mvm_up(mvm);
1274
1275         iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1276                                NULL);
1277         iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1278                                NULL);
1279
1280         mvm->last_reset_or_resume_time_jiffies = jiffies;
1281
1282         if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1283                 /* Something went wrong - we need to finish some cleanup
1284                  * that normally iwl_mvm_mac_restart_complete() below
1285                  * would do.
1286                  */
1287                 clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1288         }
1289
1290         return ret;
1291 }
1292
1293 int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1294 {
1295         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1296         int ret;
1297         int retry, max_retry = 0;
1298
1299         mutex_lock(&mvm->mutex);
1300
1301         /* we are starting the mac not in error flow, and restart is enabled */
1302         if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
1303             iwlwifi_mod_params.fw_restart) {
1304                 max_retry = IWL_MAX_INIT_RETRY;
1305                 /*
1306                  * This will prevent mac80211 recovery flows to trigger during
1307                  * init failures
1308                  */
1309                 set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1310         }
1311
1312         for (retry = 0; retry <= max_retry; retry++) {
1313                 ret = __iwl_mvm_mac_start(mvm);
1314                 if (!ret)
1315                         break;
1316
1317                 IWL_ERR(mvm, "mac start retry %d\n", retry);
1318         }
1319         clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1320
1321         mutex_unlock(&mvm->mutex);
1322
1323         iwl_mvm_mei_set_sw_rfkill_state(mvm);
1324
1325         return ret;
1326 }
1327
1328 static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1329 {
1330         int ret;
1331
1332         guard(mvm)(mvm);
1333
1334         clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1335
1336         ret = iwl_mvm_update_quotas(mvm, true, NULL);
1337         if (ret)
1338                 IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1339                         ret);
1340
1341         iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1342
1343         /*
1344          * If we have TDLS peers, remove them. We don't know the last seqno/PN
1345          * of packets the FW sent out, so we must reconnect.
1346          */
1347         iwl_mvm_teardown_tdls_peers(mvm);
1348
1349         IWL_INFO(mvm, "restart completed\n");
1350 }
1351
1352 void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1353                                    enum ieee80211_reconfig_type reconfig_type)
1354 {
1355         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1356
1357         switch (reconfig_type) {
1358         case IEEE80211_RECONFIG_TYPE_RESTART:
1359                 iwl_mvm_restart_complete(mvm);
1360                 break;
1361         case IEEE80211_RECONFIG_TYPE_SUSPEND:
1362                 break;
1363         }
1364 }
1365
1366 void __iwl_mvm_mac_stop(struct iwl_mvm *mvm, bool suspend)
1367 {
1368         lockdep_assert_held(&mvm->mutex);
1369
1370         iwl_mvm_ftm_initiator_smooth_stop(mvm);
1371
1372         /* firmware counters are obviously reset now, but we shouldn't
1373          * partially track so also clear the fw_reset_accu counters.
1374          */
1375         memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1376
1377         /* async_handlers_wk is now blocked */
1378
1379         if (!iwl_mvm_has_new_station_api(mvm->fw))
1380                 iwl_mvm_rm_aux_sta(mvm);
1381
1382         if (suspend &&
1383             mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000) {
1384                 iwl_mvm_fast_suspend(mvm);
1385                 /* From this point on, we won't touch the device */
1386                 iwl_mvm_mei_device_state(mvm, false);
1387         } else {
1388                 iwl_mvm_stop_device(mvm);
1389         }
1390
1391         iwl_mvm_async_handlers_purge(mvm);
1392         /* async_handlers_list is empty and will stay empty: HW is stopped */
1393
1394         /*
1395          * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1396          * hw (as restart_complete() won't be called in this case) and mac80211
1397          * won't execute the restart.
1398          * But make sure to cleanup interfaces that have gone down before/during
1399          * HW restart was requested.
1400          */
1401         if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1402             test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1403                                &mvm->status))
1404                 ieee80211_iterate_interfaces(mvm->hw, 0,
1405                                              iwl_mvm_cleanup_iterator, mvm);
1406
1407         /* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1408          * make sure there's nothing left there and warn if any is found.
1409          */
1410         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1411                 int i;
1412
1413                 for (i = 0; i < mvm->max_scans; i++) {
1414                         if (WARN_ONCE(mvm->scan_uid_status[i],
1415                                       "UMAC scan UID %d status was not cleaned\n",
1416                                       i))
1417                                 mvm->scan_uid_status[i] = 0;
1418                 }
1419         }
1420 }
1421
1422 void iwl_mvm_mac_stop(struct ieee80211_hw *hw, bool suspend)
1423 {
1424         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1425
1426         /* Stop internal MLO scan, if running */
1427         mutex_lock(&mvm->mutex);
1428         iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
1429         mutex_unlock(&mvm->mutex);
1430
1431         wiphy_work_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
1432         wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1433         flush_work(&mvm->async_handlers_wk);
1434         flush_work(&mvm->add_stream_wk);
1435
1436         /*
1437          * Lock and clear the firmware running bit here already, so that
1438          * new commands coming in elsewhere, e.g. from debugfs, will not
1439          * be able to proceed. This is important here because one of those
1440          * debugfs files causes the firmware dump to be triggered, and if we
1441          * don't stop debugfs accesses before canceling that it could be
1442          * retriggered after we flush it but before we've cleared the bit.
1443          */
1444         clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1445
1446         cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1447         cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1448
1449         /*
1450          * The work item could be running or queued if the
1451          * ROC time event stops just as we get here.
1452          */
1453         flush_work(&mvm->roc_done_wk);
1454
1455         iwl_mvm_mei_set_sw_rfkill_state(mvm);
1456
1457         mutex_lock(&mvm->mutex);
1458         __iwl_mvm_mac_stop(mvm, suspend);
1459         mutex_unlock(&mvm->mutex);
1460
1461         /*
1462          * The worker might have been waiting for the mutex, let it run and
1463          * discover that its list is now empty.
1464          */
1465         cancel_work_sync(&mvm->async_handlers_wk);
1466         wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1467 }
1468
1469 struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1470 {
1471         u16 i;
1472
1473         lockdep_assert_held(&mvm->mutex);
1474
1475         for (i = 0; i < NUM_PHY_CTX; i++)
1476                 if (!mvm->phy_ctxts[i].ref)
1477                         return &mvm->phy_ctxts[i];
1478
1479         IWL_ERR(mvm, "No available PHY context\n");
1480         return NULL;
1481 }
1482
1483 int iwl_mvm_set_tx_power(struct iwl_mvm *mvm,
1484                          struct ieee80211_bss_conf *link_conf,
1485                          s16 tx_power)
1486 {
1487         u32 cmd_id = REDUCE_TX_POWER_CMD;
1488         u32 mac_id = iwl_mvm_vif_from_mac80211(link_conf->vif)->id;
1489         int len;
1490         struct iwl_dev_tx_power_cmd_v3_v8 cmd = {
1491                 .common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1492                 .common.mac_context_id = cpu_to_le32(mac_id),
1493         };
1494         struct iwl_dev_tx_power_cmd cmd_v9_v10;
1495         u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 3);
1496         u16 u_tx_power = tx_power == IWL_DEFAULT_MAX_TX_POWER ?
1497                 IWL_DEV_MAX_TX_POWER : 8 * tx_power;
1498         void *cmd_data = &cmd;
1499
1500         cmd.common.pwr_restriction = cpu_to_le16(u_tx_power);
1501
1502         if (cmd_ver > 8) {
1503                 /* Those fields sit on the same place for v9 and v10 */
1504                 cmd_v9_v10.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC);
1505                 cmd_v9_v10.common.mac_context_id = cpu_to_le32(mac_id);
1506                 cmd_v9_v10.common.pwr_restriction = cpu_to_le16(u_tx_power);
1507                 cmd_data = &cmd_v9_v10;
1508         }
1509
1510         if (cmd_ver == 10)
1511                 len = sizeof(cmd_v9_v10.v10);
1512         else if (cmd_ver == 9)
1513                 len = sizeof(cmd_v9_v10.v9);
1514         else if (cmd_ver == 8)
1515                 len = sizeof(cmd.v8);
1516         else if (cmd_ver == 7)
1517                 len = sizeof(cmd.v7);
1518         else if (cmd_ver == 6)
1519                 len = sizeof(cmd.v6);
1520         else if (fw_has_api(&mvm->fw->ucode_capa,
1521                             IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1522                 len = sizeof(cmd.v5);
1523         else if (fw_has_capa(&mvm->fw->ucode_capa,
1524                              IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1525                 len = sizeof(cmd.v4);
1526         else
1527                 len = sizeof(cmd.v3);
1528
1529         /* all structs have the same common part, add its length */
1530         len += sizeof(cmd.common);
1531
1532         if (cmd_ver < 9)
1533                 len += sizeof(cmd.per_band);
1534
1535         return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, cmd_data);
1536
1537 }
1538
1539 static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1540 {
1541         struct ieee80211_hw *hw = data;
1542         int i;
1543
1544         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1545                 struct iwl_mvm_txq *mvmtxq =
1546                         iwl_mvm_txq_from_mac80211(sta->txq[i]);
1547
1548                 clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1549                 iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1550         }
1551 }
1552
1553 int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1554                                 struct ieee80211_vif *vif,
1555                                 struct ieee80211_bss_conf *link_conf)
1556 {
1557         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1558         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1559         int ret;
1560
1561         mutex_lock(&mvm->mutex);
1562
1563         if (vif->type == NL80211_IFTYPE_STATION) {
1564                 struct iwl_mvm_sta *mvmsta;
1565                 unsigned int link_id = link_conf->link_id;
1566                 u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1567
1568                 mvmvif->csa_bcn_pending = false;
1569                 mvmvif->csa_blocks_tx = false;
1570                 mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1571
1572                 if (WARN_ON(!mvmsta)) {
1573                         ret = -EIO;
1574                         goto out_unlock;
1575                 }
1576
1577                 iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1578                 if (mvm->mld_api_is_used)
1579                         iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1580                 else
1581                         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1582
1583                 if (!fw_has_capa(&mvm->fw->ucode_capa,
1584                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1585                         ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1586                         if (ret)
1587                                 goto out_unlock;
1588
1589                         iwl_mvm_stop_session_protection(mvm, vif);
1590                 }
1591         } else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1592                 struct iwl_mvm_txq *mvmtxq =
1593                         iwl_mvm_txq_from_mac80211(vif->txq);
1594
1595                 clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1596
1597                 local_bh_disable();
1598                 iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1599                 ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1600                 local_bh_enable();
1601
1602                 mvmvif->csa_blocks_tx = false;
1603         }
1604
1605         mvmvif->ps_disabled = false;
1606
1607         ret = iwl_mvm_power_update_ps(mvm);
1608
1609 out_unlock:
1610         if (mvmvif->csa_failed)
1611                 ret = -EIO;
1612         mutex_unlock(&mvm->mutex);
1613
1614         return ret;
1615 }
1616
1617 void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1618                                   struct ieee80211_vif *vif,
1619                                   struct ieee80211_bss_conf *link_conf)
1620 {
1621         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1622         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1623         struct iwl_chan_switch_te_cmd cmd = {
1624                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1625                                                           mvmvif->color)),
1626                 .action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1627         };
1628
1629         /*
1630          * In the new flow since FW is in charge of the timing,
1631          * if driver has canceled the channel switch he will receive the
1632          * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1633          */
1634         if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1635                                     CHANNEL_SWITCH_ERROR_NOTIF, 0))
1636                 return;
1637
1638         IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1639
1640         mutex_lock(&mvm->mutex);
1641         if (!fw_has_capa(&mvm->fw->ucode_capa,
1642                          IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1643                 iwl_mvm_remove_csa_period(mvm, vif);
1644         else
1645                 WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1646                                              WIDE_ID(MAC_CONF_GROUP,
1647                                                      CHANNEL_SWITCH_TIME_EVENT_CMD),
1648                                              0, sizeof(cmd), &cmd));
1649         mvmvif->csa_failed = true;
1650         mutex_unlock(&mvm->mutex);
1651
1652         /* If we're here, we can't support MLD */
1653         iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1654 }
1655
1656 void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1657 {
1658         struct iwl_mvm_vif *mvmvif;
1659         struct ieee80211_vif *vif;
1660
1661         mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1662         vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1663
1664         /* Trigger disconnect (should clear the CSA state) */
1665         ieee80211_chswitch_done(vif, false, 0);
1666 }
1667
1668 static u8
1669 iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1670 {
1671         int data_start;
1672         int control_start;
1673         int bw;
1674
1675         if (chandef->width == NL80211_CHAN_WIDTH_320)
1676                 bw = 320;
1677         else if (chandef->width == NL80211_CHAN_WIDTH_160)
1678                 bw = 160;
1679         else
1680                 return 0;
1681
1682         /* data is bw wide so the start is half the width */
1683         data_start = chandef->center_freq1 - bw / 2;
1684         /* control is 20Mhz width */
1685         control_start = chandef->chan->center_freq - 10;
1686
1687         return (control_start - data_start) / 80;
1688 }
1689
1690 static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
1691                                          struct ieee80211_vif *vif)
1692 {
1693         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1694         int ret;
1695
1696         lockdep_assert_held(&mvm->mutex);
1697
1698         ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1699         if (ret) {
1700                 IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1701                 return ret;
1702         }
1703
1704         /* Only queue for this station is the mcast queue,
1705          * which shouldn't be in TFD mask anyway
1706          */
1707         return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
1708                                         vif->type,
1709                                         IWL_STA_MULTICAST);
1710 }
1711
1712 static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1713                                         struct wiphy_work *wk)
1714 {
1715         struct iwl_mvm_vif *mvmvif =
1716                 container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1717         struct iwl_mvm *mvm = mvmvif->mvm;
1718         struct ieee80211_vif *vif =
1719                 container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1720
1721         guard(mvm)(mvm);
1722         iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1723 }
1724
1725 static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
1726 {
1727         struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
1728                                                   mlo_int_scan_wk.work);
1729         struct ieee80211_vif *vif =
1730                 container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1731
1732         guard(mvm)(mvmvif->mvm);
1733         iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
1734 }
1735
1736 static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1737 {
1738         struct iwl_mvm_vif *mvmvif =
1739                 container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1740         struct iwl_mvm *mvm = mvmvif->mvm;
1741         struct ieee80211_vif *vif =
1742                 container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1743
1744         guard(mvm)(mvm);
1745         iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1746 }
1747
1748 static void iwl_mvm_unblock_esr_tmp_non_bss(struct wiphy *wiphy,
1749                                             struct wiphy_work *wk)
1750 {
1751         struct iwl_mvm_vif *mvmvif =
1752                 container_of(wk, struct iwl_mvm_vif,
1753                              unblock_esr_tmp_non_bss_wk.work);
1754         struct iwl_mvm *mvm = mvmvif->mvm;
1755         struct ieee80211_vif *vif =
1756                 container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1757
1758         mutex_lock(&mvm->mutex);
1759         iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TMP_NON_BSS);
1760         mutex_unlock(&mvm->mutex);
1761 }
1762
1763 void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
1764 {
1765         lockdep_assert_held(&mvm->mutex);
1766
1767         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1768                 return;
1769
1770         INIT_DELAYED_WORK(&mvmvif->csa_work,
1771                           iwl_mvm_channel_switch_disconnect_wk);
1772
1773         wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1774                                 iwl_mvm_prevent_esr_done_wk);
1775
1776         wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
1777                                 iwl_mvm_mlo_int_scan_wk);
1778
1779         wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1780                         iwl_mvm_unblock_esr_tpt);
1781
1782         wiphy_delayed_work_init(&mvmvif->unblock_esr_tmp_non_bss_wk,
1783                                 iwl_mvm_unblock_esr_tmp_non_bss);
1784 }
1785
1786 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1787                                      struct ieee80211_vif *vif)
1788 {
1789         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1790         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1791         int ret;
1792         int i;
1793
1794         mutex_lock(&mvm->mutex);
1795
1796         iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
1797
1798         mvmvif->mvm = mvm;
1799
1800         /* the first link always points to the default one */
1801         mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1802         mvmvif->deflink.active = 0;
1803         mvmvif->link[0] = &mvmvif->deflink;
1804
1805         ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1806         if (ret)
1807                 goto out;
1808
1809         /*
1810          * Not much to do here. The stack will not allow interface
1811          * types or combinations that we didn't advertise, so we
1812          * don't really have to check the types.
1813          */
1814
1815         /* make sure that beacon statistics don't go backwards with FW reset */
1816         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1817                 for_each_mvm_vif_valid_link(mvmvif, i)
1818                         mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1819                                 mvmvif->link[i]->beacon_stats.num_beacons;
1820
1821         /* Allocate resources for the MAC context, and add it to the fw  */
1822         ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1823         if (ret)
1824                 goto out;
1825
1826         rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1827
1828         /* Currently not much to do for NAN */
1829         if (vif->type == NL80211_IFTYPE_NAN) {
1830                 ret = 0;
1831                 goto out;
1832         }
1833
1834         /*
1835          * The AP binding flow can be done only after the beacon
1836          * template is configured (which happens only in the mac80211
1837          * start_ap() flow), and adding the broadcast station can happen
1838          * only after the binding.
1839          * In addition, since modifying the MAC before adding a bcast
1840          * station is not allowed by the FW, delay the adding of MAC context to
1841          * the point where we can also add the bcast station.
1842          * In short: there's not much we can do at this point, other than
1843          * allocating resources :)
1844          */
1845         if (vif->type == NL80211_IFTYPE_AP ||
1846             vif->type == NL80211_IFTYPE_ADHOC) {
1847                 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1848                         iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1849                 ret = 0;
1850                 goto out;
1851         }
1852
1853         mvmvif->features |= hw->netdev_features;
1854
1855         ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1856         if (ret)
1857                 goto out_unlock;
1858
1859         ret = iwl_mvm_power_update_mac(mvm);
1860         if (ret)
1861                 goto out_remove_mac;
1862
1863         /* beacon filtering */
1864         ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1865         if (ret)
1866                 goto out_remove_mac;
1867
1868         if (!mvm->bf_allowed_vif &&
1869             vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1870                 mvm->bf_allowed_vif = mvmvif;
1871                 vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1872                                      IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1873         }
1874
1875         if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1876                 mvm->p2p_device_vif = vif;
1877
1878         iwl_mvm_tcm_add_vif(mvm, vif);
1879
1880         if (vif->type == NL80211_IFTYPE_MONITOR) {
1881                 mvm->monitor_on = true;
1882                 mvm->monitor_p80 =
1883                         iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1884         }
1885
1886         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1887                 iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1888
1889         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1890             vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1891             !mvm->csme_vif && mvm->mei_registered) {
1892                 iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1893                 iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1894                 mvm->csme_vif = vif;
1895         }
1896
1897 out:
1898         if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1899                      vif->type == NL80211_IFTYPE_ADHOC))
1900                 ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1901
1902         goto out_unlock;
1903
1904  out_remove_mac:
1905         mvmvif->deflink.phy_ctxt = NULL;
1906         iwl_mvm_mac_ctxt_remove(mvm, vif);
1907  out_unlock:
1908         mutex_unlock(&mvm->mutex);
1909
1910         return ret;
1911 }
1912
1913 void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1914                                  struct ieee80211_vif *vif)
1915 {
1916         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1917
1918         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1919                 /*
1920                  * Flush the ROC worker which will flush the OFFCHANNEL queue.
1921                  * We assume here that all the packets sent to the OFFCHANNEL
1922                  * queue are sent in ROC session.
1923                  */
1924                 flush_work(&mvm->roc_done_wk);
1925         }
1926
1927         wiphy_delayed_work_cancel(mvm->hw->wiphy,
1928                                   &mvmvif->prevent_esr_done_wk);
1929
1930         wiphy_delayed_work_cancel(mvm->hw->wiphy,
1931                                   &mvmvif->mlo_int_scan_wk);
1932
1933         wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1934         wiphy_delayed_work_cancel(mvm->hw->wiphy,
1935                                   &mvmvif->unblock_esr_tmp_non_bss_wk);
1936
1937         cancel_delayed_work_sync(&mvmvif->csa_work);
1938 }
1939
1940 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1941                                          struct ieee80211_vif *vif)
1942 {
1943         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1944         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1945         struct iwl_probe_resp_data *probe_data;
1946
1947         iwl_mvm_prepare_mac_removal(mvm, vif);
1948
1949         if (!(vif->type == NL80211_IFTYPE_AP ||
1950               vif->type == NL80211_IFTYPE_ADHOC))
1951                 iwl_mvm_tcm_rm_vif(mvm, vif);
1952
1953         mutex_lock(&mvm->mutex);
1954
1955         if (vif == mvm->csme_vif) {
1956                 iwl_mei_set_netdev(NULL);
1957                 mvm->csme_vif = NULL;
1958         }
1959
1960         probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1961                                                lockdep_is_held(&mvm->mutex));
1962         RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1963         if (probe_data)
1964                 kfree_rcu(probe_data, rcu_head);
1965
1966         if (mvm->bf_allowed_vif == mvmvif) {
1967                 mvm->bf_allowed_vif = NULL;
1968                 vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1969                                        IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1970         }
1971
1972         if (vif->bss_conf.ftm_responder)
1973                 memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1974
1975         iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1976
1977         /*
1978          * For AP/GO interface, the tear down of the resources allocated to the
1979          * interface is be handled as part of the stop_ap flow.
1980          */
1981         if (vif->type == NL80211_IFTYPE_AP ||
1982             vif->type == NL80211_IFTYPE_ADHOC) {
1983 #ifdef CONFIG_NL80211_TESTMODE
1984                 if (vif == mvm->noa_vif) {
1985                         mvm->noa_vif = NULL;
1986                         mvm->noa_duration = 0;
1987                 }
1988 #endif
1989                 goto out;
1990         }
1991
1992         iwl_mvm_power_update_mac(mvm);
1993
1994         /* Before the interface removal, mac80211 would cancel the ROC, and the
1995          * ROC worker would be scheduled if needed. The worker would be flushed
1996          * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
1997          * binding etc. so nothing needs to be done here.
1998          */
1999         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
2000                 if (mvmvif->deflink.phy_ctxt) {
2001                         iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
2002                         mvmvif->deflink.phy_ctxt = NULL;
2003                 }
2004                 mvm->p2p_device_vif = NULL;
2005         }
2006
2007         iwl_mvm_mac_ctxt_remove(mvm, vif);
2008
2009         RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
2010
2011         if (vif->type == NL80211_IFTYPE_MONITOR)
2012                 mvm->monitor_on = false;
2013
2014 out:
2015         iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
2016         if (vif->type == NL80211_IFTYPE_AP ||
2017             vif->type == NL80211_IFTYPE_ADHOC) {
2018                 iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
2019                 iwl_mvm_dealloc_bcast_sta(mvm, vif);
2020         }
2021
2022         mutex_unlock(&mvm->mutex);
2023 }
2024
2025 struct iwl_mvm_mc_iter_data {
2026         struct iwl_mvm *mvm;
2027         int port_id;
2028 };
2029
2030 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
2031                                       struct ieee80211_vif *vif)
2032 {
2033         struct iwl_mvm_mc_iter_data *data = _data;
2034         struct iwl_mvm *mvm = data->mvm;
2035         struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
2036         struct iwl_host_cmd hcmd = {
2037                 .id = MCAST_FILTER_CMD,
2038                 .flags = CMD_ASYNC,
2039                 .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
2040         };
2041         int ret, len;
2042
2043         /* if we don't have free ports, mcast frames will be dropped */
2044         if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
2045                 return;
2046
2047         if (vif->type != NL80211_IFTYPE_STATION ||
2048             !vif->cfg.assoc)
2049                 return;
2050
2051         cmd->port_id = data->port_id++;
2052         memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
2053         len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
2054
2055         hcmd.len[0] = len;
2056         hcmd.data[0] = cmd;
2057
2058         ret = iwl_mvm_send_cmd(mvm, &hcmd);
2059         if (ret)
2060                 IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
2061 }
2062
2063 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
2064 {
2065         struct iwl_mvm_mc_iter_data iter_data = {
2066                 .mvm = mvm,
2067         };
2068         int ret;
2069
2070         lockdep_assert_held(&mvm->mutex);
2071
2072         if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2073                 return;
2074
2075         ieee80211_iterate_active_interfaces_atomic(
2076                 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2077                 iwl_mvm_mc_iface_iterator, &iter_data);
2078
2079         /*
2080          * Send a (synchronous) ech command so that we wait for the
2081          * multiple asynchronous MCAST_FILTER_CMD commands sent by
2082          * the interface iterator. Otherwise, we might get here over
2083          * and over again (by userspace just sending a lot of these)
2084          * and the CPU can send them faster than the firmware can
2085          * process them.
2086          * Note that the CPU is still faster - but with this we'll
2087          * actually send fewer commands overall because the CPU will
2088          * not schedule the work in mac80211 as frequently if it's
2089          * still running when rescheduled (possibly multiple times).
2090          */
2091         ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2092         if (ret)
2093                 IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2094 }
2095
2096 u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2097                               struct netdev_hw_addr_list *mc_list)
2098 {
2099         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2100         struct iwl_mcast_filter_cmd *cmd;
2101         struct netdev_hw_addr *addr;
2102         int addr_count;
2103         bool pass_all;
2104         int len;
2105
2106         addr_count = netdev_hw_addr_list_count(mc_list);
2107         pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2108                    IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2109         if (pass_all)
2110                 addr_count = 0;
2111
2112         len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2113         cmd = kzalloc(len, GFP_ATOMIC);
2114         if (!cmd)
2115                 return 0;
2116
2117         if (pass_all) {
2118                 cmd->pass_all = 1;
2119                 return (u64)(unsigned long)cmd;
2120         }
2121
2122         netdev_hw_addr_list_for_each(addr, mc_list) {
2123                 IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2124                                    cmd->count, addr->addr);
2125                 memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2126                        addr->addr, ETH_ALEN);
2127                 cmd->count++;
2128         }
2129
2130         return (u64)(unsigned long)cmd;
2131 }
2132
2133 void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2134                               unsigned int changed_flags,
2135                               unsigned int *total_flags, u64 multicast)
2136 {
2137         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2138         struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2139
2140         guard(mvm)(mvm);
2141
2142         /* replace previous configuration */
2143         kfree(mvm->mcast_filter_cmd);
2144         mvm->mcast_filter_cmd = cmd;
2145
2146         if (!cmd)
2147                 goto out;
2148
2149         if (changed_flags & FIF_ALLMULTI)
2150                 cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
2151
2152         if (cmd->pass_all)
2153                 cmd->count = 0;
2154
2155         iwl_mvm_recalc_multicast(mvm);
2156 out:
2157         *total_flags = 0;
2158 }
2159
2160 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2161                                         struct ieee80211_vif *vif,
2162                                         unsigned int filter_flags,
2163                                         unsigned int changed_flags)
2164 {
2165         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2166
2167         /* We support only filter for probe requests */
2168         if (!(changed_flags & FIF_PROBE_REQ))
2169                 return;
2170
2171         /* Supported only for p2p client interfaces */
2172         if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2173             !vif->p2p)
2174                 return;
2175
2176         guard(mvm)(mvm);
2177         iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2178 }
2179
2180 int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2181 {
2182         struct iwl_mu_group_mgmt_cmd cmd = {};
2183
2184         memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2185                WLAN_MEMBERSHIP_LEN);
2186         memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2187                WLAN_USER_POSITION_LEN);
2188
2189         return iwl_mvm_send_cmd_pdu(mvm,
2190                                     WIDE_ID(DATA_PATH_GROUP,
2191                                             UPDATE_MU_GROUPS_CMD),
2192                                     0, sizeof(cmd), &cmd);
2193 }
2194
2195 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2196                                            struct ieee80211_vif *vif)
2197 {
2198         if (vif->bss_conf.mu_mimo_owner) {
2199                 struct iwl_mu_group_mgmt_notif *notif = _data;
2200
2201                 /*
2202                  * MU-MIMO Group Id action frame is little endian. We treat
2203                  * the data received from firmware as if it came from the
2204                  * action frame, so no conversion is needed.
2205                  */
2206                 ieee80211_update_mu_groups(vif, 0,
2207                                            (u8 *)&notif->membership_status,
2208                                            (u8 *)&notif->user_position);
2209         }
2210 }
2211
2212 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2213                                struct iwl_rx_cmd_buffer *rxb)
2214 {
2215         struct iwl_rx_packet *pkt = rxb_addr(rxb);
2216         struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2217
2218         ieee80211_iterate_active_interfaces_atomic(
2219                         mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2220                         iwl_mvm_mu_mimo_iface_iterator, notif);
2221 }
2222
2223 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2224 {
2225         u8 byte_num = ppe_pos_bit / 8;
2226         u8 bit_num = ppe_pos_bit % 8;
2227         u8 residue_bits;
2228         u8 res;
2229
2230         if (bit_num <= 5)
2231                 return (ppe[byte_num] >> bit_num) &
2232                        (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2233
2234         /*
2235          * If bit_num > 5, we have to combine bits with next byte.
2236          * Calculate how many bits we need to take from current byte (called
2237          * here "residue_bits"), and add them to bits from next byte.
2238          */
2239
2240         residue_bits = 8 - bit_num;
2241
2242         res = (ppe[byte_num + 1] &
2243                (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2244               residue_bits;
2245         res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2246
2247         return res;
2248 }
2249
2250 static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2251                               struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2252                               u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2253                               bool inheritance)
2254 {
2255         int i;
2256
2257         /*
2258         * FW currently supports only nss == MAX_HE_SUPP_NSS
2259         *
2260         * If nss > MAX: we can ignore values we don't support
2261         * If nss < MAX: we can set zeros in other streams
2262         */
2263         if (nss > MAX_HE_SUPP_NSS) {
2264                 IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2265                                MAX_HE_SUPP_NSS);
2266                 nss = MAX_HE_SUPP_NSS;
2267         }
2268
2269         for (i = 0; i < nss; i++) {
2270                 u8 ru_index_tmp = ru_index_bitmap << 1;
2271                 u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2272                 u8 bw;
2273
2274                 for (bw = 0;
2275                      bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2276                      bw++) {
2277                         ru_index_tmp >>= 1;
2278
2279                         /*
2280                         * According to the 11be spec, if for a specific BW the PPE Thresholds
2281                         * isn't present - it should inherit the thresholds from the last
2282                         * BW for which we had PPE Thresholds. In 11ax though, we don't have
2283                         * this inheritance - continue in this case
2284                         */
2285                         if (!(ru_index_tmp & 1)) {
2286                                 if (inheritance)
2287                                         goto set_thresholds;
2288                                 else
2289                                         continue;
2290                         }
2291
2292                         high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2293                         ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2294                         low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2295                         ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2296
2297 set_thresholds:
2298                         pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2299                         pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2300                 }
2301         }
2302 }
2303
2304 static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2305                                             struct ieee80211_link_sta *link_sta,
2306                                             struct iwl_he_pkt_ext_v2 *pkt_ext,
2307                                             bool inheritance)
2308 {
2309         u8 nss = (link_sta->he_cap.ppe_thres[0] &
2310                   IEEE80211_PPE_THRES_NSS_MASK) + 1;
2311         u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2312         u8 ru_index_bitmap =
2313                 u8_get_bits(*ppe,
2314                             IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2315         /* Starting after PPE header */
2316         u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2317
2318         iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2319                           inheritance);
2320 }
2321
2322 static int
2323 iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2324                                          u8 nominal_padding)
2325 {
2326         int low_th = -1;
2327         int high_th = -1;
2328         int i;
2329
2330         /* all the macros are the same for EHT and HE */
2331         switch (nominal_padding) {
2332         case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2333                 low_th = IWL_HE_PKT_EXT_NONE;
2334                 high_th = IWL_HE_PKT_EXT_NONE;
2335                 break;
2336         case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2337                 low_th = IWL_HE_PKT_EXT_BPSK;
2338                 high_th = IWL_HE_PKT_EXT_NONE;
2339                 break;
2340         case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2341         case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2342                 low_th = IWL_HE_PKT_EXT_NONE;
2343                 high_th = IWL_HE_PKT_EXT_BPSK;
2344                 break;
2345         }
2346
2347         if (low_th < 0 || high_th < 0)
2348                 return -EINVAL;
2349
2350         /* Set the PPE thresholds accordingly */
2351         for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2352                 u8 bw;
2353
2354                 for (bw = 0;
2355                         bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2356                         bw++) {
2357                         pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2358                         pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2359                 }
2360         }
2361
2362         return 0;
2363 }
2364
2365 static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2366                                          u8 nominal_padding)
2367 {
2368         int i;
2369
2370         for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2371                 u8 bw;
2372
2373                 for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2374                      bw++) {
2375                         u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2376
2377                         if (nominal_padding >
2378                             IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2379                             qam_th[1] == IWL_HE_PKT_EXT_NONE)
2380                                 qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2381                         else if (nominal_padding ==
2382                                  IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2383                                  qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2384                                  qam_th[1] == IWL_HE_PKT_EXT_NONE)
2385                                 qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2386                 }
2387         }
2388 }
2389
2390 /* Set the pkt_ext field according to PPE Thresholds element */
2391 int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2392                             struct ieee80211_link_sta *link_sta,
2393                             struct iwl_he_pkt_ext_v2 *pkt_ext)
2394 {
2395         u8 nominal_padding;
2396         int i, ret = 0;
2397
2398         if (WARN_ON(!link_sta))
2399                 return -EINVAL;
2400
2401         /* Initialize the PPE thresholds to "None" (7), as described in Table
2402          * 9-262ac of 80211.ax/D3.0.
2403          */
2404         memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2405                sizeof(struct iwl_he_pkt_ext_v2));
2406
2407         if (link_sta->eht_cap.has_eht) {
2408                 nominal_padding =
2409                         u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2410                                     IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2411
2412                 /* If PPE Thresholds exists, parse them into a FW-familiar
2413                  * format.
2414                  */
2415                 if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2416                     IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2417                         u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2418                                 IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2419                         u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2420                         u8 ru_index_bitmap =
2421                                 u16_get_bits(*ppe,
2422                                              IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2423                          /* Starting after PPE header */
2424                         u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2425
2426                         iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2427                                           ppe, ppe_pos_bit, true);
2428                 /* EHT PPE Thresholds doesn't exist - set the API according to
2429                  * HE PPE Tresholds
2430                  */
2431                 } else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2432                            IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2433                         /* Even though HE Capabilities IE doesn't contain PPE
2434                          * Thresholds for BW 320Mhz, thresholds for this BW will
2435                          * be filled in with the same values as 160Mhz, due to
2436                          * the inheritance, as required.
2437                          */
2438                         iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2439                                                         true);
2440
2441                         /* According to the requirements, for MCSs 12-13 the
2442                          * maximum value between HE PPE Threshold and Common
2443                          * Nominal Packet Padding needs to be taken
2444                          */
2445                         iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2446
2447                 /* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2448                  * take the Thresholds from Common Nominal Packet Padding field
2449                  */
2450                 } else {
2451                         ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2452                                                                        nominal_padding);
2453                 }
2454         } else if (link_sta->he_cap.has_he) {
2455                 /* If PPE Thresholds exist, parse them into a FW-familiar format. */
2456                 if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2457                         IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2458                         iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2459                                                         false);
2460                 /* PPE Thresholds doesn't exist - set the API PPE values
2461                  * according to Common Nominal Packet Padding field.
2462                  */
2463                 } else {
2464                         nominal_padding =
2465                                 u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2466                                             IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2467                         if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2468                                 ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2469                                                                                nominal_padding);
2470                 }
2471         }
2472
2473         for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2474                 int bw;
2475
2476                 for (bw = 0;
2477                      bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2478                      bw++) {
2479                         u8 *qam_th =
2480                                 &pkt_ext->pkt_ext_qam_th[i][bw][0];
2481
2482                         IWL_DEBUG_HT(mvm,
2483                                      "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2484                                      i, bw, qam_th[0], qam_th[1]);
2485                 }
2486         }
2487         return ret;
2488 }
2489
2490 /*
2491  * This function sets the MU EDCA parameters ans returns whether MU EDCA
2492  * is enabled or not
2493  */
2494 bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2495                                    const struct iwl_mvm_vif_link_info *link_info,
2496                                    struct iwl_he_backoff_conf *trig_based_txf)
2497 {
2498         int i;
2499         /* Mark MU EDCA as enabled, unless none detected on some AC */
2500         bool mu_edca_enabled = true;
2501
2502         for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2503                 const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2504                         &link_info->queue_params[i].mu_edca_param_rec;
2505                 u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2506
2507                 if (!link_info->queue_params[i].mu_edca) {
2508                         mu_edca_enabled = false;
2509                         break;
2510                 }
2511
2512                 trig_based_txf[ac].cwmin =
2513                         cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2514                 trig_based_txf[ac].cwmax =
2515                         cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2516                 trig_based_txf[ac].aifsn =
2517                         cpu_to_le16(mu_edca->aifsn & 0xf);
2518                 trig_based_txf[ac].mu_time =
2519                         cpu_to_le16(mu_edca->mu_edca_timer);
2520         }
2521
2522         return mu_edca_enabled;
2523 }
2524
2525 bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2526 {
2527         const struct ieee80211_supported_band *sband;
2528         const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2529
2530         /* This capability is the same for all bands,
2531          * so take it from one of them.
2532          */
2533         sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2534         own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2535
2536         return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2537                                IEEE80211_HE_MAC_CAP2_ACK_EN));
2538 }
2539
2540 __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2541                                  struct ieee80211_link_sta *link_sta)
2542 {
2543         u8 *mac_cap_info =
2544                 &link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2545         __le32 htc_flags = 0;
2546
2547         if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2548                 htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2549         if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2550             (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2551                 u8 link_adap =
2552                         ((mac_cap_info[2] &
2553                           IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2554                          (mac_cap_info[1] &
2555                           IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2556
2557                 if (link_adap == 2)
2558                         htc_flags |=
2559                                 cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2560                 else if (link_adap == 3)
2561                         htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2562         }
2563         if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2564                 htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2565         if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2566                 htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2567         if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2568                 htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2569
2570         return htc_flags;
2571 }
2572
2573 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2574                                struct ieee80211_vif *vif, u8 sta_id)
2575 {
2576         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2577         struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2578                 .sta_id = sta_id,
2579                 .tid_limit = IWL_MAX_TID_COUNT,
2580                 .bss_color = vif->bss_conf.he_bss_color.color,
2581                 .htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2582                 .frame_time_rts_th =
2583                         cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2584         };
2585         struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2586         u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2587         u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2588         int size;
2589         struct ieee80211_sta *sta;
2590         u32 flags;
2591         int i;
2592         void *cmd;
2593
2594         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2595                 ver = 1;
2596
2597         switch (ver) {
2598         case 1:
2599                 /* same layout as v2 except some data at the end */
2600                 cmd = &sta_ctxt_cmd_v2;
2601                 size = sizeof(struct iwl_he_sta_context_cmd_v1);
2602                 break;
2603         case 2:
2604                 cmd = &sta_ctxt_cmd_v2;
2605                 size = sizeof(struct iwl_he_sta_context_cmd_v2);
2606                 break;
2607         case 3:
2608                 cmd = &sta_ctxt_cmd;
2609                 size = sizeof(struct iwl_he_sta_context_cmd_v3);
2610                 break;
2611         default:
2612                 IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2613                 return;
2614         }
2615
2616         rcu_read_lock();
2617
2618         sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2619         if (IS_ERR_OR_NULL(sta)) {
2620                 rcu_read_unlock();
2621                 WARN(1, "Can't find STA to configure HE\n");
2622                 return;
2623         }
2624
2625         if (!sta->deflink.he_cap.has_he) {
2626                 rcu_read_unlock();
2627                 return;
2628         }
2629
2630         flags = 0;
2631
2632         /* Block 26-tone RU OFDMA transmissions */
2633         if (mvmvif->deflink.he_ru_2mhz_block)
2634                 flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2635
2636         /* HTC flags */
2637         sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2638
2639         /* PPE Thresholds */
2640         if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2641                 flags |= STA_CTXT_HE_PACKET_EXT;
2642
2643         if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2644             IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2645                 flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2646
2647         if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2648             IEEE80211_HE_MAC_CAP2_ACK_EN)
2649                 flags |= STA_CTXT_HE_ACK_ENABLED;
2650
2651         rcu_read_unlock();
2652
2653         if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2654                                           &sta_ctxt_cmd.trig_based_txf[0]))
2655                 flags |= STA_CTXT_HE_MU_EDCA_CW;
2656
2657         if (vif->bss_conf.uora_exists) {
2658                 flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2659
2660                 sta_ctxt_cmd.rand_alloc_ecwmin =
2661                         vif->bss_conf.uora_ocw_range & 0x7;
2662                 sta_ctxt_cmd.rand_alloc_ecwmax =
2663                         (vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2664         }
2665
2666         if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2667                 flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2668
2669         if (vif->bss_conf.nontransmitted) {
2670                 flags |= STA_CTXT_HE_REF_BSSID_VALID;
2671                 ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2672                                 vif->bss_conf.transmitter_bssid);
2673                 sta_ctxt_cmd.max_bssid_indicator =
2674                         vif->bss_conf.bssid_indicator;
2675                 sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2676                 sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2677                 sta_ctxt_cmd.profile_periodicity =
2678                         vif->bss_conf.profile_periodicity;
2679         }
2680
2681         sta_ctxt_cmd.flags = cpu_to_le32(flags);
2682
2683         if (ver < 3) {
2684                 /* fields before pkt_ext */
2685                 BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2686                              offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2687                 memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2688                        offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2689
2690                 /* pkt_ext */
2691                 for (i = 0;
2692                      i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2693                      i++) {
2694                         u8 bw;
2695
2696                         for (bw = 0;
2697                              bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2698                              bw++) {
2699                                 BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2700                                              sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2701
2702                                 memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2703                                        &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2704                                        sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2705                         }
2706                 }
2707
2708                 /* fields after pkt_ext */
2709                 BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2710                              offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2711                              sizeof(sta_ctxt_cmd_v2) -
2712                              offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2713                 memcpy((u8 *)&sta_ctxt_cmd_v2 +
2714                                 offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2715                        (u8 *)&sta_ctxt_cmd +
2716                                 offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2717                        sizeof(sta_ctxt_cmd) -
2718                                 offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2719                 sta_ctxt_cmd_v2.reserved3 = 0;
2720         }
2721
2722         if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2723                 IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2724 }
2725
2726 void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2727                            u32 duration_override, unsigned int link_id)
2728 {
2729         u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2730         u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2731
2732         if (duration_override > duration)
2733                 duration = duration_override;
2734
2735         /* Try really hard to protect the session and hear a beacon
2736          * The new session protection command allows us to protect the
2737          * session for a much longer time since the firmware will internally
2738          * create two events: a 300TU one with a very high priority that
2739          * won't be fragmented which should be enough for 99% of the cases,
2740          * and another one (which we configure here to be 900TU long) which
2741          * will have a slightly lower priority, but more importantly, can be
2742          * fragmented so that it'll allow other activities to run.
2743          */
2744         if (fw_has_capa(&mvm->fw->ucode_capa,
2745                         IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2746                 iwl_mvm_schedule_session_protection(mvm, vif, 900,
2747                                                     min_duration, false,
2748                                                     link_id);
2749         else
2750                 iwl_mvm_protect_session(mvm, vif, duration,
2751                                         min_duration, 500, false);
2752 }
2753
2754 /* Handle association common part to MLD and non-MLD modes */
2755 void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2756                                             struct ieee80211_vif *vif,
2757                                             u64 changes)
2758 {
2759         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2760         int ret;
2761         int link_id;
2762
2763         /* The firmware tracks the MU-MIMO group on its own.
2764          * However, on HW restart we should restore this data.
2765          */
2766         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2767             (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2768                 ret = iwl_mvm_update_mu_groups(mvm, vif);
2769                 if (ret)
2770                         IWL_ERR(mvm,
2771                                 "failed to update VHT MU_MIMO groups\n");
2772         }
2773
2774         iwl_mvm_recalc_multicast(mvm);
2775
2776         /* reset rssi values */
2777         for_each_mvm_vif_valid_link(mvmvif, link_id)
2778                 mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2779
2780         iwl_mvm_bt_coex_vif_change(mvm);
2781         iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2782                                             IEEE80211_SMPS_AUTOMATIC);
2783         if (fw_has_capa(&mvm->fw->ucode_capa,
2784                         IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2785                 iwl_mvm_config_scan(mvm);
2786 }
2787
2788 /* Execute the common part for MLD and non-MLD modes */
2789 void
2790 iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2791                                         struct ieee80211_vif *vif,
2792                                         struct ieee80211_bss_conf *link_conf,
2793                                         u64 changes)
2794 {
2795         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2796         int ret;
2797
2798         if (changes & BSS_CHANGED_BEACON_INFO) {
2799                 /* We received a beacon from the associated AP so
2800                  * remove the session protection.
2801                  */
2802                 iwl_mvm_stop_session_protection(mvm, vif);
2803
2804                 iwl_mvm_sf_update(mvm, vif, false);
2805                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2806         }
2807
2808         if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2809                        /* Send power command on every beacon change,
2810                         * because we may have not enabled beacon abort yet.
2811                         */
2812                        BSS_CHANGED_BEACON_INFO)) {
2813                 ret = iwl_mvm_power_update_mac(mvm);
2814                 if (ret)
2815                         IWL_ERR(mvm, "failed to update power mode\n");
2816         }
2817
2818         if (changes & BSS_CHANGED_CQM) {
2819                 struct iwl_mvm_vif_link_info *link_info =
2820                         mvmvif->link[link_conf->link_id];
2821
2822                 IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2823                 if (link_info)
2824                         link_info->bf_data.last_cqm_event = 0;
2825
2826                 if (mvmvif->bf_enabled) {
2827                         /* FIXME: need to update per link when FW API will
2828                          * support it
2829                          */
2830                         ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2831                         if (ret)
2832                                 IWL_ERR(mvm,
2833                                         "failed to update CQM thresholds\n");
2834                 }
2835         }
2836
2837         if (changes & BSS_CHANGED_BANDWIDTH)
2838                 iwl_mvm_update_link_smps(vif, link_conf);
2839
2840         if (changes & BSS_CHANGED_TPE) {
2841                 IWL_DEBUG_CALIB(mvm, "Changing TPE\n");
2842                 iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
2843                                                         link_conf,
2844                                                         false);
2845         }
2846 }
2847
2848 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2849                                              struct ieee80211_vif *vif,
2850                                              struct ieee80211_bss_conf *bss_conf,
2851                                              u64 changes)
2852 {
2853         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2854         int ret;
2855         int i;
2856
2857         /*
2858          * Re-calculate the tsf id, as the leader-follower relations depend
2859          * on the beacon interval, which was not known when the station
2860          * interface was added.
2861          */
2862         if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2863                 if ((vif->bss_conf.he_support &&
2864                      !iwlwifi_mod_params.disable_11ax))
2865                         iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2866
2867                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2868         }
2869
2870         /* Update MU EDCA params */
2871         if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2872             vif->cfg.assoc &&
2873             (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2874                 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2875
2876         /*
2877          * If we're not associated yet, take the (new) BSSID before associating
2878          * so the firmware knows. If we're already associated, then use the old
2879          * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2880          * branch for disassociation below.
2881          */
2882         if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2883                 memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2884
2885         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2886         if (ret)
2887                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2888
2889         /* after sending it once, adopt mac80211 data */
2890         memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2891         mvmvif->associated = vif->cfg.assoc;
2892
2893         if (changes & BSS_CHANGED_ASSOC) {
2894                 if (vif->cfg.assoc) {
2895                         mvmvif->session_prot_connection_loss = false;
2896
2897                         /* clear statistics to get clean beacon counter */
2898                         iwl_mvm_request_statistics(mvm, true);
2899                         for_each_mvm_vif_valid_link(mvmvif, i)
2900                                 memset(&mvmvif->link[i]->beacon_stats, 0,
2901                                        sizeof(mvmvif->link[i]->beacon_stats));
2902
2903                         /* add quota for this interface */
2904                         ret = iwl_mvm_update_quotas(mvm, true, NULL);
2905                         if (ret) {
2906                                 IWL_ERR(mvm, "failed to update quotas\n");
2907                                 return;
2908                         }
2909
2910                         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2911                                      &mvm->status) &&
2912                             !fw_has_capa(&mvm->fw->ucode_capa,
2913                                          IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2914                                 /*
2915                                  * If we're restarting then the firmware will
2916                                  * obviously have lost synchronisation with
2917                                  * the AP. It will attempt to synchronise by
2918                                  * itself, but we can make it more reliable by
2919                                  * scheduling a session protection time event.
2920                                  *
2921                                  * The firmware needs to receive a beacon to
2922                                  * catch up with synchronisation, use 110% of
2923                                  * the beacon interval.
2924                                  *
2925                                  * Set a large maximum delay to allow for more
2926                                  * than a single interface.
2927                                  *
2928                                  * For new firmware versions, rely on the
2929                                  * firmware. This is relevant for DCM scenarios
2930                                  * only anyway.
2931                                  */
2932                                 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2933                                 iwl_mvm_protect_session(mvm, vif, dur, dur,
2934                                                         5 * dur, false);
2935                         } else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2936                                              &mvm->status) &&
2937                                    !vif->bss_conf.dtim_period) {
2938                                 /*
2939                                  * If we're not restarting and still haven't
2940                                  * heard a beacon (dtim period unknown) then
2941                                  * make sure we still have enough minimum time
2942                                  * remaining in the time event, since the auth
2943                                  * might actually have taken quite a while
2944                                  * (especially for SAE) and so the remaining
2945                                  * time could be small without us having heard
2946                                  * a beacon yet.
2947                                  */
2948                                 iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2949                         }
2950
2951                         iwl_mvm_sf_update(mvm, vif, false);
2952                         iwl_mvm_power_vif_assoc(mvm, vif);
2953                         if (vif->p2p) {
2954                                 iwl_mvm_update_smps(mvm, vif,
2955                                                     IWL_MVM_SMPS_REQ_PROT,
2956                                                     IEEE80211_SMPS_DYNAMIC, 0);
2957                         }
2958                 } else if (mvmvif->deflink.ap_sta_id != IWL_INVALID_STA) {
2959                         iwl_mvm_mei_host_disassociated(mvm);
2960                         /*
2961                          * If update fails - SF might be running in associated
2962                          * mode while disassociated - which is forbidden.
2963                          */
2964                         ret = iwl_mvm_sf_update(mvm, vif, false);
2965                         WARN_ONCE(ret &&
2966                                   !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2967                                             &mvm->status),
2968                                   "Failed to update SF upon disassociation\n");
2969
2970                         /*
2971                          * If we get an assert during the connection (after the
2972                          * station has been added, but before the vif is set
2973                          * to associated), mac80211 will re-add the station and
2974                          * then configure the vif. Since the vif is not
2975                          * associated, we would remove the station here and
2976                          * this would fail the recovery.
2977                          */
2978                         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2979                                       &mvm->status)) {
2980                                 /* first remove remaining keys */
2981                                 iwl_mvm_sec_key_remove_ap(mvm, vif,
2982                                                           &mvmvif->deflink, 0);
2983
2984                                 /*
2985                                  * Remove AP station now that
2986                                  * the MAC is unassoc
2987                                  */
2988                                 ret = iwl_mvm_rm_sta_id(mvm, vif,
2989                                                         mvmvif->deflink.ap_sta_id);
2990                                 if (ret)
2991                                         IWL_ERR(mvm,
2992                                                 "failed to remove AP station\n");
2993
2994                                 mvmvif->deflink.ap_sta_id = IWL_INVALID_STA;
2995                         }
2996
2997                         /* remove quota for this interface */
2998                         ret = iwl_mvm_update_quotas(mvm, false, NULL);
2999                         if (ret)
3000                                 IWL_ERR(mvm, "failed to update quotas\n");
3001
3002                         /* this will take the cleared BSSID from bss_conf */
3003                         ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3004                         if (ret)
3005                                 IWL_ERR(mvm,
3006                                         "failed to update MAC %pM (clear after unassoc)\n",
3007                                         vif->addr);
3008                 }
3009
3010                 iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
3011         }
3012
3013         iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
3014                                                 changes);
3015 }
3016
3017 bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
3018                                   struct ieee80211_vif *vif,
3019                                   int *ret)
3020 {
3021         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3022         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3023         int i;
3024
3025         lockdep_assert_held(&mvm->mutex);
3026
3027         mvmvif->ap_assoc_sta_count = 0;
3028
3029         /* must be set before quota calculations */
3030         mvmvif->ap_ibss_active = true;
3031
3032         /* send all the early keys to the device now */
3033         for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3034                 struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
3035
3036                 if (!key)
3037                         continue;
3038
3039                 mvmvif->ap_early_keys[i] = NULL;
3040
3041                 *ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
3042                 if (*ret)
3043                         return true;
3044         }
3045
3046         if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3047                 iwl_mvm_vif_set_low_latency(mvmvif, true,
3048                                             LOW_LATENCY_VIF_TYPE);
3049                 iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
3050         }
3051
3052         /* power updated needs to be done before quotas */
3053         iwl_mvm_power_update_mac(mvm);
3054
3055         return false;
3056 }
3057
3058 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
3059                                  struct ieee80211_vif *vif,
3060                                  struct ieee80211_bss_conf *link_conf)
3061 {
3062         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3063         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3064         int ret;
3065
3066         mutex_lock(&mvm->mutex);
3067
3068         /*
3069          * Re-calculate the tsf id, as the leader-follower relations depend on
3070          * the beacon interval, which was not known when the AP interface
3071          * was added.
3072          */
3073         if (vif->type == NL80211_IFTYPE_AP)
3074                 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3075
3076         /* For older devices need to send beacon template before adding mac
3077          * context. For the newer, the beacon is a resource that belongs to a
3078          * MAC, so need to send beacon template after adding the mac.
3079          */
3080         if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3081                 /* Add the mac context */
3082                 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3083                 if (ret)
3084                         goto out_unlock;
3085
3086                 /* Send the beacon template */
3087                 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3088                 if (ret)
3089                         goto out_unlock;
3090         } else {
3091                 /* Send the beacon template */
3092                 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3093                 if (ret)
3094                         goto out_unlock;
3095
3096                 /* Add the mac context */
3097                 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3098                 if (ret)
3099                         goto out_unlock;
3100         }
3101
3102         /* Perform the binding */
3103         ret = iwl_mvm_binding_add_vif(mvm, vif);
3104         if (ret)
3105                 goto out_remove;
3106
3107         /*
3108          * This is not very nice, but the simplest:
3109          * For older FWs adding the mcast sta before the bcast station may
3110          * cause assert 0x2b00.
3111          * This is fixed in later FW so make the order of removal depend on
3112          * the TLV
3113          */
3114         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3115                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
3116                 if (ret)
3117                         goto out_unbind;
3118                 /*
3119                  * Send the bcast station. At this stage the TBTT and DTIM time
3120                  * events are added and applied to the scheduler
3121                  */
3122                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3123                 if (ret) {
3124                         iwl_mvm_rm_mcast_sta(mvm, vif);
3125                         goto out_unbind;
3126                 }
3127         } else {
3128                 /*
3129                  * Send the bcast station. At this stage the TBTT and DTIM time
3130                  * events are added and applied to the scheduler
3131                  */
3132                 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3133                 if (ret)
3134                         goto out_unbind;
3135                 ret = iwl_mvm_add_mcast_sta(mvm, vif);
3136                 if (ret) {
3137                         iwl_mvm_send_rm_bcast_sta(mvm, vif);
3138                         goto out_unbind;
3139                 }
3140         }
3141
3142         if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3143                 goto out_failed;
3144
3145         ret = iwl_mvm_update_quotas(mvm, false, NULL);
3146         if (ret)
3147                 goto out_failed;
3148
3149         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3150         if (vif->p2p && mvm->p2p_device_vif)
3151                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3152
3153         iwl_mvm_bt_coex_vif_change(mvm);
3154
3155         /* we don't support TDLS during DCM */
3156         if (iwl_mvm_phy_ctx_count(mvm) > 1)
3157                 iwl_mvm_teardown_tdls_peers(mvm);
3158
3159         iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3160
3161         goto out_unlock;
3162
3163 out_failed:
3164         iwl_mvm_power_update_mac(mvm);
3165         mvmvif->ap_ibss_active = false;
3166         iwl_mvm_send_rm_bcast_sta(mvm, vif);
3167         iwl_mvm_rm_mcast_sta(mvm, vif);
3168 out_unbind:
3169         iwl_mvm_binding_remove_vif(mvm, vif);
3170 out_remove:
3171         iwl_mvm_mac_ctxt_remove(mvm, vif);
3172 out_unlock:
3173         mutex_unlock(&mvm->mutex);
3174         return ret;
3175 }
3176
3177 static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3178                             struct ieee80211_vif *vif,
3179                             struct ieee80211_bss_conf *link_conf)
3180 {
3181         return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3182 }
3183
3184 static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3185                               struct ieee80211_vif *vif)
3186 {
3187         return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3188 }
3189
3190 /* Common part for MLD and non-MLD ops */
3191 void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3192                                  struct ieee80211_vif *vif)
3193 {
3194         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3195
3196         lockdep_assert_held(&mvm->mutex);
3197
3198         iwl_mvm_prepare_mac_removal(mvm, vif);
3199
3200         /* Handle AP stop while in CSA */
3201         if (rcu_access_pointer(mvm->csa_vif) == vif) {
3202                 iwl_mvm_remove_time_event(mvm, mvmvif,
3203                                           &mvmvif->time_event_data);
3204                 RCU_INIT_POINTER(mvm->csa_vif, NULL);
3205                 mvmvif->csa_countdown = false;
3206         }
3207
3208         if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3209                 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3210                 mvm->csa_tx_block_bcn_timeout = 0;
3211         }
3212
3213         mvmvif->ap_ibss_active = false;
3214         mvm->ap_last_beacon_gp2 = 0;
3215
3216         if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3217                 iwl_mvm_vif_set_low_latency(mvmvif, false,
3218                                             LOW_LATENCY_VIF_TYPE);
3219                 iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
3220         }
3221
3222         iwl_mvm_bt_coex_vif_change(mvm);
3223 }
3224
3225 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3226                                  struct ieee80211_vif *vif,
3227                                  struct ieee80211_bss_conf *link_conf)
3228 {
3229         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3230
3231         guard(mvm)(mvm);
3232
3233         iwl_mvm_stop_ap_ibss_common(mvm, vif);
3234
3235         /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3236         if (vif->p2p && mvm->p2p_device_vif)
3237                 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3238
3239         iwl_mvm_update_quotas(mvm, false, NULL);
3240
3241         iwl_mvm_ftm_responder_clear(mvm, vif);
3242
3243         /*
3244          * This is not very nice, but the simplest:
3245          * For older FWs removing the mcast sta before the bcast station may
3246          * cause assert 0x2b00.
3247          * This is fixed in later FW (which will stop beaconing when removing
3248          * bcast station).
3249          * So make the order of removal depend on the TLV
3250          */
3251         if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3252                 iwl_mvm_rm_mcast_sta(mvm, vif);
3253         iwl_mvm_send_rm_bcast_sta(mvm, vif);
3254         if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3255                 iwl_mvm_rm_mcast_sta(mvm, vif);
3256         iwl_mvm_binding_remove_vif(mvm, vif);
3257
3258         iwl_mvm_power_update_mac(mvm);
3259
3260         iwl_mvm_mac_ctxt_remove(mvm, vif);
3261 }
3262
3263 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3264                             struct ieee80211_vif *vif,
3265                             struct ieee80211_bss_conf *link_conf)
3266 {
3267         iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3268 }
3269
3270 static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3271                               struct ieee80211_vif *vif)
3272 {
3273         iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3274 }
3275
3276 static void
3277 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3278                                  struct ieee80211_vif *vif,
3279                                  struct ieee80211_bss_conf *bss_conf,
3280                                  u64 changes)
3281 {
3282         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3283
3284         /* Changes will be applied when the AP/IBSS is started */
3285         if (!mvmvif->ap_ibss_active)
3286                 return;
3287
3288         if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3289                        BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3290             iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3291                 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3292
3293         /* Need to send a new beacon template to the FW */
3294         if (changes & BSS_CHANGED_BEACON &&
3295             iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3296                 IWL_WARN(mvm, "Failed updating beacon data\n");
3297
3298         if (changes & BSS_CHANGED_FTM_RESPONDER) {
3299                 int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3300
3301                 if (ret)
3302                         IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3303                                  ret);
3304         }
3305
3306 }
3307
3308 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3309                                      struct ieee80211_vif *vif,
3310                                      struct ieee80211_bss_conf *bss_conf,
3311                                      u64 changes)
3312 {
3313         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3314
3315         guard(mvm)(mvm);
3316
3317         if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3318                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3319
3320         switch (vif->type) {
3321         case NL80211_IFTYPE_STATION:
3322                 iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3323                 break;
3324         case NL80211_IFTYPE_AP:
3325         case NL80211_IFTYPE_ADHOC:
3326                 iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3327                 break;
3328         case NL80211_IFTYPE_MONITOR:
3329                 if (changes & BSS_CHANGED_MU_GROUPS)
3330                         iwl_mvm_update_mu_groups(mvm, vif);
3331                 break;
3332         default:
3333                 /* shouldn't happen */
3334                 WARN_ON_ONCE(1);
3335         }
3336
3337         if (changes & BSS_CHANGED_TXPOWER) {
3338                 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3339                                 bss_conf->txpower);
3340                 iwl_mvm_set_tx_power(mvm, bss_conf, bss_conf->txpower);
3341         }
3342 }
3343
3344 int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3345                         struct ieee80211_scan_request *hw_req)
3346 {
3347         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3348
3349         if (hw_req->req.n_channels == 0 ||
3350             hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3351                 return -EINVAL;
3352
3353         guard(mvm)(mvm);
3354         return iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3355 }
3356
3357 void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3358                                 struct ieee80211_vif *vif)
3359 {
3360         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3361
3362         guard(mvm)(mvm);
3363
3364         /* Due to a race condition, it's possible that mac80211 asks
3365          * us to stop a hw_scan when it's already stopped.  This can
3366          * happen, for instance, if we stopped the scan ourselves,
3367          * called ieee80211_scan_completed() and the userspace called
3368          * cancel scan scan before ieee80211_scan_work() could run.
3369          * To handle that, simply return if the scan is not running.
3370         */
3371         if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3372                 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3373 }
3374
3375 void
3376 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3377                                   struct ieee80211_sta *sta, u16 tids,
3378                                   int num_frames,
3379                                   enum ieee80211_frame_release_type reason,
3380                                   bool more_data)
3381 {
3382         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3383
3384         /* Called when we need to transmit (a) frame(s) from mac80211 */
3385
3386         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3387                                           tids, more_data, false);
3388 }
3389
3390 void
3391 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3392                                     struct ieee80211_sta *sta, u16 tids,
3393                                     int num_frames,
3394                                     enum ieee80211_frame_release_type reason,
3395                                     bool more_data)
3396 {
3397         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3398
3399         /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3400
3401         iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3402                                           tids, more_data, true);
3403 }
3404
3405 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3406                                      enum sta_notify_cmd cmd,
3407                                      struct ieee80211_sta *sta)
3408 {
3409         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3410         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3411         unsigned long txqs = 0, tids = 0;
3412         int tid;
3413
3414         /*
3415          * If we have TVQM then we get too high queue numbers - luckily
3416          * we really shouldn't get here with that because such hardware
3417          * should have firmware supporting buffer station offload.
3418          */
3419         if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3420                 return;
3421
3422         spin_lock_bh(&mvmsta->lock);
3423         for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3424                 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3425
3426                 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3427                         continue;
3428
3429                 __set_bit(tid_data->txq_id, &txqs);
3430
3431                 if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3432                         continue;
3433
3434                 __set_bit(tid, &tids);
3435         }
3436
3437         switch (cmd) {
3438         case STA_NOTIFY_SLEEP:
3439                 for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3440                         ieee80211_sta_set_buffered(sta, tid, true);
3441
3442                 if (txqs)
3443                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3444                 /*
3445                  * The fw updates the STA to be asleep. Tx packets on the Tx
3446                  * queues to this station will not be transmitted. The fw will
3447                  * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3448                  */
3449                 break;
3450         case STA_NOTIFY_AWAKE:
3451                 if (WARN_ON(mvmsta->deflink.sta_id == IWL_INVALID_STA))
3452                         break;
3453
3454                 if (txqs)
3455                         iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3456                 iwl_mvm_sta_modify_ps_wake(mvm, sta);
3457                 break;
3458         default:
3459                 break;
3460         }
3461         spin_unlock_bh(&mvmsta->lock);
3462 }
3463
3464 void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3465                             enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3466 {
3467         __iwl_mvm_mac_sta_notify(hw, cmd, sta);
3468 }
3469
3470 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3471 {
3472         struct iwl_rx_packet *pkt = rxb_addr(rxb);
3473         struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3474         struct ieee80211_sta *sta;
3475         struct iwl_mvm_sta *mvmsta;
3476         bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3477
3478         if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3479                 return;
3480
3481         rcu_read_lock();
3482         sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3483         if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3484                 rcu_read_unlock();
3485                 return;
3486         }
3487
3488         mvmsta = iwl_mvm_sta_from_mac80211(sta);
3489
3490         if (!mvmsta->vif ||
3491             mvmsta->vif->type != NL80211_IFTYPE_AP) {
3492                 rcu_read_unlock();
3493                 return;
3494         }
3495
3496         if (mvmsta->sleeping != sleeping) {
3497                 mvmsta->sleeping = sleeping;
3498                 __iwl_mvm_mac_sta_notify(mvm->hw,
3499                         sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3500                         sta);
3501                 ieee80211_sta_ps_transition(sta, sleeping);
3502         }
3503
3504         if (sleeping) {
3505                 switch (notif->type) {
3506                 case IWL_MVM_PM_EVENT_AWAKE:
3507                 case IWL_MVM_PM_EVENT_ASLEEP:
3508                         break;
3509                 case IWL_MVM_PM_EVENT_UAPSD:
3510                         ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3511                         break;
3512                 case IWL_MVM_PM_EVENT_PS_POLL:
3513                         ieee80211_sta_pspoll(sta);
3514                         break;
3515                 default:
3516                         break;
3517                 }
3518         }
3519
3520         rcu_read_unlock();
3521 }
3522
3523 void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3524                                 struct ieee80211_vif *vif,
3525                                 struct ieee80211_sta *sta)
3526 {
3527         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3528         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3529         unsigned int link_id;
3530
3531         lockdep_assert_wiphy(mvm->hw->wiphy);
3532
3533         /*
3534          * This is called before mac80211 does RCU synchronisation,
3535          * so here we already invalidate our internal RCU-protected
3536          * station pointer. The rest of the code will thus no longer
3537          * be able to find the station this way, and we don't rely
3538          * on further RCU synchronisation after the sta_state()
3539          * callback deleted the station.
3540          * Since there's mvm->mutex here, no need to have RCU lock for
3541          * mvm_sta->link access.
3542          */
3543         guard(mvm)(mvm);
3544         for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3545                 struct iwl_mvm_link_sta *link_sta;
3546                 u32 sta_id;
3547
3548                 if (!mvm_sta->link[link_id])
3549                         continue;
3550
3551                 link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3552                                                      lockdep_is_held(&mvm->mutex));
3553                 sta_id = link_sta->sta_id;
3554                 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3555                         RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3556                                          ERR_PTR(-ENOENT));
3557                         RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3558                 }
3559         }
3560 }
3561
3562 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3563                                 const u8 *bssid)
3564 {
3565         int i;
3566
3567         if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3568                 struct iwl_mvm_tcm_mac *mdata;
3569
3570                 mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3571                 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3572                 mdata->opened_rx_ba_sessions = false;
3573         }
3574
3575         if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3576                 return;
3577
3578         if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3579                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3580                 return;
3581         }
3582
3583         if (!vif->p2p &&
3584             (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3585                 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3586                 return;
3587         }
3588
3589         for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3590                 if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3591                         vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3592                         return;
3593                 }
3594         }
3595
3596         vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3597 }
3598
3599 static void
3600 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3601                            struct ieee80211_vif *vif, u8 *peer_addr,
3602                            enum nl80211_tdls_operation action)
3603 {
3604         struct iwl_fw_dbg_trigger_tlv *trig;
3605         struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3606
3607         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3608                                      FW_DBG_TRIGGER_TDLS);
3609         if (!trig)
3610                 return;
3611
3612         tdls_trig = (void *)trig->data;
3613
3614         if (!(tdls_trig->action_bitmap & BIT(action)))
3615                 return;
3616
3617         if (tdls_trig->peer_mode &&
3618             memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3619                 return;
3620
3621         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3622                                 "TDLS event occurred, peer %pM, action %d",
3623                                 peer_addr, action);
3624 }
3625
3626 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3627         bool tolerated;
3628 };
3629
3630 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3631                                                     struct cfg80211_bss *bss,
3632                                                     void *_data)
3633 {
3634         struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3635         const struct cfg80211_bss_ies *ies;
3636         const struct element *elem;
3637
3638         rcu_read_lock();
3639         ies = rcu_dereference(bss->ies);
3640         elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3641                                   ies->len);
3642
3643         if (!elem || elem->datalen < 10 ||
3644             !(elem->data[10] &
3645               WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3646                 data->tolerated = false;
3647         }
3648         rcu_read_unlock();
3649 }
3650
3651 static void
3652 iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3653                                    struct ieee80211_vif *vif,
3654                                    unsigned int link_id,
3655                                    struct ieee80211_bss_conf *link_conf)
3656 {
3657         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3658         struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3659                 .tolerated = true,
3660         };
3661
3662         if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
3663                          !mvmvif->link[link_id]))
3664                 return;
3665
3666         if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
3667                 mvmvif->link[link_id]->he_ru_2mhz_block = false;
3668                 return;
3669         }
3670
3671         cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
3672                           iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3673                           &iter_data);
3674
3675         /*
3676          * If there is at least one AP on radar channel that cannot
3677          * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3678          */
3679         mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3680 }
3681
3682 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3683                                                struct ieee80211_vif *vif)
3684 {
3685         struct ieee80211_supported_band *sband;
3686         const struct ieee80211_sta_he_cap *he_cap;
3687
3688         if (vif->type != NL80211_IFTYPE_STATION)
3689                 return;
3690
3691         if (!mvm->cca_40mhz_workaround)
3692                 return;
3693
3694         /* decrement and check that we reached zero */
3695         mvm->cca_40mhz_workaround--;
3696         if (mvm->cca_40mhz_workaround)
3697                 return;
3698
3699         sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3700
3701         sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3702
3703         he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3704
3705         if (he_cap) {
3706                 /* we know that ours is writable */
3707                 struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3708
3709                 he->he_cap_elem.phy_cap_info[0] |=
3710                         IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3711         }
3712 }
3713
3714 static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3715                                         struct ieee80211_vif *vif,
3716                                         struct iwl_mvm_sta *mvm_sta)
3717 {
3718 #if IS_ENABLED(CONFIG_IWLMEI)
3719         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3720         struct iwl_mei_conn_info conn_info = {
3721                 .ssid_len = vif->cfg.ssid_len,
3722         };
3723
3724         if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3725                 return;
3726
3727         if (!mvm->mei_registered)
3728                 return;
3729
3730         /* FIXME: MEI needs to be updated for MLO */
3731         if (!vif->bss_conf.chanreq.oper.chan)
3732                 return;
3733
3734         conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
3735
3736         switch (mvm_sta->pairwise_cipher) {
3737         case WLAN_CIPHER_SUITE_TKIP:
3738                 conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3739                 break;
3740         case WLAN_CIPHER_SUITE_CCMP:
3741                 conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3742                 break;
3743         case WLAN_CIPHER_SUITE_GCMP:
3744                 conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3745                 break;
3746         case WLAN_CIPHER_SUITE_GCMP_256:
3747                 conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3748                 break;
3749         case 0:
3750                 /* open profile */
3751                 break;
3752         default:
3753                 /* cipher not supported, don't send anything to iwlmei */
3754                 return;
3755         }
3756
3757         switch (mvmvif->rekey_data.akm) {
3758         case WLAN_AKM_SUITE_SAE & 0xff:
3759                 conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3760                 break;
3761         case WLAN_AKM_SUITE_PSK & 0xff:
3762                 conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3763                 break;
3764         case WLAN_AKM_SUITE_8021X & 0xff:
3765                 conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3766                 break;
3767         case 0:
3768                 /* open profile */
3769                 conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3770                 break;
3771         default:
3772                 /* auth method / AKM not supported */
3773                 /* TODO: All the FT vesions of these? */
3774                 return;
3775         }
3776
3777         memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3778         memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3779
3780         /* TODO: add support for collocated AP data */
3781         iwl_mei_host_associated(&conn_info, NULL);
3782 #endif
3783 }
3784
3785 static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3786                                             struct ieee80211_vif *vif,
3787                                             bool force_assoc_off)
3788 {
3789         return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3790 }
3791
3792 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3793                                  struct ieee80211_vif *vif,
3794                                  struct ieee80211_sta *sta,
3795                                  enum ieee80211_sta_state old_state,
3796                                  enum ieee80211_sta_state new_state)
3797 {
3798         static const struct iwl_mvm_sta_state_ops callbacks = {
3799                 .add_sta = iwl_mvm_add_sta,
3800                 .update_sta = iwl_mvm_update_sta,
3801                 .rm_sta = iwl_mvm_rm_sta,
3802                 .mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3803         };
3804
3805         return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3806                                             &callbacks);
3807 }
3808
3809 /* FIXME: temporary making two assumptions in all sta handling functions:
3810  *      (1) when setting sta state, the link exists and protected
3811  *      (2) if a link is valid in sta then it's valid in vif (can
3812  *      use same index in the link array)
3813  */
3814 static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3815                                            struct ieee80211_vif *vif,
3816                                            struct ieee80211_sta *sta)
3817 {
3818         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3819         unsigned int link_id;
3820
3821         for_each_mvm_vif_valid_link(mvmvif, link_id) {
3822                 struct ieee80211_bss_conf *conf =
3823                         link_conf_dereference_check(vif, link_id);
3824                 struct ieee80211_link_sta *link_sta =
3825                         link_sta_dereference_check(sta, link_id);
3826
3827                 if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3828                         continue;
3829
3830                 iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3831                                      mvmvif->link[link_id]->phy_ctxt->channel->band);
3832         }
3833 }
3834
3835 static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3836                                       struct ieee80211_vif *vif,
3837                                       struct ieee80211_sta *sta)
3838 {
3839         struct ieee80211_link_sta *link_sta;
3840         unsigned int link_id;
3841
3842         /* Beacon interval check - firmware will crash if the beacon
3843          * interval is less than 16. We can't avoid connecting at all,
3844          * so refuse the station state change, this will cause mac80211
3845          * to abandon attempts to connect to this AP, and eventually
3846          * wpa_s will blocklist the AP...
3847          */
3848
3849         for_each_sta_active_link(vif, sta, link_sta, link_id) {
3850                 struct ieee80211_bss_conf *link_conf =
3851                         link_conf_dereference_protected(vif, link_id);
3852
3853                 if (!link_conf)
3854                         continue;
3855
3856                 if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3857                         IWL_ERR(mvm,
3858                                 "Beacon interval %d for AP %pM is too small\n",
3859                                 link_conf->beacon_int, link_sta->addr);
3860                         return false;
3861                 }
3862
3863                 link_conf->he_support = link_sta->he_cap.has_he;
3864         }
3865
3866         return true;
3867 }
3868
3869 static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3870                                        struct ieee80211_vif *vif,
3871                                        struct ieee80211_sta *sta,
3872                                        bool is_sta)
3873 {
3874         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3875         struct ieee80211_link_sta *link_sta;
3876         unsigned int link_id;
3877
3878         for_each_sta_active_link(vif, sta, link_sta, link_id) {
3879                 struct ieee80211_bss_conf *link_conf =
3880                         link_conf_dereference_protected(vif, link_id);
3881
3882                 if (!link_conf || !mvmvif->link[link_id])
3883                         continue;
3884
3885                 link_conf->he_support = link_sta->he_cap.has_he;
3886
3887                 if (is_sta) {
3888                         mvmvif->link[link_id]->he_ru_2mhz_block = false;
3889                         if (link_sta->he_cap.has_he)
3890                                 iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3891                                                                    link_id,
3892                                                                    link_conf);
3893                 }
3894         }
3895 }
3896
3897 static int
3898 iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3899                                    struct ieee80211_vif *vif,
3900                                    struct ieee80211_sta *sta,
3901                                    const struct iwl_mvm_sta_state_ops *callbacks)
3902 {
3903         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3904         struct ieee80211_link_sta *link_sta;
3905         unsigned int i;
3906         int ret;
3907
3908         lockdep_assert_held(&mvm->mutex);
3909
3910         if (vif->type == NL80211_IFTYPE_STATION &&
3911             !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3912                 return -EINVAL;
3913
3914         if (sta->tdls &&
3915             (vif->p2p ||
3916              iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
3917              iwl_mvm_phy_ctx_count(mvm) > 1)) {
3918                 IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3919                 return -EBUSY;
3920         }
3921
3922         ret = callbacks->add_sta(mvm, vif, sta);
3923         if (sta->tdls && ret == 0) {
3924                 iwl_mvm_recalc_tdls_state(mvm, vif, true);
3925                 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3926                                            NL80211_TDLS_SETUP);
3927         }
3928
3929         if (ret)
3930                 return ret;
3931
3932         for_each_sta_active_link(vif, sta, link_sta, i)
3933                 link_sta->agg.max_rc_amsdu_len = 1;
3934
3935         ieee80211_sta_recalc_aggregates(sta);
3936
3937         if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3938                 mvmvif->ap_sta = sta;
3939
3940         /*
3941          * Initialize the rates here already - this really tells
3942          * the firmware only what the supported legacy rates are
3943          * (may be) since it's initialized already from what the
3944          * AP advertised in the beacon/probe response. This will
3945          * allow the firmware to send auth/assoc frames with one
3946          * of the supported rates already, rather than having to
3947          * use a mandatory rate.
3948          * If we're the AP, we'll just assume mandatory rates at
3949          * this point, but we know nothing about the STA anyway.
3950          */
3951         iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3952
3953         return 0;
3954 }
3955
3956 static int
3957 iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3958                                 struct iwl_mvm *mvm,
3959                                 struct ieee80211_vif *vif,
3960                                 struct ieee80211_sta *sta,
3961                                 const struct iwl_mvm_sta_state_ops *callbacks)
3962 {
3963         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3964         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3965         struct ieee80211_link_sta *link_sta;
3966         unsigned int link_id;
3967
3968         lockdep_assert_held(&mvm->mutex);
3969
3970         if (vif->type == NL80211_IFTYPE_AP) {
3971                 iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3972                 mvmvif->ap_assoc_sta_count++;
3973                 callbacks->mac_ctxt_changed(mvm, vif, false);
3974
3975                 /* since the below is not for MLD API, it's ok to use
3976                  * the default bss_conf
3977                  */
3978                 if (!mvm->mld_api_is_used &&
3979                     (vif->bss_conf.he_support &&
3980                      !iwlwifi_mod_params.disable_11ax))
3981                         iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3982         } else if (vif->type == NL80211_IFTYPE_STATION) {
3983                 iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3984
3985                 callbacks->mac_ctxt_changed(mvm, vif, false);
3986
3987                 if (!mvm->mld_api_is_used)
3988                         goto out;
3989
3990                 for_each_sta_active_link(vif, sta, link_sta, link_id) {
3991                         struct ieee80211_bss_conf *link_conf =
3992                                 link_conf_dereference_protected(vif, link_id);
3993
3994                         if (WARN_ON(!link_conf))
3995                                 return -EINVAL;
3996                         if (!mvmvif->link[link_id])
3997                                 continue;
3998
3999                         iwl_mvm_link_changed(mvm, vif, link_conf,
4000                                              LINK_CONTEXT_MODIFY_ALL &
4001                                              ~LINK_CONTEXT_MODIFY_ACTIVE,
4002                                              true);
4003                 }
4004         }
4005
4006 out:
4007         iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4008
4009         return callbacks->update_sta(mvm, vif, sta);
4010 }
4011
4012 static int
4013 iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
4014                                       struct ieee80211_vif *vif,
4015                                       struct ieee80211_sta *sta,
4016                                       const struct iwl_mvm_sta_state_ops *callbacks)
4017 {
4018         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4019         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4020
4021         lockdep_assert_held(&mvm->mutex);
4022
4023         /* we don't support TDLS during DCM */
4024         if (iwl_mvm_phy_ctx_count(mvm) > 1)
4025                 iwl_mvm_teardown_tdls_peers(mvm);
4026
4027         if (sta->tdls) {
4028                 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4029                                            NL80211_TDLS_ENABLE_LINK);
4030         } else {
4031                 /* enable beacon filtering */
4032                 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
4033
4034                 mvmvif->authorized = 1;
4035
4036                 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
4037                         mvmvif->link_selection_res = vif->active_links;
4038                         mvmvif->link_selection_primary =
4039                                 vif->active_links ? __ffs(vif->active_links) : 0;
4040                 }
4041
4042                 callbacks->mac_ctxt_changed(mvm, vif, false);
4043                 iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
4044
4045                 memset(&mvmvif->last_esr_exit, 0,
4046                        sizeof(mvmvif->last_esr_exit));
4047
4048                 iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
4049
4050                 /* Block until FW notif will arrive */
4051                 iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
4052
4053                 /* when client is authorized (AP station marked as such),
4054                  * try to enable the best link(s).
4055                  */
4056                 if (vif->type == NL80211_IFTYPE_STATION &&
4057                     !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4058                         iwl_mvm_select_links(mvm, vif);
4059         }
4060
4061         mvm_sta->authorized = true;
4062
4063         /* MFP is set by default before the station is authorized.
4064          * Clear it here in case it's not used.
4065          */
4066         if (!sta->mfp) {
4067                 int ret = callbacks->update_sta(mvm, vif, sta);
4068
4069                 if (ret)
4070                         return ret;
4071         }
4072
4073         iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4074
4075         return 0;
4076 }
4077
4078 static int
4079 iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
4080                                       struct ieee80211_vif *vif,
4081                                       struct ieee80211_sta *sta,
4082                                       const struct iwl_mvm_sta_state_ops *callbacks)
4083 {
4084         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4085         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4086
4087         lockdep_assert_held(&mvm->mutex);
4088
4089         mvmsta->authorized = false;
4090
4091         /* once we move into assoc state, need to update rate scale to
4092          * disable using wide bandwidth
4093          */
4094         iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4095
4096         if (!sta->tdls) {
4097                 /* Set this but don't call iwl_mvm_mac_ctxt_changed()
4098                  * yet to avoid sending high prio again for a little
4099                  * time.
4100                  */
4101                 mvmvif->authorized = 0;
4102                 mvmvif->link_selection_res = 0;
4103
4104                 /* disable beacon filtering */
4105                 iwl_mvm_disable_beacon_filter(mvm, vif);
4106
4107                 wiphy_delayed_work_cancel(mvm->hw->wiphy,
4108                                           &mvmvif->prevent_esr_done_wk);
4109
4110                 wiphy_delayed_work_cancel(mvm->hw->wiphy,
4111                                           &mvmvif->mlo_int_scan_wk);
4112
4113                 wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4114                 wiphy_delayed_work_cancel(mvm->hw->wiphy,
4115                                           &mvmvif->unblock_esr_tmp_non_bss_wk);
4116
4117                 /* No need for the periodic statistics anymore */
4118                 if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
4119                         iwl_mvm_request_periodic_system_statistics(mvm, false);
4120         }
4121
4122         return 0;
4123 }
4124
4125 /* Common part for MLD and non-MLD modes */
4126 int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
4127                                  struct ieee80211_vif *vif,
4128                                  struct ieee80211_sta *sta,
4129                                  enum ieee80211_sta_state old_state,
4130                                  enum ieee80211_sta_state new_state,
4131                                  const struct iwl_mvm_sta_state_ops *callbacks)
4132 {
4133         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4134         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4135         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4136         struct ieee80211_link_sta *link_sta;
4137         unsigned int link_id;
4138         int ret;
4139
4140         IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4141                            sta->addr, old_state, new_state);
4142
4143         /*
4144          * If we are in a STA removal flow and in DQA mode:
4145          *
4146          * This is after the sync_rcu part, so the queues have already been
4147          * flushed. No more TXs on their way in mac80211's path, and no more in
4148          * the queues.
4149          * Also, we won't be getting any new TX frames for this station.
4150          * What we might have are deferred TX frames that need to be taken care
4151          * of.
4152          *
4153          * Drop any still-queued deferred-frame before removing the STA, and
4154          * make sure the worker is no longer handling frames for this STA.
4155          */
4156         if (old_state == IEEE80211_STA_NONE &&
4157             new_state == IEEE80211_STA_NOTEXIST) {
4158                 flush_work(&mvm->add_stream_wk);
4159
4160                 /*
4161                  * No need to make sure deferred TX indication is off since the
4162                  * worker will already remove it if it was on
4163                  */
4164
4165                 /*
4166                  * Additionally, reset the 40 MHz capability if we disconnected
4167                  * from the AP now.
4168                  */
4169                 iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4170
4171                 /* Also free dup data just in case any assertions below fail */
4172                 kfree(mvm_sta->dup_data);
4173         }
4174
4175         mutex_lock(&mvm->mutex);
4176
4177         /* this would be a mac80211 bug ... but don't crash, unless we had a
4178          * firmware crash while we were activating a link, in which case it is
4179          * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4180          * recovery flow since we spit tons of error messages anyway.
4181          */
4182         for_each_sta_active_link(vif, sta, link_sta, link_id) {
4183                 if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
4184                                  !mvmvif->link[link_id]->phy_ctxt)) {
4185                         mutex_unlock(&mvm->mutex);
4186                         return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4187                                         &mvm->status) ? 0 : -EINVAL;
4188                 }
4189         }
4190
4191         /* track whether or not the station is associated */
4192         mvm_sta->sta_state = new_state;
4193
4194         if (old_state == IEEE80211_STA_NOTEXIST &&
4195             new_state == IEEE80211_STA_NONE) {
4196                 ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
4197                                                          callbacks);
4198                 if (ret < 0)
4199                         goto out_unlock;
4200         } else if (old_state == IEEE80211_STA_NONE &&
4201                    new_state == IEEE80211_STA_AUTH) {
4202                 /*
4203                  * EBS may be disabled due to previous failures reported by FW.
4204                  * Reset EBS status here assuming environment has been changed.
4205                  */
4206                 mvm->last_ebs_successful = true;
4207                 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4208                 ret = 0;
4209         } else if (old_state == IEEE80211_STA_AUTH &&
4210                    new_state == IEEE80211_STA_ASSOC) {
4211                 ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
4212                                                       callbacks);
4213         } else if (old_state == IEEE80211_STA_ASSOC &&
4214                    new_state == IEEE80211_STA_AUTHORIZED) {
4215                 ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
4216                                                             callbacks);
4217         } else if (old_state == IEEE80211_STA_AUTHORIZED &&
4218                    new_state == IEEE80211_STA_ASSOC) {
4219                 ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
4220                                                             callbacks);
4221         } else if (old_state == IEEE80211_STA_ASSOC &&
4222                    new_state == IEEE80211_STA_AUTH) {
4223                 if (vif->type == NL80211_IFTYPE_AP) {
4224                         mvmvif->ap_assoc_sta_count--;
4225                         callbacks->mac_ctxt_changed(mvm, vif, false);
4226                 } else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4227                         iwl_mvm_stop_session_protection(mvm, vif);
4228                 ret = 0;
4229         } else if (old_state == IEEE80211_STA_AUTH &&
4230                    new_state == IEEE80211_STA_NONE) {
4231                 ret = 0;
4232         } else if (old_state == IEEE80211_STA_NONE &&
4233                    new_state == IEEE80211_STA_NOTEXIST) {
4234                 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4235                         iwl_mvm_stop_session_protection(mvm, vif);
4236                         mvmvif->ap_sta = NULL;
4237                 }
4238                 ret = callbacks->rm_sta(mvm, vif, sta);
4239                 if (sta->tdls) {
4240                         iwl_mvm_recalc_tdls_state(mvm, vif, false);
4241                         iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4242                                                    NL80211_TDLS_DISABLE_LINK);
4243                 }
4244
4245                 if (unlikely(ret &&
4246                              test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4247                                       &mvm->status)))
4248                         ret = 0;
4249         } else {
4250                 ret = -EIO;
4251         }
4252  out_unlock:
4253         mutex_unlock(&mvm->mutex);
4254
4255         if (sta->tdls && ret == 0) {
4256                 if (old_state == IEEE80211_STA_NOTEXIST &&
4257                     new_state == IEEE80211_STA_NONE)
4258                         ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4259                 else if (old_state == IEEE80211_STA_NONE &&
4260                          new_state == IEEE80211_STA_NOTEXIST)
4261                         ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4262         }
4263
4264         return ret;
4265 }
4266
4267 int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4268 {
4269         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4270
4271         mvm->rts_threshold = value;
4272
4273         return 0;
4274 }
4275
4276 void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4277                            struct ieee80211_link_sta *link_sta, u32 changed)
4278 {
4279         struct ieee80211_sta *sta = link_sta->sta;
4280         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4281
4282         if (changed & (IEEE80211_RC_BW_CHANGED |
4283                        IEEE80211_RC_SUPP_RATES_CHANGED |
4284                        IEEE80211_RC_NSS_CHANGED))
4285                 iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4286
4287         if (vif->type == NL80211_IFTYPE_STATION &&
4288             changed & IEEE80211_RC_NSS_CHANGED)
4289                 iwl_mvm_sf_update(mvm, vif, false);
4290 }
4291
4292 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4293                                struct ieee80211_vif *vif,
4294                                unsigned int link_id, u16 ac,
4295                                const struct ieee80211_tx_queue_params *params)
4296 {
4297         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4298         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4299
4300         mvmvif->deflink.queue_params[ac] = *params;
4301
4302         /*
4303          * No need to update right away, we'll get BSS_CHANGED_QOS
4304          * The exception is P2P_DEVICE interface which needs immediate update.
4305          */
4306         if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4307                 guard(mvm)(mvm);
4308                 return iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4309         }
4310         return 0;
4311 }
4312
4313 void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4314                                 struct ieee80211_vif *vif,
4315                                 struct ieee80211_prep_tx_info *info)
4316 {
4317         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4318         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4319
4320         if (info->was_assoc && !mvmvif->session_prot_connection_loss)
4321                 return;
4322
4323         guard(mvm)(mvm);
4324         iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4325 }
4326
4327 void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4328                                  struct ieee80211_vif *vif,
4329                                  struct ieee80211_prep_tx_info *info)
4330 {
4331         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4332
4333         /* for successful cases (auth/assoc), don't cancel session protection */
4334         if (info->success)
4335                 return;
4336
4337         guard(mvm)(mvm);
4338         iwl_mvm_stop_session_protection(mvm, vif);
4339 }
4340
4341 int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4342                                  struct ieee80211_vif *vif,
4343                                  struct cfg80211_sched_scan_request *req,
4344                                  struct ieee80211_scan_ies *ies)
4345 {
4346         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4347
4348         guard(mvm)(mvm);
4349
4350         if (!vif->cfg.idle)
4351                 return -EBUSY;
4352
4353         return iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4354 }
4355
4356 int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4357                                 struct ieee80211_vif *vif)
4358 {
4359         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4360         int ret;
4361
4362         mutex_lock(&mvm->mutex);
4363
4364         /* Due to a race condition, it's possible that mac80211 asks
4365          * us to stop a sched_scan when it's already stopped.  This
4366          * can happen, for instance, if we stopped the scan ourselves,
4367          * called ieee80211_sched_scan_stopped() and the userspace called
4368          * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4369          * could run.  To handle this, simply return if the scan is
4370          * not running.
4371         */
4372         if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4373                 mutex_unlock(&mvm->mutex);
4374                 return 0;
4375         }
4376
4377         ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4378         mutex_unlock(&mvm->mutex);
4379         iwl_mvm_wait_for_async_handlers(mvm);
4380
4381         return ret;
4382 }
4383
4384 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4385                                  enum set_key_cmd cmd,
4386                                  struct ieee80211_vif *vif,
4387                                  struct ieee80211_sta *sta,
4388                                  struct ieee80211_key_conf *key)
4389 {
4390         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4391         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4392         struct iwl_mvm_sta *mvmsta = NULL;
4393         struct iwl_mvm_key_pn *ptk_pn = NULL;
4394         int keyidx = key->keyidx;
4395         u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4396         u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4397         int ret, i;
4398         u8 key_offset;
4399
4400         if (sta)
4401                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
4402
4403         switch (key->cipher) {
4404         case WLAN_CIPHER_SUITE_TKIP:
4405                 if (!mvm->trans->trans_cfg->gen2) {
4406                         key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4407                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4408                 } else if (vif->type == NL80211_IFTYPE_STATION) {
4409                         key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4410                 } else {
4411                         IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4412                         return -EOPNOTSUPP;
4413                 }
4414                 break;
4415         case WLAN_CIPHER_SUITE_CCMP:
4416         case WLAN_CIPHER_SUITE_GCMP:
4417         case WLAN_CIPHER_SUITE_GCMP_256:
4418                 if (!iwl_mvm_has_new_tx_api(mvm))
4419                         key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4420                 break;
4421         case WLAN_CIPHER_SUITE_AES_CMAC:
4422         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4423         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4424                 WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4425                 break;
4426         case WLAN_CIPHER_SUITE_WEP40:
4427         case WLAN_CIPHER_SUITE_WEP104:
4428                 if (vif->type == NL80211_IFTYPE_STATION)
4429                         break;
4430                 if (iwl_mvm_has_new_tx_api(mvm))
4431                         return -EOPNOTSUPP;
4432                 /* support HW crypto on TX */
4433                 return 0;
4434         default:
4435                 return -EOPNOTSUPP;
4436         }
4437
4438         switch (cmd) {
4439         case SET_KEY:
4440                 if (vif->type == NL80211_IFTYPE_STATION &&
4441                     (keyidx == 6 || keyidx == 7))
4442                         rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4443                                            key);
4444
4445                 if ((vif->type == NL80211_IFTYPE_ADHOC ||
4446                      vif->type == NL80211_IFTYPE_AP) && !sta) {
4447                         /*
4448                          * GTK on AP interface is a TX-only key, return 0;
4449                          * on IBSS they're per-station and because we're lazy
4450                          * we don't support them for RX, so do the same.
4451                          * CMAC/GMAC in AP/IBSS modes must be done in software
4452                          * on older NICs.
4453                          *
4454                          * Except, of course, beacon protection - it must be
4455                          * offloaded since we just set a beacon template, and
4456                          * then we must also offload the IGTK (not just BIGTK)
4457                          * for firmware reasons.
4458                          *
4459                          * So just check for beacon protection - if we don't
4460                          * have it we cannot get here with keyidx >= 6, and
4461                          * if we do have it we need to send the key to FW in
4462                          * all cases (CMAC/GMAC).
4463                          */
4464                         if (!wiphy_ext_feature_isset(hw->wiphy,
4465                                                      NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4466                             (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4467                              key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4468                              key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4469                                 ret = -EOPNOTSUPP;
4470                                 break;
4471                         }
4472
4473                         if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4474                             key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4475                             !iwl_mvm_has_new_tx_api(mvm)) {
4476                                 key->hw_key_idx = STA_KEY_IDX_INVALID;
4477                                 ret = 0;
4478                                 break;
4479                         }
4480
4481                         if (!mvmvif->ap_ibss_active) {
4482                                 for (i = 0;
4483                                      i < ARRAY_SIZE(mvmvif->ap_early_keys);
4484                                      i++) {
4485                                         if (!mvmvif->ap_early_keys[i]) {
4486                                                 mvmvif->ap_early_keys[i] = key;
4487                                                 break;
4488                                         }
4489                                 }
4490
4491                                 if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4492                                         ret = -ENOSPC;
4493                                 else
4494                                         ret = 0;
4495
4496                                 break;
4497                         }
4498                 }
4499
4500                 /* During FW restart, in order to restore the state as it was,
4501                  * don't try to reprogram keys we previously failed for.
4502                  */
4503                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4504                     key->hw_key_idx == STA_KEY_IDX_INVALID) {
4505                         IWL_DEBUG_MAC80211(mvm,
4506                                            "skip invalid idx key programming during restart\n");
4507                         ret = 0;
4508                         break;
4509                 }
4510
4511                 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4512                     mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4513                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4514                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4515                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4516                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4517                         struct ieee80211_key_seq seq;
4518                         int tid, q;
4519
4520                         WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4521                         ptk_pn = kzalloc(struct_size(ptk_pn, q,
4522                                                      mvm->trans->num_rx_queues),
4523                                          GFP_KERNEL);
4524                         if (!ptk_pn) {
4525                                 ret = -ENOMEM;
4526                                 break;
4527                         }
4528
4529                         for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4530                                 ieee80211_get_key_rx_seq(key, tid, &seq);
4531                                 for (q = 0; q < mvm->trans->num_rx_queues; q++)
4532                                         memcpy(ptk_pn->q[q].pn[tid],
4533                                                seq.ccmp.pn,
4534                                                IEEE80211_CCMP_PN_LEN);
4535                         }
4536
4537                         rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4538                 }
4539
4540                 /* in HW restart reuse the index, otherwise request a new one */
4541                 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4542                         key_offset = key->hw_key_idx;
4543                 else
4544                         key_offset = STA_KEY_IDX_INVALID;
4545
4546                 if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4547                         mvmsta->pairwise_cipher = key->cipher;
4548
4549                 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4550                                    sta ? sta->addr : NULL, key->keyidx);
4551
4552                 if (sec_key_ver)
4553                         ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4554                 else
4555                         ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4556
4557                 if (ret) {
4558                         IWL_WARN(mvm, "set key failed\n");
4559                         key->hw_key_idx = STA_KEY_IDX_INVALID;
4560                         if (ptk_pn) {
4561                                 RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4562                                 kfree(ptk_pn);
4563                         }
4564                         /*
4565                          * can't add key for RX, but we don't need it
4566                          * in the device for TX so still return 0,
4567                          * unless we have new TX API where we cannot
4568                          * put key material into the TX_CMD
4569                          */
4570                         if (iwl_mvm_has_new_tx_api(mvm))
4571                                 ret = -EOPNOTSUPP;
4572                         else
4573                                 ret = 0;
4574                 }
4575
4576                 break;
4577         case DISABLE_KEY:
4578                 if (vif->type == NL80211_IFTYPE_STATION &&
4579                     (keyidx == 6 || keyidx == 7))
4580                         RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4581                                          NULL);
4582
4583                 ret = -ENOENT;
4584                 for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4585                         if (mvmvif->ap_early_keys[i] == key) {
4586                                 mvmvif->ap_early_keys[i] = NULL;
4587                                 ret = 0;
4588                         }
4589                 }
4590
4591                 /* found in pending list - don't do anything else */
4592                 if (ret == 0)
4593                         break;
4594
4595                 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4596                         ret = 0;
4597                         break;
4598                 }
4599
4600                 if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4601                     key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4602                     (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4603                      key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4604                      key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4605                         ptk_pn = rcu_dereference_protected(
4606                                                 mvmsta->ptk_pn[keyidx],
4607                                                 lockdep_is_held(&mvm->mutex));
4608                         RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4609                         if (ptk_pn)
4610                                 kfree_rcu(ptk_pn, rcu_head);
4611                 }
4612
4613                 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4614                 if (sec_key_ver)
4615                         ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4616                 else
4617                         ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4618                 break;
4619         default:
4620                 ret = -EINVAL;
4621         }
4622
4623         return ret;
4624 }
4625
4626 int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4627                         struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4628                         struct ieee80211_key_conf *key)
4629 {
4630         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4631
4632         guard(mvm)(mvm);
4633         return __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4634 }
4635
4636 void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4637                                  struct ieee80211_vif *vif,
4638                                  struct ieee80211_key_conf *keyconf,
4639                                  struct ieee80211_sta *sta,
4640                                  u32 iv32, u16 *phase1key)
4641 {
4642         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4643
4644         if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4645                 return;
4646
4647         iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4648 }
4649
4650
4651 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4652                                struct iwl_rx_packet *pkt, void *data)
4653 {
4654         struct iwl_mvm *mvm =
4655                 container_of(notif_wait, struct iwl_mvm, notif_wait);
4656         struct iwl_hs20_roc_res *resp;
4657         int resp_len = iwl_rx_packet_payload_len(pkt);
4658         struct iwl_mvm_time_event_data *te_data = data;
4659
4660         if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4661                 return true;
4662
4663         if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4664                 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4665                 return true;
4666         }
4667
4668         resp = (void *)pkt->data;
4669
4670         IWL_DEBUG_TE(mvm,
4671                      "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4672                      resp->status, resp->event_unique_id);
4673
4674         te_data->uid = le32_to_cpu(resp->event_unique_id);
4675         IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4676                      te_data->uid);
4677
4678         spin_lock_bh(&mvm->time_event_lock);
4679         list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4680         spin_unlock_bh(&mvm->time_event_lock);
4681
4682         return true;
4683 }
4684
4685 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4686                                     struct ieee80211_channel *channel,
4687                                     struct ieee80211_vif *vif,
4688                                     int duration)
4689 {
4690         int res;
4691         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4692         struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4693         static const u16 time_event_response[] = { HOT_SPOT_CMD };
4694         struct iwl_notification_wait wait_time_event;
4695         u32 req_dur, delay;
4696         struct iwl_hs20_roc_req aux_roc_req = {
4697                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4698                 .id_and_color =
4699                         cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4700                 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4701         };
4702         struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4703                 &aux_roc_req.channel_info);
4704         u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4705
4706         /* Set the channel info data */
4707         iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4708                               iwl_mvm_phy_band_from_nl80211(channel->band),
4709                               IWL_PHY_CHANNEL_MODE20,
4710                               0);
4711
4712         /* Set the time and duration */
4713         tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4714
4715         iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
4716         tail->duration = cpu_to_le32(req_dur);
4717         tail->apply_time_max_delay = cpu_to_le32(delay);
4718
4719         IWL_DEBUG_TE(mvm,
4720                      "ROC: Requesting to remain on channel %u for %ums\n",
4721                      channel->hw_value, req_dur);
4722         IWL_DEBUG_TE(mvm,
4723                      "\t(requested = %ums, max_delay = %ums)\n",
4724                      duration, delay);
4725
4726         /* Set the node address */
4727         memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4728
4729         lockdep_assert_held(&mvm->mutex);
4730
4731         spin_lock_bh(&mvm->time_event_lock);
4732
4733         if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4734                 spin_unlock_bh(&mvm->time_event_lock);
4735                 return -EIO;
4736         }
4737
4738         te_data->vif = vif;
4739         te_data->duration = duration;
4740         te_data->id = HOT_SPOT_CMD;
4741
4742         spin_unlock_bh(&mvm->time_event_lock);
4743
4744         /*
4745          * Use a notification wait, which really just processes the
4746          * command response and doesn't wait for anything, in order
4747          * to be able to process the response and get the UID inside
4748          * the RX path. Using CMD_WANT_SKB doesn't work because it
4749          * stores the buffer and then wakes up this thread, by which
4750          * time another notification (that the time event started)
4751          * might already be processed unsuccessfully.
4752          */
4753         iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4754                                    time_event_response,
4755                                    ARRAY_SIZE(time_event_response),
4756                                    iwl_mvm_rx_aux_roc, te_data);
4757
4758         res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4759                                    &aux_roc_req);
4760
4761         if (res) {
4762                 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4763                 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4764                 goto out_clear_te;
4765         }
4766
4767         /* No need to wait for anything, so just pass 1 (0 isn't valid) */
4768         res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4769         /* should never fail */
4770         WARN_ON_ONCE(res);
4771
4772         if (res) {
4773  out_clear_te:
4774                 spin_lock_bh(&mvm->time_event_lock);
4775                 iwl_mvm_te_clear_data(mvm, te_data);
4776                 spin_unlock_bh(&mvm->time_event_lock);
4777         }
4778
4779         return res;
4780 }
4781
4782 static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4783 {
4784         int ret = 0;
4785
4786         lockdep_assert_held(&mvm->mutex);
4787
4788         if (!fw_has_capa(&mvm->fw->ucode_capa,
4789                          IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4790                 IWL_ERR(mvm, "hotspot not supported\n");
4791                 return -EINVAL;
4792         }
4793
4794         if (iwl_mvm_has_new_station_api(mvm->fw)) {
4795                 ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4796                 WARN(ret, "Failed to allocate aux station");
4797         }
4798
4799         return ret;
4800 }
4801
4802 static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4803 {
4804         int ret;
4805
4806         lockdep_assert_held(&mvm->mutex);
4807
4808         ret = iwl_mvm_binding_add_vif(mvm, vif);
4809         if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4810                 return ret;
4811
4812         /* The station and queue allocation must be done only after the binding
4813          * is done, as otherwise the FW might incorrectly configure its state.
4814          */
4815         return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4816 }
4817
4818 static int iwl_mvm_roc(struct ieee80211_hw *hw,
4819                        struct ieee80211_vif *vif,
4820                        struct ieee80211_channel *channel,
4821                        int duration,
4822                        enum ieee80211_roc_type type)
4823 {
4824         static const struct iwl_mvm_roc_ops ops = {
4825                 .add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4826                 .link = iwl_mvm_roc_link,
4827         };
4828
4829         return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4830 }
4831
4832 static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
4833                                struct ieee80211_channel *channel,
4834                                struct ieee80211_vif *vif,
4835                                int duration)
4836 {
4837         int ret;
4838         u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
4839         u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
4840                                           IWL_FW_CMD_VER_UNKNOWN);
4841
4842         if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
4843                 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
4844         } else if (fw_ver >= 3) {
4845                 ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
4846                                           ROC_ACTIVITY_HOTSPOT);
4847         } else {
4848                 ret = -EOPNOTSUPP;
4849                 IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
4850         }
4851
4852         return ret;
4853 }
4854
4855 static int iwl_mvm_roc_p2p(struct iwl_mvm *mvm,
4856                            struct ieee80211_channel *channel,
4857                            struct ieee80211_vif *vif,
4858                            int duration,
4859                            enum ieee80211_roc_type type)
4860 {
4861         enum iwl_roc_activity activity;
4862         int ret;
4863
4864         lockdep_assert_held(&mvm->mutex);
4865
4866         switch (type) {
4867         case IEEE80211_ROC_TYPE_NORMAL:
4868                 activity = ROC_ACTIVITY_P2P_DISC;
4869                 break;
4870         case IEEE80211_ROC_TYPE_MGMT_TX:
4871                 activity = ROC_ACTIVITY_P2P_NEG;
4872                 break;
4873         default:
4874                 WARN_ONCE(1, "Got an invalid P2P ROC type\n");
4875                 return -EINVAL;
4876         }
4877
4878         ret = iwl_mvm_mld_add_aux_sta(mvm,
4879                                       iwl_mvm_get_lmac_id(mvm, channel->band));
4880         if (ret)
4881                 return ret;
4882
4883         return iwl_mvm_roc_add_cmd(mvm, channel, vif, duration, activity);
4884 }
4885
4886 static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
4887                                      struct ieee80211_vif *vif,
4888                                      struct ieee80211_channel *channel)
4889 {
4890         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4891         struct cfg80211_chan_def chandef;
4892         int i;
4893
4894         lockdep_assert_held(&mvm->mutex);
4895
4896         if (mvmvif->deflink.phy_ctxt &&
4897             channel == mvmvif->deflink.phy_ctxt->channel)
4898                 return 0;
4899
4900         /* Try using a PHY context that is already in use */
4901         for (i = 0; i < NUM_PHY_CTX; i++) {
4902                 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
4903
4904                 if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4905                         continue;
4906
4907                 if (channel == phy_ctxt->channel) {
4908                         if (mvmvif->deflink.phy_ctxt)
4909                                 iwl_mvm_phy_ctxt_unref(mvm,
4910                                                        mvmvif->deflink.phy_ctxt);
4911
4912                         mvmvif->deflink.phy_ctxt = phy_ctxt;
4913                         iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4914                         return 0;
4915                 }
4916         }
4917
4918         /* We already have a phy_ctxt, but it's not on the right channel */
4919         if (mvmvif->deflink.phy_ctxt)
4920                 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4921
4922         mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4923         if (!mvmvif->deflink.phy_ctxt)
4924                 return -ENOSPC;
4925
4926         cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4927
4928         return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
4929                                     &chandef, NULL, 1, 1);
4930 }
4931
4932 /* Execute the common part for MLD and non-MLD modes */
4933 int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4934                        struct ieee80211_channel *channel, int duration,
4935                        enum ieee80211_roc_type type,
4936                        const struct iwl_mvm_roc_ops *ops)
4937 {
4938         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4939         struct ieee80211_vif *bss_vif = iwl_mvm_get_bss_vif(mvm);
4940         u32 lmac_id;
4941         int ret;
4942
4943         IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4944                            duration, type);
4945
4946         /*
4947          * Flush the done work, just in case it's still pending, so that
4948          * the work it does can complete and we can accept new frames.
4949          */
4950         flush_work(&mvm->roc_done_wk);
4951
4952         if (!IS_ERR_OR_NULL(bss_vif)) {
4953                 ret = iwl_mvm_block_esr_sync(mvm, bss_vif,
4954                                              IWL_MVM_ESR_BLOCKED_ROC);
4955                 if (ret)
4956                         return ret;
4957         }
4958
4959         guard(mvm)(mvm);
4960
4961         switch (vif->type) {
4962         case NL80211_IFTYPE_STATION:
4963                 lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4964
4965                 /* Use aux roc framework (HS20) */
4966                 ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4967                 if (!ret)
4968                         ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4969                 return ret;
4970         case NL80211_IFTYPE_P2P_DEVICE:
4971                 /* handle below */
4972                 break;
4973         default:
4974                 IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4975                 return -EINVAL;
4976         }
4977
4978         if (iwl_mvm_has_p2p_over_aux(mvm)) {
4979                 ret = iwl_mvm_roc_p2p(mvm, channel, vif, duration, type);
4980                 return ret;
4981         }
4982
4983         ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
4984         if (ret)
4985                 return ret;
4986
4987         ret = ops->link(mvm, vif);
4988         if (ret)
4989                 return ret;
4990
4991         return iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4992 }
4993
4994 int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4995                        struct ieee80211_vif *vif)
4996 {
4997         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4998
4999         IWL_DEBUG_MAC80211(mvm, "enter\n");
5000
5001         iwl_mvm_stop_roc(mvm, vif);
5002
5003         IWL_DEBUG_MAC80211(mvm, "leave\n");
5004         return 0;
5005 }
5006
5007 struct iwl_mvm_ftm_responder_iter_data {
5008         bool responder;
5009         struct ieee80211_chanctx_conf *ctx;
5010 };
5011
5012 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
5013                                                struct ieee80211_vif *vif)
5014 {
5015         struct iwl_mvm_ftm_responder_iter_data *data = _data;
5016
5017         if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
5018             vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
5019                 data->responder = true;
5020 }
5021
5022 bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
5023                                       struct ieee80211_chanctx_conf *ctx)
5024 {
5025         struct iwl_mvm_ftm_responder_iter_data data = {
5026                 .responder = false,
5027                 .ctx = ctx,
5028         };
5029
5030         ieee80211_iterate_active_interfaces_atomic(mvm->hw,
5031                                         IEEE80211_IFACE_ITER_NORMAL,
5032                                         iwl_mvm_ftm_responder_chanctx_iter,
5033                                         &data);
5034         return data.responder;
5035 }
5036
5037 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
5038                                  struct ieee80211_chanctx_conf *ctx)
5039 {
5040         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5041         struct iwl_mvm_phy_ctxt *phy_ctxt;
5042         struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5043         int ret;
5044
5045         lockdep_assert_held(&mvm->mutex);
5046
5047         IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
5048
5049         phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
5050         if (!phy_ctxt) {
5051                 ret = -ENOSPC;
5052                 goto out;
5053         }
5054
5055         ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
5056                                    ctx->rx_chains_static,
5057                                    ctx->rx_chains_dynamic);
5058         if (ret) {
5059                 IWL_ERR(mvm, "Failed to add PHY context\n");
5060                 goto out;
5061         }
5062
5063         *phy_ctxt_id = phy_ctxt->id;
5064 out:
5065         return ret;
5066 }
5067
5068 int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
5069                         struct ieee80211_chanctx_conf *ctx)
5070 {
5071         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5072
5073         guard(mvm)(mvm);
5074         return __iwl_mvm_add_chanctx(mvm, ctx);
5075 }
5076
5077 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
5078                                      struct ieee80211_chanctx_conf *ctx)
5079 {
5080         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5081         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5082
5083         lockdep_assert_held(&mvm->mutex);
5084
5085         iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
5086 }
5087
5088 void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
5089                             struct ieee80211_chanctx_conf *ctx)
5090 {
5091         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5092
5093         guard(mvm)(mvm);
5094         __iwl_mvm_remove_chanctx(mvm, ctx);
5095 }
5096
5097 void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
5098                             struct ieee80211_chanctx_conf *ctx, u32 changed)
5099 {
5100         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5101         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5102         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5103         struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5104
5105         if (WARN_ONCE((phy_ctxt->ref > 1) &&
5106                       (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
5107                                    IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
5108                                    IEEE80211_CHANCTX_CHANGE_RADAR |
5109                                    IEEE80211_CHANCTX_CHANGE_MIN_DEF)),
5110                       "Cannot change PHY. Ref=%d, changed=0x%X\n",
5111                       phy_ctxt->ref, changed))
5112                 return;
5113
5114         guard(mvm)(mvm);
5115
5116         /* we are only changing the min_width, may be a noop */
5117         if (changed == IEEE80211_CHANCTX_CHANGE_MIN_DEF) {
5118                 if (phy_ctxt->width == def->width)
5119                         return;
5120
5121                 /* we are just toggling between 20_NOHT and 20 */
5122                 if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5123                     def->width <= NL80211_CHAN_WIDTH_20)
5124                         return;
5125         }
5126
5127         iwl_mvm_bt_coex_vif_change(mvm);
5128         iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5129                                  ctx->rx_chains_static,
5130                                  ctx->rx_chains_dynamic);
5131 }
5132
5133 /*
5134  * This function executes the common part for MLD and non-MLD modes.
5135  *
5136  * Returns true if we're done assigning the chanctx
5137  * (either on failure or success)
5138  */
5139 static bool
5140 __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5141                                     struct ieee80211_vif *vif,
5142                                     struct ieee80211_chanctx_conf *ctx,
5143                                     bool switching_chanctx, int *ret)
5144 {
5145         u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5146         struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5147         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5148
5149         lockdep_assert_held(&mvm->mutex);
5150
5151         mvmvif->deflink.phy_ctxt = phy_ctxt;
5152
5153         switch (vif->type) {
5154         case NL80211_IFTYPE_AP:
5155                 /* only needed if we're switching chanctx (i.e. during CSA) */
5156                 if (switching_chanctx) {
5157                         mvmvif->ap_ibss_active = true;
5158                         break;
5159                 }
5160                 fallthrough;
5161         case NL80211_IFTYPE_ADHOC:
5162                 /*
5163                  * The AP binding flow is handled as part of the start_ap flow
5164                  * (in bss_info_changed), similarly for IBSS.
5165                  */
5166                 *ret = 0;
5167                 return true;
5168         case NL80211_IFTYPE_STATION:
5169                 break;
5170         case NL80211_IFTYPE_MONITOR:
5171                 /* always disable PS when a monitor interface is active */
5172                 mvmvif->ps_disabled = true;
5173                 break;
5174         default:
5175                 *ret = -EINVAL;
5176                 return true;
5177         }
5178         return false;
5179 }
5180
5181 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
5182                                         struct ieee80211_vif *vif,
5183                                         struct ieee80211_bss_conf *link_conf,
5184                                         struct ieee80211_chanctx_conf *ctx,
5185                                         bool switching_chanctx)
5186 {
5187         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5188         int ret;
5189
5190         if (WARN_ON(!link_conf))
5191                 return -EINVAL;
5192
5193         if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
5194                                                 switching_chanctx, &ret))
5195                 goto out;
5196
5197         ret = iwl_mvm_binding_add_vif(mvm, vif);
5198         if (ret)
5199                 goto out;
5200
5201         /*
5202          * Power state must be updated before quotas,
5203          * otherwise fw will complain.
5204          */
5205         iwl_mvm_power_update_mac(mvm);
5206
5207         /* Setting the quota at this stage is only required for monitor
5208          * interfaces. For the other types, the bss_info changed flow
5209          * will handle quota settings.
5210          */
5211         if (vif->type == NL80211_IFTYPE_MONITOR) {
5212                 mvmvif->monitor_active = true;
5213                 ret = iwl_mvm_update_quotas(mvm, false, NULL);
5214                 if (ret)
5215                         goto out_remove_binding;
5216
5217                 ret = iwl_mvm_add_snif_sta(mvm, vif);
5218                 if (ret)
5219                         goto out_remove_binding;
5220
5221         }
5222
5223         /* Handle binding during CSA */
5224         if (vif->type == NL80211_IFTYPE_AP) {
5225                 iwl_mvm_update_quotas(mvm, false, NULL);
5226                 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5227         }
5228
5229         if (vif->type == NL80211_IFTYPE_STATION) {
5230                 if (!switching_chanctx) {
5231                         mvmvif->csa_bcn_pending = false;
5232                         goto out;
5233                 }
5234
5235                 mvmvif->csa_bcn_pending = true;
5236
5237                 if (!fw_has_capa(&mvm->fw->ucode_capa,
5238                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5239                         u32 duration = 5 * vif->bss_conf.beacon_int;
5240
5241                         /* Protect the session to make sure we hear the first
5242                          * beacon on the new channel.
5243                          */
5244                         iwl_mvm_protect_session(mvm, vif, duration, duration,
5245                                                 vif->bss_conf.beacon_int / 2,
5246                                                 true);
5247                 }
5248
5249                 iwl_mvm_update_quotas(mvm, false, NULL);
5250
5251                 iwl_mvm_send_ap_tx_power_constraint_cmd(mvm, vif,
5252                                                         link_conf,
5253                                                         false);
5254         }
5255
5256         goto out;
5257
5258 out_remove_binding:
5259         iwl_mvm_binding_remove_vif(mvm, vif);
5260         iwl_mvm_power_update_mac(mvm);
5261 out:
5262         if (ret)
5263                 mvmvif->deflink.phy_ctxt = NULL;
5264         return ret;
5265 }
5266
5267 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5268                                       struct ieee80211_vif *vif,
5269                                       struct ieee80211_bss_conf *link_conf,
5270                                       struct ieee80211_chanctx_conf *ctx)
5271 {
5272         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5273
5274         guard(mvm)(mvm);
5275         return __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5276 }
5277
5278 /*
5279  * This function executes the common part for MLD and non-MLD modes.
5280  *
5281  * Returns if chanctx unassign chanctx is done
5282  * (either on failure or success)
5283  */
5284 static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5285                                                   struct ieee80211_vif *vif,
5286                                                   bool switching_chanctx)
5287 {
5288         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5289
5290         lockdep_assert_held(&mvm->mutex);
5291         iwl_mvm_remove_time_event(mvm, mvmvif,
5292                                   &mvmvif->time_event_data);
5293
5294         switch (vif->type) {
5295         case NL80211_IFTYPE_ADHOC:
5296                 return true;
5297         case NL80211_IFTYPE_MONITOR:
5298                 mvmvif->monitor_active = false;
5299                 mvmvif->ps_disabled = false;
5300                 break;
5301         case NL80211_IFTYPE_AP:
5302                 /* This part is triggered only during CSA */
5303                 if (!switching_chanctx || !mvmvif->ap_ibss_active)
5304                         return true;
5305
5306                 mvmvif->csa_countdown = false;
5307
5308                 /* Set CS bit on all the stations */
5309                 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5310
5311                 /* Save blocked iface, the timeout is set on the next beacon */
5312                 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5313
5314                 mvmvif->ap_ibss_active = false;
5315                 break;
5316         default:
5317                 break;
5318         }
5319         return false;
5320 }
5321
5322 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5323                                            struct ieee80211_vif *vif,
5324                                            struct ieee80211_bss_conf *link_conf,
5325                                            struct ieee80211_chanctx_conf *ctx,
5326                                            bool switching_chanctx)
5327 {
5328         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5329         struct ieee80211_vif *disabled_vif = NULL;
5330
5331         if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5332                 goto out;
5333
5334         if (vif->type == NL80211_IFTYPE_MONITOR)
5335                 iwl_mvm_rm_snif_sta(mvm, vif);
5336
5337
5338         if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5339                 disabled_vif = vif;
5340                 if (!fw_has_capa(&mvm->fw->ucode_capa,
5341                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5342                         iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5343         }
5344
5345         iwl_mvm_update_quotas(mvm, false, disabled_vif);
5346         iwl_mvm_binding_remove_vif(mvm, vif);
5347
5348 out:
5349         if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5350             switching_chanctx)
5351                 return;
5352         mvmvif->deflink.phy_ctxt = NULL;
5353         iwl_mvm_power_update_mac(mvm);
5354 }
5355
5356 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5357                                          struct ieee80211_vif *vif,
5358                                          struct ieee80211_bss_conf *link_conf,
5359                                          struct ieee80211_chanctx_conf *ctx)
5360 {
5361         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5362
5363         guard(mvm)(mvm);
5364         __iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5365 }
5366
5367 static int
5368 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5369                                 struct ieee80211_vif_chanctx_switch *vifs,
5370                                 const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5371 {
5372         int ret;
5373
5374         guard(mvm)(mvm);
5375         ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5376                                     vifs[0].old_ctx, true);
5377         __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5378
5379         ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5380         if (ret) {
5381                 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5382                 goto out_reassign;
5383         }
5384
5385         ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5386                                         vifs[0].new_ctx, true);
5387         if (ret) {
5388                 IWL_ERR(mvm,
5389                         "failed to assign new_ctx during channel switch\n");
5390                 goto out_remove;
5391         }
5392
5393         /* we don't support TDLS during DCM - can be caused by channel switch */
5394         if (iwl_mvm_phy_ctx_count(mvm) > 1)
5395                 iwl_mvm_teardown_tdls_peers(mvm);
5396
5397         return 0;
5398
5399 out_remove:
5400         __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5401
5402 out_reassign:
5403         if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5404                 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5405                 goto out_restart;
5406         }
5407
5408         if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5409                                       vifs[0].old_ctx, true)) {
5410                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5411                 goto out_restart;
5412         }
5413
5414         return ret;
5415
5416 out_restart:
5417         /* things keep failing, better restart the hw */
5418         iwl_mvm_nic_restart(mvm, false);
5419         return ret;
5420 }
5421
5422 static int
5423 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5424                                     struct ieee80211_vif_chanctx_switch *vifs,
5425                                     const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5426 {
5427         int ret;
5428
5429         guard(mvm)(mvm);
5430         ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5431                                     vifs[0].old_ctx, true);
5432
5433         ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5434                                         vifs[0].new_ctx, true);
5435         if (ret) {
5436                 IWL_ERR(mvm,
5437                         "failed to assign new_ctx during channel switch\n");
5438                 goto out_reassign;
5439         }
5440
5441         return 0;
5442
5443 out_reassign:
5444         if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5445                                       vifs[0].old_ctx, true)) {
5446                 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5447                 goto out_restart;
5448         }
5449
5450         return ret;
5451
5452 out_restart:
5453         /* things keep failing, better restart the hw */
5454         iwl_mvm_nic_restart(mvm, false);
5455         return ret;
5456 }
5457
5458 /* Execute the common part for both MLD and non-MLD modes */
5459 int
5460 iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5461                                   struct ieee80211_vif_chanctx_switch *vifs,
5462                                   int n_vifs,
5463                                   enum ieee80211_chanctx_switch_mode mode,
5464                                   const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5465 {
5466         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5467         int ret;
5468
5469         /* we only support a single-vif right now */
5470         if (n_vifs > 1)
5471                 return -EOPNOTSUPP;
5472
5473         switch (mode) {
5474         case CHANCTX_SWMODE_SWAP_CONTEXTS:
5475                 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5476                 break;
5477         case CHANCTX_SWMODE_REASSIGN_VIF:
5478                 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5479                 break;
5480         default:
5481                 ret = -EOPNOTSUPP;
5482                 break;
5483         }
5484
5485         return ret;
5486 }
5487
5488 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5489                                       struct ieee80211_vif_chanctx_switch *vifs,
5490                                       int n_vifs,
5491                                       enum ieee80211_chanctx_switch_mode mode)
5492 {
5493         static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5494                 .__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5495                 .__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5496         };
5497
5498         return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5499 }
5500
5501 int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5502 {
5503         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5504
5505         return mvm->ibss_manager;
5506 }
5507
5508 static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5509                            bool set)
5510 {
5511         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5512         struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5513
5514         if (!mvm_sta || !mvm_sta->vif) {
5515                 IWL_ERR(mvm, "Station is not associated to a vif\n");
5516                 return -EINVAL;
5517         }
5518
5519         return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5520                                                &mvm_sta->vif->bss_conf);
5521 }
5522
5523 #ifdef CONFIG_NL80211_TESTMODE
5524 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5525         [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5526         [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5527         [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5528 };
5529
5530 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5531                                       struct ieee80211_vif *vif,
5532                                       void *data, int len)
5533 {
5534         struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5535         int err;
5536         u32 noa_duration;
5537
5538         err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5539                                    iwl_mvm_tm_policy, NULL);
5540         if (err)
5541                 return err;
5542
5543         if (!tb[IWL_MVM_TM_ATTR_CMD])
5544                 return -EINVAL;
5545
5546         switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5547         case IWL_MVM_TM_CMD_SET_NOA:
5548                 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5549                     !vif->bss_conf.enable_beacon ||
5550                     !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5551                         return -EINVAL;
5552
5553                 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5554                 if (noa_duration >= vif->bss_conf.beacon_int)
5555                         return -EINVAL;
5556
5557                 mvm->noa_duration = noa_duration;
5558                 mvm->noa_vif = vif;
5559
5560                 return iwl_mvm_update_quotas(mvm, true, NULL);
5561         case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5562                 /* must be associated client vif - ignore authorized */
5563                 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5564                     !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5565                     !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5566                         return -EINVAL;
5567
5568                 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5569                         return iwl_mvm_enable_beacon_filter(mvm, vif);
5570                 return iwl_mvm_disable_beacon_filter(mvm, vif);
5571         }
5572
5573         return -EOPNOTSUPP;
5574 }
5575
5576 int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5577                              struct ieee80211_vif *vif,
5578                              void *data, int len)
5579 {
5580         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5581
5582         guard(mvm)(mvm);
5583         return __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5584 }
5585 #endif
5586
5587 void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5588                             struct ieee80211_channel_switch *chsw)
5589 {
5590         /* By implementing this operation, we prevent mac80211 from
5591          * starting its own channel switch timer, so that we can call
5592          * ieee80211_chswitch_done() ourselves at the right time
5593          * (which is when the absence time event starts).
5594          */
5595
5596         IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5597                            "dummy channel switch op\n");
5598 }
5599
5600 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5601                                        struct ieee80211_vif *vif,
5602                                        struct ieee80211_channel_switch *chsw)
5603 {
5604         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5605         struct iwl_chan_switch_te_cmd cmd = {
5606                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5607                                                           mvmvif->color)),
5608                 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5609                 .tsf = cpu_to_le32(chsw->timestamp),
5610                 .cs_count = chsw->count,
5611                 .cs_mode = chsw->block_tx,
5612         };
5613
5614         lockdep_assert_held(&mvm->mutex);
5615
5616         if (chsw->delay)
5617                 cmd.cs_delayed_bcn_count =
5618                         DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5619
5620         return iwl_mvm_send_cmd_pdu(mvm,
5621                                     WIDE_ID(MAC_CONF_GROUP,
5622                                             CHANNEL_SWITCH_TIME_EVENT_CMD),
5623                                     0, sizeof(cmd), &cmd);
5624 }
5625
5626 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5627                                        struct ieee80211_vif *vif,
5628                                        struct ieee80211_channel_switch *chsw)
5629 {
5630         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5631         u32 apply_time;
5632
5633         /* Schedule the time event to a bit before beacon 1,
5634          * to make sure we're in the new channel when the
5635          * GO/AP arrives. In case count <= 1 immediately schedule the
5636          * TE (this might result with some packet loss or connection
5637          * loss).
5638          */
5639         if (chsw->count <= 1)
5640                 apply_time = 0;
5641         else
5642                 apply_time = chsw->device_timestamp +
5643                         ((vif->bss_conf.beacon_int * (chsw->count - 1) -
5644                           IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5645
5646         if (chsw->block_tx)
5647                 iwl_mvm_csa_client_absent(mvm, vif);
5648
5649         if (mvmvif->bf_enabled) {
5650                 int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
5651
5652                 if (ret)
5653                         return ret;
5654         }
5655
5656         iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5657                                     apply_time);
5658
5659         return 0;
5660 }
5661
5662 static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5663 {
5664         int i;
5665
5666         for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5667                 struct iwl_mvm_txq *mvmtxq =
5668                         iwl_mvm_txq_from_mac80211(sta->txq[i]);
5669
5670                 set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5671         }
5672 }
5673
5674 #define IWL_MAX_CSA_BLOCK_TX 1500
5675 int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5676                                struct ieee80211_vif *vif,
5677                                struct ieee80211_channel_switch *chsw)
5678 {
5679         struct ieee80211_vif *csa_vif;
5680         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5681         struct iwl_mvm_txq *mvmtxq;
5682         int ret;
5683
5684         lockdep_assert_held(&mvm->mutex);
5685
5686         mvmvif->csa_failed = false;
5687         mvmvif->csa_blocks_tx = false;
5688
5689         IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5690                            chsw->chandef.center_freq1);
5691
5692         iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5693                                        ieee80211_vif_to_wdev(vif),
5694                                        FW_DBG_TRIGGER_CHANNEL_SWITCH);
5695
5696         switch (vif->type) {
5697         case NL80211_IFTYPE_AP:
5698                 csa_vif =
5699                         rcu_dereference_protected(mvm->csa_vif,
5700                                                   lockdep_is_held(&mvm->mutex));
5701                 if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5702                               "Another CSA is already in progress"))
5703                         return -EBUSY;
5704
5705                 /* we still didn't unblock tx. prevent new CS meanwhile */
5706                 if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5707                                               lockdep_is_held(&mvm->mutex)))
5708                         return -EBUSY;
5709
5710                 rcu_assign_pointer(mvm->csa_vif, vif);
5711
5712                 if (WARN_ONCE(mvmvif->csa_countdown,
5713                               "Previous CSA countdown didn't complete"))
5714                         return -EBUSY;
5715
5716                 mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5717
5718                 if (!chsw->block_tx)
5719                         break;
5720                 /* don't need blocking in driver otherwise - mac80211 will do */
5721                 if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5722                         break;
5723
5724                 mvmvif->csa_blocks_tx = true;
5725                 mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5726                 set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5727                 ieee80211_iterate_stations_atomic(mvm->hw,
5728                                                   iwl_mvm_csa_block_txqs,
5729                                                   NULL);
5730                 break;
5731         case NL80211_IFTYPE_STATION:
5732                 mvmvif->csa_blocks_tx = chsw->block_tx;
5733
5734                 /*
5735                  * In the new flow FW is in charge of timing the switch so there
5736                  * is no need for all of this
5737                  */
5738                 if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5739                                             CHANNEL_SWITCH_ERROR_NOTIF,
5740                                             0))
5741                         break;
5742
5743                 /*
5744                  * We haven't configured the firmware to be associated yet since
5745                  * we don't know the dtim period. In this case, the firmware can't
5746                  * track the beacons.
5747                  */
5748                 if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
5749                         return -EBUSY;
5750
5751                 if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5752                     hweight16(vif->valid_links) <= 1)
5753                         schedule_delayed_work(&mvmvif->csa_work, 0);
5754
5755                 if (chsw->block_tx) {
5756                         /*
5757                          * In case of undetermined / long time with immediate
5758                          * quiet monitor status to gracefully disconnect
5759                          */
5760                         if (!chsw->count ||
5761                             chsw->count * vif->bss_conf.beacon_int >
5762                             IWL_MAX_CSA_BLOCK_TX)
5763                                 schedule_delayed_work(&mvmvif->csa_work,
5764                                                       msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5765                 }
5766
5767                 if (!fw_has_capa(&mvm->fw->ucode_capa,
5768                                  IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5769                         ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5770                         if (ret)
5771                                 return ret;
5772                 } else {
5773                         iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5774                 }
5775
5776                 mvmvif->csa_count = chsw->count;
5777                 mvmvif->csa_misbehave = false;
5778                 break;
5779         default:
5780                 break;
5781         }
5782
5783         mvmvif->ps_disabled = true;
5784
5785         ret = iwl_mvm_power_update_ps(mvm);
5786         if (ret)
5787                 return ret;
5788
5789         /* we won't be on this channel any longer */
5790         iwl_mvm_teardown_tdls_peers(mvm);
5791
5792         return ret;
5793 }
5794
5795 static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
5796                                           struct ieee80211_vif *vif,
5797                                           struct ieee80211_channel_switch *chsw)
5798 {
5799         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5800
5801         guard(mvm)(mvm);
5802         return iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5803 }
5804
5805 void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5806                                       struct ieee80211_vif *vif,
5807                                       struct ieee80211_channel_switch *chsw)
5808 {
5809         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5810         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5811         struct iwl_chan_switch_te_cmd cmd = {
5812                 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5813                                                           mvmvif->color)),
5814                 .action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5815                 .tsf = cpu_to_le32(chsw->timestamp),
5816                 .cs_count = chsw->count,
5817                 .cs_mode = chsw->block_tx,
5818         };
5819
5820         /*
5821          * In the new flow FW is in charge of timing the switch so there is no
5822          * need for all of this
5823          */
5824         if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5825                                     CHANNEL_SWITCH_ERROR_NOTIF, 0))
5826                 return;
5827
5828         if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5829                 return;
5830
5831         IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5832                            mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5833
5834         if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5835                 if (mvmvif->csa_misbehave) {
5836                         struct ieee80211_bss_conf *link_conf;
5837
5838                         /* Second time, give up on this AP*/
5839
5840                         link_conf = wiphy_dereference(hw->wiphy,
5841                                                       vif->link_conf[chsw->link_id]);
5842                         if (WARN_ON(!link_conf))
5843                                 return;
5844
5845                         iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5846                         ieee80211_chswitch_done(vif, false, 0);
5847                         mvmvif->csa_misbehave = false;
5848                         return;
5849                 }
5850                 mvmvif->csa_misbehave = true;
5851         }
5852         mvmvif->csa_count = chsw->count;
5853
5854         guard(mvm)(mvm);
5855         if (mvmvif->csa_failed)
5856                 return;
5857
5858         WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5859                                      WIDE_ID(MAC_CONF_GROUP,
5860                                              CHANNEL_SWITCH_TIME_EVENT_CMD),
5861                                      0, sizeof(cmd), &cmd));
5862 }
5863
5864 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5865 {
5866         int i;
5867
5868         if (!iwl_mvm_has_new_tx_api(mvm)) {
5869                 /* we can't ask the firmware anything if it is dead */
5870                 if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
5871                              &mvm->status))
5872                         return;
5873                 if (drop) {
5874                         guard(mvm)(mvm);
5875                         iwl_mvm_flush_tx_path(mvm,
5876                                 iwl_mvm_flushable_queues(mvm) & queues);
5877                 } else {
5878                         iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5879                 }
5880                 return;
5881         }
5882
5883         guard(mvm)(mvm);
5884         for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5885                 struct ieee80211_sta *sta;
5886
5887                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5888                                                 lockdep_is_held(&mvm->mutex));
5889                 if (IS_ERR_OR_NULL(sta))
5890                         continue;
5891
5892                 if (drop)
5893                         iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5894                 else
5895                         iwl_mvm_wait_sta_queues_empty(mvm,
5896                                         iwl_mvm_sta_from_mac80211(sta));
5897         }
5898 }
5899
5900 void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5901                        u32 queues, bool drop)
5902 {
5903         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5904         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5905         struct iwl_mvm_sta *mvmsta;
5906         struct ieee80211_sta *sta;
5907         bool ap_sta_done = false;
5908         int i;
5909         u32 msk = 0;
5910
5911         if (!vif) {
5912                 iwl_mvm_flush_no_vif(mvm, queues, drop);
5913                 return;
5914         }
5915
5916         if (!drop && hweight16(vif->active_links) <= 1) {
5917                 int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
5918                 struct ieee80211_bss_conf *link_conf;
5919
5920                 link_conf = wiphy_dereference(hw->wiphy,
5921                                               vif->link_conf[link_id]);
5922                 if (WARN_ON(!link_conf))
5923                         return;
5924                 if (link_conf->csa_active && mvmvif->csa_blocks_tx)
5925                         drop = true;
5926         }
5927
5928         /* Make sure we're done with the deferred traffic before flushing */
5929         flush_work(&mvm->add_stream_wk);
5930
5931         mutex_lock(&mvm->mutex);
5932
5933         /* flush the AP-station and all TDLS peers */
5934         for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5935                 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5936                                                 lockdep_is_held(&mvm->mutex));
5937                 if (IS_ERR_OR_NULL(sta))
5938                         continue;
5939
5940                 mvmsta = iwl_mvm_sta_from_mac80211(sta);
5941                 if (mvmsta->vif != vif)
5942                         continue;
5943
5944                 if (sta == mvmvif->ap_sta) {
5945                         if (ap_sta_done)
5946                                 continue;
5947                         ap_sta_done = true;
5948                 }
5949
5950                 if (drop) {
5951                         if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5952                                               mvmsta->tfd_queue_msk))
5953                                 IWL_ERR(mvm, "flush request fail\n");
5954                 } else {
5955                         if (iwl_mvm_has_new_tx_api(mvm))
5956                                 iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5957                         else /* only used for !iwl_mvm_has_new_tx_api() below */
5958                                 msk |= mvmsta->tfd_queue_msk;
5959                 }
5960         }
5961
5962         mutex_unlock(&mvm->mutex);
5963
5964         /* this can take a while, and we may need/want other operations
5965          * to succeed while doing this, so do it without the mutex held
5966          * If the firmware is dead, this can't work...
5967          */
5968         if (!drop && !iwl_mvm_has_new_tx_api(mvm) &&
5969             !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
5970                       &mvm->status))
5971                 iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5972 }
5973
5974 void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5975                            struct ieee80211_sta *sta)
5976 {
5977         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5978         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5979         struct iwl_mvm_link_sta *mvm_link_sta;
5980         struct ieee80211_link_sta *link_sta;
5981         int link_id;
5982
5983         guard(mvm)(mvm);
5984         for_each_sta_active_link(vif, sta, link_sta, link_id) {
5985                 mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5986                                                          lockdep_is_held(&mvm->mutex));
5987                 if (!mvm_link_sta)
5988                         continue;
5989
5990                 if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5991                                       mvmsta->tfd_queue_msk))
5992                         IWL_ERR(mvm, "flush request fail\n");
5993         }
5994 }
5995
5996 static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5997                                       struct survey_info *survey)
5998 {
5999         int chan_idx;
6000         enum nl80211_band band;
6001         int ret;
6002
6003         mutex_lock(&mvm->mutex);
6004
6005         if (!mvm->acs_survey) {
6006                 ret = -ENOENT;
6007                 goto out;
6008         }
6009
6010         /* Find and return the next entry that has a non-zero active time */
6011         for (band = 0; band < NUM_NL80211_BANDS; band++) {
6012                 struct ieee80211_supported_band *sband =
6013                         mvm->hw->wiphy->bands[band];
6014
6015                 if (!sband)
6016                         continue;
6017
6018                 for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
6019                         struct iwl_mvm_acs_survey_channel *info =
6020                                 &mvm->acs_survey->bands[band][chan_idx];
6021
6022                         if (!info->time)
6023                                 continue;
6024
6025                         /* Found (the next) channel to report */
6026                         survey->channel = &sband->channels[chan_idx];
6027                         survey->filled = SURVEY_INFO_TIME |
6028                                          SURVEY_INFO_TIME_BUSY |
6029                                          SURVEY_INFO_TIME_RX |
6030                                          SURVEY_INFO_TIME_TX;
6031                         survey->time = info->time;
6032                         survey->time_busy = info->time_busy;
6033                         survey->time_rx = info->time_rx;
6034                         survey->time_tx = info->time_tx;
6035                         survey->noise = info->noise;
6036                         if (survey->noise < 0)
6037                                 survey->filled |= SURVEY_INFO_NOISE_DBM;
6038
6039                         /* Clear time so that channel is only reported once */
6040                         info->time = 0;
6041
6042                         ret = 0;
6043                         goto out;
6044                 }
6045         }
6046
6047         ret = -ENOENT;
6048
6049 out:
6050         mutex_unlock(&mvm->mutex);
6051
6052         return ret;
6053 }
6054
6055 int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
6056                            struct survey_info *survey)
6057 {
6058         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6059         u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
6060                                            WIDE_ID(SYSTEM_GROUP,
6061                                                    SYSTEM_STATISTICS_CMD),
6062                                            IWL_FW_CMD_VER_UNKNOWN);
6063
6064         memset(survey, 0, sizeof(*survey));
6065
6066         if (!fw_has_capa(&mvm->fw->ucode_capa,
6067                          IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
6068                 return -ENOENT;
6069
6070         /*
6071          * Return the beacon stats at index zero and pass on following indices
6072          * to the function returning the full survey, most likely for ACS
6073          * (Automatic Channel Selection).
6074          */
6075         if (idx > 0)
6076                 return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
6077
6078         guard(mvm)(mvm);
6079
6080         if (iwl_mvm_firmware_running(mvm)) {
6081                 int ret = iwl_mvm_request_statistics(mvm, false);
6082
6083                 if (ret)
6084                         return ret;
6085         }
6086
6087         survey->filled = SURVEY_INFO_TIME_RX |
6088                          SURVEY_INFO_TIME_TX;
6089
6090         survey->time_rx = mvm->accu_radio_stats.rx_time +
6091                           mvm->radio_stats.rx_time;
6092         do_div(survey->time_rx, USEC_PER_MSEC);
6093
6094         survey->time_tx = mvm->accu_radio_stats.tx_time +
6095                           mvm->radio_stats.tx_time;
6096         do_div(survey->time_tx, USEC_PER_MSEC);
6097
6098         /* the new fw api doesn't support the following fields */
6099         if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
6100                 return 0;
6101
6102         survey->filled |= SURVEY_INFO_TIME |
6103                           SURVEY_INFO_TIME_SCAN;
6104         survey->time = mvm->accu_radio_stats.on_time_rf +
6105                        mvm->radio_stats.on_time_rf;
6106         do_div(survey->time, USEC_PER_MSEC);
6107
6108         survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6109                             mvm->radio_stats.on_time_scan;
6110         do_div(survey->time_scan, USEC_PER_MSEC);
6111
6112         return 0;
6113 }
6114
6115 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6116 {
6117         u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
6118         u32 gi_ltf;
6119
6120         switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6121         case RATE_MCS_CHAN_WIDTH_20:
6122                 rinfo->bw = RATE_INFO_BW_20;
6123                 break;
6124         case RATE_MCS_CHAN_WIDTH_40:
6125                 rinfo->bw = RATE_INFO_BW_40;
6126                 break;
6127         case RATE_MCS_CHAN_WIDTH_80:
6128                 rinfo->bw = RATE_INFO_BW_80;
6129                 break;
6130         case RATE_MCS_CHAN_WIDTH_160:
6131                 rinfo->bw = RATE_INFO_BW_160;
6132                 break;
6133         case RATE_MCS_CHAN_WIDTH_320:
6134                 rinfo->bw = RATE_INFO_BW_320;
6135                 break;
6136         }
6137
6138         if (format == RATE_MCS_CCK_MSK ||
6139             format == RATE_MCS_LEGACY_OFDM_MSK) {
6140                 int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
6141
6142                 /* add the offset needed to get to the legacy ofdm indices */
6143                 if (format == RATE_MCS_LEGACY_OFDM_MSK)
6144                         rate += IWL_FIRST_OFDM_RATE;
6145
6146                 switch (rate) {
6147                 case IWL_RATE_1M_INDEX:
6148                         rinfo->legacy = 10;
6149                         break;
6150                 case IWL_RATE_2M_INDEX:
6151                         rinfo->legacy = 20;
6152                         break;
6153                 case IWL_RATE_5M_INDEX:
6154                         rinfo->legacy = 55;
6155                         break;
6156                 case IWL_RATE_11M_INDEX:
6157                         rinfo->legacy = 110;
6158                         break;
6159                 case IWL_RATE_6M_INDEX:
6160                         rinfo->legacy = 60;
6161                         break;
6162                 case IWL_RATE_9M_INDEX:
6163                         rinfo->legacy = 90;
6164                         break;
6165                 case IWL_RATE_12M_INDEX:
6166                         rinfo->legacy = 120;
6167                         break;
6168                 case IWL_RATE_18M_INDEX:
6169                         rinfo->legacy = 180;
6170                         break;
6171                 case IWL_RATE_24M_INDEX:
6172                         rinfo->legacy = 240;
6173                         break;
6174                 case IWL_RATE_36M_INDEX:
6175                         rinfo->legacy = 360;
6176                         break;
6177                 case IWL_RATE_48M_INDEX:
6178                         rinfo->legacy = 480;
6179                         break;
6180                 case IWL_RATE_54M_INDEX:
6181                         rinfo->legacy = 540;
6182                 }
6183                 return;
6184         }
6185
6186         rinfo->nss = u32_get_bits(rate_n_flags,
6187                                   RATE_MCS_NSS_MSK) + 1;
6188         rinfo->mcs = format == RATE_MCS_HT_MSK ?
6189                 RATE_HT_MCS_INDEX(rate_n_flags) :
6190                 u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
6191
6192         if (rate_n_flags & RATE_MCS_SGI_MSK)
6193                 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6194
6195         switch (format) {
6196         case RATE_MCS_EHT_MSK:
6197                 /* TODO: GI/LTF/RU. How does the firmware encode them? */
6198                 rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
6199                 break;
6200         case RATE_MCS_HE_MSK:
6201                 gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6202
6203                 rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6204
6205                 if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6206                         rinfo->bw = RATE_INFO_BW_HE_RU;
6207                         rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6208                 }
6209
6210                 switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
6211                 case RATE_MCS_HE_TYPE_SU:
6212                 case RATE_MCS_HE_TYPE_EXT_SU:
6213                         if (gi_ltf == 0 || gi_ltf == 1)
6214                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6215                         else if (gi_ltf == 2)
6216                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6217                         else if (gi_ltf == 3)
6218                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6219                         else
6220                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6221                         break;
6222                 case RATE_MCS_HE_TYPE_MU:
6223                         if (gi_ltf == 0 || gi_ltf == 1)
6224                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6225                         else if (gi_ltf == 2)
6226                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6227                         else
6228                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6229                         break;
6230                 case RATE_MCS_HE_TYPE_TRIG:
6231                         if (gi_ltf == 0 || gi_ltf == 1)
6232                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6233                         else
6234                                 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6235                         break;
6236                 }
6237
6238                 if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6239                         rinfo->he_dcm = 1;
6240                 break;
6241         case RATE_MCS_HT_MSK:
6242                 rinfo->flags |= RATE_INFO_FLAGS_MCS;
6243                 break;
6244         case RATE_MCS_VHT_MSK:
6245                 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
6246                 break;
6247         }
6248 }
6249
6250 void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6251                                 struct ieee80211_vif *vif,
6252                                 struct ieee80211_sta *sta,
6253                                 struct station_info *sinfo)
6254 {
6255         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6256         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6257         struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6258         int i;
6259
6260         if (mvmsta->deflink.avg_energy) {
6261                 sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
6262                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6263         }
6264
6265         if (iwl_mvm_has_tlc_offload(mvm)) {
6266                 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6267
6268                 iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6269                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6270         }
6271
6272         /* if beacon filtering isn't on mac80211 does it anyway */
6273         if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6274                 return;
6275
6276         if (!vif->cfg.assoc)
6277                 return;
6278
6279         guard(mvm)(mvm);
6280
6281         if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
6282                 return;
6283
6284         if (iwl_mvm_request_statistics(mvm, false))
6285                 return;
6286
6287         sinfo->rx_beacon = 0;
6288         for_each_mvm_vif_valid_link(mvmvif, i)
6289                 sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6290                         mvmvif->link[i]->beacon_stats.accu_num_beacons;
6291
6292         sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6293         if (mvmvif->deflink.beacon_stats.avg_signal) {
6294                 /* firmware only reports a value after RXing a few beacons */
6295                 sinfo->rx_beacon_signal_avg =
6296                         mvmvif->deflink.beacon_stats.avg_signal;
6297                 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6298         }
6299 }
6300
6301 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6302                                             struct ieee80211_vif *vif,
6303                                             const  struct ieee80211_mlme_event *mlme)
6304 {
6305         if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
6306             (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6307                 iwl_dbg_tlv_time_point(&mvm->fwrt,
6308                                        IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6309                                        NULL);
6310                 return;
6311         }
6312
6313         if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6314                 iwl_dbg_tlv_time_point(&mvm->fwrt,
6315                                        IWL_FW_INI_TIME_POINT_DEASSOC,
6316                                        NULL);
6317                 return;
6318         }
6319 }
6320
6321 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6322                                         struct ieee80211_vif *vif,
6323                                         const struct ieee80211_event *event)
6324 {
6325 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)                               \
6326         do {                                                            \
6327                 if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))           \
6328                         break;                                          \
6329                 iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);      \
6330         } while (0)
6331
6332         struct iwl_fw_dbg_trigger_tlv *trig;
6333         struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6334
6335         if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6336                 iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6337                 return;
6338         }
6339
6340         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6341                                      FW_DBG_TRIGGER_MLME);
6342         if (!trig)
6343                 return;
6344
6345         trig_mlme = (void *)trig->data;
6346
6347         if (event->u.mlme.data == ASSOC_EVENT) {
6348                 if (event->u.mlme.status == MLME_DENIED)
6349                         CHECK_MLME_TRIGGER(stop_assoc_denied,
6350                                            "DENIED ASSOC: reason %d",
6351                                             event->u.mlme.reason);
6352                 else if (event->u.mlme.status == MLME_TIMEOUT)
6353                         CHECK_MLME_TRIGGER(stop_assoc_timeout,
6354                                            "ASSOC TIMEOUT");
6355         } else if (event->u.mlme.data == AUTH_EVENT) {
6356                 if (event->u.mlme.status == MLME_DENIED)
6357                         CHECK_MLME_TRIGGER(stop_auth_denied,
6358                                            "DENIED AUTH: reason %d",
6359                                            event->u.mlme.reason);
6360                 else if (event->u.mlme.status == MLME_TIMEOUT)
6361                         CHECK_MLME_TRIGGER(stop_auth_timeout,
6362                                            "AUTH TIMEOUT");
6363         } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
6364                 CHECK_MLME_TRIGGER(stop_rx_deauth,
6365                                    "DEAUTH RX %d", event->u.mlme.reason);
6366         } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
6367                 CHECK_MLME_TRIGGER(stop_tx_deauth,
6368                                    "DEAUTH TX %d", event->u.mlme.reason);
6369         }
6370 #undef CHECK_MLME_TRIGGER
6371 }
6372
6373 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6374                                           struct ieee80211_vif *vif,
6375                                           const struct ieee80211_event *event)
6376 {
6377         struct iwl_fw_dbg_trigger_tlv *trig;
6378         struct iwl_fw_dbg_trigger_ba *ba_trig;
6379
6380         trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6381                                      FW_DBG_TRIGGER_BA);
6382         if (!trig)
6383                 return;
6384
6385         ba_trig = (void *)trig->data;
6386
6387         if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6388                 return;
6389
6390         iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6391                                 "BAR received from %pM, tid %d, ssn %d",
6392                                 event->u.ba.sta->addr, event->u.ba.tid,
6393                                 event->u.ba.ssn);
6394 }
6395
6396 void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6397                                 struct ieee80211_vif *vif,
6398                                 const struct ieee80211_event *event)
6399 {
6400         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6401
6402         switch (event->type) {
6403         case MLME_EVENT:
6404                 iwl_mvm_event_mlme_callback(mvm, vif, event);
6405                 break;
6406         case BAR_RX_EVENT:
6407                 iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6408                 break;
6409         case BA_FRAME_TIMEOUT:
6410                 iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6411                                                      event->u.ba.tid);
6412                 break;
6413         default:
6414                 break;
6415         }
6416 }
6417
6418 #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6419 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
6420                                      enum iwl_mvm_rxq_notif_type type,
6421                                      bool sync,
6422                                      const void *data, u32 size)
6423 {
6424         struct {
6425                 struct iwl_rxq_sync_cmd cmd;
6426                 struct iwl_mvm_internal_rxq_notif notif;
6427         } __packed cmd = {
6428                 .cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
6429                 .cmd.count =
6430                         cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6431                                     size),
6432                 .notif.type = type,
6433                 .notif.sync = sync,
6434         };
6435         struct iwl_host_cmd hcmd = {
6436                 .id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6437                 .data[0] = &cmd,
6438                 .len[0] = sizeof(cmd),
6439                 .data[1] = data,
6440                 .len[1] = size,
6441                 .flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
6442         };
6443         int ret;
6444
6445         /* size must be a multiple of DWORD */
6446         if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
6447                 return;
6448
6449         if (!iwl_mvm_has_new_rx_api(mvm))
6450                 return;
6451
6452         if (sync) {
6453                 cmd.notif.cookie = mvm->queue_sync_cookie;
6454                 mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6455         }
6456
6457         ret = iwl_mvm_send_cmd(mvm, &hcmd);
6458         if (ret) {
6459                 IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6460                 goto out;
6461         }
6462
6463         if (sync) {
6464                 lockdep_assert_held(&mvm->mutex);
6465                 ret = wait_event_timeout(mvm->rx_sync_waitq,
6466                                          READ_ONCE(mvm->queue_sync_state) == 0,
6467                                          SYNC_RX_QUEUE_TIMEOUT);
6468                 WARN_ONCE(!ret, "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6469                           mvm->queue_sync_state,
6470                           mvm->queue_sync_cookie);
6471         }
6472
6473 out:
6474         if (sync) {
6475                 mvm->queue_sync_state = 0;
6476                 mvm->queue_sync_cookie++;
6477         }
6478 }
6479
6480 void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
6481 {
6482         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6483
6484         guard(mvm)(mvm);
6485         iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
6486 }
6487
6488 int
6489 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6490                                     struct ieee80211_vif *vif,
6491                                     struct cfg80211_ftm_responder_stats *stats)
6492 {
6493         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6494         struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6495
6496         if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6497             !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6498                 return -EINVAL;
6499
6500         mutex_lock(&mvm->mutex);
6501         *stats = mvm->ftm_resp_stats;
6502         mutex_unlock(&mvm->mutex);
6503
6504         stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6505                         BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6506                         BIT(NL80211_FTM_STATS_FAILED_NUM) |
6507                         BIT(NL80211_FTM_STATS_ASAP_NUM) |
6508                         BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6509                         BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6510                         BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6511                         BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6512                         BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6513
6514         return 0;
6515 }
6516
6517 int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6518                        struct cfg80211_pmsr_request *request)
6519 {
6520         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6521
6522         guard(mvm)(mvm);
6523         return iwl_mvm_ftm_start(mvm, vif, request);
6524 }
6525
6526 void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6527                         struct cfg80211_pmsr_request *request)
6528 {
6529         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6530
6531         guard(mvm)(mvm);
6532         iwl_mvm_ftm_abort(mvm, request);
6533 }
6534
6535 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6536 {
6537         u8 protocol = ip_hdr(skb)->protocol;
6538
6539         if (!IS_ENABLED(CONFIG_INET))
6540                 return false;
6541
6542         return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6543 }
6544
6545 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6546                                       struct sk_buff *head,
6547                                       struct sk_buff *skb)
6548 {
6549         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6550
6551         /* For now don't aggregate IPv6 in AMSDU */
6552         if (skb->protocol != htons(ETH_P_IP))
6553                 return false;
6554
6555         if (!iwl_mvm_is_csum_supported(mvm))
6556                 return true;
6557
6558         return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6559 }
6560
6561 int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6562                              struct ieee80211_vif *vif,
6563                              struct cfg80211_set_hw_timestamp *hwts)
6564 {
6565         struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6566         u32 protocols = 0;
6567
6568         /* HW timestamping is only supported for a specific station */
6569         if (!hwts->macaddr)
6570                 return -EOPNOTSUPP;
6571
6572         if (hwts->enable)
6573                 protocols =
6574                         IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6575
6576         guard(mvm)(mvm);
6577         return iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6578 }
6579
6580 const struct ieee80211_ops iwl_mvm_hw_ops = {
6581         .tx = iwl_mvm_mac_tx,
6582         .wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6583         .ampdu_action = iwl_mvm_mac_ampdu_action,
6584         .get_antenna = iwl_mvm_op_get_antenna,
6585         .set_antenna = iwl_mvm_op_set_antenna,
6586         .start = iwl_mvm_mac_start,
6587         .reconfig_complete = iwl_mvm_mac_reconfig_complete,
6588         .stop = iwl_mvm_mac_stop,
6589         .add_interface = iwl_mvm_mac_add_interface,
6590         .remove_interface = iwl_mvm_mac_remove_interface,
6591         .config = iwl_mvm_mac_config,
6592         .prepare_multicast = iwl_mvm_prepare_multicast,
6593         .configure_filter = iwl_mvm_configure_filter,
6594         .config_iface_filter = iwl_mvm_config_iface_filter,
6595         .bss_info_changed = iwl_mvm_bss_info_changed,
6596         .hw_scan = iwl_mvm_mac_hw_scan,
6597         .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6598         .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6599         .sta_state = iwl_mvm_mac_sta_state,
6600         .sta_notify = iwl_mvm_mac_sta_notify,
6601         .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6602         .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6603         .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6604         .link_sta_rc_update = iwl_mvm_sta_rc_update,
6605         .conf_tx = iwl_mvm_mac_conf_tx,
6606         .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6607         .mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6608         .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6609         .flush = iwl_mvm_mac_flush,
6610         .flush_sta = iwl_mvm_mac_flush_sta,
6611         .sched_scan_start = iwl_mvm_mac_sched_scan_start,
6612         .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6613         .set_key = iwl_mvm_mac_set_key,
6614         .update_tkip_key = iwl_mvm_mac_update_tkip_key,
6615         .remain_on_channel = iwl_mvm_roc,
6616         .cancel_remain_on_channel = iwl_mvm_cancel_roc,
6617         .add_chanctx = iwl_mvm_add_chanctx,
6618         .remove_chanctx = iwl_mvm_remove_chanctx,
6619         .change_chanctx = iwl_mvm_change_chanctx,
6620         .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6621         .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6622         .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6623
6624         .start_ap = iwl_mvm_start_ap,
6625         .stop_ap = iwl_mvm_stop_ap,
6626         .join_ibss = iwl_mvm_start_ibss,
6627         .leave_ibss = iwl_mvm_stop_ibss,
6628
6629         .tx_last_beacon = iwl_mvm_tx_last_beacon,
6630
6631         .set_tim = iwl_mvm_set_tim,
6632
6633         .channel_switch = iwl_mvm_channel_switch,
6634         .pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6635         .post_channel_switch = iwl_mvm_post_channel_switch,
6636         .abort_channel_switch = iwl_mvm_abort_channel_switch,
6637         .channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6638
6639         .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6640         .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6641         .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6642
6643         .event_callback = iwl_mvm_mac_event_callback,
6644
6645         .sync_rx_queues = iwl_mvm_sync_rx_queues,
6646
6647         CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6648
6649 #ifdef CONFIG_PM_SLEEP
6650         /* look at d3.c */
6651         .suspend = iwl_mvm_suspend,
6652         .resume = iwl_mvm_resume,
6653         .set_wakeup = iwl_mvm_set_wakeup,
6654         .set_rekey_data = iwl_mvm_set_rekey_data,
6655 #if IS_ENABLED(CONFIG_IPV6)
6656         .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6657 #endif
6658         .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6659 #endif
6660         .get_survey = iwl_mvm_mac_get_survey,
6661         .sta_statistics = iwl_mvm_mac_sta_statistics,
6662         .get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6663         .start_pmsr = iwl_mvm_start_pmsr,
6664         .abort_pmsr = iwl_mvm_abort_pmsr,
6665
6666         .can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6667 #ifdef CONFIG_IWLWIFI_DEBUGFS
6668         .vif_add_debugfs = iwl_mvm_vif_add_debugfs,
6669         .link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6670 #endif
6671         .set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6672 };
This page took 0.431202 seconds and 4 git commands to generate.