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