rs.c 116 KB

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