xref: /linux/drivers/net/wireless/intel/iwlwifi/mvm/mac80211.c (revision 332d2c1d713e232e163386c35a3ba0c1b90df83f)
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_cleanup_sta_iterator(void *data, struct ieee80211_sta *sta)
1132 {
1133 	struct iwl_mvm *mvm = data;
1134 	struct iwl_mvm_sta *mvm_sta;
1135 	struct ieee80211_vif *vif;
1136 	int link_id;
1137 
1138 	mvm_sta = iwl_mvm_sta_from_mac80211(sta);
1139 	vif = mvm_sta->vif;
1140 
1141 	if (!sta->valid_links)
1142 		return;
1143 
1144 	for (link_id = 0; link_id < ARRAY_SIZE((sta)->link); link_id++) {
1145 		struct iwl_mvm_link_sta *mvm_link_sta;
1146 
1147 		mvm_link_sta =
1148 			rcu_dereference_check(mvm_sta->link[link_id],
1149 					      lockdep_is_held(&mvm->mutex));
1150 		if (mvm_link_sta && !(vif->active_links & BIT(link_id))) {
1151 			/*
1152 			 * We have a link STA but the link is inactive in
1153 			 * mac80211. This will happen if we failed to
1154 			 * deactivate the link but mac80211 roll back the
1155 			 * deactivation of the link.
1156 			 * Delete the stale data to avoid issues later on.
1157 			 */
1158 			iwl_mvm_mld_free_sta_link(mvm, mvm_sta, mvm_link_sta,
1159 						  link_id, false);
1160 		}
1161 	}
1162 }
1163 
1164 static void iwl_mvm_restart_cleanup(struct iwl_mvm *mvm)
1165 {
1166 	iwl_mvm_stop_device(mvm);
1167 
1168 	mvm->cur_aid = 0;
1169 
1170 	mvm->scan_status = 0;
1171 	mvm->ps_disabled = false;
1172 	mvm->rfkill_safe_init_done = false;
1173 
1174 	/* just in case one was running */
1175 	iwl_mvm_cleanup_roc_te(mvm);
1176 	ieee80211_remain_on_channel_expired(mvm->hw);
1177 
1178 	iwl_mvm_ftm_restart(mvm);
1179 
1180 	/*
1181 	 * cleanup all interfaces, even inactive ones, as some might have
1182 	 * gone down during the HW restart
1183 	 */
1184 	ieee80211_iterate_interfaces(mvm->hw, 0, iwl_mvm_cleanup_iterator, mvm);
1185 
1186 	/* cleanup stations as links may be gone after restart */
1187 	ieee80211_iterate_stations_atomic(mvm->hw,
1188 					  iwl_mvm_cleanup_sta_iterator, mvm);
1189 
1190 	mvm->p2p_device_vif = NULL;
1191 
1192 	iwl_mvm_reset_phy_ctxts(mvm);
1193 	memset(mvm->fw_key_table, 0, sizeof(mvm->fw_key_table));
1194 	memset(&mvm->last_bt_notif, 0, sizeof(mvm->last_bt_notif));
1195 	memset(&mvm->last_bt_ci_cmd, 0, sizeof(mvm->last_bt_ci_cmd));
1196 
1197 	ieee80211_wake_queues(mvm->hw);
1198 
1199 	mvm->rx_ba_sessions = 0;
1200 	mvm->fwrt.dump.conf = FW_DBG_INVALID;
1201 	mvm->monitor_on = false;
1202 #ifdef CONFIG_IWLWIFI_DEBUGFS
1203 	mvm->beacon_inject_active = false;
1204 #endif
1205 
1206 	/* keep statistics ticking */
1207 	iwl_mvm_accu_radio_stats(mvm);
1208 }
1209 
1210 int __iwl_mvm_mac_start(struct iwl_mvm *mvm)
1211 {
1212 	int ret;
1213 
1214 	lockdep_assert_held(&mvm->mutex);
1215 
1216 	ret = iwl_mvm_mei_get_ownership(mvm);
1217 	if (ret)
1218 		return ret;
1219 
1220 	if (mvm->mei_nvm_data) {
1221 		/* We got the NIC, we can now free the MEI NVM data */
1222 		kfree(mvm->mei_nvm_data);
1223 		mvm->mei_nvm_data = NULL;
1224 
1225 		/*
1226 		 * We can't free the nvm_data we allocated based on the SAP
1227 		 * data because we registered to cfg80211 with the channels
1228 		 * allocated on mvm->nvm_data. Keep a pointer in temp_nvm_data
1229 		 * just in order to be able free it later.
1230 		 * NULLify nvm_data so that we will read the NVM from the
1231 		 * firmware this time.
1232 		 */
1233 		mvm->temp_nvm_data = mvm->nvm_data;
1234 		mvm->nvm_data = NULL;
1235 	}
1236 
1237 	if (test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status)) {
1238 		/*
1239 		 * Now convert the HW_RESTART_REQUESTED flag to IN_HW_RESTART
1240 		 * so later code will - from now on - see that we're doing it.
1241 		 */
1242 		set_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1243 		clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status);
1244 		/* Clean up some internal and mac80211 state on restart */
1245 		iwl_mvm_restart_cleanup(mvm);
1246 	}
1247 	ret = iwl_mvm_up(mvm);
1248 
1249 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_POST_INIT,
1250 			       NULL);
1251 	iwl_dbg_tlv_time_point(&mvm->fwrt, IWL_FW_INI_TIME_POINT_PERIODIC,
1252 			       NULL);
1253 
1254 	mvm->last_reset_or_resume_time_jiffies = jiffies;
1255 
1256 	if (ret && test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
1257 		/* Something went wrong - we need to finish some cleanup
1258 		 * that normally iwl_mvm_mac_restart_complete() below
1259 		 * would do.
1260 		 */
1261 		clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1262 	}
1263 
1264 	return ret;
1265 }
1266 
1267 int iwl_mvm_mac_start(struct ieee80211_hw *hw)
1268 {
1269 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1270 	int ret;
1271 
1272 	mutex_lock(&mvm->mutex);
1273 
1274 	/* we are starting the mac not in error flow, and restart is enabled */
1275 	if (!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) &&
1276 	    iwlwifi_mod_params.fw_restart) {
1277 		/*
1278 		 * This will prevent mac80211 recovery flows to trigger during
1279 		 * init failures
1280 		 */
1281 		set_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1282 	}
1283 
1284 	ret = __iwl_mvm_mac_start(mvm);
1285 	clear_bit(IWL_MVM_STATUS_STARTING, &mvm->status);
1286 
1287 	mutex_unlock(&mvm->mutex);
1288 
1289 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1290 
1291 	return ret;
1292 }
1293 
1294 static void iwl_mvm_restart_complete(struct iwl_mvm *mvm)
1295 {
1296 	int ret;
1297 
1298 	mutex_lock(&mvm->mutex);
1299 
1300 	clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status);
1301 
1302 	ret = iwl_mvm_update_quotas(mvm, true, NULL);
1303 	if (ret)
1304 		IWL_ERR(mvm, "Failed to update quotas after restart (%d)\n",
1305 			ret);
1306 
1307 	iwl_mvm_send_recovery_cmd(mvm, ERROR_RECOVERY_END_OF_RECOVERY);
1308 
1309 	/*
1310 	 * If we have TDLS peers, remove them. We don't know the last seqno/PN
1311 	 * of packets the FW sent out, so we must reconnect.
1312 	 */
1313 	iwl_mvm_teardown_tdls_peers(mvm);
1314 
1315 	mutex_unlock(&mvm->mutex);
1316 }
1317 
1318 void iwl_mvm_mac_reconfig_complete(struct ieee80211_hw *hw,
1319 				   enum ieee80211_reconfig_type reconfig_type)
1320 {
1321 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1322 
1323 	switch (reconfig_type) {
1324 	case IEEE80211_RECONFIG_TYPE_RESTART:
1325 		iwl_mvm_restart_complete(mvm);
1326 		break;
1327 	case IEEE80211_RECONFIG_TYPE_SUSPEND:
1328 		break;
1329 	}
1330 }
1331 
1332 void __iwl_mvm_mac_stop(struct iwl_mvm *mvm)
1333 {
1334 	lockdep_assert_held(&mvm->mutex);
1335 
1336 	iwl_mvm_ftm_initiator_smooth_stop(mvm);
1337 
1338 	/* firmware counters are obviously reset now, but we shouldn't
1339 	 * partially track so also clear the fw_reset_accu counters.
1340 	 */
1341 	memset(&mvm->accu_radio_stats, 0, sizeof(mvm->accu_radio_stats));
1342 
1343 	/* async_handlers_wk is now blocked */
1344 
1345 	if (!iwl_mvm_has_new_station_api(mvm->fw))
1346 		iwl_mvm_rm_aux_sta(mvm);
1347 
1348 	iwl_mvm_stop_device(mvm);
1349 
1350 	iwl_mvm_async_handlers_purge(mvm);
1351 	/* async_handlers_list is empty and will stay empty: HW is stopped */
1352 
1353 	/*
1354 	 * Clear IN_HW_RESTART and HW_RESTART_REQUESTED flag when stopping the
1355 	 * hw (as restart_complete() won't be called in this case) and mac80211
1356 	 * won't execute the restart.
1357 	 * But make sure to cleanup interfaces that have gone down before/during
1358 	 * HW restart was requested.
1359 	 */
1360 	if (test_and_clear_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) ||
1361 	    test_and_clear_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
1362 			       &mvm->status))
1363 		ieee80211_iterate_interfaces(mvm->hw, 0,
1364 					     iwl_mvm_cleanup_iterator, mvm);
1365 
1366 	/* We shouldn't have any UIDs still set.  Loop over all the UIDs to
1367 	 * make sure there's nothing left there and warn if any is found.
1368 	 */
1369 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN)) {
1370 		int i;
1371 
1372 		for (i = 0; i < mvm->max_scans; i++) {
1373 			if (WARN_ONCE(mvm->scan_uid_status[i],
1374 				      "UMAC scan UID %d status was not cleaned\n",
1375 				      i))
1376 				mvm->scan_uid_status[i] = 0;
1377 		}
1378 	}
1379 }
1380 
1381 void iwl_mvm_mac_stop(struct ieee80211_hw *hw)
1382 {
1383 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1384 
1385 	/* Stop internal MLO scan, if running */
1386 	mutex_lock(&mvm->mutex);
1387 	iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_INT_MLO, false);
1388 	mutex_unlock(&mvm->mutex);
1389 
1390 	wiphy_work_cancel(mvm->hw->wiphy, &mvm->trig_link_selection_wk);
1391 	wiphy_work_flush(mvm->hw->wiphy, &mvm->async_handlers_wiphy_wk);
1392 	flush_work(&mvm->async_handlers_wk);
1393 	flush_work(&mvm->add_stream_wk);
1394 
1395 	/*
1396 	 * Lock and clear the firmware running bit here already, so that
1397 	 * new commands coming in elsewhere, e.g. from debugfs, will not
1398 	 * be able to proceed. This is important here because one of those
1399 	 * debugfs files causes the firmware dump to be triggered, and if we
1400 	 * don't stop debugfs accesses before canceling that it could be
1401 	 * retriggered after we flush it but before we've cleared the bit.
1402 	 */
1403 	clear_bit(IWL_MVM_STATUS_FIRMWARE_RUNNING, &mvm->status);
1404 
1405 	cancel_delayed_work_sync(&mvm->cs_tx_unblock_dwork);
1406 	cancel_delayed_work_sync(&mvm->scan_timeout_dwork);
1407 
1408 	/*
1409 	 * The work item could be running or queued if the
1410 	 * ROC time event stops just as we get here.
1411 	 */
1412 	flush_work(&mvm->roc_done_wk);
1413 
1414 	iwl_mvm_mei_set_sw_rfkill_state(mvm);
1415 
1416 	mutex_lock(&mvm->mutex);
1417 	__iwl_mvm_mac_stop(mvm);
1418 	mutex_unlock(&mvm->mutex);
1419 
1420 	/*
1421 	 * The worker might have been waiting for the mutex, let it run and
1422 	 * discover that its list is now empty.
1423 	 */
1424 	cancel_work_sync(&mvm->async_handlers_wk);
1425 	wiphy_work_cancel(hw->wiphy, &mvm->async_handlers_wiphy_wk);
1426 }
1427 
1428 struct iwl_mvm_phy_ctxt *iwl_mvm_get_free_phy_ctxt(struct iwl_mvm *mvm)
1429 {
1430 	u16 i;
1431 
1432 	lockdep_assert_held(&mvm->mutex);
1433 
1434 	for (i = 0; i < NUM_PHY_CTX; i++)
1435 		if (!mvm->phy_ctxts[i].ref)
1436 			return &mvm->phy_ctxts[i];
1437 
1438 	IWL_ERR(mvm, "No available PHY context\n");
1439 	return NULL;
1440 }
1441 
1442 int iwl_mvm_set_tx_power(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1443 			 s16 tx_power)
1444 {
1445 	u32 cmd_id = REDUCE_TX_POWER_CMD;
1446 	int len;
1447 	struct iwl_dev_tx_power_cmd cmd = {
1448 		.common.set_mode = cpu_to_le32(IWL_TX_POWER_MODE_SET_MAC),
1449 		.common.mac_context_id =
1450 			cpu_to_le32(iwl_mvm_vif_from_mac80211(vif)->id),
1451 		.common.pwr_restriction = cpu_to_le16(8 * tx_power),
1452 	};
1453 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
1454 					   IWL_FW_CMD_VER_UNKNOWN);
1455 
1456 	if (tx_power == IWL_DEFAULT_MAX_TX_POWER)
1457 		cmd.common.pwr_restriction = cpu_to_le16(IWL_DEV_MAX_TX_POWER);
1458 
1459 	if (cmd_ver == 8)
1460 		len = sizeof(cmd.v8);
1461 	else if (cmd_ver == 7)
1462 		len = sizeof(cmd.v7);
1463 	else if (cmd_ver == 6)
1464 		len = sizeof(cmd.v6);
1465 	else if (fw_has_api(&mvm->fw->ucode_capa,
1466 			    IWL_UCODE_TLV_API_REDUCE_TX_POWER))
1467 		len = sizeof(cmd.v5);
1468 	else if (fw_has_capa(&mvm->fw->ucode_capa,
1469 			     IWL_UCODE_TLV_CAPA_TX_POWER_ACK))
1470 		len = sizeof(cmd.v4);
1471 	else
1472 		len = sizeof(cmd.v3);
1473 
1474 	/* all structs have the same common part, add it */
1475 	len += sizeof(cmd.common);
1476 
1477 	return iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, len, &cmd);
1478 }
1479 
1480 static void iwl_mvm_post_csa_tx(void *data, struct ieee80211_sta *sta)
1481 {
1482 	struct ieee80211_hw *hw = data;
1483 	int i;
1484 
1485 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
1486 		struct iwl_mvm_txq *mvmtxq =
1487 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
1488 
1489 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1490 		iwl_mvm_mac_itxq_xmit(hw, sta->txq[i]);
1491 	}
1492 }
1493 
1494 int iwl_mvm_post_channel_switch(struct ieee80211_hw *hw,
1495 				struct ieee80211_vif *vif,
1496 				struct ieee80211_bss_conf *link_conf)
1497 {
1498 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1499 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1500 	int ret;
1501 
1502 	mutex_lock(&mvm->mutex);
1503 
1504 	if (vif->type == NL80211_IFTYPE_STATION) {
1505 		struct iwl_mvm_sta *mvmsta;
1506 		unsigned int link_id = link_conf->link_id;
1507 		u8 ap_sta_id = mvmvif->link[link_id]->ap_sta_id;
1508 
1509 		mvmvif->csa_bcn_pending = false;
1510 		mvmvif->csa_blocks_tx = false;
1511 		mvmsta = iwl_mvm_sta_from_staid_protected(mvm, ap_sta_id);
1512 
1513 		if (WARN_ON(!mvmsta)) {
1514 			ret = -EIO;
1515 			goto out_unlock;
1516 		}
1517 
1518 		iwl_mvm_sta_modify_disable_tx(mvm, mvmsta, false);
1519 		if (mvm->mld_api_is_used)
1520 			iwl_mvm_mld_mac_ctxt_changed(mvm, vif, false);
1521 		else
1522 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1523 
1524 		if (!fw_has_capa(&mvm->fw->ucode_capa,
1525 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
1526 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
1527 			if (ret)
1528 				goto out_unlock;
1529 
1530 			iwl_mvm_stop_session_protection(mvm, vif);
1531 		}
1532 	} else if (vif->type == NL80211_IFTYPE_AP && mvmvif->csa_blocks_tx) {
1533 		struct iwl_mvm_txq *mvmtxq =
1534 			iwl_mvm_txq_from_mac80211(vif->txq);
1535 
1536 		clear_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
1537 
1538 		local_bh_disable();
1539 		iwl_mvm_mac_itxq_xmit(hw, vif->txq);
1540 		ieee80211_iterate_stations_atomic(hw, iwl_mvm_post_csa_tx, hw);
1541 		local_bh_enable();
1542 
1543 		mvmvif->csa_blocks_tx = false;
1544 	}
1545 
1546 	mvmvif->ps_disabled = false;
1547 
1548 	ret = iwl_mvm_power_update_ps(mvm);
1549 
1550 out_unlock:
1551 	if (mvmvif->csa_failed)
1552 		ret = -EIO;
1553 	mutex_unlock(&mvm->mutex);
1554 
1555 	return ret;
1556 }
1557 
1558 void iwl_mvm_abort_channel_switch(struct ieee80211_hw *hw,
1559 				  struct ieee80211_vif *vif,
1560 				  struct ieee80211_bss_conf *link_conf)
1561 {
1562 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1563 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1564 	struct iwl_chan_switch_te_cmd cmd = {
1565 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1566 							  mvmvif->color)),
1567 		.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE),
1568 	};
1569 
1570 	/*
1571 	 * In the new flow since FW is in charge of the timing,
1572 	 * if driver has canceled the channel switch he will receive the
1573 	 * CHANNEL_SWITCH_START_NOTIF notification from FW and then cancel it
1574 	 */
1575 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
1576 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
1577 		return;
1578 
1579 	IWL_DEBUG_MAC80211(mvm, "Abort CSA on mac %d\n", mvmvif->id);
1580 
1581 	mutex_lock(&mvm->mutex);
1582 	if (!fw_has_capa(&mvm->fw->ucode_capa,
1583 			 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
1584 		iwl_mvm_remove_csa_period(mvm, vif);
1585 	else
1586 		WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
1587 					     WIDE_ID(MAC_CONF_GROUP,
1588 						     CHANNEL_SWITCH_TIME_EVENT_CMD),
1589 					     0, sizeof(cmd), &cmd));
1590 	mvmvif->csa_failed = true;
1591 	mutex_unlock(&mvm->mutex);
1592 
1593 	/* If we're here, we can't support MLD */
1594 	iwl_mvm_post_channel_switch(hw, vif, &vif->bss_conf);
1595 }
1596 
1597 void iwl_mvm_channel_switch_disconnect_wk(struct work_struct *wk)
1598 {
1599 	struct iwl_mvm_vif *mvmvif;
1600 	struct ieee80211_vif *vif;
1601 
1602 	mvmvif = container_of(wk, struct iwl_mvm_vif, csa_work.work);
1603 	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1604 
1605 	/* Trigger disconnect (should clear the CSA state) */
1606 	ieee80211_chswitch_done(vif, false, 0);
1607 }
1608 
1609 static u8
1610 iwl_mvm_chandef_get_primary_80(struct cfg80211_chan_def *chandef)
1611 {
1612 	int data_start;
1613 	int control_start;
1614 	int bw;
1615 
1616 	if (chandef->width == NL80211_CHAN_WIDTH_320)
1617 		bw = 320;
1618 	else if (chandef->width == NL80211_CHAN_WIDTH_160)
1619 		bw = 160;
1620 	else
1621 		return 0;
1622 
1623 	/* data is bw wide so the start is half the width */
1624 	data_start = chandef->center_freq1 - bw / 2;
1625 	/* control is 20Mhz width */
1626 	control_start = chandef->chan->center_freq - 10;
1627 
1628 	return (control_start - data_start) / 80;
1629 }
1630 
1631 static int iwl_mvm_alloc_bcast_mcast_sta(struct iwl_mvm *mvm,
1632 					 struct ieee80211_vif *vif)
1633 {
1634 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1635 	int ret;
1636 
1637 	lockdep_assert_held(&mvm->mutex);
1638 
1639 	ret = iwl_mvm_alloc_bcast_sta(mvm, vif);
1640 	if (ret) {
1641 		IWL_ERR(mvm, "Failed to allocate bcast sta\n");
1642 		return ret;
1643 	}
1644 
1645 	/* Only queue for this station is the mcast queue,
1646 	 * which shouldn't be in TFD mask anyway
1647 	 */
1648 	return iwl_mvm_allocate_int_sta(mvm, &mvmvif->deflink.mcast_sta, 0,
1649 					vif->type,
1650 					IWL_STA_MULTICAST);
1651 }
1652 
1653 static void iwl_mvm_prevent_esr_done_wk(struct wiphy *wiphy,
1654 					struct wiphy_work *wk)
1655 {
1656 	struct iwl_mvm_vif *mvmvif =
1657 		container_of(wk, struct iwl_mvm_vif, prevent_esr_done_wk.work);
1658 	struct iwl_mvm *mvm = mvmvif->mvm;
1659 	struct ieee80211_vif *vif =
1660 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1661 
1662 	mutex_lock(&mvm->mutex);
1663 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_PREVENTION);
1664 	mutex_unlock(&mvm->mutex);
1665 }
1666 
1667 static void iwl_mvm_mlo_int_scan_wk(struct wiphy *wiphy, struct wiphy_work *wk)
1668 {
1669 	struct iwl_mvm_vif *mvmvif = container_of(wk, struct iwl_mvm_vif,
1670 						  mlo_int_scan_wk.work);
1671 	struct ieee80211_vif *vif =
1672 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1673 
1674 	mutex_lock(&mvmvif->mvm->mutex);
1675 
1676 	iwl_mvm_int_mlo_scan(mvmvif->mvm, vif);
1677 
1678 	mutex_unlock(&mvmvif->mvm->mutex);
1679 }
1680 
1681 static void iwl_mvm_unblock_esr_tpt(struct wiphy *wiphy, struct wiphy_work *wk)
1682 {
1683 	struct iwl_mvm_vif *mvmvif =
1684 		container_of(wk, struct iwl_mvm_vif, unblock_esr_tpt_wk);
1685 	struct iwl_mvm *mvm = mvmvif->mvm;
1686 	struct ieee80211_vif *vif =
1687 		container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
1688 
1689 	mutex_lock(&mvm->mutex);
1690 	iwl_mvm_unblock_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT);
1691 	mutex_unlock(&mvm->mutex);
1692 }
1693 
1694 void iwl_mvm_mac_init_mvmvif(struct iwl_mvm *mvm, struct iwl_mvm_vif *mvmvif)
1695 {
1696 	lockdep_assert_held(&mvm->mutex);
1697 
1698 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1699 		return;
1700 
1701 	INIT_DELAYED_WORK(&mvmvif->csa_work,
1702 			  iwl_mvm_channel_switch_disconnect_wk);
1703 
1704 	wiphy_delayed_work_init(&mvmvif->prevent_esr_done_wk,
1705 				iwl_mvm_prevent_esr_done_wk);
1706 
1707 	wiphy_delayed_work_init(&mvmvif->mlo_int_scan_wk,
1708 				iwl_mvm_mlo_int_scan_wk);
1709 
1710 	wiphy_work_init(&mvmvif->unblock_esr_tpt_wk,
1711 			iwl_mvm_unblock_esr_tpt);
1712 }
1713 
1714 static int iwl_mvm_mac_add_interface(struct ieee80211_hw *hw,
1715 				     struct ieee80211_vif *vif)
1716 {
1717 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1718 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1719 	int ret;
1720 	int i;
1721 
1722 	mutex_lock(&mvm->mutex);
1723 
1724 	iwl_mvm_mac_init_mvmvif(mvm, mvmvif);
1725 
1726 	mvmvif->mvm = mvm;
1727 
1728 	/* the first link always points to the default one */
1729 	mvmvif->deflink.fw_link_id = IWL_MVM_FW_LINK_ID_INVALID;
1730 	mvmvif->deflink.active = 0;
1731 	mvmvif->link[0] = &mvmvif->deflink;
1732 
1733 	ret = iwl_mvm_set_link_mapping(mvm, vif, &vif->bss_conf);
1734 	if (ret)
1735 		goto out;
1736 
1737 	/*
1738 	 * Not much to do here. The stack will not allow interface
1739 	 * types or combinations that we didn't advertise, so we
1740 	 * don't really have to check the types.
1741 	 */
1742 
1743 	/* make sure that beacon statistics don't go backwards with FW reset */
1744 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1745 		for_each_mvm_vif_valid_link(mvmvif, i)
1746 			mvmvif->link[i]->beacon_stats.accu_num_beacons +=
1747 				mvmvif->link[i]->beacon_stats.num_beacons;
1748 
1749 	/* Allocate resources for the MAC context, and add it to the fw  */
1750 	ret = iwl_mvm_mac_ctxt_init(mvm, vif);
1751 	if (ret)
1752 		goto out;
1753 
1754 	rcu_assign_pointer(mvm->vif_id_to_mac[mvmvif->id], vif);
1755 
1756 	/* Currently not much to do for NAN */
1757 	if (vif->type == NL80211_IFTYPE_NAN) {
1758 		ret = 0;
1759 		goto out;
1760 	}
1761 
1762 	/*
1763 	 * The AP binding flow can be done only after the beacon
1764 	 * template is configured (which happens only in the mac80211
1765 	 * start_ap() flow), and adding the broadcast station can happen
1766 	 * only after the binding.
1767 	 * In addition, since modifying the MAC before adding a bcast
1768 	 * station is not allowed by the FW, delay the adding of MAC context to
1769 	 * the point where we can also add the bcast station.
1770 	 * In short: there's not much we can do at this point, other than
1771 	 * allocating resources :)
1772 	 */
1773 	if (vif->type == NL80211_IFTYPE_AP ||
1774 	    vif->type == NL80211_IFTYPE_ADHOC) {
1775 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1776 			iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1777 		ret = 0;
1778 		goto out;
1779 	}
1780 
1781 	mvmvif->features |= hw->netdev_features;
1782 
1783 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
1784 	if (ret)
1785 		goto out_unlock;
1786 
1787 	ret = iwl_mvm_power_update_mac(mvm);
1788 	if (ret)
1789 		goto out_remove_mac;
1790 
1791 	/* beacon filtering */
1792 	ret = iwl_mvm_disable_beacon_filter(mvm, vif);
1793 	if (ret)
1794 		goto out_remove_mac;
1795 
1796 	if (!mvm->bf_allowed_vif &&
1797 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p) {
1798 		mvm->bf_allowed_vif = mvmvif;
1799 		vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER |
1800 				     IEEE80211_VIF_SUPPORTS_CQM_RSSI;
1801 	}
1802 
1803 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
1804 		mvm->p2p_device_vif = vif;
1805 
1806 	iwl_mvm_tcm_add_vif(mvm, vif);
1807 
1808 	if (vif->type == NL80211_IFTYPE_MONITOR) {
1809 		mvm->monitor_on = true;
1810 		mvm->monitor_p80 =
1811 			iwl_mvm_chandef_get_primary_80(&vif->bss_conf.chanreq.oper);
1812 	}
1813 
1814 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
1815 		iwl_mvm_vif_dbgfs_add_link(mvm, vif);
1816 
1817 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
1818 	    vif->type == NL80211_IFTYPE_STATION && !vif->p2p &&
1819 	    !mvm->csme_vif && mvm->mei_registered) {
1820 		iwl_mei_set_nic_info(vif->addr, mvm->nvm_data->hw_addr);
1821 		iwl_mei_set_netdev(ieee80211_vif_to_wdev(vif)->netdev);
1822 		mvm->csme_vif = vif;
1823 	}
1824 
1825 out:
1826 	if (!ret && (vif->type == NL80211_IFTYPE_AP ||
1827 		     vif->type == NL80211_IFTYPE_ADHOC))
1828 		ret = iwl_mvm_alloc_bcast_mcast_sta(mvm, vif);
1829 
1830 	goto out_unlock;
1831 
1832  out_remove_mac:
1833 	mvmvif->deflink.phy_ctxt = NULL;
1834 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1835  out_unlock:
1836 	mutex_unlock(&mvm->mutex);
1837 
1838 	return ret;
1839 }
1840 
1841 void iwl_mvm_prepare_mac_removal(struct iwl_mvm *mvm,
1842 				 struct ieee80211_vif *vif)
1843 {
1844 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1845 
1846 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1847 		/*
1848 		 * Flush the ROC worker which will flush the OFFCHANNEL queue.
1849 		 * We assume here that all the packets sent to the OFFCHANNEL
1850 		 * queue are sent in ROC session.
1851 		 */
1852 		flush_work(&mvm->roc_done_wk);
1853 	}
1854 
1855 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1856 				  &mvmvif->prevent_esr_done_wk);
1857 
1858 	wiphy_delayed_work_cancel(mvm->hw->wiphy,
1859 				  &mvmvif->mlo_int_scan_wk);
1860 
1861 	wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
1862 
1863 	cancel_delayed_work_sync(&mvmvif->csa_work);
1864 }
1865 
1866 /* This function is doing the common part of removing the interface for
1867  * both - MLD and non-MLD modes. Returns true if removing the interface
1868  * is done
1869  */
1870 static bool iwl_mvm_mac_remove_interface_common(struct ieee80211_hw *hw,
1871 						struct ieee80211_vif *vif)
1872 {
1873 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1874 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1875 	struct iwl_probe_resp_data *probe_data;
1876 
1877 	iwl_mvm_prepare_mac_removal(mvm, vif);
1878 
1879 	if (!(vif->type == NL80211_IFTYPE_AP ||
1880 	      vif->type == NL80211_IFTYPE_ADHOC))
1881 		iwl_mvm_tcm_rm_vif(mvm, vif);
1882 
1883 	mutex_lock(&mvm->mutex);
1884 
1885 	if (vif == mvm->csme_vif) {
1886 		iwl_mei_set_netdev(NULL);
1887 		mvm->csme_vif = NULL;
1888 	}
1889 
1890 	probe_data = rcu_dereference_protected(mvmvif->deflink.probe_resp_data,
1891 					       lockdep_is_held(&mvm->mutex));
1892 	RCU_INIT_POINTER(mvmvif->deflink.probe_resp_data, NULL);
1893 	if (probe_data)
1894 		kfree_rcu(probe_data, rcu_head);
1895 
1896 	if (mvm->bf_allowed_vif == mvmvif) {
1897 		mvm->bf_allowed_vif = NULL;
1898 		vif->driver_flags &= ~(IEEE80211_VIF_BEACON_FILTER |
1899 				       IEEE80211_VIF_SUPPORTS_CQM_RSSI);
1900 	}
1901 
1902 	if (vif->bss_conf.ftm_responder)
1903 		memset(&mvm->ftm_resp_stats, 0, sizeof(mvm->ftm_resp_stats));
1904 
1905 	iwl_mvm_vif_dbgfs_rm_link(mvm, vif);
1906 
1907 	/*
1908 	 * For AP/GO interface, the tear down of the resources allocated to the
1909 	 * interface is be handled as part of the stop_ap flow.
1910 	 */
1911 	if (vif->type == NL80211_IFTYPE_AP ||
1912 	    vif->type == NL80211_IFTYPE_ADHOC) {
1913 #ifdef CONFIG_NL80211_TESTMODE
1914 		if (vif == mvm->noa_vif) {
1915 			mvm->noa_vif = NULL;
1916 			mvm->noa_duration = 0;
1917 		}
1918 #endif
1919 		return true;
1920 	}
1921 
1922 	iwl_mvm_power_update_mac(mvm);
1923 	return false;
1924 }
1925 
1926 static void iwl_mvm_mac_remove_interface(struct ieee80211_hw *hw,
1927 					 struct ieee80211_vif *vif)
1928 {
1929 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1930 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1931 
1932 	if (iwl_mvm_mac_remove_interface_common(hw, vif))
1933 		goto out;
1934 
1935 	/* Before the interface removal, mac80211 would cancel the ROC, and the
1936 	 * ROC worker would be scheduled if needed. The worker would be flushed
1937 	 * in iwl_mvm_prepare_mac_removal() and thus at this point there is no
1938 	 * binding etc. so nothing needs to be done here.
1939 	 */
1940 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
1941 		if (mvmvif->deflink.phy_ctxt) {
1942 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
1943 			mvmvif->deflink.phy_ctxt = NULL;
1944 		}
1945 		mvm->p2p_device_vif = NULL;
1946 	}
1947 
1948 	iwl_mvm_unset_link_mapping(mvm, vif, &vif->bss_conf);
1949 	iwl_mvm_mac_ctxt_remove(mvm, vif);
1950 
1951 	RCU_INIT_POINTER(mvm->vif_id_to_mac[mvmvif->id], NULL);
1952 
1953 	if (vif->type == NL80211_IFTYPE_MONITOR)
1954 		mvm->monitor_on = false;
1955 
1956 out:
1957 	if (vif->type == NL80211_IFTYPE_AP ||
1958 	    vif->type == NL80211_IFTYPE_ADHOC) {
1959 		iwl_mvm_dealloc_int_sta(mvm, &mvmvif->deflink.mcast_sta);
1960 		iwl_mvm_dealloc_bcast_sta(mvm, vif);
1961 	}
1962 
1963 	mutex_unlock(&mvm->mutex);
1964 }
1965 
1966 struct iwl_mvm_mc_iter_data {
1967 	struct iwl_mvm *mvm;
1968 	int port_id;
1969 };
1970 
1971 static void iwl_mvm_mc_iface_iterator(void *_data, u8 *mac,
1972 				      struct ieee80211_vif *vif)
1973 {
1974 	struct iwl_mvm_mc_iter_data *data = _data;
1975 	struct iwl_mvm *mvm = data->mvm;
1976 	struct iwl_mcast_filter_cmd *cmd = mvm->mcast_filter_cmd;
1977 	struct iwl_host_cmd hcmd = {
1978 		.id = MCAST_FILTER_CMD,
1979 		.flags = CMD_ASYNC,
1980 		.dataflags[0] = IWL_HCMD_DFL_NOCOPY,
1981 	};
1982 	int ret, len;
1983 
1984 	/* if we don't have free ports, mcast frames will be dropped */
1985 	if (WARN_ON_ONCE(data->port_id >= MAX_PORT_ID_NUM))
1986 		return;
1987 
1988 	if (vif->type != NL80211_IFTYPE_STATION ||
1989 	    !vif->cfg.assoc)
1990 		return;
1991 
1992 	cmd->port_id = data->port_id++;
1993 	memcpy(cmd->bssid, vif->bss_conf.bssid, ETH_ALEN);
1994 	len = roundup(sizeof(*cmd) + cmd->count * ETH_ALEN, 4);
1995 
1996 	hcmd.len[0] = len;
1997 	hcmd.data[0] = cmd;
1998 
1999 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
2000 	if (ret)
2001 		IWL_ERR(mvm, "mcast filter cmd error. ret=%d\n", ret);
2002 }
2003 
2004 static void iwl_mvm_recalc_multicast(struct iwl_mvm *mvm)
2005 {
2006 	struct iwl_mvm_mc_iter_data iter_data = {
2007 		.mvm = mvm,
2008 	};
2009 	int ret;
2010 
2011 	lockdep_assert_held(&mvm->mutex);
2012 
2013 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
2014 		return;
2015 
2016 	ieee80211_iterate_active_interfaces_atomic(
2017 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2018 		iwl_mvm_mc_iface_iterator, &iter_data);
2019 
2020 	/*
2021 	 * Send a (synchronous) ech command so that we wait for the
2022 	 * multiple asynchronous MCAST_FILTER_CMD commands sent by
2023 	 * the interface iterator. Otherwise, we might get here over
2024 	 * and over again (by userspace just sending a lot of these)
2025 	 * and the CPU can send them faster than the firmware can
2026 	 * process them.
2027 	 * Note that the CPU is still faster - but with this we'll
2028 	 * actually send fewer commands overall because the CPU will
2029 	 * not schedule the work in mac80211 as frequently if it's
2030 	 * still running when rescheduled (possibly multiple times).
2031 	 */
2032 	ret = iwl_mvm_send_cmd_pdu(mvm, ECHO_CMD, 0, 0, NULL);
2033 	if (ret)
2034 		IWL_ERR(mvm, "Failed to synchronize multicast groups update\n");
2035 }
2036 
2037 u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
2038 			      struct netdev_hw_addr_list *mc_list)
2039 {
2040 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2041 	struct iwl_mcast_filter_cmd *cmd;
2042 	struct netdev_hw_addr *addr;
2043 	int addr_count;
2044 	bool pass_all;
2045 	int len;
2046 
2047 	addr_count = netdev_hw_addr_list_count(mc_list);
2048 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
2049 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
2050 	if (pass_all)
2051 		addr_count = 0;
2052 
2053 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
2054 	cmd = kzalloc(len, GFP_ATOMIC);
2055 	if (!cmd)
2056 		return 0;
2057 
2058 	if (pass_all) {
2059 		cmd->pass_all = 1;
2060 		return (u64)(unsigned long)cmd;
2061 	}
2062 
2063 	netdev_hw_addr_list_for_each(addr, mc_list) {
2064 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
2065 				   cmd->count, addr->addr);
2066 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
2067 		       addr->addr, ETH_ALEN);
2068 		cmd->count++;
2069 	}
2070 
2071 	return (u64)(unsigned long)cmd;
2072 }
2073 
2074 void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
2075 			      unsigned int changed_flags,
2076 			      unsigned int *total_flags, u64 multicast)
2077 {
2078 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2079 	struct iwl_mcast_filter_cmd *cmd = (void *)(unsigned long)multicast;
2080 
2081 	mutex_lock(&mvm->mutex);
2082 
2083 	/* replace previous configuration */
2084 	kfree(mvm->mcast_filter_cmd);
2085 	mvm->mcast_filter_cmd = cmd;
2086 
2087 	if (!cmd)
2088 		goto out;
2089 
2090 	if (changed_flags & FIF_ALLMULTI)
2091 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
2092 
2093 	if (cmd->pass_all)
2094 		cmd->count = 0;
2095 
2096 	iwl_mvm_recalc_multicast(mvm);
2097 out:
2098 	mutex_unlock(&mvm->mutex);
2099 	*total_flags = 0;
2100 }
2101 
2102 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
2103 					struct ieee80211_vif *vif,
2104 					unsigned int filter_flags,
2105 					unsigned int changed_flags)
2106 {
2107 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2108 
2109 	/* We support only filter for probe requests */
2110 	if (!(changed_flags & FIF_PROBE_REQ))
2111 		return;
2112 
2113 	/* Supported only for p2p client interfaces */
2114 	if (vif->type != NL80211_IFTYPE_STATION || !vif->cfg.assoc ||
2115 	    !vif->p2p)
2116 		return;
2117 
2118 	mutex_lock(&mvm->mutex);
2119 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2120 	mutex_unlock(&mvm->mutex);
2121 }
2122 
2123 int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2124 {
2125 	struct iwl_mu_group_mgmt_cmd cmd = {};
2126 
2127 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
2128 	       WLAN_MEMBERSHIP_LEN);
2129 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
2130 	       WLAN_USER_POSITION_LEN);
2131 
2132 	return iwl_mvm_send_cmd_pdu(mvm,
2133 				    WIDE_ID(DATA_PATH_GROUP,
2134 					    UPDATE_MU_GROUPS_CMD),
2135 				    0, sizeof(cmd), &cmd);
2136 }
2137 
2138 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
2139 					   struct ieee80211_vif *vif)
2140 {
2141 	if (vif->bss_conf.mu_mimo_owner) {
2142 		struct iwl_mu_group_mgmt_notif *notif = _data;
2143 
2144 		/*
2145 		 * MU-MIMO Group Id action frame is little endian. We treat
2146 		 * the data received from firmware as if it came from the
2147 		 * action frame, so no conversion is needed.
2148 		 */
2149 		ieee80211_update_mu_groups(vif, 0,
2150 					   (u8 *)&notif->membership_status,
2151 					   (u8 *)&notif->user_position);
2152 	}
2153 }
2154 
2155 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
2156 			       struct iwl_rx_cmd_buffer *rxb)
2157 {
2158 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2159 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
2160 
2161 	ieee80211_iterate_active_interfaces_atomic(
2162 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
2163 			iwl_mvm_mu_mimo_iface_iterator, notif);
2164 }
2165 
2166 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
2167 {
2168 	u8 byte_num = ppe_pos_bit / 8;
2169 	u8 bit_num = ppe_pos_bit % 8;
2170 	u8 residue_bits;
2171 	u8 res;
2172 
2173 	if (bit_num <= 5)
2174 		return (ppe[byte_num] >> bit_num) &
2175 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
2176 
2177 	/*
2178 	 * If bit_num > 5, we have to combine bits with next byte.
2179 	 * Calculate how many bits we need to take from current byte (called
2180 	 * here "residue_bits"), and add them to bits from next byte.
2181 	 */
2182 
2183 	residue_bits = 8 - bit_num;
2184 
2185 	res = (ppe[byte_num + 1] &
2186 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2187 	      residue_bits;
2188 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2189 
2190 	return res;
2191 }
2192 
2193 static void iwl_mvm_parse_ppe(struct iwl_mvm *mvm,
2194 			      struct iwl_he_pkt_ext_v2 *pkt_ext, u8 nss,
2195 			      u8 ru_index_bitmap, u8 *ppe, u8 ppe_pos_bit,
2196 			      bool inheritance)
2197 {
2198 	int i;
2199 
2200 	/*
2201 	* FW currently supports only nss == MAX_HE_SUPP_NSS
2202 	*
2203 	* If nss > MAX: we can ignore values we don't support
2204 	* If nss < MAX: we can set zeros in other streams
2205 	*/
2206 	if (nss > MAX_HE_SUPP_NSS) {
2207 		IWL_DEBUG_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2208 			       MAX_HE_SUPP_NSS);
2209 		nss = MAX_HE_SUPP_NSS;
2210 	}
2211 
2212 	for (i = 0; i < nss; i++) {
2213 		u8 ru_index_tmp = ru_index_bitmap << 1;
2214 		u8 low_th = IWL_HE_PKT_EXT_NONE, high_th = IWL_HE_PKT_EXT_NONE;
2215 		u8 bw;
2216 
2217 		for (bw = 0;
2218 		     bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2219 		     bw++) {
2220 			ru_index_tmp >>= 1;
2221 
2222 			/*
2223 			* According to the 11be spec, if for a specific BW the PPE Thresholds
2224 			* isn't present - it should inherit the thresholds from the last
2225 			* BW for which we had PPE Thresholds. In 11ax though, we don't have
2226 			* this inheritance - continue in this case
2227 			*/
2228 			if (!(ru_index_tmp & 1)) {
2229 				if (inheritance)
2230 					goto set_thresholds;
2231 				else
2232 					continue;
2233 			}
2234 
2235 			high_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2236 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2237 			low_th = iwl_mvm_he_get_ppe_val(ppe, ppe_pos_bit);
2238 			ppe_pos_bit += IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2239 
2240 set_thresholds:
2241 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2242 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2243 		}
2244 	}
2245 }
2246 
2247 static void iwl_mvm_set_pkt_ext_from_he_ppe(struct iwl_mvm *mvm,
2248 					    struct ieee80211_link_sta *link_sta,
2249 					    struct iwl_he_pkt_ext_v2 *pkt_ext,
2250 					    bool inheritance)
2251 {
2252 	u8 nss = (link_sta->he_cap.ppe_thres[0] &
2253 		  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2254 	u8 *ppe = &link_sta->he_cap.ppe_thres[0];
2255 	u8 ru_index_bitmap =
2256 		u8_get_bits(*ppe,
2257 			    IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK);
2258 	/* Starting after PPE header */
2259 	u8 ppe_pos_bit = IEEE80211_HE_PPE_THRES_INFO_HEADER_SIZE;
2260 
2261 	iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap, ppe, ppe_pos_bit,
2262 			  inheritance);
2263 }
2264 
2265 static int
2266 iwl_mvm_set_pkt_ext_from_nominal_padding(struct iwl_he_pkt_ext_v2 *pkt_ext,
2267 					 u8 nominal_padding)
2268 {
2269 	int low_th = -1;
2270 	int high_th = -1;
2271 	int i;
2272 
2273 	/* all the macros are the same for EHT and HE */
2274 	switch (nominal_padding) {
2275 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_0US:
2276 		low_th = IWL_HE_PKT_EXT_NONE;
2277 		high_th = IWL_HE_PKT_EXT_NONE;
2278 		break;
2279 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US:
2280 		low_th = IWL_HE_PKT_EXT_BPSK;
2281 		high_th = IWL_HE_PKT_EXT_NONE;
2282 		break;
2283 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_16US:
2284 	case IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_20US:
2285 		low_th = IWL_HE_PKT_EXT_NONE;
2286 		high_th = IWL_HE_PKT_EXT_BPSK;
2287 		break;
2288 	}
2289 
2290 	if (low_th < 0 || high_th < 0)
2291 		return -EINVAL;
2292 
2293 	/* Set the PPE thresholds accordingly */
2294 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2295 		u8 bw;
2296 
2297 		for (bw = 0;
2298 			bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2299 			bw++) {
2300 			pkt_ext->pkt_ext_qam_th[i][bw][0] = low_th;
2301 			pkt_ext->pkt_ext_qam_th[i][bw][1] = high_th;
2302 		}
2303 	}
2304 
2305 	return 0;
2306 }
2307 
2308 static void iwl_mvm_get_optimal_ppe_info(struct iwl_he_pkt_ext_v2 *pkt_ext,
2309 					 u8 nominal_padding)
2310 {
2311 	int i;
2312 
2313 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2314 		u8 bw;
2315 
2316 		for (bw = 0; bw < ARRAY_SIZE(pkt_ext->pkt_ext_qam_th[i]);
2317 		     bw++) {
2318 			u8 *qam_th = &pkt_ext->pkt_ext_qam_th[i][bw][0];
2319 
2320 			if (nominal_padding >
2321 			    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2322 			    qam_th[1] == IWL_HE_PKT_EXT_NONE)
2323 				qam_th[1] = IWL_HE_PKT_EXT_4096QAM;
2324 			else if (nominal_padding ==
2325 				 IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_8US &&
2326 				 qam_th[0] == IWL_HE_PKT_EXT_NONE &&
2327 				 qam_th[1] == IWL_HE_PKT_EXT_NONE)
2328 				qam_th[0] = IWL_HE_PKT_EXT_4096QAM;
2329 		}
2330 	}
2331 }
2332 
2333 /* Set the pkt_ext field according to PPE Thresholds element */
2334 int iwl_mvm_set_sta_pkt_ext(struct iwl_mvm *mvm,
2335 			    struct ieee80211_link_sta *link_sta,
2336 			    struct iwl_he_pkt_ext_v2 *pkt_ext)
2337 {
2338 	u8 nominal_padding;
2339 	int i, ret = 0;
2340 
2341 	if (WARN_ON(!link_sta))
2342 		return -EINVAL;
2343 
2344 	/* Initialize the PPE thresholds to "None" (7), as described in Table
2345 	 * 9-262ac of 80211.ax/D3.0.
2346 	 */
2347 	memset(pkt_ext, IWL_HE_PKT_EXT_NONE,
2348 	       sizeof(struct iwl_he_pkt_ext_v2));
2349 
2350 	if (link_sta->eht_cap.has_eht) {
2351 		nominal_padding =
2352 			u8_get_bits(link_sta->eht_cap.eht_cap_elem.phy_cap_info[5],
2353 				    IEEE80211_EHT_PHY_CAP5_COMMON_NOMINAL_PKT_PAD_MASK);
2354 
2355 		/* If PPE Thresholds exists, parse them into a FW-familiar
2356 		 * format.
2357 		 */
2358 		if (link_sta->eht_cap.eht_cap_elem.phy_cap_info[5] &
2359 		    IEEE80211_EHT_PHY_CAP5_PPE_THRESHOLD_PRESENT) {
2360 			u8 nss = (link_sta->eht_cap.eht_ppe_thres[0] &
2361 				IEEE80211_EHT_PPE_THRES_NSS_MASK) + 1;
2362 			u8 *ppe = &link_sta->eht_cap.eht_ppe_thres[0];
2363 			u8 ru_index_bitmap =
2364 				u16_get_bits(*ppe,
2365 					     IEEE80211_EHT_PPE_THRES_RU_INDEX_BITMASK_MASK);
2366 			 /* Starting after PPE header */
2367 			u8 ppe_pos_bit = IEEE80211_EHT_PPE_THRES_INFO_HEADER_SIZE;
2368 
2369 			iwl_mvm_parse_ppe(mvm, pkt_ext, nss, ru_index_bitmap,
2370 					  ppe, ppe_pos_bit, true);
2371 		/* EHT PPE Thresholds doesn't exist - set the API according to
2372 		 * HE PPE Tresholds
2373 		 */
2374 		} else if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2375 			   IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2376 			/* Even though HE Capabilities IE doesn't contain PPE
2377 			 * Thresholds for BW 320Mhz, thresholds for this BW will
2378 			 * be filled in with the same values as 160Mhz, due to
2379 			 * the inheritance, as required.
2380 			 */
2381 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2382 							true);
2383 
2384 			/* According to the requirements, for MCSs 12-13 the
2385 			 * maximum value between HE PPE Threshold and Common
2386 			 * Nominal Packet Padding needs to be taken
2387 			 */
2388 			iwl_mvm_get_optimal_ppe_info(pkt_ext, nominal_padding);
2389 
2390 		/* if PPE Thresholds doesn't present in both EHT IE and HE IE -
2391 		 * take the Thresholds from Common Nominal Packet Padding field
2392 		 */
2393 		} else {
2394 			ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2395 								       nominal_padding);
2396 		}
2397 	} else if (link_sta->he_cap.has_he) {
2398 		/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2399 		if (link_sta->he_cap.he_cap_elem.phy_cap_info[6] &
2400 			IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2401 			iwl_mvm_set_pkt_ext_from_he_ppe(mvm, link_sta, pkt_ext,
2402 							false);
2403 		/* PPE Thresholds doesn't exist - set the API PPE values
2404 		 * according to Common Nominal Packet Padding field.
2405 		 */
2406 		} else {
2407 			nominal_padding =
2408 				u8_get_bits(link_sta->he_cap.he_cap_elem.phy_cap_info[9],
2409 					    IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_MASK);
2410 			if (nominal_padding != IEEE80211_HE_PHY_CAP9_NOMINAL_PKT_PADDING_RESERVED)
2411 				ret = iwl_mvm_set_pkt_ext_from_nominal_padding(pkt_ext,
2412 									       nominal_padding);
2413 		}
2414 	}
2415 
2416 	for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2417 		int bw;
2418 
2419 		for (bw = 0;
2420 		     bw < ARRAY_SIZE(*pkt_ext->pkt_ext_qam_th[i]);
2421 		     bw++) {
2422 			u8 *qam_th =
2423 				&pkt_ext->pkt_ext_qam_th[i][bw][0];
2424 
2425 			IWL_DEBUG_HT(mvm,
2426 				     "PPE table: nss[%d] bw[%d] PPET8 = %d, PPET16 = %d\n",
2427 				     i, bw, qam_th[0], qam_th[1]);
2428 		}
2429 	}
2430 	return ret;
2431 }
2432 
2433 /*
2434  * This function sets the MU EDCA parameters ans returns whether MU EDCA
2435  * is enabled or not
2436  */
2437 bool iwl_mvm_set_fw_mu_edca_params(struct iwl_mvm *mvm,
2438 				   const struct iwl_mvm_vif_link_info *link_info,
2439 				   struct iwl_he_backoff_conf *trig_based_txf)
2440 {
2441 	int i;
2442 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2443 	bool mu_edca_enabled = true;
2444 
2445 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2446 		const struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2447 			&link_info->queue_params[i].mu_edca_param_rec;
2448 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2449 
2450 		if (!link_info->queue_params[i].mu_edca) {
2451 			mu_edca_enabled = false;
2452 			break;
2453 		}
2454 
2455 		trig_based_txf[ac].cwmin =
2456 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2457 		trig_based_txf[ac].cwmax =
2458 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2459 		trig_based_txf[ac].aifsn =
2460 			cpu_to_le16(mu_edca->aifsn & 0xf);
2461 		trig_based_txf[ac].mu_time =
2462 			cpu_to_le16(mu_edca->mu_edca_timer);
2463 	}
2464 
2465 	return mu_edca_enabled;
2466 }
2467 
2468 bool iwl_mvm_is_nic_ack_enabled(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2469 {
2470 	const struct ieee80211_supported_band *sband;
2471 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2472 
2473 	/* This capability is the same for all bands,
2474 	 * so take it from one of them.
2475 	 */
2476 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
2477 	own_he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
2478 
2479 	return (own_he_cap && (own_he_cap->he_cap_elem.mac_cap_info[2] &
2480 			       IEEE80211_HE_MAC_CAP2_ACK_EN));
2481 }
2482 
2483 __le32 iwl_mvm_get_sta_htc_flags(struct ieee80211_sta *sta,
2484 				 struct ieee80211_link_sta *link_sta)
2485 {
2486 	u8 *mac_cap_info =
2487 		&link_sta->he_cap.he_cap_elem.mac_cap_info[0];
2488 	__le32 htc_flags = 0;
2489 
2490 	if (mac_cap_info[0] & IEEE80211_HE_MAC_CAP0_HTC_HE)
2491 		htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2492 	if ((mac_cap_info[1] & IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2493 	    (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2494 		u8 link_adap =
2495 			((mac_cap_info[2] &
2496 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2497 			 (mac_cap_info[1] &
2498 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2499 
2500 		if (link_adap == 2)
2501 			htc_flags |=
2502 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2503 		else if (link_adap == 3)
2504 			htc_flags |= cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2505 	}
2506 	if (mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2507 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2508 	if (mac_cap_info[3] & IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2509 		htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2510 	if (mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2511 		htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2512 
2513 	return htc_flags;
2514 }
2515 
2516 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2517 			       struct ieee80211_vif *vif, u8 sta_id)
2518 {
2519 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2520 	struct iwl_he_sta_context_cmd_v3 sta_ctxt_cmd = {
2521 		.sta_id = sta_id,
2522 		.tid_limit = IWL_MAX_TID_COUNT,
2523 		.bss_color = vif->bss_conf.he_bss_color.color,
2524 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2525 		.frame_time_rts_th =
2526 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2527 	};
2528 	struct iwl_he_sta_context_cmd_v2 sta_ctxt_cmd_v2 = {};
2529 	u32 cmd_id = WIDE_ID(DATA_PATH_GROUP, STA_HE_CTXT_CMD);
2530 	u8 ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id, 2);
2531 	int size;
2532 	struct ieee80211_sta *sta;
2533 	u32 flags;
2534 	int i;
2535 	void *cmd;
2536 
2537 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_MBSSID_HE))
2538 		ver = 1;
2539 
2540 	switch (ver) {
2541 	case 1:
2542 		/* same layout as v2 except some data at the end */
2543 		cmd = &sta_ctxt_cmd_v2;
2544 		size = sizeof(struct iwl_he_sta_context_cmd_v1);
2545 		break;
2546 	case 2:
2547 		cmd = &sta_ctxt_cmd_v2;
2548 		size = sizeof(struct iwl_he_sta_context_cmd_v2);
2549 		break;
2550 	case 3:
2551 		cmd = &sta_ctxt_cmd;
2552 		size = sizeof(struct iwl_he_sta_context_cmd_v3);
2553 		break;
2554 	default:
2555 		IWL_ERR(mvm, "bad STA_HE_CTXT_CMD version %d\n", ver);
2556 		return;
2557 	}
2558 
2559 	rcu_read_lock();
2560 
2561 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2562 	if (IS_ERR_OR_NULL(sta)) {
2563 		rcu_read_unlock();
2564 		WARN(1, "Can't find STA to configure HE\n");
2565 		return;
2566 	}
2567 
2568 	if (!sta->deflink.he_cap.has_he) {
2569 		rcu_read_unlock();
2570 		return;
2571 	}
2572 
2573 	flags = 0;
2574 
2575 	/* Block 26-tone RU OFDMA transmissions */
2576 	if (mvmvif->deflink.he_ru_2mhz_block)
2577 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2578 
2579 	/* HTC flags */
2580 	sta_ctxt_cmd.htc_flags = iwl_mvm_get_sta_htc_flags(sta, &sta->deflink);
2581 
2582 	/* PPE Thresholds */
2583 	if (!iwl_mvm_set_sta_pkt_ext(mvm, &sta->deflink, &sta_ctxt_cmd.pkt_ext))
2584 		flags |= STA_CTXT_HE_PACKET_EXT;
2585 
2586 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2587 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2588 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2589 
2590 	if (sta->deflink.he_cap.he_cap_elem.mac_cap_info[2] &
2591 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2592 		flags |= STA_CTXT_HE_ACK_ENABLED;
2593 
2594 	rcu_read_unlock();
2595 
2596 	if (iwl_mvm_set_fw_mu_edca_params(mvm, &mvmvif->deflink,
2597 					  &sta_ctxt_cmd.trig_based_txf[0]))
2598 		flags |= STA_CTXT_HE_MU_EDCA_CW;
2599 
2600 	if (vif->bss_conf.uora_exists) {
2601 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2602 
2603 		sta_ctxt_cmd.rand_alloc_ecwmin =
2604 			vif->bss_conf.uora_ocw_range & 0x7;
2605 		sta_ctxt_cmd.rand_alloc_ecwmax =
2606 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2607 	}
2608 
2609 	if (!iwl_mvm_is_nic_ack_enabled(mvm, vif))
2610 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2611 
2612 	if (vif->bss_conf.nontransmitted) {
2613 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2614 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2615 				vif->bss_conf.transmitter_bssid);
2616 		sta_ctxt_cmd.max_bssid_indicator =
2617 			vif->bss_conf.bssid_indicator;
2618 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2619 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2620 		sta_ctxt_cmd.profile_periodicity =
2621 			vif->bss_conf.profile_periodicity;
2622 	}
2623 
2624 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2625 
2626 	if (ver < 3) {
2627 		/* fields before pkt_ext */
2628 		BUILD_BUG_ON(offsetof(typeof(sta_ctxt_cmd), pkt_ext) !=
2629 			     offsetof(typeof(sta_ctxt_cmd_v2), pkt_ext));
2630 		memcpy(&sta_ctxt_cmd_v2, &sta_ctxt_cmd,
2631 		       offsetof(typeof(sta_ctxt_cmd), pkt_ext));
2632 
2633 		/* pkt_ext */
2634 		for (i = 0;
2635 		     i < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th);
2636 		     i++) {
2637 			u8 bw;
2638 
2639 			for (bw = 0;
2640 			     bw < ARRAY_SIZE(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i]);
2641 			     bw++) {
2642 				BUILD_BUG_ON(sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]) !=
2643 					     sizeof(sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw]));
2644 
2645 				memcpy(&sta_ctxt_cmd_v2.pkt_ext.pkt_ext_qam_th[i][bw],
2646 				       &sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw],
2647 				       sizeof(sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw]));
2648 			}
2649 		}
2650 
2651 		/* fields after pkt_ext */
2652 		BUILD_BUG_ON(sizeof(sta_ctxt_cmd) -
2653 			     offsetofend(typeof(sta_ctxt_cmd), pkt_ext) !=
2654 			     sizeof(sta_ctxt_cmd_v2) -
2655 			     offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext));
2656 		memcpy((u8 *)&sta_ctxt_cmd_v2 +
2657 				offsetofend(typeof(sta_ctxt_cmd_v2), pkt_ext),
2658 		       (u8 *)&sta_ctxt_cmd +
2659 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext),
2660 		       sizeof(sta_ctxt_cmd) -
2661 				offsetofend(typeof(sta_ctxt_cmd), pkt_ext));
2662 		sta_ctxt_cmd_v2.reserved3 = 0;
2663 	}
2664 
2665 	if (iwl_mvm_send_cmd_pdu(mvm, cmd_id, 0, size, cmd))
2666 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2667 }
2668 
2669 void iwl_mvm_protect_assoc(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2670 			   u32 duration_override, unsigned int link_id)
2671 {
2672 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2673 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2674 
2675 	if (duration_override > duration)
2676 		duration = duration_override;
2677 
2678 	/* Try really hard to protect the session and hear a beacon
2679 	 * The new session protection command allows us to protect the
2680 	 * session for a much longer time since the firmware will internally
2681 	 * create two events: a 300TU one with a very high priority that
2682 	 * won't be fragmented which should be enough for 99% of the cases,
2683 	 * and another one (which we configure here to be 900TU long) which
2684 	 * will have a slightly lower priority, but more importantly, can be
2685 	 * fragmented so that it'll allow other activities to run.
2686 	 */
2687 	if (fw_has_capa(&mvm->fw->ucode_capa,
2688 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2689 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2690 						    min_duration, false,
2691 						    link_id);
2692 	else
2693 		iwl_mvm_protect_session(mvm, vif, duration,
2694 					min_duration, 500, false);
2695 }
2696 
2697 /* Handle association common part to MLD and non-MLD modes */
2698 void iwl_mvm_bss_info_changed_station_assoc(struct iwl_mvm *mvm,
2699 					    struct ieee80211_vif *vif,
2700 					    u64 changes)
2701 {
2702 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2703 	int ret;
2704 	int link_id;
2705 
2706 	/* The firmware tracks the MU-MIMO group on its own.
2707 	 * However, on HW restart we should restore this data.
2708 	 */
2709 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2710 	    (changes & BSS_CHANGED_MU_GROUPS) && vif->bss_conf.mu_mimo_owner) {
2711 		ret = iwl_mvm_update_mu_groups(mvm, vif);
2712 		if (ret)
2713 			IWL_ERR(mvm,
2714 				"failed to update VHT MU_MIMO groups\n");
2715 	}
2716 
2717 	iwl_mvm_recalc_multicast(mvm);
2718 
2719 	/* reset rssi values */
2720 	for_each_mvm_vif_valid_link(mvmvif, link_id)
2721 		mvmvif->link[link_id]->bf_data.ave_beacon_signal = 0;
2722 
2723 	iwl_mvm_bt_coex_vif_change(mvm);
2724 	iwl_mvm_update_smps_on_active_links(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2725 					    IEEE80211_SMPS_AUTOMATIC);
2726 	if (fw_has_capa(&mvm->fw->ucode_capa,
2727 			IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2728 		iwl_mvm_config_scan(mvm);
2729 }
2730 
2731 /* Execute the common part for MLD and non-MLD modes */
2732 void
2733 iwl_mvm_bss_info_changed_station_common(struct iwl_mvm *mvm,
2734 					struct ieee80211_vif *vif,
2735 					struct ieee80211_bss_conf *link_conf,
2736 					u64 changes)
2737 {
2738 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2739 	int ret;
2740 
2741 	if (changes & BSS_CHANGED_BEACON_INFO) {
2742 		/* We received a beacon from the associated AP so
2743 		 * remove the session protection.
2744 		 */
2745 		iwl_mvm_stop_session_protection(mvm, vif);
2746 
2747 		iwl_mvm_sf_update(mvm, vif, false);
2748 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
2749 	}
2750 
2751 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2752 		       /* Send power command on every beacon change,
2753 			* because we may have not enabled beacon abort yet.
2754 			*/
2755 		       BSS_CHANGED_BEACON_INFO)) {
2756 		ret = iwl_mvm_power_update_mac(mvm);
2757 		if (ret)
2758 			IWL_ERR(mvm, "failed to update power mode\n");
2759 	}
2760 
2761 	if (changes & BSS_CHANGED_CQM) {
2762 		struct iwl_mvm_vif_link_info *link_info =
2763 			mvmvif->link[link_conf->link_id];
2764 
2765 		IWL_DEBUG_MAC80211(mvm, "CQM info_changed\n");
2766 		if (link_info)
2767 			link_info->bf_data.last_cqm_event = 0;
2768 
2769 		if (mvmvif->bf_enabled) {
2770 			/* FIXME: need to update per link when FW API will
2771 			 * support it
2772 			 */
2773 			ret = iwl_mvm_enable_beacon_filter(mvm, vif);
2774 			if (ret)
2775 				IWL_ERR(mvm,
2776 					"failed to update CQM thresholds\n");
2777 		}
2778 	}
2779 
2780 	if (changes & BSS_CHANGED_BANDWIDTH)
2781 		iwl_mvm_update_link_smps(vif, link_conf);
2782 }
2783 
2784 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2785 					     struct ieee80211_vif *vif,
2786 					     struct ieee80211_bss_conf *bss_conf,
2787 					     u64 changes)
2788 {
2789 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2790 	int ret;
2791 	int i;
2792 
2793 	/*
2794 	 * Re-calculate the tsf id, as the leader-follower relations depend
2795 	 * on the beacon interval, which was not known when the station
2796 	 * interface was added.
2797 	 */
2798 	if (changes & BSS_CHANGED_ASSOC && vif->cfg.assoc) {
2799 		if ((vif->bss_conf.he_support &&
2800 		     !iwlwifi_mod_params.disable_11ax))
2801 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2802 
2803 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2804 	}
2805 
2806 	/* Update MU EDCA params */
2807 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2808 	    vif->cfg.assoc &&
2809 	    (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax))
2810 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->deflink.ap_sta_id);
2811 
2812 	/*
2813 	 * If we're not associated yet, take the (new) BSSID before associating
2814 	 * so the firmware knows. If we're already associated, then use the old
2815 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2816 	 * branch for disassociation below.
2817 	 */
2818 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2819 		memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2820 
2821 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->deflink.bssid);
2822 	if (ret)
2823 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2824 
2825 	/* after sending it once, adopt mac80211 data */
2826 	memcpy(mvmvif->deflink.bssid, bss_conf->bssid, ETH_ALEN);
2827 	mvmvif->associated = vif->cfg.assoc;
2828 
2829 	if (changes & BSS_CHANGED_ASSOC) {
2830 		if (vif->cfg.assoc) {
2831 			/* clear statistics to get clean beacon counter */
2832 			iwl_mvm_request_statistics(mvm, true);
2833 			for_each_mvm_vif_valid_link(mvmvif, i)
2834 				memset(&mvmvif->link[i]->beacon_stats, 0,
2835 				       sizeof(mvmvif->link[i]->beacon_stats));
2836 
2837 			/* add quota for this interface */
2838 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2839 			if (ret) {
2840 				IWL_ERR(mvm, "failed to update quotas\n");
2841 				return;
2842 			}
2843 
2844 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2845 				     &mvm->status) &&
2846 			    !fw_has_capa(&mvm->fw->ucode_capa,
2847 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2848 				/*
2849 				 * If we're restarting then the firmware will
2850 				 * obviously have lost synchronisation with
2851 				 * the AP. It will attempt to synchronise by
2852 				 * itself, but we can make it more reliable by
2853 				 * scheduling a session protection time event.
2854 				 *
2855 				 * The firmware needs to receive a beacon to
2856 				 * catch up with synchronisation, use 110% of
2857 				 * the beacon interval.
2858 				 *
2859 				 * Set a large maximum delay to allow for more
2860 				 * than a single interface.
2861 				 *
2862 				 * For new firmware versions, rely on the
2863 				 * firmware. This is relevant for DCM scenarios
2864 				 * only anyway.
2865 				 */
2866 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2867 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2868 							5 * dur, false);
2869 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2870 					     &mvm->status) &&
2871 				   !vif->bss_conf.dtim_period) {
2872 				/*
2873 				 * If we're not restarting and still haven't
2874 				 * heard a beacon (dtim period unknown) then
2875 				 * make sure we still have enough minimum time
2876 				 * remaining in the time event, since the auth
2877 				 * might actually have taken quite a while
2878 				 * (especially for SAE) and so the remaining
2879 				 * time could be small without us having heard
2880 				 * a beacon yet.
2881 				 */
2882 				iwl_mvm_protect_assoc(mvm, vif, 0, 0);
2883 			}
2884 
2885 			iwl_mvm_sf_update(mvm, vif, false);
2886 			iwl_mvm_power_vif_assoc(mvm, vif);
2887 			if (vif->p2p) {
2888 				iwl_mvm_update_smps(mvm, vif,
2889 						    IWL_MVM_SMPS_REQ_PROT,
2890 						    IEEE80211_SMPS_DYNAMIC, 0);
2891 			}
2892 		} else if (mvmvif->deflink.ap_sta_id != IWL_MVM_INVALID_STA) {
2893 			iwl_mvm_mei_host_disassociated(mvm);
2894 			/*
2895 			 * If update fails - SF might be running in associated
2896 			 * mode while disassociated - which is forbidden.
2897 			 */
2898 			ret = iwl_mvm_sf_update(mvm, vif, false);
2899 			WARN_ONCE(ret &&
2900 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2901 					    &mvm->status),
2902 				  "Failed to update SF upon disassociation\n");
2903 
2904 			/*
2905 			 * If we get an assert during the connection (after the
2906 			 * station has been added, but before the vif is set
2907 			 * to associated), mac80211 will re-add the station and
2908 			 * then configure the vif. Since the vif is not
2909 			 * associated, we would remove the station here and
2910 			 * this would fail the recovery.
2911 			 */
2912 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2913 				      &mvm->status)) {
2914 				/* first remove remaining keys */
2915 				iwl_mvm_sec_key_remove_ap(mvm, vif,
2916 							  &mvmvif->deflink, 0);
2917 
2918 				/*
2919 				 * Remove AP station now that
2920 				 * the MAC is unassoc
2921 				 */
2922 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2923 							mvmvif->deflink.ap_sta_id);
2924 				if (ret)
2925 					IWL_ERR(mvm,
2926 						"failed to remove AP station\n");
2927 
2928 				mvmvif->deflink.ap_sta_id = IWL_MVM_INVALID_STA;
2929 			}
2930 
2931 			/* remove quota for this interface */
2932 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2933 			if (ret)
2934 				IWL_ERR(mvm, "failed to update quotas\n");
2935 
2936 			/* this will take the cleared BSSID from bss_conf */
2937 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2938 			if (ret)
2939 				IWL_ERR(mvm,
2940 					"failed to update MAC %pM (clear after unassoc)\n",
2941 					vif->addr);
2942 		}
2943 
2944 		iwl_mvm_bss_info_changed_station_assoc(mvm, vif, changes);
2945 	}
2946 
2947 	iwl_mvm_bss_info_changed_station_common(mvm, vif, &vif->bss_conf,
2948 						changes);
2949 }
2950 
2951 bool iwl_mvm_start_ap_ibss_common(struct ieee80211_hw *hw,
2952 				  struct ieee80211_vif *vif,
2953 				  int *ret)
2954 {
2955 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2956 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2957 	int i;
2958 
2959 	lockdep_assert_held(&mvm->mutex);
2960 
2961 	mvmvif->ap_assoc_sta_count = 0;
2962 
2963 	/* must be set before quota calculations */
2964 	mvmvif->ap_ibss_active = true;
2965 
2966 	/* send all the early keys to the device now */
2967 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2968 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2969 
2970 		if (!key)
2971 			continue;
2972 
2973 		mvmvif->ap_early_keys[i] = NULL;
2974 
2975 		*ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2976 		if (*ret)
2977 			return true;
2978 	}
2979 
2980 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2981 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2982 					    LOW_LATENCY_VIF_TYPE);
2983 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2984 	}
2985 
2986 	/* power updated needs to be done before quotas */
2987 	iwl_mvm_power_update_mac(mvm);
2988 
2989 	return false;
2990 }
2991 
2992 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2993 				 struct ieee80211_vif *vif,
2994 				 struct ieee80211_bss_conf *link_conf)
2995 {
2996 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2997 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2998 	int ret;
2999 
3000 	mutex_lock(&mvm->mutex);
3001 
3002 	/*
3003 	 * Re-calculate the tsf id, as the leader-follower relations depend on
3004 	 * the beacon interval, which was not known when the AP interface
3005 	 * was added.
3006 	 */
3007 	if (vif->type == NL80211_IFTYPE_AP)
3008 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
3009 
3010 	/* For older devices need to send beacon template before adding mac
3011 	 * context. For the newer, the beacon is a resource that belongs to a
3012 	 * MAC, so need to send beacon template after adding the mac.
3013 	 */
3014 	if (mvm->trans->trans_cfg->device_family > IWL_DEVICE_FAMILY_22000) {
3015 		/* Add the mac context */
3016 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3017 		if (ret)
3018 			goto out_unlock;
3019 
3020 		/* Send the beacon template */
3021 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3022 		if (ret)
3023 			goto out_unlock;
3024 	} else {
3025 		/* Send the beacon template */
3026 		ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, link_conf);
3027 		if (ret)
3028 			goto out_unlock;
3029 
3030 		/* Add the mac context */
3031 		ret = iwl_mvm_mac_ctxt_add(mvm, vif);
3032 		if (ret)
3033 			goto out_unlock;
3034 	}
3035 
3036 	/* Perform the binding */
3037 	ret = iwl_mvm_binding_add_vif(mvm, vif);
3038 	if (ret)
3039 		goto out_remove;
3040 
3041 	/*
3042 	 * This is not very nice, but the simplest:
3043 	 * For older FWs adding the mcast sta before the bcast station may
3044 	 * cause assert 0x2b00.
3045 	 * This is fixed in later FW so make the order of removal depend on
3046 	 * the TLV
3047 	 */
3048 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
3049 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3050 		if (ret)
3051 			goto out_unbind;
3052 		/*
3053 		 * Send the bcast station. At this stage the TBTT and DTIM time
3054 		 * events are added and applied to the scheduler
3055 		 */
3056 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3057 		if (ret) {
3058 			iwl_mvm_rm_mcast_sta(mvm, vif);
3059 			goto out_unbind;
3060 		}
3061 	} else {
3062 		/*
3063 		 * Send the bcast station. At this stage the TBTT and DTIM time
3064 		 * events are added and applied to the scheduler
3065 		 */
3066 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
3067 		if (ret)
3068 			goto out_unbind;
3069 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
3070 		if (ret) {
3071 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
3072 			goto out_unbind;
3073 		}
3074 	}
3075 
3076 	if (iwl_mvm_start_ap_ibss_common(hw, vif, &ret))
3077 		goto out_failed;
3078 
3079 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
3080 	if (ret)
3081 		goto out_failed;
3082 
3083 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3084 	if (vif->p2p && mvm->p2p_device_vif)
3085 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3086 
3087 	iwl_mvm_bt_coex_vif_change(mvm);
3088 
3089 	/* we don't support TDLS during DCM */
3090 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3091 		iwl_mvm_teardown_tdls_peers(mvm);
3092 
3093 	iwl_mvm_ftm_restart_responder(mvm, vif, &vif->bss_conf);
3094 
3095 	goto out_unlock;
3096 
3097 out_failed:
3098 	iwl_mvm_power_update_mac(mvm);
3099 	mvmvif->ap_ibss_active = false;
3100 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3101 	iwl_mvm_rm_mcast_sta(mvm, vif);
3102 out_unbind:
3103 	iwl_mvm_binding_remove_vif(mvm, vif);
3104 out_remove:
3105 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3106 out_unlock:
3107 	mutex_unlock(&mvm->mutex);
3108 	return ret;
3109 }
3110 
3111 static int iwl_mvm_start_ap(struct ieee80211_hw *hw,
3112 			    struct ieee80211_vif *vif,
3113 			    struct ieee80211_bss_conf *link_conf)
3114 {
3115 	return iwl_mvm_start_ap_ibss(hw, vif, link_conf);
3116 }
3117 
3118 static int iwl_mvm_start_ibss(struct ieee80211_hw *hw,
3119 			      struct ieee80211_vif *vif)
3120 {
3121 	return iwl_mvm_start_ap_ibss(hw, vif, &vif->bss_conf);
3122 }
3123 
3124 /* Common part for MLD and non-MLD ops */
3125 void iwl_mvm_stop_ap_ibss_common(struct iwl_mvm *mvm,
3126 				 struct ieee80211_vif *vif)
3127 {
3128 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3129 
3130 	lockdep_assert_held(&mvm->mutex);
3131 
3132 	iwl_mvm_prepare_mac_removal(mvm, vif);
3133 
3134 	/* Handle AP stop while in CSA */
3135 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
3136 		iwl_mvm_remove_time_event(mvm, mvmvif,
3137 					  &mvmvif->time_event_data);
3138 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
3139 		mvmvif->csa_countdown = false;
3140 	}
3141 
3142 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
3143 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
3144 		mvm->csa_tx_block_bcn_timeout = 0;
3145 	}
3146 
3147 	mvmvif->ap_ibss_active = false;
3148 	mvm->ap_last_beacon_gp2 = 0;
3149 
3150 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
3151 		iwl_mvm_vif_set_low_latency(mvmvif, false,
3152 					    LOW_LATENCY_VIF_TYPE);
3153 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
3154 	}
3155 
3156 	iwl_mvm_bt_coex_vif_change(mvm);
3157 }
3158 
3159 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
3160 				 struct ieee80211_vif *vif,
3161 				 struct ieee80211_bss_conf *link_conf)
3162 {
3163 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3164 
3165 	mutex_lock(&mvm->mutex);
3166 
3167 	iwl_mvm_stop_ap_ibss_common(mvm, vif);
3168 
3169 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
3170 	if (vif->p2p && mvm->p2p_device_vif)
3171 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
3172 
3173 	iwl_mvm_update_quotas(mvm, false, NULL);
3174 
3175 	iwl_mvm_ftm_responder_clear(mvm, vif);
3176 
3177 	/*
3178 	 * This is not very nice, but the simplest:
3179 	 * For older FWs removing the mcast sta before the bcast station may
3180 	 * cause assert 0x2b00.
3181 	 * This is fixed in later FW (which will stop beaconing when removing
3182 	 * bcast station).
3183 	 * So make the order of removal depend on the TLV
3184 	 */
3185 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3186 		iwl_mvm_rm_mcast_sta(mvm, vif);
3187 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
3188 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
3189 		iwl_mvm_rm_mcast_sta(mvm, vif);
3190 	iwl_mvm_binding_remove_vif(mvm, vif);
3191 
3192 	iwl_mvm_power_update_mac(mvm);
3193 
3194 	iwl_mvm_mac_ctxt_remove(mvm, vif);
3195 
3196 	mutex_unlock(&mvm->mutex);
3197 }
3198 
3199 static void iwl_mvm_stop_ap(struct ieee80211_hw *hw,
3200 			    struct ieee80211_vif *vif,
3201 			    struct ieee80211_bss_conf *link_conf)
3202 {
3203 	iwl_mvm_stop_ap_ibss(hw, vif, link_conf);
3204 }
3205 
3206 static void iwl_mvm_stop_ibss(struct ieee80211_hw *hw,
3207 			      struct ieee80211_vif *vif)
3208 {
3209 	iwl_mvm_stop_ap_ibss(hw, vif, &vif->bss_conf);
3210 }
3211 
3212 static void
3213 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
3214 				 struct ieee80211_vif *vif,
3215 				 struct ieee80211_bss_conf *bss_conf,
3216 				 u64 changes)
3217 {
3218 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3219 
3220 	/* Changes will be applied when the AP/IBSS is started */
3221 	if (!mvmvif->ap_ibss_active)
3222 		return;
3223 
3224 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
3225 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
3226 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
3227 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
3228 
3229 	/* Need to send a new beacon template to the FW */
3230 	if (changes & BSS_CHANGED_BEACON &&
3231 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif, &vif->bss_conf))
3232 		IWL_WARN(mvm, "Failed updating beacon data\n");
3233 
3234 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
3235 		int ret = iwl_mvm_ftm_start_responder(mvm, vif, &vif->bss_conf);
3236 
3237 		if (ret)
3238 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
3239 				 ret);
3240 	}
3241 
3242 }
3243 
3244 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
3245 				     struct ieee80211_vif *vif,
3246 				     struct ieee80211_bss_conf *bss_conf,
3247 				     u64 changes)
3248 {
3249 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3250 
3251 	mutex_lock(&mvm->mutex);
3252 
3253 	if (changes & BSS_CHANGED_IDLE && !vif->cfg.idle)
3254 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
3255 
3256 	switch (vif->type) {
3257 	case NL80211_IFTYPE_STATION:
3258 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
3259 		break;
3260 	case NL80211_IFTYPE_AP:
3261 	case NL80211_IFTYPE_ADHOC:
3262 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
3263 		break;
3264 	case NL80211_IFTYPE_MONITOR:
3265 		if (changes & BSS_CHANGED_MU_GROUPS)
3266 			iwl_mvm_update_mu_groups(mvm, vif);
3267 		break;
3268 	default:
3269 		/* shouldn't happen */
3270 		WARN_ON_ONCE(1);
3271 	}
3272 
3273 	if (changes & BSS_CHANGED_TXPOWER) {
3274 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
3275 				bss_conf->txpower);
3276 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
3277 	}
3278 
3279 	mutex_unlock(&mvm->mutex);
3280 }
3281 
3282 int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3283 			struct ieee80211_scan_request *hw_req)
3284 {
3285 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3286 	int ret;
3287 
3288 	if (hw_req->req.n_channels == 0 ||
3289 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
3290 		return -EINVAL;
3291 
3292 	mutex_lock(&mvm->mutex);
3293 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
3294 	mutex_unlock(&mvm->mutex);
3295 
3296 	return ret;
3297 }
3298 
3299 void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
3300 				struct ieee80211_vif *vif)
3301 {
3302 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3303 
3304 	mutex_lock(&mvm->mutex);
3305 
3306 	/* Due to a race condition, it's possible that mac80211 asks
3307 	 * us to stop a hw_scan when it's already stopped.  This can
3308 	 * happen, for instance, if we stopped the scan ourselves,
3309 	 * called ieee80211_scan_completed() and the userspace called
3310 	 * cancel scan scan before ieee80211_scan_work() could run.
3311 	 * To handle that, simply return if the scan is not running.
3312 	*/
3313 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
3314 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
3315 
3316 	mutex_unlock(&mvm->mutex);
3317 }
3318 
3319 void
3320 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
3321 				  struct ieee80211_sta *sta, u16 tids,
3322 				  int num_frames,
3323 				  enum ieee80211_frame_release_type reason,
3324 				  bool more_data)
3325 {
3326 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3327 
3328 	/* Called when we need to transmit (a) frame(s) from mac80211 */
3329 
3330 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3331 					  tids, more_data, false);
3332 }
3333 
3334 void
3335 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
3336 				    struct ieee80211_sta *sta, u16 tids,
3337 				    int num_frames,
3338 				    enum ieee80211_frame_release_type reason,
3339 				    bool more_data)
3340 {
3341 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3342 
3343 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
3344 
3345 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
3346 					  tids, more_data, true);
3347 }
3348 
3349 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
3350 				     enum sta_notify_cmd cmd,
3351 				     struct ieee80211_sta *sta)
3352 {
3353 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3354 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
3355 	unsigned long txqs = 0, tids = 0;
3356 	int tid;
3357 
3358 	/*
3359 	 * If we have TVQM then we get too high queue numbers - luckily
3360 	 * we really shouldn't get here with that because such hardware
3361 	 * should have firmware supporting buffer station offload.
3362 	 */
3363 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
3364 		return;
3365 
3366 	spin_lock_bh(&mvmsta->lock);
3367 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
3368 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
3369 
3370 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
3371 			continue;
3372 
3373 		__set_bit(tid_data->txq_id, &txqs);
3374 
3375 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
3376 			continue;
3377 
3378 		__set_bit(tid, &tids);
3379 	}
3380 
3381 	switch (cmd) {
3382 	case STA_NOTIFY_SLEEP:
3383 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
3384 			ieee80211_sta_set_buffered(sta, tid, true);
3385 
3386 		if (txqs)
3387 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
3388 		/*
3389 		 * The fw updates the STA to be asleep. Tx packets on the Tx
3390 		 * queues to this station will not be transmitted. The fw will
3391 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
3392 		 */
3393 		break;
3394 	case STA_NOTIFY_AWAKE:
3395 		if (WARN_ON(mvmsta->deflink.sta_id == IWL_MVM_INVALID_STA))
3396 			break;
3397 
3398 		if (txqs)
3399 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
3400 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
3401 		break;
3402 	default:
3403 		break;
3404 	}
3405 	spin_unlock_bh(&mvmsta->lock);
3406 }
3407 
3408 void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
3409 			    enum sta_notify_cmd cmd, struct ieee80211_sta *sta)
3410 {
3411 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
3412 }
3413 
3414 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
3415 {
3416 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
3417 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
3418 	struct ieee80211_sta *sta;
3419 	struct iwl_mvm_sta *mvmsta;
3420 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
3421 
3422 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
3423 		return;
3424 
3425 	rcu_read_lock();
3426 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
3427 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
3428 		rcu_read_unlock();
3429 		return;
3430 	}
3431 
3432 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
3433 
3434 	if (!mvmsta->vif ||
3435 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
3436 		rcu_read_unlock();
3437 		return;
3438 	}
3439 
3440 	if (mvmsta->sleeping != sleeping) {
3441 		mvmsta->sleeping = sleeping;
3442 		__iwl_mvm_mac_sta_notify(mvm->hw,
3443 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
3444 			sta);
3445 		ieee80211_sta_ps_transition(sta, sleeping);
3446 	}
3447 
3448 	if (sleeping) {
3449 		switch (notif->type) {
3450 		case IWL_MVM_PM_EVENT_AWAKE:
3451 		case IWL_MVM_PM_EVENT_ASLEEP:
3452 			break;
3453 		case IWL_MVM_PM_EVENT_UAPSD:
3454 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
3455 			break;
3456 		case IWL_MVM_PM_EVENT_PS_POLL:
3457 			ieee80211_sta_pspoll(sta);
3458 			break;
3459 		default:
3460 			break;
3461 		}
3462 	}
3463 
3464 	rcu_read_unlock();
3465 }
3466 
3467 void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
3468 				struct ieee80211_vif *vif,
3469 				struct ieee80211_sta *sta)
3470 {
3471 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3472 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3473 	unsigned int link_id;
3474 
3475 	/*
3476 	 * This is called before mac80211 does RCU synchronisation,
3477 	 * so here we already invalidate our internal RCU-protected
3478 	 * station pointer. The rest of the code will thus no longer
3479 	 * be able to find the station this way, and we don't rely
3480 	 * on further RCU synchronisation after the sta_state()
3481 	 * callback deleted the station.
3482 	 * Since there's mvm->mutex here, no need to have RCU lock for
3483 	 * mvm_sta->link access.
3484 	 */
3485 	mutex_lock(&mvm->mutex);
3486 	for (link_id = 0; link_id < ARRAY_SIZE(mvm_sta->link); link_id++) {
3487 		struct iwl_mvm_link_sta *link_sta;
3488 		u32 sta_id;
3489 
3490 		if (!mvm_sta->link[link_id])
3491 			continue;
3492 
3493 		link_sta = rcu_dereference_protected(mvm_sta->link[link_id],
3494 						     lockdep_is_held(&mvm->mutex));
3495 		sta_id = link_sta->sta_id;
3496 		if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[sta_id])) {
3497 			RCU_INIT_POINTER(mvm->fw_id_to_mac_id[sta_id],
3498 					 ERR_PTR(-ENOENT));
3499 			RCU_INIT_POINTER(mvm->fw_id_to_link_sta[sta_id], NULL);
3500 		}
3501 	}
3502 	mutex_unlock(&mvm->mutex);
3503 }
3504 
3505 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
3506 				const u8 *bssid)
3507 {
3508 	int i;
3509 
3510 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3511 		struct iwl_mvm_tcm_mac *mdata;
3512 
3513 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
3514 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
3515 		mdata->opened_rx_ba_sessions = false;
3516 	}
3517 
3518 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
3519 		return;
3520 
3521 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
3522 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3523 		return;
3524 	}
3525 
3526 	if (!vif->p2p &&
3527 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3528 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3529 		return;
3530 	}
3531 
3532 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3533 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3534 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3535 			return;
3536 		}
3537 	}
3538 
3539 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3540 }
3541 
3542 static void
3543 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3544 			   struct ieee80211_vif *vif, u8 *peer_addr,
3545 			   enum nl80211_tdls_operation action)
3546 {
3547 	struct iwl_fw_dbg_trigger_tlv *trig;
3548 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3549 
3550 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3551 				     FW_DBG_TRIGGER_TDLS);
3552 	if (!trig)
3553 		return;
3554 
3555 	tdls_trig = (void *)trig->data;
3556 
3557 	if (!(tdls_trig->action_bitmap & BIT(action)))
3558 		return;
3559 
3560 	if (tdls_trig->peer_mode &&
3561 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3562 		return;
3563 
3564 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3565 				"TDLS event occurred, peer %pM, action %d",
3566 				peer_addr, action);
3567 }
3568 
3569 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3570 	bool tolerated;
3571 };
3572 
3573 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3574 						    struct cfg80211_bss *bss,
3575 						    void *_data)
3576 {
3577 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3578 	const struct cfg80211_bss_ies *ies;
3579 	const struct element *elem;
3580 
3581 	rcu_read_lock();
3582 	ies = rcu_dereference(bss->ies);
3583 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3584 				  ies->len);
3585 
3586 	if (!elem || elem->datalen < 10 ||
3587 	    !(elem->data[10] &
3588 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3589 		data->tolerated = false;
3590 	}
3591 	rcu_read_unlock();
3592 }
3593 
3594 static void
3595 iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3596 				   struct ieee80211_vif *vif,
3597 				   unsigned int link_id,
3598 				   struct ieee80211_bss_conf *link_conf)
3599 {
3600 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3601 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3602 		.tolerated = true,
3603 	};
3604 
3605 	if (WARN_ON_ONCE(!link_conf->chanreq.oper.chan ||
3606 			 !mvmvif->link[link_id]))
3607 		return;
3608 
3609 	if (!(link_conf->chanreq.oper.chan->flags & IEEE80211_CHAN_RADAR)) {
3610 		mvmvif->link[link_id]->he_ru_2mhz_block = false;
3611 		return;
3612 	}
3613 
3614 	cfg80211_bss_iter(hw->wiphy, &link_conf->chanreq.oper,
3615 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3616 			  &iter_data);
3617 
3618 	/*
3619 	 * If there is at least one AP on radar channel that cannot
3620 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3621 	 */
3622 	mvmvif->link[link_id]->he_ru_2mhz_block = !iter_data.tolerated;
3623 }
3624 
3625 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3626 					       struct ieee80211_vif *vif)
3627 {
3628 	struct ieee80211_supported_band *sband;
3629 	const struct ieee80211_sta_he_cap *he_cap;
3630 
3631 	if (vif->type != NL80211_IFTYPE_STATION)
3632 		return;
3633 
3634 	if (!mvm->cca_40mhz_workaround)
3635 		return;
3636 
3637 	/* decrement and check that we reached zero */
3638 	mvm->cca_40mhz_workaround--;
3639 	if (mvm->cca_40mhz_workaround)
3640 		return;
3641 
3642 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3643 
3644 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3645 
3646 	he_cap = ieee80211_get_he_iftype_cap_vif(sband, vif);
3647 
3648 	if (he_cap) {
3649 		/* we know that ours is writable */
3650 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3651 
3652 		he->he_cap_elem.phy_cap_info[0] |=
3653 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3654 	}
3655 }
3656 
3657 static void iwl_mvm_mei_host_associated(struct iwl_mvm *mvm,
3658 					struct ieee80211_vif *vif,
3659 					struct iwl_mvm_sta *mvm_sta)
3660 {
3661 #if IS_ENABLED(CONFIG_IWLMEI)
3662 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3663 	struct iwl_mei_conn_info conn_info = {
3664 		.ssid_len = vif->cfg.ssid_len,
3665 	};
3666 
3667 	if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3668 		return;
3669 
3670 	if (!mvm->mei_registered)
3671 		return;
3672 
3673 	/* FIXME: MEI needs to be updated for MLO */
3674 	if (!vif->bss_conf.chanreq.oper.chan)
3675 		return;
3676 
3677 	conn_info.channel = vif->bss_conf.chanreq.oper.chan->hw_value;
3678 
3679 	switch (mvm_sta->pairwise_cipher) {
3680 	case WLAN_CIPHER_SUITE_TKIP:
3681 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_TKIP;
3682 		break;
3683 	case WLAN_CIPHER_SUITE_CCMP:
3684 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_CCMP;
3685 		break;
3686 	case WLAN_CIPHER_SUITE_GCMP:
3687 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP;
3688 		break;
3689 	case WLAN_CIPHER_SUITE_GCMP_256:
3690 		conn_info.pairwise_cipher = IWL_MEI_CIPHER_GCMP_256;
3691 		break;
3692 	case 0:
3693 		/* open profile */
3694 		break;
3695 	default:
3696 		/* cipher not supported, don't send anything to iwlmei */
3697 		return;
3698 	}
3699 
3700 	switch (mvmvif->rekey_data.akm) {
3701 	case WLAN_AKM_SUITE_SAE & 0xff:
3702 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_SAE;
3703 		break;
3704 	case WLAN_AKM_SUITE_PSK & 0xff:
3705 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA_PSK;
3706 		break;
3707 	case WLAN_AKM_SUITE_8021X & 0xff:
3708 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_RSNA;
3709 		break;
3710 	case 0:
3711 		/* open profile */
3712 		conn_info.auth_mode = IWL_MEI_AKM_AUTH_OPEN;
3713 		break;
3714 	default:
3715 		/* auth method / AKM not supported */
3716 		/* TODO: All the FT vesions of these? */
3717 		return;
3718 	}
3719 
3720 	memcpy(conn_info.ssid, vif->cfg.ssid, vif->cfg.ssid_len);
3721 	memcpy(conn_info.bssid,  vif->bss_conf.bssid, ETH_ALEN);
3722 
3723 	/* TODO: add support for collocated AP data */
3724 	iwl_mei_host_associated(&conn_info, NULL);
3725 #endif
3726 }
3727 
3728 static int iwl_mvm_mac_ctxt_changed_wrapper(struct iwl_mvm *mvm,
3729 					    struct ieee80211_vif *vif,
3730 					    bool force_assoc_off)
3731 {
3732 	return iwl_mvm_mac_ctxt_changed(mvm, vif, force_assoc_off, NULL);
3733 }
3734 
3735 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3736 				 struct ieee80211_vif *vif,
3737 				 struct ieee80211_sta *sta,
3738 				 enum ieee80211_sta_state old_state,
3739 				 enum ieee80211_sta_state new_state)
3740 {
3741 	static const struct iwl_mvm_sta_state_ops callbacks = {
3742 		.add_sta = iwl_mvm_add_sta,
3743 		.update_sta = iwl_mvm_update_sta,
3744 		.rm_sta = iwl_mvm_rm_sta,
3745 		.mac_ctxt_changed = iwl_mvm_mac_ctxt_changed_wrapper,
3746 	};
3747 
3748 	return iwl_mvm_mac_sta_state_common(hw, vif, sta, old_state, new_state,
3749 					    &callbacks);
3750 }
3751 
3752 /* FIXME: temporary making two assumptions in all sta handling functions:
3753  *	(1) when setting sta state, the link exists and protected
3754  *	(2) if a link is valid in sta then it's valid in vif (can
3755  *	use same index in the link array)
3756  */
3757 static void iwl_mvm_rs_rate_init_all_links(struct iwl_mvm *mvm,
3758 					   struct ieee80211_vif *vif,
3759 					   struct ieee80211_sta *sta)
3760 {
3761 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3762 	unsigned int link_id;
3763 
3764 	for_each_mvm_vif_valid_link(mvmvif, link_id) {
3765 		struct ieee80211_bss_conf *conf =
3766 			link_conf_dereference_check(vif, link_id);
3767 		struct ieee80211_link_sta *link_sta =
3768 			link_sta_dereference_check(sta, link_id);
3769 
3770 		if (!conf || !link_sta || !mvmvif->link[link_id]->phy_ctxt)
3771 			continue;
3772 
3773 		iwl_mvm_rs_rate_init(mvm, vif, sta, conf, link_sta,
3774 				     mvmvif->link[link_id]->phy_ctxt->channel->band);
3775 	}
3776 }
3777 
3778 #define IWL_MVM_MIN_BEACON_INTERVAL_TU 16
3779 
3780 static bool iwl_mvm_vif_conf_from_sta(struct iwl_mvm *mvm,
3781 				      struct ieee80211_vif *vif,
3782 				      struct ieee80211_sta *sta)
3783 {
3784 	struct ieee80211_link_sta *link_sta;
3785 	unsigned int link_id;
3786 
3787 	/* Beacon interval check - firmware will crash if the beacon
3788 	 * interval is less than 16. We can't avoid connecting at all,
3789 	 * so refuse the station state change, this will cause mac80211
3790 	 * to abandon attempts to connect to this AP, and eventually
3791 	 * wpa_s will blocklist the AP...
3792 	 */
3793 
3794 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
3795 		struct ieee80211_bss_conf *link_conf =
3796 			link_conf_dereference_protected(vif, link_id);
3797 
3798 		if (!link_conf)
3799 			continue;
3800 
3801 		if (link_conf->beacon_int < IWL_MVM_MIN_BEACON_INTERVAL_TU) {
3802 			IWL_ERR(mvm,
3803 				"Beacon interval %d for AP %pM is too small\n",
3804 				link_conf->beacon_int, link_sta->addr);
3805 			return false;
3806 		}
3807 
3808 		link_conf->he_support = link_sta->he_cap.has_he;
3809 	}
3810 
3811 	return true;
3812 }
3813 
3814 static void iwl_mvm_vif_set_he_support(struct ieee80211_hw *hw,
3815 				       struct ieee80211_vif *vif,
3816 				       struct ieee80211_sta *sta,
3817 				       bool is_sta)
3818 {
3819 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3820 	struct ieee80211_link_sta *link_sta;
3821 	unsigned int link_id;
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 (!link_conf || !mvmvif->link[link_id])
3828 			continue;
3829 
3830 		link_conf->he_support = link_sta->he_cap.has_he;
3831 
3832 		if (is_sta) {
3833 			mvmvif->link[link_id]->he_ru_2mhz_block = false;
3834 			if (link_sta->he_cap.has_he)
3835 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif,
3836 								   link_id,
3837 								   link_conf);
3838 		}
3839 	}
3840 }
3841 
3842 static int
3843 iwl_mvm_sta_state_notexist_to_none(struct iwl_mvm *mvm,
3844 				   struct ieee80211_vif *vif,
3845 				   struct ieee80211_sta *sta,
3846 				   const struct iwl_mvm_sta_state_ops *callbacks)
3847 {
3848 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3849 	struct ieee80211_link_sta *link_sta;
3850 	unsigned int i;
3851 	int ret;
3852 
3853 	lockdep_assert_held(&mvm->mutex);
3854 
3855 	if (vif->type == NL80211_IFTYPE_STATION &&
3856 	    !iwl_mvm_vif_conf_from_sta(mvm, vif, sta))
3857 		return -EINVAL;
3858 
3859 	if (sta->tdls &&
3860 	    (vif->p2p ||
3861 	     iwl_mvm_tdls_sta_count(mvm, NULL) == IWL_MVM_TDLS_STA_COUNT ||
3862 	     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3863 		IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3864 		return -EBUSY;
3865 	}
3866 
3867 	ret = callbacks->add_sta(mvm, vif, sta);
3868 	if (sta->tdls && ret == 0) {
3869 		iwl_mvm_recalc_tdls_state(mvm, vif, true);
3870 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3871 					   NL80211_TDLS_SETUP);
3872 	}
3873 
3874 	if (ret)
3875 		return ret;
3876 
3877 	for_each_sta_active_link(vif, sta, link_sta, i)
3878 		link_sta->agg.max_rc_amsdu_len = 1;
3879 
3880 	ieee80211_sta_recalc_aggregates(sta);
3881 
3882 	if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
3883 		mvmvif->ap_sta = sta;
3884 
3885 	/*
3886 	 * Initialize the rates here already - this really tells
3887 	 * the firmware only what the supported legacy rates are
3888 	 * (may be) since it's initialized already from what the
3889 	 * AP advertised in the beacon/probe response. This will
3890 	 * allow the firmware to send auth/assoc frames with one
3891 	 * of the supported rates already, rather than having to
3892 	 * use a mandatory rate.
3893 	 * If we're the AP, we'll just assume mandatory rates at
3894 	 * this point, but we know nothing about the STA anyway.
3895 	 */
3896 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3897 
3898 	return 0;
3899 }
3900 
3901 static int
3902 iwl_mvm_sta_state_auth_to_assoc(struct ieee80211_hw *hw,
3903 				struct iwl_mvm *mvm,
3904 				struct ieee80211_vif *vif,
3905 				struct ieee80211_sta *sta,
3906 				const struct iwl_mvm_sta_state_ops *callbacks)
3907 {
3908 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3909 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3910 	struct ieee80211_link_sta *link_sta;
3911 	unsigned int link_id;
3912 
3913 	lockdep_assert_held(&mvm->mutex);
3914 
3915 	if (vif->type == NL80211_IFTYPE_AP) {
3916 		iwl_mvm_vif_set_he_support(hw, vif, sta, false);
3917 		mvmvif->ap_assoc_sta_count++;
3918 		callbacks->mac_ctxt_changed(mvm, vif, false);
3919 
3920 		/* since the below is not for MLD API, it's ok to use
3921 		 * the default bss_conf
3922 		 */
3923 		if (!mvm->mld_api_is_used &&
3924 		    (vif->bss_conf.he_support &&
3925 		     !iwlwifi_mod_params.disable_11ax))
3926 			iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->deflink.sta_id);
3927 	} else if (vif->type == NL80211_IFTYPE_STATION) {
3928 		iwl_mvm_vif_set_he_support(hw, vif, sta, true);
3929 
3930 		callbacks->mac_ctxt_changed(mvm, vif, false);
3931 
3932 		if (!mvm->mld_api_is_used)
3933 			goto out;
3934 
3935 		for_each_sta_active_link(vif, sta, link_sta, link_id) {
3936 			struct ieee80211_bss_conf *link_conf =
3937 				link_conf_dereference_protected(vif, link_id);
3938 
3939 			if (WARN_ON(!link_conf))
3940 				return -EINVAL;
3941 			if (!mvmvif->link[link_id])
3942 				continue;
3943 
3944 			iwl_mvm_link_changed(mvm, vif, link_conf,
3945 					     LINK_CONTEXT_MODIFY_ALL &
3946 					     ~LINK_CONTEXT_MODIFY_ACTIVE,
3947 					     true);
3948 		}
3949 	}
3950 
3951 out:
3952 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
3953 
3954 	return callbacks->update_sta(mvm, vif, sta);
3955 }
3956 
3957 static int
3958 iwl_mvm_sta_state_assoc_to_authorized(struct iwl_mvm *mvm,
3959 				      struct ieee80211_vif *vif,
3960 				      struct ieee80211_sta *sta,
3961 				      const struct iwl_mvm_sta_state_ops *callbacks)
3962 {
3963 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3964 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3965 
3966 	lockdep_assert_held(&mvm->mutex);
3967 
3968 	/* we don't support TDLS during DCM */
3969 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
3970 		iwl_mvm_teardown_tdls_peers(mvm);
3971 
3972 	if (sta->tdls) {
3973 		iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3974 					   NL80211_TDLS_ENABLE_LINK);
3975 	} else {
3976 		/* enable beacon filtering */
3977 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif));
3978 
3979 		mvmvif->authorized = 1;
3980 
3981 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
3982 			mvmvif->link_selection_res = vif->active_links;
3983 			mvmvif->link_selection_primary =
3984 				vif->active_links ? __ffs(vif->active_links) : 0;
3985 		}
3986 
3987 		callbacks->mac_ctxt_changed(mvm, vif, false);
3988 		iwl_mvm_mei_host_associated(mvm, vif, mvm_sta);
3989 
3990 		memset(&mvmvif->last_esr_exit, 0,
3991 		       sizeof(mvmvif->last_esr_exit));
3992 
3993 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_TPT, 0);
3994 
3995 		/* Block until FW notif will arrive */
3996 		iwl_mvm_block_esr(mvm, vif, IWL_MVM_ESR_BLOCKED_FW, 0);
3997 
3998 		/* when client is authorized (AP station marked as such),
3999 		 * try to enable the best link(s).
4000 		 */
4001 		if (vif->type == NL80211_IFTYPE_STATION &&
4002 		    !test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4003 			iwl_mvm_select_links(mvm, vif);
4004 	}
4005 
4006 	mvm_sta->authorized = true;
4007 
4008 	/* MFP is set by default before the station is authorized.
4009 	 * Clear it here in case it's not used.
4010 	 */
4011 	if (!sta->mfp) {
4012 		int ret = callbacks->update_sta(mvm, vif, sta);
4013 
4014 		if (ret)
4015 			return ret;
4016 	}
4017 
4018 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4019 
4020 	return 0;
4021 }
4022 
4023 static int
4024 iwl_mvm_sta_state_authorized_to_assoc(struct iwl_mvm *mvm,
4025 				      struct ieee80211_vif *vif,
4026 				      struct ieee80211_sta *sta,
4027 				      const struct iwl_mvm_sta_state_ops *callbacks)
4028 {
4029 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4030 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
4031 
4032 	lockdep_assert_held(&mvm->mutex);
4033 
4034 	mvmsta->authorized = false;
4035 
4036 	/* once we move into assoc state, need to update rate scale to
4037 	 * disable using wide bandwidth
4038 	 */
4039 	iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4040 
4041 	if (!sta->tdls) {
4042 		/* Set this but don't call iwl_mvm_mac_ctxt_changed()
4043 		 * yet to avoid sending high prio again for a little
4044 		 * time.
4045 		 */
4046 		mvmvif->authorized = 0;
4047 		mvmvif->link_selection_res = 0;
4048 
4049 		/* disable beacon filtering */
4050 		iwl_mvm_disable_beacon_filter(mvm, vif);
4051 
4052 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4053 					  &mvmvif->prevent_esr_done_wk);
4054 
4055 		wiphy_delayed_work_cancel(mvm->hw->wiphy,
4056 					  &mvmvif->mlo_int_scan_wk);
4057 
4058 		wiphy_work_cancel(mvm->hw->wiphy, &mvmvif->unblock_esr_tpt_wk);
4059 
4060 		/* No need for the periodic statistics anymore */
4061 		if (ieee80211_vif_is_mld(vif) && mvmvif->esr_active)
4062 			iwl_mvm_request_periodic_system_statistics(mvm, false);
4063 	}
4064 
4065 	return 0;
4066 }
4067 
4068 /* Common part for MLD and non-MLD modes */
4069 int iwl_mvm_mac_sta_state_common(struct ieee80211_hw *hw,
4070 				 struct ieee80211_vif *vif,
4071 				 struct ieee80211_sta *sta,
4072 				 enum ieee80211_sta_state old_state,
4073 				 enum ieee80211_sta_state new_state,
4074 				 const struct iwl_mvm_sta_state_ops *callbacks)
4075 {
4076 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4077 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4078 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4079 	struct ieee80211_link_sta *link_sta;
4080 	unsigned int link_id;
4081 	int ret;
4082 
4083 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
4084 			   sta->addr, old_state, new_state);
4085 
4086 	/*
4087 	 * If we are in a STA removal flow and in DQA mode:
4088 	 *
4089 	 * This is after the sync_rcu part, so the queues have already been
4090 	 * flushed. No more TXs on their way in mac80211's path, and no more in
4091 	 * the queues.
4092 	 * Also, we won't be getting any new TX frames for this station.
4093 	 * What we might have are deferred TX frames that need to be taken care
4094 	 * of.
4095 	 *
4096 	 * Drop any still-queued deferred-frame before removing the STA, and
4097 	 * make sure the worker is no longer handling frames for this STA.
4098 	 */
4099 	if (old_state == IEEE80211_STA_NONE &&
4100 	    new_state == IEEE80211_STA_NOTEXIST) {
4101 		flush_work(&mvm->add_stream_wk);
4102 
4103 		/*
4104 		 * No need to make sure deferred TX indication is off since the
4105 		 * worker will already remove it if it was on
4106 		 */
4107 
4108 		/*
4109 		 * Additionally, reset the 40 MHz capability if we disconnected
4110 		 * from the AP now.
4111 		 */
4112 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
4113 
4114 		/* Also free dup data just in case any assertions below fail */
4115 		kfree(mvm_sta->dup_data);
4116 	}
4117 
4118 	mutex_lock(&mvm->mutex);
4119 
4120 	/* this would be a mac80211 bug ... but don't crash, unless we had a
4121 	 * firmware crash while we were activating a link, in which case it is
4122 	 * legit to have phy_ctxt = NULL. Don't bother not to WARN if we are in
4123 	 * recovery flow since we spit tons of error messages anyway.
4124 	 */
4125 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
4126 		if (WARN_ON_ONCE(!mvmvif->link[link_id] ||
4127 				 !mvmvif->link[link_id]->phy_ctxt)) {
4128 			mutex_unlock(&mvm->mutex);
4129 			return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4130 					&mvm->status) ? 0 : -EINVAL;
4131 		}
4132 	}
4133 
4134 	/* track whether or not the station is associated */
4135 	mvm_sta->sta_state = new_state;
4136 
4137 	if (old_state == IEEE80211_STA_NOTEXIST &&
4138 	    new_state == IEEE80211_STA_NONE) {
4139 		ret = iwl_mvm_sta_state_notexist_to_none(mvm, vif, sta,
4140 							 callbacks);
4141 		if (ret < 0)
4142 			goto out_unlock;
4143 	} else if (old_state == IEEE80211_STA_NONE &&
4144 		   new_state == IEEE80211_STA_AUTH) {
4145 		/*
4146 		 * EBS may be disabled due to previous failures reported by FW.
4147 		 * Reset EBS status here assuming environment has been changed.
4148 		 */
4149 		mvm->last_ebs_successful = true;
4150 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
4151 		ret = 0;
4152 	} else if (old_state == IEEE80211_STA_AUTH &&
4153 		   new_state == IEEE80211_STA_ASSOC) {
4154 		ret = iwl_mvm_sta_state_auth_to_assoc(hw, mvm, vif, sta,
4155 						      callbacks);
4156 	} else if (old_state == IEEE80211_STA_ASSOC &&
4157 		   new_state == IEEE80211_STA_AUTHORIZED) {
4158 		ret = iwl_mvm_sta_state_assoc_to_authorized(mvm, vif, sta,
4159 							    callbacks);
4160 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
4161 		   new_state == IEEE80211_STA_ASSOC) {
4162 		ret = iwl_mvm_sta_state_authorized_to_assoc(mvm, vif, sta,
4163 							    callbacks);
4164 	} else if (old_state == IEEE80211_STA_ASSOC &&
4165 		   new_state == IEEE80211_STA_AUTH) {
4166 		if (vif->type == NL80211_IFTYPE_AP) {
4167 			mvmvif->ap_assoc_sta_count--;
4168 			callbacks->mac_ctxt_changed(mvm, vif, false);
4169 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
4170 			iwl_mvm_stop_session_protection(mvm, vif);
4171 		ret = 0;
4172 	} else if (old_state == IEEE80211_STA_AUTH &&
4173 		   new_state == IEEE80211_STA_NONE) {
4174 		ret = 0;
4175 	} else if (old_state == IEEE80211_STA_NONE &&
4176 		   new_state == IEEE80211_STA_NOTEXIST) {
4177 		if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
4178 			iwl_mvm_stop_session_protection(mvm, vif);
4179 			mvmvif->ap_sta = NULL;
4180 		}
4181 		ret = callbacks->rm_sta(mvm, vif, sta);
4182 		if (sta->tdls) {
4183 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
4184 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
4185 						   NL80211_TDLS_DISABLE_LINK);
4186 		}
4187 
4188 		if (unlikely(ret &&
4189 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
4190 				      &mvm->status)))
4191 			ret = 0;
4192 	} else {
4193 		ret = -EIO;
4194 	}
4195  out_unlock:
4196 	mutex_unlock(&mvm->mutex);
4197 
4198 	if (sta->tdls && ret == 0) {
4199 		if (old_state == IEEE80211_STA_NOTEXIST &&
4200 		    new_state == IEEE80211_STA_NONE)
4201 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4202 		else if (old_state == IEEE80211_STA_NONE &&
4203 			 new_state == IEEE80211_STA_NOTEXIST)
4204 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
4205 	}
4206 
4207 	return ret;
4208 }
4209 
4210 int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
4211 {
4212 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4213 
4214 	mvm->rts_threshold = value;
4215 
4216 	return 0;
4217 }
4218 
4219 void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4220 			   struct ieee80211_sta *sta, u32 changed)
4221 {
4222 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4223 
4224 	if (changed & (IEEE80211_RC_BW_CHANGED |
4225 		       IEEE80211_RC_SUPP_RATES_CHANGED |
4226 		       IEEE80211_RC_NSS_CHANGED))
4227 		iwl_mvm_rs_rate_init_all_links(mvm, vif, sta);
4228 
4229 	if (vif->type == NL80211_IFTYPE_STATION &&
4230 	    changed & IEEE80211_RC_NSS_CHANGED)
4231 		iwl_mvm_sf_update(mvm, vif, false);
4232 }
4233 
4234 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
4235 			       struct ieee80211_vif *vif,
4236 			       unsigned int link_id, u16 ac,
4237 			       const struct ieee80211_tx_queue_params *params)
4238 {
4239 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4240 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4241 
4242 	mvmvif->deflink.queue_params[ac] = *params;
4243 
4244 	/*
4245 	 * No need to update right away, we'll get BSS_CHANGED_QOS
4246 	 * The exception is P2P_DEVICE interface which needs immediate update.
4247 	 */
4248 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
4249 		int ret;
4250 
4251 		mutex_lock(&mvm->mutex);
4252 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4253 		mutex_unlock(&mvm->mutex);
4254 		return ret;
4255 	}
4256 	return 0;
4257 }
4258 
4259 void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
4260 				struct ieee80211_vif *vif,
4261 				struct ieee80211_prep_tx_info *info)
4262 {
4263 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4264 
4265 	mutex_lock(&mvm->mutex);
4266 	iwl_mvm_protect_assoc(mvm, vif, info->duration, info->link_id);
4267 	mutex_unlock(&mvm->mutex);
4268 }
4269 
4270 void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
4271 				 struct ieee80211_vif *vif,
4272 				 struct ieee80211_prep_tx_info *info)
4273 {
4274 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4275 
4276 	/* for successful cases (auth/assoc), don't cancel session protection */
4277 	if (info->success)
4278 		return;
4279 
4280 	mutex_lock(&mvm->mutex);
4281 	iwl_mvm_stop_session_protection(mvm, vif);
4282 	mutex_unlock(&mvm->mutex);
4283 }
4284 
4285 int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
4286 				 struct ieee80211_vif *vif,
4287 				 struct cfg80211_sched_scan_request *req,
4288 				 struct ieee80211_scan_ies *ies)
4289 {
4290 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4291 
4292 	int ret;
4293 
4294 	mutex_lock(&mvm->mutex);
4295 
4296 	if (!vif->cfg.idle) {
4297 		ret = -EBUSY;
4298 		goto out;
4299 	}
4300 
4301 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
4302 
4303 out:
4304 	mutex_unlock(&mvm->mutex);
4305 	return ret;
4306 }
4307 
4308 int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
4309 				struct ieee80211_vif *vif)
4310 {
4311 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4312 	int ret;
4313 
4314 	mutex_lock(&mvm->mutex);
4315 
4316 	/* Due to a race condition, it's possible that mac80211 asks
4317 	 * us to stop a sched_scan when it's already stopped.  This
4318 	 * can happen, for instance, if we stopped the scan ourselves,
4319 	 * called ieee80211_sched_scan_stopped() and the userspace called
4320 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
4321 	 * could run.  To handle this, simply return if the scan is
4322 	 * not running.
4323 	*/
4324 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
4325 		mutex_unlock(&mvm->mutex);
4326 		return 0;
4327 	}
4328 
4329 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
4330 	mutex_unlock(&mvm->mutex);
4331 	iwl_mvm_wait_for_async_handlers(mvm);
4332 
4333 	return ret;
4334 }
4335 
4336 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
4337 				 enum set_key_cmd cmd,
4338 				 struct ieee80211_vif *vif,
4339 				 struct ieee80211_sta *sta,
4340 				 struct ieee80211_key_conf *key)
4341 {
4342 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4343 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4344 	struct iwl_mvm_sta *mvmsta = NULL;
4345 	struct iwl_mvm_key_pn *ptk_pn = NULL;
4346 	int keyidx = key->keyidx;
4347 	u32 sec_key_id = WIDE_ID(DATA_PATH_GROUP, SEC_KEY_CMD);
4348 	u8 sec_key_ver = iwl_fw_lookup_cmd_ver(mvm->fw, sec_key_id, 0);
4349 	int ret, i;
4350 	u8 key_offset;
4351 
4352 	if (sta)
4353 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4354 
4355 	switch (key->cipher) {
4356 	case WLAN_CIPHER_SUITE_TKIP:
4357 		if (!mvm->trans->trans_cfg->gen2) {
4358 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
4359 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4360 		} else if (vif->type == NL80211_IFTYPE_STATION) {
4361 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
4362 		} else {
4363 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
4364 			return -EOPNOTSUPP;
4365 		}
4366 		break;
4367 	case WLAN_CIPHER_SUITE_CCMP:
4368 	case WLAN_CIPHER_SUITE_GCMP:
4369 	case WLAN_CIPHER_SUITE_GCMP_256:
4370 		if (!iwl_mvm_has_new_tx_api(mvm))
4371 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
4372 		break;
4373 	case WLAN_CIPHER_SUITE_AES_CMAC:
4374 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
4375 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
4376 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
4377 		break;
4378 	case WLAN_CIPHER_SUITE_WEP40:
4379 	case WLAN_CIPHER_SUITE_WEP104:
4380 		if (vif->type == NL80211_IFTYPE_STATION)
4381 			break;
4382 		if (iwl_mvm_has_new_tx_api(mvm))
4383 			return -EOPNOTSUPP;
4384 		/* support HW crypto on TX */
4385 		return 0;
4386 	default:
4387 		return -EOPNOTSUPP;
4388 	}
4389 
4390 	switch (cmd) {
4391 	case SET_KEY:
4392 		if (vif->type == NL80211_IFTYPE_STATION &&
4393 		    (keyidx == 6 || keyidx == 7))
4394 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
4395 					   key);
4396 
4397 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
4398 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
4399 			/*
4400 			 * GTK on AP interface is a TX-only key, return 0;
4401 			 * on IBSS they're per-station and because we're lazy
4402 			 * we don't support them for RX, so do the same.
4403 			 * CMAC/GMAC in AP/IBSS modes must be done in software
4404 			 * on older NICs.
4405 			 *
4406 			 * Except, of course, beacon protection - it must be
4407 			 * offloaded since we just set a beacon template, and
4408 			 * then we must also offload the IGTK (not just BIGTK)
4409 			 * for firmware reasons.
4410 			 *
4411 			 * So just check for beacon protection - if we don't
4412 			 * have it we cannot get here with keyidx >= 6, and
4413 			 * if we do have it we need to send the key to FW in
4414 			 * all cases (CMAC/GMAC).
4415 			 */
4416 			if (!wiphy_ext_feature_isset(hw->wiphy,
4417 						     NL80211_EXT_FEATURE_BEACON_PROTECTION) &&
4418 			    (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
4419 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
4420 			     key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256)) {
4421 				ret = -EOPNOTSUPP;
4422 				break;
4423 			}
4424 
4425 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
4426 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
4427 			    !iwl_mvm_has_new_tx_api(mvm)) {
4428 				key->hw_key_idx = STA_KEY_IDX_INVALID;
4429 				ret = 0;
4430 				break;
4431 			}
4432 
4433 			if (!mvmvif->ap_ibss_active) {
4434 				for (i = 0;
4435 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
4436 				     i++) {
4437 					if (!mvmvif->ap_early_keys[i]) {
4438 						mvmvif->ap_early_keys[i] = key;
4439 						break;
4440 					}
4441 				}
4442 
4443 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
4444 					ret = -ENOSPC;
4445 				else
4446 					ret = 0;
4447 
4448 				break;
4449 			}
4450 		}
4451 
4452 		/* During FW restart, in order to restore the state as it was,
4453 		 * don't try to reprogram keys we previously failed for.
4454 		 */
4455 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4456 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
4457 			IWL_DEBUG_MAC80211(mvm,
4458 					   "skip invalid idx key programming during restart\n");
4459 			ret = 0;
4460 			break;
4461 		}
4462 
4463 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
4464 		    mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4465 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4466 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4467 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4468 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4469 			struct ieee80211_key_seq seq;
4470 			int tid, q;
4471 
4472 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
4473 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
4474 						     mvm->trans->num_rx_queues),
4475 					 GFP_KERNEL);
4476 			if (!ptk_pn) {
4477 				ret = -ENOMEM;
4478 				break;
4479 			}
4480 
4481 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
4482 				ieee80211_get_key_rx_seq(key, tid, &seq);
4483 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
4484 					memcpy(ptk_pn->q[q].pn[tid],
4485 					       seq.ccmp.pn,
4486 					       IEEE80211_CCMP_PN_LEN);
4487 			}
4488 
4489 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
4490 		}
4491 
4492 		/* in HW restart reuse the index, otherwise request a new one */
4493 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
4494 			key_offset = key->hw_key_idx;
4495 		else
4496 			key_offset = STA_KEY_IDX_INVALID;
4497 
4498 		if (mvmsta && key->flags & IEEE80211_KEY_FLAG_PAIRWISE)
4499 			mvmsta->pairwise_cipher = key->cipher;
4500 
4501 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key (sta:%pM, id:%d)\n",
4502 				   sta ? sta->addr : NULL, key->keyidx);
4503 
4504 		if (sec_key_ver)
4505 			ret = iwl_mvm_sec_key_add(mvm, vif, sta, key);
4506 		else
4507 			ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
4508 
4509 		if (ret) {
4510 			IWL_WARN(mvm, "set key failed\n");
4511 			key->hw_key_idx = STA_KEY_IDX_INVALID;
4512 			if (ptk_pn) {
4513 				RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4514 				kfree(ptk_pn);
4515 			}
4516 			/*
4517 			 * can't add key for RX, but we don't need it
4518 			 * in the device for TX so still return 0,
4519 			 * unless we have new TX API where we cannot
4520 			 * put key material into the TX_CMD
4521 			 */
4522 			if (iwl_mvm_has_new_tx_api(mvm))
4523 				ret = -EOPNOTSUPP;
4524 			else
4525 				ret = 0;
4526 		}
4527 
4528 		break;
4529 	case DISABLE_KEY:
4530 		if (vif->type == NL80211_IFTYPE_STATION &&
4531 		    (keyidx == 6 || keyidx == 7))
4532 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
4533 					 NULL);
4534 
4535 		ret = -ENOENT;
4536 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
4537 			if (mvmvif->ap_early_keys[i] == key) {
4538 				mvmvif->ap_early_keys[i] = NULL;
4539 				ret = 0;
4540 			}
4541 		}
4542 
4543 		/* found in pending list - don't do anything else */
4544 		if (ret == 0)
4545 			break;
4546 
4547 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
4548 			ret = 0;
4549 			break;
4550 		}
4551 
4552 		if (mvmsta && iwl_mvm_has_new_rx_api(mvm) &&
4553 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
4554 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
4555 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
4556 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
4557 			ptk_pn = rcu_dereference_protected(
4558 						mvmsta->ptk_pn[keyidx],
4559 						lockdep_is_held(&mvm->mutex));
4560 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
4561 			if (ptk_pn)
4562 				kfree_rcu(ptk_pn, rcu_head);
4563 		}
4564 
4565 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
4566 		if (sec_key_ver)
4567 			ret = iwl_mvm_sec_key_del(mvm, vif, sta, key);
4568 		else
4569 			ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
4570 		break;
4571 	default:
4572 		ret = -EINVAL;
4573 	}
4574 
4575 	return ret;
4576 }
4577 
4578 int iwl_mvm_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4579 			struct ieee80211_vif *vif, struct ieee80211_sta *sta,
4580 			struct ieee80211_key_conf *key)
4581 {
4582 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4583 	int ret;
4584 
4585 	mutex_lock(&mvm->mutex);
4586 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
4587 	mutex_unlock(&mvm->mutex);
4588 
4589 	return ret;
4590 }
4591 
4592 void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
4593 				 struct ieee80211_vif *vif,
4594 				 struct ieee80211_key_conf *keyconf,
4595 				 struct ieee80211_sta *sta,
4596 				 u32 iv32, u16 *phase1key)
4597 {
4598 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4599 
4600 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
4601 		return;
4602 
4603 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
4604 }
4605 
4606 
4607 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
4608 			       struct iwl_rx_packet *pkt, void *data)
4609 {
4610 	struct iwl_mvm *mvm =
4611 		container_of(notif_wait, struct iwl_mvm, notif_wait);
4612 	struct iwl_hs20_roc_res *resp;
4613 	int resp_len = iwl_rx_packet_payload_len(pkt);
4614 	struct iwl_mvm_time_event_data *te_data = data;
4615 
4616 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
4617 		return true;
4618 
4619 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
4620 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
4621 		return true;
4622 	}
4623 
4624 	resp = (void *)pkt->data;
4625 
4626 	IWL_DEBUG_TE(mvm,
4627 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
4628 		     resp->status, resp->event_unique_id);
4629 
4630 	te_data->uid = le32_to_cpu(resp->event_unique_id);
4631 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
4632 		     te_data->uid);
4633 
4634 	spin_lock_bh(&mvm->time_event_lock);
4635 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
4636 	spin_unlock_bh(&mvm->time_event_lock);
4637 
4638 	return true;
4639 }
4640 
4641 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
4642 				    struct ieee80211_channel *channel,
4643 				    struct ieee80211_vif *vif,
4644 				    int duration)
4645 {
4646 	int res;
4647 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4648 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
4649 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
4650 	struct iwl_notification_wait wait_time_event;
4651 	u32 req_dur, delay;
4652 	struct iwl_hs20_roc_req aux_roc_req = {
4653 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4654 		.id_and_color =
4655 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
4656 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
4657 	};
4658 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
4659 		&aux_roc_req.channel_info);
4660 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
4661 
4662 	/* Set the channel info data */
4663 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
4664 			      iwl_mvm_phy_band_from_nl80211(channel->band),
4665 			      IWL_PHY_CHANNEL_MODE20,
4666 			      0);
4667 
4668 	/* Set the time and duration */
4669 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
4670 
4671 	iwl_mvm_roc_duration_and_delay(vif, duration, &req_dur, &delay);
4672 	tail->duration = cpu_to_le32(req_dur);
4673 	tail->apply_time_max_delay = cpu_to_le32(delay);
4674 
4675 	IWL_DEBUG_TE(mvm,
4676 		     "ROC: Requesting to remain on channel %u for %ums\n",
4677 		     channel->hw_value, req_dur);
4678 	IWL_DEBUG_TE(mvm,
4679 		     "\t(requested = %ums, max_delay = %ums)\n",
4680 		     duration, delay);
4681 
4682 	/* Set the node address */
4683 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
4684 
4685 	lockdep_assert_held(&mvm->mutex);
4686 
4687 	spin_lock_bh(&mvm->time_event_lock);
4688 
4689 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
4690 		spin_unlock_bh(&mvm->time_event_lock);
4691 		return -EIO;
4692 	}
4693 
4694 	te_data->vif = vif;
4695 	te_data->duration = duration;
4696 	te_data->id = HOT_SPOT_CMD;
4697 
4698 	spin_unlock_bh(&mvm->time_event_lock);
4699 
4700 	/*
4701 	 * Use a notification wait, which really just processes the
4702 	 * command response and doesn't wait for anything, in order
4703 	 * to be able to process the response and get the UID inside
4704 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
4705 	 * stores the buffer and then wakes up this thread, by which
4706 	 * time another notification (that the time event started)
4707 	 * might already be processed unsuccessfully.
4708 	 */
4709 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
4710 				   time_event_response,
4711 				   ARRAY_SIZE(time_event_response),
4712 				   iwl_mvm_rx_aux_roc, te_data);
4713 
4714 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
4715 				   &aux_roc_req);
4716 
4717 	if (res) {
4718 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
4719 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
4720 		goto out_clear_te;
4721 	}
4722 
4723 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
4724 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
4725 	/* should never fail */
4726 	WARN_ON_ONCE(res);
4727 
4728 	if (res) {
4729  out_clear_te:
4730 		spin_lock_bh(&mvm->time_event_lock);
4731 		iwl_mvm_te_clear_data(mvm, te_data);
4732 		spin_unlock_bh(&mvm->time_event_lock);
4733 	}
4734 
4735 	return res;
4736 }
4737 
4738 static int iwl_mvm_add_aux_sta_for_hs20(struct iwl_mvm *mvm, u32 lmac_id)
4739 {
4740 	int ret = 0;
4741 
4742 	lockdep_assert_held(&mvm->mutex);
4743 
4744 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4745 			 IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
4746 		IWL_ERR(mvm, "hotspot not supported\n");
4747 		return -EINVAL;
4748 	}
4749 
4750 	if (iwl_mvm_has_new_station_api(mvm->fw)) {
4751 		ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
4752 		WARN(ret, "Failed to allocate aux station");
4753 	}
4754 
4755 	return ret;
4756 }
4757 
4758 static int iwl_mvm_roc_link(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
4759 {
4760 	int ret;
4761 
4762 	lockdep_assert_held(&mvm->mutex);
4763 
4764 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4765 	if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4766 		return ret;
4767 
4768 	/* The station and queue allocation must be done only after the binding
4769 	 * is done, as otherwise the FW might incorrectly configure its state.
4770 	 */
4771 	return iwl_mvm_add_p2p_bcast_sta(mvm, vif);
4772 }
4773 
4774 static int iwl_mvm_roc(struct ieee80211_hw *hw,
4775 		       struct ieee80211_vif *vif,
4776 		       struct ieee80211_channel *channel,
4777 		       int duration,
4778 		       enum ieee80211_roc_type type)
4779 {
4780 	static const struct iwl_mvm_roc_ops ops = {
4781 		.add_aux_sta_for_hs20 = iwl_mvm_add_aux_sta_for_hs20,
4782 		.link = iwl_mvm_roc_link,
4783 	};
4784 
4785 	return iwl_mvm_roc_common(hw, vif, channel, duration, type, &ops);
4786 }
4787 
4788 static int iwl_mvm_roc_station(struct iwl_mvm *mvm,
4789 			       struct ieee80211_channel *channel,
4790 			       struct ieee80211_vif *vif,
4791 			       int duration)
4792 {
4793 	int ret;
4794 	u32 cmd_id = WIDE_ID(MAC_CONF_GROUP, ROC_CMD);
4795 	u8 fw_ver = iwl_fw_lookup_cmd_ver(mvm->fw, cmd_id,
4796 					  IWL_FW_CMD_VER_UNKNOWN);
4797 
4798 	if (fw_ver == IWL_FW_CMD_VER_UNKNOWN) {
4799 		ret = iwl_mvm_send_aux_roc_cmd(mvm, channel, vif, duration);
4800 	} else if (fw_ver >= 3) {
4801 		ret = iwl_mvm_roc_add_cmd(mvm, channel, vif, duration,
4802 					  ROC_ACTIVITY_HOTSPOT);
4803 	} else {
4804 		ret = -EOPNOTSUPP;
4805 		IWL_ERR(mvm, "ROC command version %d mismatch!\n", fw_ver);
4806 	}
4807 
4808 	return ret;
4809 }
4810 
4811 static int iwl_mvm_p2p_find_phy_ctxt(struct iwl_mvm *mvm,
4812 				     struct ieee80211_vif *vif,
4813 				     struct ieee80211_channel *channel)
4814 {
4815 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4816 	struct cfg80211_chan_def chandef;
4817 	int i;
4818 
4819 	lockdep_assert_held(&mvm->mutex);
4820 
4821 	if (mvmvif->deflink.phy_ctxt &&
4822 	    channel == mvmvif->deflink.phy_ctxt->channel)
4823 		return 0;
4824 
4825 	/* Try using a PHY context that is already in use */
4826 	for (i = 0; i < NUM_PHY_CTX; i++) {
4827 		struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[i];
4828 
4829 		if (!phy_ctxt->ref || mvmvif->deflink.phy_ctxt == phy_ctxt)
4830 			continue;
4831 
4832 		if (channel == phy_ctxt->channel) {
4833 			if (mvmvif->deflink.phy_ctxt)
4834 				iwl_mvm_phy_ctxt_unref(mvm,
4835 						       mvmvif->deflink.phy_ctxt);
4836 
4837 			mvmvif->deflink.phy_ctxt = phy_ctxt;
4838 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->deflink.phy_ctxt);
4839 			return 0;
4840 		}
4841 	}
4842 
4843 	/* We already have a phy_ctxt, but it's not on the right channel */
4844 	if (mvmvif->deflink.phy_ctxt)
4845 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->deflink.phy_ctxt);
4846 
4847 	mvmvif->deflink.phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4848 	if (!mvmvif->deflink.phy_ctxt)
4849 		return -ENOSPC;
4850 
4851 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
4852 
4853 	return iwl_mvm_phy_ctxt_add(mvm, mvmvif->deflink.phy_ctxt,
4854 				    &chandef, NULL, 1, 1);
4855 }
4856 
4857 /* Execute the common part for MLD and non-MLD modes */
4858 int iwl_mvm_roc_common(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
4859 		       struct ieee80211_channel *channel, int duration,
4860 		       enum ieee80211_roc_type type,
4861 		       const struct iwl_mvm_roc_ops *ops)
4862 {
4863 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4864 	u32 lmac_id;
4865 	int ret;
4866 
4867 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
4868 			   duration, type);
4869 
4870 	/*
4871 	 * Flush the done work, just in case it's still pending, so that
4872 	 * the work it does can complete and we can accept new frames.
4873 	 */
4874 	flush_work(&mvm->roc_done_wk);
4875 
4876 	ret = iwl_mvm_esr_non_bss_link(mvm, vif, 0, true);
4877 	if (ret)
4878 		return ret;
4879 
4880 	mutex_lock(&mvm->mutex);
4881 
4882 	switch (vif->type) {
4883 	case NL80211_IFTYPE_STATION:
4884 		lmac_id = iwl_mvm_get_lmac_id(mvm, channel->band);
4885 
4886 		/* Use aux roc framework (HS20) */
4887 		ret = ops->add_aux_sta_for_hs20(mvm, lmac_id);
4888 		if (!ret)
4889 			ret = iwl_mvm_roc_station(mvm, channel, vif, duration);
4890 		goto out_unlock;
4891 	case NL80211_IFTYPE_P2P_DEVICE:
4892 		/* handle below */
4893 		break;
4894 	default:
4895 		IWL_ERR(mvm, "ROC: Invalid vif type=%u\n", vif->type);
4896 		ret = -EINVAL;
4897 		goto out_unlock;
4898 	}
4899 
4900 
4901 	ret = iwl_mvm_p2p_find_phy_ctxt(mvm, vif, channel);
4902 	if (ret)
4903 		goto out_unlock;
4904 
4905 	ret = ops->link(mvm, vif);
4906 	if (ret)
4907 		goto out_unlock;
4908 
4909 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4910 out_unlock:
4911 	mutex_unlock(&mvm->mutex);
4912 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4913 	return ret;
4914 }
4915 
4916 int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4917 		       struct ieee80211_vif *vif)
4918 {
4919 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4920 
4921 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4922 
4923 	iwl_mvm_stop_roc(mvm, vif);
4924 
4925 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4926 	return 0;
4927 }
4928 
4929 struct iwl_mvm_ftm_responder_iter_data {
4930 	bool responder;
4931 	struct ieee80211_chanctx_conf *ctx;
4932 };
4933 
4934 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4935 					       struct ieee80211_vif *vif)
4936 {
4937 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4938 
4939 	if (rcu_access_pointer(vif->bss_conf.chanctx_conf) == data->ctx &&
4940 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4941 		data->responder = true;
4942 }
4943 
4944 bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4945 				      struct ieee80211_chanctx_conf *ctx)
4946 {
4947 	struct iwl_mvm_ftm_responder_iter_data data = {
4948 		.responder = false,
4949 		.ctx = ctx,
4950 	};
4951 
4952 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4953 					IEEE80211_IFACE_ITER_NORMAL,
4954 					iwl_mvm_ftm_responder_chanctx_iter,
4955 					&data);
4956 	return data.responder;
4957 }
4958 
4959 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4960 				 struct ieee80211_chanctx_conf *ctx)
4961 {
4962 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4963 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4964 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
4965 	int ret;
4966 
4967 	lockdep_assert_held(&mvm->mutex);
4968 
4969 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4970 
4971 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4972 	if (!phy_ctxt) {
4973 		ret = -ENOSPC;
4974 		goto out;
4975 	}
4976 
4977 	ret = iwl_mvm_phy_ctxt_add(mvm, phy_ctxt, def, &ctx->ap,
4978 				   ctx->rx_chains_static,
4979 				   ctx->rx_chains_dynamic);
4980 	if (ret) {
4981 		IWL_ERR(mvm, "Failed to add PHY context\n");
4982 		goto out;
4983 	}
4984 
4985 	*phy_ctxt_id = phy_ctxt->id;
4986 out:
4987 	return ret;
4988 }
4989 
4990 int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4991 			struct ieee80211_chanctx_conf *ctx)
4992 {
4993 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4994 	int ret;
4995 
4996 	mutex_lock(&mvm->mutex);
4997 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4998 	mutex_unlock(&mvm->mutex);
4999 
5000 	return ret;
5001 }
5002 
5003 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
5004 				     struct ieee80211_chanctx_conf *ctx)
5005 {
5006 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5007 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5008 
5009 	lockdep_assert_held(&mvm->mutex);
5010 
5011 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
5012 }
5013 
5014 void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
5015 			    struct ieee80211_chanctx_conf *ctx)
5016 {
5017 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5018 
5019 	mutex_lock(&mvm->mutex);
5020 	__iwl_mvm_remove_chanctx(mvm, ctx);
5021 	mutex_unlock(&mvm->mutex);
5022 }
5023 
5024 void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
5025 			    struct ieee80211_chanctx_conf *ctx, u32 changed)
5026 {
5027 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5028 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5029 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5030 	struct cfg80211_chan_def *def = iwl_mvm_chanctx_def(mvm, ctx);
5031 
5032 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
5033 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
5034 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
5035 				   IEEE80211_CHANCTX_CHANGE_RADAR |
5036 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
5037 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
5038 		      phy_ctxt->ref, changed))
5039 		return;
5040 
5041 	mutex_lock(&mvm->mutex);
5042 
5043 	/* we are only changing the min_width, may be a noop */
5044 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
5045 		if (phy_ctxt->width == def->width)
5046 			goto out_unlock;
5047 
5048 		/* we are just toggling between 20_NOHT and 20 */
5049 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
5050 		    def->width <= NL80211_CHAN_WIDTH_20)
5051 			goto out_unlock;
5052 	}
5053 
5054 	iwl_mvm_bt_coex_vif_change(mvm);
5055 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def, &ctx->ap,
5056 				 ctx->rx_chains_static,
5057 				 ctx->rx_chains_dynamic);
5058 
5059 out_unlock:
5060 	mutex_unlock(&mvm->mutex);
5061 }
5062 
5063 /*
5064  * This function executes the common part for MLD and non-MLD modes.
5065  *
5066  * Returns true if we're done assigning the chanctx
5067  * (either on failure or success)
5068  */
5069 static bool
5070 __iwl_mvm_assign_vif_chanctx_common(struct iwl_mvm *mvm,
5071 				    struct ieee80211_vif *vif,
5072 				    struct ieee80211_chanctx_conf *ctx,
5073 				    bool switching_chanctx, int *ret)
5074 {
5075 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
5076 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
5077 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5078 
5079 	lockdep_assert_held(&mvm->mutex);
5080 
5081 	mvmvif->deflink.phy_ctxt = phy_ctxt;
5082 
5083 	switch (vif->type) {
5084 	case NL80211_IFTYPE_AP:
5085 		/* only needed if we're switching chanctx (i.e. during CSA) */
5086 		if (switching_chanctx) {
5087 			mvmvif->ap_ibss_active = true;
5088 			break;
5089 		}
5090 		fallthrough;
5091 	case NL80211_IFTYPE_ADHOC:
5092 		/*
5093 		 * The AP binding flow is handled as part of the start_ap flow
5094 		 * (in bss_info_changed), similarly for IBSS.
5095 		 */
5096 		*ret = 0;
5097 		return true;
5098 	case NL80211_IFTYPE_STATION:
5099 		break;
5100 	case NL80211_IFTYPE_MONITOR:
5101 		/* always disable PS when a monitor interface is active */
5102 		mvmvif->ps_disabled = true;
5103 		break;
5104 	default:
5105 		*ret = -EINVAL;
5106 		return true;
5107 	}
5108 	return false;
5109 }
5110 
5111 static int __iwl_mvm_assign_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 	int ret;
5119 
5120 	if (WARN_ON(!link_conf))
5121 		return -EINVAL;
5122 
5123 	if (__iwl_mvm_assign_vif_chanctx_common(mvm, vif, ctx,
5124 						switching_chanctx, &ret))
5125 		goto out;
5126 
5127 	ret = iwl_mvm_binding_add_vif(mvm, vif);
5128 	if (ret)
5129 		goto out;
5130 
5131 	/*
5132 	 * Power state must be updated before quotas,
5133 	 * otherwise fw will complain.
5134 	 */
5135 	iwl_mvm_power_update_mac(mvm);
5136 
5137 	/* Setting the quota at this stage is only required for monitor
5138 	 * interfaces. For the other types, the bss_info changed flow
5139 	 * will handle quota settings.
5140 	 */
5141 	if (vif->type == NL80211_IFTYPE_MONITOR) {
5142 		mvmvif->monitor_active = true;
5143 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
5144 		if (ret)
5145 			goto out_remove_binding;
5146 
5147 		ret = iwl_mvm_add_snif_sta(mvm, vif);
5148 		if (ret)
5149 			goto out_remove_binding;
5150 
5151 	}
5152 
5153 	/* Handle binding during CSA */
5154 	if (vif->type == NL80211_IFTYPE_AP) {
5155 		iwl_mvm_update_quotas(mvm, false, NULL);
5156 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
5157 	}
5158 
5159 	if (vif->type == NL80211_IFTYPE_STATION) {
5160 		if (!switching_chanctx) {
5161 			mvmvif->csa_bcn_pending = false;
5162 			goto out;
5163 		}
5164 
5165 		mvmvif->csa_bcn_pending = true;
5166 
5167 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5168 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5169 			u32 duration = 5 * vif->bss_conf.beacon_int;
5170 
5171 			/* Protect the session to make sure we hear the first
5172 			 * beacon on the new channel.
5173 			 */
5174 			iwl_mvm_protect_session(mvm, vif, duration, duration,
5175 						vif->bss_conf.beacon_int / 2,
5176 						true);
5177 		}
5178 
5179 		iwl_mvm_update_quotas(mvm, false, NULL);
5180 	}
5181 
5182 	goto out;
5183 
5184 out_remove_binding:
5185 	iwl_mvm_binding_remove_vif(mvm, vif);
5186 	iwl_mvm_power_update_mac(mvm);
5187 out:
5188 	if (ret)
5189 		mvmvif->deflink.phy_ctxt = NULL;
5190 	return ret;
5191 }
5192 
5193 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
5194 				      struct ieee80211_vif *vif,
5195 				      struct ieee80211_bss_conf *link_conf,
5196 				      struct ieee80211_chanctx_conf *ctx)
5197 {
5198 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5199 	int ret;
5200 
5201 	mutex_lock(&mvm->mutex);
5202 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5203 	mutex_unlock(&mvm->mutex);
5204 
5205 	return ret;
5206 }
5207 
5208 /*
5209  * This function executes the common part for MLD and non-MLD modes.
5210  *
5211  * Returns if chanctx unassign chanctx is done
5212  * (either on failure or success)
5213  */
5214 static bool __iwl_mvm_unassign_vif_chanctx_common(struct iwl_mvm *mvm,
5215 						  struct ieee80211_vif *vif,
5216 						  bool switching_chanctx)
5217 {
5218 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5219 
5220 	lockdep_assert_held(&mvm->mutex);
5221 	iwl_mvm_remove_time_event(mvm, mvmvif,
5222 				  &mvmvif->time_event_data);
5223 
5224 	switch (vif->type) {
5225 	case NL80211_IFTYPE_ADHOC:
5226 		return true;
5227 	case NL80211_IFTYPE_MONITOR:
5228 		mvmvif->monitor_active = false;
5229 		mvmvif->ps_disabled = false;
5230 		break;
5231 	case NL80211_IFTYPE_AP:
5232 		/* This part is triggered only during CSA */
5233 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
5234 			return true;
5235 
5236 		mvmvif->csa_countdown = false;
5237 
5238 		/* Set CS bit on all the stations */
5239 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
5240 
5241 		/* Save blocked iface, the timeout is set on the next beacon */
5242 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
5243 
5244 		mvmvif->ap_ibss_active = false;
5245 		break;
5246 	default:
5247 		break;
5248 	}
5249 	return false;
5250 }
5251 
5252 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
5253 					   struct ieee80211_vif *vif,
5254 					   struct ieee80211_bss_conf *link_conf,
5255 					   struct ieee80211_chanctx_conf *ctx,
5256 					   bool switching_chanctx)
5257 {
5258 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5259 	struct ieee80211_vif *disabled_vif = NULL;
5260 
5261 	if (__iwl_mvm_unassign_vif_chanctx_common(mvm, vif, switching_chanctx))
5262 		goto out;
5263 
5264 	if (vif->type == NL80211_IFTYPE_MONITOR)
5265 		iwl_mvm_rm_snif_sta(mvm, vif);
5266 
5267 
5268 	if (vif->type == NL80211_IFTYPE_STATION && switching_chanctx) {
5269 		disabled_vif = vif;
5270 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5271 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
5272 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
5273 	}
5274 
5275 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
5276 	iwl_mvm_binding_remove_vif(mvm, vif);
5277 
5278 out:
5279 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
5280 	    switching_chanctx)
5281 		return;
5282 	mvmvif->deflink.phy_ctxt = NULL;
5283 	iwl_mvm_power_update_mac(mvm);
5284 }
5285 
5286 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
5287 					 struct ieee80211_vif *vif,
5288 					 struct ieee80211_bss_conf *link_conf,
5289 					 struct ieee80211_chanctx_conf *ctx)
5290 {
5291 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5292 
5293 	mutex_lock(&mvm->mutex);
5294 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, link_conf, ctx, false);
5295 	mutex_unlock(&mvm->mutex);
5296 }
5297 
5298 static int
5299 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
5300 				struct ieee80211_vif_chanctx_switch *vifs,
5301 				const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5302 {
5303 	int ret;
5304 
5305 	mutex_lock(&mvm->mutex);
5306 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5307 				    vifs[0].old_ctx, true);
5308 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
5309 
5310 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
5311 	if (ret) {
5312 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
5313 		goto out_reassign;
5314 	}
5315 
5316 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5317 					vifs[0].new_ctx, true);
5318 	if (ret) {
5319 		IWL_ERR(mvm,
5320 			"failed to assign new_ctx during channel switch\n");
5321 		goto out_remove;
5322 	}
5323 
5324 	/* we don't support TDLS during DCM - can be caused by channel switch */
5325 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
5326 		iwl_mvm_teardown_tdls_peers(mvm);
5327 
5328 	goto out;
5329 
5330 out_remove:
5331 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
5332 
5333 out_reassign:
5334 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
5335 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
5336 		goto out_restart;
5337 	}
5338 
5339 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5340 				      vifs[0].old_ctx, true)) {
5341 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5342 		goto out_restart;
5343 	}
5344 
5345 	goto out;
5346 
5347 out_restart:
5348 	/* things keep failing, better restart the hw */
5349 	iwl_mvm_nic_restart(mvm, false);
5350 
5351 out:
5352 	mutex_unlock(&mvm->mutex);
5353 
5354 	return ret;
5355 }
5356 
5357 static int
5358 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
5359 				    struct ieee80211_vif_chanctx_switch *vifs,
5360 				    const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5361 {
5362 	int ret;
5363 
5364 	mutex_lock(&mvm->mutex);
5365 	ops->__unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5366 				    vifs[0].old_ctx, true);
5367 
5368 	ret = ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5369 					vifs[0].new_ctx, true);
5370 	if (ret) {
5371 		IWL_ERR(mvm,
5372 			"failed to assign new_ctx during channel switch\n");
5373 		goto out_reassign;
5374 	}
5375 
5376 	goto out;
5377 
5378 out_reassign:
5379 	if (ops->__assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].link_conf,
5380 				      vifs[0].old_ctx, true)) {
5381 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
5382 		goto out_restart;
5383 	}
5384 
5385 	goto out;
5386 
5387 out_restart:
5388 	/* things keep failing, better restart the hw */
5389 	iwl_mvm_nic_restart(mvm, false);
5390 
5391 out:
5392 	mutex_unlock(&mvm->mutex);
5393 
5394 	return ret;
5395 }
5396 
5397 /* Execute the common part for both MLD and non-MLD modes */
5398 int
5399 iwl_mvm_switch_vif_chanctx_common(struct ieee80211_hw *hw,
5400 				  struct ieee80211_vif_chanctx_switch *vifs,
5401 				  int n_vifs,
5402 				  enum ieee80211_chanctx_switch_mode mode,
5403 				  const struct iwl_mvm_switch_vif_chanctx_ops *ops)
5404 {
5405 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5406 	int ret;
5407 
5408 	/* we only support a single-vif right now */
5409 	if (n_vifs > 1)
5410 		return -EOPNOTSUPP;
5411 
5412 	switch (mode) {
5413 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
5414 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs, ops);
5415 		break;
5416 	case CHANCTX_SWMODE_REASSIGN_VIF:
5417 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs, ops);
5418 		break;
5419 	default:
5420 		ret = -EOPNOTSUPP;
5421 		break;
5422 	}
5423 
5424 	return ret;
5425 }
5426 
5427 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
5428 				      struct ieee80211_vif_chanctx_switch *vifs,
5429 				      int n_vifs,
5430 				      enum ieee80211_chanctx_switch_mode mode)
5431 {
5432 	static const struct iwl_mvm_switch_vif_chanctx_ops ops = {
5433 		.__assign_vif_chanctx = __iwl_mvm_assign_vif_chanctx,
5434 		.__unassign_vif_chanctx = __iwl_mvm_unassign_vif_chanctx,
5435 	};
5436 
5437 	return iwl_mvm_switch_vif_chanctx_common(hw, vifs, n_vifs, mode, &ops);
5438 }
5439 
5440 int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
5441 {
5442 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5443 
5444 	return mvm->ibss_manager;
5445 }
5446 
5447 static int iwl_mvm_set_tim(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
5448 			   bool set)
5449 {
5450 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5451 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
5452 
5453 	if (!mvm_sta || !mvm_sta->vif) {
5454 		IWL_ERR(mvm, "Station is not associated to a vif\n");
5455 		return -EINVAL;
5456 	}
5457 
5458 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif,
5459 					       &mvm_sta->vif->bss_conf);
5460 }
5461 
5462 #ifdef CONFIG_NL80211_TESTMODE
5463 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
5464 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
5465 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
5466 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
5467 };
5468 
5469 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
5470 				      struct ieee80211_vif *vif,
5471 				      void *data, int len)
5472 {
5473 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
5474 	int err;
5475 	u32 noa_duration;
5476 
5477 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
5478 				   iwl_mvm_tm_policy, NULL);
5479 	if (err)
5480 		return err;
5481 
5482 	if (!tb[IWL_MVM_TM_ATTR_CMD])
5483 		return -EINVAL;
5484 
5485 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
5486 	case IWL_MVM_TM_CMD_SET_NOA:
5487 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
5488 		    !vif->bss_conf.enable_beacon ||
5489 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
5490 			return -EINVAL;
5491 
5492 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
5493 		if (noa_duration >= vif->bss_conf.beacon_int)
5494 			return -EINVAL;
5495 
5496 		mvm->noa_duration = noa_duration;
5497 		mvm->noa_vif = vif;
5498 
5499 		return iwl_mvm_update_quotas(mvm, true, NULL);
5500 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
5501 		/* must be associated client vif - ignore authorized */
5502 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
5503 		    !vif->cfg.assoc || !vif->bss_conf.dtim_period ||
5504 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
5505 			return -EINVAL;
5506 
5507 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
5508 			return iwl_mvm_enable_beacon_filter(mvm, vif);
5509 		return iwl_mvm_disable_beacon_filter(mvm, vif);
5510 	}
5511 
5512 	return -EOPNOTSUPP;
5513 }
5514 
5515 int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
5516 			     struct ieee80211_vif *vif,
5517 			     void *data, int len)
5518 {
5519 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5520 	int err;
5521 
5522 	mutex_lock(&mvm->mutex);
5523 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
5524 	mutex_unlock(&mvm->mutex);
5525 
5526 	return err;
5527 }
5528 #endif
5529 
5530 void iwl_mvm_channel_switch(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5531 			    struct ieee80211_channel_switch *chsw)
5532 {
5533 	/* By implementing this operation, we prevent mac80211 from
5534 	 * starting its own channel switch timer, so that we can call
5535 	 * ieee80211_chswitch_done() ourselves at the right time
5536 	 * (which is when the absence time event starts).
5537 	 */
5538 
5539 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
5540 			   "dummy channel switch op\n");
5541 }
5542 
5543 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
5544 				       struct ieee80211_vif *vif,
5545 				       struct ieee80211_channel_switch *chsw)
5546 {
5547 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5548 	struct iwl_chan_switch_te_cmd cmd = {
5549 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5550 							  mvmvif->color)),
5551 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
5552 		.tsf = cpu_to_le32(chsw->timestamp),
5553 		.cs_count = chsw->count,
5554 		.cs_mode = chsw->block_tx,
5555 	};
5556 
5557 	lockdep_assert_held(&mvm->mutex);
5558 
5559 	if (chsw->delay)
5560 		cmd.cs_delayed_bcn_count =
5561 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
5562 
5563 	return iwl_mvm_send_cmd_pdu(mvm,
5564 				    WIDE_ID(MAC_CONF_GROUP,
5565 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
5566 				    0, sizeof(cmd), &cmd);
5567 }
5568 
5569 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
5570 				       struct ieee80211_vif *vif,
5571 				       struct ieee80211_channel_switch *chsw)
5572 {
5573 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5574 	u32 apply_time;
5575 
5576 	/* Schedule the time event to a bit before beacon 1,
5577 	 * to make sure we're in the new channel when the
5578 	 * GO/AP arrives. In case count <= 1 immediately schedule the
5579 	 * TE (this might result with some packet loss or connection
5580 	 * loss).
5581 	 */
5582 	if (chsw->count <= 1)
5583 		apply_time = 0;
5584 	else
5585 		apply_time = chsw->device_timestamp +
5586 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
5587 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
5588 
5589 	if (chsw->block_tx)
5590 		iwl_mvm_csa_client_absent(mvm, vif);
5591 
5592 	if (mvmvif->bf_enabled) {
5593 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif);
5594 
5595 		if (ret)
5596 			return ret;
5597 	}
5598 
5599 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
5600 				    apply_time);
5601 
5602 	return 0;
5603 }
5604 
5605 static void iwl_mvm_csa_block_txqs(void *data, struct ieee80211_sta *sta)
5606 {
5607 	int i;
5608 
5609 	for (i = 0; i < ARRAY_SIZE(sta->txq); i++) {
5610 		struct iwl_mvm_txq *mvmtxq =
5611 			iwl_mvm_txq_from_mac80211(sta->txq[i]);
5612 
5613 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5614 	}
5615 }
5616 
5617 #define IWL_MAX_CSA_BLOCK_TX 1500
5618 int iwl_mvm_pre_channel_switch(struct iwl_mvm *mvm,
5619 			       struct ieee80211_vif *vif,
5620 			       struct ieee80211_channel_switch *chsw)
5621 {
5622 	struct ieee80211_vif *csa_vif;
5623 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5624 	struct iwl_mvm_txq *mvmtxq;
5625 	int ret;
5626 
5627 	lockdep_assert_held(&mvm->mutex);
5628 
5629 	mvmvif->csa_failed = false;
5630 	mvmvif->csa_blocks_tx = false;
5631 
5632 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
5633 			   chsw->chandef.center_freq1);
5634 
5635 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
5636 				       ieee80211_vif_to_wdev(vif),
5637 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
5638 
5639 	switch (vif->type) {
5640 	case NL80211_IFTYPE_AP:
5641 		csa_vif =
5642 			rcu_dereference_protected(mvm->csa_vif,
5643 						  lockdep_is_held(&mvm->mutex));
5644 		if (WARN_ONCE(csa_vif && csa_vif->bss_conf.csa_active,
5645 			      "Another CSA is already in progress"))
5646 			return -EBUSY;
5647 
5648 		/* we still didn't unblock tx. prevent new CS meanwhile */
5649 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
5650 					      lockdep_is_held(&mvm->mutex)))
5651 			return -EBUSY;
5652 
5653 		rcu_assign_pointer(mvm->csa_vif, vif);
5654 
5655 		if (WARN_ONCE(mvmvif->csa_countdown,
5656 			      "Previous CSA countdown didn't complete"))
5657 			return -EBUSY;
5658 
5659 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
5660 
5661 		if (!chsw->block_tx)
5662 			break;
5663 		/* don't need blocking in driver otherwise - mac80211 will do */
5664 		if (!ieee80211_hw_check(mvm->hw, HANDLES_QUIET_CSA))
5665 			break;
5666 
5667 		mvmvif->csa_blocks_tx = true;
5668 		mvmtxq = iwl_mvm_txq_from_mac80211(vif->txq);
5669 		set_bit(IWL_MVM_TXQ_STATE_STOP_AP_CSA, &mvmtxq->state);
5670 		ieee80211_iterate_stations_atomic(mvm->hw,
5671 						  iwl_mvm_csa_block_txqs,
5672 						  NULL);
5673 		break;
5674 	case NL80211_IFTYPE_STATION:
5675 		mvmvif->csa_blocks_tx = chsw->block_tx;
5676 
5677 		/*
5678 		 * In the new flow FW is in charge of timing the switch so there
5679 		 * is no need for all of this
5680 		 */
5681 		if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5682 					    CHANNEL_SWITCH_ERROR_NOTIF,
5683 					    0))
5684 			break;
5685 
5686 		/*
5687 		 * We haven't configured the firmware to be associated yet since
5688 		 * we don't know the dtim period. In this case, the firmware can't
5689 		 * track the beacons.
5690 		 */
5691 		if (!vif->cfg.assoc || !vif->bss_conf.dtim_period)
5692 			return -EBUSY;
5693 
5694 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX &&
5695 		    hweight16(vif->valid_links) <= 1)
5696 			schedule_delayed_work(&mvmvif->csa_work, 0);
5697 
5698 		if (chsw->block_tx) {
5699 			/*
5700 			 * In case of undetermined / long time with immediate
5701 			 * quiet monitor status to gracefully disconnect
5702 			 */
5703 			if (!chsw->count ||
5704 			    chsw->count * vif->bss_conf.beacon_int >
5705 			    IWL_MAX_CSA_BLOCK_TX)
5706 				schedule_delayed_work(&mvmvif->csa_work,
5707 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
5708 		}
5709 
5710 		if (!fw_has_capa(&mvm->fw->ucode_capa,
5711 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
5712 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
5713 			if (ret)
5714 				return ret;
5715 		} else {
5716 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
5717 		}
5718 
5719 		mvmvif->csa_count = chsw->count;
5720 		mvmvif->csa_misbehave = false;
5721 		break;
5722 	default:
5723 		break;
5724 	}
5725 
5726 	mvmvif->ps_disabled = true;
5727 
5728 	ret = iwl_mvm_power_update_ps(mvm);
5729 	if (ret)
5730 		return ret;
5731 
5732 	/* we won't be on this channel any longer */
5733 	iwl_mvm_teardown_tdls_peers(mvm);
5734 
5735 	return ret;
5736 }
5737 
5738 static int iwl_mvm_mac_pre_channel_switch(struct ieee80211_hw *hw,
5739 					  struct ieee80211_vif *vif,
5740 					  struct ieee80211_channel_switch *chsw)
5741 {
5742 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5743 	int ret;
5744 
5745 	mutex_lock(&mvm->mutex);
5746 	ret = iwl_mvm_pre_channel_switch(mvm, vif, chsw);
5747 	mutex_unlock(&mvm->mutex);
5748 
5749 	return ret;
5750 }
5751 
5752 void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
5753 				      struct ieee80211_vif *vif,
5754 				      struct ieee80211_channel_switch *chsw)
5755 {
5756 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5757 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5758 	struct iwl_chan_switch_te_cmd cmd = {
5759 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5760 							  mvmvif->color)),
5761 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
5762 		.tsf = cpu_to_le32(chsw->timestamp),
5763 		.cs_count = chsw->count,
5764 		.cs_mode = chsw->block_tx,
5765 	};
5766 
5767 	/*
5768 	 * In the new flow FW is in charge of timing the switch so there is no
5769 	 * need for all of this
5770 	 */
5771 	if (iwl_fw_lookup_notif_ver(mvm->fw, MAC_CONF_GROUP,
5772 				    CHANNEL_SWITCH_ERROR_NOTIF, 0))
5773 		return;
5774 
5775 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
5776 		return;
5777 
5778 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
5779 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
5780 
5781 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
5782 		if (mvmvif->csa_misbehave) {
5783 			struct ieee80211_bss_conf *link_conf;
5784 
5785 			/* Second time, give up on this AP*/
5786 
5787 			link_conf = wiphy_dereference(hw->wiphy,
5788 						      vif->link_conf[chsw->link_id]);
5789 			if (WARN_ON(!link_conf))
5790 				return;
5791 
5792 			iwl_mvm_abort_channel_switch(hw, vif, link_conf);
5793 			ieee80211_chswitch_done(vif, false, 0);
5794 			mvmvif->csa_misbehave = false;
5795 			return;
5796 		}
5797 		mvmvif->csa_misbehave = true;
5798 	}
5799 	mvmvif->csa_count = chsw->count;
5800 
5801 	mutex_lock(&mvm->mutex);
5802 	if (mvmvif->csa_failed)
5803 		goto out_unlock;
5804 
5805 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
5806 				     WIDE_ID(MAC_CONF_GROUP,
5807 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
5808 				     0, sizeof(cmd), &cmd));
5809 out_unlock:
5810 	mutex_unlock(&mvm->mutex);
5811 }
5812 
5813 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
5814 {
5815 	int i;
5816 
5817 	if (!iwl_mvm_has_new_tx_api(mvm)) {
5818 		if (drop) {
5819 			mutex_lock(&mvm->mutex);
5820 			iwl_mvm_flush_tx_path(mvm,
5821 				iwl_mvm_flushable_queues(mvm) & queues);
5822 			mutex_unlock(&mvm->mutex);
5823 		} else {
5824 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
5825 		}
5826 		return;
5827 	}
5828 
5829 	mutex_lock(&mvm->mutex);
5830 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5831 		struct ieee80211_sta *sta;
5832 
5833 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5834 						lockdep_is_held(&mvm->mutex));
5835 		if (IS_ERR_OR_NULL(sta))
5836 			continue;
5837 
5838 		if (drop)
5839 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
5840 		else
5841 			iwl_mvm_wait_sta_queues_empty(mvm,
5842 					iwl_mvm_sta_from_mac80211(sta));
5843 	}
5844 	mutex_unlock(&mvm->mutex);
5845 }
5846 
5847 void iwl_mvm_mac_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5848 		       u32 queues, bool drop)
5849 {
5850 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5851 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5852 	struct iwl_mvm_sta *mvmsta;
5853 	struct ieee80211_sta *sta;
5854 	bool ap_sta_done = false;
5855 	int i;
5856 	u32 msk = 0;
5857 
5858 	if (!vif) {
5859 		iwl_mvm_flush_no_vif(mvm, queues, drop);
5860 		return;
5861 	}
5862 
5863 	if (!drop && hweight16(vif->active_links) <= 1) {
5864 		int link_id = vif->active_links ? __ffs(vif->active_links) : 0;
5865 		struct ieee80211_bss_conf *link_conf;
5866 
5867 		link_conf = wiphy_dereference(hw->wiphy,
5868 					      vif->link_conf[link_id]);
5869 		if (WARN_ON(!link_conf))
5870 			return;
5871 		if (link_conf->csa_active && mvmvif->csa_blocks_tx)
5872 			drop = true;
5873 	}
5874 
5875 	/* Make sure we're done with the deferred traffic before flushing */
5876 	flush_work(&mvm->add_stream_wk);
5877 
5878 	mutex_lock(&mvm->mutex);
5879 
5880 	/* flush the AP-station and all TDLS peers */
5881 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
5882 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
5883 						lockdep_is_held(&mvm->mutex));
5884 		if (IS_ERR_OR_NULL(sta))
5885 			continue;
5886 
5887 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
5888 		if (mvmsta->vif != vif)
5889 			continue;
5890 
5891 		if (sta == mvmvif->ap_sta) {
5892 			if (ap_sta_done)
5893 				continue;
5894 			ap_sta_done = true;
5895 		}
5896 
5897 		if (drop) {
5898 			if (iwl_mvm_flush_sta(mvm, mvmsta->deflink.sta_id,
5899 					      mvmsta->tfd_queue_msk))
5900 				IWL_ERR(mvm, "flush request fail\n");
5901 		} else {
5902 			if (iwl_mvm_has_new_tx_api(mvm))
5903 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
5904 			else /* only used for !iwl_mvm_has_new_tx_api() below */
5905 				msk |= mvmsta->tfd_queue_msk;
5906 		}
5907 	}
5908 
5909 	mutex_unlock(&mvm->mutex);
5910 
5911 	/* this can take a while, and we may need/want other operations
5912 	 * to succeed while doing this, so do it without the mutex held
5913 	 */
5914 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
5915 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
5916 }
5917 
5918 void iwl_mvm_mac_flush_sta(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
5919 			   struct ieee80211_sta *sta)
5920 {
5921 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5922 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5923 	struct iwl_mvm_link_sta *mvm_link_sta;
5924 	struct ieee80211_link_sta *link_sta;
5925 	int link_id;
5926 
5927 	mutex_lock(&mvm->mutex);
5928 	for_each_sta_active_link(vif, sta, link_sta, link_id) {
5929 		mvm_link_sta = rcu_dereference_protected(mvmsta->link[link_id],
5930 							 lockdep_is_held(&mvm->mutex));
5931 		if (!mvm_link_sta)
5932 			continue;
5933 
5934 		if (iwl_mvm_flush_sta(mvm, mvm_link_sta->sta_id,
5935 				      mvmsta->tfd_queue_msk))
5936 			IWL_ERR(mvm, "flush request fail\n");
5937 	}
5938 	mutex_unlock(&mvm->mutex);
5939 }
5940 
5941 static int iwl_mvm_mac_get_acs_survey(struct iwl_mvm *mvm, int idx,
5942 				      struct survey_info *survey)
5943 {
5944 	int chan_idx;
5945 	enum nl80211_band band;
5946 	int ret;
5947 
5948 	mutex_lock(&mvm->mutex);
5949 
5950 	if (!mvm->acs_survey) {
5951 		ret = -ENOENT;
5952 		goto out;
5953 	}
5954 
5955 	/* Find and return the next entry that has a non-zero active time */
5956 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
5957 		struct ieee80211_supported_band *sband =
5958 			mvm->hw->wiphy->bands[band];
5959 
5960 		if (!sband)
5961 			continue;
5962 
5963 		for (chan_idx = 0; chan_idx < sband->n_channels; chan_idx++) {
5964 			struct iwl_mvm_acs_survey_channel *info =
5965 				&mvm->acs_survey->bands[band][chan_idx];
5966 
5967 			if (!info->time)
5968 				continue;
5969 
5970 			/* Found (the next) channel to report */
5971 			survey->channel = &sband->channels[chan_idx];
5972 			survey->filled = SURVEY_INFO_TIME |
5973 					 SURVEY_INFO_TIME_BUSY |
5974 					 SURVEY_INFO_TIME_RX |
5975 					 SURVEY_INFO_TIME_TX;
5976 			survey->time = info->time;
5977 			survey->time_busy = info->time_busy;
5978 			survey->time_rx = info->time_rx;
5979 			survey->time_tx = info->time_tx;
5980 			survey->noise = info->noise;
5981 			if (survey->noise < 0)
5982 				survey->filled |= SURVEY_INFO_NOISE_DBM;
5983 
5984 			/* Clear time so that channel is only reported once */
5985 			info->time = 0;
5986 
5987 			ret = 0;
5988 			goto out;
5989 		}
5990 	}
5991 
5992 	ret = -ENOENT;
5993 
5994 out:
5995 	mutex_unlock(&mvm->mutex);
5996 
5997 	return ret;
5998 }
5999 
6000 int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
6001 			   struct survey_info *survey)
6002 {
6003 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6004 	int ret = 0;
6005 	u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
6006 					   WIDE_ID(SYSTEM_GROUP,
6007 						   SYSTEM_STATISTICS_CMD),
6008 					   IWL_FW_CMD_VER_UNKNOWN);
6009 
6010 	memset(survey, 0, sizeof(*survey));
6011 
6012 	if (!fw_has_capa(&mvm->fw->ucode_capa,
6013 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
6014 		return -ENOENT;
6015 
6016 	/*
6017 	 * Return the beacon stats at index zero and pass on following indices
6018 	 * to the function returning the full survey, most likely for ACS
6019 	 * (Automatic Channel Selection).
6020 	 */
6021 	if (idx > 0)
6022 		return iwl_mvm_mac_get_acs_survey(mvm, idx - 1, survey);
6023 
6024 	mutex_lock(&mvm->mutex);
6025 
6026 	if (iwl_mvm_firmware_running(mvm)) {
6027 		ret = iwl_mvm_request_statistics(mvm, false);
6028 		if (ret)
6029 			goto out;
6030 	}
6031 
6032 	survey->filled = SURVEY_INFO_TIME_RX |
6033 			 SURVEY_INFO_TIME_TX;
6034 
6035 	survey->time_rx = mvm->accu_radio_stats.rx_time +
6036 			  mvm->radio_stats.rx_time;
6037 	do_div(survey->time_rx, USEC_PER_MSEC);
6038 
6039 	survey->time_tx = mvm->accu_radio_stats.tx_time +
6040 			  mvm->radio_stats.tx_time;
6041 	do_div(survey->time_tx, USEC_PER_MSEC);
6042 
6043 	/* the new fw api doesn't support the following fields */
6044 	if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
6045 		goto out;
6046 
6047 	survey->filled |= SURVEY_INFO_TIME |
6048 			  SURVEY_INFO_TIME_SCAN;
6049 	survey->time = mvm->accu_radio_stats.on_time_rf +
6050 		       mvm->radio_stats.on_time_rf;
6051 	do_div(survey->time, USEC_PER_MSEC);
6052 
6053 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
6054 			    mvm->radio_stats.on_time_scan;
6055 	do_div(survey->time_scan, USEC_PER_MSEC);
6056 
6057  out:
6058 	mutex_unlock(&mvm->mutex);
6059 	return ret;
6060 }
6061 
6062 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
6063 {
6064 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
6065 	u32 gi_ltf;
6066 
6067 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
6068 	case RATE_MCS_CHAN_WIDTH_20:
6069 		rinfo->bw = RATE_INFO_BW_20;
6070 		break;
6071 	case RATE_MCS_CHAN_WIDTH_40:
6072 		rinfo->bw = RATE_INFO_BW_40;
6073 		break;
6074 	case RATE_MCS_CHAN_WIDTH_80:
6075 		rinfo->bw = RATE_INFO_BW_80;
6076 		break;
6077 	case RATE_MCS_CHAN_WIDTH_160:
6078 		rinfo->bw = RATE_INFO_BW_160;
6079 		break;
6080 	case RATE_MCS_CHAN_WIDTH_320:
6081 		rinfo->bw = RATE_INFO_BW_320;
6082 		break;
6083 	}
6084 
6085 	if (format == RATE_MCS_CCK_MSK ||
6086 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
6087 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
6088 
6089 		/* add the offset needed to get to the legacy ofdm indices */
6090 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
6091 			rate += IWL_FIRST_OFDM_RATE;
6092 
6093 		switch (rate) {
6094 		case IWL_RATE_1M_INDEX:
6095 			rinfo->legacy = 10;
6096 			break;
6097 		case IWL_RATE_2M_INDEX:
6098 			rinfo->legacy = 20;
6099 			break;
6100 		case IWL_RATE_5M_INDEX:
6101 			rinfo->legacy = 55;
6102 			break;
6103 		case IWL_RATE_11M_INDEX:
6104 			rinfo->legacy = 110;
6105 			break;
6106 		case IWL_RATE_6M_INDEX:
6107 			rinfo->legacy = 60;
6108 			break;
6109 		case IWL_RATE_9M_INDEX:
6110 			rinfo->legacy = 90;
6111 			break;
6112 		case IWL_RATE_12M_INDEX:
6113 			rinfo->legacy = 120;
6114 			break;
6115 		case IWL_RATE_18M_INDEX:
6116 			rinfo->legacy = 180;
6117 			break;
6118 		case IWL_RATE_24M_INDEX:
6119 			rinfo->legacy = 240;
6120 			break;
6121 		case IWL_RATE_36M_INDEX:
6122 			rinfo->legacy = 360;
6123 			break;
6124 		case IWL_RATE_48M_INDEX:
6125 			rinfo->legacy = 480;
6126 			break;
6127 		case IWL_RATE_54M_INDEX:
6128 			rinfo->legacy = 540;
6129 		}
6130 		return;
6131 	}
6132 
6133 	rinfo->nss = u32_get_bits(rate_n_flags,
6134 				  RATE_MCS_NSS_MSK) + 1;
6135 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
6136 		RATE_HT_MCS_INDEX(rate_n_flags) :
6137 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
6138 
6139 	if (rate_n_flags & RATE_MCS_SGI_MSK)
6140 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
6141 
6142 	switch (format) {
6143 	case RATE_MCS_EHT_MSK:
6144 		/* TODO: GI/LTF/RU. How does the firmware encode them? */
6145 		rinfo->flags |= RATE_INFO_FLAGS_EHT_MCS;
6146 		break;
6147 	case RATE_MCS_HE_MSK:
6148 		gi_ltf = u32_get_bits(rate_n_flags, RATE_MCS_HE_GI_LTF_MSK);
6149 
6150 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
6151 
6152 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
6153 			rinfo->bw = RATE_INFO_BW_HE_RU;
6154 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
6155 		}
6156 
6157 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
6158 		case RATE_MCS_HE_TYPE_SU:
6159 		case RATE_MCS_HE_TYPE_EXT_SU:
6160 			if (gi_ltf == 0 || gi_ltf == 1)
6161 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6162 			else if (gi_ltf == 2)
6163 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6164 			else if (gi_ltf == 3)
6165 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6166 			else
6167 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6168 			break;
6169 		case RATE_MCS_HE_TYPE_MU:
6170 			if (gi_ltf == 0 || gi_ltf == 1)
6171 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
6172 			else if (gi_ltf == 2)
6173 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6174 			else
6175 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6176 			break;
6177 		case RATE_MCS_HE_TYPE_TRIG:
6178 			if (gi_ltf == 0 || gi_ltf == 1)
6179 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
6180 			else
6181 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
6182 			break;
6183 		}
6184 
6185 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
6186 			rinfo->he_dcm = 1;
6187 		break;
6188 	case RATE_MCS_HT_MSK:
6189 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
6190 		break;
6191 	case RATE_MCS_VHT_MSK:
6192 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
6193 		break;
6194 	}
6195 }
6196 
6197 void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
6198 				struct ieee80211_vif *vif,
6199 				struct ieee80211_sta *sta,
6200 				struct station_info *sinfo)
6201 {
6202 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6203 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6204 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
6205 	int i;
6206 
6207 	if (mvmsta->deflink.avg_energy) {
6208 		sinfo->signal_avg = -(s8)mvmsta->deflink.avg_energy;
6209 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
6210 	}
6211 
6212 	if (iwl_mvm_has_tlc_offload(mvm)) {
6213 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->deflink.lq_sta.rs_fw;
6214 
6215 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
6216 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
6217 	}
6218 
6219 	/* if beacon filtering isn't on mac80211 does it anyway */
6220 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
6221 		return;
6222 
6223 	if (!vif->cfg.assoc)
6224 		return;
6225 
6226 	mutex_lock(&mvm->mutex);
6227 
6228 	if (mvmvif->deflink.ap_sta_id != mvmsta->deflink.sta_id)
6229 		goto unlock;
6230 
6231 	if (iwl_mvm_request_statistics(mvm, false))
6232 		goto unlock;
6233 
6234 	sinfo->rx_beacon = 0;
6235 	for_each_mvm_vif_valid_link(mvmvif, i)
6236 		sinfo->rx_beacon += mvmvif->link[i]->beacon_stats.num_beacons +
6237 			mvmvif->link[i]->beacon_stats.accu_num_beacons;
6238 
6239 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
6240 	if (mvmvif->deflink.beacon_stats.avg_signal) {
6241 		/* firmware only reports a value after RXing a few beacons */
6242 		sinfo->rx_beacon_signal_avg =
6243 			mvmvif->deflink.beacon_stats.avg_signal;
6244 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
6245 	}
6246  unlock:
6247 	mutex_unlock(&mvm->mutex);
6248 }
6249 
6250 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
6251 					    struct ieee80211_vif *vif,
6252 					    const  struct ieee80211_mlme_event *mlme)
6253 {
6254 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
6255 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
6256 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6257 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
6258 				       NULL);
6259 		return;
6260 	}
6261 
6262 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
6263 		iwl_dbg_tlv_time_point(&mvm->fwrt,
6264 				       IWL_FW_INI_TIME_POINT_DEASSOC,
6265 				       NULL);
6266 		return;
6267 	}
6268 }
6269 
6270 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
6271 					struct ieee80211_vif *vif,
6272 					const struct ieee80211_event *event)
6273 {
6274 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
6275 	do {								\
6276 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
6277 			break;						\
6278 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
6279 	} while (0)
6280 
6281 	struct iwl_fw_dbg_trigger_tlv *trig;
6282 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
6283 
6284 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
6285 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
6286 		return;
6287 	}
6288 
6289 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6290 				     FW_DBG_TRIGGER_MLME);
6291 	if (!trig)
6292 		return;
6293 
6294 	trig_mlme = (void *)trig->data;
6295 
6296 	if (event->u.mlme.data == ASSOC_EVENT) {
6297 		if (event->u.mlme.status == MLME_DENIED)
6298 			CHECK_MLME_TRIGGER(stop_assoc_denied,
6299 					   "DENIED ASSOC: reason %d",
6300 					    event->u.mlme.reason);
6301 		else if (event->u.mlme.status == MLME_TIMEOUT)
6302 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
6303 					   "ASSOC TIMEOUT");
6304 	} else if (event->u.mlme.data == AUTH_EVENT) {
6305 		if (event->u.mlme.status == MLME_DENIED)
6306 			CHECK_MLME_TRIGGER(stop_auth_denied,
6307 					   "DENIED AUTH: reason %d",
6308 					   event->u.mlme.reason);
6309 		else if (event->u.mlme.status == MLME_TIMEOUT)
6310 			CHECK_MLME_TRIGGER(stop_auth_timeout,
6311 					   "AUTH TIMEOUT");
6312 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
6313 		CHECK_MLME_TRIGGER(stop_rx_deauth,
6314 				   "DEAUTH RX %d", event->u.mlme.reason);
6315 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
6316 		CHECK_MLME_TRIGGER(stop_tx_deauth,
6317 				   "DEAUTH TX %d", event->u.mlme.reason);
6318 	}
6319 #undef CHECK_MLME_TRIGGER
6320 }
6321 
6322 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
6323 					  struct ieee80211_vif *vif,
6324 					  const struct ieee80211_event *event)
6325 {
6326 	struct iwl_fw_dbg_trigger_tlv *trig;
6327 	struct iwl_fw_dbg_trigger_ba *ba_trig;
6328 
6329 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
6330 				     FW_DBG_TRIGGER_BA);
6331 	if (!trig)
6332 		return;
6333 
6334 	ba_trig = (void *)trig->data;
6335 
6336 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
6337 		return;
6338 
6339 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
6340 				"BAR received from %pM, tid %d, ssn %d",
6341 				event->u.ba.sta->addr, event->u.ba.tid,
6342 				event->u.ba.ssn);
6343 }
6344 
6345 void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
6346 				struct ieee80211_vif *vif,
6347 				const struct ieee80211_event *event)
6348 {
6349 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6350 
6351 	switch (event->type) {
6352 	case MLME_EVENT:
6353 		iwl_mvm_event_mlme_callback(mvm, vif, event);
6354 		break;
6355 	case BAR_RX_EVENT:
6356 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
6357 		break;
6358 	case BA_FRAME_TIMEOUT:
6359 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
6360 						     event->u.ba.tid);
6361 		break;
6362 	default:
6363 		break;
6364 	}
6365 }
6366 
6367 #define SYNC_RX_QUEUE_TIMEOUT (HZ)
6368 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
6369 				     enum iwl_mvm_rxq_notif_type type,
6370 				     bool sync,
6371 				     const void *data, u32 size)
6372 {
6373 	struct {
6374 		struct iwl_rxq_sync_cmd cmd;
6375 		struct iwl_mvm_internal_rxq_notif notif;
6376 	} __packed cmd = {
6377 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
6378 		.cmd.count =
6379 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
6380 				    size),
6381 		.notif.type = type,
6382 		.notif.sync = sync,
6383 	};
6384 	struct iwl_host_cmd hcmd = {
6385 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
6386 		.data[0] = &cmd,
6387 		.len[0] = sizeof(cmd),
6388 		.data[1] = data,
6389 		.len[1] = size,
6390 		.flags = CMD_SEND_IN_RFKILL | (sync ? 0 : CMD_ASYNC),
6391 	};
6392 	int ret;
6393 
6394 	/* size must be a multiple of DWORD */
6395 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
6396 		return;
6397 
6398 	if (!iwl_mvm_has_new_rx_api(mvm))
6399 		return;
6400 
6401 	if (sync) {
6402 		cmd.notif.cookie = mvm->queue_sync_cookie;
6403 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
6404 	}
6405 
6406 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
6407 	if (ret) {
6408 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
6409 		goto out;
6410 	}
6411 
6412 	if (sync) {
6413 		lockdep_assert_held(&mvm->mutex);
6414 		ret = wait_event_timeout(mvm->rx_sync_waitq,
6415 					 READ_ONCE(mvm->queue_sync_state) == 0,
6416 					 SYNC_RX_QUEUE_TIMEOUT);
6417 		WARN_ONCE(!ret, "queue sync: failed to sync, state is 0x%lx, cookie %d\n",
6418 			  mvm->queue_sync_state,
6419 			  mvm->queue_sync_cookie);
6420 	}
6421 
6422 out:
6423 	if (sync) {
6424 		mvm->queue_sync_state = 0;
6425 		mvm->queue_sync_cookie++;
6426 	}
6427 }
6428 
6429 void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
6430 {
6431 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6432 
6433 	mutex_lock(&mvm->mutex);
6434 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
6435 	mutex_unlock(&mvm->mutex);
6436 }
6437 
6438 int
6439 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
6440 				    struct ieee80211_vif *vif,
6441 				    struct cfg80211_ftm_responder_stats *stats)
6442 {
6443 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6444 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6445 
6446 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
6447 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
6448 		return -EINVAL;
6449 
6450 	mutex_lock(&mvm->mutex);
6451 	*stats = mvm->ftm_resp_stats;
6452 	mutex_unlock(&mvm->mutex);
6453 
6454 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
6455 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
6456 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
6457 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
6458 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
6459 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
6460 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
6461 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
6462 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
6463 
6464 	return 0;
6465 }
6466 
6467 int iwl_mvm_start_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6468 		       struct cfg80211_pmsr_request *request)
6469 {
6470 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6471 	int ret;
6472 
6473 	mutex_lock(&mvm->mutex);
6474 	ret = iwl_mvm_ftm_start(mvm, vif, request);
6475 	mutex_unlock(&mvm->mutex);
6476 
6477 	return ret;
6478 }
6479 
6480 void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
6481 			struct cfg80211_pmsr_request *request)
6482 {
6483 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6484 
6485 	mutex_lock(&mvm->mutex);
6486 	iwl_mvm_ftm_abort(mvm, request);
6487 	mutex_unlock(&mvm->mutex);
6488 }
6489 
6490 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
6491 {
6492 	u8 protocol = ip_hdr(skb)->protocol;
6493 
6494 	if (!IS_ENABLED(CONFIG_INET))
6495 		return false;
6496 
6497 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
6498 }
6499 
6500 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
6501 				      struct sk_buff *head,
6502 				      struct sk_buff *skb)
6503 {
6504 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6505 
6506 	/* For now don't aggregate IPv6 in AMSDU */
6507 	if (skb->protocol != htons(ETH_P_IP))
6508 		return false;
6509 
6510 	if (!iwl_mvm_is_csum_supported(mvm))
6511 		return true;
6512 
6513 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
6514 }
6515 
6516 int iwl_mvm_set_hw_timestamp(struct ieee80211_hw *hw,
6517 			     struct ieee80211_vif *vif,
6518 			     struct cfg80211_set_hw_timestamp *hwts)
6519 {
6520 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6521 	u32 protocols = 0;
6522 	int ret;
6523 
6524 	/* HW timestamping is only supported for a specific station */
6525 	if (!hwts->macaddr)
6526 		return -EOPNOTSUPP;
6527 
6528 	if (hwts->enable)
6529 		protocols =
6530 			IWL_TIME_SYNC_PROTOCOL_TM | IWL_TIME_SYNC_PROTOCOL_FTM;
6531 
6532 	mutex_lock(&mvm->mutex);
6533 	ret = iwl_mvm_time_sync_config(mvm, hwts->macaddr, protocols);
6534 	mutex_unlock(&mvm->mutex);
6535 
6536 	return ret;
6537 }
6538 
6539 const struct ieee80211_ops iwl_mvm_hw_ops = {
6540 	.tx = iwl_mvm_mac_tx,
6541 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
6542 	.ampdu_action = iwl_mvm_mac_ampdu_action,
6543 	.get_antenna = iwl_mvm_op_get_antenna,
6544 	.set_antenna = iwl_mvm_op_set_antenna,
6545 	.start = iwl_mvm_mac_start,
6546 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
6547 	.stop = iwl_mvm_mac_stop,
6548 	.add_interface = iwl_mvm_mac_add_interface,
6549 	.remove_interface = iwl_mvm_mac_remove_interface,
6550 	.config = iwl_mvm_mac_config,
6551 	.prepare_multicast = iwl_mvm_prepare_multicast,
6552 	.configure_filter = iwl_mvm_configure_filter,
6553 	.config_iface_filter = iwl_mvm_config_iface_filter,
6554 	.bss_info_changed = iwl_mvm_bss_info_changed,
6555 	.hw_scan = iwl_mvm_mac_hw_scan,
6556 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
6557 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
6558 	.sta_state = iwl_mvm_mac_sta_state,
6559 	.sta_notify = iwl_mvm_mac_sta_notify,
6560 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
6561 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
6562 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
6563 	.sta_rc_update = iwl_mvm_sta_rc_update,
6564 	.conf_tx = iwl_mvm_mac_conf_tx,
6565 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
6566 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
6567 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
6568 	.flush = iwl_mvm_mac_flush,
6569 	.flush_sta = iwl_mvm_mac_flush_sta,
6570 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
6571 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
6572 	.set_key = iwl_mvm_mac_set_key,
6573 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
6574 	.remain_on_channel = iwl_mvm_roc,
6575 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
6576 	.add_chanctx = iwl_mvm_add_chanctx,
6577 	.remove_chanctx = iwl_mvm_remove_chanctx,
6578 	.change_chanctx = iwl_mvm_change_chanctx,
6579 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
6580 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
6581 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
6582 
6583 	.start_ap = iwl_mvm_start_ap,
6584 	.stop_ap = iwl_mvm_stop_ap,
6585 	.join_ibss = iwl_mvm_start_ibss,
6586 	.leave_ibss = iwl_mvm_stop_ibss,
6587 
6588 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
6589 
6590 	.set_tim = iwl_mvm_set_tim,
6591 
6592 	.channel_switch = iwl_mvm_channel_switch,
6593 	.pre_channel_switch = iwl_mvm_mac_pre_channel_switch,
6594 	.post_channel_switch = iwl_mvm_post_channel_switch,
6595 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
6596 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
6597 
6598 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
6599 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
6600 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
6601 
6602 	.event_callback = iwl_mvm_mac_event_callback,
6603 
6604 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
6605 
6606 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
6607 
6608 #ifdef CONFIG_PM_SLEEP
6609 	/* look at d3.c */
6610 	.suspend = iwl_mvm_suspend,
6611 	.resume = iwl_mvm_resume,
6612 	.set_wakeup = iwl_mvm_set_wakeup,
6613 	.set_rekey_data = iwl_mvm_set_rekey_data,
6614 #if IS_ENABLED(CONFIG_IPV6)
6615 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
6616 #endif
6617 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
6618 #endif
6619 	.get_survey = iwl_mvm_mac_get_survey,
6620 	.sta_statistics = iwl_mvm_mac_sta_statistics,
6621 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
6622 	.start_pmsr = iwl_mvm_start_pmsr,
6623 	.abort_pmsr = iwl_mvm_abort_pmsr,
6624 
6625 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
6626 #ifdef CONFIG_IWLWIFI_DEBUGFS
6627 	.vif_add_debugfs = iwl_mvm_vif_add_debugfs,
6628 	.link_sta_add_debugfs = iwl_mvm_link_sta_add_debugfs,
6629 #endif
6630 	.set_hw_timestamp = iwl_mvm_set_hw_timestamp,
6631 };
6632