1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2011 Realtek Corporation. */
3 
4 #include "../include/drv_types.h"
5 
odm_query_rxpwrpercentage(s8 antpower)6 static u8 odm_query_rxpwrpercentage(s8 antpower)
7 {
8 	if ((antpower <= -100) || (antpower >= 20))
9 		return 0;
10 	else if (antpower >= 0)
11 		return 100;
12 	else
13 		return 100 + antpower;
14 }
15 
odm_signal_scale_mapping(struct odm_dm_struct * dm_odm,s32 currsig)16 static s32 odm_signal_scale_mapping(struct odm_dm_struct *dm_odm, s32 currsig)
17 {
18 	s32 retsig;
19 
20 	if (currsig >= 51 && currsig <= 100)
21 		retsig = 100;
22 	else if (currsig >= 41 && currsig <= 50)
23 		retsig = 80 + ((currsig - 40) * 2);
24 	else if (currsig >= 31 && currsig <= 40)
25 		retsig = 66 + (currsig - 30);
26 	else if (currsig >= 21 && currsig <= 30)
27 		retsig = 54 + (currsig - 20);
28 	else if (currsig >= 10 && currsig <= 20)
29 		retsig = 42 + (((currsig - 10) * 2) / 3);
30 	else if (currsig >= 5 && currsig <= 9)
31 		retsig = 22 + (((currsig - 5) * 3) / 2);
32 	else if (currsig >= 1 && currsig <= 4)
33 		retsig = 6 + (((currsig - 1) * 3) / 2);
34 	else
35 		retsig = currsig;
36 
37 	return retsig;
38 }
39 
odm_evm_db_to_percentage(s8 value)40 static u8 odm_evm_db_to_percentage(s8 value)
41 {
42 	/*  -33dB~0dB to 0%~99% */
43 	s8 ret_val = clamp(-value, 0, 33) * 3;
44 
45 	if (ret_val == 99)
46 		ret_val = 100;
47 
48 	return ret_val;
49 }
50 
odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct * dm_odm,struct phy_info * pPhyInfo,u8 * pPhyStatus,struct odm_per_pkt_info * pPktinfo,struct adapter * adapt)51 static void odm_RxPhyStatus92CSeries_Parsing(struct odm_dm_struct *dm_odm,
52 			struct phy_info *pPhyInfo,
53 			u8 *pPhyStatus,
54 			struct odm_per_pkt_info *pPktinfo,
55 			struct adapter *adapt)
56 {
57 	u8 i, Max_spatial_stream;
58 	s8 rx_pwr[4], rx_pwr_all = 0;
59 	u8 EVM, PWDB_ALL = 0;
60 	u8 RSSI, total_rssi = 0;
61 	u8 isCCKrate = 0;
62 	u8 rf_rx_num = 0;
63 	u8 cck_highpwr = 0;
64 	u8 LNA_idx, VGA_idx;
65 
66 	struct phy_status_rpt *pPhyStaRpt = (struct phy_status_rpt *)pPhyStatus;
67 
68 	isCCKrate = pPktinfo->Rate >= DESC92C_RATE1M && pPktinfo->Rate <= DESC92C_RATE11M;
69 
70 	if (isCCKrate) {
71 		u8 cck_agc_rpt;
72 
73 		/*  (1)Hardware does not provide RSSI for CCK */
74 		/*  (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
75 
76 		cck_highpwr = dm_odm->bCckHighPower;
77 
78 		cck_agc_rpt =  pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
79 
80 		/* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
81 		/* The RSSI formula should be modified according to the gain table */
82 		/* In 88E, cck_highpwr is always set to 1 */
83 		LNA_idx = ((cck_agc_rpt & 0xE0) >> 5);
84 		VGA_idx = (cck_agc_rpt & 0x1F);
85 		switch (LNA_idx) {
86 		case 7:
87 			if (VGA_idx <= 27)
88 				rx_pwr_all = -100 + 2 * (27 - VGA_idx); /* VGA_idx = 27~2 */
89 			else
90 				rx_pwr_all = -100;
91 			break;
92 		case 6:
93 			rx_pwr_all = -48 + 2 * (2 - VGA_idx); /* VGA_idx = 2~0 */
94 			break;
95 		case 5:
96 			rx_pwr_all = -42 + 2 * (7 - VGA_idx); /* VGA_idx = 7~5 */
97 			break;
98 		case 4:
99 			rx_pwr_all = -36 + 2 * (7 - VGA_idx); /* VGA_idx = 7~4 */
100 			break;
101 		case 3:
102 			rx_pwr_all = -24 + 2 * (7 - VGA_idx); /* VGA_idx = 7~0 */
103 			break;
104 		case 2:
105 			if (cck_highpwr)
106 				rx_pwr_all = -12 + 2 * (5 - VGA_idx); /* VGA_idx = 5~0 */
107 			else
108 				rx_pwr_all = -6 + 2 * (5 - VGA_idx);
109 			break;
110 		case 1:
111 				rx_pwr_all = 8 - 2 * VGA_idx;
112 			break;
113 		case 0:
114 				rx_pwr_all = 14 - 2 * VGA_idx;
115 			break;
116 		default:
117 			break;
118 		}
119 		rx_pwr_all += 6;
120 		PWDB_ALL = odm_query_rxpwrpercentage(rx_pwr_all);
121 		if (!cck_highpwr) {
122 			if (PWDB_ALL >= 80)
123 				PWDB_ALL = ((PWDB_ALL - 80) << 1) + ((PWDB_ALL - 80) >> 1) + 80;
124 			else if ((PWDB_ALL <= 78) && (PWDB_ALL >= 20))
125 				PWDB_ALL += 3;
126 			if (PWDB_ALL > 100)
127 				PWDB_ALL = 100;
128 		}
129 
130 		pPhyInfo->RxPWDBAll = PWDB_ALL;
131 		pPhyInfo->recvpower = rx_pwr_all;
132 		/*  (3) Get Signal Quality (EVM) */
133 		if (pPktinfo->bPacketMatchBSSID) {
134 			u8 SQ, SQ_rpt;
135 
136 			if (pPhyInfo->RxPWDBAll > 40) {
137 				SQ = 100;
138 			} else {
139 				SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
140 
141 				if (SQ_rpt > 64)
142 					SQ = 0;
143 				else if (SQ_rpt < 20)
144 					SQ = 100;
145 				else
146 					SQ = ((64 - SQ_rpt) * 100) / 44;
147 			}
148 			pPhyInfo->SignalQuality = SQ;
149 		}
150 	} else { /* is OFDM rate */
151 		/*  (1)Get RSSI for HT rate */
152 
153 		for (i = RF_PATH_A; i < RF_PATH_MAX; i++) {
154 			/*  2008/01/30 MH we will judge RF RX path now. */
155 			if (dm_odm->RFPathRxEnable & BIT(i))
156 				rf_rx_num++;
157 
158 			rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain & 0x3F) * 2) - 110;
159 			if (i == RF_PATH_A)
160 				adapt->signal_strength = rx_pwr[i];
161 
162 			pPhyInfo->RxPwr[i] = rx_pwr[i];
163 
164 			/* Translate DBM to percentage. */
165 			RSSI = odm_query_rxpwrpercentage(rx_pwr[i]);
166 			total_rssi += RSSI;
167 
168 			pPhyInfo->RxMIMOSignalStrength[i] = (u8)RSSI;
169 
170 			/* Get Rx snr value in DB */
171 			dm_odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i] / 2);
172 		}
173 		/*  (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
174 		rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1) & 0x7f) - 110;
175 
176 		PWDB_ALL = odm_query_rxpwrpercentage(rx_pwr_all);
177 
178 		pPhyInfo->RxPWDBAll = PWDB_ALL;
179 		pPhyInfo->RxPower = rx_pwr_all;
180 		pPhyInfo->recvpower = rx_pwr_all;
181 
182 		/*  (3)EVM of HT rate */
183 		if (pPktinfo->Rate >= DESC92C_RATEMCS8 && pPktinfo->Rate <= DESC92C_RATEMCS15)
184 			Max_spatial_stream = 2; /* both spatial stream make sense */
185 		else
186 			Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
187 
188 		for (i = 0; i < Max_spatial_stream; i++) {
189 			/*  Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
190 			/*  fill most significant bit to "zero" when doing shifting operation which may change a negative */
191 			/*  value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore. */
192 			EVM = odm_evm_db_to_percentage((pPhyStaRpt->stream_rxevm[i]));	/* dbm */
193 
194 			if (pPktinfo->bPacketMatchBSSID) {
195 				if (i == RF_PATH_A) /*  Fill value in RFD, Get the first spatial stream only */
196 					pPhyInfo->SignalQuality = (u8)(EVM & 0xff);
197 			}
198 		}
199 	}
200 	/* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
201 	/* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
202 	if (isCCKrate) {
203 		pPhyInfo->SignalStrength = (u8)(odm_signal_scale_mapping(dm_odm, PWDB_ALL));/* PWDB_ALL; */
204 	} else {
205 		if (rf_rx_num != 0)
206 			pPhyInfo->SignalStrength = (u8)(odm_signal_scale_mapping(dm_odm, total_rssi /= rf_rx_num));
207 	}
208 
209 	/* For 88E HW Antenna Diversity */
210 	dm_odm->DM_FatTable.antsel_rx_keep_0 = pPhyStaRpt->ant_sel;
211 	dm_odm->DM_FatTable.antsel_rx_keep_1 = pPhyStaRpt->ant_sel_b;
212 	dm_odm->DM_FatTable.antsel_rx_keep_2 = pPhyStaRpt->antsel_rx_keep_2;
213 }
214 
odm_Process_RSSIForDM(struct odm_dm_struct * dm_odm,struct phy_info * pPhyInfo,struct odm_per_pkt_info * pPktinfo)215 static void odm_Process_RSSIForDM(struct odm_dm_struct *dm_odm,
216 				  struct phy_info *pPhyInfo,
217 				  struct odm_per_pkt_info *pPktinfo)
218 {
219 	s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK;
220 	s32 UndecoratedSmoothedOFDM, RSSI_Ave;
221 	u8 isCCKrate = 0;
222 	u8 RSSI_max, RSSI_min, i;
223 	u32 OFDM_pkt = 0;
224 	u32 Weighting = 0;
225 	struct sta_info *pEntry;
226 	u8 antsel_tr_mux;
227 	struct fast_ant_train *pDM_FatTable = &dm_odm->DM_FatTable;
228 
229 	if (pPktinfo->StationID == 0xFF)
230 		return;
231 	pEntry = dm_odm->pODM_StaInfo[pPktinfo->StationID];
232 	if (!IS_STA_VALID(pEntry))
233 		return;
234 	if ((!pPktinfo->bPacketMatchBSSID))
235 		return;
236 
237 	isCCKrate = pPktinfo->Rate >= DESC92C_RATE1M && pPktinfo->Rate <= DESC92C_RATE11M;
238 
239 	/* Smart Antenna Debug Message------------------  */
240 	if ((dm_odm->AntDivType == CG_TRX_HW_ANTDIV) || (dm_odm->AntDivType == CGCS_RX_HW_ANTDIV)) {
241 		if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
242 			antsel_tr_mux = (pDM_FatTable->antsel_rx_keep_2 << 2) |
243 					(pDM_FatTable->antsel_rx_keep_1 << 1) | pDM_FatTable->antsel_rx_keep_0;
244 			ODM_AntselStatistics_88E(dm_odm, antsel_tr_mux, pPktinfo->StationID, pPhyInfo->RxPWDBAll);
245 		}
246 	}
247 
248 	/* Smart Antenna Debug Message------------------ */
249 
250 	UndecoratedSmoothedCCK =  pEntry->rssi_stat.UndecoratedSmoothedCCK;
251 	UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
252 	UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
253 
254 	if (pPktinfo->bPacketToSelf || pPktinfo->bPacketBeacon) {
255 		if (!isCCKrate) { /* ofdm rate */
256 			if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_B] == 0) {
257 				RSSI_Ave = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
258 			} else {
259 				if (pPhyInfo->RxMIMOSignalStrength[RF_PATH_A] > pPhyInfo->RxMIMOSignalStrength[RF_PATH_B]) {
260 					RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
261 					RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
262 				} else {
263 					RSSI_max = pPhyInfo->RxMIMOSignalStrength[RF_PATH_B];
264 					RSSI_min = pPhyInfo->RxMIMOSignalStrength[RF_PATH_A];
265 				}
266 				if ((RSSI_max - RSSI_min) < 3)
267 					RSSI_Ave = RSSI_max;
268 				else if ((RSSI_max - RSSI_min) < 6)
269 					RSSI_Ave = RSSI_max - 1;
270 				else if ((RSSI_max - RSSI_min) < 10)
271 					RSSI_Ave = RSSI_max - 2;
272 				else
273 					RSSI_Ave = RSSI_max - 3;
274 			}
275 
276 			/* 1 Process OFDM RSSI */
277 			if (UndecoratedSmoothedOFDM <= 0) {	/*  initialize */
278 				UndecoratedSmoothedOFDM = pPhyInfo->RxPWDBAll;
279 			} else {
280 				if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedOFDM) {
281 					UndecoratedSmoothedOFDM =
282 							(((UndecoratedSmoothedOFDM) * (Rx_Smooth_Factor - 1)) +
283 							(RSSI_Ave)) / (Rx_Smooth_Factor);
284 					UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
285 				} else {
286 					UndecoratedSmoothedOFDM =
287 							(((UndecoratedSmoothedOFDM) * (Rx_Smooth_Factor - 1)) +
288 							(RSSI_Ave)) / (Rx_Smooth_Factor);
289 				}
290 			}
291 
292 			pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap << 1) | BIT(0);
293 
294 		} else {
295 			RSSI_Ave = pPhyInfo->RxPWDBAll;
296 
297 			/* 1 Process CCK RSSI */
298 			if (UndecoratedSmoothedCCK <= 0) {	/*  initialize */
299 				UndecoratedSmoothedCCK = pPhyInfo->RxPWDBAll;
300 			} else {
301 				if (pPhyInfo->RxPWDBAll > (u32)UndecoratedSmoothedCCK) {
302 					UndecoratedSmoothedCCK =
303 							((UndecoratedSmoothedCCK * (Rx_Smooth_Factor - 1)) +
304 							pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
305 					UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
306 				} else {
307 					UndecoratedSmoothedCCK =
308 							((UndecoratedSmoothedCCK * (Rx_Smooth_Factor - 1)) +
309 							pPhyInfo->RxPWDBAll) / Rx_Smooth_Factor;
310 				}
311 			}
312 			pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap << 1;
313 		}
314 		/* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
315 		if (pEntry->rssi_stat.ValidBit >= 64)
316 			pEntry->rssi_stat.ValidBit = 64;
317 		else
318 			pEntry->rssi_stat.ValidBit++;
319 
320 		for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
321 			OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap >> i) & BIT(0);
322 
323 		if (pEntry->rssi_stat.ValidBit == 64) {
324 			Weighting = ((OFDM_pkt << 4) > 64) ? 64 : (OFDM_pkt << 4);
325 			UndecoratedSmoothedPWDB = (Weighting * UndecoratedSmoothedOFDM + (64 - Weighting) * UndecoratedSmoothedCCK) >> 6;
326 		} else {
327 			if (pEntry->rssi_stat.ValidBit != 0)
328 				UndecoratedSmoothedPWDB = (OFDM_pkt * UndecoratedSmoothedOFDM +
329 							  (pEntry->rssi_stat.ValidBit - OFDM_pkt) *
330 							  UndecoratedSmoothedCCK) / pEntry->rssi_stat.ValidBit;
331 			else
332 				UndecoratedSmoothedPWDB = 0;
333 		}
334 		pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
335 		pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
336 		pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
337 	}
338 }
339 
340 /*  Endianness before calling this API */
ODM_PhyStatusQuery(struct odm_dm_struct * dm_odm,struct phy_info * pPhyInfo,u8 * pPhyStatus,struct odm_per_pkt_info * pPktinfo,struct adapter * adapt)341 void ODM_PhyStatusQuery(struct odm_dm_struct *dm_odm,
342 			struct phy_info *pPhyInfo,
343 			u8 *pPhyStatus,
344 			struct odm_per_pkt_info *pPktinfo,
345 			struct adapter *adapt)
346 {
347 	odm_RxPhyStatus92CSeries_Parsing(dm_odm, pPhyInfo, pPhyStatus, pPktinfo, adapt);
348 	odm_Process_RSSIForDM(dm_odm, pPhyInfo, pPktinfo);
349 }
350