iwl-4965-rs.c 84 KB

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