iwl-agn-rs.c 99 KB

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