rs.c 92 KB

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