iwl-4965-rs.c 54 KB

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