rs.c 90 KB

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