rc.c 44 KB

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