iwl-agn-rs.c 87 KB

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