iwl-agn-rs.c 87 KB

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