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 <linux/netdevice.h>
  32. #include <linux/etherdevice.h>
  33. #include <linux/delay.h>
  34. #include <linux/workqueue.h>
  35. #include "../net/mac80211/rate.h"
  36. #include "iwl-4965.h"
  37. #include "iwl-core.h"
  38. #include "iwl-helpers.h"
  39. #define RS_NAME "iwl-4965-rs"
  40. #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
  41. #define IWL_NUMBER_TRY 1
  42. #define IWL_HT_NUMBER_TRY 3
  43. #define IWL_RATE_MAX_WINDOW 62 /* # tx in history window */
  44. #define IWL_RATE_MIN_FAILURE_TH 6 /* min failures to calc tpt */
  45. #define IWL_RATE_MIN_SUCCESS_TH 8 /* min successes to calc tpt */
  46. /* max time to accum history 2 seconds */
  47. #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ)
  48. static u8 rs_ht_to_legacy[] = {
  49. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  50. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  51. IWL_RATE_6M_INDEX,
  52. IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
  53. IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
  54. IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
  55. IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
  56. };
  57. struct iwl4965_rate {
  58. u32 rate_n_flags;
  59. } __attribute__ ((packed));
  60. /**
  61. * struct iwl4965_rate_scale_data -- tx success history for one rate
  62. */
  63. struct iwl4965_rate_scale_data {
  64. u64 data; /* bitmap of successful frames */
  65. s32 success_counter; /* number of frames successful */
  66. s32 success_ratio; /* per-cent * 128 */
  67. s32 counter; /* number of frames attempted */
  68. s32 average_tpt; /* success ratio * expected throughput */
  69. unsigned long stamp;
  70. };
  71. /**
  72. * struct iwl4965_scale_tbl_info -- tx params and success history for all rates
  73. *
  74. * There are two of these in struct iwl4965_lq_sta,
  75. * one for "active", and one for "search".
  76. */
  77. struct iwl4965_scale_tbl_info {
  78. enum iwl4965_table_type lq_type;
  79. enum iwl4965_antenna_type antenna_type;
  80. u8 is_SGI; /* 1 = short guard interval */
  81. u8 is_fat; /* 1 = 40 MHz channel width */
  82. u8 is_dup; /* 1 = duplicated data streams */
  83. u8 action; /* change modulation; IWL_[LEGACY/SISO/MIMO]_SWITCH_* */
  84. s32 *expected_tpt; /* throughput metrics; expected_tpt_G, etc. */
  85. struct iwl4965_rate current_rate; /* rate_n_flags, uCode API format */
  86. struct iwl4965_rate_scale_data win[IWL_RATE_COUNT]; /* rate histories */
  87. };
  88. #ifdef CONFIG_IWL4965_HT
  89. struct iwl4965_traffic_load {
  90. unsigned long time_stamp; /* age of the oldest statistics */
  91. u32 packet_count[TID_QUEUE_MAX_SIZE]; /* packet count in this time
  92. * slice */
  93. u32 total; /* total num of packets during the
  94. * last TID_MAX_TIME_DIFF */
  95. u8 queue_count; /* number of queues that has
  96. * been used since the last cleanup */
  97. u8 head; /* start of the circular buffer */
  98. };
  99. #endif /* CONFIG_IWL4965_HT */
  100. /**
  101. * struct iwl4965_lq_sta -- driver's rate scaling private structure
  102. *
  103. * Pointer to this gets passed back and forth between driver and mac80211.
  104. */
  105. struct iwl4965_lq_sta {
  106. u8 active_tbl; /* index of active table, range 0-1 */
  107. u8 enable_counter; /* indicates HT mode */
  108. u8 stay_in_tbl; /* 1: disallow, 0: allow search for new mode */
  109. u8 search_better_tbl; /* 1: currently trying alternate mode */
  110. s32 last_tpt;
  111. /* The following determine when to search for a new mode */
  112. u32 table_count_limit;
  113. u32 max_failure_limit; /* # failed frames before new search */
  114. u32 max_success_limit; /* # successful frames before new search */
  115. u32 table_count;
  116. u32 total_failed; /* total failed frames, any/all rates */
  117. u32 total_success; /* total successful frames, any/all rates */
  118. u32 flush_timer; /* time staying in mode before new search */
  119. u8 action_counter; /* # mode-switch actions tried */
  120. u8 antenna;
  121. u8 valid_antenna;
  122. u8 is_green;
  123. u8 is_dup;
  124. enum ieee80211_band band;
  125. u8 ibss_sta_added;
  126. /* The following are bitmaps of rates; IWL_RATE_6M_MASK, etc. */
  127. u32 supp_rates;
  128. u16 active_rate;
  129. u16 active_siso_rate;
  130. u16 active_mimo_rate;
  131. u16 active_rate_basic;
  132. struct iwl_link_quality_cmd lq;
  133. struct iwl4965_scale_tbl_info lq_info[LQ_SIZE]; /* "active", "search" */
  134. #ifdef CONFIG_IWL4965_HT
  135. struct iwl4965_traffic_load load[TID_MAX_LOAD_COUNT];
  136. u8 tx_agg_tid_en;
  137. #endif
  138. #ifdef CONFIG_MAC80211_DEBUGFS
  139. struct dentry *rs_sta_dbgfs_scale_table_file;
  140. struct dentry *rs_sta_dbgfs_stats_table_file;
  141. #ifdef CONFIG_IWL4965_HT
  142. struct dentry *rs_sta_dbgfs_tx_agg_tid_en_file;
  143. #endif
  144. struct iwl4965_rate dbg_fixed;
  145. struct iwl_priv *drv;
  146. #endif
  147. };
  148. static void rs_rate_scale_perform(struct iwl_priv *priv,
  149. struct net_device *dev,
  150. struct ieee80211_hdr *hdr,
  151. struct sta_info *sta);
  152. static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
  153. struct iwl4965_rate *tx_mcs,
  154. struct iwl_link_quality_cmd *tbl);
  155. #ifdef CONFIG_MAC80211_DEBUGFS
  156. static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
  157. struct iwl4965_rate *mcs, int index);
  158. #else
  159. static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
  160. struct iwl4965_rate *mcs, int index)
  161. {}
  162. #endif
  163. /*
  164. * Expected throughput metrics for following rates:
  165. * 1, 2, 5.5, 11, 6, 9, 12, 18, 24, 36, 48, 54, 60 MBits
  166. * "G" is the only table that supports CCK (the first 4 rates).
  167. */
  168. static s32 expected_tpt_A[IWL_RATE_COUNT] = {
  169. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
  170. };
  171. static s32 expected_tpt_G[IWL_RATE_COUNT] = {
  172. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
  173. };
  174. static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
  175. 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
  176. };
  177. static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
  178. 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
  179. };
  180. static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
  181. 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
  182. };
  183. static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
  184. 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
  185. };
  186. static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
  187. 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
  188. };
  189. static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
  190. 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
  191. };
  192. static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
  193. 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
  194. };
  195. static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
  196. 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
  197. };
  198. static inline u8 iwl4965_rate_get_rate(u32 rate_n_flags)
  199. {
  200. return (u8)(rate_n_flags & 0xFF);
  201. }
  202. static void rs_rate_scale_clear_window(struct iwl4965_rate_scale_data *window)
  203. {
  204. window->data = 0;
  205. window->success_counter = 0;
  206. window->success_ratio = IWL_INVALID_VALUE;
  207. window->counter = 0;
  208. window->average_tpt = IWL_INVALID_VALUE;
  209. window->stamp = 0;
  210. }
  211. #ifdef CONFIG_IWL4965_HT
  212. /*
  213. * removes the old data from the statistics. All data that is older than
  214. * TID_MAX_TIME_DIFF, will be deleted.
  215. */
  216. static void rs_tl_rm_old_stats(struct iwl4965_traffic_load *tl, u32 curr_time)
  217. {
  218. /* The oldest age we want to keep */
  219. u32 oldest_time = curr_time - TID_MAX_TIME_DIFF;
  220. while (tl->queue_count &&
  221. (tl->time_stamp < oldest_time)) {
  222. tl->total -= tl->packet_count[tl->head];
  223. tl->packet_count[tl->head] = 0;
  224. tl->time_stamp += TID_QUEUE_CELL_SPACING;
  225. tl->queue_count--;
  226. tl->head++;
  227. if (tl->head >= TID_QUEUE_MAX_SIZE)
  228. tl->head = 0;
  229. }
  230. }
  231. /*
  232. * increment traffic load value for tid and also remove
  233. * any old values if passed the certain time period
  234. */
  235. static void rs_tl_add_packet(struct iwl4965_lq_sta *lq_data, u8 tid)
  236. {
  237. u32 curr_time = jiffies_to_msecs(jiffies);
  238. u32 time_diff;
  239. s32 index;
  240. struct iwl4965_traffic_load *tl = NULL;
  241. if (tid >= TID_MAX_LOAD_COUNT)
  242. return;
  243. tl = &lq_data->load[tid];
  244. curr_time -= curr_time % TID_ROUND_VALUE;
  245. /* Happens only for the first packet. Initialize the data */
  246. if (!(tl->queue_count)) {
  247. tl->total = 1;
  248. tl->time_stamp = curr_time;
  249. tl->queue_count = 1;
  250. tl->head = 0;
  251. tl->packet_count[0] = 1;
  252. return;
  253. }
  254. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  255. index = time_diff / TID_QUEUE_CELL_SPACING;
  256. /* The history is too long: remove data that is older than */
  257. /* TID_MAX_TIME_DIFF */
  258. if (index >= TID_QUEUE_MAX_SIZE)
  259. rs_tl_rm_old_stats(tl, curr_time);
  260. index = (tl->head + index) % TID_QUEUE_MAX_SIZE;
  261. tl->packet_count[index] = tl->packet_count[index] + 1;
  262. tl->total = tl->total + 1;
  263. if ((index + 1) > tl->queue_count)
  264. tl->queue_count = index + 1;
  265. }
  266. /*
  267. get the traffic load value for tid
  268. */
  269. static u32 rs_tl_get_load(struct iwl4965_lq_sta *lq_data, u8 tid)
  270. {
  271. u32 curr_time = jiffies_to_msecs(jiffies);
  272. u32 time_diff;
  273. s32 index;
  274. struct iwl4965_traffic_load *tl = NULL;
  275. if (tid >= TID_MAX_LOAD_COUNT)
  276. return 0;
  277. tl = &(lq_data->load[tid]);
  278. curr_time -= curr_time % TID_ROUND_VALUE;
  279. if (!(tl->queue_count))
  280. return 0;
  281. time_diff = TIME_WRAP_AROUND(tl->time_stamp, curr_time);
  282. index = time_diff / TID_QUEUE_CELL_SPACING;
  283. /* The history is too long: remove data that is older than */
  284. /* TID_MAX_TIME_DIFF */
  285. if (index >= TID_QUEUE_MAX_SIZE)
  286. rs_tl_rm_old_stats(tl, curr_time);
  287. return tl->total;
  288. }
  289. static void rs_tl_turn_on_agg_for_tid(struct iwl_priv *priv,
  290. struct iwl4965_lq_sta *lq_data, u8 tid,
  291. struct sta_info *sta)
  292. {
  293. unsigned long state;
  294. DECLARE_MAC_BUF(mac);
  295. spin_lock_bh(&sta->ampdu_mlme.ampdu_tx);
  296. state = sta->ampdu_mlme.tid_state_tx[tid];
  297. spin_unlock_bh(&sta->ampdu_mlme.ampdu_tx);
  298. if (state == HT_AGG_STATE_IDLE &&
  299. rs_tl_get_load(lq_data, tid) > IWL_AGG_LOAD_THRESHOLD) {
  300. IWL_DEBUG_HT("Starting Tx agg: STA: %s tid: %d\n",
  301. print_mac(mac, sta->addr), tid);
  302. ieee80211_start_tx_ba_session(priv->hw, sta->addr, tid);
  303. }
  304. }
  305. static void rs_tl_turn_on_agg(struct iwl_priv *priv, u8 tid,
  306. struct iwl4965_lq_sta *lq_data,
  307. struct sta_info *sta)
  308. {
  309. if ((tid < TID_MAX_LOAD_COUNT))
  310. rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
  311. else if (tid == IWL_AGG_ALL_TID)
  312. for (tid = 0; tid < TID_MAX_LOAD_COUNT; tid++)
  313. rs_tl_turn_on_agg_for_tid(priv, lq_data, tid, sta);
  314. }
  315. #endif /* CONFIG_IWLWIFI_HT */
  316. /**
  317. * rs_collect_tx_data - Update the success/failure sliding window
  318. *
  319. * We keep a sliding window of the last 62 packets transmitted
  320. * at this rate. window->data contains the bitmask of successful
  321. * packets.
  322. */
  323. static int rs_collect_tx_data(struct iwl4965_rate_scale_data *windows,
  324. int scale_index, s32 tpt, int retries,
  325. int successes)
  326. {
  327. struct iwl4965_rate_scale_data *window = NULL;
  328. u64 mask;
  329. u8 win_size = IWL_RATE_MAX_WINDOW;
  330. s32 fail_count;
  331. if (scale_index < 0 || scale_index >= IWL_RATE_COUNT)
  332. return -EINVAL;
  333. /* Select data for current tx bit rate */
  334. window = &(windows[scale_index]);
  335. /*
  336. * Keep track of only the latest 62 tx frame attempts in this rate's
  337. * history window; anything older isn't really relevant any more.
  338. * If we have filled up the sliding window, drop the oldest attempt;
  339. * if the oldest attempt (highest bit in bitmap) shows "success",
  340. * subtract "1" from the success counter (this is the main reason
  341. * we keep these bitmaps!).
  342. */
  343. while (retries > 0) {
  344. if (window->counter >= win_size) {
  345. window->counter = win_size - 1;
  346. mask = 1;
  347. mask = (mask << (win_size - 1));
  348. if (window->data & mask) {
  349. window->data &= ~mask;
  350. window->success_counter =
  351. window->success_counter - 1;
  352. }
  353. }
  354. /* Increment frames-attempted counter */
  355. window->counter++;
  356. /* Shift bitmap by one frame (throw away oldest history),
  357. * OR in "1", and increment "success" if this
  358. * frame was successful. */
  359. mask = window->data;
  360. window->data = (mask << 1);
  361. if (successes > 0) {
  362. window->success_counter = window->success_counter + 1;
  363. window->data |= 0x1;
  364. successes--;
  365. }
  366. retries--;
  367. }
  368. /* Calculate current success ratio, avoid divide-by-0! */
  369. if (window->counter > 0)
  370. window->success_ratio = 128 * (100 * window->success_counter)
  371. / window->counter;
  372. else
  373. window->success_ratio = IWL_INVALID_VALUE;
  374. fail_count = window->counter - window->success_counter;
  375. /* Calculate average throughput, if we have enough history. */
  376. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  377. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  378. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  379. else
  380. window->average_tpt = IWL_INVALID_VALUE;
  381. /* Tag this window as having been updated */
  382. window->stamp = jiffies;
  383. return 0;
  384. }
  385. /*
  386. * Fill uCode API rate_n_flags field, based on "search" or "active" table.
  387. */
  388. static void rs_mcs_from_tbl(struct iwl4965_rate *mcs_rate,
  389. struct iwl4965_scale_tbl_info *tbl,
  390. int index, u8 use_green)
  391. {
  392. if (is_legacy(tbl->lq_type)) {
  393. mcs_rate->rate_n_flags = iwl4965_rates[index].plcp;
  394. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  395. mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
  396. } else if (is_siso(tbl->lq_type)) {
  397. if (index > IWL_LAST_OFDM_RATE)
  398. index = IWL_LAST_OFDM_RATE;
  399. mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_siso |
  400. RATE_MCS_HT_MSK;
  401. } else {
  402. if (index > IWL_LAST_OFDM_RATE)
  403. index = IWL_LAST_OFDM_RATE;
  404. mcs_rate->rate_n_flags = iwl4965_rates[index].plcp_mimo |
  405. RATE_MCS_HT_MSK;
  406. }
  407. switch (tbl->antenna_type) {
  408. case ANT_BOTH:
  409. mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
  410. break;
  411. case ANT_MAIN:
  412. mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
  413. break;
  414. case ANT_AUX:
  415. mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
  416. break;
  417. case ANT_NONE:
  418. break;
  419. }
  420. if (is_legacy(tbl->lq_type))
  421. return;
  422. if (tbl->is_fat) {
  423. if (tbl->is_dup)
  424. mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
  425. else
  426. mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
  427. }
  428. if (tbl->is_SGI)
  429. mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
  430. if (use_green) {
  431. mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
  432. if (is_siso(tbl->lq_type))
  433. mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
  434. }
  435. }
  436. /*
  437. * Interpret uCode API's rate_n_flags format,
  438. * fill "search" or "active" tx mode table.
  439. */
  440. static int rs_get_tbl_info_from_mcs(const struct iwl4965_rate *mcs_rate,
  441. enum ieee80211_band band,
  442. struct iwl4965_scale_tbl_info *tbl,
  443. int *rate_idx)
  444. {
  445. int index;
  446. u32 ant_msk;
  447. index = iwl4965_hwrate_to_plcp_idx(mcs_rate->rate_n_flags);
  448. if (index == IWL_RATE_INVALID) {
  449. *rate_idx = -1;
  450. return -EINVAL;
  451. }
  452. tbl->is_SGI = 0; /* default legacy setup */
  453. tbl->is_fat = 0;
  454. tbl->is_dup = 0;
  455. tbl->antenna_type = ANT_BOTH; /* default MIMO setup */
  456. /* legacy rate format */
  457. if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
  458. ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
  459. if (ant_msk == RATE_MCS_ANT_AB_MSK)
  460. tbl->lq_type = LQ_NONE;
  461. else {
  462. if (band == IEEE80211_BAND_5GHZ)
  463. tbl->lq_type = LQ_A;
  464. else
  465. tbl->lq_type = LQ_G;
  466. if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
  467. tbl->antenna_type = ANT_MAIN;
  468. else
  469. tbl->antenna_type = ANT_AUX;
  470. }
  471. *rate_idx = index;
  472. /* HT rate format, SISO (might be 20 MHz legacy or 40 MHz fat width) */
  473. } else if (iwl4965_rate_get_rate(mcs_rate->rate_n_flags)
  474. <= IWL_RATE_SISO_60M_PLCP) {
  475. tbl->lq_type = LQ_SISO;
  476. ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
  477. if (ant_msk == RATE_MCS_ANT_AB_MSK)
  478. tbl->lq_type = LQ_NONE;
  479. else {
  480. if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
  481. tbl->antenna_type = ANT_MAIN;
  482. else
  483. tbl->antenna_type = ANT_AUX;
  484. }
  485. if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
  486. tbl->is_SGI = 1;
  487. if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
  488. (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
  489. tbl->is_fat = 1;
  490. if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
  491. tbl->is_dup = 1;
  492. *rate_idx = index;
  493. /* HT rate format, MIMO (might be 20 MHz legacy or 40 MHz fat width) */
  494. } else {
  495. tbl->lq_type = LQ_MIMO;
  496. if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
  497. tbl->is_SGI = 1;
  498. if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
  499. (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
  500. tbl->is_fat = 1;
  501. if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
  502. tbl->is_dup = 1;
  503. *rate_idx = index;
  504. }
  505. return 0;
  506. }
  507. static inline void rs_toggle_antenna(struct iwl4965_rate *new_rate,
  508. struct iwl4965_scale_tbl_info *tbl)
  509. {
  510. if (tbl->antenna_type == ANT_AUX) {
  511. tbl->antenna_type = ANT_MAIN;
  512. new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
  513. new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
  514. } else {
  515. tbl->antenna_type = ANT_AUX;
  516. new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
  517. new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
  518. }
  519. }
  520. static inline u8 rs_use_green(struct iwl_priv *priv,
  521. struct ieee80211_conf *conf)
  522. {
  523. #ifdef CONFIG_IWL4965_HT
  524. return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
  525. priv->current_ht_config.is_green_field &&
  526. !priv->current_ht_config.non_GF_STA_present);
  527. #endif /* CONFIG_IWL4965_HT */
  528. return 0;
  529. }
  530. /**
  531. * rs_get_supported_rates - get the available rates
  532. *
  533. * if management frame or broadcast frame only return
  534. * basic available rates.
  535. *
  536. */
  537. static void rs_get_supported_rates(struct iwl4965_lq_sta *lq_sta,
  538. struct ieee80211_hdr *hdr,
  539. enum iwl4965_table_type rate_type,
  540. u16 *data_rate)
  541. {
  542. if (is_legacy(rate_type))
  543. *data_rate = lq_sta->active_rate;
  544. else {
  545. if (is_siso(rate_type))
  546. *data_rate = lq_sta->active_siso_rate;
  547. else
  548. *data_rate = lq_sta->active_mimo_rate;
  549. }
  550. if (hdr && is_multicast_ether_addr(hdr->addr1) &&
  551. lq_sta->active_rate_basic) {
  552. *data_rate = lq_sta->active_rate_basic;
  553. }
  554. }
  555. static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
  556. {
  557. u8 high = IWL_RATE_INVALID;
  558. u8 low = IWL_RATE_INVALID;
  559. /* 802.11A or ht walks to the next literal adjacent rate in
  560. * the rate table */
  561. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  562. int i;
  563. u32 mask;
  564. /* Find the previous rate that is in the rate mask */
  565. i = index - 1;
  566. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  567. if (rate_mask & mask) {
  568. low = i;
  569. break;
  570. }
  571. }
  572. /* Find the next rate that is in the rate mask */
  573. i = index + 1;
  574. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  575. if (rate_mask & mask) {
  576. high = i;
  577. break;
  578. }
  579. }
  580. return (high << 8) | low;
  581. }
  582. low = index;
  583. while (low != IWL_RATE_INVALID) {
  584. low = iwl4965_rates[low].prev_rs;
  585. if (low == IWL_RATE_INVALID)
  586. break;
  587. if (rate_mask & (1 << low))
  588. break;
  589. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  590. }
  591. high = index;
  592. while (high != IWL_RATE_INVALID) {
  593. high = iwl4965_rates[high].next_rs;
  594. if (high == IWL_RATE_INVALID)
  595. break;
  596. if (rate_mask & (1 << high))
  597. break;
  598. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  599. }
  600. return (high << 8) | low;
  601. }
  602. static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
  603. struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
  604. u8 ht_possible, struct iwl4965_rate *mcs_rate)
  605. {
  606. s32 low;
  607. u16 rate_mask;
  608. u16 high_low;
  609. u8 switch_to_legacy = 0;
  610. u8 is_green = lq_sta->is_green;
  611. /* check if we need to switch from HT to legacy rates.
  612. * assumption is that mandatory rates (1Mbps or 6Mbps)
  613. * are always supported (spec demand) */
  614. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
  615. switch_to_legacy = 1;
  616. scale_index = rs_ht_to_legacy[scale_index];
  617. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  618. tbl->lq_type = LQ_A;
  619. else
  620. tbl->lq_type = LQ_G;
  621. if ((tbl->antenna_type == ANT_BOTH) ||
  622. (tbl->antenna_type == ANT_NONE))
  623. tbl->antenna_type = ANT_MAIN;
  624. tbl->is_fat = 0;
  625. tbl->is_SGI = 0;
  626. }
  627. rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
  628. /* Mask with station rate restriction */
  629. if (is_legacy(tbl->lq_type)) {
  630. /* supp_rates has no CCK bits in A mode */
  631. if (lq_sta->band == IEEE80211_BAND_5GHZ)
  632. rate_mask = (u16)(rate_mask &
  633. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  634. else
  635. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  636. }
  637. /* If we switched from HT to legacy, check current rate */
  638. if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
  639. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  640. return;
  641. }
  642. high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
  643. low = high_low & 0xff;
  644. if (low != IWL_RATE_INVALID)
  645. rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
  646. else
  647. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  648. }
  649. /*
  650. * mac80211 sends us Tx status
  651. */
  652. static void rs_tx_status(void *priv_rate, struct net_device *dev,
  653. struct sk_buff *skb,
  654. struct ieee80211_tx_status *tx_resp)
  655. {
  656. int status;
  657. u8 retries;
  658. int rs_index, index = 0;
  659. struct iwl4965_lq_sta *lq_sta;
  660. struct iwl_link_quality_cmd *table;
  661. struct sta_info *sta;
  662. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  663. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  664. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  665. struct ieee80211_hw *hw = local_to_hw(local);
  666. struct iwl4965_rate_scale_data *window = NULL;
  667. struct iwl4965_rate_scale_data *search_win = NULL;
  668. struct iwl4965_rate tx_mcs;
  669. struct iwl4965_scale_tbl_info tbl_type;
  670. struct iwl4965_scale_tbl_info *curr_tbl, *search_tbl;
  671. u8 active_index = 0;
  672. u16 fc = le16_to_cpu(hdr->frame_control);
  673. s32 tpt = 0;
  674. IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
  675. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  676. return;
  677. /* This packet was aggregated but doesn't carry rate scale info */
  678. if ((tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) &&
  679. !(tx_resp->flags & IEEE80211_TX_STATUS_AMPDU))
  680. return;
  681. retries = tx_resp->retry_count;
  682. if (retries > 15)
  683. retries = 15;
  684. rcu_read_lock();
  685. sta = sta_info_get(local, hdr->addr1);
  686. if (!sta || !sta->rate_ctrl_priv)
  687. goto out;
  688. lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
  689. if (!priv->lq_mngr.lq_ready)
  690. goto out;
  691. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  692. !lq_sta->ibss_sta_added)
  693. goto out;
  694. table = &lq_sta->lq;
  695. active_index = lq_sta->active_tbl;
  696. /* Get mac80211 antenna info */
  697. lq_sta->antenna =
  698. (lq_sta->valid_antenna & local->hw.conf.antenna_sel_tx);
  699. if (!lq_sta->antenna)
  700. lq_sta->antenna = lq_sta->valid_antenna;
  701. /* Ignore mac80211 antenna info for now */
  702. lq_sta->antenna = lq_sta->valid_antenna;
  703. curr_tbl = &(lq_sta->lq_info[active_index]);
  704. search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
  705. window = (struct iwl4965_rate_scale_data *)
  706. &(curr_tbl->win[0]);
  707. search_win = (struct iwl4965_rate_scale_data *)
  708. &(search_tbl->win[0]);
  709. /*
  710. * Ignore this Tx frame response if its initial rate doesn't match
  711. * that of latest Link Quality command. There may be stragglers
  712. * from a previous Link Quality command, but we're no longer interested
  713. * in those; they're either from the "active" mode while we're trying
  714. * to check "search" mode, or a prior "search" mode after we've moved
  715. * to a new "search" mode (which might become the new "active" mode).
  716. */
  717. tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[0].rate_n_flags);
  718. rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
  719. if (priv->band == IEEE80211_BAND_5GHZ)
  720. rs_index -= IWL_FIRST_OFDM_RATE;
  721. if ((tx_resp->control.tx_rate == NULL) ||
  722. (tbl_type.is_SGI ^
  723. !!(tx_resp->control.flags & IEEE80211_TXCTL_SHORT_GI)) ||
  724. (tbl_type.is_fat ^
  725. !!(tx_resp->control.flags & IEEE80211_TXCTL_40_MHZ_WIDTH)) ||
  726. (tbl_type.is_dup ^
  727. !!(tx_resp->control.flags & IEEE80211_TXCTL_DUP_DATA)) ||
  728. (tbl_type.antenna_type ^
  729. tx_resp->control.antenna_sel_tx) ||
  730. (!!(tx_mcs.rate_n_flags & RATE_MCS_HT_MSK) ^
  731. !!(tx_resp->control.flags & IEEE80211_TXCTL_OFDM_HT)) ||
  732. (!!(tx_mcs.rate_n_flags & RATE_MCS_GF_MSK) ^
  733. !!(tx_resp->control.flags & IEEE80211_TXCTL_GREEN_FIELD)) ||
  734. (hw->wiphy->bands[priv->band]->bitrates[rs_index].bitrate !=
  735. tx_resp->control.tx_rate->bitrate)) {
  736. IWL_DEBUG_RATE("initial rate does not match 0x%x\n",
  737. tx_mcs.rate_n_flags);
  738. goto out;
  739. }
  740. /* Update frame history window with "failure" for each Tx retry. */
  741. while (retries) {
  742. /* Look up the rate and other info used for each tx attempt.
  743. * Each tx attempt steps one entry deeper in the rate table. */
  744. tx_mcs.rate_n_flags =
  745. le32_to_cpu(table->rs_table[index].rate_n_flags);
  746. rs_get_tbl_info_from_mcs(&tx_mcs, priv->band,
  747. &tbl_type, &rs_index);
  748. /* If type matches "search" table,
  749. * add failure to "search" history */
  750. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  751. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  752. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  753. if (search_tbl->expected_tpt)
  754. tpt = search_tbl->expected_tpt[rs_index];
  755. else
  756. tpt = 0;
  757. rs_collect_tx_data(search_win, rs_index, tpt, 1, 0);
  758. /* Else if type matches "current/active" table,
  759. * add failure to "current/active" history */
  760. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  761. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  762. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  763. if (curr_tbl->expected_tpt)
  764. tpt = curr_tbl->expected_tpt[rs_index];
  765. else
  766. tpt = 0;
  767. rs_collect_tx_data(window, rs_index, tpt, 1, 0);
  768. }
  769. /* If not searching for a new mode, increment failed counter
  770. * ... this helps determine when to start searching again */
  771. if (lq_sta->stay_in_tbl)
  772. lq_sta->total_failed++;
  773. --retries;
  774. index++;
  775. }
  776. /*
  777. * Find (by rate) the history window to update with final Tx attempt;
  778. * if Tx was successful first try, use original rate,
  779. * else look up the rate that was, finally, successful.
  780. */
  781. tx_mcs.rate_n_flags = le32_to_cpu(table->rs_table[index].rate_n_flags);
  782. rs_get_tbl_info_from_mcs(&tx_mcs, priv->band, &tbl_type, &rs_index);
  783. /* Update frame history window with "success" if Tx got ACKed ... */
  784. if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
  785. status = 1;
  786. else
  787. status = 0;
  788. /* If type matches "search" table,
  789. * add final tx status to "search" history */
  790. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  791. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  792. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  793. if (search_tbl->expected_tpt)
  794. tpt = search_tbl->expected_tpt[rs_index];
  795. else
  796. tpt = 0;
  797. if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
  798. rs_collect_tx_data(search_win, rs_index, tpt,
  799. tx_resp->ampdu_ack_len,
  800. tx_resp->ampdu_ack_map);
  801. else
  802. rs_collect_tx_data(search_win, rs_index, tpt,
  803. 1, status);
  804. /* Else if type matches "current/active" table,
  805. * add final tx status to "current/active" history */
  806. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  807. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  808. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  809. if (curr_tbl->expected_tpt)
  810. tpt = curr_tbl->expected_tpt[rs_index];
  811. else
  812. tpt = 0;
  813. if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU)
  814. rs_collect_tx_data(window, rs_index, tpt,
  815. tx_resp->ampdu_ack_len,
  816. tx_resp->ampdu_ack_map);
  817. else
  818. rs_collect_tx_data(window, rs_index, tpt,
  819. 1, status);
  820. }
  821. /* If not searching for new mode, increment success/failed counter
  822. * ... these help determine when to start searching again */
  823. if (lq_sta->stay_in_tbl) {
  824. if (tx_resp->control.flags & IEEE80211_TXCTL_AMPDU) {
  825. lq_sta->total_success += tx_resp->ampdu_ack_map;
  826. lq_sta->total_failed +=
  827. (tx_resp->ampdu_ack_len - tx_resp->ampdu_ack_map);
  828. } else {
  829. if (status)
  830. lq_sta->total_success++;
  831. else
  832. lq_sta->total_failed++;
  833. }
  834. }
  835. /* See if there's a better rate or modulation mode to try. */
  836. rs_rate_scale_perform(priv, dev, hdr, sta);
  837. out:
  838. rcu_read_unlock();
  839. return;
  840. }
  841. static u8 rs_is_ant_connected(u8 valid_antenna,
  842. enum iwl4965_antenna_type antenna_type)
  843. {
  844. if (antenna_type == ANT_AUX)
  845. return ((valid_antenna & 0x2) ? 1:0);
  846. else if (antenna_type == ANT_MAIN)
  847. return ((valid_antenna & 0x1) ? 1:0);
  848. else if (antenna_type == ANT_BOTH)
  849. return ((valid_antenna & 0x3) == 0x3);
  850. return 1;
  851. }
  852. static u8 rs_is_other_ant_connected(u8 valid_antenna,
  853. enum iwl4965_antenna_type antenna_type)
  854. {
  855. if (antenna_type == ANT_AUX)
  856. return rs_is_ant_connected(valid_antenna, ANT_MAIN);
  857. else
  858. return rs_is_ant_connected(valid_antenna, ANT_AUX);
  859. return 0;
  860. }
  861. /*
  862. * Begin a period of staying with a selected modulation mode.
  863. * Set "stay_in_tbl" flag to prevent any mode switches.
  864. * Set frame tx success limits according to legacy vs. high-throughput,
  865. * and reset overall (spanning all rates) tx success history statistics.
  866. * These control how long we stay using same modulation mode before
  867. * searching for a new mode.
  868. */
  869. static void rs_set_stay_in_table(u8 is_legacy,
  870. struct iwl4965_lq_sta *lq_sta)
  871. {
  872. IWL_DEBUG_HT("we are staying in the same table\n");
  873. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  874. if (is_legacy) {
  875. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  876. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  877. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  878. } else {
  879. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  880. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  881. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  882. }
  883. lq_sta->table_count = 0;
  884. lq_sta->total_failed = 0;
  885. lq_sta->total_success = 0;
  886. }
  887. /*
  888. * Find correct throughput table for given mode of modulation
  889. */
  890. static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
  891. struct iwl4965_scale_tbl_info *tbl)
  892. {
  893. if (is_legacy(tbl->lq_type)) {
  894. if (!is_a_band(tbl->lq_type))
  895. tbl->expected_tpt = expected_tpt_G;
  896. else
  897. tbl->expected_tpt = expected_tpt_A;
  898. } else if (is_siso(tbl->lq_type)) {
  899. if (tbl->is_fat && !lq_sta->is_dup)
  900. if (tbl->is_SGI)
  901. tbl->expected_tpt = expected_tpt_siso40MHzSGI;
  902. else
  903. tbl->expected_tpt = expected_tpt_siso40MHz;
  904. else if (tbl->is_SGI)
  905. tbl->expected_tpt = expected_tpt_siso20MHzSGI;
  906. else
  907. tbl->expected_tpt = expected_tpt_siso20MHz;
  908. } else if (is_mimo(tbl->lq_type)) {
  909. if (tbl->is_fat && !lq_sta->is_dup)
  910. if (tbl->is_SGI)
  911. tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
  912. else
  913. tbl->expected_tpt = expected_tpt_mimo40MHz;
  914. else if (tbl->is_SGI)
  915. tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
  916. else
  917. tbl->expected_tpt = expected_tpt_mimo20MHz;
  918. } else
  919. tbl->expected_tpt = expected_tpt_G;
  920. }
  921. #ifdef CONFIG_IWL4965_HT
  922. /*
  923. * Find starting rate for new "search" high-throughput mode of modulation.
  924. * Goal is to find lowest expected rate (under perfect conditions) that is
  925. * above the current measured throughput of "active" mode, to give new mode
  926. * a fair chance to prove itself without too many challenges.
  927. *
  928. * This gets called when transitioning to more aggressive modulation
  929. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  930. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  931. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  932. * bit rate will typically need to increase, but not if performance was bad.
  933. */
  934. static s32 rs_get_best_rate(struct iwl_priv *priv,
  935. struct iwl4965_lq_sta *lq_sta,
  936. struct iwl4965_scale_tbl_info *tbl, /* "search" */
  937. u16 rate_mask, s8 index, s8 rate)
  938. {
  939. /* "active" values */
  940. struct iwl4965_scale_tbl_info *active_tbl =
  941. &(lq_sta->lq_info[lq_sta->active_tbl]);
  942. s32 active_sr = active_tbl->win[index].success_ratio;
  943. s32 active_tpt = active_tbl->expected_tpt[index];
  944. /* expected "search" throughput */
  945. s32 *tpt_tbl = tbl->expected_tpt;
  946. s32 new_rate, high, low, start_hi;
  947. u16 high_low;
  948. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  949. for (; ;) {
  950. high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
  951. low = high_low & 0xff;
  952. high = (high_low >> 8) & 0xff;
  953. /*
  954. * Lower the "search" bit rate, to give new "search" mode
  955. * approximately the same throughput as "active" if:
  956. *
  957. * 1) "Active" mode has been working modestly well (but not
  958. * great), and expected "search" throughput (under perfect
  959. * conditions) at candidate rate is above the actual
  960. * measured "active" throughput (but less than expected
  961. * "active" throughput under perfect conditions).
  962. * OR
  963. * 2) "Active" mode has been working perfectly or very well
  964. * and expected "search" throughput (under perfect
  965. * conditions) at candidate rate is above expected
  966. * "active" throughput (under perfect conditions).
  967. */
  968. if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
  969. ((active_sr > IWL_RATE_DECREASE_TH) &&
  970. (active_sr <= IWL_RATE_HIGH_TH) &&
  971. (tpt_tbl[rate] <= active_tpt))) ||
  972. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  973. (tpt_tbl[rate] > active_tpt))) {
  974. /* (2nd or later pass)
  975. * If we've already tried to raise the rate, and are
  976. * now trying to lower it, use the higher rate. */
  977. if (start_hi != IWL_RATE_INVALID) {
  978. new_rate = start_hi;
  979. break;
  980. }
  981. new_rate = rate;
  982. /* Loop again with lower rate */
  983. if (low != IWL_RATE_INVALID)
  984. rate = low;
  985. /* Lower rate not available, use the original */
  986. else
  987. break;
  988. /* Else try to raise the "search" rate to match "active" */
  989. } else {
  990. /* (2nd or later pass)
  991. * If we've already tried to lower the rate, and are
  992. * now trying to raise it, use the lower rate. */
  993. if (new_rate != IWL_RATE_INVALID)
  994. break;
  995. /* Loop again with higher rate */
  996. else if (high != IWL_RATE_INVALID) {
  997. start_hi = high;
  998. rate = high;
  999. /* Higher rate not available, use the original */
  1000. } else {
  1001. new_rate = rate;
  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) {
  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 = iwl_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 = iwl_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. }