iwl-agn-rs.c 74 KB

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