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