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