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