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