• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*++
2 Copyright (c) Realtek Semiconductor Corp. All rights reserved.
3 
4 Module Name:
5 	RateAdaptive.c
6 
7 Abstract:
8 	Implement Rate Adaptive functions for common operations.
9 
10 Major Change History:
11 	When       Who               What
12 	---------- ---------------   -------------------------------
13 	2011-08-12 Page            Create.
14 
15 --*/
16 #include "odm_precomp.h"
17 
18 /*  Rate adaptive parameters */
19 
20 static u8 RETRY_PENALTY[PERENTRY][RETRYSIZE+1] = {
21 		{5, 4, 3, 2, 0, 3},      /* 92 , idx = 0 */
22 		{6, 5, 4, 3, 0, 4},      /* 86 , idx = 1 */
23 		{6, 5, 4, 2, 0, 4},      /* 81 , idx = 2 */
24 		{8, 7, 6, 4, 0, 6},      /* 75 , idx = 3 */
25 		{10, 9, 8, 6, 0, 8},     /* 71	, idx = 4 */
26 		{10, 9, 8, 4, 0, 8},     /* 66	, idx = 5 */
27 		{10, 9, 8, 2, 0, 8},     /* 62	, idx = 6 */
28 		{10, 9, 8, 0, 0, 8},     /* 59	, idx = 7 */
29 		{18, 17, 16, 8, 0, 16},  /* 53 , idx = 8 */
30 		{26, 25, 24, 16, 0, 24}, /* 50	, idx = 9 */
31 		{34, 33, 32, 24, 0, 32}, /* 47	, idx = 0x0a */
32 		{34, 31, 28, 20, 0, 32}, /* 43	, idx = 0x0b */
33 		{34, 31, 27, 18, 0, 32}, /* 40 , idx = 0x0c */
34 		{34, 31, 26, 16, 0, 32}, /* 37 , idx = 0x0d */
35 		{34, 30, 22, 16, 0, 32}, /* 32 , idx = 0x0e */
36 		{34, 30, 24, 16, 0, 32}, /* 26 , idx = 0x0f */
37 		{49, 46, 40, 16, 0, 48}, /* 20	, idx = 0x10 */
38 		{49, 45, 32, 0, 0, 48},  /* 17 , idx = 0x11 */
39 		{49, 45, 22, 18, 0, 48}, /* 15	, idx = 0x12 */
40 		{49, 40, 24, 16, 0, 48}, /* 12	, idx = 0x13 */
41 		{49, 32, 18, 12, 0, 48}, /* 9 , idx = 0x14 */
42 		{49, 22, 18, 14, 0, 48}, /* 6 , idx = 0x15 */
43 		{49, 16, 16, 0, 0, 48}
44 	}; /* 3, idx = 0x16 */
45 
46 static u8 PT_PENALTY[RETRYSIZE+1] = {34, 31, 30, 24, 0, 32};
47 
48 /*  wilson modify */
49 static u8 RETRY_PENALTY_IDX[2][RATESIZE] = {
50 		{4, 4, 4, 5, 4, 4, 5, 7, 7, 7, 8, 0x0a,	       /*  SS>TH */
51 		4, 4, 4, 4, 6, 0x0a, 0x0b, 0x0d,
52 		5, 5, 7, 7, 8, 0x0b, 0x0d, 0x0f},			   /*  0329 R01 */
53 		{0x0a, 0x0a, 0x0b, 0x0c, 0x0a,
54 		0x0a, 0x0b, 0x0c, 0x0d, 0x10, 0x13, 0x14,	   /*  SS<TH */
55 		0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x11, 0x13, 0x15,
56 		9, 9, 9, 9, 0x0c, 0x0e, 0x11, 0x13}
57 	};
58 
59 static u8 RETRY_PENALTY_UP_IDX[RATESIZE] = {
60 		0x0c, 0x0d, 0x0d, 0x0f, 0x0d, 0x0e,
61 		0x0f, 0x0f, 0x10, 0x12, 0x13, 0x14,	       /*  SS>TH */
62 		0x0f, 0x10, 0x10, 0x12, 0x12, 0x13, 0x14, 0x15,
63 		0x11, 0x11, 0x12, 0x13, 0x13, 0x13, 0x14, 0x15};
64 
65 static u8 RSSI_THRESHOLD[RATESIZE] = {
66 		0, 0, 0, 0,
67 		0, 0, 0, 0, 0, 0x24, 0x26, 0x2a,
68 		0x18, 0x1a, 0x1d, 0x1f, 0x21, 0x27, 0x29, 0x2a,
69 		0, 0, 0, 0x1f, 0x23, 0x28, 0x2a, 0x2c};
70 
71 static u16 N_THRESHOLD_HIGH[RATESIZE] = {
72 		4, 4, 8, 16,
73 		24, 36, 48, 72, 96, 144, 192, 216,
74 		60, 80, 100, 160, 240, 400, 560, 640,
75 		300, 320, 480, 720, 1000, 1200, 1600, 2000};
76 static u16 N_THRESHOLD_LOW[RATESIZE] = {
77 		2, 2, 4, 8,
78 		12, 18, 24, 36, 48, 72, 96, 108,
79 		30, 40, 50, 80, 120, 200, 280, 320,
80 		150, 160, 240, 360, 500, 600, 800, 1000};
81 
82 static u8 DROPING_NECESSARY[RATESIZE] = {
83 		1, 1, 1, 1,
84 		1, 2, 3, 4, 5, 6, 7, 8,
85 		1, 2, 3, 4, 5, 6, 7, 8,
86 		5, 6, 7, 8, 9, 10, 11, 12};
87 
88 static u8 PendingForRateUpFail[5] = {2, 10, 24, 40, 60};
89 static u16 DynamicTxRPTTiming[6] = {
90 	0x186a, 0x30d4, 0x493e, 0x61a8, 0x7a12 , 0x927c}; /*  200ms-1200ms */
91 
92 /*  End Rate adaptive parameters */
93 
odm_SetTxRPTTiming_8188E(struct odm_dm_struct * dm_odm,struct odm_ra_info * pRaInfo,u8 extend)94 static void odm_SetTxRPTTiming_8188E(
95 		struct odm_dm_struct *dm_odm,
96 		struct odm_ra_info *pRaInfo,
97 		u8 extend
98 	)
99 {
100 	u8 idx = 0;
101 
102 	for (idx = 0; idx < 5; idx++)
103 		if (DynamicTxRPTTiming[idx] == pRaInfo->RptTime)
104 			break;
105 
106 	if (extend == 0) { /*  back to default timing */
107 		idx = 0;  /* 200ms */
108 	} else if (extend == 1) {/*  increase the timing */
109 		idx += 1;
110 		if (idx > 5)
111 			idx = 5;
112 	} else if (extend == 2) {/*  decrease the timing */
113 		if (idx != 0)
114 			idx -= 1;
115 	}
116 	pRaInfo->RptTime = DynamicTxRPTTiming[idx];
117 
118 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("pRaInfo->RptTime = 0x%x\n", pRaInfo->RptTime));
119 }
120 
odm_RateDown_8188E(struct odm_dm_struct * dm_odm,struct odm_ra_info * pRaInfo)121 static int odm_RateDown_8188E(struct odm_dm_struct *dm_odm, struct odm_ra_info *pRaInfo)
122 {
123 	u8 RateID, LowestRate, HighestRate;
124 	u8 i;
125 
126 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("=====>odm_RateDown_8188E()\n"));
127 	if (NULL == pRaInfo) {
128 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("odm_RateDown_8188E(): pRaInfo is NULL\n"));
129 		return -1;
130 	}
131 	RateID = pRaInfo->PreRate;
132 	LowestRate = pRaInfo->LowestRate;
133 	HighestRate = pRaInfo->HighestRate;
134 
135 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
136 		     (" RateID =%d LowestRate =%d HighestRate =%d RateSGI =%d\n",
137 		     RateID, LowestRate, HighestRate, pRaInfo->RateSGI));
138 	if (RateID > HighestRate) {
139 		RateID = HighestRate;
140 	} else if (pRaInfo->RateSGI) {
141 		pRaInfo->RateSGI = 0;
142 	} else if (RateID > LowestRate) {
143 		if (RateID > 0) {
144 			for (i = RateID-1; i > LowestRate; i--) {
145 				if (pRaInfo->RAUseRate & BIT(i)) {
146 					RateID = i;
147 					goto RateDownFinish;
148 				}
149 			}
150 		}
151 	} else if (RateID <= LowestRate) {
152 		RateID = LowestRate;
153 	}
154 RateDownFinish:
155 	if (pRaInfo->RAWaitingCounter == 1) {
156 		pRaInfo->RAWaitingCounter += 1;
157 		pRaInfo->RAPendingCounter += 1;
158 	} else if (pRaInfo->RAWaitingCounter == 0) {
159 		;
160 	} else {
161 		pRaInfo->RAWaitingCounter = 0;
162 		pRaInfo->RAPendingCounter = 0;
163 	}
164 
165 	if (pRaInfo->RAPendingCounter >= 4)
166 		pRaInfo->RAPendingCounter = 4;
167 
168 	pRaInfo->DecisionRate = RateID;
169 	odm_SetTxRPTTiming_8188E(dm_odm, pRaInfo, 2);
170 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("Rate down, RPT Timing default\n"));
171 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("RAWaitingCounter %d, RAPendingCounter %d", pRaInfo->RAWaitingCounter, pRaInfo->RAPendingCounter));
172 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("Rate down to RateID %d RateSGI %d\n", RateID, pRaInfo->RateSGI));
173 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("<===== odm_RateDown_8188E()\n"));
174 	return 0;
175 }
176 
odm_RateUp_8188E(struct odm_dm_struct * dm_odm,struct odm_ra_info * pRaInfo)177 static int odm_RateUp_8188E(
178 		struct odm_dm_struct *dm_odm,
179 		struct odm_ra_info *pRaInfo
180 	)
181 {
182 	u8 RateID, HighestRate;
183 	u8 i;
184 
185 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("=====>odm_RateUp_8188E()\n"));
186 	if (NULL == pRaInfo) {
187 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("odm_RateUp_8188E(): pRaInfo is NULL\n"));
188 		return -1;
189 	}
190 	RateID = pRaInfo->PreRate;
191 	HighestRate = pRaInfo->HighestRate;
192 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
193 		     (" RateID =%d HighestRate =%d\n",
194 		     RateID, HighestRate));
195 	if (pRaInfo->RAWaitingCounter == 1) {
196 		pRaInfo->RAWaitingCounter = 0;
197 		pRaInfo->RAPendingCounter = 0;
198 	} else if (pRaInfo->RAWaitingCounter > 1) {
199 		pRaInfo->PreRssiStaRA = pRaInfo->RssiStaRA;
200 		goto RateUpfinish;
201 	}
202 	odm_SetTxRPTTiming_8188E(dm_odm, pRaInfo, 0);
203 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("odm_RateUp_8188E():Decrease RPT Timing\n"));
204 
205 	if (RateID < HighestRate) {
206 		for (i = RateID+1; i <= HighestRate; i++) {
207 			if (pRaInfo->RAUseRate & BIT(i)) {
208 				RateID = i;
209 				goto RateUpfinish;
210 			}
211 		}
212 	} else if (RateID == HighestRate) {
213 		if (pRaInfo->SGIEnable && (pRaInfo->RateSGI != 1))
214 			pRaInfo->RateSGI = 1;
215 		else if ((pRaInfo->SGIEnable) != 1)
216 			pRaInfo->RateSGI = 0;
217 	} else {
218 		RateID = HighestRate;
219 	}
220 RateUpfinish:
221 	if (pRaInfo->RAWaitingCounter == (4+PendingForRateUpFail[pRaInfo->RAPendingCounter]))
222 		pRaInfo->RAWaitingCounter = 0;
223 	else
224 		pRaInfo->RAWaitingCounter++;
225 
226 	pRaInfo->DecisionRate = RateID;
227 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("Rate up to RateID %d\n", RateID));
228 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("RAWaitingCounter %d, RAPendingCounter %d", pRaInfo->RAWaitingCounter, pRaInfo->RAPendingCounter));
229 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("<===== odm_RateUp_8188E()\n"));
230 	return 0;
231 }
232 
odm_ResetRaCounter_8188E(struct odm_ra_info * pRaInfo)233 static void odm_ResetRaCounter_8188E(struct odm_ra_info *pRaInfo)
234 {
235 	u8 RateID;
236 
237 	RateID = pRaInfo->DecisionRate;
238 	pRaInfo->NscUp = (N_THRESHOLD_HIGH[RateID]+N_THRESHOLD_LOW[RateID])>>1;
239 	pRaInfo->NscDown = (N_THRESHOLD_HIGH[RateID]+N_THRESHOLD_LOW[RateID])>>1;
240 }
241 
odm_RateDecision_8188E(struct odm_dm_struct * dm_odm,struct odm_ra_info * pRaInfo)242 static void odm_RateDecision_8188E(struct odm_dm_struct *dm_odm,
243 		struct odm_ra_info *pRaInfo
244 	)
245 {
246 	u8 RateID = 0, RtyPtID = 0, PenaltyID1 = 0, PenaltyID2 = 0;
247 	/* u32 pool_retry; */
248 	static u8 DynamicTxRPTTimingCounter;
249 
250 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("=====>odm_RateDecision_8188E()\n"));
251 
252 	if (pRaInfo->Active && (pRaInfo->TOTAL > 0)) { /*  STA used and data packet exits */
253 		if ((pRaInfo->RssiStaRA < (pRaInfo->PreRssiStaRA - 3)) ||
254 		    (pRaInfo->RssiStaRA > (pRaInfo->PreRssiStaRA + 3))) {
255 			pRaInfo->RAWaitingCounter = 0;
256 			pRaInfo->RAPendingCounter = 0;
257 		}
258 		/*  Start RA decision */
259 		if (pRaInfo->PreRate > pRaInfo->HighestRate)
260 			RateID = pRaInfo->HighestRate;
261 		else
262 			RateID = pRaInfo->PreRate;
263 		if (pRaInfo->RssiStaRA > RSSI_THRESHOLD[RateID])
264 			RtyPtID = 0;
265 		else
266 			RtyPtID = 1;
267 		PenaltyID1 = RETRY_PENALTY_IDX[RtyPtID][RateID]; /* TODO by page */
268 
269 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
270 			     (" NscDown init is %d\n", pRaInfo->NscDown));
271 		pRaInfo->NscDown += pRaInfo->RTY[0] * RETRY_PENALTY[PenaltyID1][0];
272 		pRaInfo->NscDown += pRaInfo->RTY[1] * RETRY_PENALTY[PenaltyID1][1];
273 		pRaInfo->NscDown += pRaInfo->RTY[2] * RETRY_PENALTY[PenaltyID1][2];
274 		pRaInfo->NscDown += pRaInfo->RTY[3] * RETRY_PENALTY[PenaltyID1][3];
275 		pRaInfo->NscDown += pRaInfo->RTY[4] * RETRY_PENALTY[PenaltyID1][4];
276 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
277 			     (" NscDown is %d, total*penalty[5] is %d\n",
278 			     pRaInfo->NscDown, (pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID1][5])));
279 		if (pRaInfo->NscDown > (pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID1][5]))
280 			pRaInfo->NscDown -= pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID1][5];
281 		else
282 			pRaInfo->NscDown = 0;
283 
284 		/*  rate up */
285 		PenaltyID2 = RETRY_PENALTY_UP_IDX[RateID];
286 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
287 			     (" NscUp init is %d\n", pRaInfo->NscUp));
288 		pRaInfo->NscUp += pRaInfo->RTY[0] * RETRY_PENALTY[PenaltyID2][0];
289 		pRaInfo->NscUp += pRaInfo->RTY[1] * RETRY_PENALTY[PenaltyID2][1];
290 		pRaInfo->NscUp += pRaInfo->RTY[2] * RETRY_PENALTY[PenaltyID2][2];
291 		pRaInfo->NscUp += pRaInfo->RTY[3] * RETRY_PENALTY[PenaltyID2][3];
292 		pRaInfo->NscUp += pRaInfo->RTY[4] * RETRY_PENALTY[PenaltyID2][4];
293 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
294 			     ("NscUp is %d, total*up[5] is %d\n",
295 			     pRaInfo->NscUp, (pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID2][5])));
296 		if (pRaInfo->NscUp > (pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID2][5]))
297 			pRaInfo->NscUp -= pRaInfo->TOTAL * RETRY_PENALTY[PenaltyID2][5];
298 		else
299 			pRaInfo->NscUp = 0;
300 
301 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE|ODM_COMP_INIT, ODM_DBG_LOUD,
302 			     (" RssiStaRa = %d RtyPtID =%d PenaltyID1 = 0x%x  PenaltyID2 = 0x%x RateID =%d NscDown =%d NscUp =%d SGI =%d\n",
303 			     pRaInfo->RssiStaRA, RtyPtID, PenaltyID1, PenaltyID2, RateID, pRaInfo->NscDown, pRaInfo->NscUp, pRaInfo->RateSGI));
304 		if ((pRaInfo->NscDown < N_THRESHOLD_LOW[RateID]) ||
305 		    (pRaInfo->DROP > DROPING_NECESSARY[RateID]))
306 			odm_RateDown_8188E(dm_odm, pRaInfo);
307 		else if (pRaInfo->NscUp > N_THRESHOLD_HIGH[RateID])
308 			odm_RateUp_8188E(dm_odm, pRaInfo);
309 
310 		if (pRaInfo->DecisionRate > pRaInfo->HighestRate)
311 			pRaInfo->DecisionRate = pRaInfo->HighestRate;
312 
313 		if ((pRaInfo->DecisionRate) == (pRaInfo->PreRate))
314 			DynamicTxRPTTimingCounter += 1;
315 		else
316 			DynamicTxRPTTimingCounter = 0;
317 
318 		if (DynamicTxRPTTimingCounter >= 4) {
319 			odm_SetTxRPTTiming_8188E(dm_odm, pRaInfo, 1);
320 			ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE,
321 				     ODM_DBG_LOUD, ("<===== Rate don't change 4 times, Extend RPT Timing\n"));
322 			DynamicTxRPTTimingCounter = 0;
323 		}
324 
325 		pRaInfo->PreRate = pRaInfo->DecisionRate;  /* YJ, add, 120120 */
326 
327 		odm_ResetRaCounter_8188E(pRaInfo);
328 	}
329 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, ("<===== odm_RateDecision_8188E()\n"));
330 }
331 
odm_ARFBRefresh_8188E(struct odm_dm_struct * dm_odm,struct odm_ra_info * pRaInfo)332 static int odm_ARFBRefresh_8188E(struct odm_dm_struct *dm_odm, struct odm_ra_info *pRaInfo)
333 {  /*  Wilson 2011/10/26 */
334 	struct adapter *adapt = dm_odm->Adapter;
335 	u32 MaskFromReg;
336 	s8 i;
337 
338 	switch (pRaInfo->RateID) {
339 	case RATR_INX_WIRELESS_NGB:
340 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x0f8ff015;
341 		break;
342 	case RATR_INX_WIRELESS_NG:
343 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x0f8ff010;
344 		break;
345 	case RATR_INX_WIRELESS_NB:
346 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x0f8ff005;
347 		break;
348 	case RATR_INX_WIRELESS_N:
349 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x0f8ff000;
350 		break;
351 	case RATR_INX_WIRELESS_GB:
352 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x00000ff5;
353 		break;
354 	case RATR_INX_WIRELESS_G:
355 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x00000ff0;
356 		break;
357 	case RATR_INX_WIRELESS_B:
358 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&0x0000000d;
359 		break;
360 	case 12:
361 		MaskFromReg = usb_read32(adapt, REG_ARFR0);
362 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&MaskFromReg;
363 		break;
364 	case 13:
365 		MaskFromReg = usb_read32(adapt, REG_ARFR1);
366 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&MaskFromReg;
367 		break;
368 	case 14:
369 		MaskFromReg = usb_read32(adapt, REG_ARFR2);
370 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&MaskFromReg;
371 		break;
372 	case 15:
373 		MaskFromReg = usb_read32(adapt, REG_ARFR3);
374 		pRaInfo->RAUseRate = (pRaInfo->RateMask)&MaskFromReg;
375 		break;
376 	default:
377 		pRaInfo->RAUseRate = (pRaInfo->RateMask);
378 		break;
379 	}
380 	/*  Highest rate */
381 	if (pRaInfo->RAUseRate) {
382 		for (i = RATESIZE; i >= 0; i--) {
383 			if ((pRaInfo->RAUseRate)&BIT(i)) {
384 				pRaInfo->HighestRate = i;
385 				break;
386 			}
387 		}
388 	} else {
389 		pRaInfo->HighestRate = 0;
390 	}
391 	/*  Lowest rate */
392 	if (pRaInfo->RAUseRate) {
393 		for (i = 0; i < RATESIZE; i++) {
394 			if ((pRaInfo->RAUseRate) & BIT(i)) {
395 				pRaInfo->LowestRate = i;
396 				break;
397 			}
398 		}
399 	} else {
400 		pRaInfo->LowestRate = 0;
401 	}
402 		if (pRaInfo->HighestRate > 0x13)
403 			pRaInfo->PTModeSS = 3;
404 		else if (pRaInfo->HighestRate > 0x0b)
405 			pRaInfo->PTModeSS = 2;
406 		else if (pRaInfo->HighestRate > 0x0b)
407 			pRaInfo->PTModeSS = 1;
408 		else
409 			pRaInfo->PTModeSS = 0;
410 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
411 		     ("ODM_ARFBRefresh_8188E(): PTModeSS =%d\n", pRaInfo->PTModeSS));
412 
413 	if (pRaInfo->DecisionRate > pRaInfo->HighestRate)
414 		pRaInfo->DecisionRate = pRaInfo->HighestRate;
415 
416 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
417 		     ("ODM_ARFBRefresh_8188E(): RateID =%d RateMask =%8.8x RAUseRate =%8.8x HighestRate =%d, DecisionRate =%d\n",
418 		     pRaInfo->RateID, pRaInfo->RateMask, pRaInfo->RAUseRate, pRaInfo->HighestRate, pRaInfo->DecisionRate));
419 	return 0;
420 }
421 
odm_PTTryState_8188E(struct odm_ra_info * pRaInfo)422 static void odm_PTTryState_8188E(struct odm_ra_info *pRaInfo)
423 {
424 	pRaInfo->PTTryState = 0;
425 	switch (pRaInfo->PTModeSS) {
426 	case 3:
427 		if (pRaInfo->DecisionRate >= 0x19)
428 			pRaInfo->PTTryState = 1;
429 		break;
430 	case 2:
431 		if (pRaInfo->DecisionRate >= 0x11)
432 			pRaInfo->PTTryState = 1;
433 		break;
434 	case 1:
435 		if (pRaInfo->DecisionRate >= 0x0a)
436 			pRaInfo->PTTryState = 1;
437 		break;
438 	case 0:
439 		if (pRaInfo->DecisionRate >= 0x03)
440 			pRaInfo->PTTryState = 1;
441 		break;
442 	default:
443 		pRaInfo->PTTryState = 0;
444 		break;
445 	}
446 
447 	if (pRaInfo->RssiStaRA < 48) {
448 		pRaInfo->PTStage = 0;
449 	} else if (pRaInfo->PTTryState == 1) {
450 		if ((pRaInfo->PTStopCount >= 10) ||
451 		    (pRaInfo->PTPreRssi > pRaInfo->RssiStaRA + 5) ||
452 		    (pRaInfo->PTPreRssi < pRaInfo->RssiStaRA - 5) ||
453 		    (pRaInfo->DecisionRate != pRaInfo->PTPreRate)) {
454 			if (pRaInfo->PTStage == 0)
455 				pRaInfo->PTStage = 1;
456 			else if (pRaInfo->PTStage == 1)
457 				pRaInfo->PTStage = 3;
458 			else
459 				pRaInfo->PTStage = 5;
460 
461 			pRaInfo->PTPreRssi = pRaInfo->RssiStaRA;
462 			pRaInfo->PTStopCount = 0;
463 		} else {
464 			pRaInfo->RAstage = 0;
465 			pRaInfo->PTStopCount++;
466 		}
467 	} else {
468 		pRaInfo->PTStage = 0;
469 		pRaInfo->RAstage = 0;
470 	}
471 	pRaInfo->PTPreRate = pRaInfo->DecisionRate;
472 }
473 
odm_PTDecision_8188E(struct odm_ra_info * pRaInfo)474 static void odm_PTDecision_8188E(struct odm_ra_info *pRaInfo)
475 {
476 	u8 j;
477 	u8 temp_stage;
478 	u32 numsc;
479 	u32 num_total;
480 	u8 stage_id;
481 
482 	numsc  = 0;
483 	num_total = pRaInfo->TOTAL * PT_PENALTY[5];
484 	for (j = 0; j <= 4; j++) {
485 		numsc += pRaInfo->RTY[j] * PT_PENALTY[j];
486 		if (numsc > num_total)
487 			break;
488 	}
489 
490 	j = j >> 1;
491 	temp_stage = (pRaInfo->PTStage + 1) >> 1;
492 	if (temp_stage > j)
493 		stage_id = temp_stage-j;
494 	else
495 		stage_id = 0;
496 
497 	pRaInfo->PTSmoothFactor = (pRaInfo->PTSmoothFactor>>1) + (pRaInfo->PTSmoothFactor>>2) + stage_id*16+2;
498 	if (pRaInfo->PTSmoothFactor > 192)
499 		pRaInfo->PTSmoothFactor = 192;
500 	stage_id = pRaInfo->PTSmoothFactor >> 6;
501 	temp_stage = stage_id*2;
502 	if (temp_stage != 0)
503 		temp_stage -= 1;
504 	if (pRaInfo->DROP > 3)
505 		temp_stage = 0;
506 	pRaInfo->PTStage = temp_stage;
507 }
508 
509 static void
odm_RATxRPTTimerSetting(struct odm_dm_struct * dm_odm,u16 minRptTime)510 odm_RATxRPTTimerSetting(
511 		struct odm_dm_struct *dm_odm,
512 		u16 minRptTime
513 )
514 {
515 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, (" =====>odm_RATxRPTTimerSetting()\n"));
516 
517 	if (dm_odm->CurrminRptTime != minRptTime) {
518 		ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
519 			     (" CurrminRptTime = 0x%04x minRptTime = 0x%04x\n", dm_odm->CurrminRptTime, minRptTime));
520 		rtw_rpt_timer_cfg_cmd(dm_odm->Adapter, minRptTime);
521 		dm_odm->CurrminRptTime = minRptTime;
522 	}
523 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE, (" <===== odm_RATxRPTTimerSetting()\n"));
524 }
525 
526 void
ODM_RASupport_Init(struct odm_dm_struct * dm_odm)527 ODM_RASupport_Init(
528 		struct odm_dm_struct *dm_odm
529 	)
530 {
531 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("=====>ODM_RASupport_Init()\n"));
532 
533 	dm_odm->RaSupport88E = true;
534 }
535 
ODM_RAInfo_Init(struct odm_dm_struct * dm_odm,u8 macid)536 int ODM_RAInfo_Init(struct odm_dm_struct *dm_odm, u8 macid)
537 {
538 	struct odm_ra_info *pRaInfo = &dm_odm->RAInfo[macid];
539 	u8 WirelessMode = 0xFF; /* invalid value */
540 	u8 max_rate_idx = 0x13; /* MCS7 */
541 	if (dm_odm->pWirelessMode != NULL)
542 		WirelessMode = *(dm_odm->pWirelessMode);
543 
544 	if (WirelessMode != 0xFF) {
545 		if (WirelessMode & ODM_WM_N24G)
546 			max_rate_idx = 0x13;
547 		else if (WirelessMode & ODM_WM_G)
548 			max_rate_idx = 0x0b;
549 		else if (WirelessMode & ODM_WM_B)
550 			max_rate_idx = 0x03;
551 	}
552 
553 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
554 		     ("ODM_RAInfo_Init(): WirelessMode:0x%08x , max_raid_idx:0x%02x\n",
555 		     WirelessMode, max_rate_idx));
556 
557 	pRaInfo->DecisionRate = max_rate_idx;
558 	pRaInfo->PreRate = max_rate_idx;
559 	pRaInfo->HighestRate = max_rate_idx;
560 	pRaInfo->LowestRate = 0;
561 	pRaInfo->RateID = 0;
562 	pRaInfo->RateMask = 0xffffffff;
563 	pRaInfo->RssiStaRA = 0;
564 	pRaInfo->PreRssiStaRA = 0;
565 	pRaInfo->SGIEnable = 0;
566 	pRaInfo->RAUseRate = 0xffffffff;
567 	pRaInfo->NscDown = (N_THRESHOLD_HIGH[0x13]+N_THRESHOLD_LOW[0x13])/2;
568 	pRaInfo->NscUp = (N_THRESHOLD_HIGH[0x13]+N_THRESHOLD_LOW[0x13])/2;
569 	pRaInfo->RateSGI = 0;
570 	pRaInfo->Active = 1;	/* Active is not used at present. by page, 110819 */
571 	pRaInfo->RptTime = 0x927c;
572 	pRaInfo->DROP = 0;
573 	pRaInfo->RTY[0] = 0;
574 	pRaInfo->RTY[1] = 0;
575 	pRaInfo->RTY[2] = 0;
576 	pRaInfo->RTY[3] = 0;
577 	pRaInfo->RTY[4] = 0;
578 	pRaInfo->TOTAL = 0;
579 	pRaInfo->RAWaitingCounter = 0;
580 	pRaInfo->RAPendingCounter = 0;
581 	pRaInfo->PTActive = 1;   /*  Active when this STA is use */
582 	pRaInfo->PTTryState = 0;
583 	pRaInfo->PTStage = 5; /*  Need to fill into HW_PWR_STATUS */
584 	pRaInfo->PTSmoothFactor = 192;
585 	pRaInfo->PTStopCount = 0;
586 	pRaInfo->PTPreRate = 0;
587 	pRaInfo->PTPreRssi = 0;
588 	pRaInfo->PTModeSS = 0;
589 	pRaInfo->RAstage = 0;
590 	return 0;
591 }
592 
ODM_RAInfo_Init_all(struct odm_dm_struct * dm_odm)593 int ODM_RAInfo_Init_all(struct odm_dm_struct *dm_odm)
594 {
595 	u8 macid = 0;
596 
597 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("=====>\n"));
598 	dm_odm->CurrminRptTime = 0;
599 
600 	for (macid = 0; macid < ODM_ASSOCIATE_ENTRY_NUM; macid++)
601 		ODM_RAInfo_Init(dm_odm, macid);
602 
603 	return 0;
604 }
605 
ODM_RA_GetShortGI_8188E(struct odm_dm_struct * dm_odm,u8 macid)606 u8 ODM_RA_GetShortGI_8188E(struct odm_dm_struct *dm_odm, u8 macid)
607 {
608 	if ((NULL == dm_odm) || (macid >= ASSOCIATE_ENTRY_NUM))
609 		return 0;
610 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
611 		     ("macid =%d SGI =%d\n", macid, dm_odm->RAInfo[macid].RateSGI));
612 	return dm_odm->RAInfo[macid].RateSGI;
613 }
614 
ODM_RA_GetDecisionRate_8188E(struct odm_dm_struct * dm_odm,u8 macid)615 u8 ODM_RA_GetDecisionRate_8188E(struct odm_dm_struct *dm_odm, u8 macid)
616 {
617 	u8 DecisionRate = 0;
618 
619 	if ((NULL == dm_odm) || (macid >= ASSOCIATE_ENTRY_NUM))
620 		return 0;
621 	DecisionRate = (dm_odm->RAInfo[macid].DecisionRate);
622 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
623 		(" macid =%d DecisionRate = 0x%x\n", macid, DecisionRate));
624 	return DecisionRate;
625 }
626 
ODM_RA_GetHwPwrStatus_8188E(struct odm_dm_struct * dm_odm,u8 macid)627 u8 ODM_RA_GetHwPwrStatus_8188E(struct odm_dm_struct *dm_odm, u8 macid)
628 {
629 	u8 PTStage = 5;
630 
631 	if ((NULL == dm_odm) || (macid >= ASSOCIATE_ENTRY_NUM))
632 		return 0;
633 	PTStage = (dm_odm->RAInfo[macid].PTStage);
634 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
635 		     ("macid =%d PTStage = 0x%x\n", macid, PTStage));
636 	return PTStage;
637 }
638 
ODM_RA_UpdateRateInfo_8188E(struct odm_dm_struct * dm_odm,u8 macid,u8 RateID,u32 RateMask,u8 SGIEnable)639 void ODM_RA_UpdateRateInfo_8188E(struct odm_dm_struct *dm_odm, u8 macid, u8 RateID, u32 RateMask, u8 SGIEnable)
640 {
641 	struct odm_ra_info *pRaInfo = NULL;
642 
643 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
644 		     ("macid =%d RateID = 0x%x RateMask = 0x%x SGIEnable =%d\n",
645 		     macid, RateID, RateMask, SGIEnable));
646 	if ((NULL == dm_odm) || (macid >= ASSOCIATE_ENTRY_NUM))
647 		return;
648 
649 	pRaInfo = &(dm_odm->RAInfo[macid]);
650 	pRaInfo->RateID = RateID;
651 	pRaInfo->RateMask = RateMask;
652 	pRaInfo->SGIEnable = SGIEnable;
653 	odm_ARFBRefresh_8188E(dm_odm, pRaInfo);
654 }
655 
ODM_RA_SetRSSI_8188E(struct odm_dm_struct * dm_odm,u8 macid,u8 Rssi)656 void ODM_RA_SetRSSI_8188E(struct odm_dm_struct *dm_odm, u8 macid, u8 Rssi)
657 {
658 	struct odm_ra_info *pRaInfo = NULL;
659 
660 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_TRACE,
661 		     (" macid =%d Rssi =%d\n", macid, Rssi));
662 	if ((NULL == dm_odm) || (macid >= ASSOCIATE_ENTRY_NUM))
663 		return;
664 
665 	pRaInfo = &(dm_odm->RAInfo[macid]);
666 	pRaInfo->RssiStaRA = Rssi;
667 }
668 
ODM_RA_Set_TxRPT_Time(struct odm_dm_struct * dm_odm,u16 minRptTime)669 void ODM_RA_Set_TxRPT_Time(struct odm_dm_struct *dm_odm, u16 minRptTime)
670 {
671 	struct adapter *adapt = dm_odm->Adapter;
672 
673 	usb_write16(adapt, REG_TX_RPT_TIME, minRptTime);
674 }
675 
ODM_RA_TxRPT2Handle_8188E(struct odm_dm_struct * dm_odm,u8 * TxRPT_Buf,u16 TxRPT_Len,u32 macid_entry0,u32 macid_entry1)676 void ODM_RA_TxRPT2Handle_8188E(struct odm_dm_struct *dm_odm, u8 *TxRPT_Buf, u16 TxRPT_Len, u32 macid_entry0, u32 macid_entry1)
677 {
678 	struct odm_ra_info *pRAInfo = NULL;
679 	u8 MacId = 0;
680 	u8 *pBuffer = NULL;
681 	u32 valid = 0, ItemNum = 0;
682 	u16 minRptTime = 0x927c;
683 
684 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
685 		     ("=====>ODM_RA_TxRPT2Handle_8188E(): valid0 =%d valid1 =%d BufferLength =%d\n",
686 		     macid_entry0, macid_entry1, TxRPT_Len));
687 
688 	ItemNum = TxRPT_Len >> 3;
689 	pBuffer = TxRPT_Buf;
690 
691 	do {
692 		if (MacId >= ASSOCIATE_ENTRY_NUM)
693 			valid = 0;
694 		else if (MacId >= 32)
695 			valid = (1 << (MacId - 32)) & macid_entry1;
696 		else
697 			valid = (1 << MacId) & macid_entry0;
698 
699 		pRAInfo = &(dm_odm->RAInfo[MacId]);
700 		if (valid) {
701 			pRAInfo->RTY[0] = (u16)GET_TX_REPORT_TYPE1_RERTY_0(pBuffer);
702 			pRAInfo->RTY[1] = (u16)GET_TX_REPORT_TYPE1_RERTY_1(pBuffer);
703 			pRAInfo->RTY[2] = (u16)GET_TX_REPORT_TYPE1_RERTY_2(pBuffer);
704 			pRAInfo->RTY[3] = (u16)GET_TX_REPORT_TYPE1_RERTY_3(pBuffer);
705 			pRAInfo->RTY[4] = (u16)GET_TX_REPORT_TYPE1_RERTY_4(pBuffer);
706 			pRAInfo->DROP =   (u16)GET_TX_REPORT_TYPE1_DROP_0(pBuffer);
707 			pRAInfo->TOTAL = pRAInfo->RTY[0] + pRAInfo->RTY[1] +
708 					 pRAInfo->RTY[2] + pRAInfo->RTY[3] +
709 					 pRAInfo->RTY[4] + pRAInfo->DROP;
710 			if (pRAInfo->TOTAL != 0) {
711 				ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD,
712 					     ("macid =%d Total =%d R0 =%d R1 =%d R2 =%d R3 =%d R4 =%d D0 =%d valid0 =%x valid1 =%x\n",
713 					     MacId, pRAInfo->TOTAL,
714 					     pRAInfo->RTY[0], pRAInfo->RTY[1],
715 					     pRAInfo->RTY[2], pRAInfo->RTY[3],
716 					     pRAInfo->RTY[4], pRAInfo->DROP,
717 					     macid_entry0 , macid_entry1));
718 				if (pRAInfo->PTActive) {
719 					if (pRAInfo->RAstage < 5)
720 						odm_RateDecision_8188E(dm_odm, pRAInfo);
721 					else if (pRAInfo->RAstage == 5) /*  Power training try state */
722 						odm_PTTryState_8188E(pRAInfo);
723 					else /*  RAstage == 6 */
724 						odm_PTDecision_8188E(pRAInfo);
725 
726 					/*  Stage_RA counter */
727 					if (pRAInfo->RAstage <= 5)
728 						pRAInfo->RAstage++;
729 					else
730 						pRAInfo->RAstage = 0;
731 				} else {
732 					odm_RateDecision_8188E(dm_odm, pRAInfo);
733 				}
734 				ODM_RT_TRACE(dm_odm, ODM_COMP_INIT, ODM_DBG_LOUD,
735 					     ("macid =%d R0 =%d R1 =%d R2 =%d R3 =%d R4 =%d drop =%d valid0 =%x RateID =%d SGI =%d\n",
736 					     MacId,
737 					     pRAInfo->RTY[0],
738 					     pRAInfo->RTY[1],
739 					     pRAInfo->RTY[2],
740 					     pRAInfo->RTY[3],
741 					     pRAInfo->RTY[4],
742 					     pRAInfo->DROP,
743 					     macid_entry0,
744 					     pRAInfo->DecisionRate,
745 					     pRAInfo->RateSGI));
746 			} else {
747 				ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, (" TOTAL = 0!!!!\n"));
748 			}
749 		}
750 
751 		if (minRptTime > pRAInfo->RptTime)
752 			minRptTime = pRAInfo->RptTime;
753 
754 		pBuffer += TX_RPT2_ITEM_SIZE;
755 		MacId++;
756 	} while (MacId < ItemNum);
757 
758 	odm_RATxRPTTimerSetting(dm_odm, minRptTime);
759 
760 	ODM_RT_TRACE(dm_odm, ODM_COMP_RATE_ADAPTIVE, ODM_DBG_LOUD, ("<===== ODM_RA_TxRPT2Handle_8188E()\n"));
761 }
762