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