rs.c 113 KB

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