4965-rs.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/slab.h>
  30. #include <net/mac80211.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "iwl-dev.h"
  36. #include "iwl-sta.h"
  37. #include "iwl-core.h"
  38. #include "4965.h"
  39. #define IL4965_RS_NAME "iwl-4965-rs"
  40. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  41. #define IL_NUMBER_TRY 1
  42. #define IL_HT_NUMBER_TRY 3
  43. #define RATE_MAX_WINDOW 62 /* # tx in history win */
  44. #define RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
  45. #define RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  46. /* max allowed rate miss before sync LQ cmd */
  47. #define IL_MISSED_RATE_MAX 15
  48. /* max time to accum history 2 seconds */
  49. #define RATE_SCALE_FLUSH_INTVL (3*HZ)
  50. static u8 rs_ht_to_legacy[] = {
  51. RATE_6M_IDX, RATE_6M_IDX,
  52. RATE_6M_IDX, RATE_6M_IDX,
  53. RATE_6M_IDX,
  54. RATE_6M_IDX, RATE_9M_IDX,
  55. RATE_12M_IDX, RATE_18M_IDX,
  56. RATE_24M_IDX, RATE_36M_IDX,
  57. RATE_48M_IDX, RATE_54M_IDX
  58. };
  59. static const u8 ant_toggle_lookup[] = {
  60. /*ANT_NONE -> */ ANT_NONE,
  61. /*ANT_A -> */ ANT_B,
  62. /*ANT_B -> */ ANT_C,
  63. /*ANT_AB -> */ ANT_BC,
  64. /*ANT_C -> */ ANT_A,
  65. /*ANT_AC -> */ ANT_AB,
  66. /*ANT_BC -> */ ANT_AC,
  67. /*ANT_ABC -> */ ANT_ABC,
  68. };
  69. #define IL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  70. [RATE_##r##M_IDX] = { RATE_##r##M_PLCP, \
  71. RATE_SISO_##s##M_PLCP, \
  72. RATE_MIMO2_##s##M_PLCP,\
  73. RATE_##r##M_IEEE, \
  74. RATE_##ip##M_IDX, \
  75. RATE_##in##M_IDX, \
  76. RATE_##rp##M_IDX, \
  77. RATE_##rn##M_IDX, \
  78. RATE_##pp##M_IDX, \
  79. RATE_##np##M_IDX }
  80. /*
  81. * Parameter order:
  82. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  83. *
  84. * If there isn't a valid next or previous rate then INV is used which
  85. * maps to RATE_INVALID
  86. *
  87. */
  88. const struct il_rate_info il_rates[RATE_COUNT] = {
  89. IL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  90. IL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  91. IL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  92. IL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  93. IL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  94. IL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  95. IL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  96. IL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  97. IL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  98. IL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  99. IL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  100. IL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  101. IL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  102. };
  103. static int il4965_hwrate_to_plcp_idx(u32 rate_n_flags)
  104. {
  105. int idx = 0;
  106. /* HT rate format */
  107. if (rate_n_flags & RATE_MCS_HT_MSK) {
  108. idx = (rate_n_flags & 0xff);
  109. if (idx >= RATE_MIMO2_6M_PLCP)
  110. idx = idx - RATE_MIMO2_6M_PLCP;
  111. idx += IL_FIRST_OFDM_RATE;
  112. /* skip 9M not supported in ht*/
  113. if (idx >= RATE_9M_IDX)
  114. idx += 1;
  115. if (idx >= IL_FIRST_OFDM_RATE && idx <= IL_LAST_OFDM_RATE)
  116. return idx;
  117. /* legacy rate format, search for match in table */
  118. } else {
  119. for (idx = 0; idx < ARRAY_SIZE(il_rates); idx++)
  120. if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
  121. return idx;
  122. }
  123. return -1;
  124. }
  125. static void il4965_rs_rate_scale_perform(struct il_priv *il,
  126. struct sk_buff *skb,
  127. struct ieee80211_sta *sta,
  128. struct il_lq_sta *lq_sta);
  129. static void il4965_rs_fill_link_cmd(struct il_priv *il,
  130. struct il_lq_sta *lq_sta, u32 rate_n_flags);
  131. static void il4965_rs_stay_in_table(struct il_lq_sta *lq_sta,
  132. bool force_search);
  133. #ifdef CONFIG_MAC80211_DEBUGFS
  134. static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
  135. u32 *rate_n_flags, int idx);
  136. #else
  137. static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
  138. u32 *rate_n_flags, int idx)
  139. {}
  140. #endif
  141. /**
  142. * The following tables contain the expected throughput metrics for all rates
  143. *
  144. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  145. *
  146. * where invalid entries are zeros.
  147. *
  148. * CCK rates are only valid in legacy table and will only be used in G
  149. * (2.4 GHz) band.
  150. */
  151. static s32 expected_tpt_legacy[RATE_COUNT] = {
  152. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
  153. };
  154. static s32 expected_tpt_siso20MHz[4][RATE_COUNT] = {
  155. {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
  156. {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
  157. {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
  158. {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
  159. };
  160. static s32 expected_tpt_siso40MHz[4][RATE_COUNT] = {
  161. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
  162. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
  163. {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
  164. {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
  165. };
  166. static s32 expected_tpt_mimo2_20MHz[4][RATE_COUNT] = {
  167. {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
  168. {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
  169. {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
  170. {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI*/
  171. };
  172. static s32 expected_tpt_mimo2_40MHz[4][RATE_COUNT] = {
  173. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
  174. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
  175. {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
  176. {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
  177. };
  178. /* mbps, mcs */
  179. static const struct il_rate_mcs_info il_rate_mcs[RATE_COUNT] = {
  180. { "1", "BPSK DSSS"},
  181. { "2", "QPSK DSSS"},
  182. {"5.5", "BPSK CCK"},
  183. { "11", "QPSK CCK"},
  184. { "6", "BPSK 1/2"},
  185. { "9", "BPSK 1/2"},
  186. { "12", "QPSK 1/2"},
  187. { "18", "QPSK 3/4"},
  188. { "24", "16QAM 1/2"},
  189. { "36", "16QAM 3/4"},
  190. { "48", "64QAM 2/3"},
  191. { "54", "64QAM 3/4"},
  192. { "60", "64QAM 5/6"},
  193. };
  194. #define MCS_IDX_PER_STREAM (8)
  195. static inline u8 il4965_rs_extract_rate(u32 rate_n_flags)
  196. {
  197. return (u8)(rate_n_flags & 0xFF);
  198. }
  199. static void
  200. il4965_rs_rate_scale_clear_win(struct il_rate_scale_data *win)
  201. {
  202. win->data = 0;
  203. win->success_counter = 0;
  204. win->success_ratio = IL_INVALID_VALUE;
  205. win->counter = 0;
  206. win->average_tpt = IL_INVALID_VALUE;
  207. win->stamp = 0;
  208. }
  209. static inline u8 il4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  210. {
  211. return (ant_type & valid_antenna) == ant_type;
  212. }
  213. /*
  214. * removes the old data from the stats. All data that is older than
  215. * TID_MAX_TIME_DIFF, will be deleted.
  216. */
  217. static void
  218. il4965_rs_tl_rm_old_stats(struct il_traffic_load *tl, u32 curr_time)
  219. {
  220. /* The oldest age we want to keep */
  221. u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
  222. while (tl->queue_count && tl->time_stamp < oldest_time) {
  223. tl->total -= tl->packet_count[tl->head];
  224. tl->packet_count[tl->head] = 0;
  225. tl->time_stamp += TID_QUEUE_CELL_SPACING;
  226. tl->queue_count--;
  227. tl->head++;
  228. if (tl->head >= TID_QUEUE_MAX_SIZE)
  229. tl->head = 0;
  230. }
  231. }
  232. /*
  233. * increment traffic load value for tid and also remove
  234. * any old values if passed the certain time period
  235. */
  236. static u8 il4965_rs_tl_add_packet(struct il_lq_sta *lq_data,
  237. struct ieee80211_hdr *hdr)
  238. {
  239. u32 curr_time = jiffies_to_msecs(jiffies);
  240. u32 time_diff;
  241. s32 idx;
  242. struct il_traffic_load *tl = NULL;
  243. u8 tid;
  244. if (ieee80211_is_data_qos(hdr->frame_control)) {
  245. u8 *qc = ieee80211_get_qos_ctl(hdr);
  246. tid = qc[0] & 0xf;
  247. } else
  248. return MAX_TID_COUNT;
  249. if (unlikely(tid >= TID_MAX_LOAD_COUNT))
  250. return MAX_TID_COUNT;
  251. tl = &lq_data->load[tid];
  252. curr_time -= curr_time % TID_ROUND_VALUE;
  253. /* Happens only for the first packet. Initialize the data */
  254. if (!(tl->queue_count)) {
  255. tl->total = 1;
  256. tl->time_stamp = curr_time;
  257. tl->queue_count = 1;
  258. tl->head = 0;
  259. tl->packet_count[0] = 1;
  260. return MAX_TID_COUNT;
  261. }
  262. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  263. idx = time_diff / TID_QUEUE_CELL_SPACING;
  264. /* The history is too long: remove data that is older than */
  265. /* TID_MAX_TIME_DIFF */
  266. if (idx >= TID_QUEUE_MAX_SIZE)
  267. il4965_rs_tl_rm_old_stats(tl, curr_time);
  268. idx = (tl->head + idx) % TID_QUEUE_MAX_SIZE;
  269. tl->packet_count[idx] = tl->packet_count[idx] + 1;
  270. tl->total = tl->total + 1;
  271. if ((idx + 1) > tl->queue_count)
  272. tl->queue_count = idx + 1;
  273. return tid;
  274. }
  275. /*
  276. get the traffic load value for tid
  277. */
  278. static u32 il4965_rs_tl_get_load(struct il_lq_sta *lq_data, u8 tid)
  279. {
  280. u32 curr_time = jiffies_to_msecs(jiffies);
  281. u32 time_diff;
  282. s32 idx;
  283. struct il_traffic_load *tl = NULL;
  284. if (tid >= TID_MAX_LOAD_COUNT)
  285. return 0;
  286. tl = &(lq_data->load[tid]);
  287. curr_time -= curr_time % TID_ROUND_VALUE;
  288. if (!(tl->queue_count))
  289. return 0;
  290. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  291. idx = time_diff / TID_QUEUE_CELL_SPACING;
  292. /* The history is too long: remove data that is older than */
  293. /* TID_MAX_TIME_DIFF */
  294. if (idx >= TID_QUEUE_MAX_SIZE)
  295. il4965_rs_tl_rm_old_stats(tl, curr_time);
  296. return tl->total;
  297. }
  298. static int il4965_rs_tl_turn_on_agg_for_tid(struct il_priv *il,
  299. struct il_lq_sta *lq_data, u8 tid,
  300. struct ieee80211_sta *sta)
  301. {
  302. int ret = -EAGAIN;
  303. u32 load;
  304. load = il4965_rs_tl_get_load(lq_data, tid);
  305. if (load > IL_AGG_LOAD_THRESHOLD) {
  306. D_HT("Starting Tx agg: STA: %pM tid: %d\n",
  307. sta->addr, tid);
  308. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  309. if (ret == -EAGAIN) {
  310. /*
  311. * driver and mac80211 is out of sync
  312. * this might be cause by reloading firmware
  313. * stop the tx ba session here
  314. */
  315. IL_ERR("Fail start Tx agg on tid: %d\n",
  316. tid);
  317. ieee80211_stop_tx_ba_session(sta, tid);
  318. }
  319. } else {
  320. IL_ERR("Aggregation not enabled for tid %d "
  321. "because load = %u\n", tid, load);
  322. }
  323. return ret;
  324. }
  325. static void il4965_rs_tl_turn_on_agg(struct il_priv *il, u8 tid,
  326. struct il_lq_sta *lq_data,
  327. struct ieee80211_sta *sta)
  328. {
  329. if (tid < TID_MAX_LOAD_COUNT)
  330. il4965_rs_tl_turn_on_agg_for_tid(il, lq_data, tid, sta);
  331. else
  332. IL_ERR("tid exceeds max load count: %d/%d\n",
  333. tid, TID_MAX_LOAD_COUNT);
  334. }
  335. static inline int il4965_get_il4965_num_of_ant_from_rate(u32 rate_n_flags)
  336. {
  337. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  338. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  339. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  340. }
  341. /*
  342. * Static function to get the expected throughput from an il_scale_tbl_info
  343. * that wraps a NULL pointer check
  344. */
  345. static s32
  346. il4965_get_expected_tpt(struct il_scale_tbl_info *tbl, int rs_idx)
  347. {
  348. if (tbl->expected_tpt)
  349. return tbl->expected_tpt[rs_idx];
  350. return 0;
  351. }
  352. /**
  353. * il4965_rs_collect_tx_data - Update the success/failure sliding win
  354. *
  355. * We keep a sliding win of the last 62 packets transmitted
  356. * at this rate. win->data contains the bitmask of successful
  357. * packets.
  358. */
  359. static int il4965_rs_collect_tx_data(struct il_scale_tbl_info *tbl,
  360. int scale_idx, int attempts, int successes)
  361. {
  362. struct il_rate_scale_data *win = NULL;
  363. static const u64 mask = (((u64)1) << (RATE_MAX_WINDOW - 1));
  364. s32 fail_count, tpt;
  365. if (scale_idx < 0 || scale_idx >= RATE_COUNT)
  366. return -EINVAL;
  367. /* Select win for current tx bit rate */
  368. win = &(tbl->win[scale_idx]);
  369. /* Get expected throughput */
  370. tpt = il4965_get_expected_tpt(tbl, scale_idx);
  371. /*
  372. * Keep track of only the latest 62 tx frame attempts in this rate's
  373. * history win; anything older isn't really relevant any more.
  374. * If we have filled up the sliding win, drop the oldest attempt;
  375. * if the oldest attempt (highest bit in bitmap) shows "success",
  376. * subtract "1" from the success counter (this is the main reason
  377. * we keep these bitmaps!).
  378. */
  379. while (attempts > 0) {
  380. if (win->counter >= RATE_MAX_WINDOW) {
  381. /* remove earliest */
  382. win->counter = RATE_MAX_WINDOW - 1;
  383. if (win->data & mask) {
  384. win->data &= ~mask;
  385. win->success_counter--;
  386. }
  387. }
  388. /* Increment frames-attempted counter */
  389. win->counter++;
  390. /* Shift bitmap by one frame to throw away oldest history */
  391. win->data <<= 1;
  392. /* Mark the most recent #successes attempts as successful */
  393. if (successes > 0) {
  394. win->success_counter++;
  395. win->data |= 0x1;
  396. successes--;
  397. }
  398. attempts--;
  399. }
  400. /* Calculate current success ratio, avoid divide-by-0! */
  401. if (win->counter > 0)
  402. win->success_ratio = 128 * (100 * win->success_counter)
  403. / win->counter;
  404. else
  405. win->success_ratio = IL_INVALID_VALUE;
  406. fail_count = win->counter - win->success_counter;
  407. /* Calculate average throughput, if we have enough history. */
  408. if (fail_count >= RATE_MIN_FAILURE_TH ||
  409. win->success_counter >= RATE_MIN_SUCCESS_TH)
  410. win->average_tpt = (win->success_ratio * tpt + 64) / 128;
  411. else
  412. win->average_tpt = IL_INVALID_VALUE;
  413. /* Tag this win as having been updated */
  414. win->stamp = jiffies;
  415. return 0;
  416. }
  417. /*
  418. * Fill uCode API rate_n_flags field, based on "search" or "active" table.
  419. */
  420. static u32 il4965_rate_n_flags_from_tbl(struct il_priv *il,
  421. struct il_scale_tbl_info *tbl,
  422. int idx, u8 use_green)
  423. {
  424. u32 rate_n_flags = 0;
  425. if (is_legacy(tbl->lq_type)) {
  426. rate_n_flags = il_rates[idx].plcp;
  427. if (idx >= IL_FIRST_CCK_RATE && idx <= IL_LAST_CCK_RATE)
  428. rate_n_flags |= RATE_MCS_CCK_MSK;
  429. } else if (is_Ht(tbl->lq_type)) {
  430. if (idx > IL_LAST_OFDM_RATE) {
  431. IL_ERR("Invalid HT rate idx %d\n", idx);
  432. idx = IL_LAST_OFDM_RATE;
  433. }
  434. rate_n_flags = RATE_MCS_HT_MSK;
  435. if (is_siso(tbl->lq_type))
  436. rate_n_flags |= il_rates[idx].plcp_siso;
  437. else
  438. rate_n_flags |= il_rates[idx].plcp_mimo2;
  439. } else {
  440. IL_ERR("Invalid tbl->lq_type %d\n", tbl->lq_type);
  441. }
  442. rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
  443. RATE_MCS_ANT_ABC_MSK);
  444. if (is_Ht(tbl->lq_type)) {
  445. if (tbl->is_ht40) {
  446. if (tbl->is_dup)
  447. rate_n_flags |= RATE_MCS_DUP_MSK;
  448. else
  449. rate_n_flags |= RATE_MCS_HT40_MSK;
  450. }
  451. if (tbl->is_SGI)
  452. rate_n_flags |= RATE_MCS_SGI_MSK;
  453. if (use_green) {
  454. rate_n_flags |= RATE_MCS_GF_MSK;
  455. if (is_siso(tbl->lq_type) && tbl->is_SGI) {
  456. rate_n_flags &= ~RATE_MCS_SGI_MSK;
  457. IL_ERR("GF was set with SGI:SISO\n");
  458. }
  459. }
  460. }
  461. return rate_n_flags;
  462. }
  463. /*
  464. * Interpret uCode API's rate_n_flags format,
  465. * fill "search" or "active" tx mode table.
  466. */
  467. static int il4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
  468. enum ieee80211_band band,
  469. struct il_scale_tbl_info *tbl,
  470. int *rate_idx)
  471. {
  472. u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
  473. u8 il4965_num_of_ant = il4965_get_il4965_num_of_ant_from_rate(rate_n_flags);
  474. u8 mcs;
  475. memset(tbl, 0, sizeof(struct il_scale_tbl_info));
  476. *rate_idx = il4965_hwrate_to_plcp_idx(rate_n_flags);
  477. if (*rate_idx == RATE_INVALID) {
  478. *rate_idx = -1;
  479. return -EINVAL;
  480. }
  481. tbl->is_SGI = 0; /* default legacy setup */
  482. tbl->is_ht40 = 0;
  483. tbl->is_dup = 0;
  484. tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
  485. tbl->lq_type = LQ_NONE;
  486. tbl->max_search = IL_MAX_SEARCH;
  487. /* legacy rate format */
  488. if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
  489. if (il4965_num_of_ant == 1) {
  490. if (band == IEEE80211_BAND_5GHZ)
  491. tbl->lq_type = LQ_A;
  492. else
  493. tbl->lq_type = LQ_G;
  494. }
  495. /* HT rate format */
  496. } else {
  497. if (rate_n_flags & RATE_MCS_SGI_MSK)
  498. tbl->is_SGI = 1;
  499. if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
  500. (rate_n_flags & RATE_MCS_DUP_MSK))
  501. tbl->is_ht40 = 1;
  502. if (rate_n_flags & RATE_MCS_DUP_MSK)
  503. tbl->is_dup = 1;
  504. mcs = il4965_rs_extract_rate(rate_n_flags);
  505. /* SISO */
  506. if (mcs <= RATE_SISO_60M_PLCP) {
  507. if (il4965_num_of_ant == 1)
  508. tbl->lq_type = LQ_SISO; /*else NONE*/
  509. /* MIMO2 */
  510. } else {
  511. if (il4965_num_of_ant == 2)
  512. tbl->lq_type = LQ_MIMO2;
  513. }
  514. }
  515. return 0;
  516. }
  517. /* switch to another antenna/antennas and return 1 */
  518. /* if no other valid antenna found, return 0 */
  519. static int il4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
  520. struct il_scale_tbl_info *tbl)
  521. {
  522. u8 new_ant_type;
  523. if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
  524. return 0;
  525. if (!il4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
  526. return 0;
  527. new_ant_type = ant_toggle_lookup[tbl->ant_type];
  528. while (new_ant_type != tbl->ant_type &&
  529. !il4965_rs_is_valid_ant(valid_ant, new_ant_type))
  530. new_ant_type = ant_toggle_lookup[new_ant_type];
  531. if (new_ant_type == tbl->ant_type)
  532. return 0;
  533. tbl->ant_type = new_ant_type;
  534. *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
  535. *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
  536. return 1;
  537. }
  538. /**
  539. * Green-field mode is valid if the station supports it and
  540. * there are no non-GF stations present in the BSS.
  541. */
  542. static bool il4965_rs_use_green(struct ieee80211_sta *sta)
  543. {
  544. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  545. struct il_rxon_context *ctx = sta_priv->common.ctx;
  546. return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
  547. !(ctx->ht.non_gf_sta_present);
  548. }
  549. /**
  550. * il4965_rs_get_supported_rates - get the available rates
  551. *
  552. * if management frame or broadcast frame only return
  553. * basic available rates.
  554. *
  555. */
  556. static u16 il4965_rs_get_supported_rates(struct il_lq_sta *lq_sta,
  557. struct ieee80211_hdr *hdr,
  558. enum il_table_type rate_type)
  559. {
  560. if (is_legacy(rate_type)) {
  561. return lq_sta->active_legacy_rate;
  562. } else {
  563. if (is_siso(rate_type))
  564. return lq_sta->active_siso_rate;
  565. else
  566. return lq_sta->active_mimo2_rate;
  567. }
  568. }
  569. static u16
  570. il4965_rs_get_adjacent_rate(struct il_priv *il, u8 idx, u16 rate_mask,
  571. int rate_type)
  572. {
  573. u8 high = RATE_INVALID;
  574. u8 low = RATE_INVALID;
  575. /* 802.11A or ht walks to the next literal adjacent rate in
  576. * the rate table */
  577. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  578. int i;
  579. u32 mask;
  580. /* Find the previous rate that is in the rate mask */
  581. i = idx - 1;
  582. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  583. if (rate_mask & mask) {
  584. low = i;
  585. break;
  586. }
  587. }
  588. /* Find the next rate that is in the rate mask */
  589. i = idx + 1;
  590. for (mask = (1 << i); i < RATE_COUNT; i++, mask <<= 1) {
  591. if (rate_mask & mask) {
  592. high = i;
  593. break;
  594. }
  595. }
  596. return (high << 8) | low;
  597. }
  598. low = idx;
  599. while (low != RATE_INVALID) {
  600. low = il_rates[low].prev_rs;
  601. if (low == RATE_INVALID)
  602. break;
  603. if (rate_mask & (1 << low))
  604. break;
  605. D_RATE("Skipping masked lower rate: %d\n", low);
  606. }
  607. high = idx;
  608. while (high != RATE_INVALID) {
  609. high = il_rates[high].next_rs;
  610. if (high == RATE_INVALID)
  611. break;
  612. if (rate_mask & (1 << high))
  613. break;
  614. D_RATE("Skipping masked higher rate: %d\n", high);
  615. }
  616. return (high << 8) | low;
  617. }
  618. static u32 il4965_rs_get_lower_rate(struct il_lq_sta *lq_sta,
  619. struct il_scale_tbl_info *tbl,
  620. u8 scale_idx, u8 ht_possible)
  621. {
  622. s32 low;
  623. u16 rate_mask;
  624. u16 high_low;
  625. u8 switch_to_legacy = 0;
  626. u8 is_green = lq_sta->is_green;
  627. struct il_priv *il = lq_sta->drv;
  628. /* check if we need to switch from HT to legacy rates.
  629. * assumption is that mandatory rates (1Mbps or 6Mbps)
  630. * are always supported (spec demand) */
  631. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_idx)) {
  632. switch_to_legacy = 1;
  633. scale_idx = rs_ht_to_legacy[scale_idx];
  634. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  635. tbl->lq_type = LQ_A;
  636. else
  637. tbl->lq_type = LQ_G;
  638. if (il4965_num_of_ant(tbl->ant_type) > 1)
  639. tbl->ant_type =
  640. il4965_first_antenna(il->hw_params.valid_tx_ant);
  641. tbl->is_ht40 = 0;
  642. tbl->is_SGI = 0;
  643. tbl->max_search = IL_MAX_SEARCH;
  644. }
  645. rate_mask = il4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
  646. /* Mask with station rate restriction */
  647. if (is_legacy(tbl->lq_type)) {
  648. /* supp_rates has no CCK bits in A mode */
  649. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  650. rate_mask = (u16)(rate_mask &
  651. (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
  652. else
  653. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  654. }
  655. /* If we switched from HT to legacy, check current rate */
  656. if (switch_to_legacy && (rate_mask & (1 << scale_idx))) {
  657. low = scale_idx;
  658. goto out;
  659. }
  660. high_low = il4965_rs_get_adjacent_rate(lq_sta->drv,
  661. scale_idx, rate_mask,
  662. tbl->lq_type);
  663. low = high_low & 0xff;
  664. if (low == RATE_INVALID)
  665. low = scale_idx;
  666. out:
  667. return il4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
  668. }
  669. /*
  670. * Simple function to compare two rate scale table types
  671. */
  672. static bool il4965_table_type_matches(struct il_scale_tbl_info *a,
  673. struct il_scale_tbl_info *b)
  674. {
  675. return (a->lq_type == b->lq_type && a->ant_type == b->ant_type &&
  676. a->is_SGI == b->is_SGI);
  677. }
  678. /*
  679. * mac80211 sends us Tx status
  680. */
  681. static void
  682. il4965_rs_tx_status(void *il_r, struct ieee80211_supported_band *sband,
  683. struct ieee80211_sta *sta, void *il_sta,
  684. struct sk_buff *skb)
  685. {
  686. int legacy_success;
  687. int retries;
  688. int rs_idx, mac_idx, i;
  689. struct il_lq_sta *lq_sta = il_sta;
  690. struct il_link_quality_cmd *table;
  691. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  692. struct il_priv *il = (struct il_priv *)il_r;
  693. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  694. enum mac80211_rate_control_flags mac_flags;
  695. u32 tx_rate;
  696. struct il_scale_tbl_info tbl_type;
  697. struct il_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  698. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  699. struct il_rxon_context *ctx = sta_priv->common.ctx;
  700. D_RATE(
  701. "get frame ack response, update rate scale win\n");
  702. /* Treat uninitialized rate scaling data same as non-existing. */
  703. if (!lq_sta) {
  704. D_RATE("Station rate scaling not created yet.\n");
  705. return;
  706. } else if (!lq_sta->drv) {
  707. D_RATE("Rate scaling not initialized yet.\n");
  708. return;
  709. }
  710. if (!ieee80211_is_data(hdr->frame_control) ||
  711. (info->flags & IEEE80211_TX_CTL_NO_ACK))
  712. return;
  713. /* This packet was aggregated but doesn't carry status info */
  714. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  715. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  716. return;
  717. /*
  718. * Ignore this Tx frame response if its initial rate doesn't match
  719. * that of latest Link Quality command. There may be stragglers
  720. * from a previous Link Quality command, but we're no longer interested
  721. * in those; they're either from the "active" mode while we're trying
  722. * to check "search" mode, or a prior "search" mode after we've moved
  723. * to a new "search" mode (which might become the new "active" mode).
  724. */
  725. table = &lq_sta->lq;
  726. tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
  727. il4965_rs_get_tbl_info_from_mcs(tx_rate,
  728. il->band, &tbl_type, &rs_idx);
  729. if (il->band == IEEE80211_BAND_5GHZ)
  730. rs_idx -= IL_FIRST_OFDM_RATE;
  731. mac_flags = info->status.rates[0].flags;
  732. mac_idx = info->status.rates[0].idx;
  733. /* For HT packets, map MCS to PLCP */
  734. if (mac_flags & IEEE80211_TX_RC_MCS) {
  735. mac_idx &= RATE_MCS_CODE_MSK; /* Remove # of streams */
  736. if (mac_idx >= (RATE_9M_IDX - IL_FIRST_OFDM_RATE))
  737. mac_idx++;
  738. /*
  739. * mac80211 HT idx is always zero-idxed; we need to move
  740. * HT OFDM rates after CCK rates in 2.4 GHz band
  741. */
  742. if (il->band == IEEE80211_BAND_2GHZ)
  743. mac_idx += IL_FIRST_OFDM_RATE;
  744. }
  745. /* Here we actually compare this rate to the latest LQ command */
  746. if (mac_idx < 0 ||
  747. tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI) ||
  748. tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ||
  749. tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA) ||
  750. tbl_type.ant_type != info->antenna_sel_tx ||
  751. !!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS) ||
  752. !!(tx_rate & RATE_MCS_GF_MSK) != !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD) ||
  753. rs_idx != mac_idx) {
  754. D_RATE(
  755. "initial rate %d does not match %d (0x%x)\n",
  756. mac_idx, rs_idx, tx_rate);
  757. /*
  758. * Since rates mis-match, the last LQ command may have failed.
  759. * After IL_MISSED_RATE_MAX mis-matches, resync the uCode with
  760. * ... driver.
  761. */
  762. lq_sta->missed_rate_counter++;
  763. if (lq_sta->missed_rate_counter > IL_MISSED_RATE_MAX) {
  764. lq_sta->missed_rate_counter = 0;
  765. il_send_lq_cmd(il, ctx, &lq_sta->lq,
  766. CMD_ASYNC, false);
  767. }
  768. /* Regardless, ignore this status info for outdated rate */
  769. return;
  770. } else
  771. /* Rate did match, so reset the missed_rate_counter */
  772. lq_sta->missed_rate_counter = 0;
  773. /* Figure out if rate scale algorithm is in active or search table */
  774. if (il4965_table_type_matches(&tbl_type,
  775. &(lq_sta->lq_info[lq_sta->active_tbl]))) {
  776. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  777. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  778. } else if (il4965_table_type_matches(&tbl_type,
  779. &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
  780. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  781. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  782. } else {
  783. D_RATE(
  784. "Neither active nor search matches tx rate\n");
  785. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  786. D_RATE("active- lq:%x, ant:%x, SGI:%d\n",
  787. tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
  788. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  789. D_RATE("search- lq:%x, ant:%x, SGI:%d\n",
  790. tmp_tbl->lq_type, tmp_tbl->ant_type, tmp_tbl->is_SGI);
  791. D_RATE("actual- lq:%x, ant:%x, SGI:%d\n",
  792. tbl_type.lq_type, tbl_type.ant_type, tbl_type.is_SGI);
  793. /*
  794. * no matching table found, let's by-pass the data collection
  795. * and continue to perform rate scale to find the rate table
  796. */
  797. il4965_rs_stay_in_table(lq_sta, true);
  798. goto done;
  799. }
  800. /*
  801. * Updating the frame history depends on whether packets were
  802. * aggregated.
  803. *
  804. * For aggregation, all packets were transmitted at the same rate, the
  805. * first idx into rate scale table.
  806. */
  807. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  808. tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
  809. il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type,
  810. &rs_idx);
  811. il4965_rs_collect_tx_data(curr_tbl, rs_idx,
  812. info->status.ampdu_len,
  813. info->status.ampdu_ack_len);
  814. /* Update success/fail counts if not searching for new mode */
  815. if (lq_sta->stay_in_tbl) {
  816. lq_sta->total_success += info->status.ampdu_ack_len;
  817. lq_sta->total_failed += (info->status.ampdu_len -
  818. info->status.ampdu_ack_len);
  819. }
  820. } else {
  821. /*
  822. * For legacy, update frame history with for each Tx retry.
  823. */
  824. retries = info->status.rates[0].count - 1;
  825. /* HW doesn't send more than 15 retries */
  826. retries = min(retries, 15);
  827. /* The last transmission may have been successful */
  828. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  829. /* Collect data for each rate used during failed TX attempts */
  830. for (i = 0; i <= retries; ++i) {
  831. tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
  832. il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band,
  833. &tbl_type, &rs_idx);
  834. /*
  835. * Only collect stats if retried rate is in the same RS
  836. * table as active/search.
  837. */
  838. if (il4965_table_type_matches(&tbl_type, curr_tbl))
  839. tmp_tbl = curr_tbl;
  840. else if (il4965_table_type_matches(&tbl_type,
  841. other_tbl))
  842. tmp_tbl = other_tbl;
  843. else
  844. continue;
  845. il4965_rs_collect_tx_data(tmp_tbl, rs_idx, 1,
  846. i < retries ? 0 : legacy_success);
  847. }
  848. /* Update success/fail counts if not searching for new mode */
  849. if (lq_sta->stay_in_tbl) {
  850. lq_sta->total_success += legacy_success;
  851. lq_sta->total_failed += retries + (1 - legacy_success);
  852. }
  853. }
  854. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  855. lq_sta->last_rate_n_flags = tx_rate;
  856. done:
  857. /* See if there's a better rate or modulation mode to try. */
  858. if (sta && sta->supp_rates[sband->band])
  859. il4965_rs_rate_scale_perform(il, skb, sta, lq_sta);
  860. }
  861. /*
  862. * Begin a period of staying with a selected modulation mode.
  863. * Set "stay_in_tbl" flag to prevent any mode switches.
  864. * Set frame tx success limits according to legacy vs. high-throughput,
  865. * and reset overall (spanning all rates) tx success history stats.
  866. * These control how long we stay using same modulation mode before
  867. * searching for a new mode.
  868. */
  869. static void il4965_rs_set_stay_in_table(struct il_priv *il, u8 is_legacy,
  870. struct il_lq_sta *lq_sta)
  871. {
  872. D_RATE("we are staying in the same table\n");
  873. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  874. if (is_legacy) {
  875. lq_sta->table_count_limit = IL_LEGACY_TBL_COUNT;
  876. lq_sta->max_failure_limit = IL_LEGACY_FAILURE_LIMIT;
  877. lq_sta->max_success_limit = IL_LEGACY_SUCCESS_LIMIT;
  878. } else {
  879. lq_sta->table_count_limit = IL_NONE_LEGACY_TBL_COUNT;
  880. lq_sta->max_failure_limit = IL_NONE_LEGACY_FAILURE_LIMIT;
  881. lq_sta->max_success_limit = IL_NONE_LEGACY_SUCCESS_LIMIT;
  882. }
  883. lq_sta->table_count = 0;
  884. lq_sta->total_failed = 0;
  885. lq_sta->total_success = 0;
  886. lq_sta->flush_timer = jiffies;
  887. lq_sta->action_counter = 0;
  888. }
  889. /*
  890. * Find correct throughput table for given mode of modulation
  891. */
  892. static void il4965_rs_set_expected_tpt_table(struct il_lq_sta *lq_sta,
  893. struct il_scale_tbl_info *tbl)
  894. {
  895. /* Used to choose among HT tables */
  896. s32 (*ht_tbl_pointer)[RATE_COUNT];
  897. /* Check for invalid LQ type */
  898. if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
  899. tbl->expected_tpt = expected_tpt_legacy;
  900. return;
  901. }
  902. /* Legacy rates have only one table */
  903. if (is_legacy(tbl->lq_type)) {
  904. tbl->expected_tpt = expected_tpt_legacy;
  905. return;
  906. }
  907. /* Choose among many HT tables depending on number of streams
  908. * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
  909. * status */
  910. if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
  911. ht_tbl_pointer = expected_tpt_siso20MHz;
  912. else if (is_siso(tbl->lq_type))
  913. ht_tbl_pointer = expected_tpt_siso40MHz;
  914. else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
  915. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  916. else /* if (is_mimo2(tbl->lq_type)) <-- must be true */
  917. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  918. if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
  919. tbl->expected_tpt = ht_tbl_pointer[0];
  920. else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
  921. tbl->expected_tpt = ht_tbl_pointer[1];
  922. else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
  923. tbl->expected_tpt = ht_tbl_pointer[2];
  924. else /* AGG+SGI */
  925. tbl->expected_tpt = ht_tbl_pointer[3];
  926. }
  927. /*
  928. * Find starting rate for new "search" high-throughput mode of modulation.
  929. * Goal is to find lowest expected rate (under perfect conditions) that is
  930. * above the current measured throughput of "active" mode, to give new mode
  931. * a fair chance to prove itself without too many challenges.
  932. *
  933. * This gets called when transitioning to more aggressive modulation
  934. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  935. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  936. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  937. * bit rate will typically need to increase, but not if performance was bad.
  938. */
  939. static s32 il4965_rs_get_best_rate(struct il_priv *il,
  940. struct il_lq_sta *lq_sta,
  941. struct il_scale_tbl_info *tbl, /* "search" */
  942. u16 rate_mask, s8 idx)
  943. {
  944. /* "active" values */
  945. struct il_scale_tbl_info *active_tbl =
  946. &(lq_sta->lq_info[lq_sta->active_tbl]);
  947. s32 active_sr = active_tbl->win[idx].success_ratio;
  948. s32 active_tpt = active_tbl->expected_tpt[idx];
  949. /* expected "search" throughput */
  950. s32 *tpt_tbl = tbl->expected_tpt;
  951. s32 new_rate, high, low, start_hi;
  952. u16 high_low;
  953. s8 rate = idx;
  954. new_rate = high = low = start_hi = RATE_INVALID;
  955. for (; ;) {
  956. high_low = il4965_rs_get_adjacent_rate(il, rate, rate_mask,
  957. tbl->lq_type);
  958. low = high_low & 0xff;
  959. high = (high_low >> 8) & 0xff;
  960. /*
  961. * Lower the "search" bit rate, to give new "search" mode
  962. * approximately the same throughput as "active" if:
  963. *
  964. * 1) "Active" mode has been working modestly well (but not
  965. * great), and expected "search" throughput (under perfect
  966. * conditions) at candidate rate is above the actual
  967. * measured "active" throughput (but less than expected
  968. * "active" throughput under perfect conditions).
  969. * OR
  970. * 2) "Active" mode has been working perfectly or very well
  971. * and expected "search" throughput (under perfect
  972. * conditions) at candidate rate is above expected
  973. * "active" throughput (under perfect conditions).
  974. */
  975. if ((100 * tpt_tbl[rate] > lq_sta->last_tpt &&
  976. (active_sr > RATE_DECREASE_TH &&
  977. active_sr <= RATE_HIGH_TH &&
  978. tpt_tbl[rate] <= active_tpt)) ||
  979. (active_sr >= RATE_SCALE_SWITCH &&
  980. tpt_tbl[rate] > active_tpt)) {
  981. /* (2nd or later pass)
  982. * If we've already tried to raise the rate, and are
  983. * now trying to lower it, use the higher rate. */
  984. if (start_hi != RATE_INVALID) {
  985. new_rate = start_hi;
  986. break;
  987. }
  988. new_rate = rate;
  989. /* Loop again with lower rate */
  990. if (low != RATE_INVALID)
  991. rate = low;
  992. /* Lower rate not available, use the original */
  993. else
  994. break;
  995. /* Else try to raise the "search" rate to match "active" */
  996. } else {
  997. /* (2nd or later pass)
  998. * If we've already tried to lower the rate, and are
  999. * now trying to raise it, use the lower rate. */
  1000. if (new_rate != RATE_INVALID)
  1001. break;
  1002. /* Loop again with higher rate */
  1003. else if (high != RATE_INVALID) {
  1004. start_hi = high;
  1005. rate = high;
  1006. /* Higher rate not available, use the original */
  1007. } else {
  1008. new_rate = rate;
  1009. break;
  1010. }
  1011. }
  1012. }
  1013. return new_rate;
  1014. }
  1015. /*
  1016. * Set up search table for MIMO2
  1017. */
  1018. static int il4965_rs_switch_to_mimo2(struct il_priv *il,
  1019. struct il_lq_sta *lq_sta,
  1020. struct ieee80211_conf *conf,
  1021. struct ieee80211_sta *sta,
  1022. struct il_scale_tbl_info *tbl, int idx)
  1023. {
  1024. u16 rate_mask;
  1025. s32 rate;
  1026. s8 is_green = lq_sta->is_green;
  1027. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  1028. struct il_rxon_context *ctx = sta_priv->common.ctx;
  1029. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1030. return -1;
  1031. if (((sta->ht_cap.cap & IEEE80211_HT_CAP_SM_PS) >> 2)
  1032. == WLAN_HT_CAP_SM_PS_STATIC)
  1033. return -1;
  1034. /* Need both Tx chains/antennas to support MIMO */
  1035. if (il->hw_params.tx_chains_num < 2)
  1036. return -1;
  1037. D_RATE("LQ: try to switch to MIMO2\n");
  1038. tbl->lq_type = LQ_MIMO2;
  1039. tbl->is_dup = lq_sta->is_dup;
  1040. tbl->action = 0;
  1041. tbl->max_search = IL_MAX_SEARCH;
  1042. rate_mask = lq_sta->active_mimo2_rate;
  1043. if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
  1044. tbl->is_ht40 = 1;
  1045. else
  1046. tbl->is_ht40 = 0;
  1047. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1048. rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
  1049. D_RATE("LQ: MIMO2 best rate %d mask %X\n",
  1050. rate, rate_mask);
  1051. if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
  1052. D_RATE(
  1053. "Can't switch with idx %d rate mask %x\n",
  1054. rate, rate_mask);
  1055. return -1;
  1056. }
  1057. tbl->current_rate = il4965_rate_n_flags_from_tbl(il,
  1058. tbl, rate, is_green);
  1059. D_RATE("LQ: Switch to new mcs %X idx is green %X\n",
  1060. tbl->current_rate, is_green);
  1061. return 0;
  1062. }
  1063. /*
  1064. * Set up search table for SISO
  1065. */
  1066. static int il4965_rs_switch_to_siso(struct il_priv *il,
  1067. struct il_lq_sta *lq_sta,
  1068. struct ieee80211_conf *conf,
  1069. struct ieee80211_sta *sta,
  1070. struct il_scale_tbl_info *tbl, int idx)
  1071. {
  1072. u16 rate_mask;
  1073. u8 is_green = lq_sta->is_green;
  1074. s32 rate;
  1075. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  1076. struct il_rxon_context *ctx = sta_priv->common.ctx;
  1077. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1078. return -1;
  1079. D_RATE("LQ: try to switch to SISO\n");
  1080. tbl->is_dup = lq_sta->is_dup;
  1081. tbl->lq_type = LQ_SISO;
  1082. tbl->action = 0;
  1083. tbl->max_search = IL_MAX_SEARCH;
  1084. rate_mask = lq_sta->active_siso_rate;
  1085. if (il_is_ht40_tx_allowed(il, ctx, &sta->ht_cap))
  1086. tbl->is_ht40 = 1;
  1087. else
  1088. tbl->is_ht40 = 0;
  1089. if (is_green)
  1090. tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
  1091. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1092. rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
  1093. D_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
  1094. if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
  1095. D_RATE(
  1096. "can not switch with idx %d rate mask %x\n",
  1097. rate, rate_mask);
  1098. return -1;
  1099. }
  1100. tbl->current_rate = il4965_rate_n_flags_from_tbl(il,
  1101. tbl, rate, is_green);
  1102. D_RATE("LQ: Switch to new mcs %X idx is green %X\n",
  1103. tbl->current_rate, is_green);
  1104. return 0;
  1105. }
  1106. /*
  1107. * Try to switch to new modulation mode from legacy
  1108. */
  1109. static int il4965_rs_move_legacy_other(struct il_priv *il,
  1110. struct il_lq_sta *lq_sta,
  1111. struct ieee80211_conf *conf,
  1112. struct ieee80211_sta *sta,
  1113. int idx)
  1114. {
  1115. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1116. struct il_scale_tbl_info *search_tbl =
  1117. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1118. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1119. u32 sz = (sizeof(struct il_scale_tbl_info) -
  1120. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1121. u8 start_action;
  1122. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1123. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1124. int ret = 0;
  1125. u8 update_search_tbl_counter = 0;
  1126. tbl->action = IL_LEGACY_SWITCH_SISO;
  1127. start_action = tbl->action;
  1128. for (; ;) {
  1129. lq_sta->action_counter++;
  1130. switch (tbl->action) {
  1131. case IL_LEGACY_SWITCH_ANTENNA1:
  1132. case IL_LEGACY_SWITCH_ANTENNA2:
  1133. D_RATE("LQ: Legacy toggle Antenna\n");
  1134. if ((tbl->action == IL_LEGACY_SWITCH_ANTENNA1 &&
  1135. tx_chains_num <= 1) ||
  1136. (tbl->action == IL_LEGACY_SWITCH_ANTENNA2 &&
  1137. tx_chains_num <= 2))
  1138. break;
  1139. /* Don't change antenna if success has been great */
  1140. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1141. break;
  1142. /* Set up search table to try other antenna */
  1143. memcpy(search_tbl, tbl, sz);
  1144. if (il4965_rs_toggle_antenna(valid_tx_ant,
  1145. &search_tbl->current_rate, search_tbl)) {
  1146. update_search_tbl_counter = 1;
  1147. il4965_rs_set_expected_tpt_table(lq_sta,
  1148. search_tbl);
  1149. goto out;
  1150. }
  1151. break;
  1152. case IL_LEGACY_SWITCH_SISO:
  1153. D_RATE("LQ: Legacy switch to SISO\n");
  1154. /* Set up search table to try SISO */
  1155. memcpy(search_tbl, tbl, sz);
  1156. search_tbl->is_SGI = 0;
  1157. ret = il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
  1158. search_tbl, idx);
  1159. if (!ret) {
  1160. lq_sta->action_counter = 0;
  1161. goto out;
  1162. }
  1163. break;
  1164. case IL_LEGACY_SWITCH_MIMO2_AB:
  1165. case IL_LEGACY_SWITCH_MIMO2_AC:
  1166. case IL_LEGACY_SWITCH_MIMO2_BC:
  1167. D_RATE("LQ: Legacy switch to MIMO2\n");
  1168. /* Set up search table to try MIMO */
  1169. memcpy(search_tbl, tbl, sz);
  1170. search_tbl->is_SGI = 0;
  1171. if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AB)
  1172. search_tbl->ant_type = ANT_AB;
  1173. else if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AC)
  1174. search_tbl->ant_type = ANT_AC;
  1175. else
  1176. search_tbl->ant_type = ANT_BC;
  1177. if (!il4965_rs_is_valid_ant(valid_tx_ant,
  1178. search_tbl->ant_type))
  1179. break;
  1180. ret = il4965_rs_switch_to_mimo2(il, lq_sta,
  1181. conf, sta,
  1182. search_tbl, idx);
  1183. if (!ret) {
  1184. lq_sta->action_counter = 0;
  1185. goto out;
  1186. }
  1187. break;
  1188. }
  1189. tbl->action++;
  1190. if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
  1191. tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
  1192. if (tbl->action == start_action)
  1193. break;
  1194. }
  1195. search_tbl->lq_type = LQ_NONE;
  1196. return 0;
  1197. out:
  1198. lq_sta->search_better_tbl = 1;
  1199. tbl->action++;
  1200. if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
  1201. tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
  1202. if (update_search_tbl_counter)
  1203. search_tbl->action = tbl->action;
  1204. return 0;
  1205. }
  1206. /*
  1207. * Try to switch to new modulation mode from SISO
  1208. */
  1209. static int il4965_rs_move_siso_to_other(struct il_priv *il,
  1210. struct il_lq_sta *lq_sta,
  1211. struct ieee80211_conf *conf,
  1212. struct ieee80211_sta *sta, int idx)
  1213. {
  1214. u8 is_green = lq_sta->is_green;
  1215. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1216. struct il_scale_tbl_info *search_tbl =
  1217. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1218. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1219. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1220. u32 sz = (sizeof(struct il_scale_tbl_info) -
  1221. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1222. u8 start_action;
  1223. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1224. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1225. u8 update_search_tbl_counter = 0;
  1226. int ret;
  1227. start_action = tbl->action;
  1228. for (;;) {
  1229. lq_sta->action_counter++;
  1230. switch (tbl->action) {
  1231. case IL_SISO_SWITCH_ANTENNA1:
  1232. case IL_SISO_SWITCH_ANTENNA2:
  1233. D_RATE("LQ: SISO toggle Antenna\n");
  1234. if ((tbl->action == IL_SISO_SWITCH_ANTENNA1 &&
  1235. tx_chains_num <= 1) ||
  1236. (tbl->action == IL_SISO_SWITCH_ANTENNA2 &&
  1237. tx_chains_num <= 2))
  1238. break;
  1239. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1240. break;
  1241. memcpy(search_tbl, tbl, sz);
  1242. if (il4965_rs_toggle_antenna(valid_tx_ant,
  1243. &search_tbl->current_rate, search_tbl)) {
  1244. update_search_tbl_counter = 1;
  1245. goto out;
  1246. }
  1247. break;
  1248. case IL_SISO_SWITCH_MIMO2_AB:
  1249. case IL_SISO_SWITCH_MIMO2_AC:
  1250. case IL_SISO_SWITCH_MIMO2_BC:
  1251. D_RATE("LQ: SISO switch to MIMO2\n");
  1252. memcpy(search_tbl, tbl, sz);
  1253. search_tbl->is_SGI = 0;
  1254. if (tbl->action == IL_SISO_SWITCH_MIMO2_AB)
  1255. search_tbl->ant_type = ANT_AB;
  1256. else if (tbl->action == IL_SISO_SWITCH_MIMO2_AC)
  1257. search_tbl->ant_type = ANT_AC;
  1258. else
  1259. search_tbl->ant_type = ANT_BC;
  1260. if (!il4965_rs_is_valid_ant(valid_tx_ant,
  1261. search_tbl->ant_type))
  1262. break;
  1263. ret = il4965_rs_switch_to_mimo2(il, lq_sta,
  1264. conf, sta,
  1265. search_tbl, idx);
  1266. if (!ret)
  1267. goto out;
  1268. break;
  1269. case IL_SISO_SWITCH_GI:
  1270. if (!tbl->is_ht40 && !(ht_cap->cap &
  1271. IEEE80211_HT_CAP_SGI_20))
  1272. break;
  1273. if (tbl->is_ht40 && !(ht_cap->cap &
  1274. IEEE80211_HT_CAP_SGI_40))
  1275. break;
  1276. D_RATE("LQ: SISO toggle SGI/NGI\n");
  1277. memcpy(search_tbl, tbl, sz);
  1278. if (is_green) {
  1279. if (!tbl->is_SGI)
  1280. break;
  1281. else
  1282. IL_ERR(
  1283. "SGI was set in GF+SISO\n");
  1284. }
  1285. search_tbl->is_SGI = !tbl->is_SGI;
  1286. il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
  1287. if (tbl->is_SGI) {
  1288. s32 tpt = lq_sta->last_tpt / 100;
  1289. if (tpt >= search_tbl->expected_tpt[idx])
  1290. break;
  1291. }
  1292. search_tbl->current_rate =
  1293. il4965_rate_n_flags_from_tbl(il, search_tbl,
  1294. idx, is_green);
  1295. update_search_tbl_counter = 1;
  1296. goto out;
  1297. }
  1298. tbl->action++;
  1299. if (tbl->action > IL_SISO_SWITCH_GI)
  1300. tbl->action = IL_SISO_SWITCH_ANTENNA1;
  1301. if (tbl->action == start_action)
  1302. break;
  1303. }
  1304. search_tbl->lq_type = LQ_NONE;
  1305. return 0;
  1306. out:
  1307. lq_sta->search_better_tbl = 1;
  1308. tbl->action++;
  1309. if (tbl->action > IL_SISO_SWITCH_GI)
  1310. tbl->action = IL_SISO_SWITCH_ANTENNA1;
  1311. if (update_search_tbl_counter)
  1312. search_tbl->action = tbl->action;
  1313. return 0;
  1314. }
  1315. /*
  1316. * Try to switch to new modulation mode from MIMO2
  1317. */
  1318. static int il4965_rs_move_mimo2_to_other(struct il_priv *il,
  1319. struct il_lq_sta *lq_sta,
  1320. struct ieee80211_conf *conf,
  1321. struct ieee80211_sta *sta, int idx)
  1322. {
  1323. s8 is_green = lq_sta->is_green;
  1324. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1325. struct il_scale_tbl_info *search_tbl =
  1326. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1327. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1328. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1329. u32 sz = (sizeof(struct il_scale_tbl_info) -
  1330. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1331. u8 start_action;
  1332. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1333. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1334. u8 update_search_tbl_counter = 0;
  1335. int ret;
  1336. start_action = tbl->action;
  1337. for (;;) {
  1338. lq_sta->action_counter++;
  1339. switch (tbl->action) {
  1340. case IL_MIMO2_SWITCH_ANTENNA1:
  1341. case IL_MIMO2_SWITCH_ANTENNA2:
  1342. D_RATE("LQ: MIMO2 toggle Antennas\n");
  1343. if (tx_chains_num <= 2)
  1344. break;
  1345. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1346. break;
  1347. memcpy(search_tbl, tbl, sz);
  1348. if (il4965_rs_toggle_antenna(valid_tx_ant,
  1349. &search_tbl->current_rate, search_tbl)) {
  1350. update_search_tbl_counter = 1;
  1351. goto out;
  1352. }
  1353. break;
  1354. case IL_MIMO2_SWITCH_SISO_A:
  1355. case IL_MIMO2_SWITCH_SISO_B:
  1356. case IL_MIMO2_SWITCH_SISO_C:
  1357. D_RATE("LQ: MIMO2 switch to SISO\n");
  1358. /* Set up new search table for SISO */
  1359. memcpy(search_tbl, tbl, sz);
  1360. if (tbl->action == IL_MIMO2_SWITCH_SISO_A)
  1361. search_tbl->ant_type = ANT_A;
  1362. else if (tbl->action == IL_MIMO2_SWITCH_SISO_B)
  1363. search_tbl->ant_type = ANT_B;
  1364. else
  1365. search_tbl->ant_type = ANT_C;
  1366. if (!il4965_rs_is_valid_ant(valid_tx_ant,
  1367. search_tbl->ant_type))
  1368. break;
  1369. ret = il4965_rs_switch_to_siso(il, lq_sta,
  1370. conf, sta,
  1371. search_tbl, idx);
  1372. if (!ret)
  1373. goto out;
  1374. break;
  1375. case IL_MIMO2_SWITCH_GI:
  1376. if (!tbl->is_ht40 && !(ht_cap->cap &
  1377. IEEE80211_HT_CAP_SGI_20))
  1378. break;
  1379. if (tbl->is_ht40 && !(ht_cap->cap &
  1380. IEEE80211_HT_CAP_SGI_40))
  1381. break;
  1382. D_RATE("LQ: MIMO2 toggle SGI/NGI\n");
  1383. /* Set up new search table for MIMO2 */
  1384. memcpy(search_tbl, tbl, sz);
  1385. search_tbl->is_SGI = !tbl->is_SGI;
  1386. il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
  1387. /*
  1388. * If active table already uses the fastest possible
  1389. * modulation (dual stream with short guard interval),
  1390. * and it's working well, there's no need to look
  1391. * for a better type of modulation!
  1392. */
  1393. if (tbl->is_SGI) {
  1394. s32 tpt = lq_sta->last_tpt / 100;
  1395. if (tpt >= search_tbl->expected_tpt[idx])
  1396. break;
  1397. }
  1398. search_tbl->current_rate =
  1399. il4965_rate_n_flags_from_tbl(il, search_tbl,
  1400. idx, is_green);
  1401. update_search_tbl_counter = 1;
  1402. goto out;
  1403. }
  1404. tbl->action++;
  1405. if (tbl->action > IL_MIMO2_SWITCH_GI)
  1406. tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
  1407. if (tbl->action == start_action)
  1408. break;
  1409. }
  1410. search_tbl->lq_type = LQ_NONE;
  1411. return 0;
  1412. out:
  1413. lq_sta->search_better_tbl = 1;
  1414. tbl->action++;
  1415. if (tbl->action > IL_MIMO2_SWITCH_GI)
  1416. tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
  1417. if (update_search_tbl_counter)
  1418. search_tbl->action = tbl->action;
  1419. return 0;
  1420. }
  1421. /*
  1422. * Check whether we should continue using same modulation mode, or
  1423. * begin search for a new mode, based on:
  1424. * 1) # tx successes or failures while using this mode
  1425. * 2) # times calling this function
  1426. * 3) elapsed time in this mode (not used, for now)
  1427. */
  1428. static void
  1429. il4965_rs_stay_in_table(struct il_lq_sta *lq_sta, bool force_search)
  1430. {
  1431. struct il_scale_tbl_info *tbl;
  1432. int i;
  1433. int active_tbl;
  1434. int flush_interval_passed = 0;
  1435. struct il_priv *il;
  1436. il = lq_sta->drv;
  1437. active_tbl = lq_sta->active_tbl;
  1438. tbl = &(lq_sta->lq_info[active_tbl]);
  1439. /* If we've been disallowing search, see if we should now allow it */
  1440. if (lq_sta->stay_in_tbl) {
  1441. /* Elapsed time using current modulation mode */
  1442. if (lq_sta->flush_timer)
  1443. flush_interval_passed =
  1444. time_after(jiffies,
  1445. (unsigned long)(lq_sta->flush_timer +
  1446. RATE_SCALE_FLUSH_INTVL));
  1447. /*
  1448. * Check if we should allow search for new modulation mode.
  1449. * If many frames have failed or succeeded, or we've used
  1450. * this same modulation for a long time, allow search, and
  1451. * reset history stats that keep track of whether we should
  1452. * allow a new search. Also (below) reset all bitmaps and
  1453. * stats in active history.
  1454. */
  1455. if (force_search ||
  1456. lq_sta->total_failed > lq_sta->max_failure_limit ||
  1457. lq_sta->total_success > lq_sta->max_success_limit ||
  1458. (!lq_sta->search_better_tbl && lq_sta->flush_timer &&
  1459. flush_interval_passed)) {
  1460. D_RATE("LQ: stay is expired %d %d %d\n:",
  1461. lq_sta->total_failed,
  1462. lq_sta->total_success,
  1463. flush_interval_passed);
  1464. /* Allow search for new mode */
  1465. lq_sta->stay_in_tbl = 0; /* only place reset */
  1466. lq_sta->total_failed = 0;
  1467. lq_sta->total_success = 0;
  1468. lq_sta->flush_timer = 0;
  1469. /*
  1470. * Else if we've used this modulation mode enough repetitions
  1471. * (regardless of elapsed time or success/failure), reset
  1472. * history bitmaps and rate-specific stats for all rates in
  1473. * active table.
  1474. */
  1475. } else {
  1476. lq_sta->table_count++;
  1477. if (lq_sta->table_count >=
  1478. lq_sta->table_count_limit) {
  1479. lq_sta->table_count = 0;
  1480. D_RATE(
  1481. "LQ: stay in table clear win\n");
  1482. for (i = 0; i < RATE_COUNT; i++)
  1483. il4965_rs_rate_scale_clear_win(
  1484. &(tbl->win[i]));
  1485. }
  1486. }
  1487. /* If transitioning to allow "search", reset all history
  1488. * bitmaps and stats in active table (this will become the new
  1489. * "search" table). */
  1490. if (!lq_sta->stay_in_tbl) {
  1491. for (i = 0; i < RATE_COUNT; i++)
  1492. il4965_rs_rate_scale_clear_win(
  1493. &(tbl->win[i]));
  1494. }
  1495. }
  1496. }
  1497. /*
  1498. * setup rate table in uCode
  1499. * return rate_n_flags as used in the table
  1500. */
  1501. static u32 il4965_rs_update_rate_tbl(struct il_priv *il,
  1502. struct il_rxon_context *ctx,
  1503. struct il_lq_sta *lq_sta,
  1504. struct il_scale_tbl_info *tbl,
  1505. int idx, u8 is_green)
  1506. {
  1507. u32 rate;
  1508. /* Update uCode's rate table. */
  1509. rate = il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
  1510. il4965_rs_fill_link_cmd(il, lq_sta, rate);
  1511. il_send_lq_cmd(il, ctx, &lq_sta->lq, CMD_ASYNC, false);
  1512. return rate;
  1513. }
  1514. /*
  1515. * Do rate scaling and search for new modulation mode.
  1516. */
  1517. static void il4965_rs_rate_scale_perform(struct il_priv *il,
  1518. struct sk_buff *skb,
  1519. struct ieee80211_sta *sta,
  1520. struct il_lq_sta *lq_sta)
  1521. {
  1522. struct ieee80211_hw *hw = il->hw;
  1523. struct ieee80211_conf *conf = &hw->conf;
  1524. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1525. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1526. int low = RATE_INVALID;
  1527. int high = RATE_INVALID;
  1528. int idx;
  1529. int i;
  1530. struct il_rate_scale_data *win = NULL;
  1531. int current_tpt = IL_INVALID_VALUE;
  1532. int low_tpt = IL_INVALID_VALUE;
  1533. int high_tpt = IL_INVALID_VALUE;
  1534. u32 fail_count;
  1535. s8 scale_action = 0;
  1536. u16 rate_mask;
  1537. u8 update_lq = 0;
  1538. struct il_scale_tbl_info *tbl, *tbl1;
  1539. u16 rate_scale_idx_msk = 0;
  1540. u32 rate;
  1541. u8 is_green = 0;
  1542. u8 active_tbl = 0;
  1543. u8 done_search = 0;
  1544. u16 high_low;
  1545. s32 sr;
  1546. u8 tid = MAX_TID_COUNT;
  1547. struct il_tid_data *tid_data;
  1548. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  1549. struct il_rxon_context *ctx = sta_priv->common.ctx;
  1550. D_RATE("rate scale calculate new rate for skb\n");
  1551. /* Send management frames and NO_ACK data using lowest rate. */
  1552. /* TODO: this could probably be improved.. */
  1553. if (!ieee80211_is_data(hdr->frame_control) ||
  1554. (info->flags & IEEE80211_TX_CTL_NO_ACK))
  1555. return;
  1556. if (!sta || !lq_sta)
  1557. return;
  1558. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1559. tid = il4965_rs_tl_add_packet(lq_sta, hdr);
  1560. if (tid != MAX_TID_COUNT && (lq_sta->tx_agg_tid_en & (1 << tid))) {
  1561. tid_data = &il->stations[lq_sta->lq.sta_id].tid[tid];
  1562. if (tid_data->agg.state == IL_AGG_OFF)
  1563. lq_sta->is_agg = 0;
  1564. else
  1565. lq_sta->is_agg = 1;
  1566. } else
  1567. lq_sta->is_agg = 0;
  1568. /*
  1569. * Select rate-scale / modulation-mode table to work with in
  1570. * the rest of this function: "search" if searching for better
  1571. * modulation mode, or "active" if doing rate scaling within a mode.
  1572. */
  1573. if (!lq_sta->search_better_tbl)
  1574. active_tbl = lq_sta->active_tbl;
  1575. else
  1576. active_tbl = 1 - lq_sta->active_tbl;
  1577. tbl = &(lq_sta->lq_info[active_tbl]);
  1578. if (is_legacy(tbl->lq_type))
  1579. lq_sta->is_green = 0;
  1580. else
  1581. lq_sta->is_green = il4965_rs_use_green(sta);
  1582. is_green = lq_sta->is_green;
  1583. /* current tx rate */
  1584. idx = lq_sta->last_txrate_idx;
  1585. D_RATE("Rate scale idx %d for type %d\n", idx,
  1586. tbl->lq_type);
  1587. /* rates available for this association, and for modulation mode */
  1588. rate_mask = il4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
  1589. D_RATE("mask 0x%04X\n", rate_mask);
  1590. /* mask with station rate restriction */
  1591. if (is_legacy(tbl->lq_type)) {
  1592. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1593. /* supp_rates has no CCK bits in A mode */
  1594. rate_scale_idx_msk = (u16) (rate_mask &
  1595. (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
  1596. else
  1597. rate_scale_idx_msk = (u16) (rate_mask &
  1598. lq_sta->supp_rates);
  1599. } else
  1600. rate_scale_idx_msk = rate_mask;
  1601. if (!rate_scale_idx_msk)
  1602. rate_scale_idx_msk = rate_mask;
  1603. if (!((1 << idx) & rate_scale_idx_msk)) {
  1604. IL_ERR("Current Rate is not valid\n");
  1605. if (lq_sta->search_better_tbl) {
  1606. /* revert to active table if search table is not valid*/
  1607. tbl->lq_type = LQ_NONE;
  1608. lq_sta->search_better_tbl = 0;
  1609. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1610. /* get "active" rate info */
  1611. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1612. rate = il4965_rs_update_rate_tbl(il, ctx, lq_sta,
  1613. tbl, idx, is_green);
  1614. }
  1615. return;
  1616. }
  1617. /* Get expected throughput table and history win for current rate */
  1618. if (!tbl->expected_tpt) {
  1619. IL_ERR("tbl->expected_tpt is NULL\n");
  1620. return;
  1621. }
  1622. /* force user max rate if set by user */
  1623. if (lq_sta->max_rate_idx != -1 &&
  1624. lq_sta->max_rate_idx < idx) {
  1625. idx = lq_sta->max_rate_idx;
  1626. update_lq = 1;
  1627. win = &(tbl->win[idx]);
  1628. goto lq_update;
  1629. }
  1630. win = &(tbl->win[idx]);
  1631. /*
  1632. * If there is not enough history to calculate actual average
  1633. * throughput, keep analyzing results of more tx frames, without
  1634. * changing rate or mode (bypass most of the rest of this function).
  1635. * Set up new rate table in uCode only if old rate is not supported
  1636. * in current association (use new rate found above).
  1637. */
  1638. fail_count = win->counter - win->success_counter;
  1639. if (fail_count < RATE_MIN_FAILURE_TH &&
  1640. win->success_counter < RATE_MIN_SUCCESS_TH) {
  1641. D_RATE("LQ: still below TH. succ=%d total=%d "
  1642. "for idx %d\n",
  1643. win->success_counter, win->counter, idx);
  1644. /* Can't calculate this yet; not enough history */
  1645. win->average_tpt = IL_INVALID_VALUE;
  1646. /* Should we stay with this modulation mode,
  1647. * or search for a new one? */
  1648. il4965_rs_stay_in_table(lq_sta, false);
  1649. goto out;
  1650. }
  1651. /* Else we have enough samples; calculate estimate of
  1652. * actual average throughput */
  1653. if (win->average_tpt != ((win->success_ratio *
  1654. tbl->expected_tpt[idx] + 64) / 128)) {
  1655. IL_ERR(
  1656. "expected_tpt should have been calculated by now\n");
  1657. win->average_tpt = ((win->success_ratio *
  1658. tbl->expected_tpt[idx] + 64) / 128);
  1659. }
  1660. /* If we are searching for better modulation mode, check success. */
  1661. if (lq_sta->search_better_tbl) {
  1662. /* If good success, continue using the "search" mode;
  1663. * no need to send new link quality command, since we're
  1664. * continuing to use the setup that we've been trying. */
  1665. if (win->average_tpt > lq_sta->last_tpt) {
  1666. D_RATE("LQ: SWITCHING TO NEW TBL "
  1667. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1668. win->success_ratio,
  1669. win->average_tpt,
  1670. lq_sta->last_tpt);
  1671. if (!is_legacy(tbl->lq_type))
  1672. lq_sta->enable_counter = 1;
  1673. /* Swap tables; "search" becomes "active" */
  1674. lq_sta->active_tbl = active_tbl;
  1675. current_tpt = win->average_tpt;
  1676. /* Else poor success; go back to mode in "active" table */
  1677. } else {
  1678. D_RATE("LQ: GOING BACK TO THE OLD TBL "
  1679. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1680. win->success_ratio,
  1681. win->average_tpt,
  1682. lq_sta->last_tpt);
  1683. /* Nullify "search" table */
  1684. tbl->lq_type = LQ_NONE;
  1685. /* Revert to "active" table */
  1686. active_tbl = lq_sta->active_tbl;
  1687. tbl = &(lq_sta->lq_info[active_tbl]);
  1688. /* Revert to "active" rate and throughput info */
  1689. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1690. current_tpt = lq_sta->last_tpt;
  1691. /* Need to set up a new rate table in uCode */
  1692. update_lq = 1;
  1693. }
  1694. /* Either way, we've made a decision; modulation mode
  1695. * search is done, allow rate adjustment next time. */
  1696. lq_sta->search_better_tbl = 0;
  1697. done_search = 1; /* Don't switch modes below! */
  1698. goto lq_update;
  1699. }
  1700. /* (Else) not in search of better modulation mode, try for better
  1701. * starting rate, while staying in this mode. */
  1702. high_low = il4965_rs_get_adjacent_rate(il, idx,
  1703. rate_scale_idx_msk,
  1704. tbl->lq_type);
  1705. low = high_low & 0xff;
  1706. high = (high_low >> 8) & 0xff;
  1707. /* If user set max rate, dont allow higher than user constrain */
  1708. if (lq_sta->max_rate_idx != -1 &&
  1709. lq_sta->max_rate_idx < high)
  1710. high = RATE_INVALID;
  1711. sr = win->success_ratio;
  1712. /* Collect measured throughputs for current and adjacent rates */
  1713. current_tpt = win->average_tpt;
  1714. if (low != RATE_INVALID)
  1715. low_tpt = tbl->win[low].average_tpt;
  1716. if (high != RATE_INVALID)
  1717. high_tpt = tbl->win[high].average_tpt;
  1718. scale_action = 0;
  1719. /* Too many failures, decrease rate */
  1720. if (sr <= RATE_DECREASE_TH || current_tpt == 0) {
  1721. D_RATE(
  1722. "decrease rate because of low success_ratio\n");
  1723. scale_action = -1;
  1724. /* No throughput measured yet for adjacent rates; try increase. */
  1725. } else if (low_tpt == IL_INVALID_VALUE &&
  1726. high_tpt == IL_INVALID_VALUE) {
  1727. if (high != RATE_INVALID && sr >= RATE_INCREASE_TH)
  1728. scale_action = 1;
  1729. else if (low != RATE_INVALID)
  1730. scale_action = 0;
  1731. }
  1732. /* Both adjacent throughputs are measured, but neither one has better
  1733. * throughput; we're using the best rate, don't change it! */
  1734. else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE &&
  1735. low_tpt < current_tpt && high_tpt < current_tpt)
  1736. scale_action = 0;
  1737. /* At least one adjacent rate's throughput is measured,
  1738. * and may have better performance. */
  1739. else {
  1740. /* Higher adjacent rate's throughput is measured */
  1741. if (high_tpt != IL_INVALID_VALUE) {
  1742. /* Higher rate has better throughput */
  1743. if (high_tpt > current_tpt &&
  1744. sr >= RATE_INCREASE_TH) {
  1745. scale_action = 1;
  1746. } else {
  1747. scale_action = 0;
  1748. }
  1749. /* Lower adjacent rate's throughput is measured */
  1750. } else if (low_tpt != IL_INVALID_VALUE) {
  1751. /* Lower rate has better throughput */
  1752. if (low_tpt > current_tpt) {
  1753. D_RATE(
  1754. "decrease rate because of low tpt\n");
  1755. scale_action = -1;
  1756. } else if (sr >= RATE_INCREASE_TH) {
  1757. scale_action = 1;
  1758. }
  1759. }
  1760. }
  1761. /* Sanity check; asked for decrease, but success rate or throughput
  1762. * has been good at old rate. Don't change it. */
  1763. if (scale_action == -1 && low != RATE_INVALID &&
  1764. (sr > RATE_HIGH_TH || current_tpt > 100 * tbl->expected_tpt[low]))
  1765. scale_action = 0;
  1766. switch (scale_action) {
  1767. case -1:
  1768. /* Decrease starting rate, update uCode's rate table */
  1769. if (low != RATE_INVALID) {
  1770. update_lq = 1;
  1771. idx = low;
  1772. }
  1773. break;
  1774. case 1:
  1775. /* Increase starting rate, update uCode's rate table */
  1776. if (high != RATE_INVALID) {
  1777. update_lq = 1;
  1778. idx = high;
  1779. }
  1780. break;
  1781. case 0:
  1782. /* No change */
  1783. default:
  1784. break;
  1785. }
  1786. D_RATE("choose rate scale idx %d action %d low %d "
  1787. "high %d type %d\n",
  1788. idx, scale_action, low, high, tbl->lq_type);
  1789. lq_update:
  1790. /* Replace uCode's rate table for the destination station. */
  1791. if (update_lq)
  1792. rate = il4965_rs_update_rate_tbl(il, ctx, lq_sta,
  1793. tbl, idx, is_green);
  1794. /* Should we stay with this modulation mode,
  1795. * or search for a new one? */
  1796. il4965_rs_stay_in_table(lq_sta, false);
  1797. /*
  1798. * Search for new modulation mode if we're:
  1799. * 1) Not changing rates right now
  1800. * 2) Not just finishing up a search
  1801. * 3) Allowing a new search
  1802. */
  1803. if (!update_lq && !done_search && !lq_sta->stay_in_tbl &&
  1804. win->counter) {
  1805. /* Save current throughput to compare with "search" throughput*/
  1806. lq_sta->last_tpt = current_tpt;
  1807. /* Select a new "search" modulation mode to try.
  1808. * If one is found, set up the new "search" table. */
  1809. if (is_legacy(tbl->lq_type))
  1810. il4965_rs_move_legacy_other(il, lq_sta,
  1811. conf, sta, idx);
  1812. else if (is_siso(tbl->lq_type))
  1813. il4965_rs_move_siso_to_other(il, lq_sta,
  1814. conf, sta, idx);
  1815. else /* (is_mimo2(tbl->lq_type)) */
  1816. il4965_rs_move_mimo2_to_other(il, lq_sta,
  1817. conf, sta, idx);
  1818. /* If new "search" mode was selected, set up in uCode table */
  1819. if (lq_sta->search_better_tbl) {
  1820. /* Access the "search" table, clear its history. */
  1821. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1822. for (i = 0; i < RATE_COUNT; i++)
  1823. il4965_rs_rate_scale_clear_win(
  1824. &(tbl->win[i]));
  1825. /* Use new "search" start rate */
  1826. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1827. D_RATE(
  1828. "Switch current mcs: %X idx: %d\n",
  1829. tbl->current_rate, idx);
  1830. il4965_rs_fill_link_cmd(il, lq_sta,
  1831. tbl->current_rate);
  1832. il_send_lq_cmd(il, ctx,
  1833. &lq_sta->lq, CMD_ASYNC, false);
  1834. } else
  1835. done_search = 1;
  1836. }
  1837. if (done_search && !lq_sta->stay_in_tbl) {
  1838. /* If the "active" (non-search) mode was legacy,
  1839. * and we've tried switching antennas,
  1840. * but we haven't been able to try HT modes (not available),
  1841. * stay with best antenna legacy modulation for a while
  1842. * before next round of mode comparisons. */
  1843. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1844. if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
  1845. lq_sta->action_counter > tbl1->max_search) {
  1846. D_RATE("LQ: STAY in legacy table\n");
  1847. il4965_rs_set_stay_in_table(il, 1, lq_sta);
  1848. }
  1849. /* If we're in an HT mode, and all 3 mode switch actions
  1850. * have been tried and compared, stay in this best modulation
  1851. * mode for a while before next round of mode comparisons. */
  1852. if (lq_sta->enable_counter &&
  1853. lq_sta->action_counter >= tbl1->max_search) {
  1854. if (lq_sta->last_tpt > IL_AGG_TPT_THREHOLD &&
  1855. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  1856. tid != MAX_TID_COUNT) {
  1857. tid_data =
  1858. &il->stations[lq_sta->lq.sta_id].tid[tid];
  1859. if (tid_data->agg.state == IL_AGG_OFF) {
  1860. D_RATE(
  1861. "try to aggregate tid %d\n",
  1862. tid);
  1863. il4965_rs_tl_turn_on_agg(il, tid,
  1864. lq_sta, sta);
  1865. }
  1866. }
  1867. il4965_rs_set_stay_in_table(il, 0, lq_sta);
  1868. }
  1869. }
  1870. out:
  1871. tbl->current_rate = il4965_rate_n_flags_from_tbl(il, tbl,
  1872. idx, is_green);
  1873. i = idx;
  1874. lq_sta->last_txrate_idx = i;
  1875. }
  1876. /**
  1877. * il4965_rs_initialize_lq - Initialize a station's hardware rate table
  1878. *
  1879. * The uCode's station table contains a table of fallback rates
  1880. * for automatic fallback during transmission.
  1881. *
  1882. * NOTE: This sets up a default set of values. These will be replaced later
  1883. * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
  1884. * rc80211_simple.
  1885. *
  1886. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  1887. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  1888. * which requires station table entry to exist).
  1889. */
  1890. static void il4965_rs_initialize_lq(struct il_priv *il,
  1891. struct ieee80211_conf *conf,
  1892. struct ieee80211_sta *sta,
  1893. struct il_lq_sta *lq_sta)
  1894. {
  1895. struct il_scale_tbl_info *tbl;
  1896. int rate_idx;
  1897. int i;
  1898. u32 rate;
  1899. u8 use_green = il4965_rs_use_green(sta);
  1900. u8 active_tbl = 0;
  1901. u8 valid_tx_ant;
  1902. struct il_station_priv *sta_priv;
  1903. struct il_rxon_context *ctx;
  1904. if (!sta || !lq_sta)
  1905. return;
  1906. sta_priv = (void *)sta->drv_priv;
  1907. ctx = sta_priv->common.ctx;
  1908. i = lq_sta->last_txrate_idx;
  1909. valid_tx_ant = il->hw_params.valid_tx_ant;
  1910. if (!lq_sta->search_better_tbl)
  1911. active_tbl = lq_sta->active_tbl;
  1912. else
  1913. active_tbl = 1 - lq_sta->active_tbl;
  1914. tbl = &(lq_sta->lq_info[active_tbl]);
  1915. if (i < 0 || i >= RATE_COUNT)
  1916. i = 0;
  1917. rate = il_rates[i].plcp;
  1918. tbl->ant_type = il4965_first_antenna(valid_tx_ant);
  1919. rate |= tbl->ant_type << RATE_MCS_ANT_POS;
  1920. if (i >= IL_FIRST_CCK_RATE && i <= IL_LAST_CCK_RATE)
  1921. rate |= RATE_MCS_CCK_MSK;
  1922. il4965_rs_get_tbl_info_from_mcs(rate, il->band, tbl, &rate_idx);
  1923. if (!il4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
  1924. il4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
  1925. rate = il4965_rate_n_flags_from_tbl(il, tbl, rate_idx, use_green);
  1926. tbl->current_rate = rate;
  1927. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1928. il4965_rs_fill_link_cmd(NULL, lq_sta, rate);
  1929. il->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
  1930. il_send_lq_cmd(il, ctx, &lq_sta->lq, CMD_SYNC, true);
  1931. }
  1932. static void
  1933. il4965_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
  1934. struct ieee80211_tx_rate_control *txrc)
  1935. {
  1936. struct sk_buff *skb = txrc->skb;
  1937. struct ieee80211_supported_band *sband = txrc->sband;
  1938. struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
  1939. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1940. struct il_lq_sta *lq_sta = il_sta;
  1941. int rate_idx;
  1942. D_RATE("rate scale calculate new rate for skb\n");
  1943. /* Get max rate if user set max rate */
  1944. if (lq_sta) {
  1945. lq_sta->max_rate_idx = txrc->max_rate_idx;
  1946. if (sband->band == IEEE80211_BAND_5GHZ &&
  1947. lq_sta->max_rate_idx != -1)
  1948. lq_sta->max_rate_idx += IL_FIRST_OFDM_RATE;
  1949. if (lq_sta->max_rate_idx < 0 ||
  1950. lq_sta->max_rate_idx >= RATE_COUNT)
  1951. lq_sta->max_rate_idx = -1;
  1952. }
  1953. /* Treat uninitialized rate scaling data same as non-existing. */
  1954. if (lq_sta && !lq_sta->drv) {
  1955. D_RATE("Rate scaling not initialized yet.\n");
  1956. il_sta = NULL;
  1957. }
  1958. /* Send management frames and NO_ACK data using lowest rate. */
  1959. if (rate_control_send_low(sta, il_sta, txrc))
  1960. return;
  1961. if (!lq_sta)
  1962. return;
  1963. rate_idx = lq_sta->last_txrate_idx;
  1964. if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
  1965. rate_idx -= IL_FIRST_OFDM_RATE;
  1966. /* 6M and 9M shared same MCS idx */
  1967. rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
  1968. if (il4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
  1969. RATE_MIMO2_6M_PLCP)
  1970. rate_idx = rate_idx + MCS_IDX_PER_STREAM;
  1971. info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  1972. if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
  1973. info->control.rates[0].flags |=
  1974. IEEE80211_TX_RC_SHORT_GI;
  1975. if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
  1976. info->control.rates[0].flags |=
  1977. IEEE80211_TX_RC_DUP_DATA;
  1978. if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
  1979. info->control.rates[0].flags |=
  1980. IEEE80211_TX_RC_40_MHZ_WIDTH;
  1981. if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
  1982. info->control.rates[0].flags |=
  1983. IEEE80211_TX_RC_GREEN_FIELD;
  1984. } else {
  1985. /* Check for invalid rates */
  1986. if (rate_idx < 0 || rate_idx >= RATE_COUNT_LEGACY ||
  1987. (sband->band == IEEE80211_BAND_5GHZ &&
  1988. rate_idx < IL_FIRST_OFDM_RATE))
  1989. rate_idx = rate_lowest_index(sband, sta);
  1990. /* On valid 5 GHz rate, adjust idx */
  1991. else if (sband->band == IEEE80211_BAND_5GHZ)
  1992. rate_idx -= IL_FIRST_OFDM_RATE;
  1993. info->control.rates[0].flags = 0;
  1994. }
  1995. info->control.rates[0].idx = rate_idx;
  1996. }
  1997. static void *il4965_rs_alloc_sta(void *il_rate, struct ieee80211_sta *sta,
  1998. gfp_t gfp)
  1999. {
  2000. struct il_lq_sta *lq_sta;
  2001. struct il_station_priv *sta_priv =
  2002. (struct il_station_priv *) sta->drv_priv;
  2003. struct il_priv *il;
  2004. il = (struct il_priv *)il_rate;
  2005. D_RATE("create station rate scale win\n");
  2006. lq_sta = &sta_priv->lq_sta;
  2007. return lq_sta;
  2008. }
  2009. /*
  2010. * Called after adding a new station to initialize rate scaling
  2011. */
  2012. void
  2013. il4965_rs_rate_init(struct il_priv *il,
  2014. struct ieee80211_sta *sta,
  2015. u8 sta_id)
  2016. {
  2017. int i, j;
  2018. struct ieee80211_hw *hw = il->hw;
  2019. struct ieee80211_conf *conf = &il->hw->conf;
  2020. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2021. struct il_station_priv *sta_priv;
  2022. struct il_lq_sta *lq_sta;
  2023. struct ieee80211_supported_band *sband;
  2024. sta_priv = (struct il_station_priv *) sta->drv_priv;
  2025. lq_sta = &sta_priv->lq_sta;
  2026. sband = hw->wiphy->bands[conf->channel->band];
  2027. lq_sta->lq.sta_id = sta_id;
  2028. for (j = 0; j < LQ_SIZE; j++)
  2029. for (i = 0; i < RATE_COUNT; i++)
  2030. il4965_rs_rate_scale_clear_win(
  2031. &lq_sta->lq_info[j].win[i]);
  2032. lq_sta->flush_timer = 0;
  2033. lq_sta->supp_rates = sta->supp_rates[sband->band];
  2034. for (j = 0; j < LQ_SIZE; j++)
  2035. for (i = 0; i < RATE_COUNT; i++)
  2036. il4965_rs_rate_scale_clear_win(
  2037. &lq_sta->lq_info[j].win[i]);
  2038. D_RATE("LQ:"
  2039. "*** rate scale station global init for station %d ***\n",
  2040. sta_id);
  2041. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2042. * the lowest or the highest rate.. Could consider using RSSI from
  2043. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2044. * after assoc.. */
  2045. lq_sta->is_dup = 0;
  2046. lq_sta->max_rate_idx = -1;
  2047. lq_sta->missed_rate_counter = IL_MISSED_RATE_MAX;
  2048. lq_sta->is_green = il4965_rs_use_green(sta);
  2049. lq_sta->active_legacy_rate = il->active_rate & ~(0x1000);
  2050. lq_sta->band = il->band;
  2051. /*
  2052. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  2053. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  2054. */
  2055. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2056. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2057. lq_sta->active_siso_rate &= ~((u16)0x2);
  2058. lq_sta->active_siso_rate <<= IL_FIRST_OFDM_RATE;
  2059. /* Same here */
  2060. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2061. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2062. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2063. lq_sta->active_mimo2_rate <<= IL_FIRST_OFDM_RATE;
  2064. /* These values will be overridden later */
  2065. lq_sta->lq.general_params.single_stream_ant_msk =
  2066. il4965_first_antenna(il->hw_params.valid_tx_ant);
  2067. lq_sta->lq.general_params.dual_stream_ant_msk =
  2068. il->hw_params.valid_tx_ant &
  2069. ~il4965_first_antenna(il->hw_params.valid_tx_ant);
  2070. if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
  2071. lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
  2072. } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
  2073. lq_sta->lq.general_params.dual_stream_ant_msk =
  2074. il->hw_params.valid_tx_ant;
  2075. }
  2076. /* as default allow aggregation for all tids */
  2077. lq_sta->tx_agg_tid_en = IL_AGG_ALL_TID;
  2078. lq_sta->drv = il;
  2079. /* Set last_txrate_idx to lowest rate */
  2080. lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
  2081. if (sband->band == IEEE80211_BAND_5GHZ)
  2082. lq_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
  2083. lq_sta->is_agg = 0;
  2084. #ifdef CONFIG_MAC80211_DEBUGFS
  2085. lq_sta->dbg_fixed_rate = 0;
  2086. #endif
  2087. il4965_rs_initialize_lq(il, conf, sta, lq_sta);
  2088. }
  2089. static void il4965_rs_fill_link_cmd(struct il_priv *il,
  2090. struct il_lq_sta *lq_sta, u32 new_rate)
  2091. {
  2092. struct il_scale_tbl_info tbl_type;
  2093. int idx = 0;
  2094. int rate_idx;
  2095. int repeat_rate = 0;
  2096. u8 ant_toggle_cnt = 0;
  2097. u8 use_ht_possible = 1;
  2098. u8 valid_tx_ant = 0;
  2099. struct il_link_quality_cmd *lq_cmd = &lq_sta->lq;
  2100. /* Override starting rate (idx 0) if needed for debug purposes */
  2101. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2102. /* Interpret new_rate (rate_n_flags) */
  2103. il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
  2104. &tbl_type, &rate_idx);
  2105. /* How many times should we repeat the initial rate? */
  2106. if (is_legacy(tbl_type.lq_type)) {
  2107. ant_toggle_cnt = 1;
  2108. repeat_rate = IL_NUMBER_TRY;
  2109. } else {
  2110. repeat_rate = IL_HT_NUMBER_TRY;
  2111. }
  2112. lq_cmd->general_params.mimo_delimiter =
  2113. is_mimo(tbl_type.lq_type) ? 1 : 0;
  2114. /* Fill 1st table entry (idx 0) */
  2115. lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
  2116. if (il4965_num_of_ant(tbl_type.ant_type) == 1) {
  2117. lq_cmd->general_params.single_stream_ant_msk =
  2118. tbl_type.ant_type;
  2119. } else if (il4965_num_of_ant(tbl_type.ant_type) == 2) {
  2120. lq_cmd->general_params.dual_stream_ant_msk =
  2121. tbl_type.ant_type;
  2122. } /* otherwise we don't modify the existing value */
  2123. idx++;
  2124. repeat_rate--;
  2125. if (il)
  2126. valid_tx_ant = il->hw_params.valid_tx_ant;
  2127. /* Fill rest of rate table */
  2128. while (idx < LINK_QUAL_MAX_RETRY_NUM) {
  2129. /* Repeat initial/next rate.
  2130. * For legacy IL_NUMBER_TRY == 1, this loop will not execute.
  2131. * For HT IL_HT_NUMBER_TRY == 3, this executes twice. */
  2132. while (repeat_rate > 0 && idx < LINK_QUAL_MAX_RETRY_NUM) {
  2133. if (is_legacy(tbl_type.lq_type)) {
  2134. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2135. ant_toggle_cnt++;
  2136. else if (il &&
  2137. il4965_rs_toggle_antenna(valid_tx_ant,
  2138. &new_rate, &tbl_type))
  2139. ant_toggle_cnt = 1;
  2140. }
  2141. /* Override next rate if needed for debug purposes */
  2142. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2143. /* Fill next table entry */
  2144. lq_cmd->rs_table[idx].rate_n_flags =
  2145. cpu_to_le32(new_rate);
  2146. repeat_rate--;
  2147. idx++;
  2148. }
  2149. il4965_rs_get_tbl_info_from_mcs(new_rate,
  2150. lq_sta->band, &tbl_type,
  2151. &rate_idx);
  2152. /* Indicate to uCode which entries might be MIMO.
  2153. * If initial rate was MIMO, this will finally end up
  2154. * as (IL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2155. if (is_mimo(tbl_type.lq_type))
  2156. lq_cmd->general_params.mimo_delimiter = idx;
  2157. /* Get next rate */
  2158. new_rate = il4965_rs_get_lower_rate(lq_sta,
  2159. &tbl_type, rate_idx,
  2160. use_ht_possible);
  2161. /* How many times should we repeat the next rate? */
  2162. if (is_legacy(tbl_type.lq_type)) {
  2163. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2164. ant_toggle_cnt++;
  2165. else if (il &&
  2166. il4965_rs_toggle_antenna(valid_tx_ant,
  2167. &new_rate, &tbl_type))
  2168. ant_toggle_cnt = 1;
  2169. repeat_rate = IL_NUMBER_TRY;
  2170. } else {
  2171. repeat_rate = IL_HT_NUMBER_TRY;
  2172. }
  2173. /* Don't allow HT rates after next pass.
  2174. * il4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  2175. use_ht_possible = 0;
  2176. /* Override next rate if needed for debug purposes */
  2177. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2178. /* Fill next table entry */
  2179. lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
  2180. idx++;
  2181. repeat_rate--;
  2182. }
  2183. lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2184. lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2185. lq_cmd->agg_params.agg_time_limit =
  2186. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2187. }
  2188. static void
  2189. *il4965_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2190. {
  2191. return hw->priv;
  2192. }
  2193. /* rate scale requires free function to be implemented */
  2194. static void il4965_rs_free(void *il_rate)
  2195. {
  2196. return;
  2197. }
  2198. static void il4965_rs_free_sta(void *il_r, struct ieee80211_sta *sta,
  2199. void *il_sta)
  2200. {
  2201. struct il_priv *il __maybe_unused = il_r;
  2202. D_RATE("enter\n");
  2203. D_RATE("leave\n");
  2204. }
  2205. #ifdef CONFIG_MAC80211_DEBUGFS
  2206. static int il4965_open_file_generic(struct inode *inode, struct file *file)
  2207. {
  2208. file->private_data = inode->i_private;
  2209. return 0;
  2210. }
  2211. static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
  2212. u32 *rate_n_flags, int idx)
  2213. {
  2214. struct il_priv *il;
  2215. u8 valid_tx_ant;
  2216. u8 ant_sel_tx;
  2217. il = lq_sta->drv;
  2218. valid_tx_ant = il->hw_params.valid_tx_ant;
  2219. if (lq_sta->dbg_fixed_rate) {
  2220. ant_sel_tx =
  2221. ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
  2222. >> RATE_MCS_ANT_POS);
  2223. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2224. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2225. D_RATE("Fixed rate ON\n");
  2226. } else {
  2227. lq_sta->dbg_fixed_rate = 0;
  2228. IL_ERR(
  2229. "Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2230. ant_sel_tx, valid_tx_ant);
  2231. D_RATE("Fixed rate OFF\n");
  2232. }
  2233. } else {
  2234. D_RATE("Fixed rate OFF\n");
  2235. }
  2236. }
  2237. static ssize_t il4965_rs_sta_dbgfs_scale_table_write(struct file *file,
  2238. const char __user *user_buf, size_t count, loff_t *ppos)
  2239. {
  2240. struct il_lq_sta *lq_sta = file->private_data;
  2241. struct il_priv *il;
  2242. char buf[64];
  2243. size_t buf_size;
  2244. u32 parsed_rate;
  2245. struct il_station_priv *sta_priv =
  2246. container_of(lq_sta, struct il_station_priv, lq_sta);
  2247. struct il_rxon_context *ctx = sta_priv->common.ctx;
  2248. il = lq_sta->drv;
  2249. memset(buf, 0, sizeof(buf));
  2250. buf_size = min(count, sizeof(buf) - 1);
  2251. if (copy_from_user(buf, user_buf, buf_size))
  2252. return -EFAULT;
  2253. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2254. lq_sta->dbg_fixed_rate = parsed_rate;
  2255. else
  2256. lq_sta->dbg_fixed_rate = 0;
  2257. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  2258. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2259. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2260. D_RATE("sta_id %d rate 0x%X\n",
  2261. lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
  2262. if (lq_sta->dbg_fixed_rate) {
  2263. il4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
  2264. il_send_lq_cmd(lq_sta->drv, ctx, &lq_sta->lq, CMD_ASYNC,
  2265. false);
  2266. }
  2267. return count;
  2268. }
  2269. static ssize_t il4965_rs_sta_dbgfs_scale_table_read(struct file *file,
  2270. char __user *user_buf, size_t count, loff_t *ppos)
  2271. {
  2272. char *buff;
  2273. int desc = 0;
  2274. int i = 0;
  2275. int idx = 0;
  2276. ssize_t ret;
  2277. struct il_lq_sta *lq_sta = file->private_data;
  2278. struct il_priv *il;
  2279. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2280. il = lq_sta->drv;
  2281. buff = kmalloc(1024, GFP_KERNEL);
  2282. if (!buff)
  2283. return -ENOMEM;
  2284. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2285. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2286. lq_sta->total_failed, lq_sta->total_success,
  2287. lq_sta->active_legacy_rate);
  2288. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2289. lq_sta->dbg_fixed_rate);
  2290. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2291. (il->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
  2292. (il->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
  2293. (il->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
  2294. desc += sprintf(buff+desc, "lq type %s\n",
  2295. (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
  2296. if (is_Ht(tbl->lq_type)) {
  2297. desc += sprintf(buff+desc, " %s",
  2298. (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
  2299. desc += sprintf(buff+desc, " %s",
  2300. (tbl->is_ht40) ? "40MHz" : "20MHz");
  2301. desc += sprintf(buff+desc, " %s %s %s\n",
  2302. (tbl->is_SGI) ? "SGI" : "",
  2303. (lq_sta->is_green) ? "GF enabled" : "",
  2304. (lq_sta->is_agg) ? "AGG on" : "");
  2305. }
  2306. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2307. lq_sta->last_rate_n_flags);
  2308. desc += sprintf(buff+desc, "general:"
  2309. "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2310. lq_sta->lq.general_params.flags,
  2311. lq_sta->lq.general_params.mimo_delimiter,
  2312. lq_sta->lq.general_params.single_stream_ant_msk,
  2313. lq_sta->lq.general_params.dual_stream_ant_msk);
  2314. desc += sprintf(buff+desc, "agg:"
  2315. "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2316. le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
  2317. lq_sta->lq.agg_params.agg_dis_start_th,
  2318. lq_sta->lq.agg_params.agg_frame_cnt_limit);
  2319. desc += sprintf(buff+desc,
  2320. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2321. lq_sta->lq.general_params.start_rate_idx[0],
  2322. lq_sta->lq.general_params.start_rate_idx[1],
  2323. lq_sta->lq.general_params.start_rate_idx[2],
  2324. lq_sta->lq.general_params.start_rate_idx[3]);
  2325. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2326. idx = il4965_hwrate_to_plcp_idx(
  2327. le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
  2328. if (is_legacy(tbl->lq_type)) {
  2329. desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
  2330. i,
  2331. le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
  2332. il_rate_mcs[idx].mbps);
  2333. } else {
  2334. desc += sprintf(buff+desc,
  2335. " rate[%d] 0x%X %smbps (%s)\n",
  2336. i,
  2337. le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags),
  2338. il_rate_mcs[idx].mbps, il_rate_mcs[idx].mcs);
  2339. }
  2340. }
  2341. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2342. kfree(buff);
  2343. return ret;
  2344. }
  2345. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2346. .write = il4965_rs_sta_dbgfs_scale_table_write,
  2347. .read = il4965_rs_sta_dbgfs_scale_table_read,
  2348. .open = il4965_open_file_generic,
  2349. .llseek = default_llseek,
  2350. };
  2351. static ssize_t il4965_rs_sta_dbgfs_stats_table_read(struct file *file,
  2352. char __user *user_buf, size_t count, loff_t *ppos)
  2353. {
  2354. char *buff;
  2355. int desc = 0;
  2356. int i, j;
  2357. ssize_t ret;
  2358. struct il_lq_sta *lq_sta = file->private_data;
  2359. buff = kmalloc(1024, GFP_KERNEL);
  2360. if (!buff)
  2361. return -ENOMEM;
  2362. for (i = 0; i < LQ_SIZE; i++) {
  2363. desc += sprintf(buff+desc,
  2364. "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
  2365. "rate=0x%X\n",
  2366. lq_sta->active_tbl == i ? "*" : "x",
  2367. lq_sta->lq_info[i].lq_type,
  2368. lq_sta->lq_info[i].is_SGI,
  2369. lq_sta->lq_info[i].is_ht40,
  2370. lq_sta->lq_info[i].is_dup,
  2371. lq_sta->is_green,
  2372. lq_sta->lq_info[i].current_rate);
  2373. for (j = 0; j < RATE_COUNT; j++) {
  2374. desc += sprintf(buff+desc,
  2375. "counter=%d success=%d %%=%d\n",
  2376. lq_sta->lq_info[i].win[j].counter,
  2377. lq_sta->lq_info[i].win[j].success_counter,
  2378. lq_sta->lq_info[i].win[j].success_ratio);
  2379. }
  2380. }
  2381. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2382. kfree(buff);
  2383. return ret;
  2384. }
  2385. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2386. .read = il4965_rs_sta_dbgfs_stats_table_read,
  2387. .open = il4965_open_file_generic,
  2388. .llseek = default_llseek,
  2389. };
  2390. static ssize_t il4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
  2391. char __user *user_buf, size_t count, loff_t *ppos)
  2392. {
  2393. char buff[120];
  2394. int desc = 0;
  2395. ssize_t ret;
  2396. struct il_lq_sta *lq_sta = file->private_data;
  2397. struct il_priv *il;
  2398. struct il_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
  2399. il = lq_sta->drv;
  2400. if (is_Ht(tbl->lq_type))
  2401. desc += sprintf(buff+desc,
  2402. "Bit Rate= %d Mb/s\n",
  2403. tbl->expected_tpt[lq_sta->last_txrate_idx]);
  2404. else
  2405. desc += sprintf(buff+desc,
  2406. "Bit Rate= %d Mb/s\n",
  2407. il_rates[lq_sta->last_txrate_idx].ieee >> 1);
  2408. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2409. return ret;
  2410. }
  2411. static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
  2412. .read = il4965_rs_sta_dbgfs_rate_scale_data_read,
  2413. .open = il4965_open_file_generic,
  2414. .llseek = default_llseek,
  2415. };
  2416. static void il4965_rs_add_debugfs(void *il, void *il_sta,
  2417. struct dentry *dir)
  2418. {
  2419. struct il_lq_sta *lq_sta = il_sta;
  2420. lq_sta->rs_sta_dbgfs_scale_table_file =
  2421. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2422. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2423. lq_sta->rs_sta_dbgfs_stats_table_file =
  2424. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  2425. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2426. lq_sta->rs_sta_dbgfs_rate_scale_data_file =
  2427. debugfs_create_file("rate_scale_data", S_IRUSR, dir,
  2428. lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
  2429. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2430. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2431. &lq_sta->tx_agg_tid_en);
  2432. }
  2433. static void il4965_rs_remove_debugfs(void *il, void *il_sta)
  2434. {
  2435. struct il_lq_sta *lq_sta = il_sta;
  2436. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2437. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2438. debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
  2439. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2440. }
  2441. #endif
  2442. /*
  2443. * Initialization of rate scaling information is done by driver after
  2444. * the station is added. Since mac80211 calls this function before a
  2445. * station is added we ignore it.
  2446. */
  2447. static void
  2448. il4965_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
  2449. struct ieee80211_sta *sta, void *il_sta)
  2450. {
  2451. }
  2452. static struct rate_control_ops rs_4965_ops = {
  2453. .module = NULL,
  2454. .name = IL4965_RS_NAME,
  2455. .tx_status = il4965_rs_tx_status,
  2456. .get_rate = il4965_rs_get_rate,
  2457. .rate_init = il4965_rs_rate_init_stub,
  2458. .alloc = il4965_rs_alloc,
  2459. .free = il4965_rs_free,
  2460. .alloc_sta = il4965_rs_alloc_sta,
  2461. .free_sta = il4965_rs_free_sta,
  2462. #ifdef CONFIG_MAC80211_DEBUGFS
  2463. .add_sta_debugfs = il4965_rs_add_debugfs,
  2464. .remove_sta_debugfs = il4965_rs_remove_debugfs,
  2465. #endif
  2466. };
  2467. int il4965_rate_control_register(void)
  2468. {
  2469. return ieee80211_rate_control_register(&rs_4965_ops);
  2470. }
  2471. void il4965_rate_control_unregister(void)
  2472. {
  2473. ieee80211_rate_control_unregister(&rs_4965_ops);
  2474. }