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