iwl-agn-rs.c 84 KB

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