iwl-4965-rs.c 79 KB

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