iwl-4965-rs.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298
  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. #define IWL 4965
  37. #include "../net/mac80211/ieee80211_rate.h"
  38. #include "iwlwifi.h"
  39. #include "iwl-helpers.h"
  40. #define RS_NAME "iwl-4965-rs"
  41. #define NUM_TRY_BEFORE_ANTENNA_TOGGLE 1
  42. #define IWL_NUMBER_TRY 1
  43. #define IWL_HT_NUMBER_TRY 3
  44. #define IWL_RATE_MAX_WINDOW 62
  45. #define IWL_RATE_HIGH_TH 10880
  46. #define IWL_RATE_MIN_FAILURE_TH 6
  47. #define IWL_RATE_MIN_SUCCESS_TH 8
  48. #define IWL_RATE_DECREASE_TH 1920
  49. #define IWL_RATE_INCREASE_TH 8960
  50. #define IWL_RATE_SCALE_FLUSH_INTVL (2*HZ) /*2 seconds */
  51. static u8 rs_ht_to_legacy[] = {
  52. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  53. IWL_RATE_6M_INDEX, IWL_RATE_6M_INDEX,
  54. IWL_RATE_6M_INDEX,
  55. IWL_RATE_6M_INDEX, IWL_RATE_9M_INDEX,
  56. IWL_RATE_12M_INDEX, IWL_RATE_18M_INDEX,
  57. IWL_RATE_24M_INDEX, IWL_RATE_36M_INDEX,
  58. IWL_RATE_48M_INDEX, IWL_RATE_54M_INDEX
  59. };
  60. struct iwl_rate {
  61. u32 rate_n_flags;
  62. } __attribute__ ((packed));
  63. struct iwl_rate_scale_data {
  64. u64 data;
  65. s32 success_counter;
  66. s32 success_ratio;
  67. s32 counter;
  68. s32 average_tpt;
  69. unsigned long stamp;
  70. };
  71. struct iwl_scale_tbl_info {
  72. enum iwl_table_type lq_type;
  73. enum iwl_antenna_type antenna_type;
  74. u8 is_SGI;
  75. u8 is_fat;
  76. u8 is_dup;
  77. u8 action;
  78. s32 *expected_tpt;
  79. struct iwl_rate current_rate;
  80. struct iwl_rate_scale_data win[IWL_RATE_COUNT];
  81. };
  82. struct iwl_rate_scale_priv {
  83. u8 active_tbl;
  84. u8 enable_counter;
  85. u8 stay_in_tbl;
  86. u8 search_better_tbl;
  87. s32 last_tpt;
  88. u32 table_count_limit;
  89. u32 max_failure_limit;
  90. u32 max_success_limit;
  91. u32 table_count;
  92. u32 total_failed;
  93. u32 total_success;
  94. u32 flush_timer;
  95. u8 action_counter;
  96. u8 antenna;
  97. u8 valid_antenna;
  98. u8 is_green;
  99. u8 is_dup;
  100. u8 phymode;
  101. u8 ibss_sta_added;
  102. u32 supp_rates;
  103. u16 active_rate;
  104. u16 active_siso_rate;
  105. u16 active_mimo_rate;
  106. u16 active_rate_basic;
  107. struct iwl_link_quality_cmd lq;
  108. struct iwl_scale_tbl_info lq_info[LQ_SIZE];
  109. #ifdef CONFIG_MAC80211_DEBUGFS
  110. struct dentry *rs_sta_dbgfs_scale_table_file;
  111. struct dentry *rs_sta_dbgfs_stats_table_file;
  112. struct iwl_rate dbg_fixed;
  113. struct iwl_priv *drv;
  114. #endif
  115. };
  116. static void rs_rate_scale_perform(struct iwl_priv *priv,
  117. struct net_device *dev,
  118. struct ieee80211_hdr *hdr,
  119. struct sta_info *sta);
  120. static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
  121. struct iwl_rate *tx_mcs,
  122. struct iwl_link_quality_cmd *tbl);
  123. #ifdef CONFIG_MAC80211_DEBUGFS
  124. static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
  125. struct iwl_rate *mcs, int index);
  126. #else
  127. static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
  128. struct iwl_rate *mcs, int index)
  129. {}
  130. #endif
  131. static s32 expected_tpt_A[IWL_RATE_COUNT] = {
  132. 0, 0, 0, 0, 40, 57, 72, 98, 121, 154, 177, 186, 186
  133. };
  134. static s32 expected_tpt_G[IWL_RATE_COUNT] = {
  135. 7, 13, 35, 58, 40, 57, 72, 98, 121, 154, 177, 186, 186
  136. };
  137. static s32 expected_tpt_siso20MHz[IWL_RATE_COUNT] = {
  138. 0, 0, 0, 0, 42, 42, 76, 102, 124, 159, 183, 193, 202
  139. };
  140. static s32 expected_tpt_siso20MHzSGI[IWL_RATE_COUNT] = {
  141. 0, 0, 0, 0, 46, 46, 82, 110, 132, 168, 192, 202, 211
  142. };
  143. static s32 expected_tpt_mimo20MHz[IWL_RATE_COUNT] = {
  144. 0, 0, 0, 0, 74, 74, 123, 155, 179, 214, 236, 244, 251
  145. };
  146. static s32 expected_tpt_mimo20MHzSGI[IWL_RATE_COUNT] = {
  147. 0, 0, 0, 0, 81, 81, 131, 164, 188, 222, 243, 251, 257
  148. };
  149. static s32 expected_tpt_siso40MHz[IWL_RATE_COUNT] = {
  150. 0, 0, 0, 0, 77, 77, 127, 160, 184, 220, 242, 250, 257
  151. };
  152. static s32 expected_tpt_siso40MHzSGI[IWL_RATE_COUNT] = {
  153. 0, 0, 0, 0, 83, 83, 135, 169, 193, 229, 250, 257, 264
  154. };
  155. static s32 expected_tpt_mimo40MHz[IWL_RATE_COUNT] = {
  156. 0, 0, 0, 0, 123, 123, 182, 214, 235, 264, 279, 285, 289
  157. };
  158. static s32 expected_tpt_mimo40MHzSGI[IWL_RATE_COUNT] = {
  159. 0, 0, 0, 0, 131, 131, 191, 222, 242, 270, 284, 289, 293
  160. };
  161. static int iwl_lq_sync_callback(struct iwl_priv *priv,
  162. struct iwl_cmd *cmd, struct sk_buff *skb)
  163. {
  164. /*We didn't cache the SKB; let the caller free it */
  165. return 1;
  166. }
  167. static inline u8 iwl_rate_get_rate(u32 rate_n_flags)
  168. {
  169. return (u8)(rate_n_flags & 0xFF);
  170. }
  171. static int rs_send_lq_cmd(struct iwl_priv *priv,
  172. struct iwl_link_quality_cmd *lq, u8 flags)
  173. {
  174. #ifdef CONFIG_IWLWIFI_DEBUG
  175. int i;
  176. #endif
  177. int rc = -1;
  178. struct iwl_host_cmd cmd = {
  179. .id = REPLY_TX_LINK_QUALITY_CMD,
  180. .len = sizeof(struct iwl_link_quality_cmd),
  181. .meta.flags = flags,
  182. .data = lq,
  183. };
  184. if ((lq->sta_id == 0xFF) &&
  185. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  186. return rc;
  187. if (lq->sta_id == 0xFF)
  188. lq->sta_id = IWL_AP_ID;
  189. IWL_DEBUG_RATE("lq station id 0x%x\n", lq->sta_id);
  190. IWL_DEBUG_RATE("lq dta 0x%X 0x%X\n",
  191. lq->general_params.single_stream_ant_msk,
  192. lq->general_params.dual_stream_ant_msk);
  193. #ifdef CONFIG_IWLWIFI_DEBUG
  194. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  195. IWL_DEBUG_RATE("lq index %d 0x%X\n",
  196. i, lq->rs_table[i].rate_n_flags);
  197. #endif
  198. if (flags & CMD_ASYNC)
  199. cmd.meta.u.callback = iwl_lq_sync_callback;
  200. if (iwl_is_associated(priv) && priv->assoc_station_added &&
  201. priv->lq_mngr.lq_ready)
  202. rc = iwl_send_cmd(priv, &cmd);
  203. return rc;
  204. }
  205. static int rs_rate_scale_clear_window(struct iwl_rate_scale_data *window)
  206. {
  207. window->data = 0;
  208. window->success_counter = 0;
  209. window->success_ratio = IWL_INVALID_VALUE;
  210. window->counter = 0;
  211. window->average_tpt = IWL_INVALID_VALUE;
  212. window->stamp = 0;
  213. return 0;
  214. }
  215. static int rs_collect_tx_data(struct iwl_rate_scale_data *windows,
  216. int scale_index, s32 tpt, u32 status)
  217. {
  218. int rc = 0;
  219. struct iwl_rate_scale_data *window = NULL;
  220. u64 mask;
  221. u8 win_size = IWL_RATE_MAX_WINDOW;
  222. s32 fail_count;
  223. if (scale_index < 0)
  224. return -1;
  225. if (scale_index >= IWL_RATE_COUNT)
  226. return -1;
  227. window = &(windows[scale_index]);
  228. if (window->counter >= win_size) {
  229. window->counter = win_size - 1;
  230. mask = 1;
  231. mask = (mask << (win_size - 1));
  232. if ((window->data & mask)) {
  233. window->data &= ~mask;
  234. window->success_counter = window->success_counter - 1;
  235. }
  236. }
  237. window->counter = window->counter + 1;
  238. mask = window->data;
  239. window->data = (mask << 1);
  240. if (status != 0) {
  241. window->success_counter = window->success_counter + 1;
  242. window->data |= 0x1;
  243. }
  244. if (window->counter > 0)
  245. window->success_ratio = 128 * (100 * window->success_counter)
  246. / window->counter;
  247. else
  248. window->success_ratio = IWL_INVALID_VALUE;
  249. fail_count = window->counter - window->success_counter;
  250. if ((fail_count >= IWL_RATE_MIN_FAILURE_TH) ||
  251. (window->success_counter >= IWL_RATE_MIN_SUCCESS_TH))
  252. window->average_tpt = (window->success_ratio * tpt + 64) / 128;
  253. else
  254. window->average_tpt = IWL_INVALID_VALUE;
  255. window->stamp = jiffies;
  256. return rc;
  257. }
  258. int static rs_mcs_from_tbl(struct iwl_rate *mcs_rate,
  259. struct iwl_scale_tbl_info *tbl,
  260. int index, u8 use_green)
  261. {
  262. int rc = 0;
  263. if (is_legacy(tbl->lq_type)) {
  264. mcs_rate->rate_n_flags = iwl_rates[index].plcp;
  265. if (index >= IWL_FIRST_CCK_RATE && index <= IWL_LAST_CCK_RATE)
  266. mcs_rate->rate_n_flags |= RATE_MCS_CCK_MSK;
  267. } else if (is_siso(tbl->lq_type)) {
  268. if (index > IWL_LAST_OFDM_RATE)
  269. index = IWL_LAST_OFDM_RATE;
  270. mcs_rate->rate_n_flags = iwl_rates[index].plcp_siso |
  271. RATE_MCS_HT_MSK;
  272. } else {
  273. if (index > IWL_LAST_OFDM_RATE)
  274. index = IWL_LAST_OFDM_RATE;
  275. mcs_rate->rate_n_flags = iwl_rates[index].plcp_mimo |
  276. RATE_MCS_HT_MSK;
  277. }
  278. switch (tbl->antenna_type) {
  279. case ANT_BOTH:
  280. mcs_rate->rate_n_flags |= RATE_MCS_ANT_AB_MSK;
  281. break;
  282. case ANT_MAIN:
  283. mcs_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
  284. break;
  285. case ANT_AUX:
  286. mcs_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
  287. break;
  288. case ANT_NONE:
  289. break;
  290. }
  291. if (is_legacy(tbl->lq_type))
  292. return rc;
  293. if (tbl->is_fat) {
  294. if (tbl->is_dup)
  295. mcs_rate->rate_n_flags |= RATE_MCS_DUP_MSK;
  296. else
  297. mcs_rate->rate_n_flags |= RATE_MCS_FAT_MSK;
  298. }
  299. if (tbl->is_SGI)
  300. mcs_rate->rate_n_flags |= RATE_MCS_SGI_MSK;
  301. if (use_green) {
  302. mcs_rate->rate_n_flags |= RATE_MCS_GF_MSK;
  303. if (is_siso(tbl->lq_type))
  304. mcs_rate->rate_n_flags &= ~RATE_MCS_SGI_MSK;
  305. }
  306. return rc;
  307. }
  308. static int rs_get_tbl_info_from_mcs(const struct iwl_rate *mcs_rate,
  309. int phymode, struct iwl_scale_tbl_info *tbl,
  310. int *rate_idx)
  311. {
  312. int index;
  313. u32 ant_msk;
  314. index = iwl_rate_index_from_plcp(mcs_rate->rate_n_flags);
  315. if (index == IWL_RATE_INVALID) {
  316. *rate_idx = -1;
  317. return -1;
  318. }
  319. tbl->is_SGI = 0;
  320. tbl->is_fat = 0;
  321. tbl->is_dup = 0;
  322. tbl->antenna_type = ANT_BOTH;
  323. if (!(mcs_rate->rate_n_flags & RATE_MCS_HT_MSK)) {
  324. ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
  325. if (ant_msk == RATE_MCS_ANT_AB_MSK)
  326. tbl->lq_type = LQ_NONE;
  327. else {
  328. if (phymode == MODE_IEEE80211A)
  329. tbl->lq_type = LQ_A;
  330. else
  331. tbl->lq_type = LQ_G;
  332. if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
  333. tbl->antenna_type = ANT_MAIN;
  334. else
  335. tbl->antenna_type = ANT_AUX;
  336. }
  337. *rate_idx = index;
  338. } else if (iwl_rate_get_rate(mcs_rate->rate_n_flags)
  339. <= IWL_RATE_SISO_60M_PLCP) {
  340. tbl->lq_type = LQ_SISO;
  341. ant_msk = (mcs_rate->rate_n_flags & RATE_MCS_ANT_AB_MSK);
  342. if (ant_msk == RATE_MCS_ANT_AB_MSK)
  343. tbl->lq_type = LQ_NONE;
  344. else {
  345. if (mcs_rate->rate_n_flags & RATE_MCS_ANT_A_MSK)
  346. tbl->antenna_type = ANT_MAIN;
  347. else
  348. tbl->antenna_type = ANT_AUX;
  349. }
  350. if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
  351. tbl->is_SGI = 1;
  352. if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
  353. (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
  354. tbl->is_fat = 1;
  355. if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
  356. tbl->is_dup = 1;
  357. *rate_idx = index;
  358. } else {
  359. tbl->lq_type = LQ_MIMO;
  360. if (mcs_rate->rate_n_flags & RATE_MCS_SGI_MSK)
  361. tbl->is_SGI = 1;
  362. if ((mcs_rate->rate_n_flags & RATE_MCS_FAT_MSK) ||
  363. (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK))
  364. tbl->is_fat = 1;
  365. if (mcs_rate->rate_n_flags & RATE_MCS_DUP_MSK)
  366. tbl->is_dup = 1;
  367. *rate_idx = index;
  368. }
  369. return 0;
  370. }
  371. static inline void rs_toggle_antenna(struct iwl_rate *new_rate,
  372. struct iwl_scale_tbl_info *tbl)
  373. {
  374. if (tbl->antenna_type == ANT_AUX) {
  375. tbl->antenna_type = ANT_MAIN;
  376. new_rate->rate_n_flags &= ~RATE_MCS_ANT_B_MSK;
  377. new_rate->rate_n_flags |= RATE_MCS_ANT_A_MSK;
  378. } else {
  379. tbl->antenna_type = ANT_AUX;
  380. new_rate->rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
  381. new_rate->rate_n_flags |= RATE_MCS_ANT_B_MSK;
  382. }
  383. }
  384. static inline s8 rs_use_green(struct iwl_priv *priv)
  385. {
  386. s8 rc = 0;
  387. #ifdef CONFIG_IWLWIFI_HT
  388. if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
  389. return 0;
  390. if ((priv->current_assoc_ht.is_green_field) &&
  391. !(priv->current_assoc_ht.operating_mode & 0x4))
  392. rc = 1;
  393. #endif /*CONFIG_IWLWIFI_HT */
  394. return rc;
  395. }
  396. /**
  397. * rs_get_supported_rates - get the available rates
  398. *
  399. * if management frame or broadcast frame only return
  400. * basic available rates.
  401. *
  402. */
  403. static void rs_get_supported_rates(struct iwl_rate_scale_priv *lq_data,
  404. struct ieee80211_hdr *hdr,
  405. enum iwl_table_type rate_type,
  406. u16 *data_rate)
  407. {
  408. if (is_legacy(rate_type))
  409. *data_rate = lq_data->active_rate;
  410. else {
  411. if (is_siso(rate_type))
  412. *data_rate = lq_data->active_siso_rate;
  413. else
  414. *data_rate = lq_data->active_mimo_rate;
  415. }
  416. if (hdr && is_multicast_ether_addr(hdr->addr1) &&
  417. lq_data->active_rate_basic)
  418. *data_rate = lq_data->active_rate_basic;
  419. }
  420. static u16 rs_get_adjacent_rate(u8 index, u16 rate_mask, int rate_type)
  421. {
  422. u8 high = IWL_RATE_INVALID;
  423. u8 low = IWL_RATE_INVALID;
  424. /* 802.11A or ht walks to the next literal adjascent rate in
  425. * the rate table */
  426. if (is_a_band(rate_type) || !is_legacy(rate_type)) {
  427. int i;
  428. u32 mask;
  429. /* Find the previous rate that is in the rate mask */
  430. i = index - 1;
  431. for (mask = (1 << i); i >= 0; i--, mask >>= 1) {
  432. if (rate_mask & mask) {
  433. low = i;
  434. break;
  435. }
  436. }
  437. /* Find the next rate that is in the rate mask */
  438. i = index + 1;
  439. for (mask = (1 << i); i < IWL_RATE_COUNT; i++, mask <<= 1) {
  440. if (rate_mask & mask) {
  441. high = i;
  442. break;
  443. }
  444. }
  445. return (high << 8) | low;
  446. }
  447. low = index;
  448. while (low != IWL_RATE_INVALID) {
  449. low = iwl_rates[low].prev_rs;
  450. if (low == IWL_RATE_INVALID)
  451. break;
  452. if (rate_mask & (1 << low))
  453. break;
  454. IWL_DEBUG_RATE("Skipping masked lower rate: %d\n", low);
  455. }
  456. high = index;
  457. while (high != IWL_RATE_INVALID) {
  458. high = iwl_rates[high].next_rs;
  459. if (high == IWL_RATE_INVALID)
  460. break;
  461. if (rate_mask & (1 << high))
  462. break;
  463. IWL_DEBUG_RATE("Skipping masked higher rate: %d\n", high);
  464. }
  465. return (high << 8) | low;
  466. }
  467. static int rs_get_lower_rate(struct iwl_rate_scale_priv *lq_data,
  468. struct iwl_scale_tbl_info *tbl, u8 scale_index,
  469. u8 ht_possible, struct iwl_rate *mcs_rate)
  470. {
  471. s32 low;
  472. u16 rate_mask;
  473. u16 high_low;
  474. u8 switch_to_legacy = 0;
  475. u8 is_green = lq_data->is_green;
  476. /* check if we need to switch from HT to legacy rates.
  477. * assumption is that mandatory rates (1Mbps or 6Mbps)
  478. * are always supported (spec demand) */
  479. if (!is_legacy(tbl->lq_type) && (!ht_possible || !scale_index)) {
  480. switch_to_legacy = 1;
  481. scale_index = rs_ht_to_legacy[scale_index];
  482. if (lq_data->phymode == MODE_IEEE80211A)
  483. tbl->lq_type = LQ_A;
  484. else
  485. tbl->lq_type = LQ_G;
  486. if ((tbl->antenna_type == ANT_BOTH) ||
  487. (tbl->antenna_type == ANT_NONE))
  488. tbl->antenna_type = ANT_MAIN;
  489. tbl->is_fat = 0;
  490. tbl->is_SGI = 0;
  491. }
  492. rs_get_supported_rates(lq_data, NULL, tbl->lq_type, &rate_mask);
  493. /* mask with station rate restriction */
  494. if (is_legacy(tbl->lq_type)) {
  495. if (lq_data->phymode == (u8) MODE_IEEE80211A)
  496. rate_mask = (u16)(rate_mask &
  497. (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
  498. else
  499. rate_mask = (u16)(rate_mask & lq_data->supp_rates);
  500. }
  501. /* if we did switched from HT to legacy check current rate */
  502. if ((switch_to_legacy) &&
  503. (rate_mask & (1 << scale_index))) {
  504. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  505. return 0;
  506. }
  507. high_low = rs_get_adjacent_rate(scale_index, rate_mask, tbl->lq_type);
  508. low = high_low & 0xff;
  509. if (low != IWL_RATE_INVALID)
  510. rs_mcs_from_tbl(mcs_rate, tbl, low, is_green);
  511. else
  512. rs_mcs_from_tbl(mcs_rate, tbl, scale_index, is_green);
  513. return 0;
  514. }
  515. static void rs_tx_status(void *priv_rate,
  516. struct net_device *dev,
  517. struct sk_buff *skb,
  518. struct ieee80211_tx_status *tx_resp)
  519. {
  520. int status;
  521. u8 retries;
  522. int rs_index, index = 0;
  523. struct iwl_rate_scale_priv *lq;
  524. struct iwl_link_quality_cmd *table;
  525. struct sta_info *sta;
  526. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  527. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  528. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  529. struct iwl_rate_scale_data *window = NULL;
  530. struct iwl_rate_scale_data *search_win = NULL;
  531. struct iwl_rate tx_mcs;
  532. struct iwl_scale_tbl_info tbl_type;
  533. struct iwl_scale_tbl_info *curr_tbl, *search_tbl;
  534. u8 active_index = 0;
  535. u16 fc = le16_to_cpu(hdr->frame_control);
  536. s32 tpt = 0;
  537. IWL_DEBUG_RATE_LIMIT("get frame ack response, update rate scale window\n");
  538. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1))
  539. return;
  540. retries = tx_resp->retry_count;
  541. if (retries > 15)
  542. retries = 15;
  543. sta = sta_info_get(local, hdr->addr1);
  544. if (!sta || !sta->rate_ctrl_priv) {
  545. if (sta)
  546. sta_info_put(sta);
  547. return;
  548. }
  549. lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
  550. if (!priv->lq_mngr.lq_ready)
  551. return;
  552. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added)
  553. return;
  554. table = &lq->lq;
  555. active_index = lq->active_tbl;
  556. lq->antenna = (lq->valid_antenna & local->hw.conf.antenna_sel_tx);
  557. if (!lq->antenna)
  558. lq->antenna = lq->valid_antenna;
  559. lq->antenna = lq->valid_antenna;
  560. curr_tbl = &(lq->lq_info[active_index]);
  561. search_tbl = &(lq->lq_info[(1 - active_index)]);
  562. window = (struct iwl_rate_scale_data *)
  563. &(curr_tbl->win[0]);
  564. search_win = (struct iwl_rate_scale_data *)
  565. &(search_tbl->win[0]);
  566. tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
  567. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  568. &tbl_type, &rs_index);
  569. if ((rs_index < 0) || (rs_index >= IWL_RATE_COUNT)) {
  570. IWL_DEBUG_RATE("bad rate index at: %d rate 0x%X\n",
  571. rs_index, tx_mcs.rate_n_flags);
  572. sta_info_put(sta);
  573. return;
  574. }
  575. if (retries &&
  576. (tx_mcs.rate_n_flags !=
  577. le32_to_cpu(table->rs_table[0].rate_n_flags))) {
  578. IWL_DEBUG_RATE("initial rate does not match 0x%x 0x%x\n",
  579. tx_mcs.rate_n_flags,
  580. le32_to_cpu(table->rs_table[0].rate_n_flags));
  581. sta_info_put(sta);
  582. return;
  583. }
  584. while (retries) {
  585. tx_mcs.rate_n_flags =
  586. le32_to_cpu(table->rs_table[index].rate_n_flags);
  587. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  588. &tbl_type, &rs_index);
  589. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  590. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  591. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  592. if (search_tbl->expected_tpt)
  593. tpt = search_tbl->expected_tpt[rs_index];
  594. else
  595. tpt = 0;
  596. rs_collect_tx_data(search_win,
  597. rs_index, tpt, 0);
  598. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  599. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  600. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  601. if (curr_tbl->expected_tpt)
  602. tpt = curr_tbl->expected_tpt[rs_index];
  603. else
  604. tpt = 0;
  605. rs_collect_tx_data(window, rs_index, tpt, 0);
  606. }
  607. if (lq->stay_in_tbl)
  608. lq->total_failed++;
  609. --retries;
  610. index++;
  611. }
  612. if (!tx_resp->retry_count)
  613. tx_mcs.rate_n_flags = tx_resp->control.tx_rate;
  614. else
  615. tx_mcs.rate_n_flags =
  616. le32_to_cpu(table->rs_table[index].rate_n_flags);
  617. rs_get_tbl_info_from_mcs(&tx_mcs, priv->phymode,
  618. &tbl_type, &rs_index);
  619. if (tx_resp->flags & IEEE80211_TX_STATUS_ACK)
  620. status = 1;
  621. else
  622. status = 0;
  623. if ((tbl_type.lq_type == search_tbl->lq_type) &&
  624. (tbl_type.antenna_type == search_tbl->antenna_type) &&
  625. (tbl_type.is_SGI == search_tbl->is_SGI)) {
  626. if (search_tbl->expected_tpt)
  627. tpt = search_tbl->expected_tpt[rs_index];
  628. else
  629. tpt = 0;
  630. rs_collect_tx_data(search_win,
  631. rs_index, tpt, status);
  632. } else if ((tbl_type.lq_type == curr_tbl->lq_type) &&
  633. (tbl_type.antenna_type == curr_tbl->antenna_type) &&
  634. (tbl_type.is_SGI == curr_tbl->is_SGI)) {
  635. if (curr_tbl->expected_tpt)
  636. tpt = curr_tbl->expected_tpt[rs_index];
  637. else
  638. tpt = 0;
  639. rs_collect_tx_data(window, rs_index, tpt, status);
  640. }
  641. if (lq->stay_in_tbl) {
  642. if (status)
  643. lq->total_success++;
  644. else
  645. lq->total_failed++;
  646. }
  647. rs_rate_scale_perform(priv, dev, hdr, sta);
  648. sta_info_put(sta);
  649. return;
  650. }
  651. static u8 rs_is_ant_connected(u8 valid_antenna,
  652. enum iwl_antenna_type antenna_type)
  653. {
  654. if (antenna_type == ANT_AUX)
  655. return ((valid_antenna & 0x2) ? 1:0);
  656. else if (antenna_type == ANT_MAIN)
  657. return ((valid_antenna & 0x1) ? 1:0);
  658. else if (antenna_type == ANT_BOTH) {
  659. if ((valid_antenna & 0x3) == 0x3)
  660. return 1;
  661. else
  662. return 0;
  663. }
  664. return 1;
  665. }
  666. static u8 rs_is_other_ant_connected(u8 valid_antenna,
  667. enum iwl_antenna_type antenna_type)
  668. {
  669. if (antenna_type == ANT_AUX)
  670. return (rs_is_ant_connected(valid_antenna, ANT_MAIN));
  671. else
  672. return (rs_is_ant_connected(valid_antenna, ANT_AUX));
  673. return 0;
  674. }
  675. static void rs_set_stay_in_table(u8 is_legacy,
  676. struct iwl_rate_scale_priv *lq_data)
  677. {
  678. IWL_DEBUG_HT("we are staying in the same table\n");
  679. lq_data->stay_in_tbl = 1;
  680. if (is_legacy) {
  681. lq_data->table_count_limit = IWL_LEGACY_TABLE_COUNT;
  682. lq_data->max_failure_limit = IWL_LEGACY_FAILURE_LIMIT;
  683. lq_data->max_success_limit = IWL_LEGACY_TABLE_COUNT;
  684. } else {
  685. lq_data->table_count_limit = IWL_NONE_LEGACY_TABLE_COUNT;
  686. lq_data->max_failure_limit = IWL_NONE_LEGACY_FAILURE_LIMIT;
  687. lq_data->max_success_limit = IWL_NONE_LEGACY_SUCCESS_LIMIT;
  688. }
  689. lq_data->table_count = 0;
  690. lq_data->total_failed = 0;
  691. lq_data->total_success = 0;
  692. }
  693. static void rs_get_expected_tpt_table(struct iwl_rate_scale_priv *lq_data,
  694. struct iwl_scale_tbl_info *tbl)
  695. {
  696. if (is_legacy(tbl->lq_type)) {
  697. if (!is_a_band(tbl->lq_type))
  698. tbl->expected_tpt = expected_tpt_G;
  699. else
  700. tbl->expected_tpt = expected_tpt_A;
  701. } else if (is_siso(tbl->lq_type)) {
  702. if (tbl->is_fat && !lq_data->is_dup)
  703. if (tbl->is_SGI)
  704. tbl->expected_tpt = expected_tpt_siso40MHzSGI;
  705. else
  706. tbl->expected_tpt = expected_tpt_siso40MHz;
  707. else if (tbl->is_SGI)
  708. tbl->expected_tpt = expected_tpt_siso20MHzSGI;
  709. else
  710. tbl->expected_tpt = expected_tpt_siso20MHz;
  711. } else if (is_mimo(tbl->lq_type)) {
  712. if (tbl->is_fat && !lq_data->is_dup)
  713. if (tbl->is_SGI)
  714. tbl->expected_tpt = expected_tpt_mimo40MHzSGI;
  715. else
  716. tbl->expected_tpt = expected_tpt_mimo40MHz;
  717. else if (tbl->is_SGI)
  718. tbl->expected_tpt = expected_tpt_mimo20MHzSGI;
  719. else
  720. tbl->expected_tpt = expected_tpt_mimo20MHz;
  721. } else
  722. tbl->expected_tpt = expected_tpt_G;
  723. }
  724. #ifdef CONFIG_IWLWIFI_HT
  725. static s32 rs_get_best_rate(struct iwl_priv *priv,
  726. struct iwl_rate_scale_priv *lq_data,
  727. struct iwl_scale_tbl_info *tbl,
  728. u16 rate_mask, s8 index, s8 rate)
  729. {
  730. struct iwl_scale_tbl_info *active_tbl =
  731. &(lq_data->lq_info[lq_data->active_tbl]);
  732. s32 new_rate, high, low, start_hi;
  733. s32 active_sr = active_tbl->win[index].success_ratio;
  734. s32 *tpt_tbl = tbl->expected_tpt;
  735. s32 active_tpt = active_tbl->expected_tpt[index];
  736. u16 high_low;
  737. new_rate = high = low = start_hi = IWL_RATE_INVALID;
  738. for (; ;) {
  739. high_low = rs_get_adjacent_rate(rate, rate_mask, tbl->lq_type);
  740. low = high_low & 0xff;
  741. high = (high_low >> 8) & 0xff;
  742. if ((((100 * tpt_tbl[rate]) > lq_data->last_tpt) &&
  743. ((active_sr > IWL_RATE_DECREASE_TH) &&
  744. (active_sr <= IWL_RATE_HIGH_TH) &&
  745. (tpt_tbl[rate] <= active_tpt))) ||
  746. ((active_sr >= IWL_RATE_SCALE_SWITCH) &&
  747. (tpt_tbl[rate] > active_tpt))) {
  748. if (start_hi != IWL_RATE_INVALID) {
  749. new_rate = start_hi;
  750. break;
  751. }
  752. new_rate = rate;
  753. if (low != IWL_RATE_INVALID)
  754. rate = low;
  755. else
  756. break;
  757. } else {
  758. if (new_rate != IWL_RATE_INVALID)
  759. break;
  760. else if (high != IWL_RATE_INVALID) {
  761. start_hi = high;
  762. rate = high;
  763. } else {
  764. new_rate = rate;
  765. break;
  766. }
  767. }
  768. }
  769. return new_rate;
  770. }
  771. #endif /* CONFIG_IWLWIFI_HT */
  772. static inline u8 rs_is_both_ant_supp(u8 valid_antenna)
  773. {
  774. return (rs_is_ant_connected(valid_antenna, ANT_BOTH));
  775. }
  776. static int rs_switch_to_mimo(struct iwl_priv *priv,
  777. struct iwl_rate_scale_priv *lq_data,
  778. struct iwl_scale_tbl_info *tbl, int index)
  779. {
  780. int rc = -1;
  781. #ifdef CONFIG_IWLWIFI_HT
  782. u16 rate_mask;
  783. s32 rate;
  784. s8 is_green = lq_data->is_green;
  785. if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
  786. return -1;
  787. IWL_DEBUG_HT("LQ: try to switch to MIMO\n");
  788. tbl->lq_type = LQ_MIMO;
  789. rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
  790. &rate_mask);
  791. if (priv->current_assoc_ht.tx_mimo_ps_mode == IWL_MIMO_PS_STATIC)
  792. return -1;
  793. if (!rs_is_both_ant_supp(lq_data->antenna))
  794. return -1;
  795. rc = 0;
  796. tbl->is_dup = lq_data->is_dup;
  797. tbl->action = 0;
  798. if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
  799. tbl->is_fat = 1;
  800. else
  801. tbl->is_fat = 0;
  802. if (tbl->is_fat) {
  803. if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
  804. tbl->is_SGI = 1;
  805. else
  806. tbl->is_SGI = 0;
  807. } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
  808. tbl->is_SGI = 1;
  809. else
  810. tbl->is_SGI = 0;
  811. rs_get_expected_tpt_table(lq_data, tbl);
  812. rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
  813. IWL_DEBUG_HT("LQ: MIMO best rate %d mask %X\n", rate, rate_mask);
  814. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask))
  815. return -1;
  816. rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
  817. IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
  818. tbl->current_rate.rate_n_flags, is_green);
  819. #endif /*CONFIG_IWLWIFI_HT */
  820. return rc;
  821. }
  822. static int rs_switch_to_siso(struct iwl_priv *priv,
  823. struct iwl_rate_scale_priv *lq_data,
  824. struct iwl_scale_tbl_info *tbl, int index)
  825. {
  826. int rc = -1;
  827. #ifdef CONFIG_IWLWIFI_HT
  828. u16 rate_mask;
  829. u8 is_green = lq_data->is_green;
  830. s32 rate;
  831. IWL_DEBUG_HT("LQ: try to switch to SISO\n");
  832. if (!priv->is_ht_enabled || !priv->current_assoc_ht.is_ht)
  833. return -1;
  834. rc = 0;
  835. tbl->is_dup = lq_data->is_dup;
  836. tbl->lq_type = LQ_SISO;
  837. tbl->action = 0;
  838. rs_get_supported_rates(lq_data, NULL, tbl->lq_type,
  839. &rate_mask);
  840. if (priv->current_channel_width == IWL_CHANNEL_WIDTH_40MHZ)
  841. tbl->is_fat = 1;
  842. else
  843. tbl->is_fat = 0;
  844. if (tbl->is_fat) {
  845. if (priv->current_assoc_ht.sgf & HT_SHORT_GI_40MHZ_ONLY)
  846. tbl->is_SGI = 1;
  847. else
  848. tbl->is_SGI = 0;
  849. } else if (priv->current_assoc_ht.sgf & HT_SHORT_GI_20MHZ_ONLY)
  850. tbl->is_SGI = 1;
  851. else
  852. tbl->is_SGI = 0;
  853. if (is_green)
  854. tbl->is_SGI = 0;
  855. rs_get_expected_tpt_table(lq_data, tbl);
  856. rate = rs_get_best_rate(priv, lq_data, tbl, rate_mask, index, index);
  857. IWL_DEBUG_HT("LQ: get best rate %d mask %X\n", rate, rate_mask);
  858. if ((rate == IWL_RATE_INVALID) || !((1 << rate) & rate_mask)) {
  859. IWL_DEBUG_HT("can not switch with index %d rate mask %x\n",
  860. rate, rate_mask);
  861. return -1;
  862. }
  863. rs_mcs_from_tbl(&tbl->current_rate, tbl, rate, is_green);
  864. IWL_DEBUG_HT("LQ: Switch to new mcs %X index is green %X\n",
  865. tbl->current_rate.rate_n_flags, is_green);
  866. #endif /*CONFIG_IWLWIFI_HT */
  867. return rc;
  868. }
  869. static int rs_move_legacy_other(struct iwl_priv *priv,
  870. struct iwl_rate_scale_priv *lq_data,
  871. int index)
  872. {
  873. int rc = 0;
  874. struct iwl_scale_tbl_info *tbl =
  875. &(lq_data->lq_info[lq_data->active_tbl]);
  876. struct iwl_scale_tbl_info *search_tbl =
  877. &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
  878. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  879. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  880. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  881. u8 start_action = tbl->action;
  882. for (; ;) {
  883. switch (tbl->action) {
  884. case IWL_LEGACY_SWITCH_ANTENNA:
  885. IWL_DEBUG_HT("LQ Legacy switch Antenna\n");
  886. search_tbl->lq_type = LQ_NONE;
  887. lq_data->action_counter++;
  888. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  889. break;
  890. if (!rs_is_other_ant_connected(lq_data->antenna,
  891. tbl->antenna_type))
  892. break;
  893. memcpy(search_tbl, tbl, sz);
  894. rs_toggle_antenna(&(search_tbl->current_rate),
  895. search_tbl);
  896. rs_get_expected_tpt_table(lq_data, search_tbl);
  897. lq_data->search_better_tbl = 1;
  898. goto out;
  899. case IWL_LEGACY_SWITCH_SISO:
  900. IWL_DEBUG_HT("LQ: Legacy switch to SISO\n");
  901. memcpy(search_tbl, tbl, sz);
  902. search_tbl->lq_type = LQ_SISO;
  903. search_tbl->is_SGI = 0;
  904. search_tbl->is_fat = 0;
  905. rc = rs_switch_to_siso(priv, lq_data, search_tbl,
  906. index);
  907. if (!rc) {
  908. lq_data->search_better_tbl = 1;
  909. lq_data->action_counter = 0;
  910. }
  911. if (!rc)
  912. goto out;
  913. break;
  914. case IWL_LEGACY_SWITCH_MIMO:
  915. IWL_DEBUG_HT("LQ: Legacy switch MIMO\n");
  916. memcpy(search_tbl, tbl, sz);
  917. search_tbl->lq_type = LQ_MIMO;
  918. search_tbl->is_SGI = 0;
  919. search_tbl->is_fat = 0;
  920. search_tbl->antenna_type = ANT_BOTH;
  921. rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
  922. index);
  923. if (!rc) {
  924. lq_data->search_better_tbl = 1;
  925. lq_data->action_counter = 0;
  926. }
  927. if (!rc)
  928. goto out;
  929. break;
  930. }
  931. tbl->action++;
  932. if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
  933. tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
  934. if (tbl->action == start_action)
  935. break;
  936. }
  937. return 0;
  938. out:
  939. tbl->action++;
  940. if (tbl->action > IWL_LEGACY_SWITCH_MIMO)
  941. tbl->action = IWL_LEGACY_SWITCH_ANTENNA;
  942. return 0;
  943. }
  944. static int rs_move_siso_to_other(struct iwl_priv *priv,
  945. struct iwl_rate_scale_priv *lq_data,
  946. int index)
  947. {
  948. int rc = -1;
  949. u8 is_green = lq_data->is_green;
  950. struct iwl_scale_tbl_info *tbl =
  951. &(lq_data->lq_info[lq_data->active_tbl]);
  952. struct iwl_scale_tbl_info *search_tbl =
  953. &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
  954. struct iwl_rate_scale_data *window = &(tbl->win[index]);
  955. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  956. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  957. u8 start_action = tbl->action;
  958. for (;;) {
  959. lq_data->action_counter++;
  960. switch (tbl->action) {
  961. case IWL_SISO_SWITCH_ANTENNA:
  962. IWL_DEBUG_HT("LQ: SISO SWITCH ANTENNA SISO\n");
  963. search_tbl->lq_type = LQ_NONE;
  964. if (window->success_ratio >= IWL_RS_GOOD_RATIO)
  965. break;
  966. if (!rs_is_other_ant_connected(lq_data->antenna,
  967. tbl->antenna_type))
  968. break;
  969. memcpy(search_tbl, tbl, sz);
  970. search_tbl->action = IWL_SISO_SWITCH_MIMO;
  971. rs_toggle_antenna(&(search_tbl->current_rate),
  972. search_tbl);
  973. lq_data->search_better_tbl = 1;
  974. goto out;
  975. case IWL_SISO_SWITCH_MIMO:
  976. IWL_DEBUG_HT("LQ: SISO SWITCH TO MIMO FROM SISO\n");
  977. memcpy(search_tbl, tbl, sz);
  978. search_tbl->lq_type = LQ_MIMO;
  979. search_tbl->is_SGI = 0;
  980. search_tbl->is_fat = 0;
  981. search_tbl->antenna_type = ANT_BOTH;
  982. rc = rs_switch_to_mimo(priv, lq_data, search_tbl,
  983. index);
  984. if (!rc)
  985. lq_data->search_better_tbl = 1;
  986. if (!rc)
  987. goto out;
  988. break;
  989. case IWL_SISO_SWITCH_GI:
  990. IWL_DEBUG_HT("LQ: SISO SWITCH TO GI\n");
  991. memcpy(search_tbl, tbl, sz);
  992. search_tbl->action = 0;
  993. if (search_tbl->is_SGI)
  994. search_tbl->is_SGI = 0;
  995. else if (!is_green)
  996. search_tbl->is_SGI = 1;
  997. else
  998. break;
  999. lq_data->search_better_tbl = 1;
  1000. if ((tbl->lq_type == LQ_SISO) &&
  1001. (tbl->is_SGI)) {
  1002. s32 tpt = lq_data->last_tpt / 100;
  1003. if (((!tbl->is_fat) &&
  1004. (tpt >= expected_tpt_siso20MHz[index])) ||
  1005. ((tbl->is_fat) &&
  1006. (tpt >= expected_tpt_siso40MHz[index])))
  1007. lq_data->search_better_tbl = 0;
  1008. }
  1009. rs_get_expected_tpt_table(lq_data, search_tbl);
  1010. rs_mcs_from_tbl(&search_tbl->current_rate,
  1011. search_tbl, index, is_green);
  1012. goto out;
  1013. }
  1014. tbl->action++;
  1015. if (tbl->action > IWL_SISO_SWITCH_GI)
  1016. tbl->action = IWL_SISO_SWITCH_ANTENNA;
  1017. if (tbl->action == start_action)
  1018. break;
  1019. }
  1020. return 0;
  1021. out:
  1022. tbl->action++;
  1023. if (tbl->action > IWL_SISO_SWITCH_GI)
  1024. tbl->action = IWL_SISO_SWITCH_ANTENNA;
  1025. return 0;
  1026. }
  1027. static int rs_move_mimo_to_other(struct iwl_priv *priv,
  1028. struct iwl_rate_scale_priv *lq_data,
  1029. int index)
  1030. {
  1031. int rc = -1;
  1032. s8 is_green = lq_data->is_green;
  1033. struct iwl_scale_tbl_info *tbl =
  1034. &(lq_data->lq_info[lq_data->active_tbl]);
  1035. struct iwl_scale_tbl_info *search_tbl =
  1036. &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
  1037. u32 sz = (sizeof(struct iwl_scale_tbl_info) -
  1038. (sizeof(struct iwl_rate_scale_data) * IWL_RATE_COUNT));
  1039. u8 start_action = tbl->action;
  1040. for (;;) {
  1041. lq_data->action_counter++;
  1042. switch (tbl->action) {
  1043. case IWL_MIMO_SWITCH_ANTENNA_A:
  1044. case IWL_MIMO_SWITCH_ANTENNA_B:
  1045. IWL_DEBUG_HT("LQ: MIMO SWITCH TO SISO\n");
  1046. memcpy(search_tbl, tbl, sz);
  1047. search_tbl->lq_type = LQ_SISO;
  1048. search_tbl->is_SGI = 0;
  1049. search_tbl->is_fat = 0;
  1050. if (tbl->action == IWL_MIMO_SWITCH_ANTENNA_A)
  1051. search_tbl->antenna_type = ANT_MAIN;
  1052. else
  1053. search_tbl->antenna_type = ANT_AUX;
  1054. rc = rs_switch_to_siso(priv, lq_data, search_tbl,
  1055. index);
  1056. if (!rc) {
  1057. lq_data->search_better_tbl = 1;
  1058. goto out;
  1059. }
  1060. break;
  1061. case IWL_MIMO_SWITCH_GI:
  1062. IWL_DEBUG_HT("LQ: MIMO SWITCH TO GI\n");
  1063. memcpy(search_tbl, tbl, sz);
  1064. search_tbl->lq_type = LQ_MIMO;
  1065. search_tbl->antenna_type = ANT_BOTH;
  1066. search_tbl->action = 0;
  1067. if (search_tbl->is_SGI)
  1068. search_tbl->is_SGI = 0;
  1069. else
  1070. search_tbl->is_SGI = 1;
  1071. lq_data->search_better_tbl = 1;
  1072. if ((tbl->lq_type == LQ_MIMO) &&
  1073. (tbl->is_SGI)) {
  1074. s32 tpt = lq_data->last_tpt / 100;
  1075. if (((!tbl->is_fat) &&
  1076. (tpt >= expected_tpt_mimo20MHz[index])) ||
  1077. ((tbl->is_fat) &&
  1078. (tpt >= expected_tpt_mimo40MHz[index])))
  1079. lq_data->search_better_tbl = 0;
  1080. }
  1081. rs_get_expected_tpt_table(lq_data, search_tbl);
  1082. rs_mcs_from_tbl(&search_tbl->current_rate,
  1083. search_tbl, index, is_green);
  1084. goto out;
  1085. }
  1086. tbl->action++;
  1087. if (tbl->action > IWL_MIMO_SWITCH_GI)
  1088. tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
  1089. if (tbl->action == start_action)
  1090. break;
  1091. }
  1092. return 0;
  1093. out:
  1094. tbl->action++;
  1095. if (tbl->action > IWL_MIMO_SWITCH_GI)
  1096. tbl->action = IWL_MIMO_SWITCH_ANTENNA_A;
  1097. return 0;
  1098. }
  1099. static void rs_stay_in_table(struct iwl_rate_scale_priv *lq_data)
  1100. {
  1101. struct iwl_scale_tbl_info *tbl;
  1102. int i;
  1103. int active_tbl;
  1104. int flush_interval_passed = 0;
  1105. active_tbl = lq_data->active_tbl;
  1106. tbl = &(lq_data->lq_info[active_tbl]);
  1107. if (lq_data->stay_in_tbl) {
  1108. if (lq_data->flush_timer)
  1109. flush_interval_passed =
  1110. time_after(jiffies,
  1111. (unsigned long)(lq_data->flush_timer +
  1112. IWL_RATE_SCALE_FLUSH_INTVL));
  1113. flush_interval_passed = 0;
  1114. if ((lq_data->total_failed > lq_data->max_failure_limit) ||
  1115. (lq_data->total_success > lq_data->max_success_limit) ||
  1116. ((!lq_data->search_better_tbl) && (lq_data->flush_timer)
  1117. && (flush_interval_passed))) {
  1118. IWL_DEBUG_HT("LQ: stay is expired %d %d %d\n:",
  1119. lq_data->total_failed,
  1120. lq_data->total_success,
  1121. flush_interval_passed);
  1122. lq_data->stay_in_tbl = 0;
  1123. lq_data->total_failed = 0;
  1124. lq_data->total_success = 0;
  1125. lq_data->flush_timer = 0;
  1126. } else if (lq_data->table_count > 0) {
  1127. lq_data->table_count++;
  1128. if (lq_data->table_count >=
  1129. lq_data->table_count_limit) {
  1130. lq_data->table_count = 0;
  1131. IWL_DEBUG_HT("LQ: stay in table clear win\n");
  1132. for (i = 0; i < IWL_RATE_COUNT; i++)
  1133. rs_rate_scale_clear_window(
  1134. &(tbl->win[i]));
  1135. }
  1136. }
  1137. if (!lq_data->stay_in_tbl) {
  1138. for (i = 0; i < IWL_RATE_COUNT; i++)
  1139. rs_rate_scale_clear_window(&(tbl->win[i]));
  1140. }
  1141. }
  1142. }
  1143. static void rs_rate_scale_perform(struct iwl_priv *priv,
  1144. struct net_device *dev,
  1145. struct ieee80211_hdr *hdr,
  1146. struct sta_info *sta)
  1147. {
  1148. int low = IWL_RATE_INVALID;
  1149. int high = IWL_RATE_INVALID;
  1150. int index;
  1151. int i;
  1152. struct iwl_rate_scale_data *window = NULL;
  1153. int current_tpt = IWL_INVALID_VALUE;
  1154. int low_tpt = IWL_INVALID_VALUE;
  1155. int high_tpt = IWL_INVALID_VALUE;
  1156. u32 fail_count;
  1157. s8 scale_action = 0;
  1158. u16 fc, rate_mask;
  1159. u8 update_lq = 0;
  1160. struct iwl_rate_scale_priv *lq_data;
  1161. struct iwl_scale_tbl_info *tbl, *tbl1;
  1162. u16 rate_scale_index_msk = 0;
  1163. struct iwl_rate mcs_rate;
  1164. u8 is_green = 0;
  1165. u8 active_tbl = 0;
  1166. u8 done_search = 0;
  1167. u16 high_low;
  1168. IWL_DEBUG_RATE("rate scale calculate new rate for skb\n");
  1169. fc = le16_to_cpu(hdr->frame_control);
  1170. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
  1171. /* Send management frames and broadcast/multicast data using
  1172. * lowest rate. */
  1173. /* TODO: this could probably be improved.. */
  1174. return;
  1175. }
  1176. if (!sta || !sta->rate_ctrl_priv)
  1177. return;
  1178. if (!priv->lq_mngr.lq_ready) {
  1179. IWL_DEBUG_RATE("still rate scaling not ready\n");
  1180. return;
  1181. }
  1182. lq_data = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
  1183. if (!lq_data->search_better_tbl)
  1184. active_tbl = lq_data->active_tbl;
  1185. else
  1186. active_tbl = 1 - lq_data->active_tbl;
  1187. tbl = &(lq_data->lq_info[active_tbl]);
  1188. is_green = lq_data->is_green;
  1189. index = sta->last_txrate;
  1190. IWL_DEBUG_RATE("Rate scale index %d for type %d\n", index,
  1191. tbl->lq_type);
  1192. rs_get_supported_rates(lq_data, hdr, tbl->lq_type,
  1193. &rate_mask);
  1194. IWL_DEBUG_RATE("mask 0x%04X \n", rate_mask);
  1195. /* mask with station rate restriction */
  1196. if (is_legacy(tbl->lq_type)) {
  1197. if (lq_data->phymode == (u8) MODE_IEEE80211A)
  1198. rate_scale_index_msk = (u16) (rate_mask &
  1199. (lq_data->supp_rates << IWL_FIRST_OFDM_RATE));
  1200. else
  1201. rate_scale_index_msk = (u16) (rate_mask &
  1202. lq_data->supp_rates);
  1203. } else
  1204. rate_scale_index_msk = rate_mask;
  1205. if (!rate_scale_index_msk)
  1206. rate_scale_index_msk = rate_mask;
  1207. if (index < 0 || !((1 << index) & rate_scale_index_msk)) {
  1208. index = IWL_INVALID_VALUE;
  1209. update_lq = 1;
  1210. /* get the lowest availabe rate */
  1211. for (i = 0; i <= IWL_RATE_COUNT; i++) {
  1212. if ((1 << i) & rate_scale_index_msk)
  1213. index = i;
  1214. }
  1215. if (index == IWL_INVALID_VALUE) {
  1216. IWL_WARNING("Can not find a suitable rate\n");
  1217. return;
  1218. }
  1219. }
  1220. if (!tbl->expected_tpt)
  1221. rs_get_expected_tpt_table(lq_data, tbl);
  1222. window = &(tbl->win[index]);
  1223. fail_count = window->counter - window->success_counter;
  1224. if (((fail_count < IWL_RATE_MIN_FAILURE_TH) &&
  1225. (window->success_counter < IWL_RATE_MIN_SUCCESS_TH))
  1226. || (tbl->expected_tpt == NULL)) {
  1227. IWL_DEBUG_RATE("LQ: still below TH succ %d total %d "
  1228. "for index %d\n",
  1229. window->success_counter, window->counter, index);
  1230. window->average_tpt = IWL_INVALID_VALUE;
  1231. rs_stay_in_table(lq_data);
  1232. if (update_lq) {
  1233. rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
  1234. rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
  1235. rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
  1236. }
  1237. goto out;
  1238. } else
  1239. window->average_tpt = ((window->success_ratio *
  1240. tbl->expected_tpt[index] + 64) / 128);
  1241. if (lq_data->search_better_tbl) {
  1242. int success_limit = IWL_RATE_SCALE_SWITCH;
  1243. if ((window->success_ratio > success_limit) ||
  1244. (window->average_tpt > lq_data->last_tpt)) {
  1245. if (!is_legacy(tbl->lq_type)) {
  1246. IWL_DEBUG_HT("LQ: we are switching to HT"
  1247. " rate suc %d current tpt %d"
  1248. " old tpt %d\n",
  1249. window->success_ratio,
  1250. window->average_tpt,
  1251. lq_data->last_tpt);
  1252. lq_data->enable_counter = 1;
  1253. }
  1254. lq_data->active_tbl = active_tbl;
  1255. current_tpt = window->average_tpt;
  1256. } else {
  1257. tbl->lq_type = LQ_NONE;
  1258. active_tbl = lq_data->active_tbl;
  1259. tbl = &(lq_data->lq_info[active_tbl]);
  1260. index = iwl_rate_index_from_plcp(
  1261. tbl->current_rate.rate_n_flags);
  1262. update_lq = 1;
  1263. current_tpt = lq_data->last_tpt;
  1264. IWL_DEBUG_HT("XXY GO BACK TO OLD TABLE\n");
  1265. }
  1266. lq_data->search_better_tbl = 0;
  1267. done_search = 1;
  1268. goto lq_update;
  1269. }
  1270. high_low = rs_get_adjacent_rate(index, rate_scale_index_msk,
  1271. tbl->lq_type);
  1272. low = high_low & 0xff;
  1273. high = (high_low >> 8) & 0xff;
  1274. current_tpt = window->average_tpt;
  1275. if (low != IWL_RATE_INVALID)
  1276. low_tpt = tbl->win[low].average_tpt;
  1277. if (high != IWL_RATE_INVALID)
  1278. high_tpt = tbl->win[high].average_tpt;
  1279. scale_action = 1;
  1280. if ((window->success_ratio <= IWL_RATE_DECREASE_TH) ||
  1281. (current_tpt == 0)) {
  1282. IWL_DEBUG_RATE("decrease rate because of low success_ratio\n");
  1283. scale_action = -1;
  1284. } else if ((low_tpt == IWL_INVALID_VALUE) &&
  1285. (high_tpt == IWL_INVALID_VALUE))
  1286. scale_action = 1;
  1287. else if ((low_tpt != IWL_INVALID_VALUE) &&
  1288. (high_tpt != IWL_INVALID_VALUE) &&
  1289. (low_tpt < current_tpt) &&
  1290. (high_tpt < current_tpt))
  1291. scale_action = 0;
  1292. else {
  1293. if (high_tpt != IWL_INVALID_VALUE) {
  1294. if (high_tpt > current_tpt)
  1295. scale_action = 1;
  1296. else {
  1297. IWL_DEBUG_RATE
  1298. ("decrease rate because of high tpt\n");
  1299. scale_action = -1;
  1300. }
  1301. } else if (low_tpt != IWL_INVALID_VALUE) {
  1302. if (low_tpt > current_tpt) {
  1303. IWL_DEBUG_RATE
  1304. ("decrease rate because of low tpt\n");
  1305. scale_action = -1;
  1306. } else
  1307. scale_action = 1;
  1308. }
  1309. }
  1310. if (scale_action == -1) {
  1311. if ((low != IWL_RATE_INVALID) &&
  1312. ((window->success_ratio > IWL_RATE_HIGH_TH) ||
  1313. (current_tpt > (100 * tbl->expected_tpt[low]))))
  1314. scale_action = 0;
  1315. } else if ((scale_action == 1) &&
  1316. (window->success_ratio < IWL_RATE_INCREASE_TH))
  1317. scale_action = 0;
  1318. switch (scale_action) {
  1319. case -1:
  1320. if (low != IWL_RATE_INVALID) {
  1321. update_lq = 1;
  1322. index = low;
  1323. }
  1324. break;
  1325. case 1:
  1326. if (high != IWL_RATE_INVALID) {
  1327. update_lq = 1;
  1328. index = high;
  1329. }
  1330. break;
  1331. case 0:
  1332. default:
  1333. break;
  1334. }
  1335. IWL_DEBUG_HT("choose rate scale index %d action %d low %d "
  1336. "high %d type %d\n",
  1337. index, scale_action, low, high, tbl->lq_type);
  1338. lq_update:
  1339. if (update_lq) {
  1340. rs_mcs_from_tbl(&mcs_rate, tbl, index, is_green);
  1341. rs_fill_link_cmd(lq_data, &mcs_rate, &lq_data->lq);
  1342. rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
  1343. }
  1344. rs_stay_in_table(lq_data);
  1345. if (!update_lq && !done_search && !lq_data->stay_in_tbl) {
  1346. lq_data->last_tpt = current_tpt;
  1347. if (is_legacy(tbl->lq_type))
  1348. rs_move_legacy_other(priv, lq_data, index);
  1349. else if (is_siso(tbl->lq_type))
  1350. rs_move_siso_to_other(priv, lq_data, index);
  1351. else
  1352. rs_move_mimo_to_other(priv, lq_data, index);
  1353. if (lq_data->search_better_tbl) {
  1354. tbl = &(lq_data->lq_info[(1 - lq_data->active_tbl)]);
  1355. for (i = 0; i < IWL_RATE_COUNT; i++)
  1356. rs_rate_scale_clear_window(&(tbl->win[i]));
  1357. index = iwl_rate_index_from_plcp(
  1358. tbl->current_rate.rate_n_flags);
  1359. IWL_DEBUG_HT("Switch current mcs: %X index: %d\n",
  1360. tbl->current_rate.rate_n_flags, index);
  1361. rs_fill_link_cmd(lq_data, &tbl->current_rate,
  1362. &lq_data->lq);
  1363. rs_send_lq_cmd(priv, &lq_data->lq, CMD_ASYNC);
  1364. }
  1365. tbl1 = &(lq_data->lq_info[lq_data->active_tbl]);
  1366. if (is_legacy(tbl1->lq_type) &&
  1367. #ifdef CONFIG_IWLWIFI_HT
  1368. !priv->current_assoc_ht.is_ht &&
  1369. #endif
  1370. (lq_data->action_counter >= 1)) {
  1371. lq_data->action_counter = 0;
  1372. IWL_DEBUG_HT("LQ: STAY in legacy table\n");
  1373. rs_set_stay_in_table(1, lq_data);
  1374. }
  1375. if (lq_data->enable_counter &&
  1376. (lq_data->action_counter >= IWL_ACTION_LIMIT)) {
  1377. #ifdef CONFIG_IWLWIFI_HT_AGG
  1378. if ((lq_data->last_tpt > TID_AGG_TPT_THREHOLD) &&
  1379. (priv->lq_mngr.agg_ctrl.auto_agg)) {
  1380. priv->lq_mngr.agg_ctrl.tid_retry =
  1381. TID_ALL_SPECIFIED;
  1382. schedule_work(&priv->agg_work);
  1383. }
  1384. #endif /*CONFIG_IWLWIFI_HT_AGG */
  1385. lq_data->action_counter = 0;
  1386. rs_set_stay_in_table(0, lq_data);
  1387. }
  1388. } else {
  1389. if ((!update_lq) && (!done_search) && (!lq_data->flush_timer))
  1390. lq_data->flush_timer = jiffies;
  1391. }
  1392. out:
  1393. rs_mcs_from_tbl(&tbl->current_rate, tbl, index, is_green);
  1394. i = index;
  1395. sta->last_txrate = i;
  1396. /* sta->txrate is an index to A mode rates which start
  1397. * at IWL_FIRST_OFDM_RATE
  1398. */
  1399. if (lq_data->phymode == (u8) MODE_IEEE80211A)
  1400. sta->txrate = i - IWL_FIRST_OFDM_RATE;
  1401. else
  1402. sta->txrate = i;
  1403. return;
  1404. }
  1405. static void rs_initialize_lq(struct iwl_priv *priv,
  1406. struct sta_info *sta)
  1407. {
  1408. int i;
  1409. struct iwl_rate_scale_priv *lq;
  1410. struct iwl_scale_tbl_info *tbl;
  1411. u8 active_tbl = 0;
  1412. int rate_idx;
  1413. u8 use_green = rs_use_green(priv);
  1414. struct iwl_rate mcs_rate;
  1415. if (!sta || !sta->rate_ctrl_priv)
  1416. goto out;
  1417. lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
  1418. i = sta->last_txrate;
  1419. if ((lq->lq.sta_id == 0xff) &&
  1420. (priv->iw_mode == IEEE80211_IF_TYPE_IBSS))
  1421. goto out;
  1422. if (!lq->search_better_tbl)
  1423. active_tbl = lq->active_tbl;
  1424. else
  1425. active_tbl = 1 - lq->active_tbl;
  1426. tbl = &(lq->lq_info[active_tbl]);
  1427. if ((i < 0) || (i >= IWL_RATE_COUNT))
  1428. i = 0;
  1429. mcs_rate.rate_n_flags = iwl_rates[i].plcp ;
  1430. mcs_rate.rate_n_flags |= RATE_MCS_ANT_B_MSK;
  1431. mcs_rate.rate_n_flags &= ~RATE_MCS_ANT_A_MSK;
  1432. if (i >= IWL_FIRST_CCK_RATE && i <= IWL_LAST_CCK_RATE)
  1433. mcs_rate.rate_n_flags |= RATE_MCS_CCK_MSK;
  1434. tbl->antenna_type = ANT_AUX;
  1435. rs_get_tbl_info_from_mcs(&mcs_rate, priv->phymode, tbl, &rate_idx);
  1436. if (!rs_is_ant_connected(priv->valid_antenna, tbl->antenna_type))
  1437. rs_toggle_antenna(&mcs_rate, tbl);
  1438. rs_mcs_from_tbl(&mcs_rate, tbl, rate_idx, use_green);
  1439. tbl->current_rate.rate_n_flags = mcs_rate.rate_n_flags;
  1440. rs_get_expected_tpt_table(lq, tbl);
  1441. rs_fill_link_cmd(lq, &mcs_rate, &lq->lq);
  1442. rs_send_lq_cmd(priv, &lq->lq, CMD_ASYNC);
  1443. out:
  1444. return;
  1445. }
  1446. static struct ieee80211_rate *rs_get_lowest_rate(struct ieee80211_local
  1447. *local)
  1448. {
  1449. struct ieee80211_hw_mode *mode = local->oper_hw_mode;
  1450. int i;
  1451. for (i = 0; i < mode->num_rates; i++) {
  1452. struct ieee80211_rate *rate = &mode->rates[i];
  1453. if (rate->flags & IEEE80211_RATE_SUPPORTED)
  1454. return rate;
  1455. }
  1456. return &mode->rates[0];
  1457. }
  1458. static struct ieee80211_rate *rs_get_rate(void *priv_rate,
  1459. struct net_device *dev,
  1460. struct sk_buff *skb,
  1461. struct rate_control_extra
  1462. *extra)
  1463. {
  1464. int i;
  1465. struct ieee80211_local *local = wdev_priv(dev->ieee80211_ptr);
  1466. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1467. struct sta_info *sta;
  1468. u16 fc;
  1469. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  1470. struct iwl_rate_scale_priv *lq;
  1471. IWL_DEBUG_RATE_LIMIT("rate scale calculate new rate for skb\n");
  1472. memset(extra, 0, sizeof(*extra));
  1473. fc = le16_to_cpu(hdr->frame_control);
  1474. if (!ieee80211_is_data(fc) || is_multicast_ether_addr(hdr->addr1)) {
  1475. /* Send management frames and broadcast/multicast data using
  1476. * lowest rate. */
  1477. /* TODO: this could probably be improved.. */
  1478. return rs_get_lowest_rate(local);
  1479. }
  1480. sta = sta_info_get(local, hdr->addr1);
  1481. if (!sta || !sta->rate_ctrl_priv) {
  1482. if (sta)
  1483. sta_info_put(sta);
  1484. return rs_get_lowest_rate(local);
  1485. }
  1486. lq = (struct iwl_rate_scale_priv *)sta->rate_ctrl_priv;
  1487. i = sta->last_txrate;
  1488. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) && !lq->ibss_sta_added) {
  1489. u8 sta_id = iwl_hw_find_station(priv, hdr->addr1);
  1490. DECLARE_MAC_BUF(mac);
  1491. if (sta_id == IWL_INVALID_STATION) {
  1492. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  1493. print_mac(mac, hdr->addr1));
  1494. sta_id = iwl_add_station(priv,
  1495. hdr->addr1, 0, CMD_ASYNC);
  1496. }
  1497. if ((sta_id != IWL_INVALID_STATION)) {
  1498. lq->lq.sta_id = sta_id;
  1499. lq->lq.rs_table[0].rate_n_flags = 0;
  1500. lq->ibss_sta_added = 1;
  1501. rs_initialize_lq(priv, sta);
  1502. }
  1503. if (!lq->ibss_sta_added)
  1504. goto done;
  1505. }
  1506. done:
  1507. sta_info_put(sta);
  1508. if ((i < 0) || (i > IWL_RATE_COUNT))
  1509. return rs_get_lowest_rate(local);
  1510. return &priv->ieee_rates[i];
  1511. }
  1512. static void *rs_alloc_sta(void *priv, gfp_t gfp)
  1513. {
  1514. struct iwl_rate_scale_priv *crl;
  1515. int i, j;
  1516. IWL_DEBUG_RATE("create station rate scale window\n");
  1517. crl = kzalloc(sizeof(struct iwl_rate_scale_priv), gfp);
  1518. if (crl == NULL)
  1519. return NULL;
  1520. crl->lq.sta_id = 0xff;
  1521. for (j = 0; j < LQ_SIZE; j++)
  1522. for (i = 0; i < IWL_RATE_COUNT; i++)
  1523. rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
  1524. return crl;
  1525. }
  1526. static void rs_rate_init(void *priv_rate, void *priv_sta,
  1527. struct ieee80211_local *local,
  1528. struct sta_info *sta)
  1529. {
  1530. int i, j;
  1531. struct ieee80211_hw_mode *mode = local->oper_hw_mode;
  1532. struct iwl_priv *priv = (struct iwl_priv *)priv_rate;
  1533. struct iwl_rate_scale_priv *crl = priv_sta;
  1534. crl->flush_timer = 0;
  1535. crl->supp_rates = sta->supp_rates;
  1536. sta->txrate = 3;
  1537. for (j = 0; j < LQ_SIZE; j++)
  1538. for (i = 0; i < IWL_RATE_COUNT; i++)
  1539. rs_rate_scale_clear_window(&(crl->lq_info[j].win[i]));
  1540. IWL_DEBUG_RATE("rate scale global init\n");
  1541. /* TODO: what is a good starting rate for STA? About middle? Maybe not
  1542. * the lowest or the highest rate.. Could consider using RSSI from
  1543. * previous packets? Need to have IEEE 802.1X auth succeed immediately
  1544. * after assoc.. */
  1545. crl->ibss_sta_added = 0;
  1546. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1547. u8 sta_id = iwl_hw_find_station(priv, sta->addr);
  1548. DECLARE_MAC_BUF(mac);
  1549. /* for IBSS the call are from tasklet */
  1550. IWL_DEBUG_HT("LQ: ADD station %s\n",
  1551. print_mac(mac, sta->addr));
  1552. if (sta_id == IWL_INVALID_STATION) {
  1553. IWL_DEBUG_RATE("LQ: ADD station %s\n",
  1554. print_mac(mac, sta->addr));
  1555. sta_id = iwl_add_station(priv,
  1556. sta->addr, 0, CMD_ASYNC);
  1557. }
  1558. if ((sta_id != IWL_INVALID_STATION)) {
  1559. crl->lq.sta_id = sta_id;
  1560. crl->lq.rs_table[0].rate_n_flags = 0;
  1561. }
  1562. /* FIXME: this is w/a remove it later */
  1563. priv->assoc_station_added = 1;
  1564. }
  1565. for (i = 0; i < mode->num_rates; i++) {
  1566. if ((sta->supp_rates & BIT(i)) &&
  1567. (mode->rates[i].flags & IEEE80211_RATE_SUPPORTED))
  1568. sta->txrate = i;
  1569. }
  1570. sta->last_txrate = sta->txrate;
  1571. /* For MODE_IEEE80211A mode cck rate are at end
  1572. * rate table
  1573. */
  1574. if (local->hw.conf.phymode == MODE_IEEE80211A)
  1575. sta->last_txrate += IWL_FIRST_OFDM_RATE;
  1576. crl->is_dup = priv->is_dup;
  1577. crl->valid_antenna = priv->valid_antenna;
  1578. crl->antenna = priv->antenna;
  1579. crl->is_green = rs_use_green(priv);
  1580. crl->active_rate = priv->active_rate;
  1581. crl->active_rate &= ~(0x1000);
  1582. crl->active_rate_basic = priv->active_rate_basic;
  1583. crl->phymode = priv->phymode;
  1584. #ifdef CONFIG_IWLWIFI_HT
  1585. crl->active_siso_rate = (priv->current_assoc_ht.supp_rates[0] << 1);
  1586. crl->active_siso_rate |= (priv->current_assoc_ht.supp_rates[0] & 0x1);
  1587. crl->active_siso_rate &= ~((u16)0x2);
  1588. crl->active_siso_rate = crl->active_siso_rate << IWL_FIRST_OFDM_RATE;
  1589. crl->active_mimo_rate = (priv->current_assoc_ht.supp_rates[1] << 1);
  1590. crl->active_mimo_rate |= (priv->current_assoc_ht.supp_rates[1] & 0x1);
  1591. crl->active_mimo_rate &= ~((u16)0x2);
  1592. crl->active_mimo_rate = crl->active_mimo_rate << IWL_FIRST_OFDM_RATE;
  1593. IWL_DEBUG_HT("MIMO RATE 0x%X SISO MASK 0x%X\n", crl->active_siso_rate,
  1594. crl->active_mimo_rate);
  1595. #endif /*CONFIG_IWLWIFI_HT*/
  1596. #ifdef CONFIG_MAC80211_DEBUGFS
  1597. crl->drv = priv;
  1598. #endif
  1599. if (priv->assoc_station_added)
  1600. priv->lq_mngr.lq_ready = 1;
  1601. rs_initialize_lq(priv, sta);
  1602. }
  1603. static void rs_fill_link_cmd(struct iwl_rate_scale_priv *lq_data,
  1604. struct iwl_rate *tx_mcs,
  1605. struct iwl_link_quality_cmd *lq_cmd)
  1606. {
  1607. int index = 0;
  1608. int rate_idx;
  1609. int repeat_rate = 0;
  1610. u8 ant_toggle_count = 0;
  1611. u8 use_ht_possible = 1;
  1612. struct iwl_rate new_rate;
  1613. struct iwl_scale_tbl_info tbl_type = { 0 };
  1614. rs_dbgfs_set_mcs(lq_data, tx_mcs, index);
  1615. rs_get_tbl_info_from_mcs(tx_mcs, lq_data->phymode,
  1616. &tbl_type, &rate_idx);
  1617. if (is_legacy(tbl_type.lq_type)) {
  1618. ant_toggle_count = 1;
  1619. repeat_rate = IWL_NUMBER_TRY;
  1620. } else
  1621. repeat_rate = IWL_HT_NUMBER_TRY;
  1622. lq_cmd->general_params.mimo_delimiter =
  1623. is_mimo(tbl_type.lq_type) ? 1 : 0;
  1624. lq_cmd->rs_table[index].rate_n_flags =
  1625. cpu_to_le32(tx_mcs->rate_n_flags);
  1626. new_rate.rate_n_flags = tx_mcs->rate_n_flags;
  1627. if (is_mimo(tbl_type.lq_type) || (tbl_type.antenna_type == ANT_MAIN))
  1628. lq_cmd->general_params.single_stream_ant_msk = 1;
  1629. else
  1630. lq_cmd->general_params.single_stream_ant_msk = 2;
  1631. index++;
  1632. repeat_rate--;
  1633. while (index < LINK_QUAL_MAX_RETRY_NUM) {
  1634. while (repeat_rate > 0 && (index < LINK_QUAL_MAX_RETRY_NUM)) {
  1635. if (is_legacy(tbl_type.lq_type)) {
  1636. if (ant_toggle_count <
  1637. NUM_TRY_BEFORE_ANTENNA_TOGGLE)
  1638. ant_toggle_count++;
  1639. else {
  1640. rs_toggle_antenna(&new_rate, &tbl_type);
  1641. ant_toggle_count = 1;
  1642. }
  1643. }
  1644. rs_dbgfs_set_mcs(lq_data, &new_rate, index);
  1645. lq_cmd->rs_table[index].rate_n_flags =
  1646. cpu_to_le32(new_rate.rate_n_flags);
  1647. repeat_rate--;
  1648. index++;
  1649. }
  1650. rs_get_tbl_info_from_mcs(&new_rate, lq_data->phymode, &tbl_type,
  1651. &rate_idx);
  1652. if (is_mimo(tbl_type.lq_type))
  1653. lq_cmd->general_params.mimo_delimiter = index;
  1654. rs_get_lower_rate(lq_data, &tbl_type, rate_idx,
  1655. use_ht_possible, &new_rate);
  1656. if (is_legacy(tbl_type.lq_type)) {
  1657. if (ant_toggle_count < NUM_TRY_BEFORE_ANTENNA_TOGGLE)
  1658. ant_toggle_count++;
  1659. else {
  1660. rs_toggle_antenna(&new_rate, &tbl_type);
  1661. ant_toggle_count = 1;
  1662. }
  1663. repeat_rate = IWL_NUMBER_TRY;
  1664. } else
  1665. repeat_rate = IWL_HT_NUMBER_TRY;
  1666. use_ht_possible = 0;
  1667. rs_dbgfs_set_mcs(lq_data, &new_rate, index);
  1668. lq_cmd->rs_table[index].rate_n_flags =
  1669. cpu_to_le32(new_rate.rate_n_flags);
  1670. index++;
  1671. repeat_rate--;
  1672. }
  1673. lq_cmd->general_params.dual_stream_ant_msk = 3;
  1674. lq_cmd->agg_params.agg_dis_start_th = 3;
  1675. lq_cmd->agg_params.agg_time_limit = cpu_to_le16(4000);
  1676. }
  1677. static void *rs_alloc(struct ieee80211_local *local)
  1678. {
  1679. return local->hw.priv;
  1680. }
  1681. /* rate scale requires free function to be implemented */
  1682. static void rs_free(void *priv_rate)
  1683. {
  1684. return;
  1685. }
  1686. static void rs_clear(void *priv_rate)
  1687. {
  1688. struct iwl_priv *priv = (struct iwl_priv *) priv_rate;
  1689. IWL_DEBUG_RATE("enter\n");
  1690. priv->lq_mngr.lq_ready = 0;
  1691. #ifdef CONFIG_IWLWIFI_HT
  1692. #ifdef CONFIG_IWLWIFI_HT_AGG
  1693. if (priv->lq_mngr.agg_ctrl.granted_ba)
  1694. iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);
  1695. #endif /*CONFIG_IWLWIFI_HT_AGG */
  1696. #endif /* CONFIG_IWLWIFI_HT */
  1697. IWL_DEBUG_RATE("leave\n");
  1698. }
  1699. static void rs_free_sta(void *priv, void *priv_sta)
  1700. {
  1701. struct iwl_rate_scale_priv *rs_priv = priv_sta;
  1702. IWL_DEBUG_RATE("enter\n");
  1703. kfree(rs_priv);
  1704. IWL_DEBUG_RATE("leave\n");
  1705. }
  1706. #ifdef CONFIG_MAC80211_DEBUGFS
  1707. static int open_file_generic(struct inode *inode, struct file *file)
  1708. {
  1709. file->private_data = inode->i_private;
  1710. return 0;
  1711. }
  1712. static void rs_dbgfs_set_mcs(struct iwl_rate_scale_priv *rs_priv,
  1713. struct iwl_rate *mcs, int index)
  1714. {
  1715. u32 base_rate;
  1716. if (rs_priv->phymode == (u8) MODE_IEEE80211A)
  1717. base_rate = 0x800D;
  1718. else
  1719. base_rate = 0x820A;
  1720. if (rs_priv->dbg_fixed.rate_n_flags) {
  1721. if (index < 12)
  1722. mcs->rate_n_flags = rs_priv->dbg_fixed.rate_n_flags;
  1723. else
  1724. mcs->rate_n_flags = base_rate;
  1725. IWL_DEBUG_RATE("Fixed rate ON\n");
  1726. return;
  1727. }
  1728. IWL_DEBUG_RATE("Fixed rate OFF\n");
  1729. }
  1730. static ssize_t rs_sta_dbgfs_scale_table_write(struct file *file,
  1731. const char __user *user_buf, size_t count, loff_t *ppos)
  1732. {
  1733. struct iwl_rate_scale_priv *rs_priv = file->private_data;
  1734. char buf[64];
  1735. int buf_size;
  1736. u32 parsed_rate;
  1737. memset(buf, 0, sizeof(buf));
  1738. buf_size = min(count, sizeof(buf) - 1);
  1739. if (copy_from_user(buf, user_buf, buf_size))
  1740. return -EFAULT;
  1741. if (sscanf(buf, "%x", &parsed_rate) == 1)
  1742. rs_priv->dbg_fixed.rate_n_flags = parsed_rate;
  1743. else
  1744. rs_priv->dbg_fixed.rate_n_flags = 0;
  1745. rs_priv->active_rate = 0x0FFF;
  1746. rs_priv->active_siso_rate = 0x1FD0;
  1747. rs_priv->active_mimo_rate = 0x1FD0;
  1748. IWL_DEBUG_RATE("sta_id %d rate 0x%X\n",
  1749. rs_priv->lq.sta_id, rs_priv->dbg_fixed.rate_n_flags);
  1750. if (rs_priv->dbg_fixed.rate_n_flags) {
  1751. rs_fill_link_cmd(rs_priv, &rs_priv->dbg_fixed, &rs_priv->lq);
  1752. rs_send_lq_cmd(rs_priv->drv, &rs_priv->lq, CMD_ASYNC);
  1753. }
  1754. return count;
  1755. }
  1756. static ssize_t rs_sta_dbgfs_scale_table_read(struct file *file,
  1757. char __user *user_buf, size_t count, loff_t *ppos)
  1758. {
  1759. char buff[1024];
  1760. int desc = 0;
  1761. int i = 0;
  1762. struct iwl_rate_scale_priv *rs_priv = file->private_data;
  1763. desc += sprintf(buff+desc, "sta_id %d\n", rs_priv->lq.sta_id);
  1764. desc += sprintf(buff+desc, "failed=%d success=%d rate=0%X\n",
  1765. rs_priv->total_failed, rs_priv->total_success,
  1766. rs_priv->active_rate);
  1767. desc += sprintf(buff+desc, "fixed rate 0x%X\n",
  1768. rs_priv->dbg_fixed.rate_n_flags);
  1769. desc += sprintf(buff+desc, "general:"
  1770. "flags=0x%X mimo-d=%d s-ant0x%x d-ant=0x%x\n",
  1771. rs_priv->lq.general_params.flags,
  1772. rs_priv->lq.general_params.mimo_delimiter,
  1773. rs_priv->lq.general_params.single_stream_ant_msk,
  1774. rs_priv->lq.general_params.dual_stream_ant_msk);
  1775. desc += sprintf(buff+desc, "agg:"
  1776. "time_limit=%d dist_start_th=%d frame_cnt_limit=%d\n",
  1777. le16_to_cpu(rs_priv->lq.agg_params.agg_time_limit),
  1778. rs_priv->lq.agg_params.agg_dis_start_th,
  1779. rs_priv->lq.agg_params.agg_frame_cnt_limit);
  1780. desc += sprintf(buff+desc,
  1781. "Start idx [0]=0x%x [1]=0x%x [2]=0x%x [3]=0x%x\n",
  1782. rs_priv->lq.general_params.start_rate_index[0],
  1783. rs_priv->lq.general_params.start_rate_index[1],
  1784. rs_priv->lq.general_params.start_rate_index[2],
  1785. rs_priv->lq.general_params.start_rate_index[3]);
  1786. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  1787. desc += sprintf(buff+desc, " rate[%d] 0x%X\n",
  1788. i, le32_to_cpu(rs_priv->lq.rs_table[i].rate_n_flags));
  1789. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  1790. }
  1791. static const struct file_operations rs_sta_dbgfs_scale_table_ops = {
  1792. .write = rs_sta_dbgfs_scale_table_write,
  1793. .read = rs_sta_dbgfs_scale_table_read,
  1794. .open = open_file_generic,
  1795. };
  1796. static ssize_t rs_sta_dbgfs_stats_table_read(struct file *file,
  1797. char __user *user_buf, size_t count, loff_t *ppos)
  1798. {
  1799. char buff[1024];
  1800. int desc = 0;
  1801. int i, j;
  1802. struct iwl_rate_scale_priv *rs_priv = file->private_data;
  1803. for (i = 0; i < LQ_SIZE; i++) {
  1804. desc += sprintf(buff+desc, "%s type=%d SGI=%d FAT=%d DUP=%d\n"
  1805. "rate=0x%X\n",
  1806. rs_priv->active_tbl == i?"*":"x",
  1807. rs_priv->lq_info[i].lq_type,
  1808. rs_priv->lq_info[i].is_SGI,
  1809. rs_priv->lq_info[i].is_fat,
  1810. rs_priv->lq_info[i].is_dup,
  1811. rs_priv->lq_info[i].current_rate.rate_n_flags);
  1812. for (j = 0; j < IWL_RATE_COUNT; j++) {
  1813. desc += sprintf(buff+desc,
  1814. "counter=%d success=%d %%=%d\n",
  1815. rs_priv->lq_info[i].win[j].counter,
  1816. rs_priv->lq_info[i].win[j].success_counter,
  1817. rs_priv->lq_info[i].win[j].success_ratio);
  1818. }
  1819. }
  1820. return simple_read_from_buffer(user_buf, count, ppos, buff, desc);
  1821. }
  1822. static const struct file_operations rs_sta_dbgfs_stats_table_ops = {
  1823. .read = rs_sta_dbgfs_stats_table_read,
  1824. .open = open_file_generic,
  1825. };
  1826. static void rs_add_debugfs(void *priv, void *priv_sta,
  1827. struct dentry *dir)
  1828. {
  1829. struct iwl_rate_scale_priv *rs_priv = priv_sta;
  1830. rs_priv->rs_sta_dbgfs_scale_table_file =
  1831. debugfs_create_file("rate_scale_table", 0600, dir,
  1832. rs_priv, &rs_sta_dbgfs_scale_table_ops);
  1833. rs_priv->rs_sta_dbgfs_stats_table_file =
  1834. debugfs_create_file("rate_stats_table", 0600, dir,
  1835. rs_priv, &rs_sta_dbgfs_stats_table_ops);
  1836. }
  1837. static void rs_remove_debugfs(void *priv, void *priv_sta)
  1838. {
  1839. struct iwl_rate_scale_priv *rs_priv = priv_sta;
  1840. debugfs_remove(rs_priv->rs_sta_dbgfs_scale_table_file);
  1841. debugfs_remove(rs_priv->rs_sta_dbgfs_stats_table_file);
  1842. }
  1843. #endif
  1844. static struct rate_control_ops rs_ops = {
  1845. .module = NULL,
  1846. .name = RS_NAME,
  1847. .tx_status = rs_tx_status,
  1848. .get_rate = rs_get_rate,
  1849. .rate_init = rs_rate_init,
  1850. .clear = rs_clear,
  1851. .alloc = rs_alloc,
  1852. .free = rs_free,
  1853. .alloc_sta = rs_alloc_sta,
  1854. .free_sta = rs_free_sta,
  1855. #ifdef CONFIG_MAC80211_DEBUGFS
  1856. .add_sta_debugfs = rs_add_debugfs,
  1857. .remove_sta_debugfs = rs_remove_debugfs,
  1858. #endif
  1859. };
  1860. int iwl_fill_rs_info(struct ieee80211_hw *hw, char *buf, u8 sta_id)
  1861. {
  1862. struct ieee80211_local *local = hw_to_local(hw);
  1863. struct iwl_priv *priv = hw->priv;
  1864. struct iwl_rate_scale_priv *rs_priv;
  1865. struct sta_info *sta;
  1866. int count = 0, i;
  1867. u32 samples = 0, success = 0, good = 0;
  1868. unsigned long now = jiffies;
  1869. u32 max_time = 0;
  1870. u8 lq_type, antenna;
  1871. sta = sta_info_get(local, priv->stations[sta_id].sta.sta.addr);
  1872. if (!sta || !sta->rate_ctrl_priv) {
  1873. if (sta) {
  1874. sta_info_put(sta);
  1875. IWL_DEBUG_RATE("leave - no private rate data!\n");
  1876. } else
  1877. IWL_DEBUG_RATE("leave - no station!\n");
  1878. return sprintf(buf, "station %d not found\n", sta_id);
  1879. }
  1880. rs_priv = (void *)sta->rate_ctrl_priv;
  1881. lq_type = rs_priv->lq_info[rs_priv->active_tbl].lq_type;
  1882. antenna = rs_priv->lq_info[rs_priv->active_tbl].antenna_type;
  1883. if (is_legacy(lq_type))
  1884. i = IWL_RATE_54M_INDEX;
  1885. else
  1886. i = IWL_RATE_60M_INDEX;
  1887. while (1) {
  1888. u64 mask;
  1889. int j;
  1890. int active = rs_priv->active_tbl;
  1891. count +=
  1892. sprintf(&buf[count], " %2dMbs: ", iwl_rates[i].ieee / 2);
  1893. mask = (1ULL << (IWL_RATE_MAX_WINDOW - 1));
  1894. for (j = 0; j < IWL_RATE_MAX_WINDOW; j++, mask >>= 1)
  1895. buf[count++] =
  1896. (rs_priv->lq_info[active].win[i].data & mask)
  1897. ? '1' : '0';
  1898. samples += rs_priv->lq_info[active].win[i].counter;
  1899. good += rs_priv->lq_info[active].win[i].success_counter;
  1900. success += rs_priv->lq_info[active].win[i].success_counter *
  1901. iwl_rates[i].ieee;
  1902. if (rs_priv->lq_info[active].win[i].stamp) {
  1903. int delta =
  1904. jiffies_to_msecs(now -
  1905. rs_priv->lq_info[active].win[i].stamp);
  1906. if (delta > max_time)
  1907. max_time = delta;
  1908. count += sprintf(&buf[count], "%5dms\n", delta);
  1909. } else
  1910. buf[count++] = '\n';
  1911. j = iwl_get_prev_ieee_rate(i);
  1912. if (j == i)
  1913. break;
  1914. i = j;
  1915. }
  1916. /* Display the average rate of all samples taken.
  1917. *
  1918. * NOTE: We multiple # of samples by 2 since the IEEE measurement
  1919. * added from iwl_rates is actually 2X the rate */
  1920. if (samples)
  1921. count += sprintf(&buf[count],
  1922. "\nAverage rate is %3d.%02dMbs over last %4dms\n"
  1923. "%3d%% success (%d good packets over %d tries)\n",
  1924. success / (2 * samples), (success * 5 / samples) % 10,
  1925. max_time, good * 100 / samples, good, samples);
  1926. else
  1927. count += sprintf(&buf[count], "\nAverage rate: 0Mbs\n");
  1928. count += sprintf(&buf[count], "\nrate scale type %d anntena %d "
  1929. "active_search %d rate index %d\n", lq_type, antenna,
  1930. rs_priv->search_better_tbl, sta->last_txrate);
  1931. sta_info_put(sta);
  1932. return count;
  1933. }
  1934. void iwl_rate_scale_init(struct ieee80211_hw *hw, s32 sta_id)
  1935. {
  1936. struct iwl_priv *priv = hw->priv;
  1937. priv->lq_mngr.lq_ready = 1;
  1938. }
  1939. void iwl_rate_control_register(struct ieee80211_hw *hw)
  1940. {
  1941. ieee80211_rate_control_register(&rs_ops);
  1942. }
  1943. void iwl_rate_control_unregister(struct ieee80211_hw *hw)
  1944. {
  1945. ieee80211_rate_control_unregister(&rs_ops);
  1946. }