iwl-agn-rs.c 73 KB

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