4965-rs.c 79 KB

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