rs.c 82 KB

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