iwl-4965-rs.c 79 KB

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