iwl-agn-rs.c 99 KB

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