18c2ecf20Sopenharmony_ci/******************************************************************************
28c2ecf20Sopenharmony_ci *
38c2ecf20Sopenharmony_ci * This file is provided under a dual BSD/GPLv2 license.  When using or
48c2ecf20Sopenharmony_ci * redistributing this file, you may do so under either license.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * GPL LICENSE SUMMARY
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
98c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
108c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * This program is free software; you can redistribute it and/or modify
138c2ecf20Sopenharmony_ci * it under the terms of version 2 of the GNU General Public License as
148c2ecf20Sopenharmony_ci * published by the Free Software Foundation.
158c2ecf20Sopenharmony_ci *
168c2ecf20Sopenharmony_ci * This program is distributed in the hope that it will be useful, but
178c2ecf20Sopenharmony_ci * WITHOUT ANY WARRANTY; without even the implied warranty of
188c2ecf20Sopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
198c2ecf20Sopenharmony_ci * General Public License for more details.
208c2ecf20Sopenharmony_ci *
218c2ecf20Sopenharmony_ci * The full GNU General Public License is included in this distribution
228c2ecf20Sopenharmony_ci * in the file called COPYING.
238c2ecf20Sopenharmony_ci *
248c2ecf20Sopenharmony_ci * Contact Information:
258c2ecf20Sopenharmony_ci *  Intel Linux Wireless <linuxwifi@intel.com>
268c2ecf20Sopenharmony_ci * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
278c2ecf20Sopenharmony_ci *
288c2ecf20Sopenharmony_ci * BSD LICENSE
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
318c2ecf20Sopenharmony_ci * Copyright(c) 2015 - 2017 Intel Deutschland GmbH
328c2ecf20Sopenharmony_ci * Copyright(c) 2012 - 2014, 2018 - 2020 Intel Corporation
338c2ecf20Sopenharmony_ci * All rights reserved.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or without
368c2ecf20Sopenharmony_ci * modification, are permitted provided that the following conditions
378c2ecf20Sopenharmony_ci * are met:
388c2ecf20Sopenharmony_ci *
398c2ecf20Sopenharmony_ci *  * Redistributions of source code must retain the above copyright
408c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer.
418c2ecf20Sopenharmony_ci *  * Redistributions in binary form must reproduce the above copyright
428c2ecf20Sopenharmony_ci *    notice, this list of conditions and the following disclaimer in
438c2ecf20Sopenharmony_ci *    the documentation and/or other materials provided with the
448c2ecf20Sopenharmony_ci *    distribution.
458c2ecf20Sopenharmony_ci *  * Neither the name Intel Corporation nor the names of its
468c2ecf20Sopenharmony_ci *    contributors may be used to endorse or promote products derived
478c2ecf20Sopenharmony_ci *    from this software without specific prior written permission.
488c2ecf20Sopenharmony_ci *
498c2ecf20Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
508c2ecf20Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
518c2ecf20Sopenharmony_ci * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
528c2ecf20Sopenharmony_ci * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
538c2ecf20Sopenharmony_ci * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
548c2ecf20Sopenharmony_ci * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
558c2ecf20Sopenharmony_ci * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
568c2ecf20Sopenharmony_ci * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
578c2ecf20Sopenharmony_ci * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
588c2ecf20Sopenharmony_ci * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
598c2ecf20Sopenharmony_ci * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
608c2ecf20Sopenharmony_ci *
618c2ecf20Sopenharmony_ci *****************************************************************************/
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
648c2ecf20Sopenharmony_ci#include <net/mac80211.h>
658c2ecf20Sopenharmony_ci#include "iwl-io.h"
668c2ecf20Sopenharmony_ci#include "iwl-prph.h"
678c2ecf20Sopenharmony_ci#include "fw-api.h"
688c2ecf20Sopenharmony_ci#include "mvm.h"
698c2ecf20Sopenharmony_ci#include "time-event.h"
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ciconst u8 iwl_mvm_ac_to_tx_fifo[] = {
728c2ecf20Sopenharmony_ci	IWL_MVM_TX_FIFO_VO,
738c2ecf20Sopenharmony_ci	IWL_MVM_TX_FIFO_VI,
748c2ecf20Sopenharmony_ci	IWL_MVM_TX_FIFO_BE,
758c2ecf20Sopenharmony_ci	IWL_MVM_TX_FIFO_BK,
768c2ecf20Sopenharmony_ci};
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ciconst u8 iwl_mvm_ac_to_gen2_tx_fifo[] = {
798c2ecf20Sopenharmony_ci	IWL_GEN2_EDCA_TX_FIFO_VO,
808c2ecf20Sopenharmony_ci	IWL_GEN2_EDCA_TX_FIFO_VI,
818c2ecf20Sopenharmony_ci	IWL_GEN2_EDCA_TX_FIFO_BE,
828c2ecf20Sopenharmony_ci	IWL_GEN2_EDCA_TX_FIFO_BK,
838c2ecf20Sopenharmony_ci	IWL_GEN2_TRIG_TX_FIFO_VO,
848c2ecf20Sopenharmony_ci	IWL_GEN2_TRIG_TX_FIFO_VI,
858c2ecf20Sopenharmony_ci	IWL_GEN2_TRIG_TX_FIFO_BE,
868c2ecf20Sopenharmony_ci	IWL_GEN2_TRIG_TX_FIFO_BK,
878c2ecf20Sopenharmony_ci};
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_cistruct iwl_mvm_mac_iface_iterator_data {
908c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
918c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
928c2ecf20Sopenharmony_ci	unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
938c2ecf20Sopenharmony_ci	unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
948c2ecf20Sopenharmony_ci	enum iwl_tsf_id preferred_tsf;
958c2ecf20Sopenharmony_ci	bool found_vif;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
998c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif)
1008c2ecf20Sopenharmony_ci{
1018c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_iface_iterator_data *data = _data;
1028c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1038c2ecf20Sopenharmony_ci	u16 min_bi;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* Skip the interface for which we are trying to assign a tsf_id  */
1068c2ecf20Sopenharmony_ci	if (vif == data->vif)
1078c2ecf20Sopenharmony_ci		return;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/*
1108c2ecf20Sopenharmony_ci	 * The TSF is a hardware/firmware resource, there are 4 and
1118c2ecf20Sopenharmony_ci	 * the driver should assign and free them as needed. However,
1128c2ecf20Sopenharmony_ci	 * there are cases where 2 MACs should share the same TSF ID
1138c2ecf20Sopenharmony_ci	 * for the purpose of clock sync, an optimization to avoid
1148c2ecf20Sopenharmony_ci	 * clock drift causing overlapping TBTTs/DTIMs for a GO and
1158c2ecf20Sopenharmony_ci	 * client in the system.
1168c2ecf20Sopenharmony_ci	 *
1178c2ecf20Sopenharmony_ci	 * The firmware will decide according to the MAC type which
1188c2ecf20Sopenharmony_ci	 * will be the leader and follower. Clients that need to sync
1198c2ecf20Sopenharmony_ci	 * with a remote station will be the leader, and an AP or GO
1208c2ecf20Sopenharmony_ci	 * will be the follower.
1218c2ecf20Sopenharmony_ci	 *
1228c2ecf20Sopenharmony_ci	 * Depending on the new interface type it can be following
1238c2ecf20Sopenharmony_ci	 * or become the leader of an existing interface.
1248c2ecf20Sopenharmony_ci	 */
1258c2ecf20Sopenharmony_ci	switch (data->vif->type) {
1268c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
1278c2ecf20Sopenharmony_ci		/*
1288c2ecf20Sopenharmony_ci		 * The new interface is a client, so if the one we're iterating
1298c2ecf20Sopenharmony_ci		 * is an AP, and the beacon interval of the AP is a multiple or
1308c2ecf20Sopenharmony_ci		 * divisor of the beacon interval of the client, the same TSF
1318c2ecf20Sopenharmony_ci		 * should be used to avoid drift between the new client and
1328c2ecf20Sopenharmony_ci		 * existing AP. The existing AP will get drift updates from the
1338c2ecf20Sopenharmony_ci		 * new client context in this case.
1348c2ecf20Sopenharmony_ci		 */
1358c2ecf20Sopenharmony_ci		if (vif->type != NL80211_IFTYPE_AP ||
1368c2ecf20Sopenharmony_ci		    data->preferred_tsf != NUM_TSF_IDS ||
1378c2ecf20Sopenharmony_ci		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
1388c2ecf20Sopenharmony_ci			break;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		min_bi = min(data->vif->bss_conf.beacon_int,
1418c2ecf20Sopenharmony_ci			     vif->bss_conf.beacon_int);
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci		if (!min_bi)
1448c2ecf20Sopenharmony_ci			break;
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci		if ((data->vif->bss_conf.beacon_int -
1478c2ecf20Sopenharmony_ci		     vif->bss_conf.beacon_int) % min_bi == 0) {
1488c2ecf20Sopenharmony_ci			data->preferred_tsf = mvmvif->tsf_id;
1498c2ecf20Sopenharmony_ci			return;
1508c2ecf20Sopenharmony_ci		}
1518c2ecf20Sopenharmony_ci		break;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_AP:
1548c2ecf20Sopenharmony_ci		/*
1558c2ecf20Sopenharmony_ci		 * The new interface is AP/GO, so if its beacon interval is a
1568c2ecf20Sopenharmony_ci		 * multiple or a divisor of the beacon interval of an existing
1578c2ecf20Sopenharmony_ci		 * interface, it should get drift updates from an existing
1588c2ecf20Sopenharmony_ci		 * client or use the same TSF as an existing GO. There's no
1598c2ecf20Sopenharmony_ci		 * drift between TSFs internally but if they used different
1608c2ecf20Sopenharmony_ci		 * TSFs then a new client MAC could update one of them and
1618c2ecf20Sopenharmony_ci		 * cause drift that way.
1628c2ecf20Sopenharmony_ci		 */
1638c2ecf20Sopenharmony_ci		if ((vif->type != NL80211_IFTYPE_AP &&
1648c2ecf20Sopenharmony_ci		     vif->type != NL80211_IFTYPE_STATION) ||
1658c2ecf20Sopenharmony_ci		    data->preferred_tsf != NUM_TSF_IDS ||
1668c2ecf20Sopenharmony_ci		    !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
1678c2ecf20Sopenharmony_ci			break;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci		min_bi = min(data->vif->bss_conf.beacon_int,
1708c2ecf20Sopenharmony_ci			     vif->bss_conf.beacon_int);
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci		if (!min_bi)
1738c2ecf20Sopenharmony_ci			break;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		if ((data->vif->bss_conf.beacon_int -
1768c2ecf20Sopenharmony_ci		     vif->bss_conf.beacon_int) % min_bi == 0) {
1778c2ecf20Sopenharmony_ci			data->preferred_tsf = mvmvif->tsf_id;
1788c2ecf20Sopenharmony_ci			return;
1798c2ecf20Sopenharmony_ci		}
1808c2ecf20Sopenharmony_ci		break;
1818c2ecf20Sopenharmony_ci	default:
1828c2ecf20Sopenharmony_ci		/*
1838c2ecf20Sopenharmony_ci		 * For all other interface types there's no need to
1848c2ecf20Sopenharmony_ci		 * take drift into account. Either they're exclusive
1858c2ecf20Sopenharmony_ci		 * like IBSS and monitor, or we don't care much about
1868c2ecf20Sopenharmony_ci		 * their TSF (like P2P Device), but we won't be able
1878c2ecf20Sopenharmony_ci		 * to share the TSF resource.
1888c2ecf20Sopenharmony_ci		 */
1898c2ecf20Sopenharmony_ci		break;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/*
1938c2ecf20Sopenharmony_ci	 * Unless we exited above, we can't share the TSF resource
1948c2ecf20Sopenharmony_ci	 * that the virtual interface we're iterating over is using
1958c2ecf20Sopenharmony_ci	 * with the new one, so clear the available bit and if this
1968c2ecf20Sopenharmony_ci	 * was the preferred one, reset that as well.
1978c2ecf20Sopenharmony_ci	 */
1988c2ecf20Sopenharmony_ci	__clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (data->preferred_tsf == mvmvif->tsf_id)
2018c2ecf20Sopenharmony_ci		data->preferred_tsf = NUM_TSF_IDS;
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
2058c2ecf20Sopenharmony_ci				       struct ieee80211_vif *vif)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_iface_iterator_data *data = _data;
2088c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/* Iterator may already find the interface being added -- skip it */
2118c2ecf20Sopenharmony_ci	if (vif == data->vif) {
2128c2ecf20Sopenharmony_ci		data->found_vif = true;
2138c2ecf20Sopenharmony_ci		return;
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	/* Mark MAC IDs as used by clearing the available bit, and
2178c2ecf20Sopenharmony_ci	 * (below) mark TSFs as used if their existing use is not
2188c2ecf20Sopenharmony_ci	 * compatible with the new interface type.
2198c2ecf20Sopenharmony_ci	 * No locking or atomic bit operations are needed since the
2208c2ecf20Sopenharmony_ci	 * data is on the stack of the caller function.
2218c2ecf20Sopenharmony_ci	 */
2228c2ecf20Sopenharmony_ci	__clear_bit(mvmvif->id, data->available_mac_ids);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	/* find a suitable tsf_id */
2258c2ecf20Sopenharmony_ci	iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_civoid iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
2298c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif)
2308c2ecf20Sopenharmony_ci{
2318c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2328c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_iface_iterator_data data = {
2338c2ecf20Sopenharmony_ci		.mvm = mvm,
2348c2ecf20Sopenharmony_ci		.vif = vif,
2358c2ecf20Sopenharmony_ci		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
2368c2ecf20Sopenharmony_ci		/* no preference yet */
2378c2ecf20Sopenharmony_ci		.preferred_tsf = NUM_TSF_IDS,
2388c2ecf20Sopenharmony_ci	};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
2418c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
2428c2ecf20Sopenharmony_ci		iwl_mvm_mac_tsf_id_iter, &data);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (data.preferred_tsf != NUM_TSF_IDS)
2458c2ecf20Sopenharmony_ci		mvmvif->tsf_id = data.preferred_tsf;
2468c2ecf20Sopenharmony_ci	else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
2478c2ecf20Sopenharmony_ci		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
2488c2ecf20Sopenharmony_ci						NUM_TSF_IDS);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
2548c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_iface_iterator_data data = {
2558c2ecf20Sopenharmony_ci		.mvm = mvm,
2568c2ecf20Sopenharmony_ci		.vif = vif,
2578c2ecf20Sopenharmony_ci		.available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
2588c2ecf20Sopenharmony_ci		.available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
2598c2ecf20Sopenharmony_ci		/* no preference yet */
2608c2ecf20Sopenharmony_ci		.preferred_tsf = NUM_TSF_IDS,
2618c2ecf20Sopenharmony_ci		.found_vif = false,
2628c2ecf20Sopenharmony_ci	};
2638c2ecf20Sopenharmony_ci	int ret, i;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	/*
2688c2ecf20Sopenharmony_ci	 * Allocate a MAC ID and a TSF for this MAC, along with the queues
2698c2ecf20Sopenharmony_ci	 * and other resources.
2708c2ecf20Sopenharmony_ci	 */
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	/*
2738c2ecf20Sopenharmony_ci	 * Before the iterator, we start with all MAC IDs and TSFs available.
2748c2ecf20Sopenharmony_ci	 *
2758c2ecf20Sopenharmony_ci	 * During iteration, all MAC IDs are cleared that are in use by other
2768c2ecf20Sopenharmony_ci	 * virtual interfaces, and all TSF IDs are cleared that can't be used
2778c2ecf20Sopenharmony_ci	 * by this new virtual interface because they're used by an interface
2788c2ecf20Sopenharmony_ci	 * that can't share it with the new one.
2798c2ecf20Sopenharmony_ci	 * At the same time, we check if there's a preferred TSF in the case
2808c2ecf20Sopenharmony_ci	 * that we should share it with another interface.
2818c2ecf20Sopenharmony_ci	 */
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	/* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
2848c2ecf20Sopenharmony_ci	switch (vif->type) {
2858c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_ADHOC:
2868c2ecf20Sopenharmony_ci		break;
2878c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
2888c2ecf20Sopenharmony_ci		if (!vif->p2p)
2898c2ecf20Sopenharmony_ci			break;
2908c2ecf20Sopenharmony_ci		/* fall through */
2918c2ecf20Sopenharmony_ci	default:
2928c2ecf20Sopenharmony_ci		__clear_bit(0, data.available_mac_ids);
2938c2ecf20Sopenharmony_ci	}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
2968c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
2978c2ecf20Sopenharmony_ci		iwl_mvm_mac_iface_iterator, &data);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	/*
3008c2ecf20Sopenharmony_ci	 * In the case we're getting here during resume, it's similar to
3018c2ecf20Sopenharmony_ci	 * firmware restart, and with RESUME_ALL the iterator will find
3028c2ecf20Sopenharmony_ci	 * the vif being added already.
3038c2ecf20Sopenharmony_ci	 * We don't want to reassign any IDs in either case since doing
3048c2ecf20Sopenharmony_ci	 * so would probably assign different IDs (as interfaces aren't
3058c2ecf20Sopenharmony_ci	 * necessarily added in the same order), but the old IDs were
3068c2ecf20Sopenharmony_ci	 * preserved anyway, so skip ID assignment for both resume and
3078c2ecf20Sopenharmony_ci	 * recovery.
3088c2ecf20Sopenharmony_ci	 */
3098c2ecf20Sopenharmony_ci	if (data.found_vif)
3108c2ecf20Sopenharmony_ci		return 0;
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* Therefore, in recovery, we can't get here */
3138c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
3148c2ecf20Sopenharmony_ci		return -EBUSY;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	mvmvif->id = find_first_bit(data.available_mac_ids,
3178c2ecf20Sopenharmony_ci				    NUM_MAC_INDEX_DRIVER);
3188c2ecf20Sopenharmony_ci	if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
3198c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
3208c2ecf20Sopenharmony_ci		ret = -EIO;
3218c2ecf20Sopenharmony_ci		goto exit_fail;
3228c2ecf20Sopenharmony_ci	}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (data.preferred_tsf != NUM_TSF_IDS)
3258c2ecf20Sopenharmony_ci		mvmvif->tsf_id = data.preferred_tsf;
3268c2ecf20Sopenharmony_ci	else
3278c2ecf20Sopenharmony_ci		mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
3288c2ecf20Sopenharmony_ci						NUM_TSF_IDS);
3298c2ecf20Sopenharmony_ci	if (mvmvif->tsf_id == NUM_TSF_IDS) {
3308c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
3318c2ecf20Sopenharmony_ci		ret = -EIO;
3328c2ecf20Sopenharmony_ci		goto exit_fail;
3338c2ecf20Sopenharmony_ci	}
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	mvmvif->color = 0;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&mvmvif->time_event_data.list);
3388c2ecf20Sopenharmony_ci	mvmvif->time_event_data.id = TE_MAX;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	/* No need to allocate data queues to P2P Device MAC and NAN.*/
3418c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
3428c2ecf20Sopenharmony_ci		return 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	/* Allocate the CAB queue for softAP and GO interfaces */
3458c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP ||
3468c2ecf20Sopenharmony_ci	    vif->type == NL80211_IFTYPE_ADHOC) {
3478c2ecf20Sopenharmony_ci		/*
3488c2ecf20Sopenharmony_ci		 * For TVQM this will be overwritten later with the FW assigned
3498c2ecf20Sopenharmony_ci		 * queue value (when queue is enabled).
3508c2ecf20Sopenharmony_ci		 */
3518c2ecf20Sopenharmony_ci		mvmvif->cab_queue = IWL_MVM_DQA_GCAST_QUEUE;
3528c2ecf20Sopenharmony_ci	}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	mvmvif->bcast_sta.sta_id = IWL_MVM_INVALID_STA;
3558c2ecf20Sopenharmony_ci	mvmvif->mcast_sta.sta_id = IWL_MVM_INVALID_STA;
3568c2ecf20Sopenharmony_ci	mvmvif->ap_sta_id = IWL_MVM_INVALID_STA;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
3598c2ecf20Sopenharmony_ci		mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_ciexit_fail:
3648c2ecf20Sopenharmony_ci	memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
3658c2ecf20Sopenharmony_ci	return ret;
3668c2ecf20Sopenharmony_ci}
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
3698c2ecf20Sopenharmony_ci			      struct ieee80211_vif *vif,
3708c2ecf20Sopenharmony_ci			      enum nl80211_band band,
3718c2ecf20Sopenharmony_ci			      u8 *cck_rates, u8 *ofdm_rates)
3728c2ecf20Sopenharmony_ci{
3738c2ecf20Sopenharmony_ci	struct ieee80211_supported_band *sband;
3748c2ecf20Sopenharmony_ci	unsigned long basic = vif->bss_conf.basic_rates;
3758c2ecf20Sopenharmony_ci	int lowest_present_ofdm = 100;
3768c2ecf20Sopenharmony_ci	int lowest_present_cck = 100;
3778c2ecf20Sopenharmony_ci	u8 cck = 0;
3788c2ecf20Sopenharmony_ci	u8 ofdm = 0;
3798c2ecf20Sopenharmony_ci	int i;
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	sband = mvm->hw->wiphy->bands[band];
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	for_each_set_bit(i, &basic, BITS_PER_LONG) {
3848c2ecf20Sopenharmony_ci		int hw = sband->bitrates[i].hw_value;
3858c2ecf20Sopenharmony_ci		if (hw >= IWL_FIRST_OFDM_RATE) {
3868c2ecf20Sopenharmony_ci			ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
3878c2ecf20Sopenharmony_ci			if (lowest_present_ofdm > hw)
3888c2ecf20Sopenharmony_ci				lowest_present_ofdm = hw;
3898c2ecf20Sopenharmony_ci		} else {
3908c2ecf20Sopenharmony_ci			BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci			cck |= BIT(hw);
3938c2ecf20Sopenharmony_ci			if (lowest_present_cck > hw)
3948c2ecf20Sopenharmony_ci				lowest_present_cck = hw;
3958c2ecf20Sopenharmony_ci		}
3968c2ecf20Sopenharmony_ci	}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/*
3998c2ecf20Sopenharmony_ci	 * Now we've got the basic rates as bitmaps in the ofdm and cck
4008c2ecf20Sopenharmony_ci	 * variables. This isn't sufficient though, as there might not
4018c2ecf20Sopenharmony_ci	 * be all the right rates in the bitmap. E.g. if the only basic
4028c2ecf20Sopenharmony_ci	 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
4038c2ecf20Sopenharmony_ci	 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
4048c2ecf20Sopenharmony_ci	 *
4058c2ecf20Sopenharmony_ci	 *    [...] a STA responding to a received frame shall transmit
4068c2ecf20Sopenharmony_ci	 *    its Control Response frame [...] at the highest rate in the
4078c2ecf20Sopenharmony_ci	 *    BSSBasicRateSet parameter that is less than or equal to the
4088c2ecf20Sopenharmony_ci	 *    rate of the immediately previous frame in the frame exchange
4098c2ecf20Sopenharmony_ci	 *    sequence ([...]) and that is of the same modulation class
4108c2ecf20Sopenharmony_ci	 *    ([...]) as the received frame. If no rate contained in the
4118c2ecf20Sopenharmony_ci	 *    BSSBasicRateSet parameter meets these conditions, then the
4128c2ecf20Sopenharmony_ci	 *    control frame sent in response to a received frame shall be
4138c2ecf20Sopenharmony_ci	 *    transmitted at the highest mandatory rate of the PHY that is
4148c2ecf20Sopenharmony_ci	 *    less than or equal to the rate of the received frame, and
4158c2ecf20Sopenharmony_ci	 *    that is of the same modulation class as the received frame.
4168c2ecf20Sopenharmony_ci	 *
4178c2ecf20Sopenharmony_ci	 * As a consequence, we need to add all mandatory rates that are
4188c2ecf20Sopenharmony_ci	 * lower than all of the basic rates to these bitmaps.
4198c2ecf20Sopenharmony_ci	 */
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
4228c2ecf20Sopenharmony_ci		ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
4238c2ecf20Sopenharmony_ci	if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
4248c2ecf20Sopenharmony_ci		ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
4258c2ecf20Sopenharmony_ci	/* 6M already there or needed so always add */
4268c2ecf20Sopenharmony_ci	ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	/*
4298c2ecf20Sopenharmony_ci	 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
4308c2ecf20Sopenharmony_ci	 * Note, however:
4318c2ecf20Sopenharmony_ci	 *  - if no CCK rates are basic, it must be ERP since there must
4328c2ecf20Sopenharmony_ci	 *    be some basic rates at all, so they're OFDM => ERP PHY
4338c2ecf20Sopenharmony_ci	 *    (or we're in 5 GHz, and the cck bitmap will never be used)
4348c2ecf20Sopenharmony_ci	 *  - if 11M is a basic rate, it must be ERP as well, so add 5.5M
4358c2ecf20Sopenharmony_ci	 *  - if 5.5M is basic, 1M and 2M are mandatory
4368c2ecf20Sopenharmony_ci	 *  - if 2M is basic, 1M is mandatory
4378c2ecf20Sopenharmony_ci	 *  - if 1M is basic, that's the only valid ACK rate.
4388c2ecf20Sopenharmony_ci	 * As a consequence, it's not as complicated as it sounds, just add
4398c2ecf20Sopenharmony_ci	 * any lower rates to the ACK rate bitmap.
4408c2ecf20Sopenharmony_ci	 */
4418c2ecf20Sopenharmony_ci	if (IWL_RATE_11M_INDEX < lowest_present_cck)
4428c2ecf20Sopenharmony_ci		cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
4438c2ecf20Sopenharmony_ci	if (IWL_RATE_5M_INDEX < lowest_present_cck)
4448c2ecf20Sopenharmony_ci		cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
4458c2ecf20Sopenharmony_ci	if (IWL_RATE_2M_INDEX < lowest_present_cck)
4468c2ecf20Sopenharmony_ci		cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
4478c2ecf20Sopenharmony_ci	/* 1M already there or needed so always add */
4488c2ecf20Sopenharmony_ci	cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	*cck_rates = cck;
4518c2ecf20Sopenharmony_ci	*ofdm_rates = ofdm;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
4558c2ecf20Sopenharmony_ci					 struct ieee80211_vif *vif,
4568c2ecf20Sopenharmony_ci					 struct iwl_mac_ctx_cmd *cmd)
4578c2ecf20Sopenharmony_ci{
4588c2ecf20Sopenharmony_ci	/* for both sta and ap, ht_operation_mode hold the protection_mode */
4598c2ecf20Sopenharmony_ci	u8 protection_mode = vif->bss_conf.ht_operation_mode &
4608c2ecf20Sopenharmony_ci				 IEEE80211_HT_OP_MODE_PROTECTION;
4618c2ecf20Sopenharmony_ci	/* The fw does not distinguish between ht and fat */
4628c2ecf20Sopenharmony_ci	u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
4658c2ecf20Sopenharmony_ci	/*
4668c2ecf20Sopenharmony_ci	 * See section 9.23.3.1 of IEEE 80211-2012.
4678c2ecf20Sopenharmony_ci	 * Nongreenfield HT STAs Present is not supported.
4688c2ecf20Sopenharmony_ci	 */
4698c2ecf20Sopenharmony_ci	switch (protection_mode) {
4708c2ecf20Sopenharmony_ci	case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
4718c2ecf20Sopenharmony_ci		break;
4728c2ecf20Sopenharmony_ci	case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
4738c2ecf20Sopenharmony_ci	case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
4748c2ecf20Sopenharmony_ci		cmd->protection_flags |= cpu_to_le32(ht_flag);
4758c2ecf20Sopenharmony_ci		break;
4768c2ecf20Sopenharmony_ci	case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
4778c2ecf20Sopenharmony_ci		/* Protect when channel wider than 20MHz */
4788c2ecf20Sopenharmony_ci		if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
4798c2ecf20Sopenharmony_ci			cmd->protection_flags |= cpu_to_le32(ht_flag);
4808c2ecf20Sopenharmony_ci		break;
4818c2ecf20Sopenharmony_ci	default:
4828c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Illegal protection mode %d\n",
4838c2ecf20Sopenharmony_ci			protection_mode);
4848c2ecf20Sopenharmony_ci		break;
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
4898c2ecf20Sopenharmony_ci					struct ieee80211_vif *vif,
4908c2ecf20Sopenharmony_ci					struct iwl_mac_ctx_cmd *cmd,
4918c2ecf20Sopenharmony_ci					const u8 *bssid_override,
4928c2ecf20Sopenharmony_ci					u32 action)
4938c2ecf20Sopenharmony_ci{
4948c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
4958c2ecf20Sopenharmony_ci	struct ieee80211_chanctx_conf *chanctx;
4968c2ecf20Sopenharmony_ci	bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
4978c2ecf20Sopenharmony_ci			     IEEE80211_HT_OP_MODE_PROTECTION);
4988c2ecf20Sopenharmony_ci	u8 cck_ack_rates, ofdm_ack_rates;
4998c2ecf20Sopenharmony_ci	const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
5008c2ecf20Sopenharmony_ci	int i;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
5038c2ecf20Sopenharmony_ci							    mvmvif->color));
5048c2ecf20Sopenharmony_ci	cmd->action = cpu_to_le32(action);
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	switch (vif->type) {
5078c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
5088c2ecf20Sopenharmony_ci		if (vif->p2p)
5098c2ecf20Sopenharmony_ci			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
5108c2ecf20Sopenharmony_ci		else
5118c2ecf20Sopenharmony_ci			cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
5128c2ecf20Sopenharmony_ci		break;
5138c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_AP:
5148c2ecf20Sopenharmony_ci		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
5158c2ecf20Sopenharmony_ci		break;
5168c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_MONITOR:
5178c2ecf20Sopenharmony_ci		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
5188c2ecf20Sopenharmony_ci		break;
5198c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_DEVICE:
5208c2ecf20Sopenharmony_ci		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
5218c2ecf20Sopenharmony_ci		break;
5228c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_ADHOC:
5238c2ecf20Sopenharmony_ci		cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
5248c2ecf20Sopenharmony_ci		break;
5258c2ecf20Sopenharmony_ci	default:
5268c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
5278c2ecf20Sopenharmony_ci	}
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
5308c2ecf20Sopenharmony_ci
5318c2ecf20Sopenharmony_ci	memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	if (bssid)
5348c2ecf20Sopenharmony_ci		memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
5358c2ecf20Sopenharmony_ci	else
5368c2ecf20Sopenharmony_ci		eth_broadcast_addr(cmd->bssid_addr);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	rcu_read_lock();
5398c2ecf20Sopenharmony_ci	chanctx = rcu_dereference(vif->chanctx_conf);
5408c2ecf20Sopenharmony_ci	iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
5418c2ecf20Sopenharmony_ci					    : NL80211_BAND_2GHZ,
5428c2ecf20Sopenharmony_ci			  &cck_ack_rates, &ofdm_ack_rates);
5438c2ecf20Sopenharmony_ci	rcu_read_unlock();
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_ci	cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
5468c2ecf20Sopenharmony_ci	cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	cmd->cck_short_preamble =
5498c2ecf20Sopenharmony_ci		cpu_to_le32(vif->bss_conf.use_short_preamble ?
5508c2ecf20Sopenharmony_ci			    MAC_FLG_SHORT_PREAMBLE : 0);
5518c2ecf20Sopenharmony_ci	cmd->short_slot =
5528c2ecf20Sopenharmony_ci		cpu_to_le32(vif->bss_conf.use_short_slot ?
5538c2ecf20Sopenharmony_ci			    MAC_FLG_SHORT_SLOT : 0);
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	cmd->filter_flags = 0;
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
5588c2ecf20Sopenharmony_ci		u8 txf = iwl_mvm_mac_ac_to_tx_fifo(mvm, i);
5598c2ecf20Sopenharmony_ci		u8 ucode_ac = iwl_mvm_mac80211_ac_to_ucode_ac(i);
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_ci		cmd->ac[ucode_ac].cw_min =
5628c2ecf20Sopenharmony_ci			cpu_to_le16(mvmvif->queue_params[i].cw_min);
5638c2ecf20Sopenharmony_ci		cmd->ac[ucode_ac].cw_max =
5648c2ecf20Sopenharmony_ci			cpu_to_le16(mvmvif->queue_params[i].cw_max);
5658c2ecf20Sopenharmony_ci		cmd->ac[ucode_ac].edca_txop =
5668c2ecf20Sopenharmony_ci			cpu_to_le16(mvmvif->queue_params[i].txop * 32);
5678c2ecf20Sopenharmony_ci		cmd->ac[ucode_ac].aifsn = mvmvif->queue_params[i].aifs;
5688c2ecf20Sopenharmony_ci		cmd->ac[ucode_ac].fifos_mask = BIT(txf);
5698c2ecf20Sopenharmony_ci	}
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	if (vif->bss_conf.qos)
5728c2ecf20Sopenharmony_ci		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	if (vif->bss_conf.use_cts_prot)
5758c2ecf20Sopenharmony_ci		cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
5788c2ecf20Sopenharmony_ci		       vif->bss_conf.use_cts_prot,
5798c2ecf20Sopenharmony_ci		       vif->bss_conf.ht_operation_mode);
5808c2ecf20Sopenharmony_ci	if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
5818c2ecf20Sopenharmony_ci		cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
5828c2ecf20Sopenharmony_ci	if (ht_enabled)
5838c2ecf20Sopenharmony_ci		iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
5848c2ecf20Sopenharmony_ci}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
5878c2ecf20Sopenharmony_ci				     struct iwl_mac_ctx_cmd *cmd)
5888c2ecf20Sopenharmony_ci{
5898c2ecf20Sopenharmony_ci	int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
5908c2ecf20Sopenharmony_ci				       sizeof(*cmd), cmd);
5918c2ecf20Sopenharmony_ci	if (ret)
5928c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
5938c2ecf20Sopenharmony_ci			le32_to_cpu(cmd->action), ret);
5948c2ecf20Sopenharmony_ci	return ret;
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
5988c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif,
5998c2ecf20Sopenharmony_ci				    u32 action, bool force_assoc_off,
6008c2ecf20Sopenharmony_ci				    const u8 *bssid_override)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
6038c2ecf20Sopenharmony_ci	struct iwl_mac_data_sta *ctxt_sta;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_STATION);
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/* Fill the common data for all mac context types */
6088c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	if (vif->p2p) {
6118c2ecf20Sopenharmony_ci		struct ieee80211_p2p_noa_attr *noa =
6128c2ecf20Sopenharmony_ci			&vif->bss_conf.p2p_noa_attr;
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci		cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
6158c2ecf20Sopenharmony_ci					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
6168c2ecf20Sopenharmony_ci		ctxt_sta = &cmd.p2p_sta.sta;
6178c2ecf20Sopenharmony_ci	} else {
6188c2ecf20Sopenharmony_ci		ctxt_sta = &cmd.sta;
6198c2ecf20Sopenharmony_ci	}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	/* We need the dtim_period to set the MAC as associated */
6228c2ecf20Sopenharmony_ci	if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
6238c2ecf20Sopenharmony_ci	    !force_assoc_off) {
6248c2ecf20Sopenharmony_ci		struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
6258c2ecf20Sopenharmony_ci		u8 ap_sta_id = mvmvif->ap_sta_id;
6268c2ecf20Sopenharmony_ci		u32 dtim_offs;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci		/*
6298c2ecf20Sopenharmony_ci		 * The DTIM count counts down, so when it is N that means N
6308c2ecf20Sopenharmony_ci		 * more beacon intervals happen until the DTIM TBTT. Therefore
6318c2ecf20Sopenharmony_ci		 * add this to the current time. If that ends up being in the
6328c2ecf20Sopenharmony_ci		 * future, the firmware will handle it.
6338c2ecf20Sopenharmony_ci		 *
6348c2ecf20Sopenharmony_ci		 * Also note that the system_timestamp (which we get here as
6358c2ecf20Sopenharmony_ci		 * "sync_device_ts") and TSF timestamp aren't at exactly the
6368c2ecf20Sopenharmony_ci		 * same offset in the frame -- the TSF is at the first symbol
6378c2ecf20Sopenharmony_ci		 * of the TSF, the system timestamp is at signal acquisition
6388c2ecf20Sopenharmony_ci		 * time. This means there's an offset between them of at most
6398c2ecf20Sopenharmony_ci		 * a few hundred microseconds (24 * 8 bits + PLCP time gives
6408c2ecf20Sopenharmony_ci		 * 384us in the longest case), this is currently not relevant
6418c2ecf20Sopenharmony_ci		 * as the firmware wakes up around 2ms before the TBTT.
6428c2ecf20Sopenharmony_ci		 */
6438c2ecf20Sopenharmony_ci		dtim_offs = vif->bss_conf.sync_dtim_count *
6448c2ecf20Sopenharmony_ci				vif->bss_conf.beacon_int;
6458c2ecf20Sopenharmony_ci		/* convert TU to usecs */
6468c2ecf20Sopenharmony_ci		dtim_offs *= 1024;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci		ctxt_sta->dtim_tsf =
6498c2ecf20Sopenharmony_ci			cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
6508c2ecf20Sopenharmony_ci		ctxt_sta->dtim_time =
6518c2ecf20Sopenharmony_ci			cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
6528c2ecf20Sopenharmony_ci		ctxt_sta->assoc_beacon_arrive_time =
6538c2ecf20Sopenharmony_ci			cpu_to_le32(vif->bss_conf.sync_device_ts);
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci		IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
6568c2ecf20Sopenharmony_ci			       le64_to_cpu(ctxt_sta->dtim_tsf),
6578c2ecf20Sopenharmony_ci			       le32_to_cpu(ctxt_sta->dtim_time),
6588c2ecf20Sopenharmony_ci			       dtim_offs);
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci		ctxt_sta->is_assoc = cpu_to_le32(1);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci		/*
6638c2ecf20Sopenharmony_ci		 * allow multicast data frames only as long as the station is
6648c2ecf20Sopenharmony_ci		 * authorized, i.e., GTK keys are already installed (if needed)
6658c2ecf20Sopenharmony_ci		 */
6668c2ecf20Sopenharmony_ci		if (ap_sta_id < mvm->fw->ucode_capa.num_stations) {
6678c2ecf20Sopenharmony_ci			struct ieee80211_sta *sta;
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci			rcu_read_lock();
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci			sta = rcu_dereference(mvm->fw_id_to_mac_id[ap_sta_id]);
6728c2ecf20Sopenharmony_ci			if (!IS_ERR_OR_NULL(sta)) {
6738c2ecf20Sopenharmony_ci				struct iwl_mvm_sta *mvmsta =
6748c2ecf20Sopenharmony_ci					iwl_mvm_sta_from_mac80211(sta);
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci				if (mvmsta->sta_state ==
6778c2ecf20Sopenharmony_ci				    IEEE80211_STA_AUTHORIZED)
6788c2ecf20Sopenharmony_ci					cmd.filter_flags |=
6798c2ecf20Sopenharmony_ci						cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
6808c2ecf20Sopenharmony_ci			}
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci			rcu_read_unlock();
6838c2ecf20Sopenharmony_ci		}
6848c2ecf20Sopenharmony_ci	} else {
6858c2ecf20Sopenharmony_ci		ctxt_sta->is_assoc = cpu_to_le32(0);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci		/* Allow beacons to pass through as long as we are not
6888c2ecf20Sopenharmony_ci		 * associated, or we do not have dtim period information.
6898c2ecf20Sopenharmony_ci		 */
6908c2ecf20Sopenharmony_ci		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
6918c2ecf20Sopenharmony_ci	}
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
6948c2ecf20Sopenharmony_ci	ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
6958c2ecf20Sopenharmony_ci					      vif->bss_conf.dtim_period);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
6988c2ecf20Sopenharmony_ci	ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (vif->probe_req_reg && vif->bss_conf.assoc && vif->p2p)
7018c2ecf20Sopenharmony_ci		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	if (vif->bss_conf.he_support && !iwlwifi_mod_params.disable_11ax) {
7048c2ecf20Sopenharmony_ci		cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_11AX);
7058c2ecf20Sopenharmony_ci		if (vif->bss_conf.twt_requester && IWL_MVM_USE_TWT) {
7068c2ecf20Sopenharmony_ci			ctxt_sta->data_policy |= cpu_to_le32(TWT_SUPPORTED);
7078c2ecf20Sopenharmony_ci			if (vif->bss_conf.twt_protected)
7088c2ecf20Sopenharmony_ci				ctxt_sta->data_policy |=
7098c2ecf20Sopenharmony_ci					cpu_to_le32(PROTECTED_TWT_SUPPORTED);
7108c2ecf20Sopenharmony_ci		}
7118c2ecf20Sopenharmony_ci	}
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
7188c2ecf20Sopenharmony_ci					 struct ieee80211_vif *vif,
7198c2ecf20Sopenharmony_ci					 u32 action)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
7228c2ecf20Sopenharmony_ci	u32 tfd_queue_msk = BIT(mvm->snif_queue);
7238c2ecf20Sopenharmony_ci	int ret;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
7268c2ecf20Sopenharmony_ci
7278c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
7288c2ecf20Sopenharmony_ci
7298c2ecf20Sopenharmony_ci	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
7308c2ecf20Sopenharmony_ci				       MAC_FILTER_IN_CONTROL_AND_MGMT |
7318c2ecf20Sopenharmony_ci				       MAC_FILTER_IN_BEACON |
7328c2ecf20Sopenharmony_ci				       MAC_FILTER_IN_PROBE_REQUEST |
7338c2ecf20Sopenharmony_ci				       MAC_FILTER_IN_CRC32 |
7348c2ecf20Sopenharmony_ci				       MAC_FILTER_ACCEPT_GRP);
7358c2ecf20Sopenharmony_ci	ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci	/* Allocate sniffer station */
7388c2ecf20Sopenharmony_ci	ret = iwl_mvm_allocate_int_sta(mvm, &mvm->snif_sta, tfd_queue_msk,
7398c2ecf20Sopenharmony_ci				       vif->type, IWL_STA_GENERAL_PURPOSE);
7408c2ecf20Sopenharmony_ci	if (ret)
7418c2ecf20Sopenharmony_ci		return ret;
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
7448c2ecf20Sopenharmony_ci}
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
7478c2ecf20Sopenharmony_ci				     struct ieee80211_vif *vif,
7488c2ecf20Sopenharmony_ci				     u32 action)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
7518c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
7588c2ecf20Sopenharmony_ci				       MAC_FILTER_IN_PROBE_REQUEST |
7598c2ecf20Sopenharmony_ci				       MAC_FILTER_ACCEPT_GRP);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
7628c2ecf20Sopenharmony_ci	cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	/* TODO: Assumes that the beacon id == mac context id */
7658c2ecf20Sopenharmony_ci	cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
7688c2ecf20Sopenharmony_ci}
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_cistruct iwl_mvm_go_iterator_data {
7718c2ecf20Sopenharmony_ci	bool go_active;
7728c2ecf20Sopenharmony_ci};
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	struct iwl_mvm_go_iterator_data *data = _data;
7778c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
7788c2ecf20Sopenharmony_ci
7798c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
7808c2ecf20Sopenharmony_ci	    mvmvif->ap_ibss_active)
7818c2ecf20Sopenharmony_ci		data->go_active = true;
7828c2ecf20Sopenharmony_ci}
7838c2ecf20Sopenharmony_ci
7848c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
7858c2ecf20Sopenharmony_ci					   struct ieee80211_vif *vif,
7868c2ecf20Sopenharmony_ci					   u32 action)
7878c2ecf20Sopenharmony_ci{
7888c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
7898c2ecf20Sopenharmony_ci	struct iwl_mvm_go_iterator_data data = {};
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	/* Override the filter flags to accept only probe requests */
7968c2ecf20Sopenharmony_ci	cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	/*
7998c2ecf20Sopenharmony_ci	 * This flag should be set to true when the P2P Device is
8008c2ecf20Sopenharmony_ci	 * discoverable and there is at least another active P2P GO. Settings
8018c2ecf20Sopenharmony_ci	 * this flag will allow the P2P Device to be discoverable on other
8028c2ecf20Sopenharmony_ci	 * channels in addition to its listen channel.
8038c2ecf20Sopenharmony_ci	 * Note that this flag should not be set in other cases as it opens the
8048c2ecf20Sopenharmony_ci	 * Rx filters on all MAC and increases the number of interrupts.
8058c2ecf20Sopenharmony_ci	 */
8068c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
8078c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
8088c2ecf20Sopenharmony_ci		iwl_mvm_go_iterator, &data);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
8118c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
8128c2ecf20Sopenharmony_ci}
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_civoid iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
8158c2ecf20Sopenharmony_ci			      __le32 *tim_index, __le32 *tim_size,
8168c2ecf20Sopenharmony_ci			      u8 *beacon, u32 frame_size)
8178c2ecf20Sopenharmony_ci{
8188c2ecf20Sopenharmony_ci	u32 tim_idx;
8198c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	/* The index is relative to frame start but we start looking at the
8228c2ecf20Sopenharmony_ci	 * variable-length part of the beacon. */
8238c2ecf20Sopenharmony_ci	tim_idx = mgmt->u.beacon.variable - beacon;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	/* Parse variable-length elements of beacon to find WLAN_EID_TIM */
8268c2ecf20Sopenharmony_ci	while ((tim_idx < (frame_size - 2)) &&
8278c2ecf20Sopenharmony_ci			(beacon[tim_idx] != WLAN_EID_TIM))
8288c2ecf20Sopenharmony_ci		tim_idx += beacon[tim_idx+1] + 2;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	/* If TIM field was found, set variables */
8318c2ecf20Sopenharmony_ci	if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
8328c2ecf20Sopenharmony_ci		*tim_index = cpu_to_le32(tim_idx);
8338c2ecf20Sopenharmony_ci		*tim_size = cpu_to_le32((u32)beacon[tim_idx + 1]);
8348c2ecf20Sopenharmony_ci	} else {
8358c2ecf20Sopenharmony_ci		IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
8368c2ecf20Sopenharmony_ci	}
8378c2ecf20Sopenharmony_ci}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_cistatic u32 iwl_mvm_find_ie_offset(u8 *beacon, u8 eid, u32 frame_size)
8408c2ecf20Sopenharmony_ci{
8418c2ecf20Sopenharmony_ci	struct ieee80211_mgmt *mgmt = (void *)beacon;
8428c2ecf20Sopenharmony_ci	const u8 *ie;
8438c2ecf20Sopenharmony_ci
8448c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(frame_size <= (mgmt->u.beacon.variable - beacon)))
8458c2ecf20Sopenharmony_ci		return 0;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	frame_size -= mgmt->u.beacon.variable - beacon;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	ie = cfg80211_find_ie(eid, mgmt->u.beacon.variable, frame_size);
8508c2ecf20Sopenharmony_ci	if (!ie)
8518c2ecf20Sopenharmony_ci		return 0;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return ie - beacon;
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ciu8 iwl_mvm_mac_ctxt_get_lowest_rate(struct ieee80211_tx_info *info,
8578c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif)
8588c2ecf20Sopenharmony_ci{
8598c2ecf20Sopenharmony_ci	u8 rate;
8608c2ecf20Sopenharmony_ci	if (info->band == NL80211_BAND_2GHZ && !vif->p2p)
8618c2ecf20Sopenharmony_ci		rate = IWL_FIRST_CCK_RATE;
8628c2ecf20Sopenharmony_ci	else
8638c2ecf20Sopenharmony_ci		rate = IWL_FIRST_OFDM_RATE;
8648c2ecf20Sopenharmony_ci
8658c2ecf20Sopenharmony_ci	return rate;
8668c2ecf20Sopenharmony_ci}
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_ctxt_set_tx(struct iwl_mvm *mvm,
8698c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif,
8708c2ecf20Sopenharmony_ci				    struct sk_buff *beacon,
8718c2ecf20Sopenharmony_ci				    struct iwl_tx_cmd *tx)
8728c2ecf20Sopenharmony_ci{
8738c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8748c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info;
8758c2ecf20Sopenharmony_ci	u8 rate;
8768c2ecf20Sopenharmony_ci	u32 tx_flags;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	info = IEEE80211_SKB_CB(beacon);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	/* Set up TX command fields */
8818c2ecf20Sopenharmony_ci	tx->len = cpu_to_le16((u16)beacon->len);
8828c2ecf20Sopenharmony_ci	tx->sta_id = mvmvif->bcast_sta.sta_id;
8838c2ecf20Sopenharmony_ci	tx->life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
8848c2ecf20Sopenharmony_ci	tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
8858c2ecf20Sopenharmony_ci	tx_flags |=
8868c2ecf20Sopenharmony_ci		iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
8878c2ecf20Sopenharmony_ci						TX_CMD_FLG_BT_PRIO_POS;
8888c2ecf20Sopenharmony_ci	tx->tx_flags = cpu_to_le32(tx_flags);
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	if (!fw_has_capa(&mvm->fw->ucode_capa,
8918c2ecf20Sopenharmony_ci			 IWL_UCODE_TLV_CAPA_BEACON_ANT_SELECTION))
8928c2ecf20Sopenharmony_ci		iwl_mvm_toggle_tx_ant(mvm, &mvm->mgmt_last_antenna_idx);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	tx->rate_n_flags =
8958c2ecf20Sopenharmony_ci		cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
8968c2ecf20Sopenharmony_ci			    RATE_MCS_ANT_POS);
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci	rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	tx->rate_n_flags |= cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
9018c2ecf20Sopenharmony_ci	if (rate == IWL_FIRST_CCK_RATE)
9028c2ecf20Sopenharmony_ci		tx->rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_send_beacon_cmd(struct iwl_mvm *mvm,
9078c2ecf20Sopenharmony_ci				     struct sk_buff *beacon,
9088c2ecf20Sopenharmony_ci				     void *data, int len)
9098c2ecf20Sopenharmony_ci{
9108c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
9118c2ecf20Sopenharmony_ci		.id = BEACON_TEMPLATE_CMD,
9128c2ecf20Sopenharmony_ci		.flags = CMD_ASYNC,
9138c2ecf20Sopenharmony_ci	};
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	cmd.len[0] = len;
9168c2ecf20Sopenharmony_ci	cmd.data[0] = data;
9178c2ecf20Sopenharmony_ci	cmd.dataflags[0] = 0;
9188c2ecf20Sopenharmony_ci	cmd.len[1] = beacon->len;
9198c2ecf20Sopenharmony_ci	cmd.data[1] = beacon->data;
9208c2ecf20Sopenharmony_ci	cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ci	return iwl_mvm_send_cmd(mvm, &cmd);
9238c2ecf20Sopenharmony_ci}
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_send_beacon_v6(struct iwl_mvm *mvm,
9268c2ecf20Sopenharmony_ci					   struct ieee80211_vif *vif,
9278c2ecf20Sopenharmony_ci					   struct sk_buff *beacon)
9288c2ecf20Sopenharmony_ci{
9298c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9308c2ecf20Sopenharmony_ci	struct iwl_mac_beacon_cmd_v6 beacon_cmd = {};
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
9358c2ecf20Sopenharmony_ci
9368c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
9378c2ecf20Sopenharmony_ci		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
9388c2ecf20Sopenharmony_ci					 &beacon_cmd.tim_size,
9398c2ecf20Sopenharmony_ci					 beacon->data, beacon->len);
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
9428c2ecf20Sopenharmony_ci						sizeof(beacon_cmd));
9438c2ecf20Sopenharmony_ci}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_send_beacon_v7(struct iwl_mvm *mvm,
9468c2ecf20Sopenharmony_ci					   struct ieee80211_vif *vif,
9478c2ecf20Sopenharmony_ci					   struct sk_buff *beacon)
9488c2ecf20Sopenharmony_ci{
9498c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9508c2ecf20Sopenharmony_ci	struct iwl_mac_beacon_cmd_v7 beacon_cmd = {};
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_set_tx(mvm, vif, beacon, &beacon_cmd.tx);
9538c2ecf20Sopenharmony_ci
9548c2ecf20Sopenharmony_ci	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
9578c2ecf20Sopenharmony_ci		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
9588c2ecf20Sopenharmony_ci					 &beacon_cmd.tim_size,
9598c2ecf20Sopenharmony_ci					 beacon->data, beacon->len);
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_ci	beacon_cmd.csa_offset =
9628c2ecf20Sopenharmony_ci		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
9638c2ecf20Sopenharmony_ci						   WLAN_EID_CHANNEL_SWITCH,
9648c2ecf20Sopenharmony_ci						   beacon->len));
9658c2ecf20Sopenharmony_ci	beacon_cmd.ecsa_offset =
9668c2ecf20Sopenharmony_ci		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
9678c2ecf20Sopenharmony_ci						   WLAN_EID_EXT_CHANSWITCH_ANN,
9688c2ecf20Sopenharmony_ci						   beacon->len));
9698c2ecf20Sopenharmony_ci
9708c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
9718c2ecf20Sopenharmony_ci						sizeof(beacon_cmd));
9728c2ecf20Sopenharmony_ci}
9738c2ecf20Sopenharmony_ci
9748c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_send_beacon_v9(struct iwl_mvm *mvm,
9758c2ecf20Sopenharmony_ci					   struct ieee80211_vif *vif,
9768c2ecf20Sopenharmony_ci					   struct sk_buff *beacon)
9778c2ecf20Sopenharmony_ci{
9788c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
9798c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(beacon);
9808c2ecf20Sopenharmony_ci	struct iwl_mac_beacon_cmd beacon_cmd = {};
9818c2ecf20Sopenharmony_ci	u8 rate = iwl_mvm_mac_ctxt_get_lowest_rate(info, vif);
9828c2ecf20Sopenharmony_ci	u16 flags;
9838c2ecf20Sopenharmony_ci
9848c2ecf20Sopenharmony_ci	flags = iwl_mvm_mac80211_idx_to_hwrate(rate);
9858c2ecf20Sopenharmony_ci
9868c2ecf20Sopenharmony_ci	if (rate == IWL_FIRST_CCK_RATE)
9878c2ecf20Sopenharmony_ci		flags |= IWL_MAC_BEACON_CCK;
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	beacon_cmd.flags = cpu_to_le16(flags);
9908c2ecf20Sopenharmony_ci	beacon_cmd.byte_cnt = cpu_to_le16((u16)beacon->len);
9918c2ecf20Sopenharmony_ci	beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
9948c2ecf20Sopenharmony_ci		iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd.tim_idx,
9958c2ecf20Sopenharmony_ci					 &beacon_cmd.tim_size,
9968c2ecf20Sopenharmony_ci					 beacon->data, beacon->len);
9978c2ecf20Sopenharmony_ci
9988c2ecf20Sopenharmony_ci	beacon_cmd.csa_offset =
9998c2ecf20Sopenharmony_ci		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
10008c2ecf20Sopenharmony_ci						   WLAN_EID_CHANNEL_SWITCH,
10018c2ecf20Sopenharmony_ci						   beacon->len));
10028c2ecf20Sopenharmony_ci	beacon_cmd.ecsa_offset =
10038c2ecf20Sopenharmony_ci		cpu_to_le32(iwl_mvm_find_ie_offset(beacon->data,
10048c2ecf20Sopenharmony_ci						   WLAN_EID_EXT_CHANSWITCH_ANN,
10058c2ecf20Sopenharmony_ci						   beacon->len));
10068c2ecf20Sopenharmony_ci
10078c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_beacon_cmd(mvm, beacon, &beacon_cmd,
10088c2ecf20Sopenharmony_ci						sizeof(beacon_cmd));
10098c2ecf20Sopenharmony_ci}
10108c2ecf20Sopenharmony_ci
10118c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
10128c2ecf20Sopenharmony_ci				 struct ieee80211_vif *vif,
10138c2ecf20Sopenharmony_ci				 struct sk_buff *beacon)
10148c2ecf20Sopenharmony_ci{
10158c2ecf20Sopenharmony_ci	if (WARN_ON(!beacon))
10168c2ecf20Sopenharmony_ci		return -EINVAL;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	if (IWL_MVM_NON_TRANSMITTING_AP)
10198c2ecf20Sopenharmony_ci		return 0;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	if (!fw_has_capa(&mvm->fw->ucode_capa,
10228c2ecf20Sopenharmony_ci			 IWL_UCODE_TLV_CAPA_CSA_AND_TBTT_OFFLOAD))
10238c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_send_beacon_v6(mvm, vif, beacon);
10248c2ecf20Sopenharmony_ci
10258c2ecf20Sopenharmony_ci	if (fw_has_api(&mvm->fw->ucode_capa,
10268c2ecf20Sopenharmony_ci		       IWL_UCODE_TLV_API_NEW_BEACON_TEMPLATE))
10278c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_send_beacon_v9(mvm, vif, beacon);
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_beacon_v7(mvm, vif, beacon);
10308c2ecf20Sopenharmony_ci}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci/* The beacon template for the AP/GO/IBSS has changed and needs update */
10338c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
10348c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif)
10358c2ecf20Sopenharmony_ci{
10368c2ecf20Sopenharmony_ci	struct sk_buff *beacon;
10378c2ecf20Sopenharmony_ci	int ret;
10388c2ecf20Sopenharmony_ci
10398c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_AP &&
10408c2ecf20Sopenharmony_ci		vif->type != NL80211_IFTYPE_ADHOC);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci	beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
10438c2ecf20Sopenharmony_ci	if (!beacon)
10448c2ecf20Sopenharmony_ci		return -ENOMEM;
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci#ifdef CONFIG_IWLWIFI_DEBUGFS
10478c2ecf20Sopenharmony_ci	if (mvm->beacon_inject_active) {
10488c2ecf20Sopenharmony_ci		dev_kfree_skb(beacon);
10498c2ecf20Sopenharmony_ci		return -EBUSY;
10508c2ecf20Sopenharmony_ci	}
10518c2ecf20Sopenharmony_ci#endif
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
10548c2ecf20Sopenharmony_ci	dev_kfree_skb(beacon);
10558c2ecf20Sopenharmony_ci	return ret;
10568c2ecf20Sopenharmony_ci}
10578c2ecf20Sopenharmony_ci
10588c2ecf20Sopenharmony_cistruct iwl_mvm_mac_ap_iterator_data {
10598c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
10608c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
10618c2ecf20Sopenharmony_ci	u32 beacon_device_ts;
10628c2ecf20Sopenharmony_ci	u16 beacon_int;
10638c2ecf20Sopenharmony_ci};
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci/* Find the beacon_device_ts and beacon_int for a managed interface */
10668c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
10678c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif)
10688c2ecf20Sopenharmony_ci{
10698c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_ap_iterator_data *data = _data;
10708c2ecf20Sopenharmony_ci
10718c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
10728c2ecf20Sopenharmony_ci		return;
10738c2ecf20Sopenharmony_ci
10748c2ecf20Sopenharmony_ci	/* Station client has higher priority over P2P client*/
10758c2ecf20Sopenharmony_ci	if (vif->p2p && data->beacon_device_ts)
10768c2ecf20Sopenharmony_ci		return;
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_ci	data->beacon_device_ts = vif->bss_conf.sync_device_ts;
10798c2ecf20Sopenharmony_ci	data->beacon_int = vif->bss_conf.beacon_int;
10808c2ecf20Sopenharmony_ci}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_ci/*
10838c2ecf20Sopenharmony_ci * Fill the specific data for mac context of type AP of P2P GO
10848c2ecf20Sopenharmony_ci */
10858c2ecf20Sopenharmony_cistatic void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
10868c2ecf20Sopenharmony_ci					 struct ieee80211_vif *vif,
10878c2ecf20Sopenharmony_ci					 struct iwl_mac_ctx_cmd *cmd,
10888c2ecf20Sopenharmony_ci					 struct iwl_mac_data_ap *ctxt_ap,
10898c2ecf20Sopenharmony_ci					 bool add)
10908c2ecf20Sopenharmony_ci{
10918c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
10928c2ecf20Sopenharmony_ci	struct iwl_mvm_mac_ap_iterator_data data = {
10938c2ecf20Sopenharmony_ci		.mvm = mvm,
10948c2ecf20Sopenharmony_ci		.vif = vif,
10958c2ecf20Sopenharmony_ci		.beacon_device_ts = 0
10968c2ecf20Sopenharmony_ci	};
10978c2ecf20Sopenharmony_ci
10988c2ecf20Sopenharmony_ci	/* in AP mode, the MCAST FIFO takes the EDCA params from VO */
10998c2ecf20Sopenharmony_ci	cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	/*
11028c2ecf20Sopenharmony_ci	 * in AP mode, pass probe requests and beacons from other APs
11038c2ecf20Sopenharmony_ci	 * (needed for ht protection); when there're no any associated
11048c2ecf20Sopenharmony_ci	 * station don't ask FW to pass beacons to prevent unnecessary
11058c2ecf20Sopenharmony_ci	 * wake-ups.
11068c2ecf20Sopenharmony_ci	 */
11078c2ecf20Sopenharmony_ci	cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
11088c2ecf20Sopenharmony_ci	if (mvmvif->ap_assoc_sta_count || !mvm->drop_bcn_ap_mode) {
11098c2ecf20Sopenharmony_ci		cmd->filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
11108c2ecf20Sopenharmony_ci		IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
11118c2ecf20Sopenharmony_ci	} else {
11128c2ecf20Sopenharmony_ci		IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
11138c2ecf20Sopenharmony_ci	}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
11168c2ecf20Sopenharmony_ci	ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
11178c2ecf20Sopenharmony_ci					     vif->bss_conf.dtim_period);
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	if (!fw_has_api(&mvm->fw->ucode_capa,
11208c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_API_STA_TYPE))
11218c2ecf20Sopenharmony_ci		ctxt_ap->mcast_qid = cpu_to_le32(mvmvif->cab_queue);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	/*
11248c2ecf20Sopenharmony_ci	 * Only set the beacon time when the MAC is being added, when we
11258c2ecf20Sopenharmony_ci	 * just modify the MAC then we should keep the time -- the firmware
11268c2ecf20Sopenharmony_ci	 * can otherwise have a "jumping" TBTT.
11278c2ecf20Sopenharmony_ci	 */
11288c2ecf20Sopenharmony_ci	if (add) {
11298c2ecf20Sopenharmony_ci		/*
11308c2ecf20Sopenharmony_ci		 * If there is a station/P2P client interface which is
11318c2ecf20Sopenharmony_ci		 * associated, set the AP's TBTT far enough from the station's
11328c2ecf20Sopenharmony_ci		 * TBTT. Otherwise, set it to the current system time
11338c2ecf20Sopenharmony_ci		 */
11348c2ecf20Sopenharmony_ci		ieee80211_iterate_active_interfaces_atomic(
11358c2ecf20Sopenharmony_ci			mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
11368c2ecf20Sopenharmony_ci			iwl_mvm_mac_ap_iterator, &data);
11378c2ecf20Sopenharmony_ci
11388c2ecf20Sopenharmony_ci		if (data.beacon_device_ts) {
11398c2ecf20Sopenharmony_ci			u32 rand = (prandom_u32() % (64 - 36)) + 36;
11408c2ecf20Sopenharmony_ci			mvmvif->ap_beacon_time = data.beacon_device_ts +
11418c2ecf20Sopenharmony_ci				ieee80211_tu_to_usec(data.beacon_int * rand /
11428c2ecf20Sopenharmony_ci						     100);
11438c2ecf20Sopenharmony_ci		} else {
11448c2ecf20Sopenharmony_ci			mvmvif->ap_beacon_time = iwl_mvm_get_systime(mvm);
11458c2ecf20Sopenharmony_ci		}
11468c2ecf20Sopenharmony_ci	}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_ci	ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
11498c2ecf20Sopenharmony_ci	ctxt_ap->beacon_tsf = 0; /* unused */
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	/* TODO: Assume that the beacon id == mac context id */
11528c2ecf20Sopenharmony_ci	ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
11538c2ecf20Sopenharmony_ci}
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
11568c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif,
11578c2ecf20Sopenharmony_ci				   u32 action)
11588c2ecf20Sopenharmony_ci{
11598c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
11608c2ecf20Sopenharmony_ci
11618c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	/* Fill the common data for all mac context types */
11648c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	/* Fill the data specific for ap mode */
11678c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.ap,
11688c2ecf20Sopenharmony_ci				     action == FW_CTXT_ACTION_ADD);
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
11718c2ecf20Sopenharmony_ci}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
11748c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif,
11758c2ecf20Sopenharmony_ci				   u32 action)
11768c2ecf20Sopenharmony_ci{
11778c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd = {};
11788c2ecf20Sopenharmony_ci	struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
11798c2ecf20Sopenharmony_ci
11808c2ecf20Sopenharmony_ci	WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	/* Fill the common data for all mac context types */
11838c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_ci	/* Fill the data specific for GO mode */
11868c2ecf20Sopenharmony_ci	iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd, &cmd.go.ap,
11878c2ecf20Sopenharmony_ci				     action == FW_CTXT_ACTION_ADD);
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
11908c2ecf20Sopenharmony_ci					IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
11918c2ecf20Sopenharmony_ci	cmd.go.opp_ps_enabled =
11928c2ecf20Sopenharmony_ci			cpu_to_le32(!!(noa->oppps_ctwindow &
11938c2ecf20Sopenharmony_ci					IEEE80211_P2P_OPPPS_ENABLE_BIT));
11948c2ecf20Sopenharmony_ci
11958c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
11968c2ecf20Sopenharmony_ci}
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_cistatic int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
11998c2ecf20Sopenharmony_ci				u32 action, bool force_assoc_off,
12008c2ecf20Sopenharmony_ci				const u8 *bssid_override)
12018c2ecf20Sopenharmony_ci{
12028c2ecf20Sopenharmony_ci	switch (vif->type) {
12038c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
12048c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
12058c2ecf20Sopenharmony_ci						force_assoc_off,
12068c2ecf20Sopenharmony_ci						bssid_override);
12078c2ecf20Sopenharmony_ci		break;
12088c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_AP:
12098c2ecf20Sopenharmony_ci		if (!vif->p2p)
12108c2ecf20Sopenharmony_ci			return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
12118c2ecf20Sopenharmony_ci		else
12128c2ecf20Sopenharmony_ci			return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
12138c2ecf20Sopenharmony_ci		break;
12148c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_MONITOR:
12158c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
12168c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_DEVICE:
12178c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
12188c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_ADHOC:
12198c2ecf20Sopenharmony_ci		return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
12208c2ecf20Sopenharmony_ci	default:
12218c2ecf20Sopenharmony_ci		break;
12228c2ecf20Sopenharmony_ci	}
12238c2ecf20Sopenharmony_ci
12248c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
12258c2ecf20Sopenharmony_ci}
12268c2ecf20Sopenharmony_ci
12278c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
12288c2ecf20Sopenharmony_ci{
12298c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
12308c2ecf20Sopenharmony_ci	int ret;
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_ci	if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
12338c2ecf20Sopenharmony_ci		      vif->addr, ieee80211_vif_type_p2p(vif)))
12348c2ecf20Sopenharmony_ci		return -EIO;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
12378c2ecf20Sopenharmony_ci				   true, NULL);
12388c2ecf20Sopenharmony_ci	if (ret)
12398c2ecf20Sopenharmony_ci		return ret;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci	/* will only do anything at resume from D3 time */
12428c2ecf20Sopenharmony_ci	iwl_mvm_set_last_nonqos_seq(mvm, vif);
12438c2ecf20Sopenharmony_ci
12448c2ecf20Sopenharmony_ci	mvmvif->uploaded = true;
12458c2ecf20Sopenharmony_ci	return 0;
12468c2ecf20Sopenharmony_ci}
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
12498c2ecf20Sopenharmony_ci			     bool force_assoc_off, const u8 *bssid_override)
12508c2ecf20Sopenharmony_ci{
12518c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci	if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
12548c2ecf20Sopenharmony_ci		      vif->addr, ieee80211_vif_type_p2p(vif)))
12558c2ecf20Sopenharmony_ci		return -EIO;
12568c2ecf20Sopenharmony_ci
12578c2ecf20Sopenharmony_ci	return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
12588c2ecf20Sopenharmony_ci				    force_assoc_off, bssid_override);
12598c2ecf20Sopenharmony_ci}
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ciint iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
12628c2ecf20Sopenharmony_ci{
12638c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
12648c2ecf20Sopenharmony_ci	struct iwl_mac_ctx_cmd cmd;
12658c2ecf20Sopenharmony_ci	int ret;
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_ci	if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
12688c2ecf20Sopenharmony_ci		      vif->addr, ieee80211_vif_type_p2p(vif)))
12698c2ecf20Sopenharmony_ci		return -EIO;
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ci	memset(&cmd, 0, sizeof(cmd));
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
12748c2ecf20Sopenharmony_ci							   mvmvif->color));
12758c2ecf20Sopenharmony_ci	cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
12768c2ecf20Sopenharmony_ci
12778c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
12788c2ecf20Sopenharmony_ci				   sizeof(cmd), &cmd);
12798c2ecf20Sopenharmony_ci	if (ret) {
12808c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
12818c2ecf20Sopenharmony_ci		return ret;
12828c2ecf20Sopenharmony_ci	}
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	mvmvif->uploaded = false;
12858c2ecf20Sopenharmony_ci
12868c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_MONITOR) {
12878c2ecf20Sopenharmony_ci		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
12888c2ecf20Sopenharmony_ci		iwl_mvm_dealloc_snif_sta(mvm);
12898c2ecf20Sopenharmony_ci	}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_ci	return 0;
12928c2ecf20Sopenharmony_ci}
12938c2ecf20Sopenharmony_ci
12948c2ecf20Sopenharmony_cistatic void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
12958c2ecf20Sopenharmony_ci				   struct ieee80211_vif *csa_vif, u32 gp2,
12968c2ecf20Sopenharmony_ci				   bool tx_success)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif =
12998c2ecf20Sopenharmony_ci			iwl_mvm_vif_from_mac80211(csa_vif);
13008c2ecf20Sopenharmony_ci
13018c2ecf20Sopenharmony_ci	/* Don't start to countdown from a failed beacon */
13028c2ecf20Sopenharmony_ci	if (!tx_success && !mvmvif->csa_countdown)
13038c2ecf20Sopenharmony_ci		return;
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	mvmvif->csa_countdown = true;
13068c2ecf20Sopenharmony_ci
13078c2ecf20Sopenharmony_ci	if (!ieee80211_beacon_cntdwn_is_complete(csa_vif)) {
13088c2ecf20Sopenharmony_ci		int c = ieee80211_beacon_update_cntdwn(csa_vif);
13098c2ecf20Sopenharmony_ci
13108c2ecf20Sopenharmony_ci		iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
13118c2ecf20Sopenharmony_ci		if (csa_vif->p2p &&
13128c2ecf20Sopenharmony_ci		    !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
13138c2ecf20Sopenharmony_ci		    tx_success) {
13148c2ecf20Sopenharmony_ci			u32 rel_time = (c + 1) *
13158c2ecf20Sopenharmony_ci				       csa_vif->bss_conf.beacon_int -
13168c2ecf20Sopenharmony_ci				       IWL_MVM_CHANNEL_SWITCH_TIME_GO;
13178c2ecf20Sopenharmony_ci			u32 apply_time = gp2 + rel_time * 1024;
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci			iwl_mvm_schedule_csa_period(mvm, csa_vif,
13208c2ecf20Sopenharmony_ci					 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
13218c2ecf20Sopenharmony_ci					 IWL_MVM_CHANNEL_SWITCH_MARGIN,
13228c2ecf20Sopenharmony_ci					 apply_time);
13238c2ecf20Sopenharmony_ci		}
13248c2ecf20Sopenharmony_ci	} else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
13258c2ecf20Sopenharmony_ci		/* we don't have CSA NoA scheduled yet, switch now */
13268c2ecf20Sopenharmony_ci		ieee80211_csa_finish(csa_vif);
13278c2ecf20Sopenharmony_ci		RCU_INIT_POINTER(mvm->csa_vif, NULL);
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci}
13308c2ecf20Sopenharmony_ci
13318c2ecf20Sopenharmony_civoid iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
13328c2ecf20Sopenharmony_ci			     struct iwl_rx_cmd_buffer *rxb)
13338c2ecf20Sopenharmony_ci{
13348c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
13358c2ecf20Sopenharmony_ci	struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
13368c2ecf20Sopenharmony_ci	struct iwl_extended_beacon_notif_v5 *beacon_v5 = (void *)pkt->data;
13378c2ecf20Sopenharmony_ci	struct ieee80211_vif *csa_vif;
13388c2ecf20Sopenharmony_ci	struct ieee80211_vif *tx_blocked_vif;
13398c2ecf20Sopenharmony_ci	struct agg_tx_status *agg_status;
13408c2ecf20Sopenharmony_ci	u16 status;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
13458c2ecf20Sopenharmony_ci
13468c2ecf20Sopenharmony_ci	if (!iwl_mvm_is_short_beacon_notif_supported(mvm)) {
13478c2ecf20Sopenharmony_ci		struct iwl_mvm_tx_resp *beacon_notify_hdr =
13488c2ecf20Sopenharmony_ci			&beacon_v5->beacon_notify_hdr;
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci		mvm->ibss_manager = beacon_v5->ibss_mgr_status != 0;
13518c2ecf20Sopenharmony_ci		agg_status = iwl_mvm_get_agg_status(mvm, beacon_notify_hdr);
13528c2ecf20Sopenharmony_ci		status = le16_to_cpu(agg_status->status) & TX_STATUS_MSK;
13538c2ecf20Sopenharmony_ci		IWL_DEBUG_RX(mvm,
13548c2ecf20Sopenharmony_ci			     "beacon status %#x retries:%d tsf:0x%016llX gp2:0x%X rate:%d\n",
13558c2ecf20Sopenharmony_ci			     status, beacon_notify_hdr->failure_frame,
13568c2ecf20Sopenharmony_ci			     le64_to_cpu(beacon->tsf),
13578c2ecf20Sopenharmony_ci			     mvm->ap_last_beacon_gp2,
13588c2ecf20Sopenharmony_ci			     le32_to_cpu(beacon_notify_hdr->initial_rate));
13598c2ecf20Sopenharmony_ci	} else {
13608c2ecf20Sopenharmony_ci		mvm->ibss_manager = beacon->ibss_mgr_status != 0;
13618c2ecf20Sopenharmony_ci		status = le32_to_cpu(beacon->status) & TX_STATUS_MSK;
13628c2ecf20Sopenharmony_ci		IWL_DEBUG_RX(mvm,
13638c2ecf20Sopenharmony_ci			     "beacon status %#x tsf:0x%016llX gp2:0x%X\n",
13648c2ecf20Sopenharmony_ci			     status, le64_to_cpu(beacon->tsf),
13658c2ecf20Sopenharmony_ci			     mvm->ap_last_beacon_gp2);
13668c2ecf20Sopenharmony_ci	}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	csa_vif = rcu_dereference_protected(mvm->csa_vif,
13698c2ecf20Sopenharmony_ci					    lockdep_is_held(&mvm->mutex));
13708c2ecf20Sopenharmony_ci	if (unlikely(csa_vif && csa_vif->csa_active))
13718c2ecf20Sopenharmony_ci		iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
13728c2ecf20Sopenharmony_ci				       (status == TX_STATUS_SUCCESS));
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
13758c2ecf20Sopenharmony_ci						lockdep_is_held(&mvm->mutex));
13768c2ecf20Sopenharmony_ci	if (unlikely(tx_blocked_vif)) {
13778c2ecf20Sopenharmony_ci		struct iwl_mvm_vif *mvmvif =
13788c2ecf20Sopenharmony_ci			iwl_mvm_vif_from_mac80211(tx_blocked_vif);
13798c2ecf20Sopenharmony_ci
13808c2ecf20Sopenharmony_ci		/*
13818c2ecf20Sopenharmony_ci		 * The channel switch is started and we have blocked the
13828c2ecf20Sopenharmony_ci		 * stations. If this is the first beacon (the timeout wasn't
13838c2ecf20Sopenharmony_ci		 * set), set the unblock timeout, otherwise countdown
13848c2ecf20Sopenharmony_ci		 */
13858c2ecf20Sopenharmony_ci		if (!mvm->csa_tx_block_bcn_timeout)
13868c2ecf20Sopenharmony_ci			mvm->csa_tx_block_bcn_timeout =
13878c2ecf20Sopenharmony_ci				IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
13888c2ecf20Sopenharmony_ci		else
13898c2ecf20Sopenharmony_ci			mvm->csa_tx_block_bcn_timeout--;
13908c2ecf20Sopenharmony_ci
13918c2ecf20Sopenharmony_ci		/* Check if the timeout is expired, and unblock tx */
13928c2ecf20Sopenharmony_ci		if (mvm->csa_tx_block_bcn_timeout == 0) {
13938c2ecf20Sopenharmony_ci			iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
13948c2ecf20Sopenharmony_ci			RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
13958c2ecf20Sopenharmony_ci		}
13968c2ecf20Sopenharmony_ci	}
13978c2ecf20Sopenharmony_ci}
13988c2ecf20Sopenharmony_ci
13998c2ecf20Sopenharmony_civoid iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
14008c2ecf20Sopenharmony_ci				     struct iwl_rx_cmd_buffer *rxb)
14018c2ecf20Sopenharmony_ci{
14028c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
14038c2ecf20Sopenharmony_ci	struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
14048c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
14058c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_tlv *trigger;
14068c2ecf20Sopenharmony_ci	u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
14078c2ecf20Sopenharmony_ci	u32 rx_missed_bcon, rx_missed_bcon_since_rx;
14088c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
14098c2ecf20Sopenharmony_ci	u32 id = le32_to_cpu(mb->mac_id);
14108c2ecf20Sopenharmony_ci	union iwl_dbg_tlv_tp_data tp_data = { .fw_pkt = pkt };
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	IWL_DEBUG_INFO(mvm,
14138c2ecf20Sopenharmony_ci		       "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
14148c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->mac_id),
14158c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->consec_missed_beacons),
14168c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
14178c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->num_recvd_beacons),
14188c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->num_expected_beacons));
14198c2ecf20Sopenharmony_ci
14208c2ecf20Sopenharmony_ci	rcu_read_lock();
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
14238c2ecf20Sopenharmony_ci	if (!vif)
14248c2ecf20Sopenharmony_ci		goto out;
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_ci	rx_missed_bcon = le32_to_cpu(mb->consec_missed_beacons);
14278c2ecf20Sopenharmony_ci	rx_missed_bcon_since_rx =
14288c2ecf20Sopenharmony_ci		le32_to_cpu(mb->consec_missed_beacons_since_last_rx);
14298c2ecf20Sopenharmony_ci	/*
14308c2ecf20Sopenharmony_ci	 * TODO: the threshold should be adjusted based on latency conditions,
14318c2ecf20Sopenharmony_ci	 * and/or in case of a CS flow on one of the other AP vifs.
14328c2ecf20Sopenharmony_ci	 */
14338c2ecf20Sopenharmony_ci	if (rx_missed_bcon > IWL_MVM_MISSED_BEACONS_THRESHOLD_LONG)
14348c2ecf20Sopenharmony_ci		iwl_mvm_connection_loss(mvm, vif, "missed beacons");
14358c2ecf20Sopenharmony_ci	else if (rx_missed_bcon_since_rx > IWL_MVM_MISSED_BEACONS_THRESHOLD)
14368c2ecf20Sopenharmony_ci		ieee80211_beacon_loss(vif);
14378c2ecf20Sopenharmony_ci
14388c2ecf20Sopenharmony_ci	iwl_dbg_tlv_time_point(&mvm->fwrt,
14398c2ecf20Sopenharmony_ci			       IWL_FW_INI_TIME_POINT_MISSED_BEACONS, &tp_data);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	trigger = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
14428c2ecf20Sopenharmony_ci					FW_DBG_TRIGGER_MISSED_BEACONS);
14438c2ecf20Sopenharmony_ci	if (!trigger)
14448c2ecf20Sopenharmony_ci		goto out;
14458c2ecf20Sopenharmony_ci
14468c2ecf20Sopenharmony_ci	bcon_trig = (void *)trigger->data;
14478c2ecf20Sopenharmony_ci	stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
14488c2ecf20Sopenharmony_ci	stop_trig_missed_bcon_since_rx =
14498c2ecf20Sopenharmony_ci		le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
14508c2ecf20Sopenharmony_ci
14518c2ecf20Sopenharmony_ci	/* TODO: implement start trigger */
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
14548c2ecf20Sopenharmony_ci	    rx_missed_bcon >= stop_trig_missed_bcon)
14558c2ecf20Sopenharmony_ci		iwl_fw_dbg_collect_trig(&mvm->fwrt, trigger, NULL);
14568c2ecf20Sopenharmony_ci
14578c2ecf20Sopenharmony_ciout:
14588c2ecf20Sopenharmony_ci	rcu_read_unlock();
14598c2ecf20Sopenharmony_ci}
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_civoid iwl_mvm_rx_stored_beacon_notif(struct iwl_mvm *mvm,
14628c2ecf20Sopenharmony_ci				    struct iwl_rx_cmd_buffer *rxb)
14638c2ecf20Sopenharmony_ci{
14648c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
14658c2ecf20Sopenharmony_ci	struct iwl_stored_beacon_notif *sb = (void *)pkt->data;
14668c2ecf20Sopenharmony_ci	struct ieee80211_rx_status rx_status;
14678c2ecf20Sopenharmony_ci	struct sk_buff *skb;
14688c2ecf20Sopenharmony_ci	u32 size = le32_to_cpu(sb->byte_count);
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_ci	if (size == 0)
14718c2ecf20Sopenharmony_ci		return;
14728c2ecf20Sopenharmony_ci
14738c2ecf20Sopenharmony_ci	skb = alloc_skb(size, GFP_ATOMIC);
14748c2ecf20Sopenharmony_ci	if (!skb) {
14758c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "alloc_skb failed\n");
14768c2ecf20Sopenharmony_ci		return;
14778c2ecf20Sopenharmony_ci	}
14788c2ecf20Sopenharmony_ci
14798c2ecf20Sopenharmony_ci	/* update rx_status according to the notification's metadata */
14808c2ecf20Sopenharmony_ci	memset(&rx_status, 0, sizeof(rx_status));
14818c2ecf20Sopenharmony_ci	rx_status.mactime = le64_to_cpu(sb->tsf);
14828c2ecf20Sopenharmony_ci	/* TSF as indicated by the firmware  is at INA time */
14838c2ecf20Sopenharmony_ci	rx_status.flag |= RX_FLAG_MACTIME_PLCP_START;
14848c2ecf20Sopenharmony_ci	rx_status.device_timestamp = le32_to_cpu(sb->system_time);
14858c2ecf20Sopenharmony_ci	rx_status.band =
14868c2ecf20Sopenharmony_ci		(sb->band & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ?
14878c2ecf20Sopenharmony_ci				NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
14888c2ecf20Sopenharmony_ci	rx_status.freq =
14898c2ecf20Sopenharmony_ci		ieee80211_channel_to_frequency(le16_to_cpu(sb->channel),
14908c2ecf20Sopenharmony_ci					       rx_status.band);
14918c2ecf20Sopenharmony_ci
14928c2ecf20Sopenharmony_ci	/* copy the data */
14938c2ecf20Sopenharmony_ci	skb_put_data(skb, sb->data, size);
14948c2ecf20Sopenharmony_ci	memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
14958c2ecf20Sopenharmony_ci
14968c2ecf20Sopenharmony_ci	/* pass it as regular rx to mac80211 */
14978c2ecf20Sopenharmony_ci	ieee80211_rx_napi(mvm->hw, NULL, skb, NULL);
14988c2ecf20Sopenharmony_ci}
14998c2ecf20Sopenharmony_ci
15008c2ecf20Sopenharmony_civoid iwl_mvm_probe_resp_data_notif(struct iwl_mvm *mvm,
15018c2ecf20Sopenharmony_ci				   struct iwl_rx_cmd_buffer *rxb)
15028c2ecf20Sopenharmony_ci{
15038c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
15048c2ecf20Sopenharmony_ci	struct iwl_probe_resp_data_notif *notif = (void *)pkt->data;
15058c2ecf20Sopenharmony_ci	struct iwl_probe_resp_data *old_data, *new_data;
15068c2ecf20Sopenharmony_ci	int len = iwl_rx_packet_payload_len(pkt);
15078c2ecf20Sopenharmony_ci	u32 id = le32_to_cpu(notif->mac_id);
15088c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
15098c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif;
15108c2ecf20Sopenharmony_ci
15118c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(len < sizeof(*notif)))
15128c2ecf20Sopenharmony_ci		return;
15138c2ecf20Sopenharmony_ci
15148c2ecf20Sopenharmony_ci	IWL_DEBUG_INFO(mvm, "Probe response data notif: noa %d, csa %d\n",
15158c2ecf20Sopenharmony_ci		       notif->noa_active, notif->csa_counter);
15168c2ecf20Sopenharmony_ci
15178c2ecf20Sopenharmony_ci	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, false);
15188c2ecf20Sopenharmony_ci	if (!vif)
15198c2ecf20Sopenharmony_ci		return;
15208c2ecf20Sopenharmony_ci
15218c2ecf20Sopenharmony_ci	mvmvif = iwl_mvm_vif_from_mac80211(vif);
15228c2ecf20Sopenharmony_ci
15238c2ecf20Sopenharmony_ci	new_data = kzalloc(sizeof(*new_data), GFP_KERNEL);
15248c2ecf20Sopenharmony_ci	if (!new_data)
15258c2ecf20Sopenharmony_ci		return;
15268c2ecf20Sopenharmony_ci
15278c2ecf20Sopenharmony_ci	memcpy(&new_data->notif, notif, sizeof(new_data->notif));
15288c2ecf20Sopenharmony_ci
15298c2ecf20Sopenharmony_ci	/* noa_attr contains 1 reserved byte, need to substruct it */
15308c2ecf20Sopenharmony_ci	new_data->noa_len = sizeof(struct ieee80211_vendor_ie) +
15318c2ecf20Sopenharmony_ci			    sizeof(new_data->notif.noa_attr) - 1;
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_ci	/*
15348c2ecf20Sopenharmony_ci	 * If it's a one time NoA, only one descriptor is needed,
15358c2ecf20Sopenharmony_ci	 * adjust the length according to len_low.
15368c2ecf20Sopenharmony_ci	 */
15378c2ecf20Sopenharmony_ci	if (new_data->notif.noa_attr.len_low ==
15388c2ecf20Sopenharmony_ci	    sizeof(struct ieee80211_p2p_noa_desc) + 2)
15398c2ecf20Sopenharmony_ci		new_data->noa_len -= sizeof(struct ieee80211_p2p_noa_desc);
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	old_data = rcu_dereference_protected(mvmvif->probe_resp_data,
15428c2ecf20Sopenharmony_ci					lockdep_is_held(&mvmvif->mvm->mutex));
15438c2ecf20Sopenharmony_ci	rcu_assign_pointer(mvmvif->probe_resp_data, new_data);
15448c2ecf20Sopenharmony_ci
15458c2ecf20Sopenharmony_ci	if (old_data)
15468c2ecf20Sopenharmony_ci		kfree_rcu(old_data, rcu_head);
15478c2ecf20Sopenharmony_ci
15488c2ecf20Sopenharmony_ci	if (notif->csa_counter != IWL_PROBE_RESP_DATA_NO_CSA &&
15498c2ecf20Sopenharmony_ci	    notif->csa_counter >= 1)
15508c2ecf20Sopenharmony_ci		ieee80211_beacon_set_cntdwn(vif, notif->csa_counter);
15518c2ecf20Sopenharmony_ci}
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_civoid iwl_mvm_channel_switch_noa_notif(struct iwl_mvm *mvm,
15548c2ecf20Sopenharmony_ci				      struct iwl_rx_cmd_buffer *rxb)
15558c2ecf20Sopenharmony_ci{
15568c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
15578c2ecf20Sopenharmony_ci	struct iwl_channel_switch_noa_notif *notif = (void *)pkt->data;
15588c2ecf20Sopenharmony_ci	struct ieee80211_vif *csa_vif, *vif;
15598c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif;
15608c2ecf20Sopenharmony_ci	int len = iwl_rx_packet_payload_len(pkt);
15618c2ecf20Sopenharmony_ci	u32 id_n_color, csa_id, mac_id;
15628c2ecf20Sopenharmony_ci
15638c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(len < sizeof(*notif)))
15648c2ecf20Sopenharmony_ci		return;
15658c2ecf20Sopenharmony_ci
15668c2ecf20Sopenharmony_ci	id_n_color = le32_to_cpu(notif->id_and_color);
15678c2ecf20Sopenharmony_ci	mac_id = id_n_color & FW_CTXT_ID_MSK;
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(mac_id >= NUM_MAC_INDEX_DRIVER))
15708c2ecf20Sopenharmony_ci		return;
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	rcu_read_lock();
15738c2ecf20Sopenharmony_ci	vif = rcu_dereference(mvm->vif_id_to_mac[mac_id]);
15748c2ecf20Sopenharmony_ci	mvmvif = iwl_mvm_vif_from_mac80211(vif);
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci	switch (vif->type) {
15778c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_AP:
15788c2ecf20Sopenharmony_ci		csa_vif = rcu_dereference(mvm->csa_vif);
15798c2ecf20Sopenharmony_ci		if (WARN_ON(!csa_vif || !csa_vif->csa_active ||
15808c2ecf20Sopenharmony_ci			    csa_vif != vif))
15818c2ecf20Sopenharmony_ci			goto out_unlock;
15828c2ecf20Sopenharmony_ci
15838c2ecf20Sopenharmony_ci		csa_id = FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color);
15848c2ecf20Sopenharmony_ci		if (WARN(csa_id != id_n_color,
15858c2ecf20Sopenharmony_ci			 "channel switch noa notification on unexpected vif (csa_vif=%d, notif=%d)",
15868c2ecf20Sopenharmony_ci			 csa_id, id_n_color))
15878c2ecf20Sopenharmony_ci			goto out_unlock;
15888c2ecf20Sopenharmony_ci
15898c2ecf20Sopenharmony_ci		IWL_DEBUG_INFO(mvm, "Channel Switch Started Notification\n");
15908c2ecf20Sopenharmony_ci
15918c2ecf20Sopenharmony_ci		schedule_delayed_work(&mvm->cs_tx_unblock_dwork,
15928c2ecf20Sopenharmony_ci				      msecs_to_jiffies(IWL_MVM_CS_UNBLOCK_TX_TIMEOUT *
15938c2ecf20Sopenharmony_ci						       csa_vif->bss_conf.beacon_int));
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_ci		ieee80211_csa_finish(csa_vif);
15968c2ecf20Sopenharmony_ci
15978c2ecf20Sopenharmony_ci		rcu_read_unlock();
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci		RCU_INIT_POINTER(mvm->csa_vif, NULL);
16008c2ecf20Sopenharmony_ci		return;
16018c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
16028c2ecf20Sopenharmony_ci		iwl_mvm_csa_client_absent(mvm, vif);
16038c2ecf20Sopenharmony_ci		cancel_delayed_work(&mvmvif->csa_work);
16048c2ecf20Sopenharmony_ci		ieee80211_chswitch_done(vif, true);
16058c2ecf20Sopenharmony_ci		break;
16068c2ecf20Sopenharmony_ci	default:
16078c2ecf20Sopenharmony_ci		/* should never happen */
16088c2ecf20Sopenharmony_ci		WARN_ON_ONCE(1);
16098c2ecf20Sopenharmony_ci		break;
16108c2ecf20Sopenharmony_ci	}
16118c2ecf20Sopenharmony_ciout_unlock:
16128c2ecf20Sopenharmony_ci	rcu_read_unlock();
16138c2ecf20Sopenharmony_ci}
16148c2ecf20Sopenharmony_ci
16158c2ecf20Sopenharmony_civoid iwl_mvm_rx_missed_vap_notif(struct iwl_mvm *mvm,
16168c2ecf20Sopenharmony_ci				 struct iwl_rx_cmd_buffer *rxb)
16178c2ecf20Sopenharmony_ci{
16188c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
16198c2ecf20Sopenharmony_ci	struct iwl_missed_vap_notif *mb = (void *)pkt->data;
16208c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
16218c2ecf20Sopenharmony_ci	u32 id = le32_to_cpu(mb->mac_id);
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	IWL_DEBUG_INFO(mvm,
16248c2ecf20Sopenharmony_ci		       "missed_vap notify mac_id=%u, num_beacon_intervals_elapsed=%u, profile_periodicity=%u\n",
16258c2ecf20Sopenharmony_ci		       le32_to_cpu(mb->mac_id),
16268c2ecf20Sopenharmony_ci		       mb->num_beacon_intervals_elapsed,
16278c2ecf20Sopenharmony_ci		       mb->profile_periodicity);
16288c2ecf20Sopenharmony_ci
16298c2ecf20Sopenharmony_ci	rcu_read_lock();
16308c2ecf20Sopenharmony_ci
16318c2ecf20Sopenharmony_ci	vif = iwl_mvm_rcu_dereference_vif_id(mvm, id, true);
16328c2ecf20Sopenharmony_ci	if (vif)
16338c2ecf20Sopenharmony_ci		iwl_mvm_connection_loss(mvm, vif, "missed vap beacon");
16348c2ecf20Sopenharmony_ci
16358c2ecf20Sopenharmony_ci	rcu_read_unlock();
16368c2ecf20Sopenharmony_ci}
1637