iwl-4965-rs.c 59 KB

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