wifi: iwlwifi: mvm: add link_conf parameter for add/remove/change link
[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);
1a3e7039
GG
2484 iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2485 IEEE80211_SMPS_AUTOMATIC);
660eba5a
MK
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 */
1a3e7039
GG
2492void
2493iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2494 struct ieee80211_vif *vif,
2495 struct ieee80211_bss_conf *link_conf,
2496 u64 changes)
660eba5a
MK
2497{
2498 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2499 int ret;
2500
2501 if (changes & BSS_CHANGED_BEACON_INFO) {
2502 /* We received a beacon from the associated AP so
2503 * remove the session protection.
2504 */
2505 iwl_mvm_stop_session_protection(mvm, vif);
2506
2507 iwl_mvm_sf_update(mvm, vif, false);
2508 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2509 }
2510
2511 if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2512 /* Send power command on every beacon change,
2513 * because we may have not enabled beacon abort yet.
2514 */
2515 BSS_CHANGED_BEACON_INFO)) {
2516 ret = iwl_mvm_power_update_mac(mvm);
2517 if (ret)
2518 IWL_ERR(mvm, "failed to update power mode\n");
2519 }
2520
2521 if (changes & BSS_CHANGED_CQM) {
2522 IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2523 /* reset cqm events tracking */
2524 mvmvif->bf_data.last_cqm_event = 0;
2525 if (mvmvif->bf_data.bf_enabled) {
2526 ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2527 if (ret)
2528 IWL_ERR(mvm,
2529 "failed to update CQM thresholds\n");
2530 }
2531 }
2532
2533 if (changes & BSS_CHANGED_BANDWIDTH)
1a3e7039 2534 iwl_mvm_update_link_smps(vif, link_conf);
660eba5a
MK
2535}
2536
8ca151b5
JB
2537static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2538 struct ieee80211_vif *vif,
2539 struct ieee80211_bss_conf *bss_conf,
7b7090b4 2540 u64 changes)
8ca151b5
JB
2541{
2542 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2543 int ret;
2544
6e97b0d2 2545 /*
cdaba917
EG
2546 * Re-calculate the tsf id, as the leader-follower relations depend
2547 * on the beacon interval, which was not known when the station
2548 * interface was added.
6e97b0d2 2549 */
f276e20b 2550 if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
cb63eb43
MK
2551 if ((vif->bss_conf.he_support &&
2552 !iwlwifi_mod_params.disable_11ax) ||
2553 (vif->bss_conf.eht_support &&
2554 !iwlwifi_mod_params.disable_11be))
650cadb7 2555 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
514c3069 2556
6e97b0d2 2557 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
514c3069 2558 }
6e97b0d2 2559
40ecdd01
ST
2560 /* Update MU EDCA params */
2561 if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
cb63eb43
MK
2562 vif->cfg.assoc &&
2563 ((vif->bss_conf.he_support &&
2564 !iwlwifi_mod_params.disable_11ax) ||
2565 (vif->bss_conf.eht_support &&
2566 !iwlwifi_mod_params.disable_11be)))
650cadb7 2567 iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
40ecdd01 2568
3dfd3a97
JB
2569 /*
2570 * If we're not associated yet, take the (new) BSSID before associating
2571 * so the firmware knows. If we're already associated, then use the old
2572 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2573 * branch for disassociation below.
2574 */
2575 if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
650cadb7 2576 memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
3dfd3a97 2577
650cadb7 2578 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
8ca151b5
JB
2579 if (ret)
2580 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2581
3dfd3a97 2582 /* after sending it once, adopt mac80211 data */
650cadb7 2583 memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
f276e20b 2584 mvmvif->associated = vif->cfg.assoc;
3dfd3a97 2585
8ca151b5 2586 if (changes & BSS_CHANGED_ASSOC) {
f276e20b 2587 if (vif->cfg.assoc) {
33cef925
JB
2588 /* clear statistics to get clean beacon counter */
2589 iwl_mvm_request_statistics(mvm, true);
650cadb7
GG
2590 memset(&mvmvif->deflink.beacon_stats, 0,
2591 sizeof(mvmvif->deflink.beacon_stats));
33cef925 2592
8ca151b5 2593 /* add quota for this interface */
7754ae79 2594 ret = iwl_mvm_update_quotas(mvm, true, NULL);
8ca151b5
JB
2595 if (ret) {
2596 IWL_ERR(mvm, "failed to update quotas\n");
2597 return;
2598 }
016d27e1
JB
2599
2600 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
fe959c7b
EG
2601 &mvm->status) &&
2602 !fw_has_capa(&mvm->fw->ucode_capa,
2603 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
016d27e1
JB
2604 /*
2605 * If we're restarting then the firmware will
2606 * obviously have lost synchronisation with
2607 * the AP. It will attempt to synchronise by
2608 * itself, but we can make it more reliable by
2609 * scheduling a session protection time event.
2610 *
2611 * The firmware needs to receive a beacon to
2612 * catch up with synchronisation, use 110% of
2613 * the beacon interval.
2614 *
2615 * Set a large maximum delay to allow for more
2616 * than a single interface.
fe959c7b
EG
2617 *
2618 * For new firmware versions, rely on the
2619 * firmware. This is relevant for DCM scenarios
2620 * only anyway.
016d27e1
JB
2621 */
2622 u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2623 iwl_mvm_protect_session(mvm, vif, dur, dur,
d20d37bc 2624 5 * dur, false);
af84ac57
JB
2625 } else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2626 &mvm->status) &&
2627 !vif->bss_conf.dtim_period) {
2628 /*
2629 * If we're not restarting and still haven't
2630 * heard a beacon (dtim period unknown) then
2631 * make sure we still have enough minimum time
2632 * remaining in the time event, since the auth
2633 * might actually have taken quite a while
2634 * (especially for SAE) and so the remaining
2635 * time could be small without us having heard
2636 * a beacon yet.
2637 */
2638 iwl_mvm_protect_assoc(mvm, vif, 0);
016d27e1 2639 }
1f3b0ff8
LE
2640
2641 iwl_mvm_sf_update(mvm, vif, false);
175a70b7 2642 iwl_mvm_power_vif_assoc(mvm, vif);
697162a1 2643 if (vif->p2p) {
697162a1
EG
2644 iwl_mvm_update_smps(mvm, vif,
2645 IWL_MVM_SMPS_REQ_PROT,
1a3e7039 2646 IEEE80211_SMPS_DYNAMIC, 0);
697162a1 2647 }
650cadb7 2648 } else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
6d19a5eb 2649 iwl_mvm_mei_host_disassociated(mvm);
1f3b0ff8
LE
2650 /*
2651 * If update fails - SF might be running in associated
2652 * mode while disassociated - which is forbidden.
2653 */
69e508b4
IP
2654 ret = iwl_mvm_sf_update(mvm, vif, false);
2655 WARN_ONCE(ret &&
2656 !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2657 &mvm->status),
1f3b0ff8
LE
2658 "Failed to update SF upon disassociation\n");
2659
6b28f978
EG
2660 /*
2661 * If we get an assert during the connection (after the
2662 * station has been added, but before the vif is set
2663 * to associated), mac80211 will re-add the station and
2664 * then configure the vif. Since the vif is not
2665 * associated, we would remove the station here and
2666 * this would fail the recovery.
2667 */
2668 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2669 &mvm->status)) {
5c75a208
JB
2670 /* first remove remaining keys */
2671 iwl_mvm_sec_key_remove_ap(mvm, vif);
2672
6b28f978
EG
2673 /*
2674 * Remove AP station now that
2675 * the MAC is unassoc
2676 */
2677 ret = iwl_mvm_rm_sta_id(mvm, vif,
650cadb7 2678 mvmvif->deflink.ap_sta_id);
6b28f978
EG
2679 if (ret)
2680 IWL_ERR(mvm,
2681 "failed to remove AP station\n");
2682
650cadb7 2683 mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
6b28f978 2684 }
37577fe2 2685
8ca151b5 2686 /* remove quota for this interface */
7754ae79 2687 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5
JB
2688 if (ret)
2689 IWL_ERR(mvm, "failed to update quotas\n");
29a90a49 2690
3dfd3a97
JB
2691 /* this will take the cleared BSSID from bss_conf */
2692 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2693 if (ret)
2694 IWL_ERR(mvm,
2695 "failed to update MAC %pM (clear after unassoc)\n",
2696 vif->addr);
8ca151b5 2697 }
a20fd398 2698
660eba5a 2699 iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
cc87d322
EH
2700 }
2701
1a3e7039
GG
2702 iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
2703 changes);
8ca151b5
JB
2704}
2705
f947b62c
MK
2706bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2707 struct ieee80211_vif *vif,
2708 int *ret)
2709{
2710 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2711 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2712 int i;
2713
2714 lockdep_assert_held(&mvm->mutex);
2715
2716 mvmvif->ap_assoc_sta_count = 0;
2717
2718 /* must be set before quota calculations */
2719 mvmvif->ap_ibss_active = true;
2720
2721 /* send all the early keys to the device now */
2722 for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2723 struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2724
2725 if (!key)
2726 continue;
2727
2728 mvmvif->ap_early_keys[i] = NULL;
2729
2730 *ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2731 if (*ret)
2732 return true;
2733 }
2734
2735 if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2736 iwl_mvm_vif_set_low_latency(mvmvif, true,
2737 LOW_LATENCY_VIF_TYPE);
2738 iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2739 }
2740
2741 /* power updated needs to be done before quotas */
2742 iwl_mvm_power_update_mac(mvm);
2743
2744 return false;
2745}
2746
5023d966 2747static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
ae7ba17b 2748 struct ieee80211_vif *vif,
b327c84c 2749 struct ieee80211_bss_conf *link_conf)
8ca151b5
JB
2750{
2751 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2752 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
f947b62c 2753 int ret;
8ca151b5
JB
2754
2755 mutex_lock(&mvm->mutex);
2756
6e97b0d2 2757 /*
cdaba917
EG
2758 * Re-calculate the tsf id, as the leader-follower relations depend on
2759 * the beacon interval, which was not known when the AP interface
2760 * was added.
6e97b0d2
IP
2761 */
2762 if (vif->type == NL80211_IFTYPE_AP)
2763 iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2764
36cf5377
GG
2765 /* For older devices need to send beacon template before adding mac
2766 * context. For the newer, the beacon is a resource that belongs to a
2767 * MAC, so need to send beacon template after adding the mac.
2768 */
2769 if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
2770 /* Add the mac context */
2771 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2772 if (ret)
2773 goto out_unlock;
2774
2775 /* Send the beacon template */
2776 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2777 if (ret)
2778 goto out_unlock;
2779 } else {
2780 /* Send the beacon template */
2781 ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
2782 if (ret)
2783 goto out_unlock;
2784
2785 /* Add the mac context */
2786 ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2787 if (ret)
2788 goto out_unlock;
2789 }
8ca151b5
JB
2790
2791 /* Perform the binding */
2792 ret = iwl_mvm_binding_add_vif(mvm, vif);
2793 if (ret)
2794 goto out_remove;
2795
63dd5d02
SS
2796 /*
2797 * This is not very nice, but the simplest:
2798 * For older FWs adding the mcast sta before the bcast station may
2799 * cause assert 0x2b00.
2800 * This is fixed in later FW so make the order of removal depend on
2801 * the TLV
2802 */
2803 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2804 ret = iwl_mvm_add_mcast_sta(mvm, vif);
2805 if (ret)
2806 goto out_unbind;
2807 /*
2808 * Send the bcast station. At this stage the TBTT and DTIM time
2809 * events are added and applied to the scheduler
2810 */
2811 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2812 if (ret) {
2813 iwl_mvm_rm_mcast_sta(mvm, vif);
2814 goto out_unbind;
2815 }
2816 } else {
2817 /*
2818 * Send the bcast station. At this stage the TBTT and DTIM time
2819 * events are added and applied to the scheduler
2820 */
75fd4fec 2821 ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
63dd5d02
SS
2822 if (ret)
2823 goto out_unbind;
75fd4fec 2824 ret = iwl_mvm_add_mcast_sta(mvm, vif);
63dd5d02
SS
2825 if (ret) {
2826 iwl_mvm_send_rm_bcast_sta(mvm, vif);
2827 goto out_unbind;
2828 }
2829 }
26d6c16b 2830
f947b62c
MK
2831 if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
2832 goto out_failed;
a11e144e 2833
7754ae79 2834 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5 2835 if (ret)
f947b62c 2836 goto out_failed;
8ca151b5 2837
5023d966 2838 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2839 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2840 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2841
8e484f0b 2842 iwl_mvm_bt_coex_vif_change(mvm);
dac94da8 2843
f697267f
AN
2844 /* we don't support TDLS during DCM */
2845 if (iwl_mvm_phy_ctx_count(mvm) > 1)
2846 iwl_mvm_teardown_tdls_peers(mvm);
2847
b73f9a4a
JB
2848 iwl_mvm_ftm_restart_responder(mvm, vif);
2849
939e4904 2850 goto out_unlock;
8ca151b5 2851
f947b62c 2852out_failed:
999609f1 2853 iwl_mvm_power_update_mac(mvm);
5691e218 2854 mvmvif->ap_ibss_active = false;
013290aa 2855 iwl_mvm_send_rm_bcast_sta(mvm, vif);
ced19f26 2856 iwl_mvm_rm_mcast_sta(mvm, vif);
8ca151b5
JB
2857out_unbind:
2858 iwl_mvm_binding_remove_vif(mvm, vif);
2859out_remove:
2860 iwl_mvm_mac_ctxt_remove(mvm, vif);
2861out_unlock:
2862 mutex_unlock(&mvm->mutex);
2863 return ret;
2864}
2865
ae7ba17b
ST
2866static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
2867 struct ieee80211_vif *vif,
b327c84c 2868 struct ieee80211_bss_conf *link_conf)
ae7ba17b 2869{
b327c84c 2870 return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
ae7ba17b
ST
2871}
2872
2873static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
2874 struct ieee80211_vif *vif)
2875{
b327c84c 2876 return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
ae7ba17b
ST
2877}
2878
fd1a54c1
MK
2879/* Common part for MLD and non-MLD ops */
2880void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
2881 struct ieee80211_vif *vif)
8ca151b5 2882{
8ca151b5
JB
2883 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2884
fd1a54c1 2885 lockdep_assert_held(&mvm->mutex);
38a12b5b 2886
fd1a54c1 2887 iwl_mvm_prepare_mac_removal(mvm, vif);
8ca151b5 2888
664322fa 2889 /* Handle AP stop while in CSA */
7f0a7c67
AO
2890 if (rcu_access_pointer(mvm->csa_vif) == vif) {
2891 iwl_mvm_remove_time_event(mvm, mvmvif,
2892 &mvmvif->time_event_data);
664322fa 2893 RCU_INIT_POINTER(mvm->csa_vif, NULL);
e9cb0327 2894 mvmvif->csa_countdown = false;
7f0a7c67 2895 }
664322fa 2896
003e5236
AO
2897 if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2898 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2899 mvm->csa_tx_block_bcn_timeout = 0;
2900 }
2901
5023d966 2902 mvmvif->ap_ibss_active = false;
1c87bbad 2903 mvm->ap_last_beacon_gp2 = 0;
8ca151b5 2904
47242744
TM
2905 if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2906 iwl_mvm_vif_set_low_latency(mvmvif, false,
2907 LOW_LATENCY_VIF_TYPE);
2908 iwl_mvm_send_low_latency_cmd(mvm, false, mvmvif->id);
2909 }
2910
8e484f0b 2911 iwl_mvm_bt_coex_vif_change(mvm);
fd1a54c1
MK
2912}
2913
2914static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2915 struct ieee80211_vif *vif,
2916 struct ieee80211_bss_conf *link_conf)
2917{
2918 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2919
2920 mutex_lock(&mvm->mutex);
2921
2922 iwl_mvm_stop_ap_ibss_common(mvm, vif);
dac94da8 2923
5023d966 2924 /* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
8ca151b5 2925 if (vif->p2p && mvm->p2p_device_vif)
3dfd3a97 2926 iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
8ca151b5 2927
7754ae79 2928 iwl_mvm_update_quotas(mvm, false, NULL);
ced19f26 2929
be82ecd3
AS
2930 iwl_mvm_ftm_responder_clear(mvm, vif);
2931
ced19f26
SS
2932 /*
2933 * This is not very nice, but the simplest:
2934 * For older FWs removing the mcast sta before the bcast station may
2935 * cause assert 0x2b00.
2936 * This is fixed in later FW (which will stop beaconing when removing
2937 * bcast station).
2938 * So make the order of removal depend on the TLV
2939 */
2940 if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2941 iwl_mvm_rm_mcast_sta(mvm, vif);
013290aa 2942 iwl_mvm_send_rm_bcast_sta(mvm, vif);
ced19f26
SS
2943 if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2944 iwl_mvm_rm_mcast_sta(mvm, vif);
8ca151b5 2945 iwl_mvm_binding_remove_vif(mvm, vif);
a11e144e 2946
999609f1 2947 iwl_mvm_power_update_mac(mvm);
a11e144e 2948
8ca151b5
JB
2949 iwl_mvm_mac_ctxt_remove(mvm, vif);
2950
2951 mutex_unlock(&mvm->mutex);
2952}
2953
ae7ba17b
ST
2954static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
2955 struct ieee80211_vif *vif,
b327c84c 2956 struct ieee80211_bss_conf *link_conf)
ae7ba17b 2957{
b327c84c 2958 iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
ae7ba17b
ST
2959}
2960
2961static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
2962 struct ieee80211_vif *vif)
2963{
b327c84c 2964 iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
ae7ba17b
ST
2965}
2966
5023d966
JB
2967static void
2968iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2969 struct ieee80211_vif *vif,
2970 struct ieee80211_bss_conf *bss_conf,
7b7090b4 2971 u64 changes)
8ca151b5 2972{
be2056fc 2973 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8a5e3660 2974
be2056fc
IP
2975 /* Changes will be applied when the AP/IBSS is started */
2976 if (!mvmvif->ap_ibss_active)
2977 return;
2978
863230da 2979 if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
f7d8b702 2980 BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3dfd3a97 2981 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
863230da 2982 IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
8a5e3660 2983
8ca151b5 2984 /* Need to send a new beacon template to the FW */
863230da 2985 if (changes & BSS_CHANGED_BEACON &&
36cf5377 2986 iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
863230da 2987 IWL_WARN(mvm, "Failed updating beacon data\n");
79b7a69d 2988
b73f9a4a
JB
2989 if (changes & BSS_CHANGED_FTM_RESPONDER) {
2990 int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2991
2992 if (ret)
2993 IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2994 ret);
2995 }
2996
8ca151b5
JB
2997}
2998
2999static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3000 struct ieee80211_vif *vif,
3001 struct ieee80211_bss_conf *bss_conf,
7b7090b4 3002 u64 changes)
660eba5a
MK
3003{
3004 struct iwl_mvm_bss_info_changed_ops callbacks = {
3005 .bss_info_changed_sta = iwl_mvm_bss_info_changed_station,
3006 .bss_info_changed_ap_ibss = iwl_mvm_bss_info_changed_ap_ibss,
3007 };
3008
3009 iwl_mvm_bss_info_changed_common(hw, vif, bss_conf, &callbacks,
3010 changes);
3011}
3012
3013void
3014iwl_mvm_bss_info_changed_common(struct ieee80211_hw *hw,
3015 struct ieee80211_vif *vif,
3016 struct ieee80211_bss_conf *bss_conf,
3017 struct iwl_mvm_bss_info_changed_ops *callbacks,
3018 u64 changes)
8ca151b5
JB
3019{
3020 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3021
3022 mutex_lock(&mvm->mutex);
3023
f276e20b 3024 if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
c7d42480 3025 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
723f02ed 3026
8ca151b5
JB
3027 switch (vif->type) {
3028 case NL80211_IFTYPE_STATION:
660eba5a 3029 callbacks->bss_info_changed_sta(mvm, vif, bss_conf, changes);
8ca151b5
JB
3030 break;
3031 case NL80211_IFTYPE_AP:
5023d966 3032 case NL80211_IFTYPE_ADHOC:
660eba5a
MK
3033 callbacks->bss_info_changed_ap_ibss(mvm, vif, bss_conf,
3034 changes);
8ca151b5 3035 break;
91b08c2d
AE
3036 case NL80211_IFTYPE_MONITOR:
3037 if (changes & BSS_CHANGED_MU_GROUPS)
3038 iwl_mvm_update_mu_groups(mvm, vif);
3039 break;
8ca151b5
JB
3040 default:
3041 /* shouldn't happen */
3042 WARN_ON_ONCE(1);
3043 }
3044
9aae43a4
JB
3045 if (changes & BSS_CHANGED_TXPOWER) {
3046 IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3047 bss_conf->txpower);
3048 iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
3049 }
3050
8ca151b5
JB
3051 mutex_unlock(&mvm->mutex);
3052}
3053
cbce62a3
MK
3054int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3055 struct ieee80211_scan_request *hw_req)
8ca151b5
JB
3056{
3057 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3058 int ret;
3059
6749dd80
LC
3060 if (hw_req->req.n_channels == 0 ||
3061 hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
8ca151b5
JB
3062 return -EINVAL;
3063
3064 mutex_lock(&mvm->mutex);
6749dd80 3065 ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
8ca151b5 3066 mutex_unlock(&mvm->mutex);
6749dd80 3067
8ca151b5
JB
3068 return ret;
3069}
3070
cbce62a3
MK
3071void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3072 struct ieee80211_vif *vif)
8ca151b5
JB
3073{
3074 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3075
3076 mutex_lock(&mvm->mutex);
3077
e7d3abab
LC
3078 /* Due to a race condition, it's possible that mac80211 asks
3079 * us to stop a hw_scan when it's already stopped. This can
3080 * happen, for instance, if we stopped the scan ourselves,
3081 * called ieee80211_scan_completed() and the userspace called
3082 * cancel scan scan before ieee80211_scan_work() could run.
3083 * To handle that, simply return if the scan is not running.
3084 */
262888fc 3085 if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
c7d42480 3086 iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
8ca151b5
JB
3087
3088 mutex_unlock(&mvm->mutex);
3089}
3090
cbce62a3 3091void
8ca151b5 3092iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3e56eadf 3093 struct ieee80211_sta *sta, u16 tids,
8ca151b5
JB
3094 int num_frames,
3095 enum ieee80211_frame_release_type reason,
3096 bool more_data)
3097{
3098 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5 3099
3e56eadf 3100 /* Called when we need to transmit (a) frame(s) from mac80211 */
8ca151b5 3101
3e56eadf
JB
3102 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3103 tids, more_data, false);
3104}
3105
cbce62a3 3106void
3e56eadf
JB
3107iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3108 struct ieee80211_sta *sta, u16 tids,
3109 int num_frames,
3110 enum ieee80211_frame_release_type reason,
3111 bool more_data)
3112{
3113 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3114
9a3fcf91 3115 /* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3e56eadf
JB
3116
3117 iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3118 tids, more_data, true);
8ca151b5
JB
3119}
3120
65e25482
JB
3121static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3122 enum sta_notify_cmd cmd,
3123 struct ieee80211_sta *sta)
8ca151b5
JB
3124{
3125 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5b577a90 3126 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
c22b0ff5 3127 unsigned long txqs = 0, tids = 0;
3e56eadf 3128 int tid;
8ca151b5 3129
960f864b
JB
3130 /*
3131 * If we have TVQM then we get too high queue numbers - luckily
3132 * we really shouldn't get here with that because such hardware
3133 * should have firmware supporting buffer station offload.
3134 */
3135 if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3136 return;
3137
c22b0ff5 3138 spin_lock_bh(&mvmsta->lock);
311590a3 3139 for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
c22b0ff5
EG
3140 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3141
6862fcee 3142 if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
1c17627b
SS
3143 continue;
3144
c22b0ff5
EG
3145 __set_bit(tid_data->txq_id, &txqs);
3146
dd32162d 3147 if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
c22b0ff5
EG
3148 continue;
3149
3150 __set_bit(tid, &tids);
3151 }
3152
8ca151b5
JB
3153 switch (cmd) {
3154 case STA_NOTIFY_SLEEP:
c22b0ff5 3155 for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3e56eadf 3156 ieee80211_sta_set_buffered(sta, tid, true);
c22b0ff5
EG
3157
3158 if (txqs)
3159 iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
8ca151b5
JB
3160 /*
3161 * The fw updates the STA to be asleep. Tx packets on the Tx
3162 * queues to this station will not be transmitted. The fw will
3163 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3164 */
3165 break;
3166 case STA_NOTIFY_AWAKE:
c8ee33e1 3167 if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
8ca151b5 3168 break;
c22b0ff5
EG
3169
3170 if (txqs)
3171 iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
9cc40712 3172 iwl_mvm_sta_modify_ps_wake(mvm, sta);
8ca151b5
JB
3173 break;
3174 default:
3175 break;
3176 }
c22b0ff5 3177 spin_unlock_bh(&mvmsta->lock);
8ca151b5
JB
3178}
3179
cbce62a3
MK
3180void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3181 enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
65e25482
JB
3182{
3183 __iwl_mvm_mac_sta_notify(hw, cmd, sta);
3184}
3185
3186void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3187{
3188 struct iwl_rx_packet *pkt = rxb_addr(rxb);
3189 struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3190 struct ieee80211_sta *sta;
3191 struct iwl_mvm_sta *mvmsta;
3192 bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3193
be9ae34e 3194 if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
65e25482
JB
3195 return;
3196
3197 rcu_read_lock();
a9560029 3198 sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
65e25482
JB
3199 if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3200 rcu_read_unlock();
3201 return;
3202 }
3203
3204 mvmsta = iwl_mvm_sta_from_mac80211(sta);
3205
3206 if (!mvmsta->vif ||
3207 mvmsta->vif->type != NL80211_IFTYPE_AP) {
3208 rcu_read_unlock();
3209 return;
3210 }
3211
3212 if (mvmsta->sleeping != sleeping) {
3213 mvmsta->sleeping = sleeping;
3214 __iwl_mvm_mac_sta_notify(mvm->hw,
3215 sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3216 sta);
3217 ieee80211_sta_ps_transition(sta, sleeping);
3218 }
3219
3220 if (sleeping) {
3221 switch (notif->type) {
3222 case IWL_MVM_PM_EVENT_AWAKE:
3223 case IWL_MVM_PM_EVENT_ASLEEP:
3224 break;
3225 case IWL_MVM_PM_EVENT_UAPSD:
3226 ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3227 break;
3228 case IWL_MVM_PM_EVENT_PS_POLL:
3229 ieee80211_sta_pspoll(sta);
3230 break;
3231 default:
3232 break;
3233 }
3234 }
3235
3236 rcu_read_unlock();
3237}
3238
cbce62a3
MK
3239void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3240 struct ieee80211_vif *vif,
3241 struct ieee80211_sta *sta)
1ddbbb0c
JB
3242{
3243 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 3244 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1ddbbb0c
JB
3245
3246 /*
3247 * This is called before mac80211 does RCU synchronisation,
3248 * so here we already invalidate our internal RCU-protected
3249 * station pointer. The rest of the code will thus no longer
3250 * be able to find the station this way, and we don't rely
3251 * on further RCU synchronisation after the sta_state()
3252 * callback deleted the station.
3253 */
3254 mutex_lock(&mvm->mutex);
c8ee33e1
GG
3255 if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id]))
3256 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->deflink.sta_id],
1ddbbb0c 3257 ERR_PTR(-ENOENT));
94939080 3258
1ddbbb0c
JB
3259 mutex_unlock(&mvm->mutex);
3260}
3261
bd1ba664
JB
3262static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3263 const u8 *bssid)
3264{
b0ffe455
JB
3265 int i;
3266
3267 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3268 struct iwl_mvm_tcm_mac *mdata;
3269
3270 mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3271 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3272 mdata->opened_rx_ba_sessions = false;
3273 }
3274
bd1ba664
JB
3275 if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3276 return;
3277
c5241b0c 3278 if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
cee5a882
AA
3279 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3280 return;
3281 }
3282
11dee0b4
EG
3283 if (!vif->p2p &&
3284 (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
bd1ba664
JB
3285 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3286 return;
3287 }
3288
b0ffe455
JB
3289 for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3290 if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3291 vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3292 return;
3293 }
3294 }
3295
bd1ba664
JB
3296 vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3297}
3298
1e8f1329
GBA
3299static void
3300iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3301 struct ieee80211_vif *vif, u8 *peer_addr,
3302 enum nl80211_tdls_operation action)
3303{
3304 struct iwl_fw_dbg_trigger_tlv *trig;
3305 struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3306
6c042d75
SS
3307 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3308 FW_DBG_TRIGGER_TDLS);
3309 if (!trig)
1e8f1329
GBA
3310 return;
3311
1e8f1329 3312 tdls_trig = (void *)trig->data;
1e8f1329
GBA
3313
3314 if (!(tdls_trig->action_bitmap & BIT(action)))
3315 return;
3316
3317 if (tdls_trig->peer_mode &&
3318 memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3319 return;
3320
7174beb6
JB
3321 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3322 "TDLS event occurred, peer %pM, action %d",
3323 peer_addr, action);
1e8f1329
GBA
3324}
3325
4f58121d
IP
3326struct iwl_mvm_he_obss_narrow_bw_ru_data {
3327 bool tolerated;
3328};
3329
3330static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3331 struct cfg80211_bss *bss,
3332 void *_data)
3333{
3334 struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
6c608cd6 3335 const struct cfg80211_bss_ies *ies;
4f58121d
IP
3336 const struct element *elem;
3337
6c608cd6
JB
3338 rcu_read_lock();
3339 ies = rcu_dereference(bss->ies);
3340 elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3341 ies->len);
4f58121d
IP
3342
3343 if (!elem || elem->datalen < 10 ||
3344 !(elem->data[10] &
3345 WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3346 data->tolerated = false;
3347 }
6c608cd6 3348 rcu_read_unlock();
4f58121d
IP
3349}
3350
3351static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3352 struct ieee80211_vif *vif)
3353{
3354 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3355 struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3356 .tolerated = true,
3357 };
3358
3359 if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
650cadb7 3360 mvmvif->deflink.he_ru_2mhz_block = false;
4f58121d
IP
3361 return;
3362 }
3363
3364 cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
3365 iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3366 &iter_data);
3367
3368 /*
3369 * If there is at least one AP on radar channel that cannot
3370 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3371 */
650cadb7 3372 mvmvif->deflink.he_ru_2mhz_block = !iter_data.tolerated;
4f58121d
IP
3373}
3374
f7d6ef33
JB
3375static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3376 struct ieee80211_vif *vif)
3377{
3378 struct ieee80211_supported_band *sband;
3379 const struct ieee80211_sta_he_cap *he_cap;
3380
3381 if (vif->type != NL80211_IFTYPE_STATION)
3382 return;
3383
3384 if (!mvm->cca_40mhz_workaround)
3385 return;
3386
3387 /* decrement and check that we reached zero */
3388 mvm->cca_40mhz_workaround--;
3389 if (mvm->cca_40mhz_workaround)
3390 return;
3391
3392 sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3393
3394 sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3395
3396 he_cap = ieee80211_get_he_iftype_cap(sband,
3397 ieee80211_vif_type_p2p(vif));
3398
3399 if (he_cap) {
3400 /* we know that ours is writable */
0301bcd5 3401 struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
f7d6ef33
JB
3402
3403 he->he_cap_elem.phy_cap_info[0] |=
3404 IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3405 }
3406}
3407
6d19a5eb
EG
3408static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3409 struct ieee80211_vif *vif,
3410 struct iwl_mvm_sta *mvm_sta)
3411{
3412#if IS_ENABLED(CONFIG_IWLMEI)
3413 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3414 struct iwl_mei_conn_info conn_info = {
f276e20b 3415 .ssid_len = vif->cfg.ssid_len,
6d19a5eb
EG
3416 .channel = vif->bss_conf.chandef.chan->hw_value,
3417 };
3418
3419 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3420 return;
3421
3422 if (!mvm->mei_registered)
3423 return;
3424
3425 switch (mvm_sta->pairwise_cipher) {
e5d3a64e
AS
3426 case WLAN_CIPHER_SUITE_TKIP:
3427 conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3428 break;
6d19a5eb
EG
3429 case WLAN_CIPHER_SUITE_CCMP:
3430 conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3431 break;
3432 case WLAN_CIPHER_SUITE_GCMP:
3433 conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3434 break;
3435 case WLAN_CIPHER_SUITE_GCMP_256:
3436 conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3437 break;
3438 case 0:
3439 /* open profile */
3440 break;
3441 default:
3442 /* cipher not supported, don't send anything to iwlmei */
3443 return;
3444 }
3445
3446 switch (mvmvif->rekey_data.akm) {
3447 case WLAN_AKM_SUITE_SAE & 0xff:
3448 conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3449 break;
3450 case WLAN_AKM_SUITE_PSK & 0xff:
3451 conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3452 break;
3453 case WLAN_AKM_SUITE_8021X & 0xff:
3454 conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3455 break;
3456 case 0:
3457 /* open profile */
3458 conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3459 break;
3460 default:
3461 /* auth method / AKM not supported */
3462 /* TODO: All the FT vesions of these? */
3463 return;
3464 }
3465
f276e20b 3466 memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
6d19a5eb
EG
3467 memcpy(conn_info.bssid, vif->bss_conf.bssid, ETH_ALEN);
3468
3469 /* TODO: add support for collocated AP data */
3470 iwl_mei_host_associated(&conn_info, NULL);
3471#endif
3472}
3473
87f7e243
MK
3474static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3475 struct ieee80211_vif *vif,
3476 bool force_assoc_off)
3477{
3478 return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3479}
3480
8ca151b5
JB
3481static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3482 struct ieee80211_vif *vif,
3483 struct ieee80211_sta *sta,
3484 enum ieee80211_sta_state old_state,
3485 enum ieee80211_sta_state new_state)
87f7e243
MK
3486{
3487 struct iwl_mvm_sta_state_ops callbacks = {
3488 .add_sta = iwl_mvm_add_sta,
3489 .update_sta = iwl_mvm_update_sta,
3490 .rm_sta = iwl_mvm_rm_sta,
3491 .mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3492 };
3493
3494 return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3495 &callbacks);
3496}
3497
3498/* Common part for MLD and non-MLD modes */
3499int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
3500 struct ieee80211_vif *vif,
3501 struct ieee80211_sta *sta,
3502 enum ieee80211_sta_state old_state,
3503 enum ieee80211_sta_state new_state,
3504 struct iwl_mvm_sta_state_ops *callbacks)
8ca151b5
JB
3505{
3506 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3507 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6ea29ce5 3508 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
3509 int ret;
3510
3511 IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3512 sta->addr, old_state, new_state);
3513
3514 /* this would be a mac80211 bug ... but don't crash */
650cadb7 3515 if (WARN_ON_ONCE(!mvmvif->deflink.phy_ctxt))
fe56d05e 3516 return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
8ca151b5 3517
24afba76
LK
3518 /*
3519 * If we are in a STA removal flow and in DQA mode:
3520 *
3521 * This is after the sync_rcu part, so the queues have already been
3522 * flushed. No more TXs on their way in mac80211's path, and no more in
3523 * the queues.
3524 * Also, we won't be getting any new TX frames for this station.
3525 * What we might have are deferred TX frames that need to be taken care
3526 * of.
3527 *
3528 * Drop any still-queued deferred-frame before removing the STA, and
3529 * make sure the worker is no longer handling frames for this STA.
3530 */
3531 if (old_state == IEEE80211_STA_NONE &&
c8f54701 3532 new_state == IEEE80211_STA_NOTEXIST) {
24afba76
LK
3533 flush_work(&mvm->add_stream_wk);
3534
3535 /*
3536 * No need to make sure deferred TX indication is off since the
3537 * worker will already remove it if it was on
3538 */
f7d6ef33
JB
3539
3540 /*
3541 * Additionally, reset the 40 MHz capability if we disconnected
3542 * from the AP now.
3543 */
3544 iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
24afba76
LK
3545 }
3546
8ca151b5 3547 mutex_lock(&mvm->mutex);
6ea29ce5 3548 /* track whether or not the station is associated */
d94c5a82 3549 mvm_sta->sta_state = new_state;
6ea29ce5 3550
8ca151b5
JB
3551 if (old_state == IEEE80211_STA_NOTEXIST &&
3552 new_state == IEEE80211_STA_NONE) {
48bc1307
JB
3553 /*
3554 * Firmware bug - it'll crash if the beacon interval is less
3555 * than 16. We can't avoid connecting at all, so refuse the
3556 * station state change, this will cause mac80211 to abandon
3557 * attempts to connect to this AP, and eventually wpa_s will
cdaba917 3558 * blocklist the AP...
48bc1307
JB
3559 */
3560 if (vif->type == NL80211_IFTYPE_STATION &&
3561 vif->bss_conf.beacon_int < 16) {
3562 IWL_ERR(mvm,
3563 "AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3564 sta->addr, vif->bss_conf.beacon_int);
3565 ret = -EINVAL;
3566 goto out_unlock;
3567 }
cf7b491d 3568
97cc1694 3569 if (vif->type == NL80211_IFTYPE_STATION)
046d2e7c 3570 vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
97cc1694 3571
cf7b491d
AN
3572 if (sta->tdls &&
3573 (vif->p2p ||
fa3d07e4
AN
3574 iwl_mvm_tdls_sta_count(mvm, NULL) ==
3575 IWL_MVM_TDLS_STA_COUNT ||
cf7b491d
AN
3576 iwl_mvm_phy_ctx_count(mvm) > 1)) {
3577 IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3578 ret = -EBUSY;
3579 goto out_unlock;
3580 }
3581
87f7e243 3582 ret = callbacks->add_sta(mvm, vif, sta);
1e8f1329 3583 if (sta->tdls && ret == 0) {
fa3d07e4 3584 iwl_mvm_recalc_tdls_state(mvm, vif, true);
1e8f1329
GBA
3585 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3586 NL80211_TDLS_SETUP);
3587 }
438af969 3588
4c51541d 3589 sta->deflink.agg.max_rc_amsdu_len = 1;
8ca151b5
JB
3590 } else if (old_state == IEEE80211_STA_NONE &&
3591 new_state == IEEE80211_STA_AUTH) {
e820c2da
HD
3592 /*
3593 * EBS may be disabled due to previous failures reported by FW.
3594 * Reset EBS status here assuming environment has been changed.
3595 */
3596 mvm->last_ebs_successful = true;
bd1ba664 3597 iwl_mvm_check_uapsd(mvm, vif, sta->addr);
8ca151b5
JB
3598 ret = 0;
3599 } else if (old_state == IEEE80211_STA_AUTH &&
3600 new_state == IEEE80211_STA_ASSOC) {
8be30c13 3601 if (vif->type == NL80211_IFTYPE_AP) {
046d2e7c 3602 vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
8be30c13 3603 mvmvif->ap_assoc_sta_count++;
87f7e243
MK
3604 callbacks->mac_ctxt_changed(mvm, vif, false);
3605 if (!mvm->mld_api_is_used &&
3606 ((vif->bss_conf.he_support &&
cb63eb43
MK
3607 !iwlwifi_mod_params.disable_11ax) ||
3608 (vif->bss_conf.eht_support &&
87f7e243 3609 !iwlwifi_mod_params.disable_11be)))
c8ee33e1
GG
3610 iwl_mvm_cfg_he_sta(mvm, vif,
3611 mvm_sta->deflink.sta_id);
9394662a 3612 } else if (vif->type == NL80211_IFTYPE_STATION) {
046d2e7c 3613 vif->bss_conf.he_support = sta->deflink.he_cap.has_he;
4f58121d 3614
650cadb7 3615 mvmvif->deflink.he_ru_2mhz_block = false;
046d2e7c 3616 if (sta->deflink.he_cap.has_he)
4f58121d
IP
3617 iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
3618
87f7e243
MK
3619 callbacks->mac_ctxt_changed(mvm, vif, false);
3620
3621 if (mvm->mld_api_is_used)
cacc1d42 3622 iwl_mvm_link_changed(mvm, vif, &vif->bss_conf,
87f7e243
MK
3623 LINK_CONTEXT_MODIFY_ALL &
3624 ~LINK_CONTEXT_MODIFY_ACTIVE,
3625 true);
8be30c13 3626 }
735a0045 3627
650cadb7
GG
3628 iwl_mvm_rs_rate_init(mvm, sta,
3629 mvmvif->deflink.phy_ctxt->channel->band,
3baf7528 3630 false);
87f7e243 3631 ret = callbacks->update_sta(mvm, vif, sta);
8ca151b5
JB
3632 } else if (old_state == IEEE80211_STA_ASSOC &&
3633 new_state == IEEE80211_STA_AUTHORIZED) {
475c6bde 3634 ret = 0;
f59e0e3c
AN
3635
3636 /* we don't support TDLS during DCM */
3637 if (iwl_mvm_phy_ctx_count(mvm) > 1)
3638 iwl_mvm_teardown_tdls_peers(mvm);
3639
4634b176 3640 if (sta->tdls) {
1e8f1329
GBA
3641 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3642 NL80211_TDLS_ENABLE_LINK);
4634b176
JB
3643 } else {
3644 /* enable beacon filtering */
3645 WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
1e8f1329 3646
8b75858c
JB
3647 mvmvif->authorized = 1;
3648
87f7e243 3649 callbacks->mac_ctxt_changed(mvm, vif, false);
6d19a5eb 3650 iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
4634b176 3651 }
50f56044 3652
650cadb7
GG
3653 iwl_mvm_rs_rate_init(mvm, sta,
3654 mvmvif->deflink.phy_ctxt->channel->band,
3baf7528 3655 true);
8ca151b5
JB
3656 } else if (old_state == IEEE80211_STA_AUTHORIZED &&
3657 new_state == IEEE80211_STA_ASSOC) {
d9e95e35
MG
3658 /* once we move into assoc state, need to update rate scale to
3659 * disable using wide bandwidth
3660 */
650cadb7
GG
3661 iwl_mvm_rs_rate_init(mvm, sta,
3662 mvmvif->deflink.phy_ctxt->channel->band,
d9e95e35 3663 false);
4634b176 3664 if (!sta->tdls) {
8b75858c 3665 /*
f5034bcf
MK
3666 * Set this but don't call iwl_mvm_mac_ctxt_changed()
3667 * yet to avoid sending high prio again for a little
3668 * time.
8b75858c
JB
3669 */
3670 mvmvif->authorized = 0;
3671
4634b176
JB
3672 /* disable beacon filtering */
3673 ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3674 WARN_ON(ret &&
3675 !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3676 &mvm->status));
3677 }
8ca151b5
JB
3678 ret = 0;
3679 } else if (old_state == IEEE80211_STA_ASSOC &&
3680 new_state == IEEE80211_STA_AUTH) {
8be30c13
AB
3681 if (vif->type == NL80211_IFTYPE_AP) {
3682 mvmvif->ap_assoc_sta_count--;
87f7e243 3683 callbacks->mac_ctxt_changed(mvm, vif, false);
d5d8ee52 3684 } else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
cf7a7457 3685 iwl_mvm_stop_session_protection(mvm, vif);
8ca151b5
JB
3686 ret = 0;
3687 } else if (old_state == IEEE80211_STA_AUTH &&
3688 new_state == IEEE80211_STA_NONE) {
3689 ret = 0;
3690 } else if (old_state == IEEE80211_STA_NONE &&
3691 new_state == IEEE80211_STA_NOTEXIST) {
d5d8ee52
EG
3692 if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3693 iwl_mvm_stop_session_protection(mvm, vif);
87f7e243 3694 ret = callbacks->rm_sta(mvm, vif, sta);
1e8f1329 3695 if (sta->tdls) {
fa3d07e4 3696 iwl_mvm_recalc_tdls_state(mvm, vif, false);
1e8f1329
GBA
3697 iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3698 NL80211_TDLS_DISABLE_LINK);
3699 }
34a880d8 3700
44135b7c
IP
3701 if (unlikely(ret &&
3702 test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3703 &mvm->status)))
3704 ret = 0;
8ca151b5
JB
3705 } else {
3706 ret = -EIO;
3707 }
48bc1307 3708 out_unlock:
8ca151b5
JB
3709 mutex_unlock(&mvm->mutex);
3710
9c126cd6
LK
3711 if (sta->tdls && ret == 0) {
3712 if (old_state == IEEE80211_STA_NOTEXIST &&
3713 new_state == IEEE80211_STA_NONE)
3714 ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3715 else if (old_state == IEEE80211_STA_NONE &&
3716 new_state == IEEE80211_STA_NOTEXIST)
3717 ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3718 }
3719
8ca151b5
JB
3720 return ret;
3721}
3722
cbce62a3 3723int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
8ca151b5
JB
3724{
3725 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3726
3727 mvm->rts_threshold = value;
3728
3729 return 0;
3730}
3731
cbce62a3
MK
3732void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3733 struct ieee80211_sta *sta, u32 changed)
1f3b0ff8
LE
3734{
3735 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
dcfe3b10
JB
3736 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3737
3738 if (changed & (IEEE80211_RC_BW_CHANGED |
3739 IEEE80211_RC_SUPP_RATES_CHANGED |
3740 IEEE80211_RC_NSS_CHANGED))
650cadb7
GG
3741 iwl_mvm_rs_rate_init(mvm, sta,
3742 mvmvif->deflink.phy_ctxt->channel->band,
dcfe3b10 3743 true);
1f3b0ff8
LE
3744
3745 if (vif->type == NL80211_IFTYPE_STATION &&
3746 changed & IEEE80211_RC_NSS_CHANGED)
3747 iwl_mvm_sf_update(mvm, vif, false);
3748}
3749
8ca151b5 3750static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
b3e2130b
JB
3751 struct ieee80211_vif *vif,
3752 unsigned int link_id, u16 ac,
8ca151b5
JB
3753 const struct ieee80211_tx_queue_params *params)
3754{
3755 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3756 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3757
650cadb7 3758 mvmvif->deflink.queue_params[ac] = *params;
8ca151b5
JB
3759
3760 /*
3761 * No need to update right away, we'll get BSS_CHANGED_QOS
3762 * The exception is P2P_DEVICE interface which needs immediate update.
3763 */
3764 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3765 int ret;
3766
3767 mutex_lock(&mvm->mutex);
3dfd3a97 3768 ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
8ca151b5
JB
3769 mutex_unlock(&mvm->mutex);
3770 return ret;
3771 }
3772 return 0;
3773}
3774
cbce62a3
MK
3775void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3776 struct ieee80211_vif *vif,
3777 struct ieee80211_prep_tx_info *info)
8ca151b5
JB
3778{
3779 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
d4e36e55 3780
8ca151b5 3781 mutex_lock(&mvm->mutex);
af84ac57 3782 iwl_mvm_protect_assoc(mvm, vif, info->duration);
8ca151b5
JB
3783 mutex_unlock(&mvm->mutex);
3784}
3785
cbce62a3
MK
3786void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
3787 struct ieee80211_vif *vif,
3788 struct ieee80211_prep_tx_info *info)
6b1259d1
JB
3789{
3790 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3791
3792 /* for successful cases (auth/assoc), don't cancel session protection */
3793 if (info->success)
3794 return;
3795
3796 mutex_lock(&mvm->mutex);
3797 iwl_mvm_stop_session_protection(mvm, vif);
3798 mutex_unlock(&mvm->mutex);
3799}
3800
cbce62a3
MK
3801int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3802 struct ieee80211_vif *vif,
3803 struct cfg80211_sched_scan_request *req,
3804 struct ieee80211_scan_ies *ies)
35a000b7
DS
3805{
3806 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
35a000b7 3807
35a000b7 3808 int ret;
4660dfbb 3809
35a000b7
DS
3810 mutex_lock(&mvm->mutex);
3811
f276e20b 3812 if (!vif->cfg.idle) {
bd5e4744
DS
3813 ret = -EBUSY;
3814 goto out;
3815 }
3816
19945dfb 3817 ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
d2496221 3818
35a000b7
DS
3819out:
3820 mutex_unlock(&mvm->mutex);
3821 return ret;
3822}
3823
cbce62a3
MK
3824int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3825 struct ieee80211_vif *vif)
35a000b7
DS
3826{
3827 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
33ea27f6 3828 int ret;
35a000b7
DS
3829
3830 mutex_lock(&mvm->mutex);
e7d3abab
LC
3831
3832 /* Due to a race condition, it's possible that mac80211 asks
3833 * us to stop a sched_scan when it's already stopped. This
3834 * can happen, for instance, if we stopped the scan ourselves,
3835 * called ieee80211_sched_scan_stopped() and the userspace called
3836 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3837 * could run. To handle this, simply return if the scan is
3838 * not running.
3839 */
262888fc 3840 if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
e7d3abab
LC
3841 mutex_unlock(&mvm->mutex);
3842 return 0;
3843 }
3844
c7d42480 3845 ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
35a000b7 3846 mutex_unlock(&mvm->mutex);
33ea27f6 3847 iwl_mvm_wait_for_async_handlers(mvm);
37e3308c 3848
33ea27f6 3849 return ret;
35a000b7
DS
3850}
3851
6569e7d3
JB
3852static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3853 enum set_key_cmd cmd,
3854 struct ieee80211_vif *vif,
3855 struct ieee80211_sta *sta,
3856 struct ieee80211_key_conf *key)
8ca151b5 3857{
c56e00a3 3858 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 3859 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
46c7b05a 3860 struct iwl_mvm_sta *mvmsta = NULL;
f5e28eac
JB
3861 struct iwl_mvm_key_pn *ptk_pn;
3862 int keyidx = key->keyidx;
5c75a208
JB
3863 u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
3864 u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
c56e00a3 3865 int ret, i;
d6ee54a9 3866 u8 key_offset;
8ca151b5 3867
46c7b05a
EG
3868 if (sta)
3869 mvmsta = iwl_mvm_sta_from_mac80211(sta);
6d19a5eb 3870
8ca151b5
JB
3871 switch (key->cipher) {
3872 case WLAN_CIPHER_SUITE_TKIP:
286ca8eb 3873 if (!mvm->trans->trans_cfg->gen2) {
7f768ad5
DS
3874 key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3875 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3876 } else if (vif->type == NL80211_IFTYPE_STATION) {
3877 key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
3878 } else {
3879 IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
3880 return -EOPNOTSUPP;
3881 }
8ca151b5 3882 break;
ca8c0f4b 3883 case WLAN_CIPHER_SUITE_CCMP:
2a53d166
AB
3884 case WLAN_CIPHER_SUITE_GCMP:
3885 case WLAN_CIPHER_SUITE_GCMP_256:
85aeb58c
DS
3886 if (!iwl_mvm_has_new_tx_api(mvm))
3887 key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
ca8c0f4b 3888 break;
8ca151b5 3889 case WLAN_CIPHER_SUITE_AES_CMAC:
8e160ab8
AB
3890 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3891 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
30686bf7 3892 WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
8ca151b5
JB
3893 break;
3894 case WLAN_CIPHER_SUITE_WEP40:
3895 case WLAN_CIPHER_SUITE_WEP104:
475c6bde
JB
3896 if (vif->type == NL80211_IFTYPE_STATION)
3897 break;
3898 if (iwl_mvm_has_new_tx_api(mvm))
3899 return -EOPNOTSUPP;
3900 /* support HW crypto on TX */
3901 return 0;
8ca151b5 3902 default:
8b3d2c48 3903 return -EOPNOTSUPP;
8ca151b5
JB
3904 }
3905
8ca151b5
JB
3906 switch (cmd) {
3907 case SET_KEY:
a5de7de7
JB
3908 if (vif->type == NL80211_IFTYPE_STATION &&
3909 (keyidx == 6 || keyidx == 7))
b1fdc250
JB
3910 rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3911 key);
3912
5023d966
JB
3913 if ((vif->type == NL80211_IFTYPE_ADHOC ||
3914 vif->type == NL80211_IFTYPE_AP) && !sta) {
3915 /*
3916 * GTK on AP interface is a TX-only key, return 0;
3917 * on IBSS they're per-station and because we're lazy
3918 * we don't support them for RX, so do the same.
8e160ab8 3919 * CMAC/GMAC in AP/IBSS modes must be done in software.
a5de7de7
JB
3920 *
3921 * Except, of course, beacon protection - it must be
3922 * offloaded since we just set a beacon template.
5023d966 3923 */
a5de7de7
JB
3924 if (keyidx < 6 &&
3925 (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3926 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3927 key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
81279c49 3928 ret = -EOPNOTSUPP;
a1c2ff30
AO
3929 break;
3930 }
85aeb58c
DS
3931
3932 if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
3933 key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
3934 !iwl_mvm_has_new_tx_api(mvm)) {
3935 key->hw_key_idx = STA_KEY_IDX_INVALID;
a1c2ff30 3936 ret = 0;
85aeb58c
DS
3937 break;
3938 }
c56e00a3
JB
3939
3940 if (!mvmvif->ap_ibss_active) {
3941 for (i = 0;
3942 i < ARRAY_SIZE(mvmvif->ap_early_keys);
3943 i++) {
3944 if (!mvmvif->ap_early_keys[i]) {
3945 mvmvif->ap_early_keys[i] = key;
3946 break;
3947 }
3948 }
3949
3950 if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3951 ret = -ENOSPC;
a1c2ff30
AO
3952 else
3953 ret = 0;
c56e00a3
JB
3954
3955 break;
3956 }
6caffd4f
JB
3957 }
3958
b546dcd6
JB
3959 /* During FW restart, in order to restore the state as it was,
3960 * don't try to reprogram keys we previously failed for.
3961 */
3962 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3963 key->hw_key_idx == STA_KEY_IDX_INVALID) {
3964 IWL_DEBUG_MAC80211(mvm,
3965 "skip invalid idx key programming during restart\n");
3966 ret = 0;
3967 break;
3968 }
3969
f5e28eac 3970 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
46c7b05a 3971 mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
f5e28eac
JB
3972 key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3973 (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
2a53d166
AB
3974 key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3975 key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
f5e28eac
JB
3976 struct ieee80211_key_seq seq;
3977 int tid, q;
3978
f5e28eac 3979 WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
acafe7e3
KC
3980 ptk_pn = kzalloc(struct_size(ptk_pn, q,
3981 mvm->trans->num_rx_queues),
f5e28eac
JB
3982 GFP_KERNEL);
3983 if (!ptk_pn) {
3984 ret = -ENOMEM;
3985 break;
3986 }
3987
3988 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3989 ieee80211_get_key_rx_seq(key, tid, &seq);
3990 for (q = 0; q < mvm->trans->num_rx_queues; q++)
3991 memcpy(ptk_pn->q[q].pn[tid],
3992 seq.ccmp.pn,
3993 IEEE80211_CCMP_PN_LEN);
3994 }
3995
3996 rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3997 }
3998
d6ee54a9
LC
3999 /* in HW restart reuse the index, otherwise request a new one */
4000 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4001 key_offset = key->hw_key_idx;
4002 else
4003 key_offset = STA_KEY_IDX_INVALID;
4004
46c7b05a 4005 if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
6d19a5eb
EG
4006 mvmsta->pairwise_cipher = key->cipher;
4007
a5de7de7
JB
4008 IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4009 sta ? sta->addr : NULL, key->keyidx);
5c75a208
JB
4010
4011 if (sec_key_ver)
4012 ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4013 else
4014 ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4015
8ca151b5
JB
4016 if (ret) {
4017 IWL_WARN(mvm, "set key failed\n");
475c6bde 4018 key->hw_key_idx = STA_KEY_IDX_INVALID;
8ca151b5
JB
4019 /*
4020 * can't add key for RX, but we don't need it
475c6bde
JB
4021 * in the device for TX so still return 0,
4022 * unless we have new TX API where we cannot
4023 * put key material into the TX_CMD
8ca151b5 4024 */
475c6bde
JB
4025 if (iwl_mvm_has_new_tx_api(mvm))
4026 ret = -EOPNOTSUPP;
4027 else
4028 ret = 0;
8ca151b5
JB
4029 }
4030
4031 break;
4032 case DISABLE_KEY:
a5de7de7
JB
4033 if (vif->type == NL80211_IFTYPE_STATION &&
4034 (keyidx == 6 || keyidx == 7))
b1fdc250
JB
4035 RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4036 NULL);
4037
c56e00a3
JB
4038 ret = -ENOENT;
4039 for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4040 if (mvmvif->ap_early_keys[i] == key) {
4041 mvmvif->ap_early_keys[i] = NULL;
4042 ret = 0;
4043 }
4044 }
4045
4046 /* found in pending list - don't do anything else */
4047 if (ret == 0)
4048 break;
4049
6caffd4f
JB
4050 if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4051 ret = 0;
4052 break;
4053 }
4054
46c7b05a 4055 if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
f5e28eac
JB
4056 key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4057 (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
2a53d166
AB
4058 key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4059 key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
f5e28eac
JB
4060 ptk_pn = rcu_dereference_protected(
4061 mvmsta->ptk_pn[keyidx],
4062 lockdep_is_held(&mvm->mutex));
4063 RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4064 if (ptk_pn)
4065 kfree_rcu(ptk_pn, rcu_head);
4066 }
4067
8ca151b5 4068 IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
5c75a208
JB
4069 if (sec_key_ver)
4070 ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4071 else
4072 ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
8ca151b5
JB
4073 break;
4074 default:
4075 ret = -EINVAL;
4076 }
4077
6569e7d3
JB
4078 return ret;
4079}
4080
cbce62a3
MK
4081int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4082 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4083 struct ieee80211_key_conf *key)
6569e7d3
JB
4084{
4085 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4086 int ret;
4087
4088 mutex_lock(&mvm->mutex);
4089 ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
8ca151b5 4090 mutex_unlock(&mvm->mutex);
6569e7d3 4091
8ca151b5
JB
4092 return ret;
4093}
4094
cbce62a3
MK
4095void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4096 struct ieee80211_vif *vif,
4097 struct ieee80211_key_conf *keyconf,
4098 struct ieee80211_sta *sta,
4099 u32 iv32, u16 *phase1key)
8ca151b5
JB
4100{
4101 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4102
5023d966
JB
4103 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4104 return;
4105
8ca151b5
JB
4106 iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4107}
4108
4109
b112889c
AM
4110static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4111 struct iwl_rx_packet *pkt, void *data)
4112{
4113 struct iwl_mvm *mvm =
4114 container_of(notif_wait, struct iwl_mvm, notif_wait);
4115 struct iwl_hs20_roc_res *resp;
4116 int resp_len = iwl_rx_packet_payload_len(pkt);
4117 struct iwl_mvm_time_event_data *te_data = data;
4118
4119 if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4120 return true;
4121
4122 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4123 IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4124 return true;
4125 }
4126
4127 resp = (void *)pkt->data;
4128
4129 IWL_DEBUG_TE(mvm,
b71a9c35 4130 "Aux ROC: Received response from ucode: status=%d uid=%d\n",
b112889c
AM
4131 resp->status, resp->event_unique_id);
4132
4133 te_data->uid = le32_to_cpu(resp->event_unique_id);
4134 IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4135 te_data->uid);
4136
4137 spin_lock_bh(&mvm->time_event_lock);
4138 list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4139 spin_unlock_bh(&mvm->time_event_lock);
4140
4141 return true;
4142}
4143
dc28e12f
MG
4144#define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
4145#define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
4146#define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
4147#define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
4148#define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
b112889c
AM
4149static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4150 struct ieee80211_channel *channel,
4151 struct ieee80211_vif *vif,
4152 int duration)
4153{
afc1e3b4 4154 int res;
b112889c
AM
4155 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4156 struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
6eb031d2 4157 static const u16 time_event_response[] = { HOT_SPOT_CMD };
b112889c 4158 struct iwl_notification_wait wait_time_event;
dc28e12f
MG
4159 u32 dtim_interval = vif->bss_conf.dtim_period *
4160 vif->bss_conf.beacon_int;
4161 u32 req_dur, delay;
b112889c
AM
4162 struct iwl_hs20_roc_req aux_roc_req = {
4163 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4164 .id_and_color =
4165 cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4166 .sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
57e861d9
DS
4167 };
4168 struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4169 &aux_roc_req.channel_info);
4170 u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4171
4172 /* Set the channel info data */
4173 iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
3717f91a 4174 iwl_mvm_phy_band_from_nl80211(channel->band),
7ac87575 4175 IWL_PHY_CHANNEL_MODE20,
57e861d9
DS
4176 0);
4177
4178 /* Set the time and duration */
afc1e3b4 4179 tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
b112889c 4180
dc28e12f
MG
4181 delay = AUX_ROC_MIN_DELAY;
4182 req_dur = MSEC_TO_TU(duration);
4183
4184 /*
4185 * If we are associated we want the delay time to be at least one
4186 * dtim interval so that the FW can wait until after the DTIM and
4187 * then start the time event, this will potentially allow us to
4188 * remain off-channel for the max duration.
4189 * Since we want to use almost a whole dtim interval we would also
4190 * like the delay to be for 2-3 dtim intervals, in case there are
4191 * other time events with higher priority.
4192 */
f276e20b 4193 if (vif->cfg.assoc) {
dc28e12f
MG
4194 delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
4195 /* We cannot remain off-channel longer than the DTIM interval */
4196 if (dtim_interval <= req_dur) {
4197 req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
4198 if (req_dur <= AUX_ROC_MIN_DURATION)
4199 req_dur = dtim_interval -
4200 AUX_ROC_MIN_SAFETY_BUFFER;
4201 }
4202 }
4203
57e861d9
DS
4204 tail->duration = cpu_to_le32(req_dur);
4205 tail->apply_time_max_delay = cpu_to_le32(delay);
dc28e12f
MG
4206
4207 IWL_DEBUG_TE(mvm,
903b3f9b
EG
4208 "ROC: Requesting to remain on channel %u for %ums\n",
4209 channel->hw_value, req_dur);
4210 IWL_DEBUG_TE(mvm,
4211 "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
4212 duration, delay, dtim_interval);
4213
b112889c 4214 /* Set the node address */
57e861d9 4215 memcpy(tail->node_addr, vif->addr, ETH_ALEN);
b112889c 4216
a6cc5163
MG
4217 lockdep_assert_held(&mvm->mutex);
4218
4219 spin_lock_bh(&mvm->time_event_lock);
4220
4221 if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4222 spin_unlock_bh(&mvm->time_event_lock);
4223 return -EIO;
4224 }
4225
b112889c
AM
4226 te_data->vif = vif;
4227 te_data->duration = duration;
4228 te_data->id = HOT_SPOT_CMD;
4229
b112889c
AM
4230 spin_unlock_bh(&mvm->time_event_lock);
4231
4232 /*
4233 * Use a notification wait, which really just processes the
4234 * command response and doesn't wait for anything, in order
4235 * to be able to process the response and get the UID inside
4236 * the RX path. Using CMD_WANT_SKB doesn't work because it
4237 * stores the buffer and then wakes up this thread, by which
4238 * time another notification (that the time event started)
4239 * might already be processed unsuccessfully.
4240 */
4241 iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4242 time_event_response,
4243 ARRAY_SIZE(time_event_response),
4244 iwl_mvm_rx_aux_roc, te_data);
4245
57e861d9 4246 res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
b112889c
AM
4247 &aux_roc_req);
4248
4249 if (res) {
4250 IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4251 iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4252 goto out_clear_te;
4253 }
4254
4255 /* No need to wait for anything, so just pass 1 (0 isn't valid) */
4256 res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4257 /* should never fail */
4258 WARN_ON_ONCE(res);
4259
4260 if (res) {
4261 out_clear_te:
4262 spin_lock_bh(&mvm->time_event_lock);
4263 iwl_mvm_te_clear_data(mvm, te_data);
4264 spin_unlock_bh(&mvm->time_event_lock);
4265 }
4266
4267 return res;
4268}
4269
feebebae
MK
4270static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4271{
4272 int ret = 0;
4273
4274 lockdep_assert_held(&mvm->mutex);
4275
4276 if (!fw_has_capa(&mvm->fw->ucode_capa,
4277 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4278 IWL_ERR(mvm, "hotspot not supported\n");
4279 return -EINVAL;
4280 }
4281
4282 if (iwl_fw_lookup_cmd_ver(mvm->fw, ADD_STA, 0) >= 12) {
4283 ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4284 WARN(ret, "Failed to allocate aux station");
4285 }
4286
4287 return ret;
4288}
4289
4290static int iwl_mvm_roc_switch_binding(struct iwl_mvm *mvm,
4291 struct ieee80211_vif *vif,
4292 struct iwl_mvm_phy_ctxt *new_phy_ctxt)
4293{
4294 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4295 int ret = 0;
4296
4297 lockdep_assert_held(&mvm->mutex);
4298
4299 /* Unbind the P2P_DEVICE from the current PHY context,
4300 * and if the PHY context is not used remove it.
4301 */
4302 ret = iwl_mvm_binding_remove_vif(mvm, vif);
4303 if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
4304 return ret;
4305
4306 iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4307
4308 /* Bind the P2P_DEVICE to the current PHY Context */
4309 mvmvif->deflink.phy_ctxt = new_phy_ctxt;
4310
4311 ret = iwl_mvm_binding_add_vif(mvm, vif);
4312 WARN(ret, "Failed binding P2P_DEVICE\n");
4313 return ret;
4314}
4315
8ca151b5
JB
4316static int iwl_mvm_roc(struct ieee80211_hw *hw,
4317 struct ieee80211_vif *vif,
4318 struct ieee80211_channel *channel,
d339d5ca
IP
4319 int duration,
4320 enum ieee80211_roc_type type)
fe8b2ad3
MK
4321{
4322 struct iwl_mvm_roc_ops ops = {
4323 .add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4324 .switch_phy_ctxt = iwl_mvm_roc_switch_binding,
4325 };
4326
4327 return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4328}
4329
4330/* Execute the common part for MLD and non-MLD modes */
4331int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4332 struct ieee80211_channel *channel, int duration,
4333 enum ieee80211_roc_type type,
4334 struct iwl_mvm_roc_ops *ops)
8ca151b5
JB
4335{
4336 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3 4337 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 4338 struct cfg80211_chan_def chandef;
31d385ae 4339 struct iwl_mvm_phy_ctxt *phy_ctxt;
8835a64f 4340 bool band_change_removal;
31d385ae 4341 int ret, i;
feebebae 4342 u32 lmac_id;
31d385ae
IP
4343
4344 IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4345 duration, type);
8ca151b5 4346
9834781c
JB
4347 /*
4348 * Flush the done work, just in case it's still pending, so that
4349 * the work it does can complete and we can accept new frames.
4350 */
6ed13164
MG
4351 flush_work(&mvm->roc_done_wk);
4352
a6cc5163
MG
4353 mutex_lock(&mvm->mutex);
4354
b112889c
AM
4355 switch (vif->type) {
4356 case NL80211_IFTYPE_STATION:
feebebae
MK
4357 lmac_id = iwl_mvm_get_lmac_id(mvm->fw, channel->band);
4358
4359 /* Use aux roc framework (HS20) */
fe8b2ad3 4360 ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
feebebae 4361 if (!ret)
5ac6c72e
LC
4362 ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
4363 vif, duration);
a6cc5163 4364 goto out_unlock;
b112889c
AM
4365 case NL80211_IFTYPE_P2P_DEVICE:
4366 /* handle below */
4367 break;
4368 default:
4369 IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
a6cc5163
MG
4370 ret = -EINVAL;
4371 goto out_unlock;
8ca151b5
JB
4372 }
4373
31d385ae
IP
4374 for (i = 0; i < NUM_PHY_CTX; i++) {
4375 phy_ctxt = &mvm->phy_ctxts[i];
650cadb7 4376 if (phy_ctxt->ref == 0 || mvmvif->deflink.phy_ctxt == phy_ctxt)
31d385ae
IP
4377 continue;
4378
4379 if (phy_ctxt->ref && channel == phy_ctxt->channel) {
fe8b2ad3 4380 ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
feebebae 4381 if (ret)
31d385ae
IP
4382 goto out_unlock;
4383
650cadb7 4384 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
31d385ae
IP
4385 goto schedule_time_event;
4386 }
4387 }
4388
4389 /* Need to update the PHY context only if the ROC channel changed */
650cadb7 4390 if (channel == mvmvif->deflink.phy_ctxt->channel)
31d385ae
IP
4391 goto schedule_time_event;
4392
8ca151b5 4393 cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
8ca151b5 4394
31d385ae 4395 /*
8835a64f
JB
4396 * Check if the remain-on-channel is on a different band and that
4397 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
4398 * so, we'll need to release and then re-configure here, since we
4399 * must not remove a PHY context that's part of a binding.
31d385ae 4400 */
8835a64f
JB
4401 band_change_removal =
4402 fw_has_capa(&mvm->fw->ucode_capa,
4403 IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
650cadb7 4404 mvmvif->deflink.phy_ctxt->channel->band != chandef.chan->band;
8835a64f 4405
650cadb7 4406 if (mvmvif->deflink.phy_ctxt->ref == 1 && !band_change_removal) {
8835a64f
JB
4407 /*
4408 * Change the PHY context configuration as it is currently
4409 * referenced only by the P2P Device MAC (and we can modify it)
4410 */
650cadb7 4411 ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->deflink.phy_ctxt,
31d385ae
IP
4412 &chandef, 1, 1);
4413 if (ret)
4414 goto out_unlock;
4415 } else {
4416 /*
8835a64f
JB
4417 * The PHY context is shared with other MACs (or we're trying to
4418 * switch bands), so remove the P2P Device from the binding,
4419 * allocate an new PHY context and create a new binding.
31d385ae
IP
4420 */
4421 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4422 if (!phy_ctxt) {
4423 ret = -ENOSPC;
4424 goto out_unlock;
4425 }
4426
4427 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
4428 1, 1);
4429 if (ret) {
4430 IWL_ERR(mvm, "Failed to change PHY context\n");
4431 goto out_unlock;
4432 }
4433
fe8b2ad3 4434 ret = ops->switch_phy_ctxt(mvm, vif, phy_ctxt);
feebebae 4435 if (ret)
31d385ae
IP
4436 goto out_unlock;
4437
650cadb7 4438 iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
31d385ae
IP
4439 }
4440
4441schedule_time_event:
8ca151b5 4442 /* Schedule the time events */
e635c797 4443 ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
8ca151b5 4444
31d385ae 4445out_unlock:
8ca151b5
JB
4446 mutex_unlock(&mvm->mutex);
4447 IWL_DEBUG_MAC80211(mvm, "leave\n");
8ca151b5
JB
4448 return ret;
4449}
4450
fe8b2ad3
MK
4451int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4452 struct ieee80211_vif *vif)
8ca151b5
JB
4453{
4454 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4455
4456 IWL_DEBUG_MAC80211(mvm, "enter\n");
4457
4458 mutex_lock(&mvm->mutex);
fe959c7b 4459 iwl_mvm_stop_roc(mvm, vif);
8ca151b5
JB
4460 mutex_unlock(&mvm->mutex);
4461
4462 IWL_DEBUG_MAC80211(mvm, "leave\n");
4463 return 0;
4464}
4465
b73f9a4a
JB
4466struct iwl_mvm_ftm_responder_iter_data {
4467 bool responder;
4468 struct ieee80211_chanctx_conf *ctx;
4469};
4470
4471static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4472 struct ieee80211_vif *vif)
4473{
4474 struct iwl_mvm_ftm_responder_iter_data *data = _data;
4475
d0a9123e 4476 if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
b73f9a4a
JB
4477 vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4478 data->responder = true;
4479}
4480
4481static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4482 struct ieee80211_chanctx_conf *ctx)
4483{
4484 struct iwl_mvm_ftm_responder_iter_data data = {
4485 .responder = false,
4486 .ctx = ctx,
4487 };
4488
4489 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4490 IEEE80211_IFACE_ITER_NORMAL,
4491 iwl_mvm_ftm_responder_chanctx_iter,
4492 &data);
4493 return data.responder;
4494}
4495
b08c1d97
LC
4496static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4497 struct ieee80211_chanctx_conf *ctx)
8ca151b5 4498{
fe0f2de3
IP
4499 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4500 struct iwl_mvm_phy_ctxt *phy_ctxt;
b73f9a4a
JB
4501 bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4502 struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
8ca151b5
JB
4503 int ret;
4504
b08c1d97
LC
4505 lockdep_assert_held(&mvm->mutex);
4506
53a9d61e 4507 IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
fe0f2de3 4508
fe0f2de3
IP
4509 phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4510 if (!phy_ctxt) {
4511 ret = -ENOSPC;
4512 goto out;
4513 }
8ca151b5 4514
b73f9a4a 4515 ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
53a9d61e
IP
4516 ctx->rx_chains_static,
4517 ctx->rx_chains_dynamic);
fe0f2de3
IP
4518 if (ret) {
4519 IWL_ERR(mvm, "Failed to add PHY context\n");
4520 goto out;
4521 }
4522
53a9d61e 4523 iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
fe0f2de3
IP
4524 *phy_ctxt_id = phy_ctxt->id;
4525out:
b08c1d97
LC
4526 return ret;
4527}
4528
cbce62a3
MK
4529int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4530 struct ieee80211_chanctx_conf *ctx)
b08c1d97
LC
4531{
4532 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4533 int ret;
4534
4535 mutex_lock(&mvm->mutex);
4536 ret = __iwl_mvm_add_chanctx(mvm, ctx);
8ca151b5 4537 mutex_unlock(&mvm->mutex);
b08c1d97 4538
8ca151b5
JB
4539 return ret;
4540}
4541
b08c1d97
LC
4542static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4543 struct ieee80211_chanctx_conf *ctx)
4544{
4545 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4546 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4547
4548 lockdep_assert_held(&mvm->mutex);
4549
4550 iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4551}
4552
cbce62a3
MK
4553void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4554 struct ieee80211_chanctx_conf *ctx)
8ca151b5
JB
4555{
4556 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
8ca151b5
JB
4557
4558 mutex_lock(&mvm->mutex);
b08c1d97 4559 __iwl_mvm_remove_chanctx(mvm, ctx);
8ca151b5
JB
4560 mutex_unlock(&mvm->mutex);
4561}
4562
cbce62a3
MK
4563void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4564 struct ieee80211_chanctx_conf *ctx, u32 changed)
8ca151b5
JB
4565{
4566 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
fe0f2de3
IP
4567 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4568 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
b73f9a4a
JB
4569 bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4570 struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
8ca151b5 4571
31d385ae
IP
4572 if (WARN_ONCE((phy_ctxt->ref > 1) &&
4573 (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4574 IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
2dceedae
AN
4575 IEEE80211_CHANCTX_CHANGE_RADAR |
4576 IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
31d385ae
IP
4577 "Cannot change PHY. Ref=%d, changed=0x%X\n",
4578 phy_ctxt->ref, changed))
4579 return;
4580
8ca151b5 4581 mutex_lock(&mvm->mutex);
7a20bcce
EG
4582
4583 /* we are only changing the min_width, may be a noop */
4584 if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
b73f9a4a 4585 if (phy_ctxt->width == def->width)
7a20bcce
EG
4586 goto out_unlock;
4587
4588 /* we are just toggling between 20_NOHT and 20 */
4589 if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
b73f9a4a 4590 def->width <= NL80211_CHAN_WIDTH_20)
7a20bcce
EG
4591 goto out_unlock;
4592 }
4593
4d66449a 4594 iwl_mvm_bt_coex_vif_change(mvm);
b73f9a4a 4595 iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
8ca151b5
JB
4596 ctx->rx_chains_static,
4597 ctx->rx_chains_dynamic);
7a20bcce
EG
4598
4599out_unlock:
8ca151b5
JB
4600 mutex_unlock(&mvm->mutex);
4601}
4602
8a919a78
MK
4603/*
4604 * This function executes the common part for MLD and non-MLD modes.
4605 *
4606 * Returns true if we're done assigning the chanctx
4607 * (either on failure or success)
4608 */
50e81437
MK
4609bool __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
4610 struct ieee80211_vif *vif,
4611 struct ieee80211_chanctx_conf *ctx,
4612 bool switching_chanctx, int *ret)
8ca151b5 4613{
fe0f2de3
IP
4614 u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4615 struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
8ca151b5 4616 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8ca151b5 4617
b08c1d97 4618 lockdep_assert_held(&mvm->mutex);
8ca151b5 4619
650cadb7 4620 mvmvif->deflink.phy_ctxt = phy_ctxt;
8ca151b5
JB
4621
4622 switch (vif->type) {
4623 case NL80211_IFTYPE_AP:
4741dd04
LC
4624 /* only needed if we're switching chanctx (i.e. during CSA) */
4625 if (switching_chanctx) {
bd3398e2
AO
4626 mvmvif->ap_ibss_active = true;
4627 break;
4628 }
5a2abdca 4629 fallthrough;
5023d966 4630 case NL80211_IFTYPE_ADHOC:
8ca151b5
JB
4631 /*
4632 * The AP binding flow is handled as part of the start_ap flow
5023d966 4633 * (in bss_info_changed), similarly for IBSS.
8ca151b5 4634 */
8a919a78
MK
4635 *ret = 0;
4636 return true;
8ca151b5 4637 case NL80211_IFTYPE_STATION:
2533edce 4638 break;
8ca151b5 4639 case NL80211_IFTYPE_MONITOR:
2533edce
LC
4640 /* always disable PS when a monitor interface is active */
4641 mvmvif->ps_disabled = true;
8ca151b5
JB
4642 break;
4643 default:
8a919a78
MK
4644 *ret = -EINVAL;
4645 return true;
8ca151b5 4646 }
8a919a78
MK
4647 return false;
4648}
4649
4650static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4651 struct ieee80211_vif *vif,
4652 struct ieee80211_chanctx_conf *ctx,
4653 bool switching_chanctx)
4654{
4655 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4656 int ret;
4657
4658 if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
4659 switching_chanctx, &ret))
4660 goto out;
8ca151b5
JB
4661
4662 ret = iwl_mvm_binding_add_vif(mvm, vif);
4663 if (ret)
b08c1d97 4664 goto out;
8ca151b5
JB
4665
4666 /*
92d85562
AB
4667 * Power state must be updated before quotas,
4668 * otherwise fw will complain.
4669 */
999609f1 4670 iwl_mvm_power_update_mac(mvm);
92d85562
AB
4671
4672 /* Setting the quota at this stage is only required for monitor
8ca151b5
JB
4673 * interfaces. For the other types, the bss_info changed flow
4674 * will handle quota settings.
4675 */
4676 if (vif->type == NL80211_IFTYPE_MONITOR) {
1e1391ca 4677 mvmvif->monitor_active = true;
7754ae79 4678 ret = iwl_mvm_update_quotas(mvm, false, NULL);
8ca151b5
JB
4679 if (ret)
4680 goto out_remove_binding;
0e39eb03
CRI
4681
4682 ret = iwl_mvm_add_snif_sta(mvm, vif);
4683 if (ret)
4684 goto out_remove_binding;
4685
8ca151b5
JB
4686 }
4687
bd3398e2 4688 /* Handle binding during CSA */
a57c688d 4689 if (vif->type == NL80211_IFTYPE_AP) {
7754ae79 4690 iwl_mvm_update_quotas(mvm, false, NULL);
3dfd3a97 4691 iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
bd3398e2
AO
4692 }
4693
8a919a78
MK
4694 if (vif->type == NL80211_IFTYPE_STATION) {
4695 if (!switching_chanctx) {
4696 mvmvif->csa_bcn_pending = false;
4697 goto out;
4698 }
4699
74a10252 4700 mvmvif->csa_bcn_pending = true;
686e7fe1 4701
74a10252
SS
4702 if (!fw_has_capa(&mvm->fw->ucode_capa,
4703 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4704 u32 duration = 3 * vif->bss_conf.beacon_int;
686e7fe1 4705
74a10252
SS
4706 /* Protect the session to make sure we hear the first
4707 * beacon on the new channel.
4708 */
4709 iwl_mvm_protect_session(mvm, vif, duration, duration,
4710 vif->bss_conf.beacon_int / 2,
4711 true);
74a10252 4712 }
686e7fe1 4713
7754ae79 4714 iwl_mvm_update_quotas(mvm, false, NULL);
0ce04ce7
LC
4715 }
4716
b08c1d97 4717 goto out;
8ca151b5 4718
b08c1d97 4719out_remove_binding:
8ca151b5 4720 iwl_mvm_binding_remove_vif(mvm, vif);
999609f1 4721 iwl_mvm_power_update_mac(mvm);
b08c1d97 4722out:
8ca151b5 4723 if (ret)
650cadb7 4724 mvmvif->deflink.phy_ctxt = NULL;
8ca151b5
JB
4725 return ret;
4726}
8a919a78 4727
b08c1d97
LC
4728static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4729 struct ieee80211_vif *vif,
727eff4d 4730 struct ieee80211_bss_conf *link_conf,
b08c1d97 4731 struct ieee80211_chanctx_conf *ctx)
8ca151b5
JB
4732{
4733 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
b08c1d97 4734 int ret;
8ca151b5
JB
4735
4736 mutex_lock(&mvm->mutex);
f0c97783 4737 ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
b08c1d97
LC
4738 mutex_unlock(&mvm->mutex);
4739
4740 return ret;
4741}
4742
daddfae5
MK
4743/*
4744 * This function executes the common part for MLD and non-MLD modes.
4745 *
4746 * Returns if chanctx unassign chanctx is done
4747 * (either on failure or success)
4748 */
6f71e90e
MK
4749bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
4750 struct ieee80211_vif *vif,
4751 bool switching_chanctx)
b08c1d97
LC
4752{
4753 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4754
4755 lockdep_assert_held(&mvm->mutex);
650cadb7
GG
4756 iwl_mvm_remove_time_event(mvm, mvmvif,
4757 &mvmvif->time_event_data);
8ca151b5 4758
8ca151b5 4759 switch (vif->type) {
5023d966 4760 case NL80211_IFTYPE_ADHOC:
daddfae5 4761 return true;
8ca151b5 4762 case NL80211_IFTYPE_MONITOR:
1e1391ca 4763 mvmvif->monitor_active = false;
2533edce 4764 mvmvif->ps_disabled = false;
8ca151b5 4765 break;
bd3398e2
AO
4766 case NL80211_IFTYPE_AP:
4767 /* This part is triggered only during CSA */
4741dd04 4768 if (!switching_chanctx || !mvmvif->ap_ibss_active)
daddfae5 4769 return true;
bd3398e2 4770
7ef0aab6
AO
4771 mvmvif->csa_countdown = false;
4772
56f4f12b
MK
4773 /* Set CS bit on all the stations */
4774 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4775
003e5236
AO
4776 /* Save blocked iface, the timeout is set on the next beacon */
4777 rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4778
bd3398e2 4779 mvmvif->ap_ibss_active = false;
f0c97783 4780 break;
daddfae5
MK
4781 default:
4782 break;
4783 }
4784 return false;
4785}
f0c97783 4786
daddfae5
MK
4787static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4788 struct ieee80211_vif *vif,
4789 struct ieee80211_chanctx_conf *ctx,
4790 bool switching_chanctx)
4791{
4792 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4793 struct ieee80211_vif *disabled_vif = NULL;
4794
4795 if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
4796 goto out;
f0c97783 4797
daddfae5
MK
4798 if (vif->type == NL80211_IFTYPE_MONITOR)
4799 iwl_mvm_rm_snif_sta(mvm, vif);
4800
daddfae5
MK
4801
4802 if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
4803 disabled_vif = vif;
74a10252
SS
4804 if (!fw_has_capa(&mvm->fw->ucode_capa,
4805 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4806 iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
8ca151b5
JB
4807 }
4808
7754ae79 4809 iwl_mvm_update_quotas(mvm, false, disabled_vif);
1e1391ca 4810 iwl_mvm_binding_remove_vif(mvm, vif);
1c2abf72 4811
b08c1d97 4812out:
bf544e9a
SS
4813 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4814 switching_chanctx)
4815 return;
650cadb7 4816 mvmvif->deflink.phy_ctxt = NULL;
999609f1 4817 iwl_mvm_power_update_mac(mvm);
b08c1d97
LC
4818}
4819
4820static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4821 struct ieee80211_vif *vif,
727eff4d 4822 struct ieee80211_bss_conf *link_conf,
b08c1d97
LC
4823 struct ieee80211_chanctx_conf *ctx)
4824{
4825 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4826
4827 mutex_lock(&mvm->mutex);
f0c97783 4828 __iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
8ca151b5
JB
4829 mutex_unlock(&mvm->mutex);
4830}
4831
50cc9574
LC
4832static int
4833iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
660eba5a
MK
4834 struct ieee80211_vif_chanctx_switch *vifs,
4835 struct iwl_mvm_switch_vif_chanctx_ops *ops)
b08c1d97 4836{
b08c1d97
LC
4837 int ret;
4838
b08c1d97 4839 mutex_lock(&mvm->mutex);
660eba5a 4840 ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
b08c1d97
LC
4841 __iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4842
4843 ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4844 if (ret) {
4845 IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4846 goto out_reassign;
4847 }
4848
660eba5a 4849 ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
f0c97783 4850 true);
b08c1d97
LC
4851 if (ret) {
4852 IWL_ERR(mvm,
4853 "failed to assign new_ctx during channel switch\n");
4854 goto out_remove;
4855 }
4856
f697267f
AN
4857 /* we don't support TDLS during DCM - can be caused by channel switch */
4858 if (iwl_mvm_phy_ctx_count(mvm) > 1)
4859 iwl_mvm_teardown_tdls_peers(mvm);
4860
b08c1d97
LC
4861 goto out;
4862
4863out_remove:
4864 __iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4865
4866out_reassign:
6fd1fb63 4867 if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
b08c1d97
LC
4868 IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4869 goto out_restart;
4870 }
4871
660eba5a
MK
4872 if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4873 true)) {
b08c1d97
LC
4874 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4875 goto out_restart;
4876 }
4877
4878 goto out;
4879
4880out_restart:
4881 /* things keep failing, better restart the hw */
4882 iwl_mvm_nic_restart(mvm, false);
4883
4884out:
4885 mutex_unlock(&mvm->mutex);
50cc9574
LC
4886
4887 return ret;
4888}
4889
48a256e8
LC
4890static int
4891iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
660eba5a
MK
4892 struct ieee80211_vif_chanctx_switch *vifs,
4893 struct iwl_mvm_switch_vif_chanctx_ops *ops)
48a256e8
LC
4894{
4895 int ret;
4896
4897 mutex_lock(&mvm->mutex);
660eba5a 4898 ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
48a256e8 4899
660eba5a 4900 ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
48a256e8
LC
4901 true);
4902 if (ret) {
4903 IWL_ERR(mvm,
4904 "failed to assign new_ctx during channel switch\n");
4905 goto out_reassign;
4906 }
4907
4908 goto out;
4909
4910out_reassign:
660eba5a
MK
4911 if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4912 true)) {
48a256e8
LC
4913 IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4914 goto out_restart;
4915 }
4916
4917 goto out;
4918
4919out_restart:
4920 /* things keep failing, better restart the hw */
4921 iwl_mvm_nic_restart(mvm, false);
4922
4923out:
4924 mutex_unlock(&mvm->mutex);
4925
4926 return ret;
4927}
4928
660eba5a
MK
4929/* Execute the common part for both MLD and non-MLD modes */
4930int
4931iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
4932 struct ieee80211_vif_chanctx_switch *vifs,
4933 int n_vifs,
4934 enum ieee80211_chanctx_switch_mode mode,
4935 struct iwl_mvm_switch_vif_chanctx_ops *ops)
50cc9574
LC
4936{
4937 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4938 int ret;
4939
4940 /* we only support a single-vif right now */
4941 if (n_vifs > 1)
4942 return -EOPNOTSUPP;
4943
4944 switch (mode) {
4945 case CHANCTX_SWMODE_SWAP_CONTEXTS:
660eba5a 4946 ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
50cc9574
LC
4947 break;
4948 case CHANCTX_SWMODE_REASSIGN_VIF:
660eba5a 4949 ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
50cc9574
LC
4950 break;
4951 default:
4952 ret = -EOPNOTSUPP;
4953 break;
4954 }
4955
b08c1d97
LC
4956 return ret;
4957}
4958
660eba5a
MK
4959static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4960 struct ieee80211_vif_chanctx_switch *vifs,
4961 int n_vifs,
4962 enum ieee80211_chanctx_switch_mode mode)
4963{
4964 struct iwl_mvm_switch_vif_chanctx_ops ops = {
4965 .__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
4966 .__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
4967 };
4968
4969 return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
4970}
4971
cbce62a3 4972int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
2f0282db
JB
4973{
4974 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4975
4976 return mvm->ibss_manager;
4977}
4978
cbce62a3
MK
4979int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
4980 bool set)
8ca151b5
JB
4981{
4982 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
9d8ce6af 4983 struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
8ca151b5
JB
4984
4985 if (!mvm_sta || !mvm_sta->vif) {
4986 IWL_ERR(mvm, "Station is not associated to a vif\n");
4987 return -EINVAL;
4988 }
4989
36cf5377
GG
4990 return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
4991 &mvm_sta->vif->bss_conf);
8ca151b5
JB
4992}
4993
507cadf2
DS
4994#ifdef CONFIG_NL80211_TESTMODE
4995static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4996 [IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4997 [IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
f6c6ad42 4998 [IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
507cadf2
DS
4999};
5000
5001static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5002 struct ieee80211_vif *vif,
5003 void *data, int len)
5004{
5005 struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5006 int err;
5007 u32 noa_duration;
5008
8cb08174
JB
5009 err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5010 iwl_mvm_tm_policy, NULL);
507cadf2
DS
5011 if (err)
5012 return err;
5013
5014 if (!tb[IWL_MVM_TM_ATTR_CMD])
5015 return -EINVAL;
5016
5017 switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5018 case IWL_MVM_TM_CMD_SET_NOA:
5019 if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5020 !vif->bss_conf.enable_beacon ||
5021 !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5022 return -EINVAL;
5023
5024 noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5025 if (noa_duration >= vif->bss_conf.beacon_int)
5026 return -EINVAL;
5027
5028 mvm->noa_duration = noa_duration;
5029 mvm->noa_vif = vif;
5030
22b21041 5031 return iwl_mvm_update_quotas(mvm, true, NULL);
f6c6ad42
JB
5032 case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5033 /* must be associated client vif - ignore authorized */
5034 if (!vif || vif->type != NL80211_IFTYPE_STATION ||
f276e20b 5035 !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
f6c6ad42
JB
5036 !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5037 return -EINVAL;
5038
5039 if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
a1022927
EG
5040 return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
5041 return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
507cadf2
DS
5042 }
5043
5044 return -EOPNOTSUPP;
5045}
5046
cbce62a3
MK
5047int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5048 struct ieee80211_vif *vif,
5049 void *data, int len)
507cadf2
DS
5050{
5051 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5052 int err;
5053
5054 mutex_lock(&mvm->mutex);
5055 err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5056 mutex_unlock(&mvm->mutex);
5057
5058 return err;
5059}
5060#endif
5061
cbce62a3
MK
5062void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5063 struct ieee80211_channel_switch *chsw)
622e3f9b
LC
5064{
5065 /* By implementing this operation, we prevent mac80211 from
5066 * starting its own channel switch timer, so that we can call
5067 * ieee80211_chswitch_done() ourselves at the right time
5068 * (which is when the absence time event starts).
5069 */
5070
5071 IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5072 "dummy channel switch op\n");
5073}
5074
74a10252
SS
5075static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5076 struct ieee80211_vif *vif,
5077 struct ieee80211_channel_switch *chsw)
5078{
5079 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5080 struct iwl_chan_switch_te_cmd cmd = {
5081 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5082 mvmvif->color)),
5083 .action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5084 .tsf = cpu_to_le32(chsw->timestamp),
5085 .cs_count = chsw->count,
77738865 5086 .cs_mode = chsw->block_tx,
74a10252
SS
5087 };
5088
5089 lockdep_assert_held(&mvm->mutex);
5090
9cfcf71c
SS
5091 if (chsw->delay)
5092 cmd.cs_delayed_bcn_count =
5093 DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5094
74a10252
SS
5095 return iwl_mvm_send_cmd_pdu(mvm,
5096 WIDE_ID(MAC_CONF_GROUP,
5097 CHANNEL_SWITCH_TIME_EVENT_CMD),
5098 0, sizeof(cmd), &cmd);
5099}
5100
0202bcf0
EG
5101static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5102 struct ieee80211_vif *vif,
5103 struct ieee80211_channel_switch *chsw)
5104{
5105 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5106 u32 apply_time;
5107
5108 /* Schedule the time event to a bit before beacon 1,
5109 * to make sure we're in the new channel when the
5110 * GO/AP arrives. In case count <= 1 immediately schedule the
5111 * TE (this might result with some packet loss or connection
5112 * loss).
5113 */
5114 if (chsw->count <= 1)
5115 apply_time = 0;
5116 else
5117 apply_time = chsw->device_timestamp +
5118 ((vif->bss_conf.beacon_int * (chsw->count - 1) -
5119 IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5120
5121 if (chsw->block_tx)
5122 iwl_mvm_csa_client_absent(mvm, vif);
5123
5124 if (mvmvif->bf_data.bf_enabled) {
5125 int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
5126
5127 if (ret)
5128 return ret;
5129 }
5130
5131 iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5132 apply_time);
5133
5134 return 0;
5135}
5136
f6780614 5137#define IWL_MAX_CSA_BLOCK_TX 1500
cbce62a3
MK
5138int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
5139 struct ieee80211_vif *vif,
5140 struct ieee80211_channel_switch *chsw)
bd3398e2
AO
5141{
5142 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
664322fa 5143 struct ieee80211_vif *csa_vif;
f6c34820 5144 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
f028905c 5145 int ret;
bd3398e2
AO
5146
5147 mutex_lock(&mvm->mutex);
664322fa 5148
81d62d5a
JB
5149 mvmvif->csa_failed = false;
5150
6b20d774 5151 IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
f028905c 5152 chsw->chandef.center_freq1);
6b20d774 5153
7174beb6
JB
5154 iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5155 ieee80211_vif_to_wdev(vif),
5156 FW_DBG_TRIGGER_CHANNEL_SWITCH);
f35d9c55 5157
6b20d774
LC
5158 switch (vif->type) {
5159 case NL80211_IFTYPE_AP:
5160 csa_vif =
5161 rcu_dereference_protected(mvm->csa_vif,
5162 lockdep_is_held(&mvm->mutex));
d0a9123e 5163 if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
6b20d774
LC
5164 "Another CSA is already in progress")) {
5165 ret = -EBUSY;
5166 goto out_unlock;
5167 }
5168
d3a108a4
AO
5169 /* we still didn't unblock tx. prevent new CS meanwhile */
5170 if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5171 lockdep_is_held(&mvm->mutex))) {
5172 ret = -EBUSY;
5173 goto out_unlock;
5174 }
5175
6b20d774 5176 rcu_assign_pointer(mvm->csa_vif, vif);
7ef0aab6 5177
7ef0aab6
AO
5178 if (WARN_ONCE(mvmvif->csa_countdown,
5179 "Previous CSA countdown didn't complete")) {
5180 ret = -EBUSY;
5181 goto out_unlock;
5182 }
5183
d3a108a4
AO
5184 mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5185
6b20d774 5186 break;
dc88b4ba 5187 case NL80211_IFTYPE_STATION:
ad12b231
NE
5188 /*
5189 * In the new flow FW is in charge of timing the switch so there
5190 * is no need for all of this
5191 */
5192 if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5193 CHANNEL_SWITCH_ERROR_NOTIF,
5194 0))
5195 break;
5196
2360acbd
EG
5197 /*
5198 * We haven't configured the firmware to be associated yet since
5199 * we don't know the dtim period. In this case, the firmware can't
5200 * track the beacons.
5201 */
f276e20b 5202 if (!vif->cfg.assoc || !vif->bss_conf.dtim_period) {
2360acbd
EG
5203 ret = -EBUSY;
5204 goto out_unlock;
5205 }
5206
87d9564e
JB
5207 if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
5208 schedule_delayed_work(&mvmvif->csa_work, 0);
5209
f6780614 5210 if (chsw->block_tx) {
f6780614
SS
5211 /*
5212 * In case of undetermined / long time with immediate
5213 * quiet monitor status to gracefully disconnect
5214 */
5215 if (!chsw->count ||
5216 chsw->count * vif->bss_conf.beacon_int >
5217 IWL_MAX_CSA_BLOCK_TX)
5218 schedule_delayed_work(&mvmvif->csa_work,
5219 msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5220 }
dc88b4ba 5221
0202bcf0
EG
5222 if (!fw_has_capa(&mvm->fw->ucode_capa,
5223 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5224 ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
c6e0a3e0
LC
5225 if (ret)
5226 goto out_unlock;
0202bcf0 5227 } else {
74a10252 5228 iwl_mvm_schedule_client_csa(mvm, vif, chsw);
0202bcf0 5229 }
81b4e44e
SS
5230
5231 mvmvif->csa_count = chsw->count;
5232 mvmvif->csa_misbehave = false;
dc88b4ba 5233 break;
6b20d774
LC
5234 default:
5235 break;
5236 }
bd3398e2 5237
f6c34820
LC
5238 mvmvif->ps_disabled = true;
5239
5240 ret = iwl_mvm_power_update_ps(mvm);
5241 if (ret)
5242 goto out_unlock;
f028905c 5243
e198f5e7
AN
5244 /* we won't be on this channel any longer */
5245 iwl_mvm_teardown_tdls_peers(mvm);
5246
bd3398e2
AO
5247out_unlock:
5248 mutex_unlock(&mvm->mutex);
f028905c
LC
5249
5250 return ret;
bd3398e2
AO
5251}
5252
cbce62a3
MK
5253void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5254 struct ieee80211_vif *vif,
5255 struct ieee80211_channel_switch *chsw)
f6c34820 5256{
f6c34820 5257 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
c37763d2
SS
5258 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5259 struct iwl_chan_switch_te_cmd cmd = {
5260 .mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5261 mvmvif->color)),
5262 .action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5263 .tsf = cpu_to_le32(chsw->timestamp),
5264 .cs_count = chsw->count,
77738865 5265 .cs_mode = chsw->block_tx,
c37763d2 5266 };
a57c688d 5267
ad12b231
NE
5268 /*
5269 * In the new flow FW is in charge of timing the switch so there is no
5270 * need for all of this
5271 */
5272 if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5273 CHANNEL_SWITCH_ERROR_NOTIF, 0))
5274 return;
5275
c37763d2
SS
5276 if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5277 return;
a57c688d 5278
c3eae059
GG
5279 IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5280 mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5281
81b4e44e
SS
5282 if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5283 if (mvmvif->csa_misbehave) {
5284 /* Second time, give up on this AP*/
5285 iwl_mvm_abort_channel_switch(hw, vif);
5286 ieee80211_chswitch_done(vif, false);
5287 mvmvif->csa_misbehave = false;
5288 return;
a57c688d 5289 }
81b4e44e 5290 mvmvif->csa_misbehave = true;
a57c688d 5291 }
81b4e44e 5292 mvmvif->csa_count = chsw->count;
a57c688d 5293
caf46377
SS
5294 mutex_lock(&mvm->mutex);
5295 if (mvmvif->csa_failed)
5296 goto out_unlock;
f6c34820 5297
c37763d2
SS
5298 WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5299 WIDE_ID(MAC_CONF_GROUP,
5300 CHANNEL_SWITCH_TIME_EVENT_CMD),
caf46377
SS
5301 0, sizeof(cmd), &cmd));
5302out_unlock:
5303 mutex_unlock(&mvm->mutex);
f6c34820
LC
5304}
5305
6110d9e5
DS
5306static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5307{
435d0827
SS
5308 int i;
5309
06195639 5310 if (!iwl_mvm_has_new_tx_api(mvm)) {
309c4848
LC
5311 if (drop) {
5312 mutex_lock(&mvm->mutex);
6110d9e5 5313 iwl_mvm_flush_tx_path(mvm,
d4e3a341 5314 iwl_mvm_flushable_queues(mvm) & queues);
309c4848
LC
5315 mutex_unlock(&mvm->mutex);
5316 } else {
06195639 5317 iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
309c4848 5318 }
435d0827
SS
5319 return;
5320 }
6110d9e5 5321
435d0827 5322 mutex_lock(&mvm->mutex);
be9ae34e 5323 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
435d0827 5324 struct ieee80211_sta *sta;
6110d9e5 5325
435d0827
SS
5326 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5327 lockdep_is_held(&mvm->mutex));
5328 if (IS_ERR_OR_NULL(sta))
5329 continue;
6110d9e5 5330
06195639 5331 if (drop)
d4e3a341 5332 iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
06195639
SS
5333 else
5334 iwl_mvm_wait_sta_queues_empty(mvm,
5335 iwl_mvm_sta_from_mac80211(sta));
6110d9e5 5336 }
435d0827 5337 mutex_unlock(&mvm->mutex);
6110d9e5
DS
5338}
5339
cbce62a3
MK
5340void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5341 u32 queues, bool drop)
c5b0e7c0
EG
5342{
5343 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5344 struct iwl_mvm_vif *mvmvif;
5345 struct iwl_mvm_sta *mvmsta;
a0f6bf2a
AN
5346 struct ieee80211_sta *sta;
5347 int i;
5348 u32 msk = 0;
c5b0e7c0 5349
6110d9e5
DS
5350 if (!vif) {
5351 iwl_mvm_flush_no_vif(mvm, queues, drop);
5352 return;
5353 }
5354
5355 if (vif->type != NL80211_IFTYPE_STATION)
c5b0e7c0
EG
5356 return;
5357
24afba76 5358 /* Make sure we're done with the deferred traffic before flushing */
c8f54701 5359 flush_work(&mvm->add_stream_wk);
24afba76 5360
c5b0e7c0
EG
5361 mutex_lock(&mvm->mutex);
5362 mvmvif = iwl_mvm_vif_from_mac80211(vif);
c5b0e7c0 5363
a0f6bf2a 5364 /* flush the AP-station and all TDLS peers */
be9ae34e 5365 for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
a0f6bf2a
AN
5366 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5367 lockdep_is_held(&mvm->mutex));
5368 if (IS_ERR_OR_NULL(sta))
5369 continue;
5370
5371 mvmsta = iwl_mvm_sta_from_mac80211(sta);
5372 if (mvmsta->vif != vif)
5373 continue;
5374
5375 /* make sure only TDLS peers or the AP are flushed */
650cadb7 5376 WARN_ON(i != mvmvif->deflink.ap_sta_id && !sta->tdls);
a0f6bf2a 5377
d49394a1 5378 if (drop) {
f9084775 5379 if (iwl_mvm_flush_sta(mvm, mvmsta, false))
d49394a1
SS
5380 IWL_ERR(mvm, "flush request fail\n");
5381 } else {
d6d517b7
SS
5382 if (iwl_mvm_has_new_tx_api(mvm))
5383 iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
f7bd883b
JB
5384 else /* only used for !iwl_mvm_has_new_tx_api() below */
5385 msk |= mvmsta->tfd_queue_msk;
d49394a1 5386 }
480acbce 5387 }
c5b0e7c0 5388
d49394a1 5389 mutex_unlock(&mvm->mutex);
4e6c48e0 5390
d49394a1
SS
5391 /* this can take a while, and we may need/want other operations
5392 * to succeed while doing this, so do it without the mutex held
5393 */
d6d517b7 5394 if (!drop && !iwl_mvm_has_new_tx_api(mvm))
a1a57877 5395 iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
c5b0e7c0
EG
5396}
5397
cbce62a3
MK
5398int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
5399 struct survey_info *survey)
91a8bcde
JB
5400{
5401 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5402 int ret;
5403
5404 memset(survey, 0, sizeof(*survey));
5405
5406 /* only support global statistics right now */
5407 if (idx != 0)
5408 return -ENOENT;
5409
280a3efa
JB
5410 if (!fw_has_capa(&mvm->fw->ucode_capa,
5411 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
91a8bcde
JB
5412 return -ENOENT;
5413
5414 mutex_lock(&mvm->mutex);
5415
aab6930d 5416 if (iwl_mvm_firmware_running(mvm)) {
33cef925 5417 ret = iwl_mvm_request_statistics(mvm, false);
91a8bcde
JB
5418 if (ret)
5419 goto out;
5420 }
5421
5422 survey->filled = SURVEY_INFO_TIME |
5423 SURVEY_INFO_TIME_RX |
5424 SURVEY_INFO_TIME_TX |
5425 SURVEY_INFO_TIME_SCAN;
5426 survey->time = mvm->accu_radio_stats.on_time_rf +
5427 mvm->radio_stats.on_time_rf;
5428 do_div(survey->time, USEC_PER_MSEC);
5429
5430 survey->time_rx = mvm->accu_radio_stats.rx_time +
5431 mvm->radio_stats.rx_time;
5432 do_div(survey->time_rx, USEC_PER_MSEC);
5433
5434 survey->time_tx = mvm->accu_radio_stats.tx_time +
5435 mvm->radio_stats.tx_time;
5436 do_div(survey->time_tx, USEC_PER_MSEC);
5437
5438 survey->time_scan = mvm->accu_radio_stats.on_time_scan +
5439 mvm->radio_stats.on_time_scan;
5440 do_div(survey->time_scan, USEC_PER_MSEC);
5441
10a7c028 5442 ret = 0;
91a8bcde
JB
5443 out:
5444 mutex_unlock(&mvm->mutex);
5445 return ret;
5446}
5447
ed780545
JB
5448static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
5449{
82cdbd11 5450 u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
7138763e 5451 u32 gi_ltf;
82cdbd11
MK
5452
5453 switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
ed780545
JB
5454 case RATE_MCS_CHAN_WIDTH_20:
5455 rinfo->bw = RATE_INFO_BW_20;
5456 break;
5457 case RATE_MCS_CHAN_WIDTH_40:
5458 rinfo->bw = RATE_INFO_BW_40;
5459 break;
5460 case RATE_MCS_CHAN_WIDTH_80:
5461 rinfo->bw = RATE_INFO_BW_80;
5462 break;
5463 case RATE_MCS_CHAN_WIDTH_160:
5464 rinfo->bw = RATE_INFO_BW_160;
5465 break;
23dcee94
JB
5466 case RATE_MCS_CHAN_WIDTH_320:
5467 rinfo->bw = RATE_INFO_BW_320;
5468 break;
ed780545
JB
5469 }
5470
82cdbd11
MK
5471 if (format == RATE_MCS_CCK_MSK ||
5472 format == RATE_MCS_LEGACY_OFDM_MSK) {
5473 int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
5474
5475 /* add the offset needed to get to the legacy ofdm indices */
5476 if (format == RATE_MCS_LEGACY_OFDM_MSK)
5477 rate += IWL_FIRST_OFDM_RATE;
5478
5479 switch (rate) {
5480 case IWL_RATE_1M_INDEX:
5481 rinfo->legacy = 10;
5482 break;
5483 case IWL_RATE_2M_INDEX:
5484 rinfo->legacy = 20;
5485 break;
5486 case IWL_RATE_5M_INDEX:
5487 rinfo->legacy = 55;
5488 break;
5489 case IWL_RATE_11M_INDEX:
5490 rinfo->legacy = 110;
5491 break;
5492 case IWL_RATE_6M_INDEX:
5493 rinfo->legacy = 60;
5494 break;
5495 case IWL_RATE_9M_INDEX:
5496 rinfo->legacy = 90;
5497 break;
5498 case IWL_RATE_12M_INDEX:
5499 rinfo->legacy = 120;
5500 break;
5501 case IWL_RATE_18M_INDEX:
5502 rinfo->legacy = 180;
5503 break;
5504 case IWL_RATE_24M_INDEX:
5505 rinfo->legacy = 240;
5506 break;
5507 case IWL_RATE_36M_INDEX:
5508 rinfo->legacy = 360;
5509 break;
5510 case IWL_RATE_48M_INDEX:
5511 rinfo->legacy = 480;
5512 break;
5513 case IWL_RATE_54M_INDEX:
5514 rinfo->legacy = 540;
5515 }
5516 return;
5517 }
5518
5519 rinfo->nss = u32_get_bits(rate_n_flags,
5520 RATE_MCS_NSS_MSK) + 1;
5521 rinfo->mcs = format == RATE_MCS_HT_MSK ?
5522 RATE_HT_MCS_INDEX(rate_n_flags) :
5523 u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
5524
7138763e
JB
5525 if (rate_n_flags & RATE_MCS_SGI_MSK)
5526 rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
5527
5528 switch (format) {
23dcee94
JB
5529 case RATE_MCS_EHT_MSK:
5530 /* TODO: GI/LTF/RU. How does the firmware encode them? */
5531 rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
5532 break;
7138763e
JB
5533 case RATE_MCS_HE_MSK:
5534 gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
ed780545
JB
5535
5536 rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
ed780545 5537
82cdbd11 5538 if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
ed780545
JB
5539 rinfo->bw = RATE_INFO_BW_HE_RU;
5540 rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5541 }
5542
82cdbd11
MK
5543 switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
5544 case RATE_MCS_HE_TYPE_SU:
5545 case RATE_MCS_HE_TYPE_EXT_SU:
ed780545
JB
5546 if (gi_ltf == 0 || gi_ltf == 1)
5547 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5548 else if (gi_ltf == 2)
5549 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
82cdbd11 5550 else if (gi_ltf == 3)
ed780545 5551 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
82cdbd11
MK
5552 else
5553 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
ed780545 5554 break;
82cdbd11 5555 case RATE_MCS_HE_TYPE_MU:
ed780545
JB
5556 if (gi_ltf == 0 || gi_ltf == 1)
5557 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5558 else if (gi_ltf == 2)
5559 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5560 else
5561 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5562 break;
82cdbd11 5563 case RATE_MCS_HE_TYPE_TRIG:
ed780545
JB
5564 if (gi_ltf == 0 || gi_ltf == 1)
5565 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5566 else
5567 rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5568 break;
5569 }
5570
5571 if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5572 rinfo->he_dcm = 1;
7138763e
JB
5573 break;
5574 case RATE_MCS_HT_MSK:
82cdbd11 5575 rinfo->flags |= RATE_INFO_FLAGS_MCS;
7138763e
JB
5576 break;
5577 case RATE_MCS_VHT_MSK:
82cdbd11 5578 rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
7138763e 5579 break;
ed780545
JB
5580 }
5581}
5582
cbce62a3
MK
5583void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5584 struct ieee80211_vif *vif,
5585 struct ieee80211_sta *sta,
5586 struct station_info *sinfo)
33cef925
JB
5587{
5588 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5589 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5590 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5591
c8ee33e1
GG
5592 if (mvmsta->deflink.avg_energy) {
5593 sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
22d0d2fa 5594 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
988b5968
SS
5595 }
5596
ed780545 5597 if (iwl_mvm_has_tlc_offload(mvm)) {
c8ee33e1 5598 struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
ed780545
JB
5599
5600 iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5601 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5602 }
5603
33cef925
JB
5604 /* if beacon filtering isn't on mac80211 does it anyway */
5605 if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5606 return;
5607
f276e20b 5608 if (!vif->cfg.assoc)
33cef925
JB
5609 return;
5610
5611 mutex_lock(&mvm->mutex);
5612
c8ee33e1 5613 if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
33cef925
JB
5614 goto unlock;
5615
5616 if (iwl_mvm_request_statistics(mvm, false))
5617 goto unlock;
5618
650cadb7
GG
5619 sinfo->rx_beacon = mvmvif->deflink.beacon_stats.num_beacons +
5620 mvmvif->deflink.beacon_stats.accu_num_beacons;
22d0d2fa 5621 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
650cadb7 5622 if (mvmvif->deflink.beacon_stats.avg_signal) {
33cef925 5623 /* firmware only reports a value after RXing a few beacons */
650cadb7
GG
5624 sinfo->rx_beacon_signal_avg =
5625 mvmvif->deflink.beacon_stats.avg_signal;
22d0d2fa 5626 sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
33cef925
JB
5627 }
5628 unlock:
5629 mutex_unlock(&mvm->mutex);
5630}
5631
119c2a13
MG
5632static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5633 struct ieee80211_vif *vif,
5634 const struct ieee80211_mlme_event *mlme)
5635{
1a81bddf
MG
5636 if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
5637 (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
119c2a13
MG
5638 iwl_dbg_tlv_time_point(&mvm->fwrt,
5639 IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5640 NULL);
5641 return;
5642 }
5643
119c2a13
MG
5644 if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5645 iwl_dbg_tlv_time_point(&mvm->fwrt,
5646 IWL_FW_INI_TIME_POINT_DEASSOC,
5647 NULL);
5648 return;
5649 }
5650}
5651
4203263d
EG
5652static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5653 struct ieee80211_vif *vif,
5654 const struct ieee80211_event *event)
d42f5350 5655{
7174beb6
JB
5656#define CHECK_MLME_TRIGGER(_cnt, _fmt...) \
5657 do { \
5658 if ((trig_mlme->_cnt) && --(trig_mlme->_cnt)) \
5659 break; \
5660 iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt); \
d42f5350
EG
5661 } while (0)
5662
d42f5350
EG
5663 struct iwl_fw_dbg_trigger_tlv *trig;
5664 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
d42f5350 5665
119c2a13
MG
5666 if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5667 iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5668 return;
5669 }
5670
6c042d75
SS
5671 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5672 FW_DBG_TRIGGER_MLME);
5673 if (!trig)
d42f5350
EG
5674 return;
5675
d42f5350 5676 trig_mlme = (void *)trig->data;
d42f5350 5677
d42f5350
EG
5678 if (event->u.mlme.data == ASSOC_EVENT) {
5679 if (event->u.mlme.status == MLME_DENIED)
4c324a51 5680 CHECK_MLME_TRIGGER(stop_assoc_denied,
d42f5350
EG
5681 "DENIED ASSOC: reason %d",
5682 event->u.mlme.reason);
5683 else if (event->u.mlme.status == MLME_TIMEOUT)
4c324a51 5684 CHECK_MLME_TRIGGER(stop_assoc_timeout,
d42f5350
EG
5685 "ASSOC TIMEOUT");
5686 } else if (event->u.mlme.data == AUTH_EVENT) {
5687 if (event->u.mlme.status == MLME_DENIED)
4c324a51 5688 CHECK_MLME_TRIGGER(stop_auth_denied,
d42f5350
EG
5689 "DENIED AUTH: reason %d",
5690 event->u.mlme.reason);
5691 else if (event->u.mlme.status == MLME_TIMEOUT)
4c324a51 5692 CHECK_MLME_TRIGGER(stop_auth_timeout,
d42f5350
EG
5693 "AUTH TIMEOUT");
5694 } else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
4c324a51 5695 CHECK_MLME_TRIGGER(stop_rx_deauth,
d42f5350
EG
5696 "DEAUTH RX %d", event->u.mlme.reason);
5697 } else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
4c324a51 5698 CHECK_MLME_TRIGGER(stop_tx_deauth,
d42f5350
EG
5699 "DEAUTH TX %d", event->u.mlme.reason);
5700 }
5701#undef CHECK_MLME_TRIGGER
5702}
5703
4203263d
EG
5704static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5705 struct ieee80211_vif *vif,
5706 const struct ieee80211_event *event)
5707{
5708 struct iwl_fw_dbg_trigger_tlv *trig;
5709 struct iwl_fw_dbg_trigger_ba *ba_trig;
5710
6c042d75
SS
5711 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5712 FW_DBG_TRIGGER_BA);
5713 if (!trig)
4203263d
EG
5714 return;
5715
4203263d 5716 ba_trig = (void *)trig->data;
4203263d
EG
5717
5718 if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5719 return;
5720
7174beb6
JB
5721 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5722 "BAR received from %pM, tid %d, ssn %d",
5723 event->u.ba.sta->addr, event->u.ba.tid,
5724 event->u.ba.ssn);
4203263d
EG
5725}
5726
cbce62a3
MK
5727void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5728 struct ieee80211_vif *vif,
5729 const struct ieee80211_event *event)
4203263d
EG
5730{
5731 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5732
5733 switch (event->type) {
5734 case MLME_EVENT:
5735 iwl_mvm_event_mlme_callback(mvm, vif, event);
5736 break;
5737 case BAR_RX_EVENT:
5738 iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5739 break;
5740 case BA_FRAME_TIMEOUT:
528a542a
EG
5741 iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5742 event->u.ba.tid);
4203263d
EG
5743 break;
5744 default:
5745 break;
5746 }
5747}
5748
d0ff5d22 5749void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
5e1688ce
JB
5750 enum iwl_mvm_rxq_notif_type type,
5751 bool sync,
5752 const void *data, u32 size)
0636b938 5753{
5e1688ce
JB
5754 struct {
5755 struct iwl_rxq_sync_cmd cmd;
5756 struct iwl_mvm_internal_rxq_notif notif;
5757 } __packed cmd = {
5758 .cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
5759 .cmd.count =
5760 cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
5761 size),
5762 .notif.type = type,
5763 .notif.sync = sync,
5764 };
5765 struct iwl_host_cmd hcmd = {
5766 .id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
5767 .data[0] = &cmd,
5768 .len[0] = sizeof(cmd),
5769 .data[1] = data,
5770 .len[1] = size,
5771 .flags = sync ? 0 : CMD_ASYNC,
5772 };
0636b938
SS
5773 int ret;
5774
5e1688ce
JB
5775 /* size must be a multiple of DWORD */
5776 if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
5777 return;
0636b938 5778
cb8550e1 5779 if (!iwl_mvm_has_new_rx_api(mvm))
0636b938
SS
5780 return;
5781
5e1688ce
JB
5782 if (sync) {
5783 cmd.notif.cookie = mvm->queue_sync_cookie;
2f7a04c7 5784 mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
a2113cc4 5785 }
0636b938 5786
5e1688ce 5787 ret = iwl_mvm_send_cmd(mvm, &hcmd);
0636b938
SS
5788 if (ret) {
5789 IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
5790 goto out;
5791 }
d0ff5d22 5792
5e1688ce 5793 if (sync) {
3c514bf8 5794 lockdep_assert_held(&mvm->mutex);
3a732c65 5795 ret = wait_event_timeout(mvm->rx_sync_waitq,
2f7a04c7 5796 READ_ONCE(mvm->queue_sync_state) == 0 ||
6ad04359 5797 iwl_mvm_is_radio_killed(mvm),
d0ff5d22 5798 HZ);
2f7a04c7
JB
5799 WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
5800 "queue sync: failed to sync, state is 0x%lx\n",
5801 mvm->queue_sync_state);
6ad04359 5802 }
0636b938
SS
5803
5804out:
5e1688ce 5805 if (sync) {
5f8a3561 5806 mvm->queue_sync_state = 0;
a2113cc4 5807 mvm->queue_sync_cookie++;
5f8a3561 5808 }
0636b938
SS
5809}
5810
cbce62a3 5811void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
0636b938
SS
5812{
5813 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5814
5815 mutex_lock(&mvm->mutex);
5e1688ce 5816 iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
0636b938
SS
5817 mutex_unlock(&mvm->mutex);
5818}
5819
cbce62a3 5820int
b73f9a4a
JB
5821iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5822 struct ieee80211_vif *vif,
5823 struct cfg80211_ftm_responder_stats *stats)
5824{
5825 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5826 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5827
5828 if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5829 !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5830 return -EINVAL;
5831
5832 mutex_lock(&mvm->mutex);
5833 *stats = mvm->ftm_resp_stats;
5834 mutex_unlock(&mvm->mutex);
5835
5836 stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5837 BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5838 BIT(NL80211_FTM_STATS_FAILED_NUM) |
5839 BIT(NL80211_FTM_STATS_ASAP_NUM) |
5840 BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5841 BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5842 BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5843 BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5844 BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5845
5846 return 0;
5847}
5848
cbce62a3
MK
5849int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5850 struct cfg80211_pmsr_request *request)
fc36ffda
JB
5851{
5852 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5853 int ret;
5854
5855 mutex_lock(&mvm->mutex);
5856 ret = iwl_mvm_ftm_start(mvm, vif, request);
5857 mutex_unlock(&mvm->mutex);
5858
5859 return ret;
5860}
5861
cbce62a3
MK
5862void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5863 struct cfg80211_pmsr_request *request)
fc36ffda
JB
5864{
5865 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5866
5867 mutex_lock(&mvm->mutex);
5868 iwl_mvm_ftm_abort(mvm, request);
5869 mutex_unlock(&mvm->mutex);
5870}
5871
438af969
SS
5872static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5873{
5874 u8 protocol = ip_hdr(skb)->protocol;
5875
5876 if (!IS_ENABLED(CONFIG_INET))
5877 return false;
5878
5879 return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5880}
5881
5882static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5883 struct sk_buff *head,
5884 struct sk_buff *skb)
5885{
5886 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5887
0792df68
JB
5888 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_BZ)
5889 return iwl_mvm_tx_csum_bz(mvm, head, true) ==
5890 iwl_mvm_tx_csum_bz(mvm, skb, true);
5891
438af969
SS
5892 /* For now don't aggregate IPv6 in AMSDU */
5893 if (skb->protocol != htons(ETH_P_IP))
5894 return false;
5895
5896 if (!iwl_mvm_is_csum_supported(mvm))
5897 return true;
5898
5899 return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5900}
5901
be8897e2
AS
5902int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
5903 struct ieee80211_vif *vif,
5904 struct cfg80211_set_hw_timestamp *hwts)
cf85123a
AS
5905{
5906 struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5907 u32 protocols = 0;
5908 int ret;
5909
5910 /* HW timestamping is only supported for a specific station */
5911 if (!hwts->macaddr)
5912 return -EOPNOTSUPP;
5913
5914 if (hwts->enable)
5915 protocols =
5916 IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
5917
5918 mutex_lock(&mvm->mutex);
5919 ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
5920 mutex_unlock(&mvm->mutex);
5921
5922 return ret;
5923}
5924
e5209263 5925const struct ieee80211_ops iwl_mvm_hw_ops = {
8ca151b5 5926 .tx = iwl_mvm_mac_tx,
cfbc6c4c 5927 .wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
8ca151b5 5928 .ampdu_action = iwl_mvm_mac_ampdu_action,
e8503aec 5929 .get_antenna = iwl_mvm_op_get_antenna,
8ca151b5 5930 .start = iwl_mvm_mac_start,
cf2c92d8 5931 .reconfig_complete = iwl_mvm_mac_reconfig_complete,
8ca151b5
JB
5932 .stop = iwl_mvm_mac_stop,
5933 .add_interface = iwl_mvm_mac_add_interface,
5934 .remove_interface = iwl_mvm_mac_remove_interface,
5935 .config = iwl_mvm_mac_config,
e59647ea 5936 .prepare_multicast = iwl_mvm_prepare_multicast,
8ca151b5 5937 .configure_filter = iwl_mvm_configure_filter,
effd1929 5938 .config_iface_filter = iwl_mvm_config_iface_filter,
8ca151b5
JB
5939 .bss_info_changed = iwl_mvm_bss_info_changed,
5940 .hw_scan = iwl_mvm_mac_hw_scan,
5941 .cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
1ddbbb0c 5942 .sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
8ca151b5
JB
5943 .sta_state = iwl_mvm_mac_sta_state,
5944 .sta_notify = iwl_mvm_mac_sta_notify,
5945 .allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
3e56eadf 5946 .release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
8ca151b5 5947 .set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
1f3b0ff8 5948 .sta_rc_update = iwl_mvm_sta_rc_update,
8ca151b5
JB
5949 .conf_tx = iwl_mvm_mac_conf_tx,
5950 .mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6b1259d1 5951 .mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
07ecd897 5952 .mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
c5b0e7c0 5953 .flush = iwl_mvm_mac_flush,
35a000b7
DS
5954 .sched_scan_start = iwl_mvm_mac_sched_scan_start,
5955 .sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
8ca151b5
JB
5956 .set_key = iwl_mvm_mac_set_key,
5957 .update_tkip_key = iwl_mvm_mac_update_tkip_key,
5958 .remain_on_channel = iwl_mvm_roc,
5959 .cancel_remain_on_channel = iwl_mvm_cancel_roc,
8ca151b5
JB
5960 .add_chanctx = iwl_mvm_add_chanctx,
5961 .remove_chanctx = iwl_mvm_remove_chanctx,
5962 .change_chanctx = iwl_mvm_change_chanctx,
5963 .assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5964 .unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
b08c1d97 5965 .switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
8ca151b5 5966
ae7ba17b
ST
5967 .start_ap = iwl_mvm_start_ap,
5968 .stop_ap = iwl_mvm_stop_ap,
5969 .join_ibss = iwl_mvm_start_ibss,
5970 .leave_ibss = iwl_mvm_stop_ibss,
8ca151b5 5971
2f0282db
JB
5972 .tx_last_beacon = iwl_mvm_tx_last_beacon,
5973
8ca151b5
JB
5974 .set_tim = iwl_mvm_set_tim,
5975
622e3f9b 5976 .channel_switch = iwl_mvm_channel_switch,
f028905c 5977 .pre_channel_switch = iwl_mvm_pre_channel_switch,
f6c34820 5978 .post_channel_switch = iwl_mvm_post_channel_switch,
79221126 5979 .abort_channel_switch = iwl_mvm_abort_channel_switch,
c37763d2 5980 .channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
bd3398e2 5981
1d3c3f63
AN
5982 .tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5983 .tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5984 .tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5985
d42f5350
EG
5986 .event_callback = iwl_mvm_mac_event_callback,
5987
0636b938
SS
5988 .sync_rx_queues = iwl_mvm_sync_rx_queues,
5989
507cadf2
DS
5990 CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5991
8ca151b5
JB
5992#ifdef CONFIG_PM_SLEEP
5993 /* look at d3.c */
5994 .suspend = iwl_mvm_suspend,
5995 .resume = iwl_mvm_resume,
5996 .set_wakeup = iwl_mvm_set_wakeup,
5997 .set_rekey_data = iwl_mvm_set_rekey_data,
5998#if IS_ENABLED(CONFIG_IPV6)
5999 .ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6000#endif
6001 .set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6002#endif
91a8bcde 6003 .get_survey = iwl_mvm_mac_get_survey,
33cef925 6004 .sta_statistics = iwl_mvm_mac_sta_statistics,
b73f9a4a 6005 .get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
fc36ffda
JB
6006 .start_pmsr = iwl_mvm_start_pmsr,
6007 .abort_pmsr = iwl_mvm_abort_pmsr,
b73f9a4a 6008
438af969 6009 .can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
177a11cf
GG
6010#ifdef CONFIG_IWLWIFI_DEBUGFS
6011 .sta_add_debugfs = iwl_mvm_sta_add_debugfs,
6012#endif
cf85123a 6013 .set_hw_timestamp = iwl_mvm_set_hw_timestamp,
8ca151b5 6014};