iwl-4965-rs.c 54 KB

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