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