rc.c 50 KB

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