iwl-agn-rs.c 85 KB

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