iwl-agn-rs.c 87 KB

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