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