iwl-agn-rs.c 74 KB

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