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