rs.c 105 KB

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