18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright(c) 2009-2012  Realtek Corporation.*/
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include "wifi.h"
58c2ecf20Sopenharmony_ci#include "stats.h"
68c2ecf20Sopenharmony_ci#include <linux/export.h>
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ciu8 rtl_query_rxpwrpercentage(s8 antpower)
98c2ecf20Sopenharmony_ci{
108c2ecf20Sopenharmony_ci	if ((antpower <= -100) || (antpower >= 20))
118c2ecf20Sopenharmony_ci		return 0;
128c2ecf20Sopenharmony_ci	else if (antpower >= 0)
138c2ecf20Sopenharmony_ci		return 100;
148c2ecf20Sopenharmony_ci	else
158c2ecf20Sopenharmony_ci		return 100 + antpower;
168c2ecf20Sopenharmony_ci}
178c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtl_query_rxpwrpercentage);
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ciu8 rtl_evm_db_to_percentage(s8 value)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	s8 ret_val = clamp(-value, 0, 33) * 3;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	if (ret_val == 99)
248c2ecf20Sopenharmony_ci		ret_val = 100;
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci	return ret_val;
278c2ecf20Sopenharmony_ci}
288c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtl_evm_db_to_percentage);
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistatic long rtl_translate_todbm(struct ieee80211_hw *hw,
318c2ecf20Sopenharmony_ci			 u8 signal_strength_index)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	long signal_power;
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci	signal_power = (long)((signal_strength_index + 1) >> 1);
368c2ecf20Sopenharmony_ci	signal_power -= 95;
378c2ecf20Sopenharmony_ci	return signal_power;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cilong rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
418c2ecf20Sopenharmony_ci{
428c2ecf20Sopenharmony_ci	long retsig;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	if (currsig >= 61 && currsig <= 100)
458c2ecf20Sopenharmony_ci		retsig = 90 + ((currsig - 60) / 4);
468c2ecf20Sopenharmony_ci	else if (currsig >= 41 && currsig <= 60)
478c2ecf20Sopenharmony_ci		retsig = 78 + ((currsig - 40) / 2);
488c2ecf20Sopenharmony_ci	else if (currsig >= 31 && currsig <= 40)
498c2ecf20Sopenharmony_ci		retsig = 66 + (currsig - 30);
508c2ecf20Sopenharmony_ci	else if (currsig >= 21 && currsig <= 30)
518c2ecf20Sopenharmony_ci		retsig = 54 + (currsig - 20);
528c2ecf20Sopenharmony_ci	else if (currsig >= 5 && currsig <= 20)
538c2ecf20Sopenharmony_ci		retsig = 42 + (((currsig - 5) * 2) / 3);
548c2ecf20Sopenharmony_ci	else if (currsig == 4)
558c2ecf20Sopenharmony_ci		retsig = 36;
568c2ecf20Sopenharmony_ci	else if (currsig == 3)
578c2ecf20Sopenharmony_ci		retsig = 27;
588c2ecf20Sopenharmony_ci	else if (currsig == 2)
598c2ecf20Sopenharmony_ci		retsig = 18;
608c2ecf20Sopenharmony_ci	else if (currsig == 1)
618c2ecf20Sopenharmony_ci		retsig = 9;
628c2ecf20Sopenharmony_ci	else
638c2ecf20Sopenharmony_ci		retsig = currsig;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	return retsig;
668c2ecf20Sopenharmony_ci}
678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtl_signal_scale_mapping);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic void rtl_process_ui_rssi(struct ieee80211_hw *hw,
708c2ecf20Sopenharmony_ci				struct rtl_stats *pstatus)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
738c2ecf20Sopenharmony_ci	struct rtl_phy *rtlphy = &(rtlpriv->phy);
748c2ecf20Sopenharmony_ci	u8 rfpath;
758c2ecf20Sopenharmony_ci	u32 last_rssi, tmpval;
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	if (!pstatus->packet_toself && !pstatus->packet_beacon)
788c2ecf20Sopenharmony_ci		return;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	rtlpriv->stats.pwdb_all_cnt += pstatus->rx_pwdb_all;
818c2ecf20Sopenharmony_ci	rtlpriv->stats.rssi_calculate_cnt++;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
848c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
858c2ecf20Sopenharmony_ci		last_rssi = rtlpriv->stats.ui_rssi.elements[
868c2ecf20Sopenharmony_ci			rtlpriv->stats.ui_rssi.index];
878c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_rssi.total_val -= last_rssi;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci	rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
908c2ecf20Sopenharmony_ci	rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
918c2ecf20Sopenharmony_ci	    pstatus->signalstrength;
928c2ecf20Sopenharmony_ci	if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
938c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_rssi.index = 0;
948c2ecf20Sopenharmony_ci	tmpval = rtlpriv->stats.ui_rssi.total_val /
958c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_rssi.total_num;
968c2ecf20Sopenharmony_ci	rtlpriv->stats.signal_strength = rtl_translate_todbm(hw,
978c2ecf20Sopenharmony_ci		(u8) tmpval);
988c2ecf20Sopenharmony_ci	pstatus->rssi = rtlpriv->stats.signal_strength;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (pstatus->is_cck)
1018c2ecf20Sopenharmony_ci		return;
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
1048c2ecf20Sopenharmony_ci	     rfpath++) {
1058c2ecf20Sopenharmony_ci		if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
1068c2ecf20Sopenharmony_ci			rtlpriv->stats.rx_rssi_percentage[rfpath] =
1078c2ecf20Sopenharmony_ci			    pstatus->rx_mimo_signalstrength[rfpath];
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		}
1108c2ecf20Sopenharmony_ci		if (pstatus->rx_mimo_signalstrength[rfpath] >
1118c2ecf20Sopenharmony_ci		    rtlpriv->stats.rx_rssi_percentage[rfpath]) {
1128c2ecf20Sopenharmony_ci			rtlpriv->stats.rx_rssi_percentage[rfpath] =
1138c2ecf20Sopenharmony_ci			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
1148c2ecf20Sopenharmony_ci			      (RX_SMOOTH_FACTOR - 1)) +
1158c2ecf20Sopenharmony_ci			     (pstatus->rx_mimo_signalstrength[rfpath])) /
1168c2ecf20Sopenharmony_ci			    (RX_SMOOTH_FACTOR);
1178c2ecf20Sopenharmony_ci			rtlpriv->stats.rx_rssi_percentage[rfpath] =
1188c2ecf20Sopenharmony_ci			    rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
1198c2ecf20Sopenharmony_ci		} else {
1208c2ecf20Sopenharmony_ci			rtlpriv->stats.rx_rssi_percentage[rfpath] =
1218c2ecf20Sopenharmony_ci			    ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
1228c2ecf20Sopenharmony_ci			      (RX_SMOOTH_FACTOR - 1)) +
1238c2ecf20Sopenharmony_ci			     (pstatus->rx_mimo_signalstrength[rfpath])) /
1248c2ecf20Sopenharmony_ci			    (RX_SMOOTH_FACTOR);
1258c2ecf20Sopenharmony_ci		}
1268c2ecf20Sopenharmony_ci		rtlpriv->stats.rx_snr_db[rfpath] = pstatus->rx_snr[rfpath];
1278c2ecf20Sopenharmony_ci		rtlpriv->stats.rx_evm_dbm[rfpath] =
1288c2ecf20Sopenharmony_ci					pstatus->rx_mimo_evm_dbm[rfpath];
1298c2ecf20Sopenharmony_ci		rtlpriv->stats.rx_cfo_short[rfpath] =
1308c2ecf20Sopenharmony_ci					pstatus->cfo_short[rfpath];
1318c2ecf20Sopenharmony_ci		rtlpriv->stats.rx_cfo_tail[rfpath] = pstatus->cfo_tail[rfpath];
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
1368c2ecf20Sopenharmony_ci					  struct rtl_stats *pstatus)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1398c2ecf20Sopenharmony_ci	int weighting = 0;
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci	if (rtlpriv->stats.recv_signal_power == 0)
1428c2ecf20Sopenharmony_ci		rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
1438c2ecf20Sopenharmony_ci	if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
1448c2ecf20Sopenharmony_ci		weighting = 5;
1458c2ecf20Sopenharmony_ci	else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
1468c2ecf20Sopenharmony_ci		weighting = (-5);
1478c2ecf20Sopenharmony_ci	rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
1488c2ecf20Sopenharmony_ci		5 + pstatus->recvsignalpower + weighting) / 6;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_cistatic void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
1528c2ecf20Sopenharmony_ci{
1538c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1548c2ecf20Sopenharmony_ci	struct rtl_sta_info *drv_priv = NULL;
1558c2ecf20Sopenharmony_ci	struct ieee80211_sta *sta = NULL;
1568c2ecf20Sopenharmony_ci	long undec_sm_pwdb;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	rcu_read_lock();
1598c2ecf20Sopenharmony_ci	if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
1608c2ecf20Sopenharmony_ci		sta = rtl_find_sta(hw, pstatus->psaddr);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	/* adhoc or ap mode */
1638c2ecf20Sopenharmony_ci	if (sta) {
1648c2ecf20Sopenharmony_ci		drv_priv = (struct rtl_sta_info *) sta->drv_priv;
1658c2ecf20Sopenharmony_ci		undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
1668c2ecf20Sopenharmony_ci	} else {
1678c2ecf20Sopenharmony_ci		undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	if (undec_sm_pwdb < 0)
1718c2ecf20Sopenharmony_ci		undec_sm_pwdb = pstatus->rx_pwdb_all;
1728c2ecf20Sopenharmony_ci	if (pstatus->rx_pwdb_all > (u32) undec_sm_pwdb) {
1738c2ecf20Sopenharmony_ci		undec_sm_pwdb = (((undec_sm_pwdb) *
1748c2ecf20Sopenharmony_ci		      (RX_SMOOTH_FACTOR - 1)) +
1758c2ecf20Sopenharmony_ci		     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
1768c2ecf20Sopenharmony_ci		undec_sm_pwdb = undec_sm_pwdb + 1;
1778c2ecf20Sopenharmony_ci	} else {
1788c2ecf20Sopenharmony_ci		undec_sm_pwdb = (((undec_sm_pwdb) *
1798c2ecf20Sopenharmony_ci		      (RX_SMOOTH_FACTOR - 1)) +
1808c2ecf20Sopenharmony_ci		     (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
1818c2ecf20Sopenharmony_ci	}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	if (sta) {
1848c2ecf20Sopenharmony_ci		drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb;
1858c2ecf20Sopenharmony_ci	} else {
1868c2ecf20Sopenharmony_ci		rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	rcu_read_unlock();
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	rtl_update_rxsignalstatistics(hw, pstatus);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void rtl_process_ui_link_quality(struct ieee80211_hw *hw,
1948c2ecf20Sopenharmony_ci					struct rtl_stats *pstatus)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct rtl_priv *rtlpriv = rtl_priv(hw);
1978c2ecf20Sopenharmony_ci	u32 last_evm, n_stream, tmpval;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	if (pstatus->signalquality == 0)
2008c2ecf20Sopenharmony_ci		return;
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	if (rtlpriv->stats.ui_link_quality.total_num++ >=
2038c2ecf20Sopenharmony_ci	    PHY_LINKQUALITY_SLID_WIN_MAX) {
2048c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_link_quality.total_num =
2058c2ecf20Sopenharmony_ci		    PHY_LINKQUALITY_SLID_WIN_MAX;
2068c2ecf20Sopenharmony_ci		last_evm = rtlpriv->stats.ui_link_quality.elements[
2078c2ecf20Sopenharmony_ci			rtlpriv->stats.ui_link_quality.index];
2088c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_link_quality.total_val -= last_evm;
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci	rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality;
2118c2ecf20Sopenharmony_ci	rtlpriv->stats.ui_link_quality.elements[
2128c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_link_quality.index++] =
2138c2ecf20Sopenharmony_ci							pstatus->signalquality;
2148c2ecf20Sopenharmony_ci	if (rtlpriv->stats.ui_link_quality.index >=
2158c2ecf20Sopenharmony_ci	    PHY_LINKQUALITY_SLID_WIN_MAX)
2168c2ecf20Sopenharmony_ci		rtlpriv->stats.ui_link_quality.index = 0;
2178c2ecf20Sopenharmony_ci	tmpval = rtlpriv->stats.ui_link_quality.total_val /
2188c2ecf20Sopenharmony_ci	    rtlpriv->stats.ui_link_quality.total_num;
2198c2ecf20Sopenharmony_ci	rtlpriv->stats.signal_quality = tmpval;
2208c2ecf20Sopenharmony_ci	rtlpriv->stats.last_sigstrength_inpercent = tmpval;
2218c2ecf20Sopenharmony_ci	for (n_stream = 0; n_stream < 2; n_stream++) {
2228c2ecf20Sopenharmony_ci		if (pstatus->rx_mimo_sig_qual[n_stream] != -1) {
2238c2ecf20Sopenharmony_ci			if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) {
2248c2ecf20Sopenharmony_ci				rtlpriv->stats.rx_evm_percentage[n_stream] =
2258c2ecf20Sopenharmony_ci				    pstatus->rx_mimo_sig_qual[n_stream];
2268c2ecf20Sopenharmony_ci			}
2278c2ecf20Sopenharmony_ci			rtlpriv->stats.rx_evm_percentage[n_stream] =
2288c2ecf20Sopenharmony_ci			    ((rtlpriv->stats.rx_evm_percentage[n_stream]
2298c2ecf20Sopenharmony_ci			      * (RX_SMOOTH_FACTOR - 1)) +
2308c2ecf20Sopenharmony_ci			     (pstatus->rx_mimo_sig_qual[n_stream] * 1)) /
2318c2ecf20Sopenharmony_ci			    (RX_SMOOTH_FACTOR);
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_civoid rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer,
2378c2ecf20Sopenharmony_ci			 struct rtl_stats *pstatus)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	if (!pstatus->packet_matchbssid)
2418c2ecf20Sopenharmony_ci		return;
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci	rtl_process_ui_rssi(hw, pstatus);
2448c2ecf20Sopenharmony_ci	rtl_process_pwdb(hw, pstatus);
2458c2ecf20Sopenharmony_ci	rtl_process_ui_link_quality(hw, pstatus);
2468c2ecf20Sopenharmony_ci}
2478c2ecf20Sopenharmony_ciEXPORT_SYMBOL(rtl_process_phyinfo);
248