rc.c 51 KB

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