rs.c 99 KB

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