iwl-4965-rs.c 56 KB

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