rc.c 43 KB

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