iwl-agn-rs.c 99 KB

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