rs.c 80 KB

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