rs.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751
  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. /* Move to the next action and wrap around to the first action in case
  965. * we're at the last action. Assumes actions start at 0.
  966. */
  967. static inline void rs_move_next_action(struct iwl_scale_tbl_info *tbl,
  968. u8 last_action)
  969. {
  970. BUILD_BUG_ON(IWL_LEGACY_FIRST_ACTION != 0);
  971. BUILD_BUG_ON(IWL_SISO_FIRST_ACTION != 0);
  972. BUILD_BUG_ON(IWL_MIMO2_FIRST_ACTION != 0);
  973. tbl->action = (tbl->action + 1) % (last_action + 1);
  974. }
  975. /*
  976. * Set up search table for MIMO2
  977. */
  978. static int rs_switch_to_mimo2(struct iwl_mvm *mvm,
  979. struct iwl_lq_sta *lq_sta,
  980. struct ieee80211_sta *sta,
  981. struct iwl_scale_tbl_info *tbl, int index)
  982. {
  983. u16 rate_mask;
  984. s32 rate;
  985. s8 is_green = lq_sta->is_green;
  986. if (!sta->ht_cap.ht_supported)
  987. return -1;
  988. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  989. return -1;
  990. /* Need both Tx chains/antennas to support MIMO */
  991. if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) < 2)
  992. return -1;
  993. IWL_DEBUG_RATE(mvm, "LQ: try to switch to MIMO2\n");
  994. tbl->lq_type = LQ_MIMO2;
  995. tbl->action = 0;
  996. tbl->max_search = IWL_MAX_SEARCH;
  997. rate_mask = lq_sta->active_mimo2_rate;
  998. if (iwl_is_ht40_tx_allowed(sta))
  999. tbl->is_ht40 = 1;
  1000. else
  1001. tbl->is_ht40 = 0;
  1002. rs_set_expected_tpt_table(lq_sta, tbl);
  1003. rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
  1004. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 best rate %d mask %X\n",
  1005. rate, rate_mask);
  1006. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1007. IWL_DEBUG_RATE(mvm, "Can't switch with index %d rate mask %x\n",
  1008. rate, rate_mask);
  1009. return -1;
  1010. }
  1011. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
  1012. IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
  1013. tbl->current_rate, is_green);
  1014. return 0;
  1015. }
  1016. /*
  1017. * Set up search table for SISO
  1018. */
  1019. static int rs_switch_to_siso(struct iwl_mvm *mvm,
  1020. struct iwl_lq_sta *lq_sta,
  1021. struct ieee80211_sta *sta,
  1022. struct iwl_scale_tbl_info *tbl, int index)
  1023. {
  1024. u16 rate_mask;
  1025. u8 is_green = lq_sta->is_green;
  1026. s32 rate;
  1027. if (!sta->ht_cap.ht_supported)
  1028. return -1;
  1029. IWL_DEBUG_RATE(mvm, "LQ: try to switch to SISO\n");
  1030. tbl->lq_type = LQ_SISO;
  1031. tbl->action = 0;
  1032. tbl->max_search = IWL_MAX_SEARCH;
  1033. rate_mask = lq_sta->active_siso_rate;
  1034. if (iwl_is_ht40_tx_allowed(sta))
  1035. tbl->is_ht40 = 1;
  1036. else
  1037. tbl->is_ht40 = 0;
  1038. if (is_green)
  1039. tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield*/
  1040. rs_set_expected_tpt_table(lq_sta, tbl);
  1041. rate = rs_get_best_rate(mvm, lq_sta, tbl, rate_mask, index);
  1042. IWL_DEBUG_RATE(mvm, "LQ: get best rate %d mask %X\n", rate, rate_mask);
  1043. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  1044. IWL_DEBUG_RATE(mvm,
  1045. "can not switch with index %d rate mask %x\n",
  1046. rate, rate_mask);
  1047. return -1;
  1048. }
  1049. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, rate, is_green);
  1050. IWL_DEBUG_RATE(mvm, "LQ: Switch to new mcs %X index is green %X\n",
  1051. tbl->current_rate, is_green);
  1052. return 0;
  1053. }
  1054. /*
  1055. * Try to switch to new modulation mode from legacy
  1056. */
  1057. static int rs_move_legacy_other(struct iwl_mvm *mvm,
  1058. struct iwl_lq_sta *lq_sta,
  1059. struct ieee80211_sta *sta,
  1060. int index)
  1061. {
  1062. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1063. struct iwl_scale_tbl_info *search_tbl =
  1064. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1065. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1066. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1067. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1068. u8 start_action;
  1069. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1070. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1071. int ret;
  1072. u8 update_search_tbl_counter = 0;
  1073. start_action = tbl->action;
  1074. while (1) {
  1075. lq_sta->action_counter++;
  1076. switch (tbl->action) {
  1077. case IWL_LEGACY_SWITCH_ANTENNA:
  1078. IWL_DEBUG_RATE(mvm, "LQ: Legacy toggle Antenna\n");
  1079. if (tx_chains_num <= 1)
  1080. break;
  1081. /* Don't change antenna if success has been great */
  1082. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1083. break;
  1084. /* Set up search table to try other antenna */
  1085. memcpy(search_tbl, tbl, sz);
  1086. if (rs_toggle_antenna(valid_tx_ant,
  1087. &search_tbl->current_rate,
  1088. search_tbl)) {
  1089. update_search_tbl_counter = 1;
  1090. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1091. goto out;
  1092. }
  1093. break;
  1094. case IWL_LEGACY_SWITCH_SISO:
  1095. IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to SISO\n");
  1096. /* Set up search table to try SISO */
  1097. memcpy(search_tbl, tbl, sz);
  1098. search_tbl->is_SGI = 0;
  1099. ret = rs_switch_to_siso(mvm, lq_sta, sta,
  1100. search_tbl, index);
  1101. if (!ret) {
  1102. lq_sta->action_counter = 0;
  1103. goto out;
  1104. }
  1105. break;
  1106. case IWL_LEGACY_SWITCH_MIMO2:
  1107. IWL_DEBUG_RATE(mvm, "LQ: Legacy switch to MIMO2\n");
  1108. /* Set up search table to try MIMO */
  1109. memcpy(search_tbl, tbl, sz);
  1110. search_tbl->is_SGI = 0;
  1111. search_tbl->ant_type = ANT_AB;
  1112. if (!rs_is_valid_ant(valid_tx_ant,
  1113. search_tbl->ant_type))
  1114. break;
  1115. ret = rs_switch_to_mimo2(mvm, lq_sta, sta,
  1116. search_tbl, index);
  1117. if (!ret) {
  1118. lq_sta->action_counter = 0;
  1119. goto out;
  1120. }
  1121. break;
  1122. default:
  1123. WARN_ON_ONCE(1);
  1124. }
  1125. rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
  1126. if (tbl->action == start_action)
  1127. break;
  1128. }
  1129. search_tbl->lq_type = LQ_NONE;
  1130. return 0;
  1131. out:
  1132. lq_sta->search_better_tbl = 1;
  1133. rs_move_next_action(tbl, IWL_LEGACY_LAST_ACTION);
  1134. if (update_search_tbl_counter)
  1135. search_tbl->action = tbl->action;
  1136. return 0;
  1137. }
  1138. /*
  1139. * Try to switch to new modulation mode from SISO
  1140. */
  1141. static int rs_move_siso_to_other(struct iwl_mvm *mvm,
  1142. struct iwl_lq_sta *lq_sta,
  1143. struct ieee80211_sta *sta, int index)
  1144. {
  1145. u8 is_green = lq_sta->is_green;
  1146. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1147. struct iwl_scale_tbl_info *search_tbl =
  1148. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1149. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  1150. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1151. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1152. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1153. u8 start_action;
  1154. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1155. u8 tx_chains_num = num_of_ant(valid_tx_ant);
  1156. u8 update_search_tbl_counter = 0;
  1157. int ret;
  1158. switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1159. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1160. /* nothing */
  1161. break;
  1162. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1163. /* avoid switching to antenna B but allow MIMO */
  1164. if (tbl->action == IWL_SISO_SWITCH_ANTENNA &&
  1165. tbl->ant_type == ANT_A)
  1166. tbl->action = IWL_SISO_SWITCH_MIMO2;
  1167. break;
  1168. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1169. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1170. /* A - avoid antenna B and MIMO. B - switch to A */
  1171. if (tbl->ant_type == ANT_A)
  1172. valid_tx_ant =
  1173. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  1174. tbl->action = IWL_SISO_SWITCH_ANTENNA;
  1175. break;
  1176. default:
  1177. IWL_ERR(mvm, "Invalid BT load %d",
  1178. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
  1179. break;
  1180. }
  1181. start_action = tbl->action;
  1182. while (1) {
  1183. lq_sta->action_counter++;
  1184. switch (tbl->action) {
  1185. case IWL_SISO_SWITCH_ANTENNA:
  1186. IWL_DEBUG_RATE(mvm, "LQ: SISO toggle Antenna\n");
  1187. if (tx_chains_num <= 1)
  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. rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
  1246. if (tbl->action == start_action)
  1247. break;
  1248. }
  1249. search_tbl->lq_type = LQ_NONE;
  1250. return 0;
  1251. out:
  1252. lq_sta->search_better_tbl = 1;
  1253. rs_move_next_action(tbl, IWL_SISO_LAST_ACTION);
  1254. if (update_search_tbl_counter)
  1255. search_tbl->action = tbl->action;
  1256. return 0;
  1257. }
  1258. /*
  1259. * Try to switch to new modulation mode from MIMO2
  1260. */
  1261. static int rs_move_mimo2_to_other(struct iwl_mvm *mvm,
  1262. struct iwl_lq_sta *lq_sta,
  1263. struct ieee80211_sta *sta, int index)
  1264. {
  1265. s8 is_green = lq_sta->is_green;
  1266. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1267. struct iwl_scale_tbl_info *search_tbl =
  1268. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1269. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1270. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1271. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1272. u8 start_action;
  1273. u8 valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1274. u8 update_search_tbl_counter = 0;
  1275. int ret;
  1276. switch (BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1277. case IWL_BT_COEX_TRAFFIC_LOAD_NONE:
  1278. /* nothing */
  1279. break;
  1280. case IWL_BT_COEX_TRAFFIC_LOAD_HIGH:
  1281. case IWL_BT_COEX_TRAFFIC_LOAD_CONTINUOUS:
  1282. /* avoid antenna B and MIMO */
  1283. if (tbl->action != IWL_MIMO2_SWITCH_SISO_A)
  1284. tbl->action = IWL_MIMO2_SWITCH_SISO_A;
  1285. break;
  1286. case IWL_BT_COEX_TRAFFIC_LOAD_LOW:
  1287. /* avoid antenna B unless MIMO */
  1288. if (tbl->action == IWL_MIMO2_SWITCH_SISO_B)
  1289. tbl->action = IWL_MIMO2_SWITCH_SISO_A;
  1290. break;
  1291. default:
  1292. IWL_ERR(mvm, "Invalid BT load %d",
  1293. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD));
  1294. break;
  1295. }
  1296. start_action = tbl->action;
  1297. while (1) {
  1298. lq_sta->action_counter++;
  1299. switch (tbl->action) {
  1300. case IWL_MIMO2_SWITCH_SISO_A:
  1301. case IWL_MIMO2_SWITCH_SISO_B:
  1302. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 switch to SISO\n");
  1303. /* Set up new search table for SISO */
  1304. memcpy(search_tbl, tbl, sz);
  1305. if (tbl->action == IWL_MIMO2_SWITCH_SISO_A)
  1306. search_tbl->ant_type = ANT_A;
  1307. else /* tbl->action == IWL_MIMO2_SWITCH_SISO_B */
  1308. search_tbl->ant_type = ANT_B;
  1309. if (!rs_is_valid_ant(valid_tx_ant,
  1310. search_tbl->ant_type))
  1311. break;
  1312. ret = rs_switch_to_siso(mvm, lq_sta, sta,
  1313. search_tbl, index);
  1314. if (!ret)
  1315. goto out;
  1316. break;
  1317. case IWL_MIMO2_SWITCH_GI:
  1318. if (!tbl->is_ht40 && !(ht_cap->cap &
  1319. IEEE80211_HT_CAP_SGI_20))
  1320. break;
  1321. if (tbl->is_ht40 && !(ht_cap->cap &
  1322. IEEE80211_HT_CAP_SGI_40))
  1323. break;
  1324. IWL_DEBUG_RATE(mvm, "LQ: MIMO2 toggle SGI/NGI\n");
  1325. /* Set up new search table for MIMO2 */
  1326. memcpy(search_tbl, tbl, sz);
  1327. search_tbl->is_SGI = !tbl->is_SGI;
  1328. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1329. /*
  1330. * If active table already uses the fastest possible
  1331. * modulation (dual stream with short guard interval),
  1332. * and it's working well, there's no need to look
  1333. * for a better type of modulation!
  1334. */
  1335. if (tbl->is_SGI) {
  1336. s32 tpt = lq_sta->last_tpt / 100;
  1337. if (tpt >= search_tbl->expected_tpt[index])
  1338. break;
  1339. }
  1340. search_tbl->current_rate =
  1341. rate_n_flags_from_tbl(mvm, search_tbl,
  1342. index, is_green);
  1343. update_search_tbl_counter = 1;
  1344. goto out;
  1345. default:
  1346. WARN_ON_ONCE(1);
  1347. }
  1348. rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
  1349. if (tbl->action == start_action)
  1350. break;
  1351. }
  1352. search_tbl->lq_type = LQ_NONE;
  1353. return 0;
  1354. out:
  1355. lq_sta->search_better_tbl = 1;
  1356. rs_move_next_action(tbl, IWL_MIMO2_LAST_ACTION);
  1357. if (update_search_tbl_counter)
  1358. search_tbl->action = tbl->action;
  1359. return 0;
  1360. }
  1361. /*
  1362. * Check whether we should continue using same modulation mode, or
  1363. * begin search for a new mode, based on:
  1364. * 1) # tx successes or failures while using this mode
  1365. * 2) # times calling this function
  1366. * 3) elapsed time in this mode (not used, for now)
  1367. */
  1368. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
  1369. {
  1370. struct iwl_scale_tbl_info *tbl;
  1371. int i;
  1372. int active_tbl;
  1373. int flush_interval_passed = 0;
  1374. struct iwl_mvm *mvm;
  1375. mvm = lq_sta->drv;
  1376. active_tbl = lq_sta->active_tbl;
  1377. tbl = &(lq_sta->lq_info[active_tbl]);
  1378. /* If we've been disallowing search, see if we should now allow it */
  1379. if (lq_sta->stay_in_tbl) {
  1380. /* Elapsed time using current modulation mode */
  1381. if (lq_sta->flush_timer)
  1382. flush_interval_passed =
  1383. time_after(jiffies,
  1384. (unsigned long)(lq_sta->flush_timer +
  1385. IWL_RATE_SCALE_FLUSH_INTVL));
  1386. /*
  1387. * Check if we should allow search for new modulation mode.
  1388. * If many frames have failed or succeeded, or we've used
  1389. * this same modulation for a long time, allow search, and
  1390. * reset history stats that keep track of whether we should
  1391. * allow a new search. Also (below) reset all bitmaps and
  1392. * stats in active history.
  1393. */
  1394. if (force_search ||
  1395. (lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1396. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1397. ((!lq_sta->search_better_tbl) &&
  1398. (lq_sta->flush_timer) && (flush_interval_passed))) {
  1399. IWL_DEBUG_RATE(mvm,
  1400. "LQ: stay is expired %d %d %d\n",
  1401. lq_sta->total_failed,
  1402. lq_sta->total_success,
  1403. flush_interval_passed);
  1404. /* Allow search for new mode */
  1405. lq_sta->stay_in_tbl = 0; /* only place reset */
  1406. lq_sta->total_failed = 0;
  1407. lq_sta->total_success = 0;
  1408. lq_sta->flush_timer = 0;
  1409. /*
  1410. * Else if we've used this modulation mode enough repetitions
  1411. * (regardless of elapsed time or success/failure), reset
  1412. * history bitmaps and rate-specific stats for all rates in
  1413. * active table.
  1414. */
  1415. } else {
  1416. lq_sta->table_count++;
  1417. if (lq_sta->table_count >=
  1418. lq_sta->table_count_limit) {
  1419. lq_sta->table_count = 0;
  1420. IWL_DEBUG_RATE(mvm,
  1421. "LQ: stay in table clear win\n");
  1422. for (i = 0; i < IWL_RATE_COUNT; i++)
  1423. rs_rate_scale_clear_window(
  1424. &(tbl->win[i]));
  1425. }
  1426. }
  1427. /* If transitioning to allow "search", reset all history
  1428. * bitmaps and stats in active table (this will become the new
  1429. * "search" table). */
  1430. if (!lq_sta->stay_in_tbl) {
  1431. for (i = 0; i < IWL_RATE_COUNT; i++)
  1432. rs_rate_scale_clear_window(&(tbl->win[i]));
  1433. }
  1434. }
  1435. }
  1436. /*
  1437. * setup rate table in uCode
  1438. */
  1439. static void rs_update_rate_tbl(struct iwl_mvm *mvm,
  1440. struct iwl_lq_sta *lq_sta,
  1441. struct iwl_scale_tbl_info *tbl,
  1442. int index, u8 is_green)
  1443. {
  1444. u32 rate;
  1445. /* Update uCode's rate table. */
  1446. rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
  1447. rs_fill_link_cmd(mvm, lq_sta, rate);
  1448. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
  1449. }
  1450. static u8 rs_get_tid(struct iwl_lq_sta *lq_data,
  1451. struct ieee80211_hdr *hdr)
  1452. {
  1453. u8 tid = IWL_MAX_TID_COUNT;
  1454. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1455. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1456. tid = qc[0] & 0xf;
  1457. }
  1458. if (unlikely(tid > IWL_MAX_TID_COUNT))
  1459. tid = IWL_MAX_TID_COUNT;
  1460. return tid;
  1461. }
  1462. /*
  1463. * Do rate scaling and search for new modulation mode.
  1464. */
  1465. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  1466. struct sk_buff *skb,
  1467. struct ieee80211_sta *sta,
  1468. struct iwl_lq_sta *lq_sta)
  1469. {
  1470. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1471. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1472. int low = IWL_RATE_INVALID;
  1473. int high = IWL_RATE_INVALID;
  1474. int index;
  1475. int i;
  1476. struct iwl_rate_scale_data *window = NULL;
  1477. int current_tpt = IWL_INVALID_VALUE;
  1478. int low_tpt = IWL_INVALID_VALUE;
  1479. int high_tpt = IWL_INVALID_VALUE;
  1480. u32 fail_count;
  1481. s8 scale_action = 0;
  1482. u16 rate_mask;
  1483. u8 update_lq = 0;
  1484. struct iwl_scale_tbl_info *tbl, *tbl1;
  1485. u16 rate_scale_index_msk = 0;
  1486. u8 is_green = 0;
  1487. u8 active_tbl = 0;
  1488. u8 done_search = 0;
  1489. u16 high_low;
  1490. s32 sr;
  1491. u8 tid = IWL_MAX_TID_COUNT;
  1492. struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
  1493. struct iwl_mvm_tid_data *tid_data;
  1494. IWL_DEBUG_RATE(mvm, "rate scale calculate new rate for skb\n");
  1495. /* Send management frames and NO_ACK data using lowest rate. */
  1496. /* TODO: this could probably be improved.. */
  1497. if (!ieee80211_is_data(hdr->frame_control) ||
  1498. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1499. return;
  1500. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1501. tid = rs_get_tid(lq_sta, hdr);
  1502. if ((tid != IWL_MAX_TID_COUNT) &&
  1503. (lq_sta->tx_agg_tid_en & (1 << tid))) {
  1504. tid_data = &sta_priv->tid_data[tid];
  1505. if (tid_data->state == IWL_AGG_OFF)
  1506. lq_sta->is_agg = 0;
  1507. else
  1508. lq_sta->is_agg = 1;
  1509. } else {
  1510. lq_sta->is_agg = 0;
  1511. }
  1512. /*
  1513. * Select rate-scale / modulation-mode table to work with in
  1514. * the rest of this function: "search" if searching for better
  1515. * modulation mode, or "active" if doing rate scaling within a mode.
  1516. */
  1517. if (!lq_sta->search_better_tbl)
  1518. active_tbl = lq_sta->active_tbl;
  1519. else
  1520. active_tbl = 1 - lq_sta->active_tbl;
  1521. tbl = &(lq_sta->lq_info[active_tbl]);
  1522. if (is_legacy(tbl->lq_type))
  1523. lq_sta->is_green = 0;
  1524. else
  1525. lq_sta->is_green = rs_use_green(sta);
  1526. is_green = lq_sta->is_green;
  1527. /* current tx rate */
  1528. index = lq_sta->last_txrate_idx;
  1529. IWL_DEBUG_RATE(mvm, "Rate scale index %d for type %d\n", index,
  1530. tbl->lq_type);
  1531. /* rates available for this association, and for modulation mode */
  1532. rate_mask = rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
  1533. IWL_DEBUG_RATE(mvm, "mask 0x%04X\n", rate_mask);
  1534. /* mask with station rate restriction */
  1535. if (is_legacy(tbl->lq_type)) {
  1536. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1537. /* supp_rates has no CCK bits in A mode */
  1538. rate_scale_index_msk = (u16) (rate_mask &
  1539. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  1540. else
  1541. rate_scale_index_msk = (u16) (rate_mask &
  1542. lq_sta->supp_rates);
  1543. } else {
  1544. rate_scale_index_msk = rate_mask;
  1545. }
  1546. if (!rate_scale_index_msk)
  1547. rate_scale_index_msk = rate_mask;
  1548. if (!((1 << index) & rate_scale_index_msk)) {
  1549. IWL_ERR(mvm, "Current Rate is not valid\n");
  1550. if (lq_sta->search_better_tbl) {
  1551. /* revert to active table if search table is not valid*/
  1552. tbl->lq_type = LQ_NONE;
  1553. lq_sta->search_better_tbl = 0;
  1554. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1555. /* get "active" rate info */
  1556. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1557. rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
  1558. }
  1559. return;
  1560. }
  1561. /* Get expected throughput table and history window for current rate */
  1562. if (!tbl->expected_tpt) {
  1563. IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
  1564. return;
  1565. }
  1566. /* force user max rate if set by user */
  1567. if ((lq_sta->max_rate_idx != -1) &&
  1568. (lq_sta->max_rate_idx < index)) {
  1569. index = lq_sta->max_rate_idx;
  1570. update_lq = 1;
  1571. window = &(tbl->win[index]);
  1572. goto lq_update;
  1573. }
  1574. window = &(tbl->win[index]);
  1575. /*
  1576. * If there is not enough history to calculate actual average
  1577. * throughput, keep analyzing results of more tx frames, without
  1578. * changing rate or mode (bypass most of the rest of this function).
  1579. * Set up new rate table in uCode only if old rate is not supported
  1580. * in current association (use new rate found above).
  1581. */
  1582. fail_count = window->counter - window->success_counter;
  1583. if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1584. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
  1585. IWL_DEBUG_RATE(mvm,
  1586. "LQ: still below TH. succ=%d total=%d for index %d\n",
  1587. window->success_counter, window->counter, index);
  1588. /* Can't calculate this yet; not enough history */
  1589. window->average_tpt = IWL_INVALID_VALUE;
  1590. /* Should we stay with this modulation mode,
  1591. * or search for a new one? */
  1592. rs_stay_in_table(lq_sta, false);
  1593. goto out;
  1594. }
  1595. /* Else we have enough samples; calculate estimate of
  1596. * actual average throughput */
  1597. if (window->average_tpt != ((window->success_ratio *
  1598. tbl->expected_tpt[index] + 64) / 128)) {
  1599. IWL_ERR(mvm,
  1600. "expected_tpt should have been calculated by now\n");
  1601. window->average_tpt = ((window->success_ratio *
  1602. tbl->expected_tpt[index] + 64) / 128);
  1603. }
  1604. /* If we are searching for better modulation mode, check success. */
  1605. if (lq_sta->search_better_tbl) {
  1606. /* If good success, continue using the "search" mode;
  1607. * no need to send new link quality command, since we're
  1608. * continuing to use the setup that we've been trying. */
  1609. if (window->average_tpt > lq_sta->last_tpt) {
  1610. IWL_DEBUG_RATE(mvm,
  1611. "LQ: SWITCHING TO NEW TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
  1612. window->success_ratio,
  1613. window->average_tpt,
  1614. lq_sta->last_tpt);
  1615. if (!is_legacy(tbl->lq_type))
  1616. lq_sta->enable_counter = 1;
  1617. /* Swap tables; "search" becomes "active" */
  1618. lq_sta->active_tbl = active_tbl;
  1619. current_tpt = window->average_tpt;
  1620. /* Else poor success; go back to mode in "active" table */
  1621. } else {
  1622. IWL_DEBUG_RATE(mvm,
  1623. "LQ: GOING BACK TO THE OLD TABLE suc=%d cur-tpt=%d old-tpt=%d\n",
  1624. window->success_ratio,
  1625. window->average_tpt,
  1626. lq_sta->last_tpt);
  1627. /* Nullify "search" table */
  1628. tbl->lq_type = LQ_NONE;
  1629. /* Revert to "active" table */
  1630. active_tbl = lq_sta->active_tbl;
  1631. tbl = &(lq_sta->lq_info[active_tbl]);
  1632. /* Revert to "active" rate and throughput info */
  1633. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1634. current_tpt = lq_sta->last_tpt;
  1635. /* Need to set up a new rate table in uCode */
  1636. update_lq = 1;
  1637. }
  1638. /* Either way, we've made a decision; modulation mode
  1639. * search is done, allow rate adjustment next time. */
  1640. lq_sta->search_better_tbl = 0;
  1641. done_search = 1; /* Don't switch modes below! */
  1642. goto lq_update;
  1643. }
  1644. /* (Else) not in search of better modulation mode, try for better
  1645. * starting rate, while staying in this mode. */
  1646. high_low = rs_get_adjacent_rate(mvm, index, rate_scale_index_msk,
  1647. tbl->lq_type);
  1648. low = high_low & 0xff;
  1649. high = (high_low >> 8) & 0xff;
  1650. /* If user set max rate, dont allow higher than user constrain */
  1651. if ((lq_sta->max_rate_idx != -1) &&
  1652. (lq_sta->max_rate_idx < high))
  1653. high = IWL_RATE_INVALID;
  1654. sr = window->success_ratio;
  1655. /* Collect measured throughputs for current and adjacent rates */
  1656. current_tpt = window->average_tpt;
  1657. if (low != IWL_RATE_INVALID)
  1658. low_tpt = tbl->win[low].average_tpt;
  1659. if (high != IWL_RATE_INVALID)
  1660. high_tpt = tbl->win[high].average_tpt;
  1661. scale_action = 0;
  1662. /* Too many failures, decrease rate */
  1663. if ((sr <= IWL_RATE_DECREASE_TH) || (current_tpt == 0)) {
  1664. IWL_DEBUG_RATE(mvm,
  1665. "decrease rate because of low success_ratio\n");
  1666. scale_action = -1;
  1667. /* No throughput measured yet for adjacent rates; try increase. */
  1668. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  1669. (high_tpt == IWL_INVALID_VALUE)) {
  1670. if (high != IWL_RATE_INVALID && sr >= IWL_RATE_INCREASE_TH)
  1671. scale_action = 1;
  1672. else if (low != IWL_RATE_INVALID)
  1673. scale_action = 0;
  1674. }
  1675. /* Both adjacent throughputs are measured, but neither one has better
  1676. * throughput; we're using the best rate, don't change it! */
  1677. else if ((low_tpt != IWL_INVALID_VALUE) &&
  1678. (high_tpt != IWL_INVALID_VALUE) &&
  1679. (low_tpt < current_tpt) &&
  1680. (high_tpt < current_tpt))
  1681. scale_action = 0;
  1682. /* At least one adjacent rate's throughput is measured,
  1683. * and may have better performance. */
  1684. else {
  1685. /* Higher adjacent rate's throughput is measured */
  1686. if (high_tpt != IWL_INVALID_VALUE) {
  1687. /* Higher rate has better throughput */
  1688. if (high_tpt > current_tpt &&
  1689. sr >= IWL_RATE_INCREASE_TH) {
  1690. scale_action = 1;
  1691. } else {
  1692. scale_action = 0;
  1693. }
  1694. /* Lower adjacent rate's throughput is measured */
  1695. } else if (low_tpt != IWL_INVALID_VALUE) {
  1696. /* Lower rate has better throughput */
  1697. if (low_tpt > current_tpt) {
  1698. IWL_DEBUG_RATE(mvm,
  1699. "decrease rate because of low tpt\n");
  1700. scale_action = -1;
  1701. } else if (sr >= IWL_RATE_INCREASE_TH) {
  1702. scale_action = 1;
  1703. }
  1704. }
  1705. }
  1706. /* Sanity check; asked for decrease, but success rate or throughput
  1707. * has been good at old rate. Don't change it. */
  1708. if ((scale_action == -1) && (low != IWL_RATE_INVALID) &&
  1709. ((sr > IWL_RATE_HIGH_TH) ||
  1710. (current_tpt > (100 * tbl->expected_tpt[low]))))
  1711. scale_action = 0;
  1712. if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
  1713. IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && (is_mimo(tbl->lq_type))) {
  1714. if (lq_sta->last_bt_traffic >
  1715. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1716. /*
  1717. * don't set scale_action, don't want to scale up if
  1718. * the rate scale doesn't otherwise think that is a
  1719. * good idea.
  1720. */
  1721. } else if (lq_sta->last_bt_traffic <=
  1722. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD)) {
  1723. scale_action = -1;
  1724. }
  1725. }
  1726. lq_sta->last_bt_traffic =
  1727. BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD);
  1728. if ((BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >=
  1729. IWL_BT_COEX_TRAFFIC_LOAD_HIGH) && is_mimo(tbl->lq_type)) {
  1730. /* search for a new modulation */
  1731. rs_stay_in_table(lq_sta, true);
  1732. goto lq_update;
  1733. }
  1734. switch (scale_action) {
  1735. case -1:
  1736. /* Decrease starting rate, update uCode's rate table */
  1737. if (low != IWL_RATE_INVALID) {
  1738. update_lq = 1;
  1739. index = low;
  1740. }
  1741. break;
  1742. case 1:
  1743. /* Increase starting rate, update uCode's rate table */
  1744. if (high != IWL_RATE_INVALID) {
  1745. update_lq = 1;
  1746. index = high;
  1747. }
  1748. break;
  1749. case 0:
  1750. /* No change */
  1751. default:
  1752. break;
  1753. }
  1754. IWL_DEBUG_RATE(mvm,
  1755. "choose rate scale index %d action %d low %d high %d type %d\n",
  1756. index, scale_action, low, high, tbl->lq_type);
  1757. lq_update:
  1758. /* Replace uCode's rate table for the destination station. */
  1759. if (update_lq)
  1760. rs_update_rate_tbl(mvm, lq_sta, tbl, index, is_green);
  1761. rs_stay_in_table(lq_sta, false);
  1762. /*
  1763. * Search for new modulation mode if we're:
  1764. * 1) Not changing rates right now
  1765. * 2) Not just finishing up a search
  1766. * 3) Allowing a new search
  1767. */
  1768. if (!update_lq && !done_search &&
  1769. !lq_sta->stay_in_tbl && window->counter) {
  1770. /* Save current throughput to compare with "search" throughput*/
  1771. lq_sta->last_tpt = current_tpt;
  1772. /* Select a new "search" modulation mode to try.
  1773. * If one is found, set up the new "search" table. */
  1774. if (is_legacy(tbl->lq_type))
  1775. rs_move_legacy_other(mvm, lq_sta, sta, index);
  1776. else if (is_siso(tbl->lq_type))
  1777. rs_move_siso_to_other(mvm, lq_sta, sta, index);
  1778. else if (is_mimo2(tbl->lq_type))
  1779. rs_move_mimo2_to_other(mvm, lq_sta, sta, index);
  1780. else
  1781. WARN_ON_ONCE(1);
  1782. /* If new "search" mode was selected, set up in uCode table */
  1783. if (lq_sta->search_better_tbl) {
  1784. /* Access the "search" table, clear its history. */
  1785. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1786. for (i = 0; i < IWL_RATE_COUNT; i++)
  1787. rs_rate_scale_clear_window(&(tbl->win[i]));
  1788. /* Use new "search" start rate */
  1789. index = iwl_hwrate_to_plcp_idx(tbl->current_rate);
  1790. IWL_DEBUG_RATE(mvm,
  1791. "Switch current mcs: %X index: %d\n",
  1792. tbl->current_rate, index);
  1793. rs_fill_link_cmd(mvm, lq_sta, tbl->current_rate);
  1794. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_ASYNC, false);
  1795. } else {
  1796. done_search = 1;
  1797. }
  1798. }
  1799. if (done_search && !lq_sta->stay_in_tbl) {
  1800. /* If the "active" (non-search) mode was legacy,
  1801. * and we've tried switching antennas,
  1802. * but we haven't been able to try HT modes (not available),
  1803. * stay with best antenna legacy modulation for a while
  1804. * before next round of mode comparisons. */
  1805. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1806. if (is_legacy(tbl1->lq_type) && !sta->ht_cap.ht_supported &&
  1807. lq_sta->action_counter > tbl1->max_search) {
  1808. IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
  1809. rs_set_stay_in_table(mvm, 1, lq_sta);
  1810. }
  1811. /* If we're in an HT mode, and all 3 mode switch actions
  1812. * have been tried and compared, stay in this best modulation
  1813. * mode for a while before next round of mode comparisons. */
  1814. if (lq_sta->enable_counter &&
  1815. (lq_sta->action_counter >= tbl1->max_search)) {
  1816. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  1817. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  1818. (tid != IWL_MAX_TID_COUNT)) {
  1819. tid_data = &sta_priv->tid_data[tid];
  1820. if (tid_data->state == IWL_AGG_OFF) {
  1821. IWL_DEBUG_RATE(mvm,
  1822. "try to aggregate tid %d\n",
  1823. tid);
  1824. rs_tl_turn_on_agg(mvm, tid,
  1825. lq_sta, sta);
  1826. }
  1827. }
  1828. rs_set_stay_in_table(mvm, 0, lq_sta);
  1829. }
  1830. }
  1831. out:
  1832. tbl->current_rate = rate_n_flags_from_tbl(mvm, tbl, index, is_green);
  1833. lq_sta->last_txrate_idx = index;
  1834. }
  1835. /**
  1836. * rs_initialize_lq - Initialize a station's hardware rate table
  1837. *
  1838. * The uCode's station table contains a table of fallback rates
  1839. * for automatic fallback during transmission.
  1840. *
  1841. * NOTE: This sets up a default set of values. These will be replaced later
  1842. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  1843. * rc80211_simple.
  1844. *
  1845. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  1846. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  1847. * which requires station table entry to exist).
  1848. */
  1849. static void rs_initialize_lq(struct iwl_mvm *mvm,
  1850. struct ieee80211_sta *sta,
  1851. struct iwl_lq_sta *lq_sta,
  1852. enum ieee80211_band band)
  1853. {
  1854. struct iwl_scale_tbl_info *tbl;
  1855. int rate_idx;
  1856. int i;
  1857. u32 rate;
  1858. u8 use_green = rs_use_green(sta);
  1859. u8 active_tbl = 0;
  1860. u8 valid_tx_ant;
  1861. if (!sta || !lq_sta)
  1862. return;
  1863. i = lq_sta->last_txrate_idx;
  1864. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  1865. if (!lq_sta->search_better_tbl)
  1866. active_tbl = lq_sta->active_tbl;
  1867. else
  1868. active_tbl = 1 - lq_sta->active_tbl;
  1869. tbl = &(lq_sta->lq_info[active_tbl]);
  1870. if ((i < 0) || (i >= IWL_RATE_COUNT))
  1871. i = 0;
  1872. rate = iwl_rates[i].plcp;
  1873. tbl->ant_type = first_antenna(valid_tx_ant);
  1874. rate |= tbl->ant_type << RATE_MCS_ANT_POS;
  1875. if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
  1876. rate |= RATE_MCS_CCK_MSK;
  1877. rs_get_tbl_info_from_mcs(rate, band, tbl, &rate_idx);
  1878. if (!rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
  1879. rs_toggle_antenna(valid_tx_ant, &rate, tbl);
  1880. rate = rate_n_flags_from_tbl(mvm, tbl, rate_idx, use_green);
  1881. tbl->current_rate = rate;
  1882. rs_set_expected_tpt_table(lq_sta, tbl);
  1883. rs_fill_link_cmd(NULL, lq_sta, rate);
  1884. /* TODO restore station should remember the lq cmd */
  1885. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, CMD_SYNC, true);
  1886. }
  1887. static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
  1888. struct ieee80211_tx_rate_control *txrc)
  1889. {
  1890. struct sk_buff *skb = txrc->skb;
  1891. struct ieee80211_supported_band *sband = txrc->sband;
  1892. struct iwl_op_mode *op_mode __maybe_unused =
  1893. (struct iwl_op_mode *)mvm_r;
  1894. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  1895. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1896. struct iwl_lq_sta *lq_sta = mvm_sta;
  1897. int rate_idx;
  1898. IWL_DEBUG_RATE_LIMIT(mvm, "rate scale calculate new rate for skb\n");
  1899. /* Get max rate if user set max rate */
  1900. if (lq_sta) {
  1901. lq_sta->max_rate_idx = txrc->max_rate_idx;
  1902. if ((sband->band == IEEE80211_BAND_5GHZ) &&
  1903. (lq_sta->max_rate_idx != -1))
  1904. lq_sta->max_rate_idx += IWL_FIRST_OFDM_RATE;
  1905. if ((lq_sta->max_rate_idx < 0) ||
  1906. (lq_sta->max_rate_idx >= IWL_RATE_COUNT))
  1907. lq_sta->max_rate_idx = -1;
  1908. }
  1909. /* Treat uninitialized rate scaling data same as non-existing. */
  1910. if (lq_sta && !lq_sta->drv) {
  1911. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  1912. mvm_sta = NULL;
  1913. }
  1914. /* Send management frames and NO_ACK data using lowest rate. */
  1915. if (rate_control_send_low(sta, mvm_sta, txrc))
  1916. return;
  1917. rate_idx = lq_sta->last_txrate_idx;
  1918. if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
  1919. rate_idx -= IWL_FIRST_OFDM_RATE;
  1920. /* 6M and 9M shared same MCS index */
  1921. rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
  1922. WARN_ON_ONCE(rs_extract_rate(lq_sta->last_rate_n_flags) >=
  1923. IWL_RATE_MIMO3_6M_PLCP);
  1924. if (rs_extract_rate(lq_sta->last_rate_n_flags) >=
  1925. IWL_RATE_MIMO2_6M_PLCP)
  1926. rate_idx = rate_idx + MCS_INDEX_PER_STREAM;
  1927. info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  1928. if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
  1929. info->control.rates[0].flags |= IEEE80211_TX_RC_SHORT_GI;
  1930. if (lq_sta->last_rate_n_flags & RATE_MCS_CHAN_WIDTH_40) /* TODO */
  1931. info->control.rates[0].flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  1932. if (lq_sta->last_rate_n_flags & RATE_HT_MCS_GF_MSK)
  1933. info->control.rates[0].flags |= IEEE80211_TX_RC_GREEN_FIELD;
  1934. } else {
  1935. /* Check for invalid rates */
  1936. if ((rate_idx < 0) || (rate_idx >= IWL_RATE_COUNT_LEGACY) ||
  1937. ((sband->band == IEEE80211_BAND_5GHZ) &&
  1938. (rate_idx < IWL_FIRST_OFDM_RATE)))
  1939. rate_idx = rate_lowest_index(sband, sta);
  1940. /* On valid 5 GHz rate, adjust index */
  1941. else if (sband->band == IEEE80211_BAND_5GHZ)
  1942. rate_idx -= IWL_FIRST_OFDM_RATE;
  1943. info->control.rates[0].flags = 0;
  1944. }
  1945. info->control.rates[0].idx = rate_idx;
  1946. info->control.rates[0].count = 1;
  1947. }
  1948. static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
  1949. gfp_t gfp)
  1950. {
  1951. struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  1952. struct iwl_op_mode *op_mode __maybe_unused =
  1953. (struct iwl_op_mode *)mvm_rate;
  1954. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  1955. IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
  1956. return &sta_priv->lq_sta;
  1957. }
  1958. /*
  1959. * Called after adding a new station to initialize rate scaling
  1960. */
  1961. void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1962. enum ieee80211_band band)
  1963. {
  1964. int i, j;
  1965. struct ieee80211_hw *hw = mvm->hw;
  1966. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1967. struct iwl_mvm_sta *sta_priv;
  1968. struct iwl_lq_sta *lq_sta;
  1969. struct ieee80211_supported_band *sband;
  1970. unsigned long supp; /* must be unsigned long for for_each_set_bit */
  1971. sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  1972. lq_sta = &sta_priv->lq_sta;
  1973. sband = hw->wiphy->bands[band];
  1974. lq_sta->lq.sta_id = sta_priv->sta_id;
  1975. for (j = 0; j < LQ_SIZE; j++)
  1976. for (i = 0; i < IWL_RATE_COUNT; i++)
  1977. rs_rate_scale_clear_window(&lq_sta->lq_info[j].win[i]);
  1978. lq_sta->flush_timer = 0;
  1979. lq_sta->supp_rates = sta->supp_rates[sband->band];
  1980. IWL_DEBUG_RATE(mvm,
  1981. "LQ: *** rate scale station global init for station %d ***\n",
  1982. sta_priv->sta_id);
  1983. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  1984. * the lowest or the highest rate.. Could consider using RSSI from
  1985. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  1986. * after assoc.. */
  1987. lq_sta->max_rate_idx = -1;
  1988. lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
  1989. lq_sta->is_green = rs_use_green(sta);
  1990. lq_sta->band = sband->band;
  1991. /*
  1992. * active legacy rates as per supported rates bitmap
  1993. */
  1994. supp = sta->supp_rates[sband->band];
  1995. lq_sta->active_legacy_rate = 0;
  1996. for_each_set_bit(i, &supp, BITS_PER_LONG)
  1997. lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
  1998. /*
  1999. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  2000. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  2001. */
  2002. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2003. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2004. lq_sta->active_siso_rate &= ~((u16)0x2);
  2005. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2006. /* Same here */
  2007. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2008. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2009. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2010. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2011. IWL_DEBUG_RATE(mvm,
  2012. "SISO-RATE=%X MIMO2-RATE=%X\n",
  2013. lq_sta->active_siso_rate,
  2014. lq_sta->active_mimo2_rate);
  2015. /* These values will be overridden later */
  2016. lq_sta->lq.single_stream_ant_msk =
  2017. first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  2018. lq_sta->lq.dual_stream_ant_msk =
  2019. iwl_fw_valid_tx_ant(mvm->fw) &
  2020. ~first_antenna(iwl_fw_valid_tx_ant(mvm->fw));
  2021. if (!lq_sta->lq.dual_stream_ant_msk) {
  2022. lq_sta->lq.dual_stream_ant_msk = ANT_AB;
  2023. } else if (num_of_ant(iwl_fw_valid_tx_ant(mvm->fw)) == 2) {
  2024. lq_sta->lq.dual_stream_ant_msk =
  2025. iwl_fw_valid_tx_ant(mvm->fw);
  2026. }
  2027. /* as default allow aggregation for all tids */
  2028. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2029. lq_sta->drv = mvm;
  2030. /* Set last_txrate_idx to lowest rate */
  2031. lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
  2032. if (sband->band == IEEE80211_BAND_5GHZ)
  2033. lq_sta->last_txrate_idx += IWL_FIRST_OFDM_RATE;
  2034. lq_sta->is_agg = 0;
  2035. #ifdef CONFIG_MAC80211_DEBUGFS
  2036. lq_sta->dbg_fixed_rate = 0;
  2037. #endif
  2038. rs_initialize_lq(mvm, sta, lq_sta, band);
  2039. }
  2040. static void rs_fill_link_cmd(struct iwl_mvm *mvm,
  2041. struct iwl_lq_sta *lq_sta, u32 new_rate)
  2042. {
  2043. struct iwl_scale_tbl_info tbl_type;
  2044. int index = 0;
  2045. int rate_idx;
  2046. int repeat_rate = 0;
  2047. u8 ant_toggle_cnt = 0;
  2048. u8 use_ht_possible = 1;
  2049. u8 valid_tx_ant = 0;
  2050. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2051. /* Override starting rate (index 0) if needed for debug purposes */
  2052. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2053. /* Interpret new_rate (rate_n_flags) */
  2054. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
  2055. &tbl_type, &rate_idx);
  2056. /* How many times should we repeat the initial rate? */
  2057. if (is_legacy(tbl_type.lq_type)) {
  2058. ant_toggle_cnt = 1;
  2059. repeat_rate = IWL_NUMBER_TRY;
  2060. } else {
  2061. repeat_rate = min(IWL_HT_NUMBER_TRY,
  2062. LINK_QUAL_AGG_DISABLE_START_DEF - 1);
  2063. }
  2064. lq_cmd->mimo_delim = is_mimo(tbl_type.lq_type) ? 1 : 0;
  2065. /* Fill 1st table entry (index 0) */
  2066. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2067. if (num_of_ant(tbl_type.ant_type) == 1)
  2068. lq_cmd->single_stream_ant_msk = tbl_type.ant_type;
  2069. else if (num_of_ant(tbl_type.ant_type) == 2)
  2070. lq_cmd->dual_stream_ant_msk = tbl_type.ant_type;
  2071. /* otherwise we don't modify the existing value */
  2072. index++;
  2073. repeat_rate--;
  2074. if (mvm)
  2075. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2076. /* Fill rest of rate table */
  2077. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  2078. /* Repeat initial/next rate.
  2079. * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
  2080. * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
  2081. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  2082. if (is_legacy(tbl_type.lq_type)) {
  2083. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2084. ant_toggle_cnt++;
  2085. else if (mvm &&
  2086. rs_toggle_antenna(valid_tx_ant,
  2087. &new_rate, &tbl_type))
  2088. ant_toggle_cnt = 1;
  2089. }
  2090. /* Override next rate if needed for debug purposes */
  2091. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2092. /* Fill next table entry */
  2093. lq_cmd->rs_table[index] =
  2094. cpu_to_le32(new_rate);
  2095. repeat_rate--;
  2096. index++;
  2097. }
  2098. rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
  2099. &rate_idx);
  2100. /* Indicate to uCode which entries might be MIMO.
  2101. * If initial rate was MIMO, this will finally end up
  2102. * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2103. if (is_mimo(tbl_type.lq_type))
  2104. lq_cmd->mimo_delim = index;
  2105. /* Get next rate */
  2106. new_rate = rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
  2107. use_ht_possible);
  2108. /* How many times should we repeat the next rate? */
  2109. if (is_legacy(tbl_type.lq_type)) {
  2110. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2111. ant_toggle_cnt++;
  2112. else if (mvm &&
  2113. rs_toggle_antenna(valid_tx_ant,
  2114. &new_rate, &tbl_type))
  2115. ant_toggle_cnt = 1;
  2116. repeat_rate = IWL_NUMBER_TRY;
  2117. } else {
  2118. repeat_rate = IWL_HT_NUMBER_TRY;
  2119. }
  2120. /* Don't allow HT rates after next pass.
  2121. * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  2122. use_ht_possible = 0;
  2123. /* Override next rate if needed for debug purposes */
  2124. rs_dbgfs_set_mcs(lq_sta, &new_rate);
  2125. /* Fill next table entry */
  2126. lq_cmd->rs_table[index] = cpu_to_le32(new_rate);
  2127. index++;
  2128. repeat_rate--;
  2129. }
  2130. lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2131. lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2132. lq_cmd->agg_time_limit =
  2133. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2134. /*
  2135. * overwrite if needed, pass aggregation time limit
  2136. * to uCode in uSec - This is racy - but heh, at least it helps...
  2137. */
  2138. if (mvm && BT_MBOX_MSG(&mvm->last_bt_notif, 3, TRAFFIC_LOAD) >= 2)
  2139. lq_cmd->agg_time_limit = cpu_to_le16(1200);
  2140. }
  2141. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2142. {
  2143. return hw->priv;
  2144. }
  2145. /* rate scale requires free function to be implemented */
  2146. static void rs_free(void *mvm_rate)
  2147. {
  2148. return;
  2149. }
  2150. static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
  2151. void *mvm_sta)
  2152. {
  2153. struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
  2154. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2155. IWL_DEBUG_RATE(mvm, "enter\n");
  2156. IWL_DEBUG_RATE(mvm, "leave\n");
  2157. }
  2158. #ifdef CONFIG_MAC80211_DEBUGFS
  2159. static void rs_dbgfs_set_mcs(struct iwl_lq_sta *lq_sta,
  2160. u32 *rate_n_flags)
  2161. {
  2162. struct iwl_mvm *mvm;
  2163. u8 valid_tx_ant;
  2164. u8 ant_sel_tx;
  2165. mvm = lq_sta->drv;
  2166. valid_tx_ant = iwl_fw_valid_tx_ant(mvm->fw);
  2167. if (lq_sta->dbg_fixed_rate) {
  2168. ant_sel_tx =
  2169. ((lq_sta->dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK)
  2170. >> RATE_MCS_ANT_POS);
  2171. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2172. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2173. IWL_DEBUG_RATE(mvm, "Fixed rate ON\n");
  2174. } else {
  2175. lq_sta->dbg_fixed_rate = 0;
  2176. IWL_ERR(mvm,
  2177. "Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2178. ant_sel_tx, valid_tx_ant);
  2179. IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
  2180. }
  2181. } else {
  2182. IWL_DEBUG_RATE(mvm, "Fixed rate OFF\n");
  2183. }
  2184. }
  2185. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2186. const char __user *user_buf, size_t count, loff_t *ppos)
  2187. {
  2188. struct iwl_lq_sta *lq_sta = file->private_data;
  2189. struct iwl_mvm *mvm;
  2190. char buf[64];
  2191. size_t buf_size;
  2192. u32 parsed_rate;
  2193. mvm = lq_sta->drv;
  2194. memset(buf, 0, sizeof(buf));
  2195. buf_size = min(count, sizeof(buf) - 1);
  2196. if (copy_from_user(buf, user_buf, buf_size))
  2197. return -EFAULT;
  2198. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2199. lq_sta->dbg_fixed_rate = parsed_rate;
  2200. else
  2201. lq_sta->dbg_fixed_rate = 0;
  2202. rs_program_fix_rate(mvm, lq_sta);
  2203. return count;
  2204. }
  2205. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2206. char __user *user_buf, size_t count, loff_t *ppos)
  2207. {
  2208. char *buff;
  2209. int desc = 0;
  2210. int i = 0;
  2211. int index = 0;
  2212. ssize_t ret;
  2213. struct iwl_lq_sta *lq_sta = file->private_data;
  2214. struct iwl_mvm *mvm;
  2215. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2216. mvm = lq_sta->drv;
  2217. buff = kmalloc(1024, GFP_KERNEL);
  2218. if (!buff)
  2219. return -ENOMEM;
  2220. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2221. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2222. lq_sta->total_failed, lq_sta->total_success,
  2223. lq_sta->active_legacy_rate);
  2224. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2225. lq_sta->dbg_fixed_rate);
  2226. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2227. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_A) ? "ANT_A," : "",
  2228. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_B) ? "ANT_B," : "",
  2229. (iwl_fw_valid_tx_ant(mvm->fw) & ANT_C) ? "ANT_C" : "");
  2230. desc += sprintf(buff+desc, "lq type %s\n",
  2231. (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
  2232. if (is_Ht(tbl->lq_type)) {
  2233. desc += sprintf(buff+desc, " %s",
  2234. (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
  2235. desc += sprintf(buff+desc, " %s",
  2236. (tbl->is_ht40) ? "40MHz" : "20MHz");
  2237. desc += sprintf(buff+desc, " %s %s %s\n",
  2238. (tbl->is_SGI) ? "SGI" : "",
  2239. (lq_sta->is_green) ? "GF enabled" : "",
  2240. (lq_sta->is_agg) ? "AGG on" : "");
  2241. }
  2242. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2243. lq_sta->last_rate_n_flags);
  2244. desc += sprintf(buff+desc,
  2245. "general: flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2246. lq_sta->lq.flags,
  2247. lq_sta->lq.mimo_delim,
  2248. lq_sta->lq.single_stream_ant_msk,
  2249. lq_sta->lq.dual_stream_ant_msk);
  2250. desc += sprintf(buff+desc,
  2251. "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2252. le16_to_cpu(lq_sta->lq.agg_time_limit),
  2253. lq_sta->lq.agg_disable_start_th,
  2254. lq_sta->lq.agg_frame_cnt_limit);
  2255. desc += sprintf(buff+desc,
  2256. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2257. lq_sta->lq.initial_rate_index[0],
  2258. lq_sta->lq.initial_rate_index[1],
  2259. lq_sta->lq.initial_rate_index[2],
  2260. lq_sta->lq.initial_rate_index[3]);
  2261. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2262. index = iwl_hwrate_to_plcp_idx(
  2263. le32_to_cpu(lq_sta->lq.rs_table[i]));
  2264. if (is_legacy(tbl->lq_type)) {
  2265. desc += sprintf(buff+desc, " rate[%d] 0x%X %smbps\n",
  2266. i, le32_to_cpu(lq_sta->lq.rs_table[i]),
  2267. iwl_rate_mcs[index].mbps);
  2268. } else {
  2269. desc += sprintf(buff+desc,
  2270. " rate[%d] 0x%X %smbps (%s)\n",
  2271. i, le32_to_cpu(lq_sta->lq.rs_table[i]),
  2272. iwl_rate_mcs[index].mbps,
  2273. iwl_rate_mcs[index].mcs);
  2274. }
  2275. }
  2276. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2277. kfree(buff);
  2278. return ret;
  2279. }
  2280. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2281. .write = rs_sta_dbgfs_scale_table_write,
  2282. .read = rs_sta_dbgfs_scale_table_read,
  2283. .open = simple_open,
  2284. .llseek = default_llseek,
  2285. };
  2286. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2287. char __user *user_buf, size_t count, loff_t *ppos)
  2288. {
  2289. char *buff;
  2290. int desc = 0;
  2291. int i, j;
  2292. ssize_t ret;
  2293. struct iwl_lq_sta *lq_sta = file->private_data;
  2294. buff = kmalloc(1024, GFP_KERNEL);
  2295. if (!buff)
  2296. return -ENOMEM;
  2297. for (i = 0; i < LQ_SIZE; i++) {
  2298. desc += sprintf(buff+desc,
  2299. "%s type=%d SGI=%d HT40=%d DUP=0 GF=%d\n"
  2300. "rate=0x%X\n",
  2301. lq_sta->active_tbl == i ? "*" : "x",
  2302. lq_sta->lq_info[i].lq_type,
  2303. lq_sta->lq_info[i].is_SGI,
  2304. lq_sta->lq_info[i].is_ht40,
  2305. lq_sta->is_green,
  2306. lq_sta->lq_info[i].current_rate);
  2307. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2308. desc += sprintf(buff+desc,
  2309. "counter=%d success=%d %%=%d\n",
  2310. lq_sta->lq_info[i].win[j].counter,
  2311. lq_sta->lq_info[i].win[j].success_counter,
  2312. lq_sta->lq_info[i].win[j].success_ratio);
  2313. }
  2314. }
  2315. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2316. kfree(buff);
  2317. return ret;
  2318. }
  2319. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2320. .read = rs_sta_dbgfs_stats_table_read,
  2321. .open = simple_open,
  2322. .llseek = default_llseek,
  2323. };
  2324. static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
  2325. {
  2326. struct iwl_lq_sta *lq_sta = mvm_sta;
  2327. lq_sta->rs_sta_dbgfs_scale_table_file =
  2328. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2329. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2330. lq_sta->rs_sta_dbgfs_stats_table_file =
  2331. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  2332. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2333. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2334. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2335. &lq_sta->tx_agg_tid_en);
  2336. }
  2337. static void rs_remove_debugfs(void *mvm, void *mvm_sta)
  2338. {
  2339. struct iwl_lq_sta *lq_sta = mvm_sta;
  2340. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2341. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2342. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2343. }
  2344. #endif
  2345. /*
  2346. * Initialization of rate scaling information is done by driver after
  2347. * the station is added. Since mac80211 calls this function before a
  2348. * station is added we ignore it.
  2349. */
  2350. static void rs_rate_init_stub(void *mvm_r,
  2351. struct ieee80211_supported_band *sband,
  2352. struct cfg80211_chan_def *chandef,
  2353. struct ieee80211_sta *sta, void *mvm_sta)
  2354. {
  2355. }
  2356. static struct rate_control_ops rs_mvm_ops = {
  2357. .module = NULL,
  2358. .name = RS_NAME,
  2359. .tx_status = rs_tx_status,
  2360. .get_rate = rs_get_rate,
  2361. .rate_init = rs_rate_init_stub,
  2362. .alloc = rs_alloc,
  2363. .free = rs_free,
  2364. .alloc_sta = rs_alloc_sta,
  2365. .free_sta = rs_free_sta,
  2366. #ifdef CONFIG_MAC80211_DEBUGFS
  2367. .add_sta_debugfs = rs_add_debugfs,
  2368. .remove_sta_debugfs = rs_remove_debugfs,
  2369. #endif
  2370. };
  2371. int iwl_mvm_rate_control_register(void)
  2372. {
  2373. return ieee80211_rate_control_register(&rs_mvm_ops);
  2374. }
  2375. void iwl_mvm_rate_control_unregister(void)
  2376. {
  2377. ieee80211_rate_control_unregister(&rs_mvm_ops);
  2378. }
  2379. /**
  2380. * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
  2381. * Tx protection, according to this rquest and previous requests,
  2382. * and send the LQ command.
  2383. * @mvmsta: The station
  2384. * @enable: Enable Tx protection?
  2385. */
  2386. int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  2387. bool enable)
  2388. {
  2389. struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
  2390. lockdep_assert_held(&mvm->mutex);
  2391. if (enable) {
  2392. if (mvmsta->tx_protection == 0)
  2393. lq->flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
  2394. mvmsta->tx_protection++;
  2395. } else {
  2396. mvmsta->tx_protection--;
  2397. if (mvmsta->tx_protection == 0)
  2398. lq->flags &= ~LQ_FLAG_SET_STA_TLC_RTS_MSK;
  2399. }
  2400. return iwl_mvm_send_lq_cmd(mvm, lq, CMD_ASYNC, false);
  2401. }