rc.c 44 KB

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