rs.c 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2005 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of version 2 of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with
  16. * this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  18. *
  19. * The full GNU General Public License is included in this distribution in the
  20. * file called LICENSE.
  21. *
  22. * Contact Information:
  23. * Intel Linux Wireless <ilw@linux.intel.com>
  24. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  25. *
  26. *****************************************************************************/
  27. #include <linux/kernel.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/slab.h>
  30. #include <net/mac80211.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "rs.h"
  36. #include "fw-api.h"
  37. #include "sta.h"
  38. #include "iwl-op-mode.h"
  39. #include "mvm.h"
  40. #define RS_NAME "iwl-mvm-rs"
  41. #define NUM_TRY_BEFORE_ANT_TOGGLE 1
  42. #define RS_LEGACY_RETRIES_PER_RATE 1
  43. #define RS_HT_VHT_RETRIES_PER_RATE 2
  44. #define RS_HT_VHT_RETRIES_PER_RATE_TW 1
  45. #define RS_INITIAL_MIMO_NUM_RATES 3
  46. #define RS_INITIAL_SISO_NUM_RATES 3
  47. #define RS_INITIAL_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
  48. #define RS_SECONDARY_LEGACY_NUM_RATES LINK_QUAL_MAX_RETRY_NUM
  49. #define RS_SECONDARY_SISO_NUM_RATES 3
  50. #define RS_SECONDARY_SISO_RETRIES 1
  51. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  52. #define IWL_RATE_MIN_FAILURE_TH 3 /* min failures to calc tpt */
  53. #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  54. /* max allowed rate miss before sync LQ cmd */
  55. #define IWL_MISSED_RATE_MAX 15
  56. #define RS_STAY_IN_COLUMN_TIMEOUT (5*HZ)
  57. #define RS_IDLE_TIMEOUT (5*HZ)
  58. static u8 rs_ht_to_legacy[] = {
  59. [IWL_RATE_MCS_0_INDEX] = IWL_RATE_6M_INDEX,
  60. [IWL_RATE_MCS_1_INDEX] = IWL_RATE_9M_INDEX,
  61. [IWL_RATE_MCS_2_INDEX] = IWL_RATE_12M_INDEX,
  62. [IWL_RATE_MCS_3_INDEX] = IWL_RATE_18M_INDEX,
  63. [IWL_RATE_MCS_4_INDEX] = IWL_RATE_24M_INDEX,
  64. [IWL_RATE_MCS_5_INDEX] = IWL_RATE_36M_INDEX,
  65. [IWL_RATE_MCS_6_INDEX] = IWL_RATE_48M_INDEX,
  66. [IWL_RATE_MCS_7_INDEX] = IWL_RATE_54M_INDEX,
  67. [IWL_RATE_MCS_8_INDEX] = IWL_RATE_54M_INDEX,
  68. [IWL_RATE_MCS_9_INDEX] = IWL_RATE_54M_INDEX,
  69. };
  70. static const u8 ant_toggle_lookup[] = {
  71. [ANT_NONE] = ANT_NONE,
  72. [ANT_A] = ANT_B,
  73. [ANT_B] = ANT_C,
  74. [ANT_AB] = ANT_BC,
  75. [ANT_C] = ANT_A,
  76. [ANT_AC] = ANT_AB,
  77. [ANT_BC] = ANT_AC,
  78. [ANT_ABC] = ANT_ABC,
  79. };
  80. #define IWL_DECLARE_RATE_INFO(r, s, rp, rn) \
  81. [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP, \
  82. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  83. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  84. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  85. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP,\
  86. IWL_RATE_##rp##M_INDEX, \
  87. IWL_RATE_##rn##M_INDEX }
  88. #define IWL_DECLARE_MCS_RATE(s) \
  89. [IWL_RATE_MCS_##s##_INDEX] = { IWL_RATE_INVM_PLCP, \
  90. IWL_RATE_HT_SISO_MCS_##s##_PLCP, \
  91. IWL_RATE_HT_MIMO2_MCS_##s##_PLCP, \
  92. IWL_RATE_VHT_SISO_MCS_##s##_PLCP, \
  93. IWL_RATE_VHT_MIMO2_MCS_##s##_PLCP, \
  94. IWL_RATE_INVM_INDEX, \
  95. IWL_RATE_INVM_INDEX }
  96. /*
  97. * Parameter order:
  98. * rate, ht rate, prev rate, next rate
  99. *
  100. * If there isn't a valid next or previous rate then INV is used which
  101. * maps to IWL_RATE_INVALID
  102. *
  103. */
  104. static const struct iwl_rs_rate_info iwl_rates[IWL_RATE_COUNT] = {
  105. IWL_DECLARE_RATE_INFO(1, INV, INV, 2), /* 1mbps */
  106. IWL_DECLARE_RATE_INFO(2, INV, 1, 5), /* 2mbps */
  107. IWL_DECLARE_RATE_INFO(5, INV, 2, 11), /*5.5mbps */
  108. IWL_DECLARE_RATE_INFO(11, INV, 9, 12), /* 11mbps */
  109. IWL_DECLARE_RATE_INFO(6, 0, 5, 11), /* 6mbps ; MCS 0 */
  110. IWL_DECLARE_RATE_INFO(9, INV, 6, 11), /* 9mbps */
  111. IWL_DECLARE_RATE_INFO(12, 1, 11, 18), /* 12mbps ; MCS 1 */
  112. IWL_DECLARE_RATE_INFO(18, 2, 12, 24), /* 18mbps ; MCS 2 */
  113. IWL_DECLARE_RATE_INFO(24, 3, 18, 36), /* 24mbps ; MCS 3 */
  114. IWL_DECLARE_RATE_INFO(36, 4, 24, 48), /* 36mbps ; MCS 4 */
  115. IWL_DECLARE_RATE_INFO(48, 5, 36, 54), /* 48mbps ; MCS 5 */
  116. IWL_DECLARE_RATE_INFO(54, 6, 48, INV), /* 54mbps ; MCS 6 */
  117. IWL_DECLARE_MCS_RATE(7), /* MCS 7 */
  118. IWL_DECLARE_MCS_RATE(8), /* MCS 8 */
  119. IWL_DECLARE_MCS_RATE(9), /* MCS 9 */
  120. };
  121. enum rs_action {
  122. RS_ACTION_STAY = 0,
  123. RS_ACTION_DOWNSCALE = -1,
  124. RS_ACTION_UPSCALE = 1,
  125. };
  126. enum rs_column_mode {
  127. RS_INVALID = 0,
  128. RS_LEGACY,
  129. RS_SISO,
  130. RS_MIMO2,
  131. };
  132. #define MAX_NEXT_COLUMNS 7
  133. #define MAX_COLUMN_CHECKS 3
  134. typedef bool (*allow_column_func_t) (struct iwl_mvm *mvm,
  135. struct ieee80211_sta *sta,
  136. struct iwl_scale_tbl_info *tbl);
  137. struct rs_tx_column {
  138. enum rs_column_mode mode;
  139. u8 ant;
  140. bool sgi;
  141. enum rs_column next_columns[MAX_NEXT_COLUMNS];
  142. allow_column_func_t checks[MAX_COLUMN_CHECKS];
  143. };
  144. static bool rs_ant_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  145. struct iwl_scale_tbl_info *tbl)
  146. {
  147. return iwl_mvm_bt_coex_is_ant_avail(mvm, tbl->rate.ant);
  148. }
  149. static bool rs_mimo_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  150. struct iwl_scale_tbl_info *tbl)
  151. {
  152. if (!sta->ht_cap.ht_supported)
  153. return false;
  154. if (sta->smps_mode == IEEE80211_SMPS_STATIC)
  155. return false;
  156. if (num_of_ant(mvm->fw->valid_tx_ant) < 2)
  157. return false;
  158. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  159. return false;
  160. return true;
  161. }
  162. static bool rs_siso_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  163. struct iwl_scale_tbl_info *tbl)
  164. {
  165. if (!sta->ht_cap.ht_supported)
  166. return false;
  167. return true;
  168. }
  169. static bool rs_sgi_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  170. struct iwl_scale_tbl_info *tbl)
  171. {
  172. struct rs_rate *rate = &tbl->rate;
  173. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  174. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  175. if (is_ht20(rate) && (ht_cap->cap &
  176. IEEE80211_HT_CAP_SGI_20))
  177. return true;
  178. if (is_ht40(rate) && (ht_cap->cap &
  179. IEEE80211_HT_CAP_SGI_40))
  180. return true;
  181. if (is_ht80(rate) && (vht_cap->cap &
  182. IEEE80211_VHT_CAP_SHORT_GI_80))
  183. return true;
  184. return false;
  185. }
  186. static const struct rs_tx_column rs_tx_columns[] = {
  187. [RS_COLUMN_LEGACY_ANT_A] = {
  188. .mode = RS_LEGACY,
  189. .ant = ANT_A,
  190. .next_columns = {
  191. RS_COLUMN_LEGACY_ANT_B,
  192. RS_COLUMN_SISO_ANT_A,
  193. RS_COLUMN_MIMO2,
  194. RS_COLUMN_INVALID,
  195. RS_COLUMN_INVALID,
  196. RS_COLUMN_INVALID,
  197. RS_COLUMN_INVALID,
  198. },
  199. .checks = {
  200. rs_ant_allow,
  201. },
  202. },
  203. [RS_COLUMN_LEGACY_ANT_B] = {
  204. .mode = RS_LEGACY,
  205. .ant = ANT_B,
  206. .next_columns = {
  207. RS_COLUMN_LEGACY_ANT_A,
  208. RS_COLUMN_SISO_ANT_B,
  209. RS_COLUMN_MIMO2,
  210. RS_COLUMN_INVALID,
  211. RS_COLUMN_INVALID,
  212. RS_COLUMN_INVALID,
  213. RS_COLUMN_INVALID,
  214. },
  215. .checks = {
  216. rs_ant_allow,
  217. },
  218. },
  219. [RS_COLUMN_SISO_ANT_A] = {
  220. .mode = RS_SISO,
  221. .ant = ANT_A,
  222. .next_columns = {
  223. RS_COLUMN_SISO_ANT_B,
  224. RS_COLUMN_MIMO2,
  225. RS_COLUMN_SISO_ANT_A_SGI,
  226. RS_COLUMN_LEGACY_ANT_A,
  227. RS_COLUMN_LEGACY_ANT_B,
  228. RS_COLUMN_INVALID,
  229. RS_COLUMN_INVALID,
  230. },
  231. .checks = {
  232. rs_siso_allow,
  233. rs_ant_allow,
  234. },
  235. },
  236. [RS_COLUMN_SISO_ANT_B] = {
  237. .mode = RS_SISO,
  238. .ant = ANT_B,
  239. .next_columns = {
  240. RS_COLUMN_SISO_ANT_A,
  241. RS_COLUMN_MIMO2,
  242. RS_COLUMN_SISO_ANT_B_SGI,
  243. RS_COLUMN_LEGACY_ANT_A,
  244. RS_COLUMN_LEGACY_ANT_B,
  245. RS_COLUMN_INVALID,
  246. RS_COLUMN_INVALID,
  247. },
  248. .checks = {
  249. rs_siso_allow,
  250. rs_ant_allow,
  251. },
  252. },
  253. [RS_COLUMN_SISO_ANT_A_SGI] = {
  254. .mode = RS_SISO,
  255. .ant = ANT_A,
  256. .sgi = true,
  257. .next_columns = {
  258. RS_COLUMN_SISO_ANT_B_SGI,
  259. RS_COLUMN_MIMO2_SGI,
  260. RS_COLUMN_SISO_ANT_A,
  261. RS_COLUMN_LEGACY_ANT_A,
  262. RS_COLUMN_LEGACY_ANT_B,
  263. RS_COLUMN_INVALID,
  264. RS_COLUMN_INVALID,
  265. },
  266. .checks = {
  267. rs_siso_allow,
  268. rs_ant_allow,
  269. rs_sgi_allow,
  270. },
  271. },
  272. [RS_COLUMN_SISO_ANT_B_SGI] = {
  273. .mode = RS_SISO,
  274. .ant = ANT_B,
  275. .sgi = true,
  276. .next_columns = {
  277. RS_COLUMN_SISO_ANT_A_SGI,
  278. RS_COLUMN_MIMO2_SGI,
  279. RS_COLUMN_SISO_ANT_B,
  280. RS_COLUMN_LEGACY_ANT_A,
  281. RS_COLUMN_LEGACY_ANT_B,
  282. RS_COLUMN_INVALID,
  283. RS_COLUMN_INVALID,
  284. },
  285. .checks = {
  286. rs_siso_allow,
  287. rs_ant_allow,
  288. rs_sgi_allow,
  289. },
  290. },
  291. [RS_COLUMN_MIMO2] = {
  292. .mode = RS_MIMO2,
  293. .ant = ANT_AB,
  294. .next_columns = {
  295. RS_COLUMN_SISO_ANT_A,
  296. RS_COLUMN_MIMO2_SGI,
  297. RS_COLUMN_LEGACY_ANT_A,
  298. RS_COLUMN_LEGACY_ANT_B,
  299. RS_COLUMN_INVALID,
  300. RS_COLUMN_INVALID,
  301. RS_COLUMN_INVALID,
  302. },
  303. .checks = {
  304. rs_mimo_allow,
  305. },
  306. },
  307. [RS_COLUMN_MIMO2_SGI] = {
  308. .mode = RS_MIMO2,
  309. .ant = ANT_AB,
  310. .sgi = true,
  311. .next_columns = {
  312. RS_COLUMN_SISO_ANT_A_SGI,
  313. RS_COLUMN_MIMO2,
  314. RS_COLUMN_LEGACY_ANT_A,
  315. RS_COLUMN_LEGACY_ANT_B,
  316. RS_COLUMN_INVALID,
  317. RS_COLUMN_INVALID,
  318. RS_COLUMN_INVALID,
  319. },
  320. .checks = {
  321. rs_mimo_allow,
  322. rs_sgi_allow,
  323. },
  324. },
  325. };
  326. static inline u8 rs_extract_rate(u32 rate_n_flags)
  327. {
  328. /* also works for HT because bits 7:6 are zero there */
  329. return (u8)(rate_n_flags & RATE_LEGACY_RATE_MSK);
  330. }
  331. static int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
  332. {
  333. int idx = 0;
  334. if (rate_n_flags & RATE_MCS_HT_MSK) {
  335. idx = rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK;
  336. idx += IWL_RATE_MCS_0_INDEX;
  337. /* skip 9M not supported in HT*/
  338. if (idx >= IWL_RATE_9M_INDEX)
  339. idx += 1;
  340. if ((idx >= IWL_FIRST_HT_RATE) && (idx <= IWL_LAST_HT_RATE))
  341. return idx;
  342. } else if (rate_n_flags & RATE_MCS_VHT_MSK) {
  343. idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK;
  344. idx += IWL_RATE_MCS_0_INDEX;
  345. /* skip 9M not supported in VHT*/
  346. if (idx >= IWL_RATE_9M_INDEX)
  347. idx++;
  348. if ((idx >= IWL_FIRST_VHT_RATE) && (idx <= IWL_LAST_VHT_RATE))
  349. return idx;
  350. } else {
  351. /* legacy rate format, search for match in table */
  352. u8 legacy_rate = rs_extract_rate(rate_n_flags);
  353. for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
  354. if (iwl_rates[idx].plcp == legacy_rate)
  355. return idx;
  356. }
  357. return IWL_RATE_INVALID;
  358. }
  359. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  360. struct ieee80211_sta *sta,
  361. struct iwl_lq_sta *lq_sta,
  362. int tid);
  363. static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
  364. struct ieee80211_sta *sta,
  365. struct iwl_lq_sta *lq_sta,
  366. const struct rs_rate *initial_rate);
  367. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search);
  368. /**
  369. * The following tables contain the expected throughput metrics for all rates
  370. *
  371. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  372. *
  373. * where invalid entries are zeros.
  374. *
  375. * CCK rates are only valid in legacy table and will only be used in G
  376. * (2.4 GHz) band.
  377. */
  378. static const u16 expected_tpt_legacy[IWL_RATE_COUNT] = {
  379. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 0, 0, 0
  380. };
  381. /* Expected TpT tables. 4 indexes:
  382. * 0 - NGI, 1 - SGI, 2 - AGG+NGI, 3 - AGG+SGI
  383. */
  384. static const u16 expected_tpt_siso_20MHz[4][IWL_RATE_COUNT] = {
  385. {0, 0, 0, 0, 42, 0, 76, 102, 124, 159, 183, 193, 202, 216, 0},
  386. {0, 0, 0, 0, 46, 0, 82, 110, 132, 168, 192, 202, 210, 225, 0},
  387. {0, 0, 0, 0, 49, 0, 97, 145, 192, 285, 375, 420, 464, 551, 0},
  388. {0, 0, 0, 0, 54, 0, 108, 160, 213, 315, 415, 465, 513, 608, 0},
  389. };
  390. static const u16 expected_tpt_siso_40MHz[4][IWL_RATE_COUNT] = {
  391. {0, 0, 0, 0, 77, 0, 127, 160, 184, 220, 242, 250, 257, 269, 275},
  392. {0, 0, 0, 0, 83, 0, 135, 169, 193, 229, 250, 257, 264, 275, 280},
  393. {0, 0, 0, 0, 101, 0, 199, 295, 389, 570, 744, 828, 911, 1070, 1173},
  394. {0, 0, 0, 0, 112, 0, 220, 326, 429, 629, 819, 912, 1000, 1173, 1284},
  395. };
  396. static const u16 expected_tpt_siso_80MHz[4][IWL_RATE_COUNT] = {
  397. {0, 0, 0, 0, 130, 0, 191, 223, 244, 273, 288, 294, 298, 305, 308},
  398. {0, 0, 0, 0, 138, 0, 200, 231, 251, 279, 293, 298, 302, 308, 312},
  399. {0, 0, 0, 0, 217, 0, 429, 634, 834, 1220, 1585, 1760, 1931, 2258, 2466},
  400. {0, 0, 0, 0, 241, 0, 475, 701, 921, 1343, 1741, 1931, 2117, 2468, 2691},
  401. };
  402. static const u16 expected_tpt_mimo2_20MHz[4][IWL_RATE_COUNT] = {
  403. {0, 0, 0, 0, 74, 0, 123, 155, 179, 213, 235, 243, 250, 261, 0},
  404. {0, 0, 0, 0, 81, 0, 131, 164, 187, 221, 242, 250, 256, 267, 0},
  405. {0, 0, 0, 0, 98, 0, 193, 286, 375, 550, 718, 799, 878, 1032, 0},
  406. {0, 0, 0, 0, 109, 0, 214, 316, 414, 607, 790, 879, 965, 1132, 0},
  407. };
  408. static const u16 expected_tpt_mimo2_40MHz[4][IWL_RATE_COUNT] = {
  409. {0, 0, 0, 0, 123, 0, 182, 214, 235, 264, 279, 285, 289, 296, 300},
  410. {0, 0, 0, 0, 131, 0, 191, 222, 242, 270, 284, 289, 293, 300, 303},
  411. {0, 0, 0, 0, 200, 0, 390, 571, 741, 1067, 1365, 1505, 1640, 1894, 2053},
  412. {0, 0, 0, 0, 221, 0, 430, 630, 816, 1169, 1490, 1641, 1784, 2053, 2221},
  413. };
  414. static const u16 expected_tpt_mimo2_80MHz[4][IWL_RATE_COUNT] = {
  415. {0, 0, 0, 0, 182, 0, 240, 264, 278, 299, 308, 311, 313, 317, 319},
  416. {0, 0, 0, 0, 190, 0, 247, 269, 282, 302, 310, 313, 315, 319, 320},
  417. {0, 0, 0, 0, 428, 0, 833, 1215, 1577, 2254, 2863, 3147, 3418, 3913, 4219},
  418. {0, 0, 0, 0, 474, 0, 920, 1338, 1732, 2464, 3116, 3418, 3705, 4225, 4545},
  419. };
  420. /* mbps, mcs */
  421. static const struct iwl_rate_mcs_info iwl_rate_mcs[IWL_RATE_COUNT] = {
  422. { "1", "BPSK DSSS"},
  423. { "2", "QPSK DSSS"},
  424. {"5.5", "BPSK CCK"},
  425. { "11", "QPSK CCK"},
  426. { "6", "BPSK 1/2"},
  427. { "9", "BPSK 1/2"},
  428. { "12", "QPSK 1/2"},
  429. { "18", "QPSK 3/4"},
  430. { "24", "16QAM 1/2"},
  431. { "36", "16QAM 3/4"},
  432. { "48", "64QAM 2/3"},
  433. { "54", "64QAM 3/4"},
  434. { "60", "64QAM 5/6"},
  435. };
  436. #define MCS_INDEX_PER_STREAM (8)
  437. static const char *rs_pretty_ant(u8 ant)
  438. {
  439. static const char * const ant_name[] = {
  440. [ANT_NONE] = "None",
  441. [ANT_A] = "A",
  442. [ANT_B] = "B",
  443. [ANT_AB] = "AB",
  444. [ANT_C] = "C",
  445. [ANT_AC] = "AC",
  446. [ANT_BC] = "BC",
  447. [ANT_ABC] = "ABC",
  448. };
  449. if (ant > ANT_ABC)
  450. return "UNKNOWN";
  451. return ant_name[ant];
  452. }
  453. static const char *rs_pretty_lq_type(enum iwl_table_type type)
  454. {
  455. static const char * const lq_types[] = {
  456. [LQ_NONE] = "NONE",
  457. [LQ_LEGACY_A] = "LEGACY_A",
  458. [LQ_LEGACY_G] = "LEGACY_G",
  459. [LQ_HT_SISO] = "HT SISO",
  460. [LQ_HT_MIMO2] = "HT MIMO",
  461. [LQ_VHT_SISO] = "VHT SISO",
  462. [LQ_VHT_MIMO2] = "VHT MIMO",
  463. };
  464. if (type < LQ_NONE || type >= LQ_MAX)
  465. return "UNKNOWN";
  466. return lq_types[type];
  467. }
  468. static inline void rs_dump_rate(struct iwl_mvm *mvm, const struct rs_rate *rate,
  469. const char *prefix)
  470. {
  471. IWL_DEBUG_RATE(mvm,
  472. "%s: (%s: %d) ANT: %s BW: %d SGI: %d LDPC: %d STBC: %d\n",
  473. prefix, rs_pretty_lq_type(rate->type),
  474. rate->index, rs_pretty_ant(rate->ant),
  475. rate->bw, rate->sgi, rate->ldpc, rate->stbc);
  476. }
  477. static void rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  478. {
  479. window->data = 0;
  480. window->success_counter = 0;
  481. window->success_ratio = IWL_INVALID_VALUE;
  482. window->counter = 0;
  483. window->average_tpt = IWL_INVALID_VALUE;
  484. }
  485. static void rs_rate_scale_clear_tbl_windows(struct iwl_mvm *mvm,
  486. struct iwl_scale_tbl_info *tbl)
  487. {
  488. int i;
  489. IWL_DEBUG_RATE(mvm, "Clearing up window stats\n");
  490. for (i = 0; i < IWL_RATE_COUNT; i++)
  491. rs_rate_scale_clear_window(&tbl->win[i]);
  492. for (i = 0; i < ARRAY_SIZE(tbl->tpc_win); i++)
  493. rs_rate_scale_clear_window(&tbl->tpc_win[i]);
  494. }
  495. static inline u8 rs_is_valid_ant(u8 valid_antenna, u8 ant_type)
  496. {
  497. return (ant_type & valid_antenna) == ant_type;
  498. }
  499. static int rs_tl_turn_on_agg_for_tid(struct iwl_mvm *mvm,
  500. struct iwl_lq_sta *lq_data, u8 tid,
  501. struct ieee80211_sta *sta)
  502. {
  503. int ret = -EAGAIN;
  504. IWL_DEBUG_HT(mvm, "Starting Tx agg: STA: %pM tid: %d\n",
  505. sta->addr, tid);
  506. ret = ieee80211_start_tx_ba_session(sta, tid, 5000);
  507. if (ret == -EAGAIN) {
  508. /*
  509. * driver and mac80211 is out of sync
  510. * this might be cause by reloading firmware
  511. * stop the tx ba session here
  512. */
  513. IWL_ERR(mvm, "Fail start Tx agg on tid: %d\n",
  514. tid);
  515. ieee80211_stop_tx_ba_session(sta, tid);
  516. }
  517. return ret;
  518. }
  519. static void rs_tl_turn_on_agg(struct iwl_mvm *mvm, u8 tid,
  520. struct iwl_lq_sta *lq_data,
  521. struct ieee80211_sta *sta)
  522. {
  523. if (tid < IWL_MAX_TID_COUNT)
  524. rs_tl_turn_on_agg_for_tid(mvm, lq_data, tid, sta);
  525. else
  526. IWL_ERR(mvm, "tid exceeds max TID count: %d/%d\n",
  527. tid, IWL_MAX_TID_COUNT);
  528. }
  529. static inline int get_num_of_ant_from_rate(u32 rate_n_flags)
  530. {
  531. return !!(rate_n_flags & RATE_MCS_ANT_A_MSK) +
  532. !!(rate_n_flags & RATE_MCS_ANT_B_MSK) +
  533. !!(rate_n_flags & RATE_MCS_ANT_C_MSK);
  534. }
  535. /*
  536. * Static function to get the expected throughput from an iwl_scale_tbl_info
  537. * that wraps a NULL pointer check
  538. */
  539. static s32 get_expected_tpt(struct iwl_scale_tbl_info *tbl, int rs_index)
  540. {
  541. if (tbl->expected_tpt)
  542. return tbl->expected_tpt[rs_index];
  543. return 0;
  544. }
  545. /**
  546. * rs_collect_tx_data - Update the success/failure sliding window
  547. *
  548. * We keep a sliding window of the last 62 packets transmitted
  549. * at this rate. window->data contains the bitmask of successful
  550. * packets.
  551. */
  552. static int _rs_collect_tx_data(struct iwl_scale_tbl_info *tbl,
  553. int scale_index, int attempts, int successes,
  554. struct iwl_rate_scale_data *window)
  555. {
  556. static const u64 mask = (((u64)1) << (IWL_RATE_MAX_WINDOW - 1));
  557. s32 fail_count, tpt;
  558. /* Get expected throughput */
  559. tpt = get_expected_tpt(tbl, scale_index);
  560. /*
  561. * Keep track of only the latest 62 tx frame attempts in this rate's
  562. * history window; anything older isn't really relevant any more.
  563. * If we have filled up the sliding window, drop the oldest attempt;
  564. * if the oldest attempt (highest bit in bitmap) shows "success",
  565. * subtract "1" from the success counter (this is the main reason
  566. * we keep these bitmaps!).
  567. */
  568. while (attempts > 0) {
  569. if (window->counter >= IWL_RATE_MAX_WINDOW) {
  570. /* remove earliest */
  571. window->counter = IWL_RATE_MAX_WINDOW - 1;
  572. if (window->data & mask) {
  573. window->data &= ~mask;
  574. window->success_counter--;
  575. }
  576. }
  577. /* Increment frames-attempted counter */
  578. window->counter++;
  579. /* Shift bitmap by one frame to throw away oldest history */
  580. window->data <<= 1;
  581. /* Mark the most recent #successes attempts as successful */
  582. if (successes > 0) {
  583. window->success_counter++;
  584. window->data |= 0x1;
  585. successes--;
  586. }
  587. attempts--;
  588. }
  589. /* Calculate current success ratio, avoid divide-by-0! */
  590. if (window->counter > 0)
  591. window->success_ratio = 128 * (100 * window->success_counter)
  592. / window->counter;
  593. else
  594. window->success_ratio = IWL_INVALID_VALUE;
  595. fail_count = window->counter - window->success_counter;
  596. /* Calculate average throughput, if we have enough history. */
  597. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  598. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  599. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  600. else
  601. window->average_tpt = IWL_INVALID_VALUE;
  602. return 0;
  603. }
  604. static int rs_collect_tx_data(struct iwl_lq_sta *lq_sta,
  605. struct iwl_scale_tbl_info *tbl,
  606. int scale_index, int attempts, int successes,
  607. u8 reduced_txp)
  608. {
  609. struct iwl_rate_scale_data *window = NULL;
  610. int ret;
  611. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  612. return -EINVAL;
  613. if (tbl->column != RS_COLUMN_INVALID) {
  614. struct lq_sta_pers *pers = &lq_sta->pers;
  615. pers->tx_stats[tbl->column][scale_index].total += attempts;
  616. pers->tx_stats[tbl->column][scale_index].success += successes;
  617. }
  618. /* Select window for current tx bit rate */
  619. window = &(tbl->win[scale_index]);
  620. ret = _rs_collect_tx_data(tbl, scale_index, attempts, successes,
  621. window);
  622. if (ret)
  623. return ret;
  624. if (WARN_ON_ONCE(reduced_txp > TPC_MAX_REDUCTION))
  625. return -EINVAL;
  626. window = &tbl->tpc_win[reduced_txp];
  627. return _rs_collect_tx_data(tbl, scale_index, attempts, successes,
  628. window);
  629. }
  630. /* Convert rs_rate object into ucode rate bitmask */
  631. static u32 ucode_rate_from_rs_rate(struct iwl_mvm *mvm,
  632. struct rs_rate *rate)
  633. {
  634. u32 ucode_rate = 0;
  635. int index = rate->index;
  636. ucode_rate |= ((rate->ant << RATE_MCS_ANT_POS) &
  637. RATE_MCS_ANT_ABC_MSK);
  638. if (is_legacy(rate)) {
  639. ucode_rate |= iwl_rates[index].plcp;
  640. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  641. ucode_rate |= RATE_MCS_CCK_MSK;
  642. return ucode_rate;
  643. }
  644. if (is_ht(rate)) {
  645. if (index < IWL_FIRST_HT_RATE || index > IWL_LAST_HT_RATE) {
  646. IWL_ERR(mvm, "Invalid HT rate index %d\n", index);
  647. index = IWL_LAST_HT_RATE;
  648. }
  649. ucode_rate |= RATE_MCS_HT_MSK;
  650. if (is_ht_siso(rate))
  651. ucode_rate |= iwl_rates[index].plcp_ht_siso;
  652. else if (is_ht_mimo2(rate))
  653. ucode_rate |= iwl_rates[index].plcp_ht_mimo2;
  654. else
  655. WARN_ON_ONCE(1);
  656. } else if (is_vht(rate)) {
  657. if (index < IWL_FIRST_VHT_RATE || index > IWL_LAST_VHT_RATE) {
  658. IWL_ERR(mvm, "Invalid VHT rate index %d\n", index);
  659. index = IWL_LAST_VHT_RATE;
  660. }
  661. ucode_rate |= RATE_MCS_VHT_MSK;
  662. if (is_vht_siso(rate))
  663. ucode_rate |= iwl_rates[index].plcp_vht_siso;
  664. else if (is_vht_mimo2(rate))
  665. ucode_rate |= iwl_rates[index].plcp_vht_mimo2;
  666. else
  667. WARN_ON_ONCE(1);
  668. } else {
  669. IWL_ERR(mvm, "Invalid rate->type %d\n", rate->type);
  670. }
  671. if (is_siso(rate) && rate->stbc) {
  672. /* To enable STBC we need to set both a flag and ANT_AB */
  673. ucode_rate |= RATE_MCS_ANT_AB_MSK;
  674. ucode_rate |= RATE_MCS_VHT_STBC_MSK;
  675. }
  676. ucode_rate |= rate->bw;
  677. if (rate->sgi)
  678. ucode_rate |= RATE_MCS_SGI_MSK;
  679. if (rate->ldpc)
  680. ucode_rate |= RATE_MCS_LDPC_MSK;
  681. return ucode_rate;
  682. }
  683. /* Convert a ucode rate into an rs_rate object */
  684. static int rs_rate_from_ucode_rate(const u32 ucode_rate,
  685. enum ieee80211_band band,
  686. struct rs_rate *rate)
  687. {
  688. u32 ant_msk = ucode_rate & RATE_MCS_ANT_ABC_MSK;
  689. u8 num_of_ant = get_num_of_ant_from_rate(ucode_rate);
  690. u8 nss;
  691. memset(rate, 0, sizeof(*rate));
  692. rate->index = iwl_hwrate_to_plcp_idx(ucode_rate);
  693. if (rate->index == IWL_RATE_INVALID)
  694. return -EINVAL;
  695. rate->ant = (ant_msk >> RATE_MCS_ANT_POS);
  696. /* Legacy */
  697. if (!(ucode_rate & RATE_MCS_HT_MSK) &&
  698. !(ucode_rate & RATE_MCS_VHT_MSK)) {
  699. if (num_of_ant == 1) {
  700. if (band == IEEE80211_BAND_5GHZ)
  701. rate->type = LQ_LEGACY_A;
  702. else
  703. rate->type = LQ_LEGACY_G;
  704. }
  705. return 0;
  706. }
  707. /* HT or VHT */
  708. if (ucode_rate & RATE_MCS_SGI_MSK)
  709. rate->sgi = true;
  710. if (ucode_rate & RATE_MCS_LDPC_MSK)
  711. rate->ldpc = true;
  712. if (ucode_rate & RATE_MCS_VHT_STBC_MSK)
  713. rate->stbc = true;
  714. rate->bw = ucode_rate & RATE_MCS_CHAN_WIDTH_MSK;
  715. if (ucode_rate & RATE_MCS_HT_MSK) {
  716. nss = ((ucode_rate & RATE_HT_MCS_NSS_MSK) >>
  717. RATE_HT_MCS_NSS_POS) + 1;
  718. if (nss == 1) {
  719. rate->type = LQ_HT_SISO;
  720. WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
  721. } else if (nss == 2) {
  722. rate->type = LQ_HT_MIMO2;
  723. WARN_ON_ONCE(num_of_ant != 2);
  724. } else {
  725. WARN_ON_ONCE(1);
  726. }
  727. } else if (ucode_rate & RATE_MCS_VHT_MSK) {
  728. nss = ((ucode_rate & RATE_VHT_MCS_NSS_MSK) >>
  729. RATE_VHT_MCS_NSS_POS) + 1;
  730. if (nss == 1) {
  731. rate->type = LQ_VHT_SISO;
  732. WARN_ON_ONCE(!rate->stbc && num_of_ant != 1);
  733. } else if (nss == 2) {
  734. rate->type = LQ_VHT_MIMO2;
  735. WARN_ON_ONCE(num_of_ant != 2);
  736. } else {
  737. WARN_ON_ONCE(1);
  738. }
  739. }
  740. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_160);
  741. WARN_ON_ONCE(rate->bw == RATE_MCS_CHAN_WIDTH_80 &&
  742. !is_vht(rate));
  743. return 0;
  744. }
  745. /* switch to another antenna/antennas and return 1 */
  746. /* if no other valid antenna found, return 0 */
  747. static int rs_toggle_antenna(u32 valid_ant, struct rs_rate *rate)
  748. {
  749. u8 new_ant_type;
  750. if (!rate->ant || rate->ant > ANT_ABC)
  751. return 0;
  752. if (!rs_is_valid_ant(valid_ant, rate->ant))
  753. return 0;
  754. new_ant_type = ant_toggle_lookup[rate->ant];
  755. while ((new_ant_type != rate->ant) &&
  756. !rs_is_valid_ant(valid_ant, new_ant_type))
  757. new_ant_type = ant_toggle_lookup[new_ant_type];
  758. if (new_ant_type == rate->ant)
  759. return 0;
  760. rate->ant = new_ant_type;
  761. return 1;
  762. }
  763. static u16 rs_get_supported_rates(struct iwl_lq_sta *lq_sta,
  764. struct rs_rate *rate)
  765. {
  766. if (is_legacy(rate))
  767. return lq_sta->active_legacy_rate;
  768. else if (is_siso(rate))
  769. return lq_sta->active_siso_rate;
  770. else if (is_mimo2(rate))
  771. return lq_sta->active_mimo2_rate;
  772. WARN_ON_ONCE(1);
  773. return 0;
  774. }
  775. static u16 rs_get_adjacent_rate(struct iwl_mvm *mvm, u8 index, u16 rate_mask,
  776. int rate_type)
  777. {
  778. u8 high = IWL_RATE_INVALID;
  779. u8 low = IWL_RATE_INVALID;
  780. /* 802.11A or ht walks to the next literal adjacent rate in
  781. * the rate table */
  782. if (is_type_a_band(rate_type) || !is_type_legacy(rate_type)) {
  783. int i;
  784. u32 mask;
  785. /* Find the previous rate that is in the rate mask */
  786. i = index - 1;
  787. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  788. if (rate_mask & mask) {
  789. low = i;
  790. break;
  791. }
  792. }
  793. /* Find the next rate that is in the rate mask */
  794. i = index + 1;
  795. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  796. if (rate_mask & mask) {
  797. high = i;
  798. break;
  799. }
  800. }
  801. return (high << 8) | low;
  802. }
  803. low = index;
  804. while (low != IWL_RATE_INVALID) {
  805. low = iwl_rates[low].prev_rs;
  806. if (low == IWL_RATE_INVALID)
  807. break;
  808. if (rate_mask & (1 << low))
  809. break;
  810. IWL_DEBUG_RATE(mvm, "Skipping masked lower rate: %d\n", low);
  811. }
  812. high = index;
  813. while (high != IWL_RATE_INVALID) {
  814. high = iwl_rates[high].next_rs;
  815. if (high == IWL_RATE_INVALID)
  816. break;
  817. if (rate_mask & (1 << high))
  818. break;
  819. IWL_DEBUG_RATE(mvm, "Skipping masked higher rate: %d\n", high);
  820. }
  821. return (high << 8) | low;
  822. }
  823. static inline bool rs_rate_supported(struct iwl_lq_sta *lq_sta,
  824. struct rs_rate *rate)
  825. {
  826. return BIT(rate->index) & rs_get_supported_rates(lq_sta, rate);
  827. }
  828. /* Get the next supported lower rate in the current column.
  829. * Return true if bottom rate in the current column was reached
  830. */
  831. static bool rs_get_lower_rate_in_column(struct iwl_lq_sta *lq_sta,
  832. struct rs_rate *rate)
  833. {
  834. u8 low;
  835. u16 high_low;
  836. u16 rate_mask;
  837. struct iwl_mvm *mvm = lq_sta->pers.drv;
  838. rate_mask = rs_get_supported_rates(lq_sta, rate);
  839. high_low = rs_get_adjacent_rate(mvm, rate->index, rate_mask,
  840. rate->type);
  841. low = high_low & 0xff;
  842. /* Bottom rate of column reached */
  843. if (low == IWL_RATE_INVALID)
  844. return true;
  845. rate->index = low;
  846. return false;
  847. }
  848. /* Get the next rate to use following a column downgrade */
  849. static void rs_get_lower_rate_down_column(struct iwl_lq_sta *lq_sta,
  850. struct rs_rate *rate)
  851. {
  852. struct iwl_mvm *mvm = lq_sta->pers.drv;
  853. if (is_legacy(rate)) {
  854. /* No column to downgrade from Legacy */
  855. return;
  856. } else if (is_siso(rate)) {
  857. /* Downgrade to Legacy if we were in SISO */
  858. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  859. rate->type = LQ_LEGACY_A;
  860. else
  861. rate->type = LQ_LEGACY_G;
  862. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  863. WARN_ON_ONCE(rate->index < IWL_RATE_MCS_0_INDEX ||
  864. rate->index > IWL_RATE_MCS_9_INDEX);
  865. rate->index = rs_ht_to_legacy[rate->index];
  866. rate->ldpc = false;
  867. } else {
  868. /* Downgrade to SISO with same MCS if in MIMO */
  869. rate->type = is_vht_mimo2(rate) ?
  870. LQ_VHT_SISO : LQ_HT_SISO;
  871. }
  872. if (num_of_ant(rate->ant) > 1)
  873. rate->ant = first_antenna(mvm->fw->valid_tx_ant);
  874. /* Relevant in both switching to SISO or Legacy */
  875. rate->sgi = false;
  876. if (!rs_rate_supported(lq_sta, rate))
  877. rs_get_lower_rate_in_column(lq_sta, rate);
  878. }
  879. /* Simple function to compare two rate scale table types */
  880. static inline bool rs_rate_match(struct rs_rate *a,
  881. struct rs_rate *b)
  882. {
  883. bool ant_match;
  884. if (a->stbc)
  885. ant_match = (b->ant == ANT_A || b->ant == ANT_B);
  886. else
  887. ant_match = (a->ant == b->ant);
  888. return (a->type == b->type) && (a->bw == b->bw) && (a->sgi == b->sgi)
  889. && ant_match;
  890. }
  891. static u32 rs_ch_width_from_mac_flags(enum mac80211_rate_control_flags flags)
  892. {
  893. if (flags & IEEE80211_TX_RC_40_MHZ_WIDTH)
  894. return RATE_MCS_CHAN_WIDTH_40;
  895. else if (flags & IEEE80211_TX_RC_80_MHZ_WIDTH)
  896. return RATE_MCS_CHAN_WIDTH_80;
  897. else if (flags & IEEE80211_TX_RC_160_MHZ_WIDTH)
  898. return RATE_MCS_CHAN_WIDTH_160;
  899. return RATE_MCS_CHAN_WIDTH_20;
  900. }
  901. static u8 rs_get_tid(struct ieee80211_hdr *hdr)
  902. {
  903. u8 tid = IWL_MAX_TID_COUNT;
  904. if (ieee80211_is_data_qos(hdr->frame_control)) {
  905. u8 *qc = ieee80211_get_qos_ctl(hdr);
  906. tid = qc[0] & 0xf;
  907. }
  908. if (unlikely(tid > IWL_MAX_TID_COUNT))
  909. tid = IWL_MAX_TID_COUNT;
  910. return tid;
  911. }
  912. void iwl_mvm_rs_tx_status(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  913. int tid, struct ieee80211_tx_info *info)
  914. {
  915. int legacy_success;
  916. int retries;
  917. int mac_index, i;
  918. struct iwl_lq_cmd *table;
  919. enum mac80211_rate_control_flags mac_flags;
  920. u32 ucode_rate;
  921. struct rs_rate rate;
  922. struct iwl_scale_tbl_info *curr_tbl, *other_tbl, *tmp_tbl;
  923. u8 reduced_txp = (uintptr_t)info->status.status_driver_data[0];
  924. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  925. struct iwl_lq_sta *lq_sta = &mvmsta->lq_sta;
  926. /* Treat uninitialized rate scaling data same as non-existing. */
  927. if (!lq_sta) {
  928. IWL_DEBUG_RATE(mvm, "Station rate scaling not created yet.\n");
  929. return;
  930. } else if (!lq_sta->pers.drv) {
  931. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  932. return;
  933. }
  934. #ifdef CONFIG_MAC80211_DEBUGFS
  935. /* Disable last tx check if we are debugging with fixed rate */
  936. if (lq_sta->pers.dbg_fixed_rate) {
  937. IWL_DEBUG_RATE(mvm, "Fixed rate. avoid rate scaling\n");
  938. return;
  939. }
  940. #endif
  941. /* This packet was aggregated but doesn't carry status info */
  942. if ((info->flags & IEEE80211_TX_CTL_AMPDU) &&
  943. !(info->flags & IEEE80211_TX_STAT_AMPDU))
  944. return;
  945. /*
  946. * Ignore this Tx frame response if its initial rate doesn't match
  947. * that of latest Link Quality command. There may be stragglers
  948. * from a previous Link Quality command, but we're no longer interested
  949. * in those; they're either from the "active" mode while we're trying
  950. * to check "search" mode, or a prior "search" mode after we've moved
  951. * to a new "search" mode (which might become the new "active" mode).
  952. */
  953. table = &lq_sta->lq;
  954. ucode_rate = le32_to_cpu(table->rs_table[0]);
  955. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  956. if (info->band == IEEE80211_BAND_5GHZ)
  957. rate.index -= IWL_FIRST_OFDM_RATE;
  958. mac_flags = info->status.rates[0].flags;
  959. mac_index = info->status.rates[0].idx;
  960. /* For HT packets, map MCS to PLCP */
  961. if (mac_flags & IEEE80211_TX_RC_MCS) {
  962. /* Remove # of streams */
  963. mac_index &= RATE_HT_MCS_RATE_CODE_MSK;
  964. if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
  965. mac_index++;
  966. /*
  967. * mac80211 HT index is always zero-indexed; we need to move
  968. * HT OFDM rates after CCK rates in 2.4 GHz band
  969. */
  970. if (info->band == IEEE80211_BAND_2GHZ)
  971. mac_index += IWL_FIRST_OFDM_RATE;
  972. } else if (mac_flags & IEEE80211_TX_RC_VHT_MCS) {
  973. mac_index &= RATE_VHT_MCS_RATE_CODE_MSK;
  974. if (mac_index >= (IWL_RATE_9M_INDEX - IWL_FIRST_OFDM_RATE))
  975. mac_index++;
  976. }
  977. if (time_after(jiffies,
  978. (unsigned long)(lq_sta->last_tx + RS_IDLE_TIMEOUT))) {
  979. int t;
  980. IWL_DEBUG_RATE(mvm, "Tx idle for too long. reinit rs\n");
  981. for (t = 0; t < IWL_MAX_TID_COUNT; t++)
  982. ieee80211_stop_tx_ba_session(sta, t);
  983. iwl_mvm_rs_rate_init(mvm, sta, info->band, false);
  984. return;
  985. }
  986. lq_sta->last_tx = jiffies;
  987. /* Here we actually compare this rate to the latest LQ command */
  988. if ((mac_index < 0) ||
  989. (rate.sgi != !!(mac_flags & IEEE80211_TX_RC_SHORT_GI)) ||
  990. (rate.bw != rs_ch_width_from_mac_flags(mac_flags)) ||
  991. (rate.ant != info->status.antenna) ||
  992. (!!(ucode_rate & RATE_MCS_HT_MSK) !=
  993. !!(mac_flags & IEEE80211_TX_RC_MCS)) ||
  994. (!!(ucode_rate & RATE_MCS_VHT_MSK) !=
  995. !!(mac_flags & IEEE80211_TX_RC_VHT_MCS)) ||
  996. (!!(ucode_rate & RATE_HT_MCS_GF_MSK) !=
  997. !!(mac_flags & IEEE80211_TX_RC_GREEN_FIELD)) ||
  998. (rate.index != mac_index)) {
  999. IWL_DEBUG_RATE(mvm,
  1000. "initial rate %d does not match %d (0x%x)\n",
  1001. mac_index, rate.index, ucode_rate);
  1002. /*
  1003. * Since rates mis-match, the last LQ command may have failed.
  1004. * After IWL_MISSED_RATE_MAX mis-matches, resync the uCode with
  1005. * ... driver.
  1006. */
  1007. lq_sta->missed_rate_counter++;
  1008. if (lq_sta->missed_rate_counter > IWL_MISSED_RATE_MAX) {
  1009. lq_sta->missed_rate_counter = 0;
  1010. IWL_DEBUG_RATE(mvm,
  1011. "Too many rates mismatch. Send sync LQ. rs_state %d\n",
  1012. lq_sta->rs_state);
  1013. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1014. }
  1015. /* Regardless, ignore this status info for outdated rate */
  1016. return;
  1017. } else
  1018. /* Rate did match, so reset the missed_rate_counter */
  1019. lq_sta->missed_rate_counter = 0;
  1020. if (!lq_sta->search_better_tbl) {
  1021. curr_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1022. other_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1023. } else {
  1024. curr_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1025. other_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1026. }
  1027. if (WARN_ON_ONCE(!rs_rate_match(&rate, &curr_tbl->rate))) {
  1028. IWL_DEBUG_RATE(mvm,
  1029. "Neither active nor search matches tx rate\n");
  1030. tmp_tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1031. rs_dump_rate(mvm, &tmp_tbl->rate, "ACTIVE");
  1032. tmp_tbl = &(lq_sta->lq_info[1 - lq_sta->active_tbl]);
  1033. rs_dump_rate(mvm, &tmp_tbl->rate, "SEARCH");
  1034. rs_dump_rate(mvm, &rate, "ACTUAL");
  1035. /*
  1036. * no matching table found, let's by-pass the data collection
  1037. * and continue to perform rate scale to find the rate table
  1038. */
  1039. rs_stay_in_table(lq_sta, true);
  1040. goto done;
  1041. }
  1042. /*
  1043. * Updating the frame history depends on whether packets were
  1044. * aggregated.
  1045. *
  1046. * For aggregation, all packets were transmitted at the same rate, the
  1047. * first index into rate scale table.
  1048. */
  1049. if (info->flags & IEEE80211_TX_STAT_AMPDU) {
  1050. /* ampdu_ack_len = 0 marks no BA was received. In this case
  1051. * treat it as a single frame loss as we don't want the success
  1052. * ratio to dip too quickly because a BA wasn't received
  1053. */
  1054. if (info->status.ampdu_ack_len == 0)
  1055. info->status.ampdu_len = 1;
  1056. ucode_rate = le32_to_cpu(table->rs_table[0]);
  1057. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  1058. rs_collect_tx_data(lq_sta, curr_tbl, rate.index,
  1059. info->status.ampdu_len,
  1060. info->status.ampdu_ack_len,
  1061. reduced_txp);
  1062. /* Update success/fail counts if not searching for new mode */
  1063. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1064. lq_sta->total_success += info->status.ampdu_ack_len;
  1065. lq_sta->total_failed += (info->status.ampdu_len -
  1066. info->status.ampdu_ack_len);
  1067. }
  1068. } else {
  1069. /*
  1070. * For legacy, update frame history with for each Tx retry.
  1071. */
  1072. retries = info->status.rates[0].count - 1;
  1073. /* HW doesn't send more than 15 retries */
  1074. retries = min(retries, 15);
  1075. /* The last transmission may have been successful */
  1076. legacy_success = !!(info->flags & IEEE80211_TX_STAT_ACK);
  1077. /* Collect data for each rate used during failed TX attempts */
  1078. for (i = 0; i <= retries; ++i) {
  1079. ucode_rate = le32_to_cpu(table->rs_table[i]);
  1080. rs_rate_from_ucode_rate(ucode_rate, info->band, &rate);
  1081. /*
  1082. * Only collect stats if retried rate is in the same RS
  1083. * table as active/search.
  1084. */
  1085. if (rs_rate_match(&rate, &curr_tbl->rate))
  1086. tmp_tbl = curr_tbl;
  1087. else if (rs_rate_match(&rate, &other_tbl->rate))
  1088. tmp_tbl = other_tbl;
  1089. else
  1090. continue;
  1091. rs_collect_tx_data(lq_sta, tmp_tbl, rate.index, 1,
  1092. i < retries ? 0 : legacy_success,
  1093. reduced_txp);
  1094. }
  1095. /* Update success/fail counts if not searching for new mode */
  1096. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1097. lq_sta->total_success += legacy_success;
  1098. lq_sta->total_failed += retries + (1 - legacy_success);
  1099. }
  1100. }
  1101. /* The last TX rate is cached in lq_sta; it's set in if/else above */
  1102. lq_sta->last_rate_n_flags = ucode_rate;
  1103. IWL_DEBUG_RATE(mvm, "reduced txpower: %d\n", reduced_txp);
  1104. done:
  1105. /* See if there's a better rate or modulation mode to try. */
  1106. if (sta->supp_rates[info->band])
  1107. rs_rate_scale_perform(mvm, sta, lq_sta, tid);
  1108. }
  1109. /*
  1110. * mac80211 sends us Tx status
  1111. */
  1112. static void rs_mac80211_tx_status(void *mvm_r,
  1113. struct ieee80211_supported_band *sband,
  1114. struct ieee80211_sta *sta, void *priv_sta,
  1115. struct sk_buff *skb)
  1116. {
  1117. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1118. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_r;
  1119. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  1120. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1121. if (!ieee80211_is_data(hdr->frame_control) ||
  1122. info->flags & IEEE80211_TX_CTL_NO_ACK)
  1123. return;
  1124. iwl_mvm_rs_tx_status(mvm, sta, rs_get_tid(hdr), info);
  1125. }
  1126. /*
  1127. * Begin a period of staying with a selected modulation mode.
  1128. * Set "stay_in_tbl" flag to prevent any mode switches.
  1129. * Set frame tx success limits according to legacy vs. high-throughput,
  1130. * and reset overall (spanning all rates) tx success history statistics.
  1131. * These control how long we stay using same modulation mode before
  1132. * searching for a new mode.
  1133. */
  1134. static void rs_set_stay_in_table(struct iwl_mvm *mvm, u8 is_legacy,
  1135. struct iwl_lq_sta *lq_sta)
  1136. {
  1137. IWL_DEBUG_RATE(mvm, "Moving to RS_STATE_STAY_IN_COLUMN\n");
  1138. lq_sta->rs_state = RS_STATE_STAY_IN_COLUMN;
  1139. if (is_legacy) {
  1140. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  1141. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  1142. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  1143. } else {
  1144. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  1145. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  1146. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  1147. }
  1148. lq_sta->table_count = 0;
  1149. lq_sta->total_failed = 0;
  1150. lq_sta->total_success = 0;
  1151. lq_sta->flush_timer = jiffies;
  1152. lq_sta->visited_columns = 0;
  1153. }
  1154. static int rs_get_max_allowed_rate(struct iwl_lq_sta *lq_sta,
  1155. const struct rs_tx_column *column)
  1156. {
  1157. switch (column->mode) {
  1158. case RS_LEGACY:
  1159. return lq_sta->max_legacy_rate_idx;
  1160. case RS_SISO:
  1161. return lq_sta->max_siso_rate_idx;
  1162. case RS_MIMO2:
  1163. return lq_sta->max_mimo2_rate_idx;
  1164. default:
  1165. WARN_ON_ONCE(1);
  1166. }
  1167. return lq_sta->max_legacy_rate_idx;
  1168. }
  1169. static const u16 *rs_get_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1170. const struct rs_tx_column *column,
  1171. u32 bw)
  1172. {
  1173. /* Used to choose among HT tables */
  1174. const u16 (*ht_tbl_pointer)[IWL_RATE_COUNT];
  1175. if (WARN_ON_ONCE(column->mode != RS_LEGACY &&
  1176. column->mode != RS_SISO &&
  1177. column->mode != RS_MIMO2))
  1178. return expected_tpt_legacy;
  1179. /* Legacy rates have only one table */
  1180. if (column->mode == RS_LEGACY)
  1181. return expected_tpt_legacy;
  1182. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1183. /* Choose among many HT tables depending on number of streams
  1184. * (SISO/MIMO2), channel width (20/40/80), SGI, and aggregation
  1185. * status */
  1186. if (column->mode == RS_SISO) {
  1187. switch (bw) {
  1188. case RATE_MCS_CHAN_WIDTH_20:
  1189. ht_tbl_pointer = expected_tpt_siso_20MHz;
  1190. break;
  1191. case RATE_MCS_CHAN_WIDTH_40:
  1192. ht_tbl_pointer = expected_tpt_siso_40MHz;
  1193. break;
  1194. case RATE_MCS_CHAN_WIDTH_80:
  1195. ht_tbl_pointer = expected_tpt_siso_80MHz;
  1196. break;
  1197. default:
  1198. WARN_ON_ONCE(1);
  1199. }
  1200. } else if (column->mode == RS_MIMO2) {
  1201. switch (bw) {
  1202. case RATE_MCS_CHAN_WIDTH_20:
  1203. ht_tbl_pointer = expected_tpt_mimo2_20MHz;
  1204. break;
  1205. case RATE_MCS_CHAN_WIDTH_40:
  1206. ht_tbl_pointer = expected_tpt_mimo2_40MHz;
  1207. break;
  1208. case RATE_MCS_CHAN_WIDTH_80:
  1209. ht_tbl_pointer = expected_tpt_mimo2_80MHz;
  1210. break;
  1211. default:
  1212. WARN_ON_ONCE(1);
  1213. }
  1214. } else {
  1215. WARN_ON_ONCE(1);
  1216. }
  1217. if (!column->sgi && !lq_sta->is_agg) /* Normal */
  1218. return ht_tbl_pointer[0];
  1219. else if (column->sgi && !lq_sta->is_agg) /* SGI */
  1220. return ht_tbl_pointer[1];
  1221. else if (!column->sgi && lq_sta->is_agg) /* AGG */
  1222. return ht_tbl_pointer[2];
  1223. else /* AGG+SGI */
  1224. return ht_tbl_pointer[3];
  1225. }
  1226. static void rs_set_expected_tpt_table(struct iwl_lq_sta *lq_sta,
  1227. struct iwl_scale_tbl_info *tbl)
  1228. {
  1229. struct rs_rate *rate = &tbl->rate;
  1230. const struct rs_tx_column *column = &rs_tx_columns[tbl->column];
  1231. tbl->expected_tpt = rs_get_expected_tpt_table(lq_sta, column, rate->bw);
  1232. }
  1233. static s32 rs_get_best_rate(struct iwl_mvm *mvm,
  1234. struct iwl_lq_sta *lq_sta,
  1235. struct iwl_scale_tbl_info *tbl, /* "search" */
  1236. unsigned long rate_mask, s8 index)
  1237. {
  1238. struct iwl_scale_tbl_info *active_tbl =
  1239. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1240. s32 success_ratio = active_tbl->win[index].success_ratio;
  1241. u16 expected_current_tpt = active_tbl->expected_tpt[index];
  1242. const u16 *tpt_tbl = tbl->expected_tpt;
  1243. u16 high_low;
  1244. u32 target_tpt;
  1245. int rate_idx;
  1246. if (success_ratio > RS_SR_NO_DECREASE) {
  1247. target_tpt = 100 * expected_current_tpt;
  1248. IWL_DEBUG_RATE(mvm,
  1249. "SR %d high. Find rate exceeding EXPECTED_CURRENT %d\n",
  1250. success_ratio, target_tpt);
  1251. } else {
  1252. target_tpt = lq_sta->last_tpt;
  1253. IWL_DEBUG_RATE(mvm,
  1254. "SR %d not thag good. Find rate exceeding ACTUAL_TPT %d\n",
  1255. success_ratio, target_tpt);
  1256. }
  1257. rate_idx = find_first_bit(&rate_mask, BITS_PER_LONG);
  1258. while (rate_idx != IWL_RATE_INVALID) {
  1259. if (target_tpt < (100 * tpt_tbl[rate_idx]))
  1260. break;
  1261. high_low = rs_get_adjacent_rate(mvm, rate_idx, rate_mask,
  1262. tbl->rate.type);
  1263. rate_idx = (high_low >> 8) & 0xff;
  1264. }
  1265. IWL_DEBUG_RATE(mvm, "Best rate found %d target_tp %d expected_new %d\n",
  1266. rate_idx, target_tpt,
  1267. rate_idx != IWL_RATE_INVALID ?
  1268. 100 * tpt_tbl[rate_idx] : IWL_INVALID_VALUE);
  1269. return rate_idx;
  1270. }
  1271. static u32 rs_bw_from_sta_bw(struct ieee80211_sta *sta)
  1272. {
  1273. if (sta->bandwidth >= IEEE80211_STA_RX_BW_80)
  1274. return RATE_MCS_CHAN_WIDTH_80;
  1275. else if (sta->bandwidth >= IEEE80211_STA_RX_BW_40)
  1276. return RATE_MCS_CHAN_WIDTH_40;
  1277. return RATE_MCS_CHAN_WIDTH_20;
  1278. }
  1279. /*
  1280. * Check whether we should continue using same modulation mode, or
  1281. * begin search for a new mode, based on:
  1282. * 1) # tx successes or failures while using this mode
  1283. * 2) # times calling this function
  1284. * 3) elapsed time in this mode (not used, for now)
  1285. */
  1286. static void rs_stay_in_table(struct iwl_lq_sta *lq_sta, bool force_search)
  1287. {
  1288. struct iwl_scale_tbl_info *tbl;
  1289. int active_tbl;
  1290. int flush_interval_passed = 0;
  1291. struct iwl_mvm *mvm;
  1292. mvm = lq_sta->pers.drv;
  1293. active_tbl = lq_sta->active_tbl;
  1294. tbl = &(lq_sta->lq_info[active_tbl]);
  1295. /* If we've been disallowing search, see if we should now allow it */
  1296. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN) {
  1297. /* Elapsed time using current modulation mode */
  1298. if (lq_sta->flush_timer)
  1299. flush_interval_passed =
  1300. time_after(jiffies,
  1301. (unsigned long)(lq_sta->flush_timer +
  1302. RS_STAY_IN_COLUMN_TIMEOUT));
  1303. /*
  1304. * Check if we should allow search for new modulation mode.
  1305. * If many frames have failed or succeeded, or we've used
  1306. * this same modulation for a long time, allow search, and
  1307. * reset history stats that keep track of whether we should
  1308. * allow a new search. Also (below) reset all bitmaps and
  1309. * stats in active history.
  1310. */
  1311. if (force_search ||
  1312. (lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1313. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1314. ((!lq_sta->search_better_tbl) &&
  1315. (lq_sta->flush_timer) && (flush_interval_passed))) {
  1316. IWL_DEBUG_RATE(mvm,
  1317. "LQ: stay is expired %d %d %d\n",
  1318. lq_sta->total_failed,
  1319. lq_sta->total_success,
  1320. flush_interval_passed);
  1321. /* Allow search for new mode */
  1322. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_STARTED;
  1323. IWL_DEBUG_RATE(mvm,
  1324. "Moving to RS_STATE_SEARCH_CYCLE_STARTED\n");
  1325. lq_sta->total_failed = 0;
  1326. lq_sta->total_success = 0;
  1327. lq_sta->flush_timer = 0;
  1328. /* mark the current column as visited */
  1329. lq_sta->visited_columns = BIT(tbl->column);
  1330. /*
  1331. * Else if we've used this modulation mode enough repetitions
  1332. * (regardless of elapsed time or success/failure), reset
  1333. * history bitmaps and rate-specific stats for all rates in
  1334. * active table.
  1335. */
  1336. } else {
  1337. lq_sta->table_count++;
  1338. if (lq_sta->table_count >=
  1339. lq_sta->table_count_limit) {
  1340. lq_sta->table_count = 0;
  1341. IWL_DEBUG_RATE(mvm,
  1342. "LQ: stay in table clear win\n");
  1343. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1344. }
  1345. }
  1346. /* If transitioning to allow "search", reset all history
  1347. * bitmaps and stats in active table (this will become the new
  1348. * "search" table). */
  1349. if (lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED) {
  1350. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1351. }
  1352. }
  1353. }
  1354. /*
  1355. * setup rate table in uCode
  1356. */
  1357. static void rs_update_rate_tbl(struct iwl_mvm *mvm,
  1358. struct ieee80211_sta *sta,
  1359. struct iwl_lq_sta *lq_sta,
  1360. struct rs_rate *rate)
  1361. {
  1362. rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
  1363. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1364. }
  1365. static enum rs_column rs_get_next_column(struct iwl_mvm *mvm,
  1366. struct iwl_lq_sta *lq_sta,
  1367. struct ieee80211_sta *sta,
  1368. struct iwl_scale_tbl_info *tbl)
  1369. {
  1370. int i, j, max_rate;
  1371. enum rs_column next_col_id;
  1372. const struct rs_tx_column *curr_col = &rs_tx_columns[tbl->column];
  1373. const struct rs_tx_column *next_col;
  1374. allow_column_func_t allow_func;
  1375. u8 valid_ants = mvm->fw->valid_tx_ant;
  1376. const u16 *expected_tpt_tbl;
  1377. u16 tpt, max_expected_tpt;
  1378. for (i = 0; i < MAX_NEXT_COLUMNS; i++) {
  1379. next_col_id = curr_col->next_columns[i];
  1380. if (next_col_id == RS_COLUMN_INVALID)
  1381. continue;
  1382. if (lq_sta->visited_columns & BIT(next_col_id)) {
  1383. IWL_DEBUG_RATE(mvm, "Skip already visited column %d\n",
  1384. next_col_id);
  1385. continue;
  1386. }
  1387. next_col = &rs_tx_columns[next_col_id];
  1388. if (!rs_is_valid_ant(valid_ants, next_col->ant)) {
  1389. IWL_DEBUG_RATE(mvm,
  1390. "Skip column %d as ANT config isn't supported by chip. valid_ants 0x%x column ant 0x%x\n",
  1391. next_col_id, valid_ants, next_col->ant);
  1392. continue;
  1393. }
  1394. for (j = 0; j < MAX_COLUMN_CHECKS; j++) {
  1395. allow_func = next_col->checks[j];
  1396. if (allow_func && !allow_func(mvm, sta, tbl))
  1397. break;
  1398. }
  1399. if (j != MAX_COLUMN_CHECKS) {
  1400. IWL_DEBUG_RATE(mvm,
  1401. "Skip column %d: not allowed (check %d failed)\n",
  1402. next_col_id, j);
  1403. continue;
  1404. }
  1405. tpt = lq_sta->last_tpt / 100;
  1406. expected_tpt_tbl = rs_get_expected_tpt_table(lq_sta, next_col,
  1407. rs_bw_from_sta_bw(sta));
  1408. if (WARN_ON_ONCE(!expected_tpt_tbl))
  1409. continue;
  1410. max_rate = rs_get_max_allowed_rate(lq_sta, next_col);
  1411. if (WARN_ON_ONCE(max_rate == IWL_RATE_INVALID))
  1412. continue;
  1413. max_expected_tpt = expected_tpt_tbl[max_rate];
  1414. if (tpt >= max_expected_tpt) {
  1415. IWL_DEBUG_RATE(mvm,
  1416. "Skip column %d: can't beat current TPT. Max expected %d current %d\n",
  1417. next_col_id, max_expected_tpt, tpt);
  1418. continue;
  1419. }
  1420. IWL_DEBUG_RATE(mvm,
  1421. "Found potential column %d. Max expected %d current %d\n",
  1422. next_col_id, max_expected_tpt, tpt);
  1423. break;
  1424. }
  1425. if (i == MAX_NEXT_COLUMNS)
  1426. return RS_COLUMN_INVALID;
  1427. return next_col_id;
  1428. }
  1429. static int rs_switch_to_column(struct iwl_mvm *mvm,
  1430. struct iwl_lq_sta *lq_sta,
  1431. struct ieee80211_sta *sta,
  1432. enum rs_column col_id)
  1433. {
  1434. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1435. struct iwl_scale_tbl_info *search_tbl =
  1436. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1437. struct rs_rate *rate = &search_tbl->rate;
  1438. const struct rs_tx_column *column = &rs_tx_columns[col_id];
  1439. const struct rs_tx_column *curr_column = &rs_tx_columns[tbl->column];
  1440. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1441. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1442. unsigned long rate_mask = 0;
  1443. u32 rate_idx = 0;
  1444. memcpy(search_tbl, tbl, sz);
  1445. rate->sgi = column->sgi;
  1446. rate->ant = column->ant;
  1447. if (column->mode == RS_LEGACY) {
  1448. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  1449. rate->type = LQ_LEGACY_A;
  1450. else
  1451. rate->type = LQ_LEGACY_G;
  1452. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  1453. rate->ldpc = false;
  1454. rate_mask = lq_sta->active_legacy_rate;
  1455. } else if (column->mode == RS_SISO) {
  1456. rate->type = lq_sta->is_vht ? LQ_VHT_SISO : LQ_HT_SISO;
  1457. rate_mask = lq_sta->active_siso_rate;
  1458. } else if (column->mode == RS_MIMO2) {
  1459. rate->type = lq_sta->is_vht ? LQ_VHT_MIMO2 : LQ_HT_MIMO2;
  1460. rate_mask = lq_sta->active_mimo2_rate;
  1461. } else {
  1462. WARN_ON_ONCE("Bad column mode");
  1463. }
  1464. if (column->mode != RS_LEGACY) {
  1465. rate->bw = rs_bw_from_sta_bw(sta);
  1466. rate->ldpc = lq_sta->ldpc;
  1467. }
  1468. search_tbl->column = col_id;
  1469. rs_set_expected_tpt_table(lq_sta, search_tbl);
  1470. lq_sta->visited_columns |= BIT(col_id);
  1471. /* Get the best matching rate if we're changing modes. e.g.
  1472. * SISO->MIMO, LEGACY->SISO, MIMO->SISO
  1473. */
  1474. if (curr_column->mode != column->mode) {
  1475. rate_idx = rs_get_best_rate(mvm, lq_sta, search_tbl,
  1476. rate_mask, rate->index);
  1477. if ((rate_idx == IWL_RATE_INVALID) ||
  1478. !(BIT(rate_idx) & rate_mask)) {
  1479. IWL_DEBUG_RATE(mvm,
  1480. "can not switch with index %d"
  1481. " rate mask %lx\n",
  1482. rate_idx, rate_mask);
  1483. goto err;
  1484. }
  1485. rate->index = rate_idx;
  1486. }
  1487. IWL_DEBUG_RATE(mvm, "Switched to column %d: Index %d\n",
  1488. col_id, rate->index);
  1489. return 0;
  1490. err:
  1491. rate->type = LQ_NONE;
  1492. return -1;
  1493. }
  1494. static enum rs_action rs_get_rate_action(struct iwl_mvm *mvm,
  1495. struct iwl_scale_tbl_info *tbl,
  1496. s32 sr, int low, int high,
  1497. int current_tpt,
  1498. int low_tpt, int high_tpt)
  1499. {
  1500. enum rs_action action = RS_ACTION_STAY;
  1501. if ((sr <= RS_SR_FORCE_DECREASE) || (current_tpt == 0)) {
  1502. IWL_DEBUG_RATE(mvm,
  1503. "Decrease rate because of low SR\n");
  1504. return RS_ACTION_DOWNSCALE;
  1505. }
  1506. if ((low_tpt == IWL_INVALID_VALUE) &&
  1507. (high_tpt == IWL_INVALID_VALUE) &&
  1508. (high != IWL_RATE_INVALID)) {
  1509. IWL_DEBUG_RATE(mvm,
  1510. "No data about high/low rates. Increase rate\n");
  1511. return RS_ACTION_UPSCALE;
  1512. }
  1513. if ((high_tpt == IWL_INVALID_VALUE) &&
  1514. (high != IWL_RATE_INVALID) &&
  1515. (low_tpt != IWL_INVALID_VALUE) &&
  1516. (low_tpt < current_tpt)) {
  1517. IWL_DEBUG_RATE(mvm,
  1518. "No data about high rate and low rate is worse. Increase rate\n");
  1519. return RS_ACTION_UPSCALE;
  1520. }
  1521. if ((high_tpt != IWL_INVALID_VALUE) &&
  1522. (high_tpt > current_tpt)) {
  1523. IWL_DEBUG_RATE(mvm,
  1524. "Higher rate is better. Increate rate\n");
  1525. return RS_ACTION_UPSCALE;
  1526. }
  1527. if ((low_tpt != IWL_INVALID_VALUE) &&
  1528. (high_tpt != IWL_INVALID_VALUE) &&
  1529. (low_tpt < current_tpt) &&
  1530. (high_tpt < current_tpt)) {
  1531. IWL_DEBUG_RATE(mvm,
  1532. "Both high and low are worse. Maintain rate\n");
  1533. return RS_ACTION_STAY;
  1534. }
  1535. if ((low_tpt != IWL_INVALID_VALUE) &&
  1536. (low_tpt > current_tpt)) {
  1537. IWL_DEBUG_RATE(mvm,
  1538. "Lower rate is better\n");
  1539. action = RS_ACTION_DOWNSCALE;
  1540. goto out;
  1541. }
  1542. if ((low_tpt == IWL_INVALID_VALUE) &&
  1543. (low != IWL_RATE_INVALID)) {
  1544. IWL_DEBUG_RATE(mvm,
  1545. "No data about lower rate\n");
  1546. action = RS_ACTION_DOWNSCALE;
  1547. goto out;
  1548. }
  1549. IWL_DEBUG_RATE(mvm, "Maintain rate\n");
  1550. out:
  1551. if ((action == RS_ACTION_DOWNSCALE) && (low != IWL_RATE_INVALID)) {
  1552. if (sr >= RS_SR_NO_DECREASE) {
  1553. IWL_DEBUG_RATE(mvm,
  1554. "SR is above NO DECREASE. Avoid downscale\n");
  1555. action = RS_ACTION_STAY;
  1556. } else if (current_tpt > (100 * tbl->expected_tpt[low])) {
  1557. IWL_DEBUG_RATE(mvm,
  1558. "Current TPT is higher than max expected in low rate. Avoid downscale\n");
  1559. action = RS_ACTION_STAY;
  1560. } else {
  1561. IWL_DEBUG_RATE(mvm, "Decrease rate\n");
  1562. }
  1563. }
  1564. return action;
  1565. }
  1566. static bool rs_stbc_allow(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  1567. struct iwl_lq_sta *lq_sta)
  1568. {
  1569. struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
  1570. struct ieee80211_vif *vif = mvmsta->vif;
  1571. bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
  1572. !vif->bss_conf.ps);
  1573. /* Our chip supports Tx STBC and the peer is an HT/VHT STA which
  1574. * supports STBC of at least 1*SS
  1575. */
  1576. if (!lq_sta->stbc)
  1577. return false;
  1578. if (!mvm->ps_disabled && !sta_ps_disabled)
  1579. return false;
  1580. if (!iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta))
  1581. return false;
  1582. return true;
  1583. }
  1584. static void rs_get_adjacent_txp(struct iwl_mvm *mvm, int index,
  1585. int *weaker, int *stronger)
  1586. {
  1587. *weaker = index + TPC_TX_POWER_STEP;
  1588. if (*weaker > TPC_MAX_REDUCTION)
  1589. *weaker = TPC_INVALID;
  1590. *stronger = index - TPC_TX_POWER_STEP;
  1591. if (*stronger < 0)
  1592. *stronger = TPC_INVALID;
  1593. }
  1594. static bool rs_tpc_allowed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
  1595. struct rs_rate *rate, enum ieee80211_band band)
  1596. {
  1597. int index = rate->index;
  1598. bool cam = (iwlmvm_mod_params.power_scheme == IWL_POWER_SCHEME_CAM);
  1599. bool sta_ps_disabled = (vif->type == NL80211_IFTYPE_STATION &&
  1600. !vif->bss_conf.ps);
  1601. IWL_DEBUG_RATE(mvm, "cam: %d sta_ps_disabled %d\n",
  1602. cam, sta_ps_disabled);
  1603. /*
  1604. * allow tpc only if power management is enabled, or bt coex
  1605. * activity grade allows it and we are on 2.4Ghz.
  1606. */
  1607. if ((cam || sta_ps_disabled) &&
  1608. !iwl_mvm_bt_coex_is_tpc_allowed(mvm, band))
  1609. return false;
  1610. IWL_DEBUG_RATE(mvm, "check rate, table type: %d\n", rate->type);
  1611. if (is_legacy(rate))
  1612. return index == IWL_RATE_54M_INDEX;
  1613. if (is_ht(rate))
  1614. return index == IWL_RATE_MCS_7_INDEX;
  1615. if (is_vht(rate))
  1616. return index == IWL_RATE_MCS_7_INDEX ||
  1617. index == IWL_RATE_MCS_8_INDEX ||
  1618. index == IWL_RATE_MCS_9_INDEX;
  1619. WARN_ON_ONCE(1);
  1620. return false;
  1621. }
  1622. enum tpc_action {
  1623. TPC_ACTION_STAY,
  1624. TPC_ACTION_DECREASE,
  1625. TPC_ACTION_INCREASE,
  1626. TPC_ACTION_NO_RESTIRCTION,
  1627. };
  1628. static enum tpc_action rs_get_tpc_action(struct iwl_mvm *mvm,
  1629. s32 sr, int weak, int strong,
  1630. int current_tpt,
  1631. int weak_tpt, int strong_tpt)
  1632. {
  1633. /* stay until we have valid tpt */
  1634. if (current_tpt == IWL_INVALID_VALUE) {
  1635. IWL_DEBUG_RATE(mvm, "no current tpt. stay.\n");
  1636. return TPC_ACTION_STAY;
  1637. }
  1638. /* Too many failures, increase txp */
  1639. if (sr <= TPC_SR_FORCE_INCREASE || current_tpt == 0) {
  1640. IWL_DEBUG_RATE(mvm, "increase txp because of weak SR\n");
  1641. return TPC_ACTION_NO_RESTIRCTION;
  1642. }
  1643. /* try decreasing first if applicable */
  1644. if (weak != TPC_INVALID) {
  1645. if (weak_tpt == IWL_INVALID_VALUE &&
  1646. (strong_tpt == IWL_INVALID_VALUE ||
  1647. current_tpt >= strong_tpt)) {
  1648. IWL_DEBUG_RATE(mvm,
  1649. "no weak txp measurement. decrease txp\n");
  1650. return TPC_ACTION_DECREASE;
  1651. }
  1652. if (weak_tpt > current_tpt) {
  1653. IWL_DEBUG_RATE(mvm,
  1654. "lower txp has better tpt. decrease txp\n");
  1655. return TPC_ACTION_DECREASE;
  1656. }
  1657. }
  1658. /* next, increase if needed */
  1659. if (sr < TPC_SR_NO_INCREASE && strong != TPC_INVALID) {
  1660. if (weak_tpt == IWL_INVALID_VALUE &&
  1661. strong_tpt != IWL_INVALID_VALUE &&
  1662. current_tpt < strong_tpt) {
  1663. IWL_DEBUG_RATE(mvm,
  1664. "higher txp has better tpt. increase txp\n");
  1665. return TPC_ACTION_INCREASE;
  1666. }
  1667. if (weak_tpt < current_tpt &&
  1668. (strong_tpt == IWL_INVALID_VALUE ||
  1669. strong_tpt > current_tpt)) {
  1670. IWL_DEBUG_RATE(mvm,
  1671. "lower txp has worse tpt. increase txp\n");
  1672. return TPC_ACTION_INCREASE;
  1673. }
  1674. }
  1675. IWL_DEBUG_RATE(mvm, "no need to increase or decrease txp - stay\n");
  1676. return TPC_ACTION_STAY;
  1677. }
  1678. static bool rs_tpc_perform(struct iwl_mvm *mvm,
  1679. struct ieee80211_sta *sta,
  1680. struct iwl_lq_sta *lq_sta,
  1681. struct iwl_scale_tbl_info *tbl)
  1682. {
  1683. struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
  1684. struct ieee80211_vif *vif = mvm_sta->vif;
  1685. struct ieee80211_chanctx_conf *chanctx_conf;
  1686. enum ieee80211_band band;
  1687. struct iwl_rate_scale_data *window;
  1688. struct rs_rate *rate = &tbl->rate;
  1689. enum tpc_action action;
  1690. s32 sr;
  1691. u8 cur = lq_sta->lq.reduced_tpc;
  1692. int current_tpt;
  1693. int weak, strong;
  1694. int weak_tpt = IWL_INVALID_VALUE, strong_tpt = IWL_INVALID_VALUE;
  1695. #ifdef CONFIG_MAC80211_DEBUGFS
  1696. if (lq_sta->pers.dbg_fixed_txp_reduction <= TPC_MAX_REDUCTION) {
  1697. IWL_DEBUG_RATE(mvm, "fixed tpc: %d\n",
  1698. lq_sta->pers.dbg_fixed_txp_reduction);
  1699. lq_sta->lq.reduced_tpc = lq_sta->pers.dbg_fixed_txp_reduction;
  1700. return cur != lq_sta->pers.dbg_fixed_txp_reduction;
  1701. }
  1702. #endif
  1703. rcu_read_lock();
  1704. chanctx_conf = rcu_dereference(vif->chanctx_conf);
  1705. if (WARN_ON(!chanctx_conf))
  1706. band = IEEE80211_NUM_BANDS;
  1707. else
  1708. band = chanctx_conf->def.chan->band;
  1709. rcu_read_unlock();
  1710. if (!rs_tpc_allowed(mvm, vif, rate, band)) {
  1711. IWL_DEBUG_RATE(mvm,
  1712. "tpc is not allowed. remove txp restrictions\n");
  1713. lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
  1714. return cur != TPC_NO_REDUCTION;
  1715. }
  1716. rs_get_adjacent_txp(mvm, cur, &weak, &strong);
  1717. /* Collect measured throughputs for current and adjacent rates */
  1718. window = tbl->tpc_win;
  1719. sr = window[cur].success_ratio;
  1720. current_tpt = window[cur].average_tpt;
  1721. if (weak != TPC_INVALID)
  1722. weak_tpt = window[weak].average_tpt;
  1723. if (strong != TPC_INVALID)
  1724. strong_tpt = window[strong].average_tpt;
  1725. IWL_DEBUG_RATE(mvm,
  1726. "(TPC: %d): cur_tpt %d SR %d weak %d strong %d weak_tpt %d strong_tpt %d\n",
  1727. cur, current_tpt, sr, weak, strong,
  1728. weak_tpt, strong_tpt);
  1729. action = rs_get_tpc_action(mvm, sr, weak, strong,
  1730. current_tpt, weak_tpt, strong_tpt);
  1731. /* override actions if we are on the edge */
  1732. if (weak == TPC_INVALID && action == TPC_ACTION_DECREASE) {
  1733. IWL_DEBUG_RATE(mvm, "already in lowest txp, stay\n");
  1734. action = TPC_ACTION_STAY;
  1735. } else if (strong == TPC_INVALID &&
  1736. (action == TPC_ACTION_INCREASE ||
  1737. action == TPC_ACTION_NO_RESTIRCTION)) {
  1738. IWL_DEBUG_RATE(mvm, "already in highest txp, stay\n");
  1739. action = TPC_ACTION_STAY;
  1740. }
  1741. switch (action) {
  1742. case TPC_ACTION_DECREASE:
  1743. lq_sta->lq.reduced_tpc = weak;
  1744. return true;
  1745. case TPC_ACTION_INCREASE:
  1746. lq_sta->lq.reduced_tpc = strong;
  1747. return true;
  1748. case TPC_ACTION_NO_RESTIRCTION:
  1749. lq_sta->lq.reduced_tpc = TPC_NO_REDUCTION;
  1750. return true;
  1751. case TPC_ACTION_STAY:
  1752. /* do nothing */
  1753. break;
  1754. }
  1755. return false;
  1756. }
  1757. /*
  1758. * Do rate scaling and search for new modulation mode.
  1759. */
  1760. static void rs_rate_scale_perform(struct iwl_mvm *mvm,
  1761. struct ieee80211_sta *sta,
  1762. struct iwl_lq_sta *lq_sta,
  1763. int tid)
  1764. {
  1765. int low = IWL_RATE_INVALID;
  1766. int high = IWL_RATE_INVALID;
  1767. int index;
  1768. struct iwl_rate_scale_data *window = NULL;
  1769. int current_tpt = IWL_INVALID_VALUE;
  1770. int low_tpt = IWL_INVALID_VALUE;
  1771. int high_tpt = IWL_INVALID_VALUE;
  1772. u32 fail_count;
  1773. enum rs_action scale_action = RS_ACTION_STAY;
  1774. u16 rate_mask;
  1775. u8 update_lq = 0;
  1776. struct iwl_scale_tbl_info *tbl, *tbl1;
  1777. u8 active_tbl = 0;
  1778. u8 done_search = 0;
  1779. u16 high_low;
  1780. s32 sr;
  1781. u8 prev_agg = lq_sta->is_agg;
  1782. struct iwl_mvm_sta *sta_priv = (void *)sta->drv_priv;
  1783. struct iwl_mvm_tid_data *tid_data;
  1784. struct rs_rate *rate;
  1785. lq_sta->is_agg = !!sta_priv->agg_tids;
  1786. /*
  1787. * Select rate-scale / modulation-mode table to work with in
  1788. * the rest of this function: "search" if searching for better
  1789. * modulation mode, or "active" if doing rate scaling within a mode.
  1790. */
  1791. if (!lq_sta->search_better_tbl)
  1792. active_tbl = lq_sta->active_tbl;
  1793. else
  1794. active_tbl = 1 - lq_sta->active_tbl;
  1795. tbl = &(lq_sta->lq_info[active_tbl]);
  1796. rate = &tbl->rate;
  1797. if (prev_agg != lq_sta->is_agg) {
  1798. IWL_DEBUG_RATE(mvm,
  1799. "Aggregation changed: prev %d current %d. Update expected TPT table\n",
  1800. prev_agg, lq_sta->is_agg);
  1801. rs_set_expected_tpt_table(lq_sta, tbl);
  1802. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1803. }
  1804. /* current tx rate */
  1805. index = lq_sta->last_txrate_idx;
  1806. /* rates available for this association, and for modulation mode */
  1807. rate_mask = rs_get_supported_rates(lq_sta, rate);
  1808. if (!(BIT(index) & rate_mask)) {
  1809. IWL_ERR(mvm, "Current Rate is not valid\n");
  1810. if (lq_sta->search_better_tbl) {
  1811. /* revert to active table if search table is not valid*/
  1812. rate->type = LQ_NONE;
  1813. lq_sta->search_better_tbl = 0;
  1814. tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1815. rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
  1816. }
  1817. return;
  1818. }
  1819. /* Get expected throughput table and history window for current rate */
  1820. if (!tbl->expected_tpt) {
  1821. IWL_ERR(mvm, "tbl->expected_tpt is NULL\n");
  1822. return;
  1823. }
  1824. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  1825. window = &(tbl->win[index]);
  1826. /*
  1827. * If there is not enough history to calculate actual average
  1828. * throughput, keep analyzing results of more tx frames, without
  1829. * changing rate or mode (bypass most of the rest of this function).
  1830. * Set up new rate table in uCode only if old rate is not supported
  1831. * in current association (use new rate found above).
  1832. */
  1833. fail_count = window->counter - window->success_counter;
  1834. if ((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1835. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH)) {
  1836. IWL_DEBUG_RATE(mvm,
  1837. "(%s: %d): Test Window: succ %d total %d\n",
  1838. rs_pretty_lq_type(rate->type),
  1839. index, window->success_counter, window->counter);
  1840. /* Can't calculate this yet; not enough history */
  1841. window->average_tpt = IWL_INVALID_VALUE;
  1842. /* Should we stay with this modulation mode,
  1843. * or search for a new one? */
  1844. rs_stay_in_table(lq_sta, false);
  1845. goto out;
  1846. }
  1847. /* Else we have enough samples; calculate estimate of
  1848. * actual average throughput */
  1849. if (window->average_tpt != ((window->success_ratio *
  1850. tbl->expected_tpt[index] + 64) / 128)) {
  1851. window->average_tpt = ((window->success_ratio *
  1852. tbl->expected_tpt[index] + 64) / 128);
  1853. }
  1854. /* If we are searching for better modulation mode, check success. */
  1855. if (lq_sta->search_better_tbl) {
  1856. /* If good success, continue using the "search" mode;
  1857. * no need to send new link quality command, since we're
  1858. * continuing to use the setup that we've been trying. */
  1859. if (window->average_tpt > lq_sta->last_tpt) {
  1860. IWL_DEBUG_RATE(mvm,
  1861. "SWITCHING TO NEW TABLE SR: %d "
  1862. "cur-tpt %d old-tpt %d\n",
  1863. window->success_ratio,
  1864. window->average_tpt,
  1865. lq_sta->last_tpt);
  1866. /* Swap tables; "search" becomes "active" */
  1867. lq_sta->active_tbl = active_tbl;
  1868. current_tpt = window->average_tpt;
  1869. /* Else poor success; go back to mode in "active" table */
  1870. } else {
  1871. IWL_DEBUG_RATE(mvm,
  1872. "GOING BACK TO THE OLD TABLE: SR %d "
  1873. "cur-tpt %d old-tpt %d\n",
  1874. window->success_ratio,
  1875. window->average_tpt,
  1876. lq_sta->last_tpt);
  1877. /* Nullify "search" table */
  1878. rate->type = LQ_NONE;
  1879. /* Revert to "active" table */
  1880. active_tbl = lq_sta->active_tbl;
  1881. tbl = &(lq_sta->lq_info[active_tbl]);
  1882. /* Revert to "active" rate and throughput info */
  1883. index = tbl->rate.index;
  1884. current_tpt = lq_sta->last_tpt;
  1885. /* Need to set up a new rate table in uCode */
  1886. update_lq = 1;
  1887. }
  1888. /* Either way, we've made a decision; modulation mode
  1889. * search is done, allow rate adjustment next time. */
  1890. lq_sta->search_better_tbl = 0;
  1891. done_search = 1; /* Don't switch modes below! */
  1892. goto lq_update;
  1893. }
  1894. /* (Else) not in search of better modulation mode, try for better
  1895. * starting rate, while staying in this mode. */
  1896. high_low = rs_get_adjacent_rate(mvm, index, rate_mask, rate->type);
  1897. low = high_low & 0xff;
  1898. high = (high_low >> 8) & 0xff;
  1899. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  1900. sr = window->success_ratio;
  1901. /* Collect measured throughputs for current and adjacent rates */
  1902. current_tpt = window->average_tpt;
  1903. if (low != IWL_RATE_INVALID)
  1904. low_tpt = tbl->win[low].average_tpt;
  1905. if (high != IWL_RATE_INVALID)
  1906. high_tpt = tbl->win[high].average_tpt;
  1907. IWL_DEBUG_RATE(mvm,
  1908. "(%s: %d): cur_tpt %d SR %d low %d high %d low_tpt %d high_tpt %d\n",
  1909. rs_pretty_lq_type(rate->type), index, current_tpt, sr,
  1910. low, high, low_tpt, high_tpt);
  1911. scale_action = rs_get_rate_action(mvm, tbl, sr, low, high,
  1912. current_tpt, low_tpt, high_tpt);
  1913. /* Force a search in case BT doesn't like us being in MIMO */
  1914. if (is_mimo(rate) &&
  1915. !iwl_mvm_bt_coex_is_mimo_allowed(mvm, sta)) {
  1916. IWL_DEBUG_RATE(mvm,
  1917. "BT Coex forbids MIMO. Search for new config\n");
  1918. rs_stay_in_table(lq_sta, true);
  1919. goto lq_update;
  1920. }
  1921. switch (scale_action) {
  1922. case RS_ACTION_DOWNSCALE:
  1923. /* Decrease starting rate, update uCode's rate table */
  1924. if (low != IWL_RATE_INVALID) {
  1925. update_lq = 1;
  1926. index = low;
  1927. } else {
  1928. IWL_DEBUG_RATE(mvm,
  1929. "At the bottom rate. Can't decrease\n");
  1930. }
  1931. break;
  1932. case RS_ACTION_UPSCALE:
  1933. /* Increase starting rate, update uCode's rate table */
  1934. if (high != IWL_RATE_INVALID) {
  1935. update_lq = 1;
  1936. index = high;
  1937. } else {
  1938. IWL_DEBUG_RATE(mvm,
  1939. "At the top rate. Can't increase\n");
  1940. }
  1941. break;
  1942. case RS_ACTION_STAY:
  1943. /* No change */
  1944. if (lq_sta->rs_state == RS_STATE_STAY_IN_COLUMN)
  1945. update_lq = rs_tpc_perform(mvm, sta, lq_sta, tbl);
  1946. break;
  1947. default:
  1948. break;
  1949. }
  1950. lq_update:
  1951. /* Replace uCode's rate table for the destination station. */
  1952. if (update_lq) {
  1953. tbl->rate.index = index;
  1954. rs_update_rate_tbl(mvm, sta, lq_sta, &tbl->rate);
  1955. }
  1956. rs_stay_in_table(lq_sta, false);
  1957. /*
  1958. * Search for new modulation mode if we're:
  1959. * 1) Not changing rates right now
  1960. * 2) Not just finishing up a search
  1961. * 3) Allowing a new search
  1962. */
  1963. if (!update_lq && !done_search &&
  1964. lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_STARTED
  1965. && window->counter) {
  1966. enum rs_column next_column;
  1967. /* Save current throughput to compare with "search" throughput*/
  1968. lq_sta->last_tpt = current_tpt;
  1969. IWL_DEBUG_RATE(mvm,
  1970. "Start Search: update_lq %d done_search %d rs_state %d win->counter %d\n",
  1971. update_lq, done_search, lq_sta->rs_state,
  1972. window->counter);
  1973. next_column = rs_get_next_column(mvm, lq_sta, sta, tbl);
  1974. if (next_column != RS_COLUMN_INVALID) {
  1975. int ret = rs_switch_to_column(mvm, lq_sta, sta,
  1976. next_column);
  1977. if (!ret)
  1978. lq_sta->search_better_tbl = 1;
  1979. } else {
  1980. IWL_DEBUG_RATE(mvm,
  1981. "No more columns to explore in search cycle. Go to RS_STATE_SEARCH_CYCLE_ENDED\n");
  1982. lq_sta->rs_state = RS_STATE_SEARCH_CYCLE_ENDED;
  1983. }
  1984. /* If new "search" mode was selected, set up in uCode table */
  1985. if (lq_sta->search_better_tbl) {
  1986. /* Access the "search" table, clear its history. */
  1987. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1988. rs_rate_scale_clear_tbl_windows(mvm, tbl);
  1989. /* Use new "search" start rate */
  1990. index = tbl->rate.index;
  1991. rs_dump_rate(mvm, &tbl->rate,
  1992. "Switch to SEARCH TABLE:");
  1993. rs_fill_lq_cmd(mvm, sta, lq_sta, &tbl->rate);
  1994. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, false);
  1995. } else {
  1996. done_search = 1;
  1997. }
  1998. }
  1999. if (done_search && lq_sta->rs_state == RS_STATE_SEARCH_CYCLE_ENDED) {
  2000. /* If the "active" (non-search) mode was legacy,
  2001. * and we've tried switching antennas,
  2002. * but we haven't been able to try HT modes (not available),
  2003. * stay with best antenna legacy modulation for a while
  2004. * before next round of mode comparisons. */
  2005. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2006. if (is_legacy(&tbl1->rate)) {
  2007. IWL_DEBUG_RATE(mvm, "LQ: STAY in legacy table\n");
  2008. if (tid != IWL_MAX_TID_COUNT) {
  2009. tid_data = &sta_priv->tid_data[tid];
  2010. if (tid_data->state != IWL_AGG_OFF) {
  2011. IWL_DEBUG_RATE(mvm,
  2012. "Stop aggregation on tid %d\n",
  2013. tid);
  2014. ieee80211_stop_tx_ba_session(sta, tid);
  2015. }
  2016. }
  2017. rs_set_stay_in_table(mvm, 1, lq_sta);
  2018. } else {
  2019. /* If we're in an HT mode, and all 3 mode switch actions
  2020. * have been tried and compared, stay in this best modulation
  2021. * mode for a while before next round of mode comparisons. */
  2022. if ((lq_sta->last_tpt > IWL_AGG_TPT_THREHOLD) &&
  2023. (lq_sta->tx_agg_tid_en & (1 << tid)) &&
  2024. (tid != IWL_MAX_TID_COUNT)) {
  2025. tid_data = &sta_priv->tid_data[tid];
  2026. if (tid_data->state == IWL_AGG_OFF) {
  2027. IWL_DEBUG_RATE(mvm,
  2028. "try to aggregate tid %d\n",
  2029. tid);
  2030. rs_tl_turn_on_agg(mvm, tid,
  2031. lq_sta, sta);
  2032. }
  2033. }
  2034. rs_set_stay_in_table(mvm, 0, lq_sta);
  2035. }
  2036. }
  2037. out:
  2038. lq_sta->last_txrate_idx = index;
  2039. }
  2040. struct rs_init_rate_info {
  2041. s8 rssi;
  2042. u8 rate_idx;
  2043. };
  2044. static const struct rs_init_rate_info rs_init_rates_24ghz[] = {
  2045. { -60, IWL_RATE_54M_INDEX },
  2046. { -64, IWL_RATE_48M_INDEX },
  2047. { -68, IWL_RATE_36M_INDEX },
  2048. { -80, IWL_RATE_24M_INDEX },
  2049. { -84, IWL_RATE_18M_INDEX },
  2050. { -85, IWL_RATE_12M_INDEX },
  2051. { -86, IWL_RATE_11M_INDEX },
  2052. { -88, IWL_RATE_5M_INDEX },
  2053. { -90, IWL_RATE_2M_INDEX },
  2054. { S8_MIN, IWL_RATE_1M_INDEX },
  2055. };
  2056. static const struct rs_init_rate_info rs_init_rates_5ghz[] = {
  2057. { -60, IWL_RATE_54M_INDEX },
  2058. { -64, IWL_RATE_48M_INDEX },
  2059. { -72, IWL_RATE_36M_INDEX },
  2060. { -80, IWL_RATE_24M_INDEX },
  2061. { -84, IWL_RATE_18M_INDEX },
  2062. { -85, IWL_RATE_12M_INDEX },
  2063. { -87, IWL_RATE_9M_INDEX },
  2064. { S8_MIN, IWL_RATE_6M_INDEX },
  2065. };
  2066. /* Choose an initial legacy rate and antenna to use based on the RSSI
  2067. * of last Rx
  2068. */
  2069. static void rs_get_initial_rate(struct iwl_mvm *mvm,
  2070. struct iwl_lq_sta *lq_sta,
  2071. enum ieee80211_band band,
  2072. struct rs_rate *rate)
  2073. {
  2074. int i, nentries;
  2075. s8 best_rssi = S8_MIN;
  2076. u8 best_ant = ANT_NONE;
  2077. u8 valid_tx_ant = mvm->fw->valid_tx_ant;
  2078. const struct rs_init_rate_info *initial_rates;
  2079. for (i = 0; i < ARRAY_SIZE(lq_sta->pers.chain_signal); i++) {
  2080. if (!(lq_sta->pers.chains & BIT(i)))
  2081. continue;
  2082. if (lq_sta->pers.chain_signal[i] > best_rssi) {
  2083. best_rssi = lq_sta->pers.chain_signal[i];
  2084. best_ant = BIT(i);
  2085. }
  2086. }
  2087. IWL_DEBUG_RATE(mvm, "Best ANT: %s Best RSSI: %d\n",
  2088. rs_pretty_ant(best_ant), best_rssi);
  2089. if (best_ant != ANT_A && best_ant != ANT_B)
  2090. rate->ant = first_antenna(valid_tx_ant);
  2091. else
  2092. rate->ant = best_ant;
  2093. rate->sgi = false;
  2094. rate->ldpc = false;
  2095. rate->bw = RATE_MCS_CHAN_WIDTH_20;
  2096. rate->index = find_first_bit(&lq_sta->active_legacy_rate,
  2097. BITS_PER_LONG);
  2098. if (band == IEEE80211_BAND_5GHZ) {
  2099. rate->type = LQ_LEGACY_A;
  2100. initial_rates = rs_init_rates_5ghz;
  2101. nentries = ARRAY_SIZE(rs_init_rates_5ghz);
  2102. } else {
  2103. rate->type = LQ_LEGACY_G;
  2104. initial_rates = rs_init_rates_24ghz;
  2105. nentries = ARRAY_SIZE(rs_init_rates_24ghz);
  2106. }
  2107. if (IWL_MVM_RS_RSSI_BASED_INIT_RATE) {
  2108. for (i = 0; i < nentries; i++) {
  2109. int rate_idx = initial_rates[i].rate_idx;
  2110. if ((best_rssi >= initial_rates[i].rssi) &&
  2111. (BIT(rate_idx) & lq_sta->active_legacy_rate)) {
  2112. rate->index = rate_idx;
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. IWL_DEBUG_RATE(mvm, "rate_idx %d ANT %s\n", rate->index,
  2118. rs_pretty_ant(rate->ant));
  2119. }
  2120. /* Save info about RSSI of last Rx */
  2121. void rs_update_last_rssi(struct iwl_mvm *mvm,
  2122. struct iwl_lq_sta *lq_sta,
  2123. struct ieee80211_rx_status *rx_status)
  2124. {
  2125. lq_sta->pers.chains = rx_status->chains;
  2126. lq_sta->pers.chain_signal[0] = rx_status->chain_signal[0];
  2127. lq_sta->pers.chain_signal[1] = rx_status->chain_signal[1];
  2128. lq_sta->pers.chain_signal[2] = rx_status->chain_signal[2];
  2129. }
  2130. /**
  2131. * rs_initialize_lq - Initialize a station's hardware rate table
  2132. *
  2133. * The uCode's station table contains a table of fallback rates
  2134. * for automatic fallback during transmission.
  2135. *
  2136. * NOTE: This sets up a default set of values. These will be replaced later
  2137. * if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
  2138. * rc80211_simple.
  2139. *
  2140. * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
  2141. * calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
  2142. * which requires station table entry to exist).
  2143. */
  2144. static void rs_initialize_lq(struct iwl_mvm *mvm,
  2145. struct ieee80211_sta *sta,
  2146. struct iwl_lq_sta *lq_sta,
  2147. enum ieee80211_band band,
  2148. bool init)
  2149. {
  2150. struct iwl_scale_tbl_info *tbl;
  2151. struct rs_rate *rate;
  2152. u8 active_tbl = 0;
  2153. if (!sta || !lq_sta)
  2154. return;
  2155. if (!lq_sta->search_better_tbl)
  2156. active_tbl = lq_sta->active_tbl;
  2157. else
  2158. active_tbl = 1 - lq_sta->active_tbl;
  2159. tbl = &(lq_sta->lq_info[active_tbl]);
  2160. rate = &tbl->rate;
  2161. rs_get_initial_rate(mvm, lq_sta, band, rate);
  2162. lq_sta->last_txrate_idx = rate->index;
  2163. WARN_ON_ONCE(rate->ant != ANT_A && rate->ant != ANT_B);
  2164. if (rate->ant == ANT_A)
  2165. tbl->column = RS_COLUMN_LEGACY_ANT_A;
  2166. else
  2167. tbl->column = RS_COLUMN_LEGACY_ANT_B;
  2168. rs_set_expected_tpt_table(lq_sta, tbl);
  2169. rs_fill_lq_cmd(mvm, sta, lq_sta, rate);
  2170. /* TODO restore station should remember the lq cmd */
  2171. iwl_mvm_send_lq_cmd(mvm, &lq_sta->lq, init);
  2172. }
  2173. static void rs_get_rate(void *mvm_r, struct ieee80211_sta *sta, void *mvm_sta,
  2174. struct ieee80211_tx_rate_control *txrc)
  2175. {
  2176. struct sk_buff *skb = txrc->skb;
  2177. struct iwl_op_mode *op_mode __maybe_unused =
  2178. (struct iwl_op_mode *)mvm_r;
  2179. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2180. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  2181. struct iwl_lq_sta *lq_sta = mvm_sta;
  2182. /* TODO: handle rate_idx_mask and rate_idx_mcs_mask */
  2183. /* Treat uninitialized rate scaling data same as non-existing. */
  2184. if (lq_sta && !lq_sta->pers.drv) {
  2185. IWL_DEBUG_RATE(mvm, "Rate scaling not initialized yet.\n");
  2186. mvm_sta = NULL;
  2187. }
  2188. /* Send management frames and NO_ACK data using lowest rate. */
  2189. if (rate_control_send_low(sta, mvm_sta, txrc))
  2190. return;
  2191. iwl_mvm_hwrate_to_tx_rate(lq_sta->last_rate_n_flags,
  2192. info->band, &info->control.rates[0]);
  2193. info->control.rates[0].count = 1;
  2194. }
  2195. static void *rs_alloc_sta(void *mvm_rate, struct ieee80211_sta *sta,
  2196. gfp_t gfp)
  2197. {
  2198. struct iwl_mvm_sta *sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  2199. struct iwl_op_mode *op_mode = (struct iwl_op_mode *)mvm_rate;
  2200. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  2201. struct iwl_lq_sta *lq_sta = &sta_priv->lq_sta;
  2202. IWL_DEBUG_RATE(mvm, "create station rate scale window\n");
  2203. lq_sta->pers.drv = mvm;
  2204. #ifdef CONFIG_MAC80211_DEBUGFS
  2205. lq_sta->pers.dbg_fixed_rate = 0;
  2206. lq_sta->pers.dbg_fixed_txp_reduction = TPC_INVALID;
  2207. #endif
  2208. lq_sta->pers.chains = 0;
  2209. memset(lq_sta->pers.chain_signal, 0, sizeof(lq_sta->pers.chain_signal));
  2210. return &sta_priv->lq_sta;
  2211. }
  2212. static int rs_vht_highest_rx_mcs_index(struct ieee80211_sta_vht_cap *vht_cap,
  2213. int nss)
  2214. {
  2215. u16 rx_mcs = le16_to_cpu(vht_cap->vht_mcs.rx_mcs_map) &
  2216. (0x3 << (2 * (nss - 1)));
  2217. rx_mcs >>= (2 * (nss - 1));
  2218. if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_7)
  2219. return IWL_RATE_MCS_7_INDEX;
  2220. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_8)
  2221. return IWL_RATE_MCS_8_INDEX;
  2222. else if (rx_mcs == IEEE80211_VHT_MCS_SUPPORT_0_9)
  2223. return IWL_RATE_MCS_9_INDEX;
  2224. WARN_ON_ONCE(rx_mcs != IEEE80211_VHT_MCS_NOT_SUPPORTED);
  2225. return -1;
  2226. }
  2227. static void rs_vht_set_enabled_rates(struct ieee80211_sta *sta,
  2228. struct ieee80211_sta_vht_cap *vht_cap,
  2229. struct iwl_lq_sta *lq_sta)
  2230. {
  2231. int i;
  2232. int highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 1);
  2233. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  2234. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  2235. if (i == IWL_RATE_9M_INDEX)
  2236. continue;
  2237. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  2238. if (i == IWL_RATE_MCS_9_INDEX &&
  2239. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  2240. continue;
  2241. lq_sta->active_siso_rate |= BIT(i);
  2242. }
  2243. }
  2244. if (sta->rx_nss < 2)
  2245. return;
  2246. highest_mcs = rs_vht_highest_rx_mcs_index(vht_cap, 2);
  2247. if (highest_mcs >= IWL_RATE_MCS_0_INDEX) {
  2248. for (i = IWL_RATE_MCS_0_INDEX; i <= highest_mcs; i++) {
  2249. if (i == IWL_RATE_9M_INDEX)
  2250. continue;
  2251. /* VHT MCS9 isn't valid for 20Mhz for NSS=1,2 */
  2252. if (i == IWL_RATE_MCS_9_INDEX &&
  2253. sta->bandwidth == IEEE80211_STA_RX_BW_20)
  2254. continue;
  2255. lq_sta->active_mimo2_rate |= BIT(i);
  2256. }
  2257. }
  2258. }
  2259. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2260. static void iwl_mvm_reset_frame_stats(struct iwl_mvm *mvm,
  2261. struct iwl_mvm_frame_stats *stats)
  2262. {
  2263. spin_lock_bh(&mvm->drv_stats_lock);
  2264. memset(stats, 0, sizeof(*stats));
  2265. spin_unlock_bh(&mvm->drv_stats_lock);
  2266. }
  2267. void iwl_mvm_update_frame_stats(struct iwl_mvm *mvm,
  2268. struct iwl_mvm_frame_stats *stats,
  2269. u32 rate, bool agg)
  2270. {
  2271. u8 nss = 0, mcs = 0;
  2272. spin_lock(&mvm->drv_stats_lock);
  2273. if (agg)
  2274. stats->agg_frames++;
  2275. stats->success_frames++;
  2276. switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
  2277. case RATE_MCS_CHAN_WIDTH_20:
  2278. stats->bw_20_frames++;
  2279. break;
  2280. case RATE_MCS_CHAN_WIDTH_40:
  2281. stats->bw_40_frames++;
  2282. break;
  2283. case RATE_MCS_CHAN_WIDTH_80:
  2284. stats->bw_80_frames++;
  2285. break;
  2286. default:
  2287. WARN_ONCE(1, "bad BW. rate 0x%x", rate);
  2288. }
  2289. if (rate & RATE_MCS_HT_MSK) {
  2290. stats->ht_frames++;
  2291. mcs = rate & RATE_HT_MCS_RATE_CODE_MSK;
  2292. nss = ((rate & RATE_HT_MCS_NSS_MSK) >> RATE_HT_MCS_NSS_POS) + 1;
  2293. } else if (rate & RATE_MCS_VHT_MSK) {
  2294. stats->vht_frames++;
  2295. mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
  2296. nss = ((rate & RATE_VHT_MCS_NSS_MSK) >>
  2297. RATE_VHT_MCS_NSS_POS) + 1;
  2298. } else {
  2299. stats->legacy_frames++;
  2300. }
  2301. if (nss == 1)
  2302. stats->siso_frames++;
  2303. else if (nss == 2)
  2304. stats->mimo2_frames++;
  2305. if (rate & RATE_MCS_SGI_MSK)
  2306. stats->sgi_frames++;
  2307. else
  2308. stats->ngi_frames++;
  2309. stats->last_rates[stats->last_frame_idx] = rate;
  2310. stats->last_frame_idx = (stats->last_frame_idx + 1) %
  2311. ARRAY_SIZE(stats->last_rates);
  2312. spin_unlock(&mvm->drv_stats_lock);
  2313. }
  2314. #endif
  2315. /*
  2316. * Called after adding a new station to initialize rate scaling
  2317. */
  2318. void iwl_mvm_rs_rate_init(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
  2319. enum ieee80211_band band, bool init)
  2320. {
  2321. int i, j;
  2322. struct ieee80211_hw *hw = mvm->hw;
  2323. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  2324. struct ieee80211_sta_vht_cap *vht_cap = &sta->vht_cap;
  2325. struct iwl_mvm_sta *sta_priv;
  2326. struct iwl_lq_sta *lq_sta;
  2327. struct ieee80211_supported_band *sband;
  2328. unsigned long supp; /* must be unsigned long for for_each_set_bit */
  2329. sta_priv = (struct iwl_mvm_sta *)sta->drv_priv;
  2330. lq_sta = &sta_priv->lq_sta;
  2331. /* clear all non-persistent lq data */
  2332. memset(lq_sta, 0, offsetof(typeof(*lq_sta), pers));
  2333. sband = hw->wiphy->bands[band];
  2334. lq_sta->lq.sta_id = sta_priv->sta_id;
  2335. for (j = 0; j < LQ_SIZE; j++)
  2336. rs_rate_scale_clear_tbl_windows(mvm, &lq_sta->lq_info[j]);
  2337. lq_sta->flush_timer = 0;
  2338. lq_sta->last_tx = jiffies;
  2339. IWL_DEBUG_RATE(mvm,
  2340. "LQ: *** rate scale station global init for station %d ***\n",
  2341. sta_priv->sta_id);
  2342. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  2343. * the lowest or the highest rate.. Could consider using RSSI from
  2344. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  2345. * after assoc.. */
  2346. lq_sta->missed_rate_counter = IWL_MISSED_RATE_MAX;
  2347. lq_sta->band = sband->band;
  2348. /*
  2349. * active legacy rates as per supported rates bitmap
  2350. */
  2351. supp = sta->supp_rates[sband->band];
  2352. lq_sta->active_legacy_rate = 0;
  2353. for_each_set_bit(i, &supp, BITS_PER_LONG)
  2354. lq_sta->active_legacy_rate |= BIT(sband->bitrates[i].hw_value);
  2355. /* TODO: should probably account for rx_highest for both HT/VHT */
  2356. if (!vht_cap || !vht_cap->vht_supported) {
  2357. /* active_siso_rate mask includes 9 MBits (bit 5),
  2358. * and CCK (bits 0-3), supp_rates[] does not;
  2359. * shift to convert format, force 9 MBits off.
  2360. */
  2361. lq_sta->active_siso_rate = ht_cap->mcs.rx_mask[0] << 1;
  2362. lq_sta->active_siso_rate |= ht_cap->mcs.rx_mask[0] & 0x1;
  2363. lq_sta->active_siso_rate &= ~((u16)0x2);
  2364. lq_sta->active_siso_rate <<= IWL_FIRST_OFDM_RATE;
  2365. /* Same here */
  2366. lq_sta->active_mimo2_rate = ht_cap->mcs.rx_mask[1] << 1;
  2367. lq_sta->active_mimo2_rate |= ht_cap->mcs.rx_mask[1] & 0x1;
  2368. lq_sta->active_mimo2_rate &= ~((u16)0x2);
  2369. lq_sta->active_mimo2_rate <<= IWL_FIRST_OFDM_RATE;
  2370. lq_sta->is_vht = false;
  2371. if (mvm->cfg->ht_params->ldpc &&
  2372. (ht_cap->cap & IEEE80211_HT_CAP_LDPC_CODING))
  2373. lq_sta->ldpc = true;
  2374. if (mvm->cfg->ht_params->stbc &&
  2375. (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
  2376. (ht_cap->cap & IEEE80211_HT_CAP_RX_STBC))
  2377. lq_sta->stbc = true;
  2378. } else {
  2379. rs_vht_set_enabled_rates(sta, vht_cap, lq_sta);
  2380. lq_sta->is_vht = true;
  2381. if (mvm->cfg->ht_params->ldpc &&
  2382. (vht_cap->cap & IEEE80211_VHT_CAP_RXLDPC))
  2383. lq_sta->ldpc = true;
  2384. if (mvm->cfg->ht_params->stbc &&
  2385. (num_of_ant(mvm->fw->valid_tx_ant) > 1) &&
  2386. (vht_cap->cap & IEEE80211_VHT_CAP_RXSTBC_MASK))
  2387. lq_sta->stbc = true;
  2388. }
  2389. if (IWL_MVM_RS_DISABLE_MIMO)
  2390. lq_sta->active_mimo2_rate = 0;
  2391. lq_sta->max_legacy_rate_idx = find_last_bit(&lq_sta->active_legacy_rate,
  2392. BITS_PER_LONG);
  2393. lq_sta->max_siso_rate_idx = find_last_bit(&lq_sta->active_siso_rate,
  2394. BITS_PER_LONG);
  2395. lq_sta->max_mimo2_rate_idx = find_last_bit(&lq_sta->active_mimo2_rate,
  2396. BITS_PER_LONG);
  2397. IWL_DEBUG_RATE(mvm,
  2398. "RATE MASK: LEGACY=%lX SISO=%lX MIMO2=%lX VHT=%d LDPC=%d STBC%d\n",
  2399. lq_sta->active_legacy_rate,
  2400. lq_sta->active_siso_rate,
  2401. lq_sta->active_mimo2_rate,
  2402. lq_sta->is_vht, lq_sta->ldpc, lq_sta->stbc);
  2403. IWL_DEBUG_RATE(mvm, "MAX RATE: LEGACY=%d SISO=%d MIMO2=%d\n",
  2404. lq_sta->max_legacy_rate_idx,
  2405. lq_sta->max_siso_rate_idx,
  2406. lq_sta->max_mimo2_rate_idx);
  2407. /* These values will be overridden later */
  2408. lq_sta->lq.single_stream_ant_msk =
  2409. first_antenna(mvm->fw->valid_tx_ant);
  2410. lq_sta->lq.dual_stream_ant_msk = ANT_AB;
  2411. /* as default allow aggregation for all tids */
  2412. lq_sta->tx_agg_tid_en = IWL_AGG_ALL_TID;
  2413. lq_sta->is_agg = 0;
  2414. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2415. iwl_mvm_reset_frame_stats(mvm, &mvm->drv_rx_stats);
  2416. #endif
  2417. rs_initialize_lq(mvm, sta, lq_sta, band, init);
  2418. }
  2419. static void rs_rate_update(void *mvm_r,
  2420. struct ieee80211_supported_band *sband,
  2421. struct cfg80211_chan_def *chandef,
  2422. struct ieee80211_sta *sta, void *priv_sta,
  2423. u32 changed)
  2424. {
  2425. u8 tid;
  2426. struct iwl_op_mode *op_mode =
  2427. (struct iwl_op_mode *)mvm_r;
  2428. struct iwl_mvm *mvm = IWL_OP_MODE_GET_MVM(op_mode);
  2429. /* Stop any ongoing aggregations as rs starts off assuming no agg */
  2430. for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
  2431. ieee80211_stop_tx_ba_session(sta, tid);
  2432. iwl_mvm_rs_rate_init(mvm, sta, sband->band, false);
  2433. }
  2434. #ifdef CONFIG_MAC80211_DEBUGFS
  2435. static void rs_build_rates_table_from_fixed(struct iwl_mvm *mvm,
  2436. struct iwl_lq_cmd *lq_cmd,
  2437. enum ieee80211_band band,
  2438. u32 ucode_rate)
  2439. {
  2440. struct rs_rate rate;
  2441. int i;
  2442. int num_rates = ARRAY_SIZE(lq_cmd->rs_table);
  2443. __le32 ucode_rate_le32 = cpu_to_le32(ucode_rate);
  2444. u8 ant = (ucode_rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
  2445. for (i = 0; i < num_rates; i++)
  2446. lq_cmd->rs_table[i] = ucode_rate_le32;
  2447. rs_rate_from_ucode_rate(ucode_rate, band, &rate);
  2448. if (is_mimo(&rate))
  2449. lq_cmd->mimo_delim = num_rates - 1;
  2450. else
  2451. lq_cmd->mimo_delim = 0;
  2452. lq_cmd->reduced_tpc = 0;
  2453. if (num_of_ant(ant) == 1)
  2454. lq_cmd->single_stream_ant_msk = ant;
  2455. lq_cmd->agg_frame_cnt_limit = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2456. }
  2457. #endif /* CONFIG_MAC80211_DEBUGFS */
  2458. static void rs_fill_rates_for_column(struct iwl_mvm *mvm,
  2459. struct iwl_lq_sta *lq_sta,
  2460. struct rs_rate *rate,
  2461. __le32 *rs_table, int *rs_table_index,
  2462. int num_rates, int num_retries,
  2463. u8 valid_tx_ant, bool toggle_ant)
  2464. {
  2465. int i, j;
  2466. __le32 ucode_rate;
  2467. bool bottom_reached = false;
  2468. int prev_rate_idx = rate->index;
  2469. int end = LINK_QUAL_MAX_RETRY_NUM;
  2470. int index = *rs_table_index;
  2471. for (i = 0; i < num_rates && index < end; i++) {
  2472. ucode_rate = cpu_to_le32(ucode_rate_from_rs_rate(mvm, rate));
  2473. for (j = 0; j < num_retries && index < end; j++, index++)
  2474. rs_table[index] = ucode_rate;
  2475. if (toggle_ant)
  2476. rs_toggle_antenna(valid_tx_ant, rate);
  2477. prev_rate_idx = rate->index;
  2478. bottom_reached = rs_get_lower_rate_in_column(lq_sta, rate);
  2479. if (bottom_reached && !is_legacy(rate))
  2480. break;
  2481. }
  2482. if (!bottom_reached)
  2483. rate->index = prev_rate_idx;
  2484. *rs_table_index = index;
  2485. }
  2486. /* Building the rate table is non trivial. When we're in MIMO2/VHT/80Mhz/SGI
  2487. * column the rate table should look like this:
  2488. *
  2489. * rate[0] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
  2490. * rate[1] 0x400D019 VHT | ANT: AB BW: 80Mhz MCS: 9 NSS: 2 SGI
  2491. * rate[2] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
  2492. * rate[3] 0x400D018 VHT | ANT: AB BW: 80Mhz MCS: 8 NSS: 2 SGI
  2493. * rate[4] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
  2494. * rate[5] 0x400D017 VHT | ANT: AB BW: 80Mhz MCS: 7 NSS: 2 SGI
  2495. * rate[6] 0x4005007 VHT | ANT: A BW: 80Mhz MCS: 7 NSS: 1 NGI
  2496. * rate[7] 0x4009006 VHT | ANT: B BW: 80Mhz MCS: 6 NSS: 1 NGI
  2497. * rate[8] 0x4005005 VHT | ANT: A BW: 80Mhz MCS: 5 NSS: 1 NGI
  2498. * rate[9] 0x800B Legacy | ANT: B Rate: 36 Mbps
  2499. * rate[10] 0x4009 Legacy | ANT: A Rate: 24 Mbps
  2500. * rate[11] 0x8007 Legacy | ANT: B Rate: 18 Mbps
  2501. * rate[12] 0x4005 Legacy | ANT: A Rate: 12 Mbps
  2502. * rate[13] 0x800F Legacy | ANT: B Rate: 9 Mbps
  2503. * rate[14] 0x400D Legacy | ANT: A Rate: 6 Mbps
  2504. * rate[15] 0x800D Legacy | ANT: B Rate: 6 Mbps
  2505. */
  2506. static void rs_build_rates_table(struct iwl_mvm *mvm,
  2507. struct ieee80211_sta *sta,
  2508. struct iwl_lq_sta *lq_sta,
  2509. const struct rs_rate *initial_rate)
  2510. {
  2511. struct rs_rate rate;
  2512. int num_rates, num_retries, index = 0;
  2513. u8 valid_tx_ant = 0;
  2514. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2515. bool toggle_ant = false;
  2516. memcpy(&rate, initial_rate, sizeof(rate));
  2517. valid_tx_ant = mvm->fw->valid_tx_ant;
  2518. rate.stbc = rs_stbc_allow(mvm, sta, lq_sta);
  2519. if (is_siso(&rate)) {
  2520. num_rates = RS_INITIAL_SISO_NUM_RATES;
  2521. num_retries = RS_HT_VHT_RETRIES_PER_RATE;
  2522. } else if (is_mimo(&rate)) {
  2523. num_rates = RS_INITIAL_MIMO_NUM_RATES;
  2524. num_retries = RS_HT_VHT_RETRIES_PER_RATE;
  2525. } else {
  2526. num_rates = RS_INITIAL_LEGACY_NUM_RATES;
  2527. num_retries = RS_LEGACY_RETRIES_PER_RATE;
  2528. toggle_ant = true;
  2529. }
  2530. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2531. num_rates, num_retries, valid_tx_ant,
  2532. toggle_ant);
  2533. rs_get_lower_rate_down_column(lq_sta, &rate);
  2534. if (is_siso(&rate)) {
  2535. num_rates = RS_SECONDARY_SISO_NUM_RATES;
  2536. num_retries = RS_SECONDARY_SISO_RETRIES;
  2537. lq_cmd->mimo_delim = index;
  2538. } else if (is_legacy(&rate)) {
  2539. num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
  2540. num_retries = RS_LEGACY_RETRIES_PER_RATE;
  2541. } else {
  2542. WARN_ON_ONCE(1);
  2543. }
  2544. toggle_ant = true;
  2545. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2546. num_rates, num_retries, valid_tx_ant,
  2547. toggle_ant);
  2548. rs_get_lower_rate_down_column(lq_sta, &rate);
  2549. num_rates = RS_SECONDARY_LEGACY_NUM_RATES;
  2550. num_retries = RS_LEGACY_RETRIES_PER_RATE;
  2551. rs_fill_rates_for_column(mvm, lq_sta, &rate, lq_cmd->rs_table, &index,
  2552. num_rates, num_retries, valid_tx_ant,
  2553. toggle_ant);
  2554. }
  2555. static void rs_fill_lq_cmd(struct iwl_mvm *mvm,
  2556. struct ieee80211_sta *sta,
  2557. struct iwl_lq_sta *lq_sta,
  2558. const struct rs_rate *initial_rate)
  2559. {
  2560. struct iwl_lq_cmd *lq_cmd = &lq_sta->lq;
  2561. struct iwl_mvm_sta *mvmsta;
  2562. struct iwl_mvm_vif *mvmvif;
  2563. lq_cmd->agg_disable_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2564. lq_cmd->agg_time_limit =
  2565. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2566. #ifdef CONFIG_MAC80211_DEBUGFS
  2567. if (lq_sta->pers.dbg_fixed_rate) {
  2568. rs_build_rates_table_from_fixed(mvm, lq_cmd,
  2569. lq_sta->band,
  2570. lq_sta->pers.dbg_fixed_rate);
  2571. return;
  2572. }
  2573. #endif
  2574. if (WARN_ON_ONCE(!sta || !initial_rate))
  2575. return;
  2576. rs_build_rates_table(mvm, sta, lq_sta, initial_rate);
  2577. if (num_of_ant(initial_rate->ant) == 1)
  2578. lq_cmd->single_stream_ant_msk = initial_rate->ant;
  2579. mvmsta = iwl_mvm_sta_from_mac80211(sta);
  2580. mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif);
  2581. if (num_of_ant(initial_rate->ant) == 1)
  2582. lq_cmd->single_stream_ant_msk = initial_rate->ant;
  2583. lq_cmd->agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
  2584. /*
  2585. * In case of low latency, tell the firwmare to leave a frame in the
  2586. * Tx Fifo so that it can start a transaction in the same TxOP. This
  2587. * basically allows the firmware to send bursts.
  2588. */
  2589. if (iwl_mvm_vif_low_latency(mvmvif)) {
  2590. lq_cmd->agg_frame_cnt_limit--;
  2591. if (mvm->low_latency_agg_frame_limit)
  2592. lq_cmd->agg_frame_cnt_limit =
  2593. min(lq_cmd->agg_frame_cnt_limit,
  2594. mvm->low_latency_agg_frame_limit);
  2595. }
  2596. if (mvmsta->vif->p2p)
  2597. lq_cmd->flags |= LQ_FLAG_USE_RTS_MSK;
  2598. lq_cmd->agg_time_limit =
  2599. cpu_to_le16(iwl_mvm_coex_agg_time_limit(mvm, sta));
  2600. }
  2601. static void *rs_alloc(struct ieee80211_hw *hw, struct dentry *debugfsdir)
  2602. {
  2603. return hw->priv;
  2604. }
  2605. /* rate scale requires free function to be implemented */
  2606. static void rs_free(void *mvm_rate)
  2607. {
  2608. return;
  2609. }
  2610. static void rs_free_sta(void *mvm_r, struct ieee80211_sta *sta,
  2611. void *mvm_sta)
  2612. {
  2613. struct iwl_op_mode *op_mode __maybe_unused = mvm_r;
  2614. struct iwl_mvm *mvm __maybe_unused = IWL_OP_MODE_GET_MVM(op_mode);
  2615. IWL_DEBUG_RATE(mvm, "enter\n");
  2616. IWL_DEBUG_RATE(mvm, "leave\n");
  2617. }
  2618. #ifdef CONFIG_MAC80211_DEBUGFS
  2619. int rs_pretty_print_rate(char *buf, const u32 rate)
  2620. {
  2621. char *type, *bw;
  2622. u8 mcs = 0, nss = 0;
  2623. u8 ant = (rate & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS;
  2624. if (!(rate & RATE_MCS_HT_MSK) &&
  2625. !(rate & RATE_MCS_VHT_MSK)) {
  2626. int index = iwl_hwrate_to_plcp_idx(rate);
  2627. return sprintf(buf, "Legacy | ANT: %s Rate: %s Mbps\n",
  2628. rs_pretty_ant(ant),
  2629. index == IWL_RATE_INVALID ? "BAD" :
  2630. iwl_rate_mcs[index].mbps);
  2631. }
  2632. if (rate & RATE_MCS_VHT_MSK) {
  2633. type = "VHT";
  2634. mcs = rate & RATE_VHT_MCS_RATE_CODE_MSK;
  2635. nss = ((rate & RATE_VHT_MCS_NSS_MSK)
  2636. >> RATE_VHT_MCS_NSS_POS) + 1;
  2637. } else if (rate & RATE_MCS_HT_MSK) {
  2638. type = "HT";
  2639. mcs = rate & RATE_HT_MCS_INDEX_MSK;
  2640. } else {
  2641. type = "Unknown"; /* shouldn't happen */
  2642. }
  2643. switch (rate & RATE_MCS_CHAN_WIDTH_MSK) {
  2644. case RATE_MCS_CHAN_WIDTH_20:
  2645. bw = "20Mhz";
  2646. break;
  2647. case RATE_MCS_CHAN_WIDTH_40:
  2648. bw = "40Mhz";
  2649. break;
  2650. case RATE_MCS_CHAN_WIDTH_80:
  2651. bw = "80Mhz";
  2652. break;
  2653. case RATE_MCS_CHAN_WIDTH_160:
  2654. bw = "160Mhz";
  2655. break;
  2656. default:
  2657. bw = "BAD BW";
  2658. }
  2659. return sprintf(buf, "%s | ANT: %s BW: %s MCS: %d NSS: %d %s%s%s%s%s\n",
  2660. type, rs_pretty_ant(ant), bw, mcs, nss,
  2661. (rate & RATE_MCS_SGI_MSK) ? "SGI " : "NGI ",
  2662. (rate & RATE_MCS_HT_STBC_MSK) ? "STBC " : "",
  2663. (rate & RATE_MCS_LDPC_MSK) ? "LDPC " : "",
  2664. (rate & RATE_MCS_BF_MSK) ? "BF " : "",
  2665. (rate & RATE_MCS_ZLF_MSK) ? "ZLF " : "");
  2666. }
  2667. /**
  2668. * Program the device to use fixed rate for frame transmit
  2669. * This is for debugging/testing only
  2670. * once the device start use fixed rate, we need to reload the module
  2671. * to being back the normal operation.
  2672. */
  2673. static void rs_program_fix_rate(struct iwl_mvm *mvm,
  2674. struct iwl_lq_sta *lq_sta)
  2675. {
  2676. lq_sta->active_legacy_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  2677. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2678. lq_sta->active_mimo2_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2679. IWL_DEBUG_RATE(mvm, "sta_id %d rate 0x%X\n",
  2680. lq_sta->lq.sta_id, lq_sta->pers.dbg_fixed_rate);
  2681. if (lq_sta->pers.dbg_fixed_rate) {
  2682. rs_fill_lq_cmd(mvm, NULL, lq_sta, NULL);
  2683. iwl_mvm_send_lq_cmd(lq_sta->pers.drv, &lq_sta->lq, false);
  2684. }
  2685. }
  2686. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2687. const char __user *user_buf, size_t count, loff_t *ppos)
  2688. {
  2689. struct iwl_lq_sta *lq_sta = file->private_data;
  2690. struct iwl_mvm *mvm;
  2691. char buf[64];
  2692. size_t buf_size;
  2693. u32 parsed_rate;
  2694. mvm = lq_sta->pers.drv;
  2695. memset(buf, 0, sizeof(buf));
  2696. buf_size = min(count, sizeof(buf) - 1);
  2697. if (copy_from_user(buf, user_buf, buf_size))
  2698. return -EFAULT;
  2699. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2700. lq_sta->pers.dbg_fixed_rate = parsed_rate;
  2701. else
  2702. lq_sta->pers.dbg_fixed_rate = 0;
  2703. rs_program_fix_rate(mvm, lq_sta);
  2704. return count;
  2705. }
  2706. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2707. char __user *user_buf, size_t count, loff_t *ppos)
  2708. {
  2709. char *buff;
  2710. int desc = 0;
  2711. int i = 0;
  2712. ssize_t ret;
  2713. struct iwl_lq_sta *lq_sta = file->private_data;
  2714. struct iwl_mvm *mvm;
  2715. struct iwl_scale_tbl_info *tbl = &(lq_sta->lq_info[lq_sta->active_tbl]);
  2716. struct rs_rate *rate = &tbl->rate;
  2717. mvm = lq_sta->pers.drv;
  2718. buff = kmalloc(2048, GFP_KERNEL);
  2719. if (!buff)
  2720. return -ENOMEM;
  2721. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2722. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%lX\n",
  2723. lq_sta->total_failed, lq_sta->total_success,
  2724. lq_sta->active_legacy_rate);
  2725. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2726. lq_sta->pers.dbg_fixed_rate);
  2727. desc += sprintf(buff+desc, "valid_tx_ant %s%s%s\n",
  2728. (mvm->fw->valid_tx_ant & ANT_A) ? "ANT_A," : "",
  2729. (mvm->fw->valid_tx_ant & ANT_B) ? "ANT_B," : "",
  2730. (mvm->fw->valid_tx_ant & ANT_C) ? "ANT_C" : "");
  2731. desc += sprintf(buff+desc, "lq type %s\n",
  2732. (is_legacy(rate)) ? "legacy" :
  2733. is_vht(rate) ? "VHT" : "HT");
  2734. if (!is_legacy(rate)) {
  2735. desc += sprintf(buff+desc, " %s",
  2736. (is_siso(rate)) ? "SISO" : "MIMO2");
  2737. desc += sprintf(buff+desc, " %s",
  2738. (is_ht20(rate)) ? "20MHz" :
  2739. (is_ht40(rate)) ? "40MHz" :
  2740. (is_ht80(rate)) ? "80Mhz" : "BAD BW");
  2741. desc += sprintf(buff+desc, " %s %s %s\n",
  2742. (rate->sgi) ? "SGI" : "NGI",
  2743. (rate->ldpc) ? "LDPC" : "BCC",
  2744. (lq_sta->is_agg) ? "AGG on" : "");
  2745. }
  2746. desc += sprintf(buff+desc, "last tx rate=0x%X\n",
  2747. lq_sta->last_rate_n_flags);
  2748. desc += sprintf(buff+desc,
  2749. "general: flags=0x%X mimo-d=%d s-ant=0x%x d-ant=0x%x\n",
  2750. lq_sta->lq.flags,
  2751. lq_sta->lq.mimo_delim,
  2752. lq_sta->lq.single_stream_ant_msk,
  2753. lq_sta->lq.dual_stream_ant_msk);
  2754. desc += sprintf(buff+desc,
  2755. "agg: time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2756. le16_to_cpu(lq_sta->lq.agg_time_limit),
  2757. lq_sta->lq.agg_disable_start_th,
  2758. lq_sta->lq.agg_frame_cnt_limit);
  2759. desc += sprintf(buff+desc, "reduced tpc=%d\n", lq_sta->lq.reduced_tpc);
  2760. desc += sprintf(buff+desc,
  2761. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2762. lq_sta->lq.initial_rate_index[0],
  2763. lq_sta->lq.initial_rate_index[1],
  2764. lq_sta->lq.initial_rate_index[2],
  2765. lq_sta->lq.initial_rate_index[3]);
  2766. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
  2767. u32 r = le32_to_cpu(lq_sta->lq.rs_table[i]);
  2768. desc += sprintf(buff+desc, " rate[%d] 0x%X ", i, r);
  2769. desc += rs_pretty_print_rate(buff+desc, r);
  2770. }
  2771. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2772. kfree(buff);
  2773. return ret;
  2774. }
  2775. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2776. .write = rs_sta_dbgfs_scale_table_write,
  2777. .read = rs_sta_dbgfs_scale_table_read,
  2778. .open = simple_open,
  2779. .llseek = default_llseek,
  2780. };
  2781. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2782. char __user *user_buf, size_t count, loff_t *ppos)
  2783. {
  2784. char *buff;
  2785. int desc = 0;
  2786. int i, j;
  2787. ssize_t ret;
  2788. struct iwl_scale_tbl_info *tbl;
  2789. struct rs_rate *rate;
  2790. struct iwl_lq_sta *lq_sta = file->private_data;
  2791. buff = kmalloc(1024, GFP_KERNEL);
  2792. if (!buff)
  2793. return -ENOMEM;
  2794. for (i = 0; i < LQ_SIZE; i++) {
  2795. tbl = &(lq_sta->lq_info[i]);
  2796. rate = &tbl->rate;
  2797. desc += sprintf(buff+desc,
  2798. "%s type=%d SGI=%d BW=%s DUP=0\n"
  2799. "index=%d\n",
  2800. lq_sta->active_tbl == i ? "*" : "x",
  2801. rate->type,
  2802. rate->sgi,
  2803. is_ht20(rate) ? "20Mhz" :
  2804. is_ht40(rate) ? "40Mhz" :
  2805. is_ht80(rate) ? "80Mhz" : "ERR",
  2806. rate->index);
  2807. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2808. desc += sprintf(buff+desc,
  2809. "counter=%d success=%d %%=%d\n",
  2810. tbl->win[j].counter,
  2811. tbl->win[j].success_counter,
  2812. tbl->win[j].success_ratio);
  2813. }
  2814. }
  2815. ret = simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2816. kfree(buff);
  2817. return ret;
  2818. }
  2819. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2820. .read = rs_sta_dbgfs_stats_table_read,
  2821. .open = simple_open,
  2822. .llseek = default_llseek,
  2823. };
  2824. static ssize_t rs_sta_dbgfs_drv_tx_stats_read(struct file *file,
  2825. char __user *user_buf,
  2826. size_t count, loff_t *ppos)
  2827. {
  2828. static const char * const column_name[] = {
  2829. [RS_COLUMN_LEGACY_ANT_A] = "LEGACY_ANT_A",
  2830. [RS_COLUMN_LEGACY_ANT_B] = "LEGACY_ANT_B",
  2831. [RS_COLUMN_SISO_ANT_A] = "SISO_ANT_A",
  2832. [RS_COLUMN_SISO_ANT_B] = "SISO_ANT_B",
  2833. [RS_COLUMN_SISO_ANT_A_SGI] = "SISO_ANT_A_SGI",
  2834. [RS_COLUMN_SISO_ANT_B_SGI] = "SISO_ANT_B_SGI",
  2835. [RS_COLUMN_MIMO2] = "MIMO2",
  2836. [RS_COLUMN_MIMO2_SGI] = "MIMO2_SGI",
  2837. };
  2838. static const char * const rate_name[] = {
  2839. [IWL_RATE_1M_INDEX] = "1M",
  2840. [IWL_RATE_2M_INDEX] = "2M",
  2841. [IWL_RATE_5M_INDEX] = "5.5M",
  2842. [IWL_RATE_11M_INDEX] = "11M",
  2843. [IWL_RATE_6M_INDEX] = "6M|MCS0",
  2844. [IWL_RATE_9M_INDEX] = "9M",
  2845. [IWL_RATE_12M_INDEX] = "12M|MCS1",
  2846. [IWL_RATE_18M_INDEX] = "18M|MCS2",
  2847. [IWL_RATE_24M_INDEX] = "24M|MCS3",
  2848. [IWL_RATE_36M_INDEX] = "36M|MCS4",
  2849. [IWL_RATE_48M_INDEX] = "48M|MCS5",
  2850. [IWL_RATE_54M_INDEX] = "54M|MCS6",
  2851. [IWL_RATE_MCS_7_INDEX] = "MCS7",
  2852. [IWL_RATE_MCS_8_INDEX] = "MCS8",
  2853. [IWL_RATE_MCS_9_INDEX] = "MCS9",
  2854. };
  2855. char *buff, *pos, *endpos;
  2856. int col, rate;
  2857. ssize_t ret;
  2858. struct iwl_lq_sta *lq_sta = file->private_data;
  2859. struct rs_rate_stats *stats;
  2860. static const size_t bufsz = 1024;
  2861. buff = kmalloc(bufsz, GFP_KERNEL);
  2862. if (!buff)
  2863. return -ENOMEM;
  2864. pos = buff;
  2865. endpos = pos + bufsz;
  2866. pos += scnprintf(pos, endpos - pos, "COLUMN,");
  2867. for (rate = 0; rate < IWL_RATE_COUNT; rate++)
  2868. pos += scnprintf(pos, endpos - pos, "%s,", rate_name[rate]);
  2869. pos += scnprintf(pos, endpos - pos, "\n");
  2870. for (col = 0; col < RS_COLUMN_COUNT; col++) {
  2871. pos += scnprintf(pos, endpos - pos,
  2872. "%s,", column_name[col]);
  2873. for (rate = 0; rate < IWL_RATE_COUNT; rate++) {
  2874. stats = &(lq_sta->pers.tx_stats[col][rate]);
  2875. pos += scnprintf(pos, endpos - pos,
  2876. "%llu/%llu,",
  2877. stats->success,
  2878. stats->total);
  2879. }
  2880. pos += scnprintf(pos, endpos - pos, "\n");
  2881. }
  2882. ret = simple_read_from_buffer(user_buf, count, ppos, buff, pos - buff);
  2883. kfree(buff);
  2884. return ret;
  2885. }
  2886. static ssize_t rs_sta_dbgfs_drv_tx_stats_write(struct file *file,
  2887. const char __user *user_buf,
  2888. size_t count, loff_t *ppos)
  2889. {
  2890. struct iwl_lq_sta *lq_sta = file->private_data;
  2891. memset(lq_sta->pers.tx_stats, 0, sizeof(lq_sta->pers.tx_stats));
  2892. return count;
  2893. }
  2894. static const struct file_operations rs_sta_dbgfs_drv_tx_stats_ops = {
  2895. .read = rs_sta_dbgfs_drv_tx_stats_read,
  2896. .write = rs_sta_dbgfs_drv_tx_stats_write,
  2897. .open = simple_open,
  2898. .llseek = default_llseek,
  2899. };
  2900. static void rs_add_debugfs(void *mvm, void *mvm_sta, struct dentry *dir)
  2901. {
  2902. struct iwl_lq_sta *lq_sta = mvm_sta;
  2903. debugfs_create_file("rate_scale_table", S_IRUSR | S_IWUSR, dir,
  2904. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2905. debugfs_create_file("rate_stats_table", S_IRUSR, dir,
  2906. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2907. debugfs_create_file("drv_tx_stats", S_IRUSR | S_IWUSR, dir,
  2908. lq_sta, &rs_sta_dbgfs_drv_tx_stats_ops);
  2909. debugfs_create_u8("tx_agg_tid_enable", S_IRUSR | S_IWUSR, dir,
  2910. &lq_sta->tx_agg_tid_en);
  2911. debugfs_create_u8("reduced_tpc", S_IRUSR | S_IWUSR, dir,
  2912. &lq_sta->pers.dbg_fixed_txp_reduction);
  2913. }
  2914. static void rs_remove_debugfs(void *mvm, void *mvm_sta)
  2915. {
  2916. }
  2917. #endif
  2918. /*
  2919. * Initialization of rate scaling information is done by driver after
  2920. * the station is added. Since mac80211 calls this function before a
  2921. * station is added we ignore it.
  2922. */
  2923. static void rs_rate_init_stub(void *mvm_r,
  2924. struct ieee80211_supported_band *sband,
  2925. struct cfg80211_chan_def *chandef,
  2926. struct ieee80211_sta *sta, void *mvm_sta)
  2927. {
  2928. }
  2929. static const struct rate_control_ops rs_mvm_ops = {
  2930. .name = RS_NAME,
  2931. .tx_status = rs_mac80211_tx_status,
  2932. .get_rate = rs_get_rate,
  2933. .rate_init = rs_rate_init_stub,
  2934. .alloc = rs_alloc,
  2935. .free = rs_free,
  2936. .alloc_sta = rs_alloc_sta,
  2937. .free_sta = rs_free_sta,
  2938. .rate_update = rs_rate_update,
  2939. #ifdef CONFIG_MAC80211_DEBUGFS
  2940. .add_sta_debugfs = rs_add_debugfs,
  2941. .remove_sta_debugfs = rs_remove_debugfs,
  2942. #endif
  2943. };
  2944. int iwl_mvm_rate_control_register(void)
  2945. {
  2946. return ieee80211_rate_control_register(&rs_mvm_ops);
  2947. }
  2948. void iwl_mvm_rate_control_unregister(void)
  2949. {
  2950. ieee80211_rate_control_unregister(&rs_mvm_ops);
  2951. }
  2952. /**
  2953. * iwl_mvm_tx_protection - Gets LQ command, change it to enable/disable
  2954. * Tx protection, according to this rquest and previous requests,
  2955. * and send the LQ command.
  2956. * @mvmsta: The station
  2957. * @enable: Enable Tx protection?
  2958. */
  2959. int iwl_mvm_tx_protection(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
  2960. bool enable)
  2961. {
  2962. struct iwl_lq_cmd *lq = &mvmsta->lq_sta.lq;
  2963. lockdep_assert_held(&mvm->mutex);
  2964. if (enable) {
  2965. if (mvmsta->tx_protection == 0)
  2966. lq->flags |= LQ_FLAG_USE_RTS_MSK;
  2967. mvmsta->tx_protection++;
  2968. } else {
  2969. mvmsta->tx_protection--;
  2970. if (mvmsta->tx_protection == 0)
  2971. lq->flags &= ~LQ_FLAG_USE_RTS_MSK;
  2972. }
  2973. return iwl_mvm_send_lq_cmd(mvm, lq, false);
  2974. }