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