iwl-agn-rs.c 89 KB

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