rs.c 95 KB

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