iwl-agn-rs.c 99 KB

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