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