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