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