iwl-agn-rs.c 88 KB

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