rs.c 81 KB

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