rc.c 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  1. /*
  2. * Copyright (c) 2004 Video54 Technologies, Inc.
  3. * Copyright (c) 2004-2008 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. /*
  18. * Atheros rate control algorithm
  19. */
  20. #include "core.h"
  21. /* FIXME: remove this include! */
  22. #include "../net/mac80211/rate.h"
  23. static struct ath_rate_table ar5416_11na_ratetable = {
  24. 42,
  25. {
  26. { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
  27. 5400, 0x0b, 0x00, 12,
  28. 0, 2, 1, 0, 0, 0, 0, 0 },
  29. { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
  30. 7800, 0x0f, 0x00, 18,
  31. 0, 3, 1, 1, 1, 1, 1, 0 },
  32. { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
  33. 10000, 0x0a, 0x00, 24,
  34. 2, 4, 2, 2, 2, 2, 2, 0 },
  35. { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
  36. 13900, 0x0e, 0x00, 36,
  37. 2, 6, 2, 3, 3, 3, 3, 0 },
  38. { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
  39. 17300, 0x09, 0x00, 48,
  40. 4, 10, 3, 4, 4, 4, 4, 0 },
  41. { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
  42. 23000, 0x0d, 0x00, 72,
  43. 4, 14, 3, 5, 5, 5, 5, 0 },
  44. { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
  45. 27400, 0x08, 0x00, 96,
  46. 4, 20, 3, 6, 6, 6, 6, 0 },
  47. { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
  48. 29300, 0x0c, 0x00, 108,
  49. 4, 23, 3, 7, 7, 7, 7, 0 },
  50. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
  51. 6400, 0x80, 0x00, 0,
  52. 0, 2, 3, 8, 24, 8, 24, 3216 },
  53. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
  54. 12700, 0x81, 0x00, 1,
  55. 2, 4, 3, 9, 25, 9, 25, 6434 },
  56. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
  57. 18800, 0x82, 0x00, 2,
  58. 2, 6, 3, 10, 26, 10, 26, 9650 },
  59. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
  60. 25000, 0x83, 0x00, 3,
  61. 4, 10, 3, 11, 27, 11, 27, 12868 },
  62. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
  63. 36700, 0x84, 0x00, 4,
  64. 4, 14, 3, 12, 28, 12, 28, 19304 },
  65. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
  66. 48100, 0x85, 0x00, 5,
  67. 4, 20, 3, 13, 29, 13, 29, 25740 },
  68. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
  69. 53500, 0x86, 0x00, 6,
  70. 4, 23, 3, 14, 30, 14, 30, 28956 },
  71. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
  72. 59000, 0x87, 0x00, 7,
  73. 4, 25, 3, 15, 31, 15, 32, 32180 },
  74. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
  75. 12700, 0x88, 0x00,
  76. 8, 0, 2, 3, 16, 33, 16, 33, 6430 },
  77. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
  78. 24800, 0x89, 0x00, 9,
  79. 2, 4, 3, 17, 34, 17, 34, 12860 },
  80. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
  81. 36600, 0x8a, 0x00, 10,
  82. 2, 6, 3, 18, 35, 18, 35, 19300 },
  83. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
  84. 48100, 0x8b, 0x00, 11,
  85. 4, 10, 3, 19, 36, 19, 36, 25736 },
  86. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
  87. 69500, 0x8c, 0x00, 12,
  88. 4, 14, 3, 20, 37, 20, 37, 38600 },
  89. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
  90. 89500, 0x8d, 0x00, 13,
  91. 4, 20, 3, 21, 38, 21, 38, 51472 },
  92. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
  93. 98900, 0x8e, 0x00, 14,
  94. 4, 23, 3, 22, 39, 22, 39, 57890 },
  95. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
  96. 108300, 0x8f, 0x00, 15,
  97. 4, 25, 3, 23, 40, 23, 41, 64320 },
  98. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
  99. 13200, 0x80, 0x00, 0,
  100. 0, 2, 3, 8, 24, 24, 24, 6684 },
  101. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
  102. 25900, 0x81, 0x00, 1,
  103. 2, 4, 3, 9, 25, 25, 25, 13368 },
  104. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
  105. 38600, 0x82, 0x00, 2,
  106. 2, 6, 3, 10, 26, 26, 26, 20052 },
  107. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
  108. 49800, 0x83, 0x00, 3,
  109. 4, 10, 3, 11, 27, 27, 27, 26738 },
  110. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
  111. 72200, 0x84, 0x00, 4,
  112. 4, 14, 3, 12, 28, 28, 28, 40104 },
  113. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
  114. 92900, 0x85, 0x00, 5,
  115. 4, 20, 3, 13, 29, 29, 29, 53476 },
  116. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
  117. 102700, 0x86, 0x00, 6,
  118. 4, 23, 3, 14, 30, 30, 30, 60156 },
  119. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
  120. 112000, 0x87, 0x00, 7,
  121. 4, 25, 3, 15, 31, 32, 32, 66840 },
  122. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
  123. 122000, 0x87, 0x00, 7,
  124. 4, 25, 3, 15, 31, 32, 32, 74200 },
  125. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
  126. 25800, 0x88, 0x00, 8,
  127. 0, 2, 3, 16, 33, 33, 33, 13360 },
  128. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
  129. 49800, 0x89, 0x00, 9,
  130. 2, 4, 3, 17, 34, 34, 34, 26720 },
  131. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
  132. 71900, 0x8a, 0x00, 10,
  133. 2, 6, 3, 18, 35, 35, 35, 40080 },
  134. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
  135. 92500, 0x8b, 0x00, 11,
  136. 4, 10, 3, 19, 36, 36, 36, 53440 },
  137. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
  138. 130300, 0x8c, 0x00, 12,
  139. 4, 14, 3, 20, 37, 37, 37, 80160 },
  140. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
  141. 162800, 0x8d, 0x00, 13,
  142. 4, 20, 3, 21, 38, 38, 38, 106880 },
  143. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
  144. 178200, 0x8e, 0x00, 14,
  145. 4, 23, 3, 22, 39, 39, 39, 120240 },
  146. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
  147. 192100, 0x8f, 0x00, 15,
  148. 4, 25, 3, 23, 40, 41, 41, 133600 },
  149. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
  150. 207000, 0x8f, 0x00, 15,
  151. 4, 25, 3, 23, 40, 41, 41, 148400 },
  152. },
  153. 50, /* probe interval */
  154. 50, /* rssi reduce interval */
  155. WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
  156. };
  157. /* TRUE_ALL - valid for 20/40/Legacy,
  158. * TRUE - Legacy only,
  159. * TRUE_20 - HT 20 only,
  160. * TRUE_40 - HT 40 only */
  161. /* 4ms frame limit not used for NG mode. The values filled
  162. * for HT are the 64K max aggregate limit */
  163. static struct ath_rate_table ar5416_11ng_ratetable = {
  164. 46,
  165. {
  166. { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 1000, /* 1 Mb */
  167. 900, 0x1b, 0x00, 2,
  168. 0, 0, 1, 0, 0, 0, 0, 0 },
  169. { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 2000, /* 2 Mb */
  170. 1900, 0x1a, 0x04, 4,
  171. 1, 1, 1, 1, 1, 1, 1, 0 },
  172. { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
  173. 4900, 0x19, 0x04, 11,
  174. 2, 2, 2, 2, 2, 2, 2, 0 },
  175. { TRUE_ALL, TRUE_ALL, WLAN_PHY_CCK, 11000, /* 11 Mb */
  176. 8100, 0x18, 0x04, 22,
  177. 3, 3, 2, 3, 3, 3, 3, 0 },
  178. { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
  179. 5400, 0x0b, 0x00, 12,
  180. 4, 2, 1, 4, 4, 4, 4, 0 },
  181. { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
  182. 7800, 0x0f, 0x00, 18,
  183. 4, 3, 1, 5, 5, 5, 5, 0 },
  184. { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
  185. 10100, 0x0a, 0x00, 24,
  186. 6, 4, 1, 6, 6, 6, 6, 0 },
  187. { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
  188. 14100, 0x0e, 0x00, 36,
  189. 6, 6, 2, 7, 7, 7, 7, 0 },
  190. { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
  191. 17700, 0x09, 0x00, 48,
  192. 8, 10, 3, 8, 8, 8, 8, 0 },
  193. { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
  194. 23700, 0x0d, 0x00, 72,
  195. 8, 14, 3, 9, 9, 9, 9, 0 },
  196. { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
  197. 27400, 0x08, 0x00, 96,
  198. 8, 20, 3, 10, 10, 10, 10, 0 },
  199. { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
  200. 30900, 0x0c, 0x00, 108,
  201. 8, 23, 3, 11, 11, 11, 11, 0 },
  202. { FALSE, FALSE, WLAN_PHY_HT_20_SS, 6500, /* 6.5 Mb */
  203. 6400, 0x80, 0x00, 0,
  204. 4, 2, 3, 12, 28, 12, 28, 3216 },
  205. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 13000, /* 13 Mb */
  206. 12700, 0x81, 0x00, 1,
  207. 6, 4, 3, 13, 29, 13, 29, 6434 },
  208. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 19500, /* 19.5 Mb */
  209. 18800, 0x82, 0x00, 2,
  210. 6, 6, 3, 14, 30, 14, 30, 9650 },
  211. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 26000, /* 26 Mb */
  212. 25000, 0x83, 0x00, 3,
  213. 8, 10, 3, 15, 31, 15, 31, 12868 },
  214. { TRUE_20, TRUE_20, WLAN_PHY_HT_20_SS, 39000, /* 39 Mb */
  215. 36700, 0x84, 0x00, 4,
  216. 8, 14, 3, 16, 32, 16, 32, 19304 },
  217. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 52000, /* 52 Mb */
  218. 48100, 0x85, 0x00, 5,
  219. 8, 20, 3, 17, 33, 17, 33, 25740 },
  220. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 58500, /* 58.5 Mb */
  221. 53500, 0x86, 0x00, 6,
  222. 8, 23, 3, 18, 34, 18, 34, 28956 },
  223. { FALSE, TRUE_20, WLAN_PHY_HT_20_SS, 65000, /* 65 Mb */
  224. 59000, 0x87, 0x00, 7,
  225. 8, 25, 3, 19, 35, 19, 36, 32180 },
  226. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 13000, /* 13 Mb */
  227. 12700, 0x88, 0x00, 8,
  228. 4, 2, 3, 20, 37, 20, 37, 6430 },
  229. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 26000, /* 26 Mb */
  230. 24800, 0x89, 0x00, 9,
  231. 6, 4, 3, 21, 38, 21, 38, 12860 },
  232. { FALSE, FALSE, WLAN_PHY_HT_20_DS, 39000, /* 39 Mb */
  233. 36600, 0x8a, 0x00, 10,
  234. 6, 6, 3, 22, 39, 22, 39, 19300 },
  235. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 52000, /* 52 Mb */
  236. 48100, 0x8b, 0x00, 11,
  237. 8, 10, 3, 23, 40, 23, 40, 25736 },
  238. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 78000, /* 78 Mb */
  239. 69500, 0x8c, 0x00, 12,
  240. 8, 14, 3, 24, 41, 24, 41, 38600 },
  241. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 104000, /* 104 Mb */
  242. 89500, 0x8d, 0x00, 13,
  243. 8, 20, 3, 25, 42, 25, 42, 51472 },
  244. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 117000, /* 117 Mb */
  245. 98900, 0x8e, 0x00, 14,
  246. 8, 23, 3, 26, 43, 26, 44, 57890 },
  247. { TRUE_20, FALSE, WLAN_PHY_HT_20_DS, 130000, /* 130 Mb */
  248. 108300, 0x8f, 0x00, 15,
  249. 8, 25, 3, 27, 44, 27, 45, 64320 },
  250. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 13500, /* 13.5 Mb */
  251. 13200, 0x80, 0x00, 0,
  252. 8, 2, 3, 12, 28, 28, 28, 6684 },
  253. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 27500, /* 27.0 Mb */
  254. 25900, 0x81, 0x00, 1,
  255. 8, 4, 3, 13, 29, 29, 29, 13368 },
  256. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 40500, /* 40.5 Mb */
  257. 38600, 0x82, 0x00, 2,
  258. 8, 6, 3, 14, 30, 30, 30, 20052 },
  259. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 54000, /* 54 Mb */
  260. 49800, 0x83, 0x00, 3,
  261. 8, 10, 3, 15, 31, 31, 31, 26738 },
  262. { TRUE_40, TRUE_40, WLAN_PHY_HT_40_SS, 81500, /* 81 Mb */
  263. 72200, 0x84, 0x00, 4,
  264. 8, 14, 3, 16, 32, 32, 32, 40104 },
  265. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 108000, /* 108 Mb */
  266. 92900, 0x85, 0x00, 5,
  267. 8, 20, 3, 17, 33, 33, 33, 53476 },
  268. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 121500, /* 121.5 Mb */
  269. 102700, 0x86, 0x00, 6,
  270. 8, 23, 3, 18, 34, 34, 34, 60156 },
  271. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS, 135000, /* 135 Mb */
  272. 112000, 0x87, 0x00, 7,
  273. 8, 23, 3, 19, 35, 36, 36, 66840 },
  274. { FALSE, TRUE_40, WLAN_PHY_HT_40_SS_HGI, 150000, /* 150 Mb */
  275. 122000, 0x87, 0x00, 7,
  276. 8, 25, 3, 19, 35, 36, 36, 74200 },
  277. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 27000, /* 27 Mb */
  278. 25800, 0x88, 0x00, 8,
  279. 8, 2, 3, 20, 37, 37, 37, 13360 },
  280. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 54000, /* 54 Mb */
  281. 49800, 0x89, 0x00, 9,
  282. 8, 4, 3, 21, 38, 38, 38, 26720 },
  283. { FALSE, FALSE, WLAN_PHY_HT_40_DS, 81000, /* 81 Mb */
  284. 71900, 0x8a, 0x00, 10,
  285. 8, 6, 3, 22, 39, 39, 39, 40080 },
  286. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 108000, /* 108 Mb */
  287. 92500, 0x8b, 0x00, 11,
  288. 8, 10, 3, 23, 40, 40, 40, 53440 },
  289. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 162000, /* 162 Mb */
  290. 130300, 0x8c, 0x00, 12,
  291. 8, 14, 3, 24, 41, 41, 41, 80160 },
  292. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 216000, /* 216 Mb */
  293. 162800, 0x8d, 0x00, 13,
  294. 8, 20, 3, 25, 42, 42, 42, 106880 },
  295. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 243000, /* 243 Mb */
  296. 178200, 0x8e, 0x00, 14,
  297. 8, 23, 3, 26, 43, 43, 43, 120240 },
  298. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS, 270000, /* 270 Mb */
  299. 192100, 0x8f, 0x00, 15,
  300. 8, 23, 3, 27, 44, 45, 45, 133600 },
  301. { TRUE_40, FALSE, WLAN_PHY_HT_40_DS_HGI, 300000, /* 300 Mb */
  302. 207000, 0x8f, 0x00, 15,
  303. 8, 25, 3, 27, 44, 45, 45, 148400 },
  304. },
  305. 50, /* probe interval */
  306. 50, /* rssi reduce interval */
  307. WLAN_RC_HT_FLAG, /* Phy rates allowed initially */
  308. };
  309. static struct ath_rate_table ar5416_11a_ratetable = {
  310. 8,
  311. {
  312. { TRUE, TRUE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
  313. 5400, 0x0b, 0x00, (0x80|12),
  314. 0, 2, 1, 0, 0 },
  315. { TRUE, TRUE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
  316. 7800, 0x0f, 0x00, 18,
  317. 0, 3, 1, 1, 0 },
  318. { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
  319. 10000, 0x0a, 0x00, (0x80|24),
  320. 2, 4, 2, 2, 0 },
  321. { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
  322. 13900, 0x0e, 0x00, 36,
  323. 2, 6, 2, 3, 0 },
  324. { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
  325. 17300, 0x09, 0x00, (0x80|48),
  326. 4, 10, 3, 4, 0 },
  327. { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
  328. 23000, 0x0d, 0x00, 72,
  329. 4, 14, 3, 5, 0 },
  330. { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
  331. 27400, 0x08, 0x00, 96,
  332. 4, 19, 3, 6, 0 },
  333. { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
  334. 29300, 0x0c, 0x00, 108,
  335. 4, 23, 3, 7, 0 },
  336. },
  337. 50, /* probe interval */
  338. 50, /* rssi reduce interval */
  339. 0, /* Phy rates allowed initially */
  340. };
  341. static struct ath_rate_table ar5416_11g_ratetable = {
  342. 12,
  343. {
  344. { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
  345. 900, 0x1b, 0x00, 2,
  346. 0, 0, 1, 0, 0 },
  347. { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
  348. 1900, 0x1a, 0x04, 4,
  349. 1, 1, 1, 1, 0 },
  350. { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
  351. 4900, 0x19, 0x04, 11,
  352. 2, 2, 2, 2, 0 },
  353. { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
  354. 8100, 0x18, 0x04, 22,
  355. 3, 3, 2, 3, 0 },
  356. { FALSE, FALSE, WLAN_PHY_OFDM, 6000, /* 6 Mb */
  357. 5400, 0x0b, 0x00, 12,
  358. 4, 2, 1, 4, 0 },
  359. { FALSE, FALSE, WLAN_PHY_OFDM, 9000, /* 9 Mb */
  360. 7800, 0x0f, 0x00, 18,
  361. 4, 3, 1, 5, 0 },
  362. { TRUE, TRUE, WLAN_PHY_OFDM, 12000, /* 12 Mb */
  363. 10000, 0x0a, 0x00, 24,
  364. 6, 4, 1, 6, 0 },
  365. { TRUE, TRUE, WLAN_PHY_OFDM, 18000, /* 18 Mb */
  366. 13900, 0x0e, 0x00, 36,
  367. 6, 6, 2, 7, 0 },
  368. { TRUE, TRUE, WLAN_PHY_OFDM, 24000, /* 24 Mb */
  369. 17300, 0x09, 0x00, 48,
  370. 8, 10, 3, 8, 0 },
  371. { TRUE, TRUE, WLAN_PHY_OFDM, 36000, /* 36 Mb */
  372. 23000, 0x0d, 0x00, 72,
  373. 8, 14, 3, 9, 0 },
  374. { TRUE, TRUE, WLAN_PHY_OFDM, 48000, /* 48 Mb */
  375. 27400, 0x08, 0x00, 96,
  376. 8, 19, 3, 10, 0 },
  377. { TRUE, TRUE, WLAN_PHY_OFDM, 54000, /* 54 Mb */
  378. 29300, 0x0c, 0x00, 108,
  379. 8, 23, 3, 11, 0 },
  380. },
  381. 50, /* probe interval */
  382. 50, /* rssi reduce interval */
  383. 0, /* Phy rates allowed initially */
  384. };
  385. static struct ath_rate_table ar5416_11b_ratetable = {
  386. 4,
  387. {
  388. { TRUE, TRUE, WLAN_PHY_CCK, 1000, /* 1 Mb */
  389. 900, 0x1b, 0x00, (0x80|2),
  390. 0, 0, 1, 0, 0 },
  391. { TRUE, TRUE, WLAN_PHY_CCK, 2000, /* 2 Mb */
  392. 1800, 0x1a, 0x04, (0x80|4),
  393. 1, 1, 1, 1, 0 },
  394. { TRUE, TRUE, WLAN_PHY_CCK, 5500, /* 5.5 Mb */
  395. 4300, 0x19, 0x04, (0x80|11),
  396. 1, 2, 2, 2, 0 },
  397. { TRUE, TRUE, WLAN_PHY_CCK, 11000, /* 11 Mb */
  398. 7100, 0x18, 0x04, (0x80|22),
  399. 1, 4, 100, 3, 0 },
  400. },
  401. 100, /* probe interval */
  402. 100, /* rssi reduce interval */
  403. 0, /* Phy rates allowed initially */
  404. };
  405. /*
  406. * Return the median of three numbers
  407. */
  408. static inline int8_t median(int8_t a, int8_t b, int8_t c)
  409. {
  410. if (a >= b) {
  411. if (b >= c)
  412. return b;
  413. else if (a > c)
  414. return c;
  415. else
  416. return a;
  417. } else {
  418. if (a >= c)
  419. return a;
  420. else if (b >= c)
  421. return c;
  422. else
  423. return b;
  424. }
  425. }
  426. static void ath_rc_sort_validrates(struct ath_rate_table *rate_table,
  427. struct ath_rate_node *ath_rc_priv)
  428. {
  429. u8 i, j, idx, idx_next;
  430. for (i = ath_rc_priv->max_valid_rate - 1; i > 0; i--) {
  431. for (j = 0; j <= i-1; j++) {
  432. idx = ath_rc_priv->valid_rate_index[j];
  433. idx_next = ath_rc_priv->valid_rate_index[j+1];
  434. if (rate_table->info[idx].ratekbps >
  435. rate_table->info[idx_next].ratekbps) {
  436. ath_rc_priv->valid_rate_index[j] = idx_next;
  437. ath_rc_priv->valid_rate_index[j+1] = idx;
  438. }
  439. }
  440. }
  441. }
  442. /* Access functions for valid_txrate_mask */
  443. static void ath_rc_init_valid_txmask(struct ath_rate_node *ath_rc_priv)
  444. {
  445. u8 i;
  446. for (i = 0; i < ath_rc_priv->rate_table_size; i++)
  447. ath_rc_priv->valid_rate_index[i] = FALSE;
  448. }
  449. static inline void ath_rc_set_valid_txmask(struct ath_rate_node *ath_rc_priv,
  450. u8 index, int valid_tx_rate)
  451. {
  452. ASSERT(index <= ath_rc_priv->rate_table_size);
  453. ath_rc_priv->valid_rate_index[index] = valid_tx_rate ? TRUE : FALSE;
  454. }
  455. static inline int ath_rc_isvalid_txmask(struct ath_rate_node *ath_rc_priv,
  456. u8 index)
  457. {
  458. ASSERT(index <= ath_rc_priv->rate_table_size);
  459. return ath_rc_priv->valid_rate_index[index];
  460. }
  461. /* Iterators for valid_txrate_mask */
  462. static inline int
  463. ath_rc_get_nextvalid_txrate(struct ath_rate_table *rate_table,
  464. struct ath_rate_node *ath_rc_priv,
  465. u8 cur_valid_txrate,
  466. u8 *next_idx)
  467. {
  468. u8 i;
  469. for (i = 0; i < ath_rc_priv->max_valid_rate - 1; i++) {
  470. if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
  471. *next_idx = ath_rc_priv->valid_rate_index[i+1];
  472. return TRUE;
  473. }
  474. }
  475. /* No more valid rates */
  476. *next_idx = 0;
  477. return FALSE;
  478. }
  479. /* Return true only for single stream */
  480. static int ath_rc_valid_phyrate(u32 phy, u32 capflag, int ignore_cw)
  481. {
  482. if (WLAN_RC_PHY_HT(phy) & !(capflag & WLAN_RC_HT_FLAG))
  483. return FALSE;
  484. if (WLAN_RC_PHY_DS(phy) && !(capflag & WLAN_RC_DS_FLAG))
  485. return FALSE;
  486. if (WLAN_RC_PHY_SGI(phy) && !(capflag & WLAN_RC_SGI_FLAG))
  487. return FALSE;
  488. if (!ignore_cw && WLAN_RC_PHY_HT(phy))
  489. if (WLAN_RC_PHY_40(phy) && !(capflag & WLAN_RC_40_FLAG))
  490. return FALSE;
  491. if (!WLAN_RC_PHY_40(phy) && (capflag & WLAN_RC_40_FLAG))
  492. return FALSE;
  493. return TRUE;
  494. }
  495. static inline int
  496. ath_rc_get_nextlowervalid_txrate(struct ath_rate_table *rate_table,
  497. struct ath_rate_node *ath_rc_priv,
  498. u8 cur_valid_txrate, u8 *next_idx)
  499. {
  500. int8_t i;
  501. for (i = 1; i < ath_rc_priv->max_valid_rate ; i++) {
  502. if (ath_rc_priv->valid_rate_index[i] == cur_valid_txrate) {
  503. *next_idx = ath_rc_priv->valid_rate_index[i-1];
  504. return TRUE;
  505. }
  506. }
  507. return FALSE;
  508. }
  509. /*
  510. * Initialize the Valid Rate Index from valid entries in Rate Table
  511. */
  512. static u8
  513. ath_rc_sib_init_validrates(struct ath_rate_node *ath_rc_priv,
  514. struct ath_rate_table *rate_table,
  515. u32 capflag)
  516. {
  517. u8 i, hi = 0;
  518. u32 valid;
  519. for (i = 0; i < rate_table->rate_cnt; i++) {
  520. valid = (ath_rc_priv->single_stream ?
  521. rate_table->info[i].valid_single_stream :
  522. rate_table->info[i].valid);
  523. if (valid == TRUE) {
  524. u32 phy = rate_table->info[i].phy;
  525. u8 valid_rate_count = 0;
  526. if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
  527. continue;
  528. valid_rate_count = ath_rc_priv->valid_phy_ratecnt[phy];
  529. ath_rc_priv->valid_phy_rateidx[phy][valid_rate_count] = i;
  530. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  531. ath_rc_set_valid_txmask(ath_rc_priv, i, TRUE);
  532. hi = A_MAX(hi, i);
  533. }
  534. }
  535. return hi;
  536. }
  537. /*
  538. * Initialize the Valid Rate Index from Rate Set
  539. */
  540. static u8
  541. ath_rc_sib_setvalid_rates(struct ath_rate_node *ath_rc_priv,
  542. struct ath_rate_table *rate_table,
  543. struct ath_rateset *rateset,
  544. u32 capflag)
  545. {
  546. /* XXX: Clean me up and make identation friendly */
  547. u8 i, j, hi = 0;
  548. /* Use intersection of working rates and valid rates */
  549. for (i = 0; i < rateset->rs_nrates; i++) {
  550. for (j = 0; j < rate_table->rate_cnt; j++) {
  551. u32 phy = rate_table->info[j].phy;
  552. u32 valid = (ath_rc_priv->single_stream ?
  553. rate_table->info[j].valid_single_stream :
  554. rate_table->info[j].valid);
  555. /* We allow a rate only if its valid and the
  556. * capflag matches one of the validity
  557. * (TRUE/TRUE_20/TRUE_40) flags */
  558. /* XXX: catch the negative of this branch
  559. * first and then continue */
  560. if (((rateset->rs_rates[i] & 0x7F) ==
  561. (rate_table->info[j].dot11rate & 0x7F)) &&
  562. ((valid & WLAN_RC_CAP_MODE(capflag)) ==
  563. WLAN_RC_CAP_MODE(capflag)) &&
  564. !WLAN_RC_PHY_HT(phy)) {
  565. u8 valid_rate_count = 0;
  566. if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
  567. continue;
  568. valid_rate_count =
  569. ath_rc_priv->valid_phy_ratecnt[phy];
  570. ath_rc_priv->valid_phy_rateidx[phy]
  571. [valid_rate_count] = j;
  572. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  573. ath_rc_set_valid_txmask(ath_rc_priv, j, TRUE);
  574. hi = A_MAX(hi, j);
  575. }
  576. }
  577. }
  578. return hi;
  579. }
  580. static u8
  581. ath_rc_sib_setvalid_htrates(struct ath_rate_node *ath_rc_priv,
  582. struct ath_rate_table *rate_table,
  583. u8 *mcs_set, u32 capflag)
  584. {
  585. u8 i, j, hi = 0;
  586. /* Use intersection of working rates and valid rates */
  587. for (i = 0; i < ((struct ath_rateset *)mcs_set)->rs_nrates; i++) {
  588. for (j = 0; j < rate_table->rate_cnt; j++) {
  589. u32 phy = rate_table->info[j].phy;
  590. u32 valid = (ath_rc_priv->single_stream ?
  591. rate_table->info[j].valid_single_stream :
  592. rate_table->info[j].valid);
  593. if (((((struct ath_rateset *)
  594. mcs_set)->rs_rates[i] & 0x7F) !=
  595. (rate_table->info[j].dot11rate & 0x7F)) ||
  596. !WLAN_RC_PHY_HT(phy) ||
  597. !WLAN_RC_PHY_HT_VALID(valid, capflag))
  598. continue;
  599. if (!ath_rc_valid_phyrate(phy, capflag, FALSE))
  600. continue;
  601. ath_rc_priv->valid_phy_rateidx[phy]
  602. [ath_rc_priv->valid_phy_ratecnt[phy]] = j;
  603. ath_rc_priv->valid_phy_ratecnt[phy] += 1;
  604. ath_rc_set_valid_txmask(ath_rc_priv, j, TRUE);
  605. hi = A_MAX(hi, j);
  606. }
  607. }
  608. return hi;
  609. }
  610. u8 ath_rate_findrateix(struct ath_softc *sc,
  611. u8 dot11rate)
  612. {
  613. struct ath_rate_table *ratetable;
  614. int i;
  615. ratetable = sc->hw_rate_table[sc->sc_curmode];
  616. if (WARN_ON(!ratetable))
  617. return 0;
  618. for (i = 0; i < ratetable->rate_cnt; i++) {
  619. if ((ratetable->info[i].dot11rate & 0x7f) == (dot11rate & 0x7f))
  620. return i;
  621. }
  622. return 0;
  623. }
  624. static u8 ath_rc_ratefind_ht(struct ath_softc *sc,
  625. struct ath_rate_node *ath_rc_priv,
  626. struct ath_rate_table *rate_table,
  627. int probe_allowed, int *is_probing,
  628. int is_retry)
  629. {
  630. u32 dt, best_thruput, this_thruput, now_msec;
  631. u8 rate, next_rate, best_rate, maxindex, minindex;
  632. int8_t rssi_last, rssi_reduce = 0, index = 0;
  633. *is_probing = FALSE;
  634. rssi_last = median(ath_rc_priv->rssi_last,
  635. ath_rc_priv->rssi_last_prev,
  636. ath_rc_priv->rssi_last_prev2);
  637. /*
  638. * Age (reduce) last ack rssi based on how old it is.
  639. * The bizarre numbers are so the delta is 160msec,
  640. * meaning we divide by 16.
  641. * 0msec <= dt <= 25msec: don't derate
  642. * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
  643. * 185msec <= dt: derate by 10dB
  644. */
  645. now_msec = jiffies_to_msecs(jiffies);
  646. dt = now_msec - ath_rc_priv->rssi_time;
  647. if (dt >= 185)
  648. rssi_reduce = 10;
  649. else if (dt >= 25)
  650. rssi_reduce = (u8)((dt - 25) >> 4);
  651. /* Now reduce rssi_last by rssi_reduce */
  652. if (rssi_last < rssi_reduce)
  653. rssi_last = 0;
  654. else
  655. rssi_last -= rssi_reduce;
  656. /*
  657. * Now look up the rate in the rssi table and return it.
  658. * If no rates match then we return 0 (lowest rate)
  659. */
  660. best_thruput = 0;
  661. maxindex = ath_rc_priv->max_valid_rate-1;
  662. minindex = 0;
  663. best_rate = minindex;
  664. /*
  665. * Try the higher rate first. It will reduce memory moving time
  666. * if we have very good channel characteristics.
  667. */
  668. for (index = maxindex; index >= minindex ; index--) {
  669. u8 per_thres;
  670. rate = ath_rc_priv->valid_rate_index[index];
  671. if (rate > ath_rc_priv->rate_max_phy)
  672. continue;
  673. /*
  674. * For TCP the average collision rate is around 11%,
  675. * so we ignore PERs less than this. This is to
  676. * prevent the rate we are currently using (whose
  677. * PER might be in the 10-15 range because of TCP
  678. * collisions) looking worse than the next lower
  679. * rate whose PER has decayed close to 0. If we
  680. * used to next lower rate, its PER would grow to
  681. * 10-15 and we would be worse off then staying
  682. * at the current rate.
  683. */
  684. per_thres = ath_rc_priv->state[rate].per;
  685. if (per_thres < 12)
  686. per_thres = 12;
  687. this_thruput = rate_table->info[rate].user_ratekbps *
  688. (100 - per_thres);
  689. if (best_thruput <= this_thruput) {
  690. best_thruput = this_thruput;
  691. best_rate = rate;
  692. }
  693. }
  694. rate = best_rate;
  695. /* if we are retrying for more than half the number
  696. * of max retries, use the min rate for the next retry
  697. */
  698. if (is_retry)
  699. rate = ath_rc_priv->valid_rate_index[minindex];
  700. ath_rc_priv->rssi_last_lookup = rssi_last;
  701. /*
  702. * Must check the actual rate (ratekbps) to account for
  703. * non-monoticity of 11g's rate table
  704. */
  705. if (rate >= ath_rc_priv->rate_max_phy && probe_allowed) {
  706. rate = ath_rc_priv->rate_max_phy;
  707. /* Probe the next allowed phy state */
  708. /* FIXME:XXXX Check to make sure ratMax is checked properly */
  709. if (ath_rc_get_nextvalid_txrate(rate_table,
  710. ath_rc_priv, rate, &next_rate) &&
  711. (now_msec - ath_rc_priv->probe_time >
  712. rate_table->probe_interval) &&
  713. (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
  714. rate = next_rate;
  715. ath_rc_priv->probe_rate = rate;
  716. ath_rc_priv->probe_time = now_msec;
  717. ath_rc_priv->hw_maxretry_pktcnt = 0;
  718. *is_probing = TRUE;
  719. }
  720. }
  721. /*
  722. * Make sure rate is not higher than the allowed maximum.
  723. * We should also enforce the min, but I suspect the min is
  724. * normally 1 rather than 0 because of the rate 9 vs 6 issue
  725. * in the old code.
  726. */
  727. if (rate > (ath_rc_priv->rate_table_size - 1))
  728. rate = ath_rc_priv->rate_table_size - 1;
  729. ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
  730. (rate_table->info[rate].valid_single_stream &&
  731. ath_rc_priv->single_stream));
  732. return rate;
  733. }
  734. static void ath_rc_rate_set_series(struct ath_rate_table *rate_table ,
  735. struct ath_rc_series *series,
  736. u8 tries,
  737. u8 rix,
  738. int rtsctsenable)
  739. {
  740. series->tries = tries;
  741. series->flags = (rtsctsenable ? ATH_RC_RTSCTS_FLAG : 0) |
  742. (WLAN_RC_PHY_DS(rate_table->info[rix].phy) ?
  743. ATH_RC_DS_FLAG : 0) |
  744. (WLAN_RC_PHY_40(rate_table->info[rix].phy) ?
  745. ATH_RC_CW40_FLAG : 0) |
  746. (WLAN_RC_PHY_SGI(rate_table->info[rix].phy) ?
  747. ATH_RC_SGI_FLAG : 0);
  748. series->rix = rate_table->info[rix].base_index;
  749. series->max_4ms_framelen = rate_table->info[rix].max_4ms_framelen;
  750. }
  751. static u8 ath_rc_rate_getidx(struct ath_softc *sc,
  752. struct ath_rate_node *ath_rc_priv,
  753. struct ath_rate_table *rate_table,
  754. u8 rix, u16 stepdown,
  755. u16 min_rate)
  756. {
  757. u32 j;
  758. u8 nextindex;
  759. if (min_rate) {
  760. for (j = RATE_TABLE_SIZE; j > 0; j--) {
  761. if (ath_rc_get_nextlowervalid_txrate(rate_table,
  762. ath_rc_priv, rix, &nextindex))
  763. rix = nextindex;
  764. else
  765. break;
  766. }
  767. } else {
  768. for (j = stepdown; j > 0; j--) {
  769. if (ath_rc_get_nextlowervalid_txrate(rate_table,
  770. ath_rc_priv, rix, &nextindex))
  771. rix = nextindex;
  772. else
  773. break;
  774. }
  775. }
  776. return rix;
  777. }
  778. static void ath_rc_ratefind(struct ath_softc *sc,
  779. struct ath_rate_node *ath_rc_priv,
  780. int num_tries, int num_rates, unsigned int rcflag,
  781. struct ath_rc_series series[], int *is_probe,
  782. int is_retry)
  783. {
  784. u8 try_per_rate = 0, i = 0, rix, nrix;
  785. struct ath_rate_table *rate_table;
  786. rate_table = sc->hw_rate_table[sc->sc_curmode];
  787. rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table,
  788. (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
  789. is_probe, is_retry);
  790. nrix = rix;
  791. if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*is_probe)) {
  792. /* set one try for probe rates. For the
  793. * probes don't enable rts */
  794. ath_rc_rate_set_series(rate_table,
  795. &series[i++], 1, nrix, FALSE);
  796. try_per_rate = (num_tries/num_rates);
  797. /* Get the next tried/allowed rate. No RTS for the next series
  798. * after the probe rate
  799. */
  800. nrix = ath_rc_rate_getidx(sc,
  801. ath_rc_priv, rate_table, nrix, 1, FALSE);
  802. ath_rc_rate_set_series(rate_table,
  803. &series[i++], try_per_rate, nrix, 0);
  804. } else {
  805. try_per_rate = (num_tries/num_rates);
  806. /* Set the choosen rate. No RTS for first series entry. */
  807. ath_rc_rate_set_series(rate_table,
  808. &series[i++], try_per_rate, nrix, FALSE);
  809. }
  810. /* Fill in the other rates for multirate retry */
  811. for ( ; i < num_rates; i++) {
  812. u8 try_num;
  813. u8 min_rate;
  814. try_num = ((i + 1) == num_rates) ?
  815. num_tries - (try_per_rate * i) : try_per_rate ;
  816. min_rate = (((i + 1) == num_rates) &&
  817. (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
  818. nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
  819. rate_table, nrix, 1, min_rate);
  820. /* All other rates in the series have RTS enabled */
  821. ath_rc_rate_set_series(rate_table,
  822. &series[i], try_num, nrix, TRUE);
  823. }
  824. /*
  825. * NB:Change rate series to enable aggregation when operating
  826. * at lower MCS rates. When first rate in series is MCS2
  827. * in HT40 @ 2.4GHz, series should look like:
  828. *
  829. * {MCS2, MCS1, MCS0, MCS0}.
  830. *
  831. * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
  832. * look like:
  833. *
  834. * {MCS3, MCS2, MCS1, MCS1}
  835. *
  836. * So, set fourth rate in series to be same as third one for
  837. * above conditions.
  838. */
  839. if ((sc->sc_curmode == ATH9K_MODE_11NG_HT20) ||
  840. (sc->sc_curmode == ATH9K_MODE_11NG_HT40PLUS) ||
  841. (sc->sc_curmode == ATH9K_MODE_11NG_HT40MINUS)) {
  842. u8 dot11rate = rate_table->info[rix].dot11rate;
  843. u8 phy = rate_table->info[rix].phy;
  844. if (i == 4 &&
  845. ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
  846. (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
  847. series[3].rix = series[2].rix;
  848. series[3].flags = series[2].flags;
  849. series[3].max_4ms_framelen = series[2].max_4ms_framelen;
  850. }
  851. }
  852. }
  853. /*
  854. * Return the Tx rate series.
  855. */
  856. static void ath_rate_findrate(struct ath_softc *sc,
  857. struct ath_rate_node *ath_rc_priv,
  858. int num_tries,
  859. int num_rates,
  860. unsigned int rcflag,
  861. struct ath_rc_series series[],
  862. int *is_probe,
  863. int is_retry)
  864. {
  865. if (!num_rates || !num_tries)
  866. return;
  867. ath_rc_ratefind(sc, ath_rc_priv, num_tries, num_rates,
  868. rcflag, series, is_probe, is_retry);
  869. }
  870. static void ath_rc_update_ht(struct ath_softc *sc,
  871. struct ath_rate_node *ath_rc_priv,
  872. struct ath_tx_info_priv *info_priv,
  873. int tx_rate, int xretries, int retries)
  874. {
  875. u32 now_msec = jiffies_to_msecs(jiffies);
  876. int state_change = FALSE, rate, count;
  877. u8 last_per;
  878. struct ath_rate_table *rate_table = sc->hw_rate_table[sc->sc_curmode];
  879. static u32 nretry_to_per_lookup[10] = {
  880. 100 * 0 / 1,
  881. 100 * 1 / 4,
  882. 100 * 1 / 2,
  883. 100 * 3 / 4,
  884. 100 * 4 / 5,
  885. 100 * 5 / 6,
  886. 100 * 6 / 7,
  887. 100 * 7 / 8,
  888. 100 * 8 / 9,
  889. 100 * 9 / 10
  890. };
  891. if (!ath_rc_priv)
  892. return;
  893. ASSERT(tx_rate >= 0);
  894. if (tx_rate < 0)
  895. return;
  896. /* To compensate for some imbalance between ctrl and ext. channel */
  897. if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
  898. info_priv->tx.ts_rssi =
  899. info_priv->tx.ts_rssi < 3 ? 0 :
  900. info_priv->tx.ts_rssi - 3;
  901. last_per = ath_rc_priv->state[tx_rate].per;
  902. if (xretries) {
  903. /* Update the PER. */
  904. if (xretries == 1) {
  905. ath_rc_priv->state[tx_rate].per += 30;
  906. if (ath_rc_priv->state[tx_rate].per > 100)
  907. ath_rc_priv->state[tx_rate].per = 100;
  908. } else {
  909. /* xretries == 2 */
  910. count = ARRAY_SIZE(nretry_to_per_lookup);
  911. if (retries >= count)
  912. retries = count - 1;
  913. /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
  914. ath_rc_priv->state[tx_rate].per =
  915. (u8)(ath_rc_priv->state[tx_rate].per -
  916. (ath_rc_priv->state[tx_rate].per >> 3) +
  917. ((100) >> 3));
  918. }
  919. /* xretries == 1 or 2 */
  920. if (ath_rc_priv->probe_rate == tx_rate)
  921. ath_rc_priv->probe_rate = 0;
  922. } else { /* xretries == 0 */
  923. /* Update the PER. */
  924. /* Make sure it doesn't index out of array's bounds. */
  925. count = ARRAY_SIZE(nretry_to_per_lookup);
  926. if (retries >= count)
  927. retries = count - 1;
  928. if (info_priv->n_bad_frames) {
  929. /* new_PER = 7/8*old_PER + 1/8*(currentPER)
  930. * Assuming that n_frames is not 0. The current PER
  931. * from the retries is 100 * retries / (retries+1),
  932. * since the first retries attempts failed, and the
  933. * next one worked. For the one that worked,
  934. * n_bad_frames subframes out of n_frames wored,
  935. * so the PER for that part is
  936. * 100 * n_bad_frames / n_frames, and it contributes
  937. * 100 * n_bad_frames / (n_frames * (retries+1)) to
  938. * the above PER. The expression below is a
  939. * simplified version of the sum of these two terms.
  940. */
  941. if (info_priv->n_frames > 0)
  942. ath_rc_priv->state[tx_rate].per
  943. = (u8)
  944. (ath_rc_priv->state[tx_rate].per -
  945. (ath_rc_priv->state[tx_rate].per >> 3) +
  946. ((100*(retries*info_priv->n_frames +
  947. info_priv->n_bad_frames) /
  948. (info_priv->n_frames *
  949. (retries+1))) >> 3));
  950. } else {
  951. /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
  952. ath_rc_priv->state[tx_rate].per = (u8)
  953. (ath_rc_priv->state[tx_rate].per -
  954. (ath_rc_priv->state[tx_rate].per >> 3) +
  955. (nretry_to_per_lookup[retries] >> 3));
  956. }
  957. ath_rc_priv->rssi_last_prev2 = ath_rc_priv->rssi_last_prev;
  958. ath_rc_priv->rssi_last_prev = ath_rc_priv->rssi_last;
  959. ath_rc_priv->rssi_last = info_priv->tx.ts_rssi;
  960. ath_rc_priv->rssi_time = now_msec;
  961. /*
  962. * If we got at most one retry then increase the max rate if
  963. * this was a probe. Otherwise, ignore the probe.
  964. */
  965. if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
  966. if (retries > 0 || 2 * info_priv->n_bad_frames >
  967. info_priv->n_frames) {
  968. /*
  969. * Since we probed with just a single attempt,
  970. * any retries means the probe failed. Also,
  971. * if the attempt worked, but more than half
  972. * the subframes were bad then also consider
  973. * the probe a failure.
  974. */
  975. ath_rc_priv->probe_rate = 0;
  976. } else {
  977. u8 probe_rate = 0;
  978. ath_rc_priv->rate_max_phy = ath_rc_priv->probe_rate;
  979. probe_rate = ath_rc_priv->probe_rate;
  980. if (ath_rc_priv->state[probe_rate].per > 30)
  981. ath_rc_priv->state[probe_rate].per = 20;
  982. ath_rc_priv->probe_rate = 0;
  983. /*
  984. * Since this probe succeeded, we allow the next
  985. * probe twice as soon. This allows the maxRate
  986. * to move up faster if the probes are
  987. * succesful.
  988. */
  989. ath_rc_priv->probe_time = now_msec -
  990. rate_table->probe_interval / 2;
  991. }
  992. }
  993. if (retries > 0) {
  994. /*
  995. * Don't update anything. We don't know if
  996. * this was because of collisions or poor signal.
  997. *
  998. * Later: if rssi_ack is close to
  999. * ath_rc_priv->state[txRate].rssi_thres and we see lots
  1000. * of retries, then we could increase
  1001. * ath_rc_priv->state[txRate].rssi_thres.
  1002. */
  1003. ath_rc_priv->hw_maxretry_pktcnt = 0;
  1004. } else {
  1005. /*
  1006. * It worked with no retries. First ignore bogus (small)
  1007. * rssi_ack values.
  1008. */
  1009. if (tx_rate == ath_rc_priv->rate_max_phy &&
  1010. ath_rc_priv->hw_maxretry_pktcnt < 255) {
  1011. ath_rc_priv->hw_maxretry_pktcnt++;
  1012. }
  1013. if (info_priv->tx.ts_rssi >=
  1014. rate_table->info[tx_rate].rssi_ack_validmin) {
  1015. /* Average the rssi */
  1016. if (tx_rate != ath_rc_priv->rssi_sum_rate) {
  1017. ath_rc_priv->rssi_sum_rate = tx_rate;
  1018. ath_rc_priv->rssi_sum =
  1019. ath_rc_priv->rssi_sum_cnt = 0;
  1020. }
  1021. ath_rc_priv->rssi_sum += info_priv->tx.ts_rssi;
  1022. ath_rc_priv->rssi_sum_cnt++;
  1023. if (ath_rc_priv->rssi_sum_cnt > 4) {
  1024. int32_t rssi_ackAvg =
  1025. (ath_rc_priv->rssi_sum + 2) / 4;
  1026. int8_t rssi_thres =
  1027. ath_rc_priv->state[tx_rate].
  1028. rssi_thres;
  1029. int8_t rssi_ack_vmin =
  1030. rate_table->info[tx_rate].
  1031. rssi_ack_validmin;
  1032. ath_rc_priv->rssi_sum =
  1033. ath_rc_priv->rssi_sum_cnt = 0;
  1034. /* Now reduce the current
  1035. * rssi threshold. */
  1036. if ((rssi_ackAvg < rssi_thres + 2) &&
  1037. (rssi_thres > rssi_ack_vmin)) {
  1038. ath_rc_priv->state[tx_rate].
  1039. rssi_thres--;
  1040. }
  1041. state_change = TRUE;
  1042. }
  1043. }
  1044. }
  1045. }
  1046. /* For all cases */
  1047. /*
  1048. * If this rate looks bad (high PER) then stop using it for
  1049. * a while (except if we are probing).
  1050. */
  1051. if (ath_rc_priv->state[tx_rate].per >= 55 && tx_rate > 0 &&
  1052. rate_table->info[tx_rate].ratekbps <=
  1053. rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
  1054. ath_rc_get_nextlowervalid_txrate(rate_table, ath_rc_priv,
  1055. (u8) tx_rate, &ath_rc_priv->rate_max_phy);
  1056. /* Don't probe for a little while. */
  1057. ath_rc_priv->probe_time = now_msec;
  1058. }
  1059. if (state_change) {
  1060. /*
  1061. * Make sure the rates above this have higher rssi thresholds.
  1062. * (Note: Monotonicity is kept within the OFDM rates and
  1063. * within the CCK rates. However, no adjustment is
  1064. * made to keep the rssi thresholds monotonically
  1065. * increasing between the CCK and OFDM rates.)
  1066. */
  1067. for (rate = tx_rate; rate <
  1068. ath_rc_priv->rate_table_size - 1; rate++) {
  1069. if (rate_table->info[rate+1].phy !=
  1070. rate_table->info[tx_rate].phy)
  1071. break;
  1072. if (ath_rc_priv->state[rate].rssi_thres +
  1073. rate_table->info[rate].rssi_ack_deltamin >
  1074. ath_rc_priv->state[rate+1].rssi_thres) {
  1075. ath_rc_priv->state[rate+1].rssi_thres =
  1076. ath_rc_priv->state[rate].
  1077. rssi_thres +
  1078. rate_table->info[rate].
  1079. rssi_ack_deltamin;
  1080. }
  1081. }
  1082. /* Make sure the rates below this have lower rssi thresholds. */
  1083. for (rate = tx_rate - 1; rate >= 0; rate--) {
  1084. if (rate_table->info[rate].phy !=
  1085. rate_table->info[tx_rate].phy)
  1086. break;
  1087. if (ath_rc_priv->state[rate].rssi_thres +
  1088. rate_table->info[rate].rssi_ack_deltamin >
  1089. ath_rc_priv->state[rate+1].rssi_thres) {
  1090. if (ath_rc_priv->state[rate+1].rssi_thres <
  1091. rate_table->info[rate].
  1092. rssi_ack_deltamin)
  1093. ath_rc_priv->state[rate].rssi_thres = 0;
  1094. else {
  1095. ath_rc_priv->state[rate].rssi_thres =
  1096. ath_rc_priv->state[rate+1].
  1097. rssi_thres -
  1098. rate_table->info[rate].
  1099. rssi_ack_deltamin;
  1100. }
  1101. if (ath_rc_priv->state[rate].rssi_thres <
  1102. rate_table->info[rate].
  1103. rssi_ack_validmin) {
  1104. ath_rc_priv->state[rate].rssi_thres =
  1105. rate_table->info[rate].
  1106. rssi_ack_validmin;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. /* Make sure the rates below this have lower PER */
  1112. /* Monotonicity is kept only for rates below the current rate. */
  1113. if (ath_rc_priv->state[tx_rate].per < last_per) {
  1114. for (rate = tx_rate - 1; rate >= 0; rate--) {
  1115. if (rate_table->info[rate].phy !=
  1116. rate_table->info[tx_rate].phy)
  1117. break;
  1118. if (ath_rc_priv->state[rate].per >
  1119. ath_rc_priv->state[rate+1].per) {
  1120. ath_rc_priv->state[rate].per =
  1121. ath_rc_priv->state[rate+1].per;
  1122. }
  1123. }
  1124. }
  1125. /* Maintain monotonicity for rates above the current rate */
  1126. for (rate = tx_rate; rate < ath_rc_priv->rate_table_size - 1; rate++) {
  1127. if (ath_rc_priv->state[rate+1].per < ath_rc_priv->state[rate].per)
  1128. ath_rc_priv->state[rate+1].per =
  1129. ath_rc_priv->state[rate].per;
  1130. }
  1131. /* Every so often, we reduce the thresholds and
  1132. * PER (different for CCK and OFDM). */
  1133. if (now_msec - ath_rc_priv->rssi_down_time >=
  1134. rate_table->rssi_reduce_interval) {
  1135. for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
  1136. if (ath_rc_priv->state[rate].rssi_thres >
  1137. rate_table->info[rate].rssi_ack_validmin)
  1138. ath_rc_priv->state[rate].rssi_thres -= 1;
  1139. }
  1140. ath_rc_priv->rssi_down_time = now_msec;
  1141. }
  1142. /* Every so often, we reduce the thresholds
  1143. * and PER (different for CCK and OFDM). */
  1144. if (now_msec - ath_rc_priv->per_down_time >=
  1145. rate_table->rssi_reduce_interval) {
  1146. for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
  1147. ath_rc_priv->state[rate].per =
  1148. 7 * ath_rc_priv->state[rate].per / 8;
  1149. }
  1150. ath_rc_priv->per_down_time = now_msec;
  1151. }
  1152. }
  1153. /*
  1154. * This routine is called in rate control callback tx_status() to give
  1155. * the status of previous frames.
  1156. */
  1157. static void ath_rc_update(struct ath_softc *sc,
  1158. struct ath_rate_node *ath_rc_priv,
  1159. struct ath_tx_info_priv *info_priv, int final_ts_idx,
  1160. int xretries, int long_retry)
  1161. {
  1162. struct ath_rate_table *rate_table;
  1163. struct ath_rc_series rcs[4];
  1164. u8 flags;
  1165. u32 series = 0, rix;
  1166. memcpy(rcs, info_priv->rcs, 4 * sizeof(rcs[0]));
  1167. rate_table = sc->hw_rate_table[sc->sc_curmode];
  1168. ASSERT(rcs[0].tries != 0);
  1169. /*
  1170. * If the first rate is not the final index, there
  1171. * are intermediate rate failures to be processed.
  1172. */
  1173. if (final_ts_idx != 0) {
  1174. /* Process intermediate rates that failed.*/
  1175. for (series = 0; series < final_ts_idx ; series++) {
  1176. if (rcs[series].tries != 0) {
  1177. flags = rcs[series].flags;
  1178. /* If HT40 and we have switched mode from
  1179. * 40 to 20 => don't update */
  1180. if ((flags & ATH_RC_CW40_FLAG) &&
  1181. (ath_rc_priv->rc_phy_mode !=
  1182. (flags & ATH_RC_CW40_FLAG)))
  1183. return;
  1184. if ((flags & ATH_RC_CW40_FLAG) &&
  1185. (flags & ATH_RC_SGI_FLAG))
  1186. rix = rate_table->info[
  1187. rcs[series].rix].ht_index;
  1188. else if (flags & ATH_RC_SGI_FLAG)
  1189. rix = rate_table->info[
  1190. rcs[series].rix].sgi_index;
  1191. else if (flags & ATH_RC_CW40_FLAG)
  1192. rix = rate_table->info[
  1193. rcs[series].rix].cw40index;
  1194. else
  1195. rix = rate_table->info[
  1196. rcs[series].rix].base_index;
  1197. ath_rc_update_ht(sc, ath_rc_priv,
  1198. info_priv, rix,
  1199. xretries ? 1 : 2,
  1200. rcs[series].tries);
  1201. }
  1202. }
  1203. } else {
  1204. /*
  1205. * Handle the special case of MIMO PS burst, where the second
  1206. * aggregate is sent out with only one rate and one try.
  1207. * Treating it as an excessive retry penalizes the rate
  1208. * inordinately.
  1209. */
  1210. if (rcs[0].tries == 1 && xretries == 1)
  1211. xretries = 2;
  1212. }
  1213. flags = rcs[series].flags;
  1214. /* If HT40 and we have switched mode from 40 to 20 => don't update */
  1215. if ((flags & ATH_RC_CW40_FLAG) &&
  1216. (ath_rc_priv->rc_phy_mode != (flags & ATH_RC_CW40_FLAG)))
  1217. return;
  1218. if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_SGI_FLAG))
  1219. rix = rate_table->info[rcs[series].rix].ht_index;
  1220. else if (flags & ATH_RC_SGI_FLAG)
  1221. rix = rate_table->info[rcs[series].rix].sgi_index;
  1222. else if (flags & ATH_RC_CW40_FLAG)
  1223. rix = rate_table->info[rcs[series].rix].cw40index;
  1224. else
  1225. rix = rate_table->info[rcs[series].rix].base_index;
  1226. ath_rc_update_ht(sc, ath_rc_priv, info_priv, rix,
  1227. xretries, long_retry);
  1228. }
  1229. /*
  1230. * Process a tx descriptor for a completed transmit (success or failure).
  1231. */
  1232. static void ath_rate_tx_complete(struct ath_softc *sc,
  1233. struct ath_node *an,
  1234. struct ath_rate_node *rc_priv,
  1235. struct ath_tx_info_priv *info_priv)
  1236. {
  1237. int final_ts_idx = info_priv->tx.ts_rateindex;
  1238. int tx_status = 0, is_underrun = 0;
  1239. if (info_priv->tx.ts_status & ATH9K_TXERR_FILT)
  1240. return;
  1241. if (info_priv->tx.ts_rssi > 0) {
  1242. ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
  1243. info_priv->tx.ts_rssi);
  1244. }
  1245. /*
  1246. * If underrun error is seen assume it as an excessive retry only
  1247. * if prefetch trigger level have reached the max (0x3f for 5416)
  1248. * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
  1249. * times. This affects how ratectrl updates PER for the failed rate.
  1250. */
  1251. if (info_priv->tx.ts_flags &
  1252. (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN) &&
  1253. ((sc->sc_ah->ah_txTrigLevel) >= rc_priv->tx_triglevel_max)) {
  1254. tx_status = 1;
  1255. is_underrun = 1;
  1256. }
  1257. if ((info_priv->tx.ts_status & ATH9K_TXERR_XRETRY) ||
  1258. (info_priv->tx.ts_status & ATH9K_TXERR_FIFO))
  1259. tx_status = 1;
  1260. ath_rc_update(sc, rc_priv, info_priv, final_ts_idx, tx_status,
  1261. (is_underrun) ? ATH_11N_TXMAXTRY :
  1262. info_priv->tx.ts_longretry);
  1263. }
  1264. static void ath_rc_init(struct ath_softc *sc,
  1265. struct ath_rate_node *ath_rc_priv,
  1266. struct ieee80211_supported_band *sband,
  1267. struct ieee80211_sta *sta)
  1268. {
  1269. struct ath_rate_table *rate_table = NULL;
  1270. struct ath_rateset *rateset = &ath_rc_priv->neg_rates;
  1271. u8 *ht_mcs = (u8 *)&ath_rc_priv->neg_ht_rates;
  1272. u8 i, j, k, hi = 0, hthi = 0;
  1273. rate_table = sc->hw_rate_table[sc->sc_curmode];
  1274. if (sta->ht_cap.ht_supported) {
  1275. if (sband->band == IEEE80211_BAND_2GHZ)
  1276. rate_table = sc->hw_rate_table[ATH9K_MODE_11NG_HT20];
  1277. else
  1278. rate_table = sc->hw_rate_table[ATH9K_MODE_11NA_HT20];
  1279. ath_rc_priv->ht_cap = (WLAN_RC_HT_FLAG | WLAN_RC_DS_FLAG);
  1280. if (sta->ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40)
  1281. ath_rc_priv->ht_cap |= WLAN_RC_40_FLAG;
  1282. }
  1283. /* Initial rate table size. Will change depending
  1284. * on the working rate set */
  1285. ath_rc_priv->rate_table_size = MAX_TX_RATE_TBL;
  1286. /* Initialize thresholds according to the global rate table */
  1287. for (i = 0 ; i < ath_rc_priv->rate_table_size; i++) {
  1288. ath_rc_priv->state[i].rssi_thres =
  1289. rate_table->info[i].rssi_ack_validmin;
  1290. ath_rc_priv->state[i].per = 0;
  1291. }
  1292. /* Determine the valid rates */
  1293. ath_rc_init_valid_txmask(ath_rc_priv);
  1294. for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
  1295. for (j = 0; j < MAX_TX_RATE_PHY; j++)
  1296. ath_rc_priv->valid_phy_rateidx[i][j] = 0;
  1297. ath_rc_priv->valid_phy_ratecnt[i] = 0;
  1298. }
  1299. ath_rc_priv->rc_phy_mode = (ath_rc_priv->ht_cap & WLAN_RC_40_FLAG);
  1300. /* Set stream capability */
  1301. ath_rc_priv->single_stream = (ath_rc_priv->ht_cap & WLAN_RC_DS_FLAG) ? 0 : 1;
  1302. if (!rateset->rs_nrates) {
  1303. /* No working rate, just initialize valid rates */
  1304. hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
  1305. ath_rc_priv->ht_cap);
  1306. } else {
  1307. /* Use intersection of working rates and valid rates */
  1308. hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
  1309. rateset, ath_rc_priv->ht_cap);
  1310. if (ath_rc_priv->ht_cap & WLAN_RC_HT_FLAG) {
  1311. hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
  1312. rate_table,
  1313. ht_mcs,
  1314. ath_rc_priv->ht_cap);
  1315. }
  1316. hi = A_MAX(hi, hthi);
  1317. }
  1318. ath_rc_priv->rate_table_size = hi + 1;
  1319. ath_rc_priv->rate_max_phy = 0;
  1320. ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
  1321. for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
  1322. for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
  1323. ath_rc_priv->valid_rate_index[k++] =
  1324. ath_rc_priv->valid_phy_rateidx[i][j];
  1325. }
  1326. if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, TRUE)
  1327. || !ath_rc_priv->valid_phy_ratecnt[i])
  1328. continue;
  1329. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
  1330. }
  1331. ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
  1332. ASSERT(k <= MAX_TX_RATE_TBL);
  1333. ath_rc_priv->max_valid_rate = k;
  1334. /*
  1335. * Some third party vendors don't send the supported rate series in
  1336. * order. So sorting to make sure its in order, otherwise our RateFind
  1337. * Algo will select wrong rates
  1338. */
  1339. ath_rc_sort_validrates(rate_table, ath_rc_priv);
  1340. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
  1341. }
  1342. /* Rate Control callbacks */
  1343. static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
  1344. struct ieee80211_sta *sta, void *priv_sta,
  1345. struct sk_buff *skb)
  1346. {
  1347. struct ath_softc *sc = priv;
  1348. struct ath_tx_info_priv *tx_info_priv;
  1349. struct ath_node *an;
  1350. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1351. struct ieee80211_hdr *hdr;
  1352. __le16 fc;
  1353. hdr = (struct ieee80211_hdr *)skb->data;
  1354. fc = hdr->frame_control;
  1355. /* XXX: UGLY HACK!! */
  1356. tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
  1357. an = (struct ath_node *)sta->drv_priv;
  1358. if (tx_info_priv == NULL)
  1359. return;
  1360. if (an && priv_sta && ieee80211_is_data(fc))
  1361. ath_rate_tx_complete(sc, an, priv_sta, tx_info_priv);
  1362. kfree(tx_info_priv);
  1363. tx_info->control.vif = NULL;
  1364. }
  1365. static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  1366. struct ieee80211_tx_rate_control *txrc)
  1367. {
  1368. struct ieee80211_supported_band *sband = txrc->sband;
  1369. struct sk_buff *skb = txrc->skb;
  1370. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1371. struct ath_softc *sc = priv;
  1372. struct ieee80211_hw *hw = sc->hw;
  1373. struct ath_tx_info_priv *tx_info_priv;
  1374. struct ath_rate_node *ath_rc_priv = priv_sta;
  1375. struct ath_node *an;
  1376. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1377. int is_probe = FALSE;
  1378. s8 lowest_idx;
  1379. __le16 fc = hdr->frame_control;
  1380. u8 *qc, tid;
  1381. DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
  1382. /* allocate driver private area of tx_info, XXX: UGLY HACK! */
  1383. tx_info->control.vif = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
  1384. tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
  1385. ASSERT(tx_info_priv != NULL);
  1386. lowest_idx = rate_lowest_index(sband, sta);
  1387. tx_info_priv->min_rate = (sband->bitrates[lowest_idx].bitrate * 2) / 10;
  1388. /* lowest rate for management and multicast/broadcast frames */
  1389. if (!ieee80211_is_data(fc) ||
  1390. is_multicast_ether_addr(hdr->addr1) || !sta) {
  1391. tx_info->control.rates[0].idx = lowest_idx;
  1392. return;
  1393. }
  1394. /* Find tx rate for unicast frames */
  1395. ath_rate_findrate(sc, ath_rc_priv,
  1396. ATH_11N_TXMAXTRY, 4,
  1397. ATH_RC_PROBE_ALLOWED,
  1398. tx_info_priv->rcs,
  1399. &is_probe,
  1400. false);
  1401. #if 0
  1402. if (is_probe)
  1403. sel->probe_idx = ath_rc_priv->tx_ratectrl.probe_rate;
  1404. #endif
  1405. /* Ratecontrol sometimes returns invalid rate index */
  1406. if (tx_info_priv->rcs[0].rix != 0xff)
  1407. ath_rc_priv->prev_data_rix = tx_info_priv->rcs[0].rix;
  1408. else
  1409. tx_info_priv->rcs[0].rix = ath_rc_priv->prev_data_rix;
  1410. tx_info->control.rates[0].idx = tx_info_priv->rcs[0].rix;
  1411. /* Check if aggregation has to be enabled for this tid */
  1412. if (hw->conf.ht.enabled) {
  1413. if (ieee80211_is_data_qos(fc)) {
  1414. qc = ieee80211_get_qos_ctl(hdr);
  1415. tid = qc[0] & 0xf;
  1416. an = (struct ath_node *)sta->drv_priv;
  1417. if(ath_tx_aggr_check(sc, an, tid))
  1418. ieee80211_start_tx_ba_session(hw, hdr->addr1, tid);
  1419. }
  1420. }
  1421. }
  1422. static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1423. struct ieee80211_sta *sta, void *priv_sta)
  1424. {
  1425. struct ath_softc *sc = priv;
  1426. struct ath_rate_node *ath_rc_priv = priv_sta;
  1427. int i, j = 0;
  1428. for (i = 0; i < sband->n_bitrates; i++) {
  1429. if (sta->supp_rates[sband->band] & BIT(i)) {
  1430. ath_rc_priv->neg_rates.rs_rates[j]
  1431. = (sband->bitrates[i].bitrate * 2) / 10;
  1432. j++;
  1433. }
  1434. }
  1435. ath_rc_priv->neg_rates.rs_nrates = j;
  1436. if (sta->ht_cap.ht_supported) {
  1437. for (i = 0, j = 0; i < 77; i++) {
  1438. if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
  1439. ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
  1440. if (j == ATH_RATE_MAX)
  1441. break;
  1442. }
  1443. ath_rc_priv->neg_ht_rates.rs_nrates = j;
  1444. }
  1445. ath_rc_init(sc, priv_sta, sband, sta);
  1446. }
  1447. static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  1448. {
  1449. return hw->priv;
  1450. }
  1451. static void ath_rate_free(void *priv)
  1452. {
  1453. return;
  1454. }
  1455. static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1456. {
  1457. struct ath_softc *sc = priv;
  1458. struct ath_rate_node *rate_priv;
  1459. rate_priv = kzalloc(sizeof(struct ath_rate_node), gfp);
  1460. if (!rate_priv) {
  1461. DPRINTF(sc, ATH_DBG_FATAL,
  1462. "%s: Unable to allocate private rc structure\n",
  1463. __func__);
  1464. return NULL;
  1465. }
  1466. rate_priv->rssi_down_time = jiffies_to_msecs(jiffies);
  1467. rate_priv->tx_triglevel_max = sc->sc_ah->ah_caps.tx_triglevel_max;
  1468. return rate_priv;
  1469. }
  1470. static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
  1471. void *priv_sta)
  1472. {
  1473. struct ath_rate_node *rate_priv = priv_sta;
  1474. kfree(rate_priv);
  1475. }
  1476. static struct rate_control_ops ath_rate_ops = {
  1477. .module = NULL,
  1478. .name = "ath9k_rate_control",
  1479. .tx_status = ath_tx_status,
  1480. .get_rate = ath_get_rate,
  1481. .rate_init = ath_rate_init,
  1482. .alloc = ath_rate_alloc,
  1483. .free = ath_rate_free,
  1484. .alloc_sta = ath_rate_alloc_sta,
  1485. .free_sta = ath_rate_free_sta,
  1486. };
  1487. void ath_rate_attach(struct ath_softc *sc)
  1488. {
  1489. sc->hw_rate_table[ATH9K_MODE_11B] =
  1490. &ar5416_11b_ratetable;
  1491. sc->hw_rate_table[ATH9K_MODE_11A] =
  1492. &ar5416_11a_ratetable;
  1493. sc->hw_rate_table[ATH9K_MODE_11G] =
  1494. &ar5416_11g_ratetable;
  1495. sc->hw_rate_table[ATH9K_MODE_11NA_HT20] =
  1496. &ar5416_11na_ratetable;
  1497. sc->hw_rate_table[ATH9K_MODE_11NG_HT20] =
  1498. &ar5416_11ng_ratetable;
  1499. sc->hw_rate_table[ATH9K_MODE_11NA_HT40PLUS] =
  1500. &ar5416_11na_ratetable;
  1501. sc->hw_rate_table[ATH9K_MODE_11NA_HT40MINUS] =
  1502. &ar5416_11na_ratetable;
  1503. sc->hw_rate_table[ATH9K_MODE_11NG_HT40PLUS] =
  1504. &ar5416_11ng_ratetable;
  1505. sc->hw_rate_table[ATH9K_MODE_11NG_HT40MINUS] =
  1506. &ar5416_11ng_ratetable;
  1507. }
  1508. int ath_rate_control_register(void)
  1509. {
  1510. return ieee80211_rate_control_register(&ath_rate_ops);
  1511. }
  1512. void ath_rate_control_unregister(void)
  1513. {
  1514. ieee80211_rate_control_unregister(&ath_rate_ops);
  1515. }