]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/winbond/mto.c
Staging: w35und: remove ->adapter from struct _HW_DATA_T
[mv-sheeva.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 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
216
217 static int PeriodTotalTxPkt = 0;
218 static int PeriodTotalTxPktRetry = 0;
219
220 typedef struct
221 {
222         s32 RSSI;
223         u8  TxRate;
224 }RSSI2RATE;
225
226 static RSSI2RATE RSSI2RateTbl[RSSI2RATE_SIZE] =
227 {
228         {-740, 108},  // 54M
229         {-760, 96},  // 48M
230         {-820, 72},  // 36M
231         {-850, 48},  // 24M
232         {-870, 36},  // 18M
233         {-890, 24},  // 12M
234         {-900, 12},  // 6M
235         {-920, 11}, // 5.5M
236         {-950, 4}, // 2M
237 };
238 static u8 last_rate_ant; //this is used for antenna backoff-hh
239
240 u8      boSparseTxTraffic = false;
241
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);
256
257 //===========================================================================
258 //  MTO_Init --
259 //
260 //  Description:
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
263 //    Tx Rate table.
264 //
265 //  Arguments:
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
270 //
271 //  sample code:
272 //      {
273 //              u8 RateArray[4] = {2, 4, 11, 22};
274 //              MTO_SetDTORateRange(RateArray, 4);
275 //      }
276 //
277 //  Return Value:
278 //    None
279 //============================================================================
280 void MTO_SetDTORateRange(MTO_FUNC_INPUT,u8 *pRateArray, u8 ArraySize)
281 {
282         u8      i, j=0;
283
284         for(i=0;i<ArraySize;i++)
285         {
286                 if(pRateArray[i] == 22)
287                         break;
288         }
289         if(i < ArraySize) //we need adjust the order of rate list because 11Mbps rate exists
290         {
291                 for(;i>0;i--)
292                 {
293                         if(pRateArray[i-1] <= 11)
294                                 break;
295                         pRateArray[i] = pRateArray[i-1];
296                 }
297                 pRateArray[i] = 22;
298                 MTO_OFDM_RATE_LEVEL() = i;
299         }
300         else
301         {
302                 for(i=0; i<ArraySize; i++)
303                 {
304                         if (pRateArray[i] >= 12)
305                                 break;
306                 }
307                 MTO_OFDM_RATE_LEVEL() = i;
308         }
309
310         for(i=0;i<ArraySize;i++)
311         {
312                 MTO_Data_Rate_Tbl[i] = pRateArray[i];
313                 for(;j<MTO_MAX_DATA_RATE_LEVELS;j++)
314                 {
315                         if(Stardard_Data_Rate_Tbl[j] == pRateArray[i])
316                                 break;
317                 }
318                 Level2PerTbl[i] = j;
319                 #ifdef _PE_DTO_DUMP_
320                 WBDEBUG(("[MTO]:Op Rate[%d]: %d\n",i, MTO_Data_Rate_Tbl[i]));
321                 #endif
322         }
323         MTO_DataRateAvailableLevel = ArraySize;
324         if( MTO_DATA().RatePolicy ) // 0 means that no registry setting
325         {
326                 if( MTO_DATA().RatePolicy == 1 )
327                         TxRateRec.tx_rate = 0;  //ascent
328                 else
329                         TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ;     //descent
330         }
331         else
332         {
333                 if( MTO_INITTXRATE_MODE )
334                         TxRateRec.tx_rate = 0;  //ascent
335                 else
336                         TxRateRec.tx_rate = MTO_DataRateAvailableLevel -1 ;     //descent
337         }
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();
342 }
343
344 //===========================================================================
345 //  MTO_Init --
346 //
347 //  Description:
348 //    Initialize MTO parameters.
349 //
350 //    This function should be invoked during system initialization.
351 //
352 //  Arguments:
353 //    adapter      - The pointer to the Miniport adapter Context
354 //
355 //  Return Value:
356 //    None
357 //============================================================================
358 void MTO_Init(MTO_FUNC_INPUT)
359 {
360     int i;
361         //WBDEBUG(("[MTO] -> MTO_Init()\n"));
362         //[WKCHEN]pMTOcore_data = pcore_data;
363 // 20040510 Turbo add for global variable
364     MTO_TMR_CNT()       = 0;
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;
370
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;
376
377     MTO_TMR_AGING()     = MTO_DEFAULT_TMR_AGING;
378     MTO_TMR_PERIODIC()  = MTO_DEFAULT_TMR_PERIODIC;
379
380     //[WKCHEN]MTO_CCA_MODE_SETUP()= (u8) hal_get_cca_mode(MTO_HAL());
381     //[WKCHEN]MTO_CCA_MODE()      = MTO_CCA_MODE_SETUP();
382
383     //MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_LONG;
384     MTO_PREAMBLE_TYPE() = MTO_PREAMBLE_SHORT;   // for test
385
386     MTO_ANT_SEL()       = hal_get_antenna_number(MTO_HAL());
387     MTO_ANT_MAC()       = MTO_ANT_SEL();
388     MTO_CNT_ANT(0)      = 0;
389     MTO_CNT_ANT(1)      = 0;
390     MTO_SQ_ANT(0)       = 0;
391     MTO_SQ_ANT(1)       = 0;
392     MTO_ANT_DIVERSITY() = MTO_ANTENNA_DIVERSITY_ON;
393     //CardSet_AntennaDiversity(adapter, MTO_ANT_DIVERSITY());
394     //PLMESetAntennaDiversity( adapter, MTO_ANT_DIVERSITY());
395
396     MTO_AGING_TIMEOUT() = 0;//MTO_TMR_AGING() / MTO_TMR_PERIODIC();
397
398     // The following parameters should be initialized to the values set by user
399     //
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;
407         */
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;
416         //#else
417     //MTO_ANT_DIVERSITY_ENABLE()  = 0;
418         //#endif
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++)
428                 retryrate_rec[i]=5;
429
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)
442         {
443                 switch( MTO_HAL()->phy_type)
444                 {
445                         case RF_AIROHA_2230:
446                         case RF_AIROHA_2230S: // 20060420 Add this
447                                 MTOPARA_TXPOWER_INDEX() = 46; // MAX-8 // @@ Only for AL 2230
448                                 break;
449                         case RF_AIROHA_7230:
450                                 MTOPARA_TXPOWER_INDEX() = 49;
451                                 break;
452                         case RF_WB_242:
453                                 MTOPARA_TXPOWER_INDEX() = 10;
454                                 break;
455                         case RF_WB_242_1:
456                                 MTOPARA_TXPOWER_INDEX() = 24; // ->10 20060316.1 modify
457                                 break;
458                 }
459         }
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         //------------------------------------------------
464
465         // For RSSI turning 20060808.4 Cancel load from EEPROM
466         MTO_DATA().RSSI_high = -41;
467         MTO_DATA().RSSI_low = -60;
468 }
469
470 //---------------------------------------------------------------------------//
471 static u32 DTO_Rx_Info[13][3];
472 static u32 DTO_RxCRCFail_Info[13][3];
473 static u32 AntennaToggleBkoffTimer=5;
474 typedef struct{
475         int RxRate;
476         int RxRatePkts;
477         int index;
478 }RXRATE_ANT;
479 RXRATE_ANT RxRatePeakAnt[3];
480
481 #define ANT0    0
482 #define ANT1    1
483 #define OLD_ANT 2
484
485 void SearchPeakRxRate(int index)
486 {
487         int i;
488         RxRatePeakAnt[index].RxRatePkts=0;
489         //Find out the best rx rate which is used on different antenna
490         for(i=1;i<13;i++)
491         {
492                 if(DTO_Rx_Info[i][index] > (u32) RxRatePeakAnt[index].RxRatePkts)
493                 {
494                         RxRatePeakAnt[index].RxRatePkts = DTO_Rx_Info[i][index];
495                         RxRatePeakAnt[index].RxRate = rate_tbl[i];
496                         RxRatePeakAnt[index].index = i;
497                 }
498         }
499 }
500
501 void ResetDTO_RxInfo(int index, MTO_FUNC_INPUT)
502 {
503         int i;
504
505         #ifdef _PE_DTO_DUMP_
506         WBDEBUG(("ResetDTOrx\n"));
507         #endif
508
509         for(i=0;i<13;i++)
510                 DTO_Rx_Info[i][index] = MTO_HAL()->rx_ok_count[i];
511
512         for(i=0;i<13;i++)
513                 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i];
514
515         TotalTxPkt = 0;
516         TotalTxPktRetry = 0;
517 }
518
519 void GetDTO_RxInfo(int index, MTO_FUNC_INPUT)
520 {
521         int i;
522
523         #ifdef _PE_DTO_DUMP_
524         WBDEBUG(("GetDTOrx\n"));
525         #endif
526
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]));
528         for(i=0;i<13;i++)
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;
531
532         for(i=0;i<13;i++)
533                 DTO_RxCRCFail_Info[i][index] = MTO_HAL()->rx_err_count[i] - DTO_RxCRCFail_Info[i][index];
534
535         TxPktPerAnt[index] = TotalTxPkt;
536         TxPktRetryPerAnt[index] = TotalTxPktRetry;
537         TotalTxPkt = 0;
538         TotalTxPktRetry = 0;
539 }
540
541 unsigned char TxDominate(int index)
542 {
543         int tmp;
544
545         tmp = TxPktPerAnt[index] + DTO_Rx_Info[0][index];
546
547         if(Divide(TxPktPerAnt[index]*100, tmp) > 40)
548                 return true;
549         else
550                 return false;
551 }
552
553 unsigned char CmpTxRetryRate(int index1, int index2)
554 {
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]);
558         #ifdef _PE_DTO_DUMP_
559         WBDEBUG(("[MTO]:TxRetry Ant0: (%d%%)  Ant1: (%d%%) \n ", tx_retry_rate1, tx_retry_rate2));
560         #endif
561
562         if(tx_retry_rate1 > tx_retry_rate2)
563                 return true;
564         else
565                 return false;
566 }
567
568 void GetFreshAntennaData(MTO_FUNC_INPUT)
569 {
570     u8      x;
571
572         x = hal_get_antenna_number(MTO_HAL());
573         //hal_get_bss_pk_cnt(MTO_HAL());
574         //hal_get_est_sq3(MTO_HAL(), 1);
575         old_antenna[0] = x;
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;
582
583         if (MTO_ANT_DIVERSITY() != MTO_ANTENNA_DIVERSITY_ON ||
584                 MTO_ANT_DIVERSITY_ENABLE() != 1)
585         AntennaToggleBkoffTimer=1;
586         #ifdef _PE_DTO_DUMP_
587         WBDEBUG(("[HHDTO]:**last data rate=%d,now data rate=%d**antenna toggle timer=%d",last_rate_ant,MTO_RATE_LEVEL(),AntennaToggleBkoffTimer));
588         #endif
589         last_rate_ant=MTO_RATE_LEVEL();
590         if(AntennaToggleBkoffTimer==0)
591         {
592                 MTO_TOGGLE_STATE() = TOGGLE_STATE_WAIT0;
593                 #ifdef _PE_DTO_DUMP_
594                 WBDEBUG(("[HHDTO]:===state is starting==for antenna toggle==="));
595                 #endif
596         }
597         else
598                 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
599
600         if ((MTO_BACKOFF_TMR()!=0)&&(MTO_RATE_LEVEL()>MTO_DataRateAvailableLevel - 3))
601         {
602                 MTO_TOGGLE_STATE() = TOGGLE_STATE_IDLE;
603                 #ifdef _PE_DTO_DUMP_
604                 WBDEBUG(("[HHDTO]:===the data rate is %d (good)and will not toogle  ===",MTO_DATA_RATE()>>1));
605                 #endif
606         }
607
608
609 }
610
611 int WB_PCR[2]; //packet correct rate
612
613 u8 CalcNewRate(MTO_FUNC_INPUT, u8 old_rate, u32 retry_cnt, u32 tx_frag_cnt)
614 {
615         int i;
616         u8 new_rate;
617     u32 retry_rate;
618         int TxThrouput1, TxThrouput2, TxThrouput3, BestThroupht;
619
620         if(tx_frag_cnt < MTOPARA_TXCOUNT_TH_FOR_CALC_RATE()) //too few packets transmit
621         {
622                 return 0xff;
623         }
624         retry_rate = Divide(retry_cnt * 100, tx_frag_cnt);
625
626         if(retry_rate > 90) retry_rate = 90; //always truncate to 90% due to lookup table size
627         #ifdef _PE_DTO_DUMP_
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]));
641         #endif
642
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
646         else
647         {
648                 for (i=0;i<MTO_DataRateAvailableLevel;i++) //reset all retry rate
649                         retryrate_rec[i]=0;
650                 retryrate_rec[old_rate] = retry_rate;
651                 #ifdef _PE_DTO_DUMP_
652                 WBDEBUG(("Reset retry rate table\n"));
653                 #endif
654         }
655
656         if(TxRateRec.tx_rate > old_rate)   //Decrease Tx Rate
657         {
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)
661                 {
662                         new_rate = TxRateRec.tx_rate;
663                         BestThroupht = TxThrouput1;
664                 }
665                 else
666                 {
667                         new_rate = old_rate;
668                         BestThroupht = TxThrouput2;
669                 }
670                 if((old_rate > 0) &&(retry_rate>MTOPARA_TXRATE_DEC_TH()))   //Min Rate
671                 {
672                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
673                         if(BestThroupht < TxThrouput3)
674                         {
675                                 new_rate = old_rate - 1;
676                                 #ifdef _PE_DTO_DUMP_
677                                 WBDEBUG(("--------\n"));
678                                 #endif
679                                 BestThroupht = TxThrouput3;
680                         }
681                 }
682         }
683         else if(TxRateRec.tx_rate < old_rate)  //Increase Tx Rate
684         {
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)
688                 {
689                         new_rate = TxRateRec.tx_rate;
690                         BestThroupht = TxThrouput1;
691                 }
692                 else
693                 {
694                         new_rate = old_rate;
695                         BestThroupht = TxThrouput2;
696                 }
697                 if ((old_rate < MTO_DataRateAvailableLevel - 1)&&(retry_rate<MTOPARA_TXRATE_INC_TH()))
698                 {
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]];
702                         else
703                                 TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
704                         if(BestThroupht < TxThrouput3)
705                         {
706                                 new_rate = old_rate + 1;
707                                 #ifdef _PE_DTO_DUMP_
708                                 WBDEBUG(("++++++++++\n"));
709                                 #endif
710                                 BestThroupht = TxThrouput3;
711                         }
712                 }
713         }
714         else  //Tx Rate no change
715         {
716                 TxThrouput2 = Rate_PER_TBL[retry_rate][Level2PerTbl[old_rate]];
717                 new_rate = old_rate;
718                 BestThroupht = TxThrouput2;
719
720                 if (retry_rate <MTOPARA_TXRATE_EQ_TH())    //th for change higher rate
721                 {
722                         if(old_rate < MTO_DataRateAvailableLevel - 1)
723                         {
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]];
727                                 else
728                                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate+1]][Level2PerTbl[old_rate+1]];
729                                 if(BestThroupht < TxThrouput3)
730                                 {
731                                         new_rate = old_rate + 1;
732                                         BestThroupht = TxThrouput3;
733                                         #ifdef _PE_DTO_DUMP_
734                                         WBDEBUG(("=++++++++++\n"));
735                                         #endif
736                                 }
737                         }
738                 }
739                 else
740                 if(old_rate > 0)   //Min Rate
741                 {
742                         TxThrouput3 = Rate_PER_TBL[retryrate_rec[old_rate-1]][Level2PerTbl[old_rate-1]];
743                         if(BestThroupht < TxThrouput3)
744                         {
745                                 new_rate = old_rate - 1;
746                                 #ifdef _PE_DTO_DUMP_
747                                 WBDEBUG(("=--------\n"));
748                                 #endif
749                                 BestThroupht = TxThrouput3;
750                         }
751                 }
752         }
753
754         if (!LOCAL_IS_IBSS_MODE())
755         {
756         max_rssi_rate = GetMaxRateLevelFromRSSI();
757         #ifdef _PE_DTO_DUMP_
758         WBDEBUG(("[MTO]:RSSI2Rate=%d\n", MTO_Data_Rate_Tbl[max_rssi_rate]));
759         #endif
760         if(new_rate > max_rssi_rate)
761                 new_rate = max_rssi_rate;
762         }
763
764         //save new rate;
765         TxRateRec.tx_rate = old_rate;
766         TxRateRec.tx_retry_rate = (u8) retry_rate;
767         TxRetryRate = retry_rate;
768         return new_rate;
769 }
770
771 void SmoothRSSI(s32 new_rssi)
772 {
773         RSSISmoothed = RSSISmoothed + new_rssi - RSSIBuf[RSSIBufIndex];
774         RSSIBuf[RSSIBufIndex] = new_rssi;
775         RSSIBufIndex = (RSSIBufIndex + 1) % 10;
776 }
777
778 u8 GetMaxRateLevelFromRSSI(void)
779 {
780         u8 i;
781         u8 TxRate;
782
783         for(i=0;i<RSSI2RATE_SIZE;i++)
784         {
785                 if(RSSISmoothed > RSSI2RateTbl[i].RSSI)
786                         break;
787         }
788         #ifdef _PE_DTO_DUMP_
789         WBDEBUG(("[MTO]:RSSI=%d\n", Divide(RSSISmoothed, 10)));
790         #endif
791         if(i < RSSI2RATE_SIZE)
792                 TxRate = RSSI2RateTbl[i].TxRate;
793         else
794                 TxRate = 2;  //divided by 2 = 1Mbps
795
796         for(i=MTO_DataRateAvailableLevel-1;i>0;i--)
797         {
798                 if(TxRate >=MTO_Data_Rate_Tbl[i])
799                         break;
800         }
801         return i;
802 }
803
804 //===========================================================================
805 //  Description:
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)
810 {
811         MTO_TXFLOWCOUNT()++;
812         if ((MTO_ENABLE==1) && (MTO_RATE_CHANGE_ENABLE()==1))
813         {
814                 if(tx_rate == MTO_DATA_RATE())
815                 {
816                         if (index == 0)
817                         {
818                                 if (boSparseTxTraffic)
819                                         MTO_HAL()->dto_tx_frag_count += MTOPARA_PERIODIC_CHECK_CYCLE();
820                                 else
821                                         MTO_HAL()->dto_tx_frag_count += 1;
822                         }
823                         else
824                         {
825                                 if (index<8)
826                                 {
827                                         MTO_HAL()->dto_tx_retry_count += index;
828                                         MTO_HAL()->dto_tx_frag_count += (index+1);
829                                 }
830                                 else
831                                 {
832                                         MTO_HAL()->dto_tx_retry_count += 7;
833                                         MTO_HAL()->dto_tx_frag_count += 7;
834                                 }
835                         }
836                 }
837                 else if(MTO_DATA_RATE()>48 && tx_rate ==48)
838                 {//ALFRED
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
842                         {
843                                 MTO_HAL()->dto_tx_retry_count += index;
844                                 MTO_HAL()->dto_tx_frag_count += (index+1);
845                         }
846                         else
847                         {
848                                 MTO_HAL()->dto_tx_retry_count += 3;
849                                 MTO_HAL()->dto_tx_frag_count += 3;
850                         }
851
852                 }
853         }
854         else
855         {
856                 MTO_HAL()->dto_tx_retry_count += index;
857                 MTO_HAL()->dto_tx_frag_count += (index+1);
858         }
859         TotalTxPkt ++;
860         TotalTxPktRetry += (index+1);
861
862         PeriodTotalTxPkt ++;
863         PeriodTotalTxPktRetry += (index+1);
864 }
865
866 u8 MTO_GetTxFallbackRate(MTO_FUNC_INPUT)
867 {
868         return MTO_DATA_FALLBACK_RATE();
869 }
870
871
872 //===========================================================================
873 //  MTO_TxFailed --
874 //
875 //  Description:
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.
879 //
880 //  Arguments:
881 //    adapter      - The pointer to the Miniport adapter Context
882 //
883 //  Return Value:
884 //    None
885 //============================================================================
886 void MTO_TxFailed(MTO_FUNC_INPUT)
887 {
888         return;
889 }
890
891 int Divide(int a, int b)
892 {
893         if (b==0) b=1;
894         return a/b;
895 }
896
897