rs.c 98 KB

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