iwl-4965-rs.c 79 KB

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