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