iwl-4965-rs.c 80 KB

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