rs.c 82 KB

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