iwl-agn-rs.c 98 KB

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