iwl-agn-rs.c 88 KB

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