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