iwl-agn-rs.c 99 KB

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