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