1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 
8 #include "odm_precomp.h"
9 
10 #define READ_AND_CONFIG_MP(ic, txt) (ODM_ReadAndConfig_MP_##ic##txt(pDM_Odm))
11 #define READ_AND_CONFIG     READ_AND_CONFIG_MP
12 #define GET_VERSION_MP(ic, txt) (ODM_GetVersion_MP_##ic##txt())
13 #define GET_VERSION(ic, txt) (pDM_Odm->bIsMPChip?GET_VERSION_MP(ic, txt):GET_VERSION_TC(ic, txt))
14 
15 static u8 odm_QueryRxPwrPercentage(s8 AntPower)
16 {
17 	if ((AntPower <= -100) || (AntPower >= 20))
18 		return	0;
19 	else if (AntPower >= 0)
20 		return	100;
21 	else
22 		return 100 + AntPower;
23 
24 }
25 
26 s32 odm_SignalScaleMapping(struct dm_odm_t *pDM_Odm, s32 CurrSig)
27 {
28 	s32 RetSig = 0;
29 
30 	if (pDM_Odm->SupportInterface  == ODM_ITRF_SDIO) {
31 		if (CurrSig >= 51 && CurrSig <= 100)
32 			RetSig = 100;
33 		else if (CurrSig >= 41 && CurrSig <= 50)
34 			RetSig = 80 + ((CurrSig - 40)*2);
35 		else if (CurrSig >= 31 && CurrSig <= 40)
36 			RetSig = 66 + (CurrSig - 30);
37 		else if (CurrSig >= 21 && CurrSig <= 30)
38 			RetSig = 54 + (CurrSig - 20);
39 		else if (CurrSig >= 10 && CurrSig <= 20)
40 			RetSig = 42 + (((CurrSig - 10) * 2) / 3);
41 		else if (CurrSig >= 5 && CurrSig <= 9)
42 			RetSig = 22 + (((CurrSig - 5) * 3) / 2);
43 		else if (CurrSig >= 1 && CurrSig <= 4)
44 			RetSig = 6 + (((CurrSig - 1) * 3) / 2);
45 		else
46 			RetSig = CurrSig;
47 	}
48 
49 	return RetSig;
50 }
51 
52 static u8 odm_EVMdbToPercentage(s8 Value)
53 {
54 	/*  */
55 	/*  -33dB~0dB to 0%~99% */
56 	/*  */
57 	s8 ret_val;
58 
59 	ret_val = Value;
60 	ret_val /= 2;
61 
62 	if (ret_val >= 0)
63 		ret_val = 0;
64 	if (ret_val <= -33)
65 		ret_val = -33;
66 
67 	ret_val = 0 - ret_val;
68 	ret_val *= 3;
69 
70 	if (ret_val == 99)
71 		ret_val = 100;
72 
73 	return ret_val;
74 }
75 
76 static void odm_RxPhyStatus92CSeries_Parsing(
77 	struct dm_odm_t *pDM_Odm,
78 	struct odm_phy_info *pPhyInfo,
79 	u8 *pPhyStatus,
80 	struct odm_packet_info *pPktinfo
81 )
82 {
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 	bool isCCKrate = false;
88 	u8 rf_rx_num = 0;
89 	u8 LNA_idx, VGA_idx;
90 	struct phy_status_rpt_8192cd_t *pPhyStaRpt = (struct phy_status_rpt_8192cd_t *)pPhyStatus;
91 
92 	isCCKrate = pPktinfo->data_rate <= DESC_RATE11M;
93 	pPhyInfo->rx_mimo_signal_quality[ODM_RF_PATH_A] = -1;
94 	pPhyInfo->rx_mimo_signal_quality[ODM_RF_PATH_B] = -1;
95 
96 
97 	if (isCCKrate) {
98 		u8 cck_agc_rpt;
99 
100 		pDM_Odm->PhyDbgInfo.NumQryPhyStatusCCK++;
101 		/*  */
102 		/*  (1)Hardware does not provide RSSI for CCK */
103 		/*  (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
104 		/*  */
105 
106 		cck_agc_rpt = pPhyStaRpt->cck_agc_rpt_ofdm_cfosho_a;
107 
108 		/* 2011.11.28 LukeLee: 88E use different LNA & VGA gain table */
109 		/* The RSSI formula should be modified according to the gain table */
110 		LNA_idx = ((cck_agc_rpt & 0xE0)>>5);
111 		VGA_idx = (cck_agc_rpt & 0x1F);
112 		rx_pwr_all = odm_CCKRSSI_8723B(LNA_idx, VGA_idx);
113 		PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
114 		if (PWDB_ALL > 100)
115 			PWDB_ALL = 100;
116 
117 		pPhyInfo->rx_pwd_ba11 = PWDB_ALL;
118 		pPhyInfo->bt_rx_rssi_percentage = PWDB_ALL;
119 		pPhyInfo->recv_signal_power = rx_pwr_all;
120 		/*  */
121 		/*  (3) Get Signal Quality (EVM) */
122 		/*  */
123 		/* if (pPktinfo->bPacketMatchBSSID) */
124 		{
125 			u8 SQ, SQ_rpt;
126 
127 			if (pPhyInfo->rx_pwd_ba11 > 40 && !pDM_Odm->bInHctTest)
128 				SQ = 100;
129 			else {
130 				SQ_rpt = pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all;
131 
132 				if (SQ_rpt > 64)
133 					SQ = 0;
134 				else if (SQ_rpt < 20)
135 					SQ = 100;
136 				else
137 					SQ = ((64-SQ_rpt) * 100) / 44;
138 
139 			}
140 
141 			pPhyInfo->signal_quality = SQ;
142 			pPhyInfo->rx_mimo_signal_quality[ODM_RF_PATH_A] = SQ;
143 			pPhyInfo->rx_mimo_signal_quality[ODM_RF_PATH_B] = -1;
144 		}
145 	} else { /* is OFDM rate */
146 		pDM_Odm->PhyDbgInfo.NumQryPhyStatusOFDM++;
147 
148 		/*  */
149 		/*  (1)Get RSSI for HT rate */
150 		/*  */
151 
152 		for (i = ODM_RF_PATH_A; i < ODM_RF_PATH_MAX; i++) {
153 			/*  2008/01/30 MH we will judge RF RX path now. */
154 			if (pDM_Odm->RFPathRxEnable & BIT(i))
155 				rf_rx_num++;
156 			/* else */
157 				/* continue; */
158 
159 			rx_pwr[i] = ((pPhyStaRpt->path_agc[i].gain&0x3F)*2) - 110;
160 
161 
162 			pPhyInfo->rx_pwr[i] = rx_pwr[i];
163 
164 			/* Translate DBM to percentage. */
165 			RSSI = odm_QueryRxPwrPercentage(rx_pwr[i]);
166 			total_rssi += RSSI;
167 
168 			pPhyInfo->rx_mimo_signal_strength[i] = (u8) RSSI;
169 
170 			/* Get Rx snr value in DB */
171 			pPhyInfo->rx_snr[i] = pDM_Odm->PhyDbgInfo.RxSNRdB[i] = (s32)(pPhyStaRpt->path_rxsnr[i]/2);
172 		}
173 
174 
175 		/*  */
176 		/*  (2)PWDB, Average PWDB calculated by hardware (for rate adaptive) */
177 		/*  */
178 		rx_pwr_all = (((pPhyStaRpt->cck_sig_qual_ofdm_pwdb_all) >> 1)&0x7f)-110;
179 
180 		PWDB_ALL_BT = PWDB_ALL = odm_QueryRxPwrPercentage(rx_pwr_all);
181 
182 		pPhyInfo->rx_pwd_ba11 = PWDB_ALL;
183 		pPhyInfo->bt_rx_rssi_percentage = PWDB_ALL_BT;
184 		pPhyInfo->rx_power = rx_pwr_all;
185 		pPhyInfo->recv_signal_power = rx_pwr_all;
186 
187 		{/* pMgntInfo->CustomerID != RT_CID_819x_Lenovo */
188 			/*  */
189 			/*  (3)EVM of HT rate */
190 			/*  */
191 			if (pPktinfo->data_rate >= DESC_RATEMCS8 && pPktinfo->data_rate <= DESC_RATEMCS15)
192 				Max_spatial_stream = 2; /* both spatial stream make sense */
193 			else
194 				Max_spatial_stream = 1; /* only spatial stream 1 makes sense */
195 
196 			for (i = 0; i < Max_spatial_stream; i++) {
197 				/*  Do not use shift operation like "rx_evmX >>= 1" because the compilor of free build environment */
198 				/*  fill most significant bit to "zero" when doing shifting operation which may change a negative */
199 				/*  value to positive one, then the dbm value (which is supposed to be negative)  is not correct anymore. */
200 				EVM = odm_EVMdbToPercentage((pPhyStaRpt->stream_rxevm[i]));	/* dbm */
201 
202 				/* if (pPktinfo->bPacketMatchBSSID) */
203 				{
204 					if (i == ODM_RF_PATH_A) /*  Fill value in RFD, Get the first spatial stream only */
205 						pPhyInfo->signal_quality = (u8)(EVM & 0xff);
206 
207 					pPhyInfo->rx_mimo_signal_quality[i] = (u8)(EVM & 0xff);
208 				}
209 			}
210 		}
211 
212 		ODM_ParsingCFO(pDM_Odm, pPktinfo, pPhyStaRpt->path_cfotail);
213 
214 	}
215 
216 	/* UI BSS List signal strength(in percentage), make it good looking, from 0~100. */
217 	/* It is assigned to the BSS List in GetValueFromBeaconOrProbeRsp(). */
218 	if (isCCKrate) {
219 		pPhyInfo->signal_strength = (u8)(odm_SignalScaleMapping(pDM_Odm, PWDB_ALL));/* PWDB_ALL; */
220 	} else {
221 		if (rf_rx_num != 0) {
222 			pPhyInfo->signal_strength = (u8)(odm_SignalScaleMapping(pDM_Odm, total_rssi /= rf_rx_num));
223 		}
224 	}
225 }
226 
227 static void odm_Process_RSSIForDM(
228 	struct dm_odm_t *pDM_Odm, struct odm_phy_info *pPhyInfo, struct odm_packet_info *pPktinfo
229 )
230 {
231 
232 	s32 UndecoratedSmoothedPWDB, UndecoratedSmoothedCCK, UndecoratedSmoothedOFDM, RSSI_Ave;
233 	u8 isCCKrate = 0;
234 	u8 RSSI_max, RSSI_min, i;
235 	u32 OFDM_pkt = 0;
236 	u32 Weighting = 0;
237 	PSTA_INFO_T pEntry;
238 
239 
240 	if (pPktinfo->station_id == 0xFF)
241 		return;
242 
243 	pEntry = pDM_Odm->pODM_StaInfo[pPktinfo->station_id];
244 
245 	if (!IS_STA_VALID(pEntry))
246 		return;
247 
248 	if ((!pPktinfo->bssid_match))
249 		return;
250 
251 	if (pPktinfo->is_beacon)
252 		pDM_Odm->PhyDbgInfo.NumQryBeaconPkt++;
253 
254 	isCCKrate = ((pPktinfo->data_rate <= DESC_RATE11M)) ? true : false;
255 	pDM_Odm->RxRate = pPktinfo->data_rate;
256 
257 	/* Statistic for antenna/path diversity------------------ */
258 	if (pDM_Odm->SupportAbility & ODM_BB_ANT_DIV) {
259 
260 	}
261 
262 	/* Smart Antenna Debug Message------------------ */
263 
264 	UndecoratedSmoothedCCK = pEntry->rssi_stat.UndecoratedSmoothedCCK;
265 	UndecoratedSmoothedOFDM = pEntry->rssi_stat.UndecoratedSmoothedOFDM;
266 	UndecoratedSmoothedPWDB = pEntry->rssi_stat.UndecoratedSmoothedPWDB;
267 
268 	if (pPktinfo->to_self || pPktinfo->is_beacon) {
269 
270 		if (!isCCKrate) { /* ofdm rate */
271 			if (pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_B] == 0) {
272 				RSSI_Ave = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A];
273 				pDM_Odm->RSSI_A = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A];
274 				pDM_Odm->RSSI_B = 0;
275 			} else {
276 				pDM_Odm->RSSI_A =  pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A];
277 				pDM_Odm->RSSI_B = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_B];
278 
279 				if (
280 					pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A] >
281 					pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_B]
282 				) {
283 					RSSI_max = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A];
284 					RSSI_min = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_B];
285 				} else {
286 					RSSI_max = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_B];
287 					RSSI_min = pPhyInfo->rx_mimo_signal_strength[ODM_RF_PATH_A];
288 				}
289 
290 				if ((RSSI_max-RSSI_min) < 3)
291 					RSSI_Ave = RSSI_max;
292 				else if ((RSSI_max-RSSI_min) < 6)
293 					RSSI_Ave = RSSI_max - 1;
294 				else if ((RSSI_max-RSSI_min) < 10)
295 					RSSI_Ave = RSSI_max - 2;
296 				else
297 					RSSI_Ave = RSSI_max - 3;
298 			}
299 
300 			/* 1 Process OFDM RSSI */
301 			if (UndecoratedSmoothedOFDM <= 0)	/*  initialize */
302 				UndecoratedSmoothedOFDM = pPhyInfo->rx_pwd_ba11;
303 			else {
304 				if (pPhyInfo->rx_pwd_ba11 > (u32)UndecoratedSmoothedOFDM) {
305 					UndecoratedSmoothedOFDM =
306 							((UndecoratedSmoothedOFDM*(Rx_Smooth_Factor-1)) +
307 							RSSI_Ave)/Rx_Smooth_Factor;
308 					UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM + 1;
309 				} else {
310 					UndecoratedSmoothedOFDM =
311 							((UndecoratedSmoothedOFDM*(Rx_Smooth_Factor-1)) +
312 							RSSI_Ave)/Rx_Smooth_Factor;
313 				}
314 			}
315 
316 			pEntry->rssi_stat.PacketMap = (pEntry->rssi_stat.PacketMap<<1) | BIT0;
317 
318 		} else {
319 			RSSI_Ave = pPhyInfo->rx_pwd_ba11;
320 			pDM_Odm->RSSI_A = (u8) pPhyInfo->rx_pwd_ba11;
321 			pDM_Odm->RSSI_B = 0;
322 
323 			/* 1 Process CCK RSSI */
324 			if (UndecoratedSmoothedCCK <= 0)	/*  initialize */
325 				UndecoratedSmoothedCCK = pPhyInfo->rx_pwd_ba11;
326 			else {
327 				if (pPhyInfo->rx_pwd_ba11 > (u32)UndecoratedSmoothedCCK) {
328 					UndecoratedSmoothedCCK =
329 							((UndecoratedSmoothedCCK*(Rx_Smooth_Factor-1)) +
330 							pPhyInfo->rx_pwd_ba11)/Rx_Smooth_Factor;
331 					UndecoratedSmoothedCCK = UndecoratedSmoothedCCK + 1;
332 				} else {
333 					UndecoratedSmoothedCCK =
334 							((UndecoratedSmoothedCCK*(Rx_Smooth_Factor-1)) +
335 							pPhyInfo->rx_pwd_ba11)/Rx_Smooth_Factor;
336 				}
337 			}
338 			pEntry->rssi_stat.PacketMap = pEntry->rssi_stat.PacketMap<<1;
339 		}
340 
341 		/* if (pEntry) */
342 		{
343 			/* 2011.07.28 LukeLee: modified to prevent unstable CCK RSSI */
344 			if (pEntry->rssi_stat.ValidBit >= 64)
345 				pEntry->rssi_stat.ValidBit = 64;
346 			else
347 				pEntry->rssi_stat.ValidBit++;
348 
349 			for (i = 0; i < pEntry->rssi_stat.ValidBit; i++)
350 				OFDM_pkt += (u8)(pEntry->rssi_stat.PacketMap>>i)&BIT0;
351 
352 			if (pEntry->rssi_stat.ValidBit == 64) {
353 				Weighting = ((OFDM_pkt<<4) > 64)?64:(OFDM_pkt<<4);
354 				UndecoratedSmoothedPWDB = (Weighting*UndecoratedSmoothedOFDM+(64-Weighting)*UndecoratedSmoothedCCK)>>6;
355 			} else {
356 				if (pEntry->rssi_stat.ValidBit != 0)
357 					UndecoratedSmoothedPWDB = (OFDM_pkt*UndecoratedSmoothedOFDM+(pEntry->rssi_stat.ValidBit-OFDM_pkt)*UndecoratedSmoothedCCK)/pEntry->rssi_stat.ValidBit;
358 				else
359 					UndecoratedSmoothedPWDB = 0;
360 			}
361 
362 			pEntry->rssi_stat.UndecoratedSmoothedCCK = UndecoratedSmoothedCCK;
363 			pEntry->rssi_stat.UndecoratedSmoothedOFDM = UndecoratedSmoothedOFDM;
364 			pEntry->rssi_stat.UndecoratedSmoothedPWDB = UndecoratedSmoothedPWDB;
365 		}
366 
367 	}
368 }
369 
370 
371 /*  */
372 /*  Endianness before calling this API */
373 /*  */
374 static void ODM_PhyStatusQuery_92CSeries(
375 	struct dm_odm_t *pDM_Odm,
376 	struct odm_phy_info *pPhyInfo,
377 	u8 *pPhyStatus,
378 	struct odm_packet_info *pPktinfo
379 )
380 {
381 
382 	odm_RxPhyStatus92CSeries_Parsing(pDM_Odm, pPhyInfo, pPhyStatus, pPktinfo);
383 
384 	if (!pDM_Odm->RSSI_test)
385 		odm_Process_RSSIForDM(pDM_Odm, pPhyInfo, pPktinfo);
386 }
387 
388 void ODM_PhyStatusQuery(
389 	struct dm_odm_t *pDM_Odm,
390 	struct odm_phy_info *pPhyInfo,
391 	u8 *pPhyStatus,
392 	struct odm_packet_info *pPktinfo
393 )
394 {
395 
396 	ODM_PhyStatusQuery_92CSeries(pDM_Odm, pPhyInfo, pPhyStatus, pPktinfo);
397 }
398 
399 /*  */
400 /*  If you want to add a new IC, Please follow below template and generate a new one. */
401 /*  */
402 /*  */
403 
404 enum hal_status ODM_ConfigRFWithHeaderFile(
405 	struct dm_odm_t *pDM_Odm,
406 	enum ODM_RF_Config_Type ConfigType,
407 	enum odm_rf_radio_path_e eRFPath
408 )
409 {
410 	if (ConfigType == CONFIG_RF_RADIO)
411 		READ_AND_CONFIG(8723B, _RadioA);
412 	else if (ConfigType == CONFIG_RF_TXPWR_LMT)
413 		READ_AND_CONFIG(8723B, _TXPWR_LMT);
414 
415 	return HAL_STATUS_SUCCESS;
416 }
417 
418 enum hal_status ODM_ConfigRFWithTxPwrTrackHeaderFile(struct dm_odm_t *pDM_Odm)
419 {
420 	if (pDM_Odm->SupportInterface == ODM_ITRF_SDIO)
421 		READ_AND_CONFIG(8723B, _TxPowerTrack_SDIO);
422 
423 	return HAL_STATUS_SUCCESS;
424 }
425 
426 enum hal_status ODM_ConfigBBWithHeaderFile(
427 	struct dm_odm_t *pDM_Odm, enum ODM_BB_Config_Type ConfigType
428 )
429 {
430 	if (ConfigType == CONFIG_BB_PHY_REG)
431 		READ_AND_CONFIG(8723B, _PHY_REG);
432 	else if (ConfigType == CONFIG_BB_AGC_TAB)
433 		READ_AND_CONFIG(8723B, _AGC_TAB);
434 	else if (ConfigType == CONFIG_BB_PHY_REG_PG)
435 		READ_AND_CONFIG(8723B, _PHY_REG_PG);
436 
437 	return HAL_STATUS_SUCCESS;
438 }
439 
440