xref: /freebsd/sys/contrib/dev/iwlwifi/mvm/mac80211.c (revision 681ce946f33e75c590e97c53076e86dff1fe8f4a)
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 
1678 	lockdep_assert_held(&mvm->mutex);
1679 
1680 	if (WARN_ON_ONCE(!mvm->mcast_filter_cmd))
1681 		return;
1682 
1683 	ieee80211_iterate_active_interfaces_atomic(
1684 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1685 		iwl_mvm_mc_iface_iterator, &iter_data);
1686 }
1687 
1688 static u64 iwl_mvm_prepare_multicast(struct ieee80211_hw *hw,
1689 				     struct netdev_hw_addr_list *mc_list)
1690 {
1691 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1692 	struct iwl_mcast_filter_cmd *cmd;
1693 	struct netdev_hw_addr *addr;
1694 	int addr_count;
1695 	bool pass_all;
1696 	int len;
1697 
1698 	addr_count = netdev_hw_addr_list_count(mc_list);
1699 	pass_all = addr_count > MAX_MCAST_FILTERING_ADDRESSES ||
1700 		   IWL_MVM_FW_MCAST_FILTER_PASS_ALL;
1701 	if (pass_all)
1702 		addr_count = 0;
1703 
1704 	len = roundup(sizeof(*cmd) + addr_count * ETH_ALEN, 4);
1705 	cmd = kzalloc(len, GFP_ATOMIC);
1706 	if (!cmd)
1707 		return 0;
1708 
1709 	if (pass_all) {
1710 		cmd->pass_all = 1;
1711 		return (u64)(uintptr_t)cmd;
1712 	}
1713 
1714 	netdev_hw_addr_list_for_each(addr, mc_list) {
1715 #if defined(__linux__)
1716 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %pM\n",
1717 				   cmd->count, addr->addr);
1718 #elif defined(__FreeBSD__)
1719 		IWL_DEBUG_MAC80211(mvm, "mcast addr (%d): %6D\n",
1720 				   cmd->count, addr->addr, ":");
1721 #endif
1722 		memcpy(&cmd->addr_list[cmd->count * ETH_ALEN],
1723 		       addr->addr, ETH_ALEN);
1724 		cmd->count++;
1725 	}
1726 
1727 	return (u64)(uintptr_t)cmd;
1728 }
1729 
1730 static void iwl_mvm_configure_filter(struct ieee80211_hw *hw,
1731 				     unsigned int changed_flags,
1732 				     unsigned int *total_flags,
1733 				     u64 multicast)
1734 {
1735 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1736 	struct iwl_mcast_filter_cmd *cmd = (void *)(uintptr_t)multicast;
1737 
1738 	mutex_lock(&mvm->mutex);
1739 
1740 	/* replace previous configuration */
1741 	kfree(mvm->mcast_filter_cmd);
1742 	mvm->mcast_filter_cmd = cmd;
1743 
1744 	if (!cmd)
1745 		goto out;
1746 
1747 	if (changed_flags & FIF_ALLMULTI)
1748 		cmd->pass_all = !!(*total_flags & FIF_ALLMULTI);
1749 
1750 	if (cmd->pass_all)
1751 		cmd->count = 0;
1752 
1753 	iwl_mvm_recalc_multicast(mvm);
1754 out:
1755 	mutex_unlock(&mvm->mutex);
1756 	*total_flags = 0;
1757 }
1758 
1759 static void iwl_mvm_config_iface_filter(struct ieee80211_hw *hw,
1760 					struct ieee80211_vif *vif,
1761 					unsigned int filter_flags,
1762 					unsigned int changed_flags)
1763 {
1764 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
1765 
1766 	/* We support only filter for probe requests */
1767 	if (!(changed_flags & FIF_PROBE_REQ))
1768 		return;
1769 
1770 	/* Supported only for p2p client interfaces */
1771 	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc ||
1772 	    !vif->p2p)
1773 		return;
1774 
1775 	mutex_lock(&mvm->mutex);
1776 	iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
1777 	mutex_unlock(&mvm->mutex);
1778 }
1779 
1780 #ifdef CONFIG_IWLWIFI_BCAST_FILTERING
1781 struct iwl_bcast_iter_data {
1782 	struct iwl_mvm *mvm;
1783 	struct iwl_bcast_filter_cmd *cmd;
1784 	u8 current_filter;
1785 };
1786 
1787 static void
1788 iwl_mvm_set_bcast_filter(struct ieee80211_vif *vif,
1789 			 const struct iwl_fw_bcast_filter *in_filter,
1790 			 struct iwl_fw_bcast_filter *out_filter)
1791 {
1792 	struct iwl_fw_bcast_filter_attr *attr;
1793 	int i;
1794 
1795 	memcpy(out_filter, in_filter, sizeof(*out_filter));
1796 
1797 	for (i = 0; i < ARRAY_SIZE(out_filter->attrs); i++) {
1798 		attr = &out_filter->attrs[i];
1799 
1800 		if (!attr->mask)
1801 			break;
1802 
1803 		switch (attr->reserved1) {
1804 		case cpu_to_le16(BC_FILTER_MAGIC_IP):
1805 			if (vif->bss_conf.arp_addr_cnt != 1) {
1806 				attr->mask = 0;
1807 				continue;
1808 			}
1809 
1810 			attr->val = vif->bss_conf.arp_addr_list[0];
1811 			break;
1812 		case cpu_to_le16(BC_FILTER_MAGIC_MAC):
1813 			attr->val = *(__be32 *)&vif->addr[2];
1814 			break;
1815 		default:
1816 			break;
1817 		}
1818 		attr->reserved1 = 0;
1819 		out_filter->num_attrs++;
1820 	}
1821 }
1822 
1823 static void iwl_mvm_bcast_filter_iterator(void *_data, u8 *mac,
1824 					  struct ieee80211_vif *vif)
1825 {
1826 	struct iwl_bcast_iter_data *data = _data;
1827 	struct iwl_mvm *mvm = data->mvm;
1828 	struct iwl_bcast_filter_cmd *cmd = data->cmd;
1829 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1830 	struct iwl_fw_bcast_mac *bcast_mac;
1831 	int i;
1832 
1833 	if (WARN_ON(mvmvif->id >= ARRAY_SIZE(cmd->macs)))
1834 		return;
1835 
1836 	bcast_mac = &cmd->macs[mvmvif->id];
1837 
1838 	/*
1839 	 * enable filtering only for associated stations, but not for P2P
1840 	 * Clients
1841 	 */
1842 	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p ||
1843 	    !vif->bss_conf.assoc)
1844 		return;
1845 
1846 	bcast_mac->default_discard = 1;
1847 
1848 	/* copy all configured filters */
1849 	for (i = 0; mvm->bcast_filters[i].attrs[0].mask; i++) {
1850 		/*
1851 		 * Make sure we don't exceed our filters limit.
1852 		 * if there is still a valid filter to be configured,
1853 		 * be on the safe side and just allow bcast for this mac.
1854 		 */
1855 		if (WARN_ON_ONCE(data->current_filter >=
1856 				 ARRAY_SIZE(cmd->filters))) {
1857 			bcast_mac->default_discard = 0;
1858 			bcast_mac->attached_filters = 0;
1859 			break;
1860 		}
1861 
1862 		iwl_mvm_set_bcast_filter(vif,
1863 					 &mvm->bcast_filters[i],
1864 					 &cmd->filters[data->current_filter]);
1865 
1866 		/* skip current filter if it contains no attributes */
1867 		if (!cmd->filters[data->current_filter].num_attrs)
1868 			continue;
1869 
1870 		/* attach the filter to current mac */
1871 		bcast_mac->attached_filters |=
1872 				cpu_to_le16(BIT(data->current_filter));
1873 
1874 		data->current_filter++;
1875 	}
1876 }
1877 
1878 bool iwl_mvm_bcast_filter_build_cmd(struct iwl_mvm *mvm,
1879 				    struct iwl_bcast_filter_cmd *cmd)
1880 {
1881 	struct iwl_bcast_iter_data iter_data = {
1882 		.mvm = mvm,
1883 		.cmd = cmd,
1884 	};
1885 
1886 	if (IWL_MVM_FW_BCAST_FILTER_PASS_ALL)
1887 		return false;
1888 
1889 	memset(cmd, 0, sizeof(*cmd));
1890 	cmd->max_bcast_filters = ARRAY_SIZE(cmd->filters);
1891 	cmd->max_macs = ARRAY_SIZE(cmd->macs);
1892 
1893 #ifdef CONFIG_IWLWIFI_DEBUGFS
1894 	/* use debugfs filters/macs if override is configured */
1895 	if (mvm->dbgfs_bcast_filtering.override) {
1896 		memcpy(cmd->filters, &mvm->dbgfs_bcast_filtering.cmd.filters,
1897 		       sizeof(cmd->filters));
1898 		memcpy(cmd->macs, &mvm->dbgfs_bcast_filtering.cmd.macs,
1899 		       sizeof(cmd->macs));
1900 		return true;
1901 	}
1902 #endif
1903 
1904 	/* if no filters are configured, do nothing */
1905 	if (!mvm->bcast_filters)
1906 		return false;
1907 
1908 	/* configure and attach these filters for each associated sta vif */
1909 	ieee80211_iterate_active_interfaces(
1910 		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1911 		iwl_mvm_bcast_filter_iterator, &iter_data);
1912 
1913 	return true;
1914 }
1915 
1916 static int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1917 {
1918 	struct iwl_bcast_filter_cmd cmd;
1919 
1920 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_BCAST_FILTERING))
1921 		return 0;
1922 
1923 	if (!iwl_mvm_bcast_filter_build_cmd(mvm, &cmd))
1924 		return 0;
1925 
1926 	return iwl_mvm_send_cmd_pdu(mvm, BCAST_FILTER_CMD, 0,
1927 				    sizeof(cmd), &cmd);
1928 }
1929 #else
1930 static inline int iwl_mvm_configure_bcast_filter(struct iwl_mvm *mvm)
1931 {
1932 	return 0;
1933 }
1934 #endif
1935 
1936 static int iwl_mvm_update_mu_groups(struct iwl_mvm *mvm,
1937 				    struct ieee80211_vif *vif)
1938 {
1939 	struct iwl_mu_group_mgmt_cmd cmd = {};
1940 
1941 	memcpy(cmd.membership_status, vif->bss_conf.mu_group.membership,
1942 	       WLAN_MEMBERSHIP_LEN);
1943 	memcpy(cmd.user_position, vif->bss_conf.mu_group.position,
1944 	       WLAN_USER_POSITION_LEN);
1945 
1946 	return iwl_mvm_send_cmd_pdu(mvm,
1947 				    WIDE_ID(DATA_PATH_GROUP,
1948 					    UPDATE_MU_GROUPS_CMD),
1949 				    0, sizeof(cmd), &cmd);
1950 }
1951 
1952 static void iwl_mvm_mu_mimo_iface_iterator(void *_data, u8 *mac,
1953 					   struct ieee80211_vif *vif)
1954 {
1955 	if (vif->mu_mimo_owner) {
1956 		struct iwl_mu_group_mgmt_notif *notif = _data;
1957 
1958 		/*
1959 		 * MU-MIMO Group Id action frame is little endian. We treat
1960 		 * the data received from firmware as if it came from the
1961 		 * action frame, so no conversion is needed.
1962 		 */
1963 		ieee80211_update_mu_groups(vif,
1964 					   (u8 *)&notif->membership_status,
1965 					   (u8 *)&notif->user_position);
1966 	}
1967 }
1968 
1969 void iwl_mvm_mu_mimo_grp_notif(struct iwl_mvm *mvm,
1970 			       struct iwl_rx_cmd_buffer *rxb)
1971 {
1972 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
1973 	struct iwl_mu_group_mgmt_notif *notif = (void *)pkt->data;
1974 
1975 	ieee80211_iterate_active_interfaces_atomic(
1976 			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
1977 			iwl_mvm_mu_mimo_iface_iterator, notif);
1978 }
1979 
1980 static u8 iwl_mvm_he_get_ppe_val(u8 *ppe, u8 ppe_pos_bit)
1981 {
1982 	u8 byte_num = ppe_pos_bit / 8;
1983 	u8 bit_num = ppe_pos_bit % 8;
1984 	u8 residue_bits;
1985 	u8 res;
1986 
1987 	if (bit_num <= 5)
1988 		return (ppe[byte_num] >> bit_num) &
1989 		       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE) - 1);
1990 
1991 	/*
1992 	 * If bit_num > 5, we have to combine bits with next byte.
1993 	 * Calculate how many bits we need to take from current byte (called
1994 	 * here "residue_bits"), and add them to bits from next byte.
1995 	 */
1996 
1997 	residue_bits = 8 - bit_num;
1998 
1999 	res = (ppe[byte_num + 1] &
2000 	       (BIT(IEEE80211_PPE_THRES_INFO_PPET_SIZE - residue_bits) - 1)) <<
2001 	      residue_bits;
2002 	res += (ppe[byte_num] >> bit_num) & (BIT(residue_bits) - 1);
2003 
2004 	return res;
2005 }
2006 
2007 static void iwl_mvm_cfg_he_sta(struct iwl_mvm *mvm,
2008 			       struct ieee80211_vif *vif, u8 sta_id)
2009 {
2010 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2011 	struct iwl_he_sta_context_cmd sta_ctxt_cmd = {
2012 		.sta_id = sta_id,
2013 		.tid_limit = IWL_MAX_TID_COUNT,
2014 		.bss_color = vif->bss_conf.he_bss_color.color,
2015 		.htc_trig_based_pkt_ext = vif->bss_conf.htc_trig_based_pkt_ext,
2016 		.frame_time_rts_th =
2017 			cpu_to_le16(vif->bss_conf.frame_time_rts_th),
2018 	};
2019 	int size = fw_has_api(&mvm->fw->ucode_capa,
2020 			      IWL_UCODE_TLV_API_MBSSID_HE) ?
2021 		   sizeof(sta_ctxt_cmd) :
2022 		   sizeof(struct iwl_he_sta_context_cmd_v1);
2023 	struct ieee80211_sta *sta;
2024 	u32 flags;
2025 	int i;
2026 	const struct ieee80211_sta_he_cap *own_he_cap = NULL;
2027 	struct ieee80211_chanctx_conf *chanctx_conf;
2028 	const struct ieee80211_supported_band *sband;
2029 
2030 	rcu_read_lock();
2031 
2032 	chanctx_conf = rcu_dereference(vif->chanctx_conf);
2033 	if (WARN_ON(!chanctx_conf)) {
2034 		rcu_read_unlock();
2035 		return;
2036 	}
2037 
2038 	sband = mvm->hw->wiphy->bands[chanctx_conf->def.chan->band];
2039 	own_he_cap = ieee80211_get_he_iftype_cap(sband,
2040 						 ieee80211_vif_type_p2p(vif));
2041 
2042 	sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_ctxt_cmd.sta_id]);
2043 	if (IS_ERR_OR_NULL(sta)) {
2044 		rcu_read_unlock();
2045 		WARN(1, "Can't find STA to configure HE\n");
2046 		return;
2047 	}
2048 
2049 	if (!sta->he_cap.has_he) {
2050 		rcu_read_unlock();
2051 		return;
2052 	}
2053 
2054 	flags = 0;
2055 
2056 	/* Block 26-tone RU OFDMA transmissions */
2057 	if (mvmvif->he_ru_2mhz_block)
2058 		flags |= STA_CTXT_HE_RU_2MHZ_BLOCK;
2059 
2060 	/* HTC flags */
2061 	if (sta->he_cap.he_cap_elem.mac_cap_info[0] &
2062 	    IEEE80211_HE_MAC_CAP0_HTC_HE)
2063 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_SUPPORT);
2064 	if ((sta->he_cap.he_cap_elem.mac_cap_info[1] &
2065 	      IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION) ||
2066 	    (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2067 	      IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION)) {
2068 		u8 link_adap =
2069 			((sta->he_cap.he_cap_elem.mac_cap_info[2] &
2070 			  IEEE80211_HE_MAC_CAP2_LINK_ADAPTATION) << 1) +
2071 			 (sta->he_cap.he_cap_elem.mac_cap_info[1] &
2072 			  IEEE80211_HE_MAC_CAP1_LINK_ADAPTATION);
2073 
2074 		if (link_adap == 2)
2075 			sta_ctxt_cmd.htc_flags |=
2076 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_UNSOLICITED);
2077 		else if (link_adap == 3)
2078 			sta_ctxt_cmd.htc_flags |=
2079 				cpu_to_le32(IWL_HE_HTC_LINK_ADAP_BOTH);
2080 	}
2081 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] & IEEE80211_HE_MAC_CAP2_BSR)
2082 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BSR_SUPP);
2083 	if (sta->he_cap.he_cap_elem.mac_cap_info[3] &
2084 	    IEEE80211_HE_MAC_CAP3_OMI_CONTROL)
2085 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_OMI_SUPP);
2086 	if (sta->he_cap.he_cap_elem.mac_cap_info[4] & IEEE80211_HE_MAC_CAP4_BQR)
2087 		sta_ctxt_cmd.htc_flags |= cpu_to_le32(IWL_HE_HTC_BQR_SUPP);
2088 
2089 	/*
2090 	 * Initialize the PPE thresholds to "None" (7), as described in Table
2091 	 * 9-262ac of 80211.ax/D3.0.
2092 	 */
2093 	memset(&sta_ctxt_cmd.pkt_ext, 7, sizeof(sta_ctxt_cmd.pkt_ext));
2094 
2095 	/* If PPE Thresholds exist, parse them into a FW-familiar format. */
2096 	if (sta->he_cap.he_cap_elem.phy_cap_info[6] &
2097 	    IEEE80211_HE_PHY_CAP6_PPE_THRESHOLD_PRESENT) {
2098 		u8 nss = (sta->he_cap.ppe_thres[0] &
2099 			  IEEE80211_PPE_THRES_NSS_MASK) + 1;
2100 		u8 ru_index_bitmap =
2101 			(sta->he_cap.ppe_thres[0] &
2102 			 IEEE80211_PPE_THRES_RU_INDEX_BITMASK_MASK) >>
2103 			IEEE80211_PPE_THRES_RU_INDEX_BITMASK_POS;
2104 		u8 *ppe = &sta->he_cap.ppe_thres[0];
2105 		u8 ppe_pos_bit = 7; /* Starting after PPE header */
2106 
2107 		/*
2108 		 * FW currently supports only nss == MAX_HE_SUPP_NSS
2109 		 *
2110 		 * If nss > MAX: we can ignore values we don't support
2111 		 * If nss < MAX: we can set zeros in other streams
2112 		 */
2113 		if (nss > MAX_HE_SUPP_NSS) {
2114 			IWL_INFO(mvm, "Got NSS = %d - trimming to %d\n", nss,
2115 				 MAX_HE_SUPP_NSS);
2116 			nss = MAX_HE_SUPP_NSS;
2117 		}
2118 
2119 		for (i = 0; i < nss; i++) {
2120 			u8 ru_index_tmp = ru_index_bitmap << 1;
2121 			u8 bw;
2122 
2123 			for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX; bw++) {
2124 				ru_index_tmp >>= 1;
2125 				if (!(ru_index_tmp & 1))
2126 					continue;
2127 
2128 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][1] =
2129 					iwl_mvm_he_get_ppe_val(ppe,
2130 							       ppe_pos_bit);
2131 				ppe_pos_bit +=
2132 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2133 				sta_ctxt_cmd.pkt_ext.pkt_ext_qam_th[i][bw][0] =
2134 					iwl_mvm_he_get_ppe_val(ppe,
2135 							       ppe_pos_bit);
2136 				ppe_pos_bit +=
2137 					IEEE80211_PPE_THRES_INFO_PPET_SIZE;
2138 			}
2139 		}
2140 
2141 		flags |= STA_CTXT_HE_PACKET_EXT;
2142 	} else if ((sta->he_cap.he_cap_elem.phy_cap_info[9] &
2143 		    IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) !=
2144 		  IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_RESERVED) {
2145 		int low_th = -1;
2146 		int high_th = -1;
2147 
2148 		/* Take the PPE thresholds from the nominal padding info */
2149 		switch (sta->he_cap.he_cap_elem.phy_cap_info[9] &
2150 			IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_MASK) {
2151 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_0US:
2152 			low_th = IWL_HE_PKT_EXT_NONE;
2153 			high_th = IWL_HE_PKT_EXT_NONE;
2154 			break;
2155 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_8US:
2156 			low_th = IWL_HE_PKT_EXT_BPSK;
2157 			high_th = IWL_HE_PKT_EXT_NONE;
2158 			break;
2159 		case IEEE80211_HE_PHY_CAP9_NOMIMAL_PKT_PADDING_16US:
2160 			low_th = IWL_HE_PKT_EXT_NONE;
2161 			high_th = IWL_HE_PKT_EXT_BPSK;
2162 			break;
2163 		}
2164 
2165 		/* Set the PPE thresholds accordingly */
2166 		if (low_th >= 0 && high_th >= 0) {
2167 			struct iwl_he_pkt_ext *pkt_ext =
2168 				(struct iwl_he_pkt_ext *)&sta_ctxt_cmd.pkt_ext;
2169 
2170 			for (i = 0; i < MAX_HE_SUPP_NSS; i++) {
2171 				u8 bw;
2172 
2173 				for (bw = 0; bw < MAX_HE_CHANNEL_BW_INDX;
2174 				     bw++) {
2175 					pkt_ext->pkt_ext_qam_th[i][bw][0] =
2176 						low_th;
2177 					pkt_ext->pkt_ext_qam_th[i][bw][1] =
2178 						high_th;
2179 				}
2180 			}
2181 
2182 			flags |= STA_CTXT_HE_PACKET_EXT;
2183 		}
2184 	}
2185 
2186 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2187 	    IEEE80211_HE_MAC_CAP2_32BIT_BA_BITMAP)
2188 		flags |= STA_CTXT_HE_32BIT_BA_BITMAP;
2189 
2190 	if (sta->he_cap.he_cap_elem.mac_cap_info[2] &
2191 	    IEEE80211_HE_MAC_CAP2_ACK_EN)
2192 		flags |= STA_CTXT_HE_ACK_ENABLED;
2193 
2194 	rcu_read_unlock();
2195 
2196 	/* Mark MU EDCA as enabled, unless none detected on some AC */
2197 	flags |= STA_CTXT_HE_MU_EDCA_CW;
2198 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2199 		struct ieee80211_he_mu_edca_param_ac_rec *mu_edca =
2200 			&mvmvif->queue_params[i].mu_edca_param_rec;
2201 		u8 ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
2202 
2203 		if (!mvmvif->queue_params[i].mu_edca) {
2204 			flags &= ~STA_CTXT_HE_MU_EDCA_CW;
2205 			break;
2206 		}
2207 
2208 		sta_ctxt_cmd.trig_based_txf[ac].cwmin =
2209 			cpu_to_le16(mu_edca->ecw_min_max & 0xf);
2210 		sta_ctxt_cmd.trig_based_txf[ac].cwmax =
2211 			cpu_to_le16((mu_edca->ecw_min_max & 0xf0) >> 4);
2212 		sta_ctxt_cmd.trig_based_txf[ac].aifsn =
2213 			cpu_to_le16(mu_edca->aifsn);
2214 		sta_ctxt_cmd.trig_based_txf[ac].mu_time =
2215 			cpu_to_le16(mu_edca->mu_edca_timer);
2216 	}
2217 
2218 
2219 	if (vif->bss_conf.uora_exists) {
2220 		flags |= STA_CTXT_HE_TRIG_RND_ALLOC;
2221 
2222 		sta_ctxt_cmd.rand_alloc_ecwmin =
2223 			vif->bss_conf.uora_ocw_range & 0x7;
2224 		sta_ctxt_cmd.rand_alloc_ecwmax =
2225 			(vif->bss_conf.uora_ocw_range >> 3) & 0x7;
2226 	}
2227 
2228 	if (own_he_cap && !(own_he_cap->he_cap_elem.mac_cap_info[2] &
2229 			    IEEE80211_HE_MAC_CAP2_ACK_EN))
2230 		flags |= STA_CTXT_HE_NIC_NOT_ACK_ENABLED;
2231 
2232 	if (vif->bss_conf.nontransmitted) {
2233 		flags |= STA_CTXT_HE_REF_BSSID_VALID;
2234 		ether_addr_copy(sta_ctxt_cmd.ref_bssid_addr,
2235 				vif->bss_conf.transmitter_bssid);
2236 		sta_ctxt_cmd.max_bssid_indicator =
2237 			vif->bss_conf.bssid_indicator;
2238 		sta_ctxt_cmd.bssid_index = vif->bss_conf.bssid_index;
2239 		sta_ctxt_cmd.ema_ap = vif->bss_conf.ema_ap;
2240 		sta_ctxt_cmd.profile_periodicity =
2241 			vif->bss_conf.profile_periodicity;
2242 	}
2243 
2244 	sta_ctxt_cmd.flags = cpu_to_le32(flags);
2245 
2246 	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(STA_HE_CTXT_CMD,
2247 						 DATA_PATH_GROUP, 0),
2248 				 0, size, &sta_ctxt_cmd))
2249 		IWL_ERR(mvm, "Failed to config FW to work HE!\n");
2250 }
2251 
2252 static void iwl_mvm_protect_assoc(struct iwl_mvm *mvm,
2253 				  struct ieee80211_vif *vif,
2254 				  u32 duration_override)
2255 {
2256 	u32 duration = IWL_MVM_TE_SESSION_PROTECTION_MAX_TIME_MS;
2257 	u32 min_duration = IWL_MVM_TE_SESSION_PROTECTION_MIN_TIME_MS;
2258 
2259 	if (duration_override > duration)
2260 		duration = duration_override;
2261 
2262 	/* Try really hard to protect the session and hear a beacon
2263 	 * The new session protection command allows us to protect the
2264 	 * session for a much longer time since the firmware will internally
2265 	 * create two events: a 300TU one with a very high priority that
2266 	 * won't be fragmented which should be enough for 99% of the cases,
2267 	 * and another one (which we configure here to be 900TU long) which
2268 	 * will have a slightly lower priority, but more importantly, can be
2269 	 * fragmented so that it'll allow other activities to run.
2270 	 */
2271 	if (fw_has_capa(&mvm->fw->ucode_capa,
2272 			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2273 		iwl_mvm_schedule_session_protection(mvm, vif, 900,
2274 						    min_duration, false);
2275 	else
2276 		iwl_mvm_protect_session(mvm, vif, duration,
2277 					min_duration, 500, false);
2278 }
2279 
2280 static void iwl_mvm_bss_info_changed_station(struct iwl_mvm *mvm,
2281 					     struct ieee80211_vif *vif,
2282 					     struct ieee80211_bss_conf *bss_conf,
2283 					     u32 changes)
2284 {
2285 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2286 	int ret;
2287 
2288 	/*
2289 	 * Re-calculate the tsf id, as the leader-follower relations depend
2290 	 * on the beacon interval, which was not known when the station
2291 	 * interface was added.
2292 	 */
2293 	if (changes & BSS_CHANGED_ASSOC && bss_conf->assoc) {
2294 		if (vif->bss_conf.he_support &&
2295 		    !iwlwifi_mod_params.disable_11ax)
2296 			iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2297 
2298 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2299 	}
2300 
2301 	/* Update MU EDCA params */
2302 	if (changes & BSS_CHANGED_QOS && mvmvif->associated &&
2303 	    bss_conf->assoc && vif->bss_conf.he_support &&
2304 	    !iwlwifi_mod_params.disable_11ax)
2305 		iwl_mvm_cfg_he_sta(mvm, vif, mvmvif->ap_sta_id);
2306 
2307 	/*
2308 	 * If we're not associated yet, take the (new) BSSID before associating
2309 	 * so the firmware knows. If we're already associated, then use the old
2310 	 * BSSID here, and we'll send a cleared one later in the CHANGED_ASSOC
2311 	 * branch for disassociation below.
2312 	 */
2313 	if (changes & BSS_CHANGED_BSSID && !mvmvif->associated)
2314 		memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2315 
2316 	ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, mvmvif->bssid);
2317 	if (ret)
2318 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2319 
2320 	/* after sending it once, adopt mac80211 data */
2321 	memcpy(mvmvif->bssid, bss_conf->bssid, ETH_ALEN);
2322 	mvmvif->associated = bss_conf->assoc;
2323 
2324 	if (changes & BSS_CHANGED_ASSOC) {
2325 		if (bss_conf->assoc) {
2326 			/* clear statistics to get clean beacon counter */
2327 			iwl_mvm_request_statistics(mvm, true);
2328 			memset(&mvmvif->beacon_stats, 0,
2329 			       sizeof(mvmvif->beacon_stats));
2330 
2331 			/* add quota for this interface */
2332 			ret = iwl_mvm_update_quotas(mvm, true, NULL);
2333 			if (ret) {
2334 				IWL_ERR(mvm, "failed to update quotas\n");
2335 				return;
2336 			}
2337 
2338 			if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2339 				     &mvm->status) &&
2340 			    !fw_has_capa(&mvm->fw->ucode_capa,
2341 					 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD)) {
2342 				/*
2343 				 * If we're restarting then the firmware will
2344 				 * obviously have lost synchronisation with
2345 				 * the AP. It will attempt to synchronise by
2346 				 * itself, but we can make it more reliable by
2347 				 * scheduling a session protection time event.
2348 				 *
2349 				 * The firmware needs to receive a beacon to
2350 				 * catch up with synchronisation, use 110% of
2351 				 * the beacon interval.
2352 				 *
2353 				 * Set a large maximum delay to allow for more
2354 				 * than a single interface.
2355 				 *
2356 				 * For new firmware versions, rely on the
2357 				 * firmware. This is relevant for DCM scenarios
2358 				 * only anyway.
2359 				 */
2360 				u32 dur = (11 * vif->bss_conf.beacon_int) / 10;
2361 				iwl_mvm_protect_session(mvm, vif, dur, dur,
2362 							5 * dur, false);
2363 			} else if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2364 					     &mvm->status) &&
2365 				   !vif->bss_conf.dtim_period) {
2366 				/*
2367 				 * If we're not restarting and still haven't
2368 				 * heard a beacon (dtim period unknown) then
2369 				 * make sure we still have enough minimum time
2370 				 * remaining in the time event, since the auth
2371 				 * might actually have taken quite a while
2372 				 * (especially for SAE) and so the remaining
2373 				 * time could be small without us having heard
2374 				 * a beacon yet.
2375 				 */
2376 				iwl_mvm_protect_assoc(mvm, vif, 0);
2377 			}
2378 
2379 			iwl_mvm_sf_update(mvm, vif, false);
2380 			iwl_mvm_power_vif_assoc(mvm, vif);
2381 			if (vif->p2p) {
2382 				iwl_mvm_update_smps(mvm, vif,
2383 						    IWL_MVM_SMPS_REQ_PROT,
2384 						    IEEE80211_SMPS_DYNAMIC);
2385 			}
2386 		} else if (mvmvif->ap_sta_id != IWL_MVM_INVALID_STA) {
2387 			/*
2388 			 * If update fails - SF might be running in associated
2389 			 * mode while disassociated - which is forbidden.
2390 			 */
2391 			ret = iwl_mvm_sf_update(mvm, vif, false);
2392 			WARN_ONCE(ret &&
2393 				  !test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
2394 					    &mvm->status),
2395 				  "Failed to update SF upon disassociation\n");
2396 
2397 			/*
2398 			 * If we get an assert during the connection (after the
2399 			 * station has been added, but before the vif is set
2400 			 * to associated), mac80211 will re-add the station and
2401 			 * then configure the vif. Since the vif is not
2402 			 * associated, we would remove the station here and
2403 			 * this would fail the recovery.
2404 			 */
2405 			if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART,
2406 				      &mvm->status)) {
2407 				/*
2408 				 * Remove AP station now that
2409 				 * the MAC is unassoc
2410 				 */
2411 				ret = iwl_mvm_rm_sta_id(mvm, vif,
2412 							mvmvif->ap_sta_id);
2413 				if (ret)
2414 					IWL_ERR(mvm,
2415 						"failed to remove AP station\n");
2416 
2417 				mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
2418 			}
2419 
2420 			/* remove quota for this interface */
2421 			ret = iwl_mvm_update_quotas(mvm, false, NULL);
2422 			if (ret)
2423 				IWL_ERR(mvm, "failed to update quotas\n");
2424 
2425 			/* this will take the cleared BSSID from bss_conf */
2426 			ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
2427 			if (ret)
2428 				IWL_ERR(mvm,
2429 					"failed to update MAC %pM (clear after unassoc)\n",
2430 					vif->addr);
2431 		}
2432 
2433 		/*
2434 		 * The firmware tracks the MU-MIMO group on its own.
2435 		 * However, on HW restart we should restore this data.
2436 		 */
2437 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
2438 		    (changes & BSS_CHANGED_MU_GROUPS) && vif->mu_mimo_owner) {
2439 			ret = iwl_mvm_update_mu_groups(mvm, vif);
2440 			if (ret)
2441 				IWL_ERR(mvm,
2442 					"failed to update VHT MU_MIMO groups\n");
2443 		}
2444 
2445 		iwl_mvm_recalc_multicast(mvm);
2446 		iwl_mvm_configure_bcast_filter(mvm);
2447 
2448 		/* reset rssi values */
2449 		mvmvif->bf_data.ave_beacon_signal = 0;
2450 
2451 		iwl_mvm_bt_coex_vif_change(mvm);
2452 		iwl_mvm_update_smps(mvm, vif, IWL_MVM_SMPS_REQ_TT,
2453 				    IEEE80211_SMPS_AUTOMATIC);
2454 		if (fw_has_capa(&mvm->fw->ucode_capa,
2455 				IWL_UCODE_TLV_CAPA_UMAC_SCAN))
2456 			iwl_mvm_config_scan(mvm);
2457 	}
2458 
2459 	if (changes & BSS_CHANGED_BEACON_INFO) {
2460 		/*
2461 		 * We received a beacon from the associated AP so
2462 		 * remove the session protection.
2463 		 * A firmware with the new API will remove it automatically.
2464 		 */
2465 		if (!fw_has_capa(&mvm->fw->ucode_capa,
2466 				 IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2467 			iwl_mvm_stop_session_protection(mvm, vif);
2468 
2469 		iwl_mvm_sf_update(mvm, vif, false);
2470 		WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
2471 	}
2472 
2473 	if (changes & (BSS_CHANGED_PS | BSS_CHANGED_P2P_PS | BSS_CHANGED_QOS |
2474 		       /*
2475 			* Send power command on every beacon change,
2476 			* because we may have not enabled beacon abort yet.
2477 			*/
2478 		       BSS_CHANGED_BEACON_INFO)) {
2479 		ret = iwl_mvm_power_update_mac(mvm);
2480 		if (ret)
2481 			IWL_ERR(mvm, "failed to update power mode\n");
2482 	}
2483 
2484 	if (changes & BSS_CHANGED_CQM) {
2485 		IWL_DEBUG_MAC80211(mvm, "cqm info_changed\n");
2486 		/* reset cqm events tracking */
2487 		mvmvif->bf_data.last_cqm_event = 0;
2488 		if (mvmvif->bf_data.bf_enabled) {
2489 			ret = iwl_mvm_enable_beacon_filter(mvm, vif, 0);
2490 			if (ret)
2491 				IWL_ERR(mvm,
2492 					"failed to update CQM thresholds\n");
2493 		}
2494 	}
2495 
2496 	if (changes & BSS_CHANGED_ARP_FILTER) {
2497 		IWL_DEBUG_MAC80211(mvm, "arp filter changed\n");
2498 		iwl_mvm_configure_bcast_filter(mvm);
2499 	}
2500 
2501 	if (changes & BSS_CHANGED_BANDWIDTH)
2502 		iwl_mvm_apply_fw_smps_request(vif);
2503 }
2504 
2505 static int iwl_mvm_start_ap_ibss(struct ieee80211_hw *hw,
2506 				 struct ieee80211_vif *vif)
2507 {
2508 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2509 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2510 	int ret, i;
2511 
2512 	mutex_lock(&mvm->mutex);
2513 
2514 	/* Send the beacon template */
2515 	ret = iwl_mvm_mac_ctxt_beacon_changed(mvm, vif);
2516 	if (ret)
2517 		goto out_unlock;
2518 
2519 	/*
2520 	 * Re-calculate the tsf id, as the leader-follower relations depend on
2521 	 * the beacon interval, which was not known when the AP interface
2522 	 * was added.
2523 	 */
2524 	if (vif->type == NL80211_IFTYPE_AP)
2525 		iwl_mvm_mac_ctxt_recalc_tsf_id(mvm, vif);
2526 
2527 	mvmvif->ap_assoc_sta_count = 0;
2528 
2529 	/* Add the mac context */
2530 	ret = iwl_mvm_mac_ctxt_add(mvm, vif);
2531 	if (ret)
2532 		goto out_unlock;
2533 
2534 	/* Perform the binding */
2535 	ret = iwl_mvm_binding_add_vif(mvm, vif);
2536 	if (ret)
2537 		goto out_remove;
2538 
2539 	/*
2540 	 * This is not very nice, but the simplest:
2541 	 * For older FWs adding the mcast sta before the bcast station may
2542 	 * cause assert 0x2b00.
2543 	 * This is fixed in later FW so make the order of removal depend on
2544 	 * the TLV
2545 	 */
2546 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE)) {
2547 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2548 		if (ret)
2549 			goto out_unbind;
2550 		/*
2551 		 * Send the bcast station. At this stage the TBTT and DTIM time
2552 		 * events are added and applied to the scheduler
2553 		 */
2554 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2555 		if (ret) {
2556 			iwl_mvm_rm_mcast_sta(mvm, vif);
2557 			goto out_unbind;
2558 		}
2559 	} else {
2560 		/*
2561 		 * Send the bcast station. At this stage the TBTT and DTIM time
2562 		 * events are added and applied to the scheduler
2563 		 */
2564 		ret = iwl_mvm_send_add_bcast_sta(mvm, vif);
2565 		if (ret)
2566 			goto out_unbind;
2567 		ret = iwl_mvm_add_mcast_sta(mvm, vif);
2568 		if (ret) {
2569 			iwl_mvm_send_rm_bcast_sta(mvm, vif);
2570 			goto out_unbind;
2571 		}
2572 	}
2573 
2574 	/* must be set before quota calculations */
2575 	mvmvif->ap_ibss_active = true;
2576 
2577 	/* send all the early keys to the device now */
2578 	for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
2579 		struct ieee80211_key_conf *key = mvmvif->ap_early_keys[i];
2580 
2581 		if (!key)
2582 			continue;
2583 
2584 		mvmvif->ap_early_keys[i] = NULL;
2585 
2586 		ret = __iwl_mvm_mac_set_key(hw, SET_KEY, vif, NULL, key);
2587 		if (ret)
2588 			goto out_quota_failed;
2589 	}
2590 
2591 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2592 		iwl_mvm_vif_set_low_latency(mvmvif, true,
2593 					    LOW_LATENCY_VIF_TYPE);
2594 		iwl_mvm_send_low_latency_cmd(mvm, true, mvmvif->id);
2595 	}
2596 
2597 	/* power updated needs to be done before quotas */
2598 	iwl_mvm_power_update_mac(mvm);
2599 
2600 	ret = iwl_mvm_update_quotas(mvm, false, NULL);
2601 	if (ret)
2602 		goto out_quota_failed;
2603 
2604 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2605 	if (vif->p2p && mvm->p2p_device_vif)
2606 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2607 
2608 	iwl_mvm_bt_coex_vif_change(mvm);
2609 
2610 	/* we don't support TDLS during DCM */
2611 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
2612 		iwl_mvm_teardown_tdls_peers(mvm);
2613 
2614 	iwl_mvm_ftm_restart_responder(mvm, vif);
2615 
2616 	goto out_unlock;
2617 
2618 out_quota_failed:
2619 	iwl_mvm_power_update_mac(mvm);
2620 	mvmvif->ap_ibss_active = false;
2621 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2622 	iwl_mvm_rm_mcast_sta(mvm, vif);
2623 out_unbind:
2624 	iwl_mvm_binding_remove_vif(mvm, vif);
2625 out_remove:
2626 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2627 out_unlock:
2628 	mutex_unlock(&mvm->mutex);
2629 	return ret;
2630 }
2631 
2632 static void iwl_mvm_stop_ap_ibss(struct ieee80211_hw *hw,
2633 				 struct ieee80211_vif *vif)
2634 {
2635 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2636 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2637 
2638 	iwl_mvm_prepare_mac_removal(mvm, vif);
2639 
2640 	mutex_lock(&mvm->mutex);
2641 
2642 	/* Handle AP stop while in CSA */
2643 	if (rcu_access_pointer(mvm->csa_vif) == vif) {
2644 		iwl_mvm_remove_time_event(mvm, mvmvif,
2645 					  &mvmvif->time_event_data);
2646 		RCU_INIT_POINTER(mvm->csa_vif, NULL);
2647 		mvmvif->csa_countdown = false;
2648 	}
2649 
2650 	if (rcu_access_pointer(mvm->csa_tx_blocked_vif) == vif) {
2651 		RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
2652 		mvm->csa_tx_block_bcn_timeout = 0;
2653 	}
2654 
2655 	mvmvif->ap_ibss_active = false;
2656 	mvm->ap_last_beacon_gp2 = 0;
2657 
2658 	if (vif->type == NL80211_IFTYPE_AP && !vif->p2p) {
2659 		iwl_mvm_vif_set_low_latency(mvmvif, false,
2660 					    LOW_LATENCY_VIF_TYPE);
2661 		iwl_mvm_send_low_latency_cmd(mvm, false,  mvmvif->id);
2662 	}
2663 
2664 	iwl_mvm_bt_coex_vif_change(mvm);
2665 
2666 	/* Need to update the P2P Device MAC (only GO, IBSS is single vif) */
2667 	if (vif->p2p && mvm->p2p_device_vif)
2668 		iwl_mvm_mac_ctxt_changed(mvm, mvm->p2p_device_vif, false, NULL);
2669 
2670 	iwl_mvm_update_quotas(mvm, false, NULL);
2671 
2672 	iwl_mvm_ftm_responder_clear(mvm, vif);
2673 
2674 	/*
2675 	 * This is not very nice, but the simplest:
2676 	 * For older FWs removing the mcast sta before the bcast station may
2677 	 * cause assert 0x2b00.
2678 	 * This is fixed in later FW (which will stop beaconing when removing
2679 	 * bcast station).
2680 	 * So make the order of removal depend on the TLV
2681 	 */
2682 	if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2683 		iwl_mvm_rm_mcast_sta(mvm, vif);
2684 	iwl_mvm_send_rm_bcast_sta(mvm, vif);
2685 	if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_STA_TYPE))
2686 		iwl_mvm_rm_mcast_sta(mvm, vif);
2687 	iwl_mvm_binding_remove_vif(mvm, vif);
2688 
2689 	iwl_mvm_power_update_mac(mvm);
2690 
2691 	iwl_mvm_mac_ctxt_remove(mvm, vif);
2692 
2693 	mutex_unlock(&mvm->mutex);
2694 }
2695 
2696 static void
2697 iwl_mvm_bss_info_changed_ap_ibss(struct iwl_mvm *mvm,
2698 				 struct ieee80211_vif *vif,
2699 				 struct ieee80211_bss_conf *bss_conf,
2700 				 u32 changes)
2701 {
2702 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2703 
2704 	/* Changes will be applied when the AP/IBSS is started */
2705 	if (!mvmvif->ap_ibss_active)
2706 		return;
2707 
2708 	if (changes & (BSS_CHANGED_ERP_CTS_PROT | BSS_CHANGED_HT |
2709 		       BSS_CHANGED_BANDWIDTH | BSS_CHANGED_QOS) &&
2710 	    iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL))
2711 		IWL_ERR(mvm, "failed to update MAC %pM\n", vif->addr);
2712 
2713 	/* Need to send a new beacon template to the FW */
2714 	if (changes & BSS_CHANGED_BEACON &&
2715 	    iwl_mvm_mac_ctxt_beacon_changed(mvm, vif))
2716 		IWL_WARN(mvm, "Failed updating beacon data\n");
2717 
2718 	if (changes & BSS_CHANGED_FTM_RESPONDER) {
2719 		int ret = iwl_mvm_ftm_start_responder(mvm, vif);
2720 
2721 		if (ret)
2722 			IWL_WARN(mvm, "Failed to enable FTM responder (%d)\n",
2723 				 ret);
2724 	}
2725 
2726 }
2727 
2728 static void iwl_mvm_bss_info_changed(struct ieee80211_hw *hw,
2729 				     struct ieee80211_vif *vif,
2730 				     struct ieee80211_bss_conf *bss_conf,
2731 				     u32 changes)
2732 {
2733 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2734 
2735 	mutex_lock(&mvm->mutex);
2736 
2737 	if (changes & BSS_CHANGED_IDLE && !bss_conf->idle)
2738 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, true);
2739 
2740 	switch (vif->type) {
2741 	case NL80211_IFTYPE_STATION:
2742 		iwl_mvm_bss_info_changed_station(mvm, vif, bss_conf, changes);
2743 		break;
2744 	case NL80211_IFTYPE_AP:
2745 	case NL80211_IFTYPE_ADHOC:
2746 		iwl_mvm_bss_info_changed_ap_ibss(mvm, vif, bss_conf, changes);
2747 		break;
2748 	case NL80211_IFTYPE_MONITOR:
2749 		if (changes & BSS_CHANGED_MU_GROUPS)
2750 			iwl_mvm_update_mu_groups(mvm, vif);
2751 		break;
2752 	default:
2753 		/* shouldn't happen */
2754 		WARN_ON_ONCE(1);
2755 	}
2756 
2757 	if (changes & BSS_CHANGED_TXPOWER) {
2758 		IWL_DEBUG_CALIB(mvm, "Changing TX Power to %d dBm\n",
2759 				bss_conf->txpower);
2760 		iwl_mvm_set_tx_power(mvm, vif, bss_conf->txpower);
2761 	}
2762 
2763 	mutex_unlock(&mvm->mutex);
2764 }
2765 
2766 static int iwl_mvm_mac_hw_scan(struct ieee80211_hw *hw,
2767 			       struct ieee80211_vif *vif,
2768 			       struct ieee80211_scan_request *hw_req)
2769 {
2770 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2771 	int ret;
2772 
2773 	if (hw_req->req.n_channels == 0 ||
2774 	    hw_req->req.n_channels > mvm->fw->ucode_capa.n_scan_channels)
2775 		return -EINVAL;
2776 
2777 	mutex_lock(&mvm->mutex);
2778 	ret = iwl_mvm_reg_scan_start(mvm, vif, &hw_req->req, &hw_req->ies);
2779 	mutex_unlock(&mvm->mutex);
2780 
2781 	return ret;
2782 }
2783 
2784 static void iwl_mvm_mac_cancel_hw_scan(struct ieee80211_hw *hw,
2785 				       struct ieee80211_vif *vif)
2786 {
2787 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2788 
2789 	mutex_lock(&mvm->mutex);
2790 
2791 	/* Due to a race condition, it's possible that mac80211 asks
2792 	 * us to stop a hw_scan when it's already stopped.  This can
2793 	 * happen, for instance, if we stopped the scan ourselves,
2794 	 * called ieee80211_scan_completed() and the userspace called
2795 	 * cancel scan scan before ieee80211_scan_work() could run.
2796 	 * To handle that, simply return if the scan is not running.
2797 	*/
2798 	if (mvm->scan_status & IWL_MVM_SCAN_REGULAR)
2799 		iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_REGULAR, true);
2800 
2801 	mutex_unlock(&mvm->mutex);
2802 }
2803 
2804 static void
2805 iwl_mvm_mac_allow_buffered_frames(struct ieee80211_hw *hw,
2806 				  struct ieee80211_sta *sta, u16 tids,
2807 				  int num_frames,
2808 				  enum ieee80211_frame_release_type reason,
2809 				  bool more_data)
2810 {
2811 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2812 
2813 	/* Called when we need to transmit (a) frame(s) from mac80211 */
2814 
2815 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2816 					  tids, more_data, false);
2817 }
2818 
2819 static void
2820 iwl_mvm_mac_release_buffered_frames(struct ieee80211_hw *hw,
2821 				    struct ieee80211_sta *sta, u16 tids,
2822 				    int num_frames,
2823 				    enum ieee80211_frame_release_type reason,
2824 				    bool more_data)
2825 {
2826 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2827 
2828 	/* Called when we need to transmit (a) frame(s) from agg or dqa queue */
2829 
2830 	iwl_mvm_sta_modify_sleep_tx_count(mvm, sta, reason, num_frames,
2831 					  tids, more_data, true);
2832 }
2833 
2834 static void __iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2835 				     enum sta_notify_cmd cmd,
2836 				     struct ieee80211_sta *sta)
2837 {
2838 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2839 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
2840 	unsigned long txqs = 0, tids = 0;
2841 	int tid;
2842 
2843 	/*
2844 	 * If we have TVQM then we get too high queue numbers - luckily
2845 	 * we really shouldn't get here with that because such hardware
2846 	 * should have firmware supporting buffer station offload.
2847 	 */
2848 	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
2849 		return;
2850 
2851 	spin_lock_bh(&mvmsta->lock);
2852 	for (tid = 0; tid < ARRAY_SIZE(mvmsta->tid_data); tid++) {
2853 		struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
2854 
2855 		if (tid_data->txq_id == IWL_MVM_INVALID_QUEUE)
2856 			continue;
2857 
2858 		__set_bit(tid_data->txq_id, &txqs);
2859 
2860 		if (iwl_mvm_tid_queued(mvm, tid_data) == 0)
2861 			continue;
2862 
2863 		__set_bit(tid, &tids);
2864 	}
2865 
2866 	switch (cmd) {
2867 	case STA_NOTIFY_SLEEP:
2868 		for_each_set_bit(tid, &tids, IWL_MAX_TID_COUNT)
2869 			ieee80211_sta_set_buffered(sta, tid, true);
2870 
2871 		if (txqs)
2872 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, true);
2873 		/*
2874 		 * The fw updates the STA to be asleep. Tx packets on the Tx
2875 		 * queues to this station will not be transmitted. The fw will
2876 		 * send a Tx response with TX_STATUS_FAIL_DEST_PS.
2877 		 */
2878 		break;
2879 	case STA_NOTIFY_AWAKE:
2880 		if (WARN_ON(mvmsta->sta_id == IWL_MVM_INVALID_STA))
2881 			break;
2882 
2883 		if (txqs)
2884 			iwl_trans_freeze_txq_timer(mvm->trans, txqs, false);
2885 		iwl_mvm_sta_modify_ps_wake(mvm, sta);
2886 		break;
2887 	default:
2888 		break;
2889 	}
2890 	spin_unlock_bh(&mvmsta->lock);
2891 }
2892 
2893 static void iwl_mvm_mac_sta_notify(struct ieee80211_hw *hw,
2894 				   struct ieee80211_vif *vif,
2895 				   enum sta_notify_cmd cmd,
2896 				   struct ieee80211_sta *sta)
2897 {
2898 	__iwl_mvm_mac_sta_notify(hw, cmd, sta);
2899 }
2900 
2901 void iwl_mvm_sta_pm_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2902 {
2903 	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2904 	struct iwl_mvm_pm_state_notification *notif = (void *)pkt->data;
2905 	struct ieee80211_sta *sta;
2906 	struct iwl_mvm_sta *mvmsta;
2907 	bool sleeping = (notif->type != IWL_MVM_PM_EVENT_AWAKE);
2908 
2909 	if (WARN_ON(notif->sta_id >= mvm->fw->ucode_capa.num_stations))
2910 		return;
2911 
2912 	rcu_read_lock();
2913 	sta = rcu_dereference(mvm->fw_id_to_mac_id[notif->sta_id]);
2914 	if (WARN_ON(IS_ERR_OR_NULL(sta))) {
2915 		rcu_read_unlock();
2916 		return;
2917 	}
2918 
2919 	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2920 
2921 	if (!mvmsta->vif ||
2922 	    mvmsta->vif->type != NL80211_IFTYPE_AP) {
2923 		rcu_read_unlock();
2924 		return;
2925 	}
2926 
2927 	if (mvmsta->sleeping != sleeping) {
2928 		mvmsta->sleeping = sleeping;
2929 		__iwl_mvm_mac_sta_notify(mvm->hw,
2930 			sleeping ? STA_NOTIFY_SLEEP : STA_NOTIFY_AWAKE,
2931 			sta);
2932 		ieee80211_sta_ps_transition(sta, sleeping);
2933 	}
2934 
2935 	if (sleeping) {
2936 		switch (notif->type) {
2937 		case IWL_MVM_PM_EVENT_AWAKE:
2938 		case IWL_MVM_PM_EVENT_ASLEEP:
2939 			break;
2940 		case IWL_MVM_PM_EVENT_UAPSD:
2941 			ieee80211_sta_uapsd_trigger(sta, IEEE80211_NUM_TIDS);
2942 			break;
2943 		case IWL_MVM_PM_EVENT_PS_POLL:
2944 			ieee80211_sta_pspoll(sta);
2945 			break;
2946 		default:
2947 			break;
2948 		}
2949 	}
2950 
2951 	rcu_read_unlock();
2952 }
2953 
2954 static void iwl_mvm_sta_pre_rcu_remove(struct ieee80211_hw *hw,
2955 				       struct ieee80211_vif *vif,
2956 				       struct ieee80211_sta *sta)
2957 {
2958 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2959 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
2960 
2961 	/*
2962 	 * This is called before mac80211 does RCU synchronisation,
2963 	 * so here we already invalidate our internal RCU-protected
2964 	 * station pointer. The rest of the code will thus no longer
2965 	 * be able to find the station this way, and we don't rely
2966 	 * on further RCU synchronisation after the sta_state()
2967 	 * callback deleted the station.
2968 	 */
2969 	mutex_lock(&mvm->mutex);
2970 	if (sta == rcu_access_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id]))
2971 		rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
2972 				   ERR_PTR(-ENOENT));
2973 
2974 	mutex_unlock(&mvm->mutex);
2975 }
2976 
2977 static void iwl_mvm_check_uapsd(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
2978 				const u8 *bssid)
2979 {
2980 	int i;
2981 
2982 	if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
2983 		struct iwl_mvm_tcm_mac *mdata;
2984 
2985 		mdata = &mvm->tcm.data[iwl_mvm_vif_from_mac80211(vif)->id];
2986 		ewma_rate_init(&mdata->uapsd_nonagg_detect.rate);
2987 		mdata->opened_rx_ba_sessions = false;
2988 	}
2989 
2990 	if (!(mvm->fw->ucode_capa.flags & IWL_UCODE_TLV_FLAGS_UAPSD_SUPPORT))
2991 		return;
2992 
2993 	if (vif->p2p && !iwl_mvm_is_p2p_scm_uapsd_supported(mvm)) {
2994 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
2995 		return;
2996 	}
2997 
2998 	if (!vif->p2p &&
2999 	    (iwlwifi_mod_params.uapsd_disable & IWL_DISABLE_UAPSD_BSS)) {
3000 		vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3001 		return;
3002 	}
3003 
3004 	for (i = 0; i < IWL_MVM_UAPSD_NOAGG_LIST_LEN; i++) {
3005 		if (ether_addr_equal(mvm->uapsd_noagg_bssids[i].addr, bssid)) {
3006 			vif->driver_flags &= ~IEEE80211_VIF_SUPPORTS_UAPSD;
3007 			return;
3008 		}
3009 	}
3010 
3011 	vif->driver_flags |= IEEE80211_VIF_SUPPORTS_UAPSD;
3012 }
3013 
3014 static void
3015 iwl_mvm_tdls_check_trigger(struct iwl_mvm *mvm,
3016 			   struct ieee80211_vif *vif, u8 *peer_addr,
3017 			   enum nl80211_tdls_operation action)
3018 {
3019 	struct iwl_fw_dbg_trigger_tlv *trig;
3020 	struct iwl_fw_dbg_trigger_tdls *tdls_trig;
3021 
3022 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
3023 				     FW_DBG_TRIGGER_TDLS);
3024 	if (!trig)
3025 		return;
3026 
3027 	tdls_trig = (void *)trig->data;
3028 
3029 	if (!(tdls_trig->action_bitmap & BIT(action)))
3030 		return;
3031 
3032 	if (tdls_trig->peer_mode &&
3033 	    memcmp(tdls_trig->peer, peer_addr, ETH_ALEN) != 0)
3034 		return;
3035 
3036 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
3037 				"TDLS event occurred, peer %pM, action %d",
3038 				peer_addr, action);
3039 }
3040 
3041 struct iwl_mvm_he_obss_narrow_bw_ru_data {
3042 	bool tolerated;
3043 };
3044 
3045 static void iwl_mvm_check_he_obss_narrow_bw_ru_iter(struct wiphy *wiphy,
3046 						    struct cfg80211_bss *bss,
3047 						    void *_data)
3048 {
3049 	struct iwl_mvm_he_obss_narrow_bw_ru_data *data = _data;
3050 	const struct cfg80211_bss_ies *ies;
3051 	const struct element *elem;
3052 
3053 	rcu_read_lock();
3054 	ies = rcu_dereference(bss->ies);
3055 	elem = cfg80211_find_elem(WLAN_EID_EXT_CAPABILITY, ies->data,
3056 				  ies->len);
3057 
3058 	if (!elem || elem->datalen < 10 ||
3059 	    !(elem->data[10] &
3060 	      WLAN_EXT_CAPA10_OBSS_NARROW_BW_RU_TOLERANCE_SUPPORT)) {
3061 		data->tolerated = false;
3062 	}
3063 	rcu_read_unlock();
3064 }
3065 
3066 static void iwl_mvm_check_he_obss_narrow_bw_ru(struct ieee80211_hw *hw,
3067 					       struct ieee80211_vif *vif)
3068 {
3069 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3070 	struct iwl_mvm_he_obss_narrow_bw_ru_data iter_data = {
3071 		.tolerated = true,
3072 	};
3073 
3074 	if (!(vif->bss_conf.chandef.chan->flags & IEEE80211_CHAN_RADAR)) {
3075 		mvmvif->he_ru_2mhz_block = false;
3076 		return;
3077 	}
3078 
3079 	cfg80211_bss_iter(hw->wiphy, &vif->bss_conf.chandef,
3080 			  iwl_mvm_check_he_obss_narrow_bw_ru_iter,
3081 			  &iter_data);
3082 
3083 	/*
3084 	 * If there is at least one AP on radar channel that cannot
3085 	 * tolerate 26-tone RU UL OFDMA transmissions using HE TB PPDU.
3086 	 */
3087 	mvmvif->he_ru_2mhz_block = !iter_data.tolerated;
3088 }
3089 
3090 static void iwl_mvm_reset_cca_40mhz_workaround(struct iwl_mvm *mvm,
3091 					       struct ieee80211_vif *vif)
3092 {
3093 	struct ieee80211_supported_band *sband;
3094 	const struct ieee80211_sta_he_cap *he_cap;
3095 
3096 	if (vif->type != NL80211_IFTYPE_STATION)
3097 		return;
3098 
3099 	if (!mvm->cca_40mhz_workaround)
3100 		return;
3101 
3102 	/* decrement and check that we reached zero */
3103 	mvm->cca_40mhz_workaround--;
3104 	if (mvm->cca_40mhz_workaround)
3105 		return;
3106 
3107 	sband = mvm->hw->wiphy->bands[NL80211_BAND_2GHZ];
3108 
3109 	sband->ht_cap.cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
3110 
3111 	he_cap = ieee80211_get_he_iftype_cap(sband,
3112 					     ieee80211_vif_type_p2p(vif));
3113 
3114 	if (he_cap) {
3115 		/* we know that ours is writable */
3116 		struct ieee80211_sta_he_cap *he = (void *)(uintptr_t)he_cap;
3117 
3118 		he->he_cap_elem.phy_cap_info[0] |=
3119 			IEEE80211_HE_PHY_CAP0_CHANNEL_WIDTH_SET_40MHZ_IN_2G;
3120 	}
3121 }
3122 
3123 static int iwl_mvm_mac_sta_state(struct ieee80211_hw *hw,
3124 				 struct ieee80211_vif *vif,
3125 				 struct ieee80211_sta *sta,
3126 				 enum ieee80211_sta_state old_state,
3127 				 enum ieee80211_sta_state new_state)
3128 {
3129 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3130 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3131 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
3132 	int ret;
3133 
3134 	IWL_DEBUG_MAC80211(mvm, "station %pM state change %d->%d\n",
3135 			   sta->addr, old_state, new_state);
3136 
3137 	/* this would be a mac80211 bug ... but don't crash */
3138 	if (WARN_ON_ONCE(!mvmvif->phy_ctxt))
3139 		return test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED, &mvm->status) ? 0 : -EINVAL;
3140 
3141 	/*
3142 	 * If we are in a STA removal flow and in DQA mode:
3143 	 *
3144 	 * This is after the sync_rcu part, so the queues have already been
3145 	 * flushed. No more TXs on their way in mac80211's path, and no more in
3146 	 * the queues.
3147 	 * Also, we won't be getting any new TX frames for this station.
3148 	 * What we might have are deferred TX frames that need to be taken care
3149 	 * of.
3150 	 *
3151 	 * Drop any still-queued deferred-frame before removing the STA, and
3152 	 * make sure the worker is no longer handling frames for this STA.
3153 	 */
3154 	if (old_state == IEEE80211_STA_NONE &&
3155 	    new_state == IEEE80211_STA_NOTEXIST) {
3156 		flush_work(&mvm->add_stream_wk);
3157 
3158 		/*
3159 		 * No need to make sure deferred TX indication is off since the
3160 		 * worker will already remove it if it was on
3161 		 */
3162 
3163 		/*
3164 		 * Additionally, reset the 40 MHz capability if we disconnected
3165 		 * from the AP now.
3166 		 */
3167 		iwl_mvm_reset_cca_40mhz_workaround(mvm, vif);
3168 	}
3169 
3170 	mutex_lock(&mvm->mutex);
3171 	/* track whether or not the station is associated */
3172 	mvm_sta->sta_state = new_state;
3173 
3174 	if (old_state == IEEE80211_STA_NOTEXIST &&
3175 	    new_state == IEEE80211_STA_NONE) {
3176 		/*
3177 		 * Firmware bug - it'll crash if the beacon interval is less
3178 		 * than 16. We can't avoid connecting at all, so refuse the
3179 		 * station state change, this will cause mac80211 to abandon
3180 		 * attempts to connect to this AP, and eventually wpa_s will
3181 		 * blocklist the AP...
3182 		 */
3183 		if (vif->type == NL80211_IFTYPE_STATION &&
3184 		    vif->bss_conf.beacon_int < 16) {
3185 			IWL_ERR(mvm,
3186 				"AP %pM beacon interval is %d, refusing due to firmware bug!\n",
3187 				sta->addr, vif->bss_conf.beacon_int);
3188 			ret = -EINVAL;
3189 			goto out_unlock;
3190 		}
3191 
3192 		if (vif->type == NL80211_IFTYPE_STATION)
3193 			vif->bss_conf.he_support = sta->he_cap.has_he;
3194 
3195 		if (sta->tdls &&
3196 		    (vif->p2p ||
3197 		     iwl_mvm_tdls_sta_count(mvm, NULL) ==
3198 						IWL_MVM_TDLS_STA_COUNT ||
3199 		     iwl_mvm_phy_ctx_count(mvm) > 1)) {
3200 			IWL_DEBUG_MAC80211(mvm, "refusing TDLS sta\n");
3201 			ret = -EBUSY;
3202 			goto out_unlock;
3203 		}
3204 
3205 		ret = iwl_mvm_add_sta(mvm, vif, sta);
3206 		if (sta->tdls && ret == 0) {
3207 			iwl_mvm_recalc_tdls_state(mvm, vif, true);
3208 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3209 						   NL80211_TDLS_SETUP);
3210 		}
3211 
3212 		sta->max_rc_amsdu_len = 1;
3213 	} else if (old_state == IEEE80211_STA_NONE &&
3214 		   new_state == IEEE80211_STA_AUTH) {
3215 		/*
3216 		 * EBS may be disabled due to previous failures reported by FW.
3217 		 * Reset EBS status here assuming environment has been changed.
3218 		 */
3219 		mvm->last_ebs_successful = true;
3220 		iwl_mvm_check_uapsd(mvm, vif, sta->addr);
3221 		ret = 0;
3222 	} else if (old_state == IEEE80211_STA_AUTH &&
3223 		   new_state == IEEE80211_STA_ASSOC) {
3224 		if (vif->type == NL80211_IFTYPE_AP) {
3225 			vif->bss_conf.he_support = sta->he_cap.has_he;
3226 			mvmvif->ap_assoc_sta_count++;
3227 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3228 			if (vif->bss_conf.he_support &&
3229 			    !iwlwifi_mod_params.disable_11ax)
3230 				iwl_mvm_cfg_he_sta(mvm, vif, mvm_sta->sta_id);
3231 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3232 			vif->bss_conf.he_support = sta->he_cap.has_he;
3233 
3234 			mvmvif->he_ru_2mhz_block = false;
3235 			if (sta->he_cap.has_he)
3236 				iwl_mvm_check_he_obss_narrow_bw_ru(hw, vif);
3237 
3238 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3239 		}
3240 
3241 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3242 				     false);
3243 		ret = iwl_mvm_update_sta(mvm, vif, sta);
3244 	} else if (old_state == IEEE80211_STA_ASSOC &&
3245 		   new_state == IEEE80211_STA_AUTHORIZED) {
3246 		ret = 0;
3247 
3248 		/* we don't support TDLS during DCM */
3249 		if (iwl_mvm_phy_ctx_count(mvm) > 1)
3250 			iwl_mvm_teardown_tdls_peers(mvm);
3251 
3252 		if (sta->tdls) {
3253 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3254 						   NL80211_TDLS_ENABLE_LINK);
3255 		} else {
3256 			/* enable beacon filtering */
3257 			WARN_ON(iwl_mvm_enable_beacon_filter(mvm, vif, 0));
3258 
3259 			mvmvif->authorized = 1;
3260 
3261 			/*
3262 			 * Now that the station is authorized, i.e., keys were already
3263 			 * installed, need to indicate to the FW that
3264 			 * multicast data frames can be forwarded to the driver
3265 			 */
3266 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3267 		}
3268 
3269 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3270 				     true);
3271 	} else if (old_state == IEEE80211_STA_AUTHORIZED &&
3272 		   new_state == IEEE80211_STA_ASSOC) {
3273 		if (!sta->tdls) {
3274 			/* Multicast data frames are no longer allowed */
3275 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3276 
3277 			/*
3278 			 * Set this after the above iwl_mvm_mac_ctxt_changed()
3279 			 * to avoid sending high prio again for a little time.
3280 			 */
3281 			mvmvif->authorized = 0;
3282 
3283 			/* disable beacon filtering */
3284 			ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
3285 			WARN_ON(ret &&
3286 				!test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3287 					  &mvm->status));
3288 		}
3289 		ret = 0;
3290 	} else if (old_state == IEEE80211_STA_ASSOC &&
3291 		   new_state == IEEE80211_STA_AUTH) {
3292 		if (vif->type == NL80211_IFTYPE_AP) {
3293 			mvmvif->ap_assoc_sta_count--;
3294 			iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3295 		} else if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls) {
3296 			/* remove session protection if still running */
3297 			iwl_mvm_stop_session_protection(mvm, vif);
3298 		}
3299 		ret = 0;
3300 	} else if (old_state == IEEE80211_STA_AUTH &&
3301 		   new_state == IEEE80211_STA_NONE) {
3302 		ret = 0;
3303 	} else if (old_state == IEEE80211_STA_NONE &&
3304 		   new_state == IEEE80211_STA_NOTEXIST) {
3305 		ret = iwl_mvm_rm_sta(mvm, vif, sta);
3306 		if (sta->tdls) {
3307 			iwl_mvm_recalc_tdls_state(mvm, vif, false);
3308 			iwl_mvm_tdls_check_trigger(mvm, vif, sta->addr,
3309 						   NL80211_TDLS_DISABLE_LINK);
3310 		}
3311 
3312 		if (unlikely(ret &&
3313 			     test_bit(IWL_MVM_STATUS_HW_RESTART_REQUESTED,
3314 				      &mvm->status)))
3315 			ret = 0;
3316 	} else {
3317 		ret = -EIO;
3318 	}
3319  out_unlock:
3320 	mutex_unlock(&mvm->mutex);
3321 
3322 	if (sta->tdls && ret == 0) {
3323 		if (old_state == IEEE80211_STA_NOTEXIST &&
3324 		    new_state == IEEE80211_STA_NONE)
3325 			ieee80211_reserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3326 		else if (old_state == IEEE80211_STA_NONE &&
3327 			 new_state == IEEE80211_STA_NOTEXIST)
3328 			ieee80211_unreserve_tid(sta, IWL_MVM_TDLS_FW_TID);
3329 	}
3330 
3331 	return ret;
3332 }
3333 
3334 static int iwl_mvm_mac_set_rts_threshold(struct ieee80211_hw *hw, u32 value)
3335 {
3336 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3337 
3338 	mvm->rts_threshold = value;
3339 
3340 	return 0;
3341 }
3342 
3343 static void iwl_mvm_sta_rc_update(struct ieee80211_hw *hw,
3344 				  struct ieee80211_vif *vif,
3345 				  struct ieee80211_sta *sta, u32 changed)
3346 {
3347 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3348 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3349 
3350 	if (changed & (IEEE80211_RC_BW_CHANGED |
3351 		       IEEE80211_RC_SUPP_RATES_CHANGED |
3352 		       IEEE80211_RC_NSS_CHANGED))
3353 		iwl_mvm_rs_rate_init(mvm, sta, mvmvif->phy_ctxt->channel->band,
3354 				     true);
3355 
3356 	if (vif->type == NL80211_IFTYPE_STATION &&
3357 	    changed & IEEE80211_RC_NSS_CHANGED)
3358 		iwl_mvm_sf_update(mvm, vif, false);
3359 }
3360 
3361 static int iwl_mvm_mac_conf_tx(struct ieee80211_hw *hw,
3362 			       struct ieee80211_vif *vif, u16 ac,
3363 			       const struct ieee80211_tx_queue_params *params)
3364 {
3365 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3366 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3367 
3368 	mvmvif->queue_params[ac] = *params;
3369 
3370 	/*
3371 	 * No need to update right away, we'll get BSS_CHANGED_QOS
3372 	 * The exception is P2P_DEVICE interface which needs immediate update.
3373 	 */
3374 	if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
3375 		int ret;
3376 
3377 		mutex_lock(&mvm->mutex);
3378 		ret = iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
3379 		mutex_unlock(&mvm->mutex);
3380 		return ret;
3381 	}
3382 	return 0;
3383 }
3384 
3385 static void iwl_mvm_mac_mgd_prepare_tx(struct ieee80211_hw *hw,
3386 				       struct ieee80211_vif *vif,
3387 				       struct ieee80211_prep_tx_info *info)
3388 {
3389 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3390 
3391 	mutex_lock(&mvm->mutex);
3392 	iwl_mvm_protect_assoc(mvm, vif, info->duration);
3393 	mutex_unlock(&mvm->mutex);
3394 }
3395 
3396 static void iwl_mvm_mac_mgd_complete_tx(struct ieee80211_hw *hw,
3397 					struct ieee80211_vif *vif,
3398 					struct ieee80211_prep_tx_info *info)
3399 {
3400 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3401 
3402 	/* for successful cases (auth/assoc), don't cancel session protection */
3403 	if (info->success)
3404 		return;
3405 
3406 	mutex_lock(&mvm->mutex);
3407 	iwl_mvm_stop_session_protection(mvm, vif);
3408 	mutex_unlock(&mvm->mutex);
3409 }
3410 
3411 static int iwl_mvm_mac_sched_scan_start(struct ieee80211_hw *hw,
3412 					struct ieee80211_vif *vif,
3413 					struct cfg80211_sched_scan_request *req,
3414 					struct ieee80211_scan_ies *ies)
3415 {
3416 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3417 
3418 	int ret;
3419 
3420 	mutex_lock(&mvm->mutex);
3421 
3422 	if (!vif->bss_conf.idle) {
3423 		ret = -EBUSY;
3424 		goto out;
3425 	}
3426 
3427 	ret = iwl_mvm_sched_scan_start(mvm, vif, req, ies, IWL_MVM_SCAN_SCHED);
3428 
3429 out:
3430 	mutex_unlock(&mvm->mutex);
3431 	return ret;
3432 }
3433 
3434 static int iwl_mvm_mac_sched_scan_stop(struct ieee80211_hw *hw,
3435 				       struct ieee80211_vif *vif)
3436 {
3437 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3438 	int ret;
3439 
3440 	mutex_lock(&mvm->mutex);
3441 
3442 	/* Due to a race condition, it's possible that mac80211 asks
3443 	 * us to stop a sched_scan when it's already stopped.  This
3444 	 * can happen, for instance, if we stopped the scan ourselves,
3445 	 * called ieee80211_sched_scan_stopped() and the userspace called
3446 	 * stop sched scan scan before ieee80211_sched_scan_stopped_work()
3447 	 * could run.  To handle this, simply return if the scan is
3448 	 * not running.
3449 	*/
3450 	if (!(mvm->scan_status & IWL_MVM_SCAN_SCHED)) {
3451 		mutex_unlock(&mvm->mutex);
3452 		return 0;
3453 	}
3454 
3455 	ret = iwl_mvm_scan_stop(mvm, IWL_MVM_SCAN_SCHED, false);
3456 	mutex_unlock(&mvm->mutex);
3457 	iwl_mvm_wait_for_async_handlers(mvm);
3458 
3459 	return ret;
3460 }
3461 
3462 static int __iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3463 				 enum set_key_cmd cmd,
3464 				 struct ieee80211_vif *vif,
3465 				 struct ieee80211_sta *sta,
3466 				 struct ieee80211_key_conf *key)
3467 {
3468 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3469 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3470 	struct iwl_mvm_sta *mvmsta;
3471 	struct iwl_mvm_key_pn *ptk_pn;
3472 	int keyidx = key->keyidx;
3473 	int ret, i;
3474 	u8 key_offset;
3475 
3476 	switch (key->cipher) {
3477 	case WLAN_CIPHER_SUITE_TKIP:
3478 		if (!mvm->trans->trans_cfg->gen2) {
3479 			key->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
3480 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3481 		} else if (vif->type == NL80211_IFTYPE_STATION) {
3482 			key->flags |= IEEE80211_KEY_FLAG_PUT_MIC_SPACE;
3483 		} else {
3484 			IWL_DEBUG_MAC80211(mvm, "Use SW encryption for TKIP\n");
3485 			return -EOPNOTSUPP;
3486 		}
3487 		break;
3488 	case WLAN_CIPHER_SUITE_CCMP:
3489 	case WLAN_CIPHER_SUITE_GCMP:
3490 	case WLAN_CIPHER_SUITE_GCMP_256:
3491 		if (!iwl_mvm_has_new_tx_api(mvm))
3492 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3493 		break;
3494 	case WLAN_CIPHER_SUITE_AES_CMAC:
3495 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
3496 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
3497 		WARN_ON_ONCE(!ieee80211_hw_check(hw, MFP_CAPABLE));
3498 		break;
3499 	case WLAN_CIPHER_SUITE_WEP40:
3500 	case WLAN_CIPHER_SUITE_WEP104:
3501 		if (vif->type == NL80211_IFTYPE_STATION)
3502 			break;
3503 		if (iwl_mvm_has_new_tx_api(mvm))
3504 			return -EOPNOTSUPP;
3505 		/* support HW crypto on TX */
3506 		return 0;
3507 	default:
3508 		/* currently FW supports only one optional cipher scheme */
3509 		if (hw->n_cipher_schemes &&
3510 		    hw->cipher_schemes->cipher == key->cipher)
3511 			key->flags |= IEEE80211_KEY_FLAG_PUT_IV_SPACE;
3512 		else
3513 			return -EOPNOTSUPP;
3514 	}
3515 
3516 	switch (cmd) {
3517 	case SET_KEY:
3518 		if (keyidx == 6 || keyidx == 7)
3519 			rcu_assign_pointer(mvmvif->bcn_prot.keys[keyidx - 6],
3520 					   key);
3521 
3522 		if ((vif->type == NL80211_IFTYPE_ADHOC ||
3523 		     vif->type == NL80211_IFTYPE_AP) && !sta) {
3524 			/*
3525 			 * GTK on AP interface is a TX-only key, return 0;
3526 			 * on IBSS they're per-station and because we're lazy
3527 			 * we don't support them for RX, so do the same.
3528 			 * CMAC/GMAC in AP/IBSS modes must be done in software.
3529 			 */
3530 			if (key->cipher == WLAN_CIPHER_SUITE_AES_CMAC ||
3531 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_128 ||
3532 			    key->cipher == WLAN_CIPHER_SUITE_BIP_GMAC_256) {
3533 				ret = -EOPNOTSUPP;
3534 				break;
3535 			}
3536 
3537 			if (key->cipher != WLAN_CIPHER_SUITE_GCMP &&
3538 			    key->cipher != WLAN_CIPHER_SUITE_GCMP_256 &&
3539 			    !iwl_mvm_has_new_tx_api(mvm)) {
3540 				key->hw_key_idx = STA_KEY_IDX_INVALID;
3541 				ret = 0;
3542 				break;
3543 			}
3544 
3545 			if (!mvmvif->ap_ibss_active) {
3546 				for (i = 0;
3547 				     i < ARRAY_SIZE(mvmvif->ap_early_keys);
3548 				     i++) {
3549 					if (!mvmvif->ap_early_keys[i]) {
3550 						mvmvif->ap_early_keys[i] = key;
3551 						break;
3552 					}
3553 				}
3554 
3555 				if (i >= ARRAY_SIZE(mvmvif->ap_early_keys))
3556 					ret = -ENOSPC;
3557 				else
3558 					ret = 0;
3559 
3560 				break;
3561 			}
3562 		}
3563 
3564 		/* During FW restart, in order to restore the state as it was,
3565 		 * don't try to reprogram keys we previously failed for.
3566 		 */
3567 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3568 		    key->hw_key_idx == STA_KEY_IDX_INVALID) {
3569 			IWL_DEBUG_MAC80211(mvm,
3570 					   "skip invalid idx key programming during restart\n");
3571 			ret = 0;
3572 			break;
3573 		}
3574 
3575 		if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status) &&
3576 		    sta && iwl_mvm_has_new_rx_api(mvm) &&
3577 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3578 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3579 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3580 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3581 			struct ieee80211_key_seq seq;
3582 			int tid, q;
3583 
3584 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3585 			WARN_ON(rcu_access_pointer(mvmsta->ptk_pn[keyidx]));
3586 			ptk_pn = kzalloc(struct_size(ptk_pn, q,
3587 						     mvm->trans->num_rx_queues),
3588 					 GFP_KERNEL);
3589 			if (!ptk_pn) {
3590 				ret = -ENOMEM;
3591 				break;
3592 			}
3593 
3594 			for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++) {
3595 				ieee80211_get_key_rx_seq(key, tid, &seq);
3596 				for (q = 0; q < mvm->trans->num_rx_queues; q++)
3597 					memcpy(ptk_pn->q[q].pn[tid],
3598 					       seq.ccmp.pn,
3599 					       IEEE80211_CCMP_PN_LEN);
3600 			}
3601 
3602 			rcu_assign_pointer(mvmsta->ptk_pn[keyidx], ptk_pn);
3603 		}
3604 
3605 		/* in HW restart reuse the index, otherwise request a new one */
3606 		if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
3607 			key_offset = key->hw_key_idx;
3608 		else
3609 			key_offset = STA_KEY_IDX_INVALID;
3610 
3611 		IWL_DEBUG_MAC80211(mvm, "set hwcrypto key\n");
3612 		ret = iwl_mvm_set_sta_key(mvm, vif, sta, key, key_offset);
3613 		if (ret) {
3614 			IWL_WARN(mvm, "set key failed\n");
3615 			key->hw_key_idx = STA_KEY_IDX_INVALID;
3616 			/*
3617 			 * can't add key for RX, but we don't need it
3618 			 * in the device for TX so still return 0,
3619 			 * unless we have new TX API where we cannot
3620 			 * put key material into the TX_CMD
3621 			 */
3622 			if (iwl_mvm_has_new_tx_api(mvm))
3623 				ret = -EOPNOTSUPP;
3624 			else
3625 				ret = 0;
3626 		}
3627 
3628 		break;
3629 	case DISABLE_KEY:
3630 		if (keyidx == 6 || keyidx == 7)
3631 			RCU_INIT_POINTER(mvmvif->bcn_prot.keys[keyidx - 6],
3632 					 NULL);
3633 
3634 		ret = -ENOENT;
3635 		for (i = 0; i < ARRAY_SIZE(mvmvif->ap_early_keys); i++) {
3636 			if (mvmvif->ap_early_keys[i] == key) {
3637 				mvmvif->ap_early_keys[i] = NULL;
3638 				ret = 0;
3639 			}
3640 		}
3641 
3642 		/* found in pending list - don't do anything else */
3643 		if (ret == 0)
3644 			break;
3645 
3646 		if (key->hw_key_idx == STA_KEY_IDX_INVALID) {
3647 			ret = 0;
3648 			break;
3649 		}
3650 
3651 		if (sta && iwl_mvm_has_new_rx_api(mvm) &&
3652 		    key->flags & IEEE80211_KEY_FLAG_PAIRWISE &&
3653 		    (key->cipher == WLAN_CIPHER_SUITE_CCMP ||
3654 		     key->cipher == WLAN_CIPHER_SUITE_GCMP ||
3655 		     key->cipher == WLAN_CIPHER_SUITE_GCMP_256)) {
3656 			mvmsta = iwl_mvm_sta_from_mac80211(sta);
3657 			ptk_pn = rcu_dereference_protected(
3658 						mvmsta->ptk_pn[keyidx],
3659 						lockdep_is_held(&mvm->mutex));
3660 			RCU_INIT_POINTER(mvmsta->ptk_pn[keyidx], NULL);
3661 			if (ptk_pn)
3662 				kfree_rcu(ptk_pn, rcu_head);
3663 		}
3664 
3665 		IWL_DEBUG_MAC80211(mvm, "disable hwcrypto key\n");
3666 		ret = iwl_mvm_remove_sta_key(mvm, vif, sta, key);
3667 		break;
3668 	default:
3669 		ret = -EINVAL;
3670 	}
3671 
3672 	return ret;
3673 }
3674 
3675 static int iwl_mvm_mac_set_key(struct ieee80211_hw *hw,
3676 			       enum set_key_cmd cmd,
3677 			       struct ieee80211_vif *vif,
3678 			       struct ieee80211_sta *sta,
3679 			       struct ieee80211_key_conf *key)
3680 {
3681 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3682 	int ret;
3683 
3684 	mutex_lock(&mvm->mutex);
3685 	ret = __iwl_mvm_mac_set_key(hw, cmd, vif, sta, key);
3686 	mutex_unlock(&mvm->mutex);
3687 
3688 	return ret;
3689 }
3690 
3691 static void iwl_mvm_mac_update_tkip_key(struct ieee80211_hw *hw,
3692 					struct ieee80211_vif *vif,
3693 					struct ieee80211_key_conf *keyconf,
3694 					struct ieee80211_sta *sta,
3695 					u32 iv32, u16 *phase1key)
3696 {
3697 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3698 
3699 	if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
3700 		return;
3701 
3702 	iwl_mvm_update_tkip_key(mvm, vif, keyconf, sta, iv32, phase1key);
3703 }
3704 
3705 
3706 static bool iwl_mvm_rx_aux_roc(struct iwl_notif_wait_data *notif_wait,
3707 			       struct iwl_rx_packet *pkt, void *data)
3708 {
3709 	struct iwl_mvm *mvm =
3710 		container_of(notif_wait, struct iwl_mvm, notif_wait);
3711 	struct iwl_hs20_roc_res *resp;
3712 	int resp_len = iwl_rx_packet_payload_len(pkt);
3713 	struct iwl_mvm_time_event_data *te_data = data;
3714 
3715 	if (WARN_ON(pkt->hdr.cmd != HOT_SPOT_CMD))
3716 		return true;
3717 
3718 	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
3719 		IWL_ERR(mvm, "Invalid HOT_SPOT_CMD response\n");
3720 		return true;
3721 	}
3722 
3723 	resp = (void *)pkt->data;
3724 
3725 	IWL_DEBUG_TE(mvm,
3726 		     "Aux ROC: Received response from ucode: status=%d uid=%d\n",
3727 		     resp->status, resp->event_unique_id);
3728 
3729 	te_data->uid = le32_to_cpu(resp->event_unique_id);
3730 	IWL_DEBUG_TE(mvm, "TIME_EVENT_CMD response - UID = 0x%x\n",
3731 		     te_data->uid);
3732 
3733 	spin_lock_bh(&mvm->time_event_lock);
3734 	list_add_tail(&te_data->list, &mvm->aux_roc_te_list);
3735 	spin_unlock_bh(&mvm->time_event_lock);
3736 
3737 	return true;
3738 }
3739 
3740 #define AUX_ROC_MIN_DURATION MSEC_TO_TU(100)
3741 #define AUX_ROC_MIN_DELAY MSEC_TO_TU(200)
3742 #define AUX_ROC_MAX_DELAY MSEC_TO_TU(600)
3743 #define AUX_ROC_SAFETY_BUFFER MSEC_TO_TU(20)
3744 #define AUX_ROC_MIN_SAFETY_BUFFER MSEC_TO_TU(10)
3745 static int iwl_mvm_send_aux_roc_cmd(struct iwl_mvm *mvm,
3746 				    struct ieee80211_channel *channel,
3747 				    struct ieee80211_vif *vif,
3748 				    int duration)
3749 {
3750 	int res;
3751 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3752 	struct iwl_mvm_time_event_data *te_data = &mvmvif->hs_time_event_data;
3753 	static const u16 time_event_response[] = { HOT_SPOT_CMD };
3754 	struct iwl_notification_wait wait_time_event;
3755 	u32 dtim_interval = vif->bss_conf.dtim_period *
3756 		vif->bss_conf.beacon_int;
3757 	u32 req_dur, delay;
3758 	struct iwl_hs20_roc_req aux_roc_req = {
3759 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
3760 		.id_and_color =
3761 			cpu_to_le32(FW_CMD_ID_AND_COLOR(MAC_INDEX_AUX, 0)),
3762 		.sta_id_and_color = cpu_to_le32(mvm->aux_sta.sta_id),
3763 	};
3764 	struct iwl_hs20_roc_req_tail *tail = iwl_mvm_chan_info_cmd_tail(mvm,
3765 		&aux_roc_req.channel_info);
3766 	u16 len = sizeof(aux_roc_req) - iwl_mvm_chan_info_padding(mvm);
3767 
3768 	/* Set the channel info data */
3769 	iwl_mvm_set_chan_info(mvm, &aux_roc_req.channel_info, channel->hw_value,
3770 			      iwl_mvm_phy_band_from_nl80211(channel->band),
3771 			      PHY_VHT_CHANNEL_MODE20,
3772 			      0);
3773 
3774 	/* Set the time and duration */
3775 	tail->apply_time = cpu_to_le32(iwl_mvm_get_systime(mvm));
3776 
3777 	delay = AUX_ROC_MIN_DELAY;
3778 	req_dur = MSEC_TO_TU(duration);
3779 
3780 	/*
3781 	 * If we are associated we want the delay time to be at least one
3782 	 * dtim interval so that the FW can wait until after the DTIM and
3783 	 * then start the time event, this will potentially allow us to
3784 	 * remain off-channel for the max duration.
3785 	 * Since we want to use almost a whole dtim interval we would also
3786 	 * like the delay to be for 2-3 dtim intervals, in case there are
3787 	 * other time events with higher priority.
3788 	 */
3789 	if (vif->bss_conf.assoc) {
3790 		delay = min_t(u32, dtim_interval * 3, AUX_ROC_MAX_DELAY);
3791 		/* We cannot remain off-channel longer than the DTIM interval */
3792 		if (dtim_interval <= req_dur) {
3793 			req_dur = dtim_interval - AUX_ROC_SAFETY_BUFFER;
3794 			if (req_dur <= AUX_ROC_MIN_DURATION)
3795 				req_dur = dtim_interval -
3796 					AUX_ROC_MIN_SAFETY_BUFFER;
3797 		}
3798 	}
3799 
3800 	tail->duration = cpu_to_le32(req_dur);
3801 	tail->apply_time_max_delay = cpu_to_le32(delay);
3802 
3803 	IWL_DEBUG_TE(mvm,
3804 		     "ROC: Requesting to remain on channel %u for %ums\n",
3805 		     channel->hw_value, req_dur);
3806 	IWL_DEBUG_TE(mvm,
3807 		     "\t(requested = %ums, max_delay = %ums, dtim_interval = %ums)\n",
3808 		     duration, delay, dtim_interval);
3809 
3810 	/* Set the node address */
3811 	memcpy(tail->node_addr, vif->addr, ETH_ALEN);
3812 
3813 	lockdep_assert_held(&mvm->mutex);
3814 
3815 	spin_lock_bh(&mvm->time_event_lock);
3816 
3817 	if (WARN_ON(te_data->id == HOT_SPOT_CMD)) {
3818 		spin_unlock_bh(&mvm->time_event_lock);
3819 		return -EIO;
3820 	}
3821 
3822 	te_data->vif = vif;
3823 	te_data->duration = duration;
3824 	te_data->id = HOT_SPOT_CMD;
3825 
3826 	spin_unlock_bh(&mvm->time_event_lock);
3827 
3828 	/*
3829 	 * Use a notification wait, which really just processes the
3830 	 * command response and doesn't wait for anything, in order
3831 	 * to be able to process the response and get the UID inside
3832 	 * the RX path. Using CMD_WANT_SKB doesn't work because it
3833 	 * stores the buffer and then wakes up this thread, by which
3834 	 * time another notification (that the time event started)
3835 	 * might already be processed unsuccessfully.
3836 	 */
3837 	iwl_init_notification_wait(&mvm->notif_wait, &wait_time_event,
3838 				   time_event_response,
3839 				   ARRAY_SIZE(time_event_response),
3840 				   iwl_mvm_rx_aux_roc, te_data);
3841 
3842 	res = iwl_mvm_send_cmd_pdu(mvm, HOT_SPOT_CMD, 0, len,
3843 				   &aux_roc_req);
3844 
3845 	if (res) {
3846 		IWL_ERR(mvm, "Couldn't send HOT_SPOT_CMD: %d\n", res);
3847 		iwl_remove_notification(&mvm->notif_wait, &wait_time_event);
3848 		goto out_clear_te;
3849 	}
3850 
3851 	/* No need to wait for anything, so just pass 1 (0 isn't valid) */
3852 	res = iwl_wait_notification(&mvm->notif_wait, &wait_time_event, 1);
3853 	/* should never fail */
3854 	WARN_ON_ONCE(res);
3855 
3856 	if (res) {
3857  out_clear_te:
3858 		spin_lock_bh(&mvm->time_event_lock);
3859 		iwl_mvm_te_clear_data(mvm, te_data);
3860 		spin_unlock_bh(&mvm->time_event_lock);
3861 	}
3862 
3863 	return res;
3864 }
3865 
3866 static int iwl_mvm_roc(struct ieee80211_hw *hw,
3867 		       struct ieee80211_vif *vif,
3868 		       struct ieee80211_channel *channel,
3869 		       int duration,
3870 		       enum ieee80211_roc_type type)
3871 {
3872 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
3873 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
3874 	struct cfg80211_chan_def chandef;
3875 	struct iwl_mvm_phy_ctxt *phy_ctxt;
3876 	bool band_change_removal;
3877 	int ret, i;
3878 
3879 	IWL_DEBUG_MAC80211(mvm, "enter (%d, %d, %d)\n", channel->hw_value,
3880 			   duration, type);
3881 
3882 	/*
3883 	 * Flush the done work, just in case it's still pending, so that
3884 	 * the work it does can complete and we can accept new frames.
3885 	 */
3886 	flush_work(&mvm->roc_done_wk);
3887 
3888 	mutex_lock(&mvm->mutex);
3889 
3890 	switch (vif->type) {
3891 	case NL80211_IFTYPE_STATION:
3892 		if (fw_has_capa(&mvm->fw->ucode_capa,
3893 				IWL_UCODE_TLV_CAPA_HOTSPOT_SUPPORT)) {
3894 			/* Use aux roc framework (HS20) */
3895 			if (iwl_fw_lookup_cmd_ver(mvm->fw, LONG_GROUP,
3896 						  ADD_STA, 0) >= 12) {
3897 				u32 lmac_id;
3898 
3899 				lmac_id = iwl_mvm_get_lmac_id(mvm->fw,
3900 							      channel->band);
3901 				ret = iwl_mvm_add_aux_sta(mvm, lmac_id);
3902 				if (WARN(ret,
3903 					 "Failed to allocate aux station"))
3904 					goto out_unlock;
3905 			}
3906 			ret = iwl_mvm_send_aux_roc_cmd(mvm, channel,
3907 						       vif, duration);
3908 			goto out_unlock;
3909 		}
3910 		IWL_ERR(mvm, "hotspot not supported\n");
3911 		ret = -EINVAL;
3912 		goto out_unlock;
3913 	case NL80211_IFTYPE_P2P_DEVICE:
3914 		/* handle below */
3915 		break;
3916 	default:
3917 		IWL_ERR(mvm, "vif isn't P2P_DEVICE: %d\n", vif->type);
3918 		ret = -EINVAL;
3919 		goto out_unlock;
3920 	}
3921 
3922 	for (i = 0; i < NUM_PHY_CTX; i++) {
3923 		phy_ctxt = &mvm->phy_ctxts[i];
3924 		if (phy_ctxt->ref == 0 || mvmvif->phy_ctxt == phy_ctxt)
3925 			continue;
3926 
3927 		if (phy_ctxt->ref && channel == phy_ctxt->channel) {
3928 			/*
3929 			 * Unbind the P2P_DEVICE from the current PHY context,
3930 			 * and if the PHY context is not used remove it.
3931 			 */
3932 			ret = iwl_mvm_binding_remove_vif(mvm, vif);
3933 			if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3934 				goto out_unlock;
3935 
3936 			iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
3937 
3938 			/* Bind the P2P_DEVICE to the current PHY Context */
3939 			mvmvif->phy_ctxt = phy_ctxt;
3940 
3941 			ret = iwl_mvm_binding_add_vif(mvm, vif);
3942 			if (WARN(ret, "Failed binding P2P_DEVICE\n"))
3943 				goto out_unlock;
3944 
3945 			iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
3946 			goto schedule_time_event;
3947 		}
3948 	}
3949 
3950 	/* Need to update the PHY context only if the ROC channel changed */
3951 	if (channel == mvmvif->phy_ctxt->channel)
3952 		goto schedule_time_event;
3953 
3954 	cfg80211_chandef_create(&chandef, channel, NL80211_CHAN_NO_HT);
3955 
3956 	/*
3957 	 * Check if the remain-on-channel is on a different band and that
3958 	 * requires context removal, see iwl_mvm_phy_ctxt_changed(). If
3959 	 * so, we'll need to release and then re-configure here, since we
3960 	 * must not remove a PHY context that's part of a binding.
3961 	 */
3962 	band_change_removal =
3963 		fw_has_capa(&mvm->fw->ucode_capa,
3964 			    IWL_UCODE_TLV_CAPA_BINDING_CDB_SUPPORT) &&
3965 		mvmvif->phy_ctxt->channel->band != chandef.chan->band;
3966 
3967 	if (mvmvif->phy_ctxt->ref == 1 && !band_change_removal) {
3968 		/*
3969 		 * Change the PHY context configuration as it is currently
3970 		 * referenced only by the P2P Device MAC (and we can modify it)
3971 		 */
3972 		ret = iwl_mvm_phy_ctxt_changed(mvm, mvmvif->phy_ctxt,
3973 					       &chandef, 1, 1);
3974 		if (ret)
3975 			goto out_unlock;
3976 	} else {
3977 		/*
3978 		 * The PHY context is shared with other MACs (or we're trying to
3979 		 * switch bands), so remove the P2P Device from the binding,
3980 		 * allocate an new PHY context and create a new binding.
3981 		 */
3982 		phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
3983 		if (!phy_ctxt) {
3984 			ret = -ENOSPC;
3985 			goto out_unlock;
3986 		}
3987 
3988 		ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, &chandef,
3989 					       1, 1);
3990 		if (ret) {
3991 			IWL_ERR(mvm, "Failed to change PHY context\n");
3992 			goto out_unlock;
3993 		}
3994 
3995 		/* Unbind the P2P_DEVICE from the current PHY context */
3996 		ret = iwl_mvm_binding_remove_vif(mvm, vif);
3997 		if (WARN(ret, "Failed unbinding P2P_DEVICE\n"))
3998 			goto out_unlock;
3999 
4000 		iwl_mvm_phy_ctxt_unref(mvm, mvmvif->phy_ctxt);
4001 
4002 		/* Bind the P2P_DEVICE to the new allocated PHY context */
4003 		mvmvif->phy_ctxt = phy_ctxt;
4004 
4005 		ret = iwl_mvm_binding_add_vif(mvm, vif);
4006 		if (WARN(ret, "Failed binding P2P_DEVICE\n"))
4007 			goto out_unlock;
4008 
4009 		iwl_mvm_phy_ctxt_ref(mvm, mvmvif->phy_ctxt);
4010 	}
4011 
4012 schedule_time_event:
4013 	/* Schedule the time events */
4014 	ret = iwl_mvm_start_p2p_roc(mvm, vif, duration, type);
4015 
4016 out_unlock:
4017 	mutex_unlock(&mvm->mutex);
4018 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4019 	return ret;
4020 }
4021 
4022 static int iwl_mvm_cancel_roc(struct ieee80211_hw *hw,
4023 			      struct ieee80211_vif *vif)
4024 {
4025 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4026 
4027 	IWL_DEBUG_MAC80211(mvm, "enter\n");
4028 
4029 	mutex_lock(&mvm->mutex);
4030 	iwl_mvm_stop_roc(mvm, vif);
4031 	mutex_unlock(&mvm->mutex);
4032 
4033 	IWL_DEBUG_MAC80211(mvm, "leave\n");
4034 	return 0;
4035 }
4036 
4037 struct iwl_mvm_ftm_responder_iter_data {
4038 	bool responder;
4039 	struct ieee80211_chanctx_conf *ctx;
4040 };
4041 
4042 static void iwl_mvm_ftm_responder_chanctx_iter(void *_data, u8 *mac,
4043 					       struct ieee80211_vif *vif)
4044 {
4045 	struct iwl_mvm_ftm_responder_iter_data *data = _data;
4046 
4047 	if (rcu_access_pointer(vif->chanctx_conf) == data->ctx &&
4048 	    vif->type == NL80211_IFTYPE_AP && vif->bss_conf.ftmr_params)
4049 		data->responder = true;
4050 }
4051 
4052 static bool iwl_mvm_is_ftm_responder_chanctx(struct iwl_mvm *mvm,
4053 					     struct ieee80211_chanctx_conf *ctx)
4054 {
4055 	struct iwl_mvm_ftm_responder_iter_data data = {
4056 		.responder = false,
4057 		.ctx = ctx,
4058 	};
4059 
4060 	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
4061 					IEEE80211_IFACE_ITER_NORMAL,
4062 					iwl_mvm_ftm_responder_chanctx_iter,
4063 					&data);
4064 	return data.responder;
4065 }
4066 
4067 static int __iwl_mvm_add_chanctx(struct iwl_mvm *mvm,
4068 				 struct ieee80211_chanctx_conf *ctx)
4069 {
4070 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4071 	struct iwl_mvm_phy_ctxt *phy_ctxt;
4072 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4073 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4074 	int ret;
4075 
4076 	lockdep_assert_held(&mvm->mutex);
4077 
4078 	IWL_DEBUG_MAC80211(mvm, "Add channel context\n");
4079 
4080 	phy_ctxt = iwl_mvm_get_free_phy_ctxt(mvm);
4081 	if (!phy_ctxt) {
4082 		ret = -ENOSPC;
4083 		goto out;
4084 	}
4085 
4086 	ret = iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4087 				       ctx->rx_chains_static,
4088 				       ctx->rx_chains_dynamic);
4089 	if (ret) {
4090 		IWL_ERR(mvm, "Failed to add PHY context\n");
4091 		goto out;
4092 	}
4093 
4094 	iwl_mvm_phy_ctxt_ref(mvm, phy_ctxt);
4095 	*phy_ctxt_id = phy_ctxt->id;
4096 out:
4097 	return ret;
4098 }
4099 
4100 static int iwl_mvm_add_chanctx(struct ieee80211_hw *hw,
4101 			       struct ieee80211_chanctx_conf *ctx)
4102 {
4103 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4104 	int ret;
4105 
4106 	mutex_lock(&mvm->mutex);
4107 	ret = __iwl_mvm_add_chanctx(mvm, ctx);
4108 	mutex_unlock(&mvm->mutex);
4109 
4110 	return ret;
4111 }
4112 
4113 static void __iwl_mvm_remove_chanctx(struct iwl_mvm *mvm,
4114 				     struct ieee80211_chanctx_conf *ctx)
4115 {
4116 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4117 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4118 
4119 	lockdep_assert_held(&mvm->mutex);
4120 
4121 	iwl_mvm_phy_ctxt_unref(mvm, phy_ctxt);
4122 }
4123 
4124 static void iwl_mvm_remove_chanctx(struct ieee80211_hw *hw,
4125 				   struct ieee80211_chanctx_conf *ctx)
4126 {
4127 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4128 
4129 	mutex_lock(&mvm->mutex);
4130 	__iwl_mvm_remove_chanctx(mvm, ctx);
4131 	mutex_unlock(&mvm->mutex);
4132 }
4133 
4134 static void iwl_mvm_change_chanctx(struct ieee80211_hw *hw,
4135 				   struct ieee80211_chanctx_conf *ctx,
4136 				   u32 changed)
4137 {
4138 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4139 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4140 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4141 	bool responder = iwl_mvm_is_ftm_responder_chanctx(mvm, ctx);
4142 	struct cfg80211_chan_def *def = responder ? &ctx->def : &ctx->min_def;
4143 
4144 	if (WARN_ONCE((phy_ctxt->ref > 1) &&
4145 		      (changed & ~(IEEE80211_CHANCTX_CHANGE_WIDTH |
4146 				   IEEE80211_CHANCTX_CHANGE_RX_CHAINS |
4147 				   IEEE80211_CHANCTX_CHANGE_RADAR |
4148 				   IEEE80211_CHANCTX_CHANGE_MIN_WIDTH)),
4149 		      "Cannot change PHY. Ref=%d, changed=0x%X\n",
4150 		      phy_ctxt->ref, changed))
4151 		return;
4152 
4153 	mutex_lock(&mvm->mutex);
4154 
4155 	/* we are only changing the min_width, may be a noop */
4156 	if (changed == IEEE80211_CHANCTX_CHANGE_MIN_WIDTH) {
4157 		if (phy_ctxt->width == def->width)
4158 			goto out_unlock;
4159 
4160 		/* we are just toggling between 20_NOHT and 20 */
4161 		if (phy_ctxt->width <= NL80211_CHAN_WIDTH_20 &&
4162 		    def->width <= NL80211_CHAN_WIDTH_20)
4163 			goto out_unlock;
4164 	}
4165 
4166 	iwl_mvm_bt_coex_vif_change(mvm);
4167 	iwl_mvm_phy_ctxt_changed(mvm, phy_ctxt, def,
4168 				 ctx->rx_chains_static,
4169 				 ctx->rx_chains_dynamic);
4170 
4171 out_unlock:
4172 	mutex_unlock(&mvm->mutex);
4173 }
4174 
4175 static int __iwl_mvm_assign_vif_chanctx(struct iwl_mvm *mvm,
4176 					struct ieee80211_vif *vif,
4177 					struct ieee80211_chanctx_conf *ctx,
4178 					bool switching_chanctx)
4179 {
4180 	u16 *phy_ctxt_id = (u16 *)ctx->drv_priv;
4181 	struct iwl_mvm_phy_ctxt *phy_ctxt = &mvm->phy_ctxts[*phy_ctxt_id];
4182 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4183 	int ret;
4184 
4185 	lockdep_assert_held(&mvm->mutex);
4186 
4187 	mvmvif->phy_ctxt = phy_ctxt;
4188 
4189 	switch (vif->type) {
4190 	case NL80211_IFTYPE_AP:
4191 		/* only needed if we're switching chanctx (i.e. during CSA) */
4192 		if (switching_chanctx) {
4193 			mvmvif->ap_ibss_active = true;
4194 			break;
4195 		}
4196 		fallthrough;
4197 	case NL80211_IFTYPE_ADHOC:
4198 		/*
4199 		 * The AP binding flow is handled as part of the start_ap flow
4200 		 * (in bss_info_changed), similarly for IBSS.
4201 		 */
4202 		ret = 0;
4203 		goto out;
4204 	case NL80211_IFTYPE_STATION:
4205 		mvmvif->csa_bcn_pending = false;
4206 		break;
4207 	case NL80211_IFTYPE_MONITOR:
4208 		/* always disable PS when a monitor interface is active */
4209 		mvmvif->ps_disabled = true;
4210 		break;
4211 	default:
4212 		ret = -EINVAL;
4213 		goto out;
4214 	}
4215 
4216 	ret = iwl_mvm_binding_add_vif(mvm, vif);
4217 	if (ret)
4218 		goto out;
4219 
4220 	/*
4221 	 * Power state must be updated before quotas,
4222 	 * otherwise fw will complain.
4223 	 */
4224 	iwl_mvm_power_update_mac(mvm);
4225 
4226 	/* Setting the quota at this stage is only required for monitor
4227 	 * interfaces. For the other types, the bss_info changed flow
4228 	 * will handle quota settings.
4229 	 */
4230 	if (vif->type == NL80211_IFTYPE_MONITOR) {
4231 		mvmvif->monitor_active = true;
4232 		ret = iwl_mvm_update_quotas(mvm, false, NULL);
4233 		if (ret)
4234 			goto out_remove_binding;
4235 
4236 		ret = iwl_mvm_add_snif_sta(mvm, vif);
4237 		if (ret)
4238 			goto out_remove_binding;
4239 
4240 	}
4241 
4242 	/* Handle binding during CSA */
4243 	if (vif->type == NL80211_IFTYPE_AP) {
4244 		iwl_mvm_update_quotas(mvm, false, NULL);
4245 		iwl_mvm_mac_ctxt_changed(mvm, vif, false, NULL);
4246 	}
4247 
4248 	if (switching_chanctx && vif->type == NL80211_IFTYPE_STATION) {
4249 		mvmvif->csa_bcn_pending = true;
4250 
4251 		if (!fw_has_capa(&mvm->fw->ucode_capa,
4252 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4253 			u32 duration = 3 * vif->bss_conf.beacon_int;
4254 
4255 			/* Protect the session to make sure we hear the first
4256 			 * beacon on the new channel.
4257 			 */
4258 			iwl_mvm_protect_session(mvm, vif, duration, duration,
4259 						vif->bss_conf.beacon_int / 2,
4260 						true);
4261 		}
4262 
4263 		iwl_mvm_update_quotas(mvm, false, NULL);
4264 	}
4265 
4266 	goto out;
4267 
4268 out_remove_binding:
4269 	iwl_mvm_binding_remove_vif(mvm, vif);
4270 	iwl_mvm_power_update_mac(mvm);
4271 out:
4272 	if (ret)
4273 		mvmvif->phy_ctxt = NULL;
4274 	return ret;
4275 }
4276 static int iwl_mvm_assign_vif_chanctx(struct ieee80211_hw *hw,
4277 				      struct ieee80211_vif *vif,
4278 				      struct ieee80211_chanctx_conf *ctx)
4279 {
4280 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4281 	int ret;
4282 
4283 	mutex_lock(&mvm->mutex);
4284 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vif, ctx, false);
4285 	mutex_unlock(&mvm->mutex);
4286 
4287 	return ret;
4288 }
4289 
4290 static void __iwl_mvm_unassign_vif_chanctx(struct iwl_mvm *mvm,
4291 					   struct ieee80211_vif *vif,
4292 					   struct ieee80211_chanctx_conf *ctx,
4293 					   bool switching_chanctx)
4294 {
4295 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4296 	struct ieee80211_vif *disabled_vif = NULL;
4297 
4298 	lockdep_assert_held(&mvm->mutex);
4299 	iwl_mvm_remove_time_event(mvm, mvmvif, &mvmvif->time_event_data);
4300 
4301 	switch (vif->type) {
4302 	case NL80211_IFTYPE_ADHOC:
4303 		goto out;
4304 	case NL80211_IFTYPE_MONITOR:
4305 		mvmvif->monitor_active = false;
4306 		mvmvif->ps_disabled = false;
4307 		iwl_mvm_rm_snif_sta(mvm, vif);
4308 		break;
4309 	case NL80211_IFTYPE_AP:
4310 		/* This part is triggered only during CSA */
4311 		if (!switching_chanctx || !mvmvif->ap_ibss_active)
4312 			goto out;
4313 
4314 		mvmvif->csa_countdown = false;
4315 
4316 		/* Set CS bit on all the stations */
4317 		iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, true);
4318 
4319 		/* Save blocked iface, the timeout is set on the next beacon */
4320 		rcu_assign_pointer(mvm->csa_tx_blocked_vif, vif);
4321 
4322 		mvmvif->ap_ibss_active = false;
4323 		break;
4324 	case NL80211_IFTYPE_STATION:
4325 		if (!switching_chanctx)
4326 			break;
4327 
4328 		disabled_vif = vif;
4329 
4330 		if (!fw_has_capa(&mvm->fw->ucode_capa,
4331 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD))
4332 			iwl_mvm_mac_ctxt_changed(mvm, vif, true, NULL);
4333 		break;
4334 	default:
4335 		break;
4336 	}
4337 
4338 	iwl_mvm_update_quotas(mvm, false, disabled_vif);
4339 	iwl_mvm_binding_remove_vif(mvm, vif);
4340 
4341 out:
4342 	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD) &&
4343 	    switching_chanctx)
4344 		return;
4345 	mvmvif->phy_ctxt = NULL;
4346 	iwl_mvm_power_update_mac(mvm);
4347 }
4348 
4349 static void iwl_mvm_unassign_vif_chanctx(struct ieee80211_hw *hw,
4350 					 struct ieee80211_vif *vif,
4351 					 struct ieee80211_chanctx_conf *ctx)
4352 {
4353 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4354 
4355 	mutex_lock(&mvm->mutex);
4356 	__iwl_mvm_unassign_vif_chanctx(mvm, vif, ctx, false);
4357 	mutex_unlock(&mvm->mutex);
4358 }
4359 
4360 static int
4361 iwl_mvm_switch_vif_chanctx_swap(struct iwl_mvm *mvm,
4362 				struct ieee80211_vif_chanctx_switch *vifs)
4363 {
4364 	int ret;
4365 
4366 	mutex_lock(&mvm->mutex);
4367 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4368 	__iwl_mvm_remove_chanctx(mvm, vifs[0].old_ctx);
4369 
4370 	ret = __iwl_mvm_add_chanctx(mvm, vifs[0].new_ctx);
4371 	if (ret) {
4372 		IWL_ERR(mvm, "failed to add new_ctx during channel switch\n");
4373 		goto out_reassign;
4374 	}
4375 
4376 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4377 					   true);
4378 	if (ret) {
4379 		IWL_ERR(mvm,
4380 			"failed to assign new_ctx during channel switch\n");
4381 		goto out_remove;
4382 	}
4383 
4384 	/* we don't support TDLS during DCM - can be caused by channel switch */
4385 	if (iwl_mvm_phy_ctx_count(mvm) > 1)
4386 		iwl_mvm_teardown_tdls_peers(mvm);
4387 
4388 	goto out;
4389 
4390 out_remove:
4391 	__iwl_mvm_remove_chanctx(mvm, vifs[0].new_ctx);
4392 
4393 out_reassign:
4394 	if (__iwl_mvm_add_chanctx(mvm, vifs[0].old_ctx)) {
4395 		IWL_ERR(mvm, "failed to add old_ctx back after failure.\n");
4396 		goto out_restart;
4397 	}
4398 
4399 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4400 					 true)) {
4401 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4402 		goto out_restart;
4403 	}
4404 
4405 	goto out;
4406 
4407 out_restart:
4408 	/* things keep failing, better restart the hw */
4409 	iwl_mvm_nic_restart(mvm, false);
4410 
4411 out:
4412 	mutex_unlock(&mvm->mutex);
4413 
4414 	return ret;
4415 }
4416 
4417 static int
4418 iwl_mvm_switch_vif_chanctx_reassign(struct iwl_mvm *mvm,
4419 				    struct ieee80211_vif_chanctx_switch *vifs)
4420 {
4421 	int ret;
4422 
4423 	mutex_lock(&mvm->mutex);
4424 	__iwl_mvm_unassign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx, true);
4425 
4426 	ret = __iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].new_ctx,
4427 					   true);
4428 	if (ret) {
4429 		IWL_ERR(mvm,
4430 			"failed to assign new_ctx during channel switch\n");
4431 		goto out_reassign;
4432 	}
4433 
4434 	goto out;
4435 
4436 out_reassign:
4437 	if (__iwl_mvm_assign_vif_chanctx(mvm, vifs[0].vif, vifs[0].old_ctx,
4438 					 true)) {
4439 		IWL_ERR(mvm, "failed to reassign old_ctx after failure.\n");
4440 		goto out_restart;
4441 	}
4442 
4443 	goto out;
4444 
4445 out_restart:
4446 	/* things keep failing, better restart the hw */
4447 	iwl_mvm_nic_restart(mvm, false);
4448 
4449 out:
4450 	mutex_unlock(&mvm->mutex);
4451 
4452 	return ret;
4453 }
4454 
4455 static int iwl_mvm_switch_vif_chanctx(struct ieee80211_hw *hw,
4456 				      struct ieee80211_vif_chanctx_switch *vifs,
4457 				      int n_vifs,
4458 				      enum ieee80211_chanctx_switch_mode mode)
4459 {
4460 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4461 	int ret;
4462 
4463 	/* we only support a single-vif right now */
4464 	if (n_vifs > 1)
4465 		return -EOPNOTSUPP;
4466 
4467 	switch (mode) {
4468 	case CHANCTX_SWMODE_SWAP_CONTEXTS:
4469 		ret = iwl_mvm_switch_vif_chanctx_swap(mvm, vifs);
4470 		break;
4471 	case CHANCTX_SWMODE_REASSIGN_VIF:
4472 		ret = iwl_mvm_switch_vif_chanctx_reassign(mvm, vifs);
4473 		break;
4474 	default:
4475 		ret = -EOPNOTSUPP;
4476 		break;
4477 	}
4478 
4479 	return ret;
4480 }
4481 
4482 static int iwl_mvm_tx_last_beacon(struct ieee80211_hw *hw)
4483 {
4484 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4485 
4486 	return mvm->ibss_manager;
4487 }
4488 
4489 static int iwl_mvm_set_tim(struct ieee80211_hw *hw,
4490 			   struct ieee80211_sta *sta,
4491 			   bool set)
4492 {
4493 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4494 	struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
4495 
4496 	if (!mvm_sta || !mvm_sta->vif) {
4497 		IWL_ERR(mvm, "Station is not associated to a vif\n");
4498 		return -EINVAL;
4499 	}
4500 
4501 	return iwl_mvm_mac_ctxt_beacon_changed(mvm, mvm_sta->vif);
4502 }
4503 
4504 #ifdef CONFIG_NL80211_TESTMODE
4505 static const struct nla_policy iwl_mvm_tm_policy[IWL_MVM_TM_ATTR_MAX + 1] = {
4506 	[IWL_MVM_TM_ATTR_CMD] = { .type = NLA_U32 },
4507 	[IWL_MVM_TM_ATTR_NOA_DURATION] = { .type = NLA_U32 },
4508 	[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE] = { .type = NLA_U32 },
4509 };
4510 
4511 static int __iwl_mvm_mac_testmode_cmd(struct iwl_mvm *mvm,
4512 				      struct ieee80211_vif *vif,
4513 				      void *data, int len)
4514 {
4515 	struct nlattr *tb[IWL_MVM_TM_ATTR_MAX + 1];
4516 	int err;
4517 	u32 noa_duration;
4518 
4519 	err = nla_parse_deprecated(tb, IWL_MVM_TM_ATTR_MAX, data, len,
4520 				   iwl_mvm_tm_policy, NULL);
4521 	if (err)
4522 		return err;
4523 
4524 	if (!tb[IWL_MVM_TM_ATTR_CMD])
4525 		return -EINVAL;
4526 
4527 	switch (nla_get_u32(tb[IWL_MVM_TM_ATTR_CMD])) {
4528 	case IWL_MVM_TM_CMD_SET_NOA:
4529 		if (!vif || vif->type != NL80211_IFTYPE_AP || !vif->p2p ||
4530 		    !vif->bss_conf.enable_beacon ||
4531 		    !tb[IWL_MVM_TM_ATTR_NOA_DURATION])
4532 			return -EINVAL;
4533 
4534 		noa_duration = nla_get_u32(tb[IWL_MVM_TM_ATTR_NOA_DURATION]);
4535 		if (noa_duration >= vif->bss_conf.beacon_int)
4536 			return -EINVAL;
4537 
4538 		mvm->noa_duration = noa_duration;
4539 		mvm->noa_vif = vif;
4540 
4541 		return iwl_mvm_update_quotas(mvm, true, NULL);
4542 	case IWL_MVM_TM_CMD_SET_BEACON_FILTER:
4543 		/* must be associated client vif - ignore authorized */
4544 		if (!vif || vif->type != NL80211_IFTYPE_STATION ||
4545 		    !vif->bss_conf.assoc || !vif->bss_conf.dtim_period ||
4546 		    !tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE])
4547 			return -EINVAL;
4548 
4549 		if (nla_get_u32(tb[IWL_MVM_TM_ATTR_BEACON_FILTER_STATE]))
4550 			return iwl_mvm_enable_beacon_filter(mvm, vif, 0);
4551 		return iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4552 	}
4553 
4554 	return -EOPNOTSUPP;
4555 }
4556 
4557 static int iwl_mvm_mac_testmode_cmd(struct ieee80211_hw *hw,
4558 				    struct ieee80211_vif *vif,
4559 				    void *data, int len)
4560 {
4561 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4562 	int err;
4563 
4564 	mutex_lock(&mvm->mutex);
4565 	err = __iwl_mvm_mac_testmode_cmd(mvm, vif, data, len);
4566 	mutex_unlock(&mvm->mutex);
4567 
4568 	return err;
4569 }
4570 #endif
4571 
4572 static void iwl_mvm_channel_switch(struct ieee80211_hw *hw,
4573 				   struct ieee80211_vif *vif,
4574 				   struct ieee80211_channel_switch *chsw)
4575 {
4576 	/* By implementing this operation, we prevent mac80211 from
4577 	 * starting its own channel switch timer, so that we can call
4578 	 * ieee80211_chswitch_done() ourselves at the right time
4579 	 * (which is when the absence time event starts).
4580 	 */
4581 
4582 	IWL_DEBUG_MAC80211(IWL_MAC80211_GET_MVM(hw),
4583 			   "dummy channel switch op\n");
4584 }
4585 
4586 static int iwl_mvm_schedule_client_csa(struct iwl_mvm *mvm,
4587 				       struct ieee80211_vif *vif,
4588 				       struct ieee80211_channel_switch *chsw)
4589 {
4590 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4591 	struct iwl_chan_switch_te_cmd cmd = {
4592 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4593 							  mvmvif->color)),
4594 		.action = cpu_to_le32(FW_CTXT_ACTION_ADD),
4595 		.tsf = cpu_to_le32(chsw->timestamp),
4596 		.cs_count = chsw->count,
4597 		.cs_mode = chsw->block_tx,
4598 	};
4599 
4600 	lockdep_assert_held(&mvm->mutex);
4601 
4602 	if (chsw->delay)
4603 		cmd.cs_delayed_bcn_count =
4604 			DIV_ROUND_UP(chsw->delay, vif->bss_conf.beacon_int);
4605 
4606 	return iwl_mvm_send_cmd_pdu(mvm,
4607 				    WIDE_ID(MAC_CONF_GROUP,
4608 					    CHANNEL_SWITCH_TIME_EVENT_CMD),
4609 				    0, sizeof(cmd), &cmd);
4610 }
4611 
4612 static int iwl_mvm_old_pre_chan_sw_sta(struct iwl_mvm *mvm,
4613 				       struct ieee80211_vif *vif,
4614 				       struct ieee80211_channel_switch *chsw)
4615 {
4616 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4617 	u32 apply_time;
4618 
4619 	/* Schedule the time event to a bit before beacon 1,
4620 	 * to make sure we're in the new channel when the
4621 	 * GO/AP arrives. In case count <= 1 immediately schedule the
4622 	 * TE (this might result with some packet loss or connection
4623 	 * loss).
4624 	 */
4625 	if (chsw->count <= 1)
4626 		apply_time = 0;
4627 	else
4628 		apply_time = chsw->device_timestamp +
4629 			((vif->bss_conf.beacon_int * (chsw->count - 1) -
4630 			  IWL_MVM_CHANNEL_SWITCH_TIME_CLIENT) * 1024);
4631 
4632 	if (chsw->block_tx)
4633 		iwl_mvm_csa_client_absent(mvm, vif);
4634 
4635 	if (mvmvif->bf_data.bf_enabled) {
4636 		int ret = iwl_mvm_disable_beacon_filter(mvm, vif, 0);
4637 
4638 		if (ret)
4639 			return ret;
4640 	}
4641 
4642 	iwl_mvm_schedule_csa_period(mvm, vif, vif->bss_conf.beacon_int,
4643 				    apply_time);
4644 
4645 	return 0;
4646 }
4647 
4648 #define IWL_MAX_CSA_BLOCK_TX 1500
4649 static int iwl_mvm_pre_channel_switch(struct ieee80211_hw *hw,
4650 				      struct ieee80211_vif *vif,
4651 				      struct ieee80211_channel_switch *chsw)
4652 {
4653 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4654 	struct ieee80211_vif *csa_vif;
4655 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4656 	int ret;
4657 
4658 	mutex_lock(&mvm->mutex);
4659 
4660 	mvmvif->csa_failed = false;
4661 
4662 	IWL_DEBUG_MAC80211(mvm, "pre CSA to freq %d\n",
4663 			   chsw->chandef.center_freq1);
4664 
4665 	iwl_fw_dbg_trigger_simple_stop(&mvm->fwrt,
4666 				       ieee80211_vif_to_wdev(vif),
4667 				       FW_DBG_TRIGGER_CHANNEL_SWITCH);
4668 
4669 	switch (vif->type) {
4670 	case NL80211_IFTYPE_AP:
4671 		csa_vif =
4672 			rcu_dereference_protected(mvm->csa_vif,
4673 						  lockdep_is_held(&mvm->mutex));
4674 		if (WARN_ONCE(csa_vif && csa_vif->csa_active,
4675 			      "Another CSA is already in progress")) {
4676 			ret = -EBUSY;
4677 			goto out_unlock;
4678 		}
4679 
4680 		/* we still didn't unblock tx. prevent new CS meanwhile */
4681 		if (rcu_dereference_protected(mvm->csa_tx_blocked_vif,
4682 					      lockdep_is_held(&mvm->mutex))) {
4683 			ret = -EBUSY;
4684 			goto out_unlock;
4685 		}
4686 
4687 		rcu_assign_pointer(mvm->csa_vif, vif);
4688 
4689 		if (WARN_ONCE(mvmvif->csa_countdown,
4690 			      "Previous CSA countdown didn't complete")) {
4691 			ret = -EBUSY;
4692 			goto out_unlock;
4693 		}
4694 
4695 		mvmvif->csa_target_freq = chsw->chandef.chan->center_freq;
4696 
4697 		break;
4698 	case NL80211_IFTYPE_STATION:
4699 		/*
4700 		 * We haven't configured the firmware to be associated yet since
4701 		 * we don't know the dtim period. In this case, the firmware can't
4702 		 * track the beacons.
4703 		 */
4704 		if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period) {
4705 			ret = -EBUSY;
4706 			goto out_unlock;
4707 		}
4708 
4709 		if (chsw->delay > IWL_MAX_CSA_BLOCK_TX)
4710 			schedule_delayed_work(&mvmvif->csa_work, 0);
4711 
4712 		if (chsw->block_tx) {
4713 			/*
4714 			 * In case of undetermined / long time with immediate
4715 			 * quiet monitor status to gracefully disconnect
4716 			 */
4717 			if (!chsw->count ||
4718 			    chsw->count * vif->bss_conf.beacon_int >
4719 			    IWL_MAX_CSA_BLOCK_TX)
4720 				schedule_delayed_work(&mvmvif->csa_work,
4721 						      msecs_to_jiffies(IWL_MAX_CSA_BLOCK_TX));
4722 		}
4723 
4724 		if (!fw_has_capa(&mvm->fw->ucode_capa,
4725 				 IWL_UCODE_TLV_CAPA_CHANNEL_SWITCH_CMD)) {
4726 			ret = iwl_mvm_old_pre_chan_sw_sta(mvm, vif, chsw);
4727 			if (ret)
4728 				goto out_unlock;
4729 		} else {
4730 			iwl_mvm_schedule_client_csa(mvm, vif, chsw);
4731 		}
4732 
4733 		mvmvif->csa_count = chsw->count;
4734 		mvmvif->csa_misbehave = false;
4735 		break;
4736 	default:
4737 		break;
4738 	}
4739 
4740 	mvmvif->ps_disabled = true;
4741 
4742 	ret = iwl_mvm_power_update_ps(mvm);
4743 	if (ret)
4744 		goto out_unlock;
4745 
4746 	/* we won't be on this channel any longer */
4747 	iwl_mvm_teardown_tdls_peers(mvm);
4748 
4749 out_unlock:
4750 	mutex_unlock(&mvm->mutex);
4751 
4752 	return ret;
4753 }
4754 
4755 static void iwl_mvm_channel_switch_rx_beacon(struct ieee80211_hw *hw,
4756 					     struct ieee80211_vif *vif,
4757 					     struct ieee80211_channel_switch *chsw)
4758 {
4759 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4760 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4761 	struct iwl_chan_switch_te_cmd cmd = {
4762 		.mac_id = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
4763 							  mvmvif->color)),
4764 		.action = cpu_to_le32(FW_CTXT_ACTION_MODIFY),
4765 		.tsf = cpu_to_le32(chsw->timestamp),
4766 		.cs_count = chsw->count,
4767 		.cs_mode = chsw->block_tx,
4768 	};
4769 
4770 	if (!fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_CS_MODIFY))
4771 		return;
4772 
4773 	IWL_DEBUG_MAC80211(mvm, "Modify CSA on mac %d count = %d (old %d) mode = %d\n",
4774 			   mvmvif->id, chsw->count, mvmvif->csa_count, chsw->block_tx);
4775 
4776 	if (chsw->count >= mvmvif->csa_count && chsw->block_tx) {
4777 		if (mvmvif->csa_misbehave) {
4778 			/* Second time, give up on this AP*/
4779 			iwl_mvm_abort_channel_switch(hw, vif);
4780 			ieee80211_chswitch_done(vif, false);
4781 			mvmvif->csa_misbehave = false;
4782 			return;
4783 		}
4784 		mvmvif->csa_misbehave = true;
4785 	}
4786 	mvmvif->csa_count = chsw->count;
4787 
4788 	mutex_lock(&mvm->mutex);
4789 	if (mvmvif->csa_failed)
4790 		goto out_unlock;
4791 
4792 	WARN_ON(iwl_mvm_send_cmd_pdu(mvm,
4793 				     WIDE_ID(MAC_CONF_GROUP,
4794 					     CHANNEL_SWITCH_TIME_EVENT_CMD),
4795 				     0, sizeof(cmd), &cmd));
4796 out_unlock:
4797 	mutex_unlock(&mvm->mutex);
4798 }
4799 
4800 static void iwl_mvm_flush_no_vif(struct iwl_mvm *mvm, u32 queues, bool drop)
4801 {
4802 	int i;
4803 
4804 	if (!iwl_mvm_has_new_tx_api(mvm)) {
4805 		if (drop) {
4806 			mutex_lock(&mvm->mutex);
4807 			iwl_mvm_flush_tx_path(mvm,
4808 				iwl_mvm_flushable_queues(mvm) & queues);
4809 			mutex_unlock(&mvm->mutex);
4810 		} else {
4811 			iwl_trans_wait_tx_queues_empty(mvm->trans, queues);
4812 		}
4813 		return;
4814 	}
4815 
4816 	mutex_lock(&mvm->mutex);
4817 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4818 		struct ieee80211_sta *sta;
4819 
4820 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4821 						lockdep_is_held(&mvm->mutex));
4822 		if (IS_ERR_OR_NULL(sta))
4823 			continue;
4824 
4825 		if (drop)
4826 			iwl_mvm_flush_sta_tids(mvm, i, 0xFFFF);
4827 		else
4828 			iwl_mvm_wait_sta_queues_empty(mvm,
4829 					iwl_mvm_sta_from_mac80211(sta));
4830 	}
4831 	mutex_unlock(&mvm->mutex);
4832 }
4833 
4834 static void iwl_mvm_mac_flush(struct ieee80211_hw *hw,
4835 			      struct ieee80211_vif *vif, u32 queues, bool drop)
4836 {
4837 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4838 	struct iwl_mvm_vif *mvmvif;
4839 	struct iwl_mvm_sta *mvmsta;
4840 	struct ieee80211_sta *sta;
4841 	int i;
4842 	u32 msk = 0;
4843 
4844 	if (!vif) {
4845 		iwl_mvm_flush_no_vif(mvm, queues, drop);
4846 		return;
4847 	}
4848 
4849 	if (vif->type != NL80211_IFTYPE_STATION)
4850 		return;
4851 
4852 	/* Make sure we're done with the deferred traffic before flushing */
4853 	flush_work(&mvm->add_stream_wk);
4854 
4855 	mutex_lock(&mvm->mutex);
4856 	mvmvif = iwl_mvm_vif_from_mac80211(vif);
4857 
4858 	/* flush the AP-station and all TDLS peers */
4859 	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
4860 		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
4861 						lockdep_is_held(&mvm->mutex));
4862 		if (IS_ERR_OR_NULL(sta))
4863 			continue;
4864 
4865 		mvmsta = iwl_mvm_sta_from_mac80211(sta);
4866 		if (mvmsta->vif != vif)
4867 			continue;
4868 
4869 		/* make sure only TDLS peers or the AP are flushed */
4870 		WARN_ON(i != mvmvif->ap_sta_id && !sta->tdls);
4871 
4872 		if (drop) {
4873 			if (iwl_mvm_flush_sta(mvm, mvmsta, false))
4874 				IWL_ERR(mvm, "flush request fail\n");
4875 		} else {
4876 			msk |= mvmsta->tfd_queue_msk;
4877 			if (iwl_mvm_has_new_tx_api(mvm))
4878 				iwl_mvm_wait_sta_queues_empty(mvm, mvmsta);
4879 		}
4880 	}
4881 
4882 	mutex_unlock(&mvm->mutex);
4883 
4884 	/* this can take a while, and we may need/want other operations
4885 	 * to succeed while doing this, so do it without the mutex held
4886 	 */
4887 	if (!drop && !iwl_mvm_has_new_tx_api(mvm))
4888 		iwl_trans_wait_tx_queues_empty(mvm->trans, msk);
4889 }
4890 
4891 static int iwl_mvm_mac_get_survey(struct ieee80211_hw *hw, int idx,
4892 				  struct survey_info *survey)
4893 {
4894 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
4895 	int ret;
4896 
4897 	memset(survey, 0, sizeof(*survey));
4898 
4899 	/* only support global statistics right now */
4900 	if (idx != 0)
4901 		return -ENOENT;
4902 
4903 	if (!fw_has_capa(&mvm->fw->ucode_capa,
4904 			 IWL_UCODE_TLV_CAPA_RADIO_BEACON_STATS))
4905 		return -ENOENT;
4906 
4907 	mutex_lock(&mvm->mutex);
4908 
4909 	if (iwl_mvm_firmware_running(mvm)) {
4910 		ret = iwl_mvm_request_statistics(mvm, false);
4911 		if (ret)
4912 			goto out;
4913 	}
4914 
4915 	survey->filled = SURVEY_INFO_TIME |
4916 			 SURVEY_INFO_TIME_RX |
4917 			 SURVEY_INFO_TIME_TX |
4918 			 SURVEY_INFO_TIME_SCAN;
4919 	survey->time = mvm->accu_radio_stats.on_time_rf +
4920 		       mvm->radio_stats.on_time_rf;
4921 	do_div(survey->time, USEC_PER_MSEC);
4922 
4923 	survey->time_rx = mvm->accu_radio_stats.rx_time +
4924 			  mvm->radio_stats.rx_time;
4925 	do_div(survey->time_rx, USEC_PER_MSEC);
4926 
4927 	survey->time_tx = mvm->accu_radio_stats.tx_time +
4928 			  mvm->radio_stats.tx_time;
4929 	do_div(survey->time_tx, USEC_PER_MSEC);
4930 
4931 	survey->time_scan = mvm->accu_radio_stats.on_time_scan +
4932 			    mvm->radio_stats.on_time_scan;
4933 	do_div(survey->time_scan, USEC_PER_MSEC);
4934 
4935 	ret = 0;
4936  out:
4937 	mutex_unlock(&mvm->mutex);
4938 	return ret;
4939 }
4940 
4941 static void iwl_mvm_set_sta_rate(u32 rate_n_flags, struct rate_info *rinfo)
4942 {
4943 	u32 format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
4944 
4945 	switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) {
4946 	case RATE_MCS_CHAN_WIDTH_20:
4947 		rinfo->bw = RATE_INFO_BW_20;
4948 		break;
4949 	case RATE_MCS_CHAN_WIDTH_40:
4950 		rinfo->bw = RATE_INFO_BW_40;
4951 		break;
4952 	case RATE_MCS_CHAN_WIDTH_80:
4953 		rinfo->bw = RATE_INFO_BW_80;
4954 		break;
4955 	case RATE_MCS_CHAN_WIDTH_160:
4956 		rinfo->bw = RATE_INFO_BW_160;
4957 		break;
4958 	}
4959 
4960 	if (format == RATE_MCS_CCK_MSK ||
4961 	    format == RATE_MCS_LEGACY_OFDM_MSK) {
4962 		int rate = u32_get_bits(rate_n_flags, RATE_LEGACY_RATE_MSK);
4963 
4964 		/* add the offset needed to get to the legacy ofdm indices */
4965 		if (format == RATE_MCS_LEGACY_OFDM_MSK)
4966 			rate += IWL_FIRST_OFDM_RATE;
4967 
4968 		switch (rate) {
4969 		case IWL_RATE_1M_INDEX:
4970 			rinfo->legacy = 10;
4971 			break;
4972 		case IWL_RATE_2M_INDEX:
4973 			rinfo->legacy = 20;
4974 			break;
4975 		case IWL_RATE_5M_INDEX:
4976 			rinfo->legacy = 55;
4977 			break;
4978 		case IWL_RATE_11M_INDEX:
4979 			rinfo->legacy = 110;
4980 			break;
4981 		case IWL_RATE_6M_INDEX:
4982 			rinfo->legacy = 60;
4983 			break;
4984 		case IWL_RATE_9M_INDEX:
4985 			rinfo->legacy = 90;
4986 			break;
4987 		case IWL_RATE_12M_INDEX:
4988 			rinfo->legacy = 120;
4989 			break;
4990 		case IWL_RATE_18M_INDEX:
4991 			rinfo->legacy = 180;
4992 			break;
4993 		case IWL_RATE_24M_INDEX:
4994 			rinfo->legacy = 240;
4995 			break;
4996 		case IWL_RATE_36M_INDEX:
4997 			rinfo->legacy = 360;
4998 			break;
4999 		case IWL_RATE_48M_INDEX:
5000 			rinfo->legacy = 480;
5001 			break;
5002 		case IWL_RATE_54M_INDEX:
5003 			rinfo->legacy = 540;
5004 		}
5005 		return;
5006 	}
5007 
5008 	rinfo->nss = u32_get_bits(rate_n_flags,
5009 				  RATE_MCS_NSS_MSK) + 1;
5010 	rinfo->mcs = format == RATE_MCS_HT_MSK ?
5011 		RATE_HT_MCS_INDEX(rate_n_flags) :
5012 		u32_get_bits(rate_n_flags, RATE_MCS_CODE_MSK);
5013 
5014 	if (format == RATE_MCS_HE_MSK) {
5015 		u32 gi_ltf = u32_get_bits(rate_n_flags,
5016 					  RATE_MCS_HE_GI_LTF_MSK);
5017 
5018 		rinfo->flags |= RATE_INFO_FLAGS_HE_MCS;
5019 
5020 		if (rate_n_flags & RATE_MCS_HE_106T_MSK) {
5021 			rinfo->bw = RATE_INFO_BW_HE_RU;
5022 			rinfo->he_ru_alloc = NL80211_RATE_INFO_HE_RU_ALLOC_106;
5023 		}
5024 
5025 		switch (rate_n_flags & RATE_MCS_HE_TYPE_MSK) {
5026 		case RATE_MCS_HE_TYPE_SU:
5027 		case RATE_MCS_HE_TYPE_EXT_SU:
5028 			if (gi_ltf == 0 || gi_ltf == 1)
5029 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5030 			else if (gi_ltf == 2)
5031 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5032 			else if (gi_ltf == 3)
5033 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5034 			else
5035 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5036 			break;
5037 		case RATE_MCS_HE_TYPE_MU:
5038 			if (gi_ltf == 0 || gi_ltf == 1)
5039 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_0_8;
5040 			else if (gi_ltf == 2)
5041 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5042 			else
5043 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5044 			break;
5045 		case RATE_MCS_HE_TYPE_TRIG:
5046 			if (gi_ltf == 0 || gi_ltf == 1)
5047 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_1_6;
5048 			else
5049 				rinfo->he_gi = NL80211_RATE_INFO_HE_GI_3_2;
5050 			break;
5051 		}
5052 
5053 		if (rate_n_flags & RATE_HE_DUAL_CARRIER_MODE_MSK)
5054 			rinfo->he_dcm = 1;
5055 		return;
5056 	}
5057 
5058 	if (rate_n_flags & RATE_MCS_SGI_MSK)
5059 		rinfo->flags |= RATE_INFO_FLAGS_SHORT_GI;
5060 
5061 	if (format == RATE_MCS_HT_MSK) {
5062 		rinfo->flags |= RATE_INFO_FLAGS_MCS;
5063 
5064 	} else if (format == RATE_MCS_VHT_MSK) {
5065 		rinfo->flags |= RATE_INFO_FLAGS_VHT_MCS;
5066 	}
5067 
5068 }
5069 
5070 static void iwl_mvm_mac_sta_statistics(struct ieee80211_hw *hw,
5071 				       struct ieee80211_vif *vif,
5072 				       struct ieee80211_sta *sta,
5073 				       struct station_info *sinfo)
5074 {
5075 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5076 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5077 	struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
5078 
5079 	if (mvmsta->avg_energy) {
5080 		sinfo->signal_avg = -(s8)mvmsta->avg_energy;
5081 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_SIGNAL_AVG);
5082 	}
5083 
5084 	if (iwl_mvm_has_tlc_offload(mvm)) {
5085 		struct iwl_lq_sta_rs_fw *lq_sta = &mvmsta->lq_sta.rs_fw;
5086 
5087 		iwl_mvm_set_sta_rate(lq_sta->last_rate_n_flags, &sinfo->txrate);
5088 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
5089 	}
5090 
5091 	/* if beacon filtering isn't on mac80211 does it anyway */
5092 	if (!(vif->driver_flags & IEEE80211_VIF_BEACON_FILTER))
5093 		return;
5094 
5095 	if (!vif->bss_conf.assoc)
5096 		return;
5097 
5098 	mutex_lock(&mvm->mutex);
5099 
5100 	if (mvmvif->ap_sta_id != mvmsta->sta_id)
5101 		goto unlock;
5102 
5103 	if (iwl_mvm_request_statistics(mvm, false))
5104 		goto unlock;
5105 
5106 	sinfo->rx_beacon = mvmvif->beacon_stats.num_beacons +
5107 			   mvmvif->beacon_stats.accu_num_beacons;
5108 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_RX);
5109 	if (mvmvif->beacon_stats.avg_signal) {
5110 		/* firmware only reports a value after RXing a few beacons */
5111 		sinfo->rx_beacon_signal_avg = mvmvif->beacon_stats.avg_signal;
5112 		sinfo->filled |= BIT_ULL(NL80211_STA_INFO_BEACON_SIGNAL_AVG);
5113 	}
5114  unlock:
5115 	mutex_unlock(&mvm->mutex);
5116 }
5117 
5118 static void iwl_mvm_event_mlme_callback_ini(struct iwl_mvm *mvm,
5119 					    struct ieee80211_vif *vif,
5120 					    const  struct ieee80211_mlme_event *mlme)
5121 {
5122 	if ((mlme->data == ASSOC_EVENT || mlme->data == AUTH_EVENT) &&
5123 	    (mlme->status == MLME_DENIED || mlme->status == MLME_TIMEOUT)) {
5124 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5125 				       IWL_FW_INI_TIME_POINT_ASSOC_FAILED,
5126 				       NULL);
5127 		return;
5128 	}
5129 
5130 	if (mlme->data == DEAUTH_RX_EVENT || mlme->data == DEAUTH_TX_EVENT) {
5131 		iwl_dbg_tlv_time_point(&mvm->fwrt,
5132 				       IWL_FW_INI_TIME_POINT_DEASSOC,
5133 				       NULL);
5134 		return;
5135 	}
5136 }
5137 
5138 static void iwl_mvm_event_mlme_callback(struct iwl_mvm *mvm,
5139 					struct ieee80211_vif *vif,
5140 					const struct ieee80211_event *event)
5141 {
5142 #define CHECK_MLME_TRIGGER(_cnt, _fmt...)				\
5143 	do {								\
5144 		if ((trig_mlme->_cnt) && --(trig_mlme->_cnt))		\
5145 			break;						\
5146 		iwl_fw_dbg_collect_trig(&(mvm)->fwrt, trig, _fmt);	\
5147 	} while (0)
5148 
5149 	struct iwl_fw_dbg_trigger_tlv *trig;
5150 	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
5151 
5152 	if (iwl_trans_dbg_ini_valid(mvm->trans)) {
5153 		iwl_mvm_event_mlme_callback_ini(mvm, vif, &event->u.mlme);
5154 		return;
5155 	}
5156 
5157 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5158 				     FW_DBG_TRIGGER_MLME);
5159 	if (!trig)
5160 		return;
5161 
5162 	trig_mlme = (void *)trig->data;
5163 
5164 	if (event->u.mlme.data == ASSOC_EVENT) {
5165 		if (event->u.mlme.status == MLME_DENIED)
5166 			CHECK_MLME_TRIGGER(stop_assoc_denied,
5167 					   "DENIED ASSOC: reason %d",
5168 					    event->u.mlme.reason);
5169 		else if (event->u.mlme.status == MLME_TIMEOUT)
5170 			CHECK_MLME_TRIGGER(stop_assoc_timeout,
5171 					   "ASSOC TIMEOUT");
5172 	} else if (event->u.mlme.data == AUTH_EVENT) {
5173 		if (event->u.mlme.status == MLME_DENIED)
5174 			CHECK_MLME_TRIGGER(stop_auth_denied,
5175 					   "DENIED AUTH: reason %d",
5176 					   event->u.mlme.reason);
5177 		else if (event->u.mlme.status == MLME_TIMEOUT)
5178 			CHECK_MLME_TRIGGER(stop_auth_timeout,
5179 					   "AUTH TIMEOUT");
5180 	} else if (event->u.mlme.data == DEAUTH_RX_EVENT) {
5181 		CHECK_MLME_TRIGGER(stop_rx_deauth,
5182 				   "DEAUTH RX %d", event->u.mlme.reason);
5183 	} else if (event->u.mlme.data == DEAUTH_TX_EVENT) {
5184 		CHECK_MLME_TRIGGER(stop_tx_deauth,
5185 				   "DEAUTH TX %d", event->u.mlme.reason);
5186 	}
5187 #undef CHECK_MLME_TRIGGER
5188 }
5189 
5190 static void iwl_mvm_event_bar_rx_callback(struct iwl_mvm *mvm,
5191 					  struct ieee80211_vif *vif,
5192 					  const struct ieee80211_event *event)
5193 {
5194 	struct iwl_fw_dbg_trigger_tlv *trig;
5195 	struct iwl_fw_dbg_trigger_ba *ba_trig;
5196 
5197 	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
5198 				     FW_DBG_TRIGGER_BA);
5199 	if (!trig)
5200 		return;
5201 
5202 	ba_trig = (void *)trig->data;
5203 
5204 	if (!(le16_to_cpu(ba_trig->rx_bar) & BIT(event->u.ba.tid)))
5205 		return;
5206 
5207 	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
5208 				"BAR received from %pM, tid %d, ssn %d",
5209 				event->u.ba.sta->addr, event->u.ba.tid,
5210 				event->u.ba.ssn);
5211 }
5212 
5213 static void iwl_mvm_mac_event_callback(struct ieee80211_hw *hw,
5214 				       struct ieee80211_vif *vif,
5215 				       const struct ieee80211_event *event)
5216 {
5217 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5218 
5219 	switch (event->type) {
5220 	case MLME_EVENT:
5221 		iwl_mvm_event_mlme_callback(mvm, vif, event);
5222 		break;
5223 	case BAR_RX_EVENT:
5224 		iwl_mvm_event_bar_rx_callback(mvm, vif, event);
5225 		break;
5226 	case BA_FRAME_TIMEOUT:
5227 		iwl_mvm_event_frame_timeout_callback(mvm, vif, event->u.ba.sta,
5228 						     event->u.ba.tid);
5229 		break;
5230 	default:
5231 		break;
5232 	}
5233 }
5234 
5235 void iwl_mvm_sync_rx_queues_internal(struct iwl_mvm *mvm,
5236 				     enum iwl_mvm_rxq_notif_type type,
5237 				     bool sync,
5238 				     const void *data, u32 size)
5239 {
5240 	struct {
5241 		struct iwl_rxq_sync_cmd cmd;
5242 		struct iwl_mvm_internal_rxq_notif notif;
5243 	} __packed cmd = {
5244 		.cmd.rxq_mask = cpu_to_le32(BIT(mvm->trans->num_rx_queues) - 1),
5245 		.cmd.count =
5246 			cpu_to_le32(sizeof(struct iwl_mvm_internal_rxq_notif) +
5247 				    size),
5248 		.notif.type = type,
5249 		.notif.sync = sync,
5250 	};
5251 	struct iwl_host_cmd hcmd = {
5252 		.id = WIDE_ID(DATA_PATH_GROUP, TRIGGER_RX_QUEUES_NOTIF_CMD),
5253 		.data[0] = &cmd,
5254 		.len[0] = sizeof(cmd),
5255 		.data[1] = data,
5256 		.len[1] = size,
5257 		.flags = sync ? 0 : CMD_ASYNC,
5258 	};
5259 	int ret;
5260 
5261 	/* size must be a multiple of DWORD */
5262 	if (WARN_ON(cmd.cmd.count & cpu_to_le32(3)))
5263 		return;
5264 
5265 	if (!iwl_mvm_has_new_rx_api(mvm))
5266 		return;
5267 
5268 	if (sync) {
5269 		cmd.notif.cookie = mvm->queue_sync_cookie;
5270 		mvm->queue_sync_state = (1 << mvm->trans->num_rx_queues) - 1;
5271 	}
5272 
5273 	ret = iwl_mvm_send_cmd(mvm, &hcmd);
5274 	if (ret) {
5275 		IWL_ERR(mvm, "Failed to trigger RX queues sync (%d)\n", ret);
5276 		goto out;
5277 	}
5278 
5279 	if (sync) {
5280 		lockdep_assert_held(&mvm->mutex);
5281 		ret = wait_event_timeout(mvm->rx_sync_waitq,
5282 					 READ_ONCE(mvm->queue_sync_state) == 0 ||
5283 					 iwl_mvm_is_radio_killed(mvm),
5284 					 HZ);
5285 		WARN_ONCE(!ret && !iwl_mvm_is_radio_killed(mvm),
5286 			  "queue sync: failed to sync, state is 0x%lx\n",
5287 			  mvm->queue_sync_state);
5288 	}
5289 
5290 out:
5291 	if (sync) {
5292 		mvm->queue_sync_state = 0;
5293 		mvm->queue_sync_cookie++;
5294 	}
5295 }
5296 
5297 static void iwl_mvm_sync_rx_queues(struct ieee80211_hw *hw)
5298 {
5299 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5300 
5301 	mutex_lock(&mvm->mutex);
5302 	iwl_mvm_sync_rx_queues_internal(mvm, IWL_MVM_RXQ_EMPTY, true, NULL, 0);
5303 	mutex_unlock(&mvm->mutex);
5304 }
5305 
5306 static int
5307 iwl_mvm_mac_get_ftm_responder_stats(struct ieee80211_hw *hw,
5308 				    struct ieee80211_vif *vif,
5309 				    struct cfg80211_ftm_responder_stats *stats)
5310 {
5311 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5312 	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
5313 
5314 	if (vif->p2p || vif->type != NL80211_IFTYPE_AP ||
5315 	    !mvmvif->ap_ibss_active || !vif->bss_conf.ftm_responder)
5316 		return -EINVAL;
5317 
5318 	mutex_lock(&mvm->mutex);
5319 	*stats = mvm->ftm_resp_stats;
5320 	mutex_unlock(&mvm->mutex);
5321 
5322 	stats->filled = BIT(NL80211_FTM_STATS_SUCCESS_NUM) |
5323 			BIT(NL80211_FTM_STATS_PARTIAL_NUM) |
5324 			BIT(NL80211_FTM_STATS_FAILED_NUM) |
5325 			BIT(NL80211_FTM_STATS_ASAP_NUM) |
5326 			BIT(NL80211_FTM_STATS_NON_ASAP_NUM) |
5327 			BIT(NL80211_FTM_STATS_TOTAL_DURATION_MSEC) |
5328 			BIT(NL80211_FTM_STATS_UNKNOWN_TRIGGERS_NUM) |
5329 			BIT(NL80211_FTM_STATS_RESCHEDULE_REQUESTS_NUM) |
5330 			BIT(NL80211_FTM_STATS_OUT_OF_WINDOW_TRIGGERS_NUM);
5331 
5332 	return 0;
5333 }
5334 
5335 static int iwl_mvm_start_pmsr(struct ieee80211_hw *hw,
5336 			      struct ieee80211_vif *vif,
5337 			      struct cfg80211_pmsr_request *request)
5338 {
5339 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5340 	int ret;
5341 
5342 	mutex_lock(&mvm->mutex);
5343 	ret = iwl_mvm_ftm_start(mvm, vif, request);
5344 	mutex_unlock(&mvm->mutex);
5345 
5346 	return ret;
5347 }
5348 
5349 static void iwl_mvm_abort_pmsr(struct ieee80211_hw *hw,
5350 			       struct ieee80211_vif *vif,
5351 			       struct cfg80211_pmsr_request *request)
5352 {
5353 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5354 
5355 	mutex_lock(&mvm->mutex);
5356 	iwl_mvm_ftm_abort(mvm, request);
5357 	mutex_unlock(&mvm->mutex);
5358 }
5359 
5360 static bool iwl_mvm_can_hw_csum(struct sk_buff *skb)
5361 {
5362 	u8 protocol = ip_hdr(skb)->protocol;
5363 
5364 	if (!IS_ENABLED(CONFIG_INET))
5365 		return false;
5366 
5367 	return protocol == IPPROTO_TCP || protocol == IPPROTO_UDP;
5368 }
5369 
5370 static bool iwl_mvm_mac_can_aggregate(struct ieee80211_hw *hw,
5371 				      struct sk_buff *head,
5372 				      struct sk_buff *skb)
5373 {
5374 	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5375 
5376 	/* For now don't aggregate IPv6 in AMSDU */
5377 	if (skb->protocol != htons(ETH_P_IP))
5378 		return false;
5379 
5380 	if (!iwl_mvm_is_csum_supported(mvm))
5381 		return true;
5382 
5383 	return iwl_mvm_can_hw_csum(skb) == iwl_mvm_can_hw_csum(head);
5384 }
5385 
5386 const struct ieee80211_ops iwl_mvm_hw_ops = {
5387 	.tx = iwl_mvm_mac_tx,
5388 	.wake_tx_queue = iwl_mvm_mac_wake_tx_queue,
5389 	.ampdu_action = iwl_mvm_mac_ampdu_action,
5390 	.get_antenna = iwl_mvm_op_get_antenna,
5391 	.start = iwl_mvm_mac_start,
5392 	.reconfig_complete = iwl_mvm_mac_reconfig_complete,
5393 	.stop = iwl_mvm_mac_stop,
5394 	.add_interface = iwl_mvm_mac_add_interface,
5395 	.remove_interface = iwl_mvm_mac_remove_interface,
5396 	.config = iwl_mvm_mac_config,
5397 	.prepare_multicast = iwl_mvm_prepare_multicast,
5398 	.configure_filter = iwl_mvm_configure_filter,
5399 	.config_iface_filter = iwl_mvm_config_iface_filter,
5400 	.bss_info_changed = iwl_mvm_bss_info_changed,
5401 	.hw_scan = iwl_mvm_mac_hw_scan,
5402 	.cancel_hw_scan = iwl_mvm_mac_cancel_hw_scan,
5403 	.sta_pre_rcu_remove = iwl_mvm_sta_pre_rcu_remove,
5404 	.sta_state = iwl_mvm_mac_sta_state,
5405 	.sta_notify = iwl_mvm_mac_sta_notify,
5406 	.allow_buffered_frames = iwl_mvm_mac_allow_buffered_frames,
5407 	.release_buffered_frames = iwl_mvm_mac_release_buffered_frames,
5408 	.set_rts_threshold = iwl_mvm_mac_set_rts_threshold,
5409 	.sta_rc_update = iwl_mvm_sta_rc_update,
5410 	.conf_tx = iwl_mvm_mac_conf_tx,
5411 	.mgd_prepare_tx = iwl_mvm_mac_mgd_prepare_tx,
5412 	.mgd_complete_tx = iwl_mvm_mac_mgd_complete_tx,
5413 	.mgd_protect_tdls_discover = iwl_mvm_mac_mgd_protect_tdls_discover,
5414 	.flush = iwl_mvm_mac_flush,
5415 	.sched_scan_start = iwl_mvm_mac_sched_scan_start,
5416 	.sched_scan_stop = iwl_mvm_mac_sched_scan_stop,
5417 	.set_key = iwl_mvm_mac_set_key,
5418 	.update_tkip_key = iwl_mvm_mac_update_tkip_key,
5419 	.remain_on_channel = iwl_mvm_roc,
5420 	.cancel_remain_on_channel = iwl_mvm_cancel_roc,
5421 	.add_chanctx = iwl_mvm_add_chanctx,
5422 	.remove_chanctx = iwl_mvm_remove_chanctx,
5423 	.change_chanctx = iwl_mvm_change_chanctx,
5424 	.assign_vif_chanctx = iwl_mvm_assign_vif_chanctx,
5425 	.unassign_vif_chanctx = iwl_mvm_unassign_vif_chanctx,
5426 	.switch_vif_chanctx = iwl_mvm_switch_vif_chanctx,
5427 
5428 	.start_ap = iwl_mvm_start_ap_ibss,
5429 	.stop_ap = iwl_mvm_stop_ap_ibss,
5430 	.join_ibss = iwl_mvm_start_ap_ibss,
5431 	.leave_ibss = iwl_mvm_stop_ap_ibss,
5432 
5433 	.tx_last_beacon = iwl_mvm_tx_last_beacon,
5434 
5435 	.set_tim = iwl_mvm_set_tim,
5436 
5437 	.channel_switch = iwl_mvm_channel_switch,
5438 	.pre_channel_switch = iwl_mvm_pre_channel_switch,
5439 	.post_channel_switch = iwl_mvm_post_channel_switch,
5440 	.abort_channel_switch = iwl_mvm_abort_channel_switch,
5441 	.channel_switch_rx_beacon = iwl_mvm_channel_switch_rx_beacon,
5442 
5443 	.tdls_channel_switch = iwl_mvm_tdls_channel_switch,
5444 	.tdls_cancel_channel_switch = iwl_mvm_tdls_cancel_channel_switch,
5445 	.tdls_recv_channel_switch = iwl_mvm_tdls_recv_channel_switch,
5446 
5447 	.event_callback = iwl_mvm_mac_event_callback,
5448 
5449 	.sync_rx_queues = iwl_mvm_sync_rx_queues,
5450 
5451 	CFG80211_TESTMODE_CMD(iwl_mvm_mac_testmode_cmd)
5452 
5453 #ifdef CONFIG_PM_SLEEP
5454 	/* look at d3.c */
5455 	.suspend = iwl_mvm_suspend,
5456 	.resume = iwl_mvm_resume,
5457 	.set_wakeup = iwl_mvm_set_wakeup,
5458 	.set_rekey_data = iwl_mvm_set_rekey_data,
5459 #if IS_ENABLED(CONFIG_IPV6)
5460 	.ipv6_addr_change = iwl_mvm_ipv6_addr_change,
5461 #endif
5462 	.set_default_unicast_key = iwl_mvm_set_default_unicast_key,
5463 #endif
5464 	.get_survey = iwl_mvm_mac_get_survey,
5465 	.sta_statistics = iwl_mvm_mac_sta_statistics,
5466 	.get_ftm_responder_stats = iwl_mvm_mac_get_ftm_responder_stats,
5467 	.start_pmsr = iwl_mvm_start_pmsr,
5468 	.abort_pmsr = iwl_mvm_abort_pmsr,
5469 
5470 	.can_aggregate_in_amsdu = iwl_mvm_mac_can_aggregate,
5471 #ifdef CONFIG_IWLWIFI_DEBUGFS
5472 	.sta_add_debugfs = iwl_mvm_sta_add_debugfs,
5473 #endif
5474 };
5475