rs.c 115 KB

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