rs.c 116 KB

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