iwl-4965-rs.c 78 KB

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