iwl-agn-rs.c 73 KB

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