rs.c 99 KB

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