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