rc.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843
  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. /*
  655. * Update rate-control state on a device state change. When
  656. * operating as a station this includes associate/reassociate
  657. * with an AP. Otherwise this gets called, for example, when
  658. * the we transition to run state when operating as an AP.
  659. */
  660. void ath_rate_newstate(struct ath_softc *sc, struct ath_vap *avp)
  661. {
  662. struct ath_rate_softc *asc = sc->sc_rc;
  663. if (avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE) {
  664. asc->fixedrix =
  665. sc->sc_rixmap[avp->av_config.av_fixed_rateset & 0xff];
  666. /* NB: check the fixed rate exists */
  667. if (asc->fixedrix == 0xff)
  668. asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
  669. } else {
  670. asc->fixedrix = IEEE80211_FIXED_RATE_NONE;
  671. }
  672. }
  673. static u8 ath_rc_ratefind_ht(struct ath_softc *sc,
  674. struct ath_rate_node *ath_rc_priv,
  675. const struct ath_rate_table *rate_table,
  676. int probe_allowed, int *is_probing,
  677. int is_retry)
  678. {
  679. u32 dt, best_thruput, this_thruput, now_msec;
  680. u8 rate, next_rate, best_rate, maxindex, minindex;
  681. int8_t rssi_last, rssi_reduce = 0, index = 0;
  682. *is_probing = FALSE;
  683. rssi_last = median(ath_rc_priv->rssi_last,
  684. ath_rc_priv->rssi_last_prev,
  685. ath_rc_priv->rssi_last_prev2);
  686. /*
  687. * Age (reduce) last ack rssi based on how old it is.
  688. * The bizarre numbers are so the delta is 160msec,
  689. * meaning we divide by 16.
  690. * 0msec <= dt <= 25msec: don't derate
  691. * 25msec <= dt <= 185msec: derate linearly from 0 to 10dB
  692. * 185msec <= dt: derate by 10dB
  693. */
  694. now_msec = jiffies_to_msecs(jiffies);
  695. dt = now_msec - ath_rc_priv->rssi_time;
  696. if (dt >= 185)
  697. rssi_reduce = 10;
  698. else if (dt >= 25)
  699. rssi_reduce = (u8)((dt - 25) >> 4);
  700. /* Now reduce rssi_last by rssi_reduce */
  701. if (rssi_last < rssi_reduce)
  702. rssi_last = 0;
  703. else
  704. rssi_last -= rssi_reduce;
  705. /*
  706. * Now look up the rate in the rssi table and return it.
  707. * If no rates match then we return 0 (lowest rate)
  708. */
  709. best_thruput = 0;
  710. maxindex = ath_rc_priv->max_valid_rate-1;
  711. minindex = 0;
  712. best_rate = minindex;
  713. /*
  714. * Try the higher rate first. It will reduce memory moving time
  715. * if we have very good channel characteristics.
  716. */
  717. for (index = maxindex; index >= minindex ; index--) {
  718. u8 per_thres;
  719. rate = ath_rc_priv->valid_rate_index[index];
  720. if (rate > ath_rc_priv->rate_max_phy)
  721. continue;
  722. /*
  723. * For TCP the average collision rate is around 11%,
  724. * so we ignore PERs less than this. This is to
  725. * prevent the rate we are currently using (whose
  726. * PER might be in the 10-15 range because of TCP
  727. * collisions) looking worse than the next lower
  728. * rate whose PER has decayed close to 0. If we
  729. * used to next lower rate, its PER would grow to
  730. * 10-15 and we would be worse off then staying
  731. * at the current rate.
  732. */
  733. per_thres = ath_rc_priv->state[rate].per;
  734. if (per_thres < 12)
  735. per_thres = 12;
  736. this_thruput = rate_table->info[rate].user_ratekbps *
  737. (100 - per_thres);
  738. if (best_thruput <= this_thruput) {
  739. best_thruput = this_thruput;
  740. best_rate = rate;
  741. }
  742. }
  743. rate = best_rate;
  744. /* if we are retrying for more than half the number
  745. * of max retries, use the min rate for the next retry
  746. */
  747. if (is_retry)
  748. rate = ath_rc_priv->valid_rate_index[minindex];
  749. ath_rc_priv->rssi_last_lookup = rssi_last;
  750. /*
  751. * Must check the actual rate (ratekbps) to account for
  752. * non-monoticity of 11g's rate table
  753. */
  754. if (rate >= ath_rc_priv->rate_max_phy && probe_allowed) {
  755. rate = ath_rc_priv->rate_max_phy;
  756. /* Probe the next allowed phy state */
  757. /* FIXME:XXXX Check to make sure ratMax is checked properly */
  758. if (ath_rc_get_nextvalid_txrate(rate_table,
  759. ath_rc_priv, rate, &next_rate) &&
  760. (now_msec - ath_rc_priv->probe_time >
  761. rate_table->probe_interval) &&
  762. (ath_rc_priv->hw_maxretry_pktcnt >= 1)) {
  763. rate = next_rate;
  764. ath_rc_priv->probe_rate = rate;
  765. ath_rc_priv->probe_time = now_msec;
  766. ath_rc_priv->hw_maxretry_pktcnt = 0;
  767. *is_probing = TRUE;
  768. }
  769. }
  770. /*
  771. * Make sure rate is not higher than the allowed maximum.
  772. * We should also enforce the min, but I suspect the min is
  773. * normally 1 rather than 0 because of the rate 9 vs 6 issue
  774. * in the old code.
  775. */
  776. if (rate > (ath_rc_priv->rate_table_size - 1))
  777. rate = ath_rc_priv->rate_table_size - 1;
  778. ASSERT((rate_table->info[rate].valid && !ath_rc_priv->single_stream) ||
  779. (rate_table->info[rate].valid_single_stream &&
  780. ath_rc_priv->single_stream));
  781. return rate;
  782. }
  783. static void ath_rc_rate_set_series(const struct ath_rate_table *rate_table ,
  784. struct ath_rc_series *series,
  785. u8 tries,
  786. u8 rix,
  787. int rtsctsenable)
  788. {
  789. series->tries = tries;
  790. series->flags = (rtsctsenable ? ATH_RC_RTSCTS_FLAG : 0) |
  791. (WLAN_RC_PHY_DS(rate_table->info[rix].phy) ?
  792. ATH_RC_DS_FLAG : 0) |
  793. (WLAN_RC_PHY_40(rate_table->info[rix].phy) ?
  794. ATH_RC_CW40_FLAG : 0) |
  795. (WLAN_RC_PHY_SGI(rate_table->info[rix].phy) ?
  796. ATH_RC_SGI_FLAG : 0);
  797. series->rix = rate_table->info[rix].base_index;
  798. series->max_4ms_framelen = rate_table->info[rix].max_4ms_framelen;
  799. }
  800. static u8 ath_rc_rate_getidx(struct ath_softc *sc,
  801. struct ath_rate_node *ath_rc_priv,
  802. const struct ath_rate_table *rate_table,
  803. u8 rix, u16 stepdown,
  804. u16 min_rate)
  805. {
  806. u32 j;
  807. u8 nextindex;
  808. if (min_rate) {
  809. for (j = RATE_TABLE_SIZE; j > 0; j--) {
  810. if (ath_rc_get_nextlowervalid_txrate(rate_table,
  811. ath_rc_priv, rix, &nextindex))
  812. rix = nextindex;
  813. else
  814. break;
  815. }
  816. } else {
  817. for (j = stepdown; j > 0; j--) {
  818. if (ath_rc_get_nextlowervalid_txrate(rate_table,
  819. ath_rc_priv, rix, &nextindex))
  820. rix = nextindex;
  821. else
  822. break;
  823. }
  824. }
  825. return rix;
  826. }
  827. static void ath_rc_ratefind(struct ath_softc *sc,
  828. struct ath_rate_node *ath_rc_priv,
  829. int num_tries, int num_rates, unsigned int rcflag,
  830. struct ath_rc_series series[], int *is_probe,
  831. int is_retry)
  832. {
  833. u8 try_per_rate = 0, i = 0, rix, nrix;
  834. struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
  835. struct ath_rate_table *rate_table;
  836. rate_table =
  837. (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
  838. rix = ath_rc_ratefind_ht(sc, ath_rc_priv, rate_table,
  839. (rcflag & ATH_RC_PROBE_ALLOWED) ? 1 : 0,
  840. is_probe, is_retry);
  841. nrix = rix;
  842. if ((rcflag & ATH_RC_PROBE_ALLOWED) && (*is_probe)) {
  843. /* set one try for probe rates. For the
  844. * probes don't enable rts */
  845. ath_rc_rate_set_series(rate_table,
  846. &series[i++], 1, nrix, FALSE);
  847. try_per_rate = (num_tries/num_rates);
  848. /* Get the next tried/allowed rate. No RTS for the next series
  849. * after the probe rate
  850. */
  851. nrix = ath_rc_rate_getidx(sc,
  852. ath_rc_priv, rate_table, nrix, 1, FALSE);
  853. ath_rc_rate_set_series(rate_table,
  854. &series[i++], try_per_rate, nrix, 0);
  855. } else {
  856. try_per_rate = (num_tries/num_rates);
  857. /* Set the choosen rate. No RTS for first series entry. */
  858. ath_rc_rate_set_series(rate_table,
  859. &series[i++], try_per_rate, nrix, FALSE);
  860. }
  861. /* Fill in the other rates for multirate retry */
  862. for ( ; i < num_rates; i++) {
  863. u8 try_num;
  864. u8 min_rate;
  865. try_num = ((i + 1) == num_rates) ?
  866. num_tries - (try_per_rate * i) : try_per_rate ;
  867. min_rate = (((i + 1) == num_rates) &&
  868. (rcflag & ATH_RC_MINRATE_LASTRATE)) ? 1 : 0;
  869. nrix = ath_rc_rate_getidx(sc, ath_rc_priv,
  870. rate_table, nrix, 1, min_rate);
  871. /* All other rates in the series have RTS enabled */
  872. ath_rc_rate_set_series(rate_table,
  873. &series[i], try_num, nrix, TRUE);
  874. }
  875. /*
  876. * NB:Change rate series to enable aggregation when operating
  877. * at lower MCS rates. When first rate in series is MCS2
  878. * in HT40 @ 2.4GHz, series should look like:
  879. *
  880. * {MCS2, MCS1, MCS0, MCS0}.
  881. *
  882. * When first rate in series is MCS3 in HT20 @ 2.4GHz, series should
  883. * look like:
  884. *
  885. * {MCS3, MCS2, MCS1, MCS1}
  886. *
  887. * So, set fourth rate in series to be same as third one for
  888. * above conditions.
  889. */
  890. if ((sc->sc_curmode == ATH9K_MODE_11NG_HT20) ||
  891. (sc->sc_curmode == ATH9K_MODE_11NG_HT40PLUS) ||
  892. (sc->sc_curmode == ATH9K_MODE_11NG_HT40MINUS)) {
  893. u8 dot11rate = rate_table->info[rix].dot11rate;
  894. u8 phy = rate_table->info[rix].phy;
  895. if (i == 4 &&
  896. ((dot11rate == 2 && phy == WLAN_RC_PHY_HT_40_SS) ||
  897. (dot11rate == 3 && phy == WLAN_RC_PHY_HT_20_SS))) {
  898. series[3].rix = series[2].rix;
  899. series[3].flags = series[2].flags;
  900. series[3].max_4ms_framelen = series[2].max_4ms_framelen;
  901. }
  902. }
  903. }
  904. /*
  905. * Return the Tx rate series.
  906. */
  907. static void ath_rate_findrate(struct ath_softc *sc,
  908. struct ath_rate_node *ath_rc_priv,
  909. int num_tries,
  910. int num_rates,
  911. unsigned int rcflag,
  912. struct ath_rc_series series[],
  913. int *is_probe,
  914. int is_retry)
  915. {
  916. struct ath_vap *avp = ath_rc_priv->avp;
  917. DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
  918. if (!num_rates || !num_tries)
  919. return;
  920. if (avp->av_config.av_fixed_rateset == IEEE80211_FIXED_RATE_NONE) {
  921. ath_rc_ratefind(sc, ath_rc_priv, num_tries, num_rates,
  922. rcflag, series, is_probe, is_retry);
  923. } else {
  924. /* Fixed rate */
  925. int idx;
  926. u8 flags;
  927. u32 rix;
  928. struct ath_rate_softc *asc = ath_rc_priv->asc;
  929. struct ath_rate_table *rate_table;
  930. rate_table = (struct ath_rate_table *)
  931. asc->hw_rate_table[sc->sc_curmode];
  932. for (idx = 0; idx < 4; idx++) {
  933. unsigned int mcs;
  934. u8 series_rix = 0;
  935. series[idx].tries = IEEE80211_RATE_IDX_ENTRY(
  936. avp->av_config.av_fixed_retryset, idx);
  937. mcs = IEEE80211_RATE_IDX_ENTRY(
  938. avp->av_config.av_fixed_rateset, idx);
  939. if (idx == 3 && (mcs & 0xf0) == 0x70)
  940. mcs = (mcs & ~0xf0)|0x80;
  941. if (!(mcs & 0x80))
  942. flags = 0;
  943. else
  944. flags = ((ath_rc_priv->ht_cap &
  945. WLAN_RC_DS_FLAG) ?
  946. ATH_RC_DS_FLAG : 0) |
  947. ((ath_rc_priv->ht_cap &
  948. WLAN_RC_40_FLAG) ?
  949. ATH_RC_CW40_FLAG : 0) |
  950. ((ath_rc_priv->ht_cap &
  951. WLAN_RC_SGI_FLAG) ?
  952. ((ath_rc_priv->ht_cap &
  953. WLAN_RC_40_FLAG) ?
  954. ATH_RC_SGI_FLAG : 0) : 0);
  955. series[idx].rix = sc->sc_rixmap[mcs];
  956. series_rix = series[idx].rix;
  957. /* XXX: Give me some cleanup love */
  958. if ((flags & ATH_RC_CW40_FLAG) &&
  959. (flags & ATH_RC_SGI_FLAG))
  960. rix = rate_table->info[series_rix].ht_index;
  961. else if (flags & ATH_RC_SGI_FLAG)
  962. rix = rate_table->info[series_rix].sgi_index;
  963. else if (flags & ATH_RC_CW40_FLAG)
  964. rix = rate_table->info[series_rix].cw40index;
  965. else
  966. rix = rate_table->info[series_rix].base_index;
  967. series[idx].max_4ms_framelen =
  968. rate_table->info[rix].max_4ms_framelen;
  969. series[idx].flags = flags;
  970. }
  971. }
  972. }
  973. static void ath_rc_update_ht(struct ath_softc *sc,
  974. struct ath_rate_node *ath_rc_priv,
  975. struct ath_tx_info_priv *info_priv,
  976. int tx_rate, int xretries, int retries)
  977. {
  978. u32 now_msec = jiffies_to_msecs(jiffies);
  979. int state_change = FALSE, rate, count;
  980. u8 last_per;
  981. struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
  982. struct ath_rate_table *rate_table =
  983. (struct ath_rate_table *)asc->hw_rate_table[sc->sc_curmode];
  984. static u32 nretry_to_per_lookup[10] = {
  985. 100 * 0 / 1,
  986. 100 * 1 / 4,
  987. 100 * 1 / 2,
  988. 100 * 3 / 4,
  989. 100 * 4 / 5,
  990. 100 * 5 / 6,
  991. 100 * 6 / 7,
  992. 100 * 7 / 8,
  993. 100 * 8 / 9,
  994. 100 * 9 / 10
  995. };
  996. if (!ath_rc_priv)
  997. return;
  998. ASSERT(tx_rate >= 0);
  999. if (tx_rate < 0)
  1000. return;
  1001. /* To compensate for some imbalance between ctrl and ext. channel */
  1002. if (WLAN_RC_PHY_40(rate_table->info[tx_rate].phy))
  1003. info_priv->tx.ts_rssi =
  1004. info_priv->tx.ts_rssi < 3 ? 0 :
  1005. info_priv->tx.ts_rssi - 3;
  1006. last_per = ath_rc_priv->state[tx_rate].per;
  1007. if (xretries) {
  1008. /* Update the PER. */
  1009. if (xretries == 1) {
  1010. ath_rc_priv->state[tx_rate].per += 30;
  1011. if (ath_rc_priv->state[tx_rate].per > 100)
  1012. ath_rc_priv->state[tx_rate].per = 100;
  1013. } else {
  1014. /* xretries == 2 */
  1015. count = ARRAY_SIZE(nretry_to_per_lookup);
  1016. if (retries >= count)
  1017. retries = count - 1;
  1018. /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
  1019. ath_rc_priv->state[tx_rate].per =
  1020. (u8)(ath_rc_priv->state[tx_rate].per -
  1021. (ath_rc_priv->state[tx_rate].per >> 3) +
  1022. ((100) >> 3));
  1023. }
  1024. /* xretries == 1 or 2 */
  1025. if (ath_rc_priv->probe_rate == tx_rate)
  1026. ath_rc_priv->probe_rate = 0;
  1027. } else { /* xretries == 0 */
  1028. /* Update the PER. */
  1029. /* Make sure it doesn't index out of array's bounds. */
  1030. count = ARRAY_SIZE(nretry_to_per_lookup);
  1031. if (retries >= count)
  1032. retries = count - 1;
  1033. if (info_priv->n_bad_frames) {
  1034. /* new_PER = 7/8*old_PER + 1/8*(currentPER)
  1035. * Assuming that n_frames is not 0. The current PER
  1036. * from the retries is 100 * retries / (retries+1),
  1037. * since the first retries attempts failed, and the
  1038. * next one worked. For the one that worked,
  1039. * n_bad_frames subframes out of n_frames wored,
  1040. * so the PER for that part is
  1041. * 100 * n_bad_frames / n_frames, and it contributes
  1042. * 100 * n_bad_frames / (n_frames * (retries+1)) to
  1043. * the above PER. The expression below is a
  1044. * simplified version of the sum of these two terms.
  1045. */
  1046. if (info_priv->n_frames > 0)
  1047. ath_rc_priv->state[tx_rate].per
  1048. = (u8)
  1049. (ath_rc_priv->state[tx_rate].per -
  1050. (ath_rc_priv->state[tx_rate].per >> 3) +
  1051. ((100*(retries*info_priv->n_frames +
  1052. info_priv->n_bad_frames) /
  1053. (info_priv->n_frames *
  1054. (retries+1))) >> 3));
  1055. } else {
  1056. /* new_PER = 7/8*old_PER + 1/8*(currentPER) */
  1057. ath_rc_priv->state[tx_rate].per = (u8)
  1058. (ath_rc_priv->state[tx_rate].per -
  1059. (ath_rc_priv->state[tx_rate].per >> 3) +
  1060. (nretry_to_per_lookup[retries] >> 3));
  1061. }
  1062. ath_rc_priv->rssi_last_prev2 = ath_rc_priv->rssi_last_prev;
  1063. ath_rc_priv->rssi_last_prev = ath_rc_priv->rssi_last;
  1064. ath_rc_priv->rssi_last = info_priv->tx.ts_rssi;
  1065. ath_rc_priv->rssi_time = now_msec;
  1066. /*
  1067. * If we got at most one retry then increase the max rate if
  1068. * this was a probe. Otherwise, ignore the probe.
  1069. */
  1070. if (ath_rc_priv->probe_rate && ath_rc_priv->probe_rate == tx_rate) {
  1071. if (retries > 0 || 2 * info_priv->n_bad_frames >
  1072. info_priv->n_frames) {
  1073. /*
  1074. * Since we probed with just a single attempt,
  1075. * any retries means the probe failed. Also,
  1076. * if the attempt worked, but more than half
  1077. * the subframes were bad then also consider
  1078. * the probe a failure.
  1079. */
  1080. ath_rc_priv->probe_rate = 0;
  1081. } else {
  1082. u8 probe_rate = 0;
  1083. ath_rc_priv->rate_max_phy = ath_rc_priv->probe_rate;
  1084. probe_rate = ath_rc_priv->probe_rate;
  1085. if (ath_rc_priv->state[probe_rate].per > 30)
  1086. ath_rc_priv->state[probe_rate].per = 20;
  1087. ath_rc_priv->probe_rate = 0;
  1088. /*
  1089. * Since this probe succeeded, we allow the next
  1090. * probe twice as soon. This allows the maxRate
  1091. * to move up faster if the probes are
  1092. * succesful.
  1093. */
  1094. ath_rc_priv->probe_time = now_msec -
  1095. rate_table->probe_interval / 2;
  1096. }
  1097. }
  1098. if (retries > 0) {
  1099. /*
  1100. * Don't update anything. We don't know if
  1101. * this was because of collisions or poor signal.
  1102. *
  1103. * Later: if rssi_ack is close to
  1104. * ath_rc_priv->state[txRate].rssi_thres and we see lots
  1105. * of retries, then we could increase
  1106. * ath_rc_priv->state[txRate].rssi_thres.
  1107. */
  1108. ath_rc_priv->hw_maxretry_pktcnt = 0;
  1109. } else {
  1110. /*
  1111. * It worked with no retries. First ignore bogus (small)
  1112. * rssi_ack values.
  1113. */
  1114. if (tx_rate == ath_rc_priv->rate_max_phy &&
  1115. ath_rc_priv->hw_maxretry_pktcnt < 255) {
  1116. ath_rc_priv->hw_maxretry_pktcnt++;
  1117. }
  1118. if (info_priv->tx.ts_rssi >=
  1119. rate_table->info[tx_rate].rssi_ack_validmin) {
  1120. /* Average the rssi */
  1121. if (tx_rate != ath_rc_priv->rssi_sum_rate) {
  1122. ath_rc_priv->rssi_sum_rate = tx_rate;
  1123. ath_rc_priv->rssi_sum =
  1124. ath_rc_priv->rssi_sum_cnt = 0;
  1125. }
  1126. ath_rc_priv->rssi_sum += info_priv->tx.ts_rssi;
  1127. ath_rc_priv->rssi_sum_cnt++;
  1128. if (ath_rc_priv->rssi_sum_cnt > 4) {
  1129. int32_t rssi_ackAvg =
  1130. (ath_rc_priv->rssi_sum + 2) / 4;
  1131. int8_t rssi_thres =
  1132. ath_rc_priv->state[tx_rate].
  1133. rssi_thres;
  1134. int8_t rssi_ack_vmin =
  1135. rate_table->info[tx_rate].
  1136. rssi_ack_validmin;
  1137. ath_rc_priv->rssi_sum =
  1138. ath_rc_priv->rssi_sum_cnt = 0;
  1139. /* Now reduce the current
  1140. * rssi threshold. */
  1141. if ((rssi_ackAvg < rssi_thres + 2) &&
  1142. (rssi_thres > rssi_ack_vmin)) {
  1143. ath_rc_priv->state[tx_rate].
  1144. rssi_thres--;
  1145. }
  1146. state_change = TRUE;
  1147. }
  1148. }
  1149. }
  1150. }
  1151. /* For all cases */
  1152. /*
  1153. * If this rate looks bad (high PER) then stop using it for
  1154. * a while (except if we are probing).
  1155. */
  1156. if (ath_rc_priv->state[tx_rate].per >= 55 && tx_rate > 0 &&
  1157. rate_table->info[tx_rate].ratekbps <=
  1158. rate_table->info[ath_rc_priv->rate_max_phy].ratekbps) {
  1159. ath_rc_get_nextlowervalid_txrate(rate_table, ath_rc_priv,
  1160. (u8) tx_rate, &ath_rc_priv->rate_max_phy);
  1161. /* Don't probe for a little while. */
  1162. ath_rc_priv->probe_time = now_msec;
  1163. }
  1164. if (state_change) {
  1165. /*
  1166. * Make sure the rates above this have higher rssi thresholds.
  1167. * (Note: Monotonicity is kept within the OFDM rates and
  1168. * within the CCK rates. However, no adjustment is
  1169. * made to keep the rssi thresholds monotonically
  1170. * increasing between the CCK and OFDM rates.)
  1171. */
  1172. for (rate = tx_rate; rate <
  1173. ath_rc_priv->rate_table_size - 1; rate++) {
  1174. if (rate_table->info[rate+1].phy !=
  1175. rate_table->info[tx_rate].phy)
  1176. break;
  1177. if (ath_rc_priv->state[rate].rssi_thres +
  1178. rate_table->info[rate].rssi_ack_deltamin >
  1179. ath_rc_priv->state[rate+1].rssi_thres) {
  1180. ath_rc_priv->state[rate+1].rssi_thres =
  1181. ath_rc_priv->state[rate].
  1182. rssi_thres +
  1183. rate_table->info[rate].
  1184. rssi_ack_deltamin;
  1185. }
  1186. }
  1187. /* Make sure the rates below this have lower rssi thresholds. */
  1188. for (rate = tx_rate - 1; rate >= 0; rate--) {
  1189. if (rate_table->info[rate].phy !=
  1190. rate_table->info[tx_rate].phy)
  1191. break;
  1192. if (ath_rc_priv->state[rate].rssi_thres +
  1193. rate_table->info[rate].rssi_ack_deltamin >
  1194. ath_rc_priv->state[rate+1].rssi_thres) {
  1195. if (ath_rc_priv->state[rate+1].rssi_thres <
  1196. rate_table->info[rate].
  1197. rssi_ack_deltamin)
  1198. ath_rc_priv->state[rate].rssi_thres = 0;
  1199. else {
  1200. ath_rc_priv->state[rate].rssi_thres =
  1201. ath_rc_priv->state[rate+1].
  1202. rssi_thres -
  1203. rate_table->info[rate].
  1204. rssi_ack_deltamin;
  1205. }
  1206. if (ath_rc_priv->state[rate].rssi_thres <
  1207. rate_table->info[rate].
  1208. rssi_ack_validmin) {
  1209. ath_rc_priv->state[rate].rssi_thres =
  1210. rate_table->info[rate].
  1211. rssi_ack_validmin;
  1212. }
  1213. }
  1214. }
  1215. }
  1216. /* Make sure the rates below this have lower PER */
  1217. /* Monotonicity is kept only for rates below the current rate. */
  1218. if (ath_rc_priv->state[tx_rate].per < last_per) {
  1219. for (rate = tx_rate - 1; rate >= 0; rate--) {
  1220. if (rate_table->info[rate].phy !=
  1221. rate_table->info[tx_rate].phy)
  1222. break;
  1223. if (ath_rc_priv->state[rate].per >
  1224. ath_rc_priv->state[rate+1].per) {
  1225. ath_rc_priv->state[rate].per =
  1226. ath_rc_priv->state[rate+1].per;
  1227. }
  1228. }
  1229. }
  1230. /* Maintain monotonicity for rates above the current rate */
  1231. for (rate = tx_rate; rate < ath_rc_priv->rate_table_size - 1; rate++) {
  1232. if (ath_rc_priv->state[rate+1].per < ath_rc_priv->state[rate].per)
  1233. ath_rc_priv->state[rate+1].per =
  1234. ath_rc_priv->state[rate].per;
  1235. }
  1236. /* Every so often, we reduce the thresholds and
  1237. * PER (different for CCK and OFDM). */
  1238. if (now_msec - ath_rc_priv->rssi_down_time >=
  1239. rate_table->rssi_reduce_interval) {
  1240. for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
  1241. if (ath_rc_priv->state[rate].rssi_thres >
  1242. rate_table->info[rate].rssi_ack_validmin)
  1243. ath_rc_priv->state[rate].rssi_thres -= 1;
  1244. }
  1245. ath_rc_priv->rssi_down_time = now_msec;
  1246. }
  1247. /* Every so often, we reduce the thresholds
  1248. * and PER (different for CCK and OFDM). */
  1249. if (now_msec - ath_rc_priv->per_down_time >=
  1250. rate_table->rssi_reduce_interval) {
  1251. for (rate = 0; rate < ath_rc_priv->rate_table_size; rate++) {
  1252. ath_rc_priv->state[rate].per =
  1253. 7 * ath_rc_priv->state[rate].per / 8;
  1254. }
  1255. ath_rc_priv->per_down_time = now_msec;
  1256. }
  1257. }
  1258. /*
  1259. * This routine is called in rate control callback tx_status() to give
  1260. * the status of previous frames.
  1261. */
  1262. static void ath_rc_update(struct ath_softc *sc,
  1263. struct ath_rate_node *ath_rc_priv,
  1264. struct ath_tx_info_priv *info_priv, int final_ts_idx,
  1265. int xretries, int long_retry)
  1266. {
  1267. struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
  1268. struct ath_rate_table *rate_table;
  1269. struct ath_rc_series rcs[4];
  1270. u8 flags;
  1271. u32 series = 0, rix;
  1272. memcpy(rcs, info_priv->rcs, 4 * sizeof(rcs[0]));
  1273. rate_table = (struct ath_rate_table *)
  1274. asc->hw_rate_table[sc->sc_curmode];
  1275. ASSERT(rcs[0].tries != 0);
  1276. /*
  1277. * If the first rate is not the final index, there
  1278. * are intermediate rate failures to be processed.
  1279. */
  1280. if (final_ts_idx != 0) {
  1281. /* Process intermediate rates that failed.*/
  1282. for (series = 0; series < final_ts_idx ; series++) {
  1283. if (rcs[series].tries != 0) {
  1284. flags = rcs[series].flags;
  1285. /* If HT40 and we have switched mode from
  1286. * 40 to 20 => don't update */
  1287. if ((flags & ATH_RC_CW40_FLAG) &&
  1288. (ath_rc_priv->rc_phy_mode !=
  1289. (flags & ATH_RC_CW40_FLAG)))
  1290. return;
  1291. if ((flags & ATH_RC_CW40_FLAG) &&
  1292. (flags & ATH_RC_SGI_FLAG))
  1293. rix = rate_table->info[
  1294. rcs[series].rix].ht_index;
  1295. else if (flags & ATH_RC_SGI_FLAG)
  1296. rix = rate_table->info[
  1297. rcs[series].rix].sgi_index;
  1298. else if (flags & ATH_RC_CW40_FLAG)
  1299. rix = rate_table->info[
  1300. rcs[series].rix].cw40index;
  1301. else
  1302. rix = rate_table->info[
  1303. rcs[series].rix].base_index;
  1304. ath_rc_update_ht(sc, ath_rc_priv,
  1305. info_priv, rix,
  1306. xretries ? 1 : 2,
  1307. rcs[series].tries);
  1308. }
  1309. }
  1310. } else {
  1311. /*
  1312. * Handle the special case of MIMO PS burst, where the second
  1313. * aggregate is sent out with only one rate and one try.
  1314. * Treating it as an excessive retry penalizes the rate
  1315. * inordinately.
  1316. */
  1317. if (rcs[0].tries == 1 && xretries == 1)
  1318. xretries = 2;
  1319. }
  1320. flags = rcs[series].flags;
  1321. /* If HT40 and we have switched mode from 40 to 20 => don't update */
  1322. if ((flags & ATH_RC_CW40_FLAG) &&
  1323. (ath_rc_priv->rc_phy_mode != (flags & ATH_RC_CW40_FLAG)))
  1324. return;
  1325. if ((flags & ATH_RC_CW40_FLAG) && (flags & ATH_RC_SGI_FLAG))
  1326. rix = rate_table->info[rcs[series].rix].ht_index;
  1327. else if (flags & ATH_RC_SGI_FLAG)
  1328. rix = rate_table->info[rcs[series].rix].sgi_index;
  1329. else if (flags & ATH_RC_CW40_FLAG)
  1330. rix = rate_table->info[rcs[series].rix].cw40index;
  1331. else
  1332. rix = rate_table->info[rcs[series].rix].base_index;
  1333. ath_rc_update_ht(sc, ath_rc_priv, info_priv, rix,
  1334. xretries, long_retry);
  1335. }
  1336. /*
  1337. * Process a tx descriptor for a completed transmit (success or failure).
  1338. */
  1339. static void ath_rate_tx_complete(struct ath_softc *sc,
  1340. struct ath_node *an,
  1341. struct ath_rate_node *rc_priv,
  1342. struct ath_tx_info_priv *info_priv)
  1343. {
  1344. int final_ts_idx = info_priv->tx.ts_rateindex;
  1345. int tx_status = 0, is_underrun = 0;
  1346. struct ath_vap *avp;
  1347. avp = rc_priv->avp;
  1348. if ((avp->av_config.av_fixed_rateset != IEEE80211_FIXED_RATE_NONE) ||
  1349. (info_priv->tx.ts_status & ATH9K_TXERR_FILT))
  1350. return;
  1351. if (info_priv->tx.ts_rssi > 0) {
  1352. ATH_RSSI_LPF(an->an_chainmask_sel.tx_avgrssi,
  1353. info_priv->tx.ts_rssi);
  1354. }
  1355. /*
  1356. * If underrun error is seen assume it as an excessive retry only
  1357. * if prefetch trigger level have reached the max (0x3f for 5416)
  1358. * Adjust the long retry as if the frame was tried ATH_11N_TXMAXTRY
  1359. * times. This affects how ratectrl updates PER for the failed rate.
  1360. */
  1361. if (info_priv->tx.ts_flags &
  1362. (ATH9K_TX_DATA_UNDERRUN | ATH9K_TX_DELIM_UNDERRUN) &&
  1363. ((sc->sc_ah->ah_txTrigLevel) >= tx_triglevel_max)) {
  1364. tx_status = 1;
  1365. is_underrun = 1;
  1366. }
  1367. if ((info_priv->tx.ts_status & ATH9K_TXERR_XRETRY) ||
  1368. (info_priv->tx.ts_status & ATH9K_TXERR_FIFO))
  1369. tx_status = 1;
  1370. ath_rc_update(sc, rc_priv, info_priv, final_ts_idx, tx_status,
  1371. (is_underrun) ? ATH_11N_TXMAXTRY :
  1372. info_priv->tx.ts_longretry);
  1373. }
  1374. /*
  1375. * Update the SIB's rate control information
  1376. *
  1377. * This should be called when the supported rates change
  1378. * (e.g. SME operation, wireless mode change)
  1379. *
  1380. * It will determine which rates are valid for use.
  1381. */
  1382. static void ath_rc_sib_update(struct ath_softc *sc,
  1383. struct ath_rate_node *ath_rc_priv,
  1384. u32 capflag, int keep_state,
  1385. struct ath_rateset *negotiated_rates,
  1386. struct ath_rateset *negotiated_htrates)
  1387. {
  1388. struct ath_rate_table *rate_table = NULL;
  1389. struct ath_rate_softc *asc = (struct ath_rate_softc *)sc->sc_rc;
  1390. struct ath_rateset *rateset = negotiated_rates;
  1391. u8 *ht_mcs = (u8 *)negotiated_htrates;
  1392. u8 i, j, k, hi = 0, hthi = 0;
  1393. rate_table = (struct ath_rate_table *)
  1394. asc->hw_rate_table[sc->sc_curmode];
  1395. /* Initial rate table size. Will change depending
  1396. * on the working rate set */
  1397. ath_rc_priv->rate_table_size = MAX_TX_RATE_TBL;
  1398. /* Initialize thresholds according to the global rate table */
  1399. for (i = 0 ; (i < ath_rc_priv->rate_table_size) && (!keep_state); i++) {
  1400. ath_rc_priv->state[i].rssi_thres =
  1401. rate_table->info[i].rssi_ack_validmin;
  1402. ath_rc_priv->state[i].per = 0;
  1403. }
  1404. /* Determine the valid rates */
  1405. ath_rc_init_valid_txmask(ath_rc_priv);
  1406. for (i = 0; i < WLAN_RC_PHY_MAX; i++) {
  1407. for (j = 0; j < MAX_TX_RATE_PHY; j++)
  1408. ath_rc_priv->valid_phy_rateidx[i][j] = 0;
  1409. ath_rc_priv->valid_phy_ratecnt[i] = 0;
  1410. }
  1411. ath_rc_priv->rc_phy_mode = (capflag & WLAN_RC_40_FLAG);
  1412. /* Set stream capability */
  1413. ath_rc_priv->single_stream = (capflag & WLAN_RC_DS_FLAG) ? 0 : 1;
  1414. if (!rateset->rs_nrates) {
  1415. /* No working rate, just initialize valid rates */
  1416. hi = ath_rc_sib_init_validrates(ath_rc_priv, rate_table,
  1417. capflag);
  1418. } else {
  1419. /* Use intersection of working rates and valid rates */
  1420. hi = ath_rc_sib_setvalid_rates(ath_rc_priv, rate_table,
  1421. rateset, capflag);
  1422. if (capflag & WLAN_RC_HT_FLAG) {
  1423. hthi = ath_rc_sib_setvalid_htrates(ath_rc_priv,
  1424. rate_table,
  1425. ht_mcs,
  1426. capflag);
  1427. }
  1428. hi = A_MAX(hi, hthi);
  1429. }
  1430. ath_rc_priv->rate_table_size = hi + 1;
  1431. ath_rc_priv->rate_max_phy = 0;
  1432. ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
  1433. for (i = 0, k = 0; i < WLAN_RC_PHY_MAX; i++) {
  1434. for (j = 0; j < ath_rc_priv->valid_phy_ratecnt[i]; j++) {
  1435. ath_rc_priv->valid_rate_index[k++] =
  1436. ath_rc_priv->valid_phy_rateidx[i][j];
  1437. }
  1438. if (!ath_rc_valid_phyrate(i, rate_table->initial_ratemax, TRUE)
  1439. || !ath_rc_priv->valid_phy_ratecnt[i])
  1440. continue;
  1441. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_phy_rateidx[i][j-1];
  1442. }
  1443. ASSERT(ath_rc_priv->rate_table_size <= MAX_TX_RATE_TBL);
  1444. ASSERT(k <= MAX_TX_RATE_TBL);
  1445. ath_rc_priv->max_valid_rate = k;
  1446. /*
  1447. * Some third party vendors don't send the supported rate series in
  1448. * order. So sorting to make sure its in order, otherwise our RateFind
  1449. * Algo will select wrong rates
  1450. */
  1451. ath_rc_sort_validrates(rate_table, ath_rc_priv);
  1452. ath_rc_priv->rate_max_phy = ath_rc_priv->valid_rate_index[k-4];
  1453. }
  1454. void ath_rc_node_update(struct ieee80211_hw *hw, struct ath_rate_node *rc_priv)
  1455. {
  1456. struct ath_softc *sc = hw->priv;
  1457. u32 capflag = 0;
  1458. if (hw->conf.ht.enabled) {
  1459. capflag |= ATH_RC_HT_FLAG | ATH_RC_DS_FLAG;
  1460. if (sc->sc_ht_info.tx_chan_width == ATH9K_HT_MACMODE_2040)
  1461. capflag |= ATH_RC_CW40_FLAG;
  1462. }
  1463. rc_priv->ht_cap =
  1464. ((capflag & ATH_RC_DS_FLAG) ? WLAN_RC_DS_FLAG : 0) |
  1465. ((capflag & ATH_RC_SGI_FLAG) ? WLAN_RC_SGI_FLAG : 0) |
  1466. ((capflag & ATH_RC_HT_FLAG) ? WLAN_RC_HT_FLAG : 0) |
  1467. ((capflag & ATH_RC_CW40_FLAG) ? WLAN_RC_40_FLAG : 0);
  1468. ath_rc_sib_update(sc, rc_priv, rc_priv->ht_cap, 0,
  1469. &rc_priv->neg_rates,
  1470. &rc_priv->neg_ht_rates);
  1471. }
  1472. /* Rate Control callbacks */
  1473. static void ath_tx_status(void *priv, struct ieee80211_supported_band *sband,
  1474. struct ieee80211_sta *sta, void *priv_sta,
  1475. struct sk_buff *skb)
  1476. {
  1477. struct ath_softc *sc = priv;
  1478. struct ath_tx_info_priv *tx_info_priv;
  1479. struct ath_node *an;
  1480. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1481. struct ieee80211_hdr *hdr;
  1482. __le16 fc;
  1483. hdr = (struct ieee80211_hdr *)skb->data;
  1484. fc = hdr->frame_control;
  1485. /* XXX: UGLY HACK!! */
  1486. tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
  1487. an = (struct ath_node *)sta->drv_priv;
  1488. if (tx_info_priv == NULL)
  1489. return;
  1490. if (an && priv_sta && ieee80211_is_data(fc))
  1491. ath_rate_tx_complete(sc, an, priv_sta, tx_info_priv);
  1492. kfree(tx_info_priv);
  1493. tx_info->control.vif = NULL;
  1494. }
  1495. static void ath_get_rate(void *priv, struct ieee80211_sta *sta, void *priv_sta,
  1496. struct ieee80211_tx_rate_control *txrc)
  1497. {
  1498. struct ieee80211_supported_band *sband = txrc->sband;
  1499. struct sk_buff *skb = txrc->skb;
  1500. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1501. struct ath_softc *sc = priv;
  1502. struct ieee80211_hw *hw = sc->hw;
  1503. struct ath_tx_info_priv *tx_info_priv;
  1504. struct ath_rate_node *ath_rc_priv = priv_sta;
  1505. struct ath_node *an;
  1506. struct ieee80211_tx_info *tx_info = IEEE80211_SKB_CB(skb);
  1507. int is_probe = FALSE;
  1508. s8 lowest_idx;
  1509. __le16 fc = hdr->frame_control;
  1510. u8 *qc, tid;
  1511. DPRINTF(sc, ATH_DBG_RATE, "%s\n", __func__);
  1512. /* allocate driver private area of tx_info, XXX: UGLY HACK! */
  1513. tx_info->control.vif = kzalloc(sizeof(*tx_info_priv), GFP_ATOMIC);
  1514. tx_info_priv = (struct ath_tx_info_priv *)tx_info->control.vif;
  1515. ASSERT(tx_info_priv != NULL);
  1516. lowest_idx = rate_lowest_index(sband, sta);
  1517. tx_info_priv->min_rate = (sband->bitrates[lowest_idx].bitrate * 2) / 10;
  1518. /* lowest rate for management and multicast/broadcast frames */
  1519. if (!ieee80211_is_data(fc) ||
  1520. is_multicast_ether_addr(hdr->addr1) || !sta) {
  1521. tx_info->control.rates[0].idx = lowest_idx;
  1522. return;
  1523. }
  1524. /* Find tx rate for unicast frames */
  1525. ath_rate_findrate(sc, ath_rc_priv,
  1526. ATH_11N_TXMAXTRY, 4,
  1527. ATH_RC_PROBE_ALLOWED,
  1528. tx_info_priv->rcs,
  1529. &is_probe,
  1530. false);
  1531. #if 0
  1532. if (is_probe)
  1533. sel->probe_idx = ath_rc_priv->tx_ratectrl.probe_rate;
  1534. #endif
  1535. /* Ratecontrol sometimes returns invalid rate index */
  1536. if (tx_info_priv->rcs[0].rix != 0xff)
  1537. ath_rc_priv->prev_data_rix = tx_info_priv->rcs[0].rix;
  1538. else
  1539. tx_info_priv->rcs[0].rix = ath_rc_priv->prev_data_rix;
  1540. tx_info->control.rates[0].idx = tx_info_priv->rcs[0].rix;
  1541. /* Check if aggregation has to be enabled for this tid */
  1542. if (hw->conf.ht.enabled) {
  1543. if (ieee80211_is_data_qos(fc)) {
  1544. qc = ieee80211_get_qos_ctl(hdr);
  1545. tid = qc[0] & 0xf;
  1546. an = (struct ath_node *)sta->drv_priv;
  1547. if(ath_tx_aggr_check(sc, an, tid))
  1548. ieee80211_start_tx_ba_session(hw, hdr->addr1, tid);
  1549. }
  1550. }
  1551. }
  1552. static void ath_rate_init(void *priv, struct ieee80211_supported_band *sband,
  1553. struct ieee80211_sta *sta, void *priv_sta)
  1554. {
  1555. struct ath_softc *sc = priv;
  1556. struct ath_rate_node *ath_rc_priv = priv_sta;
  1557. int i, j = 0;
  1558. for (i = 0; i < sband->n_bitrates; i++) {
  1559. if (sta->supp_rates[sband->band] & BIT(i)) {
  1560. ath_rc_priv->neg_rates.rs_rates[j]
  1561. = (sband->bitrates[i].bitrate * 2) / 10;
  1562. j++;
  1563. }
  1564. }
  1565. ath_rc_priv->neg_rates.rs_nrates = j;
  1566. if (sta->ht_cap.ht_supported) {
  1567. for (i = 0, j = 0; i < 77; i++) {
  1568. if (sta->ht_cap.mcs.rx_mask[i/8] & (1<<(i%8)))
  1569. ath_rc_priv->neg_ht_rates.rs_rates[j++] = i;
  1570. if (j == ATH_RATE_MAX)
  1571. break;
  1572. }
  1573. ath_rc_priv->neg_ht_rates.rs_nrates = j;
  1574. }
  1575. ath_rc_node_update(sc->hw, priv_sta);
  1576. }
  1577. static void *ath_rate_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  1578. {
  1579. return hw->priv;
  1580. }
  1581. static void ath_rate_free(void *priv)
  1582. {
  1583. return;
  1584. }
  1585. static void *ath_rate_alloc_sta(void *priv, struct ieee80211_sta *sta, gfp_t gfp)
  1586. {
  1587. struct ieee80211_vif *vif;
  1588. struct ath_softc *sc = priv;
  1589. struct ath_vap *avp;
  1590. struct ath_rate_node *rate_priv;
  1591. vif = sc->sc_vaps[0];
  1592. ASSERT(vif);
  1593. avp = (void *)vif->drv_priv;
  1594. rate_priv = kzalloc(sizeof(struct ath_rate_node), gfp);
  1595. if (!rate_priv) {
  1596. DPRINTF(sc, ATH_DBG_FATAL,
  1597. "%s: Unable to allocate private rc structure\n",
  1598. __func__);
  1599. return NULL;
  1600. }
  1601. rate_priv->avp = avp;
  1602. rate_priv->asc = sc->sc_rc;
  1603. avp->rc_node = rate_priv;
  1604. rate_priv->rssi_down_time = jiffies_to_msecs(jiffies);
  1605. return rate_priv;
  1606. }
  1607. static void ath_rate_free_sta(void *priv, struct ieee80211_sta *sta,
  1608. void *priv_sta)
  1609. {
  1610. struct ath_rate_node *rate_priv = priv_sta;
  1611. kfree(rate_priv);
  1612. }
  1613. static struct rate_control_ops ath_rate_ops = {
  1614. .module = NULL,
  1615. .name = "ath9k_rate_control",
  1616. .tx_status = ath_tx_status,
  1617. .get_rate = ath_get_rate,
  1618. .rate_init = ath_rate_init,
  1619. .alloc = ath_rate_alloc,
  1620. .free = ath_rate_free,
  1621. .alloc_sta = ath_rate_alloc_sta,
  1622. .free_sta = ath_rate_free_sta,
  1623. };
  1624. int ath_rate_control_register(void)
  1625. {
  1626. return ieee80211_rate_control_register(&ath_rate_ops);
  1627. }
  1628. void ath_rate_control_unregister(void)
  1629. {
  1630. ieee80211_rate_control_unregister(&ath_rate_ops);
  1631. }