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