iwl-4965-rs.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630
  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_lq_sta {
  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_lq_sta *lq_sta,
  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_lq_sta *lq_sta,
  138. struct iwl4965_rate *mcs, int index);
  139. #else
  140. static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
  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. struct ieee80211_conf *conf)
  429. {
  430. #ifdef CONFIG_IWL4965_HT
  431. return ((conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
  432. priv->current_ht_config.is_green_field &&
  433. !priv->current_ht_config.non_GF_STA_present);
  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_lq_sta *lq_sta,
  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_sta->active_rate;
  451. else {
  452. if (is_siso(rate_type))
  453. *data_rate = lq_sta->active_siso_rate;
  454. else
  455. *data_rate = lq_sta->active_mimo_rate;
  456. }
  457. if (hdr && is_multicast_ether_addr(hdr->addr1) &&
  458. lq_sta->active_rate_basic) {
  459. *data_rate = lq_sta->active_rate_basic;
  460. }
  461. }
  462. static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
  463. {
  464. u8 high = IWL_RATE_INVALID;
  465. u8 low = IWL_RATE_INVALID;
  466. /* 802.11A or ht walks to the next literal adjacent rate in
  467. * the rate table */
  468. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  469. int i;
  470. u32 mask;
  471. /* Find the previous rate that is in the rate mask */
  472. i = index - 1;
  473. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  474. if (rate_mask & mask) {
  475. low = i;
  476. break;
  477. }
  478. }
  479. /* Find the next rate that is in the rate mask */
  480. i = index + 1;
  481. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  482. if (rate_mask & mask) {
  483. high = i;
  484. break;
  485. }
  486. }
  487. return (high << 8) | low;
  488. }
  489. low = index;
  490. while (low != IWL_RATE_INVALID) {
  491. low = iwl4965_rates[low].prev_rs;
  492. if (low == IWL_RATE_INVALID)
  493. break;
  494. if (rate_mask & (1 << low))
  495. break;
  496. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  497. }
  498. high = index;
  499. while (high != IWL_RATE_INVALID) {
  500. high = iwl4965_rates[high].next_rs;
  501. if (high == IWL_RATE_INVALID)
  502. break;
  503. if (rate_mask & (1 << high))
  504. break;
  505. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  506. }
  507. return (high << 8) | low;
  508. }
  509. static void rs_get_lower_rate(struct iwl4965_lq_sta *lq_sta,
  510. struct iwl4965_scale_tbl_info *tbl, u8 scale_index,
  511. u8 ht_possible, struct iwl4965_rate *mcs_rate)
  512. {
  513. s32 low;
  514. u16 rate_mask;
  515. u16 high_low;
  516. u8 switch_to_legacy = 0;
  517. u8 is_green = lq_sta->is_green;
  518. /* check if we need to switch from HT to legacy rates.
  519. * assumption is that mandatory rates (1Mbps or 6Mbps)
  520. * are always supported (spec demand) */
  521. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
  522. switch_to_legacy = 1;
  523. scale_index = rs_ht_to_legacy[scale_index];
  524. if (lq_sta->phymode == MODE_IEEE80211A)
  525. tbl->lq_type = LQ_A;
  526. else
  527. tbl->lq_type = LQ_G;
  528. if ((tbl->antenna_type == ANT_BOTH) ||
  529. (tbl->antenna_type == ANT_NONE))
  530. tbl->antenna_type = ANT_MAIN;
  531. tbl->is_fat = 0;
  532. tbl->is_SGI = 0;
  533. }
  534. rs_get_supported_rates(lq_sta, NULL, tbl->lq_type, &rate_mask);
  535. /* Mask with station rate restriction */
  536. if (is_legacy(tbl->lq_type)) {
  537. /* supp_rates has no CCK bits in A mode */
  538. if (lq_sta->phymode == (u8) MODE_IEEE80211A)
  539. rate_mask = (u16)(rate_mask &
  540. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  541. else
  542. rate_mask = (u16)(rate_mask & lq_sta->supp_rates);
  543. }
  544. /* If we switched from HT to legacy, check current rate */
  545. if (switch_to_legacy && (rate_mask & (1 << scale_index))) {
  546. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  547. return;
  548. }
  549. high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
  550. low = high_low & 0xff;
  551. if (low != IWL_RATE_INVALID)
  552. rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
  553. else
  554. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  555. }
  556. /*
  557. * mac80211 sends us Tx status
  558. */
  559. static void rs_tx_status(void *priv_rate, 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_lq_sta *lq_sta;
  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_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
  593. if (!priv->lq_mngr.lq_ready)
  594. return;
  595. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  596. !lq_sta->ibss_sta_added)
  597. return;
  598. table = &lq_sta->lq;
  599. active_index = lq_sta->active_tbl;
  600. /* Get mac80211 antenna info */
  601. lq_sta->antenna =
  602. (lq_sta->valid_antenna & local->hw.conf.antenna_sel_tx);
  603. if (!lq_sta->antenna)
  604. lq_sta->antenna = lq_sta->valid_antenna;
  605. /* Ignore mac80211 antenna info for now */
  606. lq_sta->antenna = lq_sta->valid_antenna;
  607. curr_tbl = &(lq_sta->lq_info[active_index]);
  608. search_tbl = &(lq_sta->lq_info[(1 - active_index)]);
  609. window = (struct iwl4965_rate_scale_data *)
  610. &(curr_tbl->win[0]);
  611. search_win = (struct iwl4965_rate_scale_data *)
  612. &(search_tbl->win[0]);
  613. tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
  614. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  615. &tbl_type, &rs_index);
  616. if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
  617. IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
  618. rs_index, tx_mcs.rate_n_flags);
  619. sta_info_put(sta);
  620. return;
  621. }
  622. /*
  623. * Ignore this Tx frame response if its initial rate doesn't match
  624. * that of latest Link Quality command. There may be stragglers
  625. * from a previous Link Quality command, but we're no longer interested
  626. * in those; they're either from the "active" mode while we're trying
  627. * to check "search" mode, or a prior "search" mode after we've moved
  628. * to a new "search" mode (which might become the new "active" mode).
  629. */
  630. if (retries &&
  631. (tx_mcs.rate_n_flags !=
  632. le32_to_cpu(table->rs_table[0].rate_n_flags))) {
  633. IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
  634. tx_mcs.rate_n_flags,
  635. le32_to_cpu(table->rs_table[0].rate_n_flags));
  636. sta_info_put(sta);
  637. return;
  638. }
  639. /* Update frame history window with "failure" for each Tx retry. */
  640. while (retries) {
  641. /* Look up the rate and other info used for each tx attempt.
  642. * Each tx attempt steps one entry deeper in the rate table. */
  643. tx_mcs.rate_n_flags =
  644. le32_to_cpu(table->rs_table[index].rate_n_flags);
  645. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  646. &tbl_type, &rs_index);
  647. /* If type matches "search" table,
  648. * add failure to "search" history */
  649. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  650. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  651. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  652. if (search_tbl->expected_tpt)
  653. tpt = search_tbl->expected_tpt[rs_index];
  654. else
  655. tpt = 0;
  656. rs_collect_tx_data(search_win, rs_index, tpt, 0);
  657. /* Else if type matches "current/active" table,
  658. * add failure to "current/active" history */
  659. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  660. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  661. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  662. if (curr_tbl->expected_tpt)
  663. tpt = curr_tbl->expected_tpt[rs_index];
  664. else
  665. tpt = 0;
  666. rs_collect_tx_data(window, rs_index, tpt, 0);
  667. }
  668. /* If not searching for a new mode, increment failed counter
  669. * ... this helps determine when to start searching again */
  670. if (lq_sta->stay_in_tbl)
  671. lq_sta->total_failed++;
  672. --retries;
  673. index++;
  674. }
  675. /*
  676. * Find (by rate) the history window to update with final Tx attempt;
  677. * if Tx was successful first try, use original rate,
  678. * else look up the rate that was, finally, successful.
  679. */
  680. if (!tx_resp->retry_count)
  681. tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
  682. else
  683. tx_mcs.rate_n_flags =
  684. le32_to_cpu(table->rs_table[index].rate_n_flags);
  685. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  686. &tbl_type, &rs_index);
  687. /* Update frame history window with "success" if Tx got ACKed ... */
  688. if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
  689. status = 1;
  690. else
  691. status = 0;
  692. /* If type matches "search" table,
  693. * add final tx status to "search" history */
  694. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  695. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  696. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  697. if (search_tbl->expected_tpt)
  698. tpt = search_tbl->expected_tpt[rs_index];
  699. else
  700. tpt = 0;
  701. rs_collect_tx_data(search_win,
  702. rs_index, tpt, status);
  703. /* Else if type matches "current/active" table,
  704. * add final tx status to "current/active" history */
  705. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  706. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  707. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  708. if (curr_tbl->expected_tpt)
  709. tpt = curr_tbl->expected_tpt[rs_index];
  710. else
  711. tpt = 0;
  712. rs_collect_tx_data(window, rs_index, tpt, status);
  713. }
  714. /* If not searching for new mode, increment success/failed counter
  715. * ... these help determine when to start searching again */
  716. if (lq_sta->stay_in_tbl) {
  717. if (status)
  718. lq_sta->total_success++;
  719. else
  720. lq_sta->total_failed++;
  721. }
  722. /* See if there's a better rate or modulation mode to try. */
  723. rs_rate_scale_perform(priv, dev, hdr, sta);
  724. sta_info_put(sta);
  725. return;
  726. }
  727. static u8 rs_is_ant_connected(u8 valid_antenna,
  728. enum iwl4965_antenna_type antenna_type)
  729. {
  730. if (antenna_type == ANT_AUX)
  731. return ((valid_antenna & 0x2) ? 1:0);
  732. else if (antenna_type == ANT_MAIN)
  733. return ((valid_antenna & 0x1) ? 1:0);
  734. else if (antenna_type == ANT_BOTH)
  735. return ((valid_antenna & 0x3) == 0x3);
  736. return 1;
  737. }
  738. static u8 rs_is_other_ant_connected(u8 valid_antenna,
  739. enum iwl4965_antenna_type antenna_type)
  740. {
  741. if (antenna_type == ANT_AUX)
  742. return rs_is_ant_connected(valid_antenna, ANT_MAIN);
  743. else
  744. return rs_is_ant_connected(valid_antenna, ANT_AUX);
  745. return 0;
  746. }
  747. /*
  748. * Begin a period of staying with a selected modulation mode.
  749. * Set "stay_in_tbl" flag to prevent any mode switches.
  750. * Set frame tx success limits according to legacy vs. high-throughput,
  751. * and reset overall (spanning all rates) tx success history statistics.
  752. * These control how long we stay using same modulation mode before
  753. * searching for a new mode.
  754. */
  755. static void rs_set_stay_in_table(u8 is_legacy,
  756. struct iwl4965_lq_sta *lq_sta)
  757. {
  758. IWL_DEBUG_HT("we are staying in the same table\n");
  759. lq_sta->stay_in_tbl = 1; /* only place this gets set */
  760. if (is_legacy) {
  761. lq_sta->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  762. lq_sta->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  763. lq_sta->max_success_limit = IWL_LEGACY_SUCCESS_LIMIT;
  764. } else {
  765. lq_sta->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  766. lq_sta->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  767. lq_sta->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  768. }
  769. lq_sta->table_count = 0;
  770. lq_sta->total_failed = 0;
  771. lq_sta->total_success = 0;
  772. }
  773. /*
  774. * Find correct throughput table for given mode of modulation
  775. */
  776. static void rs_get_expected_tpt_table(struct iwl4965_lq_sta *lq_sta,
  777. struct iwl4965_scale_tbl_info *tbl)
  778. {
  779. if (is_legacy(tbl->lq_type)) {
  780. if (!is_a_band(tbl->lq_type))
  781. tbl->expected_tpt = expected_tpt_G;
  782. else
  783. tbl->expected_tpt = expected_tpt_A;
  784. } else if (is_siso(tbl->lq_type)) {
  785. if (tbl->is_fat && !lq_sta->is_dup)
  786. if (tbl->is_SGI)
  787. tbl->expected_tpt = expected_tpt_siso40MHzSGI;
  788. else
  789. tbl->expected_tpt = expected_tpt_siso40MHz;
  790. else if (tbl->is_SGI)
  791. tbl->expected_tpt = expected_tpt_siso20MHzSGI;
  792. else
  793. tbl->expected_tpt = expected_tpt_siso20MHz;
  794. } else if (is_mimo(tbl->lq_type)) {
  795. if (tbl->is_fat && !lq_sta->is_dup)
  796. if (tbl->is_SGI)
  797. tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
  798. else
  799. tbl->expected_tpt = expected_tpt_mimo40MHz;
  800. else if (tbl->is_SGI)
  801. tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
  802. else
  803. tbl->expected_tpt = expected_tpt_mimo20MHz;
  804. } else
  805. tbl->expected_tpt = expected_tpt_G;
  806. }
  807. #ifdef CONFIG_IWL4965_HT
  808. /*
  809. * Find starting rate for new "search" high-throughput mode of modulation.
  810. * Goal is to find lowest expected rate (under perfect conditions) that is
  811. * above the current measured throughput of "active" mode, to give new mode
  812. * a fair chance to prove itself without too many challenges.
  813. *
  814. * This gets called when transitioning to more aggressive modulation
  815. * (i.e. legacy to SISO or MIMO, or SISO to MIMO), as well as less aggressive
  816. * (i.e. MIMO to SISO). When moving to MIMO, bit rate will typically need
  817. * to decrease to match "active" throughput. When moving from MIMO to SISO,
  818. * bit rate will typically need to increase, but not if performance was bad.
  819. */
  820. static s32 rs_get_best_rate(struct iwl4965_priv *priv,
  821. struct iwl4965_lq_sta *lq_sta,
  822. struct iwl4965_scale_tbl_info *tbl, /* "search" */
  823. u16 rate_mask, s8 index, s8 rate)
  824. {
  825. /* "active" values */
  826. struct iwl4965_scale_tbl_info *active_tbl =
  827. &(lq_sta->lq_info[lq_sta->active_tbl]);
  828. s32 active_sr = active_tbl->win[index].success_ratio;
  829. s32 active_tpt = active_tbl->expected_tpt[index];
  830. /* expected "search" throughput */
  831. s32 *tpt_tbl = tbl->expected_tpt;
  832. s32 new_rate, high, low, start_hi;
  833. u16 high_low;
  834. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  835. for (; ;) {
  836. high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
  837. low = high_low & 0xff;
  838. high = (high_low >> 8) & 0xff;
  839. /*
  840. * Lower the "search" bit rate, to give new "search" mode
  841. * approximately the same throughput as "active" if:
  842. *
  843. * 1) "Active" mode has been working modestly well (but not
  844. * great), and expected "search" throughput (under perfect
  845. * conditions) at candidate rate is above the actual
  846. * measured "active" throughput (but less than expected
  847. * "active" throughput under perfect conditions).
  848. * OR
  849. * 2) "Active" mode has been working perfectly or very well
  850. * and expected "search" throughput (under perfect
  851. * conditions) at candidate rate is above expected
  852. * "active" throughput (under perfect conditions).
  853. */
  854. if ((((100 * tpt_tbl[rate]) > lq_sta->last_tpt) &&
  855. ((active_sr > IWL_RATE_DECREASE_TH) &&
  856. (active_sr <= IWL_RATE_HIGH_TH) &&
  857. (tpt_tbl[rate] <= active_tpt))) ||
  858. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  859. (tpt_tbl[rate] > active_tpt))) {
  860. /* (2nd or later pass)
  861. * If we've already tried to raise the rate, and are
  862. * now trying to lower it, use the higher rate. */
  863. if (start_hi != IWL_RATE_INVALID) {
  864. new_rate = start_hi;
  865. break;
  866. }
  867. new_rate = rate;
  868. /* Loop again with lower rate */
  869. if (low != IWL_RATE_INVALID)
  870. rate = low;
  871. /* Lower rate not available, use the original */
  872. else
  873. break;
  874. /* Else try to raise the "search" rate to match "active" */
  875. } else {
  876. /* (2nd or later pass)
  877. * If we've already tried to lower the rate, and are
  878. * now trying to raise it, use the lower rate. */
  879. if (new_rate != IWL_RATE_INVALID)
  880. break;
  881. /* Loop again with higher rate */
  882. else if (high != IWL_RATE_INVALID) {
  883. start_hi = high;
  884. rate = high;
  885. /* Higher rate not available, use the original */
  886. } else {
  887. new_rate = rate;
  888. break;
  889. }
  890. }
  891. }
  892. return new_rate;
  893. }
  894. #endif /* CONFIG_IWL4965_HT */
  895. static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
  896. {
  897. return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
  898. }
  899. /*
  900. * Set up search table for MIMO
  901. */
  902. static int rs_switch_to_mimo(struct iwl4965_priv *priv,
  903. struct iwl4965_lq_sta *lq_sta,
  904. struct ieee80211_conf *conf,
  905. struct sta_info *sta,
  906. struct iwl4965_scale_tbl_info *tbl, int index)
  907. {
  908. #ifdef CONFIG_IWL4965_HT
  909. u16 rate_mask;
  910. s32 rate;
  911. s8 is_green = lq_sta->is_green;
  912. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
  913. !sta->ht_info.ht_supported)
  914. return -1;
  915. IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
  916. tbl->lq_type = LQ_MIMO;
  917. rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
  918. &rate_mask);
  919. if (priv->current_ht_config.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
  920. return -1;
  921. /* Need both Tx chains/antennas to support MIMO */
  922. if (!rs_is_both_ant_supp(lq_sta->antenna))
  923. return -1;
  924. tbl->is_dup = lq_sta->is_dup;
  925. tbl->action = 0;
  926. if (priv->current_ht_config.supported_chan_width
  927. == IWL_CHANNEL_WIDTH_40MHZ)
  928. tbl->is_fat = 1;
  929. else
  930. tbl->is_fat = 0;
  931. if (tbl->is_fat) {
  932. if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
  933. tbl->is_SGI = 1;
  934. else
  935. tbl->is_SGI = 0;
  936. } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
  937. tbl->is_SGI = 1;
  938. else
  939. tbl->is_SGI = 0;
  940. rs_get_expected_tpt_table(lq_sta, tbl);
  941. rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
  942. IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
  943. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
  944. return -1;
  945. rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
  946. IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
  947. tbl->current_rate.rate_n_flags, is_green);
  948. return 0;
  949. #else
  950. return -1;
  951. #endif /*CONFIG_IWL4965_HT */
  952. }
  953. /*
  954. * Set up search table for SISO
  955. */
  956. static int rs_switch_to_siso(struct iwl4965_priv *priv,
  957. struct iwl4965_lq_sta *lq_sta,
  958. struct ieee80211_conf *conf,
  959. struct sta_info *sta,
  960. struct iwl4965_scale_tbl_info *tbl, int index)
  961. {
  962. #ifdef CONFIG_IWL4965_HT
  963. u16 rate_mask;
  964. u8 is_green = lq_sta->is_green;
  965. s32 rate;
  966. IWL_DEBUG_HT("LQ: try to switch to SISO\n");
  967. if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) ||
  968. !sta->ht_info.ht_supported)
  969. return -1;
  970. tbl->is_dup = lq_sta->is_dup;
  971. tbl->lq_type = LQ_SISO;
  972. tbl->action = 0;
  973. rs_get_supported_rates(lq_sta, NULL, tbl->lq_type,
  974. &rate_mask);
  975. if (priv->current_ht_config.supported_chan_width
  976. == IWL_CHANNEL_WIDTH_40MHZ)
  977. tbl->is_fat = 1;
  978. else
  979. tbl->is_fat = 0;
  980. if (tbl->is_fat) {
  981. if (priv->current_ht_config.sgf & HT_SHORT_GI_40MHZ_ONLY)
  982. tbl->is_SGI = 1;
  983. else
  984. tbl->is_SGI = 0;
  985. } else if (priv->current_ht_config.sgf & HT_SHORT_GI_20MHZ_ONLY)
  986. tbl->is_SGI = 1;
  987. else
  988. tbl->is_SGI = 0;
  989. if (is_green)
  990. tbl->is_SGI = 0;
  991. rs_get_expected_tpt_table(lq_sta, tbl);
  992. rate = rs_get_best_rate(priv, lq_sta, tbl, rate_mask, index, index);
  993. IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
  994. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  995. IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
  996. rate, rate_mask);
  997. return -1;
  998. }
  999. rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
  1000. IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
  1001. tbl->current_rate.rate_n_flags, is_green);
  1002. return 0;
  1003. #else
  1004. return -1;
  1005. #endif /*CONFIG_IWL4965_HT */
  1006. }
  1007. /*
  1008. * Try to switch to new modulation mode from legacy
  1009. */
  1010. static int rs_move_legacy_other(struct iwl4965_priv *priv,
  1011. struct iwl4965_lq_sta *lq_sta,
  1012. struct ieee80211_conf *conf,
  1013. struct sta_info *sta,
  1014. int index)
  1015. {
  1016. int ret = 0;
  1017. struct iwl4965_scale_tbl_info *tbl =
  1018. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1019. struct iwl4965_scale_tbl_info *search_tbl =
  1020. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1021. struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
  1022. u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
  1023. (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
  1024. u8 start_action = tbl->action;
  1025. for (; ;) {
  1026. switch (tbl->action) {
  1027. case IWL_LEGACY_SWITCH_ANTENNA:
  1028. IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
  1029. search_tbl->lq_type = LQ_NONE;
  1030. lq_sta->action_counter++;
  1031. /* Don't change antenna if success has been great */
  1032. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1033. break;
  1034. /* Don't change antenna if other one is not connected */
  1035. if (!rs_is_other_ant_connected(lq_sta->antenna,
  1036. tbl->antenna_type))
  1037. break;
  1038. /* Set up search table to try other antenna */
  1039. memcpy(search_tbl, tbl, sz);
  1040. rs_toggle_antenna(&(search_tbl->current_rate),
  1041. search_tbl);
  1042. rs_get_expected_tpt_table(lq_sta, search_tbl);
  1043. lq_sta->search_better_tbl = 1;
  1044. goto out;
  1045. case IWL_LEGACY_SWITCH_SISO:
  1046. IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
  1047. /* Set up search table to try SISO */
  1048. memcpy(search_tbl, tbl, sz);
  1049. search_tbl->lq_type = LQ_SISO;
  1050. search_tbl->is_SGI = 0;
  1051. search_tbl->is_fat = 0;
  1052. ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
  1053. search_tbl, index);
  1054. if (!ret) {
  1055. lq_sta->search_better_tbl = 1;
  1056. lq_sta->action_counter = 0;
  1057. goto out;
  1058. }
  1059. break;
  1060. case IWL_LEGACY_SWITCH_MIMO:
  1061. IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
  1062. /* Set up search table to try MIMO */
  1063. memcpy(search_tbl, tbl, sz);
  1064. search_tbl->lq_type = LQ_MIMO;
  1065. search_tbl->is_SGI = 0;
  1066. search_tbl->is_fat = 0;
  1067. search_tbl->antenna_type = ANT_BOTH;
  1068. ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
  1069. search_tbl, index);
  1070. if (!ret) {
  1071. lq_sta->search_better_tbl = 1;
  1072. lq_sta->action_counter = 0;
  1073. goto out;
  1074. }
  1075. break;
  1076. }
  1077. tbl->action++;
  1078. if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
  1079. tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
  1080. if (tbl->action == start_action)
  1081. break;
  1082. }
  1083. return 0;
  1084. out:
  1085. tbl->action++;
  1086. if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
  1087. tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
  1088. return 0;
  1089. }
  1090. /*
  1091. * Try to switch to new modulation mode from SISO
  1092. */
  1093. static int rs_move_siso_to_other(struct iwl4965_priv *priv,
  1094. struct iwl4965_lq_sta *lq_sta,
  1095. struct ieee80211_conf *conf,
  1096. struct sta_info *sta,
  1097. int index)
  1098. {
  1099. int ret;
  1100. u8 is_green = lq_sta->is_green;
  1101. struct iwl4965_scale_tbl_info *tbl =
  1102. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1103. struct iwl4965_scale_tbl_info *search_tbl =
  1104. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1105. struct iwl4965_rate_scale_data *window = &(tbl->win[index]);
  1106. u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
  1107. (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
  1108. u8 start_action = tbl->action;
  1109. for (;;) {
  1110. lq_sta->action_counter++;
  1111. switch (tbl->action) {
  1112. case IWL_SISO_SWITCH_ANTENNA:
  1113. IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
  1114. search_tbl->lq_type = LQ_NONE;
  1115. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  1116. break;
  1117. if (!rs_is_other_ant_connected(lq_sta->antenna,
  1118. tbl->antenna_type))
  1119. break;
  1120. memcpy(search_tbl, tbl, sz);
  1121. search_tbl->action = IWL_SISO_SWITCH_MIMO;
  1122. rs_toggle_antenna(&(search_tbl->current_rate),
  1123. search_tbl);
  1124. lq_sta->search_better_tbl = 1;
  1125. goto out;
  1126. case IWL_SISO_SWITCH_MIMO:
  1127. IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
  1128. memcpy(search_tbl, tbl, sz);
  1129. search_tbl->lq_type = LQ_MIMO;
  1130. search_tbl->is_SGI = 0;
  1131. search_tbl->is_fat = 0;
  1132. search_tbl->antenna_type = ANT_BOTH;
  1133. ret = rs_switch_to_mimo(priv, lq_sta, conf, sta,
  1134. search_tbl, index);
  1135. if (!ret) {
  1136. lq_sta->search_better_tbl = 1;
  1137. goto out;
  1138. }
  1139. break;
  1140. case IWL_SISO_SWITCH_GI:
  1141. IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
  1142. memcpy(search_tbl, tbl, sz);
  1143. search_tbl->action = 0;
  1144. if (search_tbl->is_SGI)
  1145. search_tbl->is_SGI = 0;
  1146. else if (!is_green)
  1147. search_tbl->is_SGI = 1;
  1148. else
  1149. break;
  1150. lq_sta->search_better_tbl = 1;
  1151. if ((tbl->lq_type == LQ_SISO) &&
  1152. (tbl->is_SGI)) {
  1153. s32 tpt = lq_sta->last_tpt / 100;
  1154. if (((!tbl->is_fat) &&
  1155. (tpt >= expected_tpt_siso20MHz[index])) ||
  1156. ((tbl->is_fat) &&
  1157. (tpt >= expected_tpt_siso40MHz[index])))
  1158. lq_sta->search_better_tbl = 0;
  1159. }
  1160. rs_get_expected_tpt_table(lq_sta, search_tbl);
  1161. rs_mcs_from_tbl(&search_tbl->current_rate,
  1162. search_tbl, index, is_green);
  1163. goto out;
  1164. }
  1165. tbl->action++;
  1166. if (tbl->action > IWL_SISO_SWITCH_GI)
  1167. tbl->action = IWL_SISO_SWITCH_ANTENNA;
  1168. if (tbl->action == start_action)
  1169. break;
  1170. }
  1171. return 0;
  1172. out:
  1173. tbl->action++;
  1174. if (tbl->action > IWL_SISO_SWITCH_GI)
  1175. tbl->action = IWL_SISO_SWITCH_ANTENNA;
  1176. return 0;
  1177. }
  1178. /*
  1179. * Try to switch to new modulation mode from MIMO
  1180. */
  1181. static int rs_move_mimo_to_other(struct iwl4965_priv *priv,
  1182. struct iwl4965_lq_sta *lq_sta,
  1183. struct ieee80211_conf *conf,
  1184. struct sta_info *sta,
  1185. int index)
  1186. {
  1187. int ret;
  1188. s8 is_green = lq_sta->is_green;
  1189. struct iwl4965_scale_tbl_info *tbl =
  1190. &(lq_sta->lq_info[lq_sta->active_tbl]);
  1191. struct iwl4965_scale_tbl_info *search_tbl =
  1192. &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1193. u32 sz = (sizeof(struct iwl4965_scale_tbl_info) -
  1194. (sizeof(struct iwl4965_rate_scale_data) * IWL_RATE_COUNT));
  1195. u8 start_action = tbl->action;
  1196. for (;;) {
  1197. lq_sta->action_counter++;
  1198. switch (tbl->action) {
  1199. case IWL_MIMO_SWITCH_ANTENNA_A:
  1200. case IWL_MIMO_SWITCH_ANTENNA_B:
  1201. IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
  1202. /* Set up new search table for SISO */
  1203. memcpy(search_tbl, tbl, sz);
  1204. search_tbl->lq_type = LQ_SISO;
  1205. search_tbl->is_SGI = 0;
  1206. search_tbl->is_fat = 0;
  1207. if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
  1208. search_tbl->antenna_type = ANT_MAIN;
  1209. else
  1210. search_tbl->antenna_type = ANT_AUX;
  1211. ret = rs_switch_to_siso(priv, lq_sta, conf, sta,
  1212. search_tbl, index);
  1213. if (!ret) {
  1214. lq_sta->search_better_tbl = 1;
  1215. goto out;
  1216. }
  1217. break;
  1218. case IWL_MIMO_SWITCH_GI:
  1219. IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
  1220. /* Set up new search table for MIMO */
  1221. memcpy(search_tbl, tbl, sz);
  1222. search_tbl->lq_type = LQ_MIMO;
  1223. search_tbl->antenna_type = ANT_BOTH;
  1224. search_tbl->action = 0;
  1225. if (search_tbl->is_SGI)
  1226. search_tbl->is_SGI = 0;
  1227. else
  1228. search_tbl->is_SGI = 1;
  1229. lq_sta->search_better_tbl = 1;
  1230. /*
  1231. * If active table already uses the fastest possible
  1232. * modulation (dual stream with short guard interval),
  1233. * and it's working well, there's no need to look
  1234. * for a better type of modulation!
  1235. */
  1236. if ((tbl->lq_type == LQ_MIMO) &&
  1237. (tbl->is_SGI)) {
  1238. s32 tpt = lq_sta->last_tpt / 100;
  1239. if (((!tbl->is_fat) &&
  1240. (tpt >= expected_tpt_mimo20MHz[index])) ||
  1241. ((tbl->is_fat) &&
  1242. (tpt >= expected_tpt_mimo40MHz[index])))
  1243. lq_sta->search_better_tbl = 0;
  1244. }
  1245. rs_get_expected_tpt_table(lq_sta, search_tbl);
  1246. rs_mcs_from_tbl(&search_tbl->current_rate,
  1247. search_tbl, index, is_green);
  1248. goto out;
  1249. }
  1250. tbl->action++;
  1251. if (tbl->action > IWL_MIMO_SWITCH_GI)
  1252. tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
  1253. if (tbl->action == start_action)
  1254. break;
  1255. }
  1256. return 0;
  1257. out:
  1258. tbl->action++;
  1259. if (tbl->action > IWL_MIMO_SWITCH_GI)
  1260. tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
  1261. return 0;
  1262. }
  1263. /*
  1264. * Check whether we should continue using same modulation mode, or
  1265. * begin search for a new mode, based on:
  1266. * 1) # tx successes or failures while using this mode
  1267. * 2) # times calling this function
  1268. * 3) elapsed time in this mode (not used, for now)
  1269. */
  1270. static void rs_stay_in_table(struct iwl4965_lq_sta *lq_sta)
  1271. {
  1272. struct iwl4965_scale_tbl_info *tbl;
  1273. int i;
  1274. int active_tbl;
  1275. int flush_interval_passed = 0;
  1276. active_tbl = lq_sta->active_tbl;
  1277. tbl = &(lq_sta->lq_info[active_tbl]);
  1278. /* If we've been disallowing search, see if we should now allow it */
  1279. if (lq_sta->stay_in_tbl) {
  1280. /* Elapsed time using current modulation mode */
  1281. if (lq_sta->flush_timer)
  1282. flush_interval_passed =
  1283. time_after(jiffies,
  1284. (unsigned long)(lq_sta->flush_timer +
  1285. IWL_RATE_SCALE_FLUSH_INTVL));
  1286. /* For now, disable the elapsed time criterion */
  1287. flush_interval_passed = 0;
  1288. /*
  1289. * Check if we should allow search for new modulation mode.
  1290. * If many frames have failed or succeeded, or we've used
  1291. * this same modulation for a long time, allow search, and
  1292. * reset history stats that keep track of whether we should
  1293. * allow a new search. Also (below) reset all bitmaps and
  1294. * stats in active history.
  1295. */
  1296. if ((lq_sta->total_failed > lq_sta->max_failure_limit) ||
  1297. (lq_sta->total_success > lq_sta->max_success_limit) ||
  1298. ((!lq_sta->search_better_tbl) && (lq_sta->flush_timer)
  1299. && (flush_interval_passed))) {
  1300. IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
  1301. lq_sta->total_failed,
  1302. lq_sta->total_success,
  1303. flush_interval_passed);
  1304. /* Allow search for new mode */
  1305. lq_sta->stay_in_tbl = 0; /* only place reset */
  1306. lq_sta->total_failed = 0;
  1307. lq_sta->total_success = 0;
  1308. lq_sta->flush_timer = 0;
  1309. /*
  1310. * Else if we've used this modulation mode enough repetitions
  1311. * (regardless of elapsed time or success/failure), reset
  1312. * history bitmaps and rate-specific stats for all rates in
  1313. * active table.
  1314. */
  1315. } else {
  1316. lq_sta->table_count++;
  1317. if (lq_sta->table_count >=
  1318. lq_sta->table_count_limit) {
  1319. lq_sta->table_count = 0;
  1320. IWL_DEBUG_HT("LQ: stay in table clear win\n");
  1321. for (i = 0; i < IWL_RATE_COUNT; i++)
  1322. rs_rate_scale_clear_window(
  1323. &(tbl->win[i]));
  1324. }
  1325. }
  1326. /* If transitioning to allow "search", reset all history
  1327. * bitmaps and stats in active table (this will become the new
  1328. * "search" table). */
  1329. if (!lq_sta->stay_in_tbl) {
  1330. for (i = 0; i < IWL_RATE_COUNT; i++)
  1331. rs_rate_scale_clear_window(&(tbl->win[i]));
  1332. }
  1333. }
  1334. }
  1335. /*
  1336. * Do rate scaling and search for new modulation mode.
  1337. */
  1338. static void rs_rate_scale_perform(struct iwl4965_priv *priv,
  1339. struct net_device *dev,
  1340. struct ieee80211_hdr *hdr,
  1341. struct sta_info *sta)
  1342. {
  1343. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1344. struct ieee80211_hw *hw = local_to_hw(local);
  1345. struct ieee80211_conf *conf = &hw->conf;
  1346. int low = IWL_RATE_INVALID;
  1347. int high = IWL_RATE_INVALID;
  1348. int index;
  1349. int i;
  1350. struct iwl4965_rate_scale_data *window = NULL;
  1351. int current_tpt = IWL_INVALID_VALUE;
  1352. int low_tpt = IWL_INVALID_VALUE;
  1353. int high_tpt = IWL_INVALID_VALUE;
  1354. u32 fail_count;
  1355. s8 scale_action = 0;
  1356. u16 fc, rate_mask;
  1357. u8 update_lq = 0;
  1358. struct iwl4965_lq_sta *lq_sta;
  1359. struct iwl4965_scale_tbl_info *tbl, *tbl1;
  1360. u16 rate_scale_index_msk = 0;
  1361. struct iwl4965_rate mcs_rate;
  1362. u8 is_green = 0;
  1363. u8 active_tbl = 0;
  1364. u8 done_search = 0;
  1365. u16 high_low;
  1366. IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
  1367. fc = le16_to_cpu(hdr->frame_control);
  1368. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
  1369. /* Send management frames and broadcast/multicast data using
  1370. * lowest rate. */
  1371. /* TODO: this could probably be improved.. */
  1372. return;
  1373. }
  1374. if (!sta || !sta->rate_ctrl_priv)
  1375. return;
  1376. if (!priv->lq_mngr.lq_ready) {
  1377. IWL_DEBUG_RATE("still rate scaling not ready\n");
  1378. return;
  1379. }
  1380. lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
  1381. /*
  1382. * Select rate-scale / modulation-mode table to work with in
  1383. * the rest of this function: "search" if searching for better
  1384. * modulation mode, or "active" if doing rate scaling within a mode.
  1385. */
  1386. if (!lq_sta->search_better_tbl)
  1387. active_tbl = lq_sta->active_tbl;
  1388. else
  1389. active_tbl = 1 - lq_sta->active_tbl;
  1390. tbl = &(lq_sta->lq_info[active_tbl]);
  1391. is_green = lq_sta->is_green;
  1392. /* current tx rate */
  1393. index = sta->last_txrate;
  1394. IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
  1395. tbl->lq_type);
  1396. /* rates available for this association, and for modulation mode */
  1397. rs_get_supported_rates(lq_sta, hdr, tbl->lq_type,
  1398. &rate_mask);
  1399. IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
  1400. /* mask with station rate restriction */
  1401. if (is_legacy(tbl->lq_type)) {
  1402. if (lq_sta->phymode == (u8) MODE_IEEE80211A)
  1403. /* supp_rates has no CCK bits in A mode */
  1404. rate_scale_index_msk = (u16) (rate_mask &
  1405. (lq_sta->supp_rates << IWL_FIRST_OFDM_RATE));
  1406. else
  1407. rate_scale_index_msk = (u16) (rate_mask &
  1408. lq_sta->supp_rates);
  1409. } else
  1410. rate_scale_index_msk = rate_mask;
  1411. if (!rate_scale_index_msk)
  1412. rate_scale_index_msk = rate_mask;
  1413. /* If current rate is no longer supported on current association,
  1414. * or user changed preferences for rates, find a new supported rate. */
  1415. if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
  1416. index = IWL_INVALID_VALUE;
  1417. update_lq = 1;
  1418. /* get the highest available rate */
  1419. for (i = 0; i <= IWL_RATE_COUNT; i++) {
  1420. if ((1 << i) & rate_scale_index_msk)
  1421. index = i;
  1422. }
  1423. if (index == IWL_INVALID_VALUE) {
  1424. IWL_WARNING("Can not find a suitable rate\n");
  1425. return;
  1426. }
  1427. }
  1428. /* Get expected throughput table and history window for current rate */
  1429. if (!tbl->expected_tpt)
  1430. rs_get_expected_tpt_table(lq_sta, tbl);
  1431. window = &(tbl->win[index]);
  1432. /*
  1433. * If there is not enough history to calculate actual average
  1434. * throughput, keep analyzing results of more tx frames, without
  1435. * changing rate or mode (bypass most of the rest of this function).
  1436. * Set up new rate table in uCode only if old rate is not supported
  1437. * in current association (use new rate found above).
  1438. */
  1439. fail_count = window->counter - window->success_counter;
  1440. if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1441. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
  1442. || (tbl->expected_tpt == NULL)) {
  1443. IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
  1444. "for index %d\n",
  1445. window->success_counter, window->counter, index);
  1446. /* Can't calculate this yet; not enough history */
  1447. window->average_tpt = IWL_INVALID_VALUE;
  1448. /* Should we stay with this modulation mode,
  1449. * or search for a new one? */
  1450. rs_stay_in_table(lq_sta);
  1451. /* Set up new rate table in uCode, if needed */
  1452. if (update_lq) {
  1453. rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
  1454. rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
  1455. rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  1456. }
  1457. goto out;
  1458. /* Else we have enough samples; calculate estimate of
  1459. * actual average throughput */
  1460. } else
  1461. window->average_tpt = ((window->success_ratio *
  1462. tbl->expected_tpt[index] + 64) / 128);
  1463. /* If we are searching for better modulation mode, check success. */
  1464. if (lq_sta->search_better_tbl) {
  1465. int success_limit = IWL_RATE_SCALE_SWITCH;
  1466. /* If good success, continue using the "search" mode;
  1467. * no need to send new link quality command, since we're
  1468. * continuing to use the setup that we've been trying. */
  1469. if ((window->success_ratio > success_limit) ||
  1470. (window->average_tpt > lq_sta->last_tpt)) {
  1471. if (!is_legacy(tbl->lq_type)) {
  1472. IWL_DEBUG_HT("LQ: we are switching to HT"
  1473. " rate suc %d current tpt %d"
  1474. " old tpt %d\n",
  1475. window->success_ratio,
  1476. window->average_tpt,
  1477. lq_sta->last_tpt);
  1478. lq_sta->enable_counter = 1;
  1479. }
  1480. /* Swap tables; "search" becomes "active" */
  1481. lq_sta->active_tbl = active_tbl;
  1482. current_tpt = window->average_tpt;
  1483. /* Else poor success; go back to mode in "active" table */
  1484. } else {
  1485. /* Nullify "search" table */
  1486. tbl->lq_type = LQ_NONE;
  1487. /* Revert to "active" table */
  1488. active_tbl = lq_sta->active_tbl;
  1489. tbl = &(lq_sta->lq_info[active_tbl]);
  1490. /* Revert to "active" rate and throughput info */
  1491. index = iwl4965_rate_index_from_plcp(
  1492. tbl->current_rate.rate_n_flags);
  1493. current_tpt = lq_sta->last_tpt;
  1494. /* Need to set up a new rate table in uCode */
  1495. update_lq = 1;
  1496. IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
  1497. }
  1498. /* Either way, we've made a decision; modulation mode
  1499. * search is done, allow rate adjustment next time. */
  1500. lq_sta->search_better_tbl = 0;
  1501. done_search = 1; /* Don't switch modes below! */
  1502. goto lq_update;
  1503. }
  1504. /* (Else) not in search of better modulation mode, try for better
  1505. * starting rate, while staying in this mode. */
  1506. high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
  1507. tbl->lq_type);
  1508. low = high_low & 0xff;
  1509. high = (high_low >> 8) & 0xff;
  1510. /* Collect measured throughputs for current and adjacent rates */
  1511. current_tpt = window->average_tpt;
  1512. if (low != IWL_RATE_INVALID)
  1513. low_tpt = tbl->win[low].average_tpt;
  1514. if (high != IWL_RATE_INVALID)
  1515. high_tpt = tbl->win[high].average_tpt;
  1516. /* Assume rate increase */
  1517. scale_action = 1;
  1518. /* Too many failures, decrease rate */
  1519. if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
  1520. (current_tpt == 0)) {
  1521. IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
  1522. scale_action = -1;
  1523. /* No throughput measured yet for adjacent rates; try increase. */
  1524. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  1525. (high_tpt == IWL_INVALID_VALUE))
  1526. scale_action = 1;
  1527. /* Both adjacent throughputs are measured, but neither one has better
  1528. * throughput; we're using the best rate, don't change it! */
  1529. else if ((low_tpt != IWL_INVALID_VALUE) &&
  1530. (high_tpt != IWL_INVALID_VALUE) &&
  1531. (low_tpt < current_tpt) &&
  1532. (high_tpt < current_tpt))
  1533. scale_action = 0;
  1534. /* At least one adjacent rate's throughput is measured,
  1535. * and may have better performance. */
  1536. else {
  1537. /* Higher adjacent rate's throughput is measured */
  1538. if (high_tpt != IWL_INVALID_VALUE) {
  1539. /* Higher rate has better throughput */
  1540. if (high_tpt > current_tpt)
  1541. scale_action = 1;
  1542. else {
  1543. IWL_DEBUG_RATE
  1544. ("decrease rate because of high tpt\n");
  1545. scale_action = -1;
  1546. }
  1547. /* Lower adjacent rate's throughput is measured */
  1548. } else if (low_tpt != IWL_INVALID_VALUE) {
  1549. /* Lower rate has better throughput */
  1550. if (low_tpt > current_tpt) {
  1551. IWL_DEBUG_RATE
  1552. ("decrease rate because of low tpt\n");
  1553. scale_action = -1;
  1554. } else
  1555. scale_action = 1;
  1556. }
  1557. }
  1558. /* Sanity check; asked for decrease, but success rate or throughput
  1559. * has been good at old rate. Don't change it. */
  1560. if (scale_action == -1) {
  1561. if ((low != IWL_RATE_INVALID) &&
  1562. ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  1563. (current_tpt > (100 * tbl->expected_tpt[low]))))
  1564. scale_action = 0;
  1565. /* Sanity check; asked for increase, but success rate has not been great
  1566. * even at old rate, higher rate will be worse. Don't change it. */
  1567. } else if ((scale_action == 1) &&
  1568. (window->success_ratio < IWL_RATE_INCREASE_TH))
  1569. scale_action = 0;
  1570. switch (scale_action) {
  1571. case -1:
  1572. /* Decrease starting rate, update uCode's rate table */
  1573. if (low != IWL_RATE_INVALID) {
  1574. update_lq = 1;
  1575. index = low;
  1576. }
  1577. break;
  1578. case 1:
  1579. /* Increase starting rate, update uCode's rate table */
  1580. if (high != IWL_RATE_INVALID) {
  1581. update_lq = 1;
  1582. index = high;
  1583. }
  1584. break;
  1585. case 0:
  1586. /* No change */
  1587. default:
  1588. break;
  1589. }
  1590. IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
  1591. "high %d type %d\n",
  1592. index, scale_action, low, high, tbl->lq_type);
  1593. lq_update:
  1594. /* Replace uCode's rate table for the destination station. */
  1595. if (update_lq) {
  1596. rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
  1597. rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
  1598. rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  1599. }
  1600. /* Should we stay with this modulation mode, or search for a new one? */
  1601. rs_stay_in_table(lq_sta);
  1602. /*
  1603. * Search for new modulation mode if we're:
  1604. * 1) Not changing rates right now
  1605. * 2) Not just finishing up a search
  1606. * 3) Allowing a new search
  1607. */
  1608. if (!update_lq && !done_search && !lq_sta->stay_in_tbl) {
  1609. /* Save current throughput to compare with "search" throughput*/
  1610. lq_sta->last_tpt = current_tpt;
  1611. /* Select a new "search" modulation mode to try.
  1612. * If one is found, set up the new "search" table. */
  1613. if (is_legacy(tbl->lq_type))
  1614. rs_move_legacy_other(priv, lq_sta, conf, sta, index);
  1615. else if (is_siso(tbl->lq_type))
  1616. rs_move_siso_to_other(priv, lq_sta, conf, sta, index);
  1617. else
  1618. rs_move_mimo_to_other(priv, lq_sta, conf, sta, index);
  1619. /* If new "search" mode was selected, set up in uCode table */
  1620. if (lq_sta->search_better_tbl) {
  1621. /* Access the "search" table, clear its history. */
  1622. tbl = &(lq_sta->lq_info[(1 - lq_sta->active_tbl)]);
  1623. for (i = 0; i < IWL_RATE_COUNT; i++)
  1624. rs_rate_scale_clear_window(&(tbl->win[i]));
  1625. /* Use new "search" start rate */
  1626. index = iwl4965_rate_index_from_plcp(
  1627. tbl->current_rate.rate_n_flags);
  1628. IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
  1629. tbl->current_rate.rate_n_flags, index);
  1630. rs_fill_link_cmd(lq_sta, &tbl->current_rate,
  1631. &lq_sta->lq);
  1632. rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  1633. }
  1634. /* If the "active" (non-search) mode was legacy,
  1635. * and we've tried switching antennas,
  1636. * but we haven't been able to try HT modes (not available),
  1637. * stay with best antenna legacy modulation for a while
  1638. * before next round of mode comparisons. */
  1639. tbl1 = &(lq_sta->lq_info[lq_sta->active_tbl]);
  1640. if (is_legacy(tbl1->lq_type) &&
  1641. #ifdef CONFIG_IWL4965_HT
  1642. (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) &&
  1643. #endif
  1644. (lq_sta->action_counter >= 1)) {
  1645. lq_sta->action_counter = 0;
  1646. IWL_DEBUG_HT("LQ: STAY in legacy table\n");
  1647. rs_set_stay_in_table(1, lq_sta);
  1648. }
  1649. /* If we're in an HT mode, and all 3 mode switch actions
  1650. * have been tried and compared, stay in this best modulation
  1651. * mode for a while before next round of mode comparisons. */
  1652. if (lq_sta->enable_counter &&
  1653. (lq_sta->action_counter >= IWL_ACTION_LIMIT)) {
  1654. #ifdef CONFIG_IWL4965_HT_AGG
  1655. /* If appropriate, set up aggregation! */
  1656. if ((lq_sta->last_tpt > TID_AGG_TPT_THREHOLD) &&
  1657. (priv->lq_mngr.agg_ctrl.auto_agg)) {
  1658. priv->lq_mngr.agg_ctrl.tid_retry =
  1659. TID_ALL_SPECIFIED;
  1660. schedule_work(&priv->agg_work);
  1661. }
  1662. #endif /*CONFIG_IWL4965_HT_AGG */
  1663. lq_sta->action_counter = 0;
  1664. rs_set_stay_in_table(0, lq_sta);
  1665. }
  1666. /*
  1667. * Else, don't search for a new modulation mode.
  1668. * Put new timestamp in stay-in-modulation-mode flush timer if:
  1669. * 1) Not changing rates right now
  1670. * 2) Not just finishing up a search
  1671. * 3) flush timer is empty
  1672. */
  1673. } else {
  1674. if ((!update_lq) && (!done_search) && (!lq_sta->flush_timer))
  1675. lq_sta->flush_timer = jiffies;
  1676. }
  1677. out:
  1678. rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
  1679. i = index;
  1680. sta->last_txrate = i;
  1681. /* sta->txrate is an index to A mode rates which start
  1682. * at IWL_FIRST_OFDM_RATE
  1683. */
  1684. if (lq_sta->phymode == (u8) MODE_IEEE80211A)
  1685. sta->txrate = i - IWL_FIRST_OFDM_RATE;
  1686. else
  1687. sta->txrate = i;
  1688. return;
  1689. }
  1690. static void rs_initialize_lq(struct iwl4965_priv *priv,
  1691. struct ieee80211_conf *conf,
  1692. struct sta_info *sta)
  1693. {
  1694. int i;
  1695. struct iwl4965_lq_sta *lq_sta;
  1696. struct iwl4965_scale_tbl_info *tbl;
  1697. u8 active_tbl = 0;
  1698. int rate_idx;
  1699. u8 use_green = rs_use_green(priv, conf);
  1700. struct iwl4965_rate mcs_rate;
  1701. if (!sta || !sta->rate_ctrl_priv)
  1702. goto out;
  1703. lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
  1704. i = sta->last_txrate;
  1705. if ((lq_sta->lq.sta_id == 0xff) &&
  1706. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  1707. goto out;
  1708. if (!lq_sta->search_better_tbl)
  1709. active_tbl = lq_sta->active_tbl;
  1710. else
  1711. active_tbl = 1 - lq_sta->active_tbl;
  1712. tbl = &(lq_sta->lq_info[active_tbl]);
  1713. if ((i < 0) || (i >= IWL_RATE_COUNT))
  1714. i = 0;
  1715. mcs_rate.rate_n_flags = iwl4965_rates[i].plcp ;
  1716. mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
  1717. mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
  1718. if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
  1719. mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
  1720. tbl->antenna_type = ANT_AUX;
  1721. rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
  1722. if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
  1723. rs_toggle_antenna(&mcs_rate, tbl);
  1724. rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
  1725. tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
  1726. rs_get_expected_tpt_table(lq_sta, tbl);
  1727. rs_fill_link_cmd(lq_sta, &mcs_rate, &lq_sta->lq);
  1728. rs_send_lq_cmd(priv, &lq_sta->lq, CMD_ASYNC);
  1729. out:
  1730. return;
  1731. }
  1732. static void rs_get_rate(void *priv_rate, struct net_device *dev,
  1733. struct ieee80211_hw_mode *mode, struct sk_buff *skb,
  1734. struct rate_selection *sel)
  1735. {
  1736. int i;
  1737. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1738. struct ieee80211_conf *conf = &local->hw.conf;
  1739. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1740. struct sta_info *sta;
  1741. u16 fc;
  1742. struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
  1743. struct iwl4965_lq_sta *lq_sta;
  1744. IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
  1745. sta = sta_info_get(local, hdr->addr1);
  1746. /* Send management frames and broadcast/multicast data using lowest
  1747. * rate. */
  1748. fc = le16_to_cpu(hdr->frame_control);
  1749. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1) ||
  1750. !sta || !sta->rate_ctrl_priv) {
  1751. sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
  1752. if (sta)
  1753. sta_info_put(sta);
  1754. return;
  1755. }
  1756. lq_sta = (struct iwl4965_lq_sta *)sta->rate_ctrl_priv;
  1757. i = sta->last_txrate;
  1758. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  1759. !lq_sta->ibss_sta_added) {
  1760. u8 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
  1761. DECLARE_MAC_BUF(mac);
  1762. if (sta_id == IWL_INVALID_STATION) {
  1763. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  1764. print_mac(mac, hdr->addr1));
  1765. sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
  1766. 0, CMD_ASYNC, NULL);
  1767. }
  1768. if ((sta_id != IWL_INVALID_STATION)) {
  1769. lq_sta->lq.sta_id = sta_id;
  1770. lq_sta->lq.rs_table[0].rate_n_flags = 0;
  1771. lq_sta->ibss_sta_added = 1;
  1772. rs_initialize_lq(priv, conf, sta);
  1773. }
  1774. if (!lq_sta->ibss_sta_added)
  1775. goto done;
  1776. }
  1777. done:
  1778. if ((i < 0) || (i > IWL_RATE_COUNT)) {
  1779. sel->rate = rate_lowest(local, local->oper_hw_mode, sta);
  1780. return;
  1781. }
  1782. sta_info_put(sta);
  1783. sel->rate = &priv->ieee_rates[i];
  1784. }
  1785. static void *rs_alloc_sta(void *priv, gfp_t gfp)
  1786. {
  1787. struct iwl4965_lq_sta *lq_sta;
  1788. int i, j;
  1789. IWL_DEBUG_RATE("create station rate scale window\n");
  1790. lq_sta = kzalloc(sizeof(struct iwl4965_lq_sta), gfp);
  1791. if (lq_sta == NULL)
  1792. return NULL;
  1793. lq_sta->lq.sta_id = 0xff;
  1794. for (j = 0; j < LQ_SIZE; j++)
  1795. for (i = 0; i < IWL_RATE_COUNT; i++)
  1796. rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
  1797. return lq_sta;
  1798. }
  1799. static void rs_rate_init(void *priv_rate, void *priv_sta,
  1800. struct ieee80211_local *local,
  1801. struct sta_info *sta)
  1802. {
  1803. int i, j;
  1804. struct ieee80211_conf *conf = &local->hw.conf;
  1805. struct ieee80211_hw_mode *mode = local->oper_hw_mode;
  1806. struct iwl4965_priv *priv = (struct iwl4965_priv *)priv_rate;
  1807. struct iwl4965_lq_sta *lq_sta = priv_sta;
  1808. lq_sta->flush_timer = 0;
  1809. lq_sta->supp_rates = sta->supp_rates;
  1810. sta->txrate = 3;
  1811. for (j = 0; j < LQ_SIZE; j++)
  1812. for (i = 0; i < IWL_RATE_COUNT; i++)
  1813. rs_rate_scale_clear_window(&(lq_sta->lq_info[j].win[i]));
  1814. IWL_DEBUG_RATE("rate scale global init\n");
  1815. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  1816. * the lowest or the highest rate.. Could consider using RSSI from
  1817. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  1818. * after assoc.. */
  1819. lq_sta->ibss_sta_added = 0;
  1820. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1821. u8 sta_id = iwl4965_hw_find_station(priv, sta->addr);
  1822. DECLARE_MAC_BUF(mac);
  1823. /* for IBSS the call are from tasklet */
  1824. IWL_DEBUG_HT("LQ: ADD station %s\n",
  1825. print_mac(mac, sta->addr));
  1826. if (sta_id == IWL_INVALID_STATION) {
  1827. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  1828. print_mac(mac, sta->addr));
  1829. sta_id = iwl4965_add_station_flags(priv, sta->addr,
  1830. 0, CMD_ASYNC, NULL);
  1831. }
  1832. if ((sta_id != IWL_INVALID_STATION)) {
  1833. lq_sta->lq.sta_id = sta_id;
  1834. lq_sta->lq.rs_table[0].rate_n_flags = 0;
  1835. }
  1836. /* FIXME: this is w/a remove it later */
  1837. priv->assoc_station_added = 1;
  1838. }
  1839. /* Find highest tx rate supported by hardware and destination station */
  1840. for (i = 0; i < mode->num_rates; i++) {
  1841. if ((sta->supp_rates & BIT(i)) &&
  1842. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
  1843. sta->txrate = i;
  1844. }
  1845. sta->last_txrate = sta->txrate;
  1846. /* For MODE_IEEE80211A, cck rates are at end of rate table */
  1847. if (local->hw.conf.phymode == MODE_IEEE80211A)
  1848. sta->last_txrate += IWL_FIRST_OFDM_RATE;
  1849. lq_sta->is_dup = 0;
  1850. lq_sta->valid_antenna = priv->valid_antenna;
  1851. lq_sta->antenna = priv->antenna;
  1852. lq_sta->is_green = rs_use_green(priv, conf);
  1853. lq_sta->active_rate = priv->active_rate;
  1854. lq_sta->active_rate &= ~(0x1000);
  1855. lq_sta->active_rate_basic = priv->active_rate_basic;
  1856. lq_sta->phymode = priv->phymode;
  1857. #ifdef CONFIG_IWL4965_HT
  1858. /*
  1859. * active_siso_rate mask includes 9 MBits (bit 5), and CCK (bits 0-3),
  1860. * supp_rates[] does not; shift to convert format, force 9 MBits off.
  1861. */
  1862. lq_sta->active_siso_rate = (priv->current_ht_config.supp_mcs_set[0] << 1);
  1863. lq_sta->active_siso_rate |=
  1864. (priv->current_ht_config.supp_mcs_set[0] & 0x1);
  1865. lq_sta->active_siso_rate &= ~((u16)0x2);
  1866. lq_sta->active_siso_rate =
  1867. lq_sta->active_siso_rate << IWL_FIRST_OFDM_RATE;
  1868. /* Same here */
  1869. lq_sta->active_mimo_rate = (priv->current_ht_config.supp_mcs_set[1] << 1);
  1870. lq_sta->active_mimo_rate |=
  1871. (priv->current_ht_config.supp_mcs_set[1] & 0x1);
  1872. lq_sta->active_mimo_rate &= ~((u16)0x2);
  1873. lq_sta->active_mimo_rate =
  1874. lq_sta->active_mimo_rate << IWL_FIRST_OFDM_RATE;
  1875. IWL_DEBUG_HT("SISO RATE 0x%X MIMO RATE 0x%X\n",
  1876. lq_sta->active_siso_rate,
  1877. lq_sta->active_mimo_rate);
  1878. #endif /*CONFIG_IWL4965_HT*/
  1879. #ifdef CONFIG_MAC80211_DEBUGFS
  1880. lq_sta->drv = priv;
  1881. #endif
  1882. if (priv->assoc_station_added)
  1883. priv->lq_mngr.lq_ready = 1;
  1884. rs_initialize_lq(priv, conf, sta);
  1885. }
  1886. static void rs_fill_link_cmd(struct iwl4965_lq_sta *lq_sta,
  1887. struct iwl4965_rate *tx_mcs,
  1888. struct iwl4965_link_quality_cmd *lq_cmd)
  1889. {
  1890. int index = 0;
  1891. int rate_idx;
  1892. int repeat_rate = 0;
  1893. u8 ant_toggle_count = 0;
  1894. u8 use_ht_possible = 1;
  1895. struct iwl4965_rate new_rate;
  1896. struct iwl4965_scale_tbl_info tbl_type = { 0 };
  1897. /* Override starting rate (index 0) if needed for debug purposes */
  1898. rs_dbgfs_set_mcs(lq_sta, tx_mcs, index);
  1899. /* Interpret rate_n_flags */
  1900. rs_get_tbl_info_from_mcs(tx_mcs, lq_sta->phymode,
  1901. &tbl_type, &rate_idx);
  1902. /* How many times should we repeat the initial rate? */
  1903. if (is_legacy(tbl_type.lq_type)) {
  1904. ant_toggle_count = 1;
  1905. repeat_rate = IWL_NUMBER_TRY;
  1906. } else
  1907. repeat_rate = IWL_HT_NUMBER_TRY;
  1908. lq_cmd->general_params.mimo_delimiter =
  1909. is_mimo(tbl_type.lq_type) ? 1 : 0;
  1910. /* Fill 1st table entry (index 0) */
  1911. lq_cmd->rs_table[index].rate_n_flags =
  1912. cpu_to_le32(tx_mcs->rate_n_flags);
  1913. new_rate.rate_n_flags = tx_mcs->rate_n_flags;
  1914. if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
  1915. lq_cmd->general_params.single_stream_ant_msk
  1916. = LINK_QUAL_ANT_A_MSK;
  1917. else
  1918. lq_cmd->general_params.single_stream_ant_msk
  1919. = LINK_QUAL_ANT_B_MSK;
  1920. index++;
  1921. repeat_rate--;
  1922. /* Fill rest of rate table */
  1923. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  1924. /* Repeat initial/next rate.
  1925. * For legacy IWL_NUMBER_TRY == 1, this loop will not execute.
  1926. * For HT IWL_HT_NUMBER_TRY == 3, this executes twice. */
  1927. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  1928. if (is_legacy(tbl_type.lq_type)) {
  1929. if (ant_toggle_count <
  1930. NUM_TRY_BEFORE_ANTENNA_TOGGLE)
  1931. ant_toggle_count++;
  1932. else {
  1933. rs_toggle_antenna(&new_rate, &tbl_type);
  1934. ant_toggle_count = 1;
  1935. }
  1936. }
  1937. /* Override next rate if needed for debug purposes */
  1938. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  1939. /* Fill next table entry */
  1940. lq_cmd->rs_table[index].rate_n_flags =
  1941. cpu_to_le32(new_rate.rate_n_flags);
  1942. repeat_rate--;
  1943. index++;
  1944. }
  1945. rs_get_tbl_info_from_mcs(&new_rate, lq_sta->phymode, &tbl_type,
  1946. &rate_idx);
  1947. /* Indicate to uCode which entries might be MIMO.
  1948. * If initial rate was MIMO, this will finally end up
  1949. * as (IWL_HT_NUMBER_TRY * 2), after 2nd pass, otherwise 0. */
  1950. if (is_mimo(tbl_type.lq_type))
  1951. lq_cmd->general_params.mimo_delimiter = index;
  1952. /* Get next rate */
  1953. rs_get_lower_rate(lq_sta, &tbl_type, rate_idx,
  1954. use_ht_possible, &new_rate);
  1955. /* How many times should we repeat the next rate? */
  1956. if (is_legacy(tbl_type.lq_type)) {
  1957. if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
  1958. ant_toggle_count++;
  1959. else {
  1960. rs_toggle_antenna(&new_rate, &tbl_type);
  1961. ant_toggle_count = 1;
  1962. }
  1963. repeat_rate = IWL_NUMBER_TRY;
  1964. } else
  1965. repeat_rate = IWL_HT_NUMBER_TRY;
  1966. /* Don't allow HT rates after next pass.
  1967. * rs_get_lower_rate() will change type to LQ_A or LQ_G. */
  1968. use_ht_possible = 0;
  1969. /* Override next rate if needed for debug purposes */
  1970. rs_dbgfs_set_mcs(lq_sta, &new_rate, index);
  1971. /* Fill next table entry */
  1972. lq_cmd->rs_table[index].rate_n_flags =
  1973. cpu_to_le32(new_rate.rate_n_flags);
  1974. index++;
  1975. repeat_rate--;
  1976. }
  1977. lq_cmd->general_params.dual_stream_ant_msk = 3;
  1978. lq_cmd->agg_params.agg_dis_start_th = 3;
  1979. lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
  1980. }
  1981. static void *rs_alloc(struct ieee80211_local *local)
  1982. {
  1983. return local->hw.priv;
  1984. }
  1985. /* rate scale requires free function to be implemented */
  1986. static void rs_free(void *priv_rate)
  1987. {
  1988. return;
  1989. }
  1990. static void rs_clear(void *priv_rate)
  1991. {
  1992. struct iwl4965_priv *priv = (struct iwl4965_priv *) priv_rate;
  1993. IWL_DEBUG_RATE("enter\n");
  1994. priv->lq_mngr.lq_ready = 0;
  1995. #ifdef CONFIG_IWL4965_HT
  1996. #ifdef CONFIG_IWL4965_HT_AGG
  1997. if (priv->lq_mngr.agg_ctrl.granted_ba)
  1998. iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
  1999. #endif /*CONFIG_IWL4965_HT_AGG */
  2000. #endif /* CONFIG_IWL4965_HT */
  2001. IWL_DEBUG_RATE("leave\n");
  2002. }
  2003. static void rs_free_sta(void *priv, void *priv_sta)
  2004. {
  2005. struct iwl4965_lq_sta *lq_sta = priv_sta;
  2006. IWL_DEBUG_RATE("enter\n");
  2007. kfree(lq_sta);
  2008. IWL_DEBUG_RATE("leave\n");
  2009. }
  2010. #ifdef CONFIG_MAC80211_DEBUGFS
  2011. static int open_file_generic(struct inode *inode, struct file *file)
  2012. {
  2013. file->private_data = inode->i_private;
  2014. return 0;
  2015. }
  2016. static void rs_dbgfs_set_mcs(struct iwl4965_lq_sta *lq_sta,
  2017. struct iwl4965_rate *mcs, int index)
  2018. {
  2019. u32 base_rate;
  2020. if (lq_sta->phymode == (u8) MODE_IEEE80211A)
  2021. base_rate = 0x800D;
  2022. else
  2023. base_rate = 0x820A;
  2024. if (lq_sta->dbg_fixed.rate_n_flags) {
  2025. if (index < 12)
  2026. mcs->rate_n_flags = lq_sta->dbg_fixed.rate_n_flags;
  2027. else
  2028. mcs->rate_n_flags = base_rate;
  2029. IWL_DEBUG_RATE("Fixed rate ON\n");
  2030. return;
  2031. }
  2032. IWL_DEBUG_RATE("Fixed rate OFF\n");
  2033. }
  2034. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  2035. const char __user *user_buf, size_t count, loff_t *ppos)
  2036. {
  2037. struct iwl4965_lq_sta *lq_sta = file->private_data;
  2038. char buf[64];
  2039. int buf_size;
  2040. u32 parsed_rate;
  2041. memset(buf, 0, sizeof(buf));
  2042. buf_size = min(count, sizeof(buf) - 1);
  2043. if (copy_from_user(buf, user_buf, buf_size))
  2044. return -EFAULT;
  2045. if (sscanf(buf, "%x", &parsed_rate) == 1)
  2046. lq_sta->dbg_fixed.rate_n_flags = parsed_rate;
  2047. else
  2048. lq_sta->dbg_fixed.rate_n_flags = 0;
  2049. lq_sta->active_rate = 0x0FFF; /* 1 - 54 MBits, includes CCK */
  2050. lq_sta->active_siso_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2051. lq_sta->active_mimo_rate = 0x1FD0; /* 6 - 60 MBits, no 9, no CCK */
  2052. IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
  2053. lq_sta->lq.sta_id, lq_sta->dbg_fixed.rate_n_flags);
  2054. if (lq_sta->dbg_fixed.rate_n_flags) {
  2055. rs_fill_link_cmd(lq_sta, &lq_sta->dbg_fixed, &lq_sta->lq);
  2056. rs_send_lq_cmd(lq_sta->drv, &lq_sta->lq, CMD_ASYNC);
  2057. }
  2058. return count;
  2059. }
  2060. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  2061. char __user *user_buf, size_t count, loff_t *ppos)
  2062. {
  2063. char buff[1024];
  2064. int desc = 0;
  2065. int i = 0;
  2066. struct iwl4965_lq_sta *lq_sta = file->private_data;
  2067. desc += sprintf(buff+desc, "sta_id %d\n", lq_sta->lq.sta_id);
  2068. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  2069. lq_sta->total_failed, lq_sta->total_success,
  2070. lq_sta->active_rate);
  2071. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  2072. lq_sta->dbg_fixed.rate_n_flags);
  2073. desc += sprintf(buff+desc, "general:"
  2074. "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  2075. lq_sta->lq.general_params.flags,
  2076. lq_sta->lq.general_params.mimo_delimiter,
  2077. lq_sta->lq.general_params.single_stream_ant_msk,
  2078. lq_sta->lq.general_params.dual_stream_ant_msk);
  2079. desc += sprintf(buff+desc, "agg:"
  2080. "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  2081. le16_to_cpu(lq_sta->lq.agg_params.agg_time_limit),
  2082. lq_sta->lq.agg_params.agg_dis_start_th,
  2083. lq_sta->lq.agg_params.agg_frame_cnt_limit);
  2084. desc += sprintf(buff+desc,
  2085. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  2086. lq_sta->lq.general_params.start_rate_index[0],
  2087. lq_sta->lq.general_params.start_rate_index[1],
  2088. lq_sta->lq.general_params.start_rate_index[2],
  2089. lq_sta->lq.general_params.start_rate_index[3]);
  2090. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  2091. desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
  2092. i, le32_to_cpu(lq_sta->lq.rs_table[i].rate_n_flags));
  2093. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2094. }
  2095. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  2096. .write = rs_sta_dbgfs_scale_table_write,
  2097. .read = rs_sta_dbgfs_scale_table_read,
  2098. .open = open_file_generic,
  2099. };
  2100. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  2101. char __user *user_buf, size_t count, loff_t *ppos)
  2102. {
  2103. char buff[1024];
  2104. int desc = 0;
  2105. int i, j;
  2106. struct iwl4965_lq_sta *lq_sta = file->private_data;
  2107. for (i = 0; i < LQ_SIZE; i++) {
  2108. desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
  2109. "rate=0x%X\n",
  2110. lq_sta->active_tbl == i?"*":"x",
  2111. lq_sta->lq_info[i].lq_type,
  2112. lq_sta->lq_info[i].is_SGI,
  2113. lq_sta->lq_info[i].is_fat,
  2114. lq_sta->lq_info[i].is_dup,
  2115. lq_sta->lq_info[i].current_rate.rate_n_flags);
  2116. for (j = 0; j < IWL_RATE_COUNT; j++) {
  2117. desc += sprintf(buff+desc,
  2118. "counter=%d success=%d %%=%d\n",
  2119. lq_sta->lq_info[i].win[j].counter,
  2120. lq_sta->lq_info[i].win[j].success_counter,
  2121. lq_sta->lq_info[i].win[j].success_ratio);
  2122. }
  2123. }
  2124. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  2125. }
  2126. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  2127. .read = rs_sta_dbgfs_stats_table_read,
  2128. .open = open_file_generic,
  2129. };
  2130. static void rs_add_debugfs(void *priv, void *priv_sta,
  2131. struct dentry *dir)
  2132. {
  2133. struct iwl4965_lq_sta *lq_sta = priv_sta;
  2134. lq_sta->rs_sta_dbgfs_scale_table_file =
  2135. debugfs_create_file("rate_scale_table", 0600, dir,
  2136. lq_sta, &rs_sta_dbgfs_scale_table_ops);
  2137. lq_sta->rs_sta_dbgfs_stats_table_file =
  2138. debugfs_create_file("rate_stats_table", 0600, dir,
  2139. lq_sta, &rs_sta_dbgfs_stats_table_ops);
  2140. }
  2141. static void rs_remove_debugfs(void *priv, void *priv_sta)
  2142. {
  2143. struct iwl4965_lq_sta *lq_sta = priv_sta;
  2144. debugfs_remove(lq_sta->rs_sta_dbgfs_scale_table_file);
  2145. debugfs_remove(lq_sta->rs_sta_dbgfs_stats_table_file);
  2146. }
  2147. #endif
  2148. static struct rate_control_ops rs_ops = {
  2149. .module = NULL,
  2150. .name = RS_NAME,
  2151. .tx_status = rs_tx_status,
  2152. .get_rate = rs_get_rate,
  2153. .rate_init = rs_rate_init,
  2154. .clear = rs_clear,
  2155. .alloc = rs_alloc,
  2156. .free = rs_free,
  2157. .alloc_sta = rs_alloc_sta,
  2158. .free_sta = rs_free_sta,
  2159. #ifdef CONFIG_MAC80211_DEBUGFS
  2160. .add_sta_debugfs = rs_add_debugfs,
  2161. .remove_sta_debugfs = rs_remove_debugfs,
  2162. #endif
  2163. };
  2164. int iwl4965_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
  2165. {
  2166. struct ieee80211_local *local = hw_to_local(hw);
  2167. struct iwl4965_priv *priv = hw->priv;
  2168. struct iwl4965_lq_sta *lq_sta;
  2169. struct sta_info *sta;
  2170. int cnt = 0, i;
  2171. u32 samples = 0, success = 0, good = 0;
  2172. unsigned long now = jiffies;
  2173. u32 max_time = 0;
  2174. u8 lq_type, antenna;
  2175. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  2176. if (!sta || !sta->rate_ctrl_priv) {
  2177. if (sta) {
  2178. sta_info_put(sta);
  2179. IWL_DEBUG_RATE("leave - no private rate data!\n");
  2180. } else
  2181. IWL_DEBUG_RATE("leave - no station!\n");
  2182. return sprintf(buf, "station %d not found\n", sta_id);
  2183. }
  2184. lq_sta = (void *)sta->rate_ctrl_priv;
  2185. lq_type = lq_sta->lq_info[lq_sta->active_tbl].lq_type;
  2186. antenna = lq_sta->lq_info[lq_sta->active_tbl].antenna_type;
  2187. if (is_legacy(lq_type))
  2188. i = IWL_RATE_54M_INDEX;
  2189. else
  2190. i = IWL_RATE_60M_INDEX;
  2191. while (1) {
  2192. u64 mask;
  2193. int j;
  2194. int active = lq_sta->active_tbl;
  2195. cnt +=
  2196. sprintf(&buf[cnt], " %2dMbs: ", iwl4965_rates[i].ieee / 2);
  2197. mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
  2198. for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
  2199. buf[cnt++] =
  2200. (lq_sta->lq_info[active].win[i].data & mask)
  2201. ? '1' : '0';
  2202. samples += lq_sta->lq_info[active].win[i].counter;
  2203. good += lq_sta->lq_info[active].win[i].success_counter;
  2204. success += lq_sta->lq_info[active].win[i].success_counter *
  2205. iwl4965_rates[i].ieee;
  2206. if (lq_sta->lq_info[active].win[i].stamp) {
  2207. int delta =
  2208. jiffies_to_msecs(now -
  2209. lq_sta->lq_info[active].win[i].stamp);
  2210. if (delta > max_time)
  2211. max_time = delta;
  2212. cnt += sprintf(&buf[cnt], "%5dms\n", delta);
  2213. } else
  2214. buf[cnt++] = '\n';
  2215. j = iwl4965_get_prev_ieee_rate(i);
  2216. if (j == i)
  2217. break;
  2218. i = j;
  2219. }
  2220. /* Display the average rate of all samples taken.
  2221. *
  2222. * NOTE: We multiply # of samples by 2 since the IEEE measurement
  2223. * added from iwl4965_rates is actually 2X the rate */
  2224. if (samples)
  2225. cnt += sprintf(&buf[cnt],
  2226. "\nAverage rate is %3d.%02dMbs over last %4dms\n"
  2227. "%3d%% success (%d good packets over %d tries)\n",
  2228. success / (2 * samples), (success * 5 / samples) % 10,
  2229. max_time, good * 100 / samples, good, samples);
  2230. else
  2231. cnt += sprintf(&buf[cnt], "\nAverage rate: 0Mbs\n");
  2232. cnt += sprintf(&buf[cnt], "\nrate scale type %d antenna %d "
  2233. "active_search %d rate index %d\n", lq_type, antenna,
  2234. lq_sta->search_better_tbl, sta->last_txrate);
  2235. sta_info_put(sta);
  2236. return cnt;
  2237. }
  2238. void iwl4965_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  2239. {
  2240. struct iwl4965_priv *priv = hw->priv;
  2241. priv->lq_mngr.lq_ready = 1;
  2242. }
  2243. void iwl4965_rate_control_register(struct ieee80211_hw *hw)
  2244. {
  2245. ieee80211_rate_control_register(&rs_ops);
  2246. }
  2247. void iwl4965_rate_control_unregister(struct ieee80211_hw *hw)
  2248. {
  2249. ieee80211_rate_control_unregister(&rs_ops);
  2250. }