datarate.c 12 KB

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