1 //============================================================================
5 // MAC Throughput Optimization for W89C33 802.11g WLAN STA.
7 // The following MIB attributes or internal variables will be affected
8 // while the MTO is being executed:
9 // dot11FragmentationThreshold,
11 // transmission rate and PLCP preamble type,
16 // --------------------------------------------------------------------------
17 // 20031227 UN20 Pete Chao
19 // 20031229 Turbo copy from PD43
20 // 20040210 Kevin revised
21 // Copyright (c) 2003 Winbond Electronics Corp. All rights reserved.
22 //============================================================================
24 // LA20040210_DTO kevin
25 #include "os_common.h"
30 // Declare SQ3 to rate and fragmentation threshold table
31 // Declare fragmentation thresholds table
32 #define MTO_MAX_SQ3_LEVELS 14
33 #define MTO_MAX_FRAG_TH_LEVELS 5
34 #define MTO_MAX_DATA_RATE_LEVELS 12
36 u16 MTO_Frag_Th_Tbl[MTO_MAX_FRAG_TH_LEVELS] =
38 256, 384, 512, 768, 1536
41 u8 MTO_SQ3_Level[MTO_MAX_SQ3_LEVELS] =
43 0, 26, 30, 32, 34, 35, 37, 42, 44, 46, 54, 62, 78, 81
45 u8 MTO_SQ3toRate[MTO_MAX_SQ3_LEVELS] =
47 0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
49 u8 MTO_SQ3toFrag[MTO_MAX_SQ3_LEVELS] =
51 0, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4
54 // One Exchange Time table
56 u16 MTO_One_Exchange_Time_Tbl_l[MTO_MAX_FRAG_TH_LEVELS][MTO_MAX_DATA_RATE_LEVELS] =
58 { 2554, 1474, 822, 0, 0, 636, 0, 0, 0, 0, 0, 0},
59 { 3578, 1986, 1009, 0, 0, 729, 0, 0, 0, 0, 0, 0},
60 { 4602, 2498, 1195, 0, 0, 822, 0, 0, 0, 0, 0, 0},
61 { 6650, 3522, 1567, 0, 0, 1009, 0, 0, 0, 0, 0, 0},
62 {12794, 6594, 2684, 0, 0, 1567, 0, 0, 0, 0, 0, 0}
65 u16 MTO_One_Exchange_Time_Tbl_s[MTO_MAX_FRAG_TH_LEVELS][MTO_MAX_DATA_RATE_LEVELS] =
67 { 0, 1282, 630, 404, 288, 444, 232, 172, 144, 116, 100, 96},
68 { 0, 1794, 817, 572, 400, 537, 316, 228, 188, 144, 124, 116},
69 { 0, 2306, 1003, 744, 516, 630, 400, 288, 228, 172, 144, 136},
70 { 0, 3330, 1375, 1084, 744, 817, 572, 400, 316, 228, 188, 172},
71 { 0, 6402, 2492, 2108, 1424, 1375, 1084, 740, 572, 400, 316, 284}
74 #define MTO_ONE_EXCHANGE_TIME(preamble_type, frag_th_lvl, data_rate_lvl) \
75 (preamble_type) ? MTO_One_Exchange_Time_Tbl_s[frag_th_lvl][data_rate_lvl] : \
76 MTO_One_Exchange_Time_Tbl_l[frag_th_lvl][data_rate_lvl]
78 // Declare data rate table
79 //The following table will be changed at anytime if the opration rate supported by AP don't
81 u8 MTO_Data_Rate_Tbl[MTO_MAX_DATA_RATE_LEVELS] =
83 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
86 //The Stardard_Data_Rate_Tbl and Level2PerTbl table is used to indirectly retreive PER
87 //information from Rate_PER_TBL
88 //The default settings is AP can support full rate set.
89 static u8 Stardard_Data_Rate_Tbl[MTO_MAX_DATA_RATE_LEVELS] =
91 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
93 static u8 Level2PerTbl[MTO_MAX_DATA_RATE_LEVELS] =
95 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
97 //How many kind of tx rate can be supported by AP
98 //DTO will change Rate between MTO_Data_Rate_Tbl[0] and MTO_Data_Rate_Tbl[MTO_DataRateAvailableLevel-1]
99 static u8 MTO_DataRateAvailableLevel = MTO_MAX_DATA_RATE_LEVELS;
100 //Smoothed PER table for each different RATE based on packet length of 1514
101 static int Rate_PER_TBL[91][MTO_MAX_DATA_RATE_LEVELS] = {
102 // 1M 2M 5.5M 11M 6M 9M 12M 18M 24M 36M 48M 54M
103 /* 0% */{ 93, 177, 420, 538, 690, 774, 1001, 1401, 1768, 2358, 2838, 3039},
104 /* 1% */{ 92, 176, 416, 533, 683, 767, 992, 1389, 1752, 2336, 2811, 3010},
105 /* 2% */{ 91, 174, 412, 528, 675, 760, 983, 1376, 1735, 2313, 2783, 2979},
106 /* 3% */{ 90, 172, 407, 523, 667, 753, 973, 1363, 1719, 2290, 2755, 2948},
107 /* 4% */{ 90, 170, 403, 518, 659, 746, 964, 1350, 1701, 2266, 2726, 2916},
108 /* 5% */{ 89, 169, 398, 512, 651, 738, 954, 1336, 1684, 2242, 2696, 2884},
109 /* 6% */{ 88, 167, 394, 507, 643, 731, 944, 1322, 1666, 2217, 2665, 2851},
110 /* 7% */{ 87, 165, 389, 502, 635, 723, 935, 1308, 1648, 2192, 2634, 2817},
111 /* 8% */{ 86, 163, 384, 497, 626, 716, 924, 1294, 1629, 2166, 2602, 2782},
112 /* 9% */{ 85, 161, 380, 491, 618, 708, 914, 1279, 1611, 2140, 2570, 2747},
113 /* 10% */{ 84, 160, 375, 486, 609, 700, 904, 1265, 1591, 2113, 2537, 2711},
114 /* 11% */{ 83, 158, 370, 480, 600, 692, 894, 1250, 1572, 2086, 2503, 2675},
115 /* 12% */{ 82, 156, 365, 475, 592, 684, 883, 1234, 1552, 2059, 2469, 2638},
116 /* 13% */{ 81, 154, 360, 469, 583, 676, 872, 1219, 1532, 2031, 2435, 2600},
117 /* 14% */{ 80, 152, 355, 464, 574, 668, 862, 1204, 1512, 2003, 2400, 2562},
118 /* 15% */{ 79, 150, 350, 458, 565, 660, 851, 1188, 1492, 1974, 2365, 2524},
119 /* 16% */{ 78, 148, 345, 453, 556, 652, 840, 1172, 1471, 1945, 2329, 2485},
120 /* 17% */{ 77, 146, 340, 447, 547, 643, 829, 1156, 1450, 1916, 2293, 2446},
121 /* 18% */{ 76, 144, 335, 441, 538, 635, 818, 1140, 1429, 1887, 2256, 2406},
122 /* 19% */{ 75, 143, 330, 436, 529, 627, 807, 1124, 1408, 1857, 2219, 2366},
123 /* 20% */{ 74, 141, 325, 430, 520, 618, 795, 1107, 1386, 1827, 2182, 2326},
124 /* 21% */{ 73, 139, 320, 424, 510, 610, 784, 1091, 1365, 1797, 2145, 2285},
125 /* 22% */{ 72, 137, 314, 418, 501, 601, 772, 1074, 1343, 1766, 2107, 2244},
126 /* 23% */{ 71, 135, 309, 412, 492, 592, 761, 1057, 1321, 1736, 2069, 2203},
127 /* 24% */{ 70, 133, 304, 407, 482, 584, 749, 1040, 1299, 1705, 2031, 2161},
128 /* 25% */{ 69, 131, 299, 401, 473, 575, 738, 1023, 1277, 1674, 1992, 2120},
129 /* 26% */{ 68, 129, 293, 395, 464, 566, 726, 1006, 1254, 1642, 1953, 2078},
130 /* 27% */{ 67, 127, 288, 389, 454, 557, 714, 989, 1232, 1611, 1915, 2035},
131 /* 28% */{ 66, 125, 283, 383, 445, 549, 703, 972, 1209, 1579, 1876, 1993},
132 /* 29% */{ 65, 123, 278, 377, 436, 540, 691, 955, 1187, 1548, 1836, 1951},
133 /* 30% */{ 64, 121, 272, 371, 426, 531, 679, 937, 1164, 1516, 1797, 1908},
134 /* 31% */{ 63, 119, 267, 365, 417, 522, 667, 920, 1141, 1484, 1758, 1866},
135 /* 32% */{ 62, 117, 262, 359, 407, 513, 655, 902, 1118, 1453, 1719, 1823},
136 /* 33% */{ 61, 115, 256, 353, 398, 504, 643, 885, 1095, 1421, 1679, 1781},
137 /* 34% */{ 60, 113, 251, 347, 389, 495, 631, 867, 1072, 1389, 1640, 1738},
138 /* 35% */{ 59, 111, 246, 341, 379, 486, 619, 850, 1049, 1357, 1600, 1695},
139 /* 36% */{ 58, 108, 240, 335, 370, 477, 607, 832, 1027, 1325, 1561, 1653},
140 /* 37% */{ 57, 106, 235, 329, 361, 468, 595, 815, 1004, 1293, 1522, 1610},
141 /* 38% */{ 56, 104, 230, 323, 351, 459, 584, 797, 981, 1261, 1483, 1568},
142 /* 39% */{ 55, 102, 224, 317, 342, 450, 572, 780, 958, 1230, 1443, 1526},
143 /* 40% */{ 54, 100, 219, 311, 333, 441, 560, 762, 935, 1198, 1404, 1484},
144 /* 41% */{ 53, 98, 214, 305, 324, 432, 548, 744, 912, 1166, 1366, 1442},
145 /* 42% */{ 52, 96, 209, 299, 315, 423, 536, 727, 889, 1135, 1327, 1400},
146 /* 43% */{ 51, 94, 203, 293, 306, 414, 524, 709, 866, 1104, 1289, 1358},
147 /* 44% */{ 50, 92, 198, 287, 297, 405, 512, 692, 844, 1072, 1250, 1317},
148 /* 45% */{ 49, 90, 193, 281, 288, 396, 500, 675, 821, 1041, 1212, 1276},
149 /* 46% */{ 48, 88, 188, 275, 279, 387, 488, 657, 799, 1011, 1174, 1236},
150 /* 47% */{ 47, 86, 183, 269, 271, 378, 476, 640, 777, 980, 1137, 1195},
151 /* 48% */{ 46, 84, 178, 262, 262, 369, 464, 623, 754, 949, 1100, 1155},
152 /* 49% */{ 45, 82, 173, 256, 254, 360, 452, 606, 732, 919, 1063, 1116},
153 /* 50% */{ 44, 80, 168, 251, 245, 351, 441, 589, 710, 889, 1026, 1076},
154 /* 51% */{ 43, 78, 163, 245, 237, 342, 429, 572, 689, 860, 990, 1038},
155 /* 52% */{ 42, 76, 158, 239, 228, 333, 417, 555, 667, 830, 955, 999},
156 /* 53% */{ 41, 74, 153, 233, 220, 324, 406, 538, 645, 801, 919, 961},
157 /* 54% */{ 40, 72, 148, 227, 212, 315, 394, 522, 624, 773, 884, 924},
158 /* 55% */{ 39, 70, 143, 221, 204, 307, 383, 505, 603, 744, 850, 887},
159 /* 56% */{ 38, 68, 138, 215, 196, 298, 371, 489, 582, 716, 816, 851},
160 /* 57% */{ 37, 67, 134, 209, 189, 289, 360, 473, 562, 688, 783, 815},
161 /* 58% */{ 36, 65, 129, 203, 181, 281, 349, 457, 541, 661, 750, 780},
162 /* 59% */{ 35, 63, 124, 197, 174, 272, 338, 441, 521, 634, 717, 745},
163 /* 60% */{ 34, 61, 120, 192, 166, 264, 327, 425, 501, 608, 686, 712},
164 /* 61% */{ 33, 59, 115, 186, 159, 255, 316, 409, 482, 582, 655, 678},
165 /* 62% */{ 32, 57, 111, 180, 152, 247, 305, 394, 462, 556, 624, 646},
166 /* 63% */{ 31, 55, 107, 174, 145, 238, 294, 379, 443, 531, 594, 614},
167 /* 64% */{ 30, 53, 102, 169, 138, 230, 283, 364, 425, 506, 565, 583},
168 /* 65% */{ 29, 52, 98, 163, 132, 222, 273, 349, 406, 482, 536, 553},
169 /* 66% */{ 28, 50, 94, 158, 125, 214, 262, 334, 388, 459, 508, 523},
170 /* 67% */{ 27, 48, 90, 152, 119, 206, 252, 320, 370, 436, 481, 495},
171 /* 68% */{ 26, 46, 86, 147, 113, 198, 242, 306, 353, 413, 455, 467},
172 /* 69% */{ 26, 44, 82, 141, 107, 190, 231, 292, 336, 391, 429, 440},
173 /* 70% */{ 25, 43, 78, 136, 101, 182, 221, 278, 319, 370, 405, 414},
174 /* 71% */{ 24, 41, 74, 130, 95, 174, 212, 265, 303, 350, 381, 389},
175 /* 72% */{ 23, 39, 71, 125, 90, 167, 202, 252, 287, 329, 358, 365},
176 /* 73% */{ 22, 37, 67, 119, 85, 159, 192, 239, 271, 310, 335, 342},
177 /* 74% */{ 21, 36, 63, 114, 80, 151, 183, 226, 256, 291, 314, 320},
178 /* 75% */{ 20, 34, 60, 109, 75, 144, 174, 214, 241, 273, 294, 298},
179 /* 76% */{ 19, 32, 57, 104, 70, 137, 164, 202, 227, 256, 274, 278},
180 /* 77% */{ 18, 31, 53, 99, 66, 130, 155, 190, 213, 239, 256, 259},
181 /* 78% */{ 17, 29, 50, 94, 62, 122, 146, 178, 200, 223, 238, 241},
182 /* 79% */{ 16, 28, 47, 89, 58, 115, 138, 167, 187, 208, 222, 225},
183 /* 80% */{ 16, 26, 44, 84, 54, 109, 129, 156, 175, 194, 206, 209},
184 /* 81% */{ 15, 24, 41, 79, 50, 102, 121, 146, 163, 180, 192, 194},
185 /* 82% */{ 14, 23, 39, 74, 47, 95, 113, 136, 151, 167, 178, 181},
186 /* 83% */{ 13, 21, 36, 69, 44, 89, 105, 126, 140, 155, 166, 169},
187 /* 84% */{ 12, 20, 33, 64, 41, 82, 97, 116, 130, 144, 155, 158},
188 /* 85% */{ 11, 19, 31, 60, 39, 76, 89, 107, 120, 134, 145, 149},
189 /* 86% */{ 11, 17, 29, 55, 36, 70, 82, 98, 110, 125, 136, 140},
190 /* 87% */{ 10, 16, 26, 51, 34, 64, 75, 90, 102, 116, 128, 133},
191 /* 88% */{ 9, 14, 24, 46, 32, 58, 68, 81, 93, 108, 121, 128},
192 /* 89% */{ 8, 13, 22, 42, 31, 52, 61, 74, 86, 102, 116, 124},
193 /* 90% */{ 7, 12, 21, 37, 29, 46, 54, 66, 79, 96, 112, 121}
196 #define RSSIBUF_NUM 10
197 #define RSSI2RATE_SIZE 9
199 static TXRETRY_REC TxRateRec={MTO_MAX_DATA_RATE_LEVELS - 1, 0}; //new record=>TxRateRec
200 static int TxRetryRate;
201 //static int SQ3, BSS_PK_CNT, NIDLESLOT, SLOT_CNT, INTERF_CNT, GAP_CNT, DS_EVM;
202 static s32 RSSIBuf[RSSIBUF_NUM]={-70, -70, -70, -70, -70, -70, -70, -70, -70, -70};
203 static s32 RSSISmoothed=-700;
204 static int RSSIBufIndex=0;
205 static u8 max_rssi_rate;
206 static int rate_tbl[13] = {0,1,2,5,11,6,9,12,18,24,36,48,54};
207 //[WKCHEN]static core_data_t *pMTOcore_data=NULL;
209 static int TotalTxPkt = 0;
210 static int TotalTxPktRetry = 0;
211 static int TxPktPerAnt[3] = {0,0,0};
212 static int TxPktRetryPerAnt[3] = {0,0,0};
213 //static int TxDominateFlag=false;
214 static u8 old_antenna[4]={1 ,0 ,1 ,0};
215 static int retryrate_rec[MTO_MAX_DATA_RATE_LEVELS];//this record the retry rate at different data rate
217 static int PeriodTotalTxPkt = 0;
218 static int PeriodTotalTxPktRetry = 0;
226 static RSSI2RATE RSSI2RateTbl[RSSI2RATE_SIZE] =
238 static u8 last_rate_ant; //this is used for antenna backoff-hh
240 u8 boSparseTxTraffic = false;
242 void MTO_Init(MTO_FUNC_INPUT);
243 void GetFreshAntennaData(MTO_FUNC_INPUT);
244 void TxRateReductionCtrl(MTO_FUNC_INPUT);
245 /** 1.1.31.1000 Turbo modify */
246 //void MTO_SetDTORateRange(int type);
247 void MTO_SetDTORateRange(MTO_FUNC_INPUT, u8 *pRateArray, u8 ArraySize);
248 void MTO_SetTxCount(MTO_FUNC_INPUT, u8 t0, u8 index);
249 void MTO_TxFailed(MTO_FUNC_INPUT);
250 void SmoothRSSI(s32 new_rssi);
251 void hal_get_dto_para(MTO_FUNC_INPUT, char *buffer);
252 u8 CalcNewRate(MTO_FUNC_INPUT, u8 old_rate, u32 retry_cnt, u32 tx_frag_cnt);
253 u8 GetMaxRateLevelFromRSSI(void);
254 u8 MTO_GetTxFallbackRate(MTO_FUNC_INPUT);
255 int Divide(int a, int b);
257 //===========================================================================
261 // Set DTO Tx Rate Scope because different AP could have different Rate set.
262 // After our staion join with AP, LM core will call this function to initialize
266 // pRateArray - The pointer to the Tx Rate Array by the following order
267 // - 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
268 // - DTO won't check whether rate order is invalid or not
269 // ArraySize - The array size to indicate how many tx rate we can choose
273 // u8 RateArray[4] = {2, 4, 11, 22};
274 // MTO_SetDTORateRange(RateArray, 4);
279 //============================================================================
280 void MTO_SetDTORateRange(MTO_FUNC_INPUT,u8 *pRateArray, u8 ArraySize)
284 for(i=0;i<ArraySize;i++)
286 if(pRateArray[i] == 22)
289 if(i < ArraySize) //we need adjust the order of rate list because 11Mbps rate exists
293 if(pRateArray[i-1] <= 11)
295 pRateArray[i] = pRateArray[i-1];
298 MTO_OFDM_RATE_LEVEL() = i;
302 for(i=0; i<ArraySize; i++)
304 if (pRateArray[i] >= 12)
307 MTO_OFDM_RATE_LEVEL() = i;
310 for(i=0;i<ArraySize;i++)
312 MTO_Data_Rate_Tbl[i] = pRateArray[i];
313 for(;j<MTO_MAX_DATA_RATE_LEVELS;j++)
315 if(Stardard_Data_Rate_Tbl[j] == pRateArray[i])
320 WBDEBUG(("[MTO]:Op Rate[%d]: %d\n",i, MTO_Data_Rate_Tbl[i]));
323 MTO_DataRateAvailableLevel = ArraySize;
324 if( MTO_DATA().RatePolicy ) // 0 means that no registry setting
326 if( MTO_DATA().RatePolicy == 1 )
327 TxRateRec.tx_rate = 0; //ascent
329 TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ; //descent
333 if( MTO_INITTXRATE_MODE )
334 TxRateRec.tx_rate = 0; //ascent
336 TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ; //descent
338 TxRateRec.tx_retry_rate = 0;
339 //set default rate for initial use
340 MTO_RATE_LEVEL() = TxRateRec.tx_rate;
341 MTO_FALLBACK_RATE_LEVEL() = MTO_RATE_LEVEL();
344 //===========================================================================
348 // Initialize MTO parameters.
350 // This function should be invoked during system initialization.
353 // adapter - The pointer to the Miniport adapter Context
357 //============================================================================
358 void MTO_Init(MTO_FUNC_INPUT)
361 //WBDEBUG(("[MTO] -> MTO_Init()\n"));
362 //[WKCHEN]pMTOcore_data = pcore_data;
363 // 20040510 Turbo add for global variable
365 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
366 MTO_TX_RATE_REDUCTION_STATE() = RATE_CHGSTATE_IDLE;
367 MTO_BACKOFF_TMR() = 0;
368 MTO_LAST_RATE() = 11;
369 MTO_CO_EFFICENT() = 0;
371 //MTO_TH_FIXANT() = MTO_DEFAULT_TH_FIXANT;
372 MTO_TH_CNT() = MTO_DEFAULT_TH_CNT;
373 MTO_TH_SQ3() = MTO_DEFAULT_TH_SQ3;
374 MTO_TH_IDLE_SLOT() = MTO_DEFAULT_TH_IDLE_SLOT;
375 MTO_TH_PR_INTERF() = MTO_DEFAULT_TH_PR_INTERF;
377 MTO_TMR_AGING() = MTO_DEFAULT_TMR_AGING;
378 MTO_TMR_PERIODIC() = MTO_DEFAULT_TMR_PERIODIC;
380 //[WKCHEN]MTO_CCA_MODE_SETUP()= (u8) hal_get_cca_mode(MTO_HAL());
381 //[WKCHEN]MTO_CCA_MODE() = MTO_CCA_MODE_SETUP();
383 //MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_LONG;
384 MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_SHORT; // for test
386 MTO_ANT_SEL() = hal_get_antenna_number(MTO_HAL());
387 MTO_ANT_MAC() = MTO_ANT_SEL();
392 MTO_ANT_DIVERSITY() = MTO_ANTENNA_DIVERSITY_ON;
393 //CardSet_AntennaDiversity(adapter, MTO_ANT_DIVERSITY());
394 //PLMESetAntennaDiversity( adapter, MTO_ANT_DIVERSITY());
396 MTO_AGING_TIMEOUT() = 0;//MTO_TMR_AGING() / MTO_TMR_PERIODIC();
398 // The following parameters should be initialized to the values set by user
400 //MTO_RATE_LEVEL() = 10;
401 MTO_RATE_LEVEL() = 0;
402 MTO_FALLBACK_RATE_LEVEL() = MTO_RATE_LEVEL();
403 MTO_FRAG_TH_LEVEL() = 4;
404 /** 1.1.23.1000 Turbo modify from -1 to +1
405 MTO_RTS_THRESHOLD() = MTO_FRAG_TH() - 1;
406 MTO_RTS_THRESHOLD_SETUP() = MTO_FRAG_TH() - 1;
408 MTO_RTS_THRESHOLD() = MTO_FRAG_TH() + 1;
409 MTO_RTS_THRESHOLD_SETUP() = MTO_FRAG_TH() + 1;
410 // 1.1.23.1000 Turbo add for mto change preamble from 0 to 1
411 MTO_RATE_CHANGE_ENABLE() = 1;
412 MTO_FRAG_CHANGE_ENABLE() = 0; // 1.1.29.1000 Turbo add don't support frag
413 //The default valud of ANTDIV_DEFAULT_ON will be decided by EEPROM
414 //#ifdef ANTDIV_DEFAULT_ON
415 //MTO_ANT_DIVERSITY_ENABLE() = 1;
417 //MTO_ANT_DIVERSITY_ENABLE() = 0;
419 MTO_POWER_CHANGE_ENABLE() = 1;
420 MTO_PREAMBLE_CHANGE_ENABLE()= 1;
421 MTO_RTS_CHANGE_ENABLE() = 0; // 1.1.29.1000 Turbo add don't support frag
422 // 20040512 Turbo add
423 //old_antenna[0] = 1;
424 //old_antenna[1] = 0;
425 //old_antenna[2] = 1;
426 //old_antenna[3] = 0;
427 for (i=0;i<MTO_MAX_DATA_RATE_LEVELS;i++)
430 MTO_TXFLOWCOUNT() = 0;
431 //--------- DTO threshold parameters -------------
432 //MTOPARA_PERIODIC_CHECK_CYCLE() = 50;
433 MTOPARA_PERIODIC_CHECK_CYCLE() = 10;
434 MTOPARA_RSSI_TH_FOR_ANTDIV() = 10;
435 MTOPARA_TXCOUNT_TH_FOR_CALC_RATE() = 50;
436 MTOPARA_TXRATE_INC_TH() = 10;
437 MTOPARA_TXRATE_DEC_TH() = 30;
438 MTOPARA_TXRATE_EQ_TH() = 40;
439 MTOPARA_TXRATE_BACKOFF() = 12;
440 MTOPARA_TXRETRYRATE_REDUCE() = 6;
441 if ( MTO_TXPOWER_FROM_EEPROM == 0xff)
443 switch( MTO_HAL()->phy_type)
446 case RF_AIROHA_2230S: // 20060420 Add this
447 MTOPARA_TXPOWER_INDEX() = 46; // MAX-8 // @@ Only for AL 2230
450 MTOPARA_TXPOWER_INDEX() = 49;
453 MTOPARA_TXPOWER_INDEX() = 10;
456 MTOPARA_TXPOWER_INDEX() = 24; // ->10 20060316.1 modify
460 else //follow the setting from EEPROM
461 MTOPARA_TXPOWER_INDEX() = MTO_TXPOWER_FROM_EEPROM;
462 hal_set_rf_power(MTO_HAL(), (u8)MTOPARA_TXPOWER_INDEX());
463 //------------------------------------------------
465 // For RSSI turning 20060808.4 Cancel load from EEPROM
466 MTO_DATA().RSSI_high = -41;
467 MTO_DATA().RSSI_low = -60;
470 //---------------------------------------------------------------------------//
471 static u32 DTO_Rx_Info[13][3];
472 static u32 DTO_RxCRCFail_Info[13][3];
473 static u32 AntennaToggleBkoffTimer=5;
479 RXRATE_ANT RxRatePeakAnt[3];
485 void SearchPeakRxRate(int index)
488 RxRatePeakAnt[index].RxRatePkts=0;
489 //Find out the best rx rate which is used on different antenna
492 if(DTO_Rx_Info[i][index] > (u32) RxRatePeakAnt[index].RxRatePkts)
494 RxRatePeakAnt[index].RxRatePkts = DTO_Rx_Info[i][index];
495 RxRatePeakAnt[index].RxRate = rate_tbl[i];
496 RxRatePeakAnt[index].index = i;
501 void ResetDTO_RxInfo(int index, MTO_FUNC_INPUT)
506 WBDEBUG(("ResetDTOrx\n"));
510 DTO_Rx_Info[i][index] = MTO_HAL()->rx_ok_count[i];
513 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i];
519 void GetDTO_RxInfo(int index, MTO_FUNC_INPUT)
524 WBDEBUG(("GetDTOrx\n"));
527 //PDEBUG(("[MTO]:DTO_Rx_Info[%d]=%d, rx_ok_count=%d\n", index, DTO_Rx_Info[0][index], phw_data->rx_ok_count[0]));
529 DTO_Rx_Info[i][index] = abs(MTO_HAL()->rx_ok_count[i] - DTO_Rx_Info[i][index]);
530 if(DTO_Rx_Info[0][index]==0) DTO_Rx_Info[0][index] = 1;
533 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i] - DTO_RxCRCFail_Info[i][index];
535 TxPktPerAnt[index] = TotalTxPkt;
536 TxPktRetryPerAnt[index] = TotalTxPktRetry;
541 unsigned char TxDominate(int index)
545 tmp = TxPktPerAnt[index] + DTO_Rx_Info[0][index];
547 if(Divide(TxPktPerAnt[index]*100, tmp) > 40)
553 unsigned char CmpTxRetryRate(int index1, int index2)
555 int tx_retry_rate1, tx_retry_rate2;
556 tx_retry_rate1 = Divide((TxPktRetryPerAnt[index1] - TxPktPerAnt[index1])*100, TxPktRetryPerAnt[index1]);
557 tx_retry_rate2 = Divide((TxPktRetryPerAnt[index2] - TxPktPerAnt[index2])*100, TxPktRetryPerAnt[index2]);
559 WBDEBUG(("[MTO]:TxRetry Ant0: (%d%%) Ant1: (%d%%) \n ", tx_retry_rate1, tx_retry_rate2));
562 if(tx_retry_rate1 > tx_retry_rate2)
568 void GetFreshAntennaData(MTO_FUNC_INPUT)
572 x = hal_get_antenna_number(MTO_HAL());
573 //hal_get_bss_pk_cnt(MTO_HAL());
574 //hal_get_est_sq3(MTO_HAL(), 1);
576 //if this is the function for timer
577 ResetDTO_RxInfo(x, MTO_FUNC_INPUT_DATA);
578 if(AntennaToggleBkoffTimer)
579 AntennaToggleBkoffTimer--;
580 if (abs(last_rate_ant-MTO_RATE_LEVEL())>1) //backoff timer reset
581 AntennaToggleBkoffTimer=0;
583 if (MTO_ANT_DIVERSITY() != MTO_ANTENNA_DIVERSITY_ON ||
584 MTO_ANT_DIVERSITY_ENABLE() != 1)
585 AntennaToggleBkoffTimer=1;
587 WBDEBUG(("[HHDTO]:**last data rate=%d,now data rate=%d**antenna toggle timer=%d",last_rate_ant,MTO_RATE_LEVEL(),AntennaToggleBkoffTimer));
589 last_rate_ant=MTO_RATE_LEVEL();
590 if(AntennaToggleBkoffTimer==0)
592 MTO_TOGGLE_STATE() = TOGGLE_STATE_WAIT0;
594 WBDEBUG(("[HHDTO]:===state is starting==for antenna toggle==="));
598 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
600 if ((MTO_BACKOFF_TMR()!=0)&&(MTO_RATE_LEVEL()>MTO_DataRateAvailableLevel - 3))
602 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
604 WBDEBUG(("[HHDTO]:===the data rate is %d (good)and will not toogle ===",MTO_DATA_RATE()>>1));
611 int WB_PCR[2]; //packet correct rate
613 u8 CalcNewRate(MTO_FUNC_INPUT, u8 old_rate, u32 retry_cnt, u32 tx_frag_cnt)
618 int TxThrouput1, TxThrouput2, TxThrouput3, BestThroupht;
620 if(tx_frag_cnt < MTOPARA_TXCOUNT_TH_FOR_CALC_RATE()) //too few packets transmit
624 retry_rate = Divide(retry_cnt * 100, tx_frag_cnt);
626 if(retry_rate > 90) retry_rate = 90; //always truncate to 90% due to lookup table size
628 WBDEBUG(("##### Current level =%d, Retry count =%d, Frag count =%d\n",
629 old_rate, retry_cnt, tx_frag_cnt));
630 WBDEBUG(("*##* Retry rate =%d, throughput =%d\n",
631 retry_rate, Rate_PER_TBL[retry_rate][old_rate]));
632 WBDEBUG(("TxRateRec.tx_rate =%d, Retry rate = %d, throughput = %d\n",
633 TxRateRec.tx_rate, TxRateRec.tx_retry_rate,
634 Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]]));
635 WBDEBUG(("old_rate-1 =%d, Retry rate = %d, throughput = %d\n",
636 old_rate-1, retryrate_rec[old_rate-1],
637 Rate_PER_TBL[retryrate_rec[old_rate-1]][old_rate-1]));
638 WBDEBUG(("old_rate+1 =%d, Retry rate = %d, throughput = %d\n",
639 old_rate+1, retryrate_rec[old_rate+1],
640 Rate_PER_TBL[retryrate_rec[old_rate+1]][old_rate+1]));
643 //following is for record the retry rate at the different data rate
644 if (abs(retry_rate-retryrate_rec[old_rate])<50)//---the per TH
645 retryrate_rec[old_rate] = retry_rate; //update retry rate
648 for (i=0;i<MTO_DataRateAvailableLevel;i++) //reset all retry rate
650 retryrate_rec[old_rate] = retry_rate;
652 WBDEBUG(("Reset retry rate table\n"));
656 if(TxRateRec.tx_rate > old_rate) //Decrease Tx Rate
658 TxThrouput1 = Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]];
659 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
660 if(TxThrouput1 > TxThrouput2)
662 new_rate = TxRateRec.tx_rate;
663 BestThroupht = TxThrouput1;
668 BestThroupht = TxThrouput2;
670 if((old_rate > 0) &&(retry_rate>MTOPARA_TXRATE_DEC_TH())) //Min Rate
672 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
673 if(BestThroupht < TxThrouput3)
675 new_rate = old_rate - 1;
677 WBDEBUG(("--------\n"));
679 BestThroupht = TxThrouput3;
683 else if(TxRateRec.tx_rate < old_rate) //Increase Tx Rate
685 TxThrouput1 = Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]];
686 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
687 if(TxThrouput1 > TxThrouput2)
689 new_rate = TxRateRec.tx_rate;
690 BestThroupht = TxThrouput1;
695 BestThroupht = TxThrouput2;
697 if ((old_rate < MTO_DataRateAvailableLevel - 1)&&(retry_rate<MTOPARA_TXRATE_INC_TH()))
699 //TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
700 if (retryrate_rec[old_rate+1] > MTOPARA_TXRETRYRATE_REDUCE())
701 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]-MTOPARA_TXRETRYRATE_REDUCE()][Level2PerTbl[old_rate+1]];
703 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
704 if(BestThroupht < TxThrouput3)
706 new_rate = old_rate + 1;
708 WBDEBUG(("++++++++++\n"));
710 BestThroupht = TxThrouput3;
714 else //Tx Rate no change
716 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
718 BestThroupht = TxThrouput2;
720 if (retry_rate <MTOPARA_TXRATE_EQ_TH()) //th for change higher rate
722 if(old_rate < MTO_DataRateAvailableLevel - 1)
724 //TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
725 if (retryrate_rec[old_rate+1] > MTOPARA_TXRETRYRATE_REDUCE())
726 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]-MTOPARA_TXRETRYRATE_REDUCE()][Level2PerTbl[old_rate+1]];
728 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
729 if(BestThroupht < TxThrouput3)
731 new_rate = old_rate + 1;
732 BestThroupht = TxThrouput3;
734 WBDEBUG(("=++++++++++\n"));
740 if(old_rate > 0) //Min Rate
742 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
743 if(BestThroupht < TxThrouput3)
745 new_rate = old_rate - 1;
747 WBDEBUG(("=--------\n"));
749 BestThroupht = TxThrouput3;
754 if (!LOCAL_IS_IBSS_MODE())
756 max_rssi_rate = GetMaxRateLevelFromRSSI();
758 WBDEBUG(("[MTO]:RSSI2Rate=%d\n", MTO_Data_Rate_Tbl[max_rssi_rate]));
760 if(new_rate > max_rssi_rate)
761 new_rate = max_rssi_rate;
765 TxRateRec.tx_rate = old_rate;
766 TxRateRec.tx_retry_rate = (u8) retry_rate;
767 TxRetryRate = retry_rate;
771 void SmoothRSSI(s32 new_rssi)
773 RSSISmoothed = RSSISmoothed + new_rssi - RSSIBuf[RSSIBufIndex];
774 RSSIBuf[RSSIBufIndex] = new_rssi;
775 RSSIBufIndex = (RSSIBufIndex + 1) % 10;
778 u8 GetMaxRateLevelFromRSSI(void)
783 for(i=0;i<RSSI2RATE_SIZE;i++)
785 if(RSSISmoothed > RSSI2RateTbl[i].RSSI)
789 WBDEBUG(("[MTO]:RSSI=%d\n", Divide(RSSISmoothed, 10)));
791 if(i < RSSI2RATE_SIZE)
792 TxRate = RSSI2RateTbl[i].TxRate;
794 TxRate = 2; //divided by 2 = 1Mbps
796 for(i=MTO_DataRateAvailableLevel-1;i>0;i--)
798 if(TxRate >=MTO_Data_Rate_Tbl[i])
804 //===========================================================================
806 // If we enable DTO, we will ignore the tx count with different tx rate from
807 // DTO rate. This is because when we adjust DTO tx rate, there could be some
808 // packets in the tx queue with previous tx rate
809 void MTO_SetTxCount(MTO_FUNC_INPUT, u8 tx_rate, u8 index)
812 if ((MTO_ENABLE==1) && (MTO_RATE_CHANGE_ENABLE()==1))
814 if(tx_rate == MTO_DATA_RATE())
818 if (boSparseTxTraffic)
819 MTO_HAL()->dto_tx_frag_count += MTOPARA_PERIODIC_CHECK_CYCLE();
821 MTO_HAL()->dto_tx_frag_count += 1;
827 MTO_HAL()->dto_tx_retry_count += index;
828 MTO_HAL()->dto_tx_frag_count += (index+1);
832 MTO_HAL()->dto_tx_retry_count += 7;
833 MTO_HAL()->dto_tx_frag_count += 7;
837 else if(MTO_DATA_RATE()>48 && tx_rate ==48)
839 if (index<3) //for reduciing data rate scheme ,
840 //do not calcu different data rate
841 //3 is the reducing data rate at retry
843 MTO_HAL()->dto_tx_retry_count += index;
844 MTO_HAL()->dto_tx_frag_count += (index+1);
848 MTO_HAL()->dto_tx_retry_count += 3;
849 MTO_HAL()->dto_tx_frag_count += 3;
856 MTO_HAL()->dto_tx_retry_count += index;
857 MTO_HAL()->dto_tx_frag_count += (index+1);
860 TotalTxPktRetry += (index+1);
863 PeriodTotalTxPktRetry += (index+1);
866 u8 MTO_GetTxFallbackRate(MTO_FUNC_INPUT)
868 return MTO_DATA_FALLBACK_RATE();
872 //===========================================================================
876 // Failure of transmitting a packet indicates that certain MTO parmeters
877 // may need to be adjusted. This function is called when NIC just failed
878 // to transmit a packet or when MSDULifeTime expired.
881 // adapter - The pointer to the Miniport adapter Context
885 //============================================================================
886 void MTO_TxFailed(MTO_FUNC_INPUT)
891 int Divide(int a, int b)