1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
10 #include "odm_precomp.h"
12 #define READ_AND_CONFIG READ_AND_CONFIG_MP
14 #define READ_AND_CONFIG_MP(ic, txt) (ODM_ReadAndConfig##txt##ic(dm_odm))
15 #define READ_AND_CONFIG_TC(ic, txt) (ODM_ReadAndConfig_TC##txt##ic(dm_odm))
17 static u8 odm_QueryRxPwrPercentage(s8 AntPower)
19 if ((AntPower <= -100) || (AntPower >= 20))
21 else if (AntPower >= 0)
27 /* 2012/01/12 MH MOve some signal strength smooth method to MP HAL layer. */
28 /* IF other SW team do not support the feature, remove this section.?? */
29 static s32 odm_SignalScaleMapping_92CSeries(struct odm_dm_struct *dm_odm, s32 CurrSig)
33 if (CurrSig >= 51 && CurrSig <= 100)
35 else if (CurrSig >= 41 && CurrSig <= 50)
36 RetSig = 80 + ((CurrSig - 40)*2);
37 else if (CurrSig >= 31 && CurrSig <= 40)
38 RetSig = 66 + (CurrSig - 30);
39 else if (CurrSig >= 21 && CurrSig <= 30)
40 RetSig = 54 + (CurrSig - 20);
41 else if (CurrSig >= 10 && CurrSig <= 20)
42 RetSig = 42 + (((CurrSig - 10) * 2) / 3);
43 else if (CurrSig >= 5 && CurrSig <= 9)
44 RetSig = 22 + (((CurrSig - 5) * 3) / 2);
45 else if (CurrSig >= 1 && CurrSig <= 4)
46 RetSig = 6 + (((CurrSig - 1) * 3) / 2);
52 static s32 odm_SignalScaleMapping(struct odm_dm_struct *dm_odm, s32 CurrSig)
54 return odm_SignalScaleMapping_92CSeries(dm_odm, CurrSig);
57 static u8 odm_EVMdbToPercentage(s8 Value)
59 /* -33dB~0dB to 0%~99% */
69 ret_val = 0 - ret_val;
77 static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
78 struct odm_phy_status_info *pPhyInfo,
80 struct odm_per_pkt_info *pPktinfo)
82 struct sw_ant_switch *pDM_SWAT_Table = &dm_odm->DM_SWAT_Table;
83 u8 i, Max_spatial_stream;
84 s8 rx_pwr[4], rx_pwr_all = 0;
85 u8 EVM, PWDB_ALL = 0, PWDB_ALL_BT;
86 u8 RSSI, total_rssi = 0;
92 struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
94 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
96 pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = -1;
97 pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
102 dm_odm->PhyDbgInfo.NumQryPhyStatusCCK++;
103 /* (1)Hardware does not provide RSSI for CCK */
104 /* (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
106 cck_highpwr = dm_odm->bCckHighPower;
108 cck_agc_rpt = pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
110 /* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
111 /* The RSSI formula should be modified according to the gain table */
112 /* In 88E, cck_highpwr is always set to 1 */
113 LNA_idx = (cck_agc_rpt & 0xE0) >> 5;
114 VGA_idx = cck_agc_rpt & 0x1F;
118 rx_pwr_all = -100 + 2*(27-VGA_idx); /* VGA_idx = 27~2 */
123 rx_pwr_all = -48 + 2*(2-VGA_idx); /* VGA_idx = 2~0 */
126 rx_pwr_all = -42 + 2*(7-VGA_idx); /* VGA_idx = 7~5 */
129 rx_pwr_all = -36 + 2*(7-VGA_idx); /* VGA_idx = 7~4 */
132 rx_pwr_all = -24 + 2*(7-VGA_idx); /* VGA_idx = 7~0 */
136 rx_pwr_all = -12 + 2*(5-VGA_idx); /* VGA_idx = 5~0 */
138 rx_pwr_all = -6 + 2*(5-VGA_idx);
141 rx_pwr_all = 8-2*VGA_idx;
144 rx_pwr_all = 14-2*VGA_idx;
150 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
153 PWDB_ALL = ((PWDB_ALL-80)<<1)+((PWDB_ALL-80)>>1)+80;
154 else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
160 pPhyInfo->RxPWDBAll = PWDB_ALL;
161 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL;
162 pPhyInfo->RecvSignalPower = rx_pwr_all;
163 /* (3) Get Signal Quality (EVM) */
164 if (pPktinfo->bPacketMatchBSSID) {
167 if (pPhyInfo->RxPWDBAll > 40 && !dm_odm->bInHctTest) {
170 SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
174 else if (SQ_rpt < 20)
177 SQ = ((64-SQ_rpt) * 100) / 44;
179 pPhyInfo->SignalQuality = SQ;
180 pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = SQ;
181 pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
183 } else { /* is OFDM rate */
184 dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
186 /* (1)Get RSSI for HT rate */
188 for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
189 /* 2008/01/30 MH we will judge RF RX path now. */
190 if (dm_odm->RFPathRxEnable & BIT(i))
193 rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F)*2) - 110;
195 pPhyInfo->RxPwr[i] = rx_pwr[i];
197 /* Translate DBM to percentage. */
198 RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
201 /* Modification for ext-LNA board */
202 if (dm_odm->BoardType == ODM_BOARD_HIGHPWR) {
203 if ((pPhyStaRpt->path_agc[i].trsw) == 1)
204 RSSI = (RSSI > 94) ? 100 : (RSSI + 6);
206 RSSI = (RSSI <= 16) ? (RSSI >> 3) : (RSSI - 16);
208 if ((RSSI <= 34) && (RSSI >= 4))
212 pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
214 /* Get Rx snr value in DB */
215 pPhyInfo->RxSNR[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
216 dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
218 /* (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
219 rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f) - 110;
221 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
222 PWDB_ALL_BT = PWDB_ALL;
224 pPhyInfo->RxPWDBAll = PWDB_ALL;
225 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL_BT;
226 pPhyInfo->RxPower = rx_pwr_all;
227 pPhyInfo->RecvSignalPower = rx_pwr_all;
229 /* (3)EVM of HT rate */
230 if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
231 Max_spatial_stream = 2; /* both spatial stream make sense */
233 Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
235 for (i = 0; i < Max_spatial_stream; i++) {
236 /* Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
237 /* fill most significant bit to "zero" when doing shifting operation which may change a negative */
238 /* value to positive one, then the dbm value (which is supposed to be negative) is not correct anymore. */
239 EVM = odm_EVMdbToPercentage((pPhyStaRpt->stream_rxevm[i])); /* dbm */
241 if (pPktinfo->bPacketMatchBSSID) {
242 if (i == RF_PATH_A) /* Fill value in RFD, Get the first spatial stream only */
243 pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
244 pPhyInfo->RxMIMOSignalQuality[i] = (u8)(EVM & 0xff);
248 /* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
249 /* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
251 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
254 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, total_rssi /= rf_rx_num));
257 /* For 92C/92D HW (Hybrid) Antenna Diversity */
258 pDM_SWAT_Table->antsel = pPhyStaRpt->ant_sel;
259 /* For 88E HW Antenna Diversity */
260 dm_odm->DM_FatTable.antsel_rx_keep_0 = pPhyStaRpt->ant_sel;
261 dm_odm->DM_FatTable.antsel_rx_keep_1 = pPhyStaRpt->ant_sel_b;
262 dm_odm->DM_FatTable.antsel_rx_keep_2 = pPhyStaRpt->antsel_rx_keep_2;
265 static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
266 struct odm_phy_status_info *pPhyInfo,
267 struct odm_per_pkt_info *pPktinfo)
269 s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
270 s32 UndecoratedSmoothedOFDM, RSSI_Ave;
272 u8 RSSI_max, RSSI_min, i;
275 struct sta_info *pEntry;
277 struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
279 if (pPktinfo->StationID == 0xFF)
281 pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
282 if (!IS_STA_VALID(pEntry))
284 if ((!pPktinfo->bPacketMatchBSSID))
287 isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
289 /* Smart Antenna Debug Message------------------ */
291 if (dm_odm->AntDivType == CG_TRX_SMART_ANTDIV) {
292 if (pDM_FatTable->FAT_State == FAT_TRAINING_STATE) {
293 if (pPktinfo->bPacketToSelf) {
294 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
295 (pDM_FatTable->antsel_rx_keep_1<<1) |
296 pDM_FatTable->antsel_rx_keep_0;
297 pDM_FatTable->antSumRSSI[antsel_tr_mux] += pPhyInfo->RxPWDBAll;
298 pDM_FatTable->antRSSIcnt[antsel_tr_mux]++;
301 } else if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
302 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
303 antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2<<2) |
304 (pDM_FatTable->antsel_rx_keep_1<<1) | pDM_FatTable->antsel_rx_keep_0;
305 rtl88eu_dm_ant_sel_statistics(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
308 /* Smart Antenna Debug Message------------------ */
310 UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
311 UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
312 UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
314 if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
315 if (!isCCKrate) { /* ofdm rate */
316 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
317 RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
319 if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
320 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
321 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
323 RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
324 RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
326 if ((RSSI_max - RSSI_min) < 3)
328 else if ((RSSI_max - RSSI_min) < 6)
329 RSSI_Ave = RSSI_max - 1;
330 else if ((RSSI_max - RSSI_min) < 10)
331 RSSI_Ave = RSSI_max - 2;
333 RSSI_Ave = RSSI_max - 3;
336 /* 1 Process OFDM RSSI */
337 if (UndecoratedSmoothedOFDM <= 0) { /* initialize */
338 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
340 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
341 UndecoratedSmoothedOFDM =
342 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
343 (RSSI_Ave)) / (Rx_Smooth_Factor);
344 UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
346 UndecoratedSmoothedOFDM =
347 (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
348 (RSSI_Ave)) / (Rx_Smooth_Factor);
352 pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT(0);
355 RSSI_Ave = pPhyInfo->RxPWDBAll;
357 /* 1 Process CCK RSSI */
358 if (UndecoratedSmoothedCCK <= 0) { /* initialize */
359 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
361 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
362 UndecoratedSmoothedCCK =
363 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
364 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
365 UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
367 UndecoratedSmoothedCCK =
368 ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
369 pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
372 pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
374 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
375 if (pEntry->rssi_stat.ValidBit >= 64)
376 pEntry->rssi_stat.ValidBit = 64;
378 pEntry->rssi_stat.ValidBit++;
380 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
381 OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i) & BIT(0);
383 if (pEntry->rssi_stat.ValidBit == 64) {
384 Weighting = min_t(u32, OFDM_pkt << 4, 64);
385 UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
387 if (pEntry->rssi_stat.ValidBit != 0)
388 UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
389 (pEntry->rssi_stat.ValidBit-OFDM_pkt) *
390 UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
392 UndecoratedSmoothedPWDB = 0;
394 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
395 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
396 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
400 /* Endianness before calling this API */
401 static void ODM_PhyStatusQuery_92CSeries(struct odm_dm_struct *dm_odm,
402 struct odm_phy_status_info *pPhyInfo,
404 struct odm_per_pkt_info *pPktinfo)
406 odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus,
408 if (dm_odm->RSSI_test) {
409 ;/* Select the packets to do RSSI checking for antenna switching. */
411 odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
415 void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
416 struct odm_phy_status_info *pPhyInfo,
417 u8 *pPhyStatus, struct odm_per_pkt_info *pPktinfo)
419 ODM_PhyStatusQuery_92CSeries(dm_odm, pPhyInfo, pPhyStatus, pPktinfo);