iwl-agn-rs.c 88 KB

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