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) 2012 - 2014, 2018 - 2020 Intel Corporation. All rights reserved.
98c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
108c2ecf20Sopenharmony_ci * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
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) 2012 - 2014, 2018 - 2020 Intel Corporation. All rights reserved.
318c2ecf20Sopenharmony_ci * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
328c2ecf20Sopenharmony_ci * Copyright (C) 2015 - 2017 Intel Deutschland GmbH
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#include <net/mac80211.h>
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#include "iwl-debug.h"
658c2ecf20Sopenharmony_ci#include "iwl-io.h"
668c2ecf20Sopenharmony_ci#include "iwl-prph.h"
678c2ecf20Sopenharmony_ci#include "iwl-csr.h"
688c2ecf20Sopenharmony_ci#include "mvm.h"
698c2ecf20Sopenharmony_ci#include "fw/api/rs.h"
708c2ecf20Sopenharmony_ci#include "fw/img.h"
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/*
738c2ecf20Sopenharmony_ci * Will return 0 even if the cmd failed when RFKILL is asserted unless
748c2ecf20Sopenharmony_ci * CMD_WANT_SKB is set in cmd->flags.
758c2ecf20Sopenharmony_ci */
768c2ecf20Sopenharmony_ciint iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	int ret;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
818c2ecf20Sopenharmony_ci	if (WARN_ON(mvm->d3_test_active))
828c2ecf20Sopenharmony_ci		return -EIO;
838c2ecf20Sopenharmony_ci#endif
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	/*
868c2ecf20Sopenharmony_ci	 * Synchronous commands from this op-mode must hold
878c2ecf20Sopenharmony_ci	 * the mutex, this ensures we don't try to send two
888c2ecf20Sopenharmony_ci	 * (or more) synchronous commands at a time.
898c2ecf20Sopenharmony_ci	 */
908c2ecf20Sopenharmony_ci	if (!(cmd->flags & CMD_ASYNC))
918c2ecf20Sopenharmony_ci		lockdep_assert_held(&mvm->mutex);
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	ret = iwl_trans_send_cmd(mvm->trans, cmd);
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	/*
968c2ecf20Sopenharmony_ci	 * If the caller wants the SKB, then don't hide any problems, the
978c2ecf20Sopenharmony_ci	 * caller might access the response buffer which will be NULL if
988c2ecf20Sopenharmony_ci	 * the command failed.
998c2ecf20Sopenharmony_ci	 */
1008c2ecf20Sopenharmony_ci	if (cmd->flags & CMD_WANT_SKB)
1018c2ecf20Sopenharmony_ci		return ret;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	/* Silently ignore failures if RFKILL is asserted */
1048c2ecf20Sopenharmony_ci	if (!ret || ret == -ERFKILL)
1058c2ecf20Sopenharmony_ci		return 0;
1068c2ecf20Sopenharmony_ci	return ret;
1078c2ecf20Sopenharmony_ci}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ciint iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
1108c2ecf20Sopenharmony_ci			 u32 flags, u16 len, const void *data)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
1138c2ecf20Sopenharmony_ci		.id = id,
1148c2ecf20Sopenharmony_ci		.len = { len, },
1158c2ecf20Sopenharmony_ci		.data = { data, },
1168c2ecf20Sopenharmony_ci		.flags = flags,
1178c2ecf20Sopenharmony_ci	};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	return iwl_mvm_send_cmd(mvm, &cmd);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci/*
1238c2ecf20Sopenharmony_ci * We assume that the caller set the status to the success value
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ciint iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
1268c2ecf20Sopenharmony_ci			    u32 *status)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt;
1298c2ecf20Sopenharmony_ci	struct iwl_cmd_response *resp;
1308c2ecf20Sopenharmony_ci	int ret, resp_len;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
1358c2ecf20Sopenharmony_ci	if (WARN_ON(mvm->d3_test_active))
1368c2ecf20Sopenharmony_ci		return -EIO;
1378c2ecf20Sopenharmony_ci#endif
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/*
1408c2ecf20Sopenharmony_ci	 * Only synchronous commands can wait for status,
1418c2ecf20Sopenharmony_ci	 * we use WANT_SKB so the caller can't.
1428c2ecf20Sopenharmony_ci	 */
1438c2ecf20Sopenharmony_ci	if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
1448c2ecf20Sopenharmony_ci		      "cmd flags %x", cmd->flags))
1458c2ecf20Sopenharmony_ci		return -EINVAL;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	cmd->flags |= CMD_WANT_SKB;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	ret = iwl_trans_send_cmd(mvm->trans, cmd);
1508c2ecf20Sopenharmony_ci	if (ret == -ERFKILL) {
1518c2ecf20Sopenharmony_ci		/*
1528c2ecf20Sopenharmony_ci		 * The command failed because of RFKILL, don't update
1538c2ecf20Sopenharmony_ci		 * the status, leave it as success and return 0.
1548c2ecf20Sopenharmony_ci		 */
1558c2ecf20Sopenharmony_ci		return 0;
1568c2ecf20Sopenharmony_ci	} else if (ret) {
1578c2ecf20Sopenharmony_ci		return ret;
1588c2ecf20Sopenharmony_ci	}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	pkt = cmd->resp_pkt;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	resp_len = iwl_rx_packet_payload_len(pkt);
1638c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
1648c2ecf20Sopenharmony_ci		ret = -EIO;
1658c2ecf20Sopenharmony_ci		goto out_free_resp;
1668c2ecf20Sopenharmony_ci	}
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	resp = (void *)pkt->data;
1698c2ecf20Sopenharmony_ci	*status = le32_to_cpu(resp->status);
1708c2ecf20Sopenharmony_ci out_free_resp:
1718c2ecf20Sopenharmony_ci	iwl_free_resp(cmd);
1728c2ecf20Sopenharmony_ci	return ret;
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci/*
1768c2ecf20Sopenharmony_ci * We assume that the caller set the status to the sucess value
1778c2ecf20Sopenharmony_ci */
1788c2ecf20Sopenharmony_ciint iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
1798c2ecf20Sopenharmony_ci				const void *data, u32 *status)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
1828c2ecf20Sopenharmony_ci		.id = id,
1838c2ecf20Sopenharmony_ci		.len = { len, },
1848c2ecf20Sopenharmony_ci		.data = { data, },
1858c2ecf20Sopenharmony_ci	};
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	return iwl_mvm_send_cmd_status(mvm, &cmd, status);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci#define IWL_DECLARE_RATE_INFO(r) \
1918c2ecf20Sopenharmony_ci	[IWL_RATE_##r##M_INDEX] = IWL_RATE_##r##M_PLCP
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/*
1948c2ecf20Sopenharmony_ci * Translate from fw_rate_index (IWL_RATE_XXM_INDEX) to PLCP
1958c2ecf20Sopenharmony_ci */
1968c2ecf20Sopenharmony_cistatic const u8 fw_rate_idx_to_plcp[IWL_RATE_COUNT] = {
1978c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(1),
1988c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(2),
1998c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(5),
2008c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(11),
2018c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(6),
2028c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(9),
2038c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(12),
2048c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(18),
2058c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(24),
2068c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(36),
2078c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(48),
2088c2ecf20Sopenharmony_ci	IWL_DECLARE_RATE_INFO(54),
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciint iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
2128c2ecf20Sopenharmony_ci					enum nl80211_band band)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
2158c2ecf20Sopenharmony_ci	int idx;
2168c2ecf20Sopenharmony_ci	int band_offset = 0;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* Legacy rate format, search for match in table */
2198c2ecf20Sopenharmony_ci	if (band != NL80211_BAND_2GHZ)
2208c2ecf20Sopenharmony_ci		band_offset = IWL_FIRST_OFDM_RATE;
2218c2ecf20Sopenharmony_ci	for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
2228c2ecf20Sopenharmony_ci		if (fw_rate_idx_to_plcp[idx] == rate)
2238c2ecf20Sopenharmony_ci			return idx - band_offset;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	return -1;
2268c2ecf20Sopenharmony_ci}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ciu8 iwl_mvm_mac80211_idx_to_hwrate(int rate_idx)
2298c2ecf20Sopenharmony_ci{
2308c2ecf20Sopenharmony_ci	/* Get PLCP rate for tx_cmd->rate_n_flags */
2318c2ecf20Sopenharmony_ci	return fw_rate_idx_to_plcp[rate_idx];
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ciu8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	static const u8 mac80211_ac_to_ucode_ac[] = {
2378c2ecf20Sopenharmony_ci		AC_VO,
2388c2ecf20Sopenharmony_ci		AC_VI,
2398c2ecf20Sopenharmony_ci		AC_BE,
2408c2ecf20Sopenharmony_ci		AC_BK
2418c2ecf20Sopenharmony_ci	};
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	return mac80211_ac_to_ucode_ac[ac];
2448c2ecf20Sopenharmony_ci}
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_civoid iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
2478c2ecf20Sopenharmony_ci{
2488c2ecf20Sopenharmony_ci	struct iwl_rx_packet *pkt = rxb_addr(rxb);
2498c2ecf20Sopenharmony_ci	struct iwl_error_resp *err_resp = (void *)pkt->data;
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
2528c2ecf20Sopenharmony_ci		le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
2538c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
2548c2ecf20Sopenharmony_ci		le16_to_cpu(err_resp->bad_cmd_seq_num),
2558c2ecf20Sopenharmony_ci		le32_to_cpu(err_resp->error_service));
2568c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
2578c2ecf20Sopenharmony_ci		le64_to_cpu(err_resp->timestamp));
2588c2ecf20Sopenharmony_ci}
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci/*
2618c2ecf20Sopenharmony_ci * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
2628c2ecf20Sopenharmony_ci * The parameter should also be a combination of ANT_[ABC].
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_ciu8 first_antenna(u8 mask)
2658c2ecf20Sopenharmony_ci{
2668c2ecf20Sopenharmony_ci	BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
2678c2ecf20Sopenharmony_ci	if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
2688c2ecf20Sopenharmony_ci		return BIT(0);
2698c2ecf20Sopenharmony_ci	return BIT(ffs(mask) - 1);
2708c2ecf20Sopenharmony_ci}
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci/*
2738c2ecf20Sopenharmony_ci * Toggles between TX antennas to send the probe request on.
2748c2ecf20Sopenharmony_ci * Receives the bitmask of valid TX antennas and the *index* used
2758c2ecf20Sopenharmony_ci * for the last TX, and returns the next valid *index* to use.
2768c2ecf20Sopenharmony_ci * In order to set it in the tx_cmd, must do BIT(idx).
2778c2ecf20Sopenharmony_ci */
2788c2ecf20Sopenharmony_ciu8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
2798c2ecf20Sopenharmony_ci{
2808c2ecf20Sopenharmony_ci	u8 ind = last_idx;
2818c2ecf20Sopenharmony_ci	int i;
2828c2ecf20Sopenharmony_ci
2838c2ecf20Sopenharmony_ci	for (i = 0; i < MAX_ANT_NUM; i++) {
2848c2ecf20Sopenharmony_ci		ind = (ind + 1) % MAX_ANT_NUM;
2858c2ecf20Sopenharmony_ci		if (valid & BIT(ind))
2868c2ecf20Sopenharmony_ci			return ind;
2878c2ecf20Sopenharmony_ci	}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_ci	WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
2908c2ecf20Sopenharmony_ci	return last_idx;
2918c2ecf20Sopenharmony_ci}
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci/*
2948c2ecf20Sopenharmony_ci * Note: This structure is read from the device with IO accesses,
2958c2ecf20Sopenharmony_ci * and the reading already does the endian conversion. As it is
2968c2ecf20Sopenharmony_ci * read with u32-sized accesses, any members with a different size
2978c2ecf20Sopenharmony_ci * need to be ordered correctly though!
2988c2ecf20Sopenharmony_ci */
2998c2ecf20Sopenharmony_cistruct iwl_error_event_table_v1 {
3008c2ecf20Sopenharmony_ci	u32 valid;		/* (nonzero) valid, (0) log is empty */
3018c2ecf20Sopenharmony_ci	u32 error_id;		/* type of error */
3028c2ecf20Sopenharmony_ci	u32 pc;			/* program counter */
3038c2ecf20Sopenharmony_ci	u32 blink1;		/* branch link */
3048c2ecf20Sopenharmony_ci	u32 blink2;		/* branch link */
3058c2ecf20Sopenharmony_ci	u32 ilink1;		/* interrupt link */
3068c2ecf20Sopenharmony_ci	u32 ilink2;		/* interrupt link */
3078c2ecf20Sopenharmony_ci	u32 data1;		/* error-specific data */
3088c2ecf20Sopenharmony_ci	u32 data2;		/* error-specific data */
3098c2ecf20Sopenharmony_ci	u32 data3;		/* error-specific data */
3108c2ecf20Sopenharmony_ci	u32 bcon_time;		/* beacon timer */
3118c2ecf20Sopenharmony_ci	u32 tsf_low;		/* network timestamp function timer */
3128c2ecf20Sopenharmony_ci	u32 tsf_hi;		/* network timestamp function timer */
3138c2ecf20Sopenharmony_ci	u32 gp1;		/* GP1 timer register */
3148c2ecf20Sopenharmony_ci	u32 gp2;		/* GP2 timer register */
3158c2ecf20Sopenharmony_ci	u32 gp3;		/* GP3 timer register */
3168c2ecf20Sopenharmony_ci	u32 ucode_ver;		/* uCode version */
3178c2ecf20Sopenharmony_ci	u32 hw_ver;		/* HW Silicon version */
3188c2ecf20Sopenharmony_ci	u32 brd_ver;		/* HW board version */
3198c2ecf20Sopenharmony_ci	u32 log_pc;		/* log program counter */
3208c2ecf20Sopenharmony_ci	u32 frame_ptr;		/* frame pointer */
3218c2ecf20Sopenharmony_ci	u32 stack_ptr;		/* stack pointer */
3228c2ecf20Sopenharmony_ci	u32 hcmd;		/* last host command header */
3238c2ecf20Sopenharmony_ci	u32 isr0;		/* isr status register LMPM_NIC_ISR0:
3248c2ecf20Sopenharmony_ci				 * rxtx_flag */
3258c2ecf20Sopenharmony_ci	u32 isr1;		/* isr status register LMPM_NIC_ISR1:
3268c2ecf20Sopenharmony_ci				 * host_flag */
3278c2ecf20Sopenharmony_ci	u32 isr2;		/* isr status register LMPM_NIC_ISR2:
3288c2ecf20Sopenharmony_ci				 * enc_flag */
3298c2ecf20Sopenharmony_ci	u32 isr3;		/* isr status register LMPM_NIC_ISR3:
3308c2ecf20Sopenharmony_ci				 * time_flag */
3318c2ecf20Sopenharmony_ci	u32 isr4;		/* isr status register LMPM_NIC_ISR4:
3328c2ecf20Sopenharmony_ci				 * wico interrupt */
3338c2ecf20Sopenharmony_ci	u32 isr_pref;		/* isr status register LMPM_NIC_PREF_STAT */
3348c2ecf20Sopenharmony_ci	u32 wait_event;		/* wait event() caller address */
3358c2ecf20Sopenharmony_ci	u32 l2p_control;	/* L2pControlField */
3368c2ecf20Sopenharmony_ci	u32 l2p_duration;	/* L2pDurationField */
3378c2ecf20Sopenharmony_ci	u32 l2p_mhvalid;	/* L2pMhValidBits */
3388c2ecf20Sopenharmony_ci	u32 l2p_addr_match;	/* L2pAddrMatchStat */
3398c2ecf20Sopenharmony_ci	u32 lmpm_pmg_sel;	/* indicate which clocks are turned on
3408c2ecf20Sopenharmony_ci				 * (LMPM_PMG_SEL) */
3418c2ecf20Sopenharmony_ci	u32 u_timestamp;	/* indicate when the date and time of the
3428c2ecf20Sopenharmony_ci				 * compilation */
3438c2ecf20Sopenharmony_ci	u32 flow_handler;	/* FH read/write pointers, RX credit */
3448c2ecf20Sopenharmony_ci} __packed /* LOG_ERROR_TABLE_API_S_VER_1 */;
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_cistruct iwl_error_event_table {
3478c2ecf20Sopenharmony_ci	u32 valid;		/* (nonzero) valid, (0) log is empty */
3488c2ecf20Sopenharmony_ci	u32 error_id;		/* type of error */
3498c2ecf20Sopenharmony_ci	u32 trm_hw_status0;	/* TRM HW status */
3508c2ecf20Sopenharmony_ci	u32 trm_hw_status1;	/* TRM HW status */
3518c2ecf20Sopenharmony_ci	u32 blink2;		/* branch link */
3528c2ecf20Sopenharmony_ci	u32 ilink1;		/* interrupt link */
3538c2ecf20Sopenharmony_ci	u32 ilink2;		/* interrupt link */
3548c2ecf20Sopenharmony_ci	u32 data1;		/* error-specific data */
3558c2ecf20Sopenharmony_ci	u32 data2;		/* error-specific data */
3568c2ecf20Sopenharmony_ci	u32 data3;		/* error-specific data */
3578c2ecf20Sopenharmony_ci	u32 bcon_time;		/* beacon timer */
3588c2ecf20Sopenharmony_ci	u32 tsf_low;		/* network timestamp function timer */
3598c2ecf20Sopenharmony_ci	u32 tsf_hi;		/* network timestamp function timer */
3608c2ecf20Sopenharmony_ci	u32 gp1;		/* GP1 timer register */
3618c2ecf20Sopenharmony_ci	u32 gp2;		/* GP2 timer register */
3628c2ecf20Sopenharmony_ci	u32 fw_rev_type;	/* firmware revision type */
3638c2ecf20Sopenharmony_ci	u32 major;		/* uCode version major */
3648c2ecf20Sopenharmony_ci	u32 minor;		/* uCode version minor */
3658c2ecf20Sopenharmony_ci	u32 hw_ver;		/* HW Silicon version */
3668c2ecf20Sopenharmony_ci	u32 brd_ver;		/* HW board version */
3678c2ecf20Sopenharmony_ci	u32 log_pc;		/* log program counter */
3688c2ecf20Sopenharmony_ci	u32 frame_ptr;		/* frame pointer */
3698c2ecf20Sopenharmony_ci	u32 stack_ptr;		/* stack pointer */
3708c2ecf20Sopenharmony_ci	u32 hcmd;		/* last host command header */
3718c2ecf20Sopenharmony_ci	u32 isr0;		/* isr status register LMPM_NIC_ISR0:
3728c2ecf20Sopenharmony_ci				 * rxtx_flag */
3738c2ecf20Sopenharmony_ci	u32 isr1;		/* isr status register LMPM_NIC_ISR1:
3748c2ecf20Sopenharmony_ci				 * host_flag */
3758c2ecf20Sopenharmony_ci	u32 isr2;		/* isr status register LMPM_NIC_ISR2:
3768c2ecf20Sopenharmony_ci				 * enc_flag */
3778c2ecf20Sopenharmony_ci	u32 isr3;		/* isr status register LMPM_NIC_ISR3:
3788c2ecf20Sopenharmony_ci				 * time_flag */
3798c2ecf20Sopenharmony_ci	u32 isr4;		/* isr status register LMPM_NIC_ISR4:
3808c2ecf20Sopenharmony_ci				 * wico interrupt */
3818c2ecf20Sopenharmony_ci	u32 last_cmd_id;	/* last HCMD id handled by the firmware */
3828c2ecf20Sopenharmony_ci	u32 wait_event;		/* wait event() caller address */
3838c2ecf20Sopenharmony_ci	u32 l2p_control;	/* L2pControlField */
3848c2ecf20Sopenharmony_ci	u32 l2p_duration;	/* L2pDurationField */
3858c2ecf20Sopenharmony_ci	u32 l2p_mhvalid;	/* L2pMhValidBits */
3868c2ecf20Sopenharmony_ci	u32 l2p_addr_match;	/* L2pAddrMatchStat */
3878c2ecf20Sopenharmony_ci	u32 lmpm_pmg_sel;	/* indicate which clocks are turned on
3888c2ecf20Sopenharmony_ci				 * (LMPM_PMG_SEL) */
3898c2ecf20Sopenharmony_ci	u32 u_timestamp;	/* indicate when the date and time of the
3908c2ecf20Sopenharmony_ci				 * compilation */
3918c2ecf20Sopenharmony_ci	u32 flow_handler;	/* FH read/write pointers, RX credit */
3928c2ecf20Sopenharmony_ci} __packed /* LOG_ERROR_TABLE_API_S_VER_3 */;
3938c2ecf20Sopenharmony_ci
3948c2ecf20Sopenharmony_ci/*
3958c2ecf20Sopenharmony_ci * UMAC error struct - relevant starting from family 8000 chip.
3968c2ecf20Sopenharmony_ci * Note: This structure is read from the device with IO accesses,
3978c2ecf20Sopenharmony_ci * and the reading already does the endian conversion. As it is
3988c2ecf20Sopenharmony_ci * read with u32-sized accesses, any members with a different size
3998c2ecf20Sopenharmony_ci * need to be ordered correctly though!
4008c2ecf20Sopenharmony_ci */
4018c2ecf20Sopenharmony_cistruct iwl_umac_error_event_table {
4028c2ecf20Sopenharmony_ci	u32 valid;		/* (nonzero) valid, (0) log is empty */
4038c2ecf20Sopenharmony_ci	u32 error_id;		/* type of error */
4048c2ecf20Sopenharmony_ci	u32 blink1;		/* branch link */
4058c2ecf20Sopenharmony_ci	u32 blink2;		/* branch link */
4068c2ecf20Sopenharmony_ci	u32 ilink1;		/* interrupt link */
4078c2ecf20Sopenharmony_ci	u32 ilink2;		/* interrupt link */
4088c2ecf20Sopenharmony_ci	u32 data1;		/* error-specific data */
4098c2ecf20Sopenharmony_ci	u32 data2;		/* error-specific data */
4108c2ecf20Sopenharmony_ci	u32 data3;		/* error-specific data */
4118c2ecf20Sopenharmony_ci	u32 umac_major;
4128c2ecf20Sopenharmony_ci	u32 umac_minor;
4138c2ecf20Sopenharmony_ci	u32 frame_pointer;	/* core register 27*/
4148c2ecf20Sopenharmony_ci	u32 stack_pointer;	/* core register 28 */
4158c2ecf20Sopenharmony_ci	u32 cmd_header;		/* latest host cmd sent to UMAC */
4168c2ecf20Sopenharmony_ci	u32 nic_isr_pref;	/* ISR status register */
4178c2ecf20Sopenharmony_ci} __packed;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci#define ERROR_START_OFFSET  (1 * sizeof(u32))
4208c2ecf20Sopenharmony_ci#define ERROR_ELEM_SIZE     (7 * sizeof(u32))
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic void iwl_mvm_dump_umac_error_log(struct iwl_mvm *mvm)
4238c2ecf20Sopenharmony_ci{
4248c2ecf20Sopenharmony_ci	struct iwl_trans *trans = mvm->trans;
4258c2ecf20Sopenharmony_ci	struct iwl_umac_error_event_table table;
4268c2ecf20Sopenharmony_ci	u32 base = mvm->trans->dbg.umac_error_event_table;
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci	if (!base &&
4298c2ecf20Sopenharmony_ci	    !(mvm->trans->dbg.error_event_table_tlv_status &
4308c2ecf20Sopenharmony_ci	      IWL_ERROR_EVENT_TABLE_UMAC))
4318c2ecf20Sopenharmony_ci		return;
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
4348c2ecf20Sopenharmony_ci
4358c2ecf20Sopenharmony_ci	if (table.valid)
4368c2ecf20Sopenharmony_ci		mvm->fwrt.dump.umac_err_id = table.error_id;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
4398c2ecf20Sopenharmony_ci		IWL_ERR(trans, "Start IWL Error Log Dump:\n");
4408c2ecf20Sopenharmony_ci		IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
4418c2ecf20Sopenharmony_ci			mvm->status, table.valid);
4428c2ecf20Sopenharmony_ci	}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | %s\n", table.error_id,
4458c2ecf20Sopenharmony_ci		iwl_fw_lookup_assert_desc(table.error_id));
4468c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac branchlink1\n", table.blink1);
4478c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac branchlink2\n", table.blink2);
4488c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac interruptlink1\n", table.ilink1);
4498c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac interruptlink2\n", table.ilink2);
4508c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac data1\n", table.data1);
4518c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac data2\n", table.data2);
4528c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac data3\n", table.data3);
4538c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac major\n", table.umac_major);
4548c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | umac minor\n", table.umac_minor);
4558c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | frame pointer\n", table.frame_pointer);
4568c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | stack pointer\n", table.stack_pointer);
4578c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | last host cmd\n", table.cmd_header);
4588c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr status reg\n", table.nic_isr_pref);
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_cistatic void iwl_mvm_dump_lmac_error_log(struct iwl_mvm *mvm, u8 lmac_num)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	struct iwl_trans *trans = mvm->trans;
4648c2ecf20Sopenharmony_ci	struct iwl_error_event_table table;
4658c2ecf20Sopenharmony_ci	u32 val, base = mvm->trans->dbg.lmac_error_event_table[lmac_num];
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_ci	if (mvm->fwrt.cur_fw_img == IWL_UCODE_INIT) {
4688c2ecf20Sopenharmony_ci		if (!base)
4698c2ecf20Sopenharmony_ci			base = mvm->fw->init_errlog_ptr;
4708c2ecf20Sopenharmony_ci	} else {
4718c2ecf20Sopenharmony_ci		if (!base)
4728c2ecf20Sopenharmony_ci			base = mvm->fw->inst_errlog_ptr;
4738c2ecf20Sopenharmony_ci	}
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	if (base < 0x400000) {
4768c2ecf20Sopenharmony_ci		IWL_ERR(mvm,
4778c2ecf20Sopenharmony_ci			"Not valid error log pointer 0x%08X for %s uCode\n",
4788c2ecf20Sopenharmony_ci			base,
4798c2ecf20Sopenharmony_ci			(mvm->fwrt.cur_fw_img == IWL_UCODE_INIT)
4808c2ecf20Sopenharmony_ci			? "Init" : "RT");
4818c2ecf20Sopenharmony_ci		return;
4828c2ecf20Sopenharmony_ci	}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	/* check if there is a HW error */
4858c2ecf20Sopenharmony_ci	val = iwl_trans_read_mem32(trans, base);
4868c2ecf20Sopenharmony_ci	if (((val & ~0xf) == 0xa5a5a5a0) || ((val & ~0xf) == 0x5a5a5a50)) {
4878c2ecf20Sopenharmony_ci		int err;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci		IWL_ERR(trans, "HW error, resetting before reading\n");
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci		/* reset the device */
4928c2ecf20Sopenharmony_ci		iwl_trans_sw_reset(trans);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_ci		err = iwl_finish_nic_init(trans, trans->trans_cfg);
4958c2ecf20Sopenharmony_ci		if (err)
4968c2ecf20Sopenharmony_ci			return;
4978c2ecf20Sopenharmony_ci	}
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci	iwl_trans_read_mem_bytes(trans, base, &table, sizeof(table));
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	if (table.valid)
5028c2ecf20Sopenharmony_ci		mvm->fwrt.dump.lmac_err_id[lmac_num] = table.error_id;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	if (ERROR_START_OFFSET <= table.valid * ERROR_ELEM_SIZE) {
5058c2ecf20Sopenharmony_ci		IWL_ERR(trans, "Start IWL Error Log Dump:\n");
5068c2ecf20Sopenharmony_ci		IWL_ERR(trans, "Status: 0x%08lX, count: %d\n",
5078c2ecf20Sopenharmony_ci			mvm->status, table.valid);
5088c2ecf20Sopenharmony_ci	}
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	/* Do not change this output - scripts rely on it */
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "Loaded firmware version: %s\n", mvm->fw->fw_version);
5138c2ecf20Sopenharmony_ci
5148c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | %-28s\n", table.error_id,
5158c2ecf20Sopenharmony_ci		iwl_fw_lookup_assert_desc(table.error_id));
5168c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | trm_hw_status0\n", table.trm_hw_status0);
5178c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | trm_hw_status1\n", table.trm_hw_status1);
5188c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | branchlink2\n", table.blink2);
5198c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | interruptlink1\n", table.ilink1);
5208c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | interruptlink2\n", table.ilink2);
5218c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | data1\n", table.data1);
5228c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | data2\n", table.data2);
5238c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | data3\n", table.data3);
5248c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | beacon time\n", table.bcon_time);
5258c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | tsf low\n", table.tsf_low);
5268c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | tsf hi\n", table.tsf_hi);
5278c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | time gp1\n", table.gp1);
5288c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | time gp2\n", table.gp2);
5298c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | uCode revision type\n", table.fw_rev_type);
5308c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | uCode version major\n", table.major);
5318c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | uCode version minor\n", table.minor);
5328c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | hw version\n", table.hw_ver);
5338c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | board version\n", table.brd_ver);
5348c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | hcmd\n", table.hcmd);
5358c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr0\n", table.isr0);
5368c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr1\n", table.isr1);
5378c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr2\n", table.isr2);
5388c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr3\n", table.isr3);
5398c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | isr4\n", table.isr4);
5408c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | last cmd Id\n", table.last_cmd_id);
5418c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | wait_event\n", table.wait_event);
5428c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | l2p_control\n", table.l2p_control);
5438c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | l2p_duration\n", table.l2p_duration);
5448c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | l2p_mhvalid\n", table.l2p_mhvalid);
5458c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | l2p_addr_match\n", table.l2p_addr_match);
5468c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | lmpm_pmg_sel\n", table.lmpm_pmg_sel);
5478c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | timestamp\n", table.u_timestamp);
5488c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | flow_handler\n", table.flow_handler);
5498c2ecf20Sopenharmony_ci}
5508c2ecf20Sopenharmony_ci
5518c2ecf20Sopenharmony_cistatic void iwl_mvm_dump_iml_error_log(struct iwl_mvm *mvm)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct iwl_trans *trans = mvm->trans;
5548c2ecf20Sopenharmony_ci	u32 error;
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_ci	error = iwl_read_umac_prph(trans, UMAG_SB_CPU_2_STATUS);
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_ci	IWL_ERR(trans, "IML/ROM dump:\n");
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	if (error & 0xFFFF0000)
5618c2ecf20Sopenharmony_ci		IWL_ERR(trans, "IML/ROM SYSASSERT:\n");
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | IML/ROM error/state\n", error);
5648c2ecf20Sopenharmony_ci	IWL_ERR(mvm, "0x%08X | IML/ROM data1\n",
5658c2ecf20Sopenharmony_ci		iwl_read_umac_prph(trans, UMAG_SB_CPU_1_STATUS));
5668c2ecf20Sopenharmony_ci}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_civoid iwl_mvm_dump_nic_error_log(struct iwl_mvm *mvm)
5698c2ecf20Sopenharmony_ci{
5708c2ecf20Sopenharmony_ci	if (!test_bit(STATUS_DEVICE_ENABLED, &mvm->trans->status)) {
5718c2ecf20Sopenharmony_ci		IWL_ERR(mvm,
5728c2ecf20Sopenharmony_ci			"DEVICE_ENABLED bit is not set. Aborting dump.\n");
5738c2ecf20Sopenharmony_ci		return;
5748c2ecf20Sopenharmony_ci	}
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_ci	iwl_mvm_dump_lmac_error_log(mvm, 0);
5778c2ecf20Sopenharmony_ci
5788c2ecf20Sopenharmony_ci	if (mvm->trans->dbg.lmac_error_event_table[1])
5798c2ecf20Sopenharmony_ci		iwl_mvm_dump_lmac_error_log(mvm, 1);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	iwl_mvm_dump_umac_error_log(mvm);
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_AX210)
5848c2ecf20Sopenharmony_ci		iwl_mvm_dump_iml_error_log(mvm);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	iwl_fw_error_print_fseq_regs(&mvm->fwrt);
5878c2ecf20Sopenharmony_ci}
5888c2ecf20Sopenharmony_ci
5898c2ecf20Sopenharmony_ciint iwl_mvm_reconfig_scd(struct iwl_mvm *mvm, int queue, int fifo, int sta_id,
5908c2ecf20Sopenharmony_ci			 int tid, int frame_limit, u16 ssn)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	struct iwl_scd_txq_cfg_cmd cmd = {
5938c2ecf20Sopenharmony_ci		.scd_queue = queue,
5948c2ecf20Sopenharmony_ci		.action = SCD_CFG_ENABLE_QUEUE,
5958c2ecf20Sopenharmony_ci		.window = frame_limit,
5968c2ecf20Sopenharmony_ci		.sta_id = sta_id,
5978c2ecf20Sopenharmony_ci		.ssn = cpu_to_le16(ssn),
5988c2ecf20Sopenharmony_ci		.tx_fifo = fifo,
5998c2ecf20Sopenharmony_ci		.aggregate = (queue >= IWL_MVM_DQA_MIN_DATA_QUEUE ||
6008c2ecf20Sopenharmony_ci			      queue == IWL_MVM_DQA_BSS_CLIENT_QUEUE),
6018c2ecf20Sopenharmony_ci		.tid = tid,
6028c2ecf20Sopenharmony_ci	};
6038c2ecf20Sopenharmony_ci	int ret;
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	if (WARN_ON(iwl_mvm_has_new_tx_api(mvm)))
6068c2ecf20Sopenharmony_ci		return -EINVAL;
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (WARN(mvm->queue_info[queue].tid_bitmap == 0,
6098c2ecf20Sopenharmony_ci		 "Trying to reconfig unallocated queue %d\n", queue))
6108c2ecf20Sopenharmony_ci		return -ENXIO;
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	IWL_DEBUG_TX_QUEUES(mvm, "Reconfig SCD for TXQ #%d\n", queue);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd_pdu(mvm, SCD_QUEUE_CFG, 0, sizeof(cmd), &cmd);
6158c2ecf20Sopenharmony_ci	WARN_ONCE(ret, "Failed to re-configure queue %d on FIFO %d, ret=%d\n",
6168c2ecf20Sopenharmony_ci		  queue, fifo, ret);
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	return ret;
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci/**
6228c2ecf20Sopenharmony_ci * iwl_mvm_send_lq_cmd() - Send link quality command
6238c2ecf20Sopenharmony_ci * @mvm: Driver data.
6248c2ecf20Sopenharmony_ci * @lq: Link quality command to send.
6258c2ecf20Sopenharmony_ci *
6268c2ecf20Sopenharmony_ci * The link quality command is sent as the last step of station creation.
6278c2ecf20Sopenharmony_ci * This is the special case in which init is set and we call a callback in
6288c2ecf20Sopenharmony_ci * this case to clear the state indicating that station creation is in
6298c2ecf20Sopenharmony_ci * progress.
6308c2ecf20Sopenharmony_ci */
6318c2ecf20Sopenharmony_ciint iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
6328c2ecf20Sopenharmony_ci{
6338c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
6348c2ecf20Sopenharmony_ci		.id = LQ_CMD,
6358c2ecf20Sopenharmony_ci		.len = { sizeof(struct iwl_lq_cmd), },
6368c2ecf20Sopenharmony_ci		.flags = CMD_ASYNC,
6378c2ecf20Sopenharmony_ci		.data = { lq, },
6388c2ecf20Sopenharmony_ci	};
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	if (WARN_ON(lq->sta_id == IWL_MVM_INVALID_STA ||
6418c2ecf20Sopenharmony_ci		    iwl_mvm_has_tlc_offload(mvm)))
6428c2ecf20Sopenharmony_ci		return -EINVAL;
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_ci	return iwl_mvm_send_cmd(mvm, &cmd);
6458c2ecf20Sopenharmony_ci}
6468c2ecf20Sopenharmony_ci
6478c2ecf20Sopenharmony_ci/**
6488c2ecf20Sopenharmony_ci * iwl_mvm_update_smps - Get a request to change the SMPS mode
6498c2ecf20Sopenharmony_ci * @mvm: Driver data.
6508c2ecf20Sopenharmony_ci * @vif: Pointer to the ieee80211_vif structure
6518c2ecf20Sopenharmony_ci * @req_type: The part of the driver who call for a change.
6528c2ecf20Sopenharmony_ci * @smps_request: The request to change the SMPS mode.
6538c2ecf20Sopenharmony_ci *
6548c2ecf20Sopenharmony_ci * Get a requst to change the SMPS mode,
6558c2ecf20Sopenharmony_ci * and change it according to all other requests in the driver.
6568c2ecf20Sopenharmony_ci */
6578c2ecf20Sopenharmony_civoid iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
6588c2ecf20Sopenharmony_ci			 enum iwl_mvm_smps_type_request req_type,
6598c2ecf20Sopenharmony_ci			 enum ieee80211_smps_mode smps_request)
6608c2ecf20Sopenharmony_ci{
6618c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif;
6628c2ecf20Sopenharmony_ci	enum ieee80211_smps_mode smps_mode;
6638c2ecf20Sopenharmony_ci	int i;
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	/* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
6688c2ecf20Sopenharmony_ci	if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
6698c2ecf20Sopenharmony_ci		return;
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (vif->type == NL80211_IFTYPE_AP)
6728c2ecf20Sopenharmony_ci		smps_mode = IEEE80211_SMPS_OFF;
6738c2ecf20Sopenharmony_ci	else
6748c2ecf20Sopenharmony_ci		smps_mode = IEEE80211_SMPS_AUTOMATIC;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci	mvmvif = iwl_mvm_vif_from_mac80211(vif);
6778c2ecf20Sopenharmony_ci	mvmvif->smps_requests[req_type] = smps_request;
6788c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
6798c2ecf20Sopenharmony_ci		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC) {
6808c2ecf20Sopenharmony_ci			smps_mode = IEEE80211_SMPS_STATIC;
6818c2ecf20Sopenharmony_ci			break;
6828c2ecf20Sopenharmony_ci		}
6838c2ecf20Sopenharmony_ci		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
6848c2ecf20Sopenharmony_ci			smps_mode = IEEE80211_SMPS_DYNAMIC;
6858c2ecf20Sopenharmony_ci	}
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	ieee80211_request_smps(vif, smps_mode);
6888c2ecf20Sopenharmony_ci}
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ciint iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
6918c2ecf20Sopenharmony_ci{
6928c2ecf20Sopenharmony_ci	struct iwl_statistics_cmd scmd = {
6938c2ecf20Sopenharmony_ci		.flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
6948c2ecf20Sopenharmony_ci	};
6958c2ecf20Sopenharmony_ci	struct iwl_host_cmd cmd = {
6968c2ecf20Sopenharmony_ci		.id = STATISTICS_CMD,
6978c2ecf20Sopenharmony_ci		.len[0] = sizeof(scmd),
6988c2ecf20Sopenharmony_ci		.data[0] = &scmd,
6998c2ecf20Sopenharmony_ci		.flags = CMD_WANT_SKB,
7008c2ecf20Sopenharmony_ci	};
7018c2ecf20Sopenharmony_ci	int ret;
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	ret = iwl_mvm_send_cmd(mvm, &cmd);
7048c2ecf20Sopenharmony_ci	if (ret)
7058c2ecf20Sopenharmony_ci		return ret;
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
7088c2ecf20Sopenharmony_ci	iwl_free_resp(&cmd);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	if (clear)
7118c2ecf20Sopenharmony_ci		iwl_mvm_accu_radio_stats(mvm);
7128c2ecf20Sopenharmony_ci
7138c2ecf20Sopenharmony_ci	return 0;
7148c2ecf20Sopenharmony_ci}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_civoid iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
7178c2ecf20Sopenharmony_ci{
7188c2ecf20Sopenharmony_ci	mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
7198c2ecf20Sopenharmony_ci	mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
7208c2ecf20Sopenharmony_ci	mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
7218c2ecf20Sopenharmony_ci	mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic void iwl_mvm_diversity_iter(void *_data, u8 *mac,
7258c2ecf20Sopenharmony_ci				   struct ieee80211_vif *vif)
7268c2ecf20Sopenharmony_ci{
7278c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
7288c2ecf20Sopenharmony_ci	bool *result = _data;
7298c2ecf20Sopenharmony_ci	int i;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
7328c2ecf20Sopenharmony_ci		if (mvmvif->smps_requests[i] == IEEE80211_SMPS_STATIC ||
7338c2ecf20Sopenharmony_ci		    mvmvif->smps_requests[i] == IEEE80211_SMPS_DYNAMIC)
7348c2ecf20Sopenharmony_ci			*result = false;
7358c2ecf20Sopenharmony_ci	}
7368c2ecf20Sopenharmony_ci}
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cibool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm)
7398c2ecf20Sopenharmony_ci{
7408c2ecf20Sopenharmony_ci	bool result = true;
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
7458c2ecf20Sopenharmony_ci		return false;
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
7488c2ecf20Sopenharmony_ci		return false;
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci	if (mvm->cfg->rx_with_siso_diversity)
7518c2ecf20Sopenharmony_ci		return false;
7528c2ecf20Sopenharmony_ci
7538c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
7548c2ecf20Sopenharmony_ci			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
7558c2ecf20Sopenharmony_ci			iwl_mvm_diversity_iter, &result);
7568c2ecf20Sopenharmony_ci
7578c2ecf20Sopenharmony_ci	return result;
7588c2ecf20Sopenharmony_ci}
7598c2ecf20Sopenharmony_ci
7608c2ecf20Sopenharmony_civoid iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
7618c2ecf20Sopenharmony_ci				  bool low_latency, u16 mac_id)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	struct iwl_mac_low_latency_cmd cmd = {
7648c2ecf20Sopenharmony_ci		.mac_id = cpu_to_le32(mac_id)
7658c2ecf20Sopenharmony_ci	};
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci	if (!fw_has_capa(&mvm->fw->ucode_capa,
7688c2ecf20Sopenharmony_ci			 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
7698c2ecf20Sopenharmony_ci		return;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	if (low_latency) {
7728c2ecf20Sopenharmony_ci		/* currently we don't care about the direction */
7738c2ecf20Sopenharmony_ci		cmd.low_latency_rx = 1;
7748c2ecf20Sopenharmony_ci		cmd.low_latency_tx = 1;
7758c2ecf20Sopenharmony_ci	}
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	if (iwl_mvm_send_cmd_pdu(mvm, iwl_cmd_id(LOW_LATENCY_CMD,
7788c2ecf20Sopenharmony_ci						 MAC_CONF_GROUP, 0),
7798c2ecf20Sopenharmony_ci				 0, sizeof(cmd), &cmd))
7808c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "Failed to send low latency command\n");
7818c2ecf20Sopenharmony_ci}
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ciint iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
7848c2ecf20Sopenharmony_ci			       bool low_latency,
7858c2ecf20Sopenharmony_ci			       enum iwl_mvm_low_latency_cause cause)
7868c2ecf20Sopenharmony_ci{
7878c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
7888c2ecf20Sopenharmony_ci	int res;
7898c2ecf20Sopenharmony_ci	bool prev;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
7928c2ecf20Sopenharmony_ci
7938c2ecf20Sopenharmony_ci	prev = iwl_mvm_vif_low_latency(mvmvif);
7948c2ecf20Sopenharmony_ci	iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	low_latency = iwl_mvm_vif_low_latency(mvmvif);
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci	if (low_latency == prev)
7998c2ecf20Sopenharmony_ci		return 0;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_ci	res = iwl_mvm_update_quotas(mvm, false, NULL);
8048c2ecf20Sopenharmony_ci	if (res)
8058c2ecf20Sopenharmony_ci		return res;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	iwl_mvm_bt_coex_vif_change(mvm);
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	return iwl_mvm_power_update_mac(mvm);
8108c2ecf20Sopenharmony_ci}
8118c2ecf20Sopenharmony_ci
8128c2ecf20Sopenharmony_cistruct iwl_mvm_low_latency_iter {
8138c2ecf20Sopenharmony_ci	bool result;
8148c2ecf20Sopenharmony_ci	bool result_per_band[NUM_NL80211_BANDS];
8158c2ecf20Sopenharmony_ci};
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_cistatic void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	struct iwl_mvm_low_latency_iter *result = _data;
8208c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
8218c2ecf20Sopenharmony_ci	enum nl80211_band band;
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	if (iwl_mvm_vif_low_latency(mvmvif)) {
8248c2ecf20Sopenharmony_ci		result->result = true;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci		if (!mvmvif->phy_ctxt)
8278c2ecf20Sopenharmony_ci			return;
8288c2ecf20Sopenharmony_ci
8298c2ecf20Sopenharmony_ci		band = mvmvif->phy_ctxt->channel->band;
8308c2ecf20Sopenharmony_ci		result->result_per_band[band] = true;
8318c2ecf20Sopenharmony_ci	}
8328c2ecf20Sopenharmony_ci}
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_cibool iwl_mvm_low_latency(struct iwl_mvm *mvm)
8358c2ecf20Sopenharmony_ci{
8368c2ecf20Sopenharmony_ci	struct iwl_mvm_low_latency_iter data = {};
8378c2ecf20Sopenharmony_ci
8388c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
8398c2ecf20Sopenharmony_ci			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
8408c2ecf20Sopenharmony_ci			iwl_mvm_ll_iter, &data);
8418c2ecf20Sopenharmony_ci
8428c2ecf20Sopenharmony_ci	return data.result;
8438c2ecf20Sopenharmony_ci}
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_cibool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
8468c2ecf20Sopenharmony_ci{
8478c2ecf20Sopenharmony_ci	struct iwl_mvm_low_latency_iter data = {};
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
8508c2ecf20Sopenharmony_ci			mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
8518c2ecf20Sopenharmony_ci			iwl_mvm_ll_iter, &data);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	return data.result_per_band[band];
8548c2ecf20Sopenharmony_ci}
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_cistruct iwl_bss_iter_data {
8578c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
8588c2ecf20Sopenharmony_ci	bool error;
8598c2ecf20Sopenharmony_ci};
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_cistatic void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
8628c2ecf20Sopenharmony_ci				       struct ieee80211_vif *vif)
8638c2ecf20Sopenharmony_ci{
8648c2ecf20Sopenharmony_ci	struct iwl_bss_iter_data *data = _data;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
8678c2ecf20Sopenharmony_ci		return;
8688c2ecf20Sopenharmony_ci
8698c2ecf20Sopenharmony_ci	if (data->vif) {
8708c2ecf20Sopenharmony_ci		data->error = true;
8718c2ecf20Sopenharmony_ci		return;
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	data->vif = vif;
8758c2ecf20Sopenharmony_ci}
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_cistruct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
8788c2ecf20Sopenharmony_ci{
8798c2ecf20Sopenharmony_ci	struct iwl_bss_iter_data bss_iter_data = {};
8808c2ecf20Sopenharmony_ci
8818c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(
8828c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
8838c2ecf20Sopenharmony_ci		iwl_mvm_bss_iface_iterator, &bss_iter_data);
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_ci	if (bss_iter_data.error) {
8868c2ecf20Sopenharmony_ci		IWL_ERR(mvm, "More than one managed interface active!\n");
8878c2ecf20Sopenharmony_ci		return ERR_PTR(-EINVAL);
8888c2ecf20Sopenharmony_ci	}
8898c2ecf20Sopenharmony_ci
8908c2ecf20Sopenharmony_ci	return bss_iter_data.vif;
8918c2ecf20Sopenharmony_ci}
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_cistruct iwl_sta_iter_data {
8948c2ecf20Sopenharmony_ci	bool assoc;
8958c2ecf20Sopenharmony_ci};
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_cistatic void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
8988c2ecf20Sopenharmony_ci				       struct ieee80211_vif *vif)
8998c2ecf20Sopenharmony_ci{
9008c2ecf20Sopenharmony_ci	struct iwl_sta_iter_data *data = _data;
9018c2ecf20Sopenharmony_ci
9028c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_STATION)
9038c2ecf20Sopenharmony_ci		return;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	if (vif->bss_conf.assoc)
9068c2ecf20Sopenharmony_ci		data->assoc = true;
9078c2ecf20Sopenharmony_ci}
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_cibool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
9108c2ecf20Sopenharmony_ci{
9118c2ecf20Sopenharmony_ci	struct iwl_sta_iter_data data = {
9128c2ecf20Sopenharmony_ci		.assoc = false,
9138c2ecf20Sopenharmony_ci	};
9148c2ecf20Sopenharmony_ci
9158c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
9168c2ecf20Sopenharmony_ci						   IEEE80211_IFACE_ITER_NORMAL,
9178c2ecf20Sopenharmony_ci						   iwl_mvm_sta_iface_iterator,
9188c2ecf20Sopenharmony_ci						   &data);
9198c2ecf20Sopenharmony_ci	return data.assoc;
9208c2ecf20Sopenharmony_ci}
9218c2ecf20Sopenharmony_ci
9228c2ecf20Sopenharmony_ciunsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
9238c2ecf20Sopenharmony_ci				    struct ieee80211_vif *vif,
9248c2ecf20Sopenharmony_ci				    bool tdls, bool cmd_q)
9258c2ecf20Sopenharmony_ci{
9268c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_tlv *trigger;
9278c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_txq_timer *txq_timer;
9288c2ecf20Sopenharmony_ci	unsigned int default_timeout = cmd_q ?
9298c2ecf20Sopenharmony_ci		IWL_DEF_WD_TIMEOUT :
9308c2ecf20Sopenharmony_ci		mvm->trans->trans_cfg->base_params->wd_timeout;
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	if (!iwl_fw_dbg_trigger_enabled(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS)) {
9338c2ecf20Sopenharmony_ci		/*
9348c2ecf20Sopenharmony_ci		 * We can't know when the station is asleep or awake, so we
9358c2ecf20Sopenharmony_ci		 * must disable the queue hang detection.
9368c2ecf20Sopenharmony_ci		 */
9378c2ecf20Sopenharmony_ci		if (fw_has_capa(&mvm->fw->ucode_capa,
9388c2ecf20Sopenharmony_ci				IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
9398c2ecf20Sopenharmony_ci		    vif && vif->type == NL80211_IFTYPE_AP)
9408c2ecf20Sopenharmony_ci			return IWL_WATCHDOG_DISABLED;
9418c2ecf20Sopenharmony_ci		return default_timeout;
9428c2ecf20Sopenharmony_ci	}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_ci	trigger = iwl_fw_dbg_get_trigger(mvm->fw, FW_DBG_TRIGGER_TXQ_TIMERS);
9458c2ecf20Sopenharmony_ci	txq_timer = (void *)trigger->data;
9468c2ecf20Sopenharmony_ci
9478c2ecf20Sopenharmony_ci	if (tdls)
9488c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->tdls);
9498c2ecf20Sopenharmony_ci
9508c2ecf20Sopenharmony_ci	if (cmd_q)
9518c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->command_queue);
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	if (WARN_ON(!vif))
9548c2ecf20Sopenharmony_ci		return default_timeout;
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci	switch (ieee80211_vif_type_p2p(vif)) {
9578c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_ADHOC:
9588c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->ibss);
9598c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_STATION:
9608c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->bss);
9618c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_AP:
9628c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->softap);
9638c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_CLIENT:
9648c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->p2p_client);
9658c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_GO:
9668c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->p2p_go);
9678c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_P2P_DEVICE:
9688c2ecf20Sopenharmony_ci		return le32_to_cpu(txq_timer->p2p_device);
9698c2ecf20Sopenharmony_ci	case NL80211_IFTYPE_MONITOR:
9708c2ecf20Sopenharmony_ci		return default_timeout;
9718c2ecf20Sopenharmony_ci	default:
9728c2ecf20Sopenharmony_ci		WARN_ON(1);
9738c2ecf20Sopenharmony_ci		return mvm->trans->trans_cfg->base_params->wd_timeout;
9748c2ecf20Sopenharmony_ci	}
9758c2ecf20Sopenharmony_ci}
9768c2ecf20Sopenharmony_ci
9778c2ecf20Sopenharmony_civoid iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
9788c2ecf20Sopenharmony_ci			     const char *errmsg)
9798c2ecf20Sopenharmony_ci{
9808c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_tlv *trig;
9818c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_mlme *trig_mlme;
9828c2ecf20Sopenharmony_ci
9838c2ecf20Sopenharmony_ci	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
9848c2ecf20Sopenharmony_ci				     FW_DBG_TRIGGER_MLME);
9858c2ecf20Sopenharmony_ci	if (!trig)
9868c2ecf20Sopenharmony_ci		goto out;
9878c2ecf20Sopenharmony_ci
9888c2ecf20Sopenharmony_ci	trig_mlme = (void *)trig->data;
9898c2ecf20Sopenharmony_ci
9908c2ecf20Sopenharmony_ci	if (trig_mlme->stop_connection_loss &&
9918c2ecf20Sopenharmony_ci	    --trig_mlme->stop_connection_loss)
9928c2ecf20Sopenharmony_ci		goto out;
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
9958c2ecf20Sopenharmony_ci
9968c2ecf20Sopenharmony_ciout:
9978c2ecf20Sopenharmony_ci	ieee80211_connection_loss(vif);
9988c2ecf20Sopenharmony_ci}
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_civoid iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
10018c2ecf20Sopenharmony_ci					  struct ieee80211_vif *vif,
10028c2ecf20Sopenharmony_ci					  const struct ieee80211_sta *sta,
10038c2ecf20Sopenharmony_ci					  u16 tid)
10048c2ecf20Sopenharmony_ci{
10058c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_tlv *trig;
10068c2ecf20Sopenharmony_ci	struct iwl_fw_dbg_trigger_ba *ba_trig;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
10098c2ecf20Sopenharmony_ci				     FW_DBG_TRIGGER_BA);
10108c2ecf20Sopenharmony_ci	if (!trig)
10118c2ecf20Sopenharmony_ci		return;
10128c2ecf20Sopenharmony_ci
10138c2ecf20Sopenharmony_ci	ba_trig = (void *)trig->data;
10148c2ecf20Sopenharmony_ci
10158c2ecf20Sopenharmony_ci	if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
10168c2ecf20Sopenharmony_ci		return;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
10198c2ecf20Sopenharmony_ci				"Frame from %pM timed out, tid %d",
10208c2ecf20Sopenharmony_ci				sta->addr, tid);
10218c2ecf20Sopenharmony_ci}
10228c2ecf20Sopenharmony_ci
10238c2ecf20Sopenharmony_ciu8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
10248c2ecf20Sopenharmony_ci{
10258c2ecf20Sopenharmony_ci	if (!elapsed)
10268c2ecf20Sopenharmony_ci		return 0;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	return (100 * airtime / elapsed) / USEC_PER_MSEC;
10298c2ecf20Sopenharmony_ci}
10308c2ecf20Sopenharmony_ci
10318c2ecf20Sopenharmony_cistatic enum iwl_mvm_traffic_load
10328c2ecf20Sopenharmony_ciiwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
10338c2ecf20Sopenharmony_ci{
10348c2ecf20Sopenharmony_ci	u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
10358c2ecf20Sopenharmony_ci
10368c2ecf20Sopenharmony_ci	if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
10378c2ecf20Sopenharmony_ci		return IWL_MVM_TRAFFIC_HIGH;
10388c2ecf20Sopenharmony_ci	if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
10398c2ecf20Sopenharmony_ci		return IWL_MVM_TRAFFIC_MEDIUM;
10408c2ecf20Sopenharmony_ci
10418c2ecf20Sopenharmony_ci	return IWL_MVM_TRAFFIC_LOW;
10428c2ecf20Sopenharmony_ci}
10438c2ecf20Sopenharmony_ci
10448c2ecf20Sopenharmony_cistruct iwl_mvm_tcm_iter_data {
10458c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
10468c2ecf20Sopenharmony_ci	bool any_sent;
10478c2ecf20Sopenharmony_ci};
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_cistatic void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
10508c2ecf20Sopenharmony_ci{
10518c2ecf20Sopenharmony_ci	struct iwl_mvm_tcm_iter_data *data = _data;
10528c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = data->mvm;
10538c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
10548c2ecf20Sopenharmony_ci	bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
10578c2ecf20Sopenharmony_ci		return;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	low_latency = mvm->tcm.result.low_latency[mvmvif->id];
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	if (!mvm->tcm.result.change[mvmvif->id] &&
10628c2ecf20Sopenharmony_ci	    prev == low_latency) {
10638c2ecf20Sopenharmony_ci		iwl_mvm_update_quotas(mvm, false, NULL);
10648c2ecf20Sopenharmony_ci		return;
10658c2ecf20Sopenharmony_ci	}
10668c2ecf20Sopenharmony_ci
10678c2ecf20Sopenharmony_ci	if (prev != low_latency) {
10688c2ecf20Sopenharmony_ci		/* this sends traffic load and updates quota as well */
10698c2ecf20Sopenharmony_ci		iwl_mvm_update_low_latency(mvm, vif, low_latency,
10708c2ecf20Sopenharmony_ci					   LOW_LATENCY_TRAFFIC);
10718c2ecf20Sopenharmony_ci	} else {
10728c2ecf20Sopenharmony_ci		iwl_mvm_update_quotas(mvm, false, NULL);
10738c2ecf20Sopenharmony_ci	}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_ci	data->any_sent = true;
10768c2ecf20Sopenharmony_ci}
10778c2ecf20Sopenharmony_ci
10788c2ecf20Sopenharmony_cistatic void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
10798c2ecf20Sopenharmony_ci{
10808c2ecf20Sopenharmony_ci	struct iwl_mvm_tcm_iter_data data = {
10818c2ecf20Sopenharmony_ci		.mvm = mvm,
10828c2ecf20Sopenharmony_ci		.any_sent = false,
10838c2ecf20Sopenharmony_ci	};
10848c2ecf20Sopenharmony_ci
10858c2ecf20Sopenharmony_ci	mutex_lock(&mvm->mutex);
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces(
10888c2ecf20Sopenharmony_ci		mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
10898c2ecf20Sopenharmony_ci		iwl_mvm_tcm_iter, &data);
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci	if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
10928c2ecf20Sopenharmony_ci		iwl_mvm_config_scan(mvm);
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	mutex_unlock(&mvm->mutex);
10958c2ecf20Sopenharmony_ci}
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_cistatic void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
10988c2ecf20Sopenharmony_ci{
10998c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm;
11008c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif;
11018c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_ci	mvmvif = container_of(wk, struct iwl_mvm_vif,
11048c2ecf20Sopenharmony_ci			      uapsd_nonagg_detected_wk.work);
11058c2ecf20Sopenharmony_ci	vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
11068c2ecf20Sopenharmony_ci	mvm = mvmvif->mvm;
11078c2ecf20Sopenharmony_ci
11088c2ecf20Sopenharmony_ci	if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
11098c2ecf20Sopenharmony_ci		return;
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	/* remember that this AP is broken */
11128c2ecf20Sopenharmony_ci	memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
11138c2ecf20Sopenharmony_ci	       vif->bss_conf.bssid, ETH_ALEN);
11148c2ecf20Sopenharmony_ci	mvm->uapsd_noagg_bssid_write_idx++;
11158c2ecf20Sopenharmony_ci	if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
11168c2ecf20Sopenharmony_ci		mvm->uapsd_noagg_bssid_write_idx = 0;
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_ci	iwl_mvm_connection_loss(mvm, vif,
11198c2ecf20Sopenharmony_ci				"AP isn't using AMPDU with uAPSD enabled");
11208c2ecf20Sopenharmony_ci}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_cistatic void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
11238c2ecf20Sopenharmony_ci					 struct ieee80211_vif *vif)
11248c2ecf20Sopenharmony_ci{
11258c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
11268c2ecf20Sopenharmony_ci
11278c2ecf20Sopenharmony_ci	if (vif->type != NL80211_IFTYPE_STATION)
11288c2ecf20Sopenharmony_ci		return;
11298c2ecf20Sopenharmony_ci
11308c2ecf20Sopenharmony_ci	if (!vif->bss_conf.assoc)
11318c2ecf20Sopenharmony_ci		return;
11328c2ecf20Sopenharmony_ci
11338c2ecf20Sopenharmony_ci	if (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd &&
11348c2ecf20Sopenharmony_ci	    !mvmvif->queue_params[IEEE80211_AC_VI].uapsd &&
11358c2ecf20Sopenharmony_ci	    !mvmvif->queue_params[IEEE80211_AC_BE].uapsd &&
11368c2ecf20Sopenharmony_ci	    !mvmvif->queue_params[IEEE80211_AC_BK].uapsd)
11378c2ecf20Sopenharmony_ci		return;
11388c2ecf20Sopenharmony_ci
11398c2ecf20Sopenharmony_ci	if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
11408c2ecf20Sopenharmony_ci		return;
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
11438c2ecf20Sopenharmony_ci	IWL_INFO(mvm,
11448c2ecf20Sopenharmony_ci		 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
11458c2ecf20Sopenharmony_ci	schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk, 15 * HZ);
11468c2ecf20Sopenharmony_ci}
11478c2ecf20Sopenharmony_ci
11488c2ecf20Sopenharmony_cistatic void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
11498c2ecf20Sopenharmony_ci						 unsigned int elapsed,
11508c2ecf20Sopenharmony_ci						 int mac)
11518c2ecf20Sopenharmony_ci{
11528c2ecf20Sopenharmony_ci	u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
11538c2ecf20Sopenharmony_ci	u64 tpt;
11548c2ecf20Sopenharmony_ci	unsigned long rate;
11558c2ecf20Sopenharmony_ci	struct ieee80211_vif *vif;
11568c2ecf20Sopenharmony_ci
11578c2ecf20Sopenharmony_ci	rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
11608c2ecf20Sopenharmony_ci	    mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
11618c2ecf20Sopenharmony_ci		return;
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	if (iwl_mvm_has_new_rx_api(mvm)) {
11648c2ecf20Sopenharmony_ci		tpt = 8 * bytes; /* kbps */
11658c2ecf20Sopenharmony_ci		do_div(tpt, elapsed);
11668c2ecf20Sopenharmony_ci		rate *= 1000; /* kbps */
11678c2ecf20Sopenharmony_ci		if (tpt < 22 * rate / 100)
11688c2ecf20Sopenharmony_ci			return;
11698c2ecf20Sopenharmony_ci	} else {
11708c2ecf20Sopenharmony_ci		/*
11718c2ecf20Sopenharmony_ci		 * the rate here is actually the threshold, in 100Kbps units,
11728c2ecf20Sopenharmony_ci		 * so do the needed conversion from bytes to 100Kbps:
11738c2ecf20Sopenharmony_ci		 * 100kb = bits / (100 * 1000),
11748c2ecf20Sopenharmony_ci		 * 100kbps = 100kb / (msecs / 1000) ==
11758c2ecf20Sopenharmony_ci		 *           (bits / (100 * 1000)) / (msecs / 1000) ==
11768c2ecf20Sopenharmony_ci		 *           bits / (100 * msecs)
11778c2ecf20Sopenharmony_ci		 */
11788c2ecf20Sopenharmony_ci		tpt = (8 * bytes);
11798c2ecf20Sopenharmony_ci		do_div(tpt, elapsed * 100);
11808c2ecf20Sopenharmony_ci		if (tpt < rate)
11818c2ecf20Sopenharmony_ci			return;
11828c2ecf20Sopenharmony_ci	}
11838c2ecf20Sopenharmony_ci
11848c2ecf20Sopenharmony_ci	rcu_read_lock();
11858c2ecf20Sopenharmony_ci	vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
11868c2ecf20Sopenharmony_ci	if (vif)
11878c2ecf20Sopenharmony_ci		iwl_mvm_uapsd_agg_disconnect(mvm, vif);
11888c2ecf20Sopenharmony_ci	rcu_read_unlock();
11898c2ecf20Sopenharmony_ci}
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_cistatic void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
11928c2ecf20Sopenharmony_ci				 struct ieee80211_vif *vif)
11938c2ecf20Sopenharmony_ci{
11948c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
11958c2ecf20Sopenharmony_ci	u32 *band = _data;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	if (!mvmvif->phy_ctxt)
11988c2ecf20Sopenharmony_ci		return;
11998c2ecf20Sopenharmony_ci
12008c2ecf20Sopenharmony_ci	band[mvmvif->id] = mvmvif->phy_ctxt->channel->band;
12018c2ecf20Sopenharmony_ci}
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cistatic unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
12048c2ecf20Sopenharmony_ci					    unsigned long ts,
12058c2ecf20Sopenharmony_ci					    bool handle_uapsd)
12068c2ecf20Sopenharmony_ci{
12078c2ecf20Sopenharmony_ci	unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
12088c2ecf20Sopenharmony_ci	unsigned int uapsd_elapsed =
12098c2ecf20Sopenharmony_ci		jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
12108c2ecf20Sopenharmony_ci	u32 total_airtime = 0;
12118c2ecf20Sopenharmony_ci	u32 band_airtime[NUM_NL80211_BANDS] = {0};
12128c2ecf20Sopenharmony_ci	u32 band[NUM_MAC_INDEX_DRIVER] = {0};
12138c2ecf20Sopenharmony_ci	int ac, mac, i;
12148c2ecf20Sopenharmony_ci	bool low_latency = false;
12158c2ecf20Sopenharmony_ci	enum iwl_mvm_traffic_load load, band_load;
12168c2ecf20Sopenharmony_ci	bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
12178c2ecf20Sopenharmony_ci
12188c2ecf20Sopenharmony_ci	if (handle_ll)
12198c2ecf20Sopenharmony_ci		mvm->tcm.ll_ts = ts;
12208c2ecf20Sopenharmony_ci	if (handle_uapsd)
12218c2ecf20Sopenharmony_ci		mvm->tcm.uapsd_nonagg_ts = ts;
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	mvm->tcm.result.elapsed = elapsed;
12248c2ecf20Sopenharmony_ci
12258c2ecf20Sopenharmony_ci	ieee80211_iterate_active_interfaces_atomic(mvm->hw,
12268c2ecf20Sopenharmony_ci						   IEEE80211_IFACE_ITER_NORMAL,
12278c2ecf20Sopenharmony_ci						   iwl_mvm_tcm_iterator,
12288c2ecf20Sopenharmony_ci						   &band);
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
12318c2ecf20Sopenharmony_ci		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
12328c2ecf20Sopenharmony_ci		u32 vo_vi_pkts = 0;
12338c2ecf20Sopenharmony_ci		u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_ci		total_airtime += airtime;
12368c2ecf20Sopenharmony_ci		band_airtime[band[mac]] += airtime;
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci		load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
12398c2ecf20Sopenharmony_ci		mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
12408c2ecf20Sopenharmony_ci		mvm->tcm.result.load[mac] = load;
12418c2ecf20Sopenharmony_ci		mvm->tcm.result.airtime[mac] = airtime;
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci		for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
12448c2ecf20Sopenharmony_ci			vo_vi_pkts += mdata->rx.pkts[ac] +
12458c2ecf20Sopenharmony_ci				      mdata->tx.pkts[ac];
12468c2ecf20Sopenharmony_ci
12478c2ecf20Sopenharmony_ci		/* enable immediately with enough packets but defer disabling */
12488c2ecf20Sopenharmony_ci		if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
12498c2ecf20Sopenharmony_ci			mvm->tcm.result.low_latency[mac] = true;
12508c2ecf20Sopenharmony_ci		else if (handle_ll)
12518c2ecf20Sopenharmony_ci			mvm->tcm.result.low_latency[mac] = false;
12528c2ecf20Sopenharmony_ci
12538c2ecf20Sopenharmony_ci		if (handle_ll) {
12548c2ecf20Sopenharmony_ci			/* clear old data */
12558c2ecf20Sopenharmony_ci			memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
12568c2ecf20Sopenharmony_ci			memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
12578c2ecf20Sopenharmony_ci		}
12588c2ecf20Sopenharmony_ci		low_latency |= mvm->tcm.result.low_latency[mac];
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci		if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
12618c2ecf20Sopenharmony_ci			iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
12628c2ecf20Sopenharmony_ci							     mac);
12638c2ecf20Sopenharmony_ci		/* clear old data */
12648c2ecf20Sopenharmony_ci		if (handle_uapsd)
12658c2ecf20Sopenharmony_ci			mdata->uapsd_nonagg_detect.rx_bytes = 0;
12668c2ecf20Sopenharmony_ci		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
12678c2ecf20Sopenharmony_ci		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
12688c2ecf20Sopenharmony_ci	}
12698c2ecf20Sopenharmony_ci
12708c2ecf20Sopenharmony_ci	load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
12718c2ecf20Sopenharmony_ci	mvm->tcm.result.global_change = load != mvm->tcm.result.global_load;
12728c2ecf20Sopenharmony_ci	mvm->tcm.result.global_load = load;
12738c2ecf20Sopenharmony_ci
12748c2ecf20Sopenharmony_ci	for (i = 0; i < NUM_NL80211_BANDS; i++) {
12758c2ecf20Sopenharmony_ci		band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
12768c2ecf20Sopenharmony_ci		mvm->tcm.result.band_load[i] = band_load;
12778c2ecf20Sopenharmony_ci	}
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_ci	/*
12808c2ecf20Sopenharmony_ci	 * If the current load isn't low we need to force re-evaluation
12818c2ecf20Sopenharmony_ci	 * in the TCM period, so that we can return to low load if there
12828c2ecf20Sopenharmony_ci	 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
12838c2ecf20Sopenharmony_ci	 * triggered by traffic).
12848c2ecf20Sopenharmony_ci	 */
12858c2ecf20Sopenharmony_ci	if (load != IWL_MVM_TRAFFIC_LOW)
12868c2ecf20Sopenharmony_ci		return MVM_TCM_PERIOD;
12878c2ecf20Sopenharmony_ci	/*
12888c2ecf20Sopenharmony_ci	 * If low-latency is active we need to force re-evaluation after
12898c2ecf20Sopenharmony_ci	 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
12908c2ecf20Sopenharmony_ci	 * when there's no traffic at all.
12918c2ecf20Sopenharmony_ci	 */
12928c2ecf20Sopenharmony_ci	if (low_latency)
12938c2ecf20Sopenharmony_ci		return MVM_LL_PERIOD;
12948c2ecf20Sopenharmony_ci	/*
12958c2ecf20Sopenharmony_ci	 * Otherwise, we don't need to run the work struct because we're
12968c2ecf20Sopenharmony_ci	 * in the default "idle" state - traffic indication is low (which
12978c2ecf20Sopenharmony_ci	 * also covers the "no traffic" case) and low-latency is disabled
12988c2ecf20Sopenharmony_ci	 * so there's no state that may need to be disabled when there's
12998c2ecf20Sopenharmony_ci	 * no traffic at all.
13008c2ecf20Sopenharmony_ci	 *
13018c2ecf20Sopenharmony_ci	 * Note that this has no impact on the regular scheduling of the
13028c2ecf20Sopenharmony_ci	 * updates triggered by traffic - those happen whenever one of the
13038c2ecf20Sopenharmony_ci	 * two timeouts expire (if there's traffic at all.)
13048c2ecf20Sopenharmony_ci	 */
13058c2ecf20Sopenharmony_ci	return 0;
13068c2ecf20Sopenharmony_ci}
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_civoid iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
13098c2ecf20Sopenharmony_ci{
13108c2ecf20Sopenharmony_ci	unsigned long ts = jiffies;
13118c2ecf20Sopenharmony_ci	bool handle_uapsd =
13128c2ecf20Sopenharmony_ci		time_after(ts, mvm->tcm.uapsd_nonagg_ts +
13138c2ecf20Sopenharmony_ci			       msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
13148c2ecf20Sopenharmony_ci
13158c2ecf20Sopenharmony_ci	spin_lock(&mvm->tcm.lock);
13168c2ecf20Sopenharmony_ci	if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
13178c2ecf20Sopenharmony_ci		spin_unlock(&mvm->tcm.lock);
13188c2ecf20Sopenharmony_ci		return;
13198c2ecf20Sopenharmony_ci	}
13208c2ecf20Sopenharmony_ci	spin_unlock(&mvm->tcm.lock);
13218c2ecf20Sopenharmony_ci
13228c2ecf20Sopenharmony_ci	if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
13238c2ecf20Sopenharmony_ci		mutex_lock(&mvm->mutex);
13248c2ecf20Sopenharmony_ci		if (iwl_mvm_request_statistics(mvm, true))
13258c2ecf20Sopenharmony_ci			handle_uapsd = false;
13268c2ecf20Sopenharmony_ci		mutex_unlock(&mvm->mutex);
13278c2ecf20Sopenharmony_ci	}
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	spin_lock(&mvm->tcm.lock);
13308c2ecf20Sopenharmony_ci	/* re-check if somebody else won the recheck race */
13318c2ecf20Sopenharmony_ci	if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
13328c2ecf20Sopenharmony_ci		/* calculate statistics */
13338c2ecf20Sopenharmony_ci		unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
13348c2ecf20Sopenharmony_ci								  handle_uapsd);
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_ci		/* the memset needs to be visible before the timestamp */
13378c2ecf20Sopenharmony_ci		smp_mb();
13388c2ecf20Sopenharmony_ci		mvm->tcm.ts = ts;
13398c2ecf20Sopenharmony_ci		if (work_delay)
13408c2ecf20Sopenharmony_ci			schedule_delayed_work(&mvm->tcm.work, work_delay);
13418c2ecf20Sopenharmony_ci	}
13428c2ecf20Sopenharmony_ci	spin_unlock(&mvm->tcm.lock);
13438c2ecf20Sopenharmony_ci
13448c2ecf20Sopenharmony_ci	iwl_mvm_tcm_results(mvm);
13458c2ecf20Sopenharmony_ci}
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_civoid iwl_mvm_tcm_work(struct work_struct *work)
13488c2ecf20Sopenharmony_ci{
13498c2ecf20Sopenharmony_ci	struct delayed_work *delayed_work = to_delayed_work(work);
13508c2ecf20Sopenharmony_ci	struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
13518c2ecf20Sopenharmony_ci					   tcm.work);
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	iwl_mvm_recalc_tcm(mvm);
13548c2ecf20Sopenharmony_ci}
13558c2ecf20Sopenharmony_ci
13568c2ecf20Sopenharmony_civoid iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
13578c2ecf20Sopenharmony_ci{
13588c2ecf20Sopenharmony_ci	spin_lock_bh(&mvm->tcm.lock);
13598c2ecf20Sopenharmony_ci	mvm->tcm.paused = true;
13608c2ecf20Sopenharmony_ci	spin_unlock_bh(&mvm->tcm.lock);
13618c2ecf20Sopenharmony_ci	if (with_cancel)
13628c2ecf20Sopenharmony_ci		cancel_delayed_work_sync(&mvm->tcm.work);
13638c2ecf20Sopenharmony_ci}
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_civoid iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
13668c2ecf20Sopenharmony_ci{
13678c2ecf20Sopenharmony_ci	int mac;
13688c2ecf20Sopenharmony_ci	bool low_latency = false;
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	spin_lock_bh(&mvm->tcm.lock);
13718c2ecf20Sopenharmony_ci	mvm->tcm.ts = jiffies;
13728c2ecf20Sopenharmony_ci	mvm->tcm.ll_ts = jiffies;
13738c2ecf20Sopenharmony_ci	for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
13748c2ecf20Sopenharmony_ci		struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
13758c2ecf20Sopenharmony_ci
13768c2ecf20Sopenharmony_ci		memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
13778c2ecf20Sopenharmony_ci		memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
13788c2ecf20Sopenharmony_ci		memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
13798c2ecf20Sopenharmony_ci		memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ci		if (mvm->tcm.result.low_latency[mac])
13828c2ecf20Sopenharmony_ci			low_latency = true;
13838c2ecf20Sopenharmony_ci	}
13848c2ecf20Sopenharmony_ci	/* The TCM data needs to be reset before "paused" flag changes */
13858c2ecf20Sopenharmony_ci	smp_mb();
13868c2ecf20Sopenharmony_ci	mvm->tcm.paused = false;
13878c2ecf20Sopenharmony_ci
13888c2ecf20Sopenharmony_ci	/*
13898c2ecf20Sopenharmony_ci	 * if the current load is not low or low latency is active, force
13908c2ecf20Sopenharmony_ci	 * re-evaluation to cover the case of no traffic.
13918c2ecf20Sopenharmony_ci	 */
13928c2ecf20Sopenharmony_ci	if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
13938c2ecf20Sopenharmony_ci		schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
13948c2ecf20Sopenharmony_ci	else if (low_latency)
13958c2ecf20Sopenharmony_ci		schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	spin_unlock_bh(&mvm->tcm.lock);
13988c2ecf20Sopenharmony_ci}
13998c2ecf20Sopenharmony_ci
14008c2ecf20Sopenharmony_civoid iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
14018c2ecf20Sopenharmony_ci{
14028c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_ci	INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
14058c2ecf20Sopenharmony_ci			  iwl_mvm_tcm_uapsd_nonagg_detected_wk);
14068c2ecf20Sopenharmony_ci}
14078c2ecf20Sopenharmony_ci
14088c2ecf20Sopenharmony_civoid iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
14098c2ecf20Sopenharmony_ci{
14108c2ecf20Sopenharmony_ci	struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_ci	cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
14138c2ecf20Sopenharmony_ci}
14148c2ecf20Sopenharmony_ci
14158c2ecf20Sopenharmony_ciu32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
14168c2ecf20Sopenharmony_ci{
14178c2ecf20Sopenharmony_ci	u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
14188c2ecf20Sopenharmony_ci
14198c2ecf20Sopenharmony_ci	if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
14208c2ecf20Sopenharmony_ci	    mvm->trans->cfg->gp2_reg_addr)
14218c2ecf20Sopenharmony_ci		reg_addr = mvm->trans->cfg->gp2_reg_addr;
14228c2ecf20Sopenharmony_ci
14238c2ecf20Sopenharmony_ci	return iwl_read_prph(mvm->trans, reg_addr);
14248c2ecf20Sopenharmony_ci}
14258c2ecf20Sopenharmony_ci
14268c2ecf20Sopenharmony_civoid iwl_mvm_get_sync_time(struct iwl_mvm *mvm, u32 *gp2, u64 *boottime)
14278c2ecf20Sopenharmony_ci{
14288c2ecf20Sopenharmony_ci	bool ps_disabled;
14298c2ecf20Sopenharmony_ci
14308c2ecf20Sopenharmony_ci	lockdep_assert_held(&mvm->mutex);
14318c2ecf20Sopenharmony_ci
14328c2ecf20Sopenharmony_ci	/* Disable power save when reading GP2 */
14338c2ecf20Sopenharmony_ci	ps_disabled = mvm->ps_disabled;
14348c2ecf20Sopenharmony_ci	if (!ps_disabled) {
14358c2ecf20Sopenharmony_ci		mvm->ps_disabled = true;
14368c2ecf20Sopenharmony_ci		iwl_mvm_power_update_device(mvm);
14378c2ecf20Sopenharmony_ci	}
14388c2ecf20Sopenharmony_ci
14398c2ecf20Sopenharmony_ci	*gp2 = iwl_mvm_get_systime(mvm);
14408c2ecf20Sopenharmony_ci	*boottime = ktime_get_boottime_ns();
14418c2ecf20Sopenharmony_ci
14428c2ecf20Sopenharmony_ci	if (!ps_disabled) {
14438c2ecf20Sopenharmony_ci		mvm->ps_disabled = ps_disabled;
14448c2ecf20Sopenharmony_ci		iwl_mvm_power_update_device(mvm);
14458c2ecf20Sopenharmony_ci	}
14468c2ecf20Sopenharmony_ci}
1447