datarate.c 12 KB

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