rs.c 99 KB

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