iwl-agn-rs.c 85 KB

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