datarate.c 10 KB

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