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