GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / staging / rtl8188eu / hal / odm_HWConfig.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7
8 /*  include files */
9
10 #include "odm_precomp.h"
11
12 #define READ_AND_CONFIG     READ_AND_CONFIG_MP
13
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))
16
17 static u8 odm_QueryRxPwrPercentage(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
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)
30 {
31         s32 RetSig = 0;
32
33         if (CurrSig >= 51 && CurrSig <= 100)
34                 RetSig = 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);
47         else
48                 RetSig = CurrSig;
49         return RetSig;
50 }
51
52 static s32 odm_SignalScaleMapping(struct odm_dm_struct *dm_odm, s32 CurrSig)
53 {
54         return odm_SignalScaleMapping_92CSeries(dm_odm, CurrSig);
55 }
56
57 static u8 odm_EVMdbToPercentage(s8 Value)
58 {
59         /*  -33dB~0dB to 0%~99% */
60         s8 ret_val;
61
62         ret_val = Value;
63
64         if (ret_val >= 0)
65                 ret_val = 0;
66         if (ret_val <= -33)
67                 ret_val = -33;
68
69         ret_val = 0 - ret_val;
70         ret_val *= 3;
71
72         if (ret_val == 99)
73                 ret_val = 100;
74         return ret_val;
75 }
76
77 static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
78                         struct odm_phy_status_info *pPhyInfo,
79                         u8 *pPhyStatus,
80                         struct odm_per_pkt_info *pPktinfo)
81 {
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;
87         u8 isCCKrate = 0;
88         u8 rf_rx_num = 0;
89         u8 cck_highpwr = 0;
90         u8 LNA_idx, VGA_idx;
91
92         struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
93
94         isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
95
96         pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = -1;
97         pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
98
99         if (isCCKrate) {
100                 u8 cck_agc_rpt;
101
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) */
105
106                 cck_highpwr = dm_odm->bCckHighPower;
107
108                 cck_agc_rpt =  pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
109
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;
115                 switch (LNA_idx) {
116                 case 7:
117                         if (VGA_idx <= 27)
118                                 rx_pwr_all = -100 + 2*(27-VGA_idx); /* VGA_idx = 27~2 */
119                         else
120                                 rx_pwr_all = -100;
121                         break;
122                 case 6:
123                         rx_pwr_all = -48 + 2*(2-VGA_idx); /* VGA_idx = 2~0 */
124                         break;
125                 case 5:
126                         rx_pwr_all = -42 + 2*(7-VGA_idx); /* VGA_idx = 7~5 */
127                         break;
128                 case 4:
129                         rx_pwr_all = -36 + 2*(7-VGA_idx); /* VGA_idx = 7~4 */
130                         break;
131                 case 3:
132                         rx_pwr_all = -24 + 2*(7-VGA_idx); /* VGA_idx = 7~0 */
133                         break;
134                 case 2:
135                         if (cck_highpwr)
136                                 rx_pwr_all = -12 + 2*(5-VGA_idx); /* VGA_idx = 5~0 */
137                         else
138                                 rx_pwr_all = -6 + 2*(5-VGA_idx);
139                         break;
140                 case 1:
141                         rx_pwr_all = 8-2*VGA_idx;
142                         break;
143                 case 0:
144                         rx_pwr_all = 14-2*VGA_idx;
145                         break;
146                 default:
147                         break;
148                 }
149                 rx_pwr_all += 6;
150                 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
151                 if (!cck_highpwr) {
152                         if (PWDB_ALL >= 80)
153                                 PWDB_ALL = ((PWDB_ALL-80)<<1)+((PWDB_ALL-80)>>1)+80;
154                         else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
155                                 PWDB_ALL += 3;
156                         if (PWDB_ALL > 100)
157                                 PWDB_ALL = 100;
158                 }
159
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) {
165                         u8 SQ, SQ_rpt;
166
167                         if (pPhyInfo->RxPWDBAll > 40 && !dm_odm->bInHctTest) {
168                                 SQ = 100;
169                         } else {
170                                 SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
171
172                                 if (SQ_rpt > 64)
173                                         SQ = 0;
174                                 else if (SQ_rpt < 20)
175                                         SQ = 100;
176                                 else
177                                         SQ = ((64-SQ_rpt) * 100) / 44;
178                         }
179                         pPhyInfo->SignalQuality = SQ;
180                         pPhyInfo->RxMIMOSignalQuality[RF_PATH_A] = SQ;
181                         pPhyInfo->RxMIMOSignalQuality[RF_PATH_B] = -1;
182                 }
183         } else { /* is OFDM rate */
184                 dm_odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
185
186                 /*  (1)Get RSSI for HT rate */
187
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))
191                                 rf_rx_num++;
192
193                         rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F)*2) - 110;
194
195                         pPhyInfo->RxPwr[i] = rx_pwr[i];
196
197                         /* Translate DBM to percentage. */
198                         RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
199                         total_rssi += RSSI;
200
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);
205                                 else
206                                         RSSI = (RSSI <= 16) ? (RSSI >> 3) : (RSSI - 16);
207
208                                 if ((RSSI <= 34) && (RSSI >= 4))
209                                         RSSI -= 4;
210                         }
211
212                         pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
213
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);
217                 }
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;
220
221                 PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
222                 PWDB_ALL_BT = PWDB_ALL;
223
224                 pPhyInfo->RxPWDBAll = PWDB_ALL;
225                 pPhyInfo->BTRxRSSIPercentage = PWDB_ALL_BT;
226                 pPhyInfo->RxPower = rx_pwr_all;
227                 pPhyInfo->RecvSignalPower = rx_pwr_all;
228
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 */
232                 else
233                         Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
234
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 */
240
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);
245                         }
246                 }
247         }
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(). */
250         if (isCCKrate) {
251                 pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
252         } else {
253                 if (rf_rx_num != 0)
254                         pPhyInfo->SignalStrength = (u8)(odm_SignalScaleMapping(dm_odm, total_rssi /= rf_rx_num));
255         }
256
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;
263 }
264
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)
268 {
269         s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
270         s32 UndecoratedSmoothedOFDM, RSSI_Ave;
271         u8 isCCKrate = 0;
272         u8 RSSI_max, RSSI_min, i;
273         u32 OFDM_pkt = 0;
274         u32 Weighting = 0;
275         struct sta_info *pEntry;
276         u8 antsel_tr_mux;
277         struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
278
279         if (pPktinfo->StationID == 0xFF)
280                 return;
281         pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
282         if (!IS_STA_VALID(pEntry))
283                 return;
284         if ((!pPktinfo->bPacketMatchBSSID))
285                 return;
286
287         isCCKrate = ((pPktinfo->Rate >= DESC92C_RATE1M) && (pPktinfo->Rate <= DESC92C_RATE11M)) ? true : false;
288
289         /* Smart Antenna Debug Message------------------  */
290
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]++;
299                         }
300                 }
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);
306                 }
307         }
308         /* Smart Antenna Debug Message------------------ */
309
310         UndecoratedSmoothedCCK =  pEntry->rssi_stat.UndecoratedSmoothedCCK;
311         UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
312         UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
313
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];
318                         } else {
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];
322                                 } else {
323                                         RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
324                                         RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
325                                 }
326                                 if ((RSSI_max - RSSI_min) < 3)
327                                         RSSI_Ave = RSSI_max;
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;
332                                 else
333                                         RSSI_Ave = RSSI_max - 3;
334                         }
335
336                         /* 1 Process OFDM RSSI */
337                         if (UndecoratedSmoothedOFDM <= 0) {     /*  initialize */
338                                 UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
339                         } else {
340                                 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
341                                         UndecoratedSmoothedOFDM =
342                                                         (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
343                                                         (RSSI_Ave)) / (Rx_Smooth_Factor);
344                                         UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
345                                 } else {
346                                         UndecoratedSmoothedOFDM =
347                                                         (((UndecoratedSmoothedOFDM)*(Rx_Smooth_Factor-1)) +
348                                                         (RSSI_Ave)) / (Rx_Smooth_Factor);
349                                 }
350                         }
351
352                         pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT(0);
353
354                 } else {
355                         RSSI_Ave = pPhyInfo->RxPWDBAll;
356
357                         /* 1 Process CCK RSSI */
358                         if (UndecoratedSmoothedCCK <= 0) {      /*  initialize */
359                                 UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
360                         } else {
361                                 if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
362                                         UndecoratedSmoothedCCK =
363                                                         ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
364                                                         pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
365                                         UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
366                                 } else {
367                                         UndecoratedSmoothedCCK =
368                                                         ((UndecoratedSmoothedCCK * (Rx_Smooth_Factor-1)) +
369                                                         pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
370                                 }
371                         }
372                         pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
373                 }
374                 /* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
375                 if (pEntry->rssi_stat.ValidBit >= 64)
376                         pEntry->rssi_stat.ValidBit = 64;
377                 else
378                         pEntry->rssi_stat.ValidBit++;
379
380                 for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
381                         OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i) & BIT(0);
382
383                 if (pEntry->rssi_stat.ValidBit == 64) {
384                         Weighting = min_t(u32, OFDM_pkt << 4, 64);
385                         UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
386                 } else {
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;
391                         else
392                                 UndecoratedSmoothedPWDB = 0;
393                 }
394                 pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
395                 pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
396                 pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
397         }
398 }
399
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,
403                                          u8 *pPhyStatus,
404                                          struct odm_per_pkt_info *pPktinfo)
405 {
406         odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus,
407                                          pPktinfo);
408         if (dm_odm->RSSI_test) {
409                 ;/*  Select the packets to do RSSI checking for antenna switching. */
410         } else {
411                 odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
412         }
413 }
414
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)
418 {
419         ODM_PhyStatusQuery_92CSeries(dm_odm, pPhyInfo, pPhyStatus, pPktinfo);
420 }