rs.c 116 KB

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