rs.c 98 KB

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