stats.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: GPL-2.0
  2. /******************************************************************************
  3. *
  4. * Copyright(c) 2009-2012 Realtek Corporation.
  5. *
  6. * Contact Information:
  7. * wlanfae <wlanfae@realtek.com>
  8. * Realtek Corporation, No. 2, Innovation Road II, Hsinchu Science Park,
  9. * Hsinchu 300, Taiwan.
  10. *
  11. * Larry Finger <Larry.Finger@lwfinger.net>
  12. *
  13. *****************************************************************************/
  14. #include "wifi.h"
  15. #include "stats.h"
  16. #include <linux/export.h>
  17. u8 rtl_query_rxpwrpercentage(s8 antpower)
  18. {
  19. if ((antpower <= -100) || (antpower >= 20))
  20. return 0;
  21. else if (antpower >= 0)
  22. return 100;
  23. else
  24. return 100 + antpower;
  25. }
  26. u8 rtl_evm_db_to_percentage(s8 value)
  27. {
  28. s8 ret_val = clamp(-value, 0, 33) * 3;
  29. if (ret_val == 99)
  30. ret_val = 100;
  31. return ret_val;
  32. }
  33. static long rtl_translate_todbm(struct ieee80211_hw *hw,
  34. u8 signal_strength_index)
  35. {
  36. long signal_power;
  37. signal_power = (long)((signal_strength_index + 1) >> 1);
  38. signal_power -= 95;
  39. return signal_power;
  40. }
  41. long rtl_signal_scale_mapping(struct ieee80211_hw *hw, long currsig)
  42. {
  43. long retsig;
  44. if (currsig >= 61 && currsig <= 100)
  45. retsig = 90 + ((currsig - 60) / 4);
  46. else if (currsig >= 41 && currsig <= 60)
  47. retsig = 78 + ((currsig - 40) / 2);
  48. else if (currsig >= 31 && currsig <= 40)
  49. retsig = 66 + (currsig - 30);
  50. else if (currsig >= 21 && currsig <= 30)
  51. retsig = 54 + (currsig - 20);
  52. else if (currsig >= 5 && currsig <= 20)
  53. retsig = 42 + (((currsig - 5) * 2) / 3);
  54. else if (currsig == 4)
  55. retsig = 36;
  56. else if (currsig == 3)
  57. retsig = 27;
  58. else if (currsig == 2)
  59. retsig = 18;
  60. else if (currsig == 1)
  61. retsig = 9;
  62. else
  63. retsig = currsig;
  64. return retsig;
  65. }
  66. static void rtl_process_ui_rssi(struct ieee80211_hw *hw,
  67. struct rtl_stats *pstatus)
  68. {
  69. struct rtl_priv *rtlpriv = rtl_priv(hw);
  70. struct rtl_phy *rtlphy = &rtlpriv->phy;
  71. u8 rfpath;
  72. u32 last_rssi, tmpval;
  73. if (!pstatus->packet_toself && !pstatus->packet_beacon)
  74. return;
  75. rtlpriv->stats.pwdb_all_cnt += pstatus->rx_pwdb_all;
  76. rtlpriv->stats.rssi_calculate_cnt++;
  77. if (rtlpriv->stats.ui_rssi.total_num++ >= PHY_RSSI_SLID_WIN_MAX) {
  78. rtlpriv->stats.ui_rssi.total_num = PHY_RSSI_SLID_WIN_MAX;
  79. last_rssi = rtlpriv->stats.ui_rssi.elements[
  80. rtlpriv->stats.ui_rssi.index];
  81. rtlpriv->stats.ui_rssi.total_val -= last_rssi;
  82. }
  83. rtlpriv->stats.ui_rssi.total_val += pstatus->signalstrength;
  84. rtlpriv->stats.ui_rssi.elements[rtlpriv->stats.ui_rssi.index++] =
  85. pstatus->signalstrength;
  86. if (rtlpriv->stats.ui_rssi.index >= PHY_RSSI_SLID_WIN_MAX)
  87. rtlpriv->stats.ui_rssi.index = 0;
  88. tmpval = rtlpriv->stats.ui_rssi.total_val /
  89. rtlpriv->stats.ui_rssi.total_num;
  90. rtlpriv->stats.signal_strength = rtl_translate_todbm(hw, (u8)tmpval);
  91. pstatus->rssi = rtlpriv->stats.signal_strength;
  92. if (pstatus->is_cck)
  93. return;
  94. for (rfpath = RF90_PATH_A; rfpath < rtlphy->num_total_rfpath;
  95. rfpath++) {
  96. if (rtlpriv->stats.rx_rssi_percentage[rfpath] == 0) {
  97. rtlpriv->stats.rx_rssi_percentage[rfpath] =
  98. pstatus->rx_mimo_signalstrength[rfpath];
  99. }
  100. if (pstatus->rx_mimo_signalstrength[rfpath] >
  101. rtlpriv->stats.rx_rssi_percentage[rfpath]) {
  102. rtlpriv->stats.rx_rssi_percentage[rfpath] =
  103. ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
  104. (RX_SMOOTH_FACTOR - 1)) +
  105. (pstatus->rx_mimo_signalstrength[rfpath])) /
  106. (RX_SMOOTH_FACTOR);
  107. rtlpriv->stats.rx_rssi_percentage[rfpath] =
  108. rtlpriv->stats.rx_rssi_percentage[rfpath] + 1;
  109. } else {
  110. rtlpriv->stats.rx_rssi_percentage[rfpath] =
  111. ((rtlpriv->stats.rx_rssi_percentage[rfpath] *
  112. (RX_SMOOTH_FACTOR - 1)) +
  113. (pstatus->rx_mimo_signalstrength[rfpath])) /
  114. (RX_SMOOTH_FACTOR);
  115. }
  116. rtlpriv->stats.rx_snr_db[rfpath] = pstatus->rx_snr[rfpath];
  117. rtlpriv->stats.rx_evm_dbm[rfpath] =
  118. pstatus->rx_mimo_evm_dbm[rfpath];
  119. rtlpriv->stats.rx_cfo_short[rfpath] =
  120. pstatus->cfo_short[rfpath];
  121. rtlpriv->stats.rx_cfo_tail[rfpath] = pstatus->cfo_tail[rfpath];
  122. }
  123. }
  124. static void rtl_update_rxsignalstatistics(struct ieee80211_hw *hw,
  125. struct rtl_stats *pstatus)
  126. {
  127. struct rtl_priv *rtlpriv = rtl_priv(hw);
  128. int weighting = 0;
  129. if (rtlpriv->stats.recv_signal_power == 0)
  130. rtlpriv->stats.recv_signal_power = pstatus->recvsignalpower;
  131. if (pstatus->recvsignalpower > rtlpriv->stats.recv_signal_power)
  132. weighting = 5;
  133. else if (pstatus->recvsignalpower < rtlpriv->stats.recv_signal_power)
  134. weighting = (-5);
  135. rtlpriv->stats.recv_signal_power = (rtlpriv->stats.recv_signal_power *
  136. 5 + pstatus->recvsignalpower + weighting) / 6;
  137. }
  138. static void rtl_process_pwdb(struct ieee80211_hw *hw, struct rtl_stats *pstatus)
  139. {
  140. struct rtl_priv *rtlpriv = rtl_priv(hw);
  141. struct rtl_sta_info *drv_priv = NULL;
  142. struct ieee80211_sta *sta = NULL;
  143. long undec_sm_pwdb;
  144. rcu_read_lock();
  145. if (rtlpriv->mac80211.opmode != NL80211_IFTYPE_STATION)
  146. sta = rtl_find_sta(hw, pstatus->psaddr);
  147. /* adhoc or ap mode */
  148. if (sta) {
  149. drv_priv = (struct rtl_sta_info *)sta->drv_priv;
  150. undec_sm_pwdb = drv_priv->rssi_stat.undec_sm_pwdb;
  151. } else {
  152. undec_sm_pwdb = rtlpriv->dm.undec_sm_pwdb;
  153. }
  154. if (undec_sm_pwdb < 0)
  155. undec_sm_pwdb = pstatus->rx_pwdb_all;
  156. if (pstatus->rx_pwdb_all > (u32)undec_sm_pwdb) {
  157. undec_sm_pwdb = (((undec_sm_pwdb) *
  158. (RX_SMOOTH_FACTOR - 1)) +
  159. (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
  160. undec_sm_pwdb = undec_sm_pwdb + 1;
  161. } else {
  162. undec_sm_pwdb = (((undec_sm_pwdb) *
  163. (RX_SMOOTH_FACTOR - 1)) +
  164. (pstatus->rx_pwdb_all)) / (RX_SMOOTH_FACTOR);
  165. }
  166. if (sta)
  167. drv_priv->rssi_stat.undec_sm_pwdb = undec_sm_pwdb;
  168. else
  169. rtlpriv->dm.undec_sm_pwdb = undec_sm_pwdb;
  170. rcu_read_unlock();
  171. rtl_update_rxsignalstatistics(hw, pstatus);
  172. }
  173. static void rtl_process_ui_link_quality(struct ieee80211_hw *hw,
  174. struct rtl_stats *pstatus)
  175. {
  176. struct rtl_priv *rtlpriv = rtl_priv(hw);
  177. u32 last_evm, n_stream, tmpval;
  178. if (pstatus->signalquality == 0)
  179. return;
  180. if (rtlpriv->stats.ui_link_quality.total_num++ >=
  181. PHY_LINKQUALITY_SLID_WIN_MAX) {
  182. rtlpriv->stats.ui_link_quality.total_num =
  183. PHY_LINKQUALITY_SLID_WIN_MAX;
  184. last_evm = rtlpriv->stats.ui_link_quality.elements[
  185. rtlpriv->stats.ui_link_quality.index];
  186. rtlpriv->stats.ui_link_quality.total_val -= last_evm;
  187. }
  188. rtlpriv->stats.ui_link_quality.total_val += pstatus->signalquality;
  189. rtlpriv->stats.ui_link_quality.elements[
  190. rtlpriv->stats.ui_link_quality.index++] =
  191. pstatus->signalquality;
  192. if (rtlpriv->stats.ui_link_quality.index >=
  193. PHY_LINKQUALITY_SLID_WIN_MAX)
  194. rtlpriv->stats.ui_link_quality.index = 0;
  195. tmpval = rtlpriv->stats.ui_link_quality.total_val /
  196. rtlpriv->stats.ui_link_quality.total_num;
  197. rtlpriv->stats.signal_quality = tmpval;
  198. rtlpriv->stats.last_sigstrength_inpercent = tmpval;
  199. for (n_stream = 0; n_stream < 2; n_stream++) {
  200. if (pstatus->rx_mimo_sig_qual[n_stream] != -1) {
  201. if (rtlpriv->stats.rx_evm_percentage[n_stream] == 0) {
  202. rtlpriv->stats.rx_evm_percentage[n_stream] =
  203. pstatus->rx_mimo_sig_qual[n_stream];
  204. }
  205. rtlpriv->stats.rx_evm_percentage[n_stream] =
  206. ((rtlpriv->stats.rx_evm_percentage[n_stream]
  207. * (RX_SMOOTH_FACTOR - 1)) +
  208. (pstatus->rx_mimo_sig_qual[n_stream] * 1)) /
  209. (RX_SMOOTH_FACTOR);
  210. }
  211. }
  212. }
  213. void rtl_process_phyinfo(struct ieee80211_hw *hw, u8 *buffer,
  214. struct rtl_stats *pstatus)
  215. {
  216. if (!pstatus->packet_matchbssid)
  217. return;
  218. rtl_process_ui_rssi(hw, pstatus);
  219. rtl_process_pwdb(hw, pstatus);
  220. rtl_process_ui_link_quality(hw, pstatus);
  221. }