rc.c 61 KB

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