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