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