rs.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  5. * Copyright(c) 2016 Intel Deutschland GmbH
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along with
  17. * this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  19. *
  20. * The full GNU General Public License is included in this distribution in the
  21. * file called LICENSE.
  22. *
  23. * Contact Information:
  24. * Intel Linux Wireless <linuxwifi@intel.com>
  25. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  26. *
  27. *****************************************************************************/
  28. #include <linux/kernel.h>
  29. #include <linux/skbuff.h>
  30. #include <linux/slab.h>
  31. #include <net/mac80211.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/etherdevice.h>
  34. #include <linux/delay.h>
  35. #include <linux/workqueue.h>
  36. #include "rs.h"
  37. #include "fw-api.h"
  38. #include "sta.h"
  39. #include "iwl-op-mode.h"
  40. #include "mvm.h"
  41. #include "debugfs.h"
  42. #define RS_NAME "iwl-mvm-rs"
  43. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  44. /* Calculations of success ratio are done in fixed point where 12800 is 100%.
  45. * Use this macro when dealing with thresholds consts set as a percentage
  46. */
  47. #define RS_PERCENT(x) (128 * x)
  48. static u8 rs_ht_to_legacy[] = {
  49. [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
  50. [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
  51. [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
  52. [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
  53. [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
  54. [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
  55. [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
  56. [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
  57. [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
  58. [IWL_RATE_MCS_9_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, rp, rn) \
  71. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  72. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  73. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  74. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  75. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
  76. IWL_RATE_##rp##M_INDEX, \
  77. IWL_RATE_##rn##M_INDEX }
  78. #define IWL_DECLARE_MCS_RATE(s) \
  79. [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
  80. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  81. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  82. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  83. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
  84. IWL_RATE_INVM_INDEX, \
  85. IWL_RATE_INVM_INDEX }
  86. /*
  87. * Parameter order:
  88. * rate, ht rate, prev rate, next rate
  89. *
  90. * If there isn't a valid next or previous rate then INV is used which
  91. * maps to IWL_RATE_INVALID
  92. *
  93. */
  94. static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
  95. IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
  96. IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
  97. IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
  98. IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
  99. IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
  100. IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
  101. IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
  102. IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
  103. IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
  104. IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
  105. IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
  106. IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
  107. IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
  108. IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
  109. IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
  110. };
  111. enum rs_action {
  112. RS_ACTION_STAY = 0,
  113. RS_ACTION_DOWNSCALE = -1,
  114. RS_ACTION_UPSCALE = 1,
  115. };
  116. enum rs_column_mode {
  117. RS_INVALID = 0,
  118. RS_LEGACY,
  119. RS_SISO,
  120. RS_MIMO2,
  121. };
  122. #define MAX_NEXT_COLUMNS 7
  123. #define MAX_COLUMN_CHECKS 3
  124. struct rs_tx_column;
  125. typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
  126. struct ieee80211_sta *sta,
  127. struct rs_rate *rate,
  128. const struct rs_tx_column *next_col);
  129. struct rs_tx_column {
  130. enum rs_column_mode mode;
  131. u8 ant;
  132. bool sgi;
  133. enum rs_column next_columns[MAX_NEXT_COLUMNS];
  134. allow_column_func_t checks[MAX_COLUMN_CHECKS];
  135. };
  136. static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  137. struct rs_rate *rate,
  138. const struct rs_tx_column *next_col)
  139. {
  140. return iwl_mvm_bt_coex_is_ant_avail(mvm, next_col->ant);
  141. }
  142. static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  143. struct rs_rate *rate,
  144. const struct rs_tx_column *next_col)
  145. {
  146. struct iwl_mvm_sta *mvmsta;
  147. struct iwl_mvm_vif *mvmvif;
  148. if (!sta->ht_cap.ht_supported)
  149. return false;
  150. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  151. return false;
  152. if (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) < 2)
  153. return false;
  154. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  155. return false;
  156. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  157. mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  158. if (mvm->nvm_data->sku_cap_mimo_disabled)
  159. return false;
  160. return true;
  161. }
  162. static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  163. struct rs_rate *rate,
  164. const struct rs_tx_column *next_col)
  165. {
  166. if (!sta->ht_cap.ht_supported)
  167. return false;
  168. return true;
  169. }
  170. static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  171. struct rs_rate *rate,
  172. const struct rs_tx_column *next_col)
  173. {
  174. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  175. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  176. if (is_ht20(rate) && (ht_cap->cap &
  177. IEEE80211_HT_CAP_SGI_20))
  178. return true;
  179. if (is_ht40(rate) && (ht_cap->cap &
  180. IEEE80211_HT_CAP_SGI_40))
  181. return true;
  182. if (is_ht80(rate) && (vht_cap->cap &
  183. IEEE80211_VHT_CAP_SHORT_GI_80))
  184. return true;
  185. return false;
  186. }
  187. static const struct rs_tx_column rs_tx_columns[] = {
  188. [RS_COLUMN_LEGACY_ANT_A] = {
  189. .mode = RS_LEGACY,
  190. .ant = ANT_A,
  191. .next_columns = {
  192. RS_COLUMN_LEGACY_ANT_B,
  193. RS_COLUMN_SISO_ANT_A,
  194. RS_COLUMN_MIMO2,
  195. RS_COLUMN_INVALID,
  196. RS_COLUMN_INVALID,
  197. RS_COLUMN_INVALID,
  198. RS_COLUMN_INVALID,
  199. },
  200. .checks = {
  201. rs_ant_allow,
  202. },
  203. },
  204. [RS_COLUMN_LEGACY_ANT_B] = {
  205. .mode = RS_LEGACY,
  206. .ant = ANT_B,
  207. .next_columns = {
  208. RS_COLUMN_LEGACY_ANT_A,
  209. RS_COLUMN_SISO_ANT_B,
  210. RS_COLUMN_MIMO2,
  211. RS_COLUMN_INVALID,
  212. RS_COLUMN_INVALID,
  213. RS_COLUMN_INVALID,
  214. RS_COLUMN_INVALID,
  215. },
  216. .checks = {
  217. rs_ant_allow,
  218. },
  219. },
  220. [RS_COLUMN_SISO_ANT_A] = {
  221. .mode = RS_SISO,
  222. .ant = ANT_A,
  223. .next_columns = {
  224. RS_COLUMN_SISO_ANT_B,
  225. RS_COLUMN_MIMO2,
  226. RS_COLUMN_SISO_ANT_A_SGI,
  227. RS_COLUMN_LEGACY_ANT_A,
  228. RS_COLUMN_LEGACY_ANT_B,
  229. RS_COLUMN_INVALID,
  230. RS_COLUMN_INVALID,
  231. },
  232. .checks = {
  233. rs_siso_allow,
  234. rs_ant_allow,
  235. },
  236. },
  237. [RS_COLUMN_SISO_ANT_B] = {
  238. .mode = RS_SISO,
  239. .ant = ANT_B,
  240. .next_columns = {
  241. RS_COLUMN_SISO_ANT_A,
  242. RS_COLUMN_MIMO2,
  243. RS_COLUMN_SISO_ANT_B_SGI,
  244. RS_COLUMN_LEGACY_ANT_A,
  245. RS_COLUMN_LEGACY_ANT_B,
  246. RS_COLUMN_INVALID,
  247. RS_COLUMN_INVALID,
  248. },
  249. .checks = {
  250. rs_siso_allow,
  251. rs_ant_allow,
  252. },
  253. },
  254. [RS_COLUMN_SISO_ANT_A_SGI] = {
  255. .mode = RS_SISO,
  256. .ant = ANT_A,
  257. .sgi = true,
  258. .next_columns = {
  259. RS_COLUMN_SISO_ANT_B_SGI,
  260. RS_COLUMN_MIMO2_SGI,
  261. RS_COLUMN_SISO_ANT_A,
  262. RS_COLUMN_LEGACY_ANT_A,
  263. RS_COLUMN_LEGACY_ANT_B,
  264. RS_COLUMN_INVALID,
  265. RS_COLUMN_INVALID,
  266. },
  267. .checks = {
  268. rs_siso_allow,
  269. rs_ant_allow,
  270. rs_sgi_allow,
  271. },
  272. },
  273. [RS_COLUMN_SISO_ANT_B_SGI] = {
  274. .mode = RS_SISO,
  275. .ant = ANT_B,
  276. .sgi = true,
  277. .next_columns = {
  278. RS_COLUMN_SISO_ANT_A_SGI,
  279. RS_COLUMN_MIMO2_SGI,
  280. RS_COLUMN_SISO_ANT_B,
  281. RS_COLUMN_LEGACY_ANT_A,
  282. RS_COLUMN_LEGACY_ANT_B,
  283. RS_COLUMN_INVALID,
  284. RS_COLUMN_INVALID,
  285. },
  286. .checks = {
  287. rs_siso_allow,
  288. rs_ant_allow,
  289. rs_sgi_allow,
  290. },
  291. },
  292. [RS_COLUMN_MIMO2] = {
  293. .mode = RS_MIMO2,
  294. .ant = ANT_AB,
  295. .next_columns = {
  296. RS_COLUMN_SISO_ANT_A,
  297. RS_COLUMN_MIMO2_SGI,
  298. RS_COLUMN_LEGACY_ANT_A,
  299. RS_COLUMN_LEGACY_ANT_B,
  300. RS_COLUMN_INVALID,
  301. RS_COLUMN_INVALID,
  302. RS_COLUMN_INVALID,
  303. },
  304. .checks = {
  305. rs_mimo_allow,
  306. },
  307. },
  308. [RS_COLUMN_MIMO2_SGI] = {
  309. .mode = RS_MIMO2,
  310. .ant = ANT_AB,
  311. .sgi = true,
  312. .next_columns = {
  313. RS_COLUMN_SISO_ANT_A_SGI,
  314. RS_COLUMN_MIMO2,
  315. RS_COLUMN_LEGACY_ANT_A,
  316. RS_COLUMN_LEGACY_ANT_B,
  317. RS_COLUMN_INVALID,
  318. RS_COLUMN_INVALID,
  319. RS_COLUMN_INVALID,
  320. },
  321. .checks = {
  322. rs_mimo_allow,
  323. rs_sgi_allow,
  324. },
  325. },
  326. };
  327. static inline u8 rs_extract_rate(u32 rate_n_flags)
  328. {
  329. /* also works for HT because bits 7:6 are zero there */
  330. return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
  331. }
  332. static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
  333. {
  334. int idx = 0;
  335. if (rate_n_flags & RATE_MCS_HT_MSK) {
  336. idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
  337. idx += IWL_RATE_MCS_0_INDEX;
  338. /* skip 9M not supported in HT*/
  339. if (idx >= IWL_RATE_9M_INDEX)
  340. idx += 1;
  341. if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
  342. return idx;
  343. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  344. idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  345. idx += IWL_RATE_MCS_0_INDEX;
  346. /* skip 9M not supported in VHT*/
  347. if (idx >= IWL_RATE_9M_INDEX)
  348. idx++;
  349. if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
  350. return idx;
  351. } else {
  352. /* legacy rate format, search for match in table */
  353. u8 legacy_rate = rs_extract_rate(rate_n_flags);
  354. for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
  355. if (iwl_rates[idx].plcp == legacy_rate)
  356. return idx;
  357. }
  358. return IWL_RATE_INVALID;
  359. }
  360. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  361. struct ieee80211_sta *sta,
  362. struct iwl_lq_sta *lq_sta,
  363. int tid);
  364. static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
  365. struct ieee80211_sta *sta,
  366. struct iwl_lq_sta *lq_sta,
  367. const struct rs_rate *initial_rate);
  368. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
  369. /**
  370. * The following tables contain the expected throughput metrics for all rates
  371. *
  372. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  373. *
  374. * where invalid entries are zeros.
  375. *
  376. * CCK rates are only valid in legacy table and will only be used in G
  377. * (2.4 GHz) band.
  378. */
  379. static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
  380. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
  381. };
  382. /* Expected TpT tables. 4 indexes:
  383. * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
  384. */
  385. static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
  386. {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
  387. {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
  388. {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
  389. {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
  390. };
  391. static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
  392. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
  393. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
  394. {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
  395. {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
  396. };
  397. static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
  398. {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
  399. {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
  400. {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
  401. {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
  402. };
  403. static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
  404. {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
  405. {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
  406. {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
  407. {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
  408. };
  409. static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
  410. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
  411. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
  412. {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
  413. {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
  414. };
  415. static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
  416. {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
  417. {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
  418. {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
  419. {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
  420. };
  421. /* mbps, mcs */
  422. static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
  423. { "1", "BPSK DSSS"},
  424. { "2", "QPSK DSSS"},
  425. {"5.5", "BPSK CCK"},
  426. { "11", "QPSK CCK"},
  427. { "6", "BPSK 1/2"},
  428. { "9", "BPSK 1/2"},
  429. { "12", "QPSK 1/2"},
  430. { "18", "QPSK 3/4"},
  431. { "24", "16QAM 1/2"},
  432. { "36", "16QAM 3/4"},
  433. { "48", "64QAM 2/3"},
  434. { "54", "64QAM 3/4"},
  435. { "60", "64QAM 5/6"},
  436. };
  437. #define MCS_INDEX_PER_STREAM (8)
  438. static const char *rs_pretty_ant(u8 ant)
  439. {
  440. static const char * const ant_name[] = {
  441. [ANT_NONE] = "None",
  442. [ANT_A] = "A",
  443. [ANT_B] = "B",
  444. [ANT_AB] = "AB",
  445. [ANT_C] = "C",
  446. [ANT_AC] = "AC",
  447. [ANT_BC] = "BC",
  448. [ANT_ABC] = "ABC",
  449. };
  450. if (ant > ANT_ABC)
  451. return "UNKNOWN";
  452. return ant_name[ant];
  453. }
  454. static const char *rs_pretty_lq_type(enum iwl_table_type type)
  455. {
  456. static const char * const lq_types[] = {
  457. [LQ_NONE] = "NONE",
  458. [LQ_LEGACY_A] = "LEGACY_A",
  459. [LQ_LEGACY_G] = "LEGACY_G",
  460. [LQ_HT_SISO] = "HT SISO",
  461. [LQ_HT_MIMO2] = "HT MIMO",
  462. [LQ_VHT_SISO] = "VHT SISO",
  463. [LQ_VHT_MIMO2] = "VHT MIMO",
  464. };
  465. if (type < LQ_NONE || type >= LQ_MAX)
  466. return "UNKNOWN";
  467. return lq_types[type];
  468. }
  469. static char *rs_pretty_rate(const struct rs_rate *rate)
  470. {
  471. static char buf[40];
  472. static const char * const legacy_rates[] = {
  473. [IWL_RATE_1M_INDEX] = "1M",
  474. [IWL_RATE_2M_INDEX] = "2M",
  475. [IWL_RATE_5M_INDEX] = "5.5M",
  476. [IWL_RATE_11M_INDEX] = "11M",
  477. [IWL_RATE_6M_INDEX] = "6M",
  478. [IWL_RATE_9M_INDEX] = "9M",
  479. [IWL_RATE_12M_INDEX] = "12M",
  480. [IWL_RATE_18M_INDEX] = "18M",
  481. [IWL_RATE_24M_INDEX] = "24M",
  482. [IWL_RATE_36M_INDEX] = "36M",
  483. [IWL_RATE_48M_INDEX] = "48M",
  484. [IWL_RATE_54M_INDEX] = "54M",
  485. };
  486. static const char *const ht_vht_rates[] = {
  487. [IWL_RATE_MCS_0_INDEX] = "MCS0",
  488. [IWL_RATE_MCS_1_INDEX] = "MCS1",
  489. [IWL_RATE_MCS_2_INDEX] = "MCS2",
  490. [IWL_RATE_MCS_3_INDEX] = "MCS3",
  491. [IWL_RATE_MCS_4_INDEX] = "MCS4",
  492. [IWL_RATE_MCS_5_INDEX] = "MCS5",
  493. [IWL_RATE_MCS_6_INDEX] = "MCS6",
  494. [IWL_RATE_MCS_7_INDEX] = "MCS7",
  495. [IWL_RATE_MCS_8_INDEX] = "MCS8",
  496. [IWL_RATE_MCS_9_INDEX] = "MCS9",
  497. };
  498. const char *rate_str;
  499. if (is_type_legacy(rate->type) && (rate->index <= IWL_RATE_54M_INDEX))
  500. rate_str = legacy_rates[rate->index];
  501. else if ((is_type_ht(rate->type) || is_type_vht(rate->type)) &&
  502. (rate->index >= IWL_RATE_MCS_0_INDEX) &&
  503. (rate->index <= IWL_RATE_MCS_9_INDEX))
  504. rate_str = ht_vht_rates[rate->index];
  505. else
  506. rate_str = "BAD_RATE";
  507. sprintf(buf, "(%s|%s|%s)", rs_pretty_lq_type(rate->type),
  508. rs_pretty_ant(rate->ant), rate_str);
  509. return buf;
  510. }
  511. static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
  512. const char *prefix)
  513. {
  514. IWL_DEBUG_RATE(mvm,
  515. "%s: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
  516. prefix, rs_pretty_rate(rate), rate->bw,
  517. rate->sgi, rate->ldpc, rate->stbc);
  518. }
  519. static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  520. {
  521. window->data = 0;
  522. window->success_counter = 0;
  523. window->success_ratio = IWL_INVALID_VALUE;
  524. window->counter = 0;
  525. window->average_tpt = IWL_INVALID_VALUE;
  526. }
  527. static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
  528. struct iwl_scale_tbl_info *tbl)
  529. {
  530. int i;
  531. IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
  532. for (i = 0; i < IWL_RATE_COUNT; i++)
  533. rs_rate_scale_clear_window(&tbl->win[i]);
  534. for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
  535. rs_rate_scale_clear_window(&tbl->tpc_win[i]);
  536. }
  537. static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  538. {
  539. return (ant_type & valid_antenna) == ant_type;
  540. }
  541. static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
  542. struct iwl_lq_sta *lq_data, u8 tid,
  543. struct ieee80211_sta *sta)
  544. {
  545. int ret = -EAGAIN;
  546. IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
  547. sta->addr, tid);
  548. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  549. if (ret == -EAGAIN) {
  550. /*
  551. * driver and mac80211 is out of sync
  552. * this might be cause by reloading firmware
  553. * stop the tx ba session here
  554. */
  555. IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
  556. tid);
  557. ieee80211_stop_tx_ba_session(sta, tid);
  558. }
  559. return ret;
  560. }
  561. static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
  562. struct iwl_lq_sta *lq_data,
  563. struct ieee80211_sta *sta)
  564. {
  565. if (tid < IWL_MAX_TID_COUNT)
  566. rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
  567. else
  568. IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
  569. tid, IWL_MAX_TID_COUNT);
  570. }
  571. static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
  572. {
  573. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  574. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  575. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  576. }
  577. /*
  578. * Static function to get the expected throughput from an iwl_scale_tbl_info
  579. * that wraps a NULL pointer check
  580. */
  581. static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
  582. {
  583. if (tbl->expected_tpt)
  584. return tbl->expected_tpt[rs_index];
  585. return 0;
  586. }
  587. /**
  588. * rs_collect_tx_data - Update the success/failure sliding window
  589. *
  590. * We keep a sliding window of the last 62 packets transmitted
  591. * at this rate. window->data contains the bitmask of successful
  592. * packets.
  593. */
  594. static int _rs_collect_tx_data(struct iwl_mvm *mvm,
  595. struct iwl_scale_tbl_info *tbl,
  596. int scale_index, int attempts, int successes,
  597. struct iwl_rate_scale_data *window)
  598. {
  599. static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
  600. s32 fail_count, tpt;
  601. /* Get expected throughput */
  602. tpt = get_expected_tpt(tbl, scale_index);
  603. /*
  604. * Keep track of only the latest 62 tx frame attempts in this rate's
  605. * history window; anything older isn't really relevant any more.
  606. * If we have filled up the sliding window, drop the oldest attempt;
  607. * if the oldest attempt (highest bit in bitmap) shows "success",
  608. * subtract "1" from the success counter (this is the main reason
  609. * we keep these bitmaps!).
  610. */
  611. while (attempts > 0) {
  612. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  613. /* remove earliest */
  614. window->counter = IWL_RATE_MAX_WINDOW - 1;
  615. if (window->data & mask) {
  616. window->data &= ~mask;
  617. window->success_counter--;
  618. }
  619. }
  620. /* Increment frames-attempted counter */
  621. window->counter++;
  622. /* Shift bitmap by one frame to throw away oldest history */
  623. window->data <<= 1;
  624. /* Mark the most recent #successes attempts as successful */
  625. if (successes > 0) {
  626. window->success_counter++;
  627. window->data |= 0x1;
  628. successes--;
  629. }
  630. attempts--;
  631. }
  632. /* Calculate current success ratio, avoid divide-by-0! */
  633. if (window->counter > 0)
  634. window->success_ratio = 128 * (100 * window->success_counter)
  635. / window->counter;
  636. else
  637. window->success_ratio = IWL_INVALID_VALUE;
  638. fail_count = window->counter - window->success_counter;
  639. /* Calculate average throughput, if we have enough history. */
  640. if ((fail_count >= IWL_MVM_RS_RATE_MIN_FAILURE_TH) ||
  641. (window->success_counter >= IWL_MVM_RS_RATE_MIN_SUCCESS_TH))
  642. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  643. else
  644. window->average_tpt = IWL_INVALID_VALUE;
  645. return 0;
  646. }
  647. static int rs_collect_tpc_data(struct iwl_mvm *mvm,
  648. struct iwl_lq_sta *lq_sta,
  649. struct iwl_scale_tbl_info *tbl,
  650. int scale_index, int attempts, int successes,
  651. u8 reduced_txp)
  652. {
  653. struct iwl_rate_scale_data *window = NULL;
  654. if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
  655. return -EINVAL;
  656. window = &tbl->tpc_win[reduced_txp];
  657. return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
  658. window);
  659. }
  660. static int rs_collect_tlc_data(struct iwl_mvm *mvm,
  661. struct iwl_lq_sta *lq_sta,
  662. struct iwl_scale_tbl_info *tbl,
  663. int scale_index, int attempts, int successes)
  664. {
  665. struct iwl_rate_scale_data *window = NULL;
  666. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  667. return -EINVAL;
  668. if (tbl->column != RS_COLUMN_INVALID) {
  669. struct lq_sta_pers *pers = &lq_sta->pers;
  670. pers->tx_stats[tbl->column][scale_index].total += attempts;
  671. pers->tx_stats[tbl->column][scale_index].success += successes;
  672. }
  673. /* Select window for current tx bit rate */
  674. window = &(tbl->win[scale_index]);
  675. return _rs_collect_tx_data(mvm, tbl, scale_index, attempts, successes,
  676. window);
  677. }
  678. /* Convert rs_rate object into ucode rate bitmask */
  679. static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
  680. struct rs_rate *rate)
  681. {
  682. u32 ucode_rate = 0;
  683. int index = rate->index;
  684. ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
  685. RATE_MCS_ANT_ABC_MSK);
  686. if (is_legacy(rate)) {
  687. ucode_rate |= iwl_rates[index].plcp;
  688. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  689. ucode_rate |= RATE_MCS_CCK_MSK;
  690. return ucode_rate;
  691. }
  692. if (is_ht(rate)) {
  693. if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
  694. IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
  695. index = IWL_LAST_HT_RATE;
  696. }
  697. ucode_rate |= RATE_MCS_HT_MSK;
  698. if (is_ht_siso(rate))
  699. ucode_rate |= iwl_rates[index].plcp_ht_siso;
  700. else if (is_ht_mimo2(rate))
  701. ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
  702. else
  703. WARN_ON_ONCE(1);
  704. } else if (is_vht(rate)) {
  705. if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
  706. IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
  707. index = IWL_LAST_VHT_RATE;
  708. }
  709. ucode_rate |= RATE_MCS_VHT_MSK;
  710. if (is_vht_siso(rate))
  711. ucode_rate |= iwl_rates[index].plcp_vht_siso;
  712. else if (is_vht_mimo2(rate))
  713. ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
  714. else
  715. WARN_ON_ONCE(1);
  716. } else {
  717. IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
  718. }
  719. if (is_siso(rate) && rate->stbc) {
  720. /* To enable STBC we need to set both a flag and ANT_AB */
  721. ucode_rate |= RATE_MCS_ANT_AB_MSK;
  722. ucode_rate |= RATE_MCS_VHT_STBC_MSK;
  723. }
  724. ucode_rate |= rate->bw;
  725. if (rate->sgi)
  726. ucode_rate |= RATE_MCS_SGI_MSK;
  727. if (rate->ldpc)
  728. ucode_rate |= RATE_MCS_LDPC_MSK;
  729. return ucode_rate;
  730. }
  731. /* Convert a ucode rate into an rs_rate object */
  732. static int rs_rate_from_ucode_rate(const u32 ucode_rate,
  733. enum ieee80211_band band,
  734. struct rs_rate *rate)
  735. {
  736. u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
  737. u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
  738. u8 nss;
  739. memset(rate, 0, sizeof(*rate));
  740. rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
  741. if (rate->index == IWL_RATE_INVALID)
  742. return -EINVAL;
  743. rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
  744. /* Legacy */
  745. if (!(ucode_rate & RATE_MCS_HT_MSK) &&
  746. !(ucode_rate & RATE_MCS_VHT_MSK)) {
  747. if (num_of_ant == 1) {
  748. if (band == IEEE80211_BAND_5GHZ)
  749. rate->type = LQ_LEGACY_A;
  750. else
  751. rate->type = LQ_LEGACY_G;
  752. }
  753. return 0;
  754. }
  755. /* HT or VHT */
  756. if (ucode_rate & RATE_MCS_SGI_MSK)
  757. rate->sgi = true;
  758. if (ucode_rate & RATE_MCS_LDPC_MSK)
  759. rate->ldpc = true;
  760. if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
  761. rate->stbc = true;
  762. if (ucode_rate & RATE_MCS_BF_MSK)
  763. rate->bfer = true;
  764. rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
  765. if (ucode_rate & RATE_MCS_HT_MSK) {
  766. nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
  767. RATE_HT_MCS_NSS_POS) + 1;
  768. if (nss == 1) {
  769. rate->type = LQ_HT_SISO;
  770. WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
  771. "stbc %d bfer %d",
  772. rate->stbc, rate->bfer);
  773. } else if (nss == 2) {
  774. rate->type = LQ_HT_MIMO2;
  775. WARN_ON_ONCE(num_of_ant != 2);
  776. } else {
  777. WARN_ON_ONCE(1);
  778. }
  779. } else if (ucode_rate & RATE_MCS_VHT_MSK) {
  780. nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
  781. RATE_VHT_MCS_NSS_POS) + 1;
  782. if (nss == 1) {
  783. rate->type = LQ_VHT_SISO;
  784. WARN_ONCE(!rate->stbc && !rate->bfer && num_of_ant != 1,
  785. "stbc %d bfer %d",
  786. rate->stbc, rate->bfer);
  787. } else if (nss == 2) {
  788. rate->type = LQ_VHT_MIMO2;
  789. WARN_ON_ONCE(num_of_ant != 2);
  790. } else {
  791. WARN_ON_ONCE(1);
  792. }
  793. }
  794. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
  795. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
  796. !is_vht(rate));
  797. return 0;
  798. }
  799. /* switch to another antenna/antennas and return 1 */
  800. /* if no other valid antenna found, return 0 */
  801. static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
  802. {
  803. u8 new_ant_type;
  804. if (!rate->ant || rate->ant > ANT_ABC)
  805. return 0;
  806. if (!rs_is_valid_ant(valid_ant, rate->ant))
  807. return 0;
  808. new_ant_type = ant_toggle_lookup[rate->ant];
  809. while ((new_ant_type != rate->ant) &&
  810. !rs_is_valid_ant(valid_ant, new_ant_type))
  811. new_ant_type = ant_toggle_lookup[new_ant_type];
  812. if (new_ant_type == rate->ant)
  813. return 0;
  814. rate->ant = new_ant_type;
  815. return 1;
  816. }
  817. static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
  818. struct rs_rate *rate)
  819. {
  820. if (is_legacy(rate))
  821. return lq_sta->active_legacy_rate;
  822. else if (is_siso(rate))
  823. return lq_sta->active_siso_rate;
  824. else if (is_mimo2(rate))
  825. return lq_sta->active_mimo2_rate;
  826. WARN_ON_ONCE(1);
  827. return 0;
  828. }
  829. static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
  830. int rate_type)
  831. {
  832. u8 high = IWL_RATE_INVALID;
  833. u8 low = IWL_RATE_INVALID;
  834. /* 802.11A or ht walks to the next literal adjacent rate in
  835. * the rate table */
  836. if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
  837. int i;
  838. u32 mask;
  839. /* Find the previous rate that is in the rate mask */
  840. i = index - 1;
  841. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  842. if (rate_mask & mask) {
  843. low = i;
  844. break;
  845. }
  846. }
  847. /* Find the next rate that is in the rate mask */
  848. i = index + 1;
  849. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  850. if (rate_mask & mask) {
  851. high = i;
  852. break;
  853. }
  854. }
  855. return (high << 8) | low;
  856. }
  857. low = index;
  858. while (low != IWL_RATE_INVALID) {
  859. low = iwl_rates[low].prev_rs;
  860. if (low == IWL_RATE_INVALID)
  861. break;
  862. if (rate_mask & (1 << low))
  863. break;
  864. }
  865. high = index;
  866. while (high != IWL_RATE_INVALID) {
  867. high = iwl_rates[high].next_rs;
  868. if (high == IWL_RATE_INVALID)
  869. break;
  870. if (rate_mask & (1 << high))
  871. break;
  872. }
  873. return (high << 8) | low;
  874. }
  875. static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
  876. struct rs_rate *rate)
  877. {
  878. return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
  879. }
  880. /* Get the next supported lower rate in the current column.
  881. * Return true if bottom rate in the current column was reached
  882. */
  883. static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
  884. struct rs_rate *rate)
  885. {
  886. u8 low;
  887. u16 high_low;
  888. u16 rate_mask;
  889. struct iwl_mvm *mvm = lq_sta->pers.drv;
  890. rate_mask = rs_get_supported_rates(lq_sta, rate);
  891. high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
  892. rate->type);
  893. low = high_low & 0xff;
  894. /* Bottom rate of column reached */
  895. if (low == IWL_RATE_INVALID)
  896. return true;
  897. rate->index = low;
  898. return false;
  899. }
  900. /* Get the next rate to use following a column downgrade */
  901. static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
  902. struct rs_rate *rate)
  903. {
  904. struct iwl_mvm *mvm = lq_sta->pers.drv;
  905. if (is_legacy(rate)) {
  906. /* No column to downgrade from Legacy */
  907. return;
  908. } else if (is_siso(rate)) {
  909. /* Downgrade to Legacy if we were in SISO */
  910. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  911. rate->type = LQ_LEGACY_A;
  912. else
  913. rate->type = LQ_LEGACY_G;
  914. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  915. WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
  916. rate->index > IWL_RATE_MCS_9_INDEX);
  917. rate->index = rs_ht_to_legacy[rate->index];
  918. rate->ldpc = false;
  919. } else {
  920. /* Downgrade to SISO with same MCS if in MIMO */
  921. rate->type = is_vht_mimo2(rate) ?
  922. LQ_VHT_SISO : LQ_HT_SISO;
  923. }
  924. if (num_of_ant(rate->ant) > 1)
  925. rate->ant = first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
  926. /* Relevant in both switching to SISO or Legacy */
  927. rate->sgi = false;
  928. if (!rs_rate_supported(lq_sta, rate))
  929. rs_get_lower_rate_in_column(lq_sta, rate);
  930. }
  931. /* Check if both rates are identical
  932. * allow_ant_mismatch enables matching a SISO rate on ANT_A or ANT_B
  933. * with a rate indicating STBC/BFER and ANT_AB.
  934. */
  935. static inline bool rs_rate_equal(struct rs_rate *a,
  936. struct rs_rate *b,
  937. bool allow_ant_mismatch)
  938. {
  939. bool ant_match = (a->ant == b->ant) && (a->stbc == b->stbc) &&
  940. (a->bfer == b->bfer);
  941. if (allow_ant_mismatch) {
  942. if (a->stbc || a->bfer) {
  943. WARN_ONCE(a->ant != ANT_AB, "stbc %d bfer %d ant %d",
  944. a->stbc, a->bfer, a->ant);
  945. ant_match |= (b->ant == ANT_A || b->ant == ANT_B);
  946. } else if (b->stbc || b->bfer) {
  947. WARN_ONCE(b->ant != ANT_AB, "stbc %d bfer %d ant %d",
  948. b->stbc, b->bfer, b->ant);
  949. ant_match |= (a->ant == ANT_A || a->ant == ANT_B);
  950. }
  951. }
  952. return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi) &&
  953. (a->ldpc == b->ldpc) && (a->index == b->index) && ant_match;
  954. }
  955. /* Check if both rates share the same column */
  956. static inline bool rs_rate_column_match(struct rs_rate *a,
  957. struct rs_rate *b)
  958. {
  959. bool ant_match;
  960. if (a->stbc || a->bfer)
  961. ant_match = (b->ant == ANT_A || b->ant == ANT_B);
  962. else
  963. ant_match = (a->ant == b->ant);
  964. return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
  965. && ant_match;
  966. }
  967. static inline enum rs_column rs_get_column_from_rate(struct rs_rate *rate)
  968. {
  969. if (is_legacy(rate)) {
  970. if (rate->ant == ANT_A)
  971. return RS_COLUMN_LEGACY_ANT_A;
  972. if (rate->ant == ANT_B)
  973. return RS_COLUMN_LEGACY_ANT_B;
  974. goto err;
  975. }
  976. if (is_siso(rate)) {
  977. if (rate->ant == ANT_A || rate->stbc || rate->bfer)
  978. return rate->sgi ? RS_COLUMN_SISO_ANT_A_SGI :
  979. RS_COLUMN_SISO_ANT_A;
  980. if (rate->ant == ANT_B)
  981. return rate->sgi ? RS_COLUMN_SISO_ANT_B_SGI :
  982. RS_COLUMN_SISO_ANT_B;
  983. goto err;
  984. }
  985. if (is_mimo(rate))
  986. return rate->sgi ? RS_COLUMN_MIMO2_SGI : RS_COLUMN_MIMO2;
  987. err:
  988. return RS_COLUMN_INVALID;
  989. }
  990. static u8 rs_get_tid(struct ieee80211_hdr *hdr)
  991. {
  992. u8 tid = IWL_MAX_TID_COUNT;
  993. if (ieee80211_is_data_qos(hdr->frame_control)) {
  994. u8 *qc = ieee80211_get_qos_ctl(hdr);
  995. tid = qc[0] & 0xf;
  996. }
  997. if (unlikely(tid > IWL_MAX_TID_COUNT))
  998. tid = IWL_MAX_TID_COUNT;
  999. return tid;
  1000. }
  1001. void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1002. int tid, struct ieee80211_tx_info *info)
  1003. {
  1004. int legacy_success;
  1005. int retries;
  1006. int i;
  1007. struct iwl_lq_cmd *table;
  1008. u32 lq_hwrate;
  1009. struct rs_rate lq_rate, tx_resp_rate;
  1010. struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  1011. u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
  1012. u32 tx_resp_hwrate = (uintptr_t)info->status.status_driver_data[1];
  1013. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1014. struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
  1015. bool allow_ant_mismatch = fw_has_api(&mvm->fw->ucode_capa,
  1016. IWL_UCODE_TLV_API_LQ_SS_PARAMS);
  1017. /* Treat uninitialized rate scaling data same as non-existing. */
  1018. if (!lq_sta) {
  1019. IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
  1020. return;
  1021. } else if (!lq_sta->pers.drv) {
  1022. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  1023. return;
  1024. }
  1025. /* This packet was aggregated but doesn't carry status info */
  1026. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  1027. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  1028. return;
  1029. rs_rate_from_ucode_rate(tx_resp_hwrate, info->band, &tx_resp_rate);
  1030. #ifdef CONFIG_MAC80211_DEBUGFS
  1031. /* Disable last tx check if we are debugging with fixed rate but
  1032. * update tx stats */
  1033. if (lq_sta->pers.dbg_fixed_rate) {
  1034. int index = tx_resp_rate.index;
  1035. enum rs_column column;
  1036. int attempts, success;
  1037. column = rs_get_column_from_rate(&tx_resp_rate);
  1038. if (WARN_ONCE(column == RS_COLUMN_INVALID,
  1039. "Can't map rate 0x%x to column",
  1040. tx_resp_hwrate))
  1041. return;
  1042. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  1043. attempts = info->status.ampdu_len;
  1044. success = info->status.ampdu_ack_len;
  1045. } else {
  1046. attempts = info->status.rates[0].count;
  1047. success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  1048. }
  1049. lq_sta->pers.tx_stats[column][index].total += attempts;
  1050. lq_sta->pers.tx_stats[column][index].success += success;
  1051. IWL_DEBUG_RATE(mvm, "Fixed rate 0x%x success %d attempts %d\n",
  1052. tx_resp_hwrate, success, attempts);
  1053. return;
  1054. }
  1055. #endif
  1056. if (time_after(jiffies,
  1057. (unsigned long)(lq_sta->last_tx +
  1058. (IWL_MVM_RS_IDLE_TIMEOUT * HZ)))) {
  1059. int t;
  1060. IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
  1061. for (t = 0; t < IWL_MAX_TID_COUNT; t++)
  1062. ieee80211_stop_tx_ba_session(sta, t);
  1063. iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
  1064. return;
  1065. }
  1066. lq_sta->last_tx = jiffies;
  1067. /* Ignore this Tx frame response if its initial rate doesn't match
  1068. * that of latest Link Quality command. There may be stragglers
  1069. * from a previous Link Quality command, but we're no longer interested
  1070. * in those; they're either from the "active" mode while we're trying
  1071. * to check "search" mode, or a prior "search" mode after we've moved
  1072. * to a new "search" mode (which might become the new "active" mode).
  1073. */
  1074. table = &lq_sta->lq;
  1075. lq_hwrate = le32_to_cpu(table->rs_table[0]);
  1076. rs_rate_from_ucode_rate(lq_hwrate, info->band, &lq_rate);
  1077. /* Here we actually compare this rate to the latest LQ command */
  1078. if (!rs_rate_equal(&tx_resp_rate, &lq_rate, allow_ant_mismatch)) {
  1079. IWL_DEBUG_RATE(mvm,
  1080. "initial tx resp rate 0x%x does not match 0x%x\n",
  1081. tx_resp_hwrate, lq_hwrate);
  1082. /*
  1083. * Since rates mis-match, the last LQ command may have failed.
  1084. * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
  1085. * ... driver.
  1086. */
  1087. lq_sta->missed_rate_counter++;
  1088. if (lq_sta->missed_rate_counter > IWL_MVM_RS_MISSED_RATE_MAX) {
  1089. lq_sta->missed_rate_counter = 0;
  1090. IWL_DEBUG_RATE(mvm,
  1091. "Too many rates mismatch. Send sync LQ. rs_state %d\n",
  1092. lq_sta->rs_state);
  1093. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1094. }
  1095. /* Regardless, ignore this status info for outdated rate */
  1096. return;
  1097. } else
  1098. /* Rate did match, so reset the missed_rate_counter */
  1099. lq_sta->missed_rate_counter = 0;
  1100. if (!lq_sta->search_better_tbl) {
  1101. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1102. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1103. } else {
  1104. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1105. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1106. }
  1107. if (WARN_ON_ONCE(!rs_rate_column_match(&lq_rate, &curr_tbl->rate))) {
  1108. IWL_DEBUG_RATE(mvm,
  1109. "Neither active nor search matches tx rate\n");
  1110. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1111. rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
  1112. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1113. rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
  1114. rs_dump_rate(mvm, &lq_rate, "ACTUAL");
  1115. /*
  1116. * no matching table found, let's by-pass the data collection
  1117. * and continue to perform rate scale to find the rate table
  1118. */
  1119. rs_stay_in_table(lq_sta, true);
  1120. goto done;
  1121. }
  1122. /*
  1123. * Updating the frame history depends on whether packets were
  1124. * aggregated.
  1125. *
  1126. * For aggregation, all packets were transmitted at the same rate, the
  1127. * first index into rate scale table.
  1128. */
  1129. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  1130. rs_collect_tpc_data(mvm, lq_sta, curr_tbl, lq_rate.index,
  1131. info->status.ampdu_len,
  1132. info->status.ampdu_ack_len,
  1133. reduced_txp);
  1134. /* ampdu_ack_len = 0 marks no BA was received. For TLC, treat
  1135. * it as a single frame loss as we don't want the success ratio
  1136. * to dip too quickly because a BA wasn't received.
  1137. * For TPC, there's no need for this optimisation since we want
  1138. * to recover very quickly from a bad power reduction and,
  1139. * therefore we'd like the success ratio to get an immediate hit
  1140. * when failing to get a BA, so we'd switch back to a lower or
  1141. * zero power reduction. When FW transmits agg with a rate
  1142. * different from the initial rate, it will not use reduced txp
  1143. * and will send BA notification twice (one empty with reduced
  1144. * txp equal to the value from LQ and one with reduced txp 0).
  1145. * We need to update counters for each txp level accordingly.
  1146. */
  1147. if (info->status.ampdu_ack_len == 0)
  1148. info->status.ampdu_len = 1;
  1149. rs_collect_tlc_data(mvm, lq_sta, curr_tbl, lq_rate.index,
  1150. info->status.ampdu_len,
  1151. info->status.ampdu_ack_len);
  1152. /* Update success/fail counts if not searching for new mode */
  1153. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1154. lq_sta->total_success += info->status.ampdu_ack_len;
  1155. lq_sta->total_failed += (info->status.ampdu_len -
  1156. info->status.ampdu_ack_len);
  1157. }
  1158. } else {
  1159. /* For legacy, update frame history with for each Tx retry. */
  1160. retries = info->status.rates[0].count - 1;
  1161. /* HW doesn't send more than 15 retries */
  1162. retries = min(retries, 15);
  1163. /* The last transmission may have been successful */
  1164. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  1165. /* Collect data for each rate used during failed TX attempts */
  1166. for (i = 0; i <= retries; ++i) {
  1167. lq_hwrate = le32_to_cpu(table->rs_table[i]);
  1168. rs_rate_from_ucode_rate(lq_hwrate, info->band,
  1169. &lq_rate);
  1170. /*
  1171. * Only collect stats if retried rate is in the same RS
  1172. * table as active/search.
  1173. */
  1174. if (rs_rate_column_match(&lq_rate, &curr_tbl->rate))
  1175. tmp_tbl = curr_tbl;
  1176. else if (rs_rate_column_match(&lq_rate,
  1177. &other_tbl->rate))
  1178. tmp_tbl = other_tbl;
  1179. else
  1180. continue;
  1181. rs_collect_tpc_data(mvm, lq_sta, tmp_tbl,
  1182. lq_rate.index, 1,
  1183. i < retries ? 0 : legacy_success,
  1184. reduced_txp);
  1185. rs_collect_tlc_data(mvm, lq_sta, tmp_tbl,
  1186. lq_rate.index, 1,
  1187. i < retries ? 0 : legacy_success);
  1188. }
  1189. /* Update success/fail counts if not searching for new mode */
  1190. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1191. lq_sta->total_success += legacy_success;
  1192. lq_sta->total_failed += retries + (1 - legacy_success);
  1193. }
  1194. }
  1195. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  1196. lq_sta->last_rate_n_flags = lq_hwrate;
  1197. IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
  1198. done:
  1199. /* See if there's a better rate or modulation mode to try. */
  1200. if (sta->supp_rates[info->band])
  1201. rs_rate_scale_perform(mvm, sta, lq_sta, tid);
  1202. }
  1203. /*
  1204. * mac80211 sends us Tx status
  1205. */
  1206. static void rs_mac80211_tx_status(void *mvm_r,
  1207. struct ieee80211_supported_band *sband,
  1208. struct ieee80211_sta *sta, void *priv_sta,
  1209. struct sk_buff *skb)
  1210. {
  1211. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1212. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
  1213. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1214. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1215. if (!iwl_mvm_sta_from_mac80211(sta)->vif)
  1216. return;
  1217. if (!ieee80211_is_data(hdr->frame_control) ||
  1218. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1219. return;
  1220. iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info);
  1221. }
  1222. /*
  1223. * Begin a period of staying with a selected modulation mode.
  1224. * Set "stay_in_tbl" flag to prevent any mode switches.
  1225. * Set frame tx success limits according to legacy vs. high-throughput,
  1226. * and reset overall (spanning all rates) tx success history statistics.
  1227. * These control how long we stay using same modulation mode before
  1228. * searching for a new mode.
  1229. */
  1230. static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
  1231. struct iwl_lq_sta *lq_sta)
  1232. {
  1233. IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
  1234. lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
  1235. if (is_legacy) {
  1236. lq_sta->table_count_limit = IWL_MVM_RS_LEGACY_TABLE_COUNT;
  1237. lq_sta->max_failure_limit = IWL_MVM_RS_LEGACY_FAILURE_LIMIT;
  1238. lq_sta->max_success_limit = IWL_MVM_RS_LEGACY_SUCCESS_LIMIT;
  1239. } else {
  1240. lq_sta->table_count_limit = IWL_MVM_RS_NON_LEGACY_TABLE_COUNT;
  1241. lq_sta->max_failure_limit = IWL_MVM_RS_NON_LEGACY_FAILURE_LIMIT;
  1242. lq_sta->max_success_limit = IWL_MVM_RS_NON_LEGACY_SUCCESS_LIMIT;
  1243. }
  1244. lq_sta->table_count = 0;
  1245. lq_sta->total_failed = 0;
  1246. lq_sta->total_success = 0;
  1247. lq_sta->flush_timer = jiffies;
  1248. lq_sta->visited_columns = 0;
  1249. }
  1250. static inline int rs_get_max_rate_from_mask(unsigned long rate_mask)
  1251. {
  1252. if (rate_mask)
  1253. return find_last_bit(&rate_mask, BITS_PER_LONG);
  1254. return IWL_RATE_INVALID;
  1255. }
  1256. static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
  1257. const struct rs_tx_column *column)
  1258. {
  1259. switch (column->mode) {
  1260. case RS_LEGACY:
  1261. return lq_sta->max_legacy_rate_idx;
  1262. case RS_SISO:
  1263. return lq_sta->max_siso_rate_idx;
  1264. case RS_MIMO2:
  1265. return lq_sta->max_mimo2_rate_idx;
  1266. default:
  1267. WARN_ON_ONCE(1);
  1268. }
  1269. return lq_sta->max_legacy_rate_idx;
  1270. }
  1271. static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1272. const struct rs_tx_column *column,
  1273. u32 bw)
  1274. {
  1275. /* Used to choose among HT tables */
  1276. const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
  1277. if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
  1278. column->mode != RS_SISO &&
  1279. column->mode != RS_MIMO2))
  1280. return expected_tpt_legacy;
  1281. /* Legacy rates have only one table */
  1282. if (column->mode == RS_LEGACY)
  1283. return expected_tpt_legacy;
  1284. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1285. /* Choose among many HT tables depending on number of streams
  1286. * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
  1287. * status */
  1288. if (column->mode == RS_SISO) {
  1289. switch (bw) {
  1290. case RATE_MCS_CHAN_WIDTH_20:
  1291. ht_tbl_pointer = expected_tpt_siso_20MHz;
  1292. break;
  1293. case RATE_MCS_CHAN_WIDTH_40:
  1294. ht_tbl_pointer = expected_tpt_siso_40MHz;
  1295. break;
  1296. case RATE_MCS_CHAN_WIDTH_80:
  1297. ht_tbl_pointer = expected_tpt_siso_80MHz;
  1298. break;
  1299. default:
  1300. WARN_ON_ONCE(1);
  1301. }
  1302. } else if (column->mode == RS_MIMO2) {
  1303. switch (bw) {
  1304. case RATE_MCS_CHAN_WIDTH_20:
  1305. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1306. break;
  1307. case RATE_MCS_CHAN_WIDTH_40:
  1308. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  1309. break;
  1310. case RATE_MCS_CHAN_WIDTH_80:
  1311. ht_tbl_pointer = expected_tpt_mimo2_80MHz;
  1312. break;
  1313. default:
  1314. WARN_ON_ONCE(1);
  1315. }
  1316. } else {
  1317. WARN_ON_ONCE(1);
  1318. }
  1319. if (!column->sgi && !lq_sta->is_agg) /* Normal */
  1320. return ht_tbl_pointer[0];
  1321. else if (column->sgi && !lq_sta->is_agg) /* SGI */
  1322. return ht_tbl_pointer[1];
  1323. else if (!column->sgi && lq_sta->is_agg) /* AGG */
  1324. return ht_tbl_pointer[2];
  1325. else /* AGG+SGI */
  1326. return ht_tbl_pointer[3];
  1327. }
  1328. static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1329. struct iwl_scale_tbl_info *tbl)
  1330. {
  1331. struct rs_rate *rate = &tbl->rate;
  1332. const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
  1333. tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
  1334. }
  1335. static s32 rs_get_best_rate(struct iwl_mvm *mvm,
  1336. struct iwl_lq_sta *lq_sta,
  1337. struct iwl_scale_tbl_info *tbl, /* "search" */
  1338. unsigned long rate_mask, s8 index)
  1339. {
  1340. struct iwl_scale_tbl_info *active_tbl =
  1341. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1342. s32 success_ratio = active_tbl->win[index].success_ratio;
  1343. u16 expected_current_tpt = active_tbl->expected_tpt[index];
  1344. const u16 *tpt_tbl = tbl->expected_tpt;
  1345. u16 high_low;
  1346. u32 target_tpt;
  1347. int rate_idx;
  1348. if (success_ratio >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
  1349. target_tpt = 100 * expected_current_tpt;
  1350. IWL_DEBUG_RATE(mvm,
  1351. "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
  1352. success_ratio, target_tpt);
  1353. } else {
  1354. target_tpt = lq_sta->last_tpt;
  1355. IWL_DEBUG_RATE(mvm,
  1356. "SR %d not that good. Find rate exceeding ACTUAL_TPT %d\n",
  1357. success_ratio, target_tpt);
  1358. }
  1359. rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
  1360. while (rate_idx != IWL_RATE_INVALID) {
  1361. if (target_tpt < (100 * tpt_tbl[rate_idx]))
  1362. break;
  1363. high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
  1364. tbl->rate.type);
  1365. rate_idx = (high_low >> 8) & 0xff;
  1366. }
  1367. IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
  1368. rate_idx, target_tpt,
  1369. rate_idx != IWL_RATE_INVALID ?
  1370. 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
  1371. return rate_idx;
  1372. }
  1373. static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
  1374. {
  1375. if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
  1376. return RATE_MCS_CHAN_WIDTH_80;
  1377. else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
  1378. return RATE_MCS_CHAN_WIDTH_40;
  1379. return RATE_MCS_CHAN_WIDTH_20;
  1380. }
  1381. /*
  1382. * Check whether we should continue using same modulation mode, or
  1383. * begin search for a new mode, based on:
  1384. * 1) # tx successes or failures while using this mode
  1385. * 2) # times calling this function
  1386. * 3) elapsed time in this mode (not used, for now)
  1387. */
  1388. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
  1389. {
  1390. struct iwl_scale_tbl_info *tbl;
  1391. int active_tbl;
  1392. int flush_interval_passed = 0;
  1393. struct iwl_mvm *mvm;
  1394. mvm = lq_sta->pers.drv;
  1395. active_tbl = lq_sta->active_tbl;
  1396. tbl = &(lq_sta->lq_info[active_tbl]);
  1397. /* If we've been disallowing search, see if we should now allow it */
  1398. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1399. /* Elapsed time using current modulation mode */
  1400. if (lq_sta->flush_timer)
  1401. flush_interval_passed =
  1402. time_after(jiffies,
  1403. (unsigned long)(lq_sta->flush_timer +
  1404. (IWL_MVM_RS_STAY_IN_COLUMN_TIMEOUT * HZ)));
  1405. /*
  1406. * Check if we should allow search for new modulation mode.
  1407. * If many frames have failed or succeeded, or we've used
  1408. * this same modulation for a long time, allow search, and
  1409. * reset history stats that keep track of whether we should
  1410. * allow a new search. Also (below) reset all bitmaps and
  1411. * stats in active history.
  1412. */
  1413. if (force_search ||
  1414. (lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1415. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1416. ((!lq_sta->search_better_tbl) &&
  1417. (lq_sta->flush_timer) && (flush_interval_passed))) {
  1418. IWL_DEBUG_RATE(mvm,
  1419. "LQ: stay is expired %d %d %d\n",
  1420. lq_sta->total_failed,
  1421. lq_sta->total_success,
  1422. flush_interval_passed);
  1423. /* Allow search for new mode */
  1424. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
  1425. IWL_DEBUG_RATE(mvm,
  1426. "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
  1427. lq_sta->total_failed = 0;
  1428. lq_sta->total_success = 0;
  1429. lq_sta->flush_timer = 0;
  1430. /* mark the current column as visited */
  1431. lq_sta->visited_columns = BIT(tbl->column);
  1432. /*
  1433. * Else if we've used this modulation mode enough repetitions
  1434. * (regardless of elapsed time or success/failure), reset
  1435. * history bitmaps and rate-specific stats for all rates in
  1436. * active table.
  1437. */
  1438. } else {
  1439. lq_sta->table_count++;
  1440. if (lq_sta->table_count >=
  1441. lq_sta->table_count_limit) {
  1442. lq_sta->table_count = 0;
  1443. IWL_DEBUG_RATE(mvm,
  1444. "LQ: stay in table clear win\n");
  1445. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1446. }
  1447. }
  1448. /* If transitioning to allow "search", reset all history
  1449. * bitmaps and stats in active table (this will become the new
  1450. * "search" table). */
  1451. if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
  1452. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1453. }
  1454. }
  1455. }
  1456. static void rs_set_amsdu_len(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1457. struct iwl_scale_tbl_info *tbl,
  1458. enum rs_action scale_action)
  1459. {
  1460. struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
  1461. if ((!is_vht(&tbl->rate) && !is_ht(&tbl->rate)) ||
  1462. tbl->rate.index < IWL_RATE_MCS_5_INDEX ||
  1463. scale_action == RS_ACTION_DOWNSCALE)
  1464. sta_priv->tlc_amsdu = false;
  1465. else
  1466. sta_priv->tlc_amsdu = true;
  1467. }
  1468. /*
  1469. * setup rate table in uCode
  1470. */
  1471. static void rs_update_rate_tbl(struct iwl_mvm *mvm,
  1472. struct ieee80211_sta *sta,
  1473. struct iwl_lq_sta *lq_sta,
  1474. struct iwl_scale_tbl_info *tbl)
  1475. {
  1476. rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
  1477. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1478. }
  1479. static bool rs_tweak_rate_tbl(struct iwl_mvm *mvm,
  1480. struct ieee80211_sta *sta,
  1481. struct iwl_lq_sta *lq_sta,
  1482. struct iwl_scale_tbl_info *tbl,
  1483. enum rs_action scale_action)
  1484. {
  1485. if (sta->bandwidth != IEEE80211_STA_RX_BW_80)
  1486. return false;
  1487. if (!is_vht_siso(&tbl->rate))
  1488. return false;
  1489. if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_80) &&
  1490. (tbl->rate.index == IWL_RATE_MCS_0_INDEX) &&
  1491. (scale_action == RS_ACTION_DOWNSCALE)) {
  1492. tbl->rate.bw = RATE_MCS_CHAN_WIDTH_20;
  1493. tbl->rate.index = IWL_RATE_MCS_4_INDEX;
  1494. IWL_DEBUG_RATE(mvm, "Switch 80Mhz SISO MCS0 -> 20Mhz MCS4\n");
  1495. goto tweaked;
  1496. }
  1497. /* Go back to 80Mhz MCS1 only if we've established that 20Mhz MCS5 is
  1498. * sustainable, i.e. we're past the test window. We can't go back
  1499. * if MCS5 is just tested as this will happen always after switching
  1500. * to 20Mhz MCS4 because the rate stats are cleared.
  1501. */
  1502. if ((tbl->rate.bw == RATE_MCS_CHAN_WIDTH_20) &&
  1503. (((tbl->rate.index == IWL_RATE_MCS_5_INDEX) &&
  1504. (scale_action == RS_ACTION_STAY)) ||
  1505. ((tbl->rate.index > IWL_RATE_MCS_5_INDEX) &&
  1506. (scale_action == RS_ACTION_UPSCALE)))) {
  1507. tbl->rate.bw = RATE_MCS_CHAN_WIDTH_80;
  1508. tbl->rate.index = IWL_RATE_MCS_1_INDEX;
  1509. IWL_DEBUG_RATE(mvm, "Switch 20Mhz SISO MCS5 -> 80Mhz MCS1\n");
  1510. goto tweaked;
  1511. }
  1512. return false;
  1513. tweaked:
  1514. rs_set_expected_tpt_table(lq_sta, tbl);
  1515. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1516. return true;
  1517. }
  1518. static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
  1519. struct iwl_lq_sta *lq_sta,
  1520. struct ieee80211_sta *sta,
  1521. struct iwl_scale_tbl_info *tbl)
  1522. {
  1523. int i, j, max_rate;
  1524. enum rs_column next_col_id;
  1525. const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
  1526. const struct rs_tx_column *next_col;
  1527. allow_column_func_t allow_func;
  1528. u8 valid_ants = iwl_mvm_get_valid_tx_ant(mvm);
  1529. const u16 *expected_tpt_tbl;
  1530. u16 tpt, max_expected_tpt;
  1531. for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
  1532. next_col_id = curr_col->next_columns[i];
  1533. if (next_col_id == RS_COLUMN_INVALID)
  1534. continue;
  1535. if (lq_sta->visited_columns & BIT(next_col_id)) {
  1536. IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
  1537. next_col_id);
  1538. continue;
  1539. }
  1540. next_col = &rs_tx_columns[next_col_id];
  1541. if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
  1542. IWL_DEBUG_RATE(mvm,
  1543. "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
  1544. next_col_id, valid_ants, next_col->ant);
  1545. continue;
  1546. }
  1547. for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
  1548. allow_func = next_col->checks[j];
  1549. if (allow_func && !allow_func(mvm, sta, &tbl->rate,
  1550. next_col))
  1551. break;
  1552. }
  1553. if (j != MAX_COLUMN_CHECKS) {
  1554. IWL_DEBUG_RATE(mvm,
  1555. "Skip column %d: not allowed (check %d failed)\n",
  1556. next_col_id, j);
  1557. continue;
  1558. }
  1559. tpt = lq_sta->last_tpt / 100;
  1560. expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
  1561. rs_bw_from_sta_bw(sta));
  1562. if (WARN_ON_ONCE(!expected_tpt_tbl))
  1563. continue;
  1564. max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
  1565. if (max_rate == IWL_RATE_INVALID) {
  1566. IWL_DEBUG_RATE(mvm,
  1567. "Skip column %d: no rate is allowed in this column\n",
  1568. next_col_id);
  1569. continue;
  1570. }
  1571. max_expected_tpt = expected_tpt_tbl[max_rate];
  1572. if (tpt >= max_expected_tpt) {
  1573. IWL_DEBUG_RATE(mvm,
  1574. "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
  1575. next_col_id, max_expected_tpt, tpt);
  1576. continue;
  1577. }
  1578. IWL_DEBUG_RATE(mvm,
  1579. "Found potential column %d. Max expected %d current %d\n",
  1580. next_col_id, max_expected_tpt, tpt);
  1581. break;
  1582. }
  1583. if (i == MAX_NEXT_COLUMNS)
  1584. return RS_COLUMN_INVALID;
  1585. return next_col_id;
  1586. }
  1587. static int rs_switch_to_column(struct iwl_mvm *mvm,
  1588. struct iwl_lq_sta *lq_sta,
  1589. struct ieee80211_sta *sta,
  1590. enum rs_column col_id)
  1591. {
  1592. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1593. struct iwl_scale_tbl_info *search_tbl =
  1594. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1595. struct rs_rate *rate = &search_tbl->rate;
  1596. const struct rs_tx_column *column = &rs_tx_columns[col_id];
  1597. const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
  1598. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1599. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1600. unsigned long rate_mask = 0;
  1601. u32 rate_idx = 0;
  1602. memcpy(search_tbl, tbl, sz);
  1603. rate->sgi = column->sgi;
  1604. rate->ant = column->ant;
  1605. if (column->mode == RS_LEGACY) {
  1606. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1607. rate->type = LQ_LEGACY_A;
  1608. else
  1609. rate->type = LQ_LEGACY_G;
  1610. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  1611. rate->ldpc = false;
  1612. rate_mask = lq_sta->active_legacy_rate;
  1613. } else if (column->mode == RS_SISO) {
  1614. rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
  1615. rate_mask = lq_sta->active_siso_rate;
  1616. } else if (column->mode == RS_MIMO2) {
  1617. rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
  1618. rate_mask = lq_sta->active_mimo2_rate;
  1619. } else {
  1620. WARN_ONCE(1, "Bad column mode");
  1621. }
  1622. if (column->mode != RS_LEGACY) {
  1623. rate->bw = rs_bw_from_sta_bw(sta);
  1624. rate->ldpc = lq_sta->ldpc;
  1625. }
  1626. search_tbl->column = col_id;
  1627. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1628. lq_sta->visited_columns |= BIT(col_id);
  1629. /* Get the best matching rate if we're changing modes. e.g.
  1630. * SISO->MIMO, LEGACY->SISO, MIMO->SISO
  1631. */
  1632. if (curr_column->mode != column->mode) {
  1633. rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
  1634. rate_mask, rate->index);
  1635. if ((rate_idx == IWL_RATE_INVALID) ||
  1636. !(BIT(rate_idx) & rate_mask)) {
  1637. IWL_DEBUG_RATE(mvm,
  1638. "can not switch with index %d"
  1639. " rate mask %lx\n",
  1640. rate_idx, rate_mask);
  1641. goto err;
  1642. }
  1643. rate->index = rate_idx;
  1644. }
  1645. IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
  1646. col_id, rate->index);
  1647. return 0;
  1648. err:
  1649. rate->type = LQ_NONE;
  1650. return -1;
  1651. }
  1652. static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
  1653. struct iwl_scale_tbl_info *tbl,
  1654. s32 sr, int low, int high,
  1655. int current_tpt,
  1656. int low_tpt, int high_tpt)
  1657. {
  1658. enum rs_action action = RS_ACTION_STAY;
  1659. if ((sr <= RS_PERCENT(IWL_MVM_RS_SR_FORCE_DECREASE)) ||
  1660. (current_tpt == 0)) {
  1661. IWL_DEBUG_RATE(mvm,
  1662. "Decrease rate because of low SR\n");
  1663. return RS_ACTION_DOWNSCALE;
  1664. }
  1665. if ((low_tpt == IWL_INVALID_VALUE) &&
  1666. (high_tpt == IWL_INVALID_VALUE) &&
  1667. (high != IWL_RATE_INVALID)) {
  1668. IWL_DEBUG_RATE(mvm,
  1669. "No data about high/low rates. Increase rate\n");
  1670. return RS_ACTION_UPSCALE;
  1671. }
  1672. if ((high_tpt == IWL_INVALID_VALUE) &&
  1673. (high != IWL_RATE_INVALID) &&
  1674. (low_tpt != IWL_INVALID_VALUE) &&
  1675. (low_tpt < current_tpt)) {
  1676. IWL_DEBUG_RATE(mvm,
  1677. "No data about high rate and low rate is worse. Increase rate\n");
  1678. return RS_ACTION_UPSCALE;
  1679. }
  1680. if ((high_tpt != IWL_INVALID_VALUE) &&
  1681. (high_tpt > current_tpt)) {
  1682. IWL_DEBUG_RATE(mvm,
  1683. "Higher rate is better. Increate rate\n");
  1684. return RS_ACTION_UPSCALE;
  1685. }
  1686. if ((low_tpt != IWL_INVALID_VALUE) &&
  1687. (high_tpt != IWL_INVALID_VALUE) &&
  1688. (low_tpt < current_tpt) &&
  1689. (high_tpt < current_tpt)) {
  1690. IWL_DEBUG_RATE(mvm,
  1691. "Both high and low are worse. Maintain rate\n");
  1692. return RS_ACTION_STAY;
  1693. }
  1694. if ((low_tpt != IWL_INVALID_VALUE) &&
  1695. (low_tpt > current_tpt)) {
  1696. IWL_DEBUG_RATE(mvm,
  1697. "Lower rate is better\n");
  1698. action = RS_ACTION_DOWNSCALE;
  1699. goto out;
  1700. }
  1701. if ((low_tpt == IWL_INVALID_VALUE) &&
  1702. (low != IWL_RATE_INVALID)) {
  1703. IWL_DEBUG_RATE(mvm,
  1704. "No data about lower rate\n");
  1705. action = RS_ACTION_DOWNSCALE;
  1706. goto out;
  1707. }
  1708. IWL_DEBUG_RATE(mvm, "Maintain rate\n");
  1709. out:
  1710. if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
  1711. if (sr >= RS_PERCENT(IWL_MVM_RS_SR_NO_DECREASE)) {
  1712. IWL_DEBUG_RATE(mvm,
  1713. "SR is above NO DECREASE. Avoid downscale\n");
  1714. action = RS_ACTION_STAY;
  1715. } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
  1716. IWL_DEBUG_RATE(mvm,
  1717. "Current TPT is higher than max expected in low rate. Avoid downscale\n");
  1718. action = RS_ACTION_STAY;
  1719. } else {
  1720. IWL_DEBUG_RATE(mvm, "Decrease rate\n");
  1721. }
  1722. }
  1723. return action;
  1724. }
  1725. static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1726. struct iwl_lq_sta *lq_sta)
  1727. {
  1728. /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
  1729. * supports STBC of at least 1*SS
  1730. */
  1731. if (!lq_sta->stbc_capable)
  1732. return false;
  1733. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  1734. return false;
  1735. return true;
  1736. }
  1737. static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
  1738. int *weaker, int *stronger)
  1739. {
  1740. *weaker = index + IWL_MVM_RS_TPC_TX_POWER_STEP;
  1741. if (*weaker > TPC_MAX_REDUCTION)
  1742. *weaker = TPC_INVALID;
  1743. *stronger = index - IWL_MVM_RS_TPC_TX_POWER_STEP;
  1744. if (*stronger < 0)
  1745. *stronger = TPC_INVALID;
  1746. }
  1747. static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1748. struct rs_rate *rate, enum ieee80211_band band)
  1749. {
  1750. int index = rate->index;
  1751. bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
  1752. bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
  1753. !vif->bss_conf.ps);
  1754. IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
  1755. cam, sta_ps_disabled);
  1756. /*
  1757. * allow tpc only if power management is enabled, or bt coex
  1758. * activity grade allows it and we are on 2.4Ghz.
  1759. */
  1760. if ((cam || sta_ps_disabled) &&
  1761. !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
  1762. return false;
  1763. IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
  1764. if (is_legacy(rate))
  1765. return index == IWL_RATE_54M_INDEX;
  1766. if (is_ht(rate))
  1767. return index == IWL_RATE_MCS_7_INDEX;
  1768. if (is_vht(rate))
  1769. return index == IWL_RATE_MCS_7_INDEX ||
  1770. index == IWL_RATE_MCS_8_INDEX ||
  1771. index == IWL_RATE_MCS_9_INDEX;
  1772. WARN_ON_ONCE(1);
  1773. return false;
  1774. }
  1775. enum tpc_action {
  1776. TPC_ACTION_STAY,
  1777. TPC_ACTION_DECREASE,
  1778. TPC_ACTION_INCREASE,
  1779. TPC_ACTION_NO_RESTIRCTION,
  1780. };
  1781. static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
  1782. s32 sr, int weak, int strong,
  1783. int current_tpt,
  1784. int weak_tpt, int strong_tpt)
  1785. {
  1786. /* stay until we have valid tpt */
  1787. if (current_tpt == IWL_INVALID_VALUE) {
  1788. IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
  1789. return TPC_ACTION_STAY;
  1790. }
  1791. /* Too many failures, increase txp */
  1792. if (sr <= RS_PERCENT(IWL_MVM_RS_TPC_SR_FORCE_INCREASE) ||
  1793. current_tpt == 0) {
  1794. IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
  1795. return TPC_ACTION_NO_RESTIRCTION;
  1796. }
  1797. /* try decreasing first if applicable */
  1798. if (sr >= RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
  1799. weak != TPC_INVALID) {
  1800. if (weak_tpt == IWL_INVALID_VALUE &&
  1801. (strong_tpt == IWL_INVALID_VALUE ||
  1802. current_tpt >= strong_tpt)) {
  1803. IWL_DEBUG_RATE(mvm,
  1804. "no weak txp measurement. decrease txp\n");
  1805. return TPC_ACTION_DECREASE;
  1806. }
  1807. if (weak_tpt > current_tpt) {
  1808. IWL_DEBUG_RATE(mvm,
  1809. "lower txp has better tpt. decrease txp\n");
  1810. return TPC_ACTION_DECREASE;
  1811. }
  1812. }
  1813. /* next, increase if needed */
  1814. if (sr < RS_PERCENT(IWL_MVM_RS_TPC_SR_NO_INCREASE) &&
  1815. strong != TPC_INVALID) {
  1816. if (weak_tpt == IWL_INVALID_VALUE &&
  1817. strong_tpt != IWL_INVALID_VALUE &&
  1818. current_tpt < strong_tpt) {
  1819. IWL_DEBUG_RATE(mvm,
  1820. "higher txp has better tpt. increase txp\n");
  1821. return TPC_ACTION_INCREASE;
  1822. }
  1823. if (weak_tpt < current_tpt &&
  1824. (strong_tpt == IWL_INVALID_VALUE ||
  1825. strong_tpt > current_tpt)) {
  1826. IWL_DEBUG_RATE(mvm,
  1827. "lower txp has worse tpt. increase txp\n");
  1828. return TPC_ACTION_INCREASE;
  1829. }
  1830. }
  1831. IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
  1832. return TPC_ACTION_STAY;
  1833. }
  1834. static bool rs_tpc_perform(struct iwl_mvm *mvm,
  1835. struct ieee80211_sta *sta,
  1836. struct iwl_lq_sta *lq_sta,
  1837. struct iwl_scale_tbl_info *tbl)
  1838. {
  1839. struct iwl_mvm_sta *mvm_sta = iwl_mvm_sta_from_mac80211(sta);
  1840. struct ieee80211_vif *vif = mvm_sta->vif;
  1841. struct ieee80211_chanctx_conf *chanctx_conf;
  1842. enum ieee80211_band band;
  1843. struct iwl_rate_scale_data *window;
  1844. struct rs_rate *rate = &tbl->rate;
  1845. enum tpc_action action;
  1846. s32 sr;
  1847. u8 cur = lq_sta->lq.reduced_tpc;
  1848. int current_tpt;
  1849. int weak, strong;
  1850. int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
  1851. #ifdef CONFIG_MAC80211_DEBUGFS
  1852. if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
  1853. IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
  1854. lq_sta->pers.dbg_fixed_txp_reduction);
  1855. lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
  1856. return cur != lq_sta->pers.dbg_fixed_txp_reduction;
  1857. }
  1858. #endif
  1859. rcu_read_lock();
  1860. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  1861. if (WARN_ON(!chanctx_conf))
  1862. band = IEEE80211_NUM_BANDS;
  1863. else
  1864. band = chanctx_conf->def.chan->band;
  1865. rcu_read_unlock();
  1866. if (!rs_tpc_allowed(mvm, vif, rate, band)) {
  1867. IWL_DEBUG_RATE(mvm,
  1868. "tpc is not allowed. remove txp restrictions\n");
  1869. lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
  1870. return cur != TPC_NO_REDUCTION;
  1871. }
  1872. rs_get_adjacent_txp(mvm, cur, &weak, &strong);
  1873. /* Collect measured throughputs for current and adjacent rates */
  1874. window = tbl->tpc_win;
  1875. sr = window[cur].success_ratio;
  1876. current_tpt = window[cur].average_tpt;
  1877. if (weak != TPC_INVALID)
  1878. weak_tpt = window[weak].average_tpt;
  1879. if (strong != TPC_INVALID)
  1880. strong_tpt = window[strong].average_tpt;
  1881. IWL_DEBUG_RATE(mvm,
  1882. "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
  1883. cur, current_tpt, sr, weak, strong,
  1884. weak_tpt, strong_tpt);
  1885. action = rs_get_tpc_action(mvm, sr, weak, strong,
  1886. current_tpt, weak_tpt, strong_tpt);
  1887. /* override actions if we are on the edge */
  1888. if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
  1889. IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
  1890. action = TPC_ACTION_STAY;
  1891. } else if (strong == TPC_INVALID &&
  1892. (action == TPC_ACTION_INCREASE ||
  1893. action == TPC_ACTION_NO_RESTIRCTION)) {
  1894. IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
  1895. action = TPC_ACTION_STAY;
  1896. }
  1897. switch (action) {
  1898. case TPC_ACTION_DECREASE:
  1899. lq_sta->lq.reduced_tpc = weak;
  1900. return true;
  1901. case TPC_ACTION_INCREASE:
  1902. lq_sta->lq.reduced_tpc = strong;
  1903. return true;
  1904. case TPC_ACTION_NO_RESTIRCTION:
  1905. lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
  1906. return true;
  1907. case TPC_ACTION_STAY:
  1908. /* do nothing */
  1909. break;
  1910. }
  1911. return false;
  1912. }
  1913. /*
  1914. * Do rate scaling and search for new modulation mode.
  1915. */
  1916. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  1917. struct ieee80211_sta *sta,
  1918. struct iwl_lq_sta *lq_sta,
  1919. int tid)
  1920. {
  1921. int low = IWL_RATE_INVALID;
  1922. int high = IWL_RATE_INVALID;
  1923. int index;
  1924. struct iwl_rate_scale_data *window = NULL;
  1925. int current_tpt = IWL_INVALID_VALUE;
  1926. int low_tpt = IWL_INVALID_VALUE;
  1927. int high_tpt = IWL_INVALID_VALUE;
  1928. u32 fail_count;
  1929. enum rs_action scale_action = RS_ACTION_STAY;
  1930. u16 rate_mask;
  1931. u8 update_lq = 0;
  1932. struct iwl_scale_tbl_info *tbl, *tbl1;
  1933. u8 active_tbl = 0;
  1934. u8 done_search = 0;
  1935. u16 high_low;
  1936. s32 sr;
  1937. u8 prev_agg = lq_sta->is_agg;
  1938. struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
  1939. struct iwl_mvm_tid_data *tid_data;
  1940. struct rs_rate *rate;
  1941. lq_sta->is_agg = !!sta_priv->agg_tids;
  1942. /*
  1943. * Select rate-scale / modulation-mode table to work with in
  1944. * the rest of this function: "search" if searching for better
  1945. * modulation mode, or "active" if doing rate scaling within a mode.
  1946. */
  1947. if (!lq_sta->search_better_tbl)
  1948. active_tbl = lq_sta->active_tbl;
  1949. else
  1950. active_tbl = 1 - lq_sta->active_tbl;
  1951. tbl = &(lq_sta->lq_info[active_tbl]);
  1952. rate = &tbl->rate;
  1953. if (prev_agg != lq_sta->is_agg) {
  1954. IWL_DEBUG_RATE(mvm,
  1955. "Aggregation changed: prev %d current %d. Update expected TPT table\n",
  1956. prev_agg, lq_sta->is_agg);
  1957. rs_set_expected_tpt_table(lq_sta, tbl);
  1958. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1959. }
  1960. /* current tx rate */
  1961. index = rate->index;
  1962. /* rates available for this association, and for modulation mode */
  1963. rate_mask = rs_get_supported_rates(lq_sta, rate);
  1964. if (!(BIT(index) & rate_mask)) {
  1965. IWL_ERR(mvm, "Current Rate is not valid\n");
  1966. if (lq_sta->search_better_tbl) {
  1967. /* revert to active table if search table is not valid*/
  1968. rate->type = LQ_NONE;
  1969. lq_sta->search_better_tbl = 0;
  1970. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1971. rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
  1972. }
  1973. return;
  1974. }
  1975. /* Get expected throughput table and history window for current rate */
  1976. if (!tbl->expected_tpt) {
  1977. IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
  1978. return;
  1979. }
  1980. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  1981. window = &(tbl->win[index]);
  1982. /*
  1983. * If there is not enough history to calculate actual average
  1984. * throughput, keep analyzing results of more tx frames, without
  1985. * changing rate or mode (bypass most of the rest of this function).
  1986. * Set up new rate table in uCode only if old rate is not supported
  1987. * in current association (use new rate found above).
  1988. */
  1989. fail_count = window->counter - window->success_counter;
  1990. if ((fail_count < IWL_MVM_RS_RATE_MIN_FAILURE_TH) &&
  1991. (window->success_counter < IWL_MVM_RS_RATE_MIN_SUCCESS_TH)) {
  1992. IWL_DEBUG_RATE(mvm,
  1993. "%s: Test Window: succ %d total %d\n",
  1994. rs_pretty_rate(rate),
  1995. window->success_counter, window->counter);
  1996. /* Can't calculate this yet; not enough history */
  1997. window->average_tpt = IWL_INVALID_VALUE;
  1998. /* Should we stay with this modulation mode,
  1999. * or search for a new one? */
  2000. rs_stay_in_table(lq_sta, false);
  2001. return;
  2002. }
  2003. /* If we are searching for better modulation mode, check success. */
  2004. if (lq_sta->search_better_tbl) {
  2005. /* If good success, continue using the "search" mode;
  2006. * no need to send new link quality command, since we're
  2007. * continuing to use the setup that we've been trying. */
  2008. if (window->average_tpt > lq_sta->last_tpt) {
  2009. IWL_DEBUG_RATE(mvm,
  2010. "SWITCHING TO NEW TABLE SR: %d "
  2011. "cur-tpt %d old-tpt %d\n",
  2012. window->success_ratio,
  2013. window->average_tpt,
  2014. lq_sta->last_tpt);
  2015. /* Swap tables; "search" becomes "active" */
  2016. lq_sta->active_tbl = active_tbl;
  2017. current_tpt = window->average_tpt;
  2018. /* Else poor success; go back to mode in "active" table */
  2019. } else {
  2020. IWL_DEBUG_RATE(mvm,
  2021. "GOING BACK TO THE OLD TABLE: SR %d "
  2022. "cur-tpt %d old-tpt %d\n",
  2023. window->success_ratio,
  2024. window->average_tpt,
  2025. lq_sta->last_tpt);
  2026. /* Nullify "search" table */
  2027. rate->type = LQ_NONE;
  2028. /* Revert to "active" table */
  2029. active_tbl = lq_sta->active_tbl;
  2030. tbl = &(lq_sta->lq_info[active_tbl]);
  2031. /* Revert to "active" rate and throughput info */
  2032. index = tbl->rate.index;
  2033. current_tpt = lq_sta->last_tpt;
  2034. /* Need to set up a new rate table in uCode */
  2035. update_lq = 1;
  2036. }
  2037. /* Either way, we've made a decision; modulation mode
  2038. * search is done, allow rate adjustment next time. */
  2039. lq_sta->search_better_tbl = 0;
  2040. done_search = 1; /* Don't switch modes below! */
  2041. goto lq_update;
  2042. }
  2043. /* (Else) not in search of better modulation mode, try for better
  2044. * starting rate, while staying in this mode. */
  2045. high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
  2046. low = high_low & 0xff;
  2047. high = (high_low >> 8) & 0xff;
  2048. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  2049. sr = window->success_ratio;
  2050. /* Collect measured throughputs for current and adjacent rates */
  2051. current_tpt = window->average_tpt;
  2052. if (low != IWL_RATE_INVALID)
  2053. low_tpt = tbl->win[low].average_tpt;
  2054. if (high != IWL_RATE_INVALID)
  2055. high_tpt = tbl->win[high].average_tpt;
  2056. IWL_DEBUG_RATE(mvm,
  2057. "%s: cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
  2058. rs_pretty_rate(rate), current_tpt, sr,
  2059. low, high, low_tpt, high_tpt);
  2060. scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
  2061. current_tpt, low_tpt, high_tpt);
  2062. /* Force a search in case BT doesn't like us being in MIMO */
  2063. if (is_mimo(rate) &&
  2064. !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
  2065. IWL_DEBUG_RATE(mvm,
  2066. "BT Coex forbids MIMO. Search for new config\n");
  2067. rs_stay_in_table(lq_sta, true);
  2068. goto lq_update;
  2069. }
  2070. switch (scale_action) {
  2071. case RS_ACTION_DOWNSCALE:
  2072. /* Decrease starting rate, update uCode's rate table */
  2073. if (low != IWL_RATE_INVALID) {
  2074. update_lq = 1;
  2075. index = low;
  2076. } else {
  2077. IWL_DEBUG_RATE(mvm,
  2078. "At the bottom rate. Can't decrease\n");
  2079. }
  2080. break;
  2081. case RS_ACTION_UPSCALE:
  2082. /* Increase starting rate, update uCode's rate table */
  2083. if (high != IWL_RATE_INVALID) {
  2084. update_lq = 1;
  2085. index = high;
  2086. } else {
  2087. IWL_DEBUG_RATE(mvm,
  2088. "At the top rate. Can't increase\n");
  2089. }
  2090. break;
  2091. case RS_ACTION_STAY:
  2092. /* No change */
  2093. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
  2094. update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
  2095. break;
  2096. default:
  2097. break;
  2098. }
  2099. lq_update:
  2100. /* Replace uCode's rate table for the destination station. */
  2101. if (update_lq) {
  2102. tbl->rate.index = index;
  2103. if (IWL_MVM_RS_80_20_FAR_RANGE_TWEAK)
  2104. rs_tweak_rate_tbl(mvm, sta, lq_sta, tbl, scale_action);
  2105. rs_set_amsdu_len(mvm, sta, tbl, scale_action);
  2106. rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
  2107. }
  2108. rs_stay_in_table(lq_sta, false);
  2109. /*
  2110. * Search for new modulation mode if we're:
  2111. * 1) Not changing rates right now
  2112. * 2) Not just finishing up a search
  2113. * 3) Allowing a new search
  2114. */
  2115. if (!update_lq && !done_search &&
  2116. lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
  2117. && window->counter) {
  2118. enum rs_column next_column;
  2119. /* Save current throughput to compare with "search" throughput*/
  2120. lq_sta->last_tpt = current_tpt;
  2121. IWL_DEBUG_RATE(mvm,
  2122. "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
  2123. update_lq, done_search, lq_sta->rs_state,
  2124. window->counter);
  2125. next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
  2126. if (next_column != RS_COLUMN_INVALID) {
  2127. int ret = rs_switch_to_column(mvm, lq_sta, sta,
  2128. next_column);
  2129. if (!ret)
  2130. lq_sta->search_better_tbl = 1;
  2131. } else {
  2132. IWL_DEBUG_RATE(mvm,
  2133. "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
  2134. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
  2135. }
  2136. /* If new "search" mode was selected, set up in uCode table */
  2137. if (lq_sta->search_better_tbl) {
  2138. /* Access the "search" table, clear its history. */
  2139. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  2140. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  2141. /* Use new "search" start rate */
  2142. index = tbl->rate.index;
  2143. rs_dump_rate(mvm, &tbl->rate,
  2144. "Switch to SEARCH TABLE:");
  2145. rs_update_rate_tbl(mvm, sta, lq_sta, tbl);
  2146. } else {
  2147. done_search = 1;
  2148. }
  2149. }
  2150. if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
  2151. /* If the "active" (non-search) mode was legacy,
  2152. * and we've tried switching antennas,
  2153. * but we haven't been able to try HT modes (not available),
  2154. * stay with best antenna legacy modulation for a while
  2155. * before next round of mode comparisons. */
  2156. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2157. if (is_legacy(&tbl1->rate)) {
  2158. IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
  2159. if (tid != IWL_MAX_TID_COUNT) {
  2160. tid_data = &sta_priv->tid_data[tid];
  2161. if (tid_data->state != IWL_AGG_OFF) {
  2162. IWL_DEBUG_RATE(mvm,
  2163. "Stop aggregation on tid %d\n",
  2164. tid);
  2165. ieee80211_stop_tx_ba_session(sta, tid);
  2166. }
  2167. }
  2168. rs_set_stay_in_table(mvm, 1, lq_sta);
  2169. } else {
  2170. /* If we're in an HT mode, and all 3 mode switch actions
  2171. * have been tried and compared, stay in this best modulation
  2172. * mode for a while before next round of mode comparisons. */
  2173. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  2174. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  2175. (tid != IWL_MAX_TID_COUNT)) {
  2176. tid_data = &sta_priv->tid_data[tid];
  2177. if (tid_data->state == IWL_AGG_OFF) {
  2178. IWL_DEBUG_RATE(mvm,
  2179. "try to aggregate tid %d\n",
  2180. tid);
  2181. rs_tl_turn_on_agg(mvm, tid,
  2182. lq_sta, sta);
  2183. }
  2184. }
  2185. rs_set_stay_in_table(mvm, 0, lq_sta);
  2186. }
  2187. }
  2188. }
  2189. struct rs_init_rate_info {
  2190. s8 rssi;
  2191. u8 rate_idx;
  2192. };
  2193. static const struct rs_init_rate_info rs_optimal_rates_24ghz_legacy[] = {
  2194. { -60, IWL_RATE_54M_INDEX },
  2195. { -64, IWL_RATE_48M_INDEX },
  2196. { -68, IWL_RATE_36M_INDEX },
  2197. { -80, IWL_RATE_24M_INDEX },
  2198. { -84, IWL_RATE_18M_INDEX },
  2199. { -85, IWL_RATE_12M_INDEX },
  2200. { -86, IWL_RATE_11M_INDEX },
  2201. { -88, IWL_RATE_5M_INDEX },
  2202. { -90, IWL_RATE_2M_INDEX },
  2203. { S8_MIN, IWL_RATE_1M_INDEX },
  2204. };
  2205. static const struct rs_init_rate_info rs_optimal_rates_5ghz_legacy[] = {
  2206. { -60, IWL_RATE_54M_INDEX },
  2207. { -64, IWL_RATE_48M_INDEX },
  2208. { -72, IWL_RATE_36M_INDEX },
  2209. { -80, IWL_RATE_24M_INDEX },
  2210. { -84, IWL_RATE_18M_INDEX },
  2211. { -85, IWL_RATE_12M_INDEX },
  2212. { -87, IWL_RATE_9M_INDEX },
  2213. { S8_MIN, IWL_RATE_6M_INDEX },
  2214. };
  2215. static const struct rs_init_rate_info rs_optimal_rates_ht[] = {
  2216. { -60, IWL_RATE_MCS_7_INDEX },
  2217. { -64, IWL_RATE_MCS_6_INDEX },
  2218. { -68, IWL_RATE_MCS_5_INDEX },
  2219. { -72, IWL_RATE_MCS_4_INDEX },
  2220. { -80, IWL_RATE_MCS_3_INDEX },
  2221. { -84, IWL_RATE_MCS_2_INDEX },
  2222. { -85, IWL_RATE_MCS_1_INDEX },
  2223. { S8_MIN, IWL_RATE_MCS_0_INDEX},
  2224. };
  2225. static const struct rs_init_rate_info rs_optimal_rates_vht_20mhz[] = {
  2226. { -60, IWL_RATE_MCS_8_INDEX },
  2227. { -64, IWL_RATE_MCS_7_INDEX },
  2228. { -68, IWL_RATE_MCS_6_INDEX },
  2229. { -72, IWL_RATE_MCS_5_INDEX },
  2230. { -80, IWL_RATE_MCS_4_INDEX },
  2231. { -84, IWL_RATE_MCS_3_INDEX },
  2232. { -85, IWL_RATE_MCS_2_INDEX },
  2233. { -87, IWL_RATE_MCS_1_INDEX },
  2234. { S8_MIN, IWL_RATE_MCS_0_INDEX},
  2235. };
  2236. static const struct rs_init_rate_info rs_optimal_rates_vht_40_80mhz[] = {
  2237. { -60, IWL_RATE_MCS_9_INDEX },
  2238. { -64, IWL_RATE_MCS_8_INDEX },
  2239. { -68, IWL_RATE_MCS_7_INDEX },
  2240. { -72, IWL_RATE_MCS_6_INDEX },
  2241. { -80, IWL_RATE_MCS_5_INDEX },
  2242. { -84, IWL_RATE_MCS_4_INDEX },
  2243. { -85, IWL_RATE_MCS_3_INDEX },
  2244. { -87, IWL_RATE_MCS_2_INDEX },
  2245. { -88, IWL_RATE_MCS_1_INDEX },
  2246. { S8_MIN, IWL_RATE_MCS_0_INDEX },
  2247. };
  2248. #define IWL_RS_LOW_RSSI_THRESHOLD (-76) /* dBm */
  2249. /* Init the optimal rate based on STA caps
  2250. * This combined with rssi is used to report the last tx rate
  2251. * to userspace when we haven't transmitted enough frames.
  2252. */
  2253. static void rs_init_optimal_rate(struct iwl_mvm *mvm,
  2254. struct ieee80211_sta *sta,
  2255. struct iwl_lq_sta *lq_sta)
  2256. {
  2257. struct rs_rate *rate = &lq_sta->optimal_rate;
  2258. if (lq_sta->max_mimo2_rate_idx != IWL_RATE_INVALID)
  2259. rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
  2260. else if (lq_sta->max_siso_rate_idx != IWL_RATE_INVALID)
  2261. rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
  2262. else if (lq_sta->band == IEEE80211_BAND_5GHZ)
  2263. rate->type = LQ_LEGACY_A;
  2264. else
  2265. rate->type = LQ_LEGACY_G;
  2266. rate->bw = rs_bw_from_sta_bw(sta);
  2267. rate->sgi = rs_sgi_allow(mvm, sta, rate, NULL);
  2268. /* ANT/LDPC/STBC aren't relevant for the rate reported to userspace */
  2269. if (is_mimo(rate)) {
  2270. lq_sta->optimal_rate_mask = lq_sta->active_mimo2_rate;
  2271. } else if (is_siso(rate)) {
  2272. lq_sta->optimal_rate_mask = lq_sta->active_siso_rate;
  2273. } else {
  2274. lq_sta->optimal_rate_mask = lq_sta->active_legacy_rate;
  2275. if (lq_sta->band == IEEE80211_BAND_5GHZ) {
  2276. lq_sta->optimal_rates = rs_optimal_rates_5ghz_legacy;
  2277. lq_sta->optimal_nentries =
  2278. ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
  2279. } else {
  2280. lq_sta->optimal_rates = rs_optimal_rates_24ghz_legacy;
  2281. lq_sta->optimal_nentries =
  2282. ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
  2283. }
  2284. }
  2285. if (is_vht(rate)) {
  2286. if (rate->bw == RATE_MCS_CHAN_WIDTH_20) {
  2287. lq_sta->optimal_rates = rs_optimal_rates_vht_20mhz;
  2288. lq_sta->optimal_nentries =
  2289. ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
  2290. } else {
  2291. lq_sta->optimal_rates = rs_optimal_rates_vht_40_80mhz;
  2292. lq_sta->optimal_nentries =
  2293. ARRAY_SIZE(rs_optimal_rates_vht_40_80mhz);
  2294. }
  2295. } else if (is_ht(rate)) {
  2296. lq_sta->optimal_rates = rs_optimal_rates_ht;
  2297. lq_sta->optimal_nentries = ARRAY_SIZE(rs_optimal_rates_ht);
  2298. }
  2299. }
  2300. /* Compute the optimal rate index based on RSSI */
  2301. static struct rs_rate *rs_get_optimal_rate(struct iwl_mvm *mvm,
  2302. struct iwl_lq_sta *lq_sta)
  2303. {
  2304. struct rs_rate *rate = &lq_sta->optimal_rate;
  2305. int i;
  2306. rate->index = find_first_bit(&lq_sta->optimal_rate_mask,
  2307. BITS_PER_LONG);
  2308. for (i = 0; i < lq_sta->optimal_nentries; i++) {
  2309. int rate_idx = lq_sta->optimal_rates[i].rate_idx;
  2310. if ((lq_sta->pers.last_rssi >= lq_sta->optimal_rates[i].rssi) &&
  2311. (BIT(rate_idx) & lq_sta->optimal_rate_mask)) {
  2312. rate->index = rate_idx;
  2313. break;
  2314. }
  2315. }
  2316. return rate;
  2317. }
  2318. /* Choose an initial legacy rate and antenna to use based on the RSSI
  2319. * of last Rx
  2320. */
  2321. static void rs_get_initial_rate(struct iwl_mvm *mvm,
  2322. struct ieee80211_sta *sta,
  2323. struct iwl_lq_sta *lq_sta,
  2324. enum ieee80211_band band,
  2325. struct rs_rate *rate)
  2326. {
  2327. int i, nentries;
  2328. unsigned long active_rate;
  2329. s8 best_rssi = S8_MIN;
  2330. u8 best_ant = ANT_NONE;
  2331. u8 valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
  2332. const struct rs_init_rate_info *initial_rates;
  2333. for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
  2334. if (!(lq_sta->pers.chains & BIT(i)))
  2335. continue;
  2336. if (lq_sta->pers.chain_signal[i] > best_rssi) {
  2337. best_rssi = lq_sta->pers.chain_signal[i];
  2338. best_ant = BIT(i);
  2339. }
  2340. }
  2341. IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
  2342. rs_pretty_ant(best_ant), best_rssi);
  2343. if (best_ant != ANT_A && best_ant != ANT_B)
  2344. rate->ant = first_antenna(valid_tx_ant);
  2345. else
  2346. rate->ant = best_ant;
  2347. rate->sgi = false;
  2348. rate->ldpc = false;
  2349. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  2350. rate->index = find_first_bit(&lq_sta->active_legacy_rate,
  2351. BITS_PER_LONG);
  2352. if (band == IEEE80211_BAND_5GHZ) {
  2353. rate->type = LQ_LEGACY_A;
  2354. initial_rates = rs_optimal_rates_5ghz_legacy;
  2355. nentries = ARRAY_SIZE(rs_optimal_rates_5ghz_legacy);
  2356. } else {
  2357. rate->type = LQ_LEGACY_G;
  2358. initial_rates = rs_optimal_rates_24ghz_legacy;
  2359. nentries = ARRAY_SIZE(rs_optimal_rates_24ghz_legacy);
  2360. }
  2361. if (!IWL_MVM_RS_RSSI_BASED_INIT_RATE)
  2362. goto out;
  2363. /* Start from a higher rate if the corresponding debug capability
  2364. * is enabled. The rate is chosen according to AP capabilities.
  2365. * In case of VHT/HT when the rssi is low fallback to the case of
  2366. * legacy rates.
  2367. */
  2368. if (sta->vht_cap.vht_supported &&
  2369. best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
  2370. if (sta->bandwidth >= IEEE80211_STA_RX_BW_40) {
  2371. initial_rates = rs_optimal_rates_vht_40_80mhz;
  2372. nentries = ARRAY_SIZE(rs_optimal_rates_vht_40_80mhz);
  2373. if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
  2374. rate->bw = RATE_MCS_CHAN_WIDTH_80;
  2375. else
  2376. rate->bw = RATE_MCS_CHAN_WIDTH_40;
  2377. } else if (sta->bandwidth == IEEE80211_STA_RX_BW_20) {
  2378. initial_rates = rs_optimal_rates_vht_20mhz;
  2379. nentries = ARRAY_SIZE(rs_optimal_rates_vht_20mhz);
  2380. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  2381. } else {
  2382. IWL_ERR(mvm, "Invalid BW %d\n", sta->bandwidth);
  2383. goto out;
  2384. }
  2385. active_rate = lq_sta->active_siso_rate;
  2386. rate->type = LQ_VHT_SISO;
  2387. } else if (sta->ht_cap.ht_supported &&
  2388. best_rssi > IWL_RS_LOW_RSSI_THRESHOLD) {
  2389. initial_rates = rs_optimal_rates_ht;
  2390. nentries = ARRAY_SIZE(rs_optimal_rates_ht);
  2391. active_rate = lq_sta->active_siso_rate;
  2392. rate->type = LQ_HT_SISO;
  2393. } else {
  2394. active_rate = lq_sta->active_legacy_rate;
  2395. }
  2396. for (i = 0; i < nentries; i++) {
  2397. int rate_idx = initial_rates[i].rate_idx;
  2398. if ((best_rssi >= initial_rates[i].rssi) &&
  2399. (BIT(rate_idx) & active_rate)) {
  2400. rate->index = rate_idx;
  2401. break;
  2402. }
  2403. }
  2404. out:
  2405. rs_dump_rate(mvm, rate, "INITIAL");
  2406. }
  2407. /* Save info about RSSI of last Rx */
  2408. void rs_update_last_rssi(struct iwl_mvm *mvm,
  2409. struct iwl_lq_sta *lq_sta,
  2410. struct ieee80211_rx_status *rx_status)
  2411. {
  2412. int i;
  2413. lq_sta->pers.chains = rx_status->chains;
  2414. lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
  2415. lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
  2416. lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
  2417. lq_sta->pers.last_rssi = S8_MIN;
  2418. for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
  2419. if (!(lq_sta->pers.chains & BIT(i)))
  2420. continue;
  2421. if (lq_sta->pers.chain_signal[i] > lq_sta->pers.last_rssi)
  2422. lq_sta->pers.last_rssi = lq_sta->pers.chain_signal[i];
  2423. }
  2424. }
  2425. /**
  2426. * rs_initialize_lq - Initialize a station's hardware rate table
  2427. *
  2428. * The uCode's station table contains a table of fallback rates
  2429. * for automatic fallback during transmission.
  2430. *
  2431. * NOTE: This sets up a default set of values. These will be replaced later
  2432. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  2433. * rc80211_simple.
  2434. *
  2435. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  2436. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  2437. * which requires station table entry to exist).
  2438. */
  2439. static void rs_initialize_lq(struct iwl_mvm *mvm,
  2440. struct ieee80211_sta *sta,
  2441. struct iwl_lq_sta *lq_sta,
  2442. enum ieee80211_band band,
  2443. bool init)
  2444. {
  2445. struct iwl_scale_tbl_info *tbl;
  2446. struct rs_rate *rate;
  2447. u8 active_tbl = 0;
  2448. if (!sta || !lq_sta)
  2449. return;
  2450. if (!lq_sta->search_better_tbl)
  2451. active_tbl = lq_sta->active_tbl;
  2452. else
  2453. active_tbl = 1 - lq_sta->active_tbl;
  2454. tbl = &(lq_sta->lq_info[active_tbl]);
  2455. rate = &tbl->rate;
  2456. rs_get_initial_rate(mvm, sta, lq_sta, band, rate);
  2457. rs_init_optimal_rate(mvm, sta, lq_sta);
  2458. WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
  2459. tbl->column = rs_get_column_from_rate(rate);
  2460. rs_set_expected_tpt_table(lq_sta, tbl);
  2461. rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
  2462. /* TODO restore station should remember the lq cmd */
  2463. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
  2464. }
  2465. static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
  2466. struct ieee80211_tx_rate_control *txrc)
  2467. {
  2468. struct sk_buff *skb = txrc->skb;
  2469. struct iwl_op_mode *op_mode __maybe_unused =
  2470. (struct iwl_op_mode *)mvm_r;
  2471. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2472. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2473. struct iwl_lq_sta *lq_sta = mvm_sta;
  2474. struct rs_rate *optimal_rate;
  2475. u32 last_ucode_rate;
  2476. if (sta && !iwl_mvm_sta_from_mac80211(sta)->vif) {
  2477. /* if vif isn't initialized mvm doesn't know about
  2478. * this station, so don't do anything with the it
  2479. */
  2480. sta = NULL;
  2481. mvm_sta = NULL;
  2482. }
  2483. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  2484. /* Treat uninitialized rate scaling data same as non-existing. */
  2485. if (lq_sta && !lq_sta->pers.drv) {
  2486. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  2487. mvm_sta = NULL;
  2488. }
  2489. /* Send management frames and NO_ACK data using lowest rate. */
  2490. if (rate_control_send_low(sta, mvm_sta, txrc))
  2491. return;
  2492. iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
  2493. info->band, &info->control.rates[0]);
  2494. info->control.rates[0].count = 1;
  2495. /* Report the optimal rate based on rssi and STA caps if we haven't
  2496. * converged yet (too little traffic) or exploring other modulations
  2497. */
  2498. if (lq_sta->rs_state != RS_STATE_STAY_IN_COLUMN) {
  2499. optimal_rate = rs_get_optimal_rate(mvm, lq_sta);
  2500. last_ucode_rate = ucode_rate_from_rs_rate(mvm,
  2501. optimal_rate);
  2502. iwl_mvm_hwrate_to_tx_rate(last_ucode_rate, info->band,
  2503. &txrc->reported_rate);
  2504. }
  2505. }
  2506. static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
  2507. gfp_t gfp)
  2508. {
  2509. struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
  2510. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
  2511. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  2512. struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
  2513. IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
  2514. lq_sta->pers.drv = mvm;
  2515. #ifdef CONFIG_MAC80211_DEBUGFS
  2516. lq_sta->pers.dbg_fixed_rate = 0;
  2517. lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
  2518. lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
  2519. #endif
  2520. lq_sta->pers.chains = 0;
  2521. memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
  2522. lq_sta->pers.last_rssi = S8_MIN;
  2523. return &sta_priv->lq_sta;
  2524. }
  2525. static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
  2526. int nss)
  2527. {
  2528. u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
  2529. (0x3 << (2 * (nss - 1)));
  2530. rx_mcs >>= (2 * (nss - 1));
  2531. if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
  2532. return IWL_RATE_MCS_7_INDEX;
  2533. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
  2534. return IWL_RATE_MCS_8_INDEX;
  2535. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
  2536. return IWL_RATE_MCS_9_INDEX;
  2537. WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
  2538. return -1;
  2539. }
  2540. static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
  2541. struct ieee80211_sta_vht_cap *vht_cap,
  2542. struct iwl_lq_sta *lq_sta)
  2543. {
  2544. int i;
  2545. int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
  2546. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  2547. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  2548. if (i == IWL_RATE_9M_INDEX)
  2549. continue;
  2550. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  2551. if (i == IWL_RATE_MCS_9_INDEX &&
  2552. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  2553. continue;
  2554. lq_sta->active_siso_rate |= BIT(i);
  2555. }
  2556. }
  2557. if (sta->rx_nss < 2)
  2558. return;
  2559. highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
  2560. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  2561. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  2562. if (i == IWL_RATE_9M_INDEX)
  2563. continue;
  2564. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  2565. if (i == IWL_RATE_MCS_9_INDEX &&
  2566. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  2567. continue;
  2568. lq_sta->active_mimo2_rate |= BIT(i);
  2569. }
  2570. }
  2571. }
  2572. static void rs_ht_init(struct iwl_mvm *mvm,
  2573. struct ieee80211_sta *sta,
  2574. struct iwl_lq_sta *lq_sta,
  2575. struct ieee80211_sta_ht_cap *ht_cap)
  2576. {
  2577. /* active_siso_rate mask includes 9 MBits (bit 5),
  2578. * and CCK (bits 0-3), supp_rates[] does not;
  2579. * shift to convert format, force 9 MBits off.
  2580. */
  2581. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2582. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2583. lq_sta->active_siso_rate &= ~((u16)0x2);
  2584. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2585. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2586. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2587. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2588. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2589. if (mvm->cfg->ht_params->ldpc &&
  2590. (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
  2591. lq_sta->ldpc = true;
  2592. if (mvm->cfg->ht_params->stbc &&
  2593. (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
  2594. (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
  2595. lq_sta->stbc_capable = true;
  2596. lq_sta->is_vht = false;
  2597. }
  2598. static void rs_vht_init(struct iwl_mvm *mvm,
  2599. struct ieee80211_sta *sta,
  2600. struct iwl_lq_sta *lq_sta,
  2601. struct ieee80211_sta_vht_cap *vht_cap)
  2602. {
  2603. rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
  2604. if (mvm->cfg->ht_params->ldpc &&
  2605. (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
  2606. lq_sta->ldpc = true;
  2607. if (mvm->cfg->ht_params->stbc &&
  2608. (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
  2609. (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
  2610. lq_sta->stbc_capable = true;
  2611. if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_BEAMFORMER) &&
  2612. (num_of_ant(iwl_mvm_get_valid_tx_ant(mvm)) > 1) &&
  2613. (vht_cap->cap & IEEE80211_VHT_CAP_SU_BEAMFORMEE_CAPABLE))
  2614. lq_sta->bfer_capable = true;
  2615. lq_sta->is_vht = true;
  2616. }
  2617. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2618. static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm)
  2619. {
  2620. spin_lock_bh(&mvm->drv_stats_lock);
  2621. memset(&mvm->drv_rx_stats, 0, sizeof(mvm->drv_rx_stats));
  2622. spin_unlock_bh(&mvm->drv_stats_lock);
  2623. }
  2624. void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm, u32 rate, bool agg)
  2625. {
  2626. u8 nss = 0, mcs = 0;
  2627. spin_lock(&mvm->drv_stats_lock);
  2628. if (agg)
  2629. mvm->drv_rx_stats.agg_frames++;
  2630. mvm->drv_rx_stats.success_frames++;
  2631. switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
  2632. case RATE_MCS_CHAN_WIDTH_20:
  2633. mvm->drv_rx_stats.bw_20_frames++;
  2634. break;
  2635. case RATE_MCS_CHAN_WIDTH_40:
  2636. mvm->drv_rx_stats.bw_40_frames++;
  2637. break;
  2638. case RATE_MCS_CHAN_WIDTH_80:
  2639. mvm->drv_rx_stats.bw_80_frames++;
  2640. break;
  2641. default:
  2642. WARN_ONCE(1, "bad BW. rate 0x%x", rate);
  2643. }
  2644. if (rate & RATE_MCS_HT_MSK) {
  2645. mvm->drv_rx_stats.ht_frames++;
  2646. mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
  2647. nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
  2648. } else if (rate & RATE_MCS_VHT_MSK) {
  2649. mvm->drv_rx_stats.vht_frames++;
  2650. mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
  2651. nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
  2652. RATE_VHT_MCS_NSS_POS) + 1;
  2653. } else {
  2654. mvm->drv_rx_stats.legacy_frames++;
  2655. }
  2656. if (nss == 1)
  2657. mvm->drv_rx_stats.siso_frames++;
  2658. else if (nss == 2)
  2659. mvm->drv_rx_stats.mimo2_frames++;
  2660. if (rate & RATE_MCS_SGI_MSK)
  2661. mvm->drv_rx_stats.sgi_frames++;
  2662. else
  2663. mvm->drv_rx_stats.ngi_frames++;
  2664. mvm->drv_rx_stats.last_rates[mvm->drv_rx_stats.last_frame_idx] = rate;
  2665. mvm->drv_rx_stats.last_frame_idx =
  2666. (mvm->drv_rx_stats.last_frame_idx + 1) %
  2667. ARRAY_SIZE(mvm->drv_rx_stats.last_rates);
  2668. spin_unlock(&mvm->drv_stats_lock);
  2669. }
  2670. #endif
  2671. /*
  2672. * Called after adding a new station to initialize rate scaling
  2673. */
  2674. void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  2675. enum ieee80211_band band, bool init)
  2676. {
  2677. int i, j;
  2678. struct ieee80211_hw *hw = mvm->hw;
  2679. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2680. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  2681. struct iwl_mvm_sta *sta_priv = iwl_mvm_sta_from_mac80211(sta);
  2682. struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
  2683. struct ieee80211_supported_band *sband;
  2684. unsigned long supp; /* must be unsigned long for for_each_set_bit */
  2685. /* clear all non-persistent lq data */
  2686. memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
  2687. sband = hw->wiphy->bands[band];
  2688. lq_sta->lq.sta_id = sta_priv->sta_id;
  2689. sta_priv->tlc_amsdu = false;
  2690. for (j = 0; j < LQ_SIZE; j++)
  2691. rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
  2692. lq_sta->flush_timer = 0;
  2693. lq_sta->last_tx = jiffies;
  2694. IWL_DEBUG_RATE(mvm,
  2695. "LQ: *** rate scale station global init for station %d ***\n",
  2696. sta_priv->sta_id);
  2697. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2698. * the lowest or the highest rate.. Could consider using RSSI from
  2699. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2700. * after assoc.. */
  2701. lq_sta->missed_rate_counter = IWL_MVM_RS_MISSED_RATE_MAX;
  2702. lq_sta->band = sband->band;
  2703. /*
  2704. * active legacy rates as per supported rates bitmap
  2705. */
  2706. supp = sta->supp_rates[sband->band];
  2707. lq_sta->active_legacy_rate = 0;
  2708. for_each_set_bit(i, &supp, BITS_PER_LONG)
  2709. lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
  2710. /* TODO: should probably account for rx_highest for both HT/VHT */
  2711. if (!vht_cap || !vht_cap->vht_supported)
  2712. rs_ht_init(mvm, sta, lq_sta, ht_cap);
  2713. else
  2714. rs_vht_init(mvm, sta, lq_sta, vht_cap);
  2715. lq_sta->max_legacy_rate_idx =
  2716. rs_get_max_rate_from_mask(lq_sta->active_legacy_rate);
  2717. lq_sta->max_siso_rate_idx =
  2718. rs_get_max_rate_from_mask(lq_sta->active_siso_rate);
  2719. lq_sta->max_mimo2_rate_idx =
  2720. rs_get_max_rate_from_mask(lq_sta->active_mimo2_rate);
  2721. IWL_DEBUG_RATE(mvm,
  2722. "LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC=%d BFER=%d\n",
  2723. lq_sta->active_legacy_rate,
  2724. lq_sta->active_siso_rate,
  2725. lq_sta->active_mimo2_rate,
  2726. lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc_capable,
  2727. lq_sta->bfer_capable);
  2728. IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
  2729. lq_sta->max_legacy_rate_idx,
  2730. lq_sta->max_siso_rate_idx,
  2731. lq_sta->max_mimo2_rate_idx);
  2732. /* These values will be overridden later */
  2733. lq_sta->lq.single_stream_ant_msk =
  2734. first_antenna(iwl_mvm_get_valid_tx_ant(mvm));
  2735. lq_sta->lq.dual_stream_ant_msk = ANT_AB;
  2736. /* as default allow aggregation for all tids */
  2737. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2738. lq_sta->is_agg = 0;
  2739. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2740. iwl_mvm_reset_frame_stats(mvm);
  2741. #endif
  2742. rs_initialize_lq(mvm, sta, lq_sta, band, init);
  2743. }
  2744. static void rs_rate_update(void *mvm_r,
  2745. struct ieee80211_supported_band *sband,
  2746. struct cfg80211_chan_def *chandef,
  2747. struct ieee80211_sta *sta, void *priv_sta,
  2748. u32 changed)
  2749. {
  2750. u8 tid;
  2751. struct iwl_op_mode *op_mode =
  2752. (struct iwl_op_mode *)mvm_r;
  2753. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  2754. if (!iwl_mvm_sta_from_mac80211(sta)->vif)
  2755. return;
  2756. /* Stop any ongoing aggregations as rs starts off assuming no agg */
  2757. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
  2758. ieee80211_stop_tx_ba_session(sta, tid);
  2759. iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
  2760. }
  2761. #ifdef CONFIG_MAC80211_DEBUGFS
  2762. static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
  2763. struct iwl_lq_cmd *lq_cmd,
  2764. enum ieee80211_band band,
  2765. u32 ucode_rate)
  2766. {
  2767. struct rs_rate rate;
  2768. int i;
  2769. int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
  2770. __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
  2771. u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
  2772. for (i = 0; i < num_rates; i++)
  2773. lq_cmd->rs_table[i] = ucode_rate_le32;
  2774. rs_rate_from_ucode_rate(ucode_rate, band, &rate);
  2775. if (is_mimo(&rate))
  2776. lq_cmd->mimo_delim = num_rates - 1;
  2777. else
  2778. lq_cmd->mimo_delim = 0;
  2779. lq_cmd->reduced_tpc = 0;
  2780. if (num_of_ant(ant) == 1)
  2781. lq_cmd->single_stream_ant_msk = ant;
  2782. lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2783. }
  2784. #endif /* CONFIG_MAC80211_DEBUGFS */
  2785. static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
  2786. struct iwl_lq_sta *lq_sta,
  2787. struct rs_rate *rate,
  2788. __le32 *rs_table, int *rs_table_index,
  2789. int num_rates, int num_retries,
  2790. u8 valid_tx_ant, bool toggle_ant)
  2791. {
  2792. int i, j;
  2793. __le32 ucode_rate;
  2794. bool bottom_reached = false;
  2795. int prev_rate_idx = rate->index;
  2796. int end = LINK_QUAL_MAX_RETRY_NUM;
  2797. int index = *rs_table_index;
  2798. for (i = 0; i < num_rates && index < end; i++) {
  2799. for (j = 0; j < num_retries && index < end; j++, index++) {
  2800. ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm,
  2801. rate));
  2802. rs_table[index] = ucode_rate;
  2803. if (toggle_ant)
  2804. rs_toggle_antenna(valid_tx_ant, rate);
  2805. }
  2806. prev_rate_idx = rate->index;
  2807. bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
  2808. if (bottom_reached && !is_legacy(rate))
  2809. break;
  2810. }
  2811. if (!bottom_reached && !is_legacy(rate))
  2812. rate->index = prev_rate_idx;
  2813. *rs_table_index = index;
  2814. }
  2815. /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
  2816. * column the rate table should look like this:
  2817. *
  2818. * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
  2819. * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
  2820. * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
  2821. * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
  2822. * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
  2823. * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
  2824. * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
  2825. * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
  2826. * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
  2827. * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
  2828. * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
  2829. * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
  2830. * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
  2831. * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
  2832. * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
  2833. * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
  2834. */
  2835. static void rs_build_rates_table(struct iwl_mvm *mvm,
  2836. struct ieee80211_sta *sta,
  2837. struct iwl_lq_sta *lq_sta,
  2838. const struct rs_rate *initial_rate)
  2839. {
  2840. struct rs_rate rate;
  2841. int num_rates, num_retries, index = 0;
  2842. u8 valid_tx_ant = 0;
  2843. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2844. bool toggle_ant = false;
  2845. memcpy(&rate, initial_rate, sizeof(rate));
  2846. valid_tx_ant = iwl_mvm_get_valid_tx_ant(mvm);
  2847. /* TODO: remove old API when min FW API hits 14 */
  2848. if (!fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS) &&
  2849. rs_stbc_allow(mvm, sta, lq_sta))
  2850. rate.stbc = true;
  2851. if (is_siso(&rate)) {
  2852. num_rates = IWL_MVM_RS_INITIAL_SISO_NUM_RATES;
  2853. num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
  2854. } else if (is_mimo(&rate)) {
  2855. num_rates = IWL_MVM_RS_INITIAL_MIMO_NUM_RATES;
  2856. num_retries = IWL_MVM_RS_HT_VHT_RETRIES_PER_RATE;
  2857. } else {
  2858. num_rates = IWL_MVM_RS_INITIAL_LEGACY_NUM_RATES;
  2859. num_retries = IWL_MVM_RS_INITIAL_LEGACY_RETRIES;
  2860. toggle_ant = true;
  2861. }
  2862. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2863. num_rates, num_retries, valid_tx_ant,
  2864. toggle_ant);
  2865. rs_get_lower_rate_down_column(lq_sta, &rate);
  2866. if (is_siso(&rate)) {
  2867. num_rates = IWL_MVM_RS_SECONDARY_SISO_NUM_RATES;
  2868. num_retries = IWL_MVM_RS_SECONDARY_SISO_RETRIES;
  2869. lq_cmd->mimo_delim = index;
  2870. } else if (is_legacy(&rate)) {
  2871. num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
  2872. num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
  2873. } else {
  2874. WARN_ON_ONCE(1);
  2875. }
  2876. toggle_ant = true;
  2877. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2878. num_rates, num_retries, valid_tx_ant,
  2879. toggle_ant);
  2880. rs_get_lower_rate_down_column(lq_sta, &rate);
  2881. num_rates = IWL_MVM_RS_SECONDARY_LEGACY_NUM_RATES;
  2882. num_retries = IWL_MVM_RS_SECONDARY_LEGACY_RETRIES;
  2883. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2884. num_rates, num_retries, valid_tx_ant,
  2885. toggle_ant);
  2886. }
  2887. struct rs_bfer_active_iter_data {
  2888. struct ieee80211_sta *exclude_sta;
  2889. struct iwl_mvm_sta *bfer_mvmsta;
  2890. };
  2891. static void rs_bfer_active_iter(void *_data,
  2892. struct ieee80211_sta *sta)
  2893. {
  2894. struct rs_bfer_active_iter_data *data = _data;
  2895. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2896. struct iwl_lq_cmd *lq_cmd = &mvmsta->lq_sta.lq;
  2897. u32 ss_params = le32_to_cpu(lq_cmd->ss_params);
  2898. if (sta == data->exclude_sta)
  2899. return;
  2900. /* The current sta has BFER allowed */
  2901. if (ss_params & LQ_SS_BFER_ALLOWED) {
  2902. WARN_ON_ONCE(data->bfer_mvmsta != NULL);
  2903. data->bfer_mvmsta = mvmsta;
  2904. }
  2905. }
  2906. static int rs_bfer_priority(struct iwl_mvm_sta *sta)
  2907. {
  2908. int prio = -1;
  2909. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(sta->vif);
  2910. switch (viftype) {
  2911. case NL80211_IFTYPE_AP:
  2912. case NL80211_IFTYPE_P2P_GO:
  2913. prio = 3;
  2914. break;
  2915. case NL80211_IFTYPE_P2P_CLIENT:
  2916. prio = 2;
  2917. break;
  2918. case NL80211_IFTYPE_STATION:
  2919. prio = 1;
  2920. break;
  2921. default:
  2922. WARN_ONCE(true, "viftype %d sta_id %d", viftype, sta->sta_id);
  2923. prio = -1;
  2924. }
  2925. return prio;
  2926. }
  2927. /* Returns >0 if sta1 has a higher BFER priority compared to sta2 */
  2928. static int rs_bfer_priority_cmp(struct iwl_mvm_sta *sta1,
  2929. struct iwl_mvm_sta *sta2)
  2930. {
  2931. int prio1 = rs_bfer_priority(sta1);
  2932. int prio2 = rs_bfer_priority(sta2);
  2933. if (prio1 > prio2)
  2934. return 1;
  2935. if (prio1 < prio2)
  2936. return -1;
  2937. return 0;
  2938. }
  2939. static void rs_set_lq_ss_params(struct iwl_mvm *mvm,
  2940. struct ieee80211_sta *sta,
  2941. struct iwl_lq_sta *lq_sta,
  2942. const struct rs_rate *initial_rate)
  2943. {
  2944. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2945. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2946. struct rs_bfer_active_iter_data data = {
  2947. .exclude_sta = sta,
  2948. .bfer_mvmsta = NULL,
  2949. };
  2950. struct iwl_mvm_sta *bfer_mvmsta = NULL;
  2951. u32 ss_params = LQ_SS_PARAMS_VALID;
  2952. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  2953. goto out;
  2954. #ifdef CONFIG_MAC80211_DEBUGFS
  2955. /* Check if forcing the decision is configured.
  2956. * Note that SISO is forced by not allowing STBC or BFER
  2957. */
  2958. if (lq_sta->pers.ss_force == RS_SS_FORCE_STBC)
  2959. ss_params |= (LQ_SS_STBC_1SS_ALLOWED | LQ_SS_FORCE);
  2960. else if (lq_sta->pers.ss_force == RS_SS_FORCE_BFER)
  2961. ss_params |= (LQ_SS_BFER_ALLOWED | LQ_SS_FORCE);
  2962. if (lq_sta->pers.ss_force != RS_SS_FORCE_NONE) {
  2963. IWL_DEBUG_RATE(mvm, "Forcing single stream Tx decision %d\n",
  2964. lq_sta->pers.ss_force);
  2965. goto out;
  2966. }
  2967. #endif
  2968. if (lq_sta->stbc_capable)
  2969. ss_params |= LQ_SS_STBC_1SS_ALLOWED;
  2970. if (!lq_sta->bfer_capable)
  2971. goto out;
  2972. ieee80211_iterate_stations_atomic(mvm->hw,
  2973. rs_bfer_active_iter,
  2974. &data);
  2975. bfer_mvmsta = data.bfer_mvmsta;
  2976. /* This code is safe as it doesn't run concurrently for different
  2977. * stations. This is guaranteed by the fact that calls to
  2978. * ieee80211_tx_status wouldn't run concurrently for a single HW.
  2979. */
  2980. if (!bfer_mvmsta) {
  2981. IWL_DEBUG_RATE(mvm, "No sta with BFER allowed found. Allow\n");
  2982. ss_params |= LQ_SS_BFER_ALLOWED;
  2983. goto out;
  2984. }
  2985. IWL_DEBUG_RATE(mvm, "Found existing sta %d with BFER activated\n",
  2986. bfer_mvmsta->sta_id);
  2987. /* Disallow BFER on another STA if active and we're a higher priority */
  2988. if (rs_bfer_priority_cmp(mvmsta, bfer_mvmsta) > 0) {
  2989. struct iwl_lq_cmd *bfersta_lq_cmd = &bfer_mvmsta->lq_sta.lq;
  2990. u32 bfersta_ss_params = le32_to_cpu(bfersta_lq_cmd->ss_params);
  2991. bfersta_ss_params &= ~LQ_SS_BFER_ALLOWED;
  2992. bfersta_lq_cmd->ss_params = cpu_to_le32(bfersta_ss_params);
  2993. iwl_mvm_send_lq_cmd(mvm, bfersta_lq_cmd, false);
  2994. ss_params |= LQ_SS_BFER_ALLOWED;
  2995. IWL_DEBUG_RATE(mvm,
  2996. "Lower priority BFER sta found (%d). Switch BFER\n",
  2997. bfer_mvmsta->sta_id);
  2998. }
  2999. out:
  3000. lq_cmd->ss_params = cpu_to_le32(ss_params);
  3001. }
  3002. static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
  3003. struct ieee80211_sta *sta,
  3004. struct iwl_lq_sta *lq_sta,
  3005. const struct rs_rate *initial_rate)
  3006. {
  3007. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  3008. struct iwl_mvm_sta *mvmsta;
  3009. struct iwl_mvm_vif *mvmvif;
  3010. lq_cmd->agg_disable_start_th = IWL_MVM_RS_AGG_DISABLE_START;
  3011. lq_cmd->agg_time_limit =
  3012. cpu_to_le16(IWL_MVM_RS_AGG_TIME_LIMIT);
  3013. #ifdef CONFIG_MAC80211_DEBUGFS
  3014. if (lq_sta->pers.dbg_fixed_rate) {
  3015. rs_build_rates_table_from_fixed(mvm, lq_cmd,
  3016. lq_sta->band,
  3017. lq_sta->pers.dbg_fixed_rate);
  3018. return;
  3019. }
  3020. #endif
  3021. if (WARN_ON_ONCE(!sta || !initial_rate))
  3022. return;
  3023. rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
  3024. if (fw_has_api(&mvm->fw->ucode_capa, IWL_UCODE_TLV_API_LQ_SS_PARAMS))
  3025. rs_set_lq_ss_params(mvm, sta, lq_sta, initial_rate);
  3026. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  3027. mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  3028. if (num_of_ant(initial_rate->ant) == 1)
  3029. lq_cmd->single_stream_ant_msk = initial_rate->ant;
  3030. lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  3031. /*
  3032. * In case of low latency, tell the firmware to leave a frame in the
  3033. * Tx Fifo so that it can start a transaction in the same TxOP. This
  3034. * basically allows the firmware to send bursts.
  3035. */
  3036. if (iwl_mvm_vif_low_latency(mvmvif))
  3037. lq_cmd->agg_frame_cnt_limit--;
  3038. if (mvmsta->vif->p2p)
  3039. lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
  3040. lq_cmd->agg_time_limit =
  3041. cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
  3042. }
  3043. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  3044. {
  3045. return hw->priv;
  3046. }
  3047. /* rate scale requires free function to be implemented */
  3048. static void rs_free(void *mvm_rate)
  3049. {
  3050. return;
  3051. }
  3052. static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
  3053. void *mvm_sta)
  3054. {
  3055. struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
  3056. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  3057. IWL_DEBUG_RATE(mvm, "enter\n");
  3058. IWL_DEBUG_RATE(mvm, "leave\n");
  3059. }
  3060. #ifdef CONFIG_MAC80211_DEBUGFS
  3061. int rs_pretty_print_rate(char *buf, const u32 rate)
  3062. {
  3063. char *type, *bw;
  3064. u8 mcs = 0, nss = 0;
  3065. u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
  3066. if (!(rate & RATE_MCS_HT_MSK) &&
  3067. !(rate & RATE_MCS_VHT_MSK)) {
  3068. int index = iwl_hwrate_to_plcp_idx(rate);
  3069. return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
  3070. rs_pretty_ant(ant),
  3071. index == IWL_RATE_INVALID ? "BAD" :
  3072. iwl_rate_mcs[index].mbps);
  3073. }
  3074. if (rate & RATE_MCS_VHT_MSK) {
  3075. type = "VHT";
  3076. mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
  3077. nss = ((rate & RATE_VHT_MCS_NSS_MSK)
  3078. >> RATE_VHT_MCS_NSS_POS) + 1;
  3079. } else if (rate & RATE_MCS_HT_MSK) {
  3080. type = "HT";
  3081. mcs = rate & RATE_HT_MCS_INDEX_MSK;
  3082. } else {
  3083. type = "Unknown"; /* shouldn't happen */
  3084. }
  3085. switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
  3086. case RATE_MCS_CHAN_WIDTH_20:
  3087. bw = "20Mhz";
  3088. break;
  3089. case RATE_MCS_CHAN_WIDTH_40:
  3090. bw = "40Mhz";
  3091. break;
  3092. case RATE_MCS_CHAN_WIDTH_80:
  3093. bw = "80Mhz";
  3094. break;
  3095. case RATE_MCS_CHAN_WIDTH_160:
  3096. bw = "160Mhz";
  3097. break;
  3098. default:
  3099. bw = "BAD BW";
  3100. }
  3101. return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
  3102. type, rs_pretty_ant(ant), bw, mcs, nss,
  3103. (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
  3104. (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
  3105. (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
  3106. (rate & RATE_MCS_BF_MSK) ? "BF " : "",
  3107. (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
  3108. }
  3109. /**
  3110. * Program the device to use fixed rate for frame transmit
  3111. * This is for debugging/testing only
  3112. * once the device start use fixed rate, we need to reload the module
  3113. * to being back the normal operation.
  3114. */
  3115. static void rs_program_fix_rate(struct iwl_mvm *mvm,
  3116. struct iwl_lq_sta *lq_sta)
  3117. {
  3118. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  3119. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  3120. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  3121. IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
  3122. lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
  3123. if (lq_sta->pers.dbg_fixed_rate) {
  3124. rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
  3125. iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
  3126. }
  3127. }
  3128. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  3129. const char __user *user_buf, size_t count, loff_t *ppos)
  3130. {
  3131. struct iwl_lq_sta *lq_sta = file->private_data;
  3132. struct iwl_mvm *mvm;
  3133. char buf[64];
  3134. size_t buf_size;
  3135. u32 parsed_rate;
  3136. mvm = lq_sta->pers.drv;
  3137. memset(buf, 0, sizeof(buf));
  3138. buf_size = min(count, sizeof(buf) - 1);
  3139. if (copy_from_user(buf, user_buf, buf_size))
  3140. return -EFAULT;
  3141. if (sscanf(buf, "%x", &parsed_rate) == 1)
  3142. lq_sta->pers.dbg_fixed_rate = parsed_rate;
  3143. else
  3144. lq_sta->pers.dbg_fixed_rate = 0;
  3145. rs_program_fix_rate(mvm, lq_sta);
  3146. return count;
  3147. }
  3148. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  3149. char __user *user_buf, size_t count, loff_t *ppos)
  3150. {
  3151. char *buff;
  3152. int desc = 0;
  3153. int i = 0;
  3154. ssize_t ret;
  3155. struct iwl_lq_sta *lq_sta = file->private_data;
  3156. struct iwl_mvm_sta *mvmsta =
  3157. container_of(lq_sta, struct iwl_mvm_sta, lq_sta);
  3158. struct iwl_mvm *mvm;
  3159. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  3160. struct rs_rate *rate = &tbl->rate;
  3161. u32 ss_params;
  3162. mvm = lq_sta->pers.drv;
  3163. buff = kmalloc(2048, GFP_KERNEL);
  3164. if (!buff)
  3165. return -ENOMEM;
  3166. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  3167. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
  3168. lq_sta->total_failed, lq_sta->total_success,
  3169. lq_sta->active_legacy_rate);
  3170. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  3171. lq_sta->pers.dbg_fixed_rate);
  3172. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  3173. (iwl_mvm_get_valid_tx_ant(mvm) & ANT_A) ? "ANT_A," : "",
  3174. (iwl_mvm_get_valid_tx_ant(mvm) & ANT_B) ? "ANT_B," : "",
  3175. (iwl_mvm_get_valid_tx_ant(mvm) & ANT_C) ? "ANT_C" : "");
  3176. desc += sprintf(buff+desc, "lq type %s\n",
  3177. (is_legacy(rate)) ? "legacy" :
  3178. is_vht(rate) ? "VHT" : "HT");
  3179. if (!is_legacy(rate)) {
  3180. desc += sprintf(buff + desc, " %s",
  3181. (is_siso(rate)) ? "SISO" : "MIMO2");
  3182. desc += sprintf(buff + desc, " %s",
  3183. (is_ht20(rate)) ? "20MHz" :
  3184. (is_ht40(rate)) ? "40MHz" :
  3185. (is_ht80(rate)) ? "80Mhz" : "BAD BW");
  3186. desc += sprintf(buff + desc, " %s %s %s %s\n",
  3187. (rate->sgi) ? "SGI" : "NGI",
  3188. (rate->ldpc) ? "LDPC" : "BCC",
  3189. (lq_sta->is_agg) ? "AGG on" : "",
  3190. (mvmsta->tlc_amsdu) ? "AMSDU on" : "");
  3191. }
  3192. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  3193. lq_sta->last_rate_n_flags);
  3194. desc += sprintf(buff+desc,
  3195. "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
  3196. lq_sta->lq.flags,
  3197. lq_sta->lq.mimo_delim,
  3198. lq_sta->lq.single_stream_ant_msk,
  3199. lq_sta->lq.dual_stream_ant_msk);
  3200. desc += sprintf(buff+desc,
  3201. "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  3202. le16_to_cpu(lq_sta->lq.agg_time_limit),
  3203. lq_sta->lq.agg_disable_start_th,
  3204. lq_sta->lq.agg_frame_cnt_limit);
  3205. desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
  3206. ss_params = le32_to_cpu(lq_sta->lq.ss_params);
  3207. desc += sprintf(buff+desc, "single stream params: %s%s%s%s\n",
  3208. (ss_params & LQ_SS_PARAMS_VALID) ?
  3209. "VALID" : "INVALID",
  3210. (ss_params & LQ_SS_BFER_ALLOWED) ?
  3211. ", BFER" : "",
  3212. (ss_params & LQ_SS_STBC_1SS_ALLOWED) ?
  3213. ", STBC" : "",
  3214. (ss_params & LQ_SS_FORCE) ?
  3215. ", FORCE" : "");
  3216. desc += sprintf(buff+desc,
  3217. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  3218. lq_sta->lq.initial_rate_index[0],
  3219. lq_sta->lq.initial_rate_index[1],
  3220. lq_sta->lq.initial_rate_index[2],
  3221. lq_sta->lq.initial_rate_index[3]);
  3222. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  3223. u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
  3224. desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
  3225. desc += rs_pretty_print_rate(buff+desc, r);
  3226. }
  3227. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  3228. kfree(buff);
  3229. return ret;
  3230. }
  3231. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  3232. .write = rs_sta_dbgfs_scale_table_write,
  3233. .read = rs_sta_dbgfs_scale_table_read,
  3234. .open = simple_open,
  3235. .llseek = default_llseek,
  3236. };
  3237. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  3238. char __user *user_buf, size_t count, loff_t *ppos)
  3239. {
  3240. char *buff;
  3241. int desc = 0;
  3242. int i, j;
  3243. ssize_t ret;
  3244. struct iwl_scale_tbl_info *tbl;
  3245. struct rs_rate *rate;
  3246. struct iwl_lq_sta *lq_sta = file->private_data;
  3247. buff = kmalloc(1024, GFP_KERNEL);
  3248. if (!buff)
  3249. return -ENOMEM;
  3250. for (i = 0; i < LQ_SIZE; i++) {
  3251. tbl = &(lq_sta->lq_info[i]);
  3252. rate = &tbl->rate;
  3253. desc += sprintf(buff+desc,
  3254. "%s type=%d SGI=%d BW=%s DUP=0\n"
  3255. "index=%d\n",
  3256. lq_sta->active_tbl == i ? "*" : "x",
  3257. rate->type,
  3258. rate->sgi,
  3259. is_ht20(rate) ? "20Mhz" :
  3260. is_ht40(rate) ? "40Mhz" :
  3261. is_ht80(rate) ? "80Mhz" : "ERR",
  3262. rate->index);
  3263. for (j = 0; j < IWL_RATE_COUNT; j++) {
  3264. desc += sprintf(buff+desc,
  3265. "counter=%d success=%d %%=%d\n",
  3266. tbl->win[j].counter,
  3267. tbl->win[j].success_counter,
  3268. tbl->win[j].success_ratio);
  3269. }
  3270. }
  3271. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  3272. kfree(buff);
  3273. return ret;
  3274. }
  3275. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  3276. .read = rs_sta_dbgfs_stats_table_read,
  3277. .open = simple_open,
  3278. .llseek = default_llseek,
  3279. };
  3280. static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
  3281. char __user *user_buf,
  3282. size_t count, loff_t *ppos)
  3283. {
  3284. static const char * const column_name[] = {
  3285. [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
  3286. [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
  3287. [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
  3288. [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
  3289. [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
  3290. [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
  3291. [RS_COLUMN_MIMO2] = "MIMO2",
  3292. [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
  3293. };
  3294. static const char * const rate_name[] = {
  3295. [IWL_RATE_1M_INDEX] = "1M",
  3296. [IWL_RATE_2M_INDEX] = "2M",
  3297. [IWL_RATE_5M_INDEX] = "5.5M",
  3298. [IWL_RATE_11M_INDEX] = "11M",
  3299. [IWL_RATE_6M_INDEX] = "6M|MCS0",
  3300. [IWL_RATE_9M_INDEX] = "9M",
  3301. [IWL_RATE_12M_INDEX] = "12M|MCS1",
  3302. [IWL_RATE_18M_INDEX] = "18M|MCS2",
  3303. [IWL_RATE_24M_INDEX] = "24M|MCS3",
  3304. [IWL_RATE_36M_INDEX] = "36M|MCS4",
  3305. [IWL_RATE_48M_INDEX] = "48M|MCS5",
  3306. [IWL_RATE_54M_INDEX] = "54M|MCS6",
  3307. [IWL_RATE_MCS_7_INDEX] = "MCS7",
  3308. [IWL_RATE_MCS_8_INDEX] = "MCS8",
  3309. [IWL_RATE_MCS_9_INDEX] = "MCS9",
  3310. };
  3311. char *buff, *pos, *endpos;
  3312. int col, rate;
  3313. ssize_t ret;
  3314. struct iwl_lq_sta *lq_sta = file->private_data;
  3315. struct rs_rate_stats *stats;
  3316. static const size_t bufsz = 1024;
  3317. buff = kmalloc(bufsz, GFP_KERNEL);
  3318. if (!buff)
  3319. return -ENOMEM;
  3320. pos = buff;
  3321. endpos = pos + bufsz;
  3322. pos += scnprintf(pos, endpos - pos, "COLUMN,");
  3323. for (rate = 0; rate < IWL_RATE_COUNT; rate++)
  3324. pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
  3325. pos += scnprintf(pos, endpos - pos, "\n");
  3326. for (col = 0; col < RS_COLUMN_COUNT; col++) {
  3327. pos += scnprintf(pos, endpos - pos,
  3328. "%s,", column_name[col]);
  3329. for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
  3330. stats = &(lq_sta->pers.tx_stats[col][rate]);
  3331. pos += scnprintf(pos, endpos - pos,
  3332. "%llu/%llu,",
  3333. stats->success,
  3334. stats->total);
  3335. }
  3336. pos += scnprintf(pos, endpos - pos, "\n");
  3337. }
  3338. ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
  3339. kfree(buff);
  3340. return ret;
  3341. }
  3342. static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
  3343. const char __user *user_buf,
  3344. size_t count, loff_t *ppos)
  3345. {
  3346. struct iwl_lq_sta *lq_sta = file->private_data;
  3347. memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
  3348. return count;
  3349. }
  3350. static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
  3351. .read = rs_sta_dbgfs_drv_tx_stats_read,
  3352. .write = rs_sta_dbgfs_drv_tx_stats_write,
  3353. .open = simple_open,
  3354. .llseek = default_llseek,
  3355. };
  3356. static ssize_t iwl_dbgfs_ss_force_read(struct file *file,
  3357. char __user *user_buf,
  3358. size_t count, loff_t *ppos)
  3359. {
  3360. struct iwl_lq_sta *lq_sta = file->private_data;
  3361. char buf[12];
  3362. int bufsz = sizeof(buf);
  3363. int pos = 0;
  3364. static const char * const ss_force_name[] = {
  3365. [RS_SS_FORCE_NONE] = "none",
  3366. [RS_SS_FORCE_STBC] = "stbc",
  3367. [RS_SS_FORCE_BFER] = "bfer",
  3368. [RS_SS_FORCE_SISO] = "siso",
  3369. };
  3370. pos += scnprintf(buf+pos, bufsz-pos, "%s\n",
  3371. ss_force_name[lq_sta->pers.ss_force]);
  3372. return simple_read_from_buffer(user_buf, count, ppos, buf, pos);
  3373. }
  3374. static ssize_t iwl_dbgfs_ss_force_write(struct iwl_lq_sta *lq_sta, char *buf,
  3375. size_t count, loff_t *ppos)
  3376. {
  3377. struct iwl_mvm *mvm = lq_sta->pers.drv;
  3378. int ret = 0;
  3379. if (!strncmp("none", buf, 4)) {
  3380. lq_sta->pers.ss_force = RS_SS_FORCE_NONE;
  3381. } else if (!strncmp("siso", buf, 4)) {
  3382. lq_sta->pers.ss_force = RS_SS_FORCE_SISO;
  3383. } else if (!strncmp("stbc", buf, 4)) {
  3384. if (lq_sta->stbc_capable) {
  3385. lq_sta->pers.ss_force = RS_SS_FORCE_STBC;
  3386. } else {
  3387. IWL_ERR(mvm,
  3388. "can't force STBC. peer doesn't support\n");
  3389. ret = -EINVAL;
  3390. }
  3391. } else if (!strncmp("bfer", buf, 4)) {
  3392. if (lq_sta->bfer_capable) {
  3393. lq_sta->pers.ss_force = RS_SS_FORCE_BFER;
  3394. } else {
  3395. IWL_ERR(mvm,
  3396. "can't force BFER. peer doesn't support\n");
  3397. ret = -EINVAL;
  3398. }
  3399. } else {
  3400. IWL_ERR(mvm, "valid values none|siso|stbc|bfer\n");
  3401. ret = -EINVAL;
  3402. }
  3403. return ret ?: count;
  3404. }
  3405. #define MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz) \
  3406. _MVM_DEBUGFS_READ_WRITE_FILE_OPS(name, bufsz, struct iwl_lq_sta)
  3407. #define MVM_DEBUGFS_ADD_FILE_RS(name, parent, mode) do { \
  3408. if (!debugfs_create_file(#name, mode, parent, lq_sta, \
  3409. &iwl_dbgfs_##name##_ops)) \
  3410. goto err; \
  3411. } while (0)
  3412. MVM_DEBUGFS_READ_WRITE_FILE_OPS(ss_force, 32);
  3413. static void rs_add_debugfs(void *mvm, void *priv_sta, struct dentry *dir)
  3414. {
  3415. struct iwl_lq_sta *lq_sta = priv_sta;
  3416. struct iwl_mvm_sta *mvmsta;
  3417. mvmsta = container_of(lq_sta, struct iwl_mvm_sta, lq_sta);
  3418. if (!mvmsta->vif)
  3419. return;
  3420. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  3421. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  3422. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  3423. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  3424. debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
  3425. lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
  3426. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  3427. &lq_sta->tx_agg_tid_en);
  3428. debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
  3429. &lq_sta->pers.dbg_fixed_txp_reduction);
  3430. MVM_DEBUGFS_ADD_FILE_RS(ss_force, dir, S_IRUSR | S_IWUSR);
  3431. return;
  3432. err:
  3433. IWL_ERR((struct iwl_mvm *)mvm, "Can't create debugfs entity\n");
  3434. }
  3435. static void rs_remove_debugfs(void *mvm, void *mvm_sta)
  3436. {
  3437. }
  3438. #endif
  3439. /*
  3440. * Initialization of rate scaling information is done by driver after
  3441. * the station is added. Since mac80211 calls this function before a
  3442. * station is added we ignore it.
  3443. */
  3444. static void rs_rate_init_stub(void *mvm_r,
  3445. struct ieee80211_supported_band *sband,
  3446. struct cfg80211_chan_def *chandef,
  3447. struct ieee80211_sta *sta, void *mvm_sta)
  3448. {
  3449. }
  3450. static const struct rate_control_ops rs_mvm_ops = {
  3451. .name = RS_NAME,
  3452. .tx_status = rs_mac80211_tx_status,
  3453. .get_rate = rs_get_rate,
  3454. .rate_init = rs_rate_init_stub,
  3455. .alloc = rs_alloc,
  3456. .free = rs_free,
  3457. .alloc_sta = rs_alloc_sta,
  3458. .free_sta = rs_free_sta,
  3459. .rate_update = rs_rate_update,
  3460. #ifdef CONFIG_MAC80211_DEBUGFS
  3461. .add_sta_debugfs = rs_add_debugfs,
  3462. .remove_sta_debugfs = rs_remove_debugfs,
  3463. #endif
  3464. };
  3465. int iwl_mvm_rate_control_register(void)
  3466. {
  3467. return ieee80211_rate_control_register(&rs_mvm_ops);
  3468. }
  3469. void iwl_mvm_rate_control_unregister(void)
  3470. {
  3471. ieee80211_rate_control_unregister(&rs_mvm_ops);
  3472. }
  3473. /**
  3474. * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
  3475. * Tx protection, according to this request and previous requests,
  3476. * and send the LQ command.
  3477. * @mvmsta: The station
  3478. * @enable: Enable Tx protection?
  3479. */
  3480. int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  3481. bool enable)
  3482. {
  3483. struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
  3484. lockdep_assert_held(&mvm->mutex);
  3485. if (enable) {
  3486. if (mvmsta->tx_protection == 0)
  3487. lq->flags |= LQ_FLAG_USE_RTS_MSK;
  3488. mvmsta->tx_protection++;
  3489. } else {
  3490. mvmsta->tx_protection--;
  3491. if (mvmsta->tx_protection == 0)
  3492. lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
  3493. }
  3494. return iwl_mvm_send_lq_cmd(mvm, lq, false);
  3495. }