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