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