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