iwl-4965-rs.c 78 KB

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