4965-rs.c 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  17. *
  18. * The full GNU General Public License is included in this distribution in the
  19. * file called LICENSE.
  20. *
  21. * Contact Information:
  22. * Intel Linux Wireless <ilw@linux.intel.com>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. *****************************************************************************/
  26. #include <linux/kernel.h>
  27. #include <linux/init.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/slab.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 "common.h"
  36. #include "4965.h"
  37. #define IL4965_RS_NAME "iwl-4965-rs"
  38. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  39. #define IL_NUMBER_TRY 1
  40. #define IL_HT_NUMBER_TRY 3
  41. #define RATE_MAX_WINDOW 62 /* # tx in history win */
  42. #define RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
  43. #define RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  44. /* max allowed rate miss before sync LQ cmd */
  45. #define IL_MISSED_RATE_MAX 15
  46. /* max time to accum history 2 seconds */
  47. #define RATE_SCALE_FLUSH_INTVL (3*HZ)
  48. static u8 rs_ht_to_legacy[] = {
  49. RATE_6M_IDX, RATE_6M_IDX,
  50. RATE_6M_IDX, RATE_6M_IDX,
  51. RATE_6M_IDX,
  52. RATE_6M_IDX, RATE_9M_IDX,
  53. RATE_12M_IDX, RATE_18M_IDX,
  54. RATE_24M_IDX, RATE_36M_IDX,
  55. RATE_48M_IDX, RATE_54M_IDX
  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. #define IL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np) \
  68. [RATE_##r##M_IDX] = { RATE_##r##M_PLCP, \
  69. RATE_SISO_##s##M_PLCP, \
  70. RATE_MIMO2_##s##M_PLCP,\
  71. RATE_##r##M_IEEE, \
  72. RATE_##ip##M_IDX, \
  73. RATE_##in##M_IDX, \
  74. RATE_##rp##M_IDX, \
  75. RATE_##rn##M_IDX, \
  76. RATE_##pp##M_IDX, \
  77. RATE_##np##M_IDX }
  78. /*
  79. * Parameter order:
  80. * rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
  81. *
  82. * If there isn't a valid next or previous rate then INV is used which
  83. * maps to RATE_INVALID
  84. *
  85. */
  86. const struct il_rate_info il_rates[RATE_COUNT] = {
  87. IL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2), /* 1mbps */
  88. IL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5), /* 2mbps */
  89. IL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11), /*5.5mbps */
  90. IL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18), /* 11mbps */
  91. IL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11), /* 6mbps */
  92. IL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11), /* 9mbps */
  93. IL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18), /* 12mbps */
  94. IL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24), /* 18mbps */
  95. IL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36), /* 24mbps */
  96. IL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48), /* 36mbps */
  97. IL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54), /* 48mbps */
  98. IL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
  99. IL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
  100. };
  101. static int
  102. il4965_hwrate_to_plcp_idx(u32 rate_n_flags)
  103. {
  104. int idx = 0;
  105. /* HT rate format */
  106. if (rate_n_flags & RATE_MCS_HT_MSK) {
  107. idx = (rate_n_flags & 0xff);
  108. if (idx >= RATE_MIMO2_6M_PLCP)
  109. idx = idx - RATE_MIMO2_6M_PLCP;
  110. idx += IL_FIRST_OFDM_RATE;
  111. /* skip 9M not supported in ht */
  112. if (idx >= RATE_9M_IDX)
  113. idx += 1;
  114. if (idx >= IL_FIRST_OFDM_RATE && idx <= IL_LAST_OFDM_RATE)
  115. return idx;
  116. /* legacy rate format, search for match in table */
  117. } else {
  118. for (idx = 0; idx < ARRAY_SIZE(il_rates); idx++)
  119. if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
  120. return idx;
  121. }
  122. return -1;
  123. }
  124. static void il4965_rs_rate_scale_perform(struct il_priv *il,
  125. struct sk_buff *skb,
  126. struct ieee80211_sta *sta,
  127. struct il_lq_sta *lq_sta);
  128. static void il4965_rs_fill_link_cmd(struct il_priv *il,
  129. struct il_lq_sta *lq_sta, u32 rate_n_flags);
  130. static void il4965_rs_stay_in_table(struct il_lq_sta *lq_sta,
  131. bool force_search);
  132. #ifdef CONFIG_MAC80211_DEBUGFS
  133. static void il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta,
  134. u32 *rate_n_flags, int idx);
  135. #else
  136. static void
  137. il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
  138. {
  139. }
  140. #endif
  141. /**
  142. * The following tables contain the expected throughput metrics for all rates
  143. *
  144. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  145. *
  146. * where invalid entries are zeros.
  147. *
  148. * CCK rates are only valid in legacy table and will only be used in G
  149. * (2.4 GHz) band.
  150. */
  151. static s32 expected_tpt_legacy[RATE_COUNT] = {
  152. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0
  153. };
  154. static s32 expected_tpt_siso20MHz[4][RATE_COUNT] = {
  155. {0, 0, 0, 0, 42, 0, 76, 102, 124, 158, 183, 193, 202}, /* Norm */
  156. {0, 0, 0, 0, 46, 0, 82, 110, 132, 167, 192, 202, 210}, /* SGI */
  157. {0, 0, 0, 0, 48, 0, 93, 135, 176, 251, 319, 351, 381}, /* AGG */
  158. {0, 0, 0, 0, 53, 0, 102, 149, 193, 275, 348, 381, 413}, /* AGG+SGI */
  159. };
  160. static s32 expected_tpt_siso40MHz[4][RATE_COUNT] = {
  161. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257}, /* Norm */
  162. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264}, /* SGI */
  163. {0, 0, 0, 0, 96, 0, 182, 259, 328, 451, 553, 598, 640}, /* AGG */
  164. {0, 0, 0, 0, 106, 0, 199, 282, 357, 487, 593, 640, 683}, /* AGG+SGI */
  165. };
  166. static s32 expected_tpt_mimo2_20MHz[4][RATE_COUNT] = {
  167. {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250}, /* Norm */
  168. {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256}, /* SGI */
  169. {0, 0, 0, 0, 92, 0, 175, 250, 317, 436, 534, 578, 619}, /* AGG */
  170. {0, 0, 0, 0, 102, 0, 192, 273, 344, 470, 573, 619, 660}, /* AGG+SGI */
  171. };
  172. static s32 expected_tpt_mimo2_40MHz[4][RATE_COUNT] = {
  173. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289}, /* Norm */
  174. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293}, /* SGI */
  175. {0, 0, 0, 0, 180, 0, 327, 446, 545, 708, 828, 878, 922}, /* AGG */
  176. {0, 0, 0, 0, 197, 0, 355, 481, 584, 752, 872, 922, 966}, /* AGG+SGI */
  177. };
  178. /* mbps, mcs */
  179. static const struct il_rate_mcs_info il_rate_mcs[RATE_COUNT] = {
  180. {"1", "BPSK DSSS"},
  181. {"2", "QPSK DSSS"},
  182. {"5.5", "BPSK CCK"},
  183. {"11", "QPSK CCK"},
  184. {"6", "BPSK 1/2"},
  185. {"9", "BPSK 1/2"},
  186. {"12", "QPSK 1/2"},
  187. {"18", "QPSK 3/4"},
  188. {"24", "16QAM 1/2"},
  189. {"36", "16QAM 3/4"},
  190. {"48", "64QAM 2/3"},
  191. {"54", "64QAM 3/4"},
  192. {"60", "64QAM 5/6"},
  193. };
  194. #define MCS_IDX_PER_STREAM (8)
  195. static inline u8
  196. il4965_rs_extract_rate(u32 rate_n_flags)
  197. {
  198. return (u8) (rate_n_flags & 0xFF);
  199. }
  200. static void
  201. il4965_rs_rate_scale_clear_win(struct il_rate_scale_data *win)
  202. {
  203. win->data = 0;
  204. win->success_counter = 0;
  205. win->success_ratio = IL_INVALID_VALUE;
  206. win->counter = 0;
  207. win->average_tpt = IL_INVALID_VALUE;
  208. win->stamp = 0;
  209. }
  210. static inline u8
  211. il4965_rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  212. {
  213. return (ant_type & valid_antenna) == ant_type;
  214. }
  215. /*
  216. * removes the old data from the stats. All data that is older than
  217. * TID_MAX_TIME_DIFF, will be deleted.
  218. */
  219. static void
  220. il4965_rs_tl_rm_old_stats(struct il_traffic_load *tl, u32 curr_time)
  221. {
  222. /* The oldest age we want to keep */
  223. u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
  224. while (tl->queue_count && tl->time_stamp < oldest_time) {
  225. tl->total -= tl->packet_count[tl->head];
  226. tl->packet_count[tl->head] = 0;
  227. tl->time_stamp += TID_QUEUE_CELL_SPACING;
  228. tl->queue_count--;
  229. tl->head++;
  230. if (tl->head >= TID_QUEUE_MAX_SIZE)
  231. tl->head = 0;
  232. }
  233. }
  234. /*
  235. * increment traffic load value for tid and also remove
  236. * any old values if passed the certain time period
  237. */
  238. static u8
  239. il4965_rs_tl_add_packet(struct il_lq_sta *lq_data, struct ieee80211_hdr *hdr)
  240. {
  241. u32 curr_time = jiffies_to_msecs(jiffies);
  242. u32 time_diff;
  243. s32 idx;
  244. struct il_traffic_load *tl = NULL;
  245. u8 tid;
  246. if (ieee80211_is_data_qos(hdr->frame_control)) {
  247. u8 *qc = ieee80211_get_qos_ctl(hdr);
  248. tid = qc[0] & 0xf;
  249. } else
  250. return MAX_TID_COUNT;
  251. if (unlikely(tid >= TID_MAX_LOAD_COUNT))
  252. return MAX_TID_COUNT;
  253. tl = &lq_data->load[tid];
  254. curr_time -= curr_time % TID_ROUND_VALUE;
  255. /* Happens only for the first packet. Initialize the data */
  256. if (!(tl->queue_count)) {
  257. tl->total = 1;
  258. tl->time_stamp = curr_time;
  259. tl->queue_count = 1;
  260. tl->head = 0;
  261. tl->packet_count[0] = 1;
  262. return MAX_TID_COUNT;
  263. }
  264. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  265. idx = time_diff / TID_QUEUE_CELL_SPACING;
  266. /* The history is too long: remove data that is older than */
  267. /* TID_MAX_TIME_DIFF */
  268. if (idx >= TID_QUEUE_MAX_SIZE)
  269. il4965_rs_tl_rm_old_stats(tl, curr_time);
  270. idx = (tl->head + idx) % TID_QUEUE_MAX_SIZE;
  271. tl->packet_count[idx] = tl->packet_count[idx] + 1;
  272. tl->total = tl->total + 1;
  273. if ((idx + 1) > tl->queue_count)
  274. tl->queue_count = idx + 1;
  275. return tid;
  276. }
  277. /*
  278. get the traffic load value for tid
  279. */
  280. static u32
  281. il4965_rs_tl_get_load(struct il_lq_sta *lq_data, u8 tid)
  282. {
  283. u32 curr_time = jiffies_to_msecs(jiffies);
  284. u32 time_diff;
  285. s32 idx;
  286. struct il_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. idx = 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 (idx >= TID_QUEUE_MAX_SIZE)
  298. il4965_rs_tl_rm_old_stats(tl, curr_time);
  299. return tl->total;
  300. }
  301. static int
  302. il4965_rs_tl_turn_on_agg_for_tid(struct il_priv *il, struct il_lq_sta *lq_data,
  303. u8 tid, struct ieee80211_sta *sta)
  304. {
  305. int ret = -EAGAIN;
  306. u32 load;
  307. load = il4965_rs_tl_get_load(lq_data, tid);
  308. if (load > IL_AGG_LOAD_THRESHOLD) {
  309. D_HT("Starting Tx agg: STA: %pM tid: %d\n", sta->addr, tid);
  310. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  311. if (ret == -EAGAIN) {
  312. /*
  313. * driver and mac80211 is out of sync
  314. * this might be cause by reloading firmware
  315. * stop the tx ba session here
  316. */
  317. IL_ERR("Fail start Tx agg on tid: %d\n", tid);
  318. ieee80211_stop_tx_ba_session(sta, tid);
  319. }
  320. } else
  321. D_HT("Aggregation not enabled for tid %d because load = %u\n",
  322. tid, load);
  323. return ret;
  324. }
  325. static void
  326. il4965_rs_tl_turn_on_agg(struct il_priv *il, u8 tid, struct il_lq_sta *lq_data,
  327. struct ieee80211_sta *sta)
  328. {
  329. if (tid < TID_MAX_LOAD_COUNT)
  330. il4965_rs_tl_turn_on_agg_for_tid(il, lq_data, tid, sta);
  331. else
  332. IL_ERR("tid exceeds max load count: %d/%d\n", tid,
  333. TID_MAX_LOAD_COUNT);
  334. }
  335. static inline int
  336. il4965_get_il4965_num_of_ant_from_rate(u32 rate_n_flags)
  337. {
  338. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  339. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  340. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  341. }
  342. /*
  343. * Static function to get the expected throughput from an il_scale_tbl_info
  344. * that wraps a NULL pointer check
  345. */
  346. static s32
  347. il4965_get_expected_tpt(struct il_scale_tbl_info *tbl, int rs_idx)
  348. {
  349. if (tbl->expected_tpt)
  350. return tbl->expected_tpt[rs_idx];
  351. return 0;
  352. }
  353. /**
  354. * il4965_rs_collect_tx_data - Update the success/failure sliding win
  355. *
  356. * We keep a sliding win of the last 62 packets transmitted
  357. * at this rate. win->data contains the bitmask of successful
  358. * packets.
  359. */
  360. static int
  361. il4965_rs_collect_tx_data(struct il_scale_tbl_info *tbl, int scale_idx,
  362. int attempts, int successes)
  363. {
  364. struct il_rate_scale_data *win = NULL;
  365. static const u64 mask = (((u64) 1) << (RATE_MAX_WINDOW - 1));
  366. s32 fail_count, tpt;
  367. if (scale_idx < 0 || scale_idx >= RATE_COUNT)
  368. return -EINVAL;
  369. /* Select win for current tx bit rate */
  370. win = &(tbl->win[scale_idx]);
  371. /* Get expected throughput */
  372. tpt = il4965_get_expected_tpt(tbl, scale_idx);
  373. /*
  374. * Keep track of only the latest 62 tx frame attempts in this rate's
  375. * history win; anything older isn't really relevant any more.
  376. * If we have filled up the sliding win, drop the oldest attempt;
  377. * if the oldest attempt (highest bit in bitmap) shows "success",
  378. * subtract "1" from the success counter (this is the main reason
  379. * we keep these bitmaps!).
  380. */
  381. while (attempts > 0) {
  382. if (win->counter >= RATE_MAX_WINDOW) {
  383. /* remove earliest */
  384. win->counter = RATE_MAX_WINDOW - 1;
  385. if (win->data & mask) {
  386. win->data &= ~mask;
  387. win->success_counter--;
  388. }
  389. }
  390. /* Increment frames-attempted counter */
  391. win->counter++;
  392. /* Shift bitmap by one frame to throw away oldest history */
  393. win->data <<= 1;
  394. /* Mark the most recent #successes attempts as successful */
  395. if (successes > 0) {
  396. win->success_counter++;
  397. win->data |= 0x1;
  398. successes--;
  399. }
  400. attempts--;
  401. }
  402. /* Calculate current success ratio, avoid divide-by-0! */
  403. if (win->counter > 0)
  404. win->success_ratio =
  405. 128 * (100 * win->success_counter) / win->counter;
  406. else
  407. win->success_ratio = IL_INVALID_VALUE;
  408. fail_count = win->counter - win->success_counter;
  409. /* Calculate average throughput, if we have enough history. */
  410. if (fail_count >= RATE_MIN_FAILURE_TH ||
  411. win->success_counter >= RATE_MIN_SUCCESS_TH)
  412. win->average_tpt = (win->success_ratio * tpt + 64) / 128;
  413. else
  414. win->average_tpt = IL_INVALID_VALUE;
  415. /* Tag this win as having been updated */
  416. win->stamp = jiffies;
  417. return 0;
  418. }
  419. /*
  420. * Fill uCode API rate_n_flags field, based on "search" or "active" table.
  421. */
  422. static u32
  423. il4965_rate_n_flags_from_tbl(struct il_priv *il, struct il_scale_tbl_info *tbl,
  424. int idx, u8 use_green)
  425. {
  426. u32 rate_n_flags = 0;
  427. if (is_legacy(tbl->lq_type)) {
  428. rate_n_flags = il_rates[idx].plcp;
  429. if (idx >= IL_FIRST_CCK_RATE && idx <= IL_LAST_CCK_RATE)
  430. rate_n_flags |= RATE_MCS_CCK_MSK;
  431. } else if (is_Ht(tbl->lq_type)) {
  432. if (idx > IL_LAST_OFDM_RATE) {
  433. IL_ERR("Invalid HT rate idx %d\n", idx);
  434. idx = IL_LAST_OFDM_RATE;
  435. }
  436. rate_n_flags = RATE_MCS_HT_MSK;
  437. if (is_siso(tbl->lq_type))
  438. rate_n_flags |= il_rates[idx].plcp_siso;
  439. else
  440. rate_n_flags |= il_rates[idx].plcp_mimo2;
  441. } else {
  442. IL_ERR("Invalid tbl->lq_type %d\n", tbl->lq_type);
  443. }
  444. rate_n_flags |=
  445. ((tbl->ant_type << RATE_MCS_ANT_POS) & RATE_MCS_ANT_ABC_MSK);
  446. if (is_Ht(tbl->lq_type)) {
  447. if (tbl->is_ht40) {
  448. if (tbl->is_dup)
  449. rate_n_flags |= RATE_MCS_DUP_MSK;
  450. else
  451. rate_n_flags |= RATE_MCS_HT40_MSK;
  452. }
  453. if (tbl->is_SGI)
  454. rate_n_flags |= RATE_MCS_SGI_MSK;
  455. if (use_green) {
  456. rate_n_flags |= RATE_MCS_GF_MSK;
  457. if (is_siso(tbl->lq_type) && tbl->is_SGI) {
  458. rate_n_flags &= ~RATE_MCS_SGI_MSK;
  459. IL_ERR("GF was set with SGI:SISO\n");
  460. }
  461. }
  462. }
  463. return rate_n_flags;
  464. }
  465. /*
  466. * Interpret uCode API's rate_n_flags format,
  467. * fill "search" or "active" tx mode table.
  468. */
  469. static int
  470. il4965_rs_get_tbl_info_from_mcs(const u32 rate_n_flags,
  471. enum ieee80211_band band,
  472. struct il_scale_tbl_info *tbl, int *rate_idx)
  473. {
  474. u32 ant_msk = (rate_n_flags & RATE_MCS_ANT_ABC_MSK);
  475. u8 il4965_num_of_ant =
  476. il4965_get_il4965_num_of_ant_from_rate(rate_n_flags);
  477. u8 mcs;
  478. memset(tbl, 0, sizeof(struct il_scale_tbl_info));
  479. *rate_idx = il4965_hwrate_to_plcp_idx(rate_n_flags);
  480. if (*rate_idx == RATE_INVALID) {
  481. *rate_idx = -1;
  482. return -EINVAL;
  483. }
  484. tbl->is_SGI = 0; /* default legacy setup */
  485. tbl->is_ht40 = 0;
  486. tbl->is_dup = 0;
  487. tbl->ant_type = (ant_msk >> RATE_MCS_ANT_POS);
  488. tbl->lq_type = LQ_NONE;
  489. tbl->max_search = IL_MAX_SEARCH;
  490. /* legacy rate format */
  491. if (!(rate_n_flags & RATE_MCS_HT_MSK)) {
  492. if (il4965_num_of_ant == 1) {
  493. if (band == IEEE80211_BAND_5GHZ)
  494. tbl->lq_type = LQ_A;
  495. else
  496. tbl->lq_type = LQ_G;
  497. }
  498. /* HT rate format */
  499. } else {
  500. if (rate_n_flags & RATE_MCS_SGI_MSK)
  501. tbl->is_SGI = 1;
  502. if ((rate_n_flags & RATE_MCS_HT40_MSK) ||
  503. (rate_n_flags & RATE_MCS_DUP_MSK))
  504. tbl->is_ht40 = 1;
  505. if (rate_n_flags & RATE_MCS_DUP_MSK)
  506. tbl->is_dup = 1;
  507. mcs = il4965_rs_extract_rate(rate_n_flags);
  508. /* SISO */
  509. if (mcs <= RATE_SISO_60M_PLCP) {
  510. if (il4965_num_of_ant == 1)
  511. tbl->lq_type = LQ_SISO; /*else NONE */
  512. /* MIMO2 */
  513. } else {
  514. if (il4965_num_of_ant == 2)
  515. tbl->lq_type = LQ_MIMO2;
  516. }
  517. }
  518. return 0;
  519. }
  520. /* switch to another antenna/antennas and return 1 */
  521. /* if no other valid antenna found, return 0 */
  522. static int
  523. il4965_rs_toggle_antenna(u32 valid_ant, u32 *rate_n_flags,
  524. struct il_scale_tbl_info *tbl)
  525. {
  526. u8 new_ant_type;
  527. if (!tbl->ant_type || tbl->ant_type > ANT_ABC)
  528. return 0;
  529. if (!il4965_rs_is_valid_ant(valid_ant, tbl->ant_type))
  530. return 0;
  531. new_ant_type = ant_toggle_lookup[tbl->ant_type];
  532. while (new_ant_type != tbl->ant_type &&
  533. !il4965_rs_is_valid_ant(valid_ant, new_ant_type))
  534. new_ant_type = ant_toggle_lookup[new_ant_type];
  535. if (new_ant_type == tbl->ant_type)
  536. return 0;
  537. tbl->ant_type = new_ant_type;
  538. *rate_n_flags &= ~RATE_MCS_ANT_ABC_MSK;
  539. *rate_n_flags |= new_ant_type << RATE_MCS_ANT_POS;
  540. return 1;
  541. }
  542. /**
  543. * Green-field mode is valid if the station supports it and
  544. * there are no non-GF stations present in the BSS.
  545. */
  546. static bool
  547. il4965_rs_use_green(struct il_priv *il, struct ieee80211_sta *sta)
  548. {
  549. return (sta->ht_cap.cap & IEEE80211_HT_CAP_GRN_FLD) &&
  550. !il->ht.non_gf_sta_present;
  551. }
  552. /**
  553. * il4965_rs_get_supported_rates - get the available rates
  554. *
  555. * if management frame or broadcast frame only return
  556. * basic available rates.
  557. *
  558. */
  559. static u16
  560. il4965_rs_get_supported_rates(struct il_lq_sta *lq_sta,
  561. struct ieee80211_hdr *hdr,
  562. enum il_table_type rate_type)
  563. {
  564. if (is_legacy(rate_type)) {
  565. return lq_sta->active_legacy_rate;
  566. } else {
  567. if (is_siso(rate_type))
  568. return lq_sta->active_siso_rate;
  569. else
  570. return lq_sta->active_mimo2_rate;
  571. }
  572. }
  573. static u16
  574. il4965_rs_get_adjacent_rate(struct il_priv *il, u8 idx, u16 rate_mask,
  575. int rate_type)
  576. {
  577. u8 high = RATE_INVALID;
  578. u8 low = RATE_INVALID;
  579. /* 802.11A or ht walks to the next literal adjacent rate in
  580. * the rate table */
  581. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  582. int i;
  583. u32 mask;
  584. /* Find the previous rate that is in the rate mask */
  585. i = idx - 1;
  586. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  587. if (rate_mask & mask) {
  588. low = i;
  589. break;
  590. }
  591. }
  592. /* Find the next rate that is in the rate mask */
  593. i = idx + 1;
  594. for (mask = (1 << i); i < RATE_COUNT; i++, mask <<= 1) {
  595. if (rate_mask & mask) {
  596. high = i;
  597. break;
  598. }
  599. }
  600. return (high << 8) | low;
  601. }
  602. low = idx;
  603. while (low != RATE_INVALID) {
  604. low = il_rates[low].prev_rs;
  605. if (low == RATE_INVALID)
  606. break;
  607. if (rate_mask & (1 << low))
  608. break;
  609. D_RATE("Skipping masked lower rate: %d\n", low);
  610. }
  611. high = idx;
  612. while (high != RATE_INVALID) {
  613. high = il_rates[high].next_rs;
  614. if (high == RATE_INVALID)
  615. break;
  616. if (rate_mask & (1 << high))
  617. break;
  618. D_RATE("Skipping masked higher rate: %d\n", high);
  619. }
  620. return (high << 8) | low;
  621. }
  622. static u32
  623. il4965_rs_get_lower_rate(struct il_lq_sta *lq_sta,
  624. struct il_scale_tbl_info *tbl, u8 scale_idx,
  625. u8 ht_possible)
  626. {
  627. s32 low;
  628. u16 rate_mask;
  629. u16 high_low;
  630. u8 switch_to_legacy = 0;
  631. u8 is_green = lq_sta->is_green;
  632. struct il_priv *il = lq_sta->drv;
  633. /* check if we need to switch from HT to legacy rates.
  634. * assumption is that mandatory rates (1Mbps or 6Mbps)
  635. * are always supported (spec demand) */
  636. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_idx)) {
  637. switch_to_legacy = 1;
  638. scale_idx = rs_ht_to_legacy[scale_idx];
  639. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  640. tbl->lq_type = LQ_A;
  641. else
  642. tbl->lq_type = LQ_G;
  643. if (il4965_num_of_ant(tbl->ant_type) > 1)
  644. tbl->ant_type =
  645. il4965_first_antenna(il->hw_params.valid_tx_ant);
  646. tbl->is_ht40 = 0;
  647. tbl->is_SGI = 0;
  648. tbl->max_search = IL_MAX_SEARCH;
  649. }
  650. rate_mask = il4965_rs_get_supported_rates(lq_sta, NULL, tbl->lq_type);
  651. /* Mask with station rate restriction */
  652. if (is_legacy(tbl->lq_type)) {
  653. /* supp_rates has no CCK bits in A mode */
  654. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  655. rate_mask =
  656. (u16) (rate_mask &
  657. (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
  658. else
  659. rate_mask = (u16) (rate_mask & lq_sta->supp_rates);
  660. }
  661. /* If we switched from HT to legacy, check current rate */
  662. if (switch_to_legacy && (rate_mask & (1 << scale_idx))) {
  663. low = scale_idx;
  664. goto out;
  665. }
  666. high_low =
  667. il4965_rs_get_adjacent_rate(lq_sta->drv, scale_idx, rate_mask,
  668. tbl->lq_type);
  669. low = high_low & 0xff;
  670. if (low == RATE_INVALID)
  671. low = scale_idx;
  672. out:
  673. return il4965_rate_n_flags_from_tbl(lq_sta->drv, tbl, low, is_green);
  674. }
  675. /*
  676. * Simple function to compare two rate scale table types
  677. */
  678. static bool
  679. il4965_table_type_matches(struct il_scale_tbl_info *a,
  680. struct il_scale_tbl_info *b)
  681. {
  682. return (a->lq_type == b->lq_type && a->ant_type == b->ant_type &&
  683. a->is_SGI == b->is_SGI);
  684. }
  685. /*
  686. * mac80211 sends us Tx status
  687. */
  688. static void
  689. il4965_rs_tx_status(void *il_r, struct ieee80211_supported_band *sband,
  690. struct ieee80211_sta *sta, void *il_sta,
  691. struct sk_buff *skb)
  692. {
  693. int legacy_success;
  694. int retries;
  695. int rs_idx, mac_idx, i;
  696. struct il_lq_sta *lq_sta = il_sta;
  697. struct il_link_quality_cmd *table;
  698. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  699. struct il_priv *il = (struct il_priv *)il_r;
  700. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  701. enum mac80211_rate_control_flags mac_flags;
  702. u32 tx_rate;
  703. struct il_scale_tbl_info tbl_type;
  704. struct il_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  705. D_RATE("get frame ack response, update rate scale win\n");
  706. /* Treat uninitialized rate scaling data same as non-existing. */
  707. if (!lq_sta) {
  708. D_RATE("Station rate scaling not created yet.\n");
  709. return;
  710. } else if (!lq_sta->drv) {
  711. D_RATE("Rate scaling not initialized yet.\n");
  712. return;
  713. }
  714. if (!ieee80211_is_data(hdr->frame_control) ||
  715. (info->flags & IEEE80211_TX_CTL_NO_ACK))
  716. return;
  717. /* This packet was aggregated but doesn't carry status info */
  718. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  719. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  720. return;
  721. /*
  722. * Ignore this Tx frame response if its initial rate doesn't match
  723. * that of latest Link Quality command. There may be stragglers
  724. * from a previous Link Quality command, but we're no longer interested
  725. * in those; they're either from the "active" mode while we're trying
  726. * to check "search" mode, or a prior "search" mode after we've moved
  727. * to a new "search" mode (which might become the new "active" mode).
  728. */
  729. table = &lq_sta->lq;
  730. tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
  731. il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type, &rs_idx);
  732. if (il->band == IEEE80211_BAND_5GHZ)
  733. rs_idx -= IL_FIRST_OFDM_RATE;
  734. mac_flags = info->status.rates[0].flags;
  735. mac_idx = info->status.rates[0].idx;
  736. /* For HT packets, map MCS to PLCP */
  737. if (mac_flags & IEEE80211_TX_RC_MCS) {
  738. mac_idx &= RATE_MCS_CODE_MSK; /* Remove # of streams */
  739. if (mac_idx >= (RATE_9M_IDX - IL_FIRST_OFDM_RATE))
  740. mac_idx++;
  741. /*
  742. * mac80211 HT idx is always zero-idxed; we need to move
  743. * HT OFDM rates after CCK rates in 2.4 GHz band
  744. */
  745. if (il->band == IEEE80211_BAND_2GHZ)
  746. mac_idx += IL_FIRST_OFDM_RATE;
  747. }
  748. /* Here we actually compare this rate to the latest LQ command */
  749. if (mac_idx < 0 ||
  750. tbl_type.is_SGI != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI) ||
  751. tbl_type.is_ht40 != !!(mac_flags & IEEE80211_TX_RC_40_MHZ_WIDTH) ||
  752. tbl_type.is_dup != !!(mac_flags & IEEE80211_TX_RC_DUP_DATA) ||
  753. tbl_type.ant_type != info->status.antenna ||
  754. !!(tx_rate & RATE_MCS_HT_MSK) != !!(mac_flags & IEEE80211_TX_RC_MCS)
  755. || !!(tx_rate & RATE_MCS_GF_MSK) !=
  756. !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD) || rs_idx != mac_idx) {
  757. D_RATE("initial rate %d does not match %d (0x%x)\n", mac_idx,
  758. rs_idx, tx_rate);
  759. /*
  760. * Since rates mis-match, the last LQ command may have failed.
  761. * After IL_MISSED_RATE_MAX mis-matches, resync the uCode with
  762. * ... driver.
  763. */
  764. lq_sta->missed_rate_counter++;
  765. if (lq_sta->missed_rate_counter > IL_MISSED_RATE_MAX) {
  766. lq_sta->missed_rate_counter = 0;
  767. il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
  768. }
  769. /* Regardless, ignore this status info for outdated rate */
  770. return;
  771. } else
  772. /* Rate did match, so reset the missed_rate_counter */
  773. lq_sta->missed_rate_counter = 0;
  774. /* Figure out if rate scale algorithm is in active or search table */
  775. if (il4965_table_type_matches
  776. (&tbl_type, &(lq_sta->lq_info[lq_sta->active_tbl]))) {
  777. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  778. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  779. } else
  780. if (il4965_table_type_matches
  781. (&tbl_type, &lq_sta->lq_info[1 - lq_sta->active_tbl])) {
  782. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  783. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  784. } else {
  785. D_RATE("Neither active nor search matches tx rate\n");
  786. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  787. D_RATE("active- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
  788. tmp_tbl->ant_type, tmp_tbl->is_SGI);
  789. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  790. D_RATE("search- lq:%x, ant:%x, SGI:%d\n", tmp_tbl->lq_type,
  791. tmp_tbl->ant_type, tmp_tbl->is_SGI);
  792. D_RATE("actual- lq:%x, ant:%x, SGI:%d\n", tbl_type.lq_type,
  793. tbl_type.ant_type, tbl_type.is_SGI);
  794. /*
  795. * no matching table found, let's by-pass the data collection
  796. * and continue to perform rate scale to find the rate table
  797. */
  798. il4965_rs_stay_in_table(lq_sta, true);
  799. goto done;
  800. }
  801. /*
  802. * Updating the frame history depends on whether packets were
  803. * aggregated.
  804. *
  805. * For aggregation, all packets were transmitted at the same rate, the
  806. * first idx into rate scale table.
  807. */
  808. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  809. tx_rate = le32_to_cpu(table->rs_table[0].rate_n_flags);
  810. il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band, &tbl_type,
  811. &rs_idx);
  812. il4965_rs_collect_tx_data(curr_tbl, rs_idx,
  813. info->status.ampdu_len,
  814. info->status.ampdu_ack_len);
  815. /* Update success/fail counts if not searching for new mode */
  816. if (lq_sta->stay_in_tbl) {
  817. lq_sta->total_success += info->status.ampdu_ack_len;
  818. lq_sta->total_failed +=
  819. (info->status.ampdu_len -
  820. info->status.ampdu_ack_len);
  821. }
  822. } else {
  823. /*
  824. * For legacy, update frame history with for each Tx retry.
  825. */
  826. retries = info->status.rates[0].count - 1;
  827. /* HW doesn't send more than 15 retries */
  828. retries = min(retries, 15);
  829. /* The last transmission may have been successful */
  830. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  831. /* Collect data for each rate used during failed TX attempts */
  832. for (i = 0; i <= retries; ++i) {
  833. tx_rate = le32_to_cpu(table->rs_table[i].rate_n_flags);
  834. il4965_rs_get_tbl_info_from_mcs(tx_rate, il->band,
  835. &tbl_type, &rs_idx);
  836. /*
  837. * Only collect stats if retried rate is in the same RS
  838. * table as active/search.
  839. */
  840. if (il4965_table_type_matches(&tbl_type, curr_tbl))
  841. tmp_tbl = curr_tbl;
  842. else if (il4965_table_type_matches
  843. (&tbl_type, other_tbl))
  844. tmp_tbl = other_tbl;
  845. else
  846. continue;
  847. il4965_rs_collect_tx_data(tmp_tbl, rs_idx, 1,
  848. i <
  849. retries ? 0 : legacy_success);
  850. }
  851. /* Update success/fail counts if not searching for new mode */
  852. if (lq_sta->stay_in_tbl) {
  853. lq_sta->total_success += legacy_success;
  854. lq_sta->total_failed += retries + (1 - legacy_success);
  855. }
  856. }
  857. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  858. lq_sta->last_rate_n_flags = tx_rate;
  859. done:
  860. /* See if there's a better rate or modulation mode to try. */
  861. if (sta->supp_rates[sband->band])
  862. il4965_rs_rate_scale_perform(il, skb, sta, lq_sta);
  863. }
  864. /*
  865. * Begin a period of staying with a selected modulation mode.
  866. * Set "stay_in_tbl" flag to prevent any mode switches.
  867. * Set frame tx success limits according to legacy vs. high-throughput,
  868. * and reset overall (spanning all rates) tx success history stats.
  869. * These control how long we stay using same modulation mode before
  870. * searching for a new mode.
  871. */
  872. static void
  873. il4965_rs_set_stay_in_table(struct il_priv *il, u8 is_legacy,
  874. struct il_lq_sta *lq_sta)
  875. {
  876. D_RATE("we are staying in the same table\n");
  877. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  878. if (is_legacy) {
  879. lq_sta->table_count_limit = IL_LEGACY_TBL_COUNT;
  880. lq_sta->max_failure_limit = IL_LEGACY_FAILURE_LIMIT;
  881. lq_sta->max_success_limit = IL_LEGACY_SUCCESS_LIMIT;
  882. } else {
  883. lq_sta->table_count_limit = IL_NONE_LEGACY_TBL_COUNT;
  884. lq_sta->max_failure_limit = IL_NONE_LEGACY_FAILURE_LIMIT;
  885. lq_sta->max_success_limit = IL_NONE_LEGACY_SUCCESS_LIMIT;
  886. }
  887. lq_sta->table_count = 0;
  888. lq_sta->total_failed = 0;
  889. lq_sta->total_success = 0;
  890. lq_sta->flush_timer = jiffies;
  891. lq_sta->action_counter = 0;
  892. }
  893. /*
  894. * Find correct throughput table for given mode of modulation
  895. */
  896. static void
  897. il4965_rs_set_expected_tpt_table(struct il_lq_sta *lq_sta,
  898. struct il_scale_tbl_info *tbl)
  899. {
  900. /* Used to choose among HT tables */
  901. s32(*ht_tbl_pointer)[RATE_COUNT];
  902. /* Check for invalid LQ type */
  903. if (WARN_ON_ONCE(!is_legacy(tbl->lq_type) && !is_Ht(tbl->lq_type))) {
  904. tbl->expected_tpt = expected_tpt_legacy;
  905. return;
  906. }
  907. /* Legacy rates have only one table */
  908. if (is_legacy(tbl->lq_type)) {
  909. tbl->expected_tpt = expected_tpt_legacy;
  910. return;
  911. }
  912. /* Choose among many HT tables depending on number of streams
  913. * (SISO/MIMO2), channel width (20/40), SGI, and aggregation
  914. * status */
  915. if (is_siso(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
  916. ht_tbl_pointer = expected_tpt_siso20MHz;
  917. else if (is_siso(tbl->lq_type))
  918. ht_tbl_pointer = expected_tpt_siso40MHz;
  919. else if (is_mimo2(tbl->lq_type) && (!tbl->is_ht40 || lq_sta->is_dup))
  920. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  921. else /* if (is_mimo2(tbl->lq_type)) <-- must be true */
  922. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  923. if (!tbl->is_SGI && !lq_sta->is_agg) /* Normal */
  924. tbl->expected_tpt = ht_tbl_pointer[0];
  925. else if (tbl->is_SGI && !lq_sta->is_agg) /* SGI */
  926. tbl->expected_tpt = ht_tbl_pointer[1];
  927. else if (!tbl->is_SGI && lq_sta->is_agg) /* AGG */
  928. tbl->expected_tpt = ht_tbl_pointer[2];
  929. else /* AGG+SGI */
  930. tbl->expected_tpt = ht_tbl_pointer[3];
  931. }
  932. /*
  933. * Find starting rate for new "search" high-throughput mode of modulation.
  934. * Goal is to find lowest expected rate (under perfect conditions) that is
  935. * above the current measured throughput of "active" mode, to give new mode
  936. * a fair chance to prove itself without too many challenges.
  937. *
  938. * This gets called when transitioning to more aggressive modulation
  939. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  940. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  941. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  942. * bit rate will typically need to increase, but not if performance was bad.
  943. */
  944. static s32
  945. il4965_rs_get_best_rate(struct il_priv *il, struct il_lq_sta *lq_sta,
  946. struct il_scale_tbl_info *tbl, /* "search" */
  947. u16 rate_mask, s8 idx)
  948. {
  949. /* "active" values */
  950. struct il_scale_tbl_info *active_tbl =
  951. &(lq_sta->lq_info[lq_sta->active_tbl]);
  952. s32 active_sr = active_tbl->win[idx].success_ratio;
  953. s32 active_tpt = active_tbl->expected_tpt[idx];
  954. /* expected "search" throughput */
  955. s32 *tpt_tbl = tbl->expected_tpt;
  956. s32 new_rate, high, low, start_hi;
  957. u16 high_low;
  958. s8 rate = idx;
  959. new_rate = high = low = start_hi = RATE_INVALID;
  960. for (;;) {
  961. high_low =
  962. il4965_rs_get_adjacent_rate(il, rate, rate_mask,
  963. tbl->lq_type);
  964. low = high_low & 0xff;
  965. high = (high_low >> 8) & 0xff;
  966. /*
  967. * Lower the "search" bit rate, to give new "search" mode
  968. * approximately the same throughput as "active" if:
  969. *
  970. * 1) "Active" mode has been working modestly well (but not
  971. * great), and expected "search" throughput (under perfect
  972. * conditions) at candidate rate is above the actual
  973. * measured "active" throughput (but less than expected
  974. * "active" throughput under perfect conditions).
  975. * OR
  976. * 2) "Active" mode has been working perfectly or very well
  977. * and expected "search" throughput (under perfect
  978. * conditions) at candidate rate is above expected
  979. * "active" throughput (under perfect conditions).
  980. */
  981. if ((100 * tpt_tbl[rate] > lq_sta->last_tpt &&
  982. (active_sr > RATE_DECREASE_TH && active_sr <= RATE_HIGH_TH
  983. && tpt_tbl[rate] <= active_tpt)) ||
  984. (active_sr >= RATE_SCALE_SWITCH &&
  985. tpt_tbl[rate] > active_tpt)) {
  986. /* (2nd or later pass)
  987. * If we've already tried to raise the rate, and are
  988. * now trying to lower it, use the higher rate. */
  989. if (start_hi != RATE_INVALID) {
  990. new_rate = start_hi;
  991. break;
  992. }
  993. new_rate = rate;
  994. /* Loop again with lower rate */
  995. if (low != RATE_INVALID)
  996. rate = low;
  997. /* Lower rate not available, use the original */
  998. else
  999. break;
  1000. /* Else try to raise the "search" rate to match "active" */
  1001. } else {
  1002. /* (2nd or later pass)
  1003. * If we've already tried to lower the rate, and are
  1004. * now trying to raise it, use the lower rate. */
  1005. if (new_rate != RATE_INVALID)
  1006. break;
  1007. /* Loop again with higher rate */
  1008. else if (high != RATE_INVALID) {
  1009. start_hi = high;
  1010. rate = high;
  1011. /* Higher rate not available, use the original */
  1012. } else {
  1013. new_rate = rate;
  1014. break;
  1015. }
  1016. }
  1017. }
  1018. return new_rate;
  1019. }
  1020. /*
  1021. * Set up search table for MIMO2
  1022. */
  1023. static int
  1024. il4965_rs_switch_to_mimo2(struct il_priv *il, struct il_lq_sta *lq_sta,
  1025. struct ieee80211_conf *conf,
  1026. struct ieee80211_sta *sta,
  1027. struct il_scale_tbl_info *tbl, int idx)
  1028. {
  1029. u16 rate_mask;
  1030. s32 rate;
  1031. s8 is_green = lq_sta->is_green;
  1032. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1033. return -1;
  1034. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  1035. return -1;
  1036. /* Need both Tx chains/antennas to support MIMO */
  1037. if (il->hw_params.tx_chains_num < 2)
  1038. return -1;
  1039. D_RATE("LQ: try to switch to MIMO2\n");
  1040. tbl->lq_type = LQ_MIMO2;
  1041. tbl->is_dup = lq_sta->is_dup;
  1042. tbl->action = 0;
  1043. tbl->max_search = IL_MAX_SEARCH;
  1044. rate_mask = lq_sta->active_mimo2_rate;
  1045. if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
  1046. tbl->is_ht40 = 1;
  1047. else
  1048. tbl->is_ht40 = 0;
  1049. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1050. rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
  1051. D_RATE("LQ: MIMO2 best rate %d mask %X\n", rate, rate_mask);
  1052. if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
  1053. D_RATE("Can't switch with idx %d rate mask %x\n", rate,
  1054. rate_mask);
  1055. return -1;
  1056. }
  1057. tbl->current_rate =
  1058. il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
  1059. D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
  1060. is_green);
  1061. return 0;
  1062. }
  1063. /*
  1064. * Set up search table for SISO
  1065. */
  1066. static int
  1067. il4965_rs_switch_to_siso(struct il_priv *il, struct il_lq_sta *lq_sta,
  1068. struct ieee80211_conf *conf, struct ieee80211_sta *sta,
  1069. struct il_scale_tbl_info *tbl, int idx)
  1070. {
  1071. u16 rate_mask;
  1072. u8 is_green = lq_sta->is_green;
  1073. s32 rate;
  1074. if (!conf_is_ht(conf) || !sta->ht_cap.ht_supported)
  1075. return -1;
  1076. D_RATE("LQ: try to switch to SISO\n");
  1077. tbl->is_dup = lq_sta->is_dup;
  1078. tbl->lq_type = LQ_SISO;
  1079. tbl->action = 0;
  1080. tbl->max_search = IL_MAX_SEARCH;
  1081. rate_mask = lq_sta->active_siso_rate;
  1082. if (il_is_ht40_tx_allowed(il, &sta->ht_cap))
  1083. tbl->is_ht40 = 1;
  1084. else
  1085. tbl->is_ht40 = 0;
  1086. if (is_green)
  1087. tbl->is_SGI = 0; /*11n spec: no SGI in SISO+Greenfield */
  1088. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1089. rate = il4965_rs_get_best_rate(il, lq_sta, tbl, rate_mask, idx);
  1090. D_RATE("LQ: get best rate %d mask %X\n", rate, rate_mask);
  1091. if (rate == RATE_INVALID || !((1 << rate) & rate_mask)) {
  1092. D_RATE("can not switch with idx %d rate mask %x\n", rate,
  1093. rate_mask);
  1094. return -1;
  1095. }
  1096. tbl->current_rate =
  1097. il4965_rate_n_flags_from_tbl(il, tbl, rate, is_green);
  1098. D_RATE("LQ: Switch to new mcs %X idx is green %X\n", tbl->current_rate,
  1099. is_green);
  1100. return 0;
  1101. }
  1102. /*
  1103. * Try to switch to new modulation mode from legacy
  1104. */
  1105. static int
  1106. il4965_rs_move_legacy_other(struct il_priv *il, struct il_lq_sta *lq_sta,
  1107. struct ieee80211_conf *conf,
  1108. struct ieee80211_sta *sta, int idx)
  1109. {
  1110. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1111. struct il_scale_tbl_info *search_tbl =
  1112. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1113. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1114. u32 sz =
  1115. (sizeof(struct il_scale_tbl_info) -
  1116. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1117. u8 start_action;
  1118. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1119. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1120. int ret = 0;
  1121. u8 update_search_tbl_counter = 0;
  1122. tbl->action = IL_LEGACY_SWITCH_SISO;
  1123. start_action = tbl->action;
  1124. for (;;) {
  1125. lq_sta->action_counter++;
  1126. switch (tbl->action) {
  1127. case IL_LEGACY_SWITCH_ANTENNA1:
  1128. case IL_LEGACY_SWITCH_ANTENNA2:
  1129. D_RATE("LQ: Legacy toggle Antenna\n");
  1130. if ((tbl->action == IL_LEGACY_SWITCH_ANTENNA1 &&
  1131. tx_chains_num <= 1) ||
  1132. (tbl->action == IL_LEGACY_SWITCH_ANTENNA2 &&
  1133. tx_chains_num <= 2))
  1134. break;
  1135. /* Don't change antenna if success has been great */
  1136. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1137. break;
  1138. /* Set up search table to try other antenna */
  1139. memcpy(search_tbl, tbl, sz);
  1140. if (il4965_rs_toggle_antenna
  1141. (valid_tx_ant, &search_tbl->current_rate,
  1142. search_tbl)) {
  1143. update_search_tbl_counter = 1;
  1144. il4965_rs_set_expected_tpt_table(lq_sta,
  1145. search_tbl);
  1146. goto out;
  1147. }
  1148. break;
  1149. case IL_LEGACY_SWITCH_SISO:
  1150. D_RATE("LQ: Legacy switch to SISO\n");
  1151. /* Set up search table to try SISO */
  1152. memcpy(search_tbl, tbl, sz);
  1153. search_tbl->is_SGI = 0;
  1154. ret =
  1155. il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
  1156. search_tbl, idx);
  1157. if (!ret) {
  1158. lq_sta->action_counter = 0;
  1159. goto out;
  1160. }
  1161. break;
  1162. case IL_LEGACY_SWITCH_MIMO2_AB:
  1163. case IL_LEGACY_SWITCH_MIMO2_AC:
  1164. case IL_LEGACY_SWITCH_MIMO2_BC:
  1165. D_RATE("LQ: Legacy switch to MIMO2\n");
  1166. /* Set up search table to try MIMO */
  1167. memcpy(search_tbl, tbl, sz);
  1168. search_tbl->is_SGI = 0;
  1169. if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AB)
  1170. search_tbl->ant_type = ANT_AB;
  1171. else if (tbl->action == IL_LEGACY_SWITCH_MIMO2_AC)
  1172. search_tbl->ant_type = ANT_AC;
  1173. else
  1174. search_tbl->ant_type = ANT_BC;
  1175. if (!il4965_rs_is_valid_ant
  1176. (valid_tx_ant, search_tbl->ant_type))
  1177. break;
  1178. ret =
  1179. il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
  1180. search_tbl, idx);
  1181. if (!ret) {
  1182. lq_sta->action_counter = 0;
  1183. goto out;
  1184. }
  1185. break;
  1186. }
  1187. tbl->action++;
  1188. if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
  1189. tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
  1190. if (tbl->action == start_action)
  1191. break;
  1192. }
  1193. search_tbl->lq_type = LQ_NONE;
  1194. return 0;
  1195. out:
  1196. lq_sta->search_better_tbl = 1;
  1197. tbl->action++;
  1198. if (tbl->action > IL_LEGACY_SWITCH_MIMO2_BC)
  1199. tbl->action = IL_LEGACY_SWITCH_ANTENNA1;
  1200. if (update_search_tbl_counter)
  1201. search_tbl->action = tbl->action;
  1202. return 0;
  1203. }
  1204. /*
  1205. * Try to switch to new modulation mode from SISO
  1206. */
  1207. static int
  1208. il4965_rs_move_siso_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
  1209. struct ieee80211_conf *conf,
  1210. struct ieee80211_sta *sta, int idx)
  1211. {
  1212. u8 is_green = lq_sta->is_green;
  1213. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1214. struct il_scale_tbl_info *search_tbl =
  1215. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1216. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1217. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1218. u32 sz =
  1219. (sizeof(struct il_scale_tbl_info) -
  1220. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1221. u8 start_action;
  1222. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1223. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1224. u8 update_search_tbl_counter = 0;
  1225. int ret;
  1226. start_action = tbl->action;
  1227. for (;;) {
  1228. lq_sta->action_counter++;
  1229. switch (tbl->action) {
  1230. case IL_SISO_SWITCH_ANTENNA1:
  1231. case IL_SISO_SWITCH_ANTENNA2:
  1232. D_RATE("LQ: SISO toggle Antenna\n");
  1233. if ((tbl->action == IL_SISO_SWITCH_ANTENNA1 &&
  1234. tx_chains_num <= 1) ||
  1235. (tbl->action == IL_SISO_SWITCH_ANTENNA2 &&
  1236. tx_chains_num <= 2))
  1237. break;
  1238. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1239. break;
  1240. memcpy(search_tbl, tbl, sz);
  1241. if (il4965_rs_toggle_antenna
  1242. (valid_tx_ant, &search_tbl->current_rate,
  1243. search_tbl)) {
  1244. update_search_tbl_counter = 1;
  1245. goto out;
  1246. }
  1247. break;
  1248. case IL_SISO_SWITCH_MIMO2_AB:
  1249. case IL_SISO_SWITCH_MIMO2_AC:
  1250. case IL_SISO_SWITCH_MIMO2_BC:
  1251. D_RATE("LQ: SISO switch to MIMO2\n");
  1252. memcpy(search_tbl, tbl, sz);
  1253. search_tbl->is_SGI = 0;
  1254. if (tbl->action == IL_SISO_SWITCH_MIMO2_AB)
  1255. search_tbl->ant_type = ANT_AB;
  1256. else if (tbl->action == IL_SISO_SWITCH_MIMO2_AC)
  1257. search_tbl->ant_type = ANT_AC;
  1258. else
  1259. search_tbl->ant_type = ANT_BC;
  1260. if (!il4965_rs_is_valid_ant
  1261. (valid_tx_ant, search_tbl->ant_type))
  1262. break;
  1263. ret =
  1264. il4965_rs_switch_to_mimo2(il, lq_sta, conf, sta,
  1265. search_tbl, idx);
  1266. if (!ret)
  1267. goto out;
  1268. break;
  1269. case IL_SISO_SWITCH_GI:
  1270. if (!tbl->is_ht40 &&
  1271. !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
  1272. break;
  1273. if (tbl->is_ht40 &&
  1274. !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
  1275. break;
  1276. D_RATE("LQ: SISO toggle SGI/NGI\n");
  1277. memcpy(search_tbl, tbl, sz);
  1278. if (is_green) {
  1279. if (!tbl->is_SGI)
  1280. break;
  1281. else
  1282. IL_ERR("SGI was set in GF+SISO\n");
  1283. }
  1284. search_tbl->is_SGI = !tbl->is_SGI;
  1285. il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
  1286. if (tbl->is_SGI) {
  1287. s32 tpt = lq_sta->last_tpt / 100;
  1288. if (tpt >= search_tbl->expected_tpt[idx])
  1289. break;
  1290. }
  1291. search_tbl->current_rate =
  1292. il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
  1293. is_green);
  1294. update_search_tbl_counter = 1;
  1295. goto out;
  1296. }
  1297. tbl->action++;
  1298. if (tbl->action > IL_SISO_SWITCH_GI)
  1299. tbl->action = IL_SISO_SWITCH_ANTENNA1;
  1300. if (tbl->action == start_action)
  1301. break;
  1302. }
  1303. search_tbl->lq_type = LQ_NONE;
  1304. return 0;
  1305. out:
  1306. lq_sta->search_better_tbl = 1;
  1307. tbl->action++;
  1308. if (tbl->action > IL_SISO_SWITCH_GI)
  1309. tbl->action = IL_SISO_SWITCH_ANTENNA1;
  1310. if (update_search_tbl_counter)
  1311. search_tbl->action = tbl->action;
  1312. return 0;
  1313. }
  1314. /*
  1315. * Try to switch to new modulation mode from MIMO2
  1316. */
  1317. static int
  1318. il4965_rs_move_mimo2_to_other(struct il_priv *il, struct il_lq_sta *lq_sta,
  1319. struct ieee80211_conf *conf,
  1320. struct ieee80211_sta *sta, int idx)
  1321. {
  1322. s8 is_green = lq_sta->is_green;
  1323. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1324. struct il_scale_tbl_info *search_tbl =
  1325. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1326. struct il_rate_scale_data *win = &(tbl->win[idx]);
  1327. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1328. u32 sz =
  1329. (sizeof(struct il_scale_tbl_info) -
  1330. (sizeof(struct il_rate_scale_data) * RATE_COUNT));
  1331. u8 start_action;
  1332. u8 valid_tx_ant = il->hw_params.valid_tx_ant;
  1333. u8 tx_chains_num = il->hw_params.tx_chains_num;
  1334. u8 update_search_tbl_counter = 0;
  1335. int ret;
  1336. start_action = tbl->action;
  1337. for (;;) {
  1338. lq_sta->action_counter++;
  1339. switch (tbl->action) {
  1340. case IL_MIMO2_SWITCH_ANTENNA1:
  1341. case IL_MIMO2_SWITCH_ANTENNA2:
  1342. D_RATE("LQ: MIMO2 toggle Antennas\n");
  1343. if (tx_chains_num <= 2)
  1344. break;
  1345. if (win->success_ratio >= IL_RS_GOOD_RATIO)
  1346. break;
  1347. memcpy(search_tbl, tbl, sz);
  1348. if (il4965_rs_toggle_antenna
  1349. (valid_tx_ant, &search_tbl->current_rate,
  1350. search_tbl)) {
  1351. update_search_tbl_counter = 1;
  1352. goto out;
  1353. }
  1354. break;
  1355. case IL_MIMO2_SWITCH_SISO_A:
  1356. case IL_MIMO2_SWITCH_SISO_B:
  1357. case IL_MIMO2_SWITCH_SISO_C:
  1358. D_RATE("LQ: MIMO2 switch to SISO\n");
  1359. /* Set up new search table for SISO */
  1360. memcpy(search_tbl, tbl, sz);
  1361. if (tbl->action == IL_MIMO2_SWITCH_SISO_A)
  1362. search_tbl->ant_type = ANT_A;
  1363. else if (tbl->action == IL_MIMO2_SWITCH_SISO_B)
  1364. search_tbl->ant_type = ANT_B;
  1365. else
  1366. search_tbl->ant_type = ANT_C;
  1367. if (!il4965_rs_is_valid_ant
  1368. (valid_tx_ant, search_tbl->ant_type))
  1369. break;
  1370. ret =
  1371. il4965_rs_switch_to_siso(il, lq_sta, conf, sta,
  1372. search_tbl, idx);
  1373. if (!ret)
  1374. goto out;
  1375. break;
  1376. case IL_MIMO2_SWITCH_GI:
  1377. if (!tbl->is_ht40 &&
  1378. !(ht_cap->cap & IEEE80211_HT_CAP_SGI_20))
  1379. break;
  1380. if (tbl->is_ht40 &&
  1381. !(ht_cap->cap & IEEE80211_HT_CAP_SGI_40))
  1382. break;
  1383. D_RATE("LQ: MIMO2 toggle SGI/NGI\n");
  1384. /* Set up new search table for MIMO2 */
  1385. memcpy(search_tbl, tbl, sz);
  1386. search_tbl->is_SGI = !tbl->is_SGI;
  1387. il4965_rs_set_expected_tpt_table(lq_sta, search_tbl);
  1388. /*
  1389. * If active table already uses the fastest possible
  1390. * modulation (dual stream with short guard interval),
  1391. * and it's working well, there's no need to look
  1392. * for a better type of modulation!
  1393. */
  1394. if (tbl->is_SGI) {
  1395. s32 tpt = lq_sta->last_tpt / 100;
  1396. if (tpt >= search_tbl->expected_tpt[idx])
  1397. break;
  1398. }
  1399. search_tbl->current_rate =
  1400. il4965_rate_n_flags_from_tbl(il, search_tbl, idx,
  1401. is_green);
  1402. update_search_tbl_counter = 1;
  1403. goto out;
  1404. }
  1405. tbl->action++;
  1406. if (tbl->action > IL_MIMO2_SWITCH_GI)
  1407. tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
  1408. if (tbl->action == start_action)
  1409. break;
  1410. }
  1411. search_tbl->lq_type = LQ_NONE;
  1412. return 0;
  1413. out:
  1414. lq_sta->search_better_tbl = 1;
  1415. tbl->action++;
  1416. if (tbl->action > IL_MIMO2_SWITCH_GI)
  1417. tbl->action = IL_MIMO2_SWITCH_ANTENNA1;
  1418. if (update_search_tbl_counter)
  1419. search_tbl->action = tbl->action;
  1420. return 0;
  1421. }
  1422. /*
  1423. * Check whether we should continue using same modulation mode, or
  1424. * begin search for a new mode, based on:
  1425. * 1) # tx successes or failures while using this mode
  1426. * 2) # times calling this function
  1427. * 3) elapsed time in this mode (not used, for now)
  1428. */
  1429. static void
  1430. il4965_rs_stay_in_table(struct il_lq_sta *lq_sta, bool force_search)
  1431. {
  1432. struct il_scale_tbl_info *tbl;
  1433. int i;
  1434. int active_tbl;
  1435. int flush_interval_passed = 0;
  1436. struct il_priv *il;
  1437. il = lq_sta->drv;
  1438. active_tbl = lq_sta->active_tbl;
  1439. tbl = &(lq_sta->lq_info[active_tbl]);
  1440. /* If we've been disallowing search, see if we should now allow it */
  1441. if (lq_sta->stay_in_tbl) {
  1442. /* Elapsed time using current modulation mode */
  1443. if (lq_sta->flush_timer)
  1444. flush_interval_passed =
  1445. time_after(jiffies,
  1446. (unsigned long)(lq_sta->flush_timer +
  1447. RATE_SCALE_FLUSH_INTVL));
  1448. /*
  1449. * Check if we should allow search for new modulation mode.
  1450. * If many frames have failed or succeeded, or we've used
  1451. * this same modulation for a long time, allow search, and
  1452. * reset history stats that keep track of whether we should
  1453. * allow a new search. Also (below) reset all bitmaps and
  1454. * stats in active history.
  1455. */
  1456. if (force_search ||
  1457. lq_sta->total_failed > lq_sta->max_failure_limit ||
  1458. lq_sta->total_success > lq_sta->max_success_limit ||
  1459. (!lq_sta->search_better_tbl && lq_sta->flush_timer &&
  1460. flush_interval_passed)) {
  1461. D_RATE("LQ: stay is expired %d %d %d\n:",
  1462. lq_sta->total_failed, lq_sta->total_success,
  1463. flush_interval_passed);
  1464. /* Allow search for new mode */
  1465. lq_sta->stay_in_tbl = 0; /* only place reset */
  1466. lq_sta->total_failed = 0;
  1467. lq_sta->total_success = 0;
  1468. lq_sta->flush_timer = 0;
  1469. /*
  1470. * Else if we've used this modulation mode enough repetitions
  1471. * (regardless of elapsed time or success/failure), reset
  1472. * history bitmaps and rate-specific stats for all rates in
  1473. * active table.
  1474. */
  1475. } else {
  1476. lq_sta->table_count++;
  1477. if (lq_sta->table_count >= lq_sta->table_count_limit) {
  1478. lq_sta->table_count = 0;
  1479. D_RATE("LQ: stay in table clear win\n");
  1480. for (i = 0; i < RATE_COUNT; i++)
  1481. il4965_rs_rate_scale_clear_win(&
  1482. (tbl->
  1483. win
  1484. [i]));
  1485. }
  1486. }
  1487. /* If transitioning to allow "search", reset all history
  1488. * bitmaps and stats in active table (this will become the new
  1489. * "search" table). */
  1490. if (!lq_sta->stay_in_tbl) {
  1491. for (i = 0; i < RATE_COUNT; i++)
  1492. il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
  1493. }
  1494. }
  1495. }
  1496. /*
  1497. * setup rate table in uCode
  1498. */
  1499. static void
  1500. il4965_rs_update_rate_tbl(struct il_priv *il, struct il_lq_sta *lq_sta,
  1501. struct il_scale_tbl_info *tbl, int idx, u8 is_green)
  1502. {
  1503. u32 rate;
  1504. /* Update uCode's rate table. */
  1505. rate = il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
  1506. il4965_rs_fill_link_cmd(il, lq_sta, rate);
  1507. il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
  1508. }
  1509. /*
  1510. * Do rate scaling and search for new modulation mode.
  1511. */
  1512. static void
  1513. il4965_rs_rate_scale_perform(struct il_priv *il, struct sk_buff *skb,
  1514. struct ieee80211_sta *sta,
  1515. struct il_lq_sta *lq_sta)
  1516. {
  1517. struct ieee80211_hw *hw = il->hw;
  1518. struct ieee80211_conf *conf = &hw->conf;
  1519. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1520. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1521. int low = RATE_INVALID;
  1522. int high = RATE_INVALID;
  1523. int idx;
  1524. int i;
  1525. struct il_rate_scale_data *win = NULL;
  1526. int current_tpt = IL_INVALID_VALUE;
  1527. int low_tpt = IL_INVALID_VALUE;
  1528. int high_tpt = IL_INVALID_VALUE;
  1529. u32 fail_count;
  1530. s8 scale_action = 0;
  1531. u16 rate_mask;
  1532. u8 update_lq = 0;
  1533. struct il_scale_tbl_info *tbl, *tbl1;
  1534. u16 rate_scale_idx_msk = 0;
  1535. u8 is_green = 0;
  1536. u8 active_tbl = 0;
  1537. u8 done_search = 0;
  1538. u16 high_low;
  1539. s32 sr;
  1540. u8 tid = MAX_TID_COUNT;
  1541. struct il_tid_data *tid_data;
  1542. D_RATE("rate scale calculate new rate for skb\n");
  1543. /* Send management frames and NO_ACK data using lowest rate. */
  1544. /* TODO: this could probably be improved.. */
  1545. if (!ieee80211_is_data(hdr->frame_control) ||
  1546. (info->flags & IEEE80211_TX_CTL_NO_ACK))
  1547. return;
  1548. lq_sta->supp_rates = sta->supp_rates[lq_sta->band];
  1549. tid = il4965_rs_tl_add_packet(lq_sta, hdr);
  1550. if (tid != MAX_TID_COUNT && (lq_sta->tx_agg_tid_en & (1 << tid))) {
  1551. tid_data = &il->stations[lq_sta->lq.sta_id].tid[tid];
  1552. if (tid_data->agg.state == IL_AGG_OFF)
  1553. lq_sta->is_agg = 0;
  1554. else
  1555. lq_sta->is_agg = 1;
  1556. } else
  1557. lq_sta->is_agg = 0;
  1558. /*
  1559. * Select rate-scale / modulation-mode table to work with in
  1560. * the rest of this function: "search" if searching for better
  1561. * modulation mode, or "active" if doing rate scaling within a mode.
  1562. */
  1563. if (!lq_sta->search_better_tbl)
  1564. active_tbl = lq_sta->active_tbl;
  1565. else
  1566. active_tbl = 1 - lq_sta->active_tbl;
  1567. tbl = &(lq_sta->lq_info[active_tbl]);
  1568. if (is_legacy(tbl->lq_type))
  1569. lq_sta->is_green = 0;
  1570. else
  1571. lq_sta->is_green = il4965_rs_use_green(il, sta);
  1572. is_green = lq_sta->is_green;
  1573. /* current tx rate */
  1574. idx = lq_sta->last_txrate_idx;
  1575. D_RATE("Rate scale idx %d for type %d\n", idx, tbl->lq_type);
  1576. /* rates available for this association, and for modulation mode */
  1577. rate_mask = il4965_rs_get_supported_rates(lq_sta, hdr, tbl->lq_type);
  1578. D_RATE("mask 0x%04X\n", rate_mask);
  1579. /* mask with station rate restriction */
  1580. if (is_legacy(tbl->lq_type)) {
  1581. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1582. /* supp_rates has no CCK bits in A mode */
  1583. rate_scale_idx_msk =
  1584. (u16) (rate_mask &
  1585. (lq_sta->supp_rates << IL_FIRST_OFDM_RATE));
  1586. else
  1587. rate_scale_idx_msk =
  1588. (u16) (rate_mask & lq_sta->supp_rates);
  1589. } else
  1590. rate_scale_idx_msk = rate_mask;
  1591. if (!rate_scale_idx_msk)
  1592. rate_scale_idx_msk = rate_mask;
  1593. if (!((1 << idx) & rate_scale_idx_msk)) {
  1594. IL_ERR("Current Rate is not valid\n");
  1595. if (lq_sta->search_better_tbl) {
  1596. /* revert to active table if search table is not valid */
  1597. tbl->lq_type = LQ_NONE;
  1598. lq_sta->search_better_tbl = 0;
  1599. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1600. /* get "active" rate info */
  1601. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1602. il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx,
  1603. is_green);
  1604. }
  1605. return;
  1606. }
  1607. /* Get expected throughput table and history win for current rate */
  1608. if (!tbl->expected_tpt) {
  1609. IL_ERR("tbl->expected_tpt is NULL\n");
  1610. return;
  1611. }
  1612. /* force user max rate if set by user */
  1613. if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < idx) {
  1614. idx = lq_sta->max_rate_idx;
  1615. update_lq = 1;
  1616. win = &(tbl->win[idx]);
  1617. goto lq_update;
  1618. }
  1619. win = &(tbl->win[idx]);
  1620. /*
  1621. * If there is not enough history to calculate actual average
  1622. * throughput, keep analyzing results of more tx frames, without
  1623. * changing rate or mode (bypass most of the rest of this function).
  1624. * Set up new rate table in uCode only if old rate is not supported
  1625. * in current association (use new rate found above).
  1626. */
  1627. fail_count = win->counter - win->success_counter;
  1628. if (fail_count < RATE_MIN_FAILURE_TH &&
  1629. win->success_counter < RATE_MIN_SUCCESS_TH) {
  1630. D_RATE("LQ: still below TH. succ=%d total=%d " "for idx %d\n",
  1631. win->success_counter, win->counter, idx);
  1632. /* Can't calculate this yet; not enough history */
  1633. win->average_tpt = IL_INVALID_VALUE;
  1634. /* Should we stay with this modulation mode,
  1635. * or search for a new one? */
  1636. il4965_rs_stay_in_table(lq_sta, false);
  1637. goto out;
  1638. }
  1639. /* Else we have enough samples; calculate estimate of
  1640. * actual average throughput */
  1641. if (win->average_tpt !=
  1642. ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128)) {
  1643. IL_ERR("expected_tpt should have been calculated by now\n");
  1644. win->average_tpt =
  1645. ((win->success_ratio * tbl->expected_tpt[idx] + 64) / 128);
  1646. }
  1647. /* If we are searching for better modulation mode, check success. */
  1648. if (lq_sta->search_better_tbl) {
  1649. /* If good success, continue using the "search" mode;
  1650. * no need to send new link quality command, since we're
  1651. * continuing to use the setup that we've been trying. */
  1652. if (win->average_tpt > lq_sta->last_tpt) {
  1653. D_RATE("LQ: SWITCHING TO NEW TBL "
  1654. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1655. win->success_ratio, win->average_tpt,
  1656. lq_sta->last_tpt);
  1657. if (!is_legacy(tbl->lq_type))
  1658. lq_sta->enable_counter = 1;
  1659. /* Swap tables; "search" becomes "active" */
  1660. lq_sta->active_tbl = active_tbl;
  1661. current_tpt = win->average_tpt;
  1662. /* Else poor success; go back to mode in "active" table */
  1663. } else {
  1664. D_RATE("LQ: GOING BACK TO THE OLD TBL "
  1665. "suc=%d cur-tpt=%d old-tpt=%d\n",
  1666. win->success_ratio, win->average_tpt,
  1667. lq_sta->last_tpt);
  1668. /* Nullify "search" table */
  1669. tbl->lq_type = LQ_NONE;
  1670. /* Revert to "active" table */
  1671. active_tbl = lq_sta->active_tbl;
  1672. tbl = &(lq_sta->lq_info[active_tbl]);
  1673. /* Revert to "active" rate and throughput info */
  1674. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1675. current_tpt = lq_sta->last_tpt;
  1676. /* Need to set up a new rate table in uCode */
  1677. update_lq = 1;
  1678. }
  1679. /* Either way, we've made a decision; modulation mode
  1680. * search is done, allow rate adjustment next time. */
  1681. lq_sta->search_better_tbl = 0;
  1682. done_search = 1; /* Don't switch modes below! */
  1683. goto lq_update;
  1684. }
  1685. /* (Else) not in search of better modulation mode, try for better
  1686. * starting rate, while staying in this mode. */
  1687. high_low =
  1688. il4965_rs_get_adjacent_rate(il, idx, rate_scale_idx_msk,
  1689. tbl->lq_type);
  1690. low = high_low & 0xff;
  1691. high = (high_low >> 8) & 0xff;
  1692. /* If user set max rate, dont allow higher than user constrain */
  1693. if (lq_sta->max_rate_idx != -1 && lq_sta->max_rate_idx < high)
  1694. high = RATE_INVALID;
  1695. sr = win->success_ratio;
  1696. /* Collect measured throughputs for current and adjacent rates */
  1697. current_tpt = win->average_tpt;
  1698. if (low != RATE_INVALID)
  1699. low_tpt = tbl->win[low].average_tpt;
  1700. if (high != RATE_INVALID)
  1701. high_tpt = tbl->win[high].average_tpt;
  1702. scale_action = 0;
  1703. /* Too many failures, decrease rate */
  1704. if (sr <= RATE_DECREASE_TH || current_tpt == 0) {
  1705. D_RATE("decrease rate because of low success_ratio\n");
  1706. scale_action = -1;
  1707. /* No throughput measured yet for adjacent rates; try increase. */
  1708. } else if (low_tpt == IL_INVALID_VALUE && high_tpt == IL_INVALID_VALUE) {
  1709. if (high != RATE_INVALID && sr >= RATE_INCREASE_TH)
  1710. scale_action = 1;
  1711. else if (low != RATE_INVALID)
  1712. scale_action = 0;
  1713. }
  1714. /* Both adjacent throughputs are measured, but neither one has better
  1715. * throughput; we're using the best rate, don't change it! */
  1716. else if (low_tpt != IL_INVALID_VALUE && high_tpt != IL_INVALID_VALUE &&
  1717. low_tpt < current_tpt && high_tpt < current_tpt)
  1718. scale_action = 0;
  1719. /* At least one adjacent rate's throughput is measured,
  1720. * and may have better performance. */
  1721. else {
  1722. /* Higher adjacent rate's throughput is measured */
  1723. if (high_tpt != IL_INVALID_VALUE) {
  1724. /* Higher rate has better throughput */
  1725. if (high_tpt > current_tpt && sr >= RATE_INCREASE_TH)
  1726. scale_action = 1;
  1727. else
  1728. scale_action = 0;
  1729. /* Lower adjacent rate's throughput is measured */
  1730. } else if (low_tpt != IL_INVALID_VALUE) {
  1731. /* Lower rate has better throughput */
  1732. if (low_tpt > current_tpt) {
  1733. D_RATE("decrease rate because of low tpt\n");
  1734. scale_action = -1;
  1735. } else if (sr >= RATE_INCREASE_TH) {
  1736. scale_action = 1;
  1737. }
  1738. }
  1739. }
  1740. /* Sanity check; asked for decrease, but success rate or throughput
  1741. * has been good at old rate. Don't change it. */
  1742. if (scale_action == -1 && low != RATE_INVALID &&
  1743. (sr > RATE_HIGH_TH || current_tpt > 100 * tbl->expected_tpt[low]))
  1744. scale_action = 0;
  1745. switch (scale_action) {
  1746. case -1:
  1747. /* Decrease starting rate, update uCode's rate table */
  1748. if (low != RATE_INVALID) {
  1749. update_lq = 1;
  1750. idx = low;
  1751. }
  1752. break;
  1753. case 1:
  1754. /* Increase starting rate, update uCode's rate table */
  1755. if (high != RATE_INVALID) {
  1756. update_lq = 1;
  1757. idx = high;
  1758. }
  1759. break;
  1760. case 0:
  1761. /* No change */
  1762. default:
  1763. break;
  1764. }
  1765. D_RATE("choose rate scale idx %d action %d low %d " "high %d type %d\n",
  1766. idx, scale_action, low, high, tbl->lq_type);
  1767. lq_update:
  1768. /* Replace uCode's rate table for the destination station. */
  1769. if (update_lq)
  1770. il4965_rs_update_rate_tbl(il, lq_sta, tbl, idx, is_green);
  1771. /* Should we stay with this modulation mode,
  1772. * or search for a new one? */
  1773. il4965_rs_stay_in_table(lq_sta, false);
  1774. /*
  1775. * Search for new modulation mode if we're:
  1776. * 1) Not changing rates right now
  1777. * 2) Not just finishing up a search
  1778. * 3) Allowing a new search
  1779. */
  1780. if (!update_lq && !done_search && !lq_sta->stay_in_tbl && win->counter) {
  1781. /* Save current throughput to compare with "search" throughput */
  1782. lq_sta->last_tpt = current_tpt;
  1783. /* Select a new "search" modulation mode to try.
  1784. * If one is found, set up the new "search" table. */
  1785. if (is_legacy(tbl->lq_type))
  1786. il4965_rs_move_legacy_other(il, lq_sta, conf, sta, idx);
  1787. else if (is_siso(tbl->lq_type))
  1788. il4965_rs_move_siso_to_other(il, lq_sta, conf, sta,
  1789. idx);
  1790. else /* (is_mimo2(tbl->lq_type)) */
  1791. il4965_rs_move_mimo2_to_other(il, lq_sta, conf, sta,
  1792. idx);
  1793. /* If new "search" mode was selected, set up in uCode table */
  1794. if (lq_sta->search_better_tbl) {
  1795. /* Access the "search" table, clear its history. */
  1796. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1797. for (i = 0; i < RATE_COUNT; i++)
  1798. il4965_rs_rate_scale_clear_win(&(tbl->win[i]));
  1799. /* Use new "search" start rate */
  1800. idx = il4965_hwrate_to_plcp_idx(tbl->current_rate);
  1801. D_RATE("Switch current mcs: %X idx: %d\n",
  1802. tbl->current_rate, idx);
  1803. il4965_rs_fill_link_cmd(il, lq_sta, tbl->current_rate);
  1804. il_send_lq_cmd(il, &lq_sta->lq, CMD_ASYNC, false);
  1805. } else
  1806. done_search = 1;
  1807. }
  1808. if (done_search && !lq_sta->stay_in_tbl) {
  1809. /* If the "active" (non-search) mode was legacy,
  1810. * and we've tried switching antennas,
  1811. * but we haven't been able to try HT modes (not available),
  1812. * stay with best antenna legacy modulation for a while
  1813. * before next round of mode comparisons. */
  1814. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1815. if (is_legacy(tbl1->lq_type) && !conf_is_ht(conf) &&
  1816. lq_sta->action_counter > tbl1->max_search) {
  1817. D_RATE("LQ: STAY in legacy table\n");
  1818. il4965_rs_set_stay_in_table(il, 1, lq_sta);
  1819. }
  1820. /* If we're in an HT mode, and all 3 mode switch actions
  1821. * have been tried and compared, stay in this best modulation
  1822. * mode for a while before next round of mode comparisons. */
  1823. if (lq_sta->enable_counter &&
  1824. lq_sta->action_counter >= tbl1->max_search) {
  1825. if (lq_sta->last_tpt > IL_AGG_TPT_THREHOLD &&
  1826. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  1827. tid != MAX_TID_COUNT) {
  1828. tid_data =
  1829. &il->stations[lq_sta->lq.sta_id].tid[tid];
  1830. if (tid_data->agg.state == IL_AGG_OFF) {
  1831. D_RATE("try to aggregate tid %d\n",
  1832. tid);
  1833. il4965_rs_tl_turn_on_agg(il, tid,
  1834. lq_sta, sta);
  1835. }
  1836. }
  1837. il4965_rs_set_stay_in_table(il, 0, lq_sta);
  1838. }
  1839. }
  1840. out:
  1841. tbl->current_rate =
  1842. il4965_rate_n_flags_from_tbl(il, tbl, idx, is_green);
  1843. i = idx;
  1844. lq_sta->last_txrate_idx = i;
  1845. }
  1846. /**
  1847. * il4965_rs_initialize_lq - Initialize a station's hardware rate table
  1848. *
  1849. * The uCode's station table contains a table of fallback rates
  1850. * for automatic fallback during transmission.
  1851. *
  1852. * NOTE: This sets up a default set of values. These will be replaced later
  1853. * if the driver's iwl-4965-rs rate scaling algorithm is used, instead of
  1854. * rc80211_simple.
  1855. *
  1856. * NOTE: Run C_ADD_STA command to set up station table entry, before
  1857. * calling this function (which runs C_TX_LINK_QUALITY_CMD,
  1858. * which requires station table entry to exist).
  1859. */
  1860. static void
  1861. il4965_rs_initialize_lq(struct il_priv *il, struct ieee80211_conf *conf,
  1862. struct ieee80211_sta *sta, struct il_lq_sta *lq_sta)
  1863. {
  1864. struct il_scale_tbl_info *tbl;
  1865. int rate_idx;
  1866. int i;
  1867. u32 rate;
  1868. u8 use_green = il4965_rs_use_green(il, sta);
  1869. u8 active_tbl = 0;
  1870. u8 valid_tx_ant;
  1871. struct il_station_priv *sta_priv;
  1872. if (!sta || !lq_sta)
  1873. return;
  1874. sta_priv = (void *)sta->drv_priv;
  1875. i = lq_sta->last_txrate_idx;
  1876. valid_tx_ant = il->hw_params.valid_tx_ant;
  1877. if (!lq_sta->search_better_tbl)
  1878. active_tbl = lq_sta->active_tbl;
  1879. else
  1880. active_tbl = 1 - lq_sta->active_tbl;
  1881. tbl = &(lq_sta->lq_info[active_tbl]);
  1882. if (i < 0 || i >= RATE_COUNT)
  1883. i = 0;
  1884. rate = il_rates[i].plcp;
  1885. tbl->ant_type = il4965_first_antenna(valid_tx_ant);
  1886. rate |= tbl->ant_type << RATE_MCS_ANT_POS;
  1887. if (i >= IL_FIRST_CCK_RATE && i <= IL_LAST_CCK_RATE)
  1888. rate |= RATE_MCS_CCK_MSK;
  1889. il4965_rs_get_tbl_info_from_mcs(rate, il->band, tbl, &rate_idx);
  1890. if (!il4965_rs_is_valid_ant(valid_tx_ant, tbl->ant_type))
  1891. il4965_rs_toggle_antenna(valid_tx_ant, &rate, tbl);
  1892. rate = il4965_rate_n_flags_from_tbl(il, tbl, rate_idx, use_green);
  1893. tbl->current_rate = rate;
  1894. il4965_rs_set_expected_tpt_table(lq_sta, tbl);
  1895. il4965_rs_fill_link_cmd(NULL, lq_sta, rate);
  1896. il->stations[lq_sta->lq.sta_id].lq = &lq_sta->lq;
  1897. il_send_lq_cmd(il, &lq_sta->lq, CMD_SYNC, true);
  1898. }
  1899. static void
  1900. il4965_rs_get_rate(void *il_r, struct ieee80211_sta *sta, void *il_sta,
  1901. struct ieee80211_tx_rate_control *txrc)
  1902. {
  1903. struct sk_buff *skb = txrc->skb;
  1904. struct ieee80211_supported_band *sband = txrc->sband;
  1905. struct il_priv *il __maybe_unused = (struct il_priv *)il_r;
  1906. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1907. struct il_lq_sta *lq_sta = il_sta;
  1908. int rate_idx;
  1909. D_RATE("rate scale calculate new rate for skb\n");
  1910. /* Get max rate if user set max rate */
  1911. if (lq_sta) {
  1912. lq_sta->max_rate_idx = txrc->max_rate_idx;
  1913. if (sband->band == IEEE80211_BAND_5GHZ &&
  1914. lq_sta->max_rate_idx != -1)
  1915. lq_sta->max_rate_idx += IL_FIRST_OFDM_RATE;
  1916. if (lq_sta->max_rate_idx < 0 ||
  1917. lq_sta->max_rate_idx >= RATE_COUNT)
  1918. lq_sta->max_rate_idx = -1;
  1919. }
  1920. /* Treat uninitialized rate scaling data same as non-existing. */
  1921. if (lq_sta && !lq_sta->drv) {
  1922. D_RATE("Rate scaling not initialized yet.\n");
  1923. il_sta = NULL;
  1924. }
  1925. /* Send management frames and NO_ACK data using lowest rate. */
  1926. if (rate_control_send_low(sta, il_sta, txrc))
  1927. return;
  1928. if (!lq_sta)
  1929. return;
  1930. rate_idx = lq_sta->last_txrate_idx;
  1931. if (lq_sta->last_rate_n_flags & RATE_MCS_HT_MSK) {
  1932. rate_idx -= IL_FIRST_OFDM_RATE;
  1933. /* 6M and 9M shared same MCS idx */
  1934. rate_idx = (rate_idx > 0) ? (rate_idx - 1) : 0;
  1935. if (il4965_rs_extract_rate(lq_sta->last_rate_n_flags) >=
  1936. RATE_MIMO2_6M_PLCP)
  1937. rate_idx = rate_idx + MCS_IDX_PER_STREAM;
  1938. info->control.rates[0].flags = IEEE80211_TX_RC_MCS;
  1939. if (lq_sta->last_rate_n_flags & RATE_MCS_SGI_MSK)
  1940. info->control.rates[0].flags |=
  1941. IEEE80211_TX_RC_SHORT_GI;
  1942. if (lq_sta->last_rate_n_flags & RATE_MCS_DUP_MSK)
  1943. info->control.rates[0].flags |=
  1944. IEEE80211_TX_RC_DUP_DATA;
  1945. if (lq_sta->last_rate_n_flags & RATE_MCS_HT40_MSK)
  1946. info->control.rates[0].flags |=
  1947. IEEE80211_TX_RC_40_MHZ_WIDTH;
  1948. if (lq_sta->last_rate_n_flags & RATE_MCS_GF_MSK)
  1949. info->control.rates[0].flags |=
  1950. IEEE80211_TX_RC_GREEN_FIELD;
  1951. } else {
  1952. /* Check for invalid rates */
  1953. if (rate_idx < 0 || rate_idx >= RATE_COUNT_LEGACY ||
  1954. (sband->band == IEEE80211_BAND_5GHZ &&
  1955. rate_idx < IL_FIRST_OFDM_RATE))
  1956. rate_idx = rate_lowest_index(sband, sta);
  1957. /* On valid 5 GHz rate, adjust idx */
  1958. else if (sband->band == IEEE80211_BAND_5GHZ)
  1959. rate_idx -= IL_FIRST_OFDM_RATE;
  1960. info->control.rates[0].flags = 0;
  1961. }
  1962. info->control.rates[0].idx = rate_idx;
  1963. }
  1964. static void *
  1965. il4965_rs_alloc_sta(void *il_rate, struct ieee80211_sta *sta, gfp_t gfp)
  1966. {
  1967. struct il_station_priv *sta_priv =
  1968. (struct il_station_priv *)sta->drv_priv;
  1969. struct il_priv *il;
  1970. il = (struct il_priv *)il_rate;
  1971. D_RATE("create station rate scale win\n");
  1972. return &sta_priv->lq_sta;
  1973. }
  1974. /*
  1975. * Called after adding a new station to initialize rate scaling
  1976. */
  1977. void
  1978. il4965_rs_rate_init(struct il_priv *il, struct ieee80211_sta *sta, u8 sta_id)
  1979. {
  1980. int i, j;
  1981. struct ieee80211_hw *hw = il->hw;
  1982. struct ieee80211_conf *conf = &il->hw->conf;
  1983. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1984. struct il_station_priv *sta_priv;
  1985. struct il_lq_sta *lq_sta;
  1986. struct ieee80211_supported_band *sband;
  1987. sta_priv = (struct il_station_priv *)sta->drv_priv;
  1988. lq_sta = &sta_priv->lq_sta;
  1989. sband = hw->wiphy->bands[conf->channel->band];
  1990. lq_sta->lq.sta_id = sta_id;
  1991. for (j = 0; j < LQ_SIZE; j++)
  1992. for (i = 0; i < RATE_COUNT; i++)
  1993. il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
  1994. win[i]);
  1995. lq_sta->flush_timer = 0;
  1996. lq_sta->supp_rates = sta->supp_rates[sband->band];
  1997. for (j = 0; j < LQ_SIZE; j++)
  1998. for (i = 0; i < RATE_COUNT; i++)
  1999. il4965_rs_rate_scale_clear_win(&lq_sta->lq_info[j].
  2000. win[i]);
  2001. D_RATE("LQ:" "*** rate scale station global init for station %d ***\n",
  2002. sta_id);
  2003. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2004. * the lowest or the highest rate.. Could consider using RSSI from
  2005. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2006. * after assoc.. */
  2007. lq_sta->is_dup = 0;
  2008. lq_sta->max_rate_idx = -1;
  2009. lq_sta->missed_rate_counter = IL_MISSED_RATE_MAX;
  2010. lq_sta->is_green = il4965_rs_use_green(il, sta);
  2011. lq_sta->active_legacy_rate = il->active_rate & ~(0x1000);
  2012. lq_sta->band = il->band;
  2013. /*
  2014. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  2015. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  2016. */
  2017. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2018. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2019. lq_sta->active_siso_rate &= ~((u16) 0x2);
  2020. lq_sta->active_siso_rate <<= IL_FIRST_OFDM_RATE;
  2021. /* Same here */
  2022. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2023. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2024. lq_sta->active_mimo2_rate &= ~((u16) 0x2);
  2025. lq_sta->active_mimo2_rate <<= IL_FIRST_OFDM_RATE;
  2026. /* These values will be overridden later */
  2027. lq_sta->lq.general_params.single_stream_ant_msk =
  2028. il4965_first_antenna(il->hw_params.valid_tx_ant);
  2029. lq_sta->lq.general_params.dual_stream_ant_msk =
  2030. il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
  2031. valid_tx_ant);
  2032. if (!lq_sta->lq.general_params.dual_stream_ant_msk) {
  2033. lq_sta->lq.general_params.dual_stream_ant_msk = ANT_AB;
  2034. } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
  2035. lq_sta->lq.general_params.dual_stream_ant_msk =
  2036. il->hw_params.valid_tx_ant;
  2037. }
  2038. /* as default allow aggregation for all tids */
  2039. lq_sta->tx_agg_tid_en = IL_AGG_ALL_TID;
  2040. lq_sta->drv = il;
  2041. /* Set last_txrate_idx to lowest rate */
  2042. lq_sta->last_txrate_idx = rate_lowest_index(sband, sta);
  2043. if (sband->band == IEEE80211_BAND_5GHZ)
  2044. lq_sta->last_txrate_idx += IL_FIRST_OFDM_RATE;
  2045. lq_sta->is_agg = 0;
  2046. #ifdef CONFIG_MAC80211_DEBUGFS
  2047. lq_sta->dbg_fixed_rate = 0;
  2048. #endif
  2049. il4965_rs_initialize_lq(il, conf, sta, lq_sta);
  2050. }
  2051. static void
  2052. il4965_rs_fill_link_cmd(struct il_priv *il, struct il_lq_sta *lq_sta,
  2053. u32 new_rate)
  2054. {
  2055. struct il_scale_tbl_info tbl_type;
  2056. int idx = 0;
  2057. int rate_idx;
  2058. int repeat_rate = 0;
  2059. u8 ant_toggle_cnt = 0;
  2060. u8 use_ht_possible = 1;
  2061. u8 valid_tx_ant = 0;
  2062. struct il_link_quality_cmd *lq_cmd = &lq_sta->lq;
  2063. /* Override starting rate (idx 0) if needed for debug purposes */
  2064. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2065. /* Interpret new_rate (rate_n_flags) */
  2066. il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band, &tbl_type,
  2067. &rate_idx);
  2068. /* How many times should we repeat the initial rate? */
  2069. if (is_legacy(tbl_type.lq_type)) {
  2070. ant_toggle_cnt = 1;
  2071. repeat_rate = IL_NUMBER_TRY;
  2072. } else {
  2073. repeat_rate = IL_HT_NUMBER_TRY;
  2074. }
  2075. lq_cmd->general_params.mimo_delimiter =
  2076. is_mimo(tbl_type.lq_type) ? 1 : 0;
  2077. /* Fill 1st table entry (idx 0) */
  2078. lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
  2079. if (il4965_num_of_ant(tbl_type.ant_type) == 1) {
  2080. lq_cmd->general_params.single_stream_ant_msk =
  2081. tbl_type.ant_type;
  2082. } else if (il4965_num_of_ant(tbl_type.ant_type) == 2) {
  2083. lq_cmd->general_params.dual_stream_ant_msk = tbl_type.ant_type;
  2084. }
  2085. /* otherwise we don't modify the existing value */
  2086. idx++;
  2087. repeat_rate--;
  2088. if (il)
  2089. valid_tx_ant = il->hw_params.valid_tx_ant;
  2090. /* Fill rest of rate table */
  2091. while (idx < LINK_QUAL_MAX_RETRY_NUM) {
  2092. /* Repeat initial/next rate.
  2093. * For legacy IL_NUMBER_TRY == 1, this loop will not execute.
  2094. * For HT IL_HT_NUMBER_TRY == 3, this executes twice. */
  2095. while (repeat_rate > 0 && idx < LINK_QUAL_MAX_RETRY_NUM) {
  2096. if (is_legacy(tbl_type.lq_type)) {
  2097. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2098. ant_toggle_cnt++;
  2099. else if (il &&
  2100. il4965_rs_toggle_antenna(valid_tx_ant,
  2101. &new_rate,
  2102. &tbl_type))
  2103. ant_toggle_cnt = 1;
  2104. }
  2105. /* Override next rate if needed for debug purposes */
  2106. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2107. /* Fill next table entry */
  2108. lq_cmd->rs_table[idx].rate_n_flags =
  2109. cpu_to_le32(new_rate);
  2110. repeat_rate--;
  2111. idx++;
  2112. }
  2113. il4965_rs_get_tbl_info_from_mcs(new_rate, lq_sta->band,
  2114. &tbl_type, &rate_idx);
  2115. /* Indicate to uCode which entries might be MIMO.
  2116. * If initial rate was MIMO, this will finally end up
  2117. * as (IL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  2118. if (is_mimo(tbl_type.lq_type))
  2119. lq_cmd->general_params.mimo_delimiter = idx;
  2120. /* Get next rate */
  2121. new_rate =
  2122. il4965_rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
  2123. use_ht_possible);
  2124. /* How many times should we repeat the next rate? */
  2125. if (is_legacy(tbl_type.lq_type)) {
  2126. if (ant_toggle_cnt < NUM_TRY_BEFORE_ANT_TOGGLE)
  2127. ant_toggle_cnt++;
  2128. else if (il &&
  2129. il4965_rs_toggle_antenna(valid_tx_ant,
  2130. &new_rate, &tbl_type))
  2131. ant_toggle_cnt = 1;
  2132. repeat_rate = IL_NUMBER_TRY;
  2133. } else {
  2134. repeat_rate = IL_HT_NUMBER_TRY;
  2135. }
  2136. /* Don't allow HT rates after next pass.
  2137. * il4965_rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  2138. use_ht_possible = 0;
  2139. /* Override next rate if needed for debug purposes */
  2140. il4965_rs_dbgfs_set_mcs(lq_sta, &new_rate, idx);
  2141. /* Fill next table entry */
  2142. lq_cmd->rs_table[idx].rate_n_flags = cpu_to_le32(new_rate);
  2143. idx++;
  2144. repeat_rate--;
  2145. }
  2146. lq_cmd->agg_params.agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2147. lq_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2148. lq_cmd->agg_params.agg_time_limit =
  2149. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2150. }
  2151. static void *
  2152. il4965_rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2153. {
  2154. return hw->priv;
  2155. }
  2156. /* rate scale requires free function to be implemented */
  2157. static void
  2158. il4965_rs_free(void *il_rate)
  2159. {
  2160. return;
  2161. }
  2162. static void
  2163. il4965_rs_free_sta(void *il_r, struct ieee80211_sta *sta, void *il_sta)
  2164. {
  2165. struct il_priv *il __maybe_unused = il_r;
  2166. D_RATE("enter\n");
  2167. D_RATE("leave\n");
  2168. }
  2169. #ifdef CONFIG_MAC80211_DEBUGFS
  2170. static void
  2171. il4965_rs_dbgfs_set_mcs(struct il_lq_sta *lq_sta, u32 * rate_n_flags, int idx)
  2172. {
  2173. struct il_priv *il;
  2174. u8 valid_tx_ant;
  2175. u8 ant_sel_tx;
  2176. il = lq_sta->drv;
  2177. valid_tx_ant = il->hw_params.valid_tx_ant;
  2178. if (lq_sta->dbg_fixed_rate) {
  2179. ant_sel_tx =
  2180. ((lq_sta->
  2181. dbg_fixed_rate & RATE_MCS_ANT_ABC_MSK) >>
  2182. RATE_MCS_ANT_POS);
  2183. if ((valid_tx_ant & ant_sel_tx) == ant_sel_tx) {
  2184. *rate_n_flags = lq_sta->dbg_fixed_rate;
  2185. D_RATE("Fixed rate ON\n");
  2186. } else {
  2187. lq_sta->dbg_fixed_rate = 0;
  2188. IL_ERR
  2189. ("Invalid antenna selection 0x%X, Valid is 0x%X\n",
  2190. ant_sel_tx, valid_tx_ant);
  2191. D_RATE("Fixed rate OFF\n");
  2192. }
  2193. } else {
  2194. D_RATE("Fixed rate OFF\n");
  2195. }
  2196. }
  2197. static ssize_t
  2198. il4965_rs_sta_dbgfs_scale_table_write(struct file *file,
  2199. const char __user *user_buf,
  2200. size_t count, loff_t *ppos)
  2201. {
  2202. struct il_lq_sta *lq_sta = file->private_data;
  2203. struct il_priv *il;
  2204. char buf[64];
  2205. size_t buf_size;
  2206. u32 parsed_rate;
  2207. il = lq_sta->drv;
  2208. memset(buf, 0, sizeof(buf));
  2209. buf_size = min(count, sizeof(buf) - 1);
  2210. if (copy_from_user(buf, user_buf, buf_size))
  2211. return -EFAULT;
  2212. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2213. lq_sta->dbg_fixed_rate = parsed_rate;
  2214. else
  2215. lq_sta->dbg_fixed_rate = 0;
  2216. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  2217. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2218. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2219. D_RATE("sta_id %d rate 0x%X\n", lq_sta->lq.sta_id,
  2220. lq_sta->dbg_fixed_rate);
  2221. if (lq_sta->dbg_fixed_rate) {
  2222. il4965_rs_fill_link_cmd(NULL, lq_sta, lq_sta->dbg_fixed_rate);
  2223. il_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC, false);
  2224. }
  2225. return count;
  2226. }
  2227. static ssize_t
  2228. il4965_rs_sta_dbgfs_scale_table_read(struct file *file, char __user *user_buf,
  2229. size_t count, loff_t *ppos)
  2230. {
  2231. char *buff;
  2232. int desc = 0;
  2233. int i = 0;
  2234. int idx = 0;
  2235. ssize_t ret;
  2236. struct il_lq_sta *lq_sta = file->private_data;
  2237. struct il_priv *il;
  2238. struct il_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2239. il = lq_sta->drv;
  2240. buff = kmalloc(1024, GFP_KERNEL);
  2241. if (!buff)
  2242. return -ENOMEM;
  2243. desc += sprintf(buff + desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2244. desc +=
  2245. sprintf(buff + desc, "failed=%d success=%d rate=0%X\n",
  2246. lq_sta->total_failed, lq_sta->total_success,
  2247. lq_sta->active_legacy_rate);
  2248. desc +=
  2249. sprintf(buff + desc, "fixed rate 0x%X\n", lq_sta->dbg_fixed_rate);
  2250. desc +=
  2251. sprintf(buff + desc, "valid_tx_ant %s%s%s\n",
  2252. (il->hw_params.valid_tx_ant & ANT_A) ? "ANT_A," : "",
  2253. (il->hw_params.valid_tx_ant & ANT_B) ? "ANT_B," : "",
  2254. (il->hw_params.valid_tx_ant & ANT_C) ? "ANT_C" : "");
  2255. desc +=
  2256. sprintf(buff + desc, "lq type %s\n",
  2257. (is_legacy(tbl->lq_type)) ? "legacy" : "HT");
  2258. if (is_Ht(tbl->lq_type)) {
  2259. desc +=
  2260. sprintf(buff + desc, " %s",
  2261. (is_siso(tbl->lq_type)) ? "SISO" : "MIMO2");
  2262. desc +=
  2263. sprintf(buff + desc, " %s",
  2264. (tbl->is_ht40) ? "40MHz" : "20MHz");
  2265. desc +=
  2266. sprintf(buff + desc, " %s %s %s\n",
  2267. (tbl->is_SGI) ? "SGI" : "",
  2268. (lq_sta->is_green) ? "GF enabled" : "",
  2269. (lq_sta->is_agg) ? "AGG on" : "");
  2270. }
  2271. desc +=
  2272. sprintf(buff + desc, "last tx rate=0x%X\n",
  2273. lq_sta->last_rate_n_flags);
  2274. desc +=
  2275. sprintf(buff + desc,
  2276. "general:" "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2277. lq_sta->lq.general_params.flags,
  2278. lq_sta->lq.general_params.mimo_delimiter,
  2279. lq_sta->lq.general_params.single_stream_ant_msk,
  2280. lq_sta->lq.general_params.dual_stream_ant_msk);
  2281. desc +=
  2282. sprintf(buff + desc,
  2283. "agg:"
  2284. "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2285. le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
  2286. lq_sta->lq.agg_params.agg_dis_start_th,
  2287. lq_sta->lq.agg_params.agg_frame_cnt_limit);
  2288. desc +=
  2289. sprintf(buff + desc,
  2290. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2291. lq_sta->lq.general_params.start_rate_idx[0],
  2292. lq_sta->lq.general_params.start_rate_idx[1],
  2293. lq_sta->lq.general_params.start_rate_idx[2],
  2294. lq_sta->lq.general_params.start_rate_idx[3]);
  2295. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2296. idx =
  2297. il4965_hwrate_to_plcp_idx(le32_to_cpu
  2298. (lq_sta->lq.rs_table[i].
  2299. rate_n_flags));
  2300. if (is_legacy(tbl->lq_type)) {
  2301. desc +=
  2302. sprintf(buff + desc, " rate[%d] 0x%X %smbps\n", i,
  2303. le32_to_cpu(lq_sta->lq.rs_table[i].
  2304. rate_n_flags),
  2305. il_rate_mcs[idx].mbps);
  2306. } else {
  2307. desc +=
  2308. sprintf(buff + desc, " rate[%d] 0x%X %smbps (%s)\n",
  2309. i,
  2310. le32_to_cpu(lq_sta->lq.rs_table[i].
  2311. rate_n_flags),
  2312. il_rate_mcs[idx].mbps,
  2313. il_rate_mcs[idx].mcs);
  2314. }
  2315. }
  2316. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2317. kfree(buff);
  2318. return ret;
  2319. }
  2320. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2321. .write = il4965_rs_sta_dbgfs_scale_table_write,
  2322. .read = il4965_rs_sta_dbgfs_scale_table_read,
  2323. .open = simple_open,
  2324. .llseek = default_llseek,
  2325. };
  2326. static ssize_t
  2327. il4965_rs_sta_dbgfs_stats_table_read(struct file *file, char __user *user_buf,
  2328. size_t count, loff_t *ppos)
  2329. {
  2330. char *buff;
  2331. int desc = 0;
  2332. int i, j;
  2333. ssize_t ret;
  2334. struct il_lq_sta *lq_sta = file->private_data;
  2335. buff = kmalloc(1024, GFP_KERNEL);
  2336. if (!buff)
  2337. return -ENOMEM;
  2338. for (i = 0; i < LQ_SIZE; i++) {
  2339. desc +=
  2340. sprintf(buff + desc,
  2341. "%s type=%d SGI=%d HT40=%d DUP=%d GF=%d\n"
  2342. "rate=0x%X\n", lq_sta->active_tbl == i ? "*" : "x",
  2343. lq_sta->lq_info[i].lq_type,
  2344. lq_sta->lq_info[i].is_SGI,
  2345. lq_sta->lq_info[i].is_ht40,
  2346. lq_sta->lq_info[i].is_dup, lq_sta->is_green,
  2347. lq_sta->lq_info[i].current_rate);
  2348. for (j = 0; j < RATE_COUNT; j++) {
  2349. desc +=
  2350. sprintf(buff + desc,
  2351. "counter=%d success=%d %%=%d\n",
  2352. lq_sta->lq_info[i].win[j].counter,
  2353. lq_sta->lq_info[i].win[j].success_counter,
  2354. lq_sta->lq_info[i].win[j].success_ratio);
  2355. }
  2356. }
  2357. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2358. kfree(buff);
  2359. return ret;
  2360. }
  2361. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2362. .read = il4965_rs_sta_dbgfs_stats_table_read,
  2363. .open = simple_open,
  2364. .llseek = default_llseek,
  2365. };
  2366. static ssize_t
  2367. il4965_rs_sta_dbgfs_rate_scale_data_read(struct file *file,
  2368. char __user *user_buf, size_t count,
  2369. loff_t *ppos)
  2370. {
  2371. char buff[120];
  2372. int desc = 0;
  2373. struct il_lq_sta *lq_sta = file->private_data;
  2374. struct il_scale_tbl_info *tbl = &lq_sta->lq_info[lq_sta->active_tbl];
  2375. if (is_Ht(tbl->lq_type))
  2376. desc +=
  2377. sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
  2378. tbl->expected_tpt[lq_sta->last_txrate_idx]);
  2379. else
  2380. desc +=
  2381. sprintf(buff + desc, "Bit Rate= %d Mb/s\n",
  2382. il_rates[lq_sta->last_txrate_idx].ieee >> 1);
  2383. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2384. }
  2385. static const struct file_operations rs_sta_dbgfs_rate_scale_data_ops = {
  2386. .read = il4965_rs_sta_dbgfs_rate_scale_data_read,
  2387. .open = simple_open,
  2388. .llseek = default_llseek,
  2389. };
  2390. static void
  2391. il4965_rs_add_debugfs(void *il, void *il_sta, struct dentry *dir)
  2392. {
  2393. struct il_lq_sta *lq_sta = il_sta;
  2394. lq_sta->rs_sta_dbgfs_scale_table_file =
  2395. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2396. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2397. lq_sta->rs_sta_dbgfs_stats_table_file =
  2398. debugfs_create_file("rate_stats_table", S_IRUSR, dir, lq_sta,
  2399. &rs_sta_dbgfs_stats_table_ops);
  2400. lq_sta->rs_sta_dbgfs_rate_scale_data_file =
  2401. debugfs_create_file("rate_scale_data", S_IRUSR, dir, lq_sta,
  2402. &rs_sta_dbgfs_rate_scale_data_ops);
  2403. lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file =
  2404. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2405. &lq_sta->tx_agg_tid_en);
  2406. }
  2407. static void
  2408. il4965_rs_remove_debugfs(void *il, void *il_sta)
  2409. {
  2410. struct il_lq_sta *lq_sta = il_sta;
  2411. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2412. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2413. debugfs_remove(lq_sta->rs_sta_dbgfs_rate_scale_data_file);
  2414. debugfs_remove(lq_sta->rs_sta_dbgfs_tx_agg_tid_en_file);
  2415. }
  2416. #endif
  2417. /*
  2418. * Initialization of rate scaling information is done by driver after
  2419. * the station is added. Since mac80211 calls this function before a
  2420. * station is added we ignore it.
  2421. */
  2422. static void
  2423. il4965_rs_rate_init_stub(void *il_r, struct ieee80211_supported_band *sband,
  2424. struct ieee80211_sta *sta, void *il_sta)
  2425. {
  2426. }
  2427. static struct rate_control_ops rs_4965_ops = {
  2428. .module = NULL,
  2429. .name = IL4965_RS_NAME,
  2430. .tx_status = il4965_rs_tx_status,
  2431. .get_rate = il4965_rs_get_rate,
  2432. .rate_init = il4965_rs_rate_init_stub,
  2433. .alloc = il4965_rs_alloc,
  2434. .free = il4965_rs_free,
  2435. .alloc_sta = il4965_rs_alloc_sta,
  2436. .free_sta = il4965_rs_free_sta,
  2437. #ifdef CONFIG_MAC80211_DEBUGFS
  2438. .add_sta_debugfs = il4965_rs_add_debugfs,
  2439. .remove_sta_debugfs = il4965_rs_remove_debugfs,
  2440. #endif
  2441. };
  2442. int
  2443. il4965_rate_control_register(void)
  2444. {
  2445. return ieee80211_rate_control_register(&rs_4965_ops);
  2446. }
  2447. void
  2448. il4965_rate_control_unregister(void)
  2449. {
  2450. ieee80211_rate_control_unregister(&rs_4965_ops);
  2451. }