rs.c 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2013 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.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. #define RS_NAME "iwl-mvm-rs"
  41. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  42. #define IWL_NUMBER_TRY 1
  43. #define IWL_HT_NUMBER_TRY 3
  44. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  45. #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
  46. #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  47. /* max allowed rate miss before sync LQ cmd */
  48. #define IWL_MISSED_RATE_MAX 15
  49. /* max time to accum history 2 seconds */
  50. #define IWL_RATE_SCALE_FLUSH_INTVL (3*HZ)
  51. static u8 rs_ht_to_legacy[] = {
  52. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  53. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  54. IWL_RATE_6M_INDEX,
  55. IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
  56. IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
  57. IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
  58. IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
  59. };
  60. static const u8 ant_toggle_lookup[] = {
  61. /*ANT_NONE -> */ ANT_NONE,
  62. /*ANT_A -> */ ANT_B,
  63. /*ANT_B -> */ ANT_C,
  64. /*ANT_AB -> */ ANT_BC,
  65. /*ANT_C -> */ ANT_A,
  66. /*ANT_AC -> */ ANT_AB,
  67. /*ANT_BC -> */ ANT_AC,
  68. /*ANT_ABC -> */ ANT_ABC,
  69. };
  70. #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  71. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  72. IWL_RATE_SISO_##s##M_PLCP, \
  73. IWL_RATE_MIMO2_##s##M_PLCP,\
  74. IWL_RATE_MIMO3_##s##M_PLCP,\
  75. IWL_RATE_##r##M_IEEE, \
  76. IWL_RATE_##ip##M_INDEX, \
  77. IWL_RATE_##in##M_INDEX, \
  78. IWL_RATE_##rp##M_INDEX, \
  79. IWL_RATE_##rn##M_INDEX, \
  80. IWL_RATE_##pp##M_INDEX, \
  81. IWL_RATE_##np##M_INDEX }
  82. /*
  83. * Parameter order:
  84. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  85. *
  86. * If there isn't a valid next or previous rate then INV is used which
  87. * maps to IWL_RATE_INVALID
  88. *
  89. */
  90. static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
  91. IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  92. IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  93. IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  94. IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  95. IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  96. IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  97. IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  98. IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  99. IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  100. IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  101. IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  102. IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  103. IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  104. /* FIXME:RS: ^^ should be INV (legacy) */
  105. };
  106. static inline u8 rs_extract_rate(u32 rate_n_flags)
  107. {
  108. /* also works for HT because bits 7:6 are zero there */
  109. return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
  110. }
  111. static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
  112. {
  113. int idx = 0;
  114. /* HT rate format */
  115. if (rate_n_flags & RATE_MCS_HT_MSK) {
  116. idx = rs_extract_rate(rate_n_flags);
  117. if (idx >= IWL_RATE_MIMO3_6M_PLCP)
  118. idx = idx - IWL_RATE_MIMO3_6M_PLCP;
  119. else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
  120. idx = idx - IWL_RATE_MIMO2_6M_PLCP;
  121. idx += IWL_FIRST_OFDM_RATE;
  122. /* skip 9M not supported in ht*/
  123. if (idx >= IWL_RATE_9M_INDEX)
  124. idx += 1;
  125. if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
  126. return idx;
  127. /* legacy rate format, search for match in table */
  128. } else {
  129. for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
  130. if (iwl_rates[idx].plcp ==
  131. rs_extract_rate(rate_n_flags))
  132. return idx;
  133. }
  134. return -1;
  135. }
  136. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  137. struct sk_buff *skb,
  138. struct ieee80211_sta *sta,
  139. struct iwl_lq_sta *lq_sta);
  140. static void rs_fill_link_cmd(struct iwl_mvm *mvm,
  141. struct iwl_lq_sta *lq_sta, u32 rate_n_flags);
  142. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
  143. #ifdef CONFIG_MAC80211_DEBUGFS
  144. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  145. u32 *rate_n_flags, int index);
  146. #else
  147. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  148. u32 *rate_n_flags, int index)
  149. {}
  150. #endif
  151. /**
  152. * The following tables contain the expected throughput metrics for all rates
  153. *
  154. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  155. *
  156. * where invalid entries are zeros.
  157. *
  158. * CCK rates are only valid in legacy table and will only be used in G
  159. * (2.4 GHz) band.
  160. */
  161. static s32 expected_tpt_legacy[IWL_RATE_COUNT] = {
  162. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
  163. };
  164. static s32 expected_tpt_siso20MHz[4][IWL_RATE_COUNT] = {
  165. {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202}, /* Norm */
  166. {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210}, /* SGI */
  167. {0, 0, 0, 0, 47, 0, 91, 133, 171, 242, 305, 334, 362}, /* AGG */
  168. {0, 0, 0, 0, 52, 0, 101, 145, 187, 264, 330, 361, 390}, /* AGG+SGI */
  169. };
  170. static s32 expected_tpt_siso40MHz[4][IWL_RATE_COUNT] = {
  171. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
  172. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
  173. {0, 0, 0, 0, 94, 0, 177, 249, 313, 423, 512, 550, 586}, /* AGG */
  174. {0, 0, 0, 0, 104, 0, 193, 270, 338, 454, 545, 584, 620}, /* AGG+SGI */
  175. };
  176. static s32 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
  177. {0, 0, 0, 0, 74, 0, 123, 155, 179, 214, 236, 244, 251}, /* Norm */
  178. {0, 0, 0, 0, 81, 0, 131, 164, 188, 223, 243, 251, 257}, /* SGI */
  179. {0, 0, 0, 0, 89, 0, 167, 235, 296, 402, 488, 526, 560}, /* AGG */
  180. {0, 0, 0, 0, 97, 0, 182, 255, 320, 431, 520, 558, 593}, /* AGG+SGI*/
  181. };
  182. static s32 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
  183. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
  184. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
  185. {0, 0, 0, 0, 171, 0, 305, 410, 496, 634, 731, 771, 805}, /* AGG */
  186. {0, 0, 0, 0, 186, 0, 329, 439, 527, 667, 764, 803, 838}, /* AGG+SGI */
  187. };
  188. static s32 expected_tpt_mimo3_20MHz[4][IWL_RATE_COUNT] = {
  189. {0, 0, 0, 0, 99, 0, 153, 186, 208, 239, 256, 263, 268}, /* Norm */
  190. {0, 0, 0, 0, 106, 0, 162, 194, 215, 246, 262, 268, 273}, /* SGI */
  191. {0, 0, 0, 0, 134, 0, 249, 346, 431, 574, 685, 732, 775}, /* AGG */
  192. {0, 0, 0, 0, 148, 0, 272, 376, 465, 614, 727, 775, 818}, /* AGG+SGI */
  193. };
  194. static s32 expected_tpt_mimo3_40MHz[4][IWL_RATE_COUNT] = {
  195. {0, 0, 0, 0, 152, 0, 211, 239, 255, 279, 290, 294, 297}, /* Norm */
  196. {0, 0, 0, 0, 160, 0, 219, 245, 261, 284, 294, 297, 300}, /* SGI */
  197. {0, 0, 0, 0, 254, 0, 443, 584, 695, 868, 984, 1030, 1070}, /* AGG */
  198. {0, 0, 0, 0, 277, 0, 478, 624, 737, 911, 1026, 1070, 1109}, /* AGG+SGI */
  199. };
  200. /* mbps, mcs */
  201. static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
  202. { "1", "BPSK DSSS"},
  203. { "2", "QPSK DSSS"},
  204. {"5.5", "BPSK CCK"},
  205. { "11", "QPSK CCK"},
  206. { "6", "BPSK 1/2"},
  207. { "9", "BPSK 1/2"},
  208. { "12", "QPSK 1/2"},
  209. { "18", "QPSK 3/4"},
  210. { "24", "16QAM 1/2"},
  211. { "36", "16QAM 3/4"},
  212. { "48", "64QAM 2/3"},
  213. { "54", "64QAM 3/4"},
  214. { "60", "64QAM 5/6"},
  215. };
  216. #define MCS_INDEX_PER_STREAM (8)
  217. static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  218. {
  219. window->data = 0;
  220. window->success_counter = 0;
  221. window->success_ratio = IWL_INVALID_VALUE;
  222. window->counter = 0;
  223. window->average_tpt = IWL_INVALID_VALUE;
  224. window->stamp = 0;
  225. }
  226. static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  227. {
  228. return (ant_type & valid_antenna) == ant_type;
  229. }
  230. /*
  231. * removes the old data from the statistics. All data that is older than
  232. * TID_MAX_TIME_DIFF, will be deleted.
  233. */
  234. static void rs_tl_rm_old_stats(struct iwl_traffic_load *tl, u32 curr_time)
  235. {
  236. /* The oldest age we want to keep */
  237. u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
  238. while (tl->queue_count &&
  239. (tl->time_stamp < oldest_time)) {
  240. tl->total -= tl->packet_count[tl->head];
  241. tl->packet_count[tl->head] = 0;
  242. tl->time_stamp += TID_QUEUE_CELL_SPACING;
  243. tl->queue_count--;
  244. tl->head++;
  245. if (tl->head >= TID_QUEUE_MAX_SIZE)
  246. tl->head = 0;
  247. }
  248. }
  249. /*
  250. * increment traffic load value for tid and also remove
  251. * any old values if passed the certain time period
  252. */
  253. static u8 rs_tl_add_packet(struct iwl_lq_sta *lq_data,
  254. struct ieee80211_hdr *hdr)
  255. {
  256. u32 curr_time = jiffies_to_msecs(jiffies);
  257. u32 time_diff;
  258. s32 index;
  259. struct iwl_traffic_load *tl = NULL;
  260. u8 tid;
  261. if (ieee80211_is_data_qos(hdr->frame_control)) {
  262. u8 *qc = ieee80211_get_qos_ctl(hdr);
  263. tid = qc[0] & 0xf;
  264. } else {
  265. return IWL_MAX_TID_COUNT;
  266. }
  267. if (unlikely(tid >= IWL_MAX_TID_COUNT))
  268. return IWL_MAX_TID_COUNT;
  269. tl = &lq_data->load[tid];
  270. curr_time -= curr_time % TID_ROUND_VALUE;
  271. /* Happens only for the first packet. Initialize the data */
  272. if (!(tl->queue_count)) {
  273. tl->total = 1;
  274. tl->time_stamp = curr_time;
  275. tl->queue_count = 1;
  276. tl->head = 0;
  277. tl->packet_count[0] = 1;
  278. return IWL_MAX_TID_COUNT;
  279. }
  280. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  281. index = time_diff / TID_QUEUE_CELL_SPACING;
  282. /* The history is too long: remove data that is older than */
  283. /* TID_MAX_TIME_DIFF */
  284. if (index >= TID_QUEUE_MAX_SIZE)
  285. rs_tl_rm_old_stats(tl, curr_time);
  286. index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
  287. tl->packet_count[index] = tl->packet_count[index] + 1;
  288. tl->total = tl->total + 1;
  289. if ((index + 1) > tl->queue_count)
  290. tl->queue_count = index + 1;
  291. return tid;
  292. }
  293. #ifdef CONFIG_MAC80211_DEBUGFS
  294. /**
  295. * Program the device to use fixed rate for frame transmit
  296. * This is for debugging/testing only
  297. * once the device start use fixed rate, we need to reload the module
  298. * to being back the normal operation.
  299. */
  300. static void rs_program_fix_rate(struct iwl_mvm *mvm,
  301. struct iwl_lq_sta *lq_sta)
  302. {
  303. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  304. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  305. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  306. lq_sta->active_mimo3_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  307. IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
  308. lq_sta->lq.sta_id, lq_sta->dbg_fixed_rate);
  309. if (lq_sta->dbg_fixed_rate) {
  310. rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
  311. iwl_mvm_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
  312. }
  313. }
  314. #endif
  315. /*
  316. get the traffic load value for tid
  317. */
  318. static u32 rs_tl_get_load(struct iwl_lq_sta *lq_data, u8 tid)
  319. {
  320. u32 curr_time = jiffies_to_msecs(jiffies);
  321. u32 time_diff;
  322. s32 index;
  323. struct iwl_traffic_load *tl = NULL;
  324. if (tid >= IWL_MAX_TID_COUNT)
  325. return 0;
  326. tl = &(lq_data->load[tid]);
  327. curr_time -= curr_time % TID_ROUND_VALUE;
  328. if (!(tl->queue_count))
  329. return 0;
  330. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  331. index = time_diff / TID_QUEUE_CELL_SPACING;
  332. /* The history is too long: remove data that is older than */
  333. /* TID_MAX_TIME_DIFF */
  334. if (index >= TID_QUEUE_MAX_SIZE)
  335. rs_tl_rm_old_stats(tl, curr_time);
  336. return tl->total;
  337. }
  338. static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
  339. struct iwl_lq_sta *lq_data, u8 tid,
  340. struct ieee80211_sta *sta)
  341. {
  342. int ret = -EAGAIN;
  343. u32 load;
  344. load = rs_tl_get_load(lq_data, tid);
  345. /*
  346. * Don't create TX aggregation sessions when in high
  347. * BT traffic, as they would just be disrupted by BT.
  348. */
  349. if (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2) {
  350. IWL_DEBUG_COEX(mvm, "BT traffic (%d), no aggregation allowed\n",
  351. BT_MBOX_MSG(&mvm->last_bt_notif,
  352. 3, TRAFFIC_LOAD));
  353. return ret;
  354. }
  355. if ((iwlwifi_mod_params.auto_agg) || (load > IWL_AGG_LOAD_THRESHOLD)) {
  356. IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
  357. sta->addr, tid);
  358. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  359. if (ret == -EAGAIN) {
  360. /*
  361. * driver and mac80211 is out of sync
  362. * this might be cause by reloading firmware
  363. * stop the tx ba session here
  364. */
  365. IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
  366. tid);
  367. ieee80211_stop_tx_ba_session(sta, tid);
  368. }
  369. } else {
  370. IWL_DEBUG_HT(mvm,
  371. "Aggregation not enabled for tid %d because load = %u\n",
  372. tid, load);
  373. }
  374. return ret;
  375. }
  376. static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
  377. struct iwl_lq_sta *lq_data,
  378. struct ieee80211_sta *sta)
  379. {
  380. if (tid < IWL_MAX_TID_COUNT)
  381. rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
  382. else
  383. IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
  384. tid, IWL_MAX_TID_COUNT);
  385. }
  386. static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
  387. {
  388. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  389. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  390. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  391. }
  392. /*
  393. * Static function to get the expected throughput from an iwl_scale_tbl_info
  394. * that wraps a NULL pointer check
  395. */
  396. static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
  397. {
  398. if (tbl->expected_tpt)
  399. return tbl->expected_tpt[rs_index];
  400. return 0;
  401. }
  402. /**
  403. * rs_collect_tx_data - Update the success/failure sliding window
  404. *
  405. * We keep a sliding window of the last 62 packets transmitted
  406. * at this rate. window->data contains the bitmask of successful
  407. * packets.
  408. */
  409. static int rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
  410. int scale_index, int attempts, int successes)
  411. {
  412. struct iwl_rate_scale_data *window = NULL;
  413. static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
  414. s32 fail_count, tpt;
  415. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  416. return -EINVAL;
  417. /* Select window for current tx bit rate */
  418. window = &(tbl->win[scale_index]);
  419. /* Get expected throughput */
  420. tpt = get_expected_tpt(tbl, scale_index);
  421. /*
  422. * Keep track of only the latest 62 tx frame attempts in this rate's
  423. * history window; anything older isn't really relevant any more.
  424. * If we have filled up the sliding window, drop the oldest attempt;
  425. * if the oldest attempt (highest bit in bitmap) shows "success",
  426. * subtract "1" from the success counter (this is the main reason
  427. * we keep these bitmaps!).
  428. */
  429. while (attempts > 0) {
  430. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  431. /* remove earliest */
  432. window->counter = IWL_RATE_MAX_WINDOW - 1;
  433. if (window->data & mask) {
  434. window->data &= ~mask;
  435. window->success_counter--;
  436. }
  437. }
  438. /* Increment frames-attempted counter */
  439. window->counter++;
  440. /* Shift bitmap by one frame to throw away oldest history */
  441. window->data <<= 1;
  442. /* Mark the most recent #successes attempts as successful */
  443. if (successes > 0) {
  444. window->success_counter++;
  445. window->data |= 0x1;
  446. successes--;
  447. }
  448. attempts--;
  449. }
  450. /* Calculate current success ratio, avoid divide-by-0! */
  451. if (window->counter > 0)
  452. window->success_ratio = 128 * (100 * window->success_counter)
  453. / window->counter;
  454. else
  455. window->success_ratio = IWL_INVALID_VALUE;
  456. fail_count = window->counter - window->success_counter;
  457. /* Calculate average throughput, if we have enough history. */
  458. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  459. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  460. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  461. else
  462. window->average_tpt = IWL_INVALID_VALUE;
  463. /* Tag this window as having been updated */
  464. window->stamp = jiffies;
  465. return 0;
  466. }
  467. /*
  468. * Fill uCode API rate_n_flags field, based on "search" or "active" table.
  469. */
  470. /* FIXME:RS:remove this function and put the flags statically in the table */
  471. static u32 rate_n_flags_from_tbl(struct iwl_mvm *mvm,
  472. struct iwl_scale_tbl_info *tbl,
  473. int index, u8 use_green)
  474. {
  475. u32 rate_n_flags = 0;
  476. if (is_legacy(tbl->lq_type)) {
  477. rate_n_flags = iwl_rates[index].plcp;
  478. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  479. rate_n_flags |= RATE_MCS_CCK_MSK;
  480. } else if (is_Ht(tbl->lq_type)) {
  481. if (index > IWL_LAST_OFDM_RATE) {
  482. IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
  483. index = IWL_LAST_OFDM_RATE;
  484. }
  485. rate_n_flags = RATE_MCS_HT_MSK;
  486. if (is_siso(tbl->lq_type))
  487. rate_n_flags |= iwl_rates[index].plcp_siso;
  488. else if (is_mimo2(tbl->lq_type))
  489. rate_n_flags |= iwl_rates[index].plcp_mimo2;
  490. else
  491. rate_n_flags |= iwl_rates[index].plcp_mimo3;
  492. } else {
  493. IWL_ERR(mvm, "Invalid tbl->lq_type %d\n", tbl->lq_type);
  494. }
  495. rate_n_flags |= ((tbl->ant_type << RATE_MCS_ANT_POS) &
  496. RATE_MCS_ANT_ABC_MSK);
  497. if (is_Ht(tbl->lq_type)) {
  498. if (tbl->is_ht40)
  499. rate_n_flags |= RATE_MCS_CHAN_WIDTH_40;
  500. if (tbl->is_SGI)
  501. rate_n_flags |= RATE_MCS_SGI_MSK;
  502. if (use_green) {
  503. rate_n_flags |= RATE_HT_MCS_GF_MSK;
  504. if (is_siso(tbl->lq_type) && tbl->is_SGI) {
  505. rate_n_flags &= ~RATE_MCS_SGI_MSK;
  506. IWL_ERR(mvm, "GF was set with SGI:SISO\n");
  507. }
  508. }
  509. }
  510. return rate_n_flags;
  511. }
  512. /*
  513. * Interpret uCode API's rate_n_flags format,
  514. * fill "search" or "active" tx mode table.
  515. */
  516. static int rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
  517. enum ieee80211_band band,
  518. struct iwl_scale_tbl_info *tbl,
  519. int *rate_idx)
  520. {
  521. u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
  522. u8 num_of_ant = get_num_of_ant_from_rate(rate_n_flags);
  523. u8 mcs;
  524. memset(tbl, 0, sizeof(struct iwl_scale_tbl_info));
  525. *rate_idx = iwl_hwrate_to_plcp_idx(rate_n_flags);
  526. if (*rate_idx == IWL_RATE_INVALID) {
  527. *rate_idx = -1;
  528. return -EINVAL;
  529. }
  530. tbl->is_SGI = 0; /* default legacy setup */
  531. tbl->is_ht40 = 0;
  532. tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
  533. tbl->lq_type = LQ_NONE;
  534. tbl->max_search = IWL_MAX_SEARCH;
  535. /* legacy rate format */
  536. if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
  537. if (num_of_ant == 1) {
  538. if (band == IEEE80211_BAND_5GHZ)
  539. tbl->lq_type = LQ_A;
  540. else
  541. tbl->lq_type = LQ_G;
  542. }
  543. /* HT rate format */
  544. } else {
  545. if (rate_n_flags & RATE_MCS_SGI_MSK)
  546. tbl->is_SGI = 1;
  547. if (rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
  548. tbl->is_ht40 = 1;
  549. mcs = rs_extract_rate(rate_n_flags);
  550. /* SISO */
  551. if (mcs <= IWL_RATE_SISO_60M_PLCP) {
  552. if (num_of_ant == 1)
  553. tbl->lq_type = LQ_SISO; /*else NONE*/
  554. /* MIMO2 */
  555. } else if (mcs <= IWL_RATE_MIMO2_60M_PLCP) {
  556. if (num_of_ant == 2)
  557. tbl->lq_type = LQ_MIMO2;
  558. /* MIMO3 */
  559. } else {
  560. if (num_of_ant == 3) {
  561. tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
  562. tbl->lq_type = LQ_MIMO3;
  563. }
  564. }
  565. }
  566. return 0;
  567. }
  568. /* switch to another antenna/antennas and return 1 */
  569. /* if no other valid antenna found, return 0 */
  570. static int rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
  571. struct iwl_scale_tbl_info *tbl)
  572. {
  573. u8 new_ant_type;
  574. if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
  575. return 0;
  576. if (!rs_is_valid_ant(valid_ant, tbl->ant_type))
  577. return 0;
  578. new_ant_type = ant_toggle_lookup[tbl->ant_type];
  579. while ((new_ant_type != tbl->ant_type) &&
  580. !rs_is_valid_ant(valid_ant, new_ant_type))
  581. new_ant_type = ant_toggle_lookup[new_ant_type];
  582. if (new_ant_type == tbl->ant_type)
  583. return 0;
  584. tbl->ant_type = new_ant_type;
  585. *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
  586. *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
  587. return 1;
  588. }
  589. /**
  590. * Green-field mode is valid if the station supports it and
  591. * there are no non-GF stations present in the BSS.
  592. */
  593. static bool rs_use_green(struct ieee80211_sta *sta)
  594. {
  595. /*
  596. * There's a bug somewhere in this code that causes the
  597. * scaling to get stuck because GF+SGI can't be combined
  598. * in SISO rates. Until we find that bug, disable GF, it
  599. * has only limited benefit and we still interoperate with
  600. * GF APs since we can always receive GF transmissions.
  601. */
  602. return false;
  603. }
  604. /**
  605. * rs_get_supported_rates - get the available rates
  606. *
  607. * if management frame or broadcast frame only return
  608. * basic available rates.
  609. *
  610. */
  611. static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
  612. struct ieee80211_hdr *hdr,
  613. enum iwl_table_type rate_type)
  614. {
  615. if (is_legacy(rate_type)) {
  616. return lq_sta->active_legacy_rate;
  617. } else {
  618. if (is_siso(rate_type))
  619. return lq_sta->active_siso_rate;
  620. else if (is_mimo2(rate_type))
  621. return lq_sta->active_mimo2_rate;
  622. else
  623. return lq_sta->active_mimo3_rate;
  624. }
  625. }
  626. static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
  627. int rate_type)
  628. {
  629. u8 high = IWL_RATE_INVALID;
  630. u8 low = IWL_RATE_INVALID;
  631. /* 802.11A or ht walks to the next literal adjacent rate in
  632. * the rate table */
  633. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  634. int i;
  635. u32 mask;
  636. /* Find the previous rate that is in the rate mask */
  637. i = index - 1;
  638. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  639. if (rate_mask & mask) {
  640. low = i;
  641. break;
  642. }
  643. }
  644. /* Find the next rate that is in the rate mask */
  645. i = index + 1;
  646. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  647. if (rate_mask & mask) {
  648. high = i;
  649. break;
  650. }
  651. }
  652. return (high << 8) | low;
  653. }
  654. low = index;
  655. while (low != IWL_RATE_INVALID) {
  656. low = iwl_rates[low].prev_rs;
  657. if (low == IWL_RATE_INVALID)
  658. break;
  659. if (rate_mask & (1 << low))
  660. break;
  661. IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
  662. }
  663. high = index;
  664. while (high != IWL_RATE_INVALID) {
  665. high = iwl_rates[high].next_rs;
  666. if (high == IWL_RATE_INVALID)
  667. break;
  668. if (rate_mask & (1 << high))
  669. break;
  670. IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
  671. }
  672. return (high << 8) | low;
  673. }
  674. static u32 rs_get_lower_rate(struct iwl_lq_sta *lq_sta,
  675. struct iwl_scale_tbl_info *tbl,
  676. u8 scale_index, u8 ht_possible)
  677. {
  678. s32 low;
  679. u16 rate_mask;
  680. u16 high_low;
  681. u8 switch_to_legacy = 0;
  682. u8 is_green = lq_sta->is_green;
  683. struct iwl_mvm *mvm = lq_sta->drv;
  684. /* check if we need to switch from HT to legacy rates.
  685. * assumption is that mandatory rates (1Mbps or 6Mbps)
  686. * are always supported (spec demand) */
  687. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
  688. switch_to_legacy = 1;
  689. scale_index = rs_ht_to_legacy[scale_index];
  690. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  691. tbl->lq_type = LQ_A;
  692. else
  693. tbl->lq_type = LQ_G;
  694. if (num_of_ant(tbl->ant_type) > 1)
  695. tbl->ant_type =
  696. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  697. tbl->is_ht40 = 0;
  698. tbl->is_SGI = 0;
  699. tbl->max_search = IWL_MAX_SEARCH;
  700. }
  701. rate_mask = rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
  702. /* Mask with station rate restriction */
  703. if (is_legacy(tbl->lq_type)) {
  704. /* supp_rates has no CCK bits in A mode */
  705. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  706. rate_mask = (u16)(rate_mask &
  707. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  708. else
  709. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  710. }
  711. /* If we switched from HT to legacy, check current rate */
  712. if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
  713. low = scale_index;
  714. goto out;
  715. }
  716. high_low = rs_get_adjacent_rate(lq_sta->drv, scale_index, rate_mask,
  717. tbl->lq_type);
  718. low = high_low & 0xff;
  719. if (low == IWL_RATE_INVALID)
  720. low = scale_index;
  721. out:
  722. return rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
  723. }
  724. /*
  725. * Simple function to compare two rate scale table types
  726. */
  727. static bool table_type_matches(struct iwl_scale_tbl_info *a,
  728. struct iwl_scale_tbl_info *b)
  729. {
  730. return (a->lq_type == b->lq_type) && (a->ant_type == b->ant_type) &&
  731. (a->is_SGI == b->is_SGI);
  732. }
  733. /*
  734. * mac80211 sends us Tx status
  735. */
  736. static void rs_tx_status(void *mvm_r, struct ieee80211_supported_band *sband,
  737. struct ieee80211_sta *sta, void *priv_sta,
  738. struct sk_buff *skb)
  739. {
  740. int legacy_success;
  741. int retries;
  742. int rs_index, mac_index, i;
  743. struct iwl_lq_sta *lq_sta = priv_sta;
  744. struct iwl_lq_cmd *table;
  745. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  746. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
  747. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  748. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  749. enum mac80211_rate_control_flags mac_flags;
  750. u32 tx_rate;
  751. struct iwl_scale_tbl_info tbl_type;
  752. struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  753. IWL_DEBUG_RATE_LIMIT(mvm,
  754. "get frame ack response, update rate scale window\n");
  755. /* Treat uninitialized rate scaling data same as non-existing. */
  756. if (!lq_sta) {
  757. IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
  758. return;
  759. } else if (!lq_sta->drv) {
  760. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  761. return;
  762. }
  763. if (!ieee80211_is_data(hdr->frame_control) ||
  764. info->flags & IEEE80211_TX_CTL_NO_ACK)
  765. return;
  766. /* This packet was aggregated but doesn't carry status info */
  767. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  768. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  769. return;
  770. /*
  771. * Ignore this Tx frame response if its initial rate doesn't match
  772. * that of latest Link Quality command. There may be stragglers
  773. * from a previous Link Quality command, but we're no longer interested
  774. * in those; they're either from the "active" mode while we're trying
  775. * to check "search" mode, or a prior "search" mode after we've moved
  776. * to a new "search" mode (which might become the new "active" mode).
  777. */
  778. table = &lq_sta->lq;
  779. tx_rate = le32_to_cpu(table->rs_table[0]);
  780. rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type, &rs_index);
  781. if (info->band == IEEE80211_BAND_5GHZ)
  782. rs_index -= IWL_FIRST_OFDM_RATE;
  783. mac_flags = info->status.rates[0].flags;
  784. mac_index = info->status.rates[0].idx;
  785. /* For HT packets, map MCS to PLCP */
  786. if (mac_flags & IEEE80211_TX_RC_MCS) {
  787. /* Remove # of streams */
  788. mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
  789. if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
  790. mac_index++;
  791. /*
  792. * mac80211 HT index is always zero-indexed; we need to move
  793. * HT OFDM rates after CCK rates in 2.4 GHz band
  794. */
  795. if (info->band == IEEE80211_BAND_2GHZ)
  796. mac_index += IWL_FIRST_OFDM_RATE;
  797. }
  798. /* Here we actually compare this rate to the latest LQ command */
  799. if ((mac_index < 0) ||
  800. (tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
  801. (tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH)) ||
  802. (tbl_type.ant_type != info->status.antenna) ||
  803. (!!(tx_rate & RATE_MCS_HT_MSK) !=
  804. !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
  805. (!!(tx_rate & RATE_HT_MCS_GF_MSK) !=
  806. !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
  807. (rs_index != mac_index)) {
  808. IWL_DEBUG_RATE(mvm,
  809. "initial rate %d does not match %d (0x%x)\n",
  810. mac_index, rs_index, tx_rate);
  811. /*
  812. * Since rates mis-match, the last LQ command may have failed.
  813. * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
  814. * ... driver.
  815. */
  816. lq_sta->missed_rate_counter++;
  817. if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
  818. lq_sta->missed_rate_counter = 0;
  819. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
  820. }
  821. /* Regardless, ignore this status info for outdated rate */
  822. return;
  823. } else
  824. /* Rate did match, so reset the missed_rate_counter */
  825. lq_sta->missed_rate_counter = 0;
  826. /* Figure out if rate scale algorithm is in active or search table */
  827. if (table_type_matches(&tbl_type,
  828. &(lq_sta->lq_info[lq_sta->active_tbl]))) {
  829. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  830. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  831. } else if (table_type_matches(
  832. &tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
  833. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  834. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  835. } else {
  836. IWL_DEBUG_RATE(mvm,
  837. "Neither active nor search matches tx rate\n");
  838. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  839. IWL_DEBUG_RATE(mvm, "active- lq:%x, ant:%x, SGI:%d\n",
  840. tmp_tbl->lq_type, tmp_tbl->ant_type,
  841. tmp_tbl->is_SGI);
  842. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  843. IWL_DEBUG_RATE(mvm, "search- lq:%x, ant:%x, SGI:%d\n",
  844. tmp_tbl->lq_type, tmp_tbl->ant_type,
  845. tmp_tbl->is_SGI);
  846. IWL_DEBUG_RATE(mvm, "actual- lq:%x, ant:%x, SGI:%d\n",
  847. tbl_type.lq_type, tbl_type.ant_type,
  848. tbl_type.is_SGI);
  849. /*
  850. * no matching table found, let's by-pass the data collection
  851. * and continue to perform rate scale to find the rate table
  852. */
  853. rs_stay_in_table(lq_sta, true);
  854. goto done;
  855. }
  856. /*
  857. * Updating the frame history depends on whether packets were
  858. * aggregated.
  859. *
  860. * For aggregation, all packets were transmitted at the same rate, the
  861. * first index into rate scale table.
  862. */
  863. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  864. tx_rate = le32_to_cpu(table->rs_table[0]);
  865. rs_get_tbl_info_from_mcs(tx_rate, info->band, &tbl_type,
  866. &rs_index);
  867. rs_collect_tx_data(curr_tbl, rs_index,
  868. info->status.ampdu_len,
  869. info->status.ampdu_ack_len);
  870. /* Update success/fail counts if not searching for new mode */
  871. if (lq_sta->stay_in_tbl) {
  872. lq_sta->total_success += info->status.ampdu_ack_len;
  873. lq_sta->total_failed += (info->status.ampdu_len -
  874. info->status.ampdu_ack_len);
  875. }
  876. } else {
  877. /*
  878. * For legacy, update frame history with for each Tx retry.
  879. */
  880. retries = info->status.rates[0].count - 1;
  881. /* HW doesn't send more than 15 retries */
  882. retries = min(retries, 15);
  883. /* The last transmission may have been successful */
  884. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  885. /* Collect data for each rate used during failed TX attempts */
  886. for (i = 0; i <= retries; ++i) {
  887. tx_rate = le32_to_cpu(table->rs_table[i]);
  888. rs_get_tbl_info_from_mcs(tx_rate, info->band,
  889. &tbl_type, &rs_index);
  890. /*
  891. * Only collect stats if retried rate is in the same RS
  892. * table as active/search.
  893. */
  894. if (table_type_matches(&tbl_type, curr_tbl))
  895. tmp_tbl = curr_tbl;
  896. else if (table_type_matches(&tbl_type, other_tbl))
  897. tmp_tbl = other_tbl;
  898. else
  899. continue;
  900. rs_collect_tx_data(tmp_tbl, rs_index, 1,
  901. i < retries ? 0 : legacy_success);
  902. }
  903. /* Update success/fail counts if not searching for new mode */
  904. if (lq_sta->stay_in_tbl) {
  905. lq_sta->total_success += legacy_success;
  906. lq_sta->total_failed += retries + (1 - legacy_success);
  907. }
  908. }
  909. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  910. lq_sta->last_rate_n_flags = tx_rate;
  911. done:
  912. /* See if there's a better rate or modulation mode to try. */
  913. if (sta && sta->supp_rates[sband->band])
  914. rs_rate_scale_perform(mvm, skb, sta, lq_sta);
  915. }
  916. /*
  917. * Begin a period of staying with a selected modulation mode.
  918. * Set "stay_in_tbl" flag to prevent any mode switches.
  919. * Set frame tx success limits according to legacy vs. high-throughput,
  920. * and reset overall (spanning all rates) tx success history statistics.
  921. * These control how long we stay using same modulation mode before
  922. * searching for a new mode.
  923. */
  924. static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
  925. struct iwl_lq_sta *lq_sta)
  926. {
  927. IWL_DEBUG_RATE(mvm, "we are staying in the same table\n");
  928. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  929. if (is_legacy) {
  930. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  931. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  932. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  933. } else {
  934. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  935. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  936. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  937. }
  938. lq_sta->table_count = 0;
  939. lq_sta->total_failed = 0;
  940. lq_sta->total_success = 0;
  941. lq_sta->flush_timer = jiffies;
  942. lq_sta->action_counter = 0;
  943. }
  944. /*
  945. * Find correct throughput table for given mode of modulation
  946. */
  947. static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  948. struct iwl_scale_tbl_info *tbl)
  949. {
  950. /* Used to choose among HT tables */
  951. s32 (*ht_tbl_pointer)[IWL_RATE_COUNT];
  952. /* Check for invalid LQ type */
  953. if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
  954. tbl->expected_tpt = expected_tpt_legacy;
  955. return;
  956. }
  957. /* Legacy rates have only one table */
  958. if (is_legacy(tbl->lq_type)) {
  959. tbl->expected_tpt = expected_tpt_legacy;
  960. return;
  961. }
  962. /* Choose among many HT tables depending on number of streams
  963. * (SISO/MIMO2/MIMO3), channel width (20/40), SGI, and aggregation
  964. * status */
  965. if (is_siso(tbl->lq_type) && !tbl->is_ht40)
  966. ht_tbl_pointer = expected_tpt_siso20MHz;
  967. else if (is_siso(tbl->lq_type))
  968. ht_tbl_pointer = expected_tpt_siso40MHz;
  969. else if (is_mimo2(tbl->lq_type) && !tbl->is_ht40)
  970. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  971. else if (is_mimo2(tbl->lq_type))
  972. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  973. else if (is_mimo3(tbl->lq_type) && !tbl->is_ht40)
  974. ht_tbl_pointer = expected_tpt_mimo3_20MHz;
  975. else /* if (is_mimo3(tbl->lq_type)) <-- must be true */
  976. ht_tbl_pointer = expected_tpt_mimo3_40MHz;
  977. if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
  978. tbl->expected_tpt = ht_tbl_pointer[0];
  979. else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
  980. tbl->expected_tpt = ht_tbl_pointer[1];
  981. else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
  982. tbl->expected_tpt = ht_tbl_pointer[2];
  983. else /* AGG+SGI */
  984. tbl->expected_tpt = ht_tbl_pointer[3];
  985. }
  986. /*
  987. * Find starting rate for new "search" high-throughput mode of modulation.
  988. * Goal is to find lowest expected rate (under perfect conditions) that is
  989. * above the current measured throughput of "active" mode, to give new mode
  990. * a fair chance to prove itself without too many challenges.
  991. *
  992. * This gets called when transitioning to more aggressive modulation
  993. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  994. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  995. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  996. * bit rate will typically need to increase, but not if performance was bad.
  997. */
  998. static s32 rs_get_best_rate(struct iwl_mvm *mvm,
  999. struct iwl_lq_sta *lq_sta,
  1000. struct iwl_scale_tbl_info *tbl, /* "search" */
  1001. u16 rate_mask, s8 index)
  1002. {
  1003. /* "active" values */
  1004. struct iwl_scale_tbl_info *active_tbl =
  1005. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1006. s32 active_sr = active_tbl->win[index].success_ratio;
  1007. s32 active_tpt = active_tbl->expected_tpt[index];
  1008. /* expected "search" throughput */
  1009. s32 *tpt_tbl = tbl->expected_tpt;
  1010. s32 new_rate, high, low, start_hi;
  1011. u16 high_low;
  1012. s8 rate = index;
  1013. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  1014. while (1) {
  1015. high_low = rs_get_adjacent_rate(mvm, rate, rate_mask,
  1016. tbl->lq_type);
  1017. low = high_low & 0xff;
  1018. high = (high_low >> 8) & 0xff;
  1019. /*
  1020. * Lower the "search" bit rate, to give new "search" mode
  1021. * approximately the same throughput as "active" if:
  1022. *
  1023. * 1) "Active" mode has been working modestly well (but not
  1024. * great), and expected "search" throughput (under perfect
  1025. * conditions) at candidate rate is above the actual
  1026. * measured "active" throughput (but less than expected
  1027. * "active" throughput under perfect conditions).
  1028. * OR
  1029. * 2) "Active" mode has been working perfectly or very well
  1030. * and expected "search" throughput (under perfect
  1031. * conditions) at candidate rate is above expected
  1032. * "active" throughput (under perfect conditions).
  1033. */
  1034. if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
  1035. ((active_sr > IWL_RATE_DECREASE_TH) &&
  1036. (active_sr <= IWL_RATE_HIGH_TH) &&
  1037. (tpt_tbl[rate] <= active_tpt))) ||
  1038. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  1039. (tpt_tbl[rate] > active_tpt))) {
  1040. /* (2nd or later pass)
  1041. * If we've already tried to raise the rate, and are
  1042. * now trying to lower it, use the higher rate. */
  1043. if (start_hi != IWL_RATE_INVALID) {
  1044. new_rate = start_hi;
  1045. break;
  1046. }
  1047. new_rate = rate;
  1048. /* Loop again with lower rate */
  1049. if (low != IWL_RATE_INVALID)
  1050. rate = low;
  1051. /* Lower rate not available, use the original */
  1052. else
  1053. break;
  1054. /* Else try to raise the "search" rate to match "active" */
  1055. } else {
  1056. /* (2nd or later pass)
  1057. * If we've already tried to lower the rate, and are
  1058. * now trying to raise it, use the lower rate. */
  1059. if (new_rate != IWL_RATE_INVALID)
  1060. break;
  1061. /* Loop again with higher rate */
  1062. else if (high != IWL_RATE_INVALID) {
  1063. start_hi = high;
  1064. rate = high;
  1065. /* Higher rate not available, use the original */
  1066. } else {
  1067. new_rate = rate;
  1068. break;
  1069. }
  1070. }
  1071. }
  1072. return new_rate;
  1073. }
  1074. static bool iwl_is_ht40_tx_allowed(struct ieee80211_sta *sta)
  1075. {
  1076. return sta->bandwidth >= IEEE80211_STA_RX_BW_40;
  1077. }
  1078. /*
  1079. * Set up search table for MIMO2
  1080. */
  1081. static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
  1082. struct iwl_lq_sta *lq_sta,
  1083. struct ieee80211_sta *sta,
  1084. struct iwl_scale_tbl_info *tbl, int index)
  1085. {
  1086. u16 rate_mask;
  1087. s32 rate;
  1088. s8 is_green = lq_sta->is_green;
  1089. if (!sta->ht_cap.ht_supported)
  1090. return -1;
  1091. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  1092. return -1;
  1093. /* Need both Tx chains/antennas to support MIMO */
  1094. if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
  1095. return -1;
  1096. IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
  1097. tbl->lq_type = LQ_MIMO2;
  1098. tbl->action = 0;
  1099. tbl->max_search = IWL_MAX_SEARCH;
  1100. rate_mask = lq_sta->active_mimo2_rate;
  1101. if (iwl_is_ht40_tx_allowed(sta))
  1102. tbl->is_ht40 = 1;
  1103. else
  1104. tbl->is_ht40 = 0;
  1105. rs_set_expected_tpt_table(lq_sta, tbl);
  1106. rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
  1107. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
  1108. rate, rate_mask);
  1109. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1110. IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
  1111. rate, rate_mask);
  1112. return -1;
  1113. }
  1114. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
  1115. IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
  1116. tbl->current_rate, is_green);
  1117. return 0;
  1118. }
  1119. /*
  1120. * Set up search table for MIMO3
  1121. */
  1122. static int rs_switch_to_mimo3(struct iwl_mvm *mvm,
  1123. struct iwl_lq_sta *lq_sta,
  1124. struct ieee80211_sta *sta,
  1125. struct iwl_scale_tbl_info *tbl, int index)
  1126. {
  1127. u16 rate_mask;
  1128. s32 rate;
  1129. s8 is_green = lq_sta->is_green;
  1130. if (!sta->ht_cap.ht_supported)
  1131. return -1;
  1132. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  1133. return -1;
  1134. /* Need both Tx chains/antennas to support MIMO */
  1135. if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 3)
  1136. return -1;
  1137. IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO3\n");
  1138. tbl->lq_type = LQ_MIMO3;
  1139. tbl->action = 0;
  1140. tbl->max_search = IWL_MAX_11N_MIMO3_SEARCH;
  1141. rate_mask = lq_sta->active_mimo3_rate;
  1142. if (iwl_is_ht40_tx_allowed(sta))
  1143. tbl->is_ht40 = 1;
  1144. else
  1145. tbl->is_ht40 = 0;
  1146. rs_set_expected_tpt_table(lq_sta, tbl);
  1147. rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
  1148. IWL_DEBUG_RATE(mvm, "LQ: MIMO3 best rate %d mask %X\n",
  1149. rate, rate_mask);
  1150. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1151. IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
  1152. rate, rate_mask);
  1153. return -1;
  1154. }
  1155. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
  1156. IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
  1157. tbl->current_rate, is_green);
  1158. return 0;
  1159. }
  1160. /*
  1161. * Set up search table for SISO
  1162. */
  1163. static int rs_switch_to_siso(struct iwl_mvm *mvm,
  1164. struct iwl_lq_sta *lq_sta,
  1165. struct ieee80211_sta *sta,
  1166. struct iwl_scale_tbl_info *tbl, int index)
  1167. {
  1168. u16 rate_mask;
  1169. u8 is_green = lq_sta->is_green;
  1170. s32 rate;
  1171. if (!sta->ht_cap.ht_supported)
  1172. return -1;
  1173. IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
  1174. tbl->lq_type = LQ_SISO;
  1175. tbl->action = 0;
  1176. tbl->max_search = IWL_MAX_SEARCH;
  1177. rate_mask = lq_sta->active_siso_rate;
  1178. if (iwl_is_ht40_tx_allowed(sta))
  1179. tbl->is_ht40 = 1;
  1180. else
  1181. tbl->is_ht40 = 0;
  1182. if (is_green)
  1183. tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
  1184. rs_set_expected_tpt_table(lq_sta, tbl);
  1185. rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
  1186. IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
  1187. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1188. IWL_DEBUG_RATE(mvm,
  1189. "can not switch with index %d rate mask %x\n",
  1190. rate, rate_mask);
  1191. return -1;
  1192. }
  1193. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
  1194. IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
  1195. tbl->current_rate, is_green);
  1196. return 0;
  1197. }
  1198. /*
  1199. * Try to switch to new modulation mode from legacy
  1200. */
  1201. static int rs_move_legacy_other(struct iwl_mvm *mvm,
  1202. struct iwl_lq_sta *lq_sta,
  1203. struct ieee80211_sta *sta,
  1204. int index)
  1205. {
  1206. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1207. struct iwl_scale_tbl_info *search_tbl =
  1208. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1209. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1210. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1211. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1212. u8 start_action;
  1213. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1214. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1215. int ret;
  1216. u8 update_search_tbl_counter = 0;
  1217. start_action = tbl->action;
  1218. while (1) {
  1219. lq_sta->action_counter++;
  1220. switch (tbl->action) {
  1221. case IWL_LEGACY_SWITCH_ANTENNA1:
  1222. case IWL_LEGACY_SWITCH_ANTENNA2:
  1223. IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
  1224. if ((tbl->action == IWL_LEGACY_SWITCH_ANTENNA1 &&
  1225. tx_chains_num <= 1) ||
  1226. (tbl->action == IWL_LEGACY_SWITCH_ANTENNA2 &&
  1227. tx_chains_num <= 2))
  1228. break;
  1229. /* Don't change antenna if success has been great */
  1230. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1231. break;
  1232. /* Set up search table to try other antenna */
  1233. memcpy(search_tbl, tbl, sz);
  1234. if (rs_toggle_antenna(valid_tx_ant,
  1235. &search_tbl->current_rate,
  1236. search_tbl)) {
  1237. update_search_tbl_counter = 1;
  1238. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1239. goto out;
  1240. }
  1241. break;
  1242. case IWL_LEGACY_SWITCH_SISO:
  1243. IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
  1244. /* Set up search table to try SISO */
  1245. memcpy(search_tbl, tbl, sz);
  1246. search_tbl->is_SGI = 0;
  1247. ret = rs_switch_to_siso(mvm, lq_sta, sta,
  1248. search_tbl, index);
  1249. if (!ret) {
  1250. lq_sta->action_counter = 0;
  1251. goto out;
  1252. }
  1253. break;
  1254. case IWL_LEGACY_SWITCH_MIMO2_AB:
  1255. case IWL_LEGACY_SWITCH_MIMO2_AC:
  1256. case IWL_LEGACY_SWITCH_MIMO2_BC:
  1257. IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
  1258. /* Set up search table to try MIMO */
  1259. memcpy(search_tbl, tbl, sz);
  1260. search_tbl->is_SGI = 0;
  1261. if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AB)
  1262. search_tbl->ant_type = ANT_AB;
  1263. else if (tbl->action == IWL_LEGACY_SWITCH_MIMO2_AC)
  1264. search_tbl->ant_type = ANT_AC;
  1265. else
  1266. search_tbl->ant_type = ANT_BC;
  1267. if (!rs_is_valid_ant(valid_tx_ant,
  1268. search_tbl->ant_type))
  1269. break;
  1270. ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
  1271. search_tbl, index);
  1272. if (!ret) {
  1273. lq_sta->action_counter = 0;
  1274. goto out;
  1275. }
  1276. break;
  1277. case IWL_LEGACY_SWITCH_MIMO3_ABC:
  1278. IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO3\n");
  1279. /* Set up search table to try MIMO3 */
  1280. memcpy(search_tbl, tbl, sz);
  1281. search_tbl->is_SGI = 0;
  1282. search_tbl->ant_type = ANT_ABC;
  1283. if (!rs_is_valid_ant(valid_tx_ant,
  1284. search_tbl->ant_type))
  1285. break;
  1286. ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
  1287. search_tbl, index);
  1288. if (!ret) {
  1289. lq_sta->action_counter = 0;
  1290. goto out;
  1291. }
  1292. break;
  1293. }
  1294. tbl->action++;
  1295. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1296. tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
  1297. if (tbl->action == start_action)
  1298. break;
  1299. }
  1300. search_tbl->lq_type = LQ_NONE;
  1301. return 0;
  1302. out:
  1303. lq_sta->search_better_tbl = 1;
  1304. tbl->action++;
  1305. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1306. tbl->action = IWL_LEGACY_SWITCH_ANTENNA1;
  1307. if (update_search_tbl_counter)
  1308. search_tbl->action = tbl->action;
  1309. return 0;
  1310. }
  1311. /*
  1312. * Try to switch to new modulation mode from SISO
  1313. */
  1314. static int rs_move_siso_to_other(struct iwl_mvm *mvm,
  1315. struct iwl_lq_sta *lq_sta,
  1316. struct ieee80211_sta *sta, int index)
  1317. {
  1318. u8 is_green = lq_sta->is_green;
  1319. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1320. struct iwl_scale_tbl_info *search_tbl =
  1321. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1322. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1323. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1324. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1325. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1326. u8 start_action;
  1327. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1328. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1329. u8 update_search_tbl_counter = 0;
  1330. int ret;
  1331. switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1332. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1333. /* nothing */
  1334. break;
  1335. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1336. /* avoid antenna B unless MIMO */
  1337. if (tbl->action == IWL_SISO_SWITCH_ANTENNA2)
  1338. tbl->action = IWL_SISO_SWITCH_MIMO2_AB;
  1339. break;
  1340. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1341. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1342. /* avoid antenna B and MIMO */
  1343. valid_tx_ant =
  1344. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  1345. if (tbl->action != IWL_SISO_SWITCH_ANTENNA1)
  1346. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1347. break;
  1348. default:
  1349. IWL_ERR(mvm, "Invalid BT load %d",
  1350. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
  1351. break;
  1352. }
  1353. start_action = tbl->action;
  1354. while (1) {
  1355. lq_sta->action_counter++;
  1356. switch (tbl->action) {
  1357. case IWL_SISO_SWITCH_ANTENNA1:
  1358. case IWL_SISO_SWITCH_ANTENNA2:
  1359. IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
  1360. if ((tbl->action == IWL_SISO_SWITCH_ANTENNA1 &&
  1361. tx_chains_num <= 1) ||
  1362. (tbl->action == IWL_SISO_SWITCH_ANTENNA2 &&
  1363. tx_chains_num <= 2))
  1364. break;
  1365. if (window->success_ratio >= IWL_RS_GOOD_RATIO &&
  1366. BT_MBOX_MSG(&mvm->last_bt_notif, 3,
  1367. TRAFFIC_LOAD) == 0)
  1368. break;
  1369. memcpy(search_tbl, tbl, sz);
  1370. if (rs_toggle_antenna(valid_tx_ant,
  1371. &search_tbl->current_rate,
  1372. search_tbl)) {
  1373. update_search_tbl_counter = 1;
  1374. goto out;
  1375. }
  1376. break;
  1377. case IWL_SISO_SWITCH_MIMO2_AB:
  1378. case IWL_SISO_SWITCH_MIMO2_AC:
  1379. case IWL_SISO_SWITCH_MIMO2_BC:
  1380. IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO2\n");
  1381. memcpy(search_tbl, tbl, sz);
  1382. search_tbl->is_SGI = 0;
  1383. if (tbl->action == IWL_SISO_SWITCH_MIMO2_AB)
  1384. search_tbl->ant_type = ANT_AB;
  1385. else if (tbl->action == IWL_SISO_SWITCH_MIMO2_AC)
  1386. search_tbl->ant_type = ANT_AC;
  1387. else
  1388. search_tbl->ant_type = ANT_BC;
  1389. if (!rs_is_valid_ant(valid_tx_ant,
  1390. search_tbl->ant_type))
  1391. break;
  1392. ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
  1393. search_tbl, index);
  1394. if (!ret)
  1395. goto out;
  1396. break;
  1397. case IWL_SISO_SWITCH_GI:
  1398. if (!tbl->is_ht40 && !(ht_cap->cap &
  1399. IEEE80211_HT_CAP_SGI_20))
  1400. break;
  1401. if (tbl->is_ht40 && !(ht_cap->cap &
  1402. IEEE80211_HT_CAP_SGI_40))
  1403. break;
  1404. IWL_DEBUG_RATE(mvm, "LQ: SISO toggle SGI/NGI\n");
  1405. memcpy(search_tbl, tbl, sz);
  1406. if (is_green) {
  1407. if (!tbl->is_SGI)
  1408. break;
  1409. else
  1410. IWL_ERR(mvm,
  1411. "SGI was set in GF+SISO\n");
  1412. }
  1413. search_tbl->is_SGI = !tbl->is_SGI;
  1414. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1415. if (tbl->is_SGI) {
  1416. s32 tpt = lq_sta->last_tpt / 100;
  1417. if (tpt >= search_tbl->expected_tpt[index])
  1418. break;
  1419. }
  1420. search_tbl->current_rate =
  1421. rate_n_flags_from_tbl(mvm, search_tbl,
  1422. index, is_green);
  1423. update_search_tbl_counter = 1;
  1424. goto out;
  1425. case IWL_SISO_SWITCH_MIMO3_ABC:
  1426. IWL_DEBUG_RATE(mvm, "LQ: SISO switch to MIMO3\n");
  1427. memcpy(search_tbl, tbl, sz);
  1428. search_tbl->is_SGI = 0;
  1429. search_tbl->ant_type = ANT_ABC;
  1430. if (!rs_is_valid_ant(valid_tx_ant,
  1431. search_tbl->ant_type))
  1432. break;
  1433. ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
  1434. search_tbl, index);
  1435. if (!ret)
  1436. goto out;
  1437. break;
  1438. }
  1439. tbl->action++;
  1440. if (tbl->action > IWL_LEGACY_SWITCH_MIMO3_ABC)
  1441. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1442. if (tbl->action == start_action)
  1443. break;
  1444. }
  1445. search_tbl->lq_type = LQ_NONE;
  1446. return 0;
  1447. out:
  1448. lq_sta->search_better_tbl = 1;
  1449. tbl->action++;
  1450. if (tbl->action > IWL_SISO_SWITCH_MIMO3_ABC)
  1451. tbl->action = IWL_SISO_SWITCH_ANTENNA1;
  1452. if (update_search_tbl_counter)
  1453. search_tbl->action = tbl->action;
  1454. return 0;
  1455. }
  1456. /*
  1457. * Try to switch to new modulation mode from MIMO2
  1458. */
  1459. static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
  1460. struct iwl_lq_sta *lq_sta,
  1461. struct ieee80211_sta *sta, int index)
  1462. {
  1463. s8 is_green = lq_sta->is_green;
  1464. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1465. struct iwl_scale_tbl_info *search_tbl =
  1466. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1467. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1468. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1469. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1470. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1471. u8 start_action;
  1472. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1473. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1474. u8 update_search_tbl_counter = 0;
  1475. int ret;
  1476. switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1477. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1478. /* nothing */
  1479. break;
  1480. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1481. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1482. /* avoid antenna B and MIMO */
  1483. if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
  1484. tbl->action = IWL_MIMO2_SWITCH_SISO_A;
  1485. break;
  1486. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1487. /* avoid antenna B unless MIMO */
  1488. if (tbl->action == IWL_MIMO2_SWITCH_SISO_B ||
  1489. tbl->action == IWL_MIMO2_SWITCH_SISO_C)
  1490. tbl->action = IWL_MIMO2_SWITCH_SISO_A;
  1491. break;
  1492. default:
  1493. IWL_ERR(mvm, "Invalid BT load %d",
  1494. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
  1495. break;
  1496. }
  1497. start_action = tbl->action;
  1498. while (1) {
  1499. lq_sta->action_counter++;
  1500. switch (tbl->action) {
  1501. case IWL_MIMO2_SWITCH_ANTENNA1:
  1502. case IWL_MIMO2_SWITCH_ANTENNA2:
  1503. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle Antennas\n");
  1504. if (tx_chains_num <= 2)
  1505. break;
  1506. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1507. break;
  1508. memcpy(search_tbl, tbl, sz);
  1509. if (rs_toggle_antenna(valid_tx_ant,
  1510. &search_tbl->current_rate,
  1511. search_tbl)) {
  1512. update_search_tbl_counter = 1;
  1513. goto out;
  1514. }
  1515. break;
  1516. case IWL_MIMO2_SWITCH_SISO_A:
  1517. case IWL_MIMO2_SWITCH_SISO_B:
  1518. case IWL_MIMO2_SWITCH_SISO_C:
  1519. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
  1520. /* Set up new search table for SISO */
  1521. memcpy(search_tbl, tbl, sz);
  1522. if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
  1523. search_tbl->ant_type = ANT_A;
  1524. else if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
  1525. search_tbl->ant_type = ANT_B;
  1526. else
  1527. search_tbl->ant_type = ANT_C;
  1528. if (!rs_is_valid_ant(valid_tx_ant,
  1529. search_tbl->ant_type))
  1530. break;
  1531. ret = rs_switch_to_siso(mvm, lq_sta, sta,
  1532. search_tbl, index);
  1533. if (!ret)
  1534. goto out;
  1535. break;
  1536. case IWL_MIMO2_SWITCH_GI:
  1537. if (!tbl->is_ht40 && !(ht_cap->cap &
  1538. IEEE80211_HT_CAP_SGI_20))
  1539. break;
  1540. if (tbl->is_ht40 && !(ht_cap->cap &
  1541. IEEE80211_HT_CAP_SGI_40))
  1542. break;
  1543. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
  1544. /* Set up new search table for MIMO2 */
  1545. memcpy(search_tbl, tbl, sz);
  1546. search_tbl->is_SGI = !tbl->is_SGI;
  1547. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1548. /*
  1549. * If active table already uses the fastest possible
  1550. * modulation (dual stream with short guard interval),
  1551. * and it's working well, there's no need to look
  1552. * for a better type of modulation!
  1553. */
  1554. if (tbl->is_SGI) {
  1555. s32 tpt = lq_sta->last_tpt / 100;
  1556. if (tpt >= search_tbl->expected_tpt[index])
  1557. break;
  1558. }
  1559. search_tbl->current_rate =
  1560. rate_n_flags_from_tbl(mvm, search_tbl,
  1561. index, is_green);
  1562. update_search_tbl_counter = 1;
  1563. goto out;
  1564. case IWL_MIMO2_SWITCH_MIMO3_ABC:
  1565. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to MIMO3\n");
  1566. memcpy(search_tbl, tbl, sz);
  1567. search_tbl->is_SGI = 0;
  1568. search_tbl->ant_type = ANT_ABC;
  1569. if (!rs_is_valid_ant(valid_tx_ant,
  1570. search_tbl->ant_type))
  1571. break;
  1572. ret = rs_switch_to_mimo3(mvm, lq_sta, sta,
  1573. search_tbl, index);
  1574. if (!ret)
  1575. goto out;
  1576. break;
  1577. }
  1578. tbl->action++;
  1579. if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
  1580. tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
  1581. if (tbl->action == start_action)
  1582. break;
  1583. }
  1584. search_tbl->lq_type = LQ_NONE;
  1585. return 0;
  1586. out:
  1587. lq_sta->search_better_tbl = 1;
  1588. tbl->action++;
  1589. if (tbl->action > IWL_MIMO2_SWITCH_MIMO3_ABC)
  1590. tbl->action = IWL_MIMO2_SWITCH_ANTENNA1;
  1591. if (update_search_tbl_counter)
  1592. search_tbl->action = tbl->action;
  1593. return 0;
  1594. }
  1595. /*
  1596. * Try to switch to new modulation mode from MIMO3
  1597. */
  1598. static int rs_move_mimo3_to_other(struct iwl_mvm *mvm,
  1599. struct iwl_lq_sta *lq_sta,
  1600. struct ieee80211_sta *sta, int index)
  1601. {
  1602. s8 is_green = lq_sta->is_green;
  1603. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1604. struct iwl_scale_tbl_info *search_tbl =
  1605. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1606. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1607. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1608. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1609. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1610. u8 start_action;
  1611. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1612. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1613. int ret;
  1614. u8 update_search_tbl_counter = 0;
  1615. switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1616. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1617. /* nothing */
  1618. break;
  1619. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1620. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1621. /* avoid antenna B and MIMO */
  1622. if (tbl->action != IWL_MIMO3_SWITCH_SISO_A)
  1623. tbl->action = IWL_MIMO3_SWITCH_SISO_A;
  1624. break;
  1625. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1626. /* avoid antenna B unless MIMO */
  1627. if (tbl->action == IWL_MIMO3_SWITCH_SISO_B ||
  1628. tbl->action == IWL_MIMO3_SWITCH_SISO_C)
  1629. tbl->action = IWL_MIMO3_SWITCH_SISO_A;
  1630. break;
  1631. default:
  1632. IWL_ERR(mvm, "Invalid BT load %d",
  1633. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
  1634. break;
  1635. }
  1636. start_action = tbl->action;
  1637. while (1) {
  1638. lq_sta->action_counter++;
  1639. switch (tbl->action) {
  1640. case IWL_MIMO3_SWITCH_ANTENNA1:
  1641. case IWL_MIMO3_SWITCH_ANTENNA2:
  1642. IWL_DEBUG_RATE(mvm, "LQ: MIMO3 toggle Antennas\n");
  1643. if (tx_chains_num <= 3)
  1644. break;
  1645. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1646. break;
  1647. memcpy(search_tbl, tbl, sz);
  1648. if (rs_toggle_antenna(valid_tx_ant,
  1649. &search_tbl->current_rate,
  1650. search_tbl))
  1651. goto out;
  1652. break;
  1653. case IWL_MIMO3_SWITCH_SISO_A:
  1654. case IWL_MIMO3_SWITCH_SISO_B:
  1655. case IWL_MIMO3_SWITCH_SISO_C:
  1656. IWL_DEBUG_RATE(mvm, "LQ: MIMO3 switch to SISO\n");
  1657. /* Set up new search table for SISO */
  1658. memcpy(search_tbl, tbl, sz);
  1659. if (tbl->action == IWL_MIMO3_SWITCH_SISO_A)
  1660. search_tbl->ant_type = ANT_A;
  1661. else if (tbl->action == IWL_MIMO3_SWITCH_SISO_B)
  1662. search_tbl->ant_type = ANT_B;
  1663. else
  1664. search_tbl->ant_type = ANT_C;
  1665. if (!rs_is_valid_ant(valid_tx_ant,
  1666. search_tbl->ant_type))
  1667. break;
  1668. ret = rs_switch_to_siso(mvm, lq_sta, sta,
  1669. search_tbl, index);
  1670. if (!ret)
  1671. goto out;
  1672. break;
  1673. case IWL_MIMO3_SWITCH_MIMO2_AB:
  1674. case IWL_MIMO3_SWITCH_MIMO2_AC:
  1675. case IWL_MIMO3_SWITCH_MIMO2_BC:
  1676. IWL_DEBUG_RATE(mvm, "LQ: MIMO3 switch to MIMO2\n");
  1677. memcpy(search_tbl, tbl, sz);
  1678. search_tbl->is_SGI = 0;
  1679. if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AB)
  1680. search_tbl->ant_type = ANT_AB;
  1681. else if (tbl->action == IWL_MIMO3_SWITCH_MIMO2_AC)
  1682. search_tbl->ant_type = ANT_AC;
  1683. else
  1684. search_tbl->ant_type = ANT_BC;
  1685. if (!rs_is_valid_ant(valid_tx_ant,
  1686. search_tbl->ant_type))
  1687. break;
  1688. ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
  1689. search_tbl, index);
  1690. if (!ret)
  1691. goto out;
  1692. break;
  1693. case IWL_MIMO3_SWITCH_GI:
  1694. if (!tbl->is_ht40 && !(ht_cap->cap &
  1695. IEEE80211_HT_CAP_SGI_20))
  1696. break;
  1697. if (tbl->is_ht40 && !(ht_cap->cap &
  1698. IEEE80211_HT_CAP_SGI_40))
  1699. break;
  1700. IWL_DEBUG_RATE(mvm, "LQ: MIMO3 toggle SGI/NGI\n");
  1701. /* Set up new search table for MIMO */
  1702. memcpy(search_tbl, tbl, sz);
  1703. search_tbl->is_SGI = !tbl->is_SGI;
  1704. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1705. /*
  1706. * If active table already uses the fastest possible
  1707. * modulation (dual stream with short guard interval),
  1708. * and it's working well, there's no need to look
  1709. * for a better type of modulation!
  1710. */
  1711. if (tbl->is_SGI) {
  1712. s32 tpt = lq_sta->last_tpt / 100;
  1713. if (tpt >= search_tbl->expected_tpt[index])
  1714. break;
  1715. }
  1716. search_tbl->current_rate =
  1717. rate_n_flags_from_tbl(mvm, search_tbl,
  1718. index, is_green);
  1719. update_search_tbl_counter = 1;
  1720. goto out;
  1721. }
  1722. tbl->action++;
  1723. if (tbl->action > IWL_MIMO3_SWITCH_GI)
  1724. tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
  1725. if (tbl->action == start_action)
  1726. break;
  1727. }
  1728. search_tbl->lq_type = LQ_NONE;
  1729. return 0;
  1730. out:
  1731. lq_sta->search_better_tbl = 1;
  1732. tbl->action++;
  1733. if (tbl->action > IWL_MIMO3_SWITCH_GI)
  1734. tbl->action = IWL_MIMO3_SWITCH_ANTENNA1;
  1735. if (update_search_tbl_counter)
  1736. search_tbl->action = tbl->action;
  1737. return 0;
  1738. }
  1739. /*
  1740. * Check whether we should continue using same modulation mode, or
  1741. * begin search for a new mode, based on:
  1742. * 1) # tx successes or failures while using this mode
  1743. * 2) # times calling this function
  1744. * 3) elapsed time in this mode (not used, for now)
  1745. */
  1746. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
  1747. {
  1748. struct iwl_scale_tbl_info *tbl;
  1749. int i;
  1750. int active_tbl;
  1751. int flush_interval_passed = 0;
  1752. struct iwl_mvm *mvm;
  1753. mvm = lq_sta->drv;
  1754. active_tbl = lq_sta->active_tbl;
  1755. tbl = &(lq_sta->lq_info[active_tbl]);
  1756. /* If we've been disallowing search, see if we should now allow it */
  1757. if (lq_sta->stay_in_tbl) {
  1758. /* Elapsed time using current modulation mode */
  1759. if (lq_sta->flush_timer)
  1760. flush_interval_passed =
  1761. time_after(jiffies,
  1762. (unsigned long)(lq_sta->flush_timer +
  1763. IWL_RATE_SCALE_FLUSH_INTVL));
  1764. /*
  1765. * Check if we should allow search for new modulation mode.
  1766. * If many frames have failed or succeeded, or we've used
  1767. * this same modulation for a long time, allow search, and
  1768. * reset history stats that keep track of whether we should
  1769. * allow a new search. Also (below) reset all bitmaps and
  1770. * stats in active history.
  1771. */
  1772. if (force_search ||
  1773. (lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1774. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1775. ((!lq_sta->search_better_tbl) &&
  1776. (lq_sta->flush_timer) && (flush_interval_passed))) {
  1777. IWL_DEBUG_RATE(mvm,
  1778. "LQ: stay is expired %d %d %d\n",
  1779. lq_sta->total_failed,
  1780. lq_sta->total_success,
  1781. flush_interval_passed);
  1782. /* Allow search for new mode */
  1783. lq_sta->stay_in_tbl = 0; /* only place reset */
  1784. lq_sta->total_failed = 0;
  1785. lq_sta->total_success = 0;
  1786. lq_sta->flush_timer = 0;
  1787. /*
  1788. * Else if we've used this modulation mode enough repetitions
  1789. * (regardless of elapsed time or success/failure), reset
  1790. * history bitmaps and rate-specific stats for all rates in
  1791. * active table.
  1792. */
  1793. } else {
  1794. lq_sta->table_count++;
  1795. if (lq_sta->table_count >=
  1796. lq_sta->table_count_limit) {
  1797. lq_sta->table_count = 0;
  1798. IWL_DEBUG_RATE(mvm,
  1799. "LQ: stay in table clear win\n");
  1800. for (i = 0; i < IWL_RATE_COUNT; i++)
  1801. rs_rate_scale_clear_window(
  1802. &(tbl->win[i]));
  1803. }
  1804. }
  1805. /* If transitioning to allow "search", reset all history
  1806. * bitmaps and stats in active table (this will become the new
  1807. * "search" table). */
  1808. if (!lq_sta->stay_in_tbl) {
  1809. for (i = 0; i < IWL_RATE_COUNT; i++)
  1810. rs_rate_scale_clear_window(&(tbl->win[i]));
  1811. }
  1812. }
  1813. }
  1814. /*
  1815. * setup rate table in uCode
  1816. */
  1817. static void rs_update_rate_tbl(struct iwl_mvm *mvm,
  1818. struct iwl_lq_sta *lq_sta,
  1819. struct iwl_scale_tbl_info *tbl,
  1820. int index, u8 is_green)
  1821. {
  1822. u32 rate;
  1823. /* Update uCode's rate table. */
  1824. rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
  1825. rs_fill_link_cmd(mvm, lq_sta, rate);
  1826. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
  1827. }
  1828. /*
  1829. * Do rate scaling and search for new modulation mode.
  1830. */
  1831. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  1832. struct sk_buff *skb,
  1833. struct ieee80211_sta *sta,
  1834. struct iwl_lq_sta *lq_sta)
  1835. {
  1836. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1837. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1838. int low = IWL_RATE_INVALID;
  1839. int high = IWL_RATE_INVALID;
  1840. int index;
  1841. int i;
  1842. struct iwl_rate_scale_data *window = NULL;
  1843. int current_tpt = IWL_INVALID_VALUE;
  1844. int low_tpt = IWL_INVALID_VALUE;
  1845. int high_tpt = IWL_INVALID_VALUE;
  1846. u32 fail_count;
  1847. s8 scale_action = 0;
  1848. u16 rate_mask;
  1849. u8 update_lq = 0;
  1850. struct iwl_scale_tbl_info *tbl, *tbl1;
  1851. u16 rate_scale_index_msk = 0;
  1852. u8 is_green = 0;
  1853. u8 active_tbl = 0;
  1854. u8 done_search = 0;
  1855. u16 high_low;
  1856. s32 sr;
  1857. u8 tid = IWL_MAX_TID_COUNT;
  1858. struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
  1859. struct iwl_mvm_tid_data *tid_data;
  1860. IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
  1861. /* Send management frames and NO_ACK data using lowest rate. */
  1862. /* TODO: this could probably be improved.. */
  1863. if (!ieee80211_is_data(hdr->frame_control) ||
  1864. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1865. return;
  1866. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1867. tid = rs_tl_add_packet(lq_sta, hdr);
  1868. if ((tid != IWL_MAX_TID_COUNT) &&
  1869. (lq_sta->tx_agg_tid_en & (1 << tid))) {
  1870. tid_data = &sta_priv->tid_data[tid];
  1871. if (tid_data->state == IWL_AGG_OFF)
  1872. lq_sta->is_agg = 0;
  1873. else
  1874. lq_sta->is_agg = 1;
  1875. } else {
  1876. lq_sta->is_agg = 0;
  1877. }
  1878. /*
  1879. * Select rate-scale / modulation-mode table to work with in
  1880. * the rest of this function: "search" if searching for better
  1881. * modulation mode, or "active" if doing rate scaling within a mode.
  1882. */
  1883. if (!lq_sta->search_better_tbl)
  1884. active_tbl = lq_sta->active_tbl;
  1885. else
  1886. active_tbl = 1 - lq_sta->active_tbl;
  1887. tbl = &(lq_sta->lq_info[active_tbl]);
  1888. if (is_legacy(tbl->lq_type))
  1889. lq_sta->is_green = 0;
  1890. else
  1891. lq_sta->is_green = rs_use_green(sta);
  1892. is_green = lq_sta->is_green;
  1893. /* current tx rate */
  1894. index = lq_sta->last_txrate_idx;
  1895. IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
  1896. tbl->lq_type);
  1897. /* rates available for this association, and for modulation mode */
  1898. rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
  1899. IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
  1900. /* mask with station rate restriction */
  1901. if (is_legacy(tbl->lq_type)) {
  1902. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1903. /* supp_rates has no CCK bits in A mode */
  1904. rate_scale_index_msk = (u16) (rate_mask &
  1905. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  1906. else
  1907. rate_scale_index_msk = (u16) (rate_mask &
  1908. lq_sta->supp_rates);
  1909. } else {
  1910. rate_scale_index_msk = rate_mask;
  1911. }
  1912. if (!rate_scale_index_msk)
  1913. rate_scale_index_msk = rate_mask;
  1914. if (!((1 << index) & rate_scale_index_msk)) {
  1915. IWL_ERR(mvm, "Current Rate is not valid\n");
  1916. if (lq_sta->search_better_tbl) {
  1917. /* revert to active table if search table is not valid*/
  1918. tbl->lq_type = LQ_NONE;
  1919. lq_sta->search_better_tbl = 0;
  1920. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1921. /* get "active" rate info */
  1922. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1923. rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
  1924. }
  1925. return;
  1926. }
  1927. /* Get expected throughput table and history window for current rate */
  1928. if (!tbl->expected_tpt) {
  1929. IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
  1930. return;
  1931. }
  1932. /* force user max rate if set by user */
  1933. if ((lq_sta->max_rate_idx != -1) &&
  1934. (lq_sta->max_rate_idx < index)) {
  1935. index = lq_sta->max_rate_idx;
  1936. update_lq = 1;
  1937. window = &(tbl->win[index]);
  1938. goto lq_update;
  1939. }
  1940. window = &(tbl->win[index]);
  1941. /*
  1942. * If there is not enough history to calculate actual average
  1943. * throughput, keep analyzing results of more tx frames, without
  1944. * changing rate or mode (bypass most of the rest of this function).
  1945. * Set up new rate table in uCode only if old rate is not supported
  1946. * in current association (use new rate found above).
  1947. */
  1948. fail_count = window->counter - window->success_counter;
  1949. if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1950. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
  1951. IWL_DEBUG_RATE(mvm,
  1952. "LQ: still below TH. succ=%d total=%d for index %d\n",
  1953. window->success_counter, window->counter, index);
  1954. /* Can't calculate this yet; not enough history */
  1955. window->average_tpt = IWL_INVALID_VALUE;
  1956. /* Should we stay with this modulation mode,
  1957. * or search for a new one? */
  1958. rs_stay_in_table(lq_sta, false);
  1959. goto out;
  1960. }
  1961. /* Else we have enough samples; calculate estimate of
  1962. * actual average throughput */
  1963. if (window->average_tpt != ((window->success_ratio *
  1964. tbl->expected_tpt[index] + 64) / 128)) {
  1965. IWL_ERR(mvm,
  1966. "expected_tpt should have been calculated by now\n");
  1967. window->average_tpt = ((window->success_ratio *
  1968. tbl->expected_tpt[index] + 64) / 128);
  1969. }
  1970. /* If we are searching for better modulation mode, check success. */
  1971. if (lq_sta->search_better_tbl) {
  1972. /* If good success, continue using the "search" mode;
  1973. * no need to send new link quality command, since we're
  1974. * continuing to use the setup that we've been trying. */
  1975. if (window->average_tpt > lq_sta->last_tpt) {
  1976. IWL_DEBUG_RATE(mvm,
  1977. "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
  1978. window->success_ratio,
  1979. window->average_tpt,
  1980. lq_sta->last_tpt);
  1981. if (!is_legacy(tbl->lq_type))
  1982. lq_sta->enable_counter = 1;
  1983. /* Swap tables; "search" becomes "active" */
  1984. lq_sta->active_tbl = active_tbl;
  1985. current_tpt = window->average_tpt;
  1986. /* Else poor success; go back to mode in "active" table */
  1987. } else {
  1988. IWL_DEBUG_RATE(mvm,
  1989. "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
  1990. window->success_ratio,
  1991. window->average_tpt,
  1992. lq_sta->last_tpt);
  1993. /* Nullify "search" table */
  1994. tbl->lq_type = LQ_NONE;
  1995. /* Revert to "active" table */
  1996. active_tbl = lq_sta->active_tbl;
  1997. tbl = &(lq_sta->lq_info[active_tbl]);
  1998. /* Revert to "active" rate and throughput info */
  1999. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  2000. current_tpt = lq_sta->last_tpt;
  2001. /* Need to set up a new rate table in uCode */
  2002. update_lq = 1;
  2003. }
  2004. /* Either way, we've made a decision; modulation mode
  2005. * search is done, allow rate adjustment next time. */
  2006. lq_sta->search_better_tbl = 0;
  2007. done_search = 1; /* Don't switch modes below! */
  2008. goto lq_update;
  2009. }
  2010. /* (Else) not in search of better modulation mode, try for better
  2011. * starting rate, while staying in this mode. */
  2012. high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
  2013. tbl->lq_type);
  2014. low = high_low & 0xff;
  2015. high = (high_low >> 8) & 0xff;
  2016. /* If user set max rate, dont allow higher than user constrain */
  2017. if ((lq_sta->max_rate_idx != -1) &&
  2018. (lq_sta->max_rate_idx < high))
  2019. high = IWL_RATE_INVALID;
  2020. sr = window->success_ratio;
  2021. /* Collect measured throughputs for current and adjacent rates */
  2022. current_tpt = window->average_tpt;
  2023. if (low != IWL_RATE_INVALID)
  2024. low_tpt = tbl->win[low].average_tpt;
  2025. if (high != IWL_RATE_INVALID)
  2026. high_tpt = tbl->win[high].average_tpt;
  2027. scale_action = 0;
  2028. /* Too many failures, decrease rate */
  2029. if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
  2030. IWL_DEBUG_RATE(mvm,
  2031. "decrease rate because of low success_ratio\n");
  2032. scale_action = -1;
  2033. /* No throughput measured yet for adjacent rates; try increase. */
  2034. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  2035. (high_tpt == IWL_INVALID_VALUE)) {
  2036. if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
  2037. scale_action = 1;
  2038. else if (low != IWL_RATE_INVALID)
  2039. scale_action = 0;
  2040. }
  2041. /* Both adjacent throughputs are measured, but neither one has better
  2042. * throughput; we're using the best rate, don't change it! */
  2043. else if ((low_tpt != IWL_INVALID_VALUE) &&
  2044. (high_tpt != IWL_INVALID_VALUE) &&
  2045. (low_tpt < current_tpt) &&
  2046. (high_tpt < current_tpt))
  2047. scale_action = 0;
  2048. /* At least one adjacent rate's throughput is measured,
  2049. * and may have better performance. */
  2050. else {
  2051. /* Higher adjacent rate's throughput is measured */
  2052. if (high_tpt != IWL_INVALID_VALUE) {
  2053. /* Higher rate has better throughput */
  2054. if (high_tpt > current_tpt &&
  2055. sr >= IWL_RATE_INCREASE_TH) {
  2056. scale_action = 1;
  2057. } else {
  2058. scale_action = 0;
  2059. }
  2060. /* Lower adjacent rate's throughput is measured */
  2061. } else if (low_tpt != IWL_INVALID_VALUE) {
  2062. /* Lower rate has better throughput */
  2063. if (low_tpt > current_tpt) {
  2064. IWL_DEBUG_RATE(mvm,
  2065. "decrease rate because of low tpt\n");
  2066. scale_action = -1;
  2067. } else if (sr >= IWL_RATE_INCREASE_TH) {
  2068. scale_action = 1;
  2069. }
  2070. }
  2071. }
  2072. /* Sanity check; asked for decrease, but success rate or throughput
  2073. * has been good at old rate. Don't change it. */
  2074. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  2075. ((sr > IWL_RATE_HIGH_TH) ||
  2076. (current_tpt > (100 * tbl->expected_tpt[low]))))
  2077. scale_action = 0;
  2078. if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
  2079. IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
  2080. (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
  2081. if (lq_sta->last_bt_traffic >
  2082. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  2083. /*
  2084. * don't set scale_action, don't want to scale up if
  2085. * the rate scale doesn't otherwise think that is a
  2086. * good idea.
  2087. */
  2088. } else if (lq_sta->last_bt_traffic <=
  2089. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  2090. scale_action = -1;
  2091. }
  2092. }
  2093. lq_sta->last_bt_traffic =
  2094. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD);
  2095. if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
  2096. IWL_BT_COEX_TRAFFIC_LOAD_HIGH) &&
  2097. (is_mimo2(tbl->lq_type) || is_mimo3(tbl->lq_type))) {
  2098. /* search for a new modulation */
  2099. rs_stay_in_table(lq_sta, true);
  2100. goto lq_update;
  2101. }
  2102. switch (scale_action) {
  2103. case -1:
  2104. /* Decrease starting rate, update uCode's rate table */
  2105. if (low != IWL_RATE_INVALID) {
  2106. update_lq = 1;
  2107. index = low;
  2108. }
  2109. break;
  2110. case 1:
  2111. /* Increase starting rate, update uCode's rate table */
  2112. if (high != IWL_RATE_INVALID) {
  2113. update_lq = 1;
  2114. index = high;
  2115. }
  2116. break;
  2117. case 0:
  2118. /* No change */
  2119. default:
  2120. break;
  2121. }
  2122. IWL_DEBUG_RATE(mvm,
  2123. "choose rate scale index %d action %d low %d high %d type %d\n",
  2124. index, scale_action, low, high, tbl->lq_type);
  2125. lq_update:
  2126. /* Replace uCode's rate table for the destination station. */
  2127. if (update_lq)
  2128. rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
  2129. rs_stay_in_table(lq_sta, false);
  2130. /*
  2131. * Search for new modulation mode if we're:
  2132. * 1) Not changing rates right now
  2133. * 2) Not just finishing up a search
  2134. * 3) Allowing a new search
  2135. */
  2136. if (!update_lq && !done_search &&
  2137. !lq_sta->stay_in_tbl && window->counter) {
  2138. /* Save current throughput to compare with "search" throughput*/
  2139. lq_sta->last_tpt = current_tpt;
  2140. /* Select a new "search" modulation mode to try.
  2141. * If one is found, set up the new "search" table. */
  2142. if (is_legacy(tbl->lq_type))
  2143. rs_move_legacy_other(mvm, lq_sta, sta, index);
  2144. else if (is_siso(tbl->lq_type))
  2145. rs_move_siso_to_other(mvm, lq_sta, sta, index);
  2146. else if (is_mimo2(tbl->lq_type))
  2147. rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
  2148. else
  2149. rs_move_mimo3_to_other(mvm, lq_sta, sta, index);
  2150. /* If new "search" mode was selected, set up in uCode table */
  2151. if (lq_sta->search_better_tbl) {
  2152. /* Access the "search" table, clear its history. */
  2153. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  2154. for (i = 0; i < IWL_RATE_COUNT; i++)
  2155. rs_rate_scale_clear_window(&(tbl->win[i]));
  2156. /* Use new "search" start rate */
  2157. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  2158. IWL_DEBUG_RATE(mvm,
  2159. "Switch current mcs: %X index: %d\n",
  2160. tbl->current_rate, index);
  2161. rs_fill_link_cmd(mvm, lq_sta, tbl->current_rate);
  2162. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
  2163. } else {
  2164. done_search = 1;
  2165. }
  2166. }
  2167. if (done_search && !lq_sta->stay_in_tbl) {
  2168. /* If the "active" (non-search) mode was legacy,
  2169. * and we've tried switching antennas,
  2170. * but we haven't been able to try HT modes (not available),
  2171. * stay with best antenna legacy modulation for a while
  2172. * before next round of mode comparisons. */
  2173. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2174. if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
  2175. lq_sta->action_counter > tbl1->max_search) {
  2176. IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
  2177. rs_set_stay_in_table(mvm, 1, lq_sta);
  2178. }
  2179. /* If we're in an HT mode, and all 3 mode switch actions
  2180. * have been tried and compared, stay in this best modulation
  2181. * mode for a while before next round of mode comparisons. */
  2182. if (lq_sta->enable_counter &&
  2183. (lq_sta->action_counter >= tbl1->max_search)) {
  2184. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  2185. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  2186. (tid != IWL_MAX_TID_COUNT)) {
  2187. tid_data = &sta_priv->tid_data[tid];
  2188. if (tid_data->state == IWL_AGG_OFF) {
  2189. IWL_DEBUG_RATE(mvm,
  2190. "try to aggregate tid %d\n",
  2191. tid);
  2192. rs_tl_turn_on_agg(mvm, tid,
  2193. lq_sta, sta);
  2194. }
  2195. }
  2196. rs_set_stay_in_table(mvm, 0, lq_sta);
  2197. }
  2198. }
  2199. out:
  2200. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
  2201. lq_sta->last_txrate_idx = index;
  2202. }
  2203. /**
  2204. * rs_initialize_lq - Initialize a station's hardware rate table
  2205. *
  2206. * The uCode's station table contains a table of fallback rates
  2207. * for automatic fallback during transmission.
  2208. *
  2209. * NOTE: This sets up a default set of values. These will be replaced later
  2210. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  2211. * rc80211_simple.
  2212. *
  2213. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  2214. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  2215. * which requires station table entry to exist).
  2216. */
  2217. static void rs_initialize_lq(struct iwl_mvm *mvm,
  2218. struct ieee80211_sta *sta,
  2219. struct iwl_lq_sta *lq_sta,
  2220. enum ieee80211_band band)
  2221. {
  2222. struct iwl_scale_tbl_info *tbl;
  2223. int rate_idx;
  2224. int i;
  2225. u32 rate;
  2226. u8 use_green = rs_use_green(sta);
  2227. u8 active_tbl = 0;
  2228. u8 valid_tx_ant;
  2229. if (!sta || !lq_sta)
  2230. return;
  2231. i = lq_sta->last_txrate_idx;
  2232. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2233. if (!lq_sta->search_better_tbl)
  2234. active_tbl = lq_sta->active_tbl;
  2235. else
  2236. active_tbl = 1 - lq_sta->active_tbl;
  2237. tbl = &(lq_sta->lq_info[active_tbl]);
  2238. if ((i < 0) || (i >= IWL_RATE_COUNT))
  2239. i = 0;
  2240. rate = iwl_rates[i].plcp;
  2241. tbl->ant_type = first_antenna(valid_tx_ant);
  2242. rate |= tbl->ant_type << RATE_MCS_ANT_POS;
  2243. if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
  2244. rate |= RATE_MCS_CCK_MSK;
  2245. rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
  2246. if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
  2247. rs_toggle_antenna(valid_tx_ant, &rate, tbl);
  2248. rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx, use_green);
  2249. tbl->current_rate = rate;
  2250. rs_set_expected_tpt_table(lq_sta, tbl);
  2251. rs_fill_link_cmd(NULL, lq_sta, rate);
  2252. /* TODO restore station should remember the lq cmd */
  2253. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
  2254. }
  2255. static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
  2256. struct ieee80211_tx_rate_control *txrc)
  2257. {
  2258. struct sk_buff *skb = txrc->skb;
  2259. struct ieee80211_supported_band *sband = txrc->sband;
  2260. struct iwl_op_mode *op_mode __maybe_unused =
  2261. (struct iwl_op_mode *)mvm_r;
  2262. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2263. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2264. struct iwl_lq_sta *lq_sta = mvm_sta;
  2265. int rate_idx;
  2266. IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
  2267. /* Get max rate if user set max rate */
  2268. if (lq_sta) {
  2269. lq_sta->max_rate_idx = txrc->max_rate_idx;
  2270. if ((sband->band == IEEE80211_BAND_5GHZ) &&
  2271. (lq_sta->max_rate_idx != -1))
  2272. lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
  2273. if ((lq_sta->max_rate_idx < 0) ||
  2274. (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
  2275. lq_sta->max_rate_idx = -1;
  2276. }
  2277. /* Treat uninitialized rate scaling data same as non-existing. */
  2278. if (lq_sta && !lq_sta->drv) {
  2279. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  2280. mvm_sta = NULL;
  2281. }
  2282. /* Send management frames and NO_ACK data using lowest rate. */
  2283. if (rate_control_send_low(sta, mvm_sta, txrc))
  2284. return;
  2285. rate_idx = lq_sta->last_txrate_idx;
  2286. if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
  2287. rate_idx -= IWL_FIRST_OFDM_RATE;
  2288. /* 6M and 9M shared same MCS index */
  2289. rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
  2290. if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
  2291. IWL_RATE_MIMO3_6M_PLCP)
  2292. rate_idx = rate_idx + (2 * MCS_INDEX_PER_STREAM);
  2293. else if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
  2294. IWL_RATE_MIMO2_6M_PLCP)
  2295. rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
  2296. info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  2297. if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
  2298. info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
  2299. if (lq_sta->last_rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
  2300. info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  2301. if (lq_sta->last_rate_n_flags & RATE_HT_MCS_GF_MSK)
  2302. info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
  2303. } else {
  2304. /* Check for invalid rates */
  2305. if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
  2306. ((sband->band == IEEE80211_BAND_5GHZ) &&
  2307. (rate_idx < IWL_FIRST_OFDM_RATE)))
  2308. rate_idx = rate_lowest_index(sband, sta);
  2309. /* On valid 5 GHz rate, adjust index */
  2310. else if (sband->band == IEEE80211_BAND_5GHZ)
  2311. rate_idx -= IWL_FIRST_OFDM_RATE;
  2312. info->control.rates[0].flags = 0;
  2313. }
  2314. info->control.rates[0].idx = rate_idx;
  2315. }
  2316. static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
  2317. gfp_t gfp)
  2318. {
  2319. struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  2320. struct iwl_op_mode *op_mode __maybe_unused =
  2321. (struct iwl_op_mode *)mvm_rate;
  2322. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2323. IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
  2324. return &sta_priv->lq_sta;
  2325. }
  2326. /*
  2327. * Called after adding a new station to initialize rate scaling
  2328. */
  2329. void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  2330. enum ieee80211_band band)
  2331. {
  2332. int i, j;
  2333. struct ieee80211_hw *hw = mvm->hw;
  2334. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2335. struct iwl_mvm_sta *sta_priv;
  2336. struct iwl_lq_sta *lq_sta;
  2337. struct ieee80211_supported_band *sband;
  2338. unsigned long supp; /* must be unsigned long for for_each_set_bit */
  2339. sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  2340. lq_sta = &sta_priv->lq_sta;
  2341. sband = hw->wiphy->bands[band];
  2342. lq_sta->lq.sta_id = sta_priv->sta_id;
  2343. for (j = 0; j < LQ_SIZE; j++)
  2344. for (i = 0; i < IWL_RATE_COUNT; i++)
  2345. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  2346. lq_sta->flush_timer = 0;
  2347. lq_sta->supp_rates = sta->supp_rates[sband->band];
  2348. for (j = 0; j < LQ_SIZE; j++)
  2349. for (i = 0; i < IWL_RATE_COUNT; i++)
  2350. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  2351. IWL_DEBUG_RATE(mvm,
  2352. "LQ: *** rate scale station global init for station %d ***\n",
  2353. sta_priv->sta_id);
  2354. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2355. * the lowest or the highest rate.. Could consider using RSSI from
  2356. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2357. * after assoc.. */
  2358. lq_sta->max_rate_idx = -1;
  2359. lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
  2360. lq_sta->is_green = rs_use_green(sta);
  2361. lq_sta->band = sband->band;
  2362. /*
  2363. * active legacy rates as per supported rates bitmap
  2364. */
  2365. supp = sta->supp_rates[sband->band];
  2366. lq_sta->active_legacy_rate = 0;
  2367. for_each_set_bit(i, &supp, BITS_PER_LONG)
  2368. lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
  2369. /*
  2370. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  2371. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  2372. */
  2373. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2374. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2375. lq_sta->active_siso_rate &= ~((u16)0x2);
  2376. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2377. /* Same here */
  2378. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2379. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2380. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2381. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2382. lq_sta->active_mimo3_rate = ht_cap->mcs.rx_mask[2] << 1;
  2383. lq_sta->active_mimo3_rate |= ht_cap->mcs.rx_mask[2] & 0x1;
  2384. lq_sta->active_mimo3_rate &= ~((u16)0x2);
  2385. lq_sta->active_mimo3_rate <<= IWL_FIRST_OFDM_RATE;
  2386. IWL_DEBUG_RATE(mvm,
  2387. "SISO-RATE=%X MIMO2-RATE=%X MIMO3-RATE=%X\n",
  2388. lq_sta->active_siso_rate,
  2389. lq_sta->active_mimo2_rate,
  2390. lq_sta->active_mimo3_rate);
  2391. /* These values will be overridden later */
  2392. lq_sta->lq.single_stream_ant_msk =
  2393. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  2394. lq_sta->lq.dual_stream_ant_msk =
  2395. iwl_fw_valid_tx_ant(mvm->fw) &
  2396. ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  2397. if (!lq_sta->lq.dual_stream_ant_msk) {
  2398. lq_sta->lq.dual_stream_ant_msk = ANT_AB;
  2399. } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
  2400. lq_sta->lq.dual_stream_ant_msk =
  2401. iwl_fw_valid_tx_ant(mvm->fw);
  2402. }
  2403. /* as default allow aggregation for all tids */
  2404. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2405. lq_sta->drv = mvm;
  2406. /* Set last_txrate_idx to lowest rate */
  2407. lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
  2408. if (sband->band == IEEE80211_BAND_5GHZ)
  2409. lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  2410. lq_sta->is_agg = 0;
  2411. #ifdef CONFIG_MAC80211_DEBUGFS
  2412. lq_sta->dbg_fixed_rate = 0;
  2413. #endif
  2414. rs_initialize_lq(mvm, sta, lq_sta, band);
  2415. }
  2416. static void rs_fill_link_cmd(struct iwl_mvm *mvm,
  2417. struct iwl_lq_sta *lq_sta, u32 new_rate)
  2418. {
  2419. struct iwl_scale_tbl_info tbl_type;
  2420. int index = 0;
  2421. int rate_idx;
  2422. int repeat_rate = 0;
  2423. u8 ant_toggle_cnt = 0;
  2424. u8 use_ht_possible = 1;
  2425. u8 valid_tx_ant = 0;
  2426. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2427. /* Override starting rate (index 0) if needed for debug purposes */
  2428. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2429. /* Interpret new_rate (rate_n_flags) */
  2430. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
  2431. &tbl_type, &rate_idx);
  2432. /* How many times should we repeat the initial rate? */
  2433. if (is_legacy(tbl_type.lq_type)) {
  2434. ant_toggle_cnt = 1;
  2435. repeat_rate = IWL_NUMBER_TRY;
  2436. } else {
  2437. repeat_rate = min(IWL_HT_NUMBER_TRY,
  2438. LINK_QUAL_AGG_DISABLE_START_DEF - 1);
  2439. }
  2440. lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
  2441. /* Fill 1st table entry (index 0) */
  2442. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2443. if (num_of_ant(tbl_type.ant_type) == 1)
  2444. lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
  2445. else if (num_of_ant(tbl_type.ant_type) == 2)
  2446. lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
  2447. /* otherwise we don't modify the existing value */
  2448. index++;
  2449. repeat_rate--;
  2450. if (mvm)
  2451. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2452. /* Fill rest of rate table */
  2453. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  2454. /* Repeat initial/next rate.
  2455. * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
  2456. * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
  2457. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  2458. if (is_legacy(tbl_type.lq_type)) {
  2459. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2460. ant_toggle_cnt++;
  2461. else if (mvm &&
  2462. rs_toggle_antenna(valid_tx_ant,
  2463. &new_rate, &tbl_type))
  2464. ant_toggle_cnt = 1;
  2465. }
  2466. /* Override next rate if needed for debug purposes */
  2467. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2468. /* Fill next table entry */
  2469. lq_cmd->rs_table[index] =
  2470. cpu_to_le32(new_rate);
  2471. repeat_rate--;
  2472. index++;
  2473. }
  2474. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
  2475. &rate_idx);
  2476. /* Indicate to uCode which entries might be MIMO.
  2477. * If initial rate was MIMO, this will finally end up
  2478. * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2479. if (is_mimo(tbl_type.lq_type))
  2480. lq_cmd->mimo_delim = index;
  2481. /* Get next rate */
  2482. new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
  2483. use_ht_possible);
  2484. /* How many times should we repeat the next rate? */
  2485. if (is_legacy(tbl_type.lq_type)) {
  2486. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2487. ant_toggle_cnt++;
  2488. else if (mvm &&
  2489. rs_toggle_antenna(valid_tx_ant,
  2490. &new_rate, &tbl_type))
  2491. ant_toggle_cnt = 1;
  2492. repeat_rate = IWL_NUMBER_TRY;
  2493. } else {
  2494. repeat_rate = IWL_HT_NUMBER_TRY;
  2495. }
  2496. /* Don't allow HT rates after next pass.
  2497. * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  2498. use_ht_possible = 0;
  2499. /* Override next rate if needed for debug purposes */
  2500. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  2501. /* Fill next table entry */
  2502. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2503. index++;
  2504. repeat_rate--;
  2505. }
  2506. lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2507. lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2508. lq_cmd->agg_time_limit =
  2509. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2510. /*
  2511. * overwrite if needed, pass aggregation time limit
  2512. * to uCode in uSec - This is racy - but heh, at least it helps...
  2513. */
  2514. if (mvm && BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2)
  2515. lq_cmd->agg_time_limit = cpu_to_le16(1200);
  2516. }
  2517. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2518. {
  2519. return hw->priv;
  2520. }
  2521. /* rate scale requires free function to be implemented */
  2522. static void rs_free(void *mvm_rate)
  2523. {
  2524. return;
  2525. }
  2526. static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
  2527. void *mvm_sta)
  2528. {
  2529. struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
  2530. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2531. IWL_DEBUG_RATE(mvm, "enter\n");
  2532. IWL_DEBUG_RATE(mvm, "leave\n");
  2533. }
  2534. #ifdef CONFIG_MAC80211_DEBUGFS
  2535. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  2536. u32 *rate_n_flags, int index)
  2537. {
  2538. struct iwl_mvm *mvm;
  2539. u8 valid_tx_ant;
  2540. u8 ant_sel_tx;
  2541. mvm = lq_sta->drv;
  2542. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2543. if (lq_sta->dbg_fixed_rate) {
  2544. ant_sel_tx =
  2545. ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
  2546. >> RATE_MCS_ANT_POS);
  2547. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2548. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2549. IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
  2550. } else {
  2551. lq_sta->dbg_fixed_rate = 0;
  2552. IWL_ERR(mvm,
  2553. "Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2554. ant_sel_tx, valid_tx_ant);
  2555. IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
  2556. }
  2557. } else {
  2558. IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
  2559. }
  2560. }
  2561. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2562. const char __user *user_buf, size_t count, loff_t *ppos)
  2563. {
  2564. struct iwl_lq_sta *lq_sta = file->private_data;
  2565. struct iwl_mvm *mvm;
  2566. char buf[64];
  2567. size_t buf_size;
  2568. u32 parsed_rate;
  2569. mvm = lq_sta->drv;
  2570. memset(buf, 0, sizeof(buf));
  2571. buf_size = min(count, sizeof(buf) - 1);
  2572. if (copy_from_user(buf, user_buf, buf_size))
  2573. return -EFAULT;
  2574. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2575. lq_sta->dbg_fixed_rate = parsed_rate;
  2576. else
  2577. lq_sta->dbg_fixed_rate = 0;
  2578. rs_program_fix_rate(mvm, lq_sta);
  2579. return count;
  2580. }
  2581. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2582. char __user *user_buf, size_t count, loff_t *ppos)
  2583. {
  2584. char *buff;
  2585. int desc = 0;
  2586. int i = 0;
  2587. int index = 0;
  2588. ssize_t ret;
  2589. struct iwl_lq_sta *lq_sta = file->private_data;
  2590. struct iwl_mvm *mvm;
  2591. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2592. mvm = lq_sta->drv;
  2593. buff = kmalloc(1024, GFP_KERNEL);
  2594. if (!buff)
  2595. return -ENOMEM;
  2596. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2597. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2598. lq_sta->total_failed, lq_sta->total_success,
  2599. lq_sta->active_legacy_rate);
  2600. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2601. lq_sta->dbg_fixed_rate);
  2602. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2603. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
  2604. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
  2605. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
  2606. desc += sprintf(buff+desc, "lq type %s\n",
  2607. (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
  2608. if (is_Ht(tbl->lq_type)) {
  2609. desc += sprintf(buff+desc, " %s",
  2610. (is_siso(tbl->lq_type)) ? "SISO" :
  2611. ((is_mimo2(tbl->lq_type)) ? "MIMO2" : "MIMO3"));
  2612. desc += sprintf(buff+desc, " %s",
  2613. (tbl->is_ht40) ? "40MHz" : "20MHz");
  2614. desc += sprintf(buff+desc, " %s %s %s\n",
  2615. (tbl->is_SGI) ? "SGI" : "",
  2616. (lq_sta->is_green) ? "GF enabled" : "",
  2617. (lq_sta->is_agg) ? "AGG on" : "");
  2618. }
  2619. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2620. lq_sta->last_rate_n_flags);
  2621. desc += sprintf(buff+desc,
  2622. "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2623. lq_sta->lq.flags,
  2624. lq_sta->lq.mimo_delim,
  2625. lq_sta->lq.single_stream_ant_msk,
  2626. lq_sta->lq.dual_stream_ant_msk);
  2627. desc += sprintf(buff+desc,
  2628. "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2629. le16_to_cpu(lq_sta->lq.agg_time_limit),
  2630. lq_sta->lq.agg_disable_start_th,
  2631. lq_sta->lq.agg_frame_cnt_limit);
  2632. desc += sprintf(buff+desc,
  2633. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2634. lq_sta->lq.initial_rate_index[0],
  2635. lq_sta->lq.initial_rate_index[1],
  2636. lq_sta->lq.initial_rate_index[2],
  2637. lq_sta->lq.initial_rate_index[3]);
  2638. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2639. index = iwl_hwrate_to_plcp_idx(
  2640. le32_to_cpu(lq_sta->lq.rs_table[i]));
  2641. if (is_legacy(tbl->lq_type)) {
  2642. desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
  2643. i, le32_to_cpu(lq_sta->lq.rs_table[i]),
  2644. iwl_rate_mcs[index].mbps);
  2645. } else {
  2646. desc += sprintf(buff+desc,
  2647. " rate[%d] 0x%X %smbps (%s)\n",
  2648. i, le32_to_cpu(lq_sta->lq.rs_table[i]),
  2649. iwl_rate_mcs[index].mbps,
  2650. iwl_rate_mcs[index].mcs);
  2651. }
  2652. }
  2653. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2654. kfree(buff);
  2655. return ret;
  2656. }
  2657. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2658. .write = rs_sta_dbgfs_scale_table_write,
  2659. .read = rs_sta_dbgfs_scale_table_read,
  2660. .open = simple_open,
  2661. .llseek = default_llseek,
  2662. };
  2663. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2664. char __user *user_buf, size_t count, loff_t *ppos)
  2665. {
  2666. char *buff;
  2667. int desc = 0;
  2668. int i, j;
  2669. ssize_t ret;
  2670. struct iwl_lq_sta *lq_sta = file->private_data;
  2671. buff = kmalloc(1024, GFP_KERNEL);
  2672. if (!buff)
  2673. return -ENOMEM;
  2674. for (i = 0; i < LQ_SIZE; i++) {
  2675. desc += sprintf(buff+desc,
  2676. "%s type=%d SGI=%d HT40=%d DUP=0 GF=%d\n"
  2677. "rate=0x%X\n",
  2678. lq_sta->active_tbl == i ? "*" : "x",
  2679. lq_sta->lq_info[i].lq_type,
  2680. lq_sta->lq_info[i].is_SGI,
  2681. lq_sta->lq_info[i].is_ht40,
  2682. lq_sta->is_green,
  2683. lq_sta->lq_info[i].current_rate);
  2684. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2685. desc += sprintf(buff+desc,
  2686. "counter=%d success=%d %%=%d\n",
  2687. lq_sta->lq_info[i].win[j].counter,
  2688. lq_sta->lq_info[i].win[j].success_counter,
  2689. lq_sta->lq_info[i].win[j].success_ratio);
  2690. }
  2691. }
  2692. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2693. kfree(buff);
  2694. return ret;
  2695. }
  2696. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2697. .read = rs_sta_dbgfs_stats_table_read,
  2698. .open = simple_open,
  2699. .llseek = default_llseek,
  2700. };
  2701. static ssize_t rs_sta_dbgfs_rate_scale_data_read(struct file *file,
  2702. char __user *user_buf, size_t count, loff_t *ppos)
  2703. {
  2704. struct iwl_lq_sta *lq_sta = file->private_data;
  2705. struct iwl_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
  2706. char buff[120];
  2707. int desc = 0;
  2708. if (is_Ht(tbl->lq_type))
  2709. desc += sprintf(buff+desc,
  2710. "Bit Rate= %d Mb/s\n",
  2711. tbl->expected_tpt[lq_sta->last_txrate_idx]);
  2712. else
  2713. desc += sprintf(buff+desc,
  2714. "Bit Rate= %d Mb/s\n",
  2715. iwl_rates[lq_sta->last_txrate_idx].ieee >> 1);
  2716. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2717. }
  2718. static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
  2719. .read = rs_sta_dbgfs_rate_scale_data_read,
  2720. .open = simple_open,
  2721. .llseek = default_llseek,
  2722. };
  2723. static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
  2724. {
  2725. struct iwl_lq_sta *lq_sta = mvm_sta;
  2726. lq_sta->rs_sta_dbgfs_scale_table_file =
  2727. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2728. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2729. lq_sta->rs_sta_dbgfs_stats_table_file =
  2730. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  2731. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2732. lq_sta->rs_sta_dbgfs_rate_scale_data_file =
  2733. debugfs_create_file("rate_scale_data", S_IRUSR, dir,
  2734. lq_sta, &rs_sta_dbgfs_rate_scale_data_ops);
  2735. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2736. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2737. &lq_sta->tx_agg_tid_en);
  2738. }
  2739. static void rs_remove_debugfs(void *mvm, void *mvm_sta)
  2740. {
  2741. struct iwl_lq_sta *lq_sta = mvm_sta;
  2742. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2743. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2744. debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
  2745. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2746. }
  2747. #endif
  2748. /*
  2749. * Initialization of rate scaling information is done by driver after
  2750. * the station is added. Since mac80211 calls this function before a
  2751. * station is added we ignore it.
  2752. */
  2753. static void rs_rate_init_stub(void *mvm_r,
  2754. struct ieee80211_supported_band *sband,
  2755. struct ieee80211_sta *sta, void *mvm_sta)
  2756. {
  2757. }
  2758. static struct rate_control_ops rs_mvm_ops = {
  2759. .module = NULL,
  2760. .name = RS_NAME,
  2761. .tx_status = rs_tx_status,
  2762. .get_rate = rs_get_rate,
  2763. .rate_init = rs_rate_init_stub,
  2764. .alloc = rs_alloc,
  2765. .free = rs_free,
  2766. .alloc_sta = rs_alloc_sta,
  2767. .free_sta = rs_free_sta,
  2768. #ifdef CONFIG_MAC80211_DEBUGFS
  2769. .add_sta_debugfs = rs_add_debugfs,
  2770. .remove_sta_debugfs = rs_remove_debugfs,
  2771. #endif
  2772. };
  2773. int iwl_mvm_rate_control_register(void)
  2774. {
  2775. return ieee80211_rate_control_register(&rs_mvm_ops);
  2776. }
  2777. void iwl_mvm_rate_control_unregister(void)
  2778. {
  2779. ieee80211_rate_control_unregister(&rs_mvm_ops);
  2780. }
  2781. /**
  2782. * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
  2783. * Tx protection, according to this rquest and previous requests,
  2784. * and send the LQ command.
  2785. * @lq: The LQ command
  2786. * @mvmsta: The station
  2787. * @enable: Enable Tx protection?
  2788. */
  2789. int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq,
  2790. struct iwl_mvm_sta *mvmsta, bool enable)
  2791. {
  2792. lockdep_assert_held(&mvm->mutex);
  2793. if (enable) {
  2794. if (mvmsta->tx_protection == 0)
  2795. lq->flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
  2796. mvmsta->tx_protection++;
  2797. } else {
  2798. mvmsta->tx_protection--;
  2799. if (mvmsta->tx_protection == 0)
  2800. lq->flags &= ~LQ_FLAG_SET_STA_TLC_RTS_MSK;
  2801. }
  2802. return iwl_mvm_send_lq_cmd(mvm, lq, CMD_ASYNC, false);
  2803. }