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