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