rs.c 91 KB

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