rs.c 117 KB

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