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