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