rc.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656
  1. /*
  2. * Copyright (c) 2004 Video54 Technologies, Inc.
  3. * Copyright (c) 2004-2009 Atheros Communications, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <linux/slab.h>
  18. #include "ath9k.h"
  19. static const struct ath_rate_table ar5416_11na_ratetable = {
  20. 68,
  21. 8, /* MCS start */
  22. {
  23. [0] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 6000,
  24. 5400, 0, 12, 0, 0, 0, 0 }, /* 6 Mb */
  25. [1] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 9000,
  26. 7800, 1, 18, 0, 1, 1, 1 }, /* 9 Mb */
  27. [2] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000,
  28. 10000, 2, 24, 2, 2, 2, 2 }, /* 12 Mb */
  29. [3] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000,
  30. 13900, 3, 36, 2, 3, 3, 3 }, /* 18 Mb */
  31. [4] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000,
  32. 17300, 4, 48, 4, 4, 4, 4 }, /* 24 Mb */
  33. [5] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000,
  34. 23000, 5, 72, 4, 5, 5, 5 }, /* 36 Mb */
  35. [6] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000,
  36. 27400, 6, 96, 4, 6, 6, 6 }, /* 48 Mb */
  37. [7] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000,
  38. 29300, 7, 108, 4, 7, 7, 7 }, /* 54 Mb */
  39. [8] = { RC_HT_SDT_2040, WLAN_RC_PHY_HT_20_SS, 6500,
  40. 6400, 0, 0, 0, 38, 8, 38 }, /* 6.5 Mb */
  41. [9] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 13000,
  42. 12700, 1, 1, 2, 39, 9, 39 }, /* 13 Mb */
  43. [10] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 19500,
  44. 18800, 2, 2, 2, 40, 10, 40 }, /* 19.5 Mb */
  45. [11] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 26000,
  46. 25000, 3, 3, 4, 41, 11, 41 }, /* 26 Mb */
  47. [12] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 39000,
  48. 36700, 4, 4, 4, 42, 12, 42 }, /* 39 Mb */
  49. [13] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 52000,
  50. 48100, 5, 5, 4, 43, 13, 43 }, /* 52 Mb */
  51. [14] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 58500,
  52. 53500, 6, 6, 4, 44, 14, 44 }, /* 58.5 Mb */
  53. [15] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 65000,
  54. 59000, 7, 7, 4, 45, 16, 46 }, /* 65 Mb */
  55. [16] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS_HGI, 72200,
  56. 65400, 7, 7, 4, 45, 16, 46 }, /* 75 Mb */
  57. [17] = { RC_INVALID, WLAN_RC_PHY_HT_20_DS, 13000,
  58. 12700, 8, 8, 0, 47, 17, 47 }, /* 13 Mb */
  59. [18] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 26000,
  60. 24800, 9, 9, 2, 48, 18, 48 }, /* 26 Mb */
  61. [19] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 39000,
  62. 36600, 10, 10, 2, 49, 19, 49 }, /* 39 Mb */
  63. [20] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 52000,
  64. 48100, 11, 11, 4, 50, 20, 50 }, /* 52 Mb */
  65. [21] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 78000,
  66. 69500, 12, 12, 4, 51, 21, 51 }, /* 78 Mb */
  67. [22] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 104000,
  68. 89500, 13, 13, 4, 52, 22, 52 }, /* 104 Mb */
  69. [23] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 117000,
  70. 98900, 14, 14, 4, 53, 23, 53 }, /* 117 Mb */
  71. [24] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 130000,
  72. 108300, 15, 15, 4, 54, 25, 55 }, /* 130 Mb */
  73. [25] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS_HGI, 144400,
  74. 120000, 15, 15, 4, 54, 25, 55 }, /* 144.4 Mb */
  75. [26] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 19500,
  76. 17400, 16, 16, 0, 56, 26, 56 }, /* 19.5 Mb */
  77. [27] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 39000,
  78. 35100, 17, 17, 2, 57, 27, 57 }, /* 39 Mb */
  79. [28] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 58500,
  80. 52600, 18, 18, 2, 58, 28, 58 }, /* 58.5 Mb */
  81. [29] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 78000,
  82. 70400, 19, 19, 4, 59, 29, 59 }, /* 78 Mb */
  83. [30] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 117000,
  84. 104900, 20, 20, 4, 60, 31, 61 }, /* 117 Mb */
  85. [31] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS_HGI, 130000,
  86. 115800, 20, 20, 4, 60, 31, 61 }, /* 130 Mb*/
  87. [32] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 156000,
  88. 137200, 21, 21, 4, 62, 33, 63 }, /* 156 Mb */
  89. [33] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 173300,
  90. 151100, 21, 21, 4, 62, 33, 63 }, /* 173.3 Mb */
  91. [34] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 175500,
  92. 152800, 22, 22, 4, 64, 35, 65 }, /* 175.5 Mb */
  93. [35] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 195000,
  94. 168400, 22, 22, 4, 64, 35, 65 }, /* 195 Mb*/
  95. [36] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 195000,
  96. 168400, 23, 23, 4, 66, 37, 67 }, /* 195 Mb */
  97. [37] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 216700,
  98. 185000, 23, 23, 4, 66, 37, 67 }, /* 216.7 Mb */
  99. [38] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 13500,
  100. 13200, 0, 0, 0, 38, 38, 38 }, /* 13.5 Mb*/
  101. [39] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 27500,
  102. 25900, 1, 1, 2, 39, 39, 39 }, /* 27.0 Mb*/
  103. [40] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 40500,
  104. 38600, 2, 2, 2, 40, 40, 40 }, /* 40.5 Mb*/
  105. [41] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 54000,
  106. 49800, 3, 3, 4, 41, 41, 41 }, /* 54 Mb */
  107. [42] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 81500,
  108. 72200, 4, 4, 4, 42, 42, 42 }, /* 81 Mb */
  109. [43] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 108000,
  110. 92900, 5, 5, 4, 43, 43, 43 }, /* 108 Mb */
  111. [44] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 121500,
  112. 102700, 6, 6, 4, 44, 44, 44 }, /* 121.5 Mb*/
  113. [45] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 135000,
  114. 112000, 7, 7, 4, 45, 46, 46 }, /* 135 Mb */
  115. [46] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000,
  116. 122000, 7, 7, 4, 45, 46, 46 }, /* 150 Mb */
  117. [47] = { RC_INVALID, WLAN_RC_PHY_HT_40_DS, 27000,
  118. 25800, 8, 8, 0, 47, 47, 47 }, /* 27 Mb */
  119. [48] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 54000,
  120. 49800, 9, 9, 2, 48, 48, 48 }, /* 54 Mb */
  121. [49] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 81000,
  122. 71900, 10, 10, 2, 49, 49, 49 }, /* 81 Mb */
  123. [50] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 108000,
  124. 92500, 11, 11, 4, 50, 50, 50 }, /* 108 Mb */
  125. [51] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 162000,
  126. 130300, 12, 12, 4, 51, 51, 51 }, /* 162 Mb */
  127. [52] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 216000,
  128. 162800, 13, 13, 4, 52, 52, 52 }, /* 216 Mb */
  129. [53] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 243000,
  130. 178200, 14, 14, 4, 53, 53, 53 }, /* 243 Mb */
  131. [54] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 270000,
  132. 192100, 15, 15, 4, 54, 55, 55 }, /* 270 Mb */
  133. [55] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS_HGI, 300000,
  134. 207000, 15, 15, 4, 54, 55, 55 }, /* 300 Mb */
  135. [56] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 40500,
  136. 36100, 16, 16, 0, 56, 56, 56 }, /* 40.5 Mb */
  137. [57] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 81000,
  138. 72900, 17, 17, 2, 57, 57, 57 }, /* 81 Mb */
  139. [58] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 121500,
  140. 108300, 18, 18, 2, 58, 58, 58 }, /* 121.5 Mb */
  141. [59] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 162000,
  142. 142000, 19, 19, 4, 59, 59, 59 }, /* 162 Mb */
  143. [60] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 243000,
  144. 205100, 20, 20, 4, 60, 61, 61 }, /* 243 Mb */
  145. [61] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS_HGI, 270000,
  146. 224700, 20, 20, 4, 60, 61, 61 }, /* 270 Mb */
  147. [62] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 324000,
  148. 263100, 21, 21, 4, 62, 63, 63 }, /* 324 Mb */
  149. [63] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 360000,
  150. 288000, 21, 21, 4, 62, 63, 63 }, /* 360 Mb */
  151. [64] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 364500,
  152. 290700, 22, 22, 4, 64, 65, 65 }, /* 364.5 Mb */
  153. [65] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 405000,
  154. 317200, 22, 22, 4, 64, 65, 65 }, /* 405 Mb */
  155. [66] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 405000,
  156. 317200, 23, 23, 4, 66, 67, 67 }, /* 405 Mb */
  157. [67] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 450000,
  158. 346400, 23, 23, 4, 66, 67, 67 }, /* 450 Mb */
  159. },
  160. 50, /* probe interval */
  161. WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
  162. };
  163. /* 4ms frame limit not used for NG mode. The values filled
  164. * for HT are the 64K max aggregate limit */
  165. static const struct ath_rate_table ar5416_11ng_ratetable = {
  166. 72,
  167. 12, /* MCS start */
  168. {
  169. [0] = { RC_ALL, WLAN_RC_PHY_CCK, 1000,
  170. 900, 0, 2, 0, 0, 0, 0 }, /* 1 Mb */
  171. [1] = { RC_ALL, WLAN_RC_PHY_CCK, 2000,
  172. 1900, 1, 4, 1, 1, 1, 1 }, /* 2 Mb */
  173. [2] = { RC_ALL, WLAN_RC_PHY_CCK, 5500,
  174. 4900, 2, 11, 2, 2, 2, 2 }, /* 5.5 Mb */
  175. [3] = { RC_ALL, WLAN_RC_PHY_CCK, 11000,
  176. 8100, 3, 22, 3, 3, 3, 3 }, /* 11 Mb */
  177. [4] = { RC_INVALID, WLAN_RC_PHY_OFDM, 6000,
  178. 5400, 4, 12, 4, 4, 4, 4 }, /* 6 Mb */
  179. [5] = { RC_INVALID, WLAN_RC_PHY_OFDM, 9000,
  180. 7800, 5, 18, 4, 5, 5, 5 }, /* 9 Mb */
  181. [6] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000,
  182. 10100, 6, 24, 6, 6, 6, 6 }, /* 12 Mb */
  183. [7] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000,
  184. 14100, 7, 36, 6, 7, 7, 7 }, /* 18 Mb */
  185. [8] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000,
  186. 17700, 8, 48, 8, 8, 8, 8 }, /* 24 Mb */
  187. [9] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000,
  188. 23700, 9, 72, 8, 9, 9, 9 }, /* 36 Mb */
  189. [10] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000,
  190. 27400, 10, 96, 8, 10, 10, 10 }, /* 48 Mb */
  191. [11] = { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000,
  192. 30900, 11, 108, 8, 11, 11, 11 }, /* 54 Mb */
  193. [12] = { RC_INVALID, WLAN_RC_PHY_HT_20_SS, 6500,
  194. 6400, 0, 0, 4, 42, 12, 42 }, /* 6.5 Mb */
  195. [13] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 13000,
  196. 12700, 1, 1, 6, 43, 13, 43 }, /* 13 Mb */
  197. [14] = { RC_HT_SDT_20, WLAN_RC_PHY_HT_20_SS, 19500,
  198. 18800, 2, 2, 6, 44, 14, 44 }, /* 19.5 Mb*/
  199. [15] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 26000,
  200. 25000, 3, 3, 8, 45, 15, 45 }, /* 26 Mb */
  201. [16] = { RC_HT_SD_20, WLAN_RC_PHY_HT_20_SS, 39000,
  202. 36700, 4, 4, 8, 46, 16, 46 }, /* 39 Mb */
  203. [17] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 52000,
  204. 48100, 5, 5, 8, 47, 17, 47 }, /* 52 Mb */
  205. [18] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 58500,
  206. 53500, 6, 6, 8, 48, 18, 48 }, /* 58.5 Mb */
  207. [19] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS, 65000,
  208. 59000, 7, 7, 8, 49, 20, 50 }, /* 65 Mb */
  209. [20] = { RC_HT_S_20, WLAN_RC_PHY_HT_20_SS_HGI, 72200,
  210. 65400, 7, 7, 8, 49, 20, 50 }, /* 65 Mb*/
  211. [21] = { RC_INVALID, WLAN_RC_PHY_HT_20_DS, 13000,
  212. 12700, 8, 8, 4, 51, 21, 51 }, /* 13 Mb */
  213. [22] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 26000,
  214. 24800, 9, 9, 6, 52, 22, 52 }, /* 26 Mb */
  215. [23] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_DS, 39000,
  216. 36600, 10, 10, 6, 53, 23, 53 }, /* 39 Mb */
  217. [24] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 52000,
  218. 48100, 11, 11, 8, 54, 24, 54 }, /* 52 Mb */
  219. [25] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 78000,
  220. 69500, 12, 12, 8, 55, 25, 55 }, /* 78 Mb */
  221. [26] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 104000,
  222. 89500, 13, 13, 8, 56, 26, 56 }, /* 104 Mb */
  223. [27] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 117000,
  224. 98900, 14, 14, 8, 57, 27, 57 }, /* 117 Mb */
  225. [28] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS, 130000,
  226. 108300, 15, 15, 8, 58, 29, 59 }, /* 130 Mb */
  227. [29] = { RC_HT_DT_20, WLAN_RC_PHY_HT_20_DS_HGI, 144400,
  228. 120000, 15, 15, 8, 58, 29, 59 }, /* 144.4 Mb */
  229. [30] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 19500,
  230. 17400, 16, 16, 4, 60, 30, 60 }, /* 19.5 Mb */
  231. [31] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 39000,
  232. 35100, 17, 17, 6, 61, 31, 61 }, /* 39 Mb */
  233. [32] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 58500,
  234. 52600, 18, 18, 6, 62, 32, 62 }, /* 58.5 Mb */
  235. [33] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 78000,
  236. 70400, 19, 19, 8, 63, 33, 63 }, /* 78 Mb */
  237. [34] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS, 117000,
  238. 104900, 20, 20, 8, 64, 35, 65 }, /* 117 Mb */
  239. [35] = { RC_INVALID, WLAN_RC_PHY_HT_20_TS_HGI, 130000,
  240. 115800, 20, 20, 8, 64, 35, 65 }, /* 130 Mb */
  241. [36] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 156000,
  242. 137200, 21, 21, 8, 66, 37, 67 }, /* 156 Mb */
  243. [37] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 173300,
  244. 151100, 21, 21, 8, 66, 37, 67 }, /* 173.3 Mb */
  245. [38] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 175500,
  246. 152800, 22, 22, 8, 68, 39, 69 }, /* 175.5 Mb */
  247. [39] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 195000,
  248. 168400, 22, 22, 8, 68, 39, 69 }, /* 195 Mb */
  249. [40] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS, 195000,
  250. 168400, 23, 23, 8, 70, 41, 71 }, /* 195 Mb */
  251. [41] = { RC_HT_T_20, WLAN_RC_PHY_HT_20_TS_HGI, 216700,
  252. 185000, 23, 23, 8, 70, 41, 71 }, /* 216.7 Mb */
  253. [42] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 13500,
  254. 13200, 0, 0, 8, 42, 42, 42 }, /* 13.5 Mb */
  255. [43] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 27500,
  256. 25900, 1, 1, 8, 43, 43, 43 }, /* 27.0 Mb */
  257. [44] = { RC_HT_SDT_40, WLAN_RC_PHY_HT_40_SS, 40500,
  258. 38600, 2, 2, 8, 44, 44, 44 }, /* 40.5 Mb */
  259. [45] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 54000,
  260. 49800, 3, 3, 8, 45, 45, 45 }, /* 54 Mb */
  261. [46] = { RC_HT_SD_40, WLAN_RC_PHY_HT_40_SS, 81500,
  262. 72200, 4, 4, 8, 46, 46, 46 }, /* 81 Mb */
  263. [47] = { RC_HT_S_40 , WLAN_RC_PHY_HT_40_SS, 108000,
  264. 92900, 5, 5, 8, 47, 47, 47 }, /* 108 Mb */
  265. [48] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 121500,
  266. 102700, 6, 6, 8, 48, 48, 48 }, /* 121.5 Mb */
  267. [49] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS, 135000,
  268. 112000, 7, 7, 8, 49, 50, 50 }, /* 135 Mb */
  269. [50] = { RC_HT_S_40, WLAN_RC_PHY_HT_40_SS_HGI, 150000,
  270. 122000, 7, 7, 8, 49, 50, 50 }, /* 150 Mb */
  271. [51] = { RC_INVALID, WLAN_RC_PHY_HT_40_DS, 27000,
  272. 25800, 8, 8, 8, 51, 51, 51 }, /* 27 Mb */
  273. [52] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 54000,
  274. 49800, 9, 9, 8, 52, 52, 52 }, /* 54 Mb */
  275. [53] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_DS, 81000,
  276. 71900, 10, 10, 8, 53, 53, 53 }, /* 81 Mb */
  277. [54] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 108000,
  278. 92500, 11, 11, 8, 54, 54, 54 }, /* 108 Mb */
  279. [55] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 162000,
  280. 130300, 12, 12, 8, 55, 55, 55 }, /* 162 Mb */
  281. [56] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 216000,
  282. 162800, 13, 13, 8, 56, 56, 56 }, /* 216 Mb */
  283. [57] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 243000,
  284. 178200, 14, 14, 8, 57, 57, 57 }, /* 243 Mb */
  285. [58] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS, 270000,
  286. 192100, 15, 15, 8, 58, 59, 59 }, /* 270 Mb */
  287. [59] = { RC_HT_DT_40, WLAN_RC_PHY_HT_40_DS_HGI, 300000,
  288. 207000, 15, 15, 8, 58, 59, 59 }, /* 300 Mb */
  289. [60] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 40500,
  290. 36100, 16, 16, 8, 60, 60, 60 }, /* 40.5 Mb */
  291. [61] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 81000,
  292. 72900, 17, 17, 8, 61, 61, 61 }, /* 81 Mb */
  293. [62] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 121500,
  294. 108300, 18, 18, 8, 62, 62, 62 }, /* 121.5 Mb */
  295. [63] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 162000,
  296. 142000, 19, 19, 8, 63, 63, 63 }, /* 162 Mb */
  297. [64] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS, 243000,
  298. 205100, 20, 20, 8, 64, 65, 65 }, /* 243 Mb */
  299. [65] = { RC_INVALID, WLAN_RC_PHY_HT_40_TS_HGI, 270000,
  300. 224700, 20, 20, 8, 64, 65, 65 }, /* 270 Mb */
  301. [66] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 324000,
  302. 263100, 21, 21, 8, 66, 67, 67 }, /* 324 Mb */
  303. [67] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 360000,
  304. 288000, 21, 21, 8, 66, 67, 67 }, /* 360 Mb */
  305. [68] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 364500,
  306. 290700, 22, 22, 8, 68, 69, 69 }, /* 364.5 Mb */
  307. [69] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 405000,
  308. 317200, 22, 22, 8, 68, 69, 69 }, /* 405 Mb */
  309. [70] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS, 405000,
  310. 317200, 23, 23, 8, 70, 71, 71 }, /* 405 Mb */
  311. [71] = { RC_HT_T_40, WLAN_RC_PHY_HT_40_TS_HGI, 450000,
  312. 346400, 23, 23, 8, 70, 71, 71 }, /* 450 Mb */
  313. },
  314. 50, /* probe interval */
  315. WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
  316. };
  317. static const struct ath_rate_table ar5416_11a_ratetable = {
  318. 8,
  319. 0,
  320. {
  321. { RC_L_SDT, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
  322. 5400, 0, 12, 0},
  323. { RC_L_SDT, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
  324. 7800, 1, 18, 0},
  325. { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
  326. 10000, 2, 24, 2},
  327. { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
  328. 13900, 3, 36, 2},
  329. { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
  330. 17300, 4, 48, 4},
  331. { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
  332. 23000, 5, 72, 4},
  333. { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
  334. 27400, 6, 96, 4},
  335. { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
  336. 29300, 7, 108, 4},
  337. },
  338. 50, /* probe interval */
  339. 0, /* Phy rates allowed initially */
  340. };
  341. static const struct ath_rate_table ar5416_11g_ratetable = {
  342. 12,
  343. 0,
  344. {
  345. { RC_L_SDT, WLAN_RC_PHY_CCK, 1000, /* 1 Mb */
  346. 900, 0, 2, 0},
  347. { RC_L_SDT, WLAN_RC_PHY_CCK, 2000, /* 2 Mb */
  348. 1900, 1, 4, 1},
  349. { RC_L_SDT, WLAN_RC_PHY_CCK, 5500, /* 5.5 Mb */
  350. 4900, 2, 11, 2},
  351. { RC_L_SDT, WLAN_RC_PHY_CCK, 11000, /* 11 Mb */
  352. 8100, 3, 22, 3},
  353. { RC_INVALID, WLAN_RC_PHY_OFDM, 6000, /* 6 Mb */
  354. 5400, 4, 12, 4},
  355. { RC_INVALID, WLAN_RC_PHY_OFDM, 9000, /* 9 Mb */
  356. 7800, 5, 18, 4},
  357. { RC_L_SDT, WLAN_RC_PHY_OFDM, 12000, /* 12 Mb */
  358. 10000, 6, 24, 6},
  359. { RC_L_SDT, WLAN_RC_PHY_OFDM, 18000, /* 18 Mb */
  360. 13900, 7, 36, 6},
  361. { RC_L_SDT, WLAN_RC_PHY_OFDM, 24000, /* 24 Mb */
  362. 17300, 8, 48, 8},
  363. { RC_L_SDT, WLAN_RC_PHY_OFDM, 36000, /* 36 Mb */
  364. 23000, 9, 72, 8},
  365. { RC_L_SDT, WLAN_RC_PHY_OFDM, 48000, /* 48 Mb */
  366. 27400, 10, 96, 8},
  367. { RC_L_SDT, WLAN_RC_PHY_OFDM, 54000, /* 54 Mb */
  368. 29300, 11, 108, 8},
  369. },
  370. 50, /* probe interval */
  371. 0, /* Phy rates allowed initially */
  372. };
  373. static int ath_rc_get_rateindex(const struct ath_rate_table *rate_table,
  374. struct ieee80211_tx_rate *rate);
  375. static inline int8_t median(int8_t a, int8_t b, int8_t c)
  376. {
  377. if (a >= b) {
  378. if (b >= c)
  379. return b;
  380. else if (a > c)
  381. return c;
  382. else
  383. return a;
  384. } else {
  385. if (a >= c)
  386. return a;
  387. else if (b >= c)
  388. return c;
  389. else
  390. return b;
  391. }
  392. }
  393. static void ath_rc_sort_validrates(const struct ath_rate_table *rate_table,
  394. struct ath_rate_priv *ath_rc_priv)
  395. {
  396. u8 i, j, idx, idx_next;
  397. for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
  398. for (j = 0; j <= i-1; j++) {
  399. idx = ath_rc_priv->valid_rate_index[j];
  400. idx_next = ath_rc_priv->valid_rate_index[j+1];
  401. if (rate_table->info[idx].ratekbps >
  402. rate_table->info[idx_next].ratekbps) {
  403. ath_rc_priv->valid_rate_index[j] = idx_next;
  404. ath_rc_priv->valid_rate_index[j+1] = idx;
  405. }
  406. }
  407. }
  408. }
  409. static void ath_rc_init_valid_txmask(struct ath_rate_priv *ath_rc_priv)
  410. {
  411. u8 i;
  412. for (i = 0; i < ath_rc_priv->rate_table_size; i++)
  413. ath_rc_priv->valid_rate_index[i] = 0;
  414. }
  415. static inline void ath_rc_set_valid_txmask(struct ath_rate_priv *ath_rc_priv,
  416. u8 index, int valid_tx_rate)
  417. {
  418. BUG_ON(index > ath_rc_priv->rate_table_size);
  419. ath_rc_priv->valid_rate_index[index] = !!valid_tx_rate;
  420. }
  421. static inline
  422. int ath_rc_get_nextvalid_txrate(const struct ath_rate_table *rate_table,
  423. struct ath_rate_priv *ath_rc_priv,
  424. u8 cur_valid_txrate,
  425. u8 *next_idx)
  426. {
  427. u8 i;
  428. for (i = 0; i < ath_rc_priv->max_valid_rate - 1; i++) {
  429. if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
  430. *next_idx = ath_rc_priv->valid_rate_index[i+1];
  431. return 1;
  432. }
  433. }
  434. /* No more valid rates */
  435. *next_idx = 0;
  436. return 0;
  437. }
  438. /* Return true only for single stream */
  439. static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
  440. {
  441. if (WLAN_RC_PHY_HT(phy) && !(capflag & WLAN_RC_HT_FLAG))
  442. return 0;
  443. if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
  444. return 0;
  445. if (WLAN_RC_PHY_TS(phy) && !(capflag & WLAN_RC_TS_FLAG))
  446. return 0;
  447. if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
  448. return 0;
  449. if (!ignore_cw && WLAN_RC_PHY_HT(phy))
  450. if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
  451. return 0;
  452. return 1;
  453. }
  454. static inline int
  455. ath_rc_get_lower_rix(const struct ath_rate_table *rate_table,
  456. struct ath_rate_priv *ath_rc_priv,
  457. u8 cur_valid_txrate, u8 *next_idx)
  458. {
  459. int8_t i;
  460. for (i = 1; i < ath_rc_priv->max_valid_rate ; i++) {
  461. if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
  462. *next_idx = ath_rc_priv->valid_rate_index[i-1];
  463. return 1;
  464. }
  465. }
  466. return 0;
  467. }
  468. static u8 ath_rc_init_validrates(struct ath_rate_priv *ath_rc_priv,
  469. const struct ath_rate_table *rate_table,
  470. u32 capflag)
  471. {
  472. u8 i, hi = 0;
  473. for (i = 0; i < rate_table->rate_cnt; i++) {
  474. if (rate_table->info[i].rate_flags & RC_LEGACY) {
  475. u32 phy = rate_table->info[i].phy;
  476. u8 valid_rate_count = 0;
  477. if (!ath_rc_valid_phyrate(phy, capflag, 0))
  478. continue;
  479. valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
  480. ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = i;
  481. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  482. ath_rc_set_valid_txmask(ath_rc_priv, i, 1);
  483. hi = i;
  484. }
  485. }
  486. return hi;
  487. }
  488. static u8 ath_rc_setvalid_rates(struct ath_rate_priv *ath_rc_priv,
  489. const struct ath_rate_table *rate_table,
  490. struct ath_rateset *rateset,
  491. u32 capflag)
  492. {
  493. u8 i, j, hi = 0;
  494. /* Use intersection of working rates and valid rates */
  495. for (i = 0; i < rateset->rs_nrates; i++) {
  496. for (j = 0; j < rate_table->rate_cnt; j++) {
  497. u32 phy = rate_table->info[j].phy;
  498. u16 rate_flags = rate_table->info[i].rate_flags;
  499. u8 rate = rateset->rs_rates[i];
  500. u8 dot11rate = rate_table->info[j].dot11rate;
  501. /* We allow a rate only if its valid and the
  502. * capflag matches one of the validity
  503. * (VALID/VALID_20/VALID_40) flags */
  504. if ((rate == dot11rate) &&
  505. (rate_flags & WLAN_RC_CAP_MODE(capflag)) ==
  506. WLAN_RC_CAP_MODE(capflag) &&
  507. (rate_flags & WLAN_RC_CAP_STREAM(capflag)) &&
  508. !WLAN_RC_PHY_HT(phy)) {
  509. u8 valid_rate_count = 0;
  510. if (!ath_rc_valid_phyrate(phy, capflag, 0))
  511. continue;
  512. valid_rate_count =
  513. ath_rc_priv->valid_phy_ratecnt[phy];
  514. ath_rc_priv->valid_phy_rateidx[phy]
  515. [valid_rate_count] = j;
  516. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  517. ath_rc_set_valid_txmask(ath_rc_priv, j, 1);
  518. hi = A_MAX(hi, j);
  519. }
  520. }
  521. }
  522. return hi;
  523. }
  524. static u8 ath_rc_setvalid_htrates(struct ath_rate_priv *ath_rc_priv,
  525. const struct ath_rate_table *rate_table,
  526. u8 *mcs_set, u32 capflag)
  527. {
  528. struct ath_rateset *rateset = (struct ath_rateset *)mcs_set;
  529. u8 i, j, hi = 0;
  530. /* Use intersection of working rates and valid rates */
  531. for (i = 0; i < rateset->rs_nrates; i++) {
  532. for (j = 0; j < rate_table->rate_cnt; j++) {
  533. u32 phy = rate_table->info[j].phy;
  534. u16 rate_flags = rate_table->info[j].rate_flags;
  535. u8 rate = rateset->rs_rates[i];
  536. u8 dot11rate = rate_table->info[j].dot11rate;
  537. if ((rate != dot11rate) || !WLAN_RC_PHY_HT(phy) ||
  538. !(rate_flags & WLAN_RC_CAP_STREAM(capflag)) ||
  539. !WLAN_RC_PHY_HT_VALID(rate_flags, capflag))
  540. continue;
  541. if (!ath_rc_valid_phyrate(phy, capflag, 0))
  542. continue;
  543. ath_rc_priv->valid_phy_rateidx[phy]
  544. [ath_rc_priv->valid_phy_ratecnt[phy]] = j;
  545. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  546. ath_rc_set_valid_txmask(ath_rc_priv, j, 1);
  547. hi = A_MAX(hi, j);
  548. }
  549. }
  550. return hi;
  551. }
  552. /* Finds the highest rate index we can use */
  553. static u8 ath_rc_get_highest_rix(struct ath_softc *sc,
  554. struct ath_rate_priv *ath_rc_priv,
  555. const struct ath_rate_table *rate_table,
  556. int *is_probing)
  557. {
  558. u32 best_thruput, this_thruput, now_msec;
  559. u8 rate, next_rate, best_rate, maxindex, minindex;
  560. int8_t index = 0;
  561. now_msec = jiffies_to_msecs(jiffies);
  562. *is_probing = 0;
  563. best_thruput = 0;
  564. maxindex = ath_rc_priv->max_valid_rate-1;
  565. minindex = 0;
  566. best_rate = minindex;
  567. /*
  568. * Try the higher rate first. It will reduce memory moving time
  569. * if we have very good channel characteristics.
  570. */
  571. for (index = maxindex; index >= minindex ; index--) {
  572. u8 per_thres;
  573. rate = ath_rc_priv->valid_rate_index[index];
  574. if (rate > ath_rc_priv->rate_max_phy)
  575. continue;
  576. /*
  577. * For TCP the average collision rate is around 11%,
  578. * so we ignore PERs less than this. This is to
  579. * prevent the rate we are currently using (whose
  580. * PER might be in the 10-15 range because of TCP
  581. * collisions) looking worse than the next lower
  582. * rate whose PER has decayed close to 0. If we
  583. * used to next lower rate, its PER would grow to
  584. * 10-15 and we would be worse off then staying
  585. * at the current rate.
  586. */
  587. per_thres = ath_rc_priv->per[rate];
  588. if (per_thres < 12)
  589. per_thres = 12;
  590. this_thruput = rate_table->info[rate].user_ratekbps *
  591. (100 - per_thres);
  592. if (best_thruput <= this_thruput) {
  593. best_thruput = this_thruput;
  594. best_rate = rate;
  595. }
  596. }
  597. rate = best_rate;
  598. /*
  599. * Must check the actual rate (ratekbps) to account for
  600. * non-monoticity of 11g's rate table
  601. */
  602. if (rate >= ath_rc_priv->rate_max_phy) {
  603. rate = ath_rc_priv->rate_max_phy;
  604. /* Probe the next allowed phy state */
  605. if (ath_rc_get_nextvalid_txrate(rate_table,
  606. ath_rc_priv, rate, &next_rate) &&
  607. (now_msec - ath_rc_priv->probe_time >
  608. rate_table->probe_interval) &&
  609. (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
  610. rate = next_rate;
  611. ath_rc_priv->probe_rate = rate;
  612. ath_rc_priv->probe_time = now_msec;
  613. ath_rc_priv->hw_maxretry_pktcnt = 0;
  614. *is_probing = 1;
  615. }
  616. }
  617. if (rate > (ath_rc_priv->rate_table_size - 1))
  618. rate = ath_rc_priv->rate_table_size - 1;
  619. if (RC_TS_ONLY(rate_table->info[rate].rate_flags) &&
  620. (ath_rc_priv->ht_cap & WLAN_RC_TS_FLAG))
  621. return rate;
  622. if (RC_DS_OR_LATER(rate_table->info[rate].rate_flags) &&
  623. (ath_rc_priv->ht_cap & (WLAN_RC_DS_FLAG | WLAN_RC_TS_FLAG)))
  624. return rate;
  625. if (RC_SS_OR_LEGACY(rate_table->info[rate].rate_flags))
  626. return rate;
  627. /* This should not happen */
  628. WARN_ON(1);
  629. rate = ath_rc_priv->valid_rate_index[0];
  630. return rate;
  631. }
  632. static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table,
  633. struct ieee80211_tx_rate *rate,
  634. struct ieee80211_tx_rate_control *txrc,
  635. u8 tries, u8 rix, int rtsctsenable)
  636. {
  637. rate->count = tries;
  638. rate->idx = rate_table->info[rix].ratecode;
  639. if (txrc->short_preamble)
  640. rate->flags |= IEEE80211_TX_RC_USE_SHORT_PREAMBLE;
  641. if (txrc->rts || rtsctsenable)
  642. rate->flags |= IEEE80211_TX_RC_USE_RTS_CTS;
  643. if (WLAN_RC_PHY_HT(rate_table->info[rix].phy)) {
  644. rate->flags |= IEEE80211_TX_RC_MCS;
  645. if (WLAN_RC_PHY_40(rate_table->info[rix].phy))
  646. rate->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  647. if (WLAN_RC_PHY_SGI(rate_table->info[rix].phy))
  648. rate->flags |= IEEE80211_TX_RC_SHORT_GI;
  649. }
  650. }
  651. static void ath_rc_rate_set_rtscts(struct ath_softc *sc,
  652. const struct ath_rate_table *rate_table,
  653. struct ieee80211_tx_info *tx_info)
  654. {
  655. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  656. int i = 0, rix = 0, cix, enable_g_protection = 0;
  657. /* get the cix for the lowest valid rix */
  658. for (i = 3; i >= 0; i--) {
  659. if (rates[i].count && (rates[i].idx >= 0)) {
  660. rix = ath_rc_get_rateindex(rate_table, &rates[i]);
  661. break;
  662. }
  663. }
  664. cix = rate_table->info[rix].ctrl_rate;
  665. /* All protection frames are transmited at 2Mb/s for 802.11g,
  666. * otherwise we transmit them at 1Mb/s */
  667. if (sc->hw->conf.channel->band == IEEE80211_BAND_2GHZ &&
  668. !conf_is_ht(&sc->hw->conf))
  669. enable_g_protection = 1;
  670. /*
  671. * If 802.11g protection is enabled, determine whether to use RTS/CTS or
  672. * just CTS. Note that this is only done for OFDM/HT unicast frames.
  673. */
  674. if ((sc->sc_flags & SC_OP_PROTECT_ENABLE) &&
  675. (rate_table->info[rix].phy == WLAN_RC_PHY_OFDM ||
  676. WLAN_RC_PHY_HT(rate_table->info[rix].phy))) {
  677. rates[0].flags |= IEEE80211_TX_RC_USE_CTS_PROTECT;
  678. cix = rate_table->info[enable_g_protection].ctrl_rate;
  679. }
  680. tx_info->control.rts_cts_rate_idx = cix;
  681. }
  682. static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  683. struct ieee80211_tx_rate_control *txrc)
  684. {
  685. struct ath_softc *sc = priv;
  686. struct ath_rate_priv *ath_rc_priv = priv_sta;
  687. const struct ath_rate_table *rate_table;
  688. struct sk_buff *skb = txrc->skb;
  689. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  690. struct ieee80211_tx_rate *rates = tx_info->control.rates;
  691. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  692. __le16 fc = hdr->frame_control;
  693. u8 try_per_rate, i = 0, rix;
  694. int is_probe = 0;
  695. if (rate_control_send_low(sta, priv_sta, txrc))
  696. return;
  697. /*
  698. * For Multi Rate Retry we use a different number of
  699. * retry attempt counts. This ends up looking like this:
  700. *
  701. * MRR[0] = 4
  702. * MRR[1] = 4
  703. * MRR[2] = 4
  704. * MRR[3] = 8
  705. *
  706. */
  707. try_per_rate = 4;
  708. rate_table = ath_rc_priv->rate_table;
  709. rix = ath_rc_get_highest_rix(sc, ath_rc_priv, rate_table, &is_probe);
  710. /*
  711. * If we're in HT mode and both us and our peer supports LDPC.
  712. * We don't need to check our own device's capabilities as our own
  713. * ht capabilities would have already been intersected with our peer's.
  714. */
  715. if (conf_is_ht(&sc->hw->conf) &&
  716. (sta->ht_cap.cap & IEEE80211_HT_CAP_LDPC_CODING))
  717. tx_info->flags |= IEEE80211_TX_CTL_LDPC;
  718. if (conf_is_ht(&sc->hw->conf) &&
  719. (sta->ht_cap.cap & IEEE80211_HT_CAP_TX_STBC))
  720. tx_info->flags |= (1 << IEEE80211_TX_CTL_STBC_SHIFT);
  721. if (is_probe) {
  722. /* set one try for probe rates. For the
  723. * probes don't enable rts */
  724. ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
  725. 1, rix, 0);
  726. /* Get the next tried/allowed rate. No RTS for the next series
  727. * after the probe rate
  728. */
  729. ath_rc_get_lower_rix(rate_table, ath_rc_priv, rix, &rix);
  730. ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
  731. try_per_rate, rix, 0);
  732. tx_info->flags |= IEEE80211_TX_CTL_RATE_CTRL_PROBE;
  733. } else {
  734. /* Set the choosen rate. No RTS for first series entry. */
  735. ath_rc_rate_set_series(rate_table, &rates[i++], txrc,
  736. try_per_rate, rix, 0);
  737. }
  738. /* Fill in the other rates for multirate retry */
  739. for ( ; i < 4; i++) {
  740. /* Use twice the number of tries for the last MRR segment. */
  741. if (i + 1 == 4)
  742. try_per_rate = 8;
  743. ath_rc_get_lower_rix(rate_table, ath_rc_priv, rix, &rix);
  744. /* All other rates in the series have RTS enabled */
  745. ath_rc_rate_set_series(rate_table, &rates[i], txrc,
  746. try_per_rate, rix, 1);
  747. }
  748. /*
  749. * NB:Change rate series to enable aggregation when operating
  750. * at lower MCS rates. When first rate in series is MCS2
  751. * in HT40 @ 2.4GHz, series should look like:
  752. *
  753. * {MCS2, MCS1, MCS0, MCS0}.
  754. *
  755. * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
  756. * look like:
  757. *
  758. * {MCS3, MCS2, MCS1, MCS1}
  759. *
  760. * So, set fourth rate in series to be same as third one for
  761. * above conditions.
  762. */
  763. if ((sc->hw->conf.channel->band == IEEE80211_BAND_2GHZ) &&
  764. (conf_is_ht(&sc->hw->conf))) {
  765. u8 dot11rate = rate_table->info[rix].dot11rate;
  766. u8 phy = rate_table->info[rix].phy;
  767. if (i == 4 &&
  768. ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
  769. (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
  770. rates[3].idx = rates[2].idx;
  771. rates[3].flags = rates[2].flags;
  772. }
  773. }
  774. /*
  775. * Force hardware to use computed duration for next
  776. * fragment by disabling multi-rate retry, which
  777. * updates duration based on the multi-rate duration table.
  778. *
  779. * FIXME: Fix duration
  780. */
  781. if (ieee80211_has_morefrags(fc) ||
  782. (le16_to_cpu(hdr->seq_ctrl) & IEEE80211_SCTL_FRAG)) {
  783. rates[1].count = rates[2].count = rates[3].count = 0;
  784. rates[1].idx = rates[2].idx = rates[3].idx = 0;
  785. rates[0].count = ATH_TXMAXTRY;
  786. }
  787. /* Setup RTS/CTS */
  788. ath_rc_rate_set_rtscts(sc, rate_table, tx_info);
  789. }
  790. static bool ath_rc_update_per(struct ath_softc *sc,
  791. const struct ath_rate_table *rate_table,
  792. struct ath_rate_priv *ath_rc_priv,
  793. struct ieee80211_tx_info *tx_info,
  794. int tx_rate, int xretries, int retries,
  795. u32 now_msec)
  796. {
  797. bool state_change = false;
  798. int count, n_bad_frames;
  799. u8 last_per;
  800. static u32 nretry_to_per_lookup[10] = {
  801. 100 * 0 / 1,
  802. 100 * 1 / 4,
  803. 100 * 1 / 2,
  804. 100 * 3 / 4,
  805. 100 * 4 / 5,
  806. 100 * 5 / 6,
  807. 100 * 6 / 7,
  808. 100 * 7 / 8,
  809. 100 * 8 / 9,
  810. 100 * 9 / 10
  811. };
  812. last_per = ath_rc_priv->per[tx_rate];
  813. n_bad_frames = tx_info->status.ampdu_len - tx_info->status.ampdu_ack_len;
  814. if (xretries) {
  815. if (xretries == 1) {
  816. ath_rc_priv->per[tx_rate] += 30;
  817. if (ath_rc_priv->per[tx_rate] > 100)
  818. ath_rc_priv->per[tx_rate] = 100;
  819. } else {
  820. /* xretries == 2 */
  821. count = ARRAY_SIZE(nretry_to_per_lookup);
  822. if (retries >= count)
  823. retries = count - 1;
  824. /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
  825. ath_rc_priv->per[tx_rate] =
  826. (u8)(last_per - (last_per >> 3) + (100 >> 3));
  827. }
  828. /* xretries == 1 or 2 */
  829. if (ath_rc_priv->probe_rate == tx_rate)
  830. ath_rc_priv->probe_rate = 0;
  831. } else { /* xretries == 0 */
  832. count = ARRAY_SIZE(nretry_to_per_lookup);
  833. if (retries >= count)
  834. retries = count - 1;
  835. if (n_bad_frames) {
  836. /* new_PER = 7/8*old_PER + 1/8*(currentPER)
  837. * Assuming that n_frames is not 0. The current PER
  838. * from the retries is 100 * retries / (retries+1),
  839. * since the first retries attempts failed, and the
  840. * next one worked. For the one that worked,
  841. * n_bad_frames subframes out of n_frames wored,
  842. * so the PER for that part is
  843. * 100 * n_bad_frames / n_frames, and it contributes
  844. * 100 * n_bad_frames / (n_frames * (retries+1)) to
  845. * the above PER. The expression below is a
  846. * simplified version of the sum of these two terms.
  847. */
  848. if (tx_info->status.ampdu_len > 0) {
  849. int n_frames, n_bad_tries;
  850. u8 cur_per, new_per;
  851. n_bad_tries = retries * tx_info->status.ampdu_len +
  852. n_bad_frames;
  853. n_frames = tx_info->status.ampdu_len * (retries + 1);
  854. cur_per = (100 * n_bad_tries / n_frames) >> 3;
  855. new_per = (u8)(last_per - (last_per >> 3) + cur_per);
  856. ath_rc_priv->per[tx_rate] = new_per;
  857. }
  858. } else {
  859. ath_rc_priv->per[tx_rate] =
  860. (u8)(last_per - (last_per >> 3) +
  861. (nretry_to_per_lookup[retries] >> 3));
  862. }
  863. /*
  864. * If we got at most one retry then increase the max rate if
  865. * this was a probe. Otherwise, ignore the probe.
  866. */
  867. if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
  868. if (retries > 0 || 2 * n_bad_frames > tx_info->status.ampdu_len) {
  869. /*
  870. * Since we probed with just a single attempt,
  871. * any retries means the probe failed. Also,
  872. * if the attempt worked, but more than half
  873. * the subframes were bad then also consider
  874. * the probe a failure.
  875. */
  876. ath_rc_priv->probe_rate = 0;
  877. } else {
  878. u8 probe_rate = 0;
  879. ath_rc_priv->rate_max_phy =
  880. ath_rc_priv->probe_rate;
  881. probe_rate = ath_rc_priv->probe_rate;
  882. if (ath_rc_priv->per[probe_rate] > 30)
  883. ath_rc_priv->per[probe_rate] = 20;
  884. ath_rc_priv->probe_rate = 0;
  885. /*
  886. * Since this probe succeeded, we allow the next
  887. * probe twice as soon. This allows the maxRate
  888. * to move up faster if the probes are
  889. * successful.
  890. */
  891. ath_rc_priv->probe_time =
  892. now_msec - rate_table->probe_interval / 2;
  893. }
  894. }
  895. if (retries > 0) {
  896. /*
  897. * Don't update anything. We don't know if
  898. * this was because of collisions or poor signal.
  899. */
  900. ath_rc_priv->hw_maxretry_pktcnt = 0;
  901. } else {
  902. /*
  903. * It worked with no retries. First ignore bogus (small)
  904. * rssi_ack values.
  905. */
  906. if (tx_rate == ath_rc_priv->rate_max_phy &&
  907. ath_rc_priv->hw_maxretry_pktcnt < 255) {
  908. ath_rc_priv->hw_maxretry_pktcnt++;
  909. }
  910. }
  911. }
  912. return state_change;
  913. }
  914. static void ath_debug_stat_retries(struct ath_rate_priv *rc, int rix,
  915. int xretries, int retries, u8 per)
  916. {
  917. struct ath_rc_stats *stats = &rc->rcstats[rix];
  918. stats->xretries += xretries;
  919. stats->retries += retries;
  920. stats->per = per;
  921. }
  922. /* Update PER, RSSI and whatever else that the code thinks it is doing.
  923. If you can make sense of all this, you really need to go out more. */
  924. static void ath_rc_update_ht(struct ath_softc *sc,
  925. struct ath_rate_priv *ath_rc_priv,
  926. struct ieee80211_tx_info *tx_info,
  927. int tx_rate, int xretries, int retries)
  928. {
  929. u32 now_msec = jiffies_to_msecs(jiffies);
  930. int rate;
  931. u8 last_per;
  932. bool state_change = false;
  933. const struct ath_rate_table *rate_table = ath_rc_priv->rate_table;
  934. int size = ath_rc_priv->rate_table_size;
  935. if ((tx_rate < 0) || (tx_rate > rate_table->rate_cnt))
  936. return;
  937. last_per = ath_rc_priv->per[tx_rate];
  938. /* Update PER first */
  939. state_change = ath_rc_update_per(sc, rate_table, ath_rc_priv,
  940. tx_info, tx_rate, xretries,
  941. retries, now_msec);
  942. /*
  943. * If this rate looks bad (high PER) then stop using it for
  944. * a while (except if we are probing).
  945. */
  946. if (ath_rc_priv->per[tx_rate] >= 55 && tx_rate > 0 &&
  947. rate_table->info[tx_rate].ratekbps <=
  948. rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
  949. ath_rc_get_lower_rix(rate_table, ath_rc_priv,
  950. (u8)tx_rate, &ath_rc_priv->rate_max_phy);
  951. /* Don't probe for a little while. */
  952. ath_rc_priv->probe_time = now_msec;
  953. }
  954. /* Make sure the rates below this have lower PER */
  955. /* Monotonicity is kept only for rates below the current rate. */
  956. if (ath_rc_priv->per[tx_rate] < last_per) {
  957. for (rate = tx_rate - 1; rate >= 0; rate--) {
  958. if (ath_rc_priv->per[rate] >
  959. ath_rc_priv->per[rate+1]) {
  960. ath_rc_priv->per[rate] =
  961. ath_rc_priv->per[rate+1];
  962. }
  963. }
  964. }
  965. /* Maintain monotonicity for rates above the current rate */
  966. for (rate = tx_rate; rate < size - 1; rate++) {
  967. if (ath_rc_priv->per[rate+1] <
  968. ath_rc_priv->per[rate])
  969. ath_rc_priv->per[rate+1] =
  970. ath_rc_priv->per[rate];
  971. }
  972. /* Every so often, we reduce the thresholds
  973. * and PER (different for CCK and OFDM). */
  974. if (now_msec - ath_rc_priv->per_down_time >=
  975. rate_table->probe_interval) {
  976. for (rate = 0; rate < size; rate++) {
  977. ath_rc_priv->per[rate] =
  978. 7 * ath_rc_priv->per[rate] / 8;
  979. }
  980. ath_rc_priv->per_down_time = now_msec;
  981. }
  982. ath_debug_stat_retries(ath_rc_priv, tx_rate, xretries, retries,
  983. ath_rc_priv->per[tx_rate]);
  984. }
  985. static int ath_rc_get_rateindex(const struct ath_rate_table *rate_table,
  986. struct ieee80211_tx_rate *rate)
  987. {
  988. int rix = 0, i = 0;
  989. int mcs_rix_off[] = { 7, 15, 20, 21, 22, 23 };
  990. if (!(rate->flags & IEEE80211_TX_RC_MCS))
  991. return rate->idx;
  992. while (rate->idx > mcs_rix_off[i] &&
  993. i < sizeof(mcs_rix_off)/sizeof(int)) {
  994. rix++; i++;
  995. }
  996. rix += rate->idx + rate_table->mcs_start;
  997. if ((rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
  998. (rate->flags & IEEE80211_TX_RC_SHORT_GI))
  999. rix = rate_table->info[rix].ht_index;
  1000. else if (rate->flags & IEEE80211_TX_RC_SHORT_GI)
  1001. rix = rate_table->info[rix].sgi_index;
  1002. else if (rate->flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  1003. rix = rate_table->info[rix].cw40index;
  1004. return rix;
  1005. }
  1006. static void ath_rc_tx_status(struct ath_softc *sc,
  1007. struct ath_rate_priv *ath_rc_priv,
  1008. struct ieee80211_tx_info *tx_info,
  1009. int final_ts_idx, int xretries, int long_retry)
  1010. {
  1011. const struct ath_rate_table *rate_table;
  1012. struct ieee80211_tx_rate *rates = tx_info->status.rates;
  1013. u8 flags;
  1014. u32 i = 0, rix;
  1015. rate_table = ath_rc_priv->rate_table;
  1016. /*
  1017. * If the first rate is not the final index, there
  1018. * are intermediate rate failures to be processed.
  1019. */
  1020. if (final_ts_idx != 0) {
  1021. /* Process intermediate rates that failed.*/
  1022. for (i = 0; i < final_ts_idx ; i++) {
  1023. if (rates[i].count != 0 && (rates[i].idx >= 0)) {
  1024. flags = rates[i].flags;
  1025. /* If HT40 and we have switched mode from
  1026. * 40 to 20 => don't update */
  1027. if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
  1028. !(ath_rc_priv->ht_cap & WLAN_RC_40_FLAG))
  1029. return;
  1030. rix = ath_rc_get_rateindex(rate_table, &rates[i]);
  1031. ath_rc_update_ht(sc, ath_rc_priv, tx_info,
  1032. rix, xretries ? 1 : 2,
  1033. rates[i].count);
  1034. }
  1035. }
  1036. } else {
  1037. /*
  1038. * Handle the special case of MIMO PS burst, where the second
  1039. * aggregate is sent out with only one rate and one try.
  1040. * Treating it as an excessive retry penalizes the rate
  1041. * inordinately.
  1042. */
  1043. if (rates[0].count == 1 && xretries == 1)
  1044. xretries = 2;
  1045. }
  1046. flags = rates[i].flags;
  1047. /* If HT40 and we have switched mode from 40 to 20 => don't update */
  1048. if ((flags & IEEE80211_TX_RC_40_MHZ_WIDTH) &&
  1049. !(ath_rc_priv->ht_cap & WLAN_RC_40_FLAG))
  1050. return;
  1051. rix = ath_rc_get_rateindex(rate_table, &rates[i]);
  1052. ath_rc_update_ht(sc, ath_rc_priv, tx_info, rix, xretries, long_retry);
  1053. }
  1054. static const
  1055. struct ath_rate_table *ath_choose_rate_table(struct ath_softc *sc,
  1056. enum ieee80211_band band,
  1057. bool is_ht)
  1058. {
  1059. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1060. switch(band) {
  1061. case IEEE80211_BAND_2GHZ:
  1062. if (is_ht)
  1063. return &ar5416_11ng_ratetable;
  1064. return &ar5416_11g_ratetable;
  1065. case IEEE80211_BAND_5GHZ:
  1066. if (is_ht)
  1067. return &ar5416_11na_ratetable;
  1068. return &ar5416_11a_ratetable;
  1069. default:
  1070. ath_print(common, ATH_DBG_CONFIG, "Invalid band\n");
  1071. return NULL;
  1072. }
  1073. }
  1074. static void ath_rc_init(struct ath_softc *sc,
  1075. struct ath_rate_priv *ath_rc_priv,
  1076. struct ieee80211_supported_band *sband,
  1077. struct ieee80211_sta *sta,
  1078. const struct ath_rate_table *rate_table)
  1079. {
  1080. struct ath_rateset *rateset = &ath_rc_priv->neg_rates;
  1081. struct ath_common *common = ath9k_hw_common(sc->sc_ah);
  1082. u8 *ht_mcs = (u8 *)&ath_rc_priv->neg_ht_rates;
  1083. u8 i, j, k, hi = 0, hthi = 0;
  1084. /* Initial rate table size. Will change depending
  1085. * on the working rate set */
  1086. ath_rc_priv->rate_table_size = RATE_TABLE_SIZE;
  1087. /* Initialize thresholds according to the global rate table */
  1088. for (i = 0 ; i < ath_rc_priv->rate_table_size; i++) {
  1089. ath_rc_priv->per[i] = 0;
  1090. }
  1091. /* Determine the valid rates */
  1092. ath_rc_init_valid_txmask(ath_rc_priv);
  1093. for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
  1094. for (j = 0; j < MAX_TX_RATE_PHY; j++)
  1095. ath_rc_priv->valid_phy_rateidx[i][j] = 0;
  1096. ath_rc_priv->valid_phy_ratecnt[i] = 0;
  1097. }
  1098. if (!rateset->rs_nrates) {
  1099. /* No working rate, just initialize valid rates */
  1100. hi = ath_rc_init_validrates(ath_rc_priv, rate_table,
  1101. ath_rc_priv->ht_cap);
  1102. } else {
  1103. /* Use intersection of working rates and valid rates */
  1104. hi = ath_rc_setvalid_rates(ath_rc_priv, rate_table,
  1105. rateset, ath_rc_priv->ht_cap);
  1106. if (ath_rc_priv->ht_cap & WLAN_RC_HT_FLAG) {
  1107. hthi = ath_rc_setvalid_htrates(ath_rc_priv,
  1108. rate_table,
  1109. ht_mcs,
  1110. ath_rc_priv->ht_cap);
  1111. }
  1112. hi = A_MAX(hi, hthi);
  1113. }
  1114. ath_rc_priv->rate_table_size = hi + 1;
  1115. ath_rc_priv->rate_max_phy = 0;
  1116. BUG_ON(ath_rc_priv->rate_table_size > RATE_TABLE_SIZE);
  1117. for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
  1118. for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
  1119. ath_rc_priv->valid_rate_index[k++] =
  1120. ath_rc_priv->valid_phy_rateidx[i][j];
  1121. }
  1122. if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, 1)
  1123. || !ath_rc_priv->valid_phy_ratecnt[i])
  1124. continue;
  1125. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
  1126. }
  1127. BUG_ON(ath_rc_priv->rate_table_size > RATE_TABLE_SIZE);
  1128. BUG_ON(k > RATE_TABLE_SIZE);
  1129. ath_rc_priv->max_valid_rate = k;
  1130. ath_rc_sort_validrates(rate_table, ath_rc_priv);
  1131. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
  1132. ath_rc_priv->rate_table = rate_table;
  1133. ath_print(common, ATH_DBG_CONFIG,
  1134. "RC Initialized with capabilities: 0x%x\n",
  1135. ath_rc_priv->ht_cap);
  1136. }
  1137. static u8 ath_rc_build_ht_caps(struct ath_softc *sc, struct ieee80211_sta *sta,
  1138. bool is_cw40, bool is_sgi)
  1139. {
  1140. u8 caps = 0;
  1141. if (sta->ht_cap.ht_supported) {
  1142. caps = WLAN_RC_HT_FLAG;
  1143. if (sta->ht_cap.mcs.rx_mask[1] && sta->ht_cap.mcs.rx_mask[2])
  1144. caps |= WLAN_RC_TS_FLAG | WLAN_RC_DS_FLAG;
  1145. else if (sta->ht_cap.mcs.rx_mask[1])
  1146. caps |= WLAN_RC_DS_FLAG;
  1147. if (is_cw40)
  1148. caps |= WLAN_RC_40_FLAG;
  1149. if (is_sgi)
  1150. caps |= WLAN_RC_SGI_FLAG;
  1151. }
  1152. return caps;
  1153. }
  1154. static bool ath_tx_aggr_check(struct ath_softc *sc, struct ath_node *an,
  1155. u8 tidno)
  1156. {
  1157. struct ath_atx_tid *txtid;
  1158. if (!(sc->sc_flags & SC_OP_TXAGGR))
  1159. return false;
  1160. txtid = ATH_AN_2_TID(an, tidno);
  1161. if (!(txtid->state & (AGGR_ADDBA_COMPLETE | AGGR_ADDBA_PROGRESS)))
  1162. return true;
  1163. return false;
  1164. }
  1165. /***********************************/
  1166. /* mac80211 Rate Control callbacks */
  1167. /***********************************/
  1168. static void ath_debug_stat_rc(struct ath_rate_priv *rc, int final_rate)
  1169. {
  1170. struct ath_rc_stats *stats;
  1171. stats = &rc->rcstats[final_rate];
  1172. stats->success++;
  1173. }
  1174. static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
  1175. struct ieee80211_sta *sta, void *priv_sta,
  1176. struct sk_buff *skb)
  1177. {
  1178. struct ath_softc *sc = priv;
  1179. struct ath_rate_priv *ath_rc_priv = priv_sta;
  1180. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1181. struct ieee80211_hdr *hdr;
  1182. int final_ts_idx = 0, tx_status = 0, is_underrun = 0;
  1183. int long_retry = 0;
  1184. __le16 fc;
  1185. int i;
  1186. hdr = (struct ieee80211_hdr *)skb->data;
  1187. fc = hdr->frame_control;
  1188. for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
  1189. struct ieee80211_tx_rate *rate = &tx_info->status.rates[i];
  1190. if (!rate->count)
  1191. break;
  1192. final_ts_idx = i;
  1193. long_retry = rate->count - 1;
  1194. }
  1195. if (!priv_sta || !ieee80211_is_data(fc))
  1196. return;
  1197. /* This packet was aggregated but doesn't carry status info */
  1198. if ((tx_info->flags & IEEE80211_TX_CTL_AMPDU) &&
  1199. !(tx_info->flags & IEEE80211_TX_STAT_AMPDU))
  1200. return;
  1201. if (tx_info->flags & IEEE80211_TX_STAT_TX_FILTERED)
  1202. return;
  1203. if (!(tx_info->flags & IEEE80211_TX_STAT_AMPDU)) {
  1204. tx_info->status.ampdu_ack_len =
  1205. (tx_info->flags & IEEE80211_TX_STAT_ACK ? 1 : 0);
  1206. tx_info->status.ampdu_len = 1;
  1207. }
  1208. /*
  1209. * If an underrun error is seen assume it as an excessive retry only
  1210. * if max frame trigger level has been reached (2 KB for singel stream,
  1211. * and 4 KB for dual stream). Adjust the long retry as if the frame was
  1212. * tried hw->max_rate_tries times to affect how ratectrl updates PER for
  1213. * the failed rate. In case of congestion on the bus penalizing these
  1214. * type of underruns should help hardware actually transmit new frames
  1215. * successfully by eventually preferring slower rates. This itself
  1216. * should also alleviate congestion on the bus.
  1217. */
  1218. if ((tx_info->pad[0] & ATH_TX_INFO_UNDERRUN) &&
  1219. (sc->sc_ah->tx_trig_level >= ath_rc_priv->tx_triglevel_max)) {
  1220. tx_status = 1;
  1221. is_underrun = 1;
  1222. }
  1223. if (tx_info->pad[0] & ATH_TX_INFO_XRETRY)
  1224. tx_status = 1;
  1225. ath_rc_tx_status(sc, ath_rc_priv, tx_info, final_ts_idx, tx_status,
  1226. (is_underrun) ? sc->hw->max_rate_tries : long_retry);
  1227. /* Check if aggregation has to be enabled for this tid */
  1228. if (conf_is_ht(&sc->hw->conf) &&
  1229. !(skb->protocol == cpu_to_be16(ETH_P_PAE))) {
  1230. if (ieee80211_is_data_qos(fc)) {
  1231. u8 *qc, tid;
  1232. struct ath_node *an;
  1233. qc = ieee80211_get_qos_ctl(hdr);
  1234. tid = qc[0] & 0xf;
  1235. an = (struct ath_node *)sta->drv_priv;
  1236. if(ath_tx_aggr_check(sc, an, tid))
  1237. ieee80211_start_tx_ba_session(sta, tid);
  1238. }
  1239. }
  1240. ath_debug_stat_rc(ath_rc_priv,
  1241. ath_rc_get_rateindex(ath_rc_priv->rate_table,
  1242. &tx_info->status.rates[final_ts_idx]));
  1243. }
  1244. static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1245. struct ieee80211_sta *sta, void *priv_sta)
  1246. {
  1247. struct ath_softc *sc = priv;
  1248. struct ath_rate_priv *ath_rc_priv = priv_sta;
  1249. const struct ath_rate_table *rate_table;
  1250. bool is_cw40, is_sgi = false;
  1251. int i, j = 0;
  1252. for (i = 0; i < sband->n_bitrates; i++) {
  1253. if (sta->supp_rates[sband->band] & BIT(i)) {
  1254. ath_rc_priv->neg_rates.rs_rates[j]
  1255. = (sband->bitrates[i].bitrate * 2) / 10;
  1256. j++;
  1257. }
  1258. }
  1259. ath_rc_priv->neg_rates.rs_nrates = j;
  1260. if (sta->ht_cap.ht_supported) {
  1261. for (i = 0, j = 0; i < 77; i++) {
  1262. if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
  1263. ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
  1264. if (j == ATH_RATE_MAX)
  1265. break;
  1266. }
  1267. ath_rc_priv->neg_ht_rates.rs_nrates = j;
  1268. }
  1269. is_cw40 = sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  1270. if (is_cw40)
  1271. is_sgi = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40;
  1272. else if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  1273. is_sgi = sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20;
  1274. /* Choose rate table first */
  1275. rate_table = ath_choose_rate_table(sc, sband->band,
  1276. sta->ht_cap.ht_supported);
  1277. ath_rc_priv->ht_cap = ath_rc_build_ht_caps(sc, sta, is_cw40, is_sgi);
  1278. ath_rc_init(sc, priv_sta, sband, sta, rate_table);
  1279. }
  1280. static void ath_rate_update(void *priv, struct ieee80211_supported_band *sband,
  1281. struct ieee80211_sta *sta, void *priv_sta,
  1282. u32 changed, enum nl80211_channel_type oper_chan_type)
  1283. {
  1284. struct ath_softc *sc = priv;
  1285. struct ath_rate_priv *ath_rc_priv = priv_sta;
  1286. const struct ath_rate_table *rate_table = NULL;
  1287. bool oper_cw40 = false, oper_sgi;
  1288. bool local_cw40 = (ath_rc_priv->ht_cap & WLAN_RC_40_FLAG) ?
  1289. true : false;
  1290. bool local_sgi = (ath_rc_priv->ht_cap & WLAN_RC_SGI_FLAG) ?
  1291. true : false;
  1292. /* FIXME: Handle AP mode later when we support CWM */
  1293. if (changed & IEEE80211_RC_HT_CHANGED) {
  1294. if (sc->sc_ah->opmode != NL80211_IFTYPE_STATION)
  1295. return;
  1296. if (oper_chan_type == NL80211_CHAN_HT40MINUS ||
  1297. oper_chan_type == NL80211_CHAN_HT40PLUS)
  1298. oper_cw40 = true;
  1299. if (oper_cw40)
  1300. oper_sgi = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_40) ?
  1301. true : false;
  1302. else if (sc->sc_ah->caps.hw_caps & ATH9K_HW_CAP_SGI_20)
  1303. oper_sgi = (sta->ht_cap.cap & IEEE80211_HT_CAP_SGI_20) ?
  1304. true : false;
  1305. else
  1306. oper_sgi = false;
  1307. if ((local_cw40 != oper_cw40) || (local_sgi != oper_sgi)) {
  1308. rate_table = ath_choose_rate_table(sc, sband->band,
  1309. sta->ht_cap.ht_supported);
  1310. ath_rc_priv->ht_cap = ath_rc_build_ht_caps(sc, sta,
  1311. oper_cw40, oper_sgi);
  1312. ath_rc_init(sc, priv_sta, sband, sta, rate_table);
  1313. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_CONFIG,
  1314. "Operating HT Bandwidth changed to: %d\n",
  1315. sc->hw->conf.channel_type);
  1316. }
  1317. }
  1318. }
  1319. #ifdef CONFIG_ATH9K_DEBUGFS
  1320. static int ath9k_debugfs_open(struct inode *inode, struct file *file)
  1321. {
  1322. file->private_data = inode->i_private;
  1323. return 0;
  1324. }
  1325. static ssize_t read_file_rcstat(struct file *file, char __user *user_buf,
  1326. size_t count, loff_t *ppos)
  1327. {
  1328. struct ath_rate_priv *rc = file->private_data;
  1329. char *buf;
  1330. unsigned int len = 0, max;
  1331. int i = 0;
  1332. ssize_t retval;
  1333. if (rc->rate_table == NULL)
  1334. return 0;
  1335. max = 80 + rc->rate_table->rate_cnt * 1024 + 1;
  1336. buf = kmalloc(max, GFP_KERNEL);
  1337. if (buf == NULL)
  1338. return -ENOMEM;
  1339. len += sprintf(buf, "%6s %6s %6s "
  1340. "%10s %10s %10s %10s\n",
  1341. "HT", "MCS", "Rate",
  1342. "Success", "Retries", "XRetries", "PER");
  1343. for (i = 0; i < rc->rate_table->rate_cnt; i++) {
  1344. u32 ratekbps = rc->rate_table->info[i].ratekbps;
  1345. struct ath_rc_stats *stats = &rc->rcstats[i];
  1346. char mcs[5];
  1347. char htmode[5];
  1348. int used_mcs = 0, used_htmode = 0;
  1349. if (WLAN_RC_PHY_HT(rc->rate_table->info[i].phy)) {
  1350. used_mcs = snprintf(mcs, 5, "%d",
  1351. rc->rate_table->info[i].ratecode);
  1352. if (WLAN_RC_PHY_40(rc->rate_table->info[i].phy))
  1353. used_htmode = snprintf(htmode, 5, "HT40");
  1354. else if (WLAN_RC_PHY_20(rc->rate_table->info[i].phy))
  1355. used_htmode = snprintf(htmode, 5, "HT20");
  1356. else
  1357. used_htmode = snprintf(htmode, 5, "????");
  1358. }
  1359. mcs[used_mcs] = '\0';
  1360. htmode[used_htmode] = '\0';
  1361. len += snprintf(buf + len, max - len,
  1362. "%6s %6s %3u.%d: "
  1363. "%10u %10u %10u %10u\n",
  1364. htmode,
  1365. mcs,
  1366. ratekbps / 1000,
  1367. (ratekbps % 1000) / 100,
  1368. stats->success,
  1369. stats->retries,
  1370. stats->xretries,
  1371. stats->per);
  1372. }
  1373. if (len > max)
  1374. len = max;
  1375. retval = simple_read_from_buffer(user_buf, count, ppos, buf, len);
  1376. kfree(buf);
  1377. return retval;
  1378. }
  1379. static const struct file_operations fops_rcstat = {
  1380. .read = read_file_rcstat,
  1381. .open = ath9k_debugfs_open,
  1382. .owner = THIS_MODULE
  1383. };
  1384. static void ath_rate_add_sta_debugfs(void *priv, void *priv_sta,
  1385. struct dentry *dir)
  1386. {
  1387. struct ath_rate_priv *rc = priv_sta;
  1388. debugfs_create_file("rc_stats", S_IRUGO, dir, rc, &fops_rcstat);
  1389. }
  1390. #endif /* CONFIG_ATH9K_DEBUGFS */
  1391. static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  1392. {
  1393. struct ath_wiphy *aphy = hw->priv;
  1394. return aphy->sc;
  1395. }
  1396. static void ath_rate_free(void *priv)
  1397. {
  1398. return;
  1399. }
  1400. static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1401. {
  1402. struct ath_softc *sc = priv;
  1403. struct ath_rate_priv *rate_priv;
  1404. rate_priv = kzalloc(sizeof(struct ath_rate_priv), gfp);
  1405. if (!rate_priv) {
  1406. ath_print(ath9k_hw_common(sc->sc_ah), ATH_DBG_FATAL,
  1407. "Unable to allocate private rc structure\n");
  1408. return NULL;
  1409. }
  1410. rate_priv->tx_triglevel_max = sc->sc_ah->caps.tx_triglevel_max;
  1411. return rate_priv;
  1412. }
  1413. static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
  1414. void *priv_sta)
  1415. {
  1416. struct ath_rate_priv *rate_priv = priv_sta;
  1417. kfree(rate_priv);
  1418. }
  1419. static struct rate_control_ops ath_rate_ops = {
  1420. .module = NULL,
  1421. .name = "ath9k_rate_control",
  1422. .tx_status = ath_tx_status,
  1423. .get_rate = ath_get_rate,
  1424. .rate_init = ath_rate_init,
  1425. .rate_update = ath_rate_update,
  1426. .alloc = ath_rate_alloc,
  1427. .free = ath_rate_free,
  1428. .alloc_sta = ath_rate_alloc_sta,
  1429. .free_sta = ath_rate_free_sta,
  1430. #ifdef CONFIG_ATH9K_DEBUGFS
  1431. .add_sta_debugfs = ath_rate_add_sta_debugfs,
  1432. #endif
  1433. };
  1434. int ath_rate_control_register(void)
  1435. {
  1436. return ieee80211_rate_control_register(&ath_rate_ops);
  1437. }
  1438. void ath_rate_control_unregister(void)
  1439. {
  1440. ieee80211_rate_control_unregister(&ath_rate_ops);
  1441. }