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