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