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) 2014 Intel Mobile Communications GmbH
98c2ecf20Sopenharmony_ci * Copyright(c) 2017 Intel Deutschland GmbH
108c2ecf20Sopenharmony_ci * Copyright(C) 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) 2014 Intel Mobile Communications GmbH
318c2ecf20Sopenharmony_ci * Copyright(c) 2017 Intel Deutschland GmbH
328c2ecf20Sopenharmony_ci * Copyright(C) 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 "mvm.h"
658c2ecf20Sopenharmony_ci#include "time-event.h"
668c2ecf20Sopenharmony_ci#include "iwl-io.h"
678c2ecf20Sopenharmony_ci#include "iwl-prph.h"
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define TU_TO_US(x) (x * 1024)
708c2ecf20Sopenharmony_ci#define TU_TO_MS(x) (TU_TO_US(x) / 1000)
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_civoid iwl_mvm_teardown_tdls_peers(struct iwl_mvm *mvm)
738c2ecf20Sopenharmony_ci{
748c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
758c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
818c2ecf20Sopenharmony_ci		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
828c2ecf20Sopenharmony_ci						lockdep_is_held(&mvm->mutex));
838c2ecf20Sopenharmony_ci		if (!sta || IS_ERR(sta) || !sta->tdls)
848c2ecf20Sopenharmony_ci			continue;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		mvmsta = iwl_mvm_sta_from_mac80211(sta);
878c2ecf20Sopenharmony_ci		ieee80211_tdls_oper_request(mvmsta->vif, sta->addr,
888c2ecf20Sopenharmony_ci				NL80211_TDLS_TEARDOWN,
898c2ecf20Sopenharmony_ci				WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED,
908c2ecf20Sopenharmony_ci				GFP_KERNEL);
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci}
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ciint iwl_mvm_tdls_sta_count(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
958c2ecf20Sopenharmony_ci{
968c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
978c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
988c2ecf20Sopenharmony_ci	int count = 0;
998c2ecf20Sopenharmony_ci	int i;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
1048c2ecf20Sopenharmony_ci		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1058c2ecf20Sopenharmony_ci						lockdep_is_held(&mvm->mutex));
1068c2ecf20Sopenharmony_ci		if (!sta || IS_ERR(sta) || !sta->tdls)
1078c2ecf20Sopenharmony_ci			continue;
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		if (vif) {
1108c2ecf20Sopenharmony_ci			mvmsta = iwl_mvm_sta_from_mac80211(sta);
1118c2ecf20Sopenharmony_ci			if (mvmsta->vif != vif)
1128c2ecf20Sopenharmony_ci				continue;
1138c2ecf20Sopenharmony_ci		}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		count++;
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	return count;
1198c2ecf20Sopenharmony_ci}
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void iwl_mvm_tdls_config(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt;
1248c2ecf20Sopenharmony_ci	struct iwl_tdls_config_res *resp;
1258c2ecf20Sopenharmony_ci	struct iwl_tdls_config_cmd tdls_cfg_cmd = {};
1268c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
1278c2ecf20Sopenharmony_ci		.id = TDLS_CONFIG_CMD,
1288c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
1298c2ecf20Sopenharmony_ci		.data = { &tdls_cfg_cmd, },
1308c2ecf20Sopenharmony_ci		.len = { sizeof(struct iwl_tdls_config_cmd), },
1318c2ecf20Sopenharmony_ci	};
1328c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
1338c2ecf20Sopenharmony_ci	int ret, i, cnt;
1348c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	tdls_cfg_cmd.id_and_color =
1398c2ecf20Sopenharmony_ci		cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id, mvmvif->color));
1408c2ecf20Sopenharmony_ci	tdls_cfg_cmd.tx_to_ap_tid = IWL_MVM_TDLS_FW_TID;
1418c2ecf20Sopenharmony_ci	tdls_cfg_cmd.tx_to_ap_ssn = cpu_to_le16(0); /* not used for now */
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	/* for now the Tx cmd is empty and unused */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	/* populate TDLS peer data */
1468c2ecf20Sopenharmony_ci	cnt = 0;
1478c2ecf20Sopenharmony_ci	for (i = 0; i < mvm->fw->ucode_capa.num_stations; i++) {
1488c2ecf20Sopenharmony_ci		sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[i],
1498c2ecf20Sopenharmony_ci						lockdep_is_held(&mvm->mutex));
1508c2ecf20Sopenharmony_ci		if (IS_ERR_OR_NULL(sta) || !sta->tdls)
1518c2ecf20Sopenharmony_ci			continue;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		tdls_cfg_cmd.sta_info[cnt].sta_id = i;
1548c2ecf20Sopenharmony_ci		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_tid =
1558c2ecf20Sopenharmony_ci							IWL_MVM_TDLS_FW_TID;
1568c2ecf20Sopenharmony_ci		tdls_cfg_cmd.sta_info[cnt].tx_to_peer_ssn = cpu_to_le16(0);
1578c2ecf20Sopenharmony_ci		tdls_cfg_cmd.sta_info[cnt].is_initiator =
1588c2ecf20Sopenharmony_ci				cpu_to_le32(sta->tdls_initiator ? 1 : 0);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci		cnt++;
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	tdls_cfg_cmd.tdls_peer_count = cnt;
1648c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm, "send TDLS config to FW for %d peers\n", cnt);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &cmd);
1678c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(ret))
1688c2ecf20Sopenharmony_ci		return;
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	pkt = cmd.resp_pkt;
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	WARN_ON_ONCE(iwl_rx_packet_payload_len(pkt) != sizeof(*resp));
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	/* we don't really care about the response at this point */
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_civoid iwl_mvm_recalc_tdls_state(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
1808c2ecf20Sopenharmony_ci			       bool sta_added)
1818c2ecf20Sopenharmony_ci{
1828c2ecf20Sopenharmony_ci	int tdls_sta_cnt = iwl_mvm_tdls_sta_count(mvm, vif);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	/* when the first peer joins, send a power update first */
1858c2ecf20Sopenharmony_ci	if (tdls_sta_cnt == 1 && sta_added)
1868c2ecf20Sopenharmony_ci		iwl_mvm_power_update_mac(mvm);
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ci	/* Configure the FW with TDLS peer info only if TDLS channel switch
1898c2ecf20Sopenharmony_ci	 * capability is set.
1908c2ecf20Sopenharmony_ci	 * TDLS config data is used currently only in TDLS channel switch code.
1918c2ecf20Sopenharmony_ci	 * Supposed to serve also TDLS buffer station which is not implemneted
1928c2ecf20Sopenharmony_ci	 * yet in FW*/
1938c2ecf20Sopenharmony_ci	if (fw_has_capa(&mvm->fw->ucode_capa,
1948c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_CAPA_TDLS_CHANNEL_SWITCH))
1958c2ecf20Sopenharmony_ci		iwl_mvm_tdls_config(mvm, vif);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	/* when the last peer leaves, send a power update last */
1988c2ecf20Sopenharmony_ci	if (tdls_sta_cnt == 0 && !sta_added)
1998c2ecf20Sopenharmony_ci		iwl_mvm_power_update_mac(mvm);
2008c2ecf20Sopenharmony_ci}
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_civoid iwl_mvm_mac_mgd_protect_tdls_discover(struct ieee80211_hw *hw,
2038c2ecf20Sopenharmony_ci					   struct ieee80211_vif *vif)
2048c2ecf20Sopenharmony_ci{
2058c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
2068c2ecf20Sopenharmony_ci	u32 duration = 2 * vif->bss_conf.dtim_period * vif->bss_conf.beacon_int;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* Protect the session to hear the TDLS setup response on the channel */
2098c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
2108c2ecf20Sopenharmony_ci	if (fw_has_capa(&mvm->fw->ucode_capa,
2118c2ecf20Sopenharmony_ci			IWL_UCODE_TLV_CAPA_SESSION_PROT_CMD))
2128c2ecf20Sopenharmony_ci		iwl_mvm_schedule_session_protection(mvm, vif, duration,
2138c2ecf20Sopenharmony_ci						    duration, true);
2148c2ecf20Sopenharmony_ci	else
2158c2ecf20Sopenharmony_ci		iwl_mvm_protect_session(mvm, vif, duration,
2168c2ecf20Sopenharmony_ci					duration, 100, true);
2178c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
2188c2ecf20Sopenharmony_ci}
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_cistatic const char *
2218c2ecf20Sopenharmony_ciiwl_mvm_tdls_cs_state_str(enum iwl_mvm_tdls_cs_state state)
2228c2ecf20Sopenharmony_ci{
2238c2ecf20Sopenharmony_ci	switch (state) {
2248c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_IDLE:
2258c2ecf20Sopenharmony_ci		return "IDLE";
2268c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_REQ_SENT:
2278c2ecf20Sopenharmony_ci		return "REQ SENT";
2288c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_RESP_RCVD:
2298c2ecf20Sopenharmony_ci		return "RESP RECEIVED";
2308c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_REQ_RCVD:
2318c2ecf20Sopenharmony_ci		return "REQ RECEIVED";
2328c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_ACTIVE:
2338c2ecf20Sopenharmony_ci		return "ACTIVE";
2348c2ecf20Sopenharmony_ci	}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	return NULL;
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic void iwl_mvm_tdls_update_cs_state(struct iwl_mvm *mvm,
2408c2ecf20Sopenharmony_ci					 enum iwl_mvm_tdls_cs_state state)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.state == state)
2438c2ecf20Sopenharmony_ci		return;
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm, "TDLS channel switch state: %s -> %s\n",
2468c2ecf20Sopenharmony_ci		       iwl_mvm_tdls_cs_state_str(mvm->tdls_cs.state),
2478c2ecf20Sopenharmony_ci		       iwl_mvm_tdls_cs_state_str(state));
2488c2ecf20Sopenharmony_ci	mvm->tdls_cs.state = state;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* we only send requests to our switching peer - update sent time */
2518c2ecf20Sopenharmony_ci	if (state == IWL_MVM_TDLS_SW_REQ_SENT)
2528c2ecf20Sopenharmony_ci		mvm->tdls_cs.peer.sent_timestamp = iwl_mvm_get_systime(mvm);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	if (state == IWL_MVM_TDLS_SW_IDLE)
2558c2ecf20Sopenharmony_ci		mvm->tdls_cs.cur_sta_id = IWL_MVM_INVALID_STA;
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_civoid iwl_mvm_rx_tdls_notif(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2618c2ecf20Sopenharmony_ci	struct iwl_tdls_channel_switch_notif *notif = (void *)pkt->data;
2628c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
2638c2ecf20Sopenharmony_ci	unsigned int delay;
2648c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
2658c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
2668c2ecf20Sopenharmony_ci	u32 sta_id = le32_to_cpu(notif->sta_id);
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci	/* can fail sometimes */
2718c2ecf20Sopenharmony_ci	if (!le32_to_cpu(notif->status)) {
2728c2ecf20Sopenharmony_ci		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
2738c2ecf20Sopenharmony_ci		return;
2748c2ecf20Sopenharmony_ci	}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	if (WARN_ON(sta_id >= mvm->fw->ucode_capa.num_stations))
2778c2ecf20Sopenharmony_ci		return;
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
2808c2ecf20Sopenharmony_ci					lockdep_is_held(&mvm->mutex));
2818c2ecf20Sopenharmony_ci	/* the station may not be here, but if it is, it must be a TDLS peer */
2828c2ecf20Sopenharmony_ci	if (IS_ERR_OR_NULL(sta) || WARN_ON(!sta->tdls))
2838c2ecf20Sopenharmony_ci		return;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	mvmsta = iwl_mvm_sta_from_mac80211(sta);
2868c2ecf20Sopenharmony_ci	vif = mvmsta->vif;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/*
2898c2ecf20Sopenharmony_ci	 * Update state and possibly switch again after this is over (DTIM).
2908c2ecf20Sopenharmony_ci	 * Also convert TU to msec.
2918c2ecf20Sopenharmony_ci	 */
2928c2ecf20Sopenharmony_ci	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
2938c2ecf20Sopenharmony_ci	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
2948c2ecf20Sopenharmony_ci			 msecs_to_jiffies(delay));
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_ACTIVE);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic int
3008c2ecf20Sopenharmony_ciiwl_mvm_tdls_check_action(struct iwl_mvm *mvm,
3018c2ecf20Sopenharmony_ci			  enum iwl_tdls_channel_switch_type type,
3028c2ecf20Sopenharmony_ci			  const u8 *peer, bool peer_initiator, u32 timestamp)
3038c2ecf20Sopenharmony_ci{
3048c2ecf20Sopenharmony_ci	bool same_peer = false;
3058c2ecf20Sopenharmony_ci	int ret = 0;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	/* get the existing peer if it's there */
3088c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE &&
3098c2ecf20Sopenharmony_ci	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
3108c2ecf20Sopenharmony_ci		struct ieee80211_sta *sta = rcu_dereference_protected(
3118c2ecf20Sopenharmony_ci				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
3128c2ecf20Sopenharmony_ci				lockdep_is_held(&mvm->mutex));
3138c2ecf20Sopenharmony_ci		if (!IS_ERR_OR_NULL(sta))
3148c2ecf20Sopenharmony_ci			same_peer = ether_addr_equal(peer, sta->addr);
3158c2ecf20Sopenharmony_ci	}
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	switch (mvm->tdls_cs.state) {
3188c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_IDLE:
3198c2ecf20Sopenharmony_ci		/*
3208c2ecf20Sopenharmony_ci		 * might be spurious packet from the peer after the switch is
3218c2ecf20Sopenharmony_ci		 * already done
3228c2ecf20Sopenharmony_ci		 */
3238c2ecf20Sopenharmony_ci		if (type == TDLS_MOVE_CH)
3248c2ecf20Sopenharmony_ci			ret = -EINVAL;
3258c2ecf20Sopenharmony_ci		break;
3268c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_REQ_SENT:
3278c2ecf20Sopenharmony_ci		/* only allow requests from the same peer */
3288c2ecf20Sopenharmony_ci		if (!same_peer)
3298c2ecf20Sopenharmony_ci			ret = -EBUSY;
3308c2ecf20Sopenharmony_ci		else if (type == TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH &&
3318c2ecf20Sopenharmony_ci			 !peer_initiator)
3328c2ecf20Sopenharmony_ci			/*
3338c2ecf20Sopenharmony_ci			 * We received a ch-switch request while an outgoing
3348c2ecf20Sopenharmony_ci			 * one is pending. Allow it if the peer is the link
3358c2ecf20Sopenharmony_ci			 * initiator.
3368c2ecf20Sopenharmony_ci			 */
3378c2ecf20Sopenharmony_ci			ret = -EBUSY;
3388c2ecf20Sopenharmony_ci		else if (type == TDLS_SEND_CHAN_SW_REQ)
3398c2ecf20Sopenharmony_ci			/* wait for idle before sending another request */
3408c2ecf20Sopenharmony_ci			ret = -EBUSY;
3418c2ecf20Sopenharmony_ci		else if (timestamp <= mvm->tdls_cs.peer.sent_timestamp)
3428c2ecf20Sopenharmony_ci			/* we got a stale response - ignore it */
3438c2ecf20Sopenharmony_ci			ret = -EINVAL;
3448c2ecf20Sopenharmony_ci		break;
3458c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_RESP_RCVD:
3468c2ecf20Sopenharmony_ci		/*
3478c2ecf20Sopenharmony_ci		 * we are waiting for the FW to give an "active" notification,
3488c2ecf20Sopenharmony_ci		 * so ignore requests in the meantime
3498c2ecf20Sopenharmony_ci		 */
3508c2ecf20Sopenharmony_ci		ret = -EBUSY;
3518c2ecf20Sopenharmony_ci		break;
3528c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_REQ_RCVD:
3538c2ecf20Sopenharmony_ci		/* as above, allow the link initiator to proceed */
3548c2ecf20Sopenharmony_ci		if (type == TDLS_SEND_CHAN_SW_REQ) {
3558c2ecf20Sopenharmony_ci			if (!same_peer)
3568c2ecf20Sopenharmony_ci				ret = -EBUSY;
3578c2ecf20Sopenharmony_ci			else if (peer_initiator) /* they are the initiator */
3588c2ecf20Sopenharmony_ci				ret = -EBUSY;
3598c2ecf20Sopenharmony_ci		} else if (type == TDLS_MOVE_CH) {
3608c2ecf20Sopenharmony_ci			ret = -EINVAL;
3618c2ecf20Sopenharmony_ci		}
3628c2ecf20Sopenharmony_ci		break;
3638c2ecf20Sopenharmony_ci	case IWL_MVM_TDLS_SW_ACTIVE:
3648c2ecf20Sopenharmony_ci		/*
3658c2ecf20Sopenharmony_ci		 * the only valid request when active is a request to return
3668c2ecf20Sopenharmony_ci		 * to the base channel by the current off-channel peer
3678c2ecf20Sopenharmony_ci		 */
3688c2ecf20Sopenharmony_ci		if (type != TDLS_MOVE_CH || !same_peer)
3698c2ecf20Sopenharmony_ci			ret = -EBUSY;
3708c2ecf20Sopenharmony_ci		break;
3718c2ecf20Sopenharmony_ci	}
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	if (ret)
3748c2ecf20Sopenharmony_ci		IWL_DEBUG_TDLS(mvm,
3758c2ecf20Sopenharmony_ci			       "Invalid TDLS action %d state %d peer %pM same_peer %d initiator %d\n",
3768c2ecf20Sopenharmony_ci			       type, mvm->tdls_cs.state, peer, same_peer,
3778c2ecf20Sopenharmony_ci			       peer_initiator);
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	return ret;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic int
3838c2ecf20Sopenharmony_ciiwl_mvm_tdls_config_channel_switch(struct iwl_mvm *mvm,
3848c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif,
3858c2ecf20Sopenharmony_ci				   enum iwl_tdls_channel_switch_type type,
3868c2ecf20Sopenharmony_ci				   const u8 *peer, bool peer_initiator,
3878c2ecf20Sopenharmony_ci				   u8 oper_class,
3888c2ecf20Sopenharmony_ci				   struct cfg80211_chan_def *chandef,
3898c2ecf20Sopenharmony_ci				   u32 timestamp, u16 switch_time,
3908c2ecf20Sopenharmony_ci				   u16 switch_timeout, struct sk_buff *skb,
3918c2ecf20Sopenharmony_ci				   u32 ch_sw_tm_ie)
3928c2ecf20Sopenharmony_ci{
3938c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
3948c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
3958c2ecf20Sopenharmony_ci	struct ieee80211_tx_info *info;
3968c2ecf20Sopenharmony_ci	struct ieee80211_hdr *hdr;
3978c2ecf20Sopenharmony_ci	struct iwl_tdls_channel_switch_cmd cmd = {0};
3988c2ecf20Sopenharmony_ci	struct iwl_tdls_channel_switch_cmd_tail *tail =
3998c2ecf20Sopenharmony_ci		iwl_mvm_chan_info_cmd_tail(mvm, &cmd.ci);
4008c2ecf20Sopenharmony_ci	u16 len = sizeof(cmd) - iwl_mvm_chan_info_padding(mvm);
4018c2ecf20Sopenharmony_ci	int ret;
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	ret = iwl_mvm_tdls_check_action(mvm, type, peer, peer_initiator,
4068c2ecf20Sopenharmony_ci					timestamp);
4078c2ecf20Sopenharmony_ci	if (ret)
4088c2ecf20Sopenharmony_ci		return ret;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	if (!skb || WARN_ON(skb->len > IWL_TDLS_CH_SW_FRAME_MAX_SIZE)) {
4118c2ecf20Sopenharmony_ci		ret = -EINVAL;
4128c2ecf20Sopenharmony_ci		goto out;
4138c2ecf20Sopenharmony_ci	}
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	cmd.switch_type = type;
4168c2ecf20Sopenharmony_ci	tail->timing.frame_timestamp = cpu_to_le32(timestamp);
4178c2ecf20Sopenharmony_ci	tail->timing.switch_time = cpu_to_le32(switch_time);
4188c2ecf20Sopenharmony_ci	tail->timing.switch_timeout = cpu_to_le32(switch_timeout);
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	rcu_read_lock();
4218c2ecf20Sopenharmony_ci	sta = ieee80211_find_sta(vif, peer);
4228c2ecf20Sopenharmony_ci	if (!sta) {
4238c2ecf20Sopenharmony_ci		rcu_read_unlock();
4248c2ecf20Sopenharmony_ci		ret = -ENOENT;
4258c2ecf20Sopenharmony_ci		goto out;
4268c2ecf20Sopenharmony_ci	}
4278c2ecf20Sopenharmony_ci	mvmsta = iwl_mvm_sta_from_mac80211(sta);
4288c2ecf20Sopenharmony_ci	cmd.peer_sta_id = cpu_to_le32(mvmsta->sta_id);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	if (!chandef) {
4318c2ecf20Sopenharmony_ci		if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
4328c2ecf20Sopenharmony_ci		    mvm->tdls_cs.peer.chandef.chan) {
4338c2ecf20Sopenharmony_ci			/* actually moving to the channel */
4348c2ecf20Sopenharmony_ci			chandef = &mvm->tdls_cs.peer.chandef;
4358c2ecf20Sopenharmony_ci		} else if (mvm->tdls_cs.state == IWL_MVM_TDLS_SW_ACTIVE &&
4368c2ecf20Sopenharmony_ci			   type == TDLS_MOVE_CH) {
4378c2ecf20Sopenharmony_ci			/* we need to return to base channel */
4388c2ecf20Sopenharmony_ci			struct ieee80211_chanctx_conf *chanctx =
4398c2ecf20Sopenharmony_ci					rcu_dereference(vif->chanctx_conf);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci			if (WARN_ON_ONCE(!chanctx)) {
4428c2ecf20Sopenharmony_ci				rcu_read_unlock();
4438c2ecf20Sopenharmony_ci				goto out;
4448c2ecf20Sopenharmony_ci			}
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci			chandef = &chanctx->def;
4478c2ecf20Sopenharmony_ci		}
4488c2ecf20Sopenharmony_ci	}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	if (chandef)
4518c2ecf20Sopenharmony_ci		iwl_mvm_set_chan_info_chandef(mvm, &cmd.ci, chandef);
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	/* keep quota calculation simple for now - 50% of DTIM for TDLS */
4548c2ecf20Sopenharmony_ci	tail->timing.max_offchan_duration =
4558c2ecf20Sopenharmony_ci			cpu_to_le32(TU_TO_US(vif->bss_conf.dtim_period *
4568c2ecf20Sopenharmony_ci					     vif->bss_conf.beacon_int) / 2);
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	/* Switch time is the first element in the switch-timing IE. */
4598c2ecf20Sopenharmony_ci	tail->frame.switch_time_offset = cpu_to_le32(ch_sw_tm_ie + 2);
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_ci	info = IEEE80211_SKB_CB(skb);
4628c2ecf20Sopenharmony_ci	hdr = (void *)skb->data;
4638c2ecf20Sopenharmony_ci	if (info->control.hw_key) {
4648c2ecf20Sopenharmony_ci		if (info->control.hw_key->cipher != WLAN_CIPHER_SUITE_CCMP) {
4658c2ecf20Sopenharmony_ci			rcu_read_unlock();
4668c2ecf20Sopenharmony_ci			ret = -EINVAL;
4678c2ecf20Sopenharmony_ci			goto out;
4688c2ecf20Sopenharmony_ci		}
4698c2ecf20Sopenharmony_ci		iwl_mvm_set_tx_cmd_ccmp(info, &tail->frame.tx_cmd);
4708c2ecf20Sopenharmony_ci	}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci	iwl_mvm_set_tx_cmd(mvm, skb, &tail->frame.tx_cmd, info,
4738c2ecf20Sopenharmony_ci			   mvmsta->sta_id);
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	iwl_mvm_set_tx_cmd_rate(mvm, &tail->frame.tx_cmd, info, sta,
4768c2ecf20Sopenharmony_ci				hdr->frame_control);
4778c2ecf20Sopenharmony_ci	rcu_read_unlock();
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	memcpy(tail->frame.data, skb->data, skb->len);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, TDLS_CHANNEL_SWITCH_CMD, 0, len, &cmd);
4828c2ecf20Sopenharmony_ci	if (ret) {
4838c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to send TDLS_CHANNEL_SWITCH cmd: %d\n",
4848c2ecf20Sopenharmony_ci			ret);
4858c2ecf20Sopenharmony_ci		goto out;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	/* channel switch has started, update state */
4898c2ecf20Sopenharmony_ci	if (type != TDLS_MOVE_CH) {
4908c2ecf20Sopenharmony_ci		mvm->tdls_cs.cur_sta_id = mvmsta->sta_id;
4918c2ecf20Sopenharmony_ci		iwl_mvm_tdls_update_cs_state(mvm,
4928c2ecf20Sopenharmony_ci					     type == TDLS_SEND_CHAN_SW_REQ ?
4938c2ecf20Sopenharmony_ci					     IWL_MVM_TDLS_SW_REQ_SENT :
4948c2ecf20Sopenharmony_ci					     IWL_MVM_TDLS_SW_REQ_RCVD);
4958c2ecf20Sopenharmony_ci	} else {
4968c2ecf20Sopenharmony_ci		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_RESP_RCVD);
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ciout:
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	/* channel switch failed - we are idle */
5028c2ecf20Sopenharmony_ci	if (ret)
5038c2ecf20Sopenharmony_ci		iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	return ret;
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_civoid iwl_mvm_tdls_ch_switch_work(struct work_struct *work)
5098c2ecf20Sopenharmony_ci{
5108c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
5118c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta;
5128c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
5138c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
5148c2ecf20Sopenharmony_ci	unsigned int delay;
5158c2ecf20Sopenharmony_ci	int ret;
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	mvm = container_of(work, struct iwl_mvm, tdls_cs.dwork.work);
5188c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/* called after an active channel switch has finished or timed-out */
5218c2ecf20Sopenharmony_ci	iwl_mvm_tdls_update_cs_state(mvm, IWL_MVM_TDLS_SW_IDLE);
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* station might be gone, in that case do nothing */
5248c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA)
5258c2ecf20Sopenharmony_ci		goto out;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	sta = rcu_dereference_protected(
5288c2ecf20Sopenharmony_ci				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
5298c2ecf20Sopenharmony_ci				lockdep_is_held(&mvm->mutex));
5308c2ecf20Sopenharmony_ci	/* the station may not be here, but if it is, it must be a TDLS peer */
5318c2ecf20Sopenharmony_ci	if (!sta || IS_ERR(sta) || WARN_ON(!sta->tdls))
5328c2ecf20Sopenharmony_ci		goto out;
5338c2ecf20Sopenharmony_ci
5348c2ecf20Sopenharmony_ci	mvmsta = iwl_mvm_sta_from_mac80211(sta);
5358c2ecf20Sopenharmony_ci	vif = mvmsta->vif;
5368c2ecf20Sopenharmony_ci	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
5378c2ecf20Sopenharmony_ci						 TDLS_SEND_CHAN_SW_REQ,
5388c2ecf20Sopenharmony_ci						 sta->addr,
5398c2ecf20Sopenharmony_ci						 mvm->tdls_cs.peer.initiator,
5408c2ecf20Sopenharmony_ci						 mvm->tdls_cs.peer.op_class,
5418c2ecf20Sopenharmony_ci						 &mvm->tdls_cs.peer.chandef,
5428c2ecf20Sopenharmony_ci						 0, 0, 0,
5438c2ecf20Sopenharmony_ci						 mvm->tdls_cs.peer.skb,
5448c2ecf20Sopenharmony_ci						 mvm->tdls_cs.peer.ch_sw_tm_ie);
5458c2ecf20Sopenharmony_ci	if (ret)
5468c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Not sending TDLS channel switch: %d\n", ret);
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	/* retry after a DTIM if we failed sending now */
5498c2ecf20Sopenharmony_ci	delay = TU_TO_MS(vif->bss_conf.dtim_period * vif->bss_conf.beacon_int);
5508c2ecf20Sopenharmony_ci	schedule_delayed_work(&mvm->tdls_cs.dwork, msecs_to_jiffies(delay));
5518c2ecf20Sopenharmony_ciout:
5528c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
5538c2ecf20Sopenharmony_ci}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ciint
5568c2ecf20Sopenharmony_ciiwl_mvm_tdls_channel_switch(struct ieee80211_hw *hw,
5578c2ecf20Sopenharmony_ci			    struct ieee80211_vif *vif,
5588c2ecf20Sopenharmony_ci			    struct ieee80211_sta *sta, u8 oper_class,
5598c2ecf20Sopenharmony_ci			    struct cfg80211_chan_def *chandef,
5608c2ecf20Sopenharmony_ci			    struct sk_buff *tmpl_skb, u32 ch_sw_tm_ie)
5618c2ecf20Sopenharmony_ci{
5628c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
5638c2ecf20Sopenharmony_ci	struct iwl_mvm_sta *mvmsta;
5648c2ecf20Sopenharmony_ci	unsigned int delay;
5658c2ecf20Sopenharmony_ci	int ret;
5668c2ecf20Sopenharmony_ci
5678c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm, "TDLS channel switch with %pM ch %d width %d\n",
5708c2ecf20Sopenharmony_ci		       sta->addr, chandef->chan->center_freq, chandef->width);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	/* we only support a single peer for channel switching */
5738c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.peer.sta_id != IWL_MVM_INVALID_STA) {
5748c2ecf20Sopenharmony_ci		IWL_DEBUG_TDLS(mvm,
5758c2ecf20Sopenharmony_ci			       "Existing peer. Can't start switch with %pM\n",
5768c2ecf20Sopenharmony_ci			       sta->addr);
5778c2ecf20Sopenharmony_ci		ret = -EBUSY;
5788c2ecf20Sopenharmony_ci		goto out;
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	ret = iwl_mvm_tdls_config_channel_switch(mvm, vif,
5828c2ecf20Sopenharmony_ci						 TDLS_SEND_CHAN_SW_REQ,
5838c2ecf20Sopenharmony_ci						 sta->addr, sta->tdls_initiator,
5848c2ecf20Sopenharmony_ci						 oper_class, chandef, 0, 0, 0,
5858c2ecf20Sopenharmony_ci						 tmpl_skb, ch_sw_tm_ie);
5868c2ecf20Sopenharmony_ci	if (ret)
5878c2ecf20Sopenharmony_ci		goto out;
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ci	/*
5908c2ecf20Sopenharmony_ci	 * Mark the peer as "in tdls switch" for this vif. We only allow a
5918c2ecf20Sopenharmony_ci	 * single such peer per vif.
5928c2ecf20Sopenharmony_ci	 */
5938c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.skb = skb_copy(tmpl_skb, GFP_KERNEL);
5948c2ecf20Sopenharmony_ci	if (!mvm->tdls_cs.peer.skb) {
5958c2ecf20Sopenharmony_ci		ret = -ENOMEM;
5968c2ecf20Sopenharmony_ci		goto out;
5978c2ecf20Sopenharmony_ci	}
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	mvmsta = iwl_mvm_sta_from_mac80211(sta);
6008c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.sta_id = mvmsta->sta_id;
6018c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.chandef = *chandef;
6028c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.initiator = sta->tdls_initiator;
6038c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.op_class = oper_class;
6048c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.ch_sw_tm_ie = ch_sw_tm_ie;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	/*
6078c2ecf20Sopenharmony_ci	 * Wait for 2 DTIM periods before attempting the next switch. The next
6088c2ecf20Sopenharmony_ci	 * switch will be made sooner if the current one completes before that.
6098c2ecf20Sopenharmony_ci	 */
6108c2ecf20Sopenharmony_ci	delay = 2 * TU_TO_MS(vif->bss_conf.dtim_period *
6118c2ecf20Sopenharmony_ci			     vif->bss_conf.beacon_int);
6128c2ecf20Sopenharmony_ci	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
6138c2ecf20Sopenharmony_ci			 msecs_to_jiffies(delay));
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ciout:
6168c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
6178c2ecf20Sopenharmony_ci	return ret;
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_civoid iwl_mvm_tdls_cancel_channel_switch(struct ieee80211_hw *hw,
6218c2ecf20Sopenharmony_ci					struct ieee80211_vif *vif,
6228c2ecf20Sopenharmony_ci					struct ieee80211_sta *sta)
6238c2ecf20Sopenharmony_ci{
6248c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6258c2ecf20Sopenharmony_ci	struct ieee80211_sta *cur_sta;
6268c2ecf20Sopenharmony_ci	bool wait_for_phy = false;
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm, "TDLS cancel channel switch with %pM\n", sta->addr);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	/* we only support a single peer for channel switching */
6338c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.peer.sta_id == IWL_MVM_INVALID_STA) {
6348c2ecf20Sopenharmony_ci		IWL_DEBUG_TDLS(mvm, "No ch switch peer - %pM\n", sta->addr);
6358c2ecf20Sopenharmony_ci		goto out;
6368c2ecf20Sopenharmony_ci	}
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	cur_sta = rcu_dereference_protected(
6398c2ecf20Sopenharmony_ci				mvm->fw_id_to_mac_id[mvm->tdls_cs.peer.sta_id],
6408c2ecf20Sopenharmony_ci				lockdep_is_held(&mvm->mutex));
6418c2ecf20Sopenharmony_ci	/* make sure it's the same peer */
6428c2ecf20Sopenharmony_ci	if (cur_sta != sta)
6438c2ecf20Sopenharmony_ci		goto out;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	/*
6468c2ecf20Sopenharmony_ci	 * If we're currently in a switch because of the now canceled peer,
6478c2ecf20Sopenharmony_ci	 * wait a DTIM here to make sure the phy is back on the base channel.
6488c2ecf20Sopenharmony_ci	 * We can't otherwise force it.
6498c2ecf20Sopenharmony_ci	 */
6508c2ecf20Sopenharmony_ci	if (mvm->tdls_cs.cur_sta_id == mvm->tdls_cs.peer.sta_id &&
6518c2ecf20Sopenharmony_ci	    mvm->tdls_cs.state != IWL_MVM_TDLS_SW_IDLE)
6528c2ecf20Sopenharmony_ci		wait_for_phy = true;
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.sta_id = IWL_MVM_INVALID_STA;
6558c2ecf20Sopenharmony_ci	dev_kfree_skb(mvm->tdls_cs.peer.skb);
6568c2ecf20Sopenharmony_ci	mvm->tdls_cs.peer.skb = NULL;
6578c2ecf20Sopenharmony_ci
6588c2ecf20Sopenharmony_ciout:
6598c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	/* make sure the phy is on the base channel */
6628c2ecf20Sopenharmony_ci	if (wait_for_phy)
6638c2ecf20Sopenharmony_ci		msleep(TU_TO_MS(vif->bss_conf.dtim_period *
6648c2ecf20Sopenharmony_ci				vif->bss_conf.beacon_int));
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_ci	/* flush the channel switch state */
6678c2ecf20Sopenharmony_ci	flush_delayed_work(&mvm->tdls_cs.dwork);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm, "TDLS ending channel switch with %pM\n", sta->addr);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_civoid
6738c2ecf20Sopenharmony_ciiwl_mvm_tdls_recv_channel_switch(struct ieee80211_hw *hw,
6748c2ecf20Sopenharmony_ci				 struct ieee80211_vif *vif,
6758c2ecf20Sopenharmony_ci				 struct ieee80211_tdls_ch_sw_params *params)
6768c2ecf20Sopenharmony_ci{
6778c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = IWL_MAC80211_GET_MVM(hw);
6788c2ecf20Sopenharmony_ci	enum iwl_tdls_channel_switch_type type;
6798c2ecf20Sopenharmony_ci	unsigned int delay;
6808c2ecf20Sopenharmony_ci	const char *action_str =
6818c2ecf20Sopenharmony_ci		params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST ?
6828c2ecf20Sopenharmony_ci		"REQ" : "RESP";
6838c2ecf20Sopenharmony_ci
6848c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
6858c2ecf20Sopenharmony_ci
6868c2ecf20Sopenharmony_ci	IWL_DEBUG_TDLS(mvm,
6878c2ecf20Sopenharmony_ci		       "Received TDLS ch switch action %s from %pM status %d\n",
6888c2ecf20Sopenharmony_ci		       action_str, params->sta->addr, params->status);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	/*
6918c2ecf20Sopenharmony_ci	 * we got a non-zero status from a peer we were switching to - move to
6928c2ecf20Sopenharmony_ci	 * the idle state and retry again later
6938c2ecf20Sopenharmony_ci	 */
6948c2ecf20Sopenharmony_ci	if (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_RESPONSE &&
6958c2ecf20Sopenharmony_ci	    params->status != 0 &&
6968c2ecf20Sopenharmony_ci	    mvm->tdls_cs.state == IWL_MVM_TDLS_SW_REQ_SENT &&
6978c2ecf20Sopenharmony_ci	    mvm->tdls_cs.cur_sta_id != IWL_MVM_INVALID_STA) {
6988c2ecf20Sopenharmony_ci		struct ieee80211_sta *cur_sta;
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci		/* make sure it's the same peer */
7018c2ecf20Sopenharmony_ci		cur_sta = rcu_dereference_protected(
7028c2ecf20Sopenharmony_ci				mvm->fw_id_to_mac_id[mvm->tdls_cs.cur_sta_id],
7038c2ecf20Sopenharmony_ci				lockdep_is_held(&mvm->mutex));
7048c2ecf20Sopenharmony_ci		if (cur_sta == params->sta) {
7058c2ecf20Sopenharmony_ci			iwl_mvm_tdls_update_cs_state(mvm,
7068c2ecf20Sopenharmony_ci						     IWL_MVM_TDLS_SW_IDLE);
7078c2ecf20Sopenharmony_ci			goto retry;
7088c2ecf20Sopenharmony_ci		}
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci	type = (params->action_code == WLAN_TDLS_CHANNEL_SWITCH_REQUEST) ?
7128c2ecf20Sopenharmony_ci	       TDLS_SEND_CHAN_SW_RESP_AND_MOVE_CH : TDLS_MOVE_CH;
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	iwl_mvm_tdls_config_channel_switch(mvm, vif, type, params->sta->addr,
7158c2ecf20Sopenharmony_ci					   params->sta->tdls_initiator, 0,
7168c2ecf20Sopenharmony_ci					   params->chandef, params->timestamp,
7178c2ecf20Sopenharmony_ci					   params->switch_time,
7188c2ecf20Sopenharmony_ci					   params->switch_timeout,
7198c2ecf20Sopenharmony_ci					   params->tmpl_skb,
7208c2ecf20Sopenharmony_ci					   params->ch_sw_tm_ie);
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ciretry:
7238c2ecf20Sopenharmony_ci	/* register a timeout in case we don't succeed in switching */
7248c2ecf20Sopenharmony_ci	delay = vif->bss_conf.dtim_period * vif->bss_conf.beacon_int *
7258c2ecf20Sopenharmony_ci		1024 / 1000;
7268c2ecf20Sopenharmony_ci	mod_delayed_work(system_wq, &mvm->tdls_cs.dwork,
7278c2ecf20Sopenharmony_ci			 msecs_to_jiffies(delay));
7288c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
7298c2ecf20Sopenharmony_ci}
730