rs.c 96 KB

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