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