iwl-agn-rs.c 98 KB

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