rs.c 111 KB

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