rs.c 105 KB

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