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