rs.c 85 KB

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