Merge branch 'for-3.11' of git://linux-nfs.org/~bfields/linux
[cascardo/linux.git] / drivers / staging / vt6656 / 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 "tmacro.h"
37 #include "mac.h"
38 #include "80211mgr.h"
39 #include "bssdb.h"
40 #include "datarate.h"
41 #include "card.h"
42 #include "baseband.h"
43 #include "srom.h"
44 #include "rf.h"
45
46 /* static int msglevel = MSG_LEVEL_DEBUG; */
47 static int          msglevel                =MSG_LEVEL_INFO;
48 const u8 acbyIERate[MAX_RATE] =
49 {0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C};
50
51 #define AUTORATE_TXOK_CNT       0x0400
52 #define AUTORATE_TXFAIL_CNT     0x0064
53 #define AUTORATE_TIMEOUT        10
54
55 void s_vResetCounter(PKnownNodeDB psNodeDBTable);
56
57 void s_vResetCounter(PKnownNodeDB psNodeDBTable)
58 {
59     u8            ii;
60
61     /* clear statistics counter for auto_rate */
62     for (ii = 0; ii <= MAX_RATE; ii++) {
63         psNodeDBTable->uTxOk[ii] = 0;
64         psNodeDBTable->uTxFail[ii] = 0;
65     }
66 }
67
68 /*+
69  *
70  * Routine Description:
71  *      Rate fallback Algorithm Implementaion
72  *
73  * Parameters:
74  *  In:
75  *      pDevice         - Pointer to the adapter
76  *      psNodeDBTable   - Pointer to Node Data Base
77  *  Out:
78  *      none
79  *
80  * Return Value: none
81  *
82 -*/
83 #define AUTORATE_TXCNT_THRESHOLD        20
84 #define AUTORATE_INC_THRESHOLD          30
85
86 /*+
87  *
88  * Description:
89  *      Get RateIdx from the value in SuppRates IE or ExtSuppRates IE
90  *
91  * Parameters:
92  *  In:
93  *      u8    - Rate value in SuppRates IE or ExtSuppRates IE
94  *  Out:
95  *      none
96  *
97  * Return Value: RateIdx
98  *
99 -*/
100 u16
101 RATEwGetRateIdx(
102      u8 byRate
103     )
104 {
105     u16    ii;
106
107     /* erase BasicRate flag */
108     byRate = byRate & 0x7F;
109
110     for (ii = 0; ii < MAX_RATE; ii ++) {
111         if (acbyIERate[ii] == byRate)
112             return ii;
113     }
114     return 0;
115 }
116
117 /*+
118  *
119  * Description:
120  *      Parsing the highest basic & support rate in rate field of frame.
121  *
122  * Parameters:
123  *  In:
124  *      pDevice         - Pointer to the adapter
125  *      pItemRates      - Pointer to Rate field defined in 802.11 spec.
126  *      pItemExtRates      - Pointer to Extended Rate field defined in 802.11 spec.
127  *  Out:
128  *      pwMaxBasicRate  - Maximum Basic Rate
129  *      pwMaxSuppRate   - Maximum Supported Rate
130  *      pbyTopCCKRate   - Maximum Basic Rate in CCK mode
131  *      pbyTopOFDMRate  - Maximum Basic Rate in OFDM mode
132  *
133  * Return Value: none
134  *
135 -*/
136
137 void RATEvParseMaxRate(struct vnt_private *pDevice,
138         PWLAN_IE_SUPP_RATES pItemRates, PWLAN_IE_SUPP_RATES pItemExtRates,
139         int bUpdateBasicRate, u16 *pwMaxBasicRate, u16 *pwMaxSuppRate,
140         u16 *pwSuppRate, u8 *pbyTopCCKRate, u8 *pbyTopOFDMRate)
141 {
142         int  ii;
143         u8 byHighSuppRate = 0, byRate = 0;
144         u16 wOldBasicRate = pDevice->wBasicRate;
145         u32 uRateLen;
146
147         if (pItemRates == NULL)
148                 return;
149
150     *pwSuppRate = 0;
151     uRateLen = pItemRates->len;
152
153     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate Len: %d\n", uRateLen);
154     if (pDevice->byBBType != BB_TYPE_11B) {
155         if (uRateLen > WLAN_RATES_MAXLEN)
156             uRateLen = WLAN_RATES_MAXLEN;
157     } else {
158         if (uRateLen > WLAN_RATES_MAXLEN_11B)
159             uRateLen = WLAN_RATES_MAXLEN_11B;
160     }
161
162     for (ii = 0; ii < uRateLen; ii++) {
163         byRate = (u8)(pItemRates->abyRates[ii]);
164         if (WLAN_MGMT_IS_BASICRATE(byRate) &&
165             (bUpdateBasicRate == true))  {
166           /*
167            * add to basic rate set, update pDevice->byTopCCKBasicRate and
168            * pDevice->byTopOFDMBasicRate
169            */
170                 CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
171             DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
172         }
173         byRate = (u8)(pItemRates->abyRates[ii]&0x7F);
174         if (byHighSuppRate == 0)
175             byHighSuppRate = byRate;
176         if (byRate > byHighSuppRate)
177             byHighSuppRate = byRate;
178         *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
179     }
180     if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
181         (pDevice->byBBType != BB_TYPE_11B)) {
182
183         unsigned int uExtRateLen = pItemExtRates->len;
184
185         if (uExtRateLen > WLAN_RATES_MAXLEN)
186             uExtRateLen = WLAN_RATES_MAXLEN;
187
188         for (ii = 0; ii < uExtRateLen ; ii++) {
189             byRate = (u8)(pItemExtRates->abyRates[ii]);
190             /* select highest basic rate */
191             if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
192               /*
193                * add to basic rate set, update pDevice->byTopCCKBasicRate and
194                * pDevice->byTopOFDMBasicRate
195                */
196                     CARDbAddBasicRate((void *)pDevice, RATEwGetRateIdx(byRate));
197                 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"ParseMaxRate AddBasicRate: %d\n", RATEwGetRateIdx(byRate));
198             }
199             byRate = (u8)(pItemExtRates->abyRates[ii]&0x7F);
200             if (byHighSuppRate == 0)
201                 byHighSuppRate = byRate;
202             if (byRate > byHighSuppRate)
203                 byHighSuppRate = byRate;
204             *pwSuppRate |= (1<<RATEwGetRateIdx(byRate));
205
206             /* DBG_PRN_GRP09(("ParseMaxRate : HighSuppRate: %d, %X\n",
207                RATEwGetRateIdx(byRate), byRate)); */
208         }
209     }
210
211     if ((pDevice->byPacketType == PK_TYPE_11GB)
212         && CARDbIsOFDMinBasicRate((void *)pDevice)) {
213         pDevice->byPacketType = PK_TYPE_11GA;
214     }
215
216     *pbyTopCCKRate = pDevice->byTopCCKBasicRate;
217     *pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
218     *pwMaxSuppRate = RATEwGetRateIdx(byHighSuppRate);
219     if ((pDevice->byPacketType==PK_TYPE_11B) || (pDevice->byPacketType==PK_TYPE_11GB))
220        *pwMaxBasicRate = pDevice->byTopCCKBasicRate;
221     else
222        *pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
223     if (wOldBasicRate != pDevice->wBasicRate)
224         CARDvSetRSPINF((void *)pDevice, pDevice->byBBType);
225
226      DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Exit ParseMaxRate\n");
227 }
228
229 /*+
230  *
231  * Routine Description:
232  *      Rate fallback Algorithm Implementaion
233  *
234  * Parameters:
235  *  In:
236  *      pDevice         - Pointer to the adapter
237  *      psNodeDBTable   - Pointer to Node Data Base
238  *  Out:
239  *      none
240  *
241  * Return Value: none
242  *
243 -*/
244 #define AUTORATE_TXCNT_THRESHOLD        20
245 #define AUTORATE_INC_THRESHOLD          30
246
247 void RATEvTxRateFallBack(struct vnt_private *pDevice,
248         PKnownNodeDB psNodeDBTable)
249 {
250         struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
251         u16 wIdxDownRate = 0;
252         int ii;
253         int bAutoRate[MAX_RATE] = {true, true, true, true, false, false, true,
254                                          true, true, true, true, true};
255         u32 dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180,
256                 240, 360, 480, 540};
257         u32 dwThroughput = 0;
258         u16 wIdxUpRate = 0;
259         u32 dwTxDiff = 0;
260
261         if (pMgmt->eScanState != WMAC_NO_SCANNING)
262                 return; /* Don't do Fallback when scanning Channel */
263
264         psNodeDBTable->uTimeCount++;
265
266     if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
267         dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
268
269     if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
270         (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
271         (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
272         return;
273     }
274
275     if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT) {
276         psNodeDBTable->uTimeCount = 0;
277     }
278
279     for (ii = 0; ii < MAX_RATE; ii++) {
280         if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
281             if (bAutoRate[ii] == true) {
282                 wIdxUpRate = (u16) ii;
283             }
284         } else {
285             bAutoRate[ii] = false;
286         }
287     }
288
289     for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
290         if ( (psNodeDBTable->uTxOk[ii] != 0) ||
291              (psNodeDBTable->uTxFail[ii] != 0) ) {
292             dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
293             if (ii < RATE_11M) {
294                 psNodeDBTable->uTxFail[ii] *= 4;
295             }
296             dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
297         }
298         DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate %d,Ok: %d, Fail:%d, Throughput:%d\n",
299                        ii, (int)psNodeDBTable->uTxOk[ii], (int)psNodeDBTable->uTxFail[ii], (int)dwThroughputTbl[ii]);
300     }
301     dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
302
303     wIdxDownRate = psNodeDBTable->wTxDataRate;
304     for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
305         ii--;
306         if ( (dwThroughputTbl[ii] > dwThroughput) &&
307              (bAutoRate[ii]==true) ) {
308             dwThroughput = dwThroughputTbl[ii];
309             wIdxDownRate = (u16) ii;
310         }
311     }
312     psNodeDBTable->wTxDataRate = wIdxDownRate;
313     if (psNodeDBTable->uTxOk[MAX_RATE]) {
314         if (psNodeDBTable->uTxOk[MAX_RATE] >
315            (psNodeDBTable->uTxFail[MAX_RATE] * 4) ) {
316             psNodeDBTable->wTxDataRate = wIdxUpRate;
317         }
318     } else { /* adhoc, if uTxOk(total) == 0 & uTxFail(total) == 0 */
319         if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
320             psNodeDBTable->wTxDataRate = wIdxUpRate;
321     }
322
323     if (pDevice->byBBType == BB_TYPE_11A) {
324         if (psNodeDBTable->wTxDataRate <= RATE_11M)
325             psNodeDBTable->wTxDataRate = RATE_6M;
326     }
327     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"uTxOk[MAX_RATE] %d, uTxFail[MAX_RATE]:%d\n",(int)psNodeDBTable->uTxOk[MAX_RATE], (int)psNodeDBTable->uTxFail[MAX_RATE]);
328     s_vResetCounter(psNodeDBTable);
329     DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Rate: %d, U:%d, D:%d\n", (int)psNodeDBTable->wTxDataRate, (int)wIdxUpRate, (int)wIdxDownRate);
330     return;
331 }
332
333 /*+
334  *
335  * Description:
336  *    This routine is used to assemble available Rate IE.
337  *
338  * Parameters:
339  *  In:
340  *    pDevice
341  *  Out:
342  *
343  * Return Value: None
344  *
345 -*/
346 u8
347 RATEuSetIE (
348      PWLAN_IE_SUPP_RATES pSrcRates,
349      PWLAN_IE_SUPP_RATES pDstRates,
350      unsigned int                uRateLen
351     )
352 {
353     unsigned int ii, uu, uRateCnt = 0;
354
355     if ((pSrcRates == NULL) || (pDstRates == NULL))
356         return 0;
357
358     if (pSrcRates->len == 0)
359         return 0;
360
361     for (ii = 0; ii < uRateLen; ii++) {
362         for (uu = 0; uu < pSrcRates->len; uu++) {
363             if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
364                 pDstRates->abyRates[uRateCnt ++] = pSrcRates->abyRates[uu];
365                 break;
366             }
367         }
368     }
369     return (u8)uRateCnt;
370 }
371