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