rs.c 117 KB

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