rc.c 50 KB

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