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