iwl-agn-rs.c 95 KB

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