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