• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const unsigned char acbyIERate[MAX_RATE] = {
53 0x02, 0x04, 0x0B, 0x16, 0x0C, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C
54 };
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
s_vResetCounter(PKnownNodeDB psNodeDBTable)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
DATARATEbyGetRateIdx(unsigned char byRate)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
wGetRateIdx(unsigned char byRate)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 
162 	return 0;
163 }
164 
165 /*+
166  *
167  * Description:
168  *      Parsing the highest basic & support rate in rate field of frame.
169  *
170  * Parameters:
171  *  In:
172  *      pDevice         - Pointer to the adapter
173  *      pItemRates      - Pointer to Rate field defined in 802.11 spec.
174  *      pItemExtRates      - Pointer to Extended Rate field defined in 802.11 spec.
175  *  Out:
176  *      pwMaxBasicRate  - Maximum Basic Rate
177  *      pwMaxSuppRate   - Maximum Supported Rate
178  *      pbyTopCCKRate   - Maximum Basic Rate in CCK mode
179  *      pbyTopOFDMRate  - Maximum Basic Rate in OFDM mode
180  *
181  * Return Value: none
182  *
183  -*/
184 void
RATEvParseMaxRate(void * pDeviceHandler,PWLAN_IE_SUPP_RATES pItemRates,PWLAN_IE_SUPP_RATES pItemExtRates,bool bUpdateBasicRate,unsigned short * pwMaxBasicRate,unsigned short * pwMaxSuppRate,unsigned short * pwSuppRate,unsigned char * pbyTopCCKRate,unsigned char * pbyTopOFDMRate)185 RATEvParseMaxRate(
186 	void *pDeviceHandler,
187 	PWLAN_IE_SUPP_RATES pItemRates,
188 	PWLAN_IE_SUPP_RATES pItemExtRates,
189 	bool bUpdateBasicRate,
190 	unsigned short *pwMaxBasicRate,
191 	unsigned short *pwMaxSuppRate,
192 	unsigned short *pwSuppRate,
193 	unsigned char *pbyTopCCKRate,
194 	unsigned char *pbyTopOFDMRate
195 )
196 {
197 	struct vnt_private *pDevice = pDeviceHandler;
198 	unsigned int ii;
199 	unsigned char byHighSuppRate = 0;
200 	unsigned char byRate = 0;
201 	unsigned short wOldBasicRate = pDevice->wBasicRate;
202 	unsigned int uRateLen;
203 
204 	if (pItemRates == NULL)
205 		return;
206 
207 	*pwSuppRate = 0;
208 	uRateLen = pItemRates->len;
209 
210 	pr_debug("ParseMaxRate Len: %d\n", uRateLen);
211 	if (pDevice->eCurrentPHYType != PHY_TYPE_11B) {
212 		if (uRateLen > WLAN_RATES_MAXLEN)
213 			uRateLen = WLAN_RATES_MAXLEN;
214 	} else {
215 		if (uRateLen > WLAN_RATES_MAXLEN_11B)
216 			uRateLen = WLAN_RATES_MAXLEN_11B;
217 	}
218 
219 	for (ii = 0; ii < uRateLen; ii++) {
220 		byRate = (unsigned char)(pItemRates->abyRates[ii]);
221 		if (WLAN_MGMT_IS_BASICRATE(byRate) && bUpdateBasicRate)  {
222 			/* Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate */
223 			CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
224 			pr_debug("ParseMaxRate AddBasicRate: %d\n",
225 				 wGetRateIdx(byRate));
226 		}
227 		byRate = (unsigned char)(pItemRates->abyRates[ii]&0x7F);
228 		if (byHighSuppRate == 0)
229 			byHighSuppRate = byRate;
230 		if (byRate > byHighSuppRate)
231 			byHighSuppRate = byRate;
232 		*pwSuppRate |= (1<<wGetRateIdx(byRate));
233 	}
234 	if ((pItemExtRates != NULL) && (pItemExtRates->byElementID == WLAN_EID_EXTSUPP_RATES) &&
235 	    (pDevice->eCurrentPHYType != PHY_TYPE_11B)) {
236 		unsigned int uExtRateLen = pItemExtRates->len;
237 
238 		if (uExtRateLen > WLAN_RATES_MAXLEN)
239 			uExtRateLen = WLAN_RATES_MAXLEN;
240 
241 		for (ii = 0; ii < uExtRateLen; ii++) {
242 			byRate = (unsigned char)(pItemExtRates->abyRates[ii]);
243 			/* select highest basic rate */
244 			if (WLAN_MGMT_IS_BASICRATE(pItemExtRates->abyRates[ii])) {
245 				/* Add to basic rate set, update pDevice->byTopCCKBasicRate and pDevice->byTopOFDMBasicRate */
246 				CARDbAddBasicRate((void *)pDevice, wGetRateIdx(byRate));
247 				pr_debug("ParseMaxRate AddBasicRate: %d\n",
248 					 wGetRateIdx(byRate));
249 			}
250 			byRate = (unsigned char)(pItemExtRates->abyRates[ii]&0x7F);
251 			if (byHighSuppRate == 0)
252 				byHighSuppRate = byRate;
253 			if (byRate > byHighSuppRate)
254 				byHighSuppRate = byRate;
255 			*pwSuppRate |= (1<<wGetRateIdx(byRate));
256 		}
257 	}
258 
259 	if ((pDevice->byPacketType == PK_TYPE_11GB) && CARDbIsOFDMinBasicRate((void *)pDevice))
260 		pDevice->byPacketType = PK_TYPE_11GA;
261 
262 	*pbyTopCCKRate = pDevice->byTopCCKBasicRate;
263 	*pbyTopOFDMRate = pDevice->byTopOFDMBasicRate;
264 	*pwMaxSuppRate = wGetRateIdx(byHighSuppRate);
265 	if ((pDevice->byPacketType == PK_TYPE_11B) || (pDevice->byPacketType == PK_TYPE_11GB))
266 		*pwMaxBasicRate = pDevice->byTopCCKBasicRate;
267 	else
268 		*pwMaxBasicRate = pDevice->byTopOFDMBasicRate;
269 	if (wOldBasicRate != pDevice->wBasicRate)
270 		CARDvSetRSPINF((void *)pDevice, pDevice->eCurrentPHYType);
271 
272 	pr_debug("Exit ParseMaxRate\n");
273 }
274 
275 /*+
276  *
277  * Routine Description:
278  *      Rate fallback Algorithm Implementaion
279  *
280  * Parameters:
281  *  In:
282  *      pDevice         - Pointer to the adapter
283  *      psNodeDBTable   - Pointer to Node Data Base
284  *  Out:
285  *      none
286  *
287  * Return Value: none
288  *
289  -*/
290 #define AUTORATE_TXCNT_THRESHOLD        20
291 #define AUTORATE_INC_THRESHOLD          30
292 
293 void
RATEvTxRateFallBack(void * pDeviceHandler,PKnownNodeDB psNodeDBTable)294 RATEvTxRateFallBack(
295 	void *pDeviceHandler,
296 	PKnownNodeDB psNodeDBTable
297 )
298 {
299 	struct vnt_private *pDevice = pDeviceHandler;
300 	unsigned short wIdxDownRate = 0;
301 	unsigned int ii;
302 	bool bAutoRate[MAX_RATE]    = {true, true, true, true, false, false, true, true, true, true, true, true};
303 	unsigned long dwThroughputTbl[MAX_RATE] = {10, 20, 55, 110, 60, 90, 120, 180, 240, 360, 480, 540};
304 	unsigned long dwThroughput = 0;
305 	unsigned short wIdxUpRate = 0;
306 	unsigned long dwTxDiff = 0;
307 
308 	if (pDevice->pMgmt->eScanState != WMAC_NO_SCANNING)
309 		/* Don't do Fallback when scanning Channel */
310 		return;
311 
312 	psNodeDBTable->uTimeCount++;
313 
314 	if (psNodeDBTable->uTxFail[MAX_RATE] > psNodeDBTable->uTxOk[MAX_RATE])
315 		dwTxDiff = psNodeDBTable->uTxFail[MAX_RATE] - psNodeDBTable->uTxOk[MAX_RATE];
316 
317 	if ((psNodeDBTable->uTxOk[MAX_RATE] < AUTORATE_TXOK_CNT) &&
318 	    (dwTxDiff < AUTORATE_TXFAIL_CNT) &&
319 	    (psNodeDBTable->uTimeCount < AUTORATE_TIMEOUT)) {
320 		return;
321 	}
322 
323 	if (psNodeDBTable->uTimeCount >= AUTORATE_TIMEOUT)
324 		psNodeDBTable->uTimeCount = 0;
325 
326 	for (ii = 0; ii < MAX_RATE; ii++) {
327 		if (psNodeDBTable->wSuppRate & (0x0001<<ii)) {
328 			if (bAutoRate[ii])
329 				wIdxUpRate = (unsigned short) ii;
330 
331 		} else {
332 			bAutoRate[ii] = false;
333 		}
334 	}
335 
336 	for (ii = 0; ii <= psNodeDBTable->wTxDataRate; ii++) {
337 		if ((psNodeDBTable->uTxOk[ii] != 0) ||
338 		    (psNodeDBTable->uTxFail[ii] != 0)) {
339 			dwThroughputTbl[ii] *= psNodeDBTable->uTxOk[ii];
340 			if (ii < RATE_11M)
341 				psNodeDBTable->uTxFail[ii] *= 4;
342 
343 			dwThroughputTbl[ii] /= (psNodeDBTable->uTxOk[ii] + psNodeDBTable->uTxFail[ii]);
344 		}
345 	}
346 	dwThroughput = dwThroughputTbl[psNodeDBTable->wTxDataRate];
347 
348 	wIdxDownRate = psNodeDBTable->wTxDataRate;
349 	for (ii = psNodeDBTable->wTxDataRate; ii > 0;) {
350 		ii--;
351 		if ((dwThroughputTbl[ii] > dwThroughput) && bAutoRate[ii]) {
352 			dwThroughput = dwThroughputTbl[ii];
353 			wIdxDownRate = (unsigned short) ii;
354 		}
355 	}
356 	psNodeDBTable->wTxDataRate = wIdxDownRate;
357 	if (psNodeDBTable->uTxOk[MAX_RATE]) {
358 		if (psNodeDBTable->uTxOk[MAX_RATE] >
359 		    (psNodeDBTable->uTxFail[MAX_RATE] * 4)) {
360 			psNodeDBTable->wTxDataRate = wIdxUpRate;
361 		}
362 	} else {
363 		/* adhoc, if uTxOk =0 & uTxFail = 0 */
364 		if (psNodeDBTable->uTxFail[MAX_RATE] == 0)
365 			psNodeDBTable->wTxDataRate = wIdxUpRate;
366 	}
367 
368 	/* 2008-5-8 <add> by chester */
369 	TxRate_iwconfig = psNodeDBTable->wTxDataRate;
370 	s_vResetCounter(psNodeDBTable);
371 }
372 
373 /*+
374  *
375  * Description:
376  *    This routine is used to assemble available Rate IE.
377  *
378  * Parameters:
379  *  In:
380  *    pDevice
381  *  Out:
382  *
383  * Return Value: None
384  *
385  -*/
386 unsigned char
RATEuSetIE(PWLAN_IE_SUPP_RATES pSrcRates,PWLAN_IE_SUPP_RATES pDstRates,unsigned int uRateLen)387 RATEuSetIE(
388 	PWLAN_IE_SUPP_RATES pSrcRates,
389 	PWLAN_IE_SUPP_RATES pDstRates,
390 	unsigned int uRateLen
391 )
392 {
393 	unsigned int ii, uu, uRateCnt = 0;
394 
395 	if ((pSrcRates == NULL) || (pDstRates == NULL))
396 		return 0;
397 
398 	if (pSrcRates->len == 0)
399 		return 0;
400 
401 	for (ii = 0; ii < uRateLen; ii++) {
402 		for (uu = 0; uu < pSrcRates->len; uu++) {
403 			if ((pSrcRates->abyRates[uu] & 0x7F) == acbyIERate[ii]) {
404 				pDstRates->abyRates[uRateCnt++] = pSrcRates->abyRates[uu];
405 				break;
406 			}
407 		}
408 	}
409 	return (unsigned char)uRateCnt;
410 }
411