rs.c 103 KB

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