rs.c 99 KB

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