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