Staging: w35und: #include cleanup
[firefly-linux-kernel-4.4.55.git] / drivers / staging / winbond / mto.c
1 //============================================================================
2 //  MTO.C -
3 //
4 //  Description:
5 //    MAC Throughput Optimization for W89C33 802.11g WLAN STA.
6 //
7 //    The following MIB attributes or internal variables will be affected
8 //    while the MTO is being executed:
9 //       dot11FragmentationThreshold,
10 //       dot11RTSThreshold,
11 //       transmission rate and PLCP preamble type,
12 //       CCA mode,
13 //       antenna diversity.
14 //
15 //  Revision history:
16 //  --------------------------------------------------------------------------
17 //           20031227  UN20 Pete Chao
18 //                     First draft
19 //  20031229           Turbo                copy from PD43
20 //  20040210           Kevin                revised
21 //  Copyright (c) 2003 Winbond Electronics Corp. All rights reserved.
22 //============================================================================
23
24 // LA20040210_DTO kevin
25 #include "os_common.h"
26 #include "sme_api.h"
27 #include "gl_80211.h"
28 #include "wbhal_f.h"
29
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
35
36 u16 MTO_Frag_Th_Tbl[MTO_MAX_FRAG_TH_LEVELS] =
37 {
38     256, 384, 512, 768, 1536
39 };
40
41 u8  MTO_SQ3_Level[MTO_MAX_SQ3_LEVELS] =
42 {
43     0, 26, 30, 32, 34, 35, 37, 42, 44, 46, 54, 62, 78, 81
44 };
45 u8  MTO_SQ3toRate[MTO_MAX_SQ3_LEVELS] =
46 {
47     0, 1, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
48 };
49 u8  MTO_SQ3toFrag[MTO_MAX_SQ3_LEVELS] =
50 {
51     0, 2, 2, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4
52 };
53
54 // One Exchange Time table
55 //
56 u16 MTO_One_Exchange_Time_Tbl_l[MTO_MAX_FRAG_TH_LEVELS][MTO_MAX_DATA_RATE_LEVELS] =
57 {
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}
63 };
64
65 u16 MTO_One_Exchange_Time_Tbl_s[MTO_MAX_FRAG_TH_LEVELS][MTO_MAX_DATA_RATE_LEVELS] =
66 {
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}
72 };
73
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]
77
78 // Declare data rate table
79 //The following table will be changed at anytime if the opration rate supported by AP don't
80 //match the table
81 u8  MTO_Data_Rate_Tbl[MTO_MAX_DATA_RATE_LEVELS] =
82 {
83     2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
84 };
85
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] =
90 {
91         2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
92 };
93 static u8  Level2PerTbl[MTO_MAX_DATA_RATE_LEVELS] =
94 {
95         0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
96 };
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}
194 };
195
196 #define RSSIBUF_NUM 10
197 #define RSSI2RATE_SIZE 9
198
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;
208
209 static int TotalTxPkt = 0;
210 static int TotalTxPktRetry = 0;
211 static int TxPktPerAnt[3] = {0,0,0};
212 static int RXRSSIANT[3] ={-70,-70,-70};
213 static int TxPktRetryPerAnt[3] = {0,0,0};
214 //static int TxDominateFlag=false;
215 static u8 old_antenna[4]={1 ,0 ,1 ,0};
216 static int retryrate_rec[MTO_MAX_DATA_RATE_LEVELS];//this record the retry rate at different data rate
217
218 static int PeriodTotalTxPkt = 0;
219 static int PeriodTotalTxPktRetry = 0;
220
221 typedef struct
222 {
223         s32 RSSI;
224         u8  TxRate;
225 }RSSI2RATE;
226
227 static RSSI2RATE RSSI2RateTbl[RSSI2RATE_SIZE] =
228 {
229         {-740, 108},  // 54M
230         {-760, 96},  // 48M
231         {-820, 72},  // 36M
232         {-850, 48},  // 24M
233         {-870, 36},  // 18M
234         {-890, 24},  // 12M
235         {-900, 12},  // 6M
236         {-920, 11}, // 5.5M
237         {-950, 4}, // 2M
238 };
239 static u8 untogglecount;
240 static u8 last_rate_ant; //this is used for antenna backoff-hh
241
242 u8      boSparseTxTraffic = false;
243
244 void MTO_Init(MTO_FUNC_INPUT);
245 void AntennaToggleInitiator(MTO_FUNC_INPUT);
246 void AntennaToggleState(MTO_FUNC_INPUT);
247 void TxPwrControl(MTO_FUNC_INPUT);
248 void GetFreshAntennaData(MTO_FUNC_INPUT);
249 void TxRateReductionCtrl(MTO_FUNC_INPUT);
250 /** 1.1.31.1000 Turbo modify */
251 //void MTO_SetDTORateRange(int type);
252 void MTO_SetDTORateRange(MTO_FUNC_INPUT, u8 *pRateArray, u8 ArraySize);
253 void MTO_SetTxCount(MTO_FUNC_INPUT, u8 t0, u8 index);
254 void MTO_TxFailed(MTO_FUNC_INPUT);
255 void SmoothRSSI(s32 new_rssi);
256 void hal_get_dto_para(MTO_FUNC_INPUT, char *buffer);
257 u8 CalcNewRate(MTO_FUNC_INPUT, u8 old_rate, u32 retry_cnt, u32 tx_frag_cnt);
258 u8 GetMaxRateLevelFromRSSI(void);
259 u8 MTO_GetTxFallbackRate(MTO_FUNC_INPUT);
260 int Divide(int a, int b);
261 void multiagc(MTO_FUNC_INPUT, u8 high_gain_mode);
262
263 //===========================================================================
264 //  MTO_Init --
265 //
266 //  Description:
267 //    Set DTO Tx Rate Scope because different AP could have different Rate set.
268 //    After our staion join with AP, LM core will call this function to initialize
269 //    Tx Rate table.
270 //
271 //  Arguments:
272 //    pRateArray      - The pointer to the Tx Rate Array by the following order
273 //                    - 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108
274 //                    - DTO won't check whether rate order is invalid or not
275 //    ArraySize       - The array size to indicate how many tx rate we can choose
276 //
277 //  sample code:
278 //      {
279 //              u8 RateArray[4] = {2, 4, 11, 22};
280 //              MTO_SetDTORateRange(RateArray, 4);
281 //      }
282 //
283 //  Return Value:
284 //    None
285 //============================================================================
286 void MTO_SetDTORateRange(MTO_FUNC_INPUT,u8 *pRateArray, u8 ArraySize)
287 {
288         u8      i, j=0;
289
290         for(i=0;i<ArraySize;i++)
291         {
292                 if(pRateArray[i] == 22)
293                         break;
294         }
295         if(i < ArraySize) //we need adjust the order of rate list because 11Mbps rate exists
296         {
297                 for(;i>0;i--)
298                 {
299                         if(pRateArray[i-1] <= 11)
300                                 break;
301                         pRateArray[i] = pRateArray[i-1];
302                 }
303                 pRateArray[i] = 22;
304                 MTO_OFDM_RATE_LEVEL() = i;
305         }
306         else
307         {
308                 for(i=0; i<ArraySize; i++)
309                 {
310                         if (pRateArray[i] >= 12)
311                                 break;
312                 }
313                 MTO_OFDM_RATE_LEVEL() = i;
314         }
315
316         for(i=0;i<ArraySize;i++)
317         {
318                 MTO_Data_Rate_Tbl[i] = pRateArray[i];
319                 for(;j<MTO_MAX_DATA_RATE_LEVELS;j++)
320                 {
321                         if(Stardard_Data_Rate_Tbl[j] == pRateArray[i])
322                                 break;
323                 }
324                 Level2PerTbl[i] = j;
325                 #ifdef _PE_DTO_DUMP_
326                 WBDEBUG(("[MTO]:Op Rate[%d]: %d\n",i, MTO_Data_Rate_Tbl[i]));
327                 #endif
328         }
329         MTO_DataRateAvailableLevel = ArraySize;
330         if( MTO_DATA().RatePolicy ) // 0 means that no registry setting
331         {
332                 if( MTO_DATA().RatePolicy == 1 )
333                         TxRateRec.tx_rate = 0;  //ascent
334                 else
335                         TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ;     //descent
336         }
337         else
338         {
339                 if( MTO_INITTXRATE_MODE )
340                         TxRateRec.tx_rate = 0;  //ascent
341                 else
342                         TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ;     //descent
343         }
344         TxRateRec.tx_retry_rate = 0;
345         //set default rate for initial use
346         MTO_RATE_LEVEL() = TxRateRec.tx_rate;
347         MTO_FALLBACK_RATE_LEVEL() = MTO_RATE_LEVEL();
348 }
349
350 //===========================================================================
351 //  MTO_Init --
352 //
353 //  Description:
354 //    Initialize MTO parameters.
355 //
356 //    This function should be invoked during system initialization.
357 //
358 //  Arguments:
359 //    adapter      - The pointer to the Miniport adapter Context
360 //
361 //  Return Value:
362 //    None
363 //============================================================================
364 void MTO_Init(MTO_FUNC_INPUT)
365 {
366     int i;
367         //WBDEBUG(("[MTO] -> MTO_Init()\n"));
368         //[WKCHEN]pMTOcore_data = pcore_data;
369 // 20040510 Turbo add for global variable
370     MTO_TMR_CNT()       = 0;
371     MTO_TOGGLE_STATE()  = TOGGLE_STATE_IDLE;
372     MTO_TX_RATE_REDUCTION_STATE() = RATE_CHGSTATE_IDLE;
373     MTO_BACKOFF_TMR()   = 0;
374     MTO_LAST_RATE()     = 11;
375     MTO_CO_EFFICENT()   = 0;
376
377     //MTO_TH_FIXANT()     = MTO_DEFAULT_TH_FIXANT;
378     MTO_TH_CNT()        = MTO_DEFAULT_TH_CNT;
379     MTO_TH_SQ3()        = MTO_DEFAULT_TH_SQ3;
380     MTO_TH_IDLE_SLOT()  = MTO_DEFAULT_TH_IDLE_SLOT;
381     MTO_TH_PR_INTERF()  = MTO_DEFAULT_TH_PR_INTERF;
382
383     MTO_TMR_AGING()     = MTO_DEFAULT_TMR_AGING;
384     MTO_TMR_PERIODIC()  = MTO_DEFAULT_TMR_PERIODIC;
385
386     //[WKCHEN]MTO_CCA_MODE_SETUP()= (u8) hal_get_cca_mode(MTO_HAL());
387     //[WKCHEN]MTO_CCA_MODE()      = MTO_CCA_MODE_SETUP();
388
389     //MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_LONG;
390     MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_SHORT;   // for test
391
392     MTO_ANT_SEL()       = hal_get_antenna_number(MTO_HAL());
393     MTO_ANT_MAC()       = MTO_ANT_SEL();
394     MTO_CNT_ANT(0)      = 0;
395     MTO_CNT_ANT(1)      = 0;
396     MTO_SQ_ANT(0)       = 0;
397     MTO_SQ_ANT(1)       = 0;
398     MTO_ANT_DIVERSITY() = MTO_ANTENNA_DIVERSITY_ON;
399     //CardSet_AntennaDiversity(adapter, MTO_ANT_DIVERSITY());
400     //PLMESetAntennaDiversity( adapter, MTO_ANT_DIVERSITY());
401
402     MTO_AGING_TIMEOUT() = 0;//MTO_TMR_AGING() / MTO_TMR_PERIODIC();
403
404     // The following parameters should be initialized to the values set by user
405     //
406     //MTO_RATE_LEVEL()            = 10;
407     MTO_RATE_LEVEL()            = 0;
408         MTO_FALLBACK_RATE_LEVEL()       = MTO_RATE_LEVEL();
409     MTO_FRAG_TH_LEVEL()         = 4;
410     /** 1.1.23.1000 Turbo modify from -1 to +1
411         MTO_RTS_THRESHOLD()         = MTO_FRAG_TH() - 1;
412     MTO_RTS_THRESHOLD_SETUP()   = MTO_FRAG_TH() - 1;
413         */
414         MTO_RTS_THRESHOLD()         = MTO_FRAG_TH() + 1;
415     MTO_RTS_THRESHOLD_SETUP()   = MTO_FRAG_TH() + 1;
416     // 1.1.23.1000 Turbo add for mto change preamble from 0 to 1
417         MTO_RATE_CHANGE_ENABLE()    = 1;
418     MTO_FRAG_CHANGE_ENABLE()    = 0;          // 1.1.29.1000 Turbo add don't support frag
419         //The default valud of ANTDIV_DEFAULT_ON will be decided by EEPROM
420         //#ifdef ANTDIV_DEFAULT_ON
421     //MTO_ANT_DIVERSITY_ENABLE()  = 1;
422         //#else
423     //MTO_ANT_DIVERSITY_ENABLE()  = 0;
424         //#endif
425     MTO_POWER_CHANGE_ENABLE()   = 1;
426         MTO_PREAMBLE_CHANGE_ENABLE()= 1;
427     MTO_RTS_CHANGE_ENABLE()     = 0;          // 1.1.29.1000 Turbo add don't support frag
428     // 20040512 Turbo add
429         //old_antenna[0] = 1;
430         //old_antenna[1] = 0;
431         //old_antenna[2] = 1;
432         //old_antenna[3] = 0;
433         for (i=0;i<MTO_MAX_DATA_RATE_LEVELS;i++)
434                 retryrate_rec[i]=5;
435
436         MTO_TXFLOWCOUNT() = 0;
437         //--------- DTO threshold parameters -------------
438         //MTOPARA_PERIODIC_CHECK_CYCLE() = 50;
439         MTOPARA_PERIODIC_CHECK_CYCLE() = 10;
440         MTOPARA_RSSI_TH_FOR_ANTDIV() = 10;
441         MTOPARA_TXCOUNT_TH_FOR_CALC_RATE() = 50;
442         MTOPARA_TXRATE_INC_TH() = 10;
443         MTOPARA_TXRATE_DEC_TH() = 30;
444         MTOPARA_TXRATE_EQ_TH() = 40;
445         MTOPARA_TXRATE_BACKOFF() = 12;
446         MTOPARA_TXRETRYRATE_REDUCE() = 6;
447         if ( MTO_TXPOWER_FROM_EEPROM == 0xff)
448         {
449                 switch( MTO_HAL()->phy_type)
450                 {
451                         case RF_AIROHA_2230:
452                         case RF_AIROHA_2230S: // 20060420 Add this
453                                 MTOPARA_TXPOWER_INDEX() = 46; // MAX-8 // @@ Only for AL 2230
454                                 break;
455                         case RF_AIROHA_7230:
456                                 MTOPARA_TXPOWER_INDEX() = 49;
457                                 break;
458                         case RF_WB_242:
459                                 MTOPARA_TXPOWER_INDEX() = 10;
460                                 break;
461                         case RF_WB_242_1:
462                                 MTOPARA_TXPOWER_INDEX() = 24; // ->10 20060316.1 modify
463                                 break;
464                 }
465         }
466         else    //follow the setting from EEPROM
467                 MTOPARA_TXPOWER_INDEX() = MTO_TXPOWER_FROM_EEPROM;
468         hal_set_rf_power(MTO_HAL(), (u8)MTOPARA_TXPOWER_INDEX());
469         //------------------------------------------------
470
471         // For RSSI turning 20060808.4 Cancel load from EEPROM
472         MTO_DATA().RSSI_high = -41;
473         MTO_DATA().RSSI_low = -60;
474 }
475
476 //---------------------------------------------------------------------------//
477 static u32 DTO_Rx_Info[13][3];
478 static u32 DTO_RxCRCFail_Info[13][3];
479 static u32 AntennaToggleBkoffTimer=5;
480 typedef struct{
481         int RxRate;
482         int RxRatePkts;
483         int index;
484 }RXRATE_ANT;
485 RXRATE_ANT RxRatePeakAnt[3];
486
487 #define ANT0    0
488 #define ANT1    1
489 #define OLD_ANT 2
490
491 void SearchPeakRxRate(int index)
492 {
493         int i;
494         RxRatePeakAnt[index].RxRatePkts=0;
495         //Find out the best rx rate which is used on different antenna
496         for(i=1;i<13;i++)
497         {
498                 if(DTO_Rx_Info[i][index] > (u32) RxRatePeakAnt[index].RxRatePkts)
499                 {
500                         RxRatePeakAnt[index].RxRatePkts = DTO_Rx_Info[i][index];
501                         RxRatePeakAnt[index].RxRate = rate_tbl[i];
502                         RxRatePeakAnt[index].index = i;
503                 }
504         }
505 }
506
507 void ResetDTO_RxInfo(int index, MTO_FUNC_INPUT)
508 {
509         int i;
510
511         #ifdef _PE_DTO_DUMP_
512         WBDEBUG(("ResetDTOrx\n"));
513         #endif
514
515         for(i=0;i<13;i++)
516                 DTO_Rx_Info[i][index] = MTO_HAL()->rx_ok_count[i];
517
518         for(i=0;i<13;i++)
519                 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i];
520
521         TotalTxPkt = 0;
522         TotalTxPktRetry = 0;
523 }
524
525 void GetDTO_RxInfo(int index, MTO_FUNC_INPUT)
526 {
527         int i;
528
529         #ifdef _PE_DTO_DUMP_
530         WBDEBUG(("GetDTOrx\n"));
531         #endif
532
533         //PDEBUG(("[MTO]:DTO_Rx_Info[%d]=%d, rx_ok_count=%d\n", index, DTO_Rx_Info[0][index], phw_data->rx_ok_count[0]));
534         for(i=0;i<13;i++)
535                 DTO_Rx_Info[i][index] = abs(MTO_HAL()->rx_ok_count[i] - DTO_Rx_Info[i][index]);
536         if(DTO_Rx_Info[0][index]==0) DTO_Rx_Info[0][index] = 1;
537
538         for(i=0;i<13;i++)
539                 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i] - DTO_RxCRCFail_Info[i][index];
540
541         TxPktPerAnt[index] = TotalTxPkt;
542         TxPktRetryPerAnt[index] = TotalTxPktRetry;
543         TotalTxPkt = 0;
544         TotalTxPktRetry = 0;
545 }
546
547 void OutputDebugInfo(int index1, int index2)
548 {
549         #ifdef _PE_DTO_DUMP_
550         WBDEBUG(("[HHDTO]:Total Rx (%d)\t\t(%d) \n ", DTO_Rx_Info[0][index1], DTO_Rx_Info[0][index2]));
551     WBDEBUG(("[HHDTO]:RECEIVE RSSI: (%d)\t\t(%d) \n ", RXRSSIANT[index1], RXRSSIANT[index2]));
552         WBDEBUG(("[HHDTO]:TX packet correct rate: (%d)%%\t\t(%d)%% \n ",Divide(TxPktPerAnt[index1]*100,TxPktRetryPerAnt[index1]), Divide(TxPktPerAnt[index2]*100,TxPktRetryPerAnt[index2])));
553         #endif
554         {
555                 int tmp1, tmp2;
556                 #ifdef _PE_DTO_DUMP_
557                 WBDEBUG(("[HHDTO]:Total Tx (%d)\t\t(%d) \n ", TxPktPerAnt[index1], TxPktPerAnt[index2]));
558                 WBDEBUG(("[HHDTO]:Total Tx retry (%d)\t\t(%d) \n ", TxPktRetryPerAnt[index1], TxPktRetryPerAnt[index2]));
559                 #endif
560                 tmp1 = TxPktPerAnt[index1] + DTO_Rx_Info[0][index1];
561                 tmp2 = TxPktPerAnt[index2] + DTO_Rx_Info[0][index2];
562                 #ifdef _PE_DTO_DUMP_
563                 WBDEBUG(("[HHDTO]:Total Tx+RX (%d)\t\t(%d) \n ", tmp1, tmp2));
564                 #endif
565         }
566 }
567
568 unsigned char TxDominate(int index)
569 {
570         int tmp;
571
572         tmp = TxPktPerAnt[index] + DTO_Rx_Info[0][index];
573
574         if(Divide(TxPktPerAnt[index]*100, tmp) > 40)
575                 return true;
576         else
577                 return false;
578 }
579
580 unsigned char CmpTxRetryRate(int index1, int index2)
581 {
582         int tx_retry_rate1, tx_retry_rate2;
583         tx_retry_rate1 = Divide((TxPktRetryPerAnt[index1] - TxPktPerAnt[index1])*100, TxPktRetryPerAnt[index1]);
584         tx_retry_rate2 = Divide((TxPktRetryPerAnt[index2] - TxPktPerAnt[index2])*100, TxPktRetryPerAnt[index2]);
585         #ifdef _PE_DTO_DUMP_
586         WBDEBUG(("[MTO]:TxRetry Ant0: (%d%%)  Ant1: (%d%%) \n ", tx_retry_rate1, tx_retry_rate2));
587         #endif
588
589         if(tx_retry_rate1 > tx_retry_rate2)
590                 return true;
591         else
592                 return false;
593 }
594
595 void GetFreshAntennaData(MTO_FUNC_INPUT)
596 {
597     u8      x;
598
599         x = hal_get_antenna_number(MTO_HAL());
600         //hal_get_bss_pk_cnt(MTO_HAL());
601         //hal_get_est_sq3(MTO_HAL(), 1);
602         old_antenna[0] = x;
603         //if this is the function for timer
604         ResetDTO_RxInfo(x, MTO_FUNC_INPUT_DATA);
605         if(AntennaToggleBkoffTimer)
606                         AntennaToggleBkoffTimer--;
607         if (abs(last_rate_ant-MTO_RATE_LEVEL())>1)  //backoff timer reset
608                 AntennaToggleBkoffTimer=0;
609
610         if (MTO_ANT_DIVERSITY() != MTO_ANTENNA_DIVERSITY_ON ||
611                 MTO_ANT_DIVERSITY_ENABLE() != 1)
612         AntennaToggleBkoffTimer=1;
613         #ifdef _PE_DTO_DUMP_
614         WBDEBUG(("[HHDTO]:**last data rate=%d,now data rate=%d**antenna toggle timer=%d",last_rate_ant,MTO_RATE_LEVEL(),AntennaToggleBkoffTimer));
615         #endif
616         last_rate_ant=MTO_RATE_LEVEL();
617         if(AntennaToggleBkoffTimer==0)
618         {
619                 MTO_TOGGLE_STATE() = TOGGLE_STATE_WAIT0;
620                 #ifdef _PE_DTO_DUMP_
621                 WBDEBUG(("[HHDTO]:===state is starting==for antenna toggle==="));
622                 #endif
623         }
624         else
625                 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
626
627         if ((MTO_BACKOFF_TMR()!=0)&&(MTO_RATE_LEVEL()>MTO_DataRateAvailableLevel - 3))
628         {
629                 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
630                 #ifdef _PE_DTO_DUMP_
631                 WBDEBUG(("[HHDTO]:===the data rate is %d (good)and will not toogle  ===",MTO_DATA_RATE()>>1));
632                 #endif
633         }
634
635
636 }
637
638 int WB_PCR[2]; //packet correct rate
639
640 void AntennaToggleState(MTO_FUNC_INPUT)
641 {
642     int decideantflag = 0;
643         u8      x;
644         s32     rssi;
645
646         if(MTO_ANT_DIVERSITY_ENABLE() != 1)
647                 return;
648         x = hal_get_antenna_number(MTO_HAL());
649         switch(MTO_TOGGLE_STATE())
650         {
651
652            //Missing.....
653            case TOGGLE_STATE_IDLE:
654          case TOGGLE_STATE_BKOFF:
655                      break;;
656
657                 case TOGGLE_STATE_WAIT0://========
658                        GetDTO_RxInfo(x, MTO_FUNC_INPUT_DATA);
659                         sme_get_rssi(MTO_FUNC_INPUT_DATA, &rssi);
660                         RXRSSIANT[x] = rssi;
661                         #ifdef _PE_DTO_DUMP_
662                         WBDEBUG(("[HHDTO] **wait0==== Collecting Ant%d--rssi=%d\n", x,RXRSSIANT[x]));
663                         #endif
664
665                         //change antenna and reset the data at changed antenna
666                         x = (~x) & 0x01;
667                         MTO_ANT_SEL() = x;
668                         hal_set_antenna_number(MTO_HAL(), MTO_ANT_SEL());
669                         LOCAL_ANTENNA_NO() = x;
670
671                         MTO_TOGGLE_STATE() = TOGGLE_STATE_WAIT1;//go to wait1
672                         ResetDTO_RxInfo(x, MTO_FUNC_INPUT_DATA);
673                         break;
674                 case TOGGLE_STATE_WAIT1://=====wait1
675                         //MTO_CNT_ANT(x) = hal_get_bss_pk_cnt(MTO_HAL());
676                         //RXRSSIANT[x] = hal_get_rssi(MTO_HAL());
677                         sme_get_rssi(MTO_FUNC_INPUT_DATA, &rssi);
678                         RXRSSIANT[x] = rssi;
679                         GetDTO_RxInfo(x, MTO_FUNC_INPUT_DATA);
680                         #ifdef _PE_DTO_DUMP_
681                         WBDEBUG(("[HHDTO] **wait1==== Collecting Ant%d--rssi=%d\n", x,RXRSSIANT[x]));
682                         #endif
683                         MTO_TOGGLE_STATE() = TOGGLE_STATE_MAKEDESISION;
684                         break;
685                 case TOGGLE_STATE_MAKEDESISION:
686                         #ifdef _PE_DTO_DUMP_
687                         WBDEBUG(("[HHDTO]:Ant--0-----------------1---\n"));
688                         OutputDebugInfo(ANT0,ANT1);
689                         #endif
690                         //PDEBUG(("[HHDTO] **decision====\n "));
691
692                         //=====following is the decision produrce
693                         //
694                         //    first: compare the rssi if difference >10
695                         //           select the larger one
696                         //           ,others go to second
697                         //    second: comapre the tx+rx packet count if difference >100
698                         //            use larger total packets antenna
699                         //    third::compare the tx PER if packets>20
700                         //                           if difference >5% using the bigger one
701                         //
702                         //    fourth:compare the RX PER if packets>20
703                         //                    if PER difference <5%
704                         //                   using old antenna
705                         //
706                         //
707                         if (abs(RXRSSIANT[ANT0]-RXRSSIANT[ANT1]) > MTOPARA_RSSI_TH_FOR_ANTDIV())//====rssi_th
708                         {
709                                 if (RXRSSIANT[ANT0]>RXRSSIANT[ANT1])
710                                 {
711                                         decideantflag=1;
712                                         MTO_ANT_MAC() = ANT0;
713                                 }
714                                 else
715                                 {
716                                         decideantflag=1;
717                                         MTO_ANT_MAC() = ANT1;
718                                 }
719                                 #ifdef _PE_DTO_DUMP_
720                                 WBDEBUG(("Select antenna by RSSI\n"));
721                                 #endif
722                         }
723                         else if  (abs(TxPktPerAnt[ANT0] + DTO_Rx_Info[0][ANT0]-TxPktPerAnt[ANT1]-DTO_Rx_Info[0][ANT1])<50)//=====total packet_th
724                         {
725                                 #ifdef _PE_DTO_DUMP_
726                                 WBDEBUG(("Total tx/rx is close\n"));
727                                 #endif
728                                 if (TxDominate(ANT0) && TxDominate(ANT1))
729                                 {
730                                         if ((TxPktPerAnt[ANT0]>10) && (TxPktPerAnt[ANT1]>10))//====tx packet_th
731                                         {
732                                                 WB_PCR[ANT0]=Divide(TxPktPerAnt[ANT0]*100,TxPktRetryPerAnt[ANT0]);
733                                                 WB_PCR[ANT1]=Divide(TxPktPerAnt[ANT1]*100,TxPktRetryPerAnt[ANT1]);
734                                                 if (abs(WB_PCR[ANT0]-WB_PCR[ANT1])>5)// tx PER_th
735                                                 {
736                                                         #ifdef _PE_DTO_DUMP_
737                                                         WBDEBUG(("Decide by Tx correct rate\n"));
738                                                         #endif
739                                                         if (WB_PCR[ANT0]>WB_PCR[ANT1])
740                                                         {
741                                                                 decideantflag=1;
742                                                                 MTO_ANT_MAC() = ANT0;
743                                                         }
744                                                         else
745                                                         {
746                                                                 decideantflag=1;
747                                                                 MTO_ANT_MAC() = ANT1;
748                                                         }
749                                                 }
750                                                 else
751                                                 {
752                                                         decideantflag=0;
753                                                         untogglecount++;
754                                                         MTO_ANT_MAC() = old_antenna[0];
755                                                 }
756                                         }
757                                         else
758                                         {
759                                                 decideantflag=0;
760                                                 MTO_ANT_MAC() = old_antenna[0];
761                                         }
762                                 }
763                                 else if ((DTO_Rx_Info[0][ANT0]>10)&&(DTO_Rx_Info[0][ANT1]>10))//rx packet th
764                                 {
765                                         #ifdef _PE_DTO_DUMP_
766                                         WBDEBUG(("Decide by Rx\n"));
767                                         #endif
768                                         if (abs(DTO_Rx_Info[0][ANT0] - DTO_Rx_Info[0][ANT1])>50)
769                                         {
770                                                 if (DTO_Rx_Info[0][ANT0] > DTO_Rx_Info[0][ANT1])
771                                                 {
772                                                         decideantflag=1;
773                                                         MTO_ANT_MAC() = ANT0;
774                                                 }
775                                                 else
776                                                 {
777                                                         decideantflag=1;
778                                                         MTO_ANT_MAC() = ANT1;
779                                                 }
780                                         }
781                                         else
782                                         {
783                                                 decideantflag=0;
784                                                 untogglecount++;
785                                                 MTO_ANT_MAC() = old_antenna[0];
786                                         }
787                                 }
788                                 else
789                                 {
790                                         decideantflag=0;
791                                         MTO_ANT_MAC() = old_antenna[0];
792                                 }
793                         }
794                         else if ((TxPktPerAnt[ANT0]+DTO_Rx_Info[0][ANT0])>(TxPktPerAnt[ANT1]+DTO_Rx_Info[0][ANT1]))//use more packekts
795                         {
796                                 #ifdef _PE_DTO_DUMP_
797                                 WBDEBUG(("decide by total tx/rx : ANT 0\n"));
798                                 #endif
799
800                                 decideantflag=1;
801                                 MTO_ANT_MAC() = ANT0;
802                         }
803                         else
804                         {
805                                 #ifdef _PE_DTO_DUMP_
806                                 WBDEBUG(("decide by total tx/rx : ANT 1\n"));
807                                 #endif
808                                 decideantflag=1;
809                                 MTO_ANT_MAC() = ANT1;
810
811                         }
812                         //this is force ant toggle
813                         if (decideantflag==1)
814                                 untogglecount=0;
815
816                         untogglecount=untogglecount%4;
817                         if (untogglecount==3) //change antenna
818                                 MTO_ANT_MAC() = ((~old_antenna[0]) & 0x1);
819                         #ifdef _PE_DTO_DUMP_
820                         WBDEBUG(("[HHDTO]:==================untoggle-count=%d",untogglecount));
821                         #endif
822
823
824
825
826                         //PDEBUG(("[HHDTO] **********************************DTO ENABLE=%d",MTO_ANT_DIVERSITY_ENABLE()));
827                         if(MTO_ANT_DIVERSITY_ENABLE() == 1)
828                         {
829                                         MTO_ANT_SEL() = MTO_ANT_MAC();
830                                         hal_set_antenna_number(MTO_HAL(), MTO_ANT_SEL());
831                                         LOCAL_ANTENNA_NO() = MTO_ANT_SEL();
832                                         #ifdef _PE_DTO_DUMP_
833                                         WBDEBUG(("[HHDTO] ==decision==*******antflag=%d******************selected antenna=%d\n",decideantflag,MTO_ANT_SEL()));
834                                         #endif
835                         }
836                         if (decideantflag)
837                         {
838                                 old_antenna[3]=old_antenna[2];//store antenna info
839                                 old_antenna[2]=old_antenna[1];
840                                 old_antenna[1]=old_antenna[0];
841                                 old_antenna[0]= MTO_ANT_MAC();
842                         }
843                         #ifdef _PE_DTO_DUMP_
844                         WBDEBUG(("[HHDTO]:**old antenna=[%d][%d][%d][%d]\n",old_antenna[0],old_antenna[1],old_antenna[2],old_antenna[3]));
845                         #endif
846                         if (old_antenna[0]!=old_antenna[1])
847                                 AntennaToggleBkoffTimer=0;
848                         else if (old_antenna[1]!=old_antenna[2])
849                                 AntennaToggleBkoffTimer=1;
850                         else if (old_antenna[2]!=old_antenna[3])
851                                 AntennaToggleBkoffTimer=2;
852                         else
853                                 AntennaToggleBkoffTimer=4;
854
855                         #ifdef _PE_DTO_DUMP_
856                         WBDEBUG(("[HHDTO]:**back off timer=%d",AntennaToggleBkoffTimer));
857                         #endif
858
859                         ResetDTO_RxInfo(MTO_ANT_MAC(), MTO_FUNC_INPUT_DATA);
860                         if (AntennaToggleBkoffTimer==0 && decideantflag)
861                                 MTO_TOGGLE_STATE() = TOGGLE_STATE_WAIT0;
862                         else
863                                 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
864                         break;
865         }
866
867 }
868
869 void multiagc(MTO_FUNC_INPUT, u8 high_gain_mode )
870 {
871         s32             rssi;
872         hw_data_t       *pHwData = MTO_HAL();
873
874         sme_get_rssi(MTO_FUNC_INPUT_DATA, &rssi);
875
876         if( (RF_WB_242 == pHwData->phy_type) ||
877                 (RF_WB_242_1 == pHwData->phy_type) ) // 20060619.5 Add
878         {
879                 if (high_gain_mode==1)
880                 {
881                         //hw_set_dxx_reg(phw_data, 0x0C, 0xf8f52230);
882                         //hw_set_dxx_reg(phw_data, 0x20, 0x06C43440);
883                         Wb35Reg_Write( pHwData, 0x100C, 0xF2F32232 ); // 940916 0xf8f52230 );
884                         Wb35Reg_Write( pHwData, 0x1020, 0x04cb3440 ); // 940915 0x06C43440
885                 }
886                 else if (high_gain_mode==0)
887                 {
888                         //hw_set_dxx_reg(phw_data, 0x0C, 0xEEEE000D);
889                         //hw_set_dxx_reg(phw_data, 0x20, 0x06c41440);
890                         Wb35Reg_Write( pHwData, 0x100C, 0xEEEE000D );
891                         Wb35Reg_Write( pHwData, 0x1020, 0x04cb1440 ); // 940915 0x06c41440
892                 }
893                 #ifdef _PE_DTO_DUMP_
894                 WBDEBUG(("[HHDTOAGC] **rssi=%d, high gain mode=%d", rssi, high_gain_mode));
895                 #endif
896         }
897 }
898
899 void TxPwrControl(MTO_FUNC_INPUT)
900 {
901     s32     rssi;
902         hw_data_t       *pHwData = MTO_HAL();
903
904         sme_get_rssi(MTO_FUNC_INPUT_DATA, &rssi);
905         if( (RF_WB_242 == pHwData->phy_type) ||
906                 (RF_WB_242_1 == pHwData->phy_type) ) // 20060619.5 Add
907         {
908                 static u8 high_gain_mode; //this is for winbond RF switch LNA
909                                           //using different register setting
910
911                 if (high_gain_mode==1)
912                 {
913                         if( rssi > MTO_DATA().RSSI_high )
914                         {
915                                 //hw_set_dxx_reg(phw_data, 0x0C, 0xf8f52230);
916                                 //hw_set_dxx_reg(phw_data, 0x20, 0x05541640);
917                                 high_gain_mode=0;
918                         }
919                         else
920                         {
921                                 //hw_set_dxx_reg(phw_data, 0x0C, 0xf8f51830);
922                                 //hw_set_dxx_reg(phw_data, 0x20, 0x05543E40);
923                                 high_gain_mode=1;
924                         }
925                 }
926                 else //if (high_gain_mode==0)
927                 {
928                         if( rssi < MTO_DATA().RSSI_low )
929                         {
930                                 //hw_set_dxx_reg(phw_data, 0x0C, 0xf8f51830);
931                                 //hw_set_dxx_reg(phw_data, 0x20, 0x05543E40);
932                                 high_gain_mode=1;
933                         }
934                         else
935                         {
936                                 //hw_set_dxx_reg(phw_data, 0x0C, 0xf8f52230);
937                                 //hw_set_dxx_reg(phw_data, 0x20, 0x05541640);
938                                 high_gain_mode=0;
939                         }
940                 }
941
942                 // Always high gain 20051014. Using the initial value only.
943                 multiagc(MTO_FUNC_INPUT_DATA, high_gain_mode);
944         }
945 }
946
947
948 u8 CalcNewRate(MTO_FUNC_INPUT, u8 old_rate, u32 retry_cnt, u32 tx_frag_cnt)
949 {
950         int i;
951         u8 new_rate;
952     u32 retry_rate;
953         int TxThrouput1, TxThrouput2, TxThrouput3, BestThroupht;
954
955         if(tx_frag_cnt < MTOPARA_TXCOUNT_TH_FOR_CALC_RATE()) //too few packets transmit
956         {
957                 return 0xff;
958         }
959         retry_rate = Divide(retry_cnt * 100, tx_frag_cnt);
960
961         if(retry_rate > 90) retry_rate = 90; //always truncate to 90% due to lookup table size
962         #ifdef _PE_DTO_DUMP_
963         WBDEBUG(("##### Current level =%d, Retry count =%d, Frag count =%d\n",
964                                                 old_rate, retry_cnt, tx_frag_cnt));
965         WBDEBUG(("*##* Retry rate =%d, throughput =%d\n",
966                                                 retry_rate, Rate_PER_TBL[retry_rate][old_rate]));
967         WBDEBUG(("TxRateRec.tx_rate =%d, Retry rate = %d, throughput = %d\n",
968                                                 TxRateRec.tx_rate, TxRateRec.tx_retry_rate,
969                                                 Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]]));
970         WBDEBUG(("old_rate-1 =%d, Retry rate = %d, throughput = %d\n",
971                                                 old_rate-1, retryrate_rec[old_rate-1],
972                                                 Rate_PER_TBL[retryrate_rec[old_rate-1]][old_rate-1]));
973         WBDEBUG(("old_rate+1 =%d, Retry rate = %d, throughput = %d\n",
974                                                 old_rate+1, retryrate_rec[old_rate+1],
975                                                 Rate_PER_TBL[retryrate_rec[old_rate+1]][old_rate+1]));
976         #endif
977
978         //following is for record the retry rate at the different data rate
979         if (abs(retry_rate-retryrate_rec[old_rate])<50)//---the per TH
980                 retryrate_rec[old_rate] = retry_rate; //update retry rate
981         else
982         {
983                 for (i=0;i<MTO_DataRateAvailableLevel;i++) //reset all retry rate
984                         retryrate_rec[i]=0;
985                 retryrate_rec[old_rate] = retry_rate;
986                 #ifdef _PE_DTO_DUMP_
987                 WBDEBUG(("Reset retry rate table\n"));
988                 #endif
989         }
990
991         if(TxRateRec.tx_rate > old_rate)   //Decrease Tx Rate
992         {
993                 TxThrouput1 = Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]];
994                 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
995                 if(TxThrouput1 > TxThrouput2)
996                 {
997                         new_rate = TxRateRec.tx_rate;
998                         BestThroupht = TxThrouput1;
999                 }
1000                 else
1001                 {
1002                         new_rate = old_rate;
1003                         BestThroupht = TxThrouput2;
1004                 }
1005                 if((old_rate > 0) &&(retry_rate>MTOPARA_TXRATE_DEC_TH()))   //Min Rate
1006                 {
1007                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
1008                         if(BestThroupht < TxThrouput3)
1009                         {
1010                                 new_rate = old_rate - 1;
1011                                 #ifdef _PE_DTO_DUMP_
1012                                 WBDEBUG(("--------\n"));
1013                                 #endif
1014                                 BestThroupht = TxThrouput3;
1015                         }
1016                 }
1017         }
1018         else if(TxRateRec.tx_rate < old_rate)  //Increase Tx Rate
1019         {
1020                 TxThrouput1 = Rate_PER_TBL[TxRateRec.tx_retry_rate][Level2PerTbl[TxRateRec.tx_rate]];
1021                 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
1022                 if(TxThrouput1 > TxThrouput2)
1023                 {
1024                         new_rate = TxRateRec.tx_rate;
1025                         BestThroupht = TxThrouput1;
1026                 }
1027                 else
1028                 {
1029                         new_rate = old_rate;
1030                         BestThroupht = TxThrouput2;
1031                 }
1032                 if ((old_rate < MTO_DataRateAvailableLevel - 1)&&(retry_rate<MTOPARA_TXRATE_INC_TH()))
1033                 {
1034                         //TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
1035                         if (retryrate_rec[old_rate+1] > MTOPARA_TXRETRYRATE_REDUCE())
1036                                 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]-MTOPARA_TXRETRYRATE_REDUCE()][Level2PerTbl[old_rate+1]];
1037                         else
1038                                 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
1039                         if(BestThroupht < TxThrouput3)
1040                         {
1041                                 new_rate = old_rate + 1;
1042                                 #ifdef _PE_DTO_DUMP_
1043                                 WBDEBUG(("++++++++++\n"));
1044                                 #endif
1045                                 BestThroupht = TxThrouput3;
1046                         }
1047                 }
1048         }
1049         else  //Tx Rate no change
1050         {
1051                 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
1052                 new_rate = old_rate;
1053                 BestThroupht = TxThrouput2;
1054
1055                 if (retry_rate <MTOPARA_TXRATE_EQ_TH())    //th for change higher rate
1056                 {
1057                         if(old_rate < MTO_DataRateAvailableLevel - 1)
1058                         {
1059                                 //TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
1060                                 if (retryrate_rec[old_rate+1] > MTOPARA_TXRETRYRATE_REDUCE())
1061                                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]-MTOPARA_TXRETRYRATE_REDUCE()][Level2PerTbl[old_rate+1]];
1062                                 else
1063                                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
1064                                 if(BestThroupht < TxThrouput3)
1065                                 {
1066                                         new_rate = old_rate + 1;
1067                                         BestThroupht = TxThrouput3;
1068                                         #ifdef _PE_DTO_DUMP_
1069                                         WBDEBUG(("=++++++++++\n"));
1070                                         #endif
1071                                 }
1072                         }
1073                 }
1074                 else
1075                 if(old_rate > 0)   //Min Rate
1076                 {
1077                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
1078                         if(BestThroupht < TxThrouput3)
1079                         {
1080                                 new_rate = old_rate - 1;
1081                                 #ifdef _PE_DTO_DUMP_
1082                                 WBDEBUG(("=--------\n"));
1083                                 #endif
1084                                 BestThroupht = TxThrouput3;
1085                         }
1086                 }
1087         }
1088
1089         if (!LOCAL_IS_IBSS_MODE())
1090         {
1091         max_rssi_rate = GetMaxRateLevelFromRSSI();
1092         #ifdef _PE_DTO_DUMP_
1093         WBDEBUG(("[MTO]:RSSI2Rate=%d\n", MTO_Data_Rate_Tbl[max_rssi_rate]));
1094         #endif
1095         if(new_rate > max_rssi_rate)
1096                 new_rate = max_rssi_rate;
1097         }
1098
1099         //save new rate;
1100         TxRateRec.tx_rate = old_rate;
1101         TxRateRec.tx_retry_rate = (u8) retry_rate;
1102         TxRetryRate = retry_rate;
1103         return new_rate;
1104 }
1105
1106 void SmoothRSSI(s32 new_rssi)
1107 {
1108         RSSISmoothed = RSSISmoothed + new_rssi - RSSIBuf[RSSIBufIndex];
1109         RSSIBuf[RSSIBufIndex] = new_rssi;
1110         RSSIBufIndex = (RSSIBufIndex + 1) % 10;
1111 }
1112
1113 u8 GetMaxRateLevelFromRSSI(void)
1114 {
1115         u8 i;
1116         u8 TxRate;
1117
1118         for(i=0;i<RSSI2RATE_SIZE;i++)
1119         {
1120                 if(RSSISmoothed > RSSI2RateTbl[i].RSSI)
1121                         break;
1122         }
1123         #ifdef _PE_DTO_DUMP_
1124         WBDEBUG(("[MTO]:RSSI=%d\n", Divide(RSSISmoothed, 10)));
1125         #endif
1126         if(i < RSSI2RATE_SIZE)
1127                 TxRate = RSSI2RateTbl[i].TxRate;
1128         else
1129                 TxRate = 2;  //divided by 2 = 1Mbps
1130
1131         for(i=MTO_DataRateAvailableLevel-1;i>0;i--)
1132         {
1133                 if(TxRate >=MTO_Data_Rate_Tbl[i])
1134                         break;
1135         }
1136         return i;
1137 }
1138
1139 //===========================================================================
1140 //  Description:
1141 //      If we enable DTO, we will ignore the tx count with different tx rate from
1142 //      DTO rate. This is because when we adjust DTO tx rate, there could be some
1143 //      packets in the tx queue with previous tx rate
1144 void MTO_SetTxCount(MTO_FUNC_INPUT, u8 tx_rate, u8 index)
1145 {
1146         MTO_TXFLOWCOUNT()++;
1147         if ((MTO_ENABLE==1) && (MTO_RATE_CHANGE_ENABLE()==1))
1148         {
1149                 if(tx_rate == MTO_DATA_RATE())
1150                 {
1151                         if (index == 0)
1152                         {
1153                                 if (boSparseTxTraffic)
1154                                         MTO_HAL()->dto_tx_frag_count += MTOPARA_PERIODIC_CHECK_CYCLE();
1155                                 else
1156                                         MTO_HAL()->dto_tx_frag_count += 1;
1157                         }
1158                         else
1159                         {
1160                                 if (index<8)
1161                                 {
1162                                         MTO_HAL()->dto_tx_retry_count += index;
1163                                         MTO_HAL()->dto_tx_frag_count += (index+1);
1164                                 }
1165                                 else
1166                                 {
1167                                         MTO_HAL()->dto_tx_retry_count += 7;
1168                                         MTO_HAL()->dto_tx_frag_count += 7;
1169                                 }
1170                         }
1171                 }
1172                 else if(MTO_DATA_RATE()>48 && tx_rate ==48)
1173                 {//ALFRED
1174                         if (index<3) //for reduciing data rate scheme ,
1175                                          //do not calcu different data rate
1176                                                  //3 is the reducing data rate at retry
1177                         {
1178                                 MTO_HAL()->dto_tx_retry_count += index;
1179                                 MTO_HAL()->dto_tx_frag_count += (index+1);
1180                         }
1181                         else
1182                         {
1183                                 MTO_HAL()->dto_tx_retry_count += 3;
1184                                 MTO_HAL()->dto_tx_frag_count += 3;
1185                         }
1186
1187                 }
1188         }
1189         else
1190         {
1191                 MTO_HAL()->dto_tx_retry_count += index;
1192                 MTO_HAL()->dto_tx_frag_count += (index+1);
1193         }
1194         TotalTxPkt ++;
1195         TotalTxPktRetry += (index+1);
1196
1197         PeriodTotalTxPkt ++;
1198         PeriodTotalTxPktRetry += (index+1);
1199 }
1200
1201 u8 MTO_GetTxFallbackRate(MTO_FUNC_INPUT)
1202 {
1203         return MTO_DATA_FALLBACK_RATE();
1204 }
1205
1206
1207 //===========================================================================
1208 //  MTO_TxFailed --
1209 //
1210 //  Description:
1211 //    Failure of transmitting a packet indicates that certain MTO parmeters
1212 //    may need to be adjusted. This function is called when NIC just failed
1213 //    to transmit a packet or when MSDULifeTime expired.
1214 //
1215 //  Arguments:
1216 //    adapter      - The pointer to the Miniport adapter Context
1217 //
1218 //  Return Value:
1219 //    None
1220 //============================================================================
1221 void MTO_TxFailed(MTO_FUNC_INPUT)
1222 {
1223         return;
1224 }
1225
1226 int Divide(int a, int b)
1227 {
1228         if (b==0) b=1;
1229         return a/b;
1230 }
1231
1232