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