iwl-agn-rs.c 98 KB

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