Merge branch 'ipvs'
[cascardo/linux.git] / drivers / staging / vt6655 / datarate.c
1 /*
2  * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
3  * All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * File: datarate.c
20  *
21  * Purpose: Handles the auto fallback & data rates functions
22  *
23  * Author: Lyndon Chen
24  *
25  * Date: July 17, 2002
26  *
27  * Functions:
28  *      RATEvParseMaxRate - Parsing the highest basic & support rate in rate field of frame
29  *      RATEvTxRateFallBack - Rate fallback Algorithm Implementaion
30  *      RATEuSetIE- Set rate IE field.
31  *
32  * Revision History:
33  *
34  */
35
36 #include "ttype.h"
37 #include "tmacro.h"
38 #include "mac.h"
39 #include "80211mgr.h"
40 #include "bssdb.h"
41 #include "datarate.h"
42 #include "card.h"
43 #include "baseband.h"
44 #include "srom.h"
45
46 /*---------------------  Static Definitions -------------------------*/
47
48 /*---------------------  Static Classes  ----------------------------*/
49
50 extern unsigned short TxRate_iwconfig; //2008-5-8 <add> by chester
51 /*---------------------  Static Variables  --------------------------*/
52 static int msglevel = MSG_LEVEL_INFO;
53 static const unsigned char acbyIERate[MAX_RATE] =
54 {0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
55
56 #define AUTORATE_TXOK_CNT       0x0400
57 #define AUTORATE_TXFAIL_CNT     0x0064
58 #define AUTORATE_TIMEOUT        10
59
60 /*---------------------  Static Functions  --------------------------*/
61
62 void s_vResetCounter(
63         PKnownNodeDB psNodeDBTable
64 );
65
66 void
67 s_vResetCounter(
68         PKnownNodeDB psNodeDBTable
69 )
70 {
71         unsigned char ii;
72
73         // clear statistic counter for auto_rate
74         for (ii = 0; ii <= MAX_RATE; ii++) {
75                 psNodeDBTable->uTxOk[ii] = 0;
76                 psNodeDBTable->uTxFail[ii] = 0;
77         }
78 }
79
80 /*---------------------  Export Variables  --------------------------*/
81
82 /*---------------------  Export Functions  --------------------------*/
83
84 /*+
85  *
86  * Description:
87  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
88  *
89  * Parameters:
90  *  In:
91  *      unsigned char - Rate value in SuppRates IE or ExtSuppRates IE
92  *  Out:
93  *      none
94  *
95  * Return Value: RateIdx
96  *
97  -*/
98 unsigned char
99 DATARATEbyGetRateIdx(
100         unsigned char byRate
101 )
102 {
103         unsigned char ii;
104
105         //Erase basicRate flag.
106         byRate = byRate & 0x7F;//0111 1111
107
108         for (ii = 0; ii < MAX_RATE; ii++) {
109                 if (acbyIERate[ii] == byRate)
110                         return ii;
111         }
112         return 0;
113 }
114
115 /*+
116  *
117  * Routine Description:
118  *      Rate fallback Algorithm Implementation
119  *
120  * Parameters:
121  *  In:
122  *      pDevice         - Pointer to the adapter
123  *      psNodeDBTable   - Pointer to Node Data Base
124  *  Out:
125  *      none
126  *
127  * Return Value: none
128  *
129  -*/
130 #define AUTORATE_TXCNT_THRESHOLD        20
131 #define AUTORATE_INC_THRESHOLD          30
132
133 /*+
134  *
135  * Description:
136  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
137  *
138  * Parameters:
139  *  In:
140  *      unsigned char - Rate value in SuppRates IE or ExtSuppRates IE
141  *  Out:
142  *      none
143  *
144  * Return Value: RateIdx
145  *
146  -*/
147 unsigned short
148 wGetRateIdx(
149         unsigned char byRate
150 )
151 {
152         unsigned short ii;
153
154         //Erase basicRate flag.
155         byRate = byRate & 0x7F;//0111 1111
156
157         for (ii = 0; ii < MAX_RATE; ii++) {
158                 if (acbyIERate[ii] == byRate)
159                         return ii;
160         }
161         return 0;
162 }
163
164 /*+
165  *
166  * Description:
167  *      Parsing the highest basic & support rate in rate field of frame.
168  *
169  * Parameters:
170  *  In:
171  *      pDevice         - Pointer to the adapter
172  *      pItemRates      - Pointer to Rate field defined in 802.11 spec.
173  *      pItemExtRates      - Pointer to Extended Rate field defined in 802.11 spec.
174  *  Out:
175  *      pwMaxBasicRate  - Maximum Basic Rate
176  *      pwMaxSuppRate   - Maximum Supported Rate
177  *      pbyTopCCKRate   - Maximum Basic Rate in CCK mode
178  *      pbyTopOFDMRate  - Maximum Basic Rate in OFDM mode
179  *
180  * Return Value: none
181  *
182  -*/
183 void
184 RATEvParseMaxRate(
185         void *pDeviceHandler,
186         PWLAN_IE_SUPP_RATES pItemRates,
187         PWLAN_IE_SUPP_RATES pItemExtRates,
188         bool bUpdateBasicRate,
189         unsigned short *pwMaxBasicRate,
190         unsigned short *pwMaxSuppRate,
191         unsigned short *pwSuppRate,
192         unsigned char *pbyTopCCKRate,
193         unsigned char *pbyTopOFDMRate
194 )
195 {
196         PSDevice  pDevice = (PSDevice) pDeviceHandler;
197         unsigned int ii;
198         unsigned char byHighSuppRate = 0;
199         unsigned char byRate = 0;
200         unsigned short wOldBasicRate = pDevice->wBasicRate;
201         unsigned int uRateLen;
202
203         if (pItemRates == NULL)
204                 return;
205
206         *pwSuppRate = 0;
207         uRateLen = pItemRates->len;
208
209         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate Len: %d\n", uRateLen);
210         if (pDevice->eCurrentPHYType != PHY_TYPE_11B) {
211                 if (uRateLen > WLAN_RATES_MAXLEN)
212                         uRateLen = WLAN_RATES_MAXLEN;
213         } else {
214                 if (uRateLen > WLAN_RATES_MAXLEN_11B)
215                         uRateLen = WLAN_RATES_MAXLEN_11B;
216         }
217
218         for (ii = 0; ii < uRateLen; ii++) {
219                 byRate = (unsigned char)(pItemRates->abyRates[ii]);
220                 if (WLAN_MGMT_IS_BASICRATE(byRate) && bUpdateBasicRate)  {
221                         // Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate
222                         CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
223                         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate AddBasicRate: %d\n", wGetRateIdx(byRate));
224                 }
225                 byRate = (unsigned char)(pItemRates->abyRates[ii]&0x7F);
226                 if (byHighSuppRate == 0)
227                         byHighSuppRate = byRate;
228                 if (byRate > byHighSuppRate)
229                         byHighSuppRate = byRate;
230                 *pwSuppRate |= (1<<wGetRateIdx(byRate));
231         }
232         if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
233             (pDevice->eCurrentPHYType != PHY_TYPE_11B)) {
234                 unsigned int uExtRateLen = pItemExtRates->len;
235
236                 if (uExtRateLen > WLAN_RATES_MAXLEN)
237                         uExtRateLen = WLAN_RATES_MAXLEN;
238
239                 for (ii = 0; ii < uExtRateLen; ii++) {
240                         byRate = (unsigned char)(pItemExtRates->abyRates[ii]);
241                         // select highest basic rate
242                         if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
243                                 // Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate
244                                 CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
245                                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "ParseMaxRate AddBasicRate: %d\n", wGetRateIdx(byRate));
246                         }
247                         byRate = (unsigned char)(pItemExtRates->abyRates[ii]&0x7F);
248                         if (byHighSuppRate == 0)
249                                 byHighSuppRate = byRate;
250                         if (byRate > byHighSuppRate)
251                                 byHighSuppRate = byRate;
252                         *pwSuppRate |= (1<<wGetRateIdx(byRate));
253                 }
254         }
255
256         if ((pDevice->byPacketType == PK_TYPE_11GB) && CARDbIsOFDMinBasicRate((void *)pDevice))
257                 pDevice->byPacketType = PK_TYPE_11GA;
258
259         *pbyTopCCKRate = pDevice->byTopCCKBasicRate;
260         *pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
261         *pwMaxSuppRate = wGetRateIdx(byHighSuppRate);
262         if ((pDevice->byPacketType == PK_TYPE_11B) || (pDevice->byPacketType == PK_TYPE_11GB))
263                 *pwMaxBasicRate = pDevice->byTopCCKBasicRate;
264         else
265                 *pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
266         if (wOldBasicRate != pDevice->wBasicRate)
267                 CARDvSetRSPINF((void *)pDevice, pDevice->eCurrentPHYType);
268
269         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "Exit ParseMaxRate\n");
270 }
271
272 /*+
273  *
274  * Routine Description:
275  *      Rate fallback Algorithm Implementaion
276  *
277  * Parameters:
278  *  In:
279  *      pDevice         - Pointer to the adapter
280  *      psNodeDBTable   - Pointer to Node Data Base
281  *  Out:
282  *      none
283  *
284  * Return Value: none
285  *
286  -*/
287 #define AUTORATE_TXCNT_THRESHOLD        20
288 #define AUTORATE_INC_THRESHOLD          30
289
290 void
291 RATEvTxRateFallBack(
292         void *pDeviceHandler,
293         PKnownNodeDB psNodeDBTable
294 )
295 {
296         PSDevice        pDevice = (PSDevice) pDeviceHandler;
297         unsigned short wIdxDownRate = 0;
298         unsigned int ii;
299         bool bAutoRate[MAX_RATE]    = {true, true, true, true, false, false, true, true, true, true, true, true};
300         unsigned long dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180, 240, 360, 480, 540};
301         unsigned long dwThroughput = 0;
302         unsigned short wIdxUpRate = 0;
303         unsigned long dwTxDiff = 0;
304
305         if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING) {
306                 // Don't do Fallback when scanning Channel
307                 return;
308         }
309
310         psNodeDBTable->uTimeCount++;
311
312         if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
313                 dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
314
315         if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
316             (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
317             (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
318                 return;
319         }
320
321         if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT)
322                 psNodeDBTable->uTimeCount = 0;
323
324         for (ii = 0; ii < MAX_RATE; ii++) {
325                 if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
326                         if (bAutoRate[ii])
327                                 wIdxUpRate = (unsigned short) ii;
328
329                 } else {
330                         bAutoRate[ii] = false;
331                 }
332         }
333
334         for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
335                 if ((psNodeDBTable->uTxOk[ii] != 0) ||
336                     (psNodeDBTable->uTxFail[ii] != 0)) {
337                         dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
338                         if (ii < RATE_11M)
339                                 psNodeDBTable->uTxFail[ii] *= 4;
340
341                         dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
342                 }
343         }
344         dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
345
346         wIdxDownRate = psNodeDBTable->wTxDataRate;
347         for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
348                 ii--;
349                 if ((dwThroughputTbl[ii] > dwThroughput) && bAutoRate[ii]) {
350                         dwThroughput = dwThroughputTbl[ii];
351                         wIdxDownRate = (unsigned short) ii;
352                 }
353         }
354         psNodeDBTable->wTxDataRate = wIdxDownRate;
355         if (psNodeDBTable->uTxOk[MAX_RATE]) {
356                 if (psNodeDBTable->uTxOk[MAX_RATE] >
357                     (psNodeDBTable->uTxFail[MAX_RATE] * 4)) {
358                         psNodeDBTable->wTxDataRate = wIdxUpRate;
359                 }
360         } else { // adhoc, if uTxOk =0 & uTxFail = 0
361                 if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
362                         psNodeDBTable->wTxDataRate = wIdxUpRate;
363         }
364 //2008-5-8 <add> by chester
365         TxRate_iwconfig = psNodeDBTable->wTxDataRate;
366         s_vResetCounter(psNodeDBTable);
367
368         return;
369 }
370
371 /*+
372  *
373  * Description:
374  *    This routine is used to assemble available Rate IE.
375  *
376  * Parameters:
377  *  In:
378  *    pDevice
379  *  Out:
380  *
381  * Return Value: None
382  *
383  -*/
384 unsigned char
385 RATEuSetIE(
386         PWLAN_IE_SUPP_RATES pSrcRates,
387         PWLAN_IE_SUPP_RATES pDstRates,
388         unsigned int uRateLen
389 )
390 {
391         unsigned int ii, uu, uRateCnt = 0;
392
393         if ((pSrcRates == NULL) || (pDstRates == NULL))
394                 return 0;
395
396         if (pSrcRates->len == 0)
397                 return 0;
398
399         for (ii = 0; ii < uRateLen; ii++) {
400                 for (uu = 0; uu < pSrcRates->len; uu++) {
401                         if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
402                                 pDstRates->abyRates[uRateCnt++] = pSrcRates->abyRates[uu];
403                                 break;
404                         }
405                 }
406         }
407         return (unsigned char)uRateCnt;
408 }