iwl-agn-rs.c 88 KB

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