iwl-4965-rs.c 74 KB

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