rs.c 93 KB

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