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