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