iwl-agn-rs.c 96 KB

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