rs.c 93 KB

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