rs.c 117 KB

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