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